Kobject: change drivers/cpufreq/cpufreq.c to use kobject_init_and_add
[linux-2.6/sactl.git] / net / irda / irlmp.c
blobf24cb755908e223c9c39eb9dd89d70e4d633dc48
1 /*********************************************************************
3 * Filename: irlmp.c
4 * Version: 1.0
5 * Description: IrDA Link Management Protocol (LMP) layer
6 * Status: Stable.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Aug 17 20:54:32 1997
9 * Modified at: Wed Jan 5 11:26:03 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
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 const char *irlmp_reasons[] = {
62 "ERROR, NOT USED",
63 "LM_USER_REQUEST",
64 "LM_LAP_DISCONNECT",
65 "LM_CONNECT_FAILURE",
66 "LM_LAP_RESET",
67 "LM_INIT_DISCONNECT",
68 "ERROR, NOT USED",
72 * Function irlmp_init (void)
74 * Create (allocate) the main IrLMP structure
77 int __init irlmp_init(void)
79 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
80 /* Initialize the irlmp structure. */
81 irlmp = kzalloc( sizeof(struct irlmp_cb), GFP_KERNEL);
82 if (irlmp == NULL)
83 return -ENOMEM;
85 irlmp->magic = LMP_MAGIC;
87 irlmp->clients = hashbin_new(HB_LOCK);
88 irlmp->services = hashbin_new(HB_LOCK);
89 irlmp->links = hashbin_new(HB_LOCK);
90 irlmp->unconnected_lsaps = hashbin_new(HB_LOCK);
91 irlmp->cachelog = hashbin_new(HB_NOLOCK);
93 if ((irlmp->clients == NULL) ||
94 (irlmp->services == NULL) ||
95 (irlmp->links == NULL) ||
96 (irlmp->unconnected_lsaps == NULL) ||
97 (irlmp->cachelog == NULL)) {
98 return -ENOMEM;
101 spin_lock_init(&irlmp->cachelog->hb_spinlock);
103 irlmp->last_lsap_sel = 0x0f; /* Reserved 0x00-0x0f */
104 strcpy(sysctl_devname, "Linux");
106 /* Do discovery every 3 seconds */
107 init_timer(&irlmp->discovery_timer);
108 irlmp_start_discovery_timer(irlmp, sysctl_discovery_timeout*HZ);
110 return 0;
114 * Function irlmp_cleanup (void)
116 * Remove IrLMP layer
119 void irlmp_cleanup(void)
121 /* Check for main structure */
122 IRDA_ASSERT(irlmp != NULL, return;);
123 IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return;);
125 del_timer(&irlmp->discovery_timer);
127 hashbin_delete(irlmp->links, (FREE_FUNC) kfree);
128 hashbin_delete(irlmp->unconnected_lsaps, (FREE_FUNC) kfree);
129 hashbin_delete(irlmp->clients, (FREE_FUNC) kfree);
130 hashbin_delete(irlmp->services, (FREE_FUNC) kfree);
131 hashbin_delete(irlmp->cachelog, (FREE_FUNC) kfree);
133 /* De-allocate main structure */
134 kfree(irlmp);
135 irlmp = NULL;
139 * Function irlmp_open_lsap (slsap, notify)
141 * Register with IrLMP and create a local LSAP,
142 * returns handle to LSAP.
144 struct lsap_cb *irlmp_open_lsap(__u8 slsap_sel, notify_t *notify, __u8 pid)
146 struct lsap_cb *self;
148 IRDA_ASSERT(notify != NULL, return NULL;);
149 IRDA_ASSERT(irlmp != NULL, return NULL;);
150 IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return NULL;);
151 IRDA_ASSERT(notify->instance != NULL, return NULL;);
153 /* Does the client care which Source LSAP selector it gets? */
154 if (slsap_sel == LSAP_ANY) {
155 slsap_sel = irlmp_find_free_slsap();
156 if (!slsap_sel)
157 return NULL;
158 } else if (irlmp_slsap_inuse(slsap_sel))
159 return NULL;
161 /* Allocate new instance of a LSAP connection */
162 self = kzalloc(sizeof(struct lsap_cb), GFP_ATOMIC);
163 if (self == NULL) {
164 IRDA_ERROR("%s: can't allocate memory\n", __FUNCTION__);
165 return NULL;
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;
175 self->pid = pid;
176 #endif /* CONFIG_IRDA_ULTRA */
177 } else
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,
189 (long) self, NULL);
191 return 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 IRDA_ASSERT(self != NULL, return;);
205 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
208 * Set some of the variables to preset values
210 self->magic = 0;
211 del_timer(&self->watchdog_timer); /* Important! */
213 if (self->conn_skb)
214 dev_kfree_skb(self->conn_skb);
216 kfree(self);
220 * Function irlmp_close_lsap (self)
222 * Close and remove LSAP
225 void irlmp_close_lsap(struct lsap_cb *self)
227 struct lap_cb *lap;
228 struct lsap_cb *lsap = NULL;
230 IRDA_ASSERT(self != NULL, return;);
231 IRDA_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)
237 lap = self->lap;
238 if (lap) {
239 IRDA_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;
253 #endif
255 self->lap = NULL;
256 /* Check if we found the LSAP! If not then try the unconnected lsaps */
257 if (!lsap) {
258 lsap = hashbin_remove(irlmp->unconnected_lsaps, (long) self,
259 NULL);
261 if (!lsap) {
262 IRDA_DEBUG(0,
263 "%s(), Looks like somebody has removed me already!\n",
264 __FUNCTION__);
265 return;
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)
280 struct lap_cb *lap;
282 IRDA_ASSERT(irlmp != NULL, return;);
283 IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return;);
284 IRDA_ASSERT(notify != NULL, return;);
287 * Allocate new instance of a LSAP connection
289 lap = kzalloc(sizeof(struct lap_cb), GFP_KERNEL);
290 if (lap == NULL) {
291 IRDA_ERROR("%s: unable to kmalloc\n", __FUNCTION__);
292 return;
295 lap->irlap = irlap;
296 lap->magic = LMP_LAP_MAGIC;
297 lap->saddr = saddr;
298 lap->daddr = DEV_ADDR_ANY;
299 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
300 lap->cache.valid = FALSE;
301 #endif
302 lap->lsaps = hashbin_new(HB_LOCK);
303 if (lap->lsaps == NULL) {
304 IRDA_WARNING("%s(), unable to kmalloc lsaps\n", __FUNCTION__);
305 kfree(lap);
306 return;
309 lap->lap_state = LAP_STANDBY;
311 init_timer(&lap->idle_timer);
314 * Insert into queue of LMP links
316 hashbin_insert(irlmp->links, (irda_queue_t *) lap, lap->saddr, NULL);
319 * We set only this variable so IrLAP can tell us on which link the
320 * different events happened on
322 irda_notify_init(notify);
323 notify->instance = lap;
327 * Function irlmp_unregister_irlap (saddr)
329 * IrLAP layer has been removed!
332 void irlmp_unregister_link(__u32 saddr)
334 struct lap_cb *link;
336 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
338 /* We must remove ourselves from the hashbin *first*. This ensure
339 * that no more LSAPs will be open on this link and no discovery
340 * will be triggered anymore. Jean II */
341 link = hashbin_remove(irlmp->links, saddr, NULL);
342 if (link) {
343 IRDA_ASSERT(link->magic == LMP_LAP_MAGIC, return;);
345 /* Kill all the LSAPs on this link. Jean II */
346 link->reason = LAP_DISC_INDICATION;
347 link->daddr = DEV_ADDR_ANY;
348 irlmp_do_lap_event(link, LM_LAP_DISCONNECT_INDICATION, NULL);
350 /* Remove all discoveries discovered at this link */
351 irlmp_expire_discoveries(irlmp->cachelog, link->saddr, TRUE);
353 /* Final cleanup */
354 del_timer(&link->idle_timer);
355 link->magic = 0;
356 hashbin_delete(link->lsaps, (FREE_FUNC) __irlmp_close_lsap);
357 kfree(link);
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;
372 struct lap_cb *lap;
373 struct lsap_cb *lsap;
374 int ret;
376 IRDA_ASSERT(self != NULL, return -EBADR;);
377 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -EBADR;);
379 IRDA_DEBUG(2,
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)) {
384 ret = -EISCONN;
385 goto err;
388 /* Client must supply destination device address */
389 if (!daddr) {
390 ret = -EINVAL;
391 goto err;
394 /* Any userdata? */
395 if (tx_skb == NULL) {
396 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
397 if (!tx_skb)
398 return -ENOMEM;
400 skb_reserve(tx_skb, LMP_MAX_HEADER);
403 /* Make room for MUX control header (3 bytes) */
404 IRDA_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;
419 unsigned long flags;
421 spin_lock_irqsave(&irlmp->cachelog->hb_spinlock, flags);
422 if (daddr != DEV_ADDR_ANY)
423 discovery = hashbin_find(irlmp->cachelog, daddr, NULL);
424 else {
425 IRDA_DEBUG(2, "%s(), no daddr\n", __FUNCTION__);
426 discovery = (discovery_t *)
427 hashbin_get_first(irlmp->cachelog);
430 if (discovery) {
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);
437 if (lap == NULL) {
438 IRDA_DEBUG(1, "%s(), Unable to find a usable link!\n", __FUNCTION__);
439 ret = -EHOSTUNREACH;
440 goto err;
443 /* Check if LAP is disconnected or already connected */
444 if (lap->daddr == DEV_ADDR_ANY)
445 lap->daddr = daddr;
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__);
454 ret = -EAGAIN;
455 goto err;
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__);
461 ret = -EBUSY;
462 goto err;
465 self->lap = lap;
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 IRDA_ASSERT(lsap != NULL, return -1;);
474 IRDA_ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
475 IRDA_ASSERT(lsap->lap != NULL, return -1;);
476 IRDA_ASSERT(lsap->lap->magic == LMP_LAP_MAGIC, return -1;);
478 hashbin_insert(self->lap->lsaps, (irda_queue_t *) self, (long) self,
479 NULL);
481 set_bit(0, &self->connected); /* TRUE */
484 * User supplied qos specifications?
486 if (qos)
487 self->qos = *qos;
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);
494 return 0;
496 err:
497 /* Cleanup */
498 if(tx_skb)
499 dev_kfree_skb(tx_skb);
500 return ret;
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)
512 int max_seg_size;
513 int lap_header_size;
514 int max_header_size;
516 IRDA_ASSERT(self != NULL, return;);
517 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
518 IRDA_ASSERT(skb != NULL, return;);
519 IRDA_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().
527 * Jean II */
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(). */
540 skb_get(skb);
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 IRDA_ASSERT(self != NULL, return -1;);
556 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
557 IRDA_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 IRDA_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);
574 return 0;
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)
585 int max_header_size;
586 int lap_header_size;
587 int max_seg_size;
589 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
591 IRDA_ASSERT(skb != NULL, return;);
592 IRDA_ASSERT(self != NULL, return;);
593 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
594 IRDA_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() */
610 skb_get(skb);
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)
626 struct lsap_cb *new;
627 unsigned long flags;
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",
638 __FUNCTION__);
639 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
640 flags);
641 return NULL;
644 /* Allocate a new instance */
645 new = kmemdup(orig, sizeof(*new), GFP_ATOMIC);
646 if (!new) {
647 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
648 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock,
649 flags);
650 return NULL;
652 /* new->lap = orig->lap; => done in the memcpy() */
653 /* new->slsap_sel = orig->slsap_sel; => done in the memcpy() */
654 new->conn_skb = NULL;
656 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
658 /* Not everything is the same */
659 new->notify.instance = instance;
661 init_timer(&new->watchdog_timer);
663 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) new,
664 (long) new, NULL);
666 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
667 /* Make sure that we invalidate the LSAP cache */
668 new->lap->cache.valid = FALSE;
669 #endif /* CONFIG_IRDA_CACHE_LAST_LSAP */
671 return new;
675 * Function irlmp_disconnect_request (handle, userdata)
677 * The service user is requesting disconnection, this will not remove the
678 * LSAP, but only mark it as disconnected
680 int irlmp_disconnect_request(struct lsap_cb *self, struct sk_buff *userdata)
682 struct lsap_cb *lsap;
684 IRDA_ASSERT(self != NULL, return -1;);
685 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
686 IRDA_ASSERT(userdata != NULL, return -1;);
688 /* Already disconnected ?
689 * There is a race condition between irlmp_disconnect_indication()
690 * and us that might mess up the hashbins below. This fixes it.
691 * Jean II */
692 if (! test_and_clear_bit(0, &self->connected)) {
693 IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
694 dev_kfree_skb(userdata);
695 return -1;
698 skb_push(userdata, LMP_CONTROL_HEADER);
701 * Do the event before the other stuff since we must know
702 * which lap layer that the frame should be transmitted on
704 irlmp_do_lsap_event(self, LM_DISCONNECT_REQUEST, userdata);
706 /* Drop reference count - see irlap_data_request(). */
707 dev_kfree_skb(userdata);
710 * Remove LSAP from list of connected LSAPs for the particular link
711 * and insert it into the list of unconnected LSAPs
713 IRDA_ASSERT(self->lap != NULL, return -1;);
714 IRDA_ASSERT(self->lap->magic == LMP_LAP_MAGIC, return -1;);
715 IRDA_ASSERT(self->lap->lsaps != NULL, return -1;);
717 lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
718 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
719 self->lap->cache.valid = FALSE;
720 #endif
722 IRDA_ASSERT(lsap != NULL, return -1;);
723 IRDA_ASSERT(lsap->magic == LMP_LSAP_MAGIC, return -1;);
724 IRDA_ASSERT(lsap == self, return -1;);
726 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) self,
727 (long) self, NULL);
729 /* Reset some values */
730 self->dlsap_sel = LSAP_ANY;
731 self->lap = NULL;
733 return 0;
735 EXPORT_SYMBOL(irlmp_disconnect_request);
738 * Function irlmp_disconnect_indication (reason, userdata)
740 * LSAP is being closed!
742 void irlmp_disconnect_indication(struct lsap_cb *self, LM_REASON reason,
743 struct sk_buff *skb)
745 struct lsap_cb *lsap;
747 IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, irlmp_reasons[reason]);
748 IRDA_ASSERT(self != NULL, return;);
749 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
751 IRDA_DEBUG(3, "%s(), slsap_sel=%02x, dlsap_sel=%02x\n",
752 __FUNCTION__, self->slsap_sel, self->dlsap_sel);
754 /* Already disconnected ?
755 * There is a race condition between irlmp_disconnect_request()
756 * and us that might mess up the hashbins below. This fixes it.
757 * Jean II */
758 if (! test_and_clear_bit(0, &self->connected)) {
759 IRDA_DEBUG(0, "%s(), already disconnected!\n", __FUNCTION__);
760 return;
764 * Remove association between this LSAP and the link it used
766 IRDA_ASSERT(self->lap != NULL, return;);
767 IRDA_ASSERT(self->lap->lsaps != NULL, return;);
769 lsap = hashbin_remove(self->lap->lsaps, (long) self, NULL);
770 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
771 self->lap->cache.valid = FALSE;
772 #endif
774 IRDA_ASSERT(lsap != NULL, return;);
775 IRDA_ASSERT(lsap == self, return;);
776 hashbin_insert(irlmp->unconnected_lsaps, (irda_queue_t *) lsap,
777 (long) lsap, NULL);
779 self->dlsap_sel = LSAP_ANY;
780 self->lap = NULL;
783 * Inform service user
785 if (self->notify.disconnect_indication) {
786 /* Don't forget to refcount it - see irlap_driver_rcv(). */
787 if(skb)
788 skb_get(skb);
789 self->notify.disconnect_indication(self->notify.instance,
790 self, reason, skb);
791 } else {
792 IRDA_DEBUG(0, "%s(), no handler\n", __FUNCTION__);
797 * Function irlmp_do_expiry (void)
799 * Do a cleanup of the discovery log (remove old entries)
801 * Note : separate from irlmp_do_discovery() so that we can handle
802 * passive discovery properly.
804 void irlmp_do_expiry(void)
806 struct lap_cb *lap;
809 * Expire discovery on all links which are *not* connected.
810 * On links which are connected, we can't do discovery
811 * anymore and can't refresh the log, so we freeze the
812 * discovery log to keep info about the device we are
813 * connected to.
814 * This info is mandatory if we want irlmp_connect_request()
815 * to work properly. - Jean II
817 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
818 while (lap != NULL) {
819 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
821 if (lap->lap_state == LAP_STANDBY) {
822 /* Expire discoveries discovered on this link */
823 irlmp_expire_discoveries(irlmp->cachelog, lap->saddr,
824 FALSE);
826 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
831 * Function irlmp_do_discovery (nslots)
833 * Do some discovery on all links
835 * Note : log expiry is done above.
837 void irlmp_do_discovery(int nslots)
839 struct lap_cb *lap;
840 __u16 *data_hintsp;
842 /* Make sure the value is sane */
843 if ((nslots != 1) && (nslots != 6) && (nslots != 8) && (nslots != 16)){
844 IRDA_WARNING("%s: invalid value for number of slots!\n",
845 __FUNCTION__);
846 nslots = sysctl_discovery_slots = 8;
849 /* Construct new discovery info to be used by IrLAP, */
850 data_hintsp = (__u16 *) irlmp->discovery_cmd.data.hints;
851 put_unaligned(irlmp->hints.word, data_hintsp);
854 * Set character set for device name (we use ASCII), and
855 * copy device name. Remember to make room for a \0 at the
856 * end
858 irlmp->discovery_cmd.data.charset = CS_ASCII;
859 strncpy(irlmp->discovery_cmd.data.info, sysctl_devname,
860 NICKNAME_MAX_LEN);
861 irlmp->discovery_cmd.name_len = strlen(irlmp->discovery_cmd.data.info);
862 irlmp->discovery_cmd.nslots = nslots;
865 * Try to send discovery packets on all links
867 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
868 while (lap != NULL) {
869 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
871 if (lap->lap_state == LAP_STANDBY) {
872 /* Try to discover */
873 irlmp_do_lap_event(lap, LM_LAP_DISCOVERY_REQUEST,
874 NULL);
876 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
881 * Function irlmp_discovery_request (nslots)
883 * Do a discovery of devices in front of the computer
885 * If the caller has registered a client discovery callback, this
886 * allow him to receive the full content of the discovery log through
887 * this callback (as normally he will receive only new discoveries).
889 void irlmp_discovery_request(int nslots)
891 /* Return current cached discovery log (in full) */
892 irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_LOG);
895 * Start a single discovery operation if discovery is not already
896 * running
898 if (!sysctl_discovery) {
899 /* Check if user wants to override the default */
900 if (nslots == DISCOVERY_DEFAULT_SLOTS)
901 nslots = sysctl_discovery_slots;
903 irlmp_do_discovery(nslots);
904 /* Note : we never do expiry here. Expiry will run on the
905 * discovery timer regardless of the state of sysctl_discovery
906 * Jean II */
909 EXPORT_SYMBOL(irlmp_discovery_request);
912 * Function irlmp_get_discoveries (pn, mask, slots)
914 * Return the current discovery log
916 * If discovery is not enabled, you should call this function again
917 * after 1 or 2 seconds (i.e. after discovery has been done).
919 struct irda_device_info *irlmp_get_discoveries(int *pn, __u16 mask, int nslots)
921 /* If discovery is not enabled, it's likely that the discovery log
922 * will be empty. So, we trigger a single discovery, so that next
923 * time the user call us there might be some results in the log.
924 * Jean II
926 if (!sysctl_discovery) {
927 /* Check if user wants to override the default */
928 if (nslots == DISCOVERY_DEFAULT_SLOTS)
929 nslots = sysctl_discovery_slots;
931 /* Start discovery - will complete sometime later */
932 irlmp_do_discovery(nslots);
933 /* Note : we never do expiry here. Expiry will run on the
934 * discovery timer regardless of the state of sysctl_discovery
935 * Jean II */
938 /* Return current cached discovery log */
939 return(irlmp_copy_discoveries(irlmp->cachelog, pn, mask, TRUE));
941 EXPORT_SYMBOL(irlmp_get_discoveries);
944 * Function irlmp_notify_client (log)
946 * Notify all about discovered devices
948 * Clients registered with IrLMP are :
949 * o IrComm
950 * o IrLAN
951 * o Any socket (in any state - ouch, that may be a lot !)
952 * The client may have defined a callback to be notified in case of
953 * partial/selective discovery based on the hints that it passed to IrLMP.
955 static inline void
956 irlmp_notify_client(irlmp_client_t *client,
957 hashbin_t *log, DISCOVERY_MODE mode)
959 discinfo_t *discoveries; /* Copy of the discovery log */
960 int number; /* Number of nodes in the log */
961 int i;
963 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
965 /* Check if client wants or not partial/selective log (optimisation) */
966 if (!client->disco_callback)
967 return;
970 * Locking notes :
971 * the old code was manipulating the log directly, which was
972 * very racy. Now, we use copy_discoveries, that protects
973 * itself while dumping the log for us.
974 * The overhead of the copy is compensated by the fact that
975 * we only pass new discoveries in normal mode and don't
976 * pass the same old entry every 3s to the caller as we used
977 * to do (virtual function calling is expensive).
978 * Jean II
982 * Now, check all discovered devices (if any), and notify client
983 * only about the services that the client is interested in
984 * We also notify only about the new devices unless the caller
985 * explicitly request a dump of the log. Jean II
987 discoveries = irlmp_copy_discoveries(log, &number,
988 client->hint_mask.word,
989 (mode == DISCOVERY_LOG));
990 /* Check if the we got some results */
991 if (discoveries == NULL)
992 return; /* No nodes discovered */
994 /* Pass all entries to the listener */
995 for(i = 0; i < number; i++)
996 client->disco_callback(&(discoveries[i]), mode, client->priv);
998 /* Free up our buffer */
999 kfree(discoveries);
1003 * Function irlmp_discovery_confirm ( self, log)
1005 * Some device(s) answered to our discovery request! Check to see which
1006 * device it is, and give indication to the client(s)
1009 void irlmp_discovery_confirm(hashbin_t *log, DISCOVERY_MODE mode)
1011 irlmp_client_t *client;
1012 irlmp_client_t *client_next;
1014 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1016 IRDA_ASSERT(log != NULL, return;);
1018 if (!(HASHBIN_GET_SIZE(log)))
1019 return;
1021 /* For each client - notify callback may touch client list */
1022 client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
1023 while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
1024 (void *) &client_next) ) {
1025 /* Check if we should notify client */
1026 irlmp_notify_client(client, log, mode);
1028 client = client_next;
1033 * Function irlmp_discovery_expiry (expiry)
1035 * This device is no longer been discovered, and therefore it is being
1036 * purged from the discovery log. Inform all clients who have
1037 * registered for this event...
1039 * Note : called exclusively from discovery.c
1040 * Note : this is no longer called under discovery spinlock, so the
1041 * client can do whatever he wants in the callback.
1043 void irlmp_discovery_expiry(discinfo_t *expiries, int number)
1045 irlmp_client_t *client;
1046 irlmp_client_t *client_next;
1047 int i;
1049 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
1051 IRDA_ASSERT(expiries != NULL, return;);
1053 /* For each client - notify callback may touch client list */
1054 client = (irlmp_client_t *) hashbin_get_first(irlmp->clients);
1055 while (NULL != hashbin_find_next(irlmp->clients, (long) client, NULL,
1056 (void *) &client_next) ) {
1058 /* Pass all entries to the listener */
1059 for(i = 0; i < number; i++) {
1060 /* Check if we should notify client */
1061 if ((client->expir_callback) &&
1062 (client->hint_mask.word & u16ho(expiries[i].hints)
1063 & 0x7f7f) )
1064 client->expir_callback(&(expiries[i]),
1065 EXPIRY_TIMEOUT,
1066 client->priv);
1069 /* Next client */
1070 client = client_next;
1075 * Function irlmp_get_discovery_response ()
1077 * Used by IrLAP to get the discovery info it needs when answering
1078 * discovery requests by other devices.
1080 discovery_t *irlmp_get_discovery_response(void)
1082 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1084 IRDA_ASSERT(irlmp != NULL, return NULL;);
1086 u16ho(irlmp->discovery_rsp.data.hints) = irlmp->hints.word;
1089 * Set character set for device name (we use ASCII), and
1090 * copy device name. Remember to make room for a \0 at the
1091 * end
1093 irlmp->discovery_rsp.data.charset = CS_ASCII;
1095 strncpy(irlmp->discovery_rsp.data.info, sysctl_devname,
1096 NICKNAME_MAX_LEN);
1097 irlmp->discovery_rsp.name_len = strlen(irlmp->discovery_rsp.data.info);
1099 return &irlmp->discovery_rsp;
1103 * Function irlmp_data_request (self, skb)
1105 * Send some data to peer device
1107 * Note on skb management :
1108 * After calling the lower layers of the IrDA stack, we always
1109 * kfree() the skb, which drop the reference count (and potentially
1110 * destroy it).
1111 * IrLMP and IrLAP may queue the packet, and in those cases will need
1112 * to use skb_get() to keep it around.
1113 * Jean II
1115 int irlmp_data_request(struct lsap_cb *self, struct sk_buff *userdata)
1117 int ret;
1119 IRDA_ASSERT(self != NULL, return -1;);
1120 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -1;);
1122 /* Make room for MUX header */
1123 IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
1124 skb_push(userdata, LMP_HEADER);
1126 ret = irlmp_do_lsap_event(self, LM_DATA_REQUEST, userdata);
1128 /* Drop reference count - see irlap_data_request(). */
1129 dev_kfree_skb(userdata);
1131 return ret;
1133 EXPORT_SYMBOL(irlmp_data_request);
1136 * Function irlmp_data_indication (handle, skb)
1138 * Got data from LAP layer so pass it up to upper layer
1141 void irlmp_data_indication(struct lsap_cb *self, struct sk_buff *skb)
1143 /* Hide LMP header from layer above */
1144 skb_pull(skb, LMP_HEADER);
1146 if (self->notify.data_indication) {
1147 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1148 skb_get(skb);
1149 self->notify.data_indication(self->notify.instance, self, skb);
1154 * Function irlmp_udata_request (self, skb)
1156 int irlmp_udata_request(struct lsap_cb *self, struct sk_buff *userdata)
1158 int ret;
1160 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1162 IRDA_ASSERT(userdata != NULL, return -1;);
1164 /* Make room for MUX header */
1165 IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER, return -1;);
1166 skb_push(userdata, LMP_HEADER);
1168 ret = irlmp_do_lsap_event(self, LM_UDATA_REQUEST, userdata);
1170 /* Drop reference count - see irlap_data_request(). */
1171 dev_kfree_skb(userdata);
1173 return ret;
1177 * Function irlmp_udata_indication (self, skb)
1179 * Send unreliable data (but still within the connection)
1182 void irlmp_udata_indication(struct lsap_cb *self, struct sk_buff *skb)
1184 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1186 IRDA_ASSERT(self != NULL, return;);
1187 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
1188 IRDA_ASSERT(skb != NULL, return;);
1190 /* Hide LMP header from layer above */
1191 skb_pull(skb, LMP_HEADER);
1193 if (self->notify.udata_indication) {
1194 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1195 skb_get(skb);
1196 self->notify.udata_indication(self->notify.instance, self,
1197 skb);
1202 * Function irlmp_connless_data_request (self, skb)
1204 #ifdef CONFIG_IRDA_ULTRA
1205 int irlmp_connless_data_request(struct lsap_cb *self, struct sk_buff *userdata,
1206 __u8 pid)
1208 struct sk_buff *clone_skb;
1209 struct lap_cb *lap;
1211 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1213 IRDA_ASSERT(userdata != NULL, return -1;);
1215 /* Make room for MUX and PID header */
1216 IRDA_ASSERT(skb_headroom(userdata) >= LMP_HEADER+LMP_PID_HEADER,
1217 return -1;);
1219 /* Insert protocol identifier */
1220 skb_push(userdata, LMP_PID_HEADER);
1221 if(self != NULL)
1222 userdata->data[0] = self->pid;
1223 else
1224 userdata->data[0] = pid;
1226 /* Connectionless sockets must use 0x70 */
1227 skb_push(userdata, LMP_HEADER);
1228 userdata->data[0] = userdata->data[1] = LSAP_CONNLESS;
1230 /* Try to send Connectionless packets out on all links */
1231 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
1232 while (lap != NULL) {
1233 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return -1;);
1235 clone_skb = skb_clone(userdata, GFP_ATOMIC);
1236 if (!clone_skb) {
1237 dev_kfree_skb(userdata);
1238 return -ENOMEM;
1241 irlap_unitdata_request(lap->irlap, clone_skb);
1242 /* irlap_unitdata_request() don't increase refcount,
1243 * so no dev_kfree_skb() - Jean II */
1245 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
1247 dev_kfree_skb(userdata);
1249 return 0;
1251 #endif /* CONFIG_IRDA_ULTRA */
1254 * Function irlmp_connless_data_indication (self, skb)
1256 * Receive unreliable data outside any connection. Mostly used by Ultra
1259 #ifdef CONFIG_IRDA_ULTRA
1260 void irlmp_connless_data_indication(struct lsap_cb *self, struct sk_buff *skb)
1262 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1264 IRDA_ASSERT(self != NULL, return;);
1265 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return;);
1266 IRDA_ASSERT(skb != NULL, return;);
1268 /* Hide LMP and PID header from layer above */
1269 skb_pull(skb, LMP_HEADER+LMP_PID_HEADER);
1271 if (self->notify.udata_indication) {
1272 /* Don't forget to refcount it - see irlap_driver_rcv(). */
1273 skb_get(skb);
1274 self->notify.udata_indication(self->notify.instance, self,
1275 skb);
1278 #endif /* CONFIG_IRDA_ULTRA */
1281 * Propagate status indication from LAP to LSAPs (via LMP)
1282 * This don't trigger any change of state in lap_cb, lmp_cb or lsap_cb,
1283 * and the event is stateless, therefore we can bypass both state machines
1284 * and send the event direct to the LSAP user.
1285 * Jean II
1287 void irlmp_status_indication(struct lap_cb *self,
1288 LINK_STATUS link, LOCK_STATUS lock)
1290 struct lsap_cb *next;
1291 struct lsap_cb *curr;
1293 /* Send status_indication to all LSAPs using this link */
1294 curr = (struct lsap_cb *) hashbin_get_first( self->lsaps);
1295 while (NULL != hashbin_find_next(self->lsaps, (long) curr, NULL,
1296 (void *) &next) ) {
1297 IRDA_ASSERT(curr->magic == LMP_LSAP_MAGIC, return;);
1299 * Inform service user if he has requested it
1301 if (curr->notify.status_indication != NULL)
1302 curr->notify.status_indication(curr->notify.instance,
1303 link, lock);
1304 else
1305 IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
1307 curr = next;
1312 * Receive flow control indication from LAP.
1313 * LAP want us to send it one more frame. We implement a simple round
1314 * robin scheduler between the active sockets so that we get a bit of
1315 * fairness. Note that the round robin is far from perfect, but it's
1316 * better than nothing.
1317 * We then poll the selected socket so that we can do synchronous
1318 * refilling of IrLAP (which allow to minimise the number of buffers).
1319 * Jean II
1321 void irlmp_flow_indication(struct lap_cb *self, LOCAL_FLOW flow)
1323 struct lsap_cb *next;
1324 struct lsap_cb *curr;
1325 int lsap_todo;
1327 IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
1328 IRDA_ASSERT(flow == FLOW_START, return;);
1330 /* Get the number of lsap. That's the only safe way to know
1331 * that we have looped around... - Jean II */
1332 lsap_todo = HASHBIN_GET_SIZE(self->lsaps);
1333 IRDA_DEBUG(4, "%s() : %d lsaps to scan\n", __FUNCTION__, lsap_todo);
1335 /* Poll lsap in order until the queue is full or until we
1336 * tried them all.
1337 * Most often, the current LSAP will have something to send,
1338 * so we will go through this loop only once. - Jean II */
1339 while((lsap_todo--) &&
1340 (IRLAP_GET_TX_QUEUE_LEN(self->irlap) < LAP_HIGH_THRESHOLD)) {
1341 /* Try to find the next lsap we should poll. */
1342 next = self->flow_next;
1343 /* If we have no lsap, restart from first one */
1344 if(next == NULL)
1345 next = (struct lsap_cb *) hashbin_get_first(self->lsaps);
1346 /* Verify current one and find the next one */
1347 curr = hashbin_find_next(self->lsaps, (long) next, NULL,
1348 (void *) &self->flow_next);
1349 /* Uh-oh... Paranoia */
1350 if(curr == NULL)
1351 break;
1352 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));
1354 /* Inform lsap user that it can send one more packet. */
1355 if (curr->notify.flow_indication != NULL)
1356 curr->notify.flow_indication(curr->notify.instance,
1357 curr, flow);
1358 else
1359 IRDA_DEBUG(1, "%s(), no handler\n", __FUNCTION__);
1363 #if 0
1365 * Function irlmp_hint_to_service (hint)
1367 * Returns a list of all servics contained in the given hint bits. This
1368 * function assumes that the hint bits have the size of two bytes only
1370 __u8 *irlmp_hint_to_service(__u8 *hint)
1372 __u8 *service;
1373 int i = 0;
1376 * Allocate array to store services in. 16 entries should be safe
1377 * since we currently only support 2 hint bytes
1379 service = kmalloc(16, GFP_ATOMIC);
1380 if (!service) {
1381 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1382 return NULL;
1385 if (!hint[0]) {
1386 IRDA_DEBUG(1, "<None>\n");
1387 kfree(service);
1388 return NULL;
1390 if (hint[0] & HINT_PNP)
1391 IRDA_DEBUG(1, "PnP Compatible ");
1392 if (hint[0] & HINT_PDA)
1393 IRDA_DEBUG(1, "PDA/Palmtop ");
1394 if (hint[0] & HINT_COMPUTER)
1395 IRDA_DEBUG(1, "Computer ");
1396 if (hint[0] & HINT_PRINTER) {
1397 IRDA_DEBUG(1, "Printer ");
1398 service[i++] = S_PRINTER;
1400 if (hint[0] & HINT_MODEM)
1401 IRDA_DEBUG(1, "Modem ");
1402 if (hint[0] & HINT_FAX)
1403 IRDA_DEBUG(1, "Fax ");
1404 if (hint[0] & HINT_LAN) {
1405 IRDA_DEBUG(1, "LAN Access ");
1406 service[i++] = S_LAN;
1409 * Test if extension byte exists. This byte will usually be
1410 * there, but this is not really required by the standard.
1411 * (IrLMP p. 29)
1413 if (hint[0] & HINT_EXTENSION) {
1414 if (hint[1] & HINT_TELEPHONY) {
1415 IRDA_DEBUG(1, "Telephony ");
1416 service[i++] = S_TELEPHONY;
1417 } if (hint[1] & HINT_FILE_SERVER)
1418 IRDA_DEBUG(1, "File Server ");
1420 if (hint[1] & HINT_COMM) {
1421 IRDA_DEBUG(1, "IrCOMM ");
1422 service[i++] = S_COMM;
1424 if (hint[1] & HINT_OBEX) {
1425 IRDA_DEBUG(1, "IrOBEX ");
1426 service[i++] = S_OBEX;
1429 IRDA_DEBUG(1, "\n");
1431 /* So that client can be notified about any discovery */
1432 service[i++] = S_ANY;
1434 service[i] = S_END;
1436 return service;
1438 #endif
1440 static const __u16 service_hint_mapping[S_END][2] = {
1441 { HINT_PNP, 0 }, /* S_PNP */
1442 { HINT_PDA, 0 }, /* S_PDA */
1443 { HINT_COMPUTER, 0 }, /* S_COMPUTER */
1444 { HINT_PRINTER, 0 }, /* S_PRINTER */
1445 { HINT_MODEM, 0 }, /* S_MODEM */
1446 { HINT_FAX, 0 }, /* S_FAX */
1447 { HINT_LAN, 0 }, /* S_LAN */
1448 { HINT_EXTENSION, HINT_TELEPHONY }, /* S_TELEPHONY */
1449 { HINT_EXTENSION, HINT_COMM }, /* S_COMM */
1450 { HINT_EXTENSION, HINT_OBEX }, /* S_OBEX */
1451 { 0xFF, 0xFF }, /* S_ANY */
1455 * Function irlmp_service_to_hint (service)
1457 * Converts a service type, to a hint bit
1459 * Returns: a 16 bit hint value, with the service bit set
1461 __u16 irlmp_service_to_hint(int service)
1463 __u16_host_order hint;
1465 hint.byte[0] = service_hint_mapping[service][0];
1466 hint.byte[1] = service_hint_mapping[service][1];
1468 return hint.word;
1470 EXPORT_SYMBOL(irlmp_service_to_hint);
1473 * Function irlmp_register_service (service)
1475 * Register local service with IrLMP
1478 void *irlmp_register_service(__u16 hints)
1480 irlmp_service_t *service;
1482 IRDA_DEBUG(4, "%s(), hints = %04x\n", __FUNCTION__, hints);
1484 /* Make a new registration */
1485 service = kmalloc(sizeof(irlmp_service_t), GFP_ATOMIC);
1486 if (!service) {
1487 IRDA_DEBUG(1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1488 return NULL;
1490 service->hints.word = hints;
1491 hashbin_insert(irlmp->services, (irda_queue_t *) service,
1492 (long) service, NULL);
1494 irlmp->hints.word |= hints;
1496 return (void *)service;
1498 EXPORT_SYMBOL(irlmp_register_service);
1501 * Function irlmp_unregister_service (handle)
1503 * Unregister service with IrLMP.
1505 * Returns: 0 on success, -1 on error
1507 int irlmp_unregister_service(void *handle)
1509 irlmp_service_t *service;
1510 unsigned long flags;
1512 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1514 if (!handle)
1515 return -1;
1517 /* Caller may call with invalid handle (it's legal) - Jean II */
1518 service = hashbin_lock_find(irlmp->services, (long) handle, NULL);
1519 if (!service) {
1520 IRDA_DEBUG(1, "%s(), Unknown service!\n", __FUNCTION__);
1521 return -1;
1524 hashbin_remove_this(irlmp->services, (irda_queue_t *) service);
1525 kfree(service);
1527 /* Remove old hint bits */
1528 irlmp->hints.word = 0;
1530 /* Refresh current hint bits */
1531 spin_lock_irqsave(&irlmp->services->hb_spinlock, flags);
1532 service = (irlmp_service_t *) hashbin_get_first(irlmp->services);
1533 while (service) {
1534 irlmp->hints.word |= service->hints.word;
1536 service = (irlmp_service_t *)hashbin_get_next(irlmp->services);
1538 spin_unlock_irqrestore(&irlmp->services->hb_spinlock, flags);
1539 return 0;
1541 EXPORT_SYMBOL(irlmp_unregister_service);
1544 * Function irlmp_register_client (hint_mask, callback1, callback2)
1546 * Register a local client with IrLMP
1547 * First callback is selective discovery (based on hints)
1548 * Second callback is for selective discovery expiries
1550 * Returns: handle > 0 on success, 0 on error
1552 void *irlmp_register_client(__u16 hint_mask, DISCOVERY_CALLBACK1 disco_clb,
1553 DISCOVERY_CALLBACK2 expir_clb, void *priv)
1555 irlmp_client_t *client;
1557 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1558 IRDA_ASSERT(irlmp != NULL, return NULL;);
1560 /* Make a new registration */
1561 client = kmalloc(sizeof(irlmp_client_t), GFP_ATOMIC);
1562 if (!client) {
1563 IRDA_DEBUG( 1, "%s(), Unable to kmalloc!\n", __FUNCTION__);
1564 return NULL;
1567 /* Register the details */
1568 client->hint_mask.word = hint_mask;
1569 client->disco_callback = disco_clb;
1570 client->expir_callback = expir_clb;
1571 client->priv = priv;
1573 hashbin_insert(irlmp->clients, (irda_queue_t *) client,
1574 (long) client, NULL);
1576 return (void *) client;
1578 EXPORT_SYMBOL(irlmp_register_client);
1581 * Function irlmp_update_client (handle, hint_mask, callback1, callback2)
1583 * Updates specified client (handle) with possibly new hint_mask and
1584 * callback
1586 * Returns: 0 on success, -1 on error
1588 int irlmp_update_client(void *handle, __u16 hint_mask,
1589 DISCOVERY_CALLBACK1 disco_clb,
1590 DISCOVERY_CALLBACK2 expir_clb, void *priv)
1592 irlmp_client_t *client;
1594 if (!handle)
1595 return -1;
1597 client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
1598 if (!client) {
1599 IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
1600 return -1;
1603 client->hint_mask.word = hint_mask;
1604 client->disco_callback = disco_clb;
1605 client->expir_callback = expir_clb;
1606 client->priv = priv;
1608 return 0;
1610 EXPORT_SYMBOL(irlmp_update_client);
1613 * Function irlmp_unregister_client (handle)
1615 * Returns: 0 on success, -1 on error
1618 int irlmp_unregister_client(void *handle)
1620 struct irlmp_client *client;
1622 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1624 if (!handle)
1625 return -1;
1627 /* Caller may call with invalid handle (it's legal) - Jean II */
1628 client = hashbin_lock_find(irlmp->clients, (long) handle, NULL);
1629 if (!client) {
1630 IRDA_DEBUG(1, "%s(), Unknown client!\n", __FUNCTION__);
1631 return -1;
1634 IRDA_DEBUG(4, "%s(), removing client!\n", __FUNCTION__);
1635 hashbin_remove_this(irlmp->clients, (irda_queue_t *) client);
1636 kfree(client);
1638 return 0;
1640 EXPORT_SYMBOL(irlmp_unregister_client);
1643 * Function irlmp_slsap_inuse (slsap)
1645 * Check if the given source LSAP selector is in use
1647 * This function is clearly not very efficient. On the mitigating side, the
1648 * stack make sure that in 99% of the cases, we are called only once
1649 * for each socket allocation. We could probably keep a bitmap
1650 * of the allocated LSAP, but I'm not sure the complexity is worth it.
1651 * Jean II
1653 static int irlmp_slsap_inuse(__u8 slsap_sel)
1655 struct lsap_cb *self;
1656 struct lap_cb *lap;
1657 unsigned long flags;
1659 IRDA_ASSERT(irlmp != NULL, return TRUE;);
1660 IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return TRUE;);
1661 IRDA_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)
1668 return FALSE;
1669 #endif /* CONFIG_IRDA_ULTRA */
1671 /* Valid values are between 0 and 127 (0x0-0x6F) */
1672 if (slsap_sel > LSAP_MAX)
1673 return TRUE;
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
1678 * the connection.
1680 spin_lock_irqsave_nested(&irlmp->links->hb_spinlock, flags,
1681 SINGLE_DEPTH_NESTING);
1682 lap = (struct lap_cb *) hashbin_get_first(irlmp->links);
1683 while (lap != NULL) {
1684 IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, goto errlap;);
1686 /* Careful for priority inversions here !
1687 * irlmp->links is never taken while another IrDA
1688 * spinlock is held, so we are safe. Jean II */
1689 spin_lock(&lap->lsaps->hb_spinlock);
1691 /* For this IrLAP, check all the LSAPs */
1692 self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
1693 while (self != NULL) {
1694 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC,
1695 goto errlsap;);
1697 if ((self->slsap_sel == slsap_sel)) {
1698 IRDA_DEBUG(4, "Source LSAP selector=%02x in use\n",
1699 self->slsap_sel);
1700 goto errlsap;
1702 self = (struct lsap_cb*) hashbin_get_next(lap->lsaps);
1704 spin_unlock(&lap->lsaps->hb_spinlock);
1706 /* Next LAP */
1707 lap = (struct lap_cb *) hashbin_get_next(irlmp->links);
1709 spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
1712 * Server sockets are typically waiting for connections and
1713 * therefore reside in the unconnected list. We don't want
1714 * to give out their LSAPs for obvious reasons...
1715 * Jean II
1717 spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
1719 self = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);
1720 while (self != NULL) {
1721 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, goto erruncon;);
1722 if ((self->slsap_sel == slsap_sel)) {
1723 IRDA_DEBUG(4, "Source LSAP selector=%02x in use (unconnected)\n",
1724 self->slsap_sel);
1725 goto erruncon;
1727 self = (struct lsap_cb*) hashbin_get_next(irlmp->unconnected_lsaps);
1729 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
1731 return FALSE;
1733 /* Error exit from within one of the two nested loops.
1734 * Make sure we release the right spinlock in the righ order.
1735 * Jean II */
1736 errlsap:
1737 spin_unlock(&lap->lsaps->hb_spinlock);
1738 IRDA_ASSERT_LABEL(errlap:)
1739 spin_unlock_irqrestore(&irlmp->links->hb_spinlock, flags);
1740 return TRUE;
1742 /* Error exit from within the unconnected loop.
1743 * Just one spinlock to release... Jean II */
1744 erruncon:
1745 spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
1746 return TRUE;
1750 * Function irlmp_find_free_slsap ()
1752 * Find a free source LSAP to use. This function is called if the service
1753 * user has requested a source LSAP equal to LM_ANY
1755 static __u8 irlmp_find_free_slsap(void)
1757 __u8 lsap_sel;
1758 int wrapped = 0;
1760 IRDA_ASSERT(irlmp != NULL, return -1;);
1761 IRDA_ASSERT(irlmp->magic == LMP_MAGIC, return -1;);
1763 /* Most users don't really care which LSAPs they are given,
1764 * and therefore we automatically give them a free LSAP.
1765 * This function try to find a suitable LSAP, i.e. which is
1766 * not in use and is within the acceptable range. Jean II */
1768 do {
1769 /* Always increment to LSAP number before using it.
1770 * In theory, we could reuse the last LSAP number, as long
1771 * as it is no longer in use. Some IrDA stack do that.
1772 * However, the previous socket may be half closed, i.e.
1773 * we closed it, we think it's no longer in use, but the
1774 * other side did not receive our close and think it's
1775 * active and still send data on it.
1776 * This is similar to what is done with PIDs and TCP ports.
1777 * Also, this reduce the number of calls to irlmp_slsap_inuse()
1778 * which is an expensive function to call.
1779 * Jean II */
1780 irlmp->last_lsap_sel++;
1782 /* Check if we need to wraparound (0x70-0x7f are reserved) */
1783 if (irlmp->last_lsap_sel > LSAP_MAX) {
1784 /* 0x00-0x10 are also reserved for well know ports */
1785 irlmp->last_lsap_sel = 0x10;
1787 /* Make sure we terminate the loop */
1788 if (wrapped++) {
1789 IRDA_ERROR("%s: no more free LSAPs !\n",
1790 __FUNCTION__);
1791 return 0;
1795 /* If the LSAP is in use, try the next one.
1796 * Despite the autoincrement, we need to check if the lsap
1797 * is really in use or not, first because LSAP may be
1798 * directly allocated in irlmp_open_lsap(), and also because
1799 * we may wraparound on old sockets. Jean II */
1800 } while (irlmp_slsap_inuse(irlmp->last_lsap_sel));
1802 /* Got it ! */
1803 lsap_sel = irlmp->last_lsap_sel;
1804 IRDA_DEBUG(4, "%s(), found free lsap_sel=%02x\n",
1805 __FUNCTION__, lsap_sel);
1807 return lsap_sel;
1811 * Function irlmp_convert_lap_reason (lap_reason)
1813 * Converts IrLAP disconnect reason codes to IrLMP disconnect reason
1814 * codes
1817 LM_REASON irlmp_convert_lap_reason( LAP_REASON lap_reason)
1819 int reason = LM_LAP_DISCONNECT;
1821 switch (lap_reason) {
1822 case LAP_DISC_INDICATION: /* Received a disconnect request from peer */
1823 IRDA_DEBUG( 1, "%s(), LAP_DISC_INDICATION\n", __FUNCTION__);
1824 reason = LM_USER_REQUEST;
1825 break;
1826 case LAP_NO_RESPONSE: /* To many retransmits without response */
1827 IRDA_DEBUG( 1, "%s(), LAP_NO_RESPONSE\n", __FUNCTION__);
1828 reason = LM_LAP_DISCONNECT;
1829 break;
1830 case LAP_RESET_INDICATION:
1831 IRDA_DEBUG( 1, "%s(), LAP_RESET_INDICATION\n", __FUNCTION__);
1832 reason = LM_LAP_RESET;
1833 break;
1834 case LAP_FOUND_NONE:
1835 case LAP_MEDIA_BUSY:
1836 case LAP_PRIMARY_CONFLICT:
1837 IRDA_DEBUG(1, "%s(), LAP_FOUND_NONE, LAP_MEDIA_BUSY or LAP_PRIMARY_CONFLICT\n", __FUNCTION__);
1838 reason = LM_CONNECT_FAILURE;
1839 break;
1840 default:
1841 IRDA_DEBUG(1, "%s(), Unknow IrLAP disconnect reason %d!\n",
1842 __FUNCTION__, lap_reason);
1843 reason = LM_LAP_DISCONNECT;
1844 break;
1847 return reason;
1850 #ifdef CONFIG_PROC_FS
1852 struct irlmp_iter_state {
1853 hashbin_t *hashbin;
1856 #define LSAP_START_TOKEN ((void *)1)
1857 #define LINK_START_TOKEN ((void *)2)
1859 static void *irlmp_seq_hb_idx(struct irlmp_iter_state *iter, loff_t *off)
1861 void *element;
1863 spin_lock_irq(&iter->hashbin->hb_spinlock);
1864 for (element = hashbin_get_first(iter->hashbin);
1865 element != NULL;
1866 element = hashbin_get_next(iter->hashbin)) {
1867 if (!off || *off-- == 0) {
1868 /* NB: hashbin left locked */
1869 return element;
1872 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1873 iter->hashbin = NULL;
1874 return NULL;
1878 static void *irlmp_seq_start(struct seq_file *seq, loff_t *pos)
1880 struct irlmp_iter_state *iter = seq->private;
1881 void *v;
1882 loff_t off = *pos;
1884 iter->hashbin = NULL;
1885 if (off-- == 0)
1886 return LSAP_START_TOKEN;
1888 iter->hashbin = irlmp->unconnected_lsaps;
1889 v = irlmp_seq_hb_idx(iter, &off);
1890 if (v)
1891 return v;
1893 if (off-- == 0)
1894 return LINK_START_TOKEN;
1896 iter->hashbin = irlmp->links;
1897 return irlmp_seq_hb_idx(iter, &off);
1900 static void *irlmp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1902 struct irlmp_iter_state *iter = seq->private;
1904 ++*pos;
1906 if (v == LSAP_START_TOKEN) { /* start of list of lsaps */
1907 iter->hashbin = irlmp->unconnected_lsaps;
1908 v = irlmp_seq_hb_idx(iter, NULL);
1909 return v ? v : LINK_START_TOKEN;
1912 if (v == LINK_START_TOKEN) { /* start of list of links */
1913 iter->hashbin = irlmp->links;
1914 return irlmp_seq_hb_idx(iter, NULL);
1917 v = hashbin_get_next(iter->hashbin);
1919 if (v == NULL) { /* no more in this hash bin */
1920 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1922 if (iter->hashbin == irlmp->unconnected_lsaps)
1923 v = LINK_START_TOKEN;
1925 iter->hashbin = NULL;
1927 return v;
1930 static void irlmp_seq_stop(struct seq_file *seq, void *v)
1932 struct irlmp_iter_state *iter = seq->private;
1934 if (iter->hashbin)
1935 spin_unlock_irq(&iter->hashbin->hb_spinlock);
1938 static int irlmp_seq_show(struct seq_file *seq, void *v)
1940 const struct irlmp_iter_state *iter = seq->private;
1941 struct lsap_cb *self = v;
1943 if (v == LSAP_START_TOKEN)
1944 seq_puts(seq, "Unconnected LSAPs:\n");
1945 else if (v == LINK_START_TOKEN)
1946 seq_puts(seq, "\nRegistered Link Layers:\n");
1947 else if (iter->hashbin == irlmp->unconnected_lsaps) {
1948 self = v;
1949 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC, return -EINVAL; );
1950 seq_printf(seq, "lsap state: %s, ",
1951 irlsap_state[ self->lsap_state]);
1952 seq_printf(seq,
1953 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1954 self->slsap_sel, self->dlsap_sel);
1955 seq_printf(seq, "(%s)", self->notify.name);
1956 seq_printf(seq, "\n");
1957 } else if (iter->hashbin == irlmp->links) {
1958 struct lap_cb *lap = v;
1960 seq_printf(seq, "lap state: %s, ",
1961 irlmp_state[lap->lap_state]);
1963 seq_printf(seq, "saddr: %#08x, daddr: %#08x, ",
1964 lap->saddr, lap->daddr);
1965 seq_printf(seq, "num lsaps: %d",
1966 HASHBIN_GET_SIZE(lap->lsaps));
1967 seq_printf(seq, "\n");
1969 /* Careful for priority inversions here !
1970 * All other uses of attrib spinlock are independent of
1971 * the object spinlock, so we are safe. Jean II */
1972 spin_lock(&lap->lsaps->hb_spinlock);
1974 seq_printf(seq, "\n Connected LSAPs:\n");
1975 for (self = (struct lsap_cb *) hashbin_get_first(lap->lsaps);
1976 self != NULL;
1977 self = (struct lsap_cb *)hashbin_get_next(lap->lsaps)) {
1978 IRDA_ASSERT(self->magic == LMP_LSAP_MAGIC,
1979 goto outloop;);
1980 seq_printf(seq, " lsap state: %s, ",
1981 irlsap_state[ self->lsap_state]);
1982 seq_printf(seq,
1983 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1984 self->slsap_sel, self->dlsap_sel);
1985 seq_printf(seq, "(%s)", self->notify.name);
1986 seq_putc(seq, '\n');
1989 IRDA_ASSERT_LABEL(outloop:)
1990 spin_unlock(&lap->lsaps->hb_spinlock);
1991 seq_putc(seq, '\n');
1992 } else
1993 return -EINVAL;
1995 return 0;
1998 static const struct seq_operations irlmp_seq_ops = {
1999 .start = irlmp_seq_start,
2000 .next = irlmp_seq_next,
2001 .stop = irlmp_seq_stop,
2002 .show = irlmp_seq_show,
2005 static int irlmp_seq_open(struct inode *inode, struct file *file)
2007 IRDA_ASSERT(irlmp != NULL, return -EINVAL;);
2009 return seq_open_private(file, &irlmp_seq_ops,
2010 sizeof(struct irlmp_iter_state));
2013 const struct file_operations irlmp_seq_fops = {
2014 .owner = THIS_MODULE,
2015 .open = irlmp_seq_open,
2016 .read = seq_read,
2017 .llseek = seq_lseek,
2018 .release = seq_release_private,
2021 #endif /* PROC_FS */