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