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/config.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/skbuff.h>
32 #include <linux/types.h>
33 #include <linux/proc_fs.h>
34 #include <linux/init.h>
35 #include <linux/kmod.h>
36 #include <linux/random.h>
37 #include <linux/seq_file.h>
39 #include <net/irda/irda.h>
40 #include <net/irda/timer.h>
41 #include <net/irda/qos.h>
42 #include <net/irda/irlap.h>
43 #include <net/irda/iriap.h>
44 #include <net/irda/irlmp.h>
45 #include <net/irda/irlmp_frame.h>
47 /* Master structure */
48 struct irlmp_cb *irlmp = NULL;
50 /* These can be altered by the sysctl interface */
51 int sysctl_discovery = 0;
52 int sysctl_discovery_timeout = 3; /* 3 seconds by default */
53 EXPORT_SYMBOL(sysctl_discovery_timeout);
54 int sysctl_discovery_slots = 6; /* 6 slots by default */
55 int sysctl_lap_keepalive_time = LM_IDLE_TIMEOUT * 1000 / HZ;
56 char sysctl_devname[65];
58 const char *irlmp_reasons[] = {
67 EXPORT_SYMBOL(irlmp_reasons);
70 * Function irlmp_init (void)
72 * Create (allocate) the main IrLMP structure
75 int __init irlmp_init(void)
77 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
78 /* Initialize the irlmp structure. */
79 irlmp = kmalloc( sizeof(struct irlmp_cb), GFP_KERNEL);
82 memset(irlmp, 0, sizeof(struct irlmp_cb));
84 irlmp->magic = LMP_MAGIC;
86 irlmp->clients = hashbin_new(HB_LOCK);
87 irlmp->services = hashbin_new(HB_LOCK);
88 irlmp->links = hashbin_new(HB_LOCK);
89 irlmp->unconnected_lsaps = hashbin_new(HB_LOCK);
90 irlmp->cachelog = hashbin_new(HB_NOLOCK);
92 if ((irlmp->clients == NULL) ||
93 (irlmp->services == NULL) ||
94 (irlmp->links == NULL) ||
95 (irlmp->unconnected_lsaps == NULL) ||
96 (irlmp->cachelog == NULL)) {
100 spin_lock_init(&irlmp->cachelog->hb_spinlock);
102 irlmp->free_lsap_sel = 0x10; /* Reserved 0x00-0x0f */
103 strcpy(sysctl_devname, "Linux");
105 /* Do discovery every 3 seconds */
106 init_timer(&irlmp->discovery_timer);
107 irlmp_start_discovery_timer(irlmp, sysctl_discovery_timeout*HZ);
113 * Function irlmp_cleanup (void)
118 void __exit irlmp_cleanup(void)
120 /* Check for main structure */
121 ASSERT(irlmp != NULL, return;);
122 ASSERT(irlmp->magic == LMP_MAGIC, return;);
124 del_timer(&irlmp->discovery_timer);
126 hashbin_delete(irlmp->links, (FREE_FUNC) kfree);
127 hashbin_delete(irlmp->unconnected_lsaps, (FREE_FUNC) kfree);
128 hashbin_delete(irlmp->clients, (FREE_FUNC) kfree);
129 hashbin_delete(irlmp->services, (FREE_FUNC) kfree);
130 hashbin_delete(irlmp->cachelog, (FREE_FUNC) kfree);
132 /* De-allocate main structure */
138 * Function irlmp_open_lsap (slsap, notify)
140 * Register with IrLMP and create a local LSAP,
141 * returns handle to LSAP.
143 struct lsap_cb *irlmp_open_lsap(__u8 slsap_sel, notify_t *notify, __u8 pid)
145 struct lsap_cb *self;
147 ASSERT(notify != NULL, return NULL;);
148 ASSERT(irlmp != NULL, return NULL;);
149 ASSERT(irlmp->magic == LMP_MAGIC, return NULL;);
150 ASSERT(notify->instance != NULL, return NULL;);
152 /* Does the client care which Source LSAP selector it gets? */
153 if (slsap_sel == LSAP_ANY) {
154 slsap_sel = irlmp_find_free_slsap();
157 } else if (irlmp_slsap_inuse(slsap_sel))
160 /* Allocate new instance of a LSAP connection */
161 self = kmalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
163 ERROR("%s: can't allocate memory", __FUNCTION__);
166 memset(self, 0, sizeof(struct lsap_cb));
168 self->magic = LMP_LSAP_MAGIC;
169 self->slsap_sel = slsap_sel;
171 /* Fix connectionless LSAP's */
172 if (slsap_sel == LSAP_CONNLESS) {
173 #ifdef CONFIG_IRDA_ULTRA
174 self->dlsap_sel = LSAP_CONNLESS;
176 #endif /* CONFIG_IRDA_ULTRA */
178 self->dlsap_sel = LSAP_ANY;
179 /* self->connected = FALSE; -> already NULL via memset() */
181 init_timer(&self->watchdog_timer);
183 self->notify = *notify;
185 self->lsap_state = LSAP_DISCONNECTED;
187 /* Insert into queue of unconnected LSAPs */
188 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
193 EXPORT_SYMBOL(irlmp_open_lsap);
196 * Function __irlmp_close_lsap (self)
198 * Remove an instance of LSAP
200 static void __irlmp_close_lsap(struct lsap_cb *self)
202 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
204 ASSERT(self != NULL, return;);
205 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
208 * Set some of the variables to preset values
211 del_timer(&self->watchdog_timer); /* Important! */
214 dev_kfree_skb(self->conn_skb);
220 * Function irlmp_close_lsap (self)
222 * Close and remove LSAP
225 void irlmp_close_lsap(struct lsap_cb *self)
228 struct lsap_cb *lsap = NULL;
230 ASSERT(self != NULL, return;);
231 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
234 * Find out if we should remove this LSAP from a link or from the
235 * list of unconnected lsaps (not associated with a link)
239 ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
240 /* We might close a LSAP before it has completed the
241 * connection setup. In those case, higher layers won't
242 * send a proper disconnect request. Harmless, except
243 * that we will forget to close LAP... - Jean II */
244 if(self->lsap_state != LSAP_DISCONNECTED) {
245 self->lsap_state = LSAP_DISCONNECTED;
246 irlmp_do_lap_event(self->lap,
247 LM_LAP_DISCONNECT_REQUEST, NULL);
249 /* Now, remove from the link */
250 lsap = hashbin_remove(lap->lsaps, (long) self, NULL);
251 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
252 lap->cache.valid = FALSE;
256 /* Check if we found the LSAP! If not then try the unconnected lsaps */
258 lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self,
263 "%s(), Looks like somebody has removed me already!\n",
267 __irlmp_close_lsap(self);
269 EXPORT_SYMBOL(irlmp_close_lsap);
272 * Function irlmp_register_irlap (saddr, notify)
274 * Register IrLAP layer with IrLMP. There is possible to have multiple
275 * instances of the IrLAP layer, each connected to different IrDA ports
278 void irlmp_register_link(struct irlap_cb *irlap, __u32 saddr, notify_t *notify)
282 ASSERT(irlmp != NULL, return;);
283 ASSERT(irlmp->magic == LMP_MAGIC, return;);
284 ASSERT(notify != NULL, return;);
287 * Allocate new instance of a LSAP connection
289 lap = kmalloc(sizeof(struct lap_cb), GFP_KERNEL);
291 ERROR("%s: unable to kmalloc\n", __FUNCTION__);
294 memset(lap, 0, sizeof(struct lap_cb));
297 lap->magic = LMP_LAP_MAGIC;
299 lap->daddr = DEV_ADDR_ANY;
300 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
301 lap->cache.valid = FALSE;
303 lap->lsaps = hashbin_new(HB_LOCK);
304 if (lap->lsaps == NULL) {
305 WARNING("%s(), unable to kmalloc lsaps\n", __FUNCTION__);
310 lap->lap_state = LAP_STANDBY;
312 init_timer(&lap->idle_timer);
315 * Insert into queue of LMP links
317 hashbin_insert(irlmp->links, (irda_queue_t *) lap, lap->saddr, NULL);
320 * We set only this variable so IrLAP can tell us on which link the
321 * different events happened on
323 irda_notify_init(notify);
324 notify->instance = lap;
328 * Function irlmp_unregister_irlap (saddr)
330 * IrLAP layer has been removed!
333 void irlmp_unregister_link(__u32 saddr)
337 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
339 /* We must remove ourselves from the hashbin *first*. This ensure
340 * that no more LSAPs will be open on this link and no discovery
341 * will be triggered anymore. Jean II */
342 link = hashbin_remove(irlmp->links, saddr, NULL);
344 ASSERT(link->magic == LMP_LAP_MAGIC, return;);
346 /* Kill all the LSAPs on this link. Jean II */
347 link->reason = LAP_DISC_INDICATION;
348 link->daddr = DEV_ADDR_ANY;
349 irlmp_do_lap_event(link, LM_LAP_DISCONNECT_INDICATION, NULL);
351 /* Remove all discoveries discovered at this link */
352 irlmp_expire_discoveries(irlmp->cachelog, link->saddr, TRUE);
355 del_timer(&link->idle_timer);
362 * Function irlmp_connect_request (handle, dlsap, userdata)
364 * Connect with a peer LSAP
367 int irlmp_connect_request(struct lsap_cb *self, __u8 dlsap_sel,
368 __u32 saddr, __u32 daddr,
369 struct qos_info *qos, struct sk_buff *userdata)
371 struct sk_buff *tx_skb = userdata;
373 struct lsap_cb *lsap;
376 ASSERT(self != NULL, return -EBADR;);
377 ASSERT(self->magic == LMP_LSAP_MAGIC, return -EBADR;);
380 "%s(), slsap_sel=%02x, dlsap_sel=%02x, saddr=%08x, daddr=%08x\n",
381 __FUNCTION__, self->slsap_sel, dlsap_sel, saddr, daddr);
383 if (test_bit(0, &self->connected)) {
388 /* Client must supply destination device address */
395 if (tx_skb == NULL) {
396 tx_skb = dev_alloc_skb(64);
400 skb_reserve(tx_skb, LMP_MAX_HEADER);
403 /* Make room for MUX control header (3 bytes) */
404 ASSERT(skb_headroom(tx_skb) >= LMP_CONTROL_HEADER, return -1;);
405 skb_push(tx_skb, LMP_CONTROL_HEADER);
407 self->dlsap_sel = dlsap_sel;
410 * Find the link to where we should try to connect since there may
411 * be more than one IrDA port on this machine. If the client has
412 * passed us the saddr (and already knows which link to use), then
413 * we use that to find the link, if not then we have to look in the
414 * discovery log and check if any of the links has discovered a
415 * device with the given daddr
417 if ((!saddr) || (saddr == DEV_ADDR_ANY)) {
418 discovery_t *discovery;
421 spin_lock_irqsave(&irlmp->cachelog->hb_spinlock, flags);
422 if (daddr != DEV_ADDR_ANY)
423 discovery = hashbin_find(irlmp->cachelog, daddr, NULL);
425 IRDA_DEBUG(2, "%s(), no daddr\n", __FUNCTION__);
426 discovery = (discovery_t *)
427 hashbin_get_first(irlmp->cachelog);
431 saddr = discovery->data.saddr;
432 daddr = discovery->data.daddr;
434 spin_unlock_irqrestore(&irlmp->cachelog->hb_spinlock, flags);
436 lap = hashbin_lock_find(irlmp->links, saddr, NULL);
438 IRDA_DEBUG(1, "%s(), Unable to find a usable link!\n", __FUNCTION__);
443 /* Check if LAP is disconnected or already connected */
444 if (lap->daddr == DEV_ADDR_ANY)
446 else if (lap->daddr != daddr) {
447 /* Check if some LSAPs are active on this LAP */
448 if (HASHBIN_GET_SIZE(lap->lsaps) == 0) {
449 /* No active connection, but LAP hasn't been
450 * disconnected yet (waiting for timeout in LAP).
451 * Maybe we could give LAP a bit of help in this case.
453 IRDA_DEBUG(0, "%s(), sorry, but I'm waiting for LAP to timeout!\n", __FUNCTION__);
458 /* LAP is already connected to a different node, and LAP
459 * can only talk to one node at a time */
460 IRDA_DEBUG(0, "%s(), sorry, but link is busy!\n", __FUNCTION__);
468 * Remove LSAP from list of unconnected LSAPs and insert it into the
469 * list of connected LSAPs for the particular link
471 lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self, NULL);
473 ASSERT(lsap != NULL, return -1;);
474 ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
475 ASSERT(lsap->lap != NULL, return -1;);
476 ASSERT(lsap->lap->magic == LMP_LAP_MAGIC, return -1;);
478 hashbin_insert(self->lap->lsaps, (irda_queue_t *) self, (long) self,
481 set_bit(0, &self->connected); /* TRUE */
484 * User supplied qos specifications?
489 irlmp_do_lsap_event(self, LM_CONNECT_REQUEST, tx_skb);
491 /* Drop reference count - see irlap_data_request(). */
492 dev_kfree_skb(tx_skb);
499 dev_kfree_skb(tx_skb);
502 EXPORT_SYMBOL(irlmp_connect_request);
505 * Function irlmp_connect_indication (self)
507 * Incoming connection
510 void irlmp_connect_indication(struct lsap_cb *self, struct sk_buff *skb)
516 ASSERT(self != NULL, return;);
517 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
518 ASSERT(skb != NULL, return;);
519 ASSERT(self->lap != NULL, return;);
521 IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
522 __FUNCTION__, self->slsap_sel, self->dlsap_sel);
524 /* Note : self->lap is set in irlmp_link_data_indication(),
525 * (case CONNECT_CMD:) because we have no way to set it here.
526 * Similarly, self->dlsap_sel is usually set in irlmp_find_lsap().
529 self->qos = *self->lap->qos;
531 max_seg_size = self->lap->qos->data_size.value-LMP_HEADER;
532 lap_header_size = IRLAP_GET_HEADER_SIZE(self->lap->irlap);
533 max_header_size = LMP_HEADER + lap_header_size;
535 /* Hide LMP_CONTROL_HEADER header from layer above */
536 skb_pull(skb, LMP_CONTROL_HEADER);
538 if (self->notify.connect_indication) {
539 /* Don't forget to refcount it - see irlap_driver_rcv(). */
541 self->notify.connect_indication(self->notify.instance, self,
542 &self->qos, max_seg_size,
543 max_header_size, skb);
548 * Function irlmp_connect_response (handle, userdata)
550 * Service user is accepting connection
553 int irlmp_connect_response(struct lsap_cb *self, struct sk_buff *userdata)
555 ASSERT(self != NULL, return -1;);
556 ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
557 ASSERT(userdata != NULL, return -1;);
559 /* We set the connected bit and move the lsap to the connected list
560 * in the state machine itself. Jean II */
562 IRDA_DEBUG(2, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
563 __FUNCTION__, self->slsap_sel, self->dlsap_sel);
565 /* Make room for MUX control header (3 bytes) */
566 ASSERT(skb_headroom(userdata) >= LMP_CONTROL_HEADER, return -1;);
567 skb_push(userdata, LMP_CONTROL_HEADER);
569 irlmp_do_lsap_event(self, LM_CONNECT_RESPONSE, userdata);
571 /* Drop reference count - see irlap_data_request(). */
572 dev_kfree_skb(userdata);
576 EXPORT_SYMBOL(irlmp_connect_response);
579 * Function irlmp_connect_confirm (handle, skb)
581 * LSAP connection confirmed peer device!
583 void irlmp_connect_confirm(struct lsap_cb *self, struct sk_buff *skb)
589 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
591 ASSERT(skb != NULL, return;);
592 ASSERT(self != NULL, return;);
593 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
594 ASSERT(self->lap != NULL, return;);
596 self->qos = *self->lap->qos;
598 max_seg_size = self->lap->qos->data_size.value-LMP_HEADER;
599 lap_header_size = IRLAP_GET_HEADER_SIZE(self->lap->irlap);
600 max_header_size = LMP_HEADER + lap_header_size;
602 IRDA_DEBUG(2, "%s(), max_header_size=%d\n",
603 __FUNCTION__, max_header_size);
605 /* Hide LMP_CONTROL_HEADER header from layer above */
606 skb_pull(skb, LMP_CONTROL_HEADER);
608 if (self->notify.connect_confirm) {
609 /* Don't forget to refcount it - see irlap_driver_rcv() */
611 self->notify.connect_confirm(self->notify.instance, self,
612 &self->qos, max_seg_size,
613 max_header_size, skb);
618 * Function irlmp_dup (orig, instance)
620 * Duplicate LSAP, can be used by servers to confirm a connection on a
621 * new LSAP so it can keep listening on the old one.
624 struct lsap_cb *irlmp_dup(struct lsap_cb *orig, void *instance)
629 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
631 spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
633 /* Only allowed to duplicate unconnected LSAP's, and only LSAPs
634 * that have received a connect indication. Jean II */
635 if ((!hashbin_find(irlmp->unconnected_lsaps, (long) orig, NULL)) ||
636 (orig->lap == NULL)) {
637 IRDA_DEBUG(0, "%s(), invalid LSAP (wrong state)\n",
639 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
644 /* Allocate a new instance */
645 new = kmalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
647 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
648 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
653 memcpy(new, orig, sizeof(struct lsap_cb));
654 /* new->lap = orig->lap; => done in the memcpy() */
655 /* new->slsap_sel = orig->slsap_sel; => done in the memcpy() */
656 new->conn_skb = NULL;
658 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
660 /* Not everything is the same */
661 new->notify.instance = instance;
663 init_timer(&new->watchdog_timer);
665 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) new,
668 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
669 /* Make sure that we invalidate the LSAP cache */
670 new->lap->cache.valid = FALSE;
671 #endif /* CONFIG_IRDA_CACHE_LAST_LSAP */
675 EXPORT_SYMBOL(irlmp_dup);
678 * Function irlmp_disconnect_request (handle, userdata)
680 * The service user is requesting disconnection, this will not remove the
681 * LSAP, but only mark it as disconnected
683 int irlmp_disconnect_request(struct lsap_cb *self, struct sk_buff *userdata)
685 struct lsap_cb *lsap;
687 ASSERT(self != NULL, return -1;);
688 ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
689 ASSERT(userdata != NULL, return -1;);
691 /* Already disconnected ?
692 * There is a race condition between irlmp_disconnect_indication()
693 * and us that might mess up the hashbins below. This fixes it.
695 if (! test_and_clear_bit(0, &self->connected)) {
696 IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
697 dev_kfree_skb(userdata);
701 skb_push(userdata, LMP_CONTROL_HEADER);
704 * Do the event before the other stuff since we must know
705 * which lap layer that the frame should be transmitted on
707 irlmp_do_lsap_event(self, LM_DISCONNECT_REQUEST, userdata);
709 /* Drop reference count - see irlap_data_request(). */
710 dev_kfree_skb(userdata);
713 * Remove LSAP from list of connected LSAPs for the particular link
714 * and insert it into the list of unconnected LSAPs
716 ASSERT(self->lap != NULL, return -1;);
717 ASSERT(self->lap->magic == LMP_LAP_MAGIC, return -1;);
718 ASSERT(self->lap->lsaps != NULL, return -1;);
720 lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
721 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
722 self->lap->cache.valid = FALSE;
725 ASSERT(lsap != NULL, return -1;);
726 ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
727 ASSERT(lsap == self, return -1;);
729 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
732 /* Reset some values */
733 self->dlsap_sel = LSAP_ANY;
738 EXPORT_SYMBOL(irlmp_disconnect_request);
741 * Function irlmp_disconnect_indication (reason, userdata)
743 * LSAP is being closed!
745 void irlmp_disconnect_indication(struct lsap_cb *self, LM_REASON reason,
748 struct lsap_cb *lsap;
750 IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, irlmp_reasons[reason]);
751 ASSERT(self != NULL, return;);
752 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
754 IRDA_DEBUG(3, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
755 __FUNCTION__, self->slsap_sel, self->dlsap_sel);
757 /* Already disconnected ?
758 * There is a race condition between irlmp_disconnect_request()
759 * and us that might mess up the hashbins below. This fixes it.
761 if (! test_and_clear_bit(0, &self->connected)) {
762 IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
767 * Remove association between this LSAP and the link it used
769 ASSERT(self->lap != NULL, return;);
770 ASSERT(self->lap->lsaps != NULL, return;);
772 lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
773 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
774 self->lap->cache.valid = FALSE;
777 ASSERT(lsap != NULL, return;);
778 ASSERT(lsap == self, return;);
779 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) lsap,
782 self->dlsap_sel = LSAP_ANY;
786 * Inform service user
788 if (self->notify.disconnect_indication) {
789 /* Don't forget to refcount it - see irlap_driver_rcv(). */
792 self->notify.disconnect_indication(self->notify.instance,
795 IRDA_DEBUG(0, "%s(), no handler\n", __FUNCTION__);
800 * Function irlmp_do_expiry (void)
802 * Do a cleanup of the discovery log (remove old entries)
804 * Note : separate from irlmp_do_discovery() so that we can handle
805 * passive discovery properly.
807 void irlmp_do_expiry()
812 * Expire discovery on all links which are *not* connected.
813 * On links which are connected, we can't do discovery
814 * anymore and can't refresh the log, so we freeze the
815 * discovery log to keep info about the device we are
817 * This info is mandatory if we want irlmp_connect_request()
818 * to work properly. - Jean II
820 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
821 while (lap != NULL) {
822 ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
824 if (lap->lap_state == LAP_STANDBY) {
825 /* Expire discoveries discovered on this link */
826 irlmp_expire_discoveries(irlmp->cachelog, lap->saddr,
829 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
834 * Function irlmp_do_discovery (nslots)
836 * Do some discovery on all links
838 * Note : log expiry is done above.
840 void irlmp_do_discovery(int nslots)
844 /* Make sure the value is sane */
845 if ((nslots != 1) && (nslots != 6) && (nslots != 8) && (nslots != 16)){
846 WARNING("%s: invalid value for number of slots!\n",
848 nslots = sysctl_discovery_slots = 8;
851 /* Construct new discovery info to be used by IrLAP, */
852 u16ho(irlmp->discovery_cmd.data.hints) = irlmp->hints.word;
855 * Set character set for device name (we use ASCII), and
856 * copy device name. Remember to make room for a \0 at the
859 irlmp->discovery_cmd.data.charset = CS_ASCII;
860 strncpy(irlmp->discovery_cmd.data.info, sysctl_devname,
862 irlmp->discovery_cmd.name_len = strlen(irlmp->discovery_cmd.data.info);
863 irlmp->discovery_cmd.nslots = nslots;
866 * Try to send discovery packets on all links
868 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
869 while (lap != NULL) {
870 ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
872 if (lap->lap_state == LAP_STANDBY) {
873 /* Try to discover */
874 irlmp_do_lap_event(lap, LM_LAP_DISCOVERY_REQUEST,
877 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
882 * Function irlmp_discovery_request (nslots)
884 * Do a discovery of devices in front of the computer
886 * If the caller has registered a client discovery callback, this
887 * allow him to receive the full content of the discovery log through
888 * this callback (as normally he will receive only new discoveries).
890 void irlmp_discovery_request(int nslots)
892 /* Return current cached discovery log (in full) */
893 irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_LOG);
896 * Start a single discovery operation if discovery is not already
899 if (!sysctl_discovery) {
900 /* Check if user wants to override the default */
901 if (nslots == DISCOVERY_DEFAULT_SLOTS)
902 nslots = sysctl_discovery_slots;
904 irlmp_do_discovery(nslots);
905 /* Note : we never do expiry here. Expiry will run on the
906 * discovery timer regardless of the state of sysctl_discovery
910 EXPORT_SYMBOL(irlmp_discovery_request);
913 * Function irlmp_get_discoveries (pn, mask, slots)
915 * Return the current discovery log
917 * If discovery is not enabled, you should call this function again
918 * after 1 or 2 seconds (i.e. after discovery has been done).
920 struct irda_device_info *irlmp_get_discoveries(int *pn, __u16 mask, int nslots)
922 /* If discovery is not enabled, it's likely that the discovery log
923 * will be empty. So, we trigger a single discovery, so that next
924 * time the user call us there might be some results in the log.
927 if (!sysctl_discovery) {
928 /* Check if user wants to override the default */
929 if (nslots == DISCOVERY_DEFAULT_SLOTS)
930 nslots = sysctl_discovery_slots;
932 /* Start discovery - will complete sometime later */
933 irlmp_do_discovery(nslots);
934 /* Note : we never do expiry here. Expiry will run on the
935 * discovery timer regardless of the state of sysctl_discovery
939 /* Return current cached discovery log */
940 return(irlmp_copy_discoveries(irlmp->cachelog, pn, mask, TRUE));
942 EXPORT_SYMBOL(irlmp_get_discoveries);
945 * Function irlmp_notify_client (log)
947 * Notify all about discovered devices
949 * Clients registered with IrLMP are :
952 * o Any socket (in any state - ouch, that may be a lot !)
953 * The client may have defined a callback to be notified in case of
954 * partial/selective discovery based on the hints that it passed to IrLMP.
957 irlmp_notify_client(irlmp_client_t *client,
958 hashbin_t *log, DISCOVERY_MODE mode)
960 discinfo_t *discoveries; /* Copy of the discovery log */
961 int number; /* Number of nodes in the log */
964 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
966 /* Check if client wants or not partial/selective log (optimisation) */
967 if (!client->disco_callback)
972 * the old code was manipulating the log directly, which was
973 * very racy. Now, we use copy_discoveries, that protects
974 * itself while dumping the log for us.
975 * The overhead of the copy is compensated by the fact that
976 * we only pass new discoveries in normal mode and don't
977 * pass the same old entry every 3s to the caller as we used
978 * to do (virtual function calling is expensive).
983 * Now, check all discovered devices (if any), and notify client
984 * only about the services that the client is interested in
985 * We also notify only about the new devices unless the caller
986 * explicitly request a dump of the log. Jean II
988 discoveries = irlmp_copy_discoveries(log, &number,
989 client->hint_mask.word,
990 (mode == DISCOVERY_LOG));
991 /* Check if the we got some results */
992 if (discoveries == NULL)
993 return; /* No nodes discovered */
995 /* Pass all entries to the listener */
996 for(i = 0; i < number; i++)
997 client->disco_callback(&(discoveries[i]), mode, client->priv);
999 /* Free up our buffer */
1004 * Function irlmp_discovery_confirm ( self, log)
1006 * Some device(s) answered to our discovery request! Check to see which
1007 * device it is, and give indication to the client(s)
1010 void irlmp_discovery_confirm(hashbin_t *log, DISCOVERY_MODE mode)
1012 irlmp_client_t *client;
1013 irlmp_client_t *client_next;
1015 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1017 ASSERT(log != NULL, return;);
1019 if (!(HASHBIN_GET_SIZE(log)))
1022 /* For each client - notify callback may touch client list */
1023 client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
1024 while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
1025 (void *) &client_next) ) {
1026 /* Check if we should notify client */
1027 irlmp_notify_client(client, log, mode);
1029 client = client_next;
1034 * Function irlmp_discovery_expiry (expiry)
1036 * This device is no longer been discovered, and therefore it is being
1037 * purged from the discovery log. Inform all clients who have
1038 * registered for this event...
1040 * Note : called exclusively from discovery.c
1041 * Note : this is no longer called under discovery spinlock, so the
1042 * client can do whatever he wants in the callback.
1044 void irlmp_discovery_expiry(discinfo_t *expiries, int number)
1046 irlmp_client_t *client;
1047 irlmp_client_t *client_next;
1050 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1052 ASSERT(expiries != NULL, return;);
1054 /* For each client - notify callback may touch client list */
1055 client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
1056 while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
1057 (void *) &client_next) ) {
1059 /* Pass all entries to the listener */
1060 for(i = 0; i < number; i++) {
1061 /* Check if we should notify client */
1062 if ((client->expir_callback) &&
1063 (client->hint_mask.word & u16ho(expiries[i].hints)
1065 client->expir_callback(&(expiries[i]),
1071 client = client_next;
1076 * Function irlmp_get_discovery_response ()
1078 * Used by IrLAP to get the discovery info it needs when answering
1079 * discovery requests by other devices.
1081 discovery_t *irlmp_get_discovery_response()
1083 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1085 ASSERT(irlmp != NULL, return NULL;);
1087 u16ho(irlmp->discovery_rsp.data.hints) = irlmp->hints.word;
1090 * Set character set for device name (we use ASCII), and
1091 * copy device name. Remember to make room for a \0 at the
1094 irlmp->discovery_rsp.data.charset = CS_ASCII;
1096 strncpy(irlmp->discovery_rsp.data.info, sysctl_devname,
1098 irlmp->discovery_rsp.name_len = strlen(irlmp->discovery_rsp.data.info);
1100 return &irlmp->discovery_rsp;
1104 * Function irlmp_data_request (self, skb)
1106 * Send some data to peer device
1108 * Note on skb management :
1109 * After calling the lower layers of the IrDA stack, we always
1110 * kfree() the skb, which drop the reference count (and potentially
1112 * IrLMP and IrLAP may queue the packet, and in those cases will need
1113 * to use skb_get() to keep it around.
1116 int irlmp_data_request(struct lsap_cb *self, struct sk_buff *userdata)
1120 ASSERT(self != NULL, return -1;);
1121 ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
1123 /* Make room for MUX header */
1124 ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
1125 skb_push(userdata, LMP_HEADER);
1127 ret = irlmp_do_lsap_event(self, LM_DATA_REQUEST, userdata);
1129 /* Drop reference count - see irlap_data_request(). */
1130 dev_kfree_skb(userdata);
1134 EXPORT_SYMBOL(irlmp_data_request);
1137 * Function irlmp_data_indication (handle, skb)
1139 * Got data from LAP layer so pass it up to upper layer
1142 void irlmp_data_indication(struct lsap_cb *self, struct sk_buff *skb)
1144 /* Hide LMP header from layer above */
1145 skb_pull(skb, LMP_HEADER);
1147 if (self->notify.data_indication) {
1148 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1150 self->notify.data_indication(self->notify.instance, self, skb);
1155 * Function irlmp_udata_request (self, skb)
1157 int irlmp_udata_request(struct lsap_cb *self, struct sk_buff *userdata)
1161 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1163 ASSERT(userdata != NULL, return -1;);
1165 /* Make room for MUX header */
1166 ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
1167 skb_push(userdata, LMP_HEADER);
1169 ret = irlmp_do_lsap_event(self, LM_UDATA_REQUEST, userdata);
1171 /* Drop reference count - see irlap_data_request(). */
1172 dev_kfree_skb(userdata);
1178 * Function irlmp_udata_indication (self, skb)
1180 * Send unreliable data (but still within the connection)
1183 void irlmp_udata_indication(struct lsap_cb *self, struct sk_buff *skb)
1185 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1187 ASSERT(self != NULL, return;);
1188 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
1189 ASSERT(skb != NULL, return;);
1191 /* Hide LMP header from layer above */
1192 skb_pull(skb, LMP_HEADER);
1194 if (self->notify.udata_indication) {
1195 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1197 self->notify.udata_indication(self->notify.instance, self,
1203 * Function irlmp_connless_data_request (self, skb)
1205 #ifdef CONFIG_IRDA_ULTRA
1206 int irlmp_connless_data_request(struct lsap_cb *self, struct sk_buff *userdata,
1209 struct sk_buff *clone_skb;
1212 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1214 ASSERT(userdata != NULL, return -1;);
1216 /* Make room for MUX and PID header */
1217 ASSERT(skb_headroom(userdata) >= LMP_HEADER+LMP_PID_HEADER,
1220 /* Insert protocol identifier */
1221 skb_push(userdata, LMP_PID_HEADER);
1223 userdata->data[0] = self->pid;
1225 userdata->data[0] = pid;
1227 /* Connectionless sockets must use 0x70 */
1228 skb_push(userdata, LMP_HEADER);
1229 userdata->data[0] = userdata->data[1] = LSAP_CONNLESS;
1231 /* Try to send Connectionless packets out on all links */
1232 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
1233 while (lap != NULL) {
1234 ASSERT(lap->magic == LMP_LAP_MAGIC, return -1;);
1236 clone_skb = skb_clone(userdata, GFP_ATOMIC);
1238 dev_kfree_skb(userdata);
1242 irlap_unitdata_request(lap->irlap, clone_skb);
1243 /* irlap_unitdata_request() don't increase refcount,
1244 * so no dev_kfree_skb() - Jean II */
1246 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
1248 dev_kfree_skb(userdata);
1252 #endif /* CONFIG_IRDA_ULTRA */
1255 * Function irlmp_connless_data_indication (self, skb)
1257 * Receive unreliable data outside any connection. Mostly used by Ultra
1260 #ifdef CONFIG_IRDA_ULTRA
1261 void irlmp_connless_data_indication(struct lsap_cb *self, struct sk_buff *skb)
1263 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1265 ASSERT(self != NULL, return;);
1266 ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
1267 ASSERT(skb != NULL, return;);
1269 /* Hide LMP and PID header from layer above */
1270 skb_pull(skb, LMP_HEADER+LMP_PID_HEADER);
1272 if (self->notify.udata_indication) {
1273 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1275 self->notify.udata_indication(self->notify.instance, self,
1279 #endif /* CONFIG_IRDA_ULTRA */
1281 void irlmp_status_request(void)
1283 IRDA_DEBUG(0, "%s(), Not implemented\n", __FUNCTION__);
1287 * Propagate status indication from LAP to LSAPs (via LMP)
1288 * This don't trigger any change of state in lap_cb, lmp_cb or lsap_cb,
1289 * and the event is stateless, therefore we can bypass both state machines
1290 * and send the event direct to the LSAP user.
1293 void irlmp_status_indication(struct lap_cb *self,
1294 LINK_STATUS link, LOCK_STATUS lock)
1296 struct lsap_cb *next;
1297 struct lsap_cb *curr;
1299 /* Send status_indication to all LSAPs using this link */
1300 curr = (struct lsap_cb *) hashbin_get_first( self->lsaps);
1301 while (NULL != hashbin_find_next(self->lsaps, (long) curr, NULL,
1303 ASSERT(curr->magic == LMP_LSAP_MAGIC, return;);
1305 * Inform service user if he has requested it
1307 if (curr->notify.status_indication != NULL)
1308 curr->notify.status_indication(curr->notify.instance,
1311 IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
1318 * Receive flow control indication from LAP.
1319 * LAP want us to send it one more frame. We implement a simple round
1320 * robin scheduler between the active sockets so that we get a bit of
1321 * fairness. Note that the round robin is far from perfect, but it's
1322 * better than nothing.
1323 * We then poll the selected socket so that we can do synchronous
1324 * refilling of IrLAP (which allow to minimise the number of buffers).
1327 void irlmp_flow_indication(struct lap_cb *self, LOCAL_FLOW flow)
1329 struct lsap_cb *next;
1330 struct lsap_cb *curr;
1333 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
1334 ASSERT(flow == FLOW_START, return;);
1336 /* Get the number of lsap. That's the only safe way to know
1337 * that we have looped around... - Jean II */
1338 lsap_todo = HASHBIN_GET_SIZE(self->lsaps);
1339 IRDA_DEBUG(4, "%s() : %d lsaps to scan\n", __FUNCTION__, lsap_todo);
1341 /* Poll lsap in order until the queue is full or until we
1343 * Most often, the current LSAP will have something to send,
1344 * so we will go through this loop only once. - Jean II */
1345 while((lsap_todo--) &&
1346 (IRLAP_GET_TX_QUEUE_LEN(self->irlap) < LAP_HIGH_THRESHOLD)) {
1347 /* Try to find the next lsap we should poll. */
1348 next = self->flow_next;
1349 /* If we have no lsap, restart from first one */
1351 next = (struct lsap_cb *) hashbin_get_first(self->lsaps);
1352 /* Verify current one and find the next one */
1353 curr = hashbin_find_next(self->lsaps, (long) next, NULL,
1354 (void *) &self->flow_next);
1355 /* Uh-oh... Paranoia */
1358 IRDA_DEBUG(4, "%s() : curr is %p, next was %p and is now %p, still %d to go - queue len = %d\n", __FUNCTION__, curr, next, self->flow_next, lsap_todo, IRLAP_GET_TX_QUEUE_LEN(self->irlap));
1360 /* Inform lsap user that it can send one more packet. */
1361 if (curr->notify.flow_indication != NULL)
1362 curr->notify.flow_indication(curr->notify.instance,
1365 IRDA_DEBUG(1, "%s(), no handler\n", __FUNCTION__);
1371 * Function irlmp_hint_to_service (hint)
1373 * Returns a list of all servics contained in the given hint bits. This
1374 * function assumes that the hint bits have the size of two bytes only
1376 __u8 *irlmp_hint_to_service(__u8 *hint)
1382 * Allocate array to store services in. 16 entries should be safe
1383 * since we currently only support 2 hint bytes
1385 service = kmalloc(16, GFP_ATOMIC);
1387 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1392 IRDA_DEBUG(1, "<None>\n");
1396 if (hint[0] & HINT_PNP)
1397 IRDA_DEBUG(1, "PnP Compatible ");
1398 if (hint[0] & HINT_PDA)
1399 IRDA_DEBUG(1, "PDA/Palmtop ");
1400 if (hint[0] & HINT_COMPUTER)
1401 IRDA_DEBUG(1, "Computer ");
1402 if (hint[0] & HINT_PRINTER) {
1403 IRDA_DEBUG(1, "Printer ");
1404 service[i++] = S_PRINTER;
1406 if (hint[0] & HINT_MODEM)
1407 IRDA_DEBUG(1, "Modem ");
1408 if (hint[0] & HINT_FAX)
1409 IRDA_DEBUG(1, "Fax ");
1410 if (hint[0] & HINT_LAN) {
1411 IRDA_DEBUG(1, "LAN Access ");
1412 service[i++] = S_LAN;
1415 * Test if extension byte exists. This byte will usually be
1416 * there, but this is not really required by the standard.
1419 if (hint[0] & HINT_EXTENSION) {
1420 if (hint[1] & HINT_TELEPHONY) {
1421 IRDA_DEBUG(1, "Telephony ");
1422 service[i++] = S_TELEPHONY;
1423 } if (hint[1] & HINT_FILE_SERVER)
1424 IRDA_DEBUG(1, "File Server ");
1426 if (hint[1] & HINT_COMM) {
1427 IRDA_DEBUG(1, "IrCOMM ");
1428 service[i++] = S_COMM;
1430 if (hint[1] & HINT_OBEX) {
1431 IRDA_DEBUG(1, "IrOBEX ");
1432 service[i++] = S_OBEX;
1435 IRDA_DEBUG(1, "\n");
1437 /* So that client can be notified about any discovery */
1438 service[i++] = S_ANY;
1446 static const __u16 service_hint_mapping[S_END][2] = {
1447 { HINT_PNP, 0 }, /* S_PNP */
1448 { HINT_PDA, 0 }, /* S_PDA */
1449 { HINT_COMPUTER, 0 }, /* S_COMPUTER */
1450 { HINT_PRINTER, 0 }, /* S_PRINTER */
1451 { HINT_MODEM, 0 }, /* S_MODEM */
1452 { HINT_FAX, 0 }, /* S_FAX */
1453 { HINT_LAN, 0 }, /* S_LAN */
1454 { HINT_EXTENSION, HINT_TELEPHONY }, /* S_TELEPHONY */
1455 { HINT_EXTENSION, HINT_COMM }, /* S_COMM */
1456 { HINT_EXTENSION, HINT_OBEX }, /* S_OBEX */
1457 { 0xFF, 0xFF }, /* S_ANY */
1461 * Function irlmp_service_to_hint (service)
1463 * Converts a service type, to a hint bit
1465 * Returns: a 16 bit hint value, with the service bit set
1467 __u16 irlmp_service_to_hint(int service)
1469 __u16_host_order hint;
1471 hint.byte[0] = service_hint_mapping[service][0];
1472 hint.byte[1] = service_hint_mapping[service][1];
1476 EXPORT_SYMBOL(irlmp_service_to_hint);
1479 * Function irlmp_register_service (service)
1481 * Register local service with IrLMP
1484 void *irlmp_register_service(__u16 hints)
1486 irlmp_service_t *service;
1488 IRDA_DEBUG(4, "%s(), hints = %04x\n", __FUNCTION__, hints);
1490 /* Make a new registration */
1491 service = kmalloc(sizeof(irlmp_service_t), GFP_ATOMIC);
1493 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1496 service->hints.word = hints;
1497 hashbin_insert(irlmp->services, (irda_queue_t *) service,
1498 (long) service, NULL);
1500 irlmp->hints.word |= hints;
1502 return (void *)service;
1504 EXPORT_SYMBOL(irlmp_register_service);
1507 * Function irlmp_unregister_service (handle)
1509 * Unregister service with IrLMP.
1511 * Returns: 0 on success, -1 on error
1513 int irlmp_unregister_service(void *handle)
1515 irlmp_service_t *service;
1516 unsigned long flags;
1518 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1523 /* Caller may call with invalid handle (it's legal) - Jean II */
1524 service = hashbin_lock_find(irlmp->services, (long) handle, NULL);
1526 IRDA_DEBUG(1, "%s(), Unknown service!\n", __FUNCTION__);
1530 hashbin_remove_this(irlmp->services, (irda_queue_t *) service);
1533 /* Remove old hint bits */
1534 irlmp->hints.word = 0;
1536 /* Refresh current hint bits */
1537 spin_lock_irqsave(&irlmp->services->hb_spinlock, flags);
1538 service = (irlmp_service_t *) hashbin_get_first(irlmp->services);
1540 irlmp->hints.word |= service->hints.word;
1542 service = (irlmp_service_t *)hashbin_get_next(irlmp->services);
1544 spin_unlock_irqrestore(&irlmp->services->hb_spinlock, flags);
1547 EXPORT_SYMBOL(irlmp_unregister_service);
1550 * Function irlmp_register_client (hint_mask, callback1, callback2)
1552 * Register a local client with IrLMP
1553 * First callback is selective discovery (based on hints)
1554 * Second callback is for selective discovery expiries
1556 * Returns: handle > 0 on success, 0 on error
1558 void *irlmp_register_client(__u16 hint_mask, DISCOVERY_CALLBACK1 disco_clb,
1559 DISCOVERY_CALLBACK2 expir_clb, void *priv)
1561 irlmp_client_t *client;
1563 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1564 ASSERT(irlmp != NULL, return 0;);
1566 /* Make a new registration */
1567 client = kmalloc(sizeof(irlmp_client_t), GFP_ATOMIC);
1569 IRDA_DEBUG( 1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1573 /* Register the details */
1574 client->hint_mask.word = hint_mask;
1575 client->disco_callback = disco_clb;
1576 client->expir_callback = expir_clb;
1577 client->priv = priv;
1579 hashbin_insert(irlmp->clients, (irda_queue_t *) client,
1580 (long) client, NULL);
1582 return (void *) client;
1584 EXPORT_SYMBOL(irlmp_register_client);
1587 * Function irlmp_update_client (handle, hint_mask, callback1, callback2)
1589 * Updates specified client (handle) with possibly new hint_mask and
1592 * Returns: 0 on success, -1 on error
1594 int irlmp_update_client(void *handle, __u16 hint_mask,
1595 DISCOVERY_CALLBACK1 disco_clb,
1596 DISCOVERY_CALLBACK2 expir_clb, void *priv)
1598 irlmp_client_t *client;
1603 client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
1605 IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
1609 client->hint_mask.word = hint_mask;
1610 client->disco_callback = disco_clb;
1611 client->expir_callback = expir_clb;
1612 client->priv = priv;
1616 EXPORT_SYMBOL(irlmp_update_client);
1619 * Function irlmp_unregister_client (handle)
1621 * Returns: 0 on success, -1 on error
1624 int irlmp_unregister_client(void *handle)
1626 struct irlmp_client *client;
1628 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1633 /* Caller may call with invalid handle (it's legal) - Jean II */
1634 client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
1636 IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
1640 IRDA_DEBUG(4, "%s(), removing client!\n", __FUNCTION__);
1641 hashbin_remove_this(irlmp->clients, (irda_queue_t *) client);
1646 EXPORT_SYMBOL(irlmp_unregister_client);
1649 * Function irlmp_slsap_inuse (slsap)
1651 * Check if the given source LSAP selector is in use
1653 int irlmp_slsap_inuse(__u8 slsap_sel)
1655 struct lsap_cb *self;
1657 unsigned long flags;
1659 ASSERT(irlmp != NULL, return TRUE;);
1660 ASSERT(irlmp->magic == LMP_MAGIC, return TRUE;);
1661 ASSERT(slsap_sel != LSAP_ANY, return TRUE;);
1663 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1665 #ifdef CONFIG_IRDA_ULTRA
1666 /* Accept all bindings to the connectionless LSAP */
1667 if (slsap_sel == LSAP_CONNLESS)
1669 #endif /* CONFIG_IRDA_ULTRA */
1671 /* Valid values are between 0 and 127 */
1672 if (slsap_sel > LSAP_MAX)
1676 * Check if slsap is already in use. To do this we have to loop over
1677 * every IrLAP connection and check every LSAP associated with each
1680 spin_lock_irqsave(&irlmp->links->hb_spinlock, flags);
1681 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
1682 while (lap != NULL) {
1683 ASSERT(lap->magic == LMP_LAP_MAGIC, return TRUE;);
1685 /* Careful for priority inversions here !
1686 * All other uses of attrib spinlock are independent of
1687 * the object spinlock, so we are safe. Jean II */
1688 spin_lock(&lap->lsaps->hb_spinlock);
1690 self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
1691 while (self != NULL) {
1692 ASSERT(self->magic == LMP_LSAP_MAGIC, return TRUE;);
1694 if ((self->slsap_sel == slsap_sel)) {
1695 IRDA_DEBUG(4, "Source LSAP selector=%02x in use\n",
1699 self = (struct lsap_cb*) hashbin_get_next(lap->lsaps);
1701 spin_unlock(&lap->lsaps->hb_spinlock);
1703 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
1705 spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
1710 * Function irlmp_find_free_slsap ()
1712 * Find a free source LSAP to use. This function is called if the service
1713 * user has requested a source LSAP equal to LM_ANY
1715 __u8 irlmp_find_free_slsap(void)
1720 ASSERT(irlmp != NULL, return -1;);
1721 ASSERT(irlmp->magic == LMP_MAGIC, return -1;);
1723 lsap_sel = irlmp->free_lsap_sel++;
1725 /* Check if the new free lsap is really free */
1726 while (irlmp_slsap_inuse(irlmp->free_lsap_sel)) {
1727 irlmp->free_lsap_sel++;
1729 /* Check if we need to wraparound (0x70-0x7f are reserved) */
1730 if (irlmp->free_lsap_sel > LSAP_MAX) {
1731 irlmp->free_lsap_sel = 10;
1733 /* Make sure we terminate the loop */
1738 IRDA_DEBUG(4, "%s(), next free lsap_sel=%02x\n",
1739 __FUNCTION__, lsap_sel);
1745 * Function irlmp_convert_lap_reason (lap_reason)
1747 * Converts IrLAP disconnect reason codes to IrLMP disconnect reason
1751 LM_REASON irlmp_convert_lap_reason( LAP_REASON lap_reason)
1753 int reason = LM_LAP_DISCONNECT;
1755 switch (lap_reason) {
1756 case LAP_DISC_INDICATION: /* Received a disconnect request from peer */
1757 IRDA_DEBUG( 1, "%s(), LAP_DISC_INDICATION\n", __FUNCTION__);
1758 reason = LM_USER_REQUEST;
1760 case LAP_NO_RESPONSE: /* To many retransmits without response */
1761 IRDA_DEBUG( 1, "%s(), LAP_NO_RESPONSE\n", __FUNCTION__);
1762 reason = LM_LAP_DISCONNECT;
1764 case LAP_RESET_INDICATION:
1765 IRDA_DEBUG( 1, "%s(), LAP_RESET_INDICATION\n", __FUNCTION__);
1766 reason = LM_LAP_RESET;
1768 case LAP_FOUND_NONE:
1769 case LAP_MEDIA_BUSY:
1770 case LAP_PRIMARY_CONFLICT:
1771 IRDA_DEBUG(1, "%s(), LAP_FOUND_NONE, LAP_MEDIA_BUSY or LAP_PRIMARY_CONFLICT\n", __FUNCTION__);
1772 reason = LM_CONNECT_FAILURE;
1775 IRDA_DEBUG(1, "%s(), Unknow IrLAP disconnect reason %d!\n",
1776 __FUNCTION__, lap_reason);
1777 reason = LM_LAP_DISCONNECT;
1784 #ifdef CONFIG_PROC_FS
1786 struct irlmp_iter_state {
1790 #define LSAP_START_TOKEN ((void *)1)
1791 #define LINK_START_TOKEN ((void *)2)
1793 static void *irlmp_seq_hb_idx(struct irlmp_iter_state *iter, loff_t *off)
1797 spin_lock_irq(&iter->hashbin->hb_spinlock);
1798 for (element = hashbin_get_first(iter->hashbin);
1800 element = hashbin_get_next(iter->hashbin)) {
1801 if (!off || *off-- == 0) {
1802 /* NB: hashbin left locked */
1806 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1807 iter->hashbin = NULL;
1812 static void *irlmp_seq_start(struct seq_file *seq, loff_t *pos)
1814 struct irlmp_iter_state *iter = seq->private;
1818 iter->hashbin = NULL;
1820 return LSAP_START_TOKEN;
1822 iter->hashbin = irlmp->unconnected_lsaps;
1823 v = irlmp_seq_hb_idx(iter, &off);
1828 return LINK_START_TOKEN;
1830 iter->hashbin = irlmp->links;
1831 return irlmp_seq_hb_idx(iter, &off);
1834 static void *irlmp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1836 struct irlmp_iter_state *iter = seq->private;
1840 if (v == LSAP_START_TOKEN) { /* start of list of lsaps */
1841 iter->hashbin = irlmp->unconnected_lsaps;
1842 v = irlmp_seq_hb_idx(iter, NULL);
1843 return v ? v : LINK_START_TOKEN;
1846 if (v == LINK_START_TOKEN) { /* start of list of links */
1847 iter->hashbin = irlmp->links;
1848 return irlmp_seq_hb_idx(iter, NULL);
1851 v = hashbin_get_next(iter->hashbin);
1853 if (v == NULL) { /* no more in this hash bin */
1854 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1856 if (iter->hashbin == irlmp->unconnected_lsaps)
1857 v = LINK_START_TOKEN;
1859 iter->hashbin = NULL;
1864 static void irlmp_seq_stop(struct seq_file *seq, void *v)
1866 struct irlmp_iter_state *iter = seq->private;
1869 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1872 static int irlmp_seq_show(struct seq_file *seq, void *v)
1874 const struct irlmp_iter_state *iter = seq->private;
1875 struct lsap_cb *self = v;
1877 if (v == LSAP_START_TOKEN)
1878 seq_puts(seq, "Unconnected LSAPs:\n");
1879 else if (v == LINK_START_TOKEN)
1880 seq_puts(seq, "\nRegistered Link Layers:\n");
1881 else if (iter->hashbin == irlmp->unconnected_lsaps) {
1883 ASSERT(self->magic == LMP_LSAP_MAGIC, return -EINVAL; );
1884 seq_printf(seq, "lsap state: %s, ",
1885 irlsap_state[ self->lsap_state]);
1887 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1888 self->slsap_sel, self->dlsap_sel);
1889 seq_printf(seq, "(%s)", self->notify.name);
1890 seq_printf(seq, "\n");
1891 } else if (iter->hashbin == irlmp->links) {
1892 struct lap_cb *lap = v;
1894 seq_printf(seq, "lap state: %s, ",
1895 irlmp_state[lap->lap_state]);
1897 seq_printf(seq, "saddr: %#08x, daddr: %#08x, ",
1898 lap->saddr, lap->daddr);
1899 seq_printf(seq, "num lsaps: %d",
1900 HASHBIN_GET_SIZE(lap->lsaps));
1901 seq_printf(seq, "\n");
1903 /* Careful for priority inversions here !
1904 * All other uses of attrib spinlock are independent of
1905 * the object spinlock, so we are safe. Jean II */
1906 spin_lock(&lap->lsaps->hb_spinlock);
1908 seq_printf(seq, "\n Connected LSAPs:\n");
1909 for (self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
1911 self = (struct lsap_cb *)hashbin_get_next(lap->lsaps)) {
1912 ASSERT(self->magic == LMP_LSAP_MAGIC, break;);
1913 seq_printf(seq, " lsap state: %s, ",
1914 irlsap_state[ self->lsap_state]);
1916 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1917 self->slsap_sel, self->dlsap_sel);
1918 seq_printf(seq, "(%s)", self->notify.name);
1919 seq_putc(seq, '\n');
1922 spin_unlock(&lap->lsaps->hb_spinlock);
1923 seq_putc(seq, '\n');
1930 static struct seq_operations irlmp_seq_ops = {
1931 .start = irlmp_seq_start,
1932 .next = irlmp_seq_next,
1933 .stop = irlmp_seq_stop,
1934 .show = irlmp_seq_show,
1937 static int irlmp_seq_open(struct inode *inode, struct file *file)
1939 struct seq_file *seq;
1941 struct irlmp_iter_state *s;
1943 ASSERT(irlmp != NULL, return -EINVAL;);
1945 s = kmalloc(sizeof(*s), GFP_KERNEL);
1949 rc = seq_open(file, &irlmp_seq_ops);
1953 seq = file->private_data;
1962 struct file_operations irlmp_seq_fops = {
1963 .owner = THIS_MODULE,
1964 .open = irlmp_seq_open,
1966 .llseek = seq_lseek,
1967 .release = seq_release_private,
1970 #endif /* PROC_FS */