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