[IRDA]: Use put_unaligned() in irlmp_do_discovery().
[safe/jmp/linux-2.6] / net / irda / irlmp.c
1 /*********************************************************************
2  *
3  * Filename:      irlmp.c
4  * Version:       1.0
5  * Description:   IrDA Link Management Protocol (LMP) layer
6  * Status:        Stable.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Aug 17 20:54:32 1997
9  * Modified at:   Wed Jan  5 11:26:03 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  *
12  *     Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
13  *     All Rights Reserved.
14  *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15  *
16  *     This program is free software; you can redistribute it and/or
17  *     modify it under the terms of the GNU General Public License as
18  *     published by the Free Software Foundation; either version 2 of
19  *     the License, or (at your option) any later version.
20  *
21  *     Neither Dag Brattli nor University of Tromsø admit liability nor
22  *     provide warranty for any of this software. This material is
23  *     provided "AS-IS" and at no charge.
24  *
25  ********************************************************************/
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/skbuff.h>
32 #include <linux/types.h>
33 #include <linux/proc_fs.h>
34 #include <linux/init.h>
35 #include <linux/kmod.h>
36 #include <linux/random.h>
37 #include <linux/seq_file.h>
38
39 #include <net/irda/irda.h>
40 #include <net/irda/timer.h>
41 #include <net/irda/qos.h>
42 #include <net/irda/irlap.h>
43 #include <net/irda/iriap.h>
44 #include <net/irda/irlmp.h>
45 #include <net/irda/irlmp_frame.h>
46
47 #include <asm/unaligned.h>
48
49 static __u8 irlmp_find_free_slsap(void);
50 static int irlmp_slsap_inuse(__u8 slsap_sel);
51
52 /* Master structure */
53 struct irlmp_cb *irlmp = NULL;
54
55 /* These can be altered by the sysctl interface */
56 int  sysctl_discovery         = 0;
57 int  sysctl_discovery_timeout = 3; /* 3 seconds by default */
58 int  sysctl_discovery_slots   = 6; /* 6 slots by default */
59 int  sysctl_lap_keepalive_time = LM_IDLE_TIMEOUT * 1000 / HZ;
60 char sysctl_devname[65];
61
62 const char *irlmp_reasons[] = {
63         "ERROR, NOT USED",
64         "LM_USER_REQUEST",
65         "LM_LAP_DISCONNECT",
66         "LM_CONNECT_FAILURE",
67         "LM_LAP_RESET",
68         "LM_INIT_DISCONNECT",
69         "ERROR, NOT USED",
70 };
71
72 /*
73  * Function irlmp_init (void)
74  *
75  *    Create (allocate) the main IrLMP structure
76  *
77  */
78 int __init irlmp_init(void)
79 {
80         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
81         /* Initialize the irlmp structure. */
82         irlmp = kmalloc( sizeof(struct irlmp_cb), GFP_KERNEL);
83         if (irlmp == NULL)
84                 return -ENOMEM;
85         memset(irlmp, 0, sizeof(struct irlmp_cb));
86
87         irlmp->magic = LMP_MAGIC;
88
89         irlmp->clients = hashbin_new(HB_LOCK);
90         irlmp->services = hashbin_new(HB_LOCK);
91         irlmp->links = hashbin_new(HB_LOCK);
92         irlmp->unconnected_lsaps = hashbin_new(HB_LOCK);
93         irlmp->cachelog = hashbin_new(HB_NOLOCK);
94
95         if ((irlmp->clients == NULL) ||
96             (irlmp->services == NULL) ||
97             (irlmp->links == NULL) ||
98             (irlmp->unconnected_lsaps == NULL) ||
99             (irlmp->cachelog == NULL)) {
100                 return -ENOMEM;
101         }
102
103         spin_lock_init(&irlmp->cachelog->hb_spinlock);
104
105         irlmp->last_lsap_sel = 0x0f; /* Reserved 0x00-0x0f */
106         strcpy(sysctl_devname, "Linux");
107
108         /* Do discovery every 3 seconds */
109         init_timer(&irlmp->discovery_timer);
110         irlmp_start_discovery_timer(irlmp, sysctl_discovery_timeout*HZ);
111
112         return 0;
113 }
114
115 /*
116  * Function irlmp_cleanup (void)
117  *
118  *    Remove IrLMP layer
119  *
120  */
121 void __exit irlmp_cleanup(void) 
122 {
123         /* Check for main structure */
124         IRDA_ASSERT(irlmp != NULL, return;);
125         IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return;);
126
127         del_timer(&irlmp->discovery_timer);
128
129         hashbin_delete(irlmp->links, (FREE_FUNC) kfree);
130         hashbin_delete(irlmp->unconnected_lsaps, (FREE_FUNC) kfree);
131         hashbin_delete(irlmp->clients, (FREE_FUNC) kfree);
132         hashbin_delete(irlmp->services, (FREE_FUNC) kfree);
133         hashbin_delete(irlmp->cachelog, (FREE_FUNC) kfree);
134
135         /* De-allocate main structure */
136         kfree(irlmp);
137         irlmp = NULL;
138 }
139
140 /*
141  * Function irlmp_open_lsap (slsap, notify)
142  *
143  *   Register with IrLMP and create a local LSAP,
144  *   returns handle to LSAP.
145  */
146 struct lsap_cb *irlmp_open_lsap(__u8 slsap_sel, notify_t *notify, __u8 pid)
147 {
148         struct lsap_cb *self;
149
150         IRDA_ASSERT(notify != NULL, return NULL;);
151         IRDA_ASSERT(irlmp != NULL, return NULL;);
152         IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return NULL;);
153         IRDA_ASSERT(notify->instance != NULL, return NULL;);
154
155         /*  Does the client care which Source LSAP selector it gets?  */
156         if (slsap_sel == LSAP_ANY) {
157                 slsap_sel = irlmp_find_free_slsap();
158                 if (!slsap_sel)
159                         return NULL;
160         } else if (irlmp_slsap_inuse(slsap_sel))
161                 return NULL;
162
163         /* Allocate new instance of a LSAP connection */
164         self = kmalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
165         if (self == NULL) {
166                 IRDA_ERROR("%s: can't allocate memory\n", __FUNCTION__);
167                 return NULL;
168         }
169         memset(self, 0, sizeof(struct lsap_cb));
170
171         self->magic = LMP_LSAP_MAGIC;
172         self->slsap_sel = slsap_sel;
173
174         /* Fix connectionless LSAP's */
175         if (slsap_sel == LSAP_CONNLESS) {
176 #ifdef CONFIG_IRDA_ULTRA
177                 self->dlsap_sel = LSAP_CONNLESS;
178                 self->pid = pid;
179 #endif /* CONFIG_IRDA_ULTRA */
180         } else
181                 self->dlsap_sel = LSAP_ANY;
182         /* self->connected = FALSE; -> already NULL via memset() */
183
184         init_timer(&self->watchdog_timer);
185
186         self->notify = *notify;
187
188         self->lsap_state = LSAP_DISCONNECTED;
189
190         /* Insert into queue of unconnected LSAPs */
191         hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
192                        (long) self, NULL);
193
194         return self;
195 }
196 EXPORT_SYMBOL(irlmp_open_lsap);
197
198 /*
199  * Function __irlmp_close_lsap (self)
200  *
201  *    Remove an instance of LSAP
202  */
203 static void __irlmp_close_lsap(struct lsap_cb *self)
204 {
205         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
206
207         IRDA_ASSERT(self != NULL, return;);
208         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
209
210         /*
211          *  Set some of the variables to preset values
212          */
213         self->magic = 0;
214         del_timer(&self->watchdog_timer); /* Important! */
215
216         if (self->conn_skb)
217                 dev_kfree_skb(self->conn_skb);
218
219         kfree(self);
220 }
221
222 /*
223  * Function irlmp_close_lsap (self)
224  *
225  *    Close and remove LSAP
226  *
227  */
228 void irlmp_close_lsap(struct lsap_cb *self)
229 {
230         struct lap_cb *lap;
231         struct lsap_cb *lsap = NULL;
232
233         IRDA_ASSERT(self != NULL, return;);
234         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
235
236         /*
237          *  Find out if we should remove this LSAP from a link or from the
238          *  list of unconnected lsaps (not associated with a link)
239          */
240         lap = self->lap;
241         if (lap) {
242                 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
243                 /* We might close a LSAP before it has completed the
244                  * connection setup. In those case, higher layers won't
245                  * send a proper disconnect request. Harmless, except
246                  * that we will forget to close LAP... - Jean II */
247                 if(self->lsap_state != LSAP_DISCONNECTED) {
248                         self->lsap_state = LSAP_DISCONNECTED;
249                         irlmp_do_lap_event(self->lap,
250                                            LM_LAP_DISCONNECT_REQUEST, NULL);
251                 }
252                 /* Now, remove from the link */
253                 lsap = hashbin_remove(lap->lsaps, (long) self, NULL);
254 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
255                 lap->cache.valid = FALSE;
256 #endif
257         }
258         self->lap = NULL;
259         /* Check if we found the LSAP! If not then try the unconnected lsaps */
260         if (!lsap) {
261                 lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self,
262                                       NULL);
263         }
264         if (!lsap) {
265                 IRDA_DEBUG(0,
266                      "%s(), Looks like somebody has removed me already!\n",
267                            __FUNCTION__);
268                 return;
269         }
270         __irlmp_close_lsap(self);
271 }
272 EXPORT_SYMBOL(irlmp_close_lsap);
273
274 /*
275  * Function irlmp_register_irlap (saddr, notify)
276  *
277  *    Register IrLAP layer with IrLMP. There is possible to have multiple
278  *    instances of the IrLAP layer, each connected to different IrDA ports
279  *
280  */
281 void irlmp_register_link(struct irlap_cb *irlap, __u32 saddr, notify_t *notify)
282 {
283         struct lap_cb *lap;
284
285         IRDA_ASSERT(irlmp != NULL, return;);
286         IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return;);
287         IRDA_ASSERT(notify != NULL, return;);
288
289         /*
290          *  Allocate new instance of a LSAP connection
291          */
292         lap = kmalloc(sizeof(struct lap_cb), GFP_KERNEL);
293         if (lap == NULL) {
294                 IRDA_ERROR("%s: unable to kmalloc\n", __FUNCTION__);
295                 return;
296         }
297         memset(lap, 0, sizeof(struct lap_cb));
298
299         lap->irlap = irlap;
300         lap->magic = LMP_LAP_MAGIC;
301         lap->saddr = saddr;
302         lap->daddr = DEV_ADDR_ANY;
303 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
304         lap->cache.valid = FALSE;
305 #endif
306         lap->lsaps = hashbin_new(HB_LOCK);
307         if (lap->lsaps == NULL) {
308                 IRDA_WARNING("%s(), unable to kmalloc lsaps\n", __FUNCTION__);
309                 kfree(lap);
310                 return;
311         }
312
313         lap->lap_state = LAP_STANDBY;
314
315         init_timer(&lap->idle_timer);
316
317         /*
318          *  Insert into queue of LMP links
319          */
320         hashbin_insert(irlmp->links, (irda_queue_t *) lap, lap->saddr, NULL);
321
322         /*
323          *  We set only this variable so IrLAP can tell us on which link the
324          *  different events happened on
325          */
326         irda_notify_init(notify);
327         notify->instance = lap;
328 }
329
330 /*
331  * Function irlmp_unregister_irlap (saddr)
332  *
333  *    IrLAP layer has been removed!
334  *
335  */
336 void irlmp_unregister_link(__u32 saddr)
337 {
338         struct lap_cb *link;
339
340         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
341
342         /* We must remove ourselves from the hashbin *first*. This ensure
343          * that no more LSAPs will be open on this link and no discovery
344          * will be triggered anymore. Jean II */
345         link = hashbin_remove(irlmp->links, saddr, NULL);
346         if (link) {
347                 IRDA_ASSERT(link->magic == LMP_LAP_MAGIC, return;);
348
349                 /* Kill all the LSAPs on this link. Jean II */
350                 link->reason = LAP_DISC_INDICATION;
351                 link->daddr = DEV_ADDR_ANY;
352                 irlmp_do_lap_event(link, LM_LAP_DISCONNECT_INDICATION, NULL);
353
354                 /* Remove all discoveries discovered at this link */
355                 irlmp_expire_discoveries(irlmp->cachelog, link->saddr, TRUE);
356
357                 /* Final cleanup */
358                 del_timer(&link->idle_timer);
359                 link->magic = 0;
360                 kfree(link);
361         }
362 }
363
364 /*
365  * Function irlmp_connect_request (handle, dlsap, userdata)
366  *
367  *    Connect with a peer LSAP
368  *
369  */
370 int irlmp_connect_request(struct lsap_cb *self, __u8 dlsap_sel,
371                           __u32 saddr, __u32 daddr,
372                           struct qos_info *qos, struct sk_buff *userdata)
373 {
374         struct sk_buff *tx_skb = userdata;
375         struct lap_cb *lap;
376         struct lsap_cb *lsap;
377         int ret;
378
379         IRDA_ASSERT(self != NULL, return -EBADR;);
380         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -EBADR;);
381
382         IRDA_DEBUG(2,
383               "%s(), slsap_sel=%02x, dlsap_sel=%02x, saddr=%08x, daddr=%08x\n",
384               __FUNCTION__, self->slsap_sel, dlsap_sel, saddr, daddr);
385
386         if (test_bit(0, &self->connected)) {
387                 ret = -EISCONN;
388                 goto err;
389         }
390
391         /* Client must supply destination device address */
392         if (!daddr) {
393                 ret = -EINVAL;
394                 goto err;
395         }
396
397         /* Any userdata? */
398         if (tx_skb == NULL) {
399                 tx_skb = dev_alloc_skb(64);
400                 if (!tx_skb)
401                         return -ENOMEM;
402
403                 skb_reserve(tx_skb, LMP_MAX_HEADER);
404         }
405
406         /* Make room for MUX control header (3 bytes) */
407         IRDA_ASSERT(skb_headroom(tx_skb) >= LMP_CONTROL_HEADER, return -1;);
408         skb_push(tx_skb, LMP_CONTROL_HEADER);
409
410         self->dlsap_sel = dlsap_sel;
411
412         /*
413          * Find the link to where we should try to connect since there may
414          * be more than one IrDA port on this machine. If the client has
415          * passed us the saddr (and already knows which link to use), then
416          * we use that to find the link, if not then we have to look in the
417          * discovery log and check if any of the links has discovered a
418          * device with the given daddr
419          */
420         if ((!saddr) || (saddr == DEV_ADDR_ANY)) {
421                 discovery_t *discovery;
422                 unsigned long flags;
423
424                 spin_lock_irqsave(&irlmp->cachelog->hb_spinlock, flags);
425                 if (daddr != DEV_ADDR_ANY)
426                         discovery = hashbin_find(irlmp->cachelog, daddr, NULL);
427                 else {
428                         IRDA_DEBUG(2, "%s(), no daddr\n", __FUNCTION__);
429                         discovery = (discovery_t *)
430                                 hashbin_get_first(irlmp->cachelog);
431                 }
432
433                 if (discovery) {
434                         saddr = discovery->data.saddr;
435                         daddr = discovery->data.daddr;
436                 }
437                 spin_unlock_irqrestore(&irlmp->cachelog->hb_spinlock, flags);
438         }
439         lap = hashbin_lock_find(irlmp->links, saddr, NULL);
440         if (lap == NULL) {
441                 IRDA_DEBUG(1, "%s(), Unable to find a usable link!\n", __FUNCTION__);
442                 ret = -EHOSTUNREACH;
443                 goto err;
444         }
445
446         /* Check if LAP is disconnected or already connected */
447         if (lap->daddr == DEV_ADDR_ANY)
448                 lap->daddr = daddr;
449         else if (lap->daddr != daddr) {
450                 /* Check if some LSAPs are active on this LAP */
451                 if (HASHBIN_GET_SIZE(lap->lsaps) == 0) {
452                         /* No active connection, but LAP hasn't been
453                          * disconnected yet (waiting for timeout in LAP).
454                          * Maybe we could give LAP a bit of help in this case.
455                          */
456                         IRDA_DEBUG(0, "%s(), sorry, but I'm waiting for LAP to timeout!\n", __FUNCTION__);
457                         ret = -EAGAIN;
458                         goto err;
459                 }
460
461                 /* LAP is already connected to a different node, and LAP
462                  * can only talk to one node at a time */
463                 IRDA_DEBUG(0, "%s(), sorry, but link is busy!\n", __FUNCTION__);
464                 ret = -EBUSY;
465                 goto err;
466         }
467
468         self->lap = lap;
469
470         /*
471          *  Remove LSAP from list of unconnected LSAPs and insert it into the
472          *  list of connected LSAPs for the particular link
473          */
474         lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self, NULL);
475
476         IRDA_ASSERT(lsap != NULL, return -1;);
477         IRDA_ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
478         IRDA_ASSERT(lsap->lap != NULL, return -1;);
479         IRDA_ASSERT(lsap->lap->magic == LMP_LAP_MAGIC, return -1;);
480
481         hashbin_insert(self->lap->lsaps, (irda_queue_t *) self, (long) self,
482                        NULL);
483
484         set_bit(0, &self->connected);   /* TRUE */
485
486         /*
487          *  User supplied qos specifications?
488          */
489         if (qos)
490                 self->qos = *qos;
491
492         irlmp_do_lsap_event(self, LM_CONNECT_REQUEST, tx_skb);
493
494         /* Drop reference count - see irlap_data_request(). */
495         dev_kfree_skb(tx_skb);
496
497         return 0;
498
499 err:
500         /* Cleanup */
501         if(tx_skb)
502                 dev_kfree_skb(tx_skb);
503         return ret;
504 }
505 EXPORT_SYMBOL(irlmp_connect_request);
506
507 /*
508  * Function irlmp_connect_indication (self)
509  *
510  *    Incoming connection
511  *
512  */
513 void irlmp_connect_indication(struct lsap_cb *self, struct sk_buff *skb)
514 {
515         int max_seg_size;
516         int lap_header_size;
517         int max_header_size;
518
519         IRDA_ASSERT(self != NULL, return;);
520         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
521         IRDA_ASSERT(skb != NULL, return;);
522         IRDA_ASSERT(self->lap != NULL, return;);
523
524         IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
525                    __FUNCTION__, self->slsap_sel, self->dlsap_sel);
526
527         /* Note : self->lap is set in irlmp_link_data_indication(),
528          * (case CONNECT_CMD:) because we have no way to set it here.
529          * Similarly, self->dlsap_sel is usually set in irlmp_find_lsap().
530          * Jean II */
531
532         self->qos = *self->lap->qos;
533
534         max_seg_size = self->lap->qos->data_size.value-LMP_HEADER;
535         lap_header_size = IRLAP_GET_HEADER_SIZE(self->lap->irlap);
536         max_header_size = LMP_HEADER + lap_header_size;
537
538         /* Hide LMP_CONTROL_HEADER header from layer above */
539         skb_pull(skb, LMP_CONTROL_HEADER);
540
541         if (self->notify.connect_indication) {
542                 /* Don't forget to refcount it - see irlap_driver_rcv(). */
543                 skb_get(skb);
544                 self->notify.connect_indication(self->notify.instance, self,
545                                                 &self->qos, max_seg_size,
546                                                 max_header_size, skb);
547         }
548 }
549
550 /*
551  * Function irlmp_connect_response (handle, userdata)
552  *
553  *    Service user is accepting connection
554  *
555  */
556 int irlmp_connect_response(struct lsap_cb *self, struct sk_buff *userdata)
557 {
558         IRDA_ASSERT(self != NULL, return -1;);
559         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
560         IRDA_ASSERT(userdata != NULL, return -1;);
561
562         /* We set the connected bit and move the lsap to the connected list
563          * in the state machine itself. Jean II */
564
565         IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
566                    __FUNCTION__, self->slsap_sel, self->dlsap_sel);
567
568         /* Make room for MUX control header (3 bytes) */
569         IRDA_ASSERT(skb_headroom(userdata) >= LMP_CONTROL_HEADER, return -1;);
570         skb_push(userdata, LMP_CONTROL_HEADER);
571
572         irlmp_do_lsap_event(self, LM_CONNECT_RESPONSE, userdata);
573
574         /* Drop reference count - see irlap_data_request(). */
575         dev_kfree_skb(userdata);
576
577         return 0;
578 }
579 EXPORT_SYMBOL(irlmp_connect_response);
580
581 /*
582  * Function irlmp_connect_confirm (handle, skb)
583  *
584  *    LSAP connection confirmed peer device!
585  */
586 void irlmp_connect_confirm(struct lsap_cb *self, struct sk_buff *skb)
587 {
588         int max_header_size;
589         int lap_header_size;
590         int max_seg_size;
591
592         IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
593
594         IRDA_ASSERT(skb != NULL, return;);
595         IRDA_ASSERT(self != NULL, return;);
596         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
597         IRDA_ASSERT(self->lap != NULL, return;);
598
599         self->qos = *self->lap->qos;
600
601         max_seg_size    = self->lap->qos->data_size.value-LMP_HEADER;
602         lap_header_size = IRLAP_GET_HEADER_SIZE(self->lap->irlap);
603         max_header_size = LMP_HEADER + lap_header_size;
604
605         IRDA_DEBUG(2, "%s(), max_header_size=%d\n",
606                    __FUNCTION__, max_header_size);
607
608         /* Hide LMP_CONTROL_HEADER header from layer above */
609         skb_pull(skb, LMP_CONTROL_HEADER);
610
611         if (self->notify.connect_confirm) {
612                 /* Don't forget to refcount it - see irlap_driver_rcv() */
613                 skb_get(skb);
614                 self->notify.connect_confirm(self->notify.instance, self,
615                                              &self->qos, max_seg_size,
616                                              max_header_size, skb);
617         }
618 }
619
620 /*
621  * Function irlmp_dup (orig, instance)
622  *
623  *    Duplicate LSAP, can be used by servers to confirm a connection on a
624  *    new LSAP so it can keep listening on the old one.
625  *
626  */
627 struct lsap_cb *irlmp_dup(struct lsap_cb *orig, void *instance)
628 {
629         struct lsap_cb *new;
630         unsigned long flags;
631
632         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
633
634         spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
635
636         /* Only allowed to duplicate unconnected LSAP's, and only LSAPs
637          * that have received a connect indication. Jean II */
638         if ((!hashbin_find(irlmp->unconnected_lsaps, (long) orig, NULL)) ||
639             (orig->lap == NULL)) {
640                 IRDA_DEBUG(0, "%s(), invalid LSAP (wrong state)\n",
641                            __FUNCTION__);
642                 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
643                                        flags);
644                 return NULL;
645         }
646
647         /* Allocate a new instance */
648         new = kmalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
649         if (!new)  {
650                 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
651                 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
652                                        flags);
653                 return NULL;
654         }
655         /* Dup */
656         memcpy(new, orig, sizeof(struct lsap_cb));
657         /* new->lap = orig->lap; => done in the memcpy() */
658         /* new->slsap_sel = orig->slsap_sel; => done in the memcpy() */
659         new->conn_skb = NULL;
660
661         spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
662
663         /* Not everything is the same */
664         new->notify.instance = instance;
665
666         init_timer(&new->watchdog_timer);
667
668         hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) new,
669                        (long) new, NULL);
670
671 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
672         /* Make sure that we invalidate the LSAP cache */
673         new->lap->cache.valid = FALSE;
674 #endif /* CONFIG_IRDA_CACHE_LAST_LSAP */
675
676         return new;
677 }
678
679 /*
680  * Function irlmp_disconnect_request (handle, userdata)
681  *
682  *    The service user is requesting disconnection, this will not remove the
683  *    LSAP, but only mark it as disconnected
684  */
685 int irlmp_disconnect_request(struct lsap_cb *self, struct sk_buff *userdata)
686 {
687         struct lsap_cb *lsap;
688
689         IRDA_ASSERT(self != NULL, return -1;);
690         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
691         IRDA_ASSERT(userdata != NULL, return -1;);
692
693         /* Already disconnected ?
694          * There is a race condition between irlmp_disconnect_indication()
695          * and us that might mess up the hashbins below. This fixes it.
696          * Jean II */
697         if (! test_and_clear_bit(0, &self->connected)) {
698                 IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
699                 dev_kfree_skb(userdata);
700                 return -1;
701         }
702
703         skb_push(userdata, LMP_CONTROL_HEADER);
704
705         /*
706          *  Do the event before the other stuff since we must know
707          *  which lap layer that the frame should be transmitted on
708          */
709         irlmp_do_lsap_event(self, LM_DISCONNECT_REQUEST, userdata);
710
711         /* Drop reference count - see irlap_data_request(). */
712         dev_kfree_skb(userdata);
713
714         /*
715          *  Remove LSAP from list of connected LSAPs for the particular link
716          *  and insert it into the list of unconnected LSAPs
717          */
718         IRDA_ASSERT(self->lap != NULL, return -1;);
719         IRDA_ASSERT(self->lap->magic == LMP_LAP_MAGIC, return -1;);
720         IRDA_ASSERT(self->lap->lsaps != NULL, return -1;);
721
722         lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
723 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
724         self->lap->cache.valid = FALSE;
725 #endif
726
727         IRDA_ASSERT(lsap != NULL, return -1;);
728         IRDA_ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
729         IRDA_ASSERT(lsap == self, return -1;);
730
731         hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
732                        (long) self, NULL);
733
734         /* Reset some values */
735         self->dlsap_sel = LSAP_ANY;
736         self->lap = NULL;
737
738         return 0;
739 }
740 EXPORT_SYMBOL(irlmp_disconnect_request);
741
742 /*
743  * Function irlmp_disconnect_indication (reason, userdata)
744  *
745  *    LSAP is being closed!
746  */
747 void irlmp_disconnect_indication(struct lsap_cb *self, LM_REASON reason,
748                                  struct sk_buff *skb)
749 {
750         struct lsap_cb *lsap;
751
752         IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, irlmp_reasons[reason]);
753         IRDA_ASSERT(self != NULL, return;);
754         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
755
756         IRDA_DEBUG(3, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
757                    __FUNCTION__, self->slsap_sel, self->dlsap_sel);
758
759         /* Already disconnected ?
760          * There is a race condition between irlmp_disconnect_request()
761          * and us that might mess up the hashbins below. This fixes it.
762          * Jean II */
763         if (! test_and_clear_bit(0, &self->connected)) {
764                 IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
765                 return;
766         }
767
768         /*
769          *  Remove association between this LSAP and the link it used
770          */
771         IRDA_ASSERT(self->lap != NULL, return;);
772         IRDA_ASSERT(self->lap->lsaps != NULL, return;);
773
774         lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
775 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
776         self->lap->cache.valid = FALSE;
777 #endif
778
779         IRDA_ASSERT(lsap != NULL, return;);
780         IRDA_ASSERT(lsap == self, return;);
781         hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) lsap,
782                        (long) lsap, NULL);
783
784         self->dlsap_sel = LSAP_ANY;
785         self->lap = NULL;
786
787         /*
788          *  Inform service user
789          */
790         if (self->notify.disconnect_indication) {
791                 /* Don't forget to refcount it - see irlap_driver_rcv(). */
792                 if(skb)
793                         skb_get(skb);
794                 self->notify.disconnect_indication(self->notify.instance,
795                                                    self, reason, skb);
796         } else {
797                 IRDA_DEBUG(0, "%s(), no handler\n", __FUNCTION__);
798         }
799 }
800
801 /*
802  * Function irlmp_do_expiry (void)
803  *
804  *    Do a cleanup of the discovery log (remove old entries)
805  *
806  * Note : separate from irlmp_do_discovery() so that we can handle
807  * passive discovery properly.
808  */
809 void irlmp_do_expiry(void)
810 {
811         struct lap_cb *lap;
812
813         /*
814          * Expire discovery on all links which are *not* connected.
815          * On links which are connected, we can't do discovery
816          * anymore and can't refresh the log, so we freeze the
817          * discovery log to keep info about the device we are
818          * connected to.
819          * This info is mandatory if we want irlmp_connect_request()
820          * to work properly. - Jean II
821          */
822         lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
823         while (lap != NULL) {
824                 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
825
826                 if (lap->lap_state == LAP_STANDBY) {
827                         /* Expire discoveries discovered on this link */
828                         irlmp_expire_discoveries(irlmp->cachelog, lap->saddr,
829                                                  FALSE);
830                 }
831                 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
832         }
833 }
834
835 /*
836  * Function irlmp_do_discovery (nslots)
837  *
838  *    Do some discovery on all links
839  *
840  * Note : log expiry is done above.
841  */
842 void irlmp_do_discovery(int nslots)
843 {
844         struct lap_cb *lap;
845         __u16 *data_hintsp;
846
847         /* Make sure the value is sane */
848         if ((nslots != 1) && (nslots != 6) && (nslots != 8) && (nslots != 16)){
849                 IRDA_WARNING("%s: invalid value for number of slots!\n",
850                              __FUNCTION__);
851                 nslots = sysctl_discovery_slots = 8;
852         }
853
854         /* Construct new discovery info to be used by IrLAP, */
855         data_hintsp = (__u16 *) irlmp->discovery_cmd.data.hints;
856         put_unaligned(irlmp->hints.word, data_hintsp);
857
858         /*
859          *  Set character set for device name (we use ASCII), and
860          *  copy device name. Remember to make room for a \0 at the
861          *  end
862          */
863         irlmp->discovery_cmd.data.charset = CS_ASCII;
864         strncpy(irlmp->discovery_cmd.data.info, sysctl_devname,
865                 NICKNAME_MAX_LEN);
866         irlmp->discovery_cmd.name_len = strlen(irlmp->discovery_cmd.data.info);
867         irlmp->discovery_cmd.nslots = nslots;
868
869         /*
870          * Try to send discovery packets on all links
871          */
872         lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
873         while (lap != NULL) {
874                 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
875
876                 if (lap->lap_state == LAP_STANDBY) {
877                         /* Try to discover */
878                         irlmp_do_lap_event(lap, LM_LAP_DISCOVERY_REQUEST,
879                                            NULL);
880                 }
881                 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
882         }
883 }
884
885 /*
886  * Function irlmp_discovery_request (nslots)
887  *
888  *    Do a discovery of devices in front of the computer
889  *
890  * If the caller has registered a client discovery callback, this
891  * allow him to receive the full content of the discovery log through
892  * this callback (as normally he will receive only new discoveries).
893  */
894 void irlmp_discovery_request(int nslots)
895 {
896         /* Return current cached discovery log (in full) */
897         irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_LOG);
898
899         /*
900          * Start a single discovery operation if discovery is not already
901          * running
902          */
903         if (!sysctl_discovery) {
904                 /* Check if user wants to override the default */
905                 if (nslots == DISCOVERY_DEFAULT_SLOTS)
906                         nslots = sysctl_discovery_slots;
907
908                 irlmp_do_discovery(nslots);
909                 /* Note : we never do expiry here. Expiry will run on the
910                  * discovery timer regardless of the state of sysctl_discovery
911                  * Jean II */
912         }
913 }
914 EXPORT_SYMBOL(irlmp_discovery_request);
915
916 /*
917  * Function irlmp_get_discoveries (pn, mask, slots)
918  *
919  *    Return the current discovery log
920  *
921  * If discovery is not enabled, you should call this function again
922  * after 1 or 2 seconds (i.e. after discovery has been done).
923  */
924 struct irda_device_info *irlmp_get_discoveries(int *pn, __u16 mask, int nslots)
925 {
926         /* If discovery is not enabled, it's likely that the discovery log
927          * will be empty. So, we trigger a single discovery, so that next
928          * time the user call us there might be some results in the log.
929          * Jean II
930          */
931         if (!sysctl_discovery) {
932                 /* Check if user wants to override the default */
933                 if (nslots == DISCOVERY_DEFAULT_SLOTS)
934                         nslots = sysctl_discovery_slots;
935
936                 /* Start discovery - will complete sometime later */
937                 irlmp_do_discovery(nslots);
938                 /* Note : we never do expiry here. Expiry will run on the
939                  * discovery timer regardless of the state of sysctl_discovery
940                  * Jean II */
941         }
942
943         /* Return current cached discovery log */
944         return(irlmp_copy_discoveries(irlmp->cachelog, pn, mask, TRUE));
945 }
946 EXPORT_SYMBOL(irlmp_get_discoveries);
947
948 /*
949  * Function irlmp_notify_client (log)
950  *
951  *    Notify all about discovered devices
952  *
953  * Clients registered with IrLMP are :
954  *      o IrComm
955  *      o IrLAN
956  *      o Any socket (in any state - ouch, that may be a lot !)
957  * The client may have defined a callback to be notified in case of
958  * partial/selective discovery based on the hints that it passed to IrLMP.
959  */
960 static inline void
961 irlmp_notify_client(irlmp_client_t *client,
962                     hashbin_t *log, DISCOVERY_MODE mode)
963 {
964         discinfo_t *discoveries;        /* Copy of the discovery log */
965         int     number;                 /* Number of nodes in the log */
966         int     i;
967
968         IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
969
970         /* Check if client wants or not partial/selective log (optimisation) */
971         if (!client->disco_callback)
972                 return;
973
974         /*
975          * Locking notes :
976          * the old code was manipulating the log directly, which was
977          * very racy. Now, we use copy_discoveries, that protects
978          * itself while dumping the log for us.
979          * The overhead of the copy is compensated by the fact that
980          * we only pass new discoveries in normal mode and don't
981          * pass the same old entry every 3s to the caller as we used
982          * to do (virtual function calling is expensive).
983          * Jean II
984          */
985
986         /*
987          * Now, check all discovered devices (if any), and notify client
988          * only about the services that the client is interested in
989          * We also notify only about the new devices unless the caller
990          * explicitly request a dump of the log. Jean II
991          */
992         discoveries = irlmp_copy_discoveries(log, &number,
993                                              client->hint_mask.word,
994                                              (mode == DISCOVERY_LOG));
995         /* Check if the we got some results */
996         if (discoveries == NULL)
997                 return; /* No nodes discovered */
998
999         /* Pass all entries to the listener */
1000         for(i = 0; i < number; i++)
1001                 client->disco_callback(&(discoveries[i]), mode, client->priv);
1002
1003         /* Free up our buffer */
1004         kfree(discoveries);
1005 }
1006
1007 /*
1008  * Function irlmp_discovery_confirm ( self, log)
1009  *
1010  *    Some device(s) answered to our discovery request! Check to see which
1011  *    device it is, and give indication to the client(s)
1012  *
1013  */
1014 void irlmp_discovery_confirm(hashbin_t *log, DISCOVERY_MODE mode)
1015 {
1016         irlmp_client_t *client;
1017         irlmp_client_t *client_next;
1018
1019         IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1020
1021         IRDA_ASSERT(log != NULL, return;);
1022
1023         if (!(HASHBIN_GET_SIZE(log)))
1024                 return;
1025
1026         /* For each client - notify callback may touch client list */
1027         client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
1028         while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
1029                                          (void *) &client_next) ) {
1030                 /* Check if we should notify client */
1031                 irlmp_notify_client(client, log, mode);
1032
1033                 client = client_next;
1034         }
1035 }
1036
1037 /*
1038  * Function irlmp_discovery_expiry (expiry)
1039  *
1040  *      This device is no longer been discovered, and therefore it is being
1041  *      purged from the discovery log. Inform all clients who have
1042  *      registered for this event...
1043  *
1044  *      Note : called exclusively from discovery.c
1045  *      Note : this is no longer called under discovery spinlock, so the
1046  *              client can do whatever he wants in the callback.
1047  */
1048 void irlmp_discovery_expiry(discinfo_t *expiries, int number)
1049 {
1050         irlmp_client_t *client;
1051         irlmp_client_t *client_next;
1052         int             i;
1053
1054         IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1055
1056         IRDA_ASSERT(expiries != NULL, return;);
1057
1058         /* For each client - notify callback may touch client list */
1059         client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
1060         while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
1061                                          (void *) &client_next) ) {
1062
1063                 /* Pass all entries to the listener */
1064                 for(i = 0; i < number; i++) {
1065                         /* Check if we should notify client */
1066                         if ((client->expir_callback) &&
1067                             (client->hint_mask.word & u16ho(expiries[i].hints)
1068                              & 0x7f7f) )
1069                                 client->expir_callback(&(expiries[i]),
1070                                                        EXPIRY_TIMEOUT,
1071                                                        client->priv);
1072                 }
1073
1074                 /* Next client */
1075                 client = client_next;
1076         }
1077 }
1078
1079 /*
1080  * Function irlmp_get_discovery_response ()
1081  *
1082  *    Used by IrLAP to get the discovery info it needs when answering
1083  *    discovery requests by other devices.
1084  */
1085 discovery_t *irlmp_get_discovery_response(void)
1086 {
1087         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1088
1089         IRDA_ASSERT(irlmp != NULL, return NULL;);
1090
1091         u16ho(irlmp->discovery_rsp.data.hints) = irlmp->hints.word;
1092
1093         /*
1094          *  Set character set for device name (we use ASCII), and
1095          *  copy device name. Remember to make room for a \0 at the
1096          *  end
1097          */
1098         irlmp->discovery_rsp.data.charset = CS_ASCII;
1099
1100         strncpy(irlmp->discovery_rsp.data.info, sysctl_devname,
1101                 NICKNAME_MAX_LEN);
1102         irlmp->discovery_rsp.name_len = strlen(irlmp->discovery_rsp.data.info);
1103
1104         return &irlmp->discovery_rsp;
1105 }
1106
1107 /*
1108  * Function irlmp_data_request (self, skb)
1109  *
1110  *    Send some data to peer device
1111  *
1112  * Note on skb management :
1113  * After calling the lower layers of the IrDA stack, we always
1114  * kfree() the skb, which drop the reference count (and potentially
1115  * destroy it).
1116  * IrLMP and IrLAP may queue the packet, and in those cases will need
1117  * to use skb_get() to keep it around.
1118  * Jean II
1119  */
1120 int irlmp_data_request(struct lsap_cb *self, struct sk_buff *userdata)
1121 {
1122         int     ret;
1123
1124         IRDA_ASSERT(self != NULL, return -1;);
1125         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
1126
1127         /* Make room for MUX header */
1128         IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
1129         skb_push(userdata, LMP_HEADER);
1130
1131         ret = irlmp_do_lsap_event(self, LM_DATA_REQUEST, userdata);
1132
1133         /* Drop reference count - see irlap_data_request(). */
1134         dev_kfree_skb(userdata);
1135
1136         return ret;
1137 }
1138 EXPORT_SYMBOL(irlmp_data_request);
1139
1140 /*
1141  * Function irlmp_data_indication (handle, skb)
1142  *
1143  *    Got data from LAP layer so pass it up to upper layer
1144  *
1145  */
1146 void irlmp_data_indication(struct lsap_cb *self, struct sk_buff *skb)
1147 {
1148         /* Hide LMP header from layer above */
1149         skb_pull(skb, LMP_HEADER);
1150
1151         if (self->notify.data_indication) {
1152                 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1153                 skb_get(skb);
1154                 self->notify.data_indication(self->notify.instance, self, skb);
1155         }
1156 }
1157
1158 /*
1159  * Function irlmp_udata_request (self, skb)
1160  */
1161 int irlmp_udata_request(struct lsap_cb *self, struct sk_buff *userdata)
1162 {
1163         int     ret;
1164
1165         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1166
1167         IRDA_ASSERT(userdata != NULL, return -1;);
1168
1169         /* Make room for MUX header */
1170         IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
1171         skb_push(userdata, LMP_HEADER);
1172
1173         ret = irlmp_do_lsap_event(self, LM_UDATA_REQUEST, userdata);
1174
1175         /* Drop reference count - see irlap_data_request(). */
1176         dev_kfree_skb(userdata);
1177
1178         return ret;
1179 }
1180
1181 /*
1182  * Function irlmp_udata_indication (self, skb)
1183  *
1184  *    Send unreliable data (but still within the connection)
1185  *
1186  */
1187 void irlmp_udata_indication(struct lsap_cb *self, struct sk_buff *skb)
1188 {
1189         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1190
1191         IRDA_ASSERT(self != NULL, return;);
1192         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
1193         IRDA_ASSERT(skb != NULL, return;);
1194
1195         /* Hide LMP header from layer above */
1196         skb_pull(skb, LMP_HEADER);
1197
1198         if (self->notify.udata_indication) {
1199                 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1200                 skb_get(skb);
1201                 self->notify.udata_indication(self->notify.instance, self,
1202                                               skb);
1203         }
1204 }
1205
1206 /*
1207  * Function irlmp_connless_data_request (self, skb)
1208  */
1209 #ifdef CONFIG_IRDA_ULTRA
1210 int irlmp_connless_data_request(struct lsap_cb *self, struct sk_buff *userdata,
1211                                 __u8 pid)
1212 {
1213         struct sk_buff *clone_skb;
1214         struct lap_cb *lap;
1215
1216         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1217
1218         IRDA_ASSERT(userdata != NULL, return -1;);
1219
1220         /* Make room for MUX and PID header */
1221         IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER+LMP_PID_HEADER,
1222                     return -1;);
1223
1224         /* Insert protocol identifier */
1225         skb_push(userdata, LMP_PID_HEADER);
1226         if(self != NULL)
1227           userdata->data[0] = self->pid;
1228         else
1229           userdata->data[0] = pid;
1230
1231         /* Connectionless sockets must use 0x70 */
1232         skb_push(userdata, LMP_HEADER);
1233         userdata->data[0] = userdata->data[1] = LSAP_CONNLESS;
1234
1235         /* Try to send Connectionless  packets out on all links */
1236         lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
1237         while (lap != NULL) {
1238                 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return -1;);
1239
1240                 clone_skb = skb_clone(userdata, GFP_ATOMIC);
1241                 if (!clone_skb) {
1242                         dev_kfree_skb(userdata);
1243                         return -ENOMEM;
1244                 }
1245
1246                 irlap_unitdata_request(lap->irlap, clone_skb);
1247                 /* irlap_unitdata_request() don't increase refcount,
1248                  * so no dev_kfree_skb() - Jean II */
1249
1250                 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
1251         }
1252         dev_kfree_skb(userdata);
1253
1254         return 0;
1255 }
1256 #endif /* CONFIG_IRDA_ULTRA */
1257
1258 /*
1259  * Function irlmp_connless_data_indication (self, skb)
1260  *
1261  *    Receive unreliable data outside any connection. Mostly used by Ultra
1262  *
1263  */
1264 #ifdef CONFIG_IRDA_ULTRA
1265 void irlmp_connless_data_indication(struct lsap_cb *self, struct sk_buff *skb)
1266 {
1267         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1268
1269         IRDA_ASSERT(self != NULL, return;);
1270         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
1271         IRDA_ASSERT(skb != NULL, return;);
1272
1273         /* Hide LMP and PID header from layer above */
1274         skb_pull(skb, LMP_HEADER+LMP_PID_HEADER);
1275
1276         if (self->notify.udata_indication) {
1277                 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1278                 skb_get(skb);
1279                 self->notify.udata_indication(self->notify.instance, self,
1280                                               skb);
1281         }
1282 }
1283 #endif /* CONFIG_IRDA_ULTRA */
1284
1285 /*
1286  * Propagate status indication from LAP to LSAPs (via LMP)
1287  * This don't trigger any change of state in lap_cb, lmp_cb or lsap_cb,
1288  * and the event is stateless, therefore we can bypass both state machines
1289  * and send the event direct to the LSAP user.
1290  * Jean II
1291  */
1292 void irlmp_status_indication(struct lap_cb *self,
1293                              LINK_STATUS link, LOCK_STATUS lock)
1294 {
1295         struct lsap_cb *next;
1296         struct lsap_cb *curr;
1297
1298         /* Send status_indication to all LSAPs using this link */
1299         curr = (struct lsap_cb *) hashbin_get_first( self->lsaps);
1300         while (NULL != hashbin_find_next(self->lsaps, (long) curr, NULL,
1301                                          (void *) &next) ) {
1302                 IRDA_ASSERT(curr->magic == LMP_LSAP_MAGIC, return;);
1303                 /*
1304                  *  Inform service user if he has requested it
1305                  */
1306                 if (curr->notify.status_indication != NULL)
1307                         curr->notify.status_indication(curr->notify.instance,
1308                                                        link, lock);
1309                 else
1310                         IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
1311
1312                 curr = next;
1313         }
1314 }
1315
1316 /*
1317  * Receive flow control indication from LAP.
1318  * LAP want us to send it one more frame. We implement a simple round
1319  * robin scheduler between the active sockets so that we get a bit of
1320  * fairness. Note that the round robin is far from perfect, but it's
1321  * better than nothing.
1322  * We then poll the selected socket so that we can do synchronous
1323  * refilling of IrLAP (which allow to minimise the number of buffers).
1324  * Jean II
1325  */
1326 void irlmp_flow_indication(struct lap_cb *self, LOCAL_FLOW flow)
1327 {
1328         struct lsap_cb *next;
1329         struct lsap_cb *curr;
1330         int     lsap_todo;
1331
1332         IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
1333         IRDA_ASSERT(flow == FLOW_START, return;);
1334
1335         /* Get the number of lsap. That's the only safe way to know
1336          * that we have looped around... - Jean II */
1337         lsap_todo = HASHBIN_GET_SIZE(self->lsaps);
1338         IRDA_DEBUG(4, "%s() : %d lsaps to scan\n", __FUNCTION__, lsap_todo);
1339
1340         /* Poll lsap in order until the queue is full or until we
1341          * tried them all.
1342          * Most often, the current LSAP will have something to send,
1343          * so we will go through this loop only once. - Jean II */
1344         while((lsap_todo--) &&
1345               (IRLAP_GET_TX_QUEUE_LEN(self->irlap) < LAP_HIGH_THRESHOLD)) {
1346                 /* Try to find the next lsap we should poll. */
1347                 next = self->flow_next;
1348                 /* If we have no lsap, restart from first one */
1349                 if(next == NULL)
1350                         next = (struct lsap_cb *) hashbin_get_first(self->lsaps);
1351                 /* Verify current one and find the next one */
1352                 curr = hashbin_find_next(self->lsaps, (long) next, NULL,
1353                                          (void *) &self->flow_next);
1354                 /* Uh-oh... Paranoia */
1355                 if(curr == NULL)
1356                         break;
1357                 IRDA_DEBUG(4, "%s() : curr is %p, next was %p and is now %p, still %d to go - queue len = %d\n", __FUNCTION__, curr, next, self->flow_next, lsap_todo, IRLAP_GET_TX_QUEUE_LEN(self->irlap));
1358
1359                 /* Inform lsap user that it can send one more packet. */
1360                 if (curr->notify.flow_indication != NULL)
1361                         curr->notify.flow_indication(curr->notify.instance,
1362                                                      curr, flow);
1363                 else
1364                         IRDA_DEBUG(1, "%s(), no handler\n", __FUNCTION__);
1365         }
1366 }
1367
1368 #if 0
1369 /*
1370  * Function irlmp_hint_to_service (hint)
1371  *
1372  *    Returns a list of all servics contained in the given hint bits. This
1373  *    function assumes that the hint bits have the size of two bytes only
1374  */
1375 __u8 *irlmp_hint_to_service(__u8 *hint)
1376 {
1377         __u8 *service;
1378         int i = 0;
1379
1380         /*
1381          * Allocate array to store services in. 16 entries should be safe
1382          * since we currently only support 2 hint bytes
1383          */
1384         service = kmalloc(16, GFP_ATOMIC);
1385         if (!service) {
1386                 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1387                 return NULL;
1388         }
1389
1390         if (!hint[0]) {
1391                 IRDA_DEBUG(1, "<None>\n");
1392                 kfree(service);
1393                 return NULL;
1394         }
1395         if (hint[0] & HINT_PNP)
1396                 IRDA_DEBUG(1, "PnP Compatible ");
1397         if (hint[0] & HINT_PDA)
1398                 IRDA_DEBUG(1, "PDA/Palmtop ");
1399         if (hint[0] & HINT_COMPUTER)
1400                 IRDA_DEBUG(1, "Computer ");
1401         if (hint[0] & HINT_PRINTER) {
1402                 IRDA_DEBUG(1, "Printer ");
1403                 service[i++] = S_PRINTER;
1404         }
1405         if (hint[0] & HINT_MODEM)
1406                 IRDA_DEBUG(1, "Modem ");
1407         if (hint[0] & HINT_FAX)
1408                 IRDA_DEBUG(1, "Fax ");
1409         if (hint[0] & HINT_LAN) {
1410                 IRDA_DEBUG(1, "LAN Access ");
1411                 service[i++] = S_LAN;
1412         }
1413         /*
1414          *  Test if extension byte exists. This byte will usually be
1415          *  there, but this is not really required by the standard.
1416          *  (IrLMP p. 29)
1417          */
1418         if (hint[0] & HINT_EXTENSION) {
1419                 if (hint[1] & HINT_TELEPHONY) {
1420                         IRDA_DEBUG(1, "Telephony ");
1421                         service[i++] = S_TELEPHONY;
1422                 } if (hint[1] & HINT_FILE_SERVER)
1423                         IRDA_DEBUG(1, "File Server ");
1424
1425                 if (hint[1] & HINT_COMM) {
1426                         IRDA_DEBUG(1, "IrCOMM ");
1427                         service[i++] = S_COMM;
1428                 }
1429                 if (hint[1] & HINT_OBEX) {
1430                         IRDA_DEBUG(1, "IrOBEX ");
1431                         service[i++] = S_OBEX;
1432                 }
1433         }
1434         IRDA_DEBUG(1, "\n");
1435
1436         /* So that client can be notified about any discovery */
1437         service[i++] = S_ANY;
1438
1439         service[i] = S_END;
1440
1441         return service;
1442 }
1443 #endif
1444
1445 static const __u16 service_hint_mapping[S_END][2] = {
1446         { HINT_PNP,             0 },                    /* S_PNP */
1447         { HINT_PDA,             0 },                    /* S_PDA */
1448         { HINT_COMPUTER,        0 },                    /* S_COMPUTER */
1449         { HINT_PRINTER,         0 },                    /* S_PRINTER */
1450         { HINT_MODEM,           0 },                    /* S_MODEM */
1451         { HINT_FAX,             0 },                    /* S_FAX */
1452         { HINT_LAN,             0 },                    /* S_LAN */
1453         { HINT_EXTENSION,       HINT_TELEPHONY },       /* S_TELEPHONY */
1454         { HINT_EXTENSION,       HINT_COMM },            /* S_COMM */
1455         { HINT_EXTENSION,       HINT_OBEX },            /* S_OBEX */
1456         { 0xFF,                 0xFF },                 /* S_ANY */
1457 };
1458
1459 /*
1460  * Function irlmp_service_to_hint (service)
1461  *
1462  *    Converts a service type, to a hint bit
1463  *
1464  *    Returns: a 16 bit hint value, with the service bit set
1465  */
1466 __u16 irlmp_service_to_hint(int service)
1467 {
1468         __u16_host_order hint;
1469
1470         hint.byte[0] = service_hint_mapping[service][0];
1471         hint.byte[1] = service_hint_mapping[service][1];
1472
1473         return hint.word;
1474 }
1475 EXPORT_SYMBOL(irlmp_service_to_hint);
1476
1477 /*
1478  * Function irlmp_register_service (service)
1479  *
1480  *    Register local service with IrLMP
1481  *
1482  */
1483 void *irlmp_register_service(__u16 hints)
1484 {
1485         irlmp_service_t *service;
1486
1487         IRDA_DEBUG(4, "%s(), hints = %04x\n", __FUNCTION__, hints);
1488
1489         /* Make a new registration */
1490         service = kmalloc(sizeof(irlmp_service_t), GFP_ATOMIC);
1491         if (!service) {
1492                 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1493                 return NULL;
1494         }
1495         service->hints.word = hints;
1496         hashbin_insert(irlmp->services, (irda_queue_t *) service,
1497                        (long) service, NULL);
1498
1499         irlmp->hints.word |= hints;
1500
1501         return (void *)service;
1502 }
1503 EXPORT_SYMBOL(irlmp_register_service);
1504
1505 /*
1506  * Function irlmp_unregister_service (handle)
1507  *
1508  *    Unregister service with IrLMP.
1509  *
1510  *    Returns: 0 on success, -1 on error
1511  */
1512 int irlmp_unregister_service(void *handle)
1513 {
1514         irlmp_service_t *service;
1515         unsigned long flags;
1516
1517         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1518
1519         if (!handle)
1520                 return -1;
1521
1522         /* Caller may call with invalid handle (it's legal) - Jean II */
1523         service = hashbin_lock_find(irlmp->services, (long) handle, NULL);
1524         if (!service) {
1525                 IRDA_DEBUG(1, "%s(), Unknown service!\n", __FUNCTION__);
1526                 return -1;
1527         }
1528
1529         hashbin_remove_this(irlmp->services, (irda_queue_t *) service);
1530         kfree(service);
1531
1532         /* Remove old hint bits */
1533         irlmp->hints.word = 0;
1534
1535         /* Refresh current hint bits */
1536         spin_lock_irqsave(&irlmp->services->hb_spinlock, flags);
1537         service = (irlmp_service_t *) hashbin_get_first(irlmp->services);
1538         while (service) {
1539                 irlmp->hints.word |= service->hints.word;
1540
1541                 service = (irlmp_service_t *)hashbin_get_next(irlmp->services);
1542         }
1543         spin_unlock_irqrestore(&irlmp->services->hb_spinlock, flags);
1544         return 0;
1545 }
1546 EXPORT_SYMBOL(irlmp_unregister_service);
1547
1548 /*
1549  * Function irlmp_register_client (hint_mask, callback1, callback2)
1550  *
1551  *    Register a local client with IrLMP
1552  *      First callback is selective discovery (based on hints)
1553  *      Second callback is for selective discovery expiries
1554  *
1555  *    Returns: handle > 0 on success, 0 on error
1556  */
1557 void *irlmp_register_client(__u16 hint_mask, DISCOVERY_CALLBACK1 disco_clb,
1558                             DISCOVERY_CALLBACK2 expir_clb, void *priv)
1559 {
1560         irlmp_client_t *client;
1561
1562         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1563         IRDA_ASSERT(irlmp != NULL, return NULL;);
1564
1565         /* Make a new registration */
1566         client = kmalloc(sizeof(irlmp_client_t), GFP_ATOMIC);
1567         if (!client) {
1568                 IRDA_DEBUG( 1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1569                 return NULL;
1570         }
1571
1572         /* Register the details */
1573         client->hint_mask.word = hint_mask;
1574         client->disco_callback = disco_clb;
1575         client->expir_callback = expir_clb;
1576         client->priv = priv;
1577
1578         hashbin_insert(irlmp->clients, (irda_queue_t *) client,
1579                        (long) client, NULL);
1580
1581         return (void *) client;
1582 }
1583 EXPORT_SYMBOL(irlmp_register_client);
1584
1585 /*
1586  * Function irlmp_update_client (handle, hint_mask, callback1, callback2)
1587  *
1588  *    Updates specified client (handle) with possibly new hint_mask and
1589  *    callback
1590  *
1591  *    Returns: 0 on success, -1 on error
1592  */
1593 int irlmp_update_client(void *handle, __u16 hint_mask,
1594                         DISCOVERY_CALLBACK1 disco_clb,
1595                         DISCOVERY_CALLBACK2 expir_clb, void *priv)
1596 {
1597         irlmp_client_t *client;
1598
1599         if (!handle)
1600                 return -1;
1601
1602         client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
1603         if (!client) {
1604                 IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
1605                 return -1;
1606         }
1607
1608         client->hint_mask.word = hint_mask;
1609         client->disco_callback = disco_clb;
1610         client->expir_callback = expir_clb;
1611         client->priv = priv;
1612
1613         return 0;
1614 }
1615 EXPORT_SYMBOL(irlmp_update_client);
1616
1617 /*
1618  * Function irlmp_unregister_client (handle)
1619  *
1620  *    Returns: 0 on success, -1 on error
1621  *
1622  */
1623 int irlmp_unregister_client(void *handle)
1624 {
1625         struct irlmp_client *client;
1626
1627         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1628
1629         if (!handle)
1630                 return -1;
1631
1632         /* Caller may call with invalid handle (it's legal) - Jean II */
1633         client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
1634         if (!client) {
1635                 IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
1636                 return -1;
1637         }
1638
1639         IRDA_DEBUG(4, "%s(), removing client!\n", __FUNCTION__);
1640         hashbin_remove_this(irlmp->clients, (irda_queue_t *) client);
1641         kfree(client);
1642
1643         return 0;
1644 }
1645 EXPORT_SYMBOL(irlmp_unregister_client);
1646
1647 /*
1648  * Function irlmp_slsap_inuse (slsap)
1649  *
1650  *    Check if the given source LSAP selector is in use
1651  *
1652  * This function is clearly not very efficient. On the mitigating side, the
1653  * stack make sure that in 99% of the cases, we are called only once
1654  * for each socket allocation. We could probably keep a bitmap
1655  * of the allocated LSAP, but I'm not sure the complexity is worth it.
1656  * Jean II
1657  */
1658 static int irlmp_slsap_inuse(__u8 slsap_sel)
1659 {
1660         struct lsap_cb *self;
1661         struct lap_cb *lap;
1662         unsigned long flags;
1663
1664         IRDA_ASSERT(irlmp != NULL, return TRUE;);
1665         IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return TRUE;);
1666         IRDA_ASSERT(slsap_sel != LSAP_ANY, return TRUE;);
1667
1668         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1669
1670 #ifdef CONFIG_IRDA_ULTRA
1671         /* Accept all bindings to the connectionless LSAP */
1672         if (slsap_sel == LSAP_CONNLESS)
1673                 return FALSE;
1674 #endif /* CONFIG_IRDA_ULTRA */
1675
1676         /* Valid values are between 0 and 127 (0x0-0x6F) */
1677         if (slsap_sel > LSAP_MAX)
1678                 return TRUE;
1679
1680         /*
1681          *  Check if slsap is already in use. To do this we have to loop over
1682          *  every IrLAP connection and check every LSAP associated with each
1683          *  the connection.
1684          */
1685         spin_lock_irqsave(&irlmp->links->hb_spinlock, flags);
1686         lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
1687         while (lap != NULL) {
1688                 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, goto errlap;);
1689
1690                 /* Careful for priority inversions here !
1691                  * irlmp->links is never taken while another IrDA
1692                  * spinlock is held, so we are safe. Jean II */
1693                 spin_lock(&lap->lsaps->hb_spinlock);
1694
1695                 /* For this IrLAP, check all the LSAPs */
1696                 self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
1697                 while (self != NULL) {
1698                         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC,
1699                                     goto errlsap;);
1700
1701                         if ((self->slsap_sel == slsap_sel)) {
1702                                 IRDA_DEBUG(4, "Source LSAP selector=%02x in use\n",
1703                                            self->slsap_sel);
1704                                 goto errlsap;
1705                         }
1706                         self = (struct lsap_cb*) hashbin_get_next(lap->lsaps);
1707                 }
1708                 spin_unlock(&lap->lsaps->hb_spinlock);
1709
1710                 /* Next LAP */
1711                 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
1712         }
1713         spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
1714
1715         /*
1716          * Server sockets are typically waiting for connections and
1717          * therefore reside in the unconnected list. We don't want
1718          * to give out their LSAPs for obvious reasons...
1719          * Jean II
1720          */
1721         spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
1722
1723         self = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);
1724         while (self != NULL) {
1725                 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, goto erruncon;);
1726                 if ((self->slsap_sel == slsap_sel)) {
1727                         IRDA_DEBUG(4, "Source LSAP selector=%02x in use (unconnected)\n",
1728                                    self->slsap_sel);
1729                         goto erruncon;
1730                 }
1731                 self = (struct lsap_cb*) hashbin_get_next(irlmp->unconnected_lsaps);
1732         }
1733         spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
1734
1735         return FALSE;
1736
1737         /* Error exit from within one of the two nested loops.
1738          * Make sure we release the right spinlock in the righ order.
1739          * Jean II */
1740 errlsap:
1741         spin_unlock(&lap->lsaps->hb_spinlock);
1742 IRDA_ASSERT_LABEL(errlap:)
1743         spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
1744         return TRUE;
1745
1746         /* Error exit from within the unconnected loop.
1747          * Just one spinlock to release... Jean II */
1748 erruncon:
1749         spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
1750         return TRUE;
1751 }
1752
1753 /*
1754  * Function irlmp_find_free_slsap ()
1755  *
1756  *    Find a free source LSAP to use. This function is called if the service
1757  *    user has requested a source LSAP equal to LM_ANY
1758  */
1759 static __u8 irlmp_find_free_slsap(void)
1760 {
1761         __u8 lsap_sel;
1762         int wrapped = 0;
1763
1764         IRDA_ASSERT(irlmp != NULL, return -1;);
1765         IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return -1;);
1766
1767         /* Most users don't really care which LSAPs they are given,
1768          * and therefore we automatically give them a free LSAP.
1769          * This function try to find a suitable LSAP, i.e. which is
1770          * not in use and is within the acceptable range. Jean II */
1771
1772         do {
1773                 /* Always increment to LSAP number before using it.
1774                  * In theory, we could reuse the last LSAP number, as long
1775                  * as it is no longer in use. Some IrDA stack do that.
1776                  * However, the previous socket may be half closed, i.e.
1777                  * we closed it, we think it's no longer in use, but the
1778                  * other side did not receive our close and think it's
1779                  * active and still send data on it.
1780                  * This is similar to what is done with PIDs and TCP ports.
1781                  * Also, this reduce the number of calls to irlmp_slsap_inuse()
1782                  * which is an expensive function to call.
1783                  * Jean II */
1784                 irlmp->last_lsap_sel++;
1785
1786                 /* Check if we need to wraparound (0x70-0x7f are reserved) */
1787                 if (irlmp->last_lsap_sel > LSAP_MAX) {
1788                         /* 0x00-0x10 are also reserved for well know ports */
1789                         irlmp->last_lsap_sel = 0x10;
1790
1791                         /* Make sure we terminate the loop */
1792                         if (wrapped++) {
1793                                 IRDA_ERROR("%s: no more free LSAPs !\n",
1794                                            __FUNCTION__);
1795                                 return 0;
1796                         }
1797                 }
1798
1799                 /* If the LSAP is in use, try the next one.
1800                  * Despite the autoincrement, we need to check if the lsap
1801                  * is really in use or not, first because LSAP may be
1802                  * directly allocated in irlmp_open_lsap(), and also because
1803                  * we may wraparound on old sockets. Jean II */
1804         } while (irlmp_slsap_inuse(irlmp->last_lsap_sel));
1805
1806         /* Got it ! */
1807         lsap_sel = irlmp->last_lsap_sel;
1808         IRDA_DEBUG(4, "%s(), found free lsap_sel=%02x\n",
1809                    __FUNCTION__, lsap_sel);
1810
1811         return lsap_sel;
1812 }
1813
1814 /*
1815  * Function irlmp_convert_lap_reason (lap_reason)
1816  *
1817  *    Converts IrLAP disconnect reason codes to IrLMP disconnect reason
1818  *    codes
1819  *
1820  */
1821 LM_REASON irlmp_convert_lap_reason( LAP_REASON lap_reason)
1822 {
1823         int reason = LM_LAP_DISCONNECT;
1824
1825         switch (lap_reason) {
1826         case LAP_DISC_INDICATION: /* Received a disconnect request from peer */
1827                 IRDA_DEBUG( 1, "%s(), LAP_DISC_INDICATION\n", __FUNCTION__);
1828                 reason = LM_USER_REQUEST;
1829                 break;
1830         case LAP_NO_RESPONSE:    /* To many retransmits without response */
1831                 IRDA_DEBUG( 1, "%s(), LAP_NO_RESPONSE\n", __FUNCTION__);
1832                 reason = LM_LAP_DISCONNECT;
1833                 break;
1834         case LAP_RESET_INDICATION:
1835                 IRDA_DEBUG( 1, "%s(), LAP_RESET_INDICATION\n", __FUNCTION__);
1836                 reason = LM_LAP_RESET;
1837                 break;
1838         case LAP_FOUND_NONE:
1839         case LAP_MEDIA_BUSY:
1840         case LAP_PRIMARY_CONFLICT:
1841                 IRDA_DEBUG(1, "%s(), LAP_FOUND_NONE, LAP_MEDIA_BUSY or LAP_PRIMARY_CONFLICT\n", __FUNCTION__);
1842                 reason = LM_CONNECT_FAILURE;
1843                 break;
1844         default:
1845                 IRDA_DEBUG(1, "%s(), Unknow IrLAP disconnect reason %d!\n",
1846                            __FUNCTION__, lap_reason);
1847                 reason = LM_LAP_DISCONNECT;
1848                 break;
1849         }
1850
1851         return reason;
1852 }
1853
1854 #ifdef CONFIG_PROC_FS
1855
1856 struct irlmp_iter_state {
1857         hashbin_t *hashbin;
1858 };
1859
1860 #define LSAP_START_TOKEN        ((void *)1)
1861 #define LINK_START_TOKEN        ((void *)2)
1862
1863 static void *irlmp_seq_hb_idx(struct irlmp_iter_state *iter, loff_t *off)
1864 {
1865         void *element;
1866
1867         spin_lock_irq(&iter->hashbin->hb_spinlock);
1868         for (element = hashbin_get_first(iter->hashbin);
1869              element != NULL; 
1870              element = hashbin_get_next(iter->hashbin)) {
1871                 if (!off || *off-- == 0) {
1872                         /* NB: hashbin left locked */
1873                         return element;
1874                 }
1875         }
1876         spin_unlock_irq(&iter->hashbin->hb_spinlock);
1877         iter->hashbin = NULL;
1878         return NULL;
1879 }
1880
1881
1882 static void *irlmp_seq_start(struct seq_file *seq, loff_t *pos)
1883 {
1884         struct irlmp_iter_state *iter = seq->private;
1885         void *v;
1886         loff_t off = *pos;
1887
1888         iter->hashbin = NULL;
1889         if (off-- == 0)
1890                 return LSAP_START_TOKEN;
1891
1892         iter->hashbin = irlmp->unconnected_lsaps;
1893         v = irlmp_seq_hb_idx(iter, &off);
1894         if (v)
1895                 return v;
1896
1897         if (off-- == 0)
1898                 return LINK_START_TOKEN;
1899
1900         iter->hashbin = irlmp->links;
1901         return irlmp_seq_hb_idx(iter, &off);
1902 }
1903
1904 static void *irlmp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1905 {
1906         struct irlmp_iter_state *iter = seq->private;
1907
1908         ++*pos;
1909
1910         if (v == LSAP_START_TOKEN) {            /* start of list of lsaps */
1911                 iter->hashbin = irlmp->unconnected_lsaps;
1912                 v = irlmp_seq_hb_idx(iter, NULL);
1913                 return v ? v : LINK_START_TOKEN;
1914         }
1915
1916         if (v == LINK_START_TOKEN) {            /* start of list of links */
1917                 iter->hashbin = irlmp->links;
1918                 return irlmp_seq_hb_idx(iter, NULL);
1919         }
1920
1921         v = hashbin_get_next(iter->hashbin);
1922
1923         if (v == NULL) {                        /* no more in this hash bin */
1924                 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1925
1926                 if (iter->hashbin == irlmp->unconnected_lsaps) 
1927                         v =  LINK_START_TOKEN;
1928
1929                 iter->hashbin = NULL;
1930         }
1931         return v;
1932 }
1933
1934 static void irlmp_seq_stop(struct seq_file *seq, void *v)
1935 {
1936         struct irlmp_iter_state *iter = seq->private;
1937
1938         if (iter->hashbin)
1939                 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1940 }
1941
1942 static int irlmp_seq_show(struct seq_file *seq, void *v)
1943 {
1944         const struct irlmp_iter_state *iter = seq->private;
1945         struct lsap_cb *self = v;
1946
1947         if (v == LSAP_START_TOKEN)
1948                 seq_puts(seq, "Unconnected LSAPs:\n");
1949         else if (v == LINK_START_TOKEN)
1950                 seq_puts(seq, "\nRegistered Link Layers:\n");
1951         else if (iter->hashbin == irlmp->unconnected_lsaps) {
1952                 self = v;
1953                 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -EINVAL; );
1954                 seq_printf(seq, "lsap state: %s, ",
1955                            irlsap_state[ self->lsap_state]);
1956                 seq_printf(seq,
1957                            "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1958                            self->slsap_sel, self->dlsap_sel);
1959                 seq_printf(seq, "(%s)", self->notify.name);
1960                 seq_printf(seq, "\n");
1961         } else if (iter->hashbin == irlmp->links) {
1962                 struct lap_cb *lap = v;
1963
1964                 seq_printf(seq, "lap state: %s, ",
1965                            irlmp_state[lap->lap_state]);
1966
1967                 seq_printf(seq, "saddr: %#08x, daddr: %#08x, ",
1968                            lap->saddr, lap->daddr);
1969                 seq_printf(seq, "num lsaps: %d",
1970                            HASHBIN_GET_SIZE(lap->lsaps));
1971                 seq_printf(seq, "\n");
1972
1973                 /* Careful for priority inversions here !
1974                  * All other uses of attrib spinlock are independent of
1975                  * the object spinlock, so we are safe. Jean II */
1976                 spin_lock(&lap->lsaps->hb_spinlock);
1977
1978                 seq_printf(seq, "\n  Connected LSAPs:\n");
1979                 for (self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
1980                      self != NULL;
1981                      self = (struct lsap_cb *)hashbin_get_next(lap->lsaps)) {
1982                         IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC,
1983                                     goto outloop;);
1984                         seq_printf(seq, "  lsap state: %s, ",
1985                                    irlsap_state[ self->lsap_state]);
1986                         seq_printf(seq,
1987                                    "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1988                                    self->slsap_sel, self->dlsap_sel);
1989                         seq_printf(seq, "(%s)", self->notify.name);
1990                         seq_putc(seq, '\n');
1991
1992                 }
1993         IRDA_ASSERT_LABEL(outloop:)
1994                 spin_unlock(&lap->lsaps->hb_spinlock);
1995                 seq_putc(seq, '\n');
1996         } else
1997                 return -EINVAL;
1998
1999         return 0;
2000 }
2001
2002 static struct seq_operations irlmp_seq_ops = {
2003         .start  = irlmp_seq_start,
2004         .next   = irlmp_seq_next,
2005         .stop   = irlmp_seq_stop,
2006         .show   = irlmp_seq_show,
2007 };
2008
2009 static int irlmp_seq_open(struct inode *inode, struct file *file)
2010 {
2011         struct seq_file *seq;
2012         int rc = -ENOMEM;
2013         struct irlmp_iter_state *s;
2014
2015         IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
2016
2017         s = kmalloc(sizeof(*s), GFP_KERNEL);
2018         if (!s)
2019                 goto out;
2020
2021         rc = seq_open(file, &irlmp_seq_ops);
2022         if (rc)
2023                 goto out_kfree;
2024
2025         seq          = file->private_data;
2026         seq->private = s;
2027 out:
2028         return rc;
2029 out_kfree:
2030         kfree(s);
2031         goto out;
2032 }
2033
2034 struct file_operations irlmp_seq_fops = {
2035         .owner          = THIS_MODULE,
2036         .open           = irlmp_seq_open,
2037         .read           = seq_read,
2038         .llseek         = seq_lseek,
2039         .release        = seq_release_private,
2040 };
2041
2042 #endif /* PROC_FS */