1 /*********************************************************************
5 * Description: IrDA Link Management Protocol (LMP) layer
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>
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>
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.
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.
25 ********************************************************************/
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>
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>
46 #include <asm/unaligned.h>
48 static __u8
irlmp_find_free_slsap(void);
49 static int irlmp_slsap_inuse(__u8 slsap_sel
);
51 /* Master structure */
52 struct irlmp_cb
*irlmp
= NULL
;
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];
61 static const char *irlmp_reasons
[] = {
72 const char *irlmp_reason_str(LM_REASON reason
)
74 reason
= min_t(size_t, reason
, ARRAY_SIZE(irlmp_reasons
) - 1);
75 return irlmp_reasons
[reason
];
79 * Function irlmp_init (void)
81 * Create (allocate) the main IrLMP structure
84 int __init
irlmp_init(void)
86 IRDA_DEBUG(1, "%s()\n", __func__
);
87 /* Initialize the irlmp structure. */
88 irlmp
= kzalloc( sizeof(struct irlmp_cb
), GFP_KERNEL
);
92 irlmp
->magic
= LMP_MAGIC
;
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
);
100 if ((irlmp
->clients
== NULL
) ||
101 (irlmp
->services
== NULL
) ||
102 (irlmp
->links
== NULL
) ||
103 (irlmp
->unconnected_lsaps
== NULL
) ||
104 (irlmp
->cachelog
== NULL
)) {
108 spin_lock_init(&irlmp
->cachelog
->hb_spinlock
);
110 irlmp
->last_lsap_sel
= 0x0f; /* Reserved 0x00-0x0f */
111 strcpy(sysctl_devname
, "Linux");
113 init_timer(&irlmp
->discovery_timer
);
115 /* Do discovery every 3 seconds, conditionally */
116 if (sysctl_discovery
)
117 irlmp_start_discovery_timer(irlmp
,
118 sysctl_discovery_timeout
*HZ
);
124 * Function irlmp_cleanup (void)
129 void irlmp_cleanup(void)
131 /* Check for main structure */
132 IRDA_ASSERT(irlmp
!= NULL
, return;);
133 IRDA_ASSERT(irlmp
->magic
== LMP_MAGIC
, return;);
135 del_timer(&irlmp
->discovery_timer
);
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
);
143 /* De-allocate main structure */
149 * Function irlmp_open_lsap (slsap, notify)
151 * Register with IrLMP and create a local LSAP,
152 * returns handle to LSAP.
154 struct lsap_cb
*irlmp_open_lsap(__u8 slsap_sel
, notify_t
*notify
, __u8 pid
)
156 struct lsap_cb
*self
;
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
;);
163 /* Does the client care which Source LSAP selector it gets? */
164 if (slsap_sel
== LSAP_ANY
) {
165 slsap_sel
= irlmp_find_free_slsap();
168 } else if (irlmp_slsap_inuse(slsap_sel
))
171 /* Allocate new instance of a LSAP connection */
172 self
= kzalloc(sizeof(struct lsap_cb
), GFP_ATOMIC
);
174 IRDA_ERROR("%s: can't allocate memory\n", __func__
);
178 self
->magic
= LMP_LSAP_MAGIC
;
179 self
->slsap_sel
= slsap_sel
;
181 /* Fix connectionless LSAP's */
182 if (slsap_sel
== LSAP_CONNLESS
) {
183 #ifdef CONFIG_IRDA_ULTRA
184 self
->dlsap_sel
= LSAP_CONNLESS
;
186 #endif /* CONFIG_IRDA_ULTRA */
188 self
->dlsap_sel
= LSAP_ANY
;
189 /* self->connected = FALSE; -> already NULL via memset() */
191 init_timer(&self
->watchdog_timer
);
193 self
->notify
= *notify
;
195 self
->lsap_state
= LSAP_DISCONNECTED
;
197 /* Insert into queue of unconnected LSAPs */
198 hashbin_insert(irlmp
->unconnected_lsaps
, (irda_queue_t
*) self
,
203 EXPORT_SYMBOL(irlmp_open_lsap
);
206 * Function __irlmp_close_lsap (self)
208 * Remove an instance of LSAP
210 static void __irlmp_close_lsap(struct lsap_cb
*self
)
212 IRDA_DEBUG(4, "%s()\n", __func__
);
214 IRDA_ASSERT(self
!= NULL
, return;);
215 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
218 * Set some of the variables to preset values
221 del_timer(&self
->watchdog_timer
); /* Important! */
224 dev_kfree_skb(self
->conn_skb
);
230 * Function irlmp_close_lsap (self)
232 * Close and remove LSAP
235 void irlmp_close_lsap(struct lsap_cb
*self
)
238 struct lsap_cb
*lsap
= NULL
;
240 IRDA_ASSERT(self
!= NULL
, return;);
241 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
244 * Find out if we should remove this LSAP from a link or from the
245 * list of unconnected lsaps (not associated with a link)
249 IRDA_ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return;);
250 /* We might close a LSAP before it has completed the
251 * connection setup. In those case, higher layers won't
252 * send a proper disconnect request. Harmless, except
253 * that we will forget to close LAP... - Jean II */
254 if(self
->lsap_state
!= LSAP_DISCONNECTED
) {
255 self
->lsap_state
= LSAP_DISCONNECTED
;
256 irlmp_do_lap_event(self
->lap
,
257 LM_LAP_DISCONNECT_REQUEST
, NULL
);
259 /* Now, remove from the link */
260 lsap
= hashbin_remove(lap
->lsaps
, (long) self
, NULL
);
261 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
262 lap
->cache
.valid
= FALSE
;
266 /* Check if we found the LSAP! If not then try the unconnected lsaps */
268 lsap
= hashbin_remove(irlmp
->unconnected_lsaps
, (long) self
,
273 "%s(), Looks like somebody has removed me already!\n",
277 __irlmp_close_lsap(self
);
279 EXPORT_SYMBOL(irlmp_close_lsap
);
282 * Function irlmp_register_irlap (saddr, notify)
284 * Register IrLAP layer with IrLMP. There is possible to have multiple
285 * instances of the IrLAP layer, each connected to different IrDA ports
288 void irlmp_register_link(struct irlap_cb
*irlap
, __u32 saddr
, notify_t
*notify
)
292 IRDA_ASSERT(irlmp
!= NULL
, return;);
293 IRDA_ASSERT(irlmp
->magic
== LMP_MAGIC
, return;);
294 IRDA_ASSERT(notify
!= NULL
, return;);
297 * Allocate new instance of a LSAP connection
299 lap
= kzalloc(sizeof(struct lap_cb
), GFP_KERNEL
);
301 IRDA_ERROR("%s: unable to kmalloc\n", __func__
);
306 lap
->magic
= LMP_LAP_MAGIC
;
308 lap
->daddr
= DEV_ADDR_ANY
;
309 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
310 lap
->cache
.valid
= FALSE
;
312 lap
->lsaps
= hashbin_new(HB_LOCK
);
313 if (lap
->lsaps
== NULL
) {
314 IRDA_WARNING("%s(), unable to kmalloc lsaps\n", __func__
);
319 lap
->lap_state
= LAP_STANDBY
;
321 init_timer(&lap
->idle_timer
);
324 * Insert into queue of LMP links
326 hashbin_insert(irlmp
->links
, (irda_queue_t
*) lap
, lap
->saddr
, NULL
);
329 * We set only this variable so IrLAP can tell us on which link the
330 * different events happened on
332 irda_notify_init(notify
);
333 notify
->instance
= lap
;
337 * Function irlmp_unregister_irlap (saddr)
339 * IrLAP layer has been removed!
342 void irlmp_unregister_link(__u32 saddr
)
346 IRDA_DEBUG(4, "%s()\n", __func__
);
348 /* We must remove ourselves from the hashbin *first*. This ensure
349 * that no more LSAPs will be open on this link and no discovery
350 * will be triggered anymore. Jean II */
351 link
= hashbin_remove(irlmp
->links
, saddr
, NULL
);
353 IRDA_ASSERT(link
->magic
== LMP_LAP_MAGIC
, return;);
355 /* Kill all the LSAPs on this link. Jean II */
356 link
->reason
= LAP_DISC_INDICATION
;
357 link
->daddr
= DEV_ADDR_ANY
;
358 irlmp_do_lap_event(link
, LM_LAP_DISCONNECT_INDICATION
, NULL
);
360 /* Remove all discoveries discovered at this link */
361 irlmp_expire_discoveries(irlmp
->cachelog
, link
->saddr
, TRUE
);
364 del_timer(&link
->idle_timer
);
366 hashbin_delete(link
->lsaps
, (FREE_FUNC
) __irlmp_close_lsap
);
372 * Function irlmp_connect_request (handle, dlsap, userdata)
374 * Connect with a peer LSAP
377 int irlmp_connect_request(struct lsap_cb
*self
, __u8 dlsap_sel
,
378 __u32 saddr
, __u32 daddr
,
379 struct qos_info
*qos
, struct sk_buff
*userdata
)
381 struct sk_buff
*tx_skb
= userdata
;
383 struct lsap_cb
*lsap
;
386 IRDA_ASSERT(self
!= NULL
, return -EBADR
;);
387 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return -EBADR
;);
390 "%s(), slsap_sel=%02x, dlsap_sel=%02x, saddr=%08x, daddr=%08x\n",
391 __func__
, self
->slsap_sel
, dlsap_sel
, saddr
, daddr
);
393 if (test_bit(0, &self
->connected
)) {
398 /* Client must supply destination device address */
405 if (tx_skb
== NULL
) {
406 tx_skb
= alloc_skb(LMP_MAX_HEADER
, GFP_ATOMIC
);
410 skb_reserve(tx_skb
, LMP_MAX_HEADER
);
413 /* Make room for MUX control header (3 bytes) */
414 IRDA_ASSERT(skb_headroom(tx_skb
) >= LMP_CONTROL_HEADER
, return -1;);
415 skb_push(tx_skb
, LMP_CONTROL_HEADER
);
417 self
->dlsap_sel
= dlsap_sel
;
420 * Find the link to where we should try to connect since there may
421 * be more than one IrDA port on this machine. If the client has
422 * passed us the saddr (and already knows which link to use), then
423 * we use that to find the link, if not then we have to look in the
424 * discovery log and check if any of the links has discovered a
425 * device with the given daddr
427 if ((!saddr
) || (saddr
== DEV_ADDR_ANY
)) {
428 discovery_t
*discovery
;
431 spin_lock_irqsave(&irlmp
->cachelog
->hb_spinlock
, flags
);
432 if (daddr
!= DEV_ADDR_ANY
)
433 discovery
= hashbin_find(irlmp
->cachelog
, daddr
, NULL
);
435 IRDA_DEBUG(2, "%s(), no daddr\n", __func__
);
436 discovery
= (discovery_t
*)
437 hashbin_get_first(irlmp
->cachelog
);
441 saddr
= discovery
->data
.saddr
;
442 daddr
= discovery
->data
.daddr
;
444 spin_unlock_irqrestore(&irlmp
->cachelog
->hb_spinlock
, flags
);
446 lap
= hashbin_lock_find(irlmp
->links
, saddr
, NULL
);
448 IRDA_DEBUG(1, "%s(), Unable to find a usable link!\n", __func__
);
453 /* Check if LAP is disconnected or already connected */
454 if (lap
->daddr
== DEV_ADDR_ANY
)
456 else if (lap
->daddr
!= daddr
) {
457 /* Check if some LSAPs are active on this LAP */
458 if (HASHBIN_GET_SIZE(lap
->lsaps
) == 0) {
459 /* No active connection, but LAP hasn't been
460 * disconnected yet (waiting for timeout in LAP).
461 * Maybe we could give LAP a bit of help in this case.
463 IRDA_DEBUG(0, "%s(), sorry, but I'm waiting for LAP to timeout!\n", __func__
);
468 /* LAP is already connected to a different node, and LAP
469 * can only talk to one node at a time */
470 IRDA_DEBUG(0, "%s(), sorry, but link is busy!\n", __func__
);
478 * Remove LSAP from list of unconnected LSAPs and insert it into the
479 * list of connected LSAPs for the particular link
481 lsap
= hashbin_remove(irlmp
->unconnected_lsaps
, (long) self
, NULL
);
483 IRDA_ASSERT(lsap
!= NULL
, return -1;);
484 IRDA_ASSERT(lsap
->magic
== LMP_LSAP_MAGIC
, return -1;);
485 IRDA_ASSERT(lsap
->lap
!= NULL
, return -1;);
486 IRDA_ASSERT(lsap
->lap
->magic
== LMP_LAP_MAGIC
, return -1;);
488 hashbin_insert(self
->lap
->lsaps
, (irda_queue_t
*) self
, (long) self
,
491 set_bit(0, &self
->connected
); /* TRUE */
494 * User supplied qos specifications?
499 irlmp_do_lsap_event(self
, LM_CONNECT_REQUEST
, tx_skb
);
501 /* Drop reference count - see irlap_data_request(). */
502 dev_kfree_skb(tx_skb
);
509 dev_kfree_skb(tx_skb
);
512 EXPORT_SYMBOL(irlmp_connect_request
);
515 * Function irlmp_connect_indication (self)
517 * Incoming connection
520 void irlmp_connect_indication(struct lsap_cb
*self
, struct sk_buff
*skb
)
526 IRDA_ASSERT(self
!= NULL
, return;);
527 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
528 IRDA_ASSERT(skb
!= NULL
, return;);
529 IRDA_ASSERT(self
->lap
!= NULL
, return;);
531 IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
532 __func__
, self
->slsap_sel
, self
->dlsap_sel
);
534 /* Note : self->lap is set in irlmp_link_data_indication(),
535 * (case CONNECT_CMD:) because we have no way to set it here.
536 * Similarly, self->dlsap_sel is usually set in irlmp_find_lsap().
539 self
->qos
= *self
->lap
->qos
;
541 max_seg_size
= self
->lap
->qos
->data_size
.value
-LMP_HEADER
;
542 lap_header_size
= IRLAP_GET_HEADER_SIZE(self
->lap
->irlap
);
543 max_header_size
= LMP_HEADER
+ lap_header_size
;
545 /* Hide LMP_CONTROL_HEADER header from layer above */
546 skb_pull(skb
, LMP_CONTROL_HEADER
);
548 if (self
->notify
.connect_indication
) {
549 /* Don't forget to refcount it - see irlap_driver_rcv(). */
551 self
->notify
.connect_indication(self
->notify
.instance
, self
,
552 &self
->qos
, max_seg_size
,
553 max_header_size
, skb
);
558 * Function irlmp_connect_response (handle, userdata)
560 * Service user is accepting connection
563 int irlmp_connect_response(struct lsap_cb
*self
, struct sk_buff
*userdata
)
565 IRDA_ASSERT(self
!= NULL
, return -1;);
566 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return -1;);
567 IRDA_ASSERT(userdata
!= NULL
, return -1;);
569 /* We set the connected bit and move the lsap to the connected list
570 * in the state machine itself. Jean II */
572 IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
573 __func__
, self
->slsap_sel
, self
->dlsap_sel
);
575 /* Make room for MUX control header (3 bytes) */
576 IRDA_ASSERT(skb_headroom(userdata
) >= LMP_CONTROL_HEADER
, return -1;);
577 skb_push(userdata
, LMP_CONTROL_HEADER
);
579 irlmp_do_lsap_event(self
, LM_CONNECT_RESPONSE
, userdata
);
581 /* Drop reference count - see irlap_data_request(). */
582 dev_kfree_skb(userdata
);
586 EXPORT_SYMBOL(irlmp_connect_response
);
589 * Function irlmp_connect_confirm (handle, skb)
591 * LSAP connection confirmed peer device!
593 void irlmp_connect_confirm(struct lsap_cb
*self
, struct sk_buff
*skb
)
599 IRDA_DEBUG(3, "%s()\n", __func__
);
601 IRDA_ASSERT(skb
!= NULL
, return;);
602 IRDA_ASSERT(self
!= NULL
, return;);
603 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
604 IRDA_ASSERT(self
->lap
!= NULL
, return;);
606 self
->qos
= *self
->lap
->qos
;
608 max_seg_size
= self
->lap
->qos
->data_size
.value
-LMP_HEADER
;
609 lap_header_size
= IRLAP_GET_HEADER_SIZE(self
->lap
->irlap
);
610 max_header_size
= LMP_HEADER
+ lap_header_size
;
612 IRDA_DEBUG(2, "%s(), max_header_size=%d\n",
613 __func__
, max_header_size
);
615 /* Hide LMP_CONTROL_HEADER header from layer above */
616 skb_pull(skb
, LMP_CONTROL_HEADER
);
618 if (self
->notify
.connect_confirm
) {
619 /* Don't forget to refcount it - see irlap_driver_rcv() */
621 self
->notify
.connect_confirm(self
->notify
.instance
, self
,
622 &self
->qos
, max_seg_size
,
623 max_header_size
, skb
);
628 * Function irlmp_dup (orig, instance)
630 * Duplicate LSAP, can be used by servers to confirm a connection on a
631 * new LSAP so it can keep listening on the old one.
634 struct lsap_cb
*irlmp_dup(struct lsap_cb
*orig
, void *instance
)
639 IRDA_DEBUG(1, "%s()\n", __func__
);
641 spin_lock_irqsave(&irlmp
->unconnected_lsaps
->hb_spinlock
, flags
);
643 /* Only allowed to duplicate unconnected LSAP's, and only LSAPs
644 * that have received a connect indication. Jean II */
645 if ((!hashbin_find(irlmp
->unconnected_lsaps
, (long) orig
, NULL
)) ||
646 (orig
->lap
== NULL
)) {
647 IRDA_DEBUG(0, "%s(), invalid LSAP (wrong state)\n",
649 spin_unlock_irqrestore(&irlmp
->unconnected_lsaps
->hb_spinlock
,
654 /* Allocate a new instance */
655 new = kmemdup(orig
, sizeof(*new), GFP_ATOMIC
);
657 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __func__
);
658 spin_unlock_irqrestore(&irlmp
->unconnected_lsaps
->hb_spinlock
,
662 /* new->lap = orig->lap; => done in the memcpy() */
663 /* new->slsap_sel = orig->slsap_sel; => done in the memcpy() */
664 new->conn_skb
= NULL
;
666 spin_unlock_irqrestore(&irlmp
->unconnected_lsaps
->hb_spinlock
, flags
);
668 /* Not everything is the same */
669 new->notify
.instance
= instance
;
671 init_timer(&new->watchdog_timer
);
673 hashbin_insert(irlmp
->unconnected_lsaps
, (irda_queue_t
*) new,
676 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
677 /* Make sure that we invalidate the LSAP cache */
678 new->lap
->cache
.valid
= FALSE
;
679 #endif /* CONFIG_IRDA_CACHE_LAST_LSAP */
685 * Function irlmp_disconnect_request (handle, userdata)
687 * The service user is requesting disconnection, this will not remove the
688 * LSAP, but only mark it as disconnected
690 int irlmp_disconnect_request(struct lsap_cb
*self
, struct sk_buff
*userdata
)
692 struct lsap_cb
*lsap
;
694 IRDA_ASSERT(self
!= NULL
, return -1;);
695 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return -1;);
696 IRDA_ASSERT(userdata
!= NULL
, return -1;);
698 /* Already disconnected ?
699 * There is a race condition between irlmp_disconnect_indication()
700 * and us that might mess up the hashbins below. This fixes it.
702 if (! test_and_clear_bit(0, &self
->connected
)) {
703 IRDA_DEBUG(0, "%s(), already disconnected!\n", __func__
);
704 dev_kfree_skb(userdata
);
708 skb_push(userdata
, LMP_CONTROL_HEADER
);
711 * Do the event before the other stuff since we must know
712 * which lap layer that the frame should be transmitted on
714 irlmp_do_lsap_event(self
, LM_DISCONNECT_REQUEST
, userdata
);
716 /* Drop reference count - see irlap_data_request(). */
717 dev_kfree_skb(userdata
);
720 * Remove LSAP from list of connected LSAPs for the particular link
721 * and insert it into the list of unconnected LSAPs
723 IRDA_ASSERT(self
->lap
!= NULL
, return -1;);
724 IRDA_ASSERT(self
->lap
->magic
== LMP_LAP_MAGIC
, return -1;);
725 IRDA_ASSERT(self
->lap
->lsaps
!= NULL
, return -1;);
727 lsap
= hashbin_remove(self
->lap
->lsaps
, (long) self
, NULL
);
728 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
729 self
->lap
->cache
.valid
= FALSE
;
732 IRDA_ASSERT(lsap
!= NULL
, return -1;);
733 IRDA_ASSERT(lsap
->magic
== LMP_LSAP_MAGIC
, return -1;);
734 IRDA_ASSERT(lsap
== self
, return -1;);
736 hashbin_insert(irlmp
->unconnected_lsaps
, (irda_queue_t
*) self
,
739 /* Reset some values */
740 self
->dlsap_sel
= LSAP_ANY
;
745 EXPORT_SYMBOL(irlmp_disconnect_request
);
748 * Function irlmp_disconnect_indication (reason, userdata)
750 * LSAP is being closed!
752 void irlmp_disconnect_indication(struct lsap_cb
*self
, LM_REASON reason
,
755 struct lsap_cb
*lsap
;
757 IRDA_DEBUG(1, "%s(), reason=%s [%d]\n", __func__
,
758 irlmp_reason_str(reason
), reason
);
759 IRDA_ASSERT(self
!= NULL
, return;);
760 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
762 IRDA_DEBUG(3, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
763 __func__
, self
->slsap_sel
, self
->dlsap_sel
);
765 /* Already disconnected ?
766 * There is a race condition between irlmp_disconnect_request()
767 * and us that might mess up the hashbins below. This fixes it.
769 if (! test_and_clear_bit(0, &self
->connected
)) {
770 IRDA_DEBUG(0, "%s(), already disconnected!\n", __func__
);
775 * Remove association between this LSAP and the link it used
777 IRDA_ASSERT(self
->lap
!= NULL
, return;);
778 IRDA_ASSERT(self
->lap
->lsaps
!= NULL
, return;);
780 lsap
= hashbin_remove(self
->lap
->lsaps
, (long) self
, NULL
);
781 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
782 self
->lap
->cache
.valid
= FALSE
;
785 IRDA_ASSERT(lsap
!= NULL
, return;);
786 IRDA_ASSERT(lsap
== self
, return;);
787 hashbin_insert(irlmp
->unconnected_lsaps
, (irda_queue_t
*) lsap
,
790 self
->dlsap_sel
= LSAP_ANY
;
794 * Inform service user
796 if (self
->notify
.disconnect_indication
) {
797 /* Don't forget to refcount it - see irlap_driver_rcv(). */
800 self
->notify
.disconnect_indication(self
->notify
.instance
,
803 IRDA_DEBUG(0, "%s(), no handler\n", __func__
);
808 * Function irlmp_do_expiry (void)
810 * Do a cleanup of the discovery log (remove old entries)
812 * Note : separate from irlmp_do_discovery() so that we can handle
813 * passive discovery properly.
815 void irlmp_do_expiry(void)
820 * Expire discovery on all links which are *not* connected.
821 * On links which are connected, we can't do discovery
822 * anymore and can't refresh the log, so we freeze the
823 * discovery log to keep info about the device we are
825 * This info is mandatory if we want irlmp_connect_request()
826 * to work properly. - Jean II
828 lap
= (struct lap_cb
*) hashbin_get_first(irlmp
->links
);
829 while (lap
!= NULL
) {
830 IRDA_ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return;);
832 if (lap
->lap_state
== LAP_STANDBY
) {
833 /* Expire discoveries discovered on this link */
834 irlmp_expire_discoveries(irlmp
->cachelog
, lap
->saddr
,
837 lap
= (struct lap_cb
*) hashbin_get_next(irlmp
->links
);
842 * Function irlmp_do_discovery (nslots)
844 * Do some discovery on all links
846 * Note : log expiry is done above.
848 void irlmp_do_discovery(int nslots
)
853 /* Make sure the value is sane */
854 if ((nslots
!= 1) && (nslots
!= 6) && (nslots
!= 8) && (nslots
!= 16)){
855 IRDA_WARNING("%s: invalid value for number of slots!\n",
857 nslots
= sysctl_discovery_slots
= 8;
860 /* Construct new discovery info to be used by IrLAP, */
861 data_hintsp
= (__u16
*) irlmp
->discovery_cmd
.data
.hints
;
862 put_unaligned(irlmp
->hints
.word
, data_hintsp
);
865 * Set character set for device name (we use ASCII), and
866 * copy device name. Remember to make room for a \0 at the
869 irlmp
->discovery_cmd
.data
.charset
= CS_ASCII
;
870 strncpy(irlmp
->discovery_cmd
.data
.info
, sysctl_devname
,
872 irlmp
->discovery_cmd
.name_len
= strlen(irlmp
->discovery_cmd
.data
.info
);
873 irlmp
->discovery_cmd
.nslots
= nslots
;
876 * Try to send discovery packets on all links
878 lap
= (struct lap_cb
*) hashbin_get_first(irlmp
->links
);
879 while (lap
!= NULL
) {
880 IRDA_ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return;);
882 if (lap
->lap_state
== LAP_STANDBY
) {
883 /* Try to discover */
884 irlmp_do_lap_event(lap
, LM_LAP_DISCOVERY_REQUEST
,
887 lap
= (struct lap_cb
*) hashbin_get_next(irlmp
->links
);
892 * Function irlmp_discovery_request (nslots)
894 * Do a discovery of devices in front of the computer
896 * If the caller has registered a client discovery callback, this
897 * allow him to receive the full content of the discovery log through
898 * this callback (as normally he will receive only new discoveries).
900 void irlmp_discovery_request(int nslots
)
902 /* Return current cached discovery log (in full) */
903 irlmp_discovery_confirm(irlmp
->cachelog
, DISCOVERY_LOG
);
906 * Start a single discovery operation if discovery is not already
909 if (!sysctl_discovery
) {
910 /* Check if user wants to override the default */
911 if (nslots
== DISCOVERY_DEFAULT_SLOTS
)
912 nslots
= sysctl_discovery_slots
;
914 irlmp_do_discovery(nslots
);
915 /* Note : we never do expiry here. Expiry will run on the
916 * discovery timer regardless of the state of sysctl_discovery
920 EXPORT_SYMBOL(irlmp_discovery_request
);
923 * Function irlmp_get_discoveries (pn, mask, slots)
925 * Return the current discovery log
927 * If discovery is not enabled, you should call this function again
928 * after 1 or 2 seconds (i.e. after discovery has been done).
930 struct irda_device_info
*irlmp_get_discoveries(int *pn
, __u16 mask
, int nslots
)
932 /* If discovery is not enabled, it's likely that the discovery log
933 * will be empty. So, we trigger a single discovery, so that next
934 * time the user call us there might be some results in the log.
937 if (!sysctl_discovery
) {
938 /* Check if user wants to override the default */
939 if (nslots
== DISCOVERY_DEFAULT_SLOTS
)
940 nslots
= sysctl_discovery_slots
;
942 /* Start discovery - will complete sometime later */
943 irlmp_do_discovery(nslots
);
944 /* Note : we never do expiry here. Expiry will run on the
945 * discovery timer regardless of the state of sysctl_discovery
949 /* Return current cached discovery log */
950 return irlmp_copy_discoveries(irlmp
->cachelog
, pn
, mask
, TRUE
);
952 EXPORT_SYMBOL(irlmp_get_discoveries
);
955 * Function irlmp_notify_client (log)
957 * Notify all about discovered devices
959 * Clients registered with IrLMP are :
962 * o Any socket (in any state - ouch, that may be a lot !)
963 * The client may have defined a callback to be notified in case of
964 * partial/selective discovery based on the hints that it passed to IrLMP.
967 irlmp_notify_client(irlmp_client_t
*client
,
968 hashbin_t
*log
, DISCOVERY_MODE mode
)
970 discinfo_t
*discoveries
; /* Copy of the discovery log */
971 int number
; /* Number of nodes in the log */
974 IRDA_DEBUG(3, "%s()\n", __func__
);
976 /* Check if client wants or not partial/selective log (optimisation) */
977 if (!client
->disco_callback
)
982 * the old code was manipulating the log directly, which was
983 * very racy. Now, we use copy_discoveries, that protects
984 * itself while dumping the log for us.
985 * The overhead of the copy is compensated by the fact that
986 * we only pass new discoveries in normal mode and don't
987 * pass the same old entry every 3s to the caller as we used
988 * to do (virtual function calling is expensive).
993 * Now, check all discovered devices (if any), and notify client
994 * only about the services that the client is interested in
995 * We also notify only about the new devices unless the caller
996 * explicitly request a dump of the log. Jean II
998 discoveries
= irlmp_copy_discoveries(log
, &number
,
999 client
->hint_mask
.word
,
1000 (mode
== DISCOVERY_LOG
));
1001 /* Check if the we got some results */
1002 if (discoveries
== NULL
)
1003 return; /* No nodes discovered */
1005 /* Pass all entries to the listener */
1006 for(i
= 0; i
< number
; i
++)
1007 client
->disco_callback(&(discoveries
[i
]), mode
, client
->priv
);
1009 /* Free up our buffer */
1014 * Function irlmp_discovery_confirm ( self, log)
1016 * Some device(s) answered to our discovery request! Check to see which
1017 * device it is, and give indication to the client(s)
1020 void irlmp_discovery_confirm(hashbin_t
*log
, DISCOVERY_MODE mode
)
1022 irlmp_client_t
*client
;
1023 irlmp_client_t
*client_next
;
1025 IRDA_DEBUG(3, "%s()\n", __func__
);
1027 IRDA_ASSERT(log
!= NULL
, return;);
1029 if (!(HASHBIN_GET_SIZE(log
)))
1032 /* For each client - notify callback may touch client list */
1033 client
= (irlmp_client_t
*) hashbin_get_first(irlmp
->clients
);
1034 while (NULL
!= hashbin_find_next(irlmp
->clients
, (long) client
, NULL
,
1035 (void *) &client_next
) ) {
1036 /* Check if we should notify client */
1037 irlmp_notify_client(client
, log
, mode
);
1039 client
= client_next
;
1044 * Function irlmp_discovery_expiry (expiry)
1046 * This device is no longer been discovered, and therefore it is being
1047 * purged from the discovery log. Inform all clients who have
1048 * registered for this event...
1050 * Note : called exclusively from discovery.c
1051 * Note : this is no longer called under discovery spinlock, so the
1052 * client can do whatever he wants in the callback.
1054 void irlmp_discovery_expiry(discinfo_t
*expiries
, int number
)
1056 irlmp_client_t
*client
;
1057 irlmp_client_t
*client_next
;
1060 IRDA_DEBUG(3, "%s()\n", __func__
);
1062 IRDA_ASSERT(expiries
!= NULL
, return;);
1064 /* For each client - notify callback may touch client list */
1065 client
= (irlmp_client_t
*) hashbin_get_first(irlmp
->clients
);
1066 while (NULL
!= hashbin_find_next(irlmp
->clients
, (long) client
, NULL
,
1067 (void *) &client_next
) ) {
1069 /* Pass all entries to the listener */
1070 for(i
= 0; i
< number
; i
++) {
1071 /* Check if we should notify client */
1072 if ((client
->expir_callback
) &&
1073 (client
->hint_mask
.word
&
1074 get_unaligned((__u16
*)expiries
[i
].hints
)
1076 client
->expir_callback(&(expiries
[i
]),
1082 client
= client_next
;
1087 * Function irlmp_get_discovery_response ()
1089 * Used by IrLAP to get the discovery info it needs when answering
1090 * discovery requests by other devices.
1092 discovery_t
*irlmp_get_discovery_response(void)
1094 IRDA_DEBUG(4, "%s()\n", __func__
);
1096 IRDA_ASSERT(irlmp
!= NULL
, return NULL
;);
1098 put_unaligned(irlmp
->hints
.word
, (__u16
*)irlmp
->discovery_rsp
.data
.hints
);
1101 * Set character set for device name (we use ASCII), and
1102 * copy device name. Remember to make room for a \0 at the
1105 irlmp
->discovery_rsp
.data
.charset
= CS_ASCII
;
1107 strncpy(irlmp
->discovery_rsp
.data
.info
, sysctl_devname
,
1109 irlmp
->discovery_rsp
.name_len
= strlen(irlmp
->discovery_rsp
.data
.info
);
1111 return &irlmp
->discovery_rsp
;
1115 * Function irlmp_data_request (self, skb)
1117 * Send some data to peer device
1119 * Note on skb management :
1120 * After calling the lower layers of the IrDA stack, we always
1121 * kfree() the skb, which drop the reference count (and potentially
1123 * IrLMP and IrLAP may queue the packet, and in those cases will need
1124 * to use skb_get() to keep it around.
1127 int irlmp_data_request(struct lsap_cb
*self
, struct sk_buff
*userdata
)
1131 IRDA_ASSERT(self
!= NULL
, return -1;);
1132 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return -1;);
1134 /* Make room for MUX header */
1135 IRDA_ASSERT(skb_headroom(userdata
) >= LMP_HEADER
, return -1;);
1136 skb_push(userdata
, LMP_HEADER
);
1138 ret
= irlmp_do_lsap_event(self
, LM_DATA_REQUEST
, userdata
);
1140 /* Drop reference count - see irlap_data_request(). */
1141 dev_kfree_skb(userdata
);
1145 EXPORT_SYMBOL(irlmp_data_request
);
1148 * Function irlmp_data_indication (handle, skb)
1150 * Got data from LAP layer so pass it up to upper layer
1153 void irlmp_data_indication(struct lsap_cb
*self
, struct sk_buff
*skb
)
1155 /* Hide LMP header from layer above */
1156 skb_pull(skb
, LMP_HEADER
);
1158 if (self
->notify
.data_indication
) {
1159 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1161 self
->notify
.data_indication(self
->notify
.instance
, self
, skb
);
1166 * Function irlmp_udata_request (self, skb)
1168 int irlmp_udata_request(struct lsap_cb
*self
, struct sk_buff
*userdata
)
1172 IRDA_DEBUG(4, "%s()\n", __func__
);
1174 IRDA_ASSERT(userdata
!= NULL
, return -1;);
1176 /* Make room for MUX header */
1177 IRDA_ASSERT(skb_headroom(userdata
) >= LMP_HEADER
, return -1;);
1178 skb_push(userdata
, LMP_HEADER
);
1180 ret
= irlmp_do_lsap_event(self
, LM_UDATA_REQUEST
, userdata
);
1182 /* Drop reference count - see irlap_data_request(). */
1183 dev_kfree_skb(userdata
);
1189 * Function irlmp_udata_indication (self, skb)
1191 * Send unreliable data (but still within the connection)
1194 void irlmp_udata_indication(struct lsap_cb
*self
, struct sk_buff
*skb
)
1196 IRDA_DEBUG(4, "%s()\n", __func__
);
1198 IRDA_ASSERT(self
!= NULL
, return;);
1199 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
1200 IRDA_ASSERT(skb
!= NULL
, return;);
1202 /* Hide LMP header from layer above */
1203 skb_pull(skb
, LMP_HEADER
);
1205 if (self
->notify
.udata_indication
) {
1206 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1208 self
->notify
.udata_indication(self
->notify
.instance
, self
,
1214 * Function irlmp_connless_data_request (self, skb)
1216 #ifdef CONFIG_IRDA_ULTRA
1217 int irlmp_connless_data_request(struct lsap_cb
*self
, struct sk_buff
*userdata
,
1220 struct sk_buff
*clone_skb
;
1223 IRDA_DEBUG(4, "%s()\n", __func__
);
1225 IRDA_ASSERT(userdata
!= NULL
, return -1;);
1227 /* Make room for MUX and PID header */
1228 IRDA_ASSERT(skb_headroom(userdata
) >= LMP_HEADER
+LMP_PID_HEADER
,
1231 /* Insert protocol identifier */
1232 skb_push(userdata
, LMP_PID_HEADER
);
1234 userdata
->data
[0] = self
->pid
;
1236 userdata
->data
[0] = pid
;
1238 /* Connectionless sockets must use 0x70 */
1239 skb_push(userdata
, LMP_HEADER
);
1240 userdata
->data
[0] = userdata
->data
[1] = LSAP_CONNLESS
;
1242 /* Try to send Connectionless packets out on all links */
1243 lap
= (struct lap_cb
*) hashbin_get_first(irlmp
->links
);
1244 while (lap
!= NULL
) {
1245 IRDA_ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return -1;);
1247 clone_skb
= skb_clone(userdata
, GFP_ATOMIC
);
1249 dev_kfree_skb(userdata
);
1253 irlap_unitdata_request(lap
->irlap
, clone_skb
);
1254 /* irlap_unitdata_request() don't increase refcount,
1255 * so no dev_kfree_skb() - Jean II */
1257 lap
= (struct lap_cb
*) hashbin_get_next(irlmp
->links
);
1259 dev_kfree_skb(userdata
);
1263 #endif /* CONFIG_IRDA_ULTRA */
1266 * Function irlmp_connless_data_indication (self, skb)
1268 * Receive unreliable data outside any connection. Mostly used by Ultra
1271 #ifdef CONFIG_IRDA_ULTRA
1272 void irlmp_connless_data_indication(struct lsap_cb
*self
, struct sk_buff
*skb
)
1274 IRDA_DEBUG(4, "%s()\n", __func__
);
1276 IRDA_ASSERT(self
!= NULL
, return;);
1277 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return;);
1278 IRDA_ASSERT(skb
!= NULL
, return;);
1280 /* Hide LMP and PID header from layer above */
1281 skb_pull(skb
, LMP_HEADER
+LMP_PID_HEADER
);
1283 if (self
->notify
.udata_indication
) {
1284 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1286 self
->notify
.udata_indication(self
->notify
.instance
, self
,
1290 #endif /* CONFIG_IRDA_ULTRA */
1293 * Propagate status indication from LAP to LSAPs (via LMP)
1294 * This don't trigger any change of state in lap_cb, lmp_cb or lsap_cb,
1295 * and the event is stateless, therefore we can bypass both state machines
1296 * and send the event direct to the LSAP user.
1299 void irlmp_status_indication(struct lap_cb
*self
,
1300 LINK_STATUS link
, LOCK_STATUS lock
)
1302 struct lsap_cb
*next
;
1303 struct lsap_cb
*curr
;
1305 /* Send status_indication to all LSAPs using this link */
1306 curr
= (struct lsap_cb
*) hashbin_get_first( self
->lsaps
);
1307 while (NULL
!= hashbin_find_next(self
->lsaps
, (long) curr
, NULL
,
1309 IRDA_ASSERT(curr
->magic
== LMP_LSAP_MAGIC
, return;);
1311 * Inform service user if he has requested it
1313 if (curr
->notify
.status_indication
!= NULL
)
1314 curr
->notify
.status_indication(curr
->notify
.instance
,
1317 IRDA_DEBUG(2, "%s(), no handler\n", __func__
);
1324 * Receive flow control indication from LAP.
1325 * LAP want us to send it one more frame. We implement a simple round
1326 * robin scheduler between the active sockets so that we get a bit of
1327 * fairness. Note that the round robin is far from perfect, but it's
1328 * better than nothing.
1329 * We then poll the selected socket so that we can do synchronous
1330 * refilling of IrLAP (which allow to minimise the number of buffers).
1333 void irlmp_flow_indication(struct lap_cb
*self
, LOCAL_FLOW flow
)
1335 struct lsap_cb
*next
;
1336 struct lsap_cb
*curr
;
1339 IRDA_ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
1340 IRDA_ASSERT(flow
== FLOW_START
, return;);
1342 /* Get the number of lsap. That's the only safe way to know
1343 * that we have looped around... - Jean II */
1344 lsap_todo
= HASHBIN_GET_SIZE(self
->lsaps
);
1345 IRDA_DEBUG(4, "%s() : %d lsaps to scan\n", __func__
, lsap_todo
);
1347 /* Poll lsap in order until the queue is full or until we
1349 * Most often, the current LSAP will have something to send,
1350 * so we will go through this loop only once. - Jean II */
1351 while((lsap_todo
--) &&
1352 (IRLAP_GET_TX_QUEUE_LEN(self
->irlap
) < LAP_HIGH_THRESHOLD
)) {
1353 /* Try to find the next lsap we should poll. */
1354 next
= self
->flow_next
;
1355 /* If we have no lsap, restart from first one */
1357 next
= (struct lsap_cb
*) hashbin_get_first(self
->lsaps
);
1358 /* Verify current one and find the next one */
1359 curr
= hashbin_find_next(self
->lsaps
, (long) next
, NULL
,
1360 (void *) &self
->flow_next
);
1361 /* Uh-oh... Paranoia */
1364 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
));
1366 /* Inform lsap user that it can send one more packet. */
1367 if (curr
->notify
.flow_indication
!= NULL
)
1368 curr
->notify
.flow_indication(curr
->notify
.instance
,
1371 IRDA_DEBUG(1, "%s(), no handler\n", __func__
);
1377 * Function irlmp_hint_to_service (hint)
1379 * Returns a list of all servics contained in the given hint bits. This
1380 * function assumes that the hint bits have the size of two bytes only
1382 __u8
*irlmp_hint_to_service(__u8
*hint
)
1388 * Allocate array to store services in. 16 entries should be safe
1389 * since we currently only support 2 hint bytes
1391 service
= kmalloc(16, GFP_ATOMIC
);
1393 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __func__
);
1398 IRDA_DEBUG(1, "<None>\n");
1402 if (hint
[0] & HINT_PNP
)
1403 IRDA_DEBUG(1, "PnP Compatible ");
1404 if (hint
[0] & HINT_PDA
)
1405 IRDA_DEBUG(1, "PDA/Palmtop ");
1406 if (hint
[0] & HINT_COMPUTER
)
1407 IRDA_DEBUG(1, "Computer ");
1408 if (hint
[0] & HINT_PRINTER
) {
1409 IRDA_DEBUG(1, "Printer ");
1410 service
[i
++] = S_PRINTER
;
1412 if (hint
[0] & HINT_MODEM
)
1413 IRDA_DEBUG(1, "Modem ");
1414 if (hint
[0] & HINT_FAX
)
1415 IRDA_DEBUG(1, "Fax ");
1416 if (hint
[0] & HINT_LAN
) {
1417 IRDA_DEBUG(1, "LAN Access ");
1418 service
[i
++] = S_LAN
;
1421 * Test if extension byte exists. This byte will usually be
1422 * there, but this is not really required by the standard.
1425 if (hint
[0] & HINT_EXTENSION
) {
1426 if (hint
[1] & HINT_TELEPHONY
) {
1427 IRDA_DEBUG(1, "Telephony ");
1428 service
[i
++] = S_TELEPHONY
;
1430 if (hint
[1] & HINT_FILE_SERVER
)
1431 IRDA_DEBUG(1, "File Server ");
1433 if (hint
[1] & HINT_COMM
) {
1434 IRDA_DEBUG(1, "IrCOMM ");
1435 service
[i
++] = S_COMM
;
1437 if (hint
[1] & HINT_OBEX
) {
1438 IRDA_DEBUG(1, "IrOBEX ");
1439 service
[i
++] = S_OBEX
;
1442 IRDA_DEBUG(1, "\n");
1444 /* So that client can be notified about any discovery */
1445 service
[i
++] = S_ANY
;
1453 static const __u16 service_hint_mapping
[S_END
][2] = {
1454 { HINT_PNP
, 0 }, /* S_PNP */
1455 { HINT_PDA
, 0 }, /* S_PDA */
1456 { HINT_COMPUTER
, 0 }, /* S_COMPUTER */
1457 { HINT_PRINTER
, 0 }, /* S_PRINTER */
1458 { HINT_MODEM
, 0 }, /* S_MODEM */
1459 { HINT_FAX
, 0 }, /* S_FAX */
1460 { HINT_LAN
, 0 }, /* S_LAN */
1461 { HINT_EXTENSION
, HINT_TELEPHONY
}, /* S_TELEPHONY */
1462 { HINT_EXTENSION
, HINT_COMM
}, /* S_COMM */
1463 { HINT_EXTENSION
, HINT_OBEX
}, /* S_OBEX */
1464 { 0xFF, 0xFF }, /* S_ANY */
1468 * Function irlmp_service_to_hint (service)
1470 * Converts a service type, to a hint bit
1472 * Returns: a 16 bit hint value, with the service bit set
1474 __u16
irlmp_service_to_hint(int service
)
1476 __u16_host_order hint
;
1478 hint
.byte
[0] = service_hint_mapping
[service
][0];
1479 hint
.byte
[1] = service_hint_mapping
[service
][1];
1483 EXPORT_SYMBOL(irlmp_service_to_hint
);
1486 * Function irlmp_register_service (service)
1488 * Register local service with IrLMP
1491 void *irlmp_register_service(__u16 hints
)
1493 irlmp_service_t
*service
;
1495 IRDA_DEBUG(4, "%s(), hints = %04x\n", __func__
, hints
);
1497 /* Make a new registration */
1498 service
= kmalloc(sizeof(irlmp_service_t
), GFP_ATOMIC
);
1500 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __func__
);
1503 service
->hints
.word
= hints
;
1504 hashbin_insert(irlmp
->services
, (irda_queue_t
*) service
,
1505 (long) service
, NULL
);
1507 irlmp
->hints
.word
|= hints
;
1509 return (void *)service
;
1511 EXPORT_SYMBOL(irlmp_register_service
);
1514 * Function irlmp_unregister_service (handle)
1516 * Unregister service with IrLMP.
1518 * Returns: 0 on success, -1 on error
1520 int irlmp_unregister_service(void *handle
)
1522 irlmp_service_t
*service
;
1523 unsigned long flags
;
1525 IRDA_DEBUG(4, "%s()\n", __func__
);
1530 /* Caller may call with invalid handle (it's legal) - Jean II */
1531 service
= hashbin_lock_find(irlmp
->services
, (long) handle
, NULL
);
1533 IRDA_DEBUG(1, "%s(), Unknown service!\n", __func__
);
1537 hashbin_remove_this(irlmp
->services
, (irda_queue_t
*) service
);
1540 /* Remove old hint bits */
1541 irlmp
->hints
.word
= 0;
1543 /* Refresh current hint bits */
1544 spin_lock_irqsave(&irlmp
->services
->hb_spinlock
, flags
);
1545 service
= (irlmp_service_t
*) hashbin_get_first(irlmp
->services
);
1547 irlmp
->hints
.word
|= service
->hints
.word
;
1549 service
= (irlmp_service_t
*)hashbin_get_next(irlmp
->services
);
1551 spin_unlock_irqrestore(&irlmp
->services
->hb_spinlock
, flags
);
1554 EXPORT_SYMBOL(irlmp_unregister_service
);
1557 * Function irlmp_register_client (hint_mask, callback1, callback2)
1559 * Register a local client with IrLMP
1560 * First callback is selective discovery (based on hints)
1561 * Second callback is for selective discovery expiries
1563 * Returns: handle > 0 on success, 0 on error
1565 void *irlmp_register_client(__u16 hint_mask
, DISCOVERY_CALLBACK1 disco_clb
,
1566 DISCOVERY_CALLBACK2 expir_clb
, void *priv
)
1568 irlmp_client_t
*client
;
1570 IRDA_DEBUG(1, "%s()\n", __func__
);
1571 IRDA_ASSERT(irlmp
!= NULL
, return NULL
;);
1573 /* Make a new registration */
1574 client
= kmalloc(sizeof(irlmp_client_t
), GFP_ATOMIC
);
1576 IRDA_DEBUG( 1, "%s(), Unable to kmalloc!\n", __func__
);
1580 /* Register the details */
1581 client
->hint_mask
.word
= hint_mask
;
1582 client
->disco_callback
= disco_clb
;
1583 client
->expir_callback
= expir_clb
;
1584 client
->priv
= priv
;
1586 hashbin_insert(irlmp
->clients
, (irda_queue_t
*) client
,
1587 (long) client
, NULL
);
1589 return (void *) client
;
1591 EXPORT_SYMBOL(irlmp_register_client
);
1594 * Function irlmp_update_client (handle, hint_mask, callback1, callback2)
1596 * Updates specified client (handle) with possibly new hint_mask and
1599 * Returns: 0 on success, -1 on error
1601 int irlmp_update_client(void *handle
, __u16 hint_mask
,
1602 DISCOVERY_CALLBACK1 disco_clb
,
1603 DISCOVERY_CALLBACK2 expir_clb
, void *priv
)
1605 irlmp_client_t
*client
;
1610 client
= hashbin_lock_find(irlmp
->clients
, (long) handle
, NULL
);
1612 IRDA_DEBUG(1, "%s(), Unknown client!\n", __func__
);
1616 client
->hint_mask
.word
= hint_mask
;
1617 client
->disco_callback
= disco_clb
;
1618 client
->expir_callback
= expir_clb
;
1619 client
->priv
= priv
;
1623 EXPORT_SYMBOL(irlmp_update_client
);
1626 * Function irlmp_unregister_client (handle)
1628 * Returns: 0 on success, -1 on error
1631 int irlmp_unregister_client(void *handle
)
1633 struct irlmp_client
*client
;
1635 IRDA_DEBUG(4, "%s()\n", __func__
);
1640 /* Caller may call with invalid handle (it's legal) - Jean II */
1641 client
= hashbin_lock_find(irlmp
->clients
, (long) handle
, NULL
);
1643 IRDA_DEBUG(1, "%s(), Unknown client!\n", __func__
);
1647 IRDA_DEBUG(4, "%s(), removing client!\n", __func__
);
1648 hashbin_remove_this(irlmp
->clients
, (irda_queue_t
*) client
);
1653 EXPORT_SYMBOL(irlmp_unregister_client
);
1656 * Function irlmp_slsap_inuse (slsap)
1658 * Check if the given source LSAP selector is in use
1660 * This function is clearly not very efficient. On the mitigating side, the
1661 * stack make sure that in 99% of the cases, we are called only once
1662 * for each socket allocation. We could probably keep a bitmap
1663 * of the allocated LSAP, but I'm not sure the complexity is worth it.
1666 static int irlmp_slsap_inuse(__u8 slsap_sel
)
1668 struct lsap_cb
*self
;
1670 unsigned long flags
;
1672 IRDA_ASSERT(irlmp
!= NULL
, return TRUE
;);
1673 IRDA_ASSERT(irlmp
->magic
== LMP_MAGIC
, return TRUE
;);
1674 IRDA_ASSERT(slsap_sel
!= LSAP_ANY
, return TRUE
;);
1676 IRDA_DEBUG(4, "%s()\n", __func__
);
1678 #ifdef CONFIG_IRDA_ULTRA
1679 /* Accept all bindings to the connectionless LSAP */
1680 if (slsap_sel
== LSAP_CONNLESS
)
1682 #endif /* CONFIG_IRDA_ULTRA */
1684 /* Valid values are between 0 and 127 (0x0-0x6F) */
1685 if (slsap_sel
> LSAP_MAX
)
1689 * Check if slsap is already in use. To do this we have to loop over
1690 * every IrLAP connection and check every LSAP associated with each
1693 spin_lock_irqsave_nested(&irlmp
->links
->hb_spinlock
, flags
,
1694 SINGLE_DEPTH_NESTING
);
1695 lap
= (struct lap_cb
*) hashbin_get_first(irlmp
->links
);
1696 while (lap
!= NULL
) {
1697 IRDA_ASSERT(lap
->magic
== LMP_LAP_MAGIC
, goto errlap
;);
1699 /* Careful for priority inversions here !
1700 * irlmp->links is never taken while another IrDA
1701 * spinlock is held, so we are safe. Jean II */
1702 spin_lock(&lap
->lsaps
->hb_spinlock
);
1704 /* For this IrLAP, check all the LSAPs */
1705 self
= (struct lsap_cb
*) hashbin_get_first(lap
->lsaps
);
1706 while (self
!= NULL
) {
1707 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
,
1710 if ((self
->slsap_sel
== slsap_sel
)) {
1711 IRDA_DEBUG(4, "Source LSAP selector=%02x in use\n",
1715 self
= (struct lsap_cb
*) hashbin_get_next(lap
->lsaps
);
1717 spin_unlock(&lap
->lsaps
->hb_spinlock
);
1720 lap
= (struct lap_cb
*) hashbin_get_next(irlmp
->links
);
1722 spin_unlock_irqrestore(&irlmp
->links
->hb_spinlock
, flags
);
1725 * Server sockets are typically waiting for connections and
1726 * therefore reside in the unconnected list. We don't want
1727 * to give out their LSAPs for obvious reasons...
1730 spin_lock_irqsave(&irlmp
->unconnected_lsaps
->hb_spinlock
, flags
);
1732 self
= (struct lsap_cb
*) hashbin_get_first(irlmp
->unconnected_lsaps
);
1733 while (self
!= NULL
) {
1734 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, goto erruncon
;);
1735 if ((self
->slsap_sel
== slsap_sel
)) {
1736 IRDA_DEBUG(4, "Source LSAP selector=%02x in use (unconnected)\n",
1740 self
= (struct lsap_cb
*) hashbin_get_next(irlmp
->unconnected_lsaps
);
1742 spin_unlock_irqrestore(&irlmp
->unconnected_lsaps
->hb_spinlock
, flags
);
1746 /* Error exit from within one of the two nested loops.
1747 * Make sure we release the right spinlock in the righ order.
1750 spin_unlock(&lap
->lsaps
->hb_spinlock
);
1751 IRDA_ASSERT_LABEL(errlap
:)
1752 spin_unlock_irqrestore(&irlmp
->links
->hb_spinlock
, flags
);
1755 /* Error exit from within the unconnected loop.
1756 * Just one spinlock to release... Jean II */
1758 spin_unlock_irqrestore(&irlmp
->unconnected_lsaps
->hb_spinlock
, flags
);
1763 * Function irlmp_find_free_slsap ()
1765 * Find a free source LSAP to use. This function is called if the service
1766 * user has requested a source LSAP equal to LM_ANY
1768 static __u8
irlmp_find_free_slsap(void)
1773 IRDA_ASSERT(irlmp
!= NULL
, return -1;);
1774 IRDA_ASSERT(irlmp
->magic
== LMP_MAGIC
, return -1;);
1776 /* Most users don't really care which LSAPs they are given,
1777 * and therefore we automatically give them a free LSAP.
1778 * This function try to find a suitable LSAP, i.e. which is
1779 * not in use and is within the acceptable range. Jean II */
1782 /* Always increment to LSAP number before using it.
1783 * In theory, we could reuse the last LSAP number, as long
1784 * as it is no longer in use. Some IrDA stack do that.
1785 * However, the previous socket may be half closed, i.e.
1786 * we closed it, we think it's no longer in use, but the
1787 * other side did not receive our close and think it's
1788 * active and still send data on it.
1789 * This is similar to what is done with PIDs and TCP ports.
1790 * Also, this reduce the number of calls to irlmp_slsap_inuse()
1791 * which is an expensive function to call.
1793 irlmp
->last_lsap_sel
++;
1795 /* Check if we need to wraparound (0x70-0x7f are reserved) */
1796 if (irlmp
->last_lsap_sel
> LSAP_MAX
) {
1797 /* 0x00-0x10 are also reserved for well know ports */
1798 irlmp
->last_lsap_sel
= 0x10;
1800 /* Make sure we terminate the loop */
1802 IRDA_ERROR("%s: no more free LSAPs !\n",
1808 /* If the LSAP is in use, try the next one.
1809 * Despite the autoincrement, we need to check if the lsap
1810 * is really in use or not, first because LSAP may be
1811 * directly allocated in irlmp_open_lsap(), and also because
1812 * we may wraparound on old sockets. Jean II */
1813 } while (irlmp_slsap_inuse(irlmp
->last_lsap_sel
));
1816 lsap_sel
= irlmp
->last_lsap_sel
;
1817 IRDA_DEBUG(4, "%s(), found free lsap_sel=%02x\n",
1818 __func__
, lsap_sel
);
1824 * Function irlmp_convert_lap_reason (lap_reason)
1826 * Converts IrLAP disconnect reason codes to IrLMP disconnect reason
1830 LM_REASON
irlmp_convert_lap_reason( LAP_REASON lap_reason
)
1832 int reason
= LM_LAP_DISCONNECT
;
1834 switch (lap_reason
) {
1835 case LAP_DISC_INDICATION
: /* Received a disconnect request from peer */
1836 IRDA_DEBUG( 1, "%s(), LAP_DISC_INDICATION\n", __func__
);
1837 reason
= LM_USER_REQUEST
;
1839 case LAP_NO_RESPONSE
: /* To many retransmits without response */
1840 IRDA_DEBUG( 1, "%s(), LAP_NO_RESPONSE\n", __func__
);
1841 reason
= LM_LAP_DISCONNECT
;
1843 case LAP_RESET_INDICATION
:
1844 IRDA_DEBUG( 1, "%s(), LAP_RESET_INDICATION\n", __func__
);
1845 reason
= LM_LAP_RESET
;
1847 case LAP_FOUND_NONE
:
1848 case LAP_MEDIA_BUSY
:
1849 case LAP_PRIMARY_CONFLICT
:
1850 IRDA_DEBUG(1, "%s(), LAP_FOUND_NONE, LAP_MEDIA_BUSY or LAP_PRIMARY_CONFLICT\n", __func__
);
1851 reason
= LM_CONNECT_FAILURE
;
1854 IRDA_DEBUG(1, "%s(), Unknown IrLAP disconnect reason %d!\n",
1855 __func__
, lap_reason
);
1856 reason
= LM_LAP_DISCONNECT
;
1863 #ifdef CONFIG_PROC_FS
1865 struct irlmp_iter_state
{
1869 #define LSAP_START_TOKEN ((void *)1)
1870 #define LINK_START_TOKEN ((void *)2)
1872 static void *irlmp_seq_hb_idx(struct irlmp_iter_state
*iter
, loff_t
*off
)
1876 spin_lock_irq(&iter
->hashbin
->hb_spinlock
);
1877 for (element
= hashbin_get_first(iter
->hashbin
);
1879 element
= hashbin_get_next(iter
->hashbin
)) {
1880 if (!off
|| *off
-- == 0) {
1881 /* NB: hashbin left locked */
1885 spin_unlock_irq(&iter
->hashbin
->hb_spinlock
);
1886 iter
->hashbin
= NULL
;
1891 static void *irlmp_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1893 struct irlmp_iter_state
*iter
= seq
->private;
1897 iter
->hashbin
= NULL
;
1899 return LSAP_START_TOKEN
;
1901 iter
->hashbin
= irlmp
->unconnected_lsaps
;
1902 v
= irlmp_seq_hb_idx(iter
, &off
);
1907 return LINK_START_TOKEN
;
1909 iter
->hashbin
= irlmp
->links
;
1910 return irlmp_seq_hb_idx(iter
, &off
);
1913 static void *irlmp_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1915 struct irlmp_iter_state
*iter
= seq
->private;
1919 if (v
== LSAP_START_TOKEN
) { /* start of list of lsaps */
1920 iter
->hashbin
= irlmp
->unconnected_lsaps
;
1921 v
= irlmp_seq_hb_idx(iter
, NULL
);
1922 return v
? v
: LINK_START_TOKEN
;
1925 if (v
== LINK_START_TOKEN
) { /* start of list of links */
1926 iter
->hashbin
= irlmp
->links
;
1927 return irlmp_seq_hb_idx(iter
, NULL
);
1930 v
= hashbin_get_next(iter
->hashbin
);
1932 if (v
== NULL
) { /* no more in this hash bin */
1933 spin_unlock_irq(&iter
->hashbin
->hb_spinlock
);
1935 if (iter
->hashbin
== irlmp
->unconnected_lsaps
)
1936 v
= LINK_START_TOKEN
;
1938 iter
->hashbin
= NULL
;
1943 static void irlmp_seq_stop(struct seq_file
*seq
, void *v
)
1945 struct irlmp_iter_state
*iter
= seq
->private;
1948 spin_unlock_irq(&iter
->hashbin
->hb_spinlock
);
1951 static int irlmp_seq_show(struct seq_file
*seq
, void *v
)
1953 const struct irlmp_iter_state
*iter
= seq
->private;
1954 struct lsap_cb
*self
= v
;
1956 if (v
== LSAP_START_TOKEN
)
1957 seq_puts(seq
, "Unconnected LSAPs:\n");
1958 else if (v
== LINK_START_TOKEN
)
1959 seq_puts(seq
, "\nRegistered Link Layers:\n");
1960 else if (iter
->hashbin
== irlmp
->unconnected_lsaps
) {
1962 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
, return -EINVAL
; );
1963 seq_printf(seq
, "lsap state: %s, ",
1964 irlsap_state
[ self
->lsap_state
]);
1966 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1967 self
->slsap_sel
, self
->dlsap_sel
);
1968 seq_printf(seq
, "(%s)", self
->notify
.name
);
1969 seq_printf(seq
, "\n");
1970 } else if (iter
->hashbin
== irlmp
->links
) {
1971 struct lap_cb
*lap
= v
;
1973 seq_printf(seq
, "lap state: %s, ",
1974 irlmp_state
[lap
->lap_state
]);
1976 seq_printf(seq
, "saddr: %#08x, daddr: %#08x, ",
1977 lap
->saddr
, lap
->daddr
);
1978 seq_printf(seq
, "num lsaps: %d",
1979 HASHBIN_GET_SIZE(lap
->lsaps
));
1980 seq_printf(seq
, "\n");
1982 /* Careful for priority inversions here !
1983 * All other uses of attrib spinlock are independent of
1984 * the object spinlock, so we are safe. Jean II */
1985 spin_lock(&lap
->lsaps
->hb_spinlock
);
1987 seq_printf(seq
, "\n Connected LSAPs:\n");
1988 for (self
= (struct lsap_cb
*) hashbin_get_first(lap
->lsaps
);
1990 self
= (struct lsap_cb
*)hashbin_get_next(lap
->lsaps
)) {
1991 IRDA_ASSERT(self
->magic
== LMP_LSAP_MAGIC
,
1993 seq_printf(seq
, " lsap state: %s, ",
1994 irlsap_state
[ self
->lsap_state
]);
1996 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1997 self
->slsap_sel
, self
->dlsap_sel
);
1998 seq_printf(seq
, "(%s)", self
->notify
.name
);
1999 seq_putc(seq
, '\n');
2002 IRDA_ASSERT_LABEL(outloop
:)
2003 spin_unlock(&lap
->lsaps
->hb_spinlock
);
2004 seq_putc(seq
, '\n');
2011 static const struct seq_operations irlmp_seq_ops
= {
2012 .start
= irlmp_seq_start
,
2013 .next
= irlmp_seq_next
,
2014 .stop
= irlmp_seq_stop
,
2015 .show
= irlmp_seq_show
,
2018 static int irlmp_seq_open(struct inode
*inode
, struct file
*file
)
2020 IRDA_ASSERT(irlmp
!= NULL
, return -EINVAL
;);
2022 return seq_open_private(file
, &irlmp_seq_ops
,
2023 sizeof(struct irlmp_iter_state
));
2026 const struct file_operations irlmp_seq_fops
= {
2027 .owner
= THIS_MODULE
,
2028 .open
= irlmp_seq_open
,
2030 .llseek
= seq_lseek
,
2031 .release
= seq_release_private
,
2034 #endif /* PROC_FS */