Linux 2.2.3pre3
[davej-history.git] / net / irda / irlmp.c
blobe433fcba69557b8569a6ebff7e058efe40055a28
1 /*********************************************************************
2 *
3 * Filename: irlmp.c
4 * Version: 0.8
5 * Description: IrDA Link Management Protocol (LMP) layer
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Aug 17 20:54:32 1997
9 * Modified at: Sat Feb 20 01:28:39 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/config.h>
27 #include <linux/malloc.h>
28 #include <linux/string.h>
29 #include <linux/skbuff.h>
30 #include <linux/types.h>
31 #include <linux/proc_fs.h>
32 #include <linux/init.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irmod.h>
36 #include <net/irda/timer.h>
37 #include <net/irda/qos.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/iriap.h>
40 #include <net/irda/irlmp.h>
41 #include <net/irda/irlmp_frame.h>
42 #include <linux/kmod.h>
44 /* Master structure */
45 struct irlmp_cb *irlmp = NULL;
47 /* These can be altered by the sysctl interface */
48 int sysctl_discovery = 0;
49 int sysctl_discovery_slots = 6;
50 char sysctl_devname[65];
52 char *lmp_reasons[] = {
53 "ERROR, NOT USED",
54 "LM_USER_REQUEST",
55 "LM_LAP_DISCONNECT",
56 "LM_CONNECT_FAILURE",
57 "LM_LAP_RESET",
58 "LM_INIT_DISCONNECT",
59 "ERROR, NOT USED",
62 __u8 *irlmp_hint_to_service( __u8 *hint);
63 #ifdef CONFIG_PROC_FS
64 int irlmp_proc_read( char *buf, char **start, off_t offset, int len,
65 int unused);
66 #endif
69 * Function irlmp_init (void)
71 * Create (allocate) the main IrLMP structure and the pointer array
72 * which will contain pointers to each instance of a LSAP.
74 __initfunc(int irlmp_init(void))
76 /* Initialize the irlmp structure. */
77 if ( irlmp == NULL) {
78 irlmp = kmalloc( sizeof(struct irlmp_cb), GFP_KERNEL);
79 if ( irlmp == NULL)
80 return -ENOMEM;
82 memset( irlmp, 0, sizeof(struct irlmp_cb));
84 irlmp->magic = LMP_MAGIC;
86 irlmp->registry = hashbin_new( HB_LOCAL);
87 irlmp->links = hashbin_new( HB_LOCAL);
88 irlmp->unconnected_lsaps = hashbin_new( HB_GLOBAL);
90 irlmp->free_lsap_sel = 0x10; /* Servers use 0x00-0x0f */
91 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
92 irlmp->cache.valid = FALSE;
93 #endif
94 strcpy( sysctl_devname, "Linux");
96 /* Do discovery every 3 seconds */
97 init_timer( &irlmp->discovery_timer);
98 irlmp_start_discovery_timer( irlmp, 600);
100 return 0;
104 * Function irlmp_cleanup (void)
106 * Remove IrLMP layer
109 void irlmp_cleanup(void)
111 /* Check for main structure */
112 ASSERT( irlmp != NULL, return;);
113 ASSERT( irlmp->magic == LMP_MAGIC, return;);
115 del_timer( &irlmp->discovery_timer);
117 /* FIXME, we need a special function to deallocate LAPs */
118 hashbin_delete( irlmp->links, (FREE_FUNC) kfree);
119 hashbin_delete( irlmp->unconnected_lsaps, (FREE_FUNC) kfree);
120 hashbin_delete( irlmp->registry, (FREE_FUNC) kfree);
122 /* De-allocate main structure */
123 kfree( irlmp);
124 irlmp = NULL;
128 * Function irlmp_open_lsap (slsap, notify)
130 * Register with IrLMP and create a local LSAP,
131 * returns handle to LSAP.
133 struct lsap_cb *irlmp_open_lsap( __u8 slsap_sel, struct notify_t *notify)
135 struct lsap_cb *self;
137 ASSERT( notify != NULL, return NULL;);
138 ASSERT( irlmp != NULL, return NULL;);
139 ASSERT( irlmp->magic == LMP_MAGIC, return NULL;);
141 DEBUG( 4, __FUNCTION__ "(), slsap_sel=%02x\n", slsap_sel);
144 * Does the client care which Source LSAP selector it gets?
146 if ( slsap_sel == LSAP_ANY) {
148 * Find unused LSAP
150 slsap_sel = irlmp_find_free_slsap();
151 if ( slsap_sel == 0)
152 return NULL;
153 } else {
155 * Client wants specific LSAP, so check if it's already
156 * in use
158 if ( irlmp_slsap_inuse( slsap_sel)) {
159 return NULL;
161 if ( slsap_sel > irlmp->free_lsap_sel)
162 irlmp->free_lsap_sel = slsap_sel+1;
166 * Allocate new instance of a LSAP connection
168 self = kmalloc( sizeof(struct lsap_cb), GFP_ATOMIC);
169 if ( self == NULL) {
170 printk( KERN_ERR "IrLMP: Can't allocate memory for "
171 "LSAP control block!\n");
172 return NULL;
174 memset( self, 0, sizeof(struct lsap_cb));
176 self->magic = LMP_LSAP_MAGIC;
177 self->slsap_sel = slsap_sel;
178 self->dlsap_sel = LSAP_ANY;
179 self->connected = FALSE;
181 init_timer( &self->watchdog_timer);
183 ASSERT( notify->instance != NULL, return NULL;);
184 self->notify = *notify;
186 irlmp_next_lsap_state( self, LSAP_DISCONNECTED);
189 * Insert into queue of unconnected LSAPs
191 hashbin_insert( irlmp->unconnected_lsaps, (QUEUE *) self,
192 self->slsap_sel, NULL);
194 return self;
198 * Function irlmp_close_lsap (self)
200 * Remove an instance of a LSAP
202 static void __irlmp_close_lsap( struct lsap_cb *self)
204 DEBUG( 4, __FUNCTION__ "()\n");
206 ASSERT( self != NULL, return;);
207 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
210 * Set some of the variables to preset values
212 self->magic = ~LMP_LSAP_MAGIC;
213 del_timer( &self->watchdog_timer); /* Important! */
215 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
216 ASSERT( irlmp != NULL, return;);
217 irlmp->cache.valid = FALSE;
218 #endif
219 kfree( self);
223 * Function irlmp_close_lsap (self)
228 void irlmp_close_lsap( struct lsap_cb *self)
230 struct lap_cb *lap;
231 struct lsap_cb *lsap;
233 ASSERT( self != NULL, return;);
234 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
236 lap = self->lap;
239 * Find out if we should remove this LSAP from a link or from the
240 * list of unconnected lsaps (not associated with a link)
242 if ( lap == NULL) {
243 lsap = hashbin_remove( irlmp->unconnected_lsaps,
244 self->slsap_sel, NULL);
245 } else {
246 ASSERT( lap != NULL, return;);
247 ASSERT( lap->magic == LMP_LAP_MAGIC, return;);
249 lsap = hashbin_remove( lap->lsaps, self->slsap_sel, NULL);
251 if ( lsap == NULL) {
252 DEBUG( 1, __FUNCTION__
253 "(), Looks like somebody has removed me already!\n");
254 return;
256 ASSERT( lsap == self, return;);
258 __irlmp_close_lsap( self);
262 * Function irlmp_register_irlap (saddr, notify)
264 * Register IrLAP layer with IrLMP. There is possible to have multiple
265 * instances of the IrLAP layer, each connected to different IrDA ports
268 void irlmp_register_irlap( struct irlap_cb *irlap, __u32 saddr,
269 struct notify_t *notify)
271 struct lap_cb *lap;
273 DEBUG( 4, __FUNCTION__ "(), Registered IrLAP, saddr = %08x\n",
274 saddr);
276 ASSERT( irlmp != NULL, return;);
277 ASSERT( irlmp->magic == LMP_MAGIC, return;);
278 ASSERT( notify != NULL, return;);
281 * Allocate new instance of a LSAP connection
283 lap = kmalloc( sizeof(struct lap_cb), GFP_KERNEL);
284 if ( lap == NULL) {
285 printk( KERN_ERR "IrLMP: Can't allocate memory for "
286 "LAP control block!\n");
287 return;
289 memset( lap, 0, sizeof(struct lap_cb));
291 lap->irlap = irlap;
292 lap->magic = LMP_LAP_MAGIC;
293 lap->saddr = saddr;
294 lap->daddr = DEV_ADDR_ANY;
295 lap->lsaps = hashbin_new( HB_GLOBAL);
296 lap->cachelog = hashbin_new( HB_LOCAL);
298 irlmp_next_lap_state( lap, LAP_STANDBY);
300 init_timer(&lap->idle_timer);
303 * Insert into queue of unconnected LSAPs
305 hashbin_insert( irlmp->links, (QUEUE *) lap, lap->saddr, NULL);
308 * We set only this variable so IrLAP can tell us on which link the
309 * different events happened on
311 irda_notify_init( notify);
312 notify->instance = lap;
316 * Function irlmp_unregister_irlap (saddr)
318 * IrLAP layer has been removed!
321 void irlmp_unregister_irlap(__u32 saddr)
323 struct lap_cb *self;
325 DEBUG(4, __FUNCTION__ "()\n");
327 self = hashbin_remove(irlmp->links, saddr, NULL);
328 if (self) {
329 ASSERT(self->magic == LMP_LAP_MAGIC, return;);
331 del_timer(&self->idle_timer);
333 self->magic = 0;
334 kfree(self);
335 } else {
336 DEBUG(1, __FUNCTION__ "(), Didn't find LAP layer!\n");
340 void dump_discoveries( hashbin_t *log)
342 DISCOVERY *d;
344 ASSERT( log != NULL, return;);
346 d = (DISCOVERY *) hashbin_get_first( log);
347 while( d != NULL) {
348 DEBUG( 1, "Discovery:\n");
349 DEBUG( 1, " daddr=%08x\n", d->daddr);
350 DEBUG( 1, " name=%s\n", d->info);
352 d = (DISCOVERY *) hashbin_get_next( log);
356 static struct lap_cb *irlmp_find_link( __u32 daddr)
358 struct lap_cb *lap;
359 unsigned long flags;
361 DEBUG( 1, __FUNCTION__
362 "(), client didn't supply saddr, so try to look it up!\n");
365 * Find out which link to connect on, and make sure nothing strange
366 * happens while we traverse the list
368 save_flags( flags);
369 cli();
371 lap = (struct lap_cb *) hashbin_get_first( irlmp->links);
372 while ( lap != NULL) {
373 ASSERT( lap->magic == LMP_LAP_MAGIC, return NULL;);
374 /* dump_discoveries( lap->cachelog); */
376 if ( hashbin_find( lap->cachelog, daddr, NULL)) {
377 DEBUG( 4, __FUNCTION__
378 "() found link to connect on!\n");
379 return lap;
381 lap = (struct lap_cb *) hashbin_get_next( irlmp->links);
383 restore_flags(flags);
385 return NULL;
389 * Function irlmp_connect_request (handle, dlsap, userdata)
391 * Connect with a peer LSAP
394 int irlmp_connect_request(struct lsap_cb *self, __u8 dlsap_sel,
395 __u32 saddr, __u32 daddr,
396 struct qos_info *qos, struct sk_buff *userdata)
398 struct sk_buff *skb = NULL;
399 struct lap_cb *lap;
400 struct lsap_cb *lsap;
402 ASSERT( self != NULL, return -1;);
403 ASSERT( self->magic == LMP_LSAP_MAGIC, return -1;);
405 DEBUG( 4, __FUNCTION__
406 "(), slsap_sel=%02x, dlsap_sel=%02x, saddr=%08x, daddr=%08x\n",
407 self->slsap_sel, dlsap_sel, saddr, daddr);
409 if ( self->connected) {
410 DEBUG( 1, __FUNCTION__ "(), Error: already connected!!\n");
412 return -EISCONN;
415 /* Any userdata? */
416 if ( userdata == NULL) {
417 skb = dev_alloc_skb( 64);
418 if (skb == NULL) {
419 DEBUG( 1, __FUNCTION__
420 "(), Could not allocate sk_buff of length %d\n",
421 64);
422 return -1;
424 skb_reserve( skb, LMP_CONTROL_HEADER+LAP_HEADER);
425 } else
426 skb = userdata;
428 /* Make room for MUX control header ( 3 bytes) */
429 ASSERT( skb_headroom( skb) >= LMP_CONTROL_HEADER, return -1;);
430 skb_push( skb, LMP_CONTROL_HEADER);
432 self->dlsap_sel = dlsap_sel;
433 self->tmp_skb = skb;
436 * Find the link to where we should try to connect since there may
437 * be more than one IrDA port on this machine. If the client has
438 * passed us the saddr (and already knows which link to use), then
439 * we use that to find the link, if not then we have to look in the
440 * discovery log and check if any of the links has discovered a
441 * device with the given daddr
443 if ( saddr)
444 lap = hashbin_find( irlmp->links, saddr, NULL);
445 else
446 lap = irlmp_find_link( daddr);
448 if ( lap == NULL) {
449 DEBUG( 1, __FUNCTION__ "(), Unable to find a usable link!\n");
451 return -EHOSTUNREACH;
453 self->lap = lap;
456 * Remove LSAP from list of unconnected LSAPs and insert it into the
457 * list of connected LSAPs for the particular link */
458 lsap = hashbin_remove( irlmp->unconnected_lsaps, self->slsap_sel,
459 NULL);
461 ASSERT( lsap != NULL, return -1;);
462 ASSERT( lsap->magic == LMP_LSAP_MAGIC, return -1;);
463 ASSERT( lsap->lap != NULL, return -1;);
464 ASSERT( lsap->lap->magic == LMP_LAP_MAGIC, return -1;);
466 hashbin_insert( self->lap->lsaps, (QUEUE *) self, self->slsap_sel,
467 NULL);
469 self->connected = TRUE;
472 * User supplied qos specifications?
474 if (qos)
475 self->qos = *qos;
477 DEBUG( 4, "*** Connecting SLSAP=%02x, DLSAP= %02x\n",
478 self->slsap_sel, self->dlsap_sel);
480 irlmp_do_lsap_event( self, LM_CONNECT_REQUEST, skb);
482 return 0;
486 * Function irlmp_connect_indication (self)
488 * Incomming connection
491 void irlmp_connect_indication( struct lsap_cb *self, struct sk_buff *skb)
493 int max_seg_size;
495 DEBUG( 4, __FUNCTION__ "()\n");
497 ASSERT( self != NULL, return;);
498 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
499 ASSERT( skb != NULL, return;);
500 ASSERT( self->lap != NULL, return;);
502 self->qos = *self->lap->qos;
504 max_seg_size = self->lap->qos->data_size.value;
505 DEBUG( 4, __FUNCTION__ "(), max_seg_size=%d\n", max_seg_size);
507 /* Hide LMP_CONTROL_HEADER header from layer above */
508 skb_pull( skb, LMP_CONTROL_HEADER);
510 if ( self->notify.connect_indication)
511 self->notify.connect_indication(self->notify.instance, self,
512 &self->qos, max_seg_size, skb);
516 * Function irlmp_connect_response (handle, userdata)
518 * Service user is accepting connection
521 void irlmp_connect_response( struct lsap_cb *self, struct sk_buff *userdata)
523 DEBUG( 4, __FUNCTION__ "()\n");
525 ASSERT( self != NULL, return;);
526 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
527 ASSERT( userdata != NULL, return;);
529 self->connected = TRUE;
531 DEBUG( 4, "irlmp_connect_response: slsap_sel=%02x, dlsap_sel=%02x\n",
532 self->slsap_sel, self->dlsap_sel);
534 /* Make room for MUX control header ( 3 bytes) */
535 ASSERT( skb_headroom( userdata) >= LMP_CONTROL_HEADER, return;);
536 skb_push( userdata, LMP_CONTROL_HEADER);
538 irlmp_do_lsap_event( self, LM_CONNECT_RESPONSE, userdata);
542 * Function irlmp_connect_confirm (handle, skb)
544 * LSAP connection confirmed peer device!
546 void irlmp_connect_confirm( struct lsap_cb *self, struct sk_buff *skb)
548 int max_seg_size;
550 DEBUG( 4, __FUNCTION__ "()\n");
552 ASSERT( skb != NULL, return;);
553 ASSERT( self != NULL, return;);
554 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
556 ASSERT( self->lap != NULL, return;);
557 self->qos = *self->lap->qos;
559 max_seg_size = self->qos.data_size.value;
560 DEBUG( 4, __FUNCTION__ "(), max_seg_size=%d\n", max_seg_size);
562 /* Hide LMP_CONTROL_HEADER header from layer above */
563 skb_pull( skb, LMP_CONTROL_HEADER);
565 if ( self->notify.connect_confirm) {
566 self->notify.connect_confirm( self->notify.instance, self,
567 &self->qos, max_seg_size, skb);
572 * Function irlmp_disconnect_request (handle, userdata)
574 * The service user is requesting disconnection, this will not remove the
575 * LSAP, but only mark it as disconnected
577 void irlmp_disconnect_request( struct lsap_cb *self, struct sk_buff *userdata)
579 struct lsap_cb *lsap;
581 DEBUG( 4, __FUNCTION__ "()\n");
583 ASSERT( self != NULL, return;);
584 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
586 /* Already disconnected? */
587 if ( !self->connected) {
588 DEBUG( 1, __FUNCTION__ "(), already disconnected!\n");
589 return;
592 ASSERT( userdata != NULL, return;);
593 ASSERT( self->connected == TRUE, return;);
595 skb_push( userdata, LMP_CONTROL_HEADER);
598 * Do the event before the other stuff since we must know
599 * which lap layer that the frame should be transmitted on
601 irlmp_do_lsap_event( self, LM_DISCONNECT_REQUEST, userdata);
604 * Remove LSAP from list of connected LSAPs for the particular link
605 * and insert it into the list of unconnected LSAPs
607 ASSERT( self->lap != NULL, return;);
608 ASSERT( self->lap->magic == LMP_LAP_MAGIC, return;);
609 ASSERT( self->lap->lsaps != NULL, return;);
611 lsap = hashbin_remove( self->lap->lsaps, self->slsap_sel, NULL);
613 ASSERT( lsap != NULL, return;);
614 ASSERT( lsap->magic == LMP_LSAP_MAGIC, return;);
615 ASSERT( lsap == self, return;);
617 hashbin_insert( irlmp->unconnected_lsaps, (QUEUE *) self,
618 self->slsap_sel, NULL);
620 /* Reset some values */
621 self->connected = FALSE;
622 self->dlsap_sel = LSAP_ANY;
623 self->lap = NULL;
627 * Function irlmp_disconnect_indication (reason, userdata)
629 * LSAP is being closed!
631 void irlmp_disconnect_indication( struct lsap_cb *self, LM_REASON reason,
632 struct sk_buff *userdata)
634 struct lsap_cb *lsap;
636 DEBUG( 1, __FUNCTION__ "(), reason=%s\n", lmp_reasons[reason]);
638 ASSERT( self != NULL, return;);
639 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
640 ASSERT( self->connected == TRUE, return;);
642 DEBUG( 3, __FUNCTION__ "(), slsap_sel=%02x, dlsap_sel=%02x\n",
643 self->slsap_sel, self->dlsap_sel);
645 self->connected = FALSE;
646 self->dlsap_sel = LSAP_ANY;
649 * Remove association between this LSAP and the link it used
651 ASSERT( self->lap != NULL, return;);
652 ASSERT( self->lap->lsaps != NULL, return;);
654 lsap = hashbin_remove( self->lap->lsaps, self->slsap_sel, NULL);
656 ASSERT( lsap != NULL, return;);
657 ASSERT( lsap == self, return;);
658 hashbin_insert( irlmp->unconnected_lsaps, (QUEUE *) lsap,
659 lsap->slsap_sel, NULL);
661 self->lap = NULL;
664 * Inform service user
666 if ( self->notify.disconnect_indication) {
667 self->notify.disconnect_indication( self->notify.instance,
668 self, reason, userdata);
673 * Function irlmp_discovery_request (nslots)
675 * Do a discovery of devices in front of the computer
678 void irlmp_discovery_request( int nslots)
680 struct lap_cb *lap;
682 DEBUG( 4, __FUNCTION__ "(), nslots=%d\n", nslots);
684 ASSERT( irlmp != NULL, return;);
686 if ( !sysctl_discovery)
687 return;
689 if ((nslots != 1)&&(nslots != 6)&&(nslots != 8)&&(nslots != 16)) {
690 DEBUG( 1, __FUNCTION__ "(), invalid number of slots value!\n");
691 nslots = sysctl_discovery_slots = 8;
695 * Construct new discovery info to be used by IrLAP,
696 * TODO: no need to do this every time!
698 irlmp->discovery_cmd.hint[0] = irlmp->hint[0];
699 irlmp->discovery_cmd.hint[1] = irlmp->hint[1];
702 * Set character set for device name (we use ASCII), and
703 * copy device name. Remember to make room for a \0 at the
704 * end
706 irlmp->discovery_cmd.charset = CS_ASCII;
708 strncpy( irlmp->discovery_cmd.info, sysctl_devname, 31);
709 irlmp->discovery_cmd.info_len = strlen( irlmp->discovery_cmd.info);
710 irlmp->discovery_cmd.nslots = nslots;
713 * Try to send discovery packets on all links
715 lap = ( struct lap_cb *) hashbin_get_first( irlmp->links);
716 while ( lap != NULL) {
717 ASSERT( lap->magic == LMP_LAP_MAGIC, return;);
719 DEBUG( 4, __FUNCTION__ "() sending request!\n");
720 irlmp_do_lap_event( lap, LM_LAP_DISCOVERY_REQUEST, NULL);
722 lap = ( struct lap_cb *) hashbin_get_next( irlmp->links);
727 * Function irlmp_check_services (discovery)
732 void irlmp_check_services( DISCOVERY *discovery)
734 struct irlmp_registration *entry;
735 struct irmanager_event event;
736 __u8 *service;
737 int i = 0;
739 DEBUG(1, "IrDA Discovered: %s\n", discovery->info);
740 DEBUG(1, " Services: ");
742 service = irlmp_hint_to_service( discovery->hint);
743 if (service != NULL) {
745 * Check all services on the device
747 while ( service[i] != S_END) {
748 DEBUG( 4, "service=%02x\n", service[i]);
749 entry = hashbin_find( irlmp->registry,
750 service[i], NULL);
751 if ( entry && entry->discovery_callback) {
752 DEBUG( 4, "discovery_callback!\n");
753 entry->discovery_callback( discovery);
754 } else {
756 * Found no clients for dealing with this
757 * service, so ask the user space irmanager
758 * to try to load the right module for us
761 event.event = EVENT_DEVICE_DISCOVERED;
762 event.service = service[i];
763 event.daddr = discovery->daddr;
764 sprintf( event.info, "%s",
765 discovery->info);
766 irmanager_notify( &event);
768 i++; /* Next service */
770 kfree( service);
775 * Function irlmp_discovery_confirm ( self, log)
777 * Some device(s) answered to our discovery request! Check to see which
778 * device it is, and give indication to the client(s)
781 void irlmp_discovery_confirm( struct lap_cb *self, hashbin_t *log)
783 DISCOVERY *discovery;
785 DEBUG( 4, __FUNCTION__ "()\n");
787 ASSERT( self != NULL, return;);
788 ASSERT( self->magic == LMP_LAP_MAGIC, return;);
791 * Now, check all discovered devices (if any)
793 discovery = ( DISCOVERY *) hashbin_get_first( log);
794 while ( discovery != NULL) {
795 self->daddr = discovery->daddr;
797 DEBUG( 4, "discovery->daddr = 0x%08x\n", discovery->daddr);
799 irlmp_check_services( discovery);
801 discovery = ( DISCOVERY *) hashbin_get_next( log);
806 * Function irlmp_discovery_indication (discovery)
808 * A remote device is discovering us!
811 void irlmp_discovery_indication( struct lap_cb *self, DISCOVERY *discovery)
813 /* struct irda_event event; */
815 ASSERT( self != NULL, return;);
816 ASSERT( self->magic == LMP_LAP_MAGIC, return;);
817 ASSERT( discovery != NULL, return;);
819 DEBUG( 4, __FUNCTION__ "()\n");
821 DEBUG( 4, "discovery->daddr = 0x%08x\n", discovery->daddr);
822 self->daddr = discovery->daddr;
824 ASSERT( self->cachelog != NULL, return;);
827 * Insert this discovery device into the discovery_log if its
828 * not there already
830 if ( !hashbin_find( self->cachelog, discovery->daddr, NULL))
831 hashbin_insert( self->cachelog, (QUEUE *) discovery,
832 discovery->daddr, NULL);
834 irlmp_check_services( discovery);
838 * Function irlmp_get_discovery_response ()
840 * Used by IrLAP to get the disocvery info it needs when answering
841 * discovery requests by other devices.
843 DISCOVERY *irlmp_get_discovery_response()
845 DEBUG( 4, "irlmp_get_discovery_response()\n");
847 ASSERT( irlmp != NULL, return NULL;);
849 irlmp->discovery_rsp.hint[0] = irlmp->hint[0];
850 irlmp->discovery_rsp.hint[1] = irlmp->hint[1];
853 * Set character set for device name (we use ASCII), and
854 * copy device name. Remember to make room for a \0 at the
855 * end
857 irlmp->discovery_rsp.charset = CS_ASCII;
859 strncpy( irlmp->discovery_rsp.info, sysctl_devname, 31);
860 irlmp->discovery_rsp.info_len = strlen( irlmp->discovery_rsp.info) + 2;
862 return &irlmp->discovery_rsp;
866 * Function irlmp_data_request (self, skb)
868 * Send some data to peer device
871 void irlmp_data_request( struct lsap_cb *self, struct sk_buff *skb)
873 DEBUG( 4, __FUNCTION__ "()\n");
875 ASSERT( skb != NULL, return;);
876 ASSERT( self != NULL, return;);
877 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
879 /* Make room for MUX header */
880 ASSERT( skb_headroom( skb) >= LMP_HEADER, return;);
881 skb_push( skb, LMP_HEADER);
883 irlmp_do_lsap_event( self, LM_DATA_REQUEST, skb);
887 * Function irlmp_data_indication (handle, skb)
889 * Got data from LAP layer so pass it up to upper layer
892 void irlmp_data_indication( struct lsap_cb *self, struct sk_buff *skb)
894 DEBUG( 4, __FUNCTION__ "()\n");
896 ASSERT( self != NULL, return;);
897 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
898 ASSERT( skb != NULL, return;);
900 /* Hide LMP header from layer above */
901 skb_pull( skb, LMP_HEADER);
903 if ( self->notify.data_indication)
904 self->notify.data_indication(self->notify.instance, self, skb);
908 * Function irlmp_udata_request (self, skb)
913 inline void irlmp_udata_request( struct lsap_cb *self, struct sk_buff *skb)
915 DEBUG( 4, __FUNCTION__ "()\n");
917 ASSERT( skb != NULL, return;);
919 /* Make room for MUX header */
920 ASSERT( skb_headroom( skb) >= LMP_HEADER, return;);
921 skb_push( skb, LMP_HEADER);
923 irlmp_do_lsap_event( self, LM_UDATA_REQUEST, skb);
927 * Function irlmp_udata_indication (self, skb)
929 * Send unreliable data (but still within the connection)
932 void irlmp_udata_indication( struct lsap_cb *self, struct sk_buff *skb)
934 DEBUG( 4, __FUNCTION__ "()\n");
936 ASSERT( self != NULL, return;);
937 ASSERT( self->magic == LMP_LSAP_MAGIC, return;);
938 ASSERT( skb != NULL, return;);
940 /* Hide LMP header from layer above */
941 skb_pull( skb, LMP_HEADER);
943 if ( self->notify.udata_indication)
944 self->notify.udata_indication( self->notify.instance, self,
945 skb);
949 * Function irlmp_connection_less_data_request (skb)
951 * Send out of connection UI frames
954 void irlmp_connectionless_data_request( struct sk_buff *skb)
956 DEBUG( 1, __FUNCTION__ "(), Sorry not implemented\n");
960 * Function irlmp_connection_less_data_indication (skb)
965 void irlmp_connectionless_data_indication( struct sk_buff *skb)
967 DEBUG( 1, __FUNCTION__ "()\n");
970 void irlmp_status_request(void)
972 DEBUG( 1, "irlmp_status_request(), Not implemented\n");
975 void irlmp_status_indication( LINK_STATUS link, LOCK_STATUS lock)
977 DEBUG( 4, "irlmp_status_indication(), Not implemented\n");
981 * Function irlmp_hint_to_service (hint)
983 * Returns a list of all servics contained in the given hint bits. This
984 * funtion assumes that the hint bits have the size of two bytes only
986 __u8 *irlmp_hint_to_service(__u8 *hint)
988 __u8 *service;
989 int i = 0;
992 * Allocate array to store services in. 16 entries should be safe
993 * since we currently only support 2 hint bytes
995 service = kmalloc( 16, GFP_ATOMIC);
996 if ( !service) {
997 DEBUG(1, __FUNCTION__ "(), Unable to kmalloc!\n");
998 return NULL;
1001 if ( !hint[0]) {
1002 DEBUG(1, "<None>\n");
1003 return NULL;
1005 if ( hint[0] & HINT_PNP)
1006 DEBUG(1, "PnP Compatible ");
1007 if ( hint[0] & HINT_PDA)
1008 DEBUG(1, "PDA/Palmtop ");
1009 if ( hint[0] & HINT_COMPUTER)
1010 DEBUG(1, "Computer ");
1011 if ( hint[0] & HINT_PRINTER) {
1012 DEBUG(1, "Printer ");
1013 service[i++] = S_PRINTER;
1015 if ( hint[0] & HINT_MODEM)
1016 DEBUG(1, "Modem ");
1017 if ( hint[0] & HINT_FAX)
1018 DEBUG(1, "Fax ");
1019 if ( hint[0] & HINT_LAN) {
1020 DEBUG(1, "LAN Access ");
1021 service[i++] = S_LAN;
1024 * Test if extension byte exists. This byte will usually be
1025 * there, but this is not really required by the standard.
1026 * (IrLMP p. 29)
1028 if ( hint[0] & HINT_EXTENSION) {
1029 if ( hint[1] & HINT_TELEPHONY) {
1030 DEBUG(1, "Telephony ");
1031 service[i++] = S_TELEPHONY;
1032 } if ( hint[1] & HINT_FILE_SERVER)
1033 DEBUG(1, "File Server ");
1035 if ( hint[1] & HINT_COMM) {
1036 DEBUG(1, "IrCOMM ");
1037 service[i++] = S_COMM;
1039 if ( hint[1] & HINT_OBEX) {
1040 DEBUG(1, "IrOBEX ");
1041 service[i++] = S_OBEX;
1044 DEBUG(1, "\n");
1046 service[i] = S_END;
1048 return service;
1052 * Function irlmp_service_to_hint (service, hint)
1057 void irlmp_service_to_hint( int service, __u8 *hint)
1059 switch (service) {
1060 case S_PNP:
1061 hint[0] |= HINT_PNP;
1062 break;
1063 case S_PDA:
1064 hint[0] |= HINT_PDA;
1065 break;
1066 case S_COMPUTER:
1067 hint[0] |= HINT_COMPUTER;
1068 break;
1069 case S_PRINTER:
1070 hint[0] |= HINT_PRINTER;
1071 break;
1072 case S_MODEM:
1073 hint[0] |= HINT_PRINTER;
1074 break;
1075 case S_LAN:
1076 hint[0] |= HINT_LAN;
1077 break;
1078 case S_COMM:
1079 hint[0] |= HINT_EXTENSION;
1080 hint[1] |= HINT_COMM;
1081 break;
1082 case S_OBEX:
1083 hint[0] |= HINT_EXTENSION;
1084 hint[1] |= HINT_OBEX;
1085 break;
1086 default:
1087 DEBUG( 1, __FUNCTION__ "(), Unknown service!\n");
1088 break;
1093 * Function irlmp_register (service, type, callback)
1095 * Register a local client or server with IrLMP
1098 void irlmp_register_layer( int service, int type, int do_discovery,
1099 DISCOVERY_CALLBACK callback)
1101 struct irlmp_registration *entry;
1103 sysctl_discovery |= do_discovery;
1105 if ( type & SERVER)
1106 irlmp_service_to_hint( service, irlmp->hint);
1108 /* Check if this service has been registred before */
1109 entry = hashbin_find( irlmp->registry, service, NULL);
1110 if ( entry != NULL) {
1111 /* Update type in entry */
1112 entry->type |= type;
1114 /* Update callback only if client, since servers don't
1115 * use callbacks, and we don't want to overwrite a
1116 * previous registred client callback
1118 if ( type & CLIENT)
1119 entry->discovery_callback = callback;
1120 return;
1123 /* Make a new registration */
1124 entry = kmalloc( sizeof( struct irlmp_registration), GFP_ATOMIC);
1125 if ( !entry) {
1126 DEBUG( 1, __FUNCTION__ "(), Unable to kmalloc!\n");
1127 return;
1130 entry->service = service;
1131 entry->type = type;
1132 entry->discovery_callback = callback;
1134 hashbin_insert( irlmp->registry, (QUEUE*) entry, entry->service, NULL);
1138 * Function irlmp_unregister (serivice)
1143 void irlmp_unregister_layer( int service, int type)
1145 struct irlmp_registration *entry;
1147 DEBUG( 4, __FUNCTION__ "()\n");
1149 entry = hashbin_find( irlmp->registry, service, NULL);
1150 if ( entry != NULL) {
1151 DEBUG( 4, "Found entry to change or remove!\n");
1152 /* Remove this type from the service registration */
1153 entry->type &= ~type;
1156 if ( !entry) {
1157 DEBUG( 1, __FUNCTION__ "Unable to find entry to unregister!\n");
1158 return;
1162 * Remove entry if there is no more client and server support
1163 * left in entry
1165 if ( !entry->type) {
1166 DEBUG( 4, __FUNCTION__ "(), removing entry!\n");
1167 entry = hashbin_remove( irlmp->registry, service, NULL);
1168 if ( entry != NULL)
1169 kfree( entry);
1172 /* Remove old hint bits */
1173 irlmp->hint[0] = 0;
1174 irlmp->hint[1] = 0;
1176 /* Refresh current hint bits */
1177 entry = (struct irlmp_registration *) hashbin_get_first( irlmp->registry);
1178 while( entry != NULL) {
1179 if ( entry->type & SERVER)
1180 irlmp_service_to_hint( entry->service,
1181 irlmp->hint);
1182 entry = (struct irlmp_registration *)
1183 hashbin_get_next( irlmp->registry);
1188 * Function irlmp_slsap_inuse (slsap)
1190 * Check if the given source LSAP selector is in use
1192 int irlmp_slsap_inuse( __u8 slsap_sel)
1194 struct lsap_cb *self;
1195 struct lap_cb *lap;
1197 ASSERT( irlmp != NULL, return TRUE;);
1198 ASSERT( irlmp->magic == LMP_MAGIC, return TRUE;);
1199 ASSERT( slsap_sel != LSAP_ANY, return TRUE;);
1201 DEBUG( 4, __FUNCTION__ "()\n");
1204 * Check if slsap is already in use. To do this we have to loop over
1205 * every IrLAP connection and check every LSAP assosiated with each
1206 * the connection.
1208 lap = ( struct lap_cb *) hashbin_get_first( irlmp->links);
1209 while ( lap != NULL) {
1210 ASSERT( lap->magic == LMP_LAP_MAGIC, return TRUE;);
1212 self = (struct lsap_cb *) hashbin_get_first( lap->lsaps);
1213 while ( self != NULL) {
1214 ASSERT( self->magic == LMP_LSAP_MAGIC, return TRUE;);
1216 if (( self->slsap_sel == slsap_sel))/* && */
1217 /* ( self->dlsap_sel == LSAP_ANY)) */
1219 DEBUG( 4, "Source LSAP selector=%02x in use\n",
1220 self->slsap_sel);
1221 return TRUE;
1223 self = (struct lsap_cb*) hashbin_get_next( lap->lsaps);
1225 lap = (struct lap_cb *) hashbin_get_next( irlmp->links);
1227 return FALSE;
1231 * Function irlmp_find_free_slsap ()
1233 * Find a free source LSAP to use. This function is called if the service
1234 * user has requested a source LSAP equal to LM_ANY
1236 __u8 irlmp_find_free_slsap(void)
1238 __u8 lsap_sel;
1240 ASSERT( irlmp != NULL, return -1;);
1241 ASSERT( irlmp->magic == LMP_MAGIC, return -1;);
1243 lsap_sel = irlmp->free_lsap_sel++;
1245 DEBUG( 4, __FUNCTION__ "(), next free lsap_sel=%02x\n", lsap_sel);
1247 return lsap_sel;
1251 * Function irlmp_convert_lap_reason (lap_reason)
1253 * Converts IrLAP disconnect reason codes to IrLMP disconnect reason
1254 * codes
1257 LM_REASON irlmp_convert_lap_reason( LAP_REASON lap_reason)
1259 int reason = LM_LAP_DISCONNECT;
1261 switch (lap_reason) {
1262 case LAP_DISC_INDICATION: /* Received a disconnect request from peer */
1263 DEBUG( 1, __FUNCTION__ "(), LAP_DISC_INDICATION\n");
1264 reason = LM_USER_REQUEST;
1265 break;
1266 case LAP_NO_RESPONSE: /* To many retransmits without response */
1267 DEBUG( 1, __FUNCTION__ "(), LAP_NO_RESPONSE\n");
1268 reason = LM_LAP_DISCONNECT;
1269 break;
1270 case LAP_RESET_INDICATION:
1271 DEBUG( 1, __FUNCTION__ "(), LAP_RESET_INDICATION\n");
1272 reason = LM_LAP_RESET;
1273 break;
1274 case LAP_FOUND_NONE:
1275 case LAP_MEDIA_BUSY:
1276 case LAP_PRIMARY_CONFLICT:
1277 DEBUG( 1, __FUNCTION__ "(), LAP_FOUND_NONE, LAP_MEDIA_BUSY or LAP_PRIMARY_CONFLICT\n");
1278 reason = LM_CONNECT_FAILURE;
1279 break;
1280 default:
1281 DEBUG( 1, __FUNCTION__
1282 "(), Unknow IrLAP disconnect reason %d!\n", lap_reason);
1283 reason = LM_LAP_DISCONNECT;
1284 break;
1287 return reason;
1290 __u32 irlmp_get_saddr(struct lsap_cb *self)
1292 DEBUG(0, __FUNCTION__ "()\n");
1294 ASSERT(self != NULL, return 0;);
1295 ASSERT(self->lap != NULL, return 0;);
1297 return self->lap->saddr;
1300 __u32 irlmp_get_daddr(struct lsap_cb *self)
1302 DEBUG(0, __FUNCTION__ "()\n");
1304 ASSERT(self != NULL, return 0;);
1305 ASSERT(self->lap != NULL, return 0;);
1307 return self->lap->daddr;
1310 #ifdef CONFIG_PROC_FS
1312 * Function irlmp_proc_read (buf, start, offset, len, unused)
1314 * Give some info to the /proc file system
1317 int irlmp_proc_read( char *buf, char **start, off_t offset, int len,
1318 int unused)
1320 struct lsap_cb *self;
1321 struct lap_cb *lap;
1322 unsigned long flags;
1324 ASSERT( irlmp != NULL, return 0;);
1326 save_flags( flags);
1327 cli();
1329 len = 0;
1331 len += sprintf( buf+len, "Unconnected LSAPs:\n");
1332 self = (struct lsap_cb *) hashbin_get_first( irlmp->unconnected_lsaps);
1333 while ( self != NULL) {
1334 ASSERT( self->magic == LMP_LSAP_MAGIC, return 0;);
1335 len += sprintf( buf+len, "lsap state: %s, ",
1336 irlsap_state[ self->lsap_state]);
1337 len += sprintf( buf+len,
1338 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1339 self->slsap_sel, self->dlsap_sel);
1340 len += sprintf( buf+len, "(%s)", self->notify.name);
1341 len += sprintf( buf+len, "\n");
1343 self = ( struct lsap_cb *) hashbin_get_next(
1344 irlmp->unconnected_lsaps);
1347 len += sprintf( buf+len, "\nRegistred Link Layers:\n");
1348 lap = (struct lap_cb *) hashbin_get_first( irlmp->links);
1349 while ( lap != NULL) {
1350 ASSERT( lap->magic == LMP_LAP_MAGIC, return 0;);
1352 len += sprintf( buf+len, "lap state: %s, ",
1353 irlmp_state[ lap->lap_state]);
1355 len += sprintf( buf+len, "saddr: %#08x, daddr: %#08x, ",
1356 lap->saddr, lap->daddr);
1357 len += sprintf( buf+len, "\n");
1359 len += sprintf( buf+len, "\nConnected LSAPs:\n");
1360 self = (struct lsap_cb *) hashbin_get_first( lap->lsaps);
1361 while ( self != NULL) {
1362 ASSERT( self->magic == LMP_LSAP_MAGIC, return 0;);
1363 len += sprintf( buf+len, "lsap state: %s, ",
1364 irlsap_state[ self->lsap_state]);
1365 len += sprintf( buf+len,
1366 "slsap_sel: %#02x, dlsap_sel: %#02x, ",
1367 self->slsap_sel, self->dlsap_sel);
1368 len += sprintf( buf+len, "(%s)", self->notify.name);
1369 len += sprintf( buf+len, "\n");
1371 self = ( struct lsap_cb *) hashbin_get_next(
1372 lap->lsaps);
1375 lap = ( struct lap_cb *) hashbin_get_next(
1376 irlmp->links);
1379 restore_flags( flags);
1381 return len;
1384 #endif /* PROC_FS */