conn rcv_lock converted to spinlock, struct cor_sock created, kernel_packet skb_clone...
[cor_2_6_31.git] / net / irda / irlan / irlan_common.c
blob774d73a76852e1ce63262f8d2a1af343668473d5
1 /*********************************************************************
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>
12 * Copyright (c) 1997, 1999 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/module.h>
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>
41 #include <asm/system.h>
42 #include <asm/byteorder.h>
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>
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>
57 /* extern char sysctl_devname[]; */
60 * Master structure
62 static LIST_HEAD(irlans);
64 static void *ckey;
65 static void *skey;
67 /* Module parameters */
68 static int eth; /* Use "eth" or "irlan" name for devices */
69 static int access = ACCESS_PEER; /* PEER, DIRECT or HOSTED */
71 #ifdef CONFIG_PROC_FS
72 static const char *irlan_access[] = {
73 "UNKNOWN",
74 "DIRECT",
75 "PEER",
76 "HOSTED"
79 static const char *irlan_media[] = {
80 "UNKNOWN",
81 "802.3",
82 "802.5"
85 extern struct proc_dir_entry *proc_irda;
87 static int irlan_seq_open(struct inode *inode, struct file *file);
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,
97 extern struct proc_dir_entry *proc_irda;
98 #endif /* CONFIG_PROC_FS */
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);
110 * Function irlan_init (void)
112 * Initialize IrLAN layer
115 static int __init irlan_init(void)
117 struct irlan_cb *new;
118 __u16 hints;
120 IRDA_DEBUG(2, "%s()\n", __func__ );
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;
130 #endif /* CONFIG_PROC_FS */
132 IRDA_DEBUG(4, "%s()\n", __func__ );
133 hints = irlmp_service_to_hint(S_LAN);
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;
141 /* Register with IrLMP as a service */
142 skey = irlmp_register_service(hints);
143 if (!skey)
144 goto err_skey;
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;
151 /* The master will only open its (listen) control TSAP */
152 irlan_provider_open_ctrl_tsap(new);
154 /* Do some fast discovery! */
155 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
157 return 0;
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 */
168 return -ENOMEM;
171 static void __exit irlan_cleanup(void)
173 struct irlan_cb *self, *next;
175 IRDA_DEBUG(4, "%s()\n", __func__ );
177 irlmp_unregister_client(ckey);
178 irlmp_unregister_service(skey);
180 #ifdef CONFIG_PROC_FS
181 remove_proc_entry("irlan", proc_irda);
182 #endif /* CONFIG_PROC_FS */
184 /* Cleanup any leftover network devices */
185 rtnl_lock();
186 list_for_each_entry_safe(self, next, &irlans, dev_list) {
187 __irlan_close(self);
189 rtnl_unlock();
193 * Function irlan_open (void)
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
198 static struct irlan_cb *irlan_open(__u32 saddr, __u32 daddr)
200 struct net_device *dev;
201 struct irlan_cb *self;
203 IRDA_DEBUG(2, "%s()\n", __func__ );
205 /* Create network device with irlan */
206 dev = alloc_irlandev(eth ? "eth%d" : "irlan%d");
207 if (!dev)
208 return NULL;
210 self = netdev_priv(dev);
211 self->dev = dev;
214 * Initialize local device structure
216 self->magic = IRLAN_MAGIC;
217 self->saddr = saddr;
218 self->daddr = daddr;
220 /* Provider access can only be PEER, DIRECT, or HOSTED */
221 self->provider.access_type = access;
222 if (access == ACCESS_DIRECT) {
224 * Since we are emulating an IrLAN sever we will have to
225 * give ourself an ethernet address!
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);
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);
241 skb_queue_head_init(&self->client.txq);
243 irlan_next_client_state(self, IRLAN_IDLE);
244 irlan_next_provider_state(self, IRLAN_IDLE);
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();
257 return self;
260 * Function __irlan_close (self)
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.
266 static void __irlan_close(struct irlan_cb *self)
268 IRDA_DEBUG(2, "%s()\n", __func__ );
270 ASSERT_RTNL();
271 IRDA_ASSERT(self != NULL, return;);
272 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
274 del_timer_sync(&self->watchdog_timer);
275 del_timer_sync(&self->client.kick_timer);
277 /* Close all open connections and remove TSAPs */
278 irlan_close_tsaps(self);
280 if (self->client.iriap)
281 iriap_close(self->client.iriap);
283 /* Remove frames queued on the control channel */
284 skb_queue_purge(&self->client.txq);
286 /* Unregister and free self via destructor */
287 unregister_netdevice(self->dev);
290 /* Find any instance of irlan, used for client discovery wakeup */
291 struct irlan_cb *irlan_get_any(void)
293 struct irlan_cb *self;
295 list_for_each_entry_rcu(self, &irlans, dev_list) {
296 return self;
298 return NULL;
302 * Function irlan_connect_indication (instance, sap, qos, max_sdu_size, skb)
304 * Here we receive the connect indication for the data channel
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)
313 struct irlan_cb *self;
314 struct tsap_cb *tsap;
316 IRDA_DEBUG(2, "%s()\n", __func__ );
318 self = (struct irlan_cb *) instance;
319 tsap = (struct tsap_cb *) sap;
321 IRDA_ASSERT(self != NULL, return;);
322 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
323 IRDA_ASSERT(tsap == self->tsap_data,return;);
325 self->max_sdu_size = max_sdu_size;
326 self->max_header_size = max_header_size;
328 IRDA_DEBUG(0, "%s: We are now connected!\n", __func__);
330 del_timer(&self->watchdog_timer);
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);
339 irlan_do_provider_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
340 irlan_do_client_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
342 if (self->provider.access_type == ACCESS_PEER) {
344 * Data channel is open, so we are now allowed to
345 * configure the remote filter
347 irlan_get_unicast_addr(self);
348 irlan_open_unicast_addr(self);
350 /* Ready to transfer Ethernet frames (at last) */
351 netif_start_queue(self->dev); /* Clear reason */
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)
360 struct irlan_cb *self;
362 self = (struct irlan_cb *) instance;
364 IRDA_ASSERT(self != NULL, return;);
365 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
367 self->max_sdu_size = max_sdu_size;
368 self->max_header_size = max_header_size;
370 /* TODO: we could set the MTU depending on the max_sdu_size */
372 IRDA_DEBUG(0, "%s: We are now connected!\n", __func__);
373 del_timer(&self->watchdog_timer);
376 * Data channel is open, so we are now allowed to configure the remote
377 * filter
379 irlan_get_unicast_addr(self);
380 irlan_open_unicast_addr(self);
382 /* Open broadcast and multicast filter by default */
383 irlan_set_broadcast_filter(self, TRUE);
384 irlan_set_multicast_filter(self, TRUE);
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);
393 * Function irlan_client_disconnect_indication (handle)
395 * Callback function for the IrTTP layer. Indicates a disconnection of
396 * the specified connection (handle)
398 static void irlan_disconnect_indication(void *instance,
399 void *sap, LM_REASON reason,
400 struct sk_buff *userdata)
402 struct irlan_cb *self;
403 struct tsap_cb *tsap;
405 IRDA_DEBUG(0, "%s(), reason=%d\n", __func__ , reason);
407 self = (struct irlan_cb *) instance;
408 tsap = (struct tsap_cb *) sap;
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;);
415 IRDA_ASSERT(tsap == self->tsap_data, return;);
417 IRDA_DEBUG(2, "IrLAN, data channel disconnected by peer!\n");
419 /* Save reason so we know if we should try to reconnect or not */
420 self->disconnect_reason = reason;
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;
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);
450 irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
451 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
453 wake_up_interruptible(&self->open_wait);
456 void irlan_open_data_tsap(struct irlan_cb *self)
458 struct tsap_cb *tsap;
459 notify_t notify;
461 IRDA_DEBUG(2, "%s()\n", __func__ );
463 IRDA_ASSERT(self != NULL, return;);
464 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
466 /* Check if already open */
467 if (self->tsap_data)
468 return;
470 irda_notify_init(&notify);
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));
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;
486 self->tsap_data = tsap;
489 * This is the data TSAP selector which we will pass to the client
490 * when the client ask for it.
492 self->stsap_sel_data = self->tsap_data->stsap_sel;
495 void irlan_close_tsaps(struct irlan_cb *self)
497 IRDA_DEBUG(4, "%s()\n", __func__ );
499 IRDA_ASSERT(self != NULL, return;);
500 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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;
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;
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;
520 self->disconnect_reason = LM_USER_REQUEST;
524 * Function irlan_ias_register (self, tsap_sel)
526 * Register with LM-IAS
529 void irlan_ias_register(struct irlan_cb *self, __u8 tsap_sel)
531 struct ias_object *obj;
532 struct ias_value *new_value;
534 IRDA_ASSERT(self != NULL, return;);
535 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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
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);
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);
571 irias_add_string_attrib(obj, "Manufacturer",
572 "Linux-IrDA Project", IAS_KERNEL_ATTR);
573 irias_insert_object(obj);
578 * Function irlan_run_ctrl_tx_queue (self)
580 * Try to send the next command in the control transmit queue
583 int irlan_run_ctrl_tx_queue(struct irlan_cb *self)
585 struct sk_buff *skb;
587 IRDA_DEBUG(2, "%s()\n", __func__ );
589 if (irda_lock(&self->client.tx_busy) == FALSE)
590 return -EBUSY;
592 skb = skb_dequeue(&self->client.txq);
593 if (!skb) {
594 self->client.tx_busy = FALSE;
595 return 0;
598 /* Check that it's really possible to send commands */
599 if ((self->client.tsap_ctrl == NULL) ||
600 (self->client.state == IRLAN_IDLE))
602 self->client.tx_busy = FALSE;
603 dev_kfree_skb(skb);
604 return -1;
606 IRDA_DEBUG(2, "%s(), sending ...\n", __func__ );
608 return irttp_data_request(self->client.tsap_ctrl, skb);
612 * Function irlan_ctrl_data_request (self, skb)
614 * This function makes sure that commands on the control channel is being
615 * sent in a command/response fashion
617 static void irlan_ctrl_data_request(struct irlan_cb *self, struct sk_buff *skb)
619 IRDA_DEBUG(2, "%s()\n", __func__ );
621 /* Queue command */
622 skb_queue_tail(&self->client.txq, skb);
624 /* Try to send command */
625 irlan_run_ctrl_tx_queue(self);
629 * Function irlan_get_provider_info (self)
631 * Send Get Provider Information command to peer IrLAN layer
634 void irlan_get_provider_info(struct irlan_cb *self)
636 struct sk_buff *skb;
637 __u8 *frame;
639 IRDA_DEBUG(4, "%s()\n", __func__ );
641 IRDA_ASSERT(self != NULL, return;);
642 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
644 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER,
645 GFP_ATOMIC);
646 if (!skb)
647 return;
649 /* Reserve space for TTP, LMP, and LAP header */
650 skb_reserve(skb, self->client.max_header_size);
651 skb_put(skb, 2);
653 frame = skb->data;
655 frame[0] = CMD_GET_PROVIDER_INFO;
656 frame[1] = 0x00; /* Zero parameters */
658 irlan_ctrl_data_request(self, skb);
662 * Function irlan_open_data_channel (self)
664 * Send an Open Data Command to provider
667 void irlan_open_data_channel(struct irlan_cb *self)
669 struct sk_buff *skb;
670 __u8 *frame;
672 IRDA_DEBUG(4, "%s()\n", __func__ );
674 IRDA_ASSERT(self != NULL, return;);
675 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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;
684 skb_reserve(skb, self->client.max_header_size);
685 skb_put(skb, 2);
687 frame = skb->data;
689 /* Build frame */
690 frame[0] = CMD_OPEN_DATA_CHANNEL;
691 frame[1] = 0x02; /* Two parameters */
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"); */
697 /* self->use_udata = TRUE; */
699 irlan_ctrl_data_request(self, skb);
702 void irlan_close_data_channel(struct irlan_cb *self)
704 struct sk_buff *skb;
705 __u8 *frame;
707 IRDA_DEBUG(4, "%s()\n", __func__ );
709 IRDA_ASSERT(self != NULL, return;);
710 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
712 /* Check if the TSAP is still there */
713 if (self->client.tsap_ctrl == NULL)
714 return;
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;
722 skb_reserve(skb, self->client.max_header_size);
723 skb_put(skb, 2);
725 frame = skb->data;
727 /* Build frame */
728 frame[0] = CMD_CLOSE_DATA_CHAN;
729 frame[1] = 0x01; /* One parameter */
731 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
733 irlan_ctrl_data_request(self, skb);
737 * Function irlan_open_unicast_addr (self)
739 * Make IrLAN provider accept ethernet frames addressed to the unicast
740 * address.
743 static void irlan_open_unicast_addr(struct irlan_cb *self)
745 struct sk_buff *skb;
746 __u8 *frame;
748 IRDA_DEBUG(4, "%s()\n", __func__ );
750 IRDA_ASSERT(self != NULL, return;);
751 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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;
761 /* Reserve space for TTP, LMP, and LAP header */
762 skb_reserve(skb, self->max_header_size);
763 skb_put(skb, 2);
765 frame = skb->data;
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");
773 irlan_ctrl_data_request(self, skb);
777 * Function irlan_set_broadcast_filter (self, status)
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.
785 void irlan_set_broadcast_filter(struct irlan_cb *self, int status)
787 struct sk_buff *skb;
788 __u8 *frame;
790 IRDA_DEBUG(2, "%s()\n", __func__ );
792 IRDA_ASSERT(self != NULL, return;);
793 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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;
804 /* Reserve space for TTP, LMP, and LAP header */
805 skb_reserve(skb, self->client.max_header_size);
806 skb_put(skb, 2);
808 frame = skb->data;
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");
819 irlan_ctrl_data_request(self, skb);
823 * Function irlan_set_multicast_filter (self, status)
825 * Make IrLAN provider accept ethernet frames addressed to the multicast
826 * address.
829 void irlan_set_multicast_filter(struct irlan_cb *self, int status)
831 struct sk_buff *skb;
832 __u8 *frame;
834 IRDA_DEBUG(2, "%s()\n", __func__ );
836 IRDA_ASSERT(self != NULL, return;);
837 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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;
848 /* Reserve space for TTP, LMP, and LAP header */
849 skb_reserve(skb, self->client.max_header_size);
850 skb_put(skb, 2);
852 frame = skb->data;
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");
863 irlan_ctrl_data_request(self, skb);
867 * Function irlan_get_unicast_addr (self)
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.
874 static void irlan_get_unicast_addr(struct irlan_cb *self)
876 struct sk_buff *skb;
877 __u8 *frame;
879 IRDA_DEBUG(2, "%s()\n", __func__ );
881 IRDA_ASSERT(self != NULL, return;);
882 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
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;
893 /* Reserve space for TTP, LMP, and LAP header */
894 skb_reserve(skb, self->client.max_header_size);
895 skb_put(skb, 2);
897 frame = skb->data;
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");
905 irlan_ctrl_data_request(self, skb);
909 * Function irlan_get_media_char (self)
914 void irlan_get_media_char(struct irlan_cb *self)
916 struct sk_buff *skb;
917 __u8 *frame;
919 IRDA_DEBUG(4, "%s()\n", __func__ );
921 IRDA_ASSERT(self != NULL, return;);
922 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
924 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
925 IRLAN_STRING_PARAMETER_LEN("MEDIA", "802.3"),
926 GFP_ATOMIC);
928 if (!skb)
929 return;
931 /* Reserve space for TTP, LMP, and LAP header */
932 skb_reserve(skb, self->client.max_header_size);
933 skb_put(skb, 2);
935 frame = skb->data;
937 /* Build frame */
938 frame[0] = CMD_GET_MEDIA_CHAR;
939 frame[1] = 0x01; /* One parameter */
941 irlan_insert_string_param(skb, "MEDIA", "802.3");
942 irlan_ctrl_data_request(self, skb);
946 * Function insert_byte_param (skb, param, value)
948 * Insert byte parameter into frame
951 int irlan_insert_byte_param(struct sk_buff *skb, char *param, __u8 value)
953 return __irlan_insert_param(skb, param, IRLAN_BYTE, value, 0, NULL, 0);
956 int irlan_insert_short_param(struct sk_buff *skb, char *param, __u16 value)
958 return __irlan_insert_param(skb, param, IRLAN_SHORT, 0, value, NULL, 0);
962 * Function insert_string (skb, param, value)
964 * Insert string parameter into frame
967 int irlan_insert_string_param(struct sk_buff *skb, char *param, char *string)
969 int string_len = strlen(string);
971 return __irlan_insert_param(skb, param, IRLAN_ARRAY, 0, 0, string,
972 string_len);
976 * Function insert_array_param(skb, param, value, len_value)
978 * Insert array parameter into frame
981 int irlan_insert_array_param(struct sk_buff *skb, char *name, __u8 *array,
982 __u16 array_len)
984 return __irlan_insert_param(skb, name, IRLAN_ARRAY, 0, 0, array,
985 array_len);
989 * Function insert_param (skb, param, value, byte)
991 * Insert parameter at end of buffer, structure of a parameter is:
993 * -----------------------------------------------------------------------
994 * | Name Length[1] | Param Name[1..255] | Val Length[2] | Value[0..1016]|
995 * -----------------------------------------------------------------------
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)
1001 __u8 *frame;
1002 __u8 param_len;
1003 __le16 tmp_le; /* Temporary value in little endian format */
1004 int n=0;
1006 if (skb == NULL) {
1007 IRDA_DEBUG(2, "%s(), Got NULL skb\n", __func__ );
1008 return 0;
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;
1029 /* Insert at end of sk-buffer */
1030 frame = skb_tail_pointer(skb);
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;
1037 skb_put(skb, param_len+value_len+3);
1039 /* Insert parameter length */
1040 frame[n++] = param_len;
1042 /* Insert parameter */
1043 memcpy(frame+n, param, param_len); n += param_len;
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 */
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;
1064 IRDA_ASSERT(n == (param_len+value_len+3), return 0;);
1066 return param_len+value_len+3;
1070 * Function irlan_extract_param (buf, name, value, len)
1072 * Extracts a single parameter name/value pair from buffer and updates
1073 * the buffer pointer to point to the next name/value pair.
1075 int irlan_extract_param(__u8 *buf, char *name, char *value, __u16 *len)
1077 __u8 name_len;
1078 __u16 val_len;
1079 int n=0;
1081 IRDA_DEBUG(4, "%s()\n", __func__ );
1083 /* get length of parameter name (1 byte) */
1084 name_len = buf[n++];
1086 if (name_len > 254) {
1087 IRDA_DEBUG(2, "%s(), name_len > 254\n", __func__ );
1088 return -RSP_INVALID_COMMAND_FORMAT;
1091 /* get parameter name */
1092 memcpy(name, buf+n, name_len);
1093 name[name_len] = '\0';
1094 n+=name_len;
1097 * Get length of parameter value (2 bytes in little endian
1098 * format)
1100 memcpy(&val_len, buf+n, 2); /* To avoid alignment problems */
1101 le16_to_cpus(&val_len); n+=2;
1103 if (val_len > 1016) {
1104 IRDA_DEBUG(2, "%s(), parameter length to long\n", __func__ );
1105 return -RSP_INVALID_COMMAND_FORMAT;
1107 *len = val_len;
1109 /* get parameter value */
1110 memcpy(value, buf+n, val_len);
1111 value[val_len] = '\0';
1112 n+=val_len;
1114 IRDA_DEBUG(4, "Parameter: %s ", name);
1115 IRDA_DEBUG(4, "Value: %s\n", value);
1117 return n;
1120 #ifdef CONFIG_PROC_FS
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
1128 static void *irlan_seq_start(struct seq_file *seq, loff_t *pos)
1130 int i = 1;
1131 struct irlan_cb *self;
1133 rcu_read_lock();
1134 if (*pos == 0)
1135 return SEQ_START_TOKEN;
1137 list_for_each_entry(self, &irlans, dev_list) {
1138 if (*pos == i)
1139 return self;
1140 ++i;
1142 return NULL;
1145 /* Return entry after v, and increment pos */
1146 static void *irlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1148 struct list_head *nxt;
1150 ++*pos;
1151 if (v == SEQ_START_TOKEN)
1152 nxt = irlans.next;
1153 else
1154 nxt = ((struct irlan_cb *)v)->dev_list.next;
1156 return (nxt == &irlans) ? NULL
1157 : list_entry(nxt, struct irlan_cb, dev_list);
1160 /* End of reading /proc file */
1161 static void irlan_seq_stop(struct seq_file *seq, void *v)
1163 rcu_read_unlock();
1168 * Show one entry in /proc file.
1170 static int irlan_seq_show(struct seq_file *seq, void *v)
1172 if (v == SEQ_START_TOKEN)
1173 seq_puts(seq, "IrLAN instances:\n");
1174 else {
1175 struct irlan_cb *self = v;
1177 IRDA_ASSERT(self != NULL, return -1;);
1178 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
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]);
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");
1203 seq_putc(seq,'\n');
1205 return 0;
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,
1215 static int irlan_seq_open(struct inode *inode, struct file *file)
1217 return seq_open(file, &irlan_seq_ops);
1219 #endif
1221 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1222 MODULE_DESCRIPTION("The Linux IrDA LAN protocol");
1223 MODULE_LICENSE("GPL");
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");
1230 module_init(irlan_init);
1231 module_exit(irlan_cleanup);