Merge git://git.infradead.org/hdrinstall-2.6
[linux-2.6] / net / irda / irlan / irlan_common.c
1 /*********************************************************************
2  *                
3  * Filename:      irlan_common.c
4  * Version:       0.9
5  * Description:   IrDA LAN Access Protocol Implementation
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Aug 31 20:14:37 1997
9  * Modified at:   Sun Dec 26 21:53:10 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * 
12  *     Copyright (c) 1997, 1999 Dag Brattli <dagb@cs.uit.no>, 
13  *     All Rights Reserved.
14  *     
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.
19  *
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.
23  *
24  ********************************************************************/
25
26 #include <linux/module.h>
27
28 #include <linux/kernel.h>
29 #include <linux/string.h>
30 #include <linux/init.h>
31 #include <linux/errno.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/random.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/moduleparam.h>
39 #include <linux/bitops.h>
40
41 #include <asm/system.h>
42 #include <asm/byteorder.h>
43
44 #include <net/irda/irda.h>
45 #include <net/irda/irttp.h>
46 #include <net/irda/irlmp.h>
47 #include <net/irda/iriap.h>
48 #include <net/irda/timer.h>
49
50 #include <net/irda/irlan_common.h>
51 #include <net/irda/irlan_client.h>
52 #include <net/irda/irlan_provider.h> 
53 #include <net/irda/irlan_eth.h>
54 #include <net/irda/irlan_filter.h>
55
56
57 /* 
58  * Send gratuitous ARP when connected to a new AP or not. May be a clever
59  * thing to do, but for some reason the machine crashes if you use DHCP. So
60  * lets not use it by default.
61  */
62 #undef CONFIG_IRLAN_SEND_GRATUITOUS_ARP
63
64 /* extern char sysctl_devname[]; */
65
66 /*
67  *  Master structure
68  */
69 static LIST_HEAD(irlans);
70
71 static void *ckey;
72 static void *skey;
73
74 /* Module parameters */
75 static int eth;   /* Use "eth" or "irlan" name for devices */
76 static int access = ACCESS_PEER; /* PEER, DIRECT or HOSTED */
77
78 #ifdef CONFIG_PROC_FS
79 static const char *irlan_access[] = {
80         "UNKNOWN",
81         "DIRECT",
82         "PEER",
83         "HOSTED"
84 };
85
86 static const char *irlan_media[] = {
87         "UNKNOWN",
88         "802.3",
89         "802.5"
90 };
91
92 extern struct proc_dir_entry *proc_irda;
93
94 static int irlan_seq_open(struct inode *inode, struct file *file);
95
96 static struct file_operations irlan_fops = {
97         .owner   = THIS_MODULE,
98         .open    = irlan_seq_open,
99         .read    = seq_read,
100         .llseek  = seq_lseek,
101         .release = seq_release,
102 };
103
104 extern struct proc_dir_entry *proc_irda;
105 #endif /* CONFIG_PROC_FS */
106
107 static struct irlan_cb *irlan_open(__u32 saddr, __u32 daddr);
108 static void __irlan_close(struct irlan_cb *self);
109 static int __irlan_insert_param(struct sk_buff *skb, char *param, int type, 
110                                 __u8 value_byte, __u16 value_short, 
111                                 __u8 *value_array, __u16 value_len);
112 static void irlan_open_unicast_addr(struct irlan_cb *self);
113 static void irlan_get_unicast_addr(struct irlan_cb *self);
114 void irlan_close_tsaps(struct irlan_cb *self);
115
116 /*
117  * Function irlan_init (void)
118  *
119  *    Initialize IrLAN layer
120  *
121  */
122 static int __init irlan_init(void)
123 {
124         struct irlan_cb *new;
125         __u16 hints;
126
127         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
128
129 #ifdef CONFIG_PROC_FS
130         { struct proc_dir_entry *proc;
131         proc = create_proc_entry("irlan", 0, proc_irda);
132         if (!proc) {
133                 printk(KERN_ERR "irlan_init: can't create /proc entry!\n");
134                 return -ENODEV;
135         }
136
137         proc->proc_fops = &irlan_fops;
138         }
139 #endif /* CONFIG_PROC_FS */
140
141         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
142         hints = irlmp_service_to_hint(S_LAN);
143
144         /* Register with IrLMP as a client */
145         ckey = irlmp_register_client(hints, &irlan_client_discovery_indication,
146                                      NULL, NULL);
147         
148         /* Register with IrLMP as a service */
149         skey = irlmp_register_service(hints);
150
151         /* Start the master IrLAN instance (the only one for now) */
152         new = irlan_open(DEV_ADDR_ANY, DEV_ADDR_ANY);
153
154         /* The master will only open its (listen) control TSAP */
155         irlan_provider_open_ctrl_tsap(new);
156
157         /* Do some fast discovery! */
158         irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
159
160         return 0;
161 }
162
163 static void __exit irlan_cleanup(void) 
164 {
165         struct irlan_cb *self, *next;
166
167         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
168
169         irlmp_unregister_client(ckey);
170         irlmp_unregister_service(skey);
171
172 #ifdef CONFIG_PROC_FS
173         remove_proc_entry("irlan", proc_irda);
174 #endif /* CONFIG_PROC_FS */
175
176         /* Cleanup any leftover network devices */
177         rtnl_lock();
178         list_for_each_entry_safe(self, next, &irlans, dev_list) {
179                 __irlan_close(self);
180         }
181         rtnl_unlock();
182 }
183
184 /*
185  * Function irlan_open (void)
186  *
187  *    Open new instance of a client/provider, we should only register the 
188  *    network device if this instance is ment for a particular client/provider
189  */
190 static struct irlan_cb *irlan_open(__u32 saddr, __u32 daddr)
191 {
192         struct net_device *dev;
193         struct irlan_cb *self;
194
195         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
196
197         /* Create network device with irlan */
198         dev = alloc_irlandev(eth ? "eth%d" : "irlan%d");
199         if (!dev)
200                 return NULL;
201
202         self = dev->priv;
203         self->dev = dev;
204
205         /*
206          *  Initialize local device structure
207          */
208         self->magic = IRLAN_MAGIC;
209         self->saddr = saddr;
210         self->daddr = daddr;
211
212         /* Provider access can only be PEER, DIRECT, or HOSTED */
213         self->provider.access_type = access;
214         if (access == ACCESS_DIRECT) {
215                 /*  
216                  * Since we are emulating an IrLAN sever we will have to
217                  * give ourself an ethernet address!  
218                  */
219                 dev->dev_addr[0] = 0x40;
220                 dev->dev_addr[1] = 0x00;
221                 dev->dev_addr[2] = 0x00;
222                 dev->dev_addr[3] = 0x00;
223                 get_random_bytes(dev->dev_addr+4, 1);
224                 get_random_bytes(dev->dev_addr+5, 1);
225         }
226
227         self->media = MEDIA_802_3;
228         self->disconnect_reason = LM_USER_REQUEST;
229         init_timer(&self->watchdog_timer);
230         init_timer(&self->client.kick_timer);
231         init_waitqueue_head(&self->open_wait);  
232         
233         skb_queue_head_init(&self->client.txq);
234         
235         irlan_next_client_state(self, IRLAN_IDLE);
236         irlan_next_provider_state(self, IRLAN_IDLE);
237
238         if (register_netdev(dev)) {
239                 IRDA_DEBUG(2, "%s(), register_netdev() failed!\n", 
240                            __FUNCTION__ );
241                 self = NULL;
242                 free_netdev(dev);
243         } else {
244                 rtnl_lock();
245                 list_add_rcu(&self->dev_list, &irlans);
246                 rtnl_unlock();
247         }
248
249         return self;
250 }
251 /*
252  * Function __irlan_close (self)
253  *
254  *    This function closes and deallocates the IrLAN client instances. Be 
255  *    aware that other functions which calls client_close() must
256  *    remove self from irlans list first.
257  */
258 static void __irlan_close(struct irlan_cb *self)
259 {
260         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
261         
262         ASSERT_RTNL();
263         IRDA_ASSERT(self != NULL, return;);
264         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
265
266         del_timer_sync(&self->watchdog_timer);
267         del_timer_sync(&self->client.kick_timer);
268
269         /* Close all open connections and remove TSAPs */
270         irlan_close_tsaps(self);
271         
272         if (self->client.iriap) 
273                 iriap_close(self->client.iriap);
274
275         /* Remove frames queued on the control channel */
276         skb_queue_purge(&self->client.txq);
277
278         /* Unregister and free self via destructor */
279         unregister_netdevice(self->dev);
280 }
281
282 /* Find any instance of irlan, used for client discovery wakeup */
283 struct irlan_cb *irlan_get_any(void)
284 {
285         struct irlan_cb *self;
286
287         list_for_each_entry_rcu(self, &irlans, dev_list) {
288                 return self;
289         }
290         return NULL;
291 }
292
293 /*
294  * Function irlan_connect_indication (instance, sap, qos, max_sdu_size, skb)
295  *
296  *    Here we receive the connect indication for the data channel
297  *
298  */
299 static void irlan_connect_indication(void *instance, void *sap,
300                                      struct qos_info *qos,
301                                      __u32 max_sdu_size,
302                                      __u8 max_header_size, 
303                                      struct sk_buff *skb)
304 {
305         struct irlan_cb *self;
306         struct tsap_cb *tsap;
307
308         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
309         
310         self = (struct irlan_cb *) instance;
311         tsap = (struct tsap_cb *) sap;
312         
313         IRDA_ASSERT(self != NULL, return;);
314         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
315         IRDA_ASSERT(tsap == self->tsap_data,return;);
316
317         self->max_sdu_size = max_sdu_size;
318         self->max_header_size = max_header_size;
319
320         IRDA_DEBUG(0, "%s: We are now connected!\n", __FUNCTION__);
321
322         del_timer(&self->watchdog_timer);
323
324         /* If you want to pass the skb to *both* state machines, you will
325          * need to skb_clone() it, so that you don't free it twice.
326          * As the state machines don't need it, git rid of it here...
327          * Jean II */
328         if (skb)
329                 dev_kfree_skb(skb);
330
331         irlan_do_provider_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
332         irlan_do_client_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
333
334         if (self->provider.access_type == ACCESS_PEER) {
335                 /* 
336                  * Data channel is open, so we are now allowed to
337                  * configure the remote filter 
338                  */
339                 irlan_get_unicast_addr(self);
340                 irlan_open_unicast_addr(self);
341         }
342         /* Ready to transfer Ethernet frames (at last) */
343         netif_start_queue(self->dev); /* Clear reason */
344 }
345
346 static void irlan_connect_confirm(void *instance, void *sap,
347                                   struct qos_info *qos, 
348                                   __u32 max_sdu_size,
349                                   __u8 max_header_size, 
350                                   struct sk_buff *skb) 
351 {
352         struct irlan_cb *self;
353
354         self = (struct irlan_cb *) instance;
355
356         IRDA_ASSERT(self != NULL, return;);
357         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
358
359         self->max_sdu_size = max_sdu_size;
360         self->max_header_size = max_header_size;
361
362         /* TODO: we could set the MTU depending on the max_sdu_size */
363
364         IRDA_DEBUG(0, "%s: We are now connected!\n", __FUNCTION__);
365         del_timer(&self->watchdog_timer);
366
367         /* 
368          * Data channel is open, so we are now allowed to configure the remote
369          * filter 
370          */
371         irlan_get_unicast_addr(self);
372         irlan_open_unicast_addr(self);
373         
374         /* Open broadcast and multicast filter by default */
375         irlan_set_broadcast_filter(self, TRUE);
376         irlan_set_multicast_filter(self, TRUE);
377
378         /* Ready to transfer Ethernet frames */
379         netif_start_queue(self->dev);
380         self->disconnect_reason = 0; /* Clear reason */
381 #ifdef CONFIG_IRLAN_SEND_GRATUITOUS_ARP
382         irlan_eth_send_gratuitous_arp(&self->dev);
383 #endif
384         wake_up_interruptible(&self->open_wait);
385 }
386
387 /*
388  * Function irlan_client_disconnect_indication (handle)
389  *
390  *    Callback function for the IrTTP layer. Indicates a disconnection of
391  *    the specified connection (handle)
392  */
393 static void irlan_disconnect_indication(void *instance,
394                                         void *sap, LM_REASON reason, 
395                                         struct sk_buff *userdata) 
396 {
397         struct irlan_cb *self;
398         struct tsap_cb *tsap;
399
400         IRDA_DEBUG(0, "%s(), reason=%d\n", __FUNCTION__ , reason);
401         
402         self = (struct irlan_cb *) instance;
403         tsap = (struct tsap_cb *) sap;
404
405         IRDA_ASSERT(self != NULL, return;);
406         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);       
407         IRDA_ASSERT(tsap != NULL, return;);
408         IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
409         
410         IRDA_ASSERT(tsap == self->tsap_data, return;);
411
412         IRDA_DEBUG(2, "IrLAN, data channel disconnected by peer!\n");
413
414         /* Save reason so we know if we should try to reconnect or not */
415         self->disconnect_reason = reason;
416         
417         switch (reason) {
418         case LM_USER_REQUEST: /* User request */
419                 IRDA_DEBUG(2, "%s(), User requested\n", __FUNCTION__ );
420                 break;
421         case LM_LAP_DISCONNECT: /* Unexpected IrLAP disconnect */
422                 IRDA_DEBUG(2, "%s(), Unexpected IrLAP disconnect\n", __FUNCTION__ );
423                 break;
424         case LM_CONNECT_FAILURE: /* Failed to establish IrLAP connection */
425                 IRDA_DEBUG(2, "%s(), IrLAP connect failed\n", __FUNCTION__ );
426                 break;
427         case LM_LAP_RESET:  /* IrLAP reset */
428                 IRDA_DEBUG(2, "%s(), IrLAP reset\n", __FUNCTION__ );
429                 break;
430         case LM_INIT_DISCONNECT:
431                 IRDA_DEBUG(2, "%s(), IrLMP connect failed\n", __FUNCTION__ );
432                 break;
433         default:
434                 IRDA_ERROR("%s(), Unknown disconnect reason\n", __FUNCTION__);
435                 break;
436         }
437         
438         /* If you want to pass the skb to *both* state machines, you will
439          * need to skb_clone() it, so that you don't free it twice.
440          * As the state machines don't need it, git rid of it here...
441          * Jean II */
442         if (userdata)
443                 dev_kfree_skb(userdata);
444
445         irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
446         irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
447         
448         wake_up_interruptible(&self->open_wait);
449 }
450
451 void irlan_open_data_tsap(struct irlan_cb *self)
452 {
453         struct tsap_cb *tsap;
454         notify_t notify;
455
456         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
457
458         IRDA_ASSERT(self != NULL, return;);
459         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
460
461         /* Check if already open */
462         if (self->tsap_data)
463                 return;
464
465         irda_notify_init(&notify);
466         
467         notify.data_indication       = irlan_eth_receive;
468         notify.udata_indication      = irlan_eth_receive;
469         notify.connect_indication    = irlan_connect_indication;
470         notify.connect_confirm       = irlan_connect_confirm;
471         notify.flow_indication       = irlan_eth_flow_indication;
472         notify.disconnect_indication = irlan_disconnect_indication;
473         notify.instance              = self;
474         strlcpy(notify.name, "IrLAN data", sizeof(notify.name));
475
476         tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
477         if (!tsap) {
478                 IRDA_DEBUG(2, "%s(), Got no tsap!\n", __FUNCTION__ );
479                 return;
480         }
481         self->tsap_data = tsap;
482
483         /* 
484          *  This is the data TSAP selector which we will pass to the client
485          *  when the client ask for it.
486          */
487         self->stsap_sel_data = self->tsap_data->stsap_sel;
488 }
489
490 void irlan_close_tsaps(struct irlan_cb *self)
491 {
492         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
493
494         IRDA_ASSERT(self != NULL, return;);
495         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
496
497         /* Disconnect and close all open TSAP connections */
498         if (self->tsap_data) {
499                 irttp_disconnect_request(self->tsap_data, NULL, P_NORMAL);
500                 irttp_close_tsap(self->tsap_data);
501                 self->tsap_data = NULL;
502         }
503         if (self->client.tsap_ctrl) {
504                 irttp_disconnect_request(self->client.tsap_ctrl, NULL, 
505                                          P_NORMAL);
506                 irttp_close_tsap(self->client.tsap_ctrl);
507                 self->client.tsap_ctrl = NULL;
508         }
509         if (self->provider.tsap_ctrl) {
510                 irttp_disconnect_request(self->provider.tsap_ctrl, NULL, 
511                                          P_NORMAL);
512                 irttp_close_tsap(self->provider.tsap_ctrl);
513                 self->provider.tsap_ctrl = NULL;
514         }
515         self->disconnect_reason = LM_USER_REQUEST;
516 }
517
518 /*
519  * Function irlan_ias_register (self, tsap_sel)
520  *
521  *    Register with LM-IAS
522  *
523  */
524 void irlan_ias_register(struct irlan_cb *self, __u8 tsap_sel)
525 {
526         struct ias_object *obj;
527         struct ias_value *new_value;
528
529         IRDA_ASSERT(self != NULL, return;);
530         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
531         
532         /* 
533          * Check if object has already been registered by a previous provider.
534          * If that is the case, we just change the value of the attribute
535          */
536         if (!irias_find_object("IrLAN")) {
537                 obj = irias_new_object("IrLAN", IAS_IRLAN_ID);
538                 irias_add_integer_attrib(obj, "IrDA:TinyTP:LsapSel", tsap_sel,
539                                          IAS_KERNEL_ATTR);
540                 irias_insert_object(obj);
541         } else {
542                 new_value = irias_new_integer_value(tsap_sel);
543                 irias_object_change_attribute("IrLAN", "IrDA:TinyTP:LsapSel",
544                                               new_value);
545         }
546         
547         /* Register PnP object only if not registered before */
548         if (!irias_find_object("PnP")) {
549                 obj = irias_new_object("PnP", IAS_PNP_ID);
550 #if 0
551                 irias_add_string_attrib(obj, "Name", sysctl_devname,
552                                         IAS_KERNEL_ATTR);
553 #else
554                 irias_add_string_attrib(obj, "Name", "Linux", IAS_KERNEL_ATTR);
555 #endif
556                 irias_add_string_attrib(obj, "DeviceID", "HWP19F0",
557                                         IAS_KERNEL_ATTR);
558                 irias_add_integer_attrib(obj, "CompCnt", 1, IAS_KERNEL_ATTR);
559                 if (self->provider.access_type == ACCESS_PEER)
560                         irias_add_string_attrib(obj, "Comp#01", "PNP8389",
561                                                 IAS_KERNEL_ATTR);
562                 else
563                         irias_add_string_attrib(obj, "Comp#01", "PNP8294",
564                                                 IAS_KERNEL_ATTR);
565
566                 irias_add_string_attrib(obj, "Manufacturer",
567                                         "Linux-IrDA Project", IAS_KERNEL_ATTR);
568                 irias_insert_object(obj);
569         }
570 }
571
572 /*
573  * Function irlan_run_ctrl_tx_queue (self)
574  *
575  *    Try to send the next command in the control transmit queue
576  *
577  */
578 int irlan_run_ctrl_tx_queue(struct irlan_cb *self)
579 {
580         struct sk_buff *skb;
581
582         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
583
584         if (irda_lock(&self->client.tx_busy) == FALSE)
585                 return -EBUSY;
586
587         skb = skb_dequeue(&self->client.txq);
588         if (!skb) {
589                 self->client.tx_busy = FALSE;
590                 return 0;
591         }
592         
593         /* Check that it's really possible to send commands */
594         if ((self->client.tsap_ctrl == NULL) || 
595             (self->client.state == IRLAN_IDLE)) 
596         {
597                 self->client.tx_busy = FALSE;
598                 dev_kfree_skb(skb);
599                 return -1;
600         }
601         IRDA_DEBUG(2, "%s(), sending ...\n", __FUNCTION__ );
602
603         return irttp_data_request(self->client.tsap_ctrl, skb);
604 }
605
606 /*
607  * Function irlan_ctrl_data_request (self, skb)
608  *
609  *    This function makes sure that commands on the control channel is being
610  *    sent in a command/response fashion
611  */
612 static void irlan_ctrl_data_request(struct irlan_cb *self, struct sk_buff *skb)
613 {
614         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
615
616         /* Queue command */
617         skb_queue_tail(&self->client.txq, skb);
618
619         /* Try to send command */
620         irlan_run_ctrl_tx_queue(self);
621 }
622
623 /*
624  * Function irlan_get_provider_info (self)
625  *
626  *    Send Get Provider Information command to peer IrLAN layer
627  *
628  */
629 void irlan_get_provider_info(struct irlan_cb *self)
630 {
631         struct sk_buff *skb;
632         __u8 *frame;
633
634         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
635         
636         IRDA_ASSERT(self != NULL, return;);
637         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
638
639         skb = dev_alloc_skb(64);
640         if (!skb)
641                 return;
642
643         /* Reserve space for TTP, LMP, and LAP header */
644         skb_reserve(skb, self->client.max_header_size);
645         skb_put(skb, 2);
646         
647         frame = skb->data;
648         
649         frame[0] = CMD_GET_PROVIDER_INFO;
650         frame[1] = 0x00;                 /* Zero parameters */
651         
652         irlan_ctrl_data_request(self, skb);
653 }
654
655 /*
656  * Function irlan_open_data_channel (self)
657  *
658  *    Send an Open Data Command to provider
659  *
660  */
661 void irlan_open_data_channel(struct irlan_cb *self) 
662 {
663         struct sk_buff *skb;
664         __u8 *frame;
665         
666         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
667
668         IRDA_ASSERT(self != NULL, return;);
669         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
670         
671         skb = dev_alloc_skb(64);
672         if (!skb)
673                 return;
674
675         skb_reserve(skb, self->client.max_header_size);
676         skb_put(skb, 2);
677         
678         frame = skb->data;
679         
680         /* Build frame */
681         frame[0] = CMD_OPEN_DATA_CHANNEL;
682         frame[1] = 0x02; /* Two parameters */
683
684         irlan_insert_string_param(skb, "MEDIA", "802.3");
685         irlan_insert_string_param(skb, "ACCESS_TYPE", "DIRECT");
686         /* irlan_insert_string_param(skb, "MODE", "UNRELIABLE"); */
687
688 /*      self->use_udata = TRUE; */
689
690         irlan_ctrl_data_request(self, skb);
691 }
692
693 void irlan_close_data_channel(struct irlan_cb *self) 
694 {
695         struct sk_buff *skb;
696         __u8 *frame;
697         
698         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
699
700         IRDA_ASSERT(self != NULL, return;);
701         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
702
703         /* Check if the TSAP is still there */
704         if (self->client.tsap_ctrl == NULL)
705                 return;
706
707         skb = dev_alloc_skb(64);
708         if (!skb)
709                 return;
710
711         skb_reserve(skb, self->client.max_header_size);
712         skb_put(skb, 2);
713         
714         frame = skb->data;
715         
716         /* Build frame */
717         frame[0] = CMD_CLOSE_DATA_CHAN;
718         frame[1] = 0x01; /* Two parameters */
719
720         irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
721
722         irlan_ctrl_data_request(self, skb);
723 }
724
725 /*
726  * Function irlan_open_unicast_addr (self)
727  *
728  *    Make IrLAN provider accept ethernet frames addressed to the unicast 
729  *    address.
730  *
731  */
732 static void irlan_open_unicast_addr(struct irlan_cb *self)
733 {
734         struct sk_buff *skb;
735         __u8 *frame;
736         
737         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
738
739         IRDA_ASSERT(self != NULL, return;);
740         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);       
741         
742         skb = dev_alloc_skb(128);
743         if (!skb)
744                 return;
745
746         /* Reserve space for TTP, LMP, and LAP header */
747         skb_reserve(skb, self->max_header_size);
748         skb_put(skb, 2);
749         
750         frame = skb->data;
751         
752         frame[0] = CMD_FILTER_OPERATION;
753         frame[1] = 0x03;                 /* Three parameters */
754         irlan_insert_byte_param(skb, "DATA_CHAN" , self->dtsap_sel_data);
755         irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
756         irlan_insert_string_param(skb, "FILTER_MODE", "FILTER"); 
757         
758         irlan_ctrl_data_request(self, skb);
759 }
760
761 /*
762  * Function irlan_set_broadcast_filter (self, status)
763  *
764  *    Make IrLAN provider accept ethernet frames addressed to the broadcast
765  *    address. Be careful with the use of this one, since there may be a lot
766  *    of broadcast traffic out there. We can still function without this
767  *    one but then _we_ have to initiate all communication with other
768  *    hosts, since ARP request for this host will not be answered.
769  */
770 void irlan_set_broadcast_filter(struct irlan_cb *self, int status) 
771 {
772         struct sk_buff *skb;
773         __u8 *frame;
774         
775         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
776
777         IRDA_ASSERT(self != NULL, return;);
778         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
779         
780         skb = dev_alloc_skb(128);
781         if (!skb)
782                 return;
783
784         /* Reserve space for TTP, LMP, and LAP header */
785         skb_reserve(skb, self->client.max_header_size);
786         skb_put(skb, 2);
787         
788         frame = skb->data;
789         
790         frame[0] = CMD_FILTER_OPERATION;
791         frame[1] = 0x03;                 /* Three parameters */
792         irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
793         irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
794         if (status)
795                 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER"); 
796         else
797                 irlan_insert_string_param(skb, "FILTER_MODE", "NONE"); 
798
799         irlan_ctrl_data_request(self, skb);
800 }
801
802 /*
803  * Function irlan_set_multicast_filter (self, status)
804  *
805  *    Make IrLAN provider accept ethernet frames addressed to the multicast
806  *    address. 
807  *
808  */
809 void irlan_set_multicast_filter(struct irlan_cb *self, int status) 
810 {
811         struct sk_buff *skb;
812         __u8 *frame;
813         
814         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
815
816         IRDA_ASSERT(self != NULL, return;);
817         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
818
819         skb = dev_alloc_skb(128);
820         if (!skb)
821                 return;
822         
823         /* Reserve space for TTP, LMP, and LAP header */
824         skb_reserve(skb, self->client.max_header_size);
825         skb_put(skb, 2);
826         
827         frame = skb->data;
828         
829         frame[0] = CMD_FILTER_OPERATION;
830         frame[1] = 0x03;                 /* Three parameters */
831         irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
832         irlan_insert_string_param(skb, "FILTER_TYPE", "MULTICAST");
833         if (status)
834                 irlan_insert_string_param(skb, "FILTER_MODE", "ALL"); 
835         else
836                 irlan_insert_string_param(skb, "FILTER_MODE", "NONE"); 
837
838         irlan_ctrl_data_request(self, skb);
839 }
840
841 /*
842  * Function irlan_get_unicast_addr (self)
843  *
844  *    Retrieves the unicast address from the IrLAN provider. This address
845  *    will be inserted into the devices structure, so the ethernet layer
846  *    can construct its packets.
847  *
848  */
849 static void irlan_get_unicast_addr(struct irlan_cb *self)
850 {
851         struct sk_buff *skb;
852         __u8 *frame;
853                 
854         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
855
856         IRDA_ASSERT(self != NULL, return;);
857         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
858         
859         skb = dev_alloc_skb(128);
860         if (!skb)
861                 return;
862
863         /* Reserve space for TTP, LMP, and LAP header */
864         skb_reserve(skb, self->client.max_header_size);
865         skb_put(skb, 2);
866         
867         frame = skb->data;
868         
869         frame[0] = CMD_FILTER_OPERATION;
870         frame[1] = 0x03;                 /* Three parameters */
871         irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
872         irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
873         irlan_insert_string_param(skb, "FILTER_OPERATION", "DYNAMIC"); 
874         
875         irlan_ctrl_data_request(self, skb);
876 }
877
878 /*
879  * Function irlan_get_media_char (self)
880  *
881  *    
882  *
883  */
884 void irlan_get_media_char(struct irlan_cb *self) 
885 {
886         struct sk_buff *skb;
887         __u8 *frame;
888         
889         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
890
891         IRDA_ASSERT(self != NULL, return;);
892         IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
893         
894         skb = dev_alloc_skb(64);
895         if (!skb)
896                 return;
897
898         /* Reserve space for TTP, LMP, and LAP header */
899         skb_reserve(skb, self->client.max_header_size);
900         skb_put(skb, 2);
901         
902         frame = skb->data;
903         
904         /* Build frame */
905         frame[0] = CMD_GET_MEDIA_CHAR;
906         frame[1] = 0x01; /* One parameter */
907         
908         irlan_insert_string_param(skb, "MEDIA", "802.3");
909         irlan_ctrl_data_request(self, skb);
910 }
911
912 /*
913  * Function insert_byte_param (skb, param, value)
914  *
915  *    Insert byte parameter into frame
916  *
917  */
918 int irlan_insert_byte_param(struct sk_buff *skb, char *param, __u8 value)
919 {
920         return __irlan_insert_param(skb, param, IRLAN_BYTE, value, 0, NULL, 0);
921 }
922
923 int irlan_insert_short_param(struct sk_buff *skb, char *param, __u16 value)
924 {
925         return __irlan_insert_param(skb, param, IRLAN_SHORT, 0, value, NULL, 0);
926 }
927
928 /*
929  * Function insert_string (skb, param, value)
930  *
931  *    Insert string parameter into frame
932  *
933  */
934 int irlan_insert_string_param(struct sk_buff *skb, char *param, char *string)
935 {
936         int string_len = strlen(string);
937
938         return __irlan_insert_param(skb, param, IRLAN_ARRAY, 0, 0, string, 
939                                     string_len);
940 }
941
942 /*
943  * Function insert_array_param(skb, param, value, len_value)
944  *
945  *    Insert array parameter into frame
946  *
947  */
948 int irlan_insert_array_param(struct sk_buff *skb, char *name, __u8 *array,
949                              __u16 array_len)
950 {
951         return __irlan_insert_param(skb, name, IRLAN_ARRAY, 0, 0, array, 
952                                     array_len);
953 }
954
955 /*
956  * Function insert_param (skb, param, value, byte)
957  *
958  *    Insert parameter at end of buffer, structure of a parameter is:
959  *
960  *    -----------------------------------------------------------------------
961  *    | Name Length[1] | Param Name[1..255] | Val Length[2] | Value[0..1016]|
962  *    -----------------------------------------------------------------------
963  */
964 static int __irlan_insert_param(struct sk_buff *skb, char *param, int type, 
965                                 __u8 value_byte, __u16 value_short, 
966                                 __u8 *value_array, __u16 value_len)
967 {
968         __u8 *frame;
969         __u8 param_len;
970         __u16 tmp_le; /* Temporary value in little endian format */
971         int n=0;
972         
973         if (skb == NULL) {
974                 IRDA_DEBUG(2, "%s(), Got NULL skb\n", __FUNCTION__ );
975                 return 0;
976         }       
977
978         param_len = strlen(param);
979         switch (type) {
980         case IRLAN_BYTE:
981                 value_len = 1;
982                 break;
983         case IRLAN_SHORT:
984                 value_len = 2;
985                 break;
986         case IRLAN_ARRAY:
987                 IRDA_ASSERT(value_array != NULL, return 0;);
988                 IRDA_ASSERT(value_len > 0, return 0;);
989                 break;
990         default:
991                 IRDA_DEBUG(2, "%s(), Unknown parameter type!\n", __FUNCTION__ );
992                 return 0;
993                 break;
994         }
995         
996         /* Insert at end of sk-buffer */
997         frame = skb->tail;
998
999         /* Make space for data */
1000         if (skb_tailroom(skb) < (param_len+value_len+3)) {
1001                 IRDA_DEBUG(2, "%s(), No more space at end of skb\n", __FUNCTION__ );
1002                 return 0;
1003         }       
1004         skb_put(skb, param_len+value_len+3);
1005         
1006         /* Insert parameter length */
1007         frame[n++] = param_len;
1008         
1009         /* Insert parameter */
1010         memcpy(frame+n, param, param_len); n += param_len;
1011         
1012         /* Insert value length (2 byte little endian format, LSB first) */
1013         tmp_le = cpu_to_le16(value_len);
1014         memcpy(frame+n, &tmp_le, 2); n += 2; /* To avoid alignment problems */
1015
1016         /* Insert value */
1017         switch (type) {
1018         case IRLAN_BYTE:
1019                 frame[n++] = value_byte;
1020                 break;
1021         case IRLAN_SHORT:
1022                 tmp_le = cpu_to_le16(value_short);
1023                 memcpy(frame+n, &tmp_le, 2); n += 2;
1024                 break;
1025         case IRLAN_ARRAY:
1026                 memcpy(frame+n, value_array, value_len); n+=value_len;
1027                 break;
1028         default:
1029                 break;
1030         }
1031         IRDA_ASSERT(n == (param_len+value_len+3), return 0;);
1032
1033         return param_len+value_len+3;
1034 }
1035
1036 /*
1037  * Function irlan_extract_param (buf, name, value, len)
1038  *
1039  *    Extracts a single parameter name/value pair from buffer and updates
1040  *    the buffer pointer to point to the next name/value pair. 
1041  */
1042 int irlan_extract_param(__u8 *buf, char *name, char *value, __u16 *len)
1043 {
1044         __u8 name_len;
1045         __u16 val_len;
1046         int n=0;
1047         
1048         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
1049         
1050         /* get length of parameter name (1 byte) */
1051         name_len = buf[n++];
1052         
1053         if (name_len > 254) {
1054                 IRDA_DEBUG(2, "%s(), name_len > 254\n", __FUNCTION__ );
1055                 return -RSP_INVALID_COMMAND_FORMAT;
1056         }
1057         
1058         /* get parameter name */
1059         memcpy(name, buf+n, name_len);
1060         name[name_len] = '\0';
1061         n+=name_len;
1062         
1063         /*  
1064          *  Get length of parameter value (2 bytes in little endian 
1065          *  format) 
1066          */
1067         memcpy(&val_len, buf+n, 2); /* To avoid alignment problems */
1068         le16_to_cpus(&val_len); n+=2;
1069         
1070         if (val_len > 1016) {
1071                 IRDA_DEBUG(2, "%s(), parameter length to long\n", __FUNCTION__ );
1072                 return -RSP_INVALID_COMMAND_FORMAT;
1073         }
1074         *len = val_len;
1075
1076         /* get parameter value */
1077         memcpy(value, buf+n, val_len);
1078         value[val_len] = '\0';
1079         n+=val_len;
1080         
1081         IRDA_DEBUG(4, "Parameter: %s ", name); 
1082         IRDA_DEBUG(4, "Value: %s\n", value); 
1083
1084         return n;
1085 }
1086
1087 #ifdef CONFIG_PROC_FS
1088
1089 /*
1090  * Start of reading /proc entries.
1091  * Return entry at pos, 
1092  *      or start_token to indicate print header line
1093  *      or NULL if end of file
1094  */
1095 static void *irlan_seq_start(struct seq_file *seq, loff_t *pos)
1096 {
1097         int i = 1;
1098         struct irlan_cb *self;
1099
1100         rcu_read_lock();
1101         if (*pos == 0)
1102                 return SEQ_START_TOKEN;
1103
1104         list_for_each_entry(self, &irlans, dev_list) {
1105                 if (*pos == i) 
1106                         return self;
1107                 ++i;
1108         }
1109         return NULL;
1110 }
1111
1112 /* Return entry after v, and increment pos */
1113 static void *irlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1114 {
1115         struct list_head *nxt;
1116
1117         ++*pos;
1118         if (v == SEQ_START_TOKEN) 
1119                 nxt = irlans.next;
1120         else
1121                 nxt = ((struct irlan_cb *)v)->dev_list.next;
1122
1123         return (nxt == &irlans) ? NULL 
1124                 : list_entry(nxt, struct irlan_cb, dev_list);
1125 }
1126
1127 /* End of reading /proc file */
1128 static void irlan_seq_stop(struct seq_file *seq, void *v)
1129 {
1130         rcu_read_unlock();
1131 }
1132
1133
1134 /*
1135  * Show one entry in /proc file.
1136  */
1137 static int irlan_seq_show(struct seq_file *seq, void *v)
1138 {
1139         if (v == SEQ_START_TOKEN)
1140                 seq_puts(seq, "IrLAN instances:\n");
1141         else {
1142                 struct irlan_cb *self = v;
1143                 
1144                 IRDA_ASSERT(self != NULL, return -1;);
1145                 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
1146
1147                 seq_printf(seq,"ifname: %s,\n",
1148                                self->dev->name);
1149                 seq_printf(seq,"client state: %s, ",
1150                                irlan_state[ self->client.state]);
1151                 seq_printf(seq,"provider state: %s,\n",
1152                                irlan_state[ self->provider.state]);
1153                 seq_printf(seq,"saddr: %#08x, ",
1154                                self->saddr);
1155                 seq_printf(seq,"daddr: %#08x\n",
1156                                self->daddr);
1157                 seq_printf(seq,"version: %d.%d,\n",
1158                                self->version[1], self->version[0]);
1159                 seq_printf(seq,"access type: %s\n", 
1160                                irlan_access[self->client.access_type]);
1161                 seq_printf(seq,"media: %s\n", 
1162                                irlan_media[self->media]);
1163                 
1164                 seq_printf(seq,"local filter:\n");
1165                 seq_printf(seq,"remote filter: ");
1166                 irlan_print_filter(seq, self->client.filter_type);
1167                 seq_printf(seq,"tx busy: %s\n", 
1168                                netif_queue_stopped(self->dev) ? "TRUE" : "FALSE");
1169                         
1170                 seq_putc(seq,'\n');
1171         }
1172         return 0;
1173 }
1174
1175 static struct seq_operations irlan_seq_ops = {
1176         .start = irlan_seq_start,
1177         .next  = irlan_seq_next,
1178         .stop  = irlan_seq_stop,
1179         .show  = irlan_seq_show,
1180 };
1181
1182 static int irlan_seq_open(struct inode *inode, struct file *file)
1183 {
1184         return seq_open(file, &irlan_seq_ops);
1185 }
1186 #endif
1187
1188 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1189 MODULE_DESCRIPTION("The Linux IrDA LAN protocol"); 
1190 MODULE_LICENSE("GPL");
1191
1192 module_param(eth, bool, 0);
1193 MODULE_PARM_DESC(eth, "Name devices ethX (0) or irlanX (1)");
1194 module_param(access, int, 0);
1195 MODULE_PARM_DESC(access, "Access type DIRECT=1, PEER=2, HOSTED=3");
1196
1197 module_init(irlan_init);
1198 module_exit(irlan_cleanup);
1199