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