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