MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / net / irda / irlan / irlan_common.c
blobe6599074e8af0c5bec3f57fdbf6588da3189bf7c
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>
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/config.h>
27 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/init.h>
32 #include <linux/errno.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/random.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/moduleparam.h>
41 #include <asm/system.h>
42 #include <asm/bitops.h>
43 #include <asm/byteorder.h>
45 #include <net/irda/irda.h>
46 #include <net/irda/irttp.h>
47 #include <net/irda/irlmp.h>
48 #include <net/irda/iriap.h>
49 #include <net/irda/timer.h>
51 #include <net/irda/irlan_common.h>
52 #include <net/irda/irlan_client.h>
53 #include <net/irda/irlan_provider.h>
54 #include <net/irda/irlan_eth.h>
55 #include <net/irda/irlan_filter.h>
58 /*
59 * Send gratuitous ARP when connected to a new AP or not. May be a clever
60 * thing to do, but for some reason the machine crashes if you use DHCP. So
61 * lets not use it by default.
63 #undef CONFIG_IRLAN_SEND_GRATUITOUS_ARP
65 /* extern char sysctl_devname[]; */
68 * Master structure
70 static LIST_HEAD(irlans);
72 static void *ckey;
73 static void *skey;
75 /* Module parameters */
76 static int eth; /* Use "eth" or "irlan" name for devices */
77 static int access = ACCESS_PEER; /* PEER, DIRECT or HOSTED */
79 #ifdef CONFIG_PROC_FS
80 static const char *irlan_access[] = {
81 "UNKNOWN",
82 "DIRECT",
83 "PEER",
84 "HOSTED"
87 static const char *irlan_media[] = {
88 "UNKNOWN",
89 "802.3",
90 "802.5"
93 extern struct proc_dir_entry *proc_irda;
95 static int irlan_seq_open(struct inode *inode, struct file *file);
97 static struct file_operations irlan_fops = {
98 .owner = THIS_MODULE,
99 .open = irlan_seq_open,
100 .read = seq_read,
101 .llseek = seq_lseek,
102 .release = seq_release,
105 extern struct proc_dir_entry *proc_irda;
106 #endif /* CONFIG_PROC_FS */
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 void irlan_close_tsaps(struct irlan_cb *self);
115 * Function irlan_init (void)
117 * Initialize IrLAN layer
120 static int __init irlan_init(void)
122 struct irlan_cb *new;
123 __u16 hints;
125 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
127 #ifdef CONFIG_PROC_FS
128 { struct proc_dir_entry *proc;
129 proc = create_proc_entry("irlan", 0, proc_irda);
130 if (!proc) {
131 printk(KERN_ERR "irlan_init: can't create /proc entry!\n");
132 return -ENODEV;
135 proc->proc_fops = &irlan_fops;
137 #endif /* CONFIG_PROC_FS */
139 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
140 hints = irlmp_service_to_hint(S_LAN);
142 /* Register with IrLMP as a client */
143 ckey = irlmp_register_client(hints, &irlan_client_discovery_indication,
144 NULL, NULL);
146 /* Register with IrLMP as a service */
147 skey = irlmp_register_service(hints);
149 /* Start the master IrLAN instance (the only one for now) */
150 new = irlan_open(DEV_ADDR_ANY, DEV_ADDR_ANY);
152 /* The master will only open its (listen) control TSAP */
153 irlan_provider_open_ctrl_tsap(new);
155 /* Do some fast discovery! */
156 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
158 return 0;
161 static void __exit irlan_cleanup(void)
163 struct irlan_cb *self, *next;
165 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
167 irlmp_unregister_client(ckey);
168 irlmp_unregister_service(skey);
170 #ifdef CONFIG_PROC_FS
171 remove_proc_entry("irlan", proc_irda);
172 #endif /* CONFIG_PROC_FS */
174 /* Cleanup any leftover network devices */
175 rtnl_lock();
176 list_for_each_entry_safe(self, next, &irlans, dev_list) {
177 __irlan_close(self);
179 rtnl_unlock();
183 * Function irlan_open (void)
185 * Open new instance of a client/provider, we should only register the
186 * network device if this instance is ment for a particular client/provider
188 struct irlan_cb *irlan_open(__u32 saddr, __u32 daddr)
190 struct net_device *dev;
191 struct irlan_cb *self;
193 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
195 /* Create network device with irlan */
196 dev = alloc_irlandev(eth ? "eth%d" : "irlan%d");
197 if (!dev)
198 return NULL;
200 self = dev->priv;
201 self->dev = dev;
204 * Initialize local device structure
206 self->magic = IRLAN_MAGIC;
207 self->saddr = saddr;
208 self->daddr = daddr;
210 /* Provider access can only be PEER, DIRECT, or HOSTED */
211 self->provider.access_type = access;
212 if (access == ACCESS_DIRECT) {
214 * Since we are emulating an IrLAN sever we will have to
215 * give ourself an ethernet address!
217 dev->dev_addr[0] = 0x40;
218 dev->dev_addr[1] = 0x00;
219 dev->dev_addr[2] = 0x00;
220 dev->dev_addr[3] = 0x00;
221 get_random_bytes(dev->dev_addr+4, 1);
222 get_random_bytes(dev->dev_addr+5, 1);
225 self->media = MEDIA_802_3;
226 self->disconnect_reason = LM_USER_REQUEST;
227 init_timer(&self->watchdog_timer);
228 init_timer(&self->client.kick_timer);
229 init_waitqueue_head(&self->open_wait);
231 skb_queue_head_init(&self->client.txq);
233 irlan_next_client_state(self, IRLAN_IDLE);
234 irlan_next_provider_state(self, IRLAN_IDLE);
236 if (register_netdev(dev)) {
237 IRDA_DEBUG(2, "%s(), register_netdev() failed!\n",
238 __FUNCTION__ );
239 self = NULL;
240 free_netdev(dev);
241 } else {
242 rtnl_lock();
243 list_add_rcu(&self->dev_list, &irlans);
244 rtnl_unlock();
247 return self;
250 * Function __irlan_close (self)
252 * This function closes and deallocates the IrLAN client instances. Be
253 * aware that other functions which calls client_close() must
254 * remove self from irlans list first.
256 static void __irlan_close(struct irlan_cb *self)
258 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
260 ASSERT_RTNL();
261 ASSERT(self != NULL, return;);
262 ASSERT(self->magic == IRLAN_MAGIC, return;);
264 del_timer_sync(&self->watchdog_timer);
265 del_timer_sync(&self->client.kick_timer);
267 /* Close all open connections and remove TSAPs */
268 irlan_close_tsaps(self);
270 if (self->client.iriap)
271 iriap_close(self->client.iriap);
273 /* Remove frames queued on the control channel */
274 skb_queue_purge(&self->client.txq);
276 /* Unregister and free self via destructor */
277 unregister_netdevice(self->dev);
280 /* Find any instance of irlan, used for client discovery wakeup */
281 struct irlan_cb *irlan_get_any(void)
283 struct irlan_cb *self;
285 list_for_each_entry_rcu(self, &irlans, dev_list) {
286 return self;
288 return NULL;
292 * Function irlan_connect_indication (instance, sap, qos, max_sdu_size, skb)
294 * Here we receive the connect indication for the data channel
297 void irlan_connect_indication(void *instance, void *sap, struct qos_info *qos,
298 __u32 max_sdu_size, __u8 max_header_size,
299 struct sk_buff *skb)
301 struct irlan_cb *self;
302 struct tsap_cb *tsap;
304 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
306 self = (struct irlan_cb *) instance;
307 tsap = (struct tsap_cb *) sap;
309 ASSERT(self != NULL, return;);
310 ASSERT(self->magic == IRLAN_MAGIC, return;);
311 ASSERT(tsap == self->tsap_data,return;);
313 self->max_sdu_size = max_sdu_size;
314 self->max_header_size = max_header_size;
316 IRDA_DEBUG(0, "%s: We are now connected!\n", __FUNCTION__);
318 del_timer(&self->watchdog_timer);
320 /* If you want to pass the skb to *both* state machines, you will
321 * need to skb_clone() it, so that you don't free it twice.
322 * As the state machines don't need it, git rid of it here...
323 * Jean II */
324 if (skb)
325 dev_kfree_skb(skb);
327 irlan_do_provider_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
328 irlan_do_client_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
330 if (self->provider.access_type == ACCESS_PEER) {
332 * Data channel is open, so we are now allowed to
333 * configure the remote filter
335 irlan_get_unicast_addr(self);
336 irlan_open_unicast_addr(self);
338 /* Ready to transfer Ethernet frames (at last) */
339 netif_start_queue(self->dev); /* Clear reason */
342 void irlan_connect_confirm(void *instance, void *sap, struct qos_info *qos,
343 __u32 max_sdu_size, __u8 max_header_size,
344 struct sk_buff *skb)
346 struct irlan_cb *self;
348 self = (struct irlan_cb *) instance;
350 ASSERT(self != NULL, return;);
351 ASSERT(self->magic == IRLAN_MAGIC, return;);
353 self->max_sdu_size = max_sdu_size;
354 self->max_header_size = max_header_size;
356 /* TODO: we could set the MTU depending on the max_sdu_size */
358 IRDA_DEBUG(0, "%s: We are now connected!\n", __FUNCTION__);
359 del_timer(&self->watchdog_timer);
362 * Data channel is open, so we are now allowed to configure the remote
363 * filter
365 irlan_get_unicast_addr(self);
366 irlan_open_unicast_addr(self);
368 /* Open broadcast and multicast filter by default */
369 irlan_set_broadcast_filter(self, TRUE);
370 irlan_set_multicast_filter(self, TRUE);
372 /* Ready to transfer Ethernet frames */
373 netif_start_queue(self->dev);
374 self->disconnect_reason = 0; /* Clear reason */
375 #ifdef CONFIG_IRLAN_SEND_GRATUITOUS_ARP
376 irlan_eth_send_gratuitous_arp(&self->dev);
377 #endif
378 wake_up_interruptible(&self->open_wait);
382 * Function irlan_client_disconnect_indication (handle)
384 * Callback function for the IrTTP layer. Indicates a disconnection of
385 * the specified connection (handle)
387 void irlan_disconnect_indication(void *instance, void *sap, LM_REASON reason,
388 struct sk_buff *userdata)
390 struct irlan_cb *self;
391 struct tsap_cb *tsap;
393 IRDA_DEBUG(0, "%s(), reason=%d\n", __FUNCTION__ , reason);
395 self = (struct irlan_cb *) instance;
396 tsap = (struct tsap_cb *) sap;
398 ASSERT(self != NULL, return;);
399 ASSERT(self->magic == IRLAN_MAGIC, return;);
400 ASSERT(tsap != NULL, return;);
401 ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
403 ASSERT(tsap == self->tsap_data, return;);
405 IRDA_DEBUG(2, "IrLAN, data channel disconnected by peer!\n");
407 /* Save reason so we know if we should try to reconnect or not */
408 self->disconnect_reason = reason;
410 switch (reason) {
411 case LM_USER_REQUEST: /* User request */
412 IRDA_DEBUG(2, "%s(), User requested\n", __FUNCTION__ );
413 break;
414 case LM_LAP_DISCONNECT: /* Unexpected IrLAP disconnect */
415 IRDA_DEBUG(2, "%s(), Unexpected IrLAP disconnect\n", __FUNCTION__ );
416 break;
417 case LM_CONNECT_FAILURE: /* Failed to establish IrLAP connection */
418 IRDA_DEBUG(2, "%s(), IrLAP connect failed\n", __FUNCTION__ );
419 break;
420 case LM_LAP_RESET: /* IrLAP reset */
421 IRDA_DEBUG(2, "%s(), IrLAP reset\n", __FUNCTION__ );
422 break;
423 case LM_INIT_DISCONNECT:
424 IRDA_DEBUG(2, "%s(), IrLMP connect failed\n", __FUNCTION__ );
425 break;
426 default:
427 ERROR("%s(), Unknown disconnect reason\n", __FUNCTION__);
428 break;
431 /* If you want to pass the skb to *both* state machines, you will
432 * need to skb_clone() it, so that you don't free it twice.
433 * As the state machines don't need it, git rid of it here...
434 * Jean II */
435 if (userdata)
436 dev_kfree_skb(userdata);
438 irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
439 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
441 wake_up_interruptible(&self->open_wait);
444 void irlan_open_data_tsap(struct irlan_cb *self)
446 struct tsap_cb *tsap;
447 notify_t notify;
449 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
451 ASSERT(self != NULL, return;);
452 ASSERT(self->magic == IRLAN_MAGIC, return;);
454 /* Check if already open */
455 if (self->tsap_data)
456 return;
458 irda_notify_init(&notify);
460 notify.data_indication = irlan_eth_receive;
461 notify.udata_indication = irlan_eth_receive;
462 notify.connect_indication = irlan_connect_indication;
463 notify.connect_confirm = irlan_connect_confirm;
464 notify.flow_indication = irlan_eth_flow_indication;
465 notify.disconnect_indication = irlan_disconnect_indication;
466 notify.instance = self;
467 strlcpy(notify.name, "IrLAN data", sizeof(notify.name));
469 tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
470 if (!tsap) {
471 IRDA_DEBUG(2, "%s(), Got no tsap!\n", __FUNCTION__ );
472 return;
474 self->tsap_data = tsap;
477 * This is the data TSAP selector which we will pass to the client
478 * when the client ask for it.
480 self->stsap_sel_data = self->tsap_data->stsap_sel;
483 void irlan_close_tsaps(struct irlan_cb *self)
485 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
487 ASSERT(self != NULL, return;);
488 ASSERT(self->magic == IRLAN_MAGIC, return;);
490 /* Disconnect and close all open TSAP connections */
491 if (self->tsap_data) {
492 irttp_disconnect_request(self->tsap_data, NULL, P_NORMAL);
493 irttp_close_tsap(self->tsap_data);
494 self->tsap_data = NULL;
496 if (self->client.tsap_ctrl) {
497 irttp_disconnect_request(self->client.tsap_ctrl, NULL,
498 P_NORMAL);
499 irttp_close_tsap(self->client.tsap_ctrl);
500 self->client.tsap_ctrl = NULL;
502 if (self->provider.tsap_ctrl) {
503 irttp_disconnect_request(self->provider.tsap_ctrl, NULL,
504 P_NORMAL);
505 irttp_close_tsap(self->provider.tsap_ctrl);
506 self->provider.tsap_ctrl = NULL;
508 self->disconnect_reason = LM_USER_REQUEST;
512 * Function irlan_ias_register (self, tsap_sel)
514 * Register with LM-IAS
517 void irlan_ias_register(struct irlan_cb *self, __u8 tsap_sel)
519 struct ias_object *obj;
520 struct ias_value *new_value;
522 ASSERT(self != NULL, return;);
523 ASSERT(self->magic == IRLAN_MAGIC, return;);
526 * Check if object has already been registered by a previous provider.
527 * If that is the case, we just change the value of the attribute
529 if (!irias_find_object("IrLAN")) {
530 obj = irias_new_object("IrLAN", IAS_IRLAN_ID);
531 irias_add_integer_attrib(obj, "IrDA:TinyTP:LsapSel", tsap_sel,
532 IAS_KERNEL_ATTR);
533 irias_insert_object(obj);
534 } else {
535 new_value = irias_new_integer_value(tsap_sel);
536 irias_object_change_attribute("IrLAN", "IrDA:TinyTP:LsapSel",
537 new_value);
540 /* Register PnP object only if not registered before */
541 if (!irias_find_object("PnP")) {
542 obj = irias_new_object("PnP", IAS_PNP_ID);
543 #if 0
544 irias_add_string_attrib(obj, "Name", sysctl_devname,
545 IAS_KERNEL_ATTR);
546 #else
547 irias_add_string_attrib(obj, "Name", "Linux", IAS_KERNEL_ATTR);
548 #endif
549 irias_add_string_attrib(obj, "DeviceID", "HWP19F0",
550 IAS_KERNEL_ATTR);
551 irias_add_integer_attrib(obj, "CompCnt", 1, IAS_KERNEL_ATTR);
552 if (self->provider.access_type == ACCESS_PEER)
553 irias_add_string_attrib(obj, "Comp#01", "PNP8389",
554 IAS_KERNEL_ATTR);
555 else
556 irias_add_string_attrib(obj, "Comp#01", "PNP8294",
557 IAS_KERNEL_ATTR);
559 irias_add_string_attrib(obj, "Manufacturer",
560 "Linux-IrDA Project", IAS_KERNEL_ATTR);
561 irias_insert_object(obj);
566 * Function irlan_run_ctrl_tx_queue (self)
568 * Try to send the next command in the control transmit queue
571 int irlan_run_ctrl_tx_queue(struct irlan_cb *self)
573 struct sk_buff *skb;
575 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
577 if (irda_lock(&self->client.tx_busy) == FALSE)
578 return -EBUSY;
580 skb = skb_dequeue(&self->client.txq);
581 if (!skb) {
582 self->client.tx_busy = FALSE;
583 return 0;
586 /* Check that it's really possible to send commands */
587 if ((self->client.tsap_ctrl == NULL) ||
588 (self->client.state == IRLAN_IDLE))
590 self->client.tx_busy = FALSE;
591 dev_kfree_skb(skb);
592 return -1;
594 IRDA_DEBUG(2, "%s(), sending ...\n", __FUNCTION__ );
596 return irttp_data_request(self->client.tsap_ctrl, skb);
600 * Function irlan_ctrl_data_request (self, skb)
602 * This function makes sure that commands on the control channel is being
603 * sent in a command/response fashion
605 void irlan_ctrl_data_request(struct irlan_cb *self, struct sk_buff *skb)
607 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
609 /* Queue command */
610 skb_queue_tail(&self->client.txq, skb);
612 /* Try to send command */
613 irlan_run_ctrl_tx_queue(self);
617 * Function irlan_get_provider_info (self)
619 * Send Get Provider Information command to peer IrLAN layer
622 void irlan_get_provider_info(struct irlan_cb *self)
624 struct sk_buff *skb;
625 __u8 *frame;
627 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
629 ASSERT(self != NULL, return;);
630 ASSERT(self->magic == IRLAN_MAGIC, return;);
632 skb = dev_alloc_skb(64);
633 if (!skb)
634 return;
636 /* Reserve space for TTP, LMP, and LAP header */
637 skb_reserve(skb, self->client.max_header_size);
638 skb_put(skb, 2);
640 frame = skb->data;
642 frame[0] = CMD_GET_PROVIDER_INFO;
643 frame[1] = 0x00; /* Zero parameters */
645 irlan_ctrl_data_request(self, skb);
649 * Function irlan_open_data_channel (self)
651 * Send an Open Data Command to provider
654 void irlan_open_data_channel(struct irlan_cb *self)
656 struct sk_buff *skb;
657 __u8 *frame;
659 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
661 ASSERT(self != NULL, return;);
662 ASSERT(self->magic == IRLAN_MAGIC, return;);
664 skb = dev_alloc_skb(64);
665 if (!skb)
666 return;
668 skb_reserve(skb, self->client.max_header_size);
669 skb_put(skb, 2);
671 frame = skb->data;
673 /* Build frame */
674 frame[0] = CMD_OPEN_DATA_CHANNEL;
675 frame[1] = 0x02; /* Two parameters */
677 irlan_insert_string_param(skb, "MEDIA", "802.3");
678 irlan_insert_string_param(skb, "ACCESS_TYPE", "DIRECT");
679 /* irlan_insert_string_param(skb, "MODE", "UNRELIABLE"); */
681 /* self->use_udata = TRUE; */
683 irlan_ctrl_data_request(self, skb);
686 void irlan_close_data_channel(struct irlan_cb *self)
688 struct sk_buff *skb;
689 __u8 *frame;
691 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
693 ASSERT(self != NULL, return;);
694 ASSERT(self->magic == IRLAN_MAGIC, return;);
696 /* Check if the TSAP is still there */
697 if (self->client.tsap_ctrl == NULL)
698 return;
700 skb = dev_alloc_skb(64);
701 if (!skb)
702 return;
704 skb_reserve(skb, self->client.max_header_size);
705 skb_put(skb, 2);
707 frame = skb->data;
709 /* Build frame */
710 frame[0] = CMD_CLOSE_DATA_CHAN;
711 frame[1] = 0x01; /* Two parameters */
713 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
715 irlan_ctrl_data_request(self, skb);
719 * Function irlan_open_unicast_addr (self)
721 * Make IrLAN provider accept ethernet frames addressed to the unicast
722 * address.
725 void irlan_open_unicast_addr(struct irlan_cb *self)
727 struct sk_buff *skb;
728 __u8 *frame;
730 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
732 ASSERT(self != NULL, return;);
733 ASSERT(self->magic == IRLAN_MAGIC, return;);
735 skb = dev_alloc_skb(128);
736 if (!skb)
737 return;
739 /* Reserve space for TTP, LMP, and LAP header */
740 skb_reserve(skb, self->max_header_size);
741 skb_put(skb, 2);
743 frame = skb->data;
745 frame[0] = CMD_FILTER_OPERATION;
746 frame[1] = 0x03; /* Three parameters */
747 irlan_insert_byte_param(skb, "DATA_CHAN" , self->dtsap_sel_data);
748 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
749 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER");
751 irlan_ctrl_data_request(self, skb);
755 * Function irlan_set_broadcast_filter (self, status)
757 * Make IrLAN provider accept ethernet frames addressed to the broadcast
758 * address. Be careful with the use of this one, since there may be a lot
759 * of broadcast traffic out there. We can still function without this
760 * one but then _we_ have to initiate all communication with other
761 * hosts, since ARP request for this host will not be answered.
763 void irlan_set_broadcast_filter(struct irlan_cb *self, int status)
765 struct sk_buff *skb;
766 __u8 *frame;
768 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
770 ASSERT(self != NULL, return;);
771 ASSERT(self->magic == IRLAN_MAGIC, return;);
773 skb = dev_alloc_skb(128);
774 if (!skb)
775 return;
777 /* Reserve space for TTP, LMP, and LAP header */
778 skb_reserve(skb, self->client.max_header_size);
779 skb_put(skb, 2);
781 frame = skb->data;
783 frame[0] = CMD_FILTER_OPERATION;
784 frame[1] = 0x03; /* Three parameters */
785 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
786 irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
787 if (status)
788 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER");
789 else
790 irlan_insert_string_param(skb, "FILTER_MODE", "NONE");
792 irlan_ctrl_data_request(self, skb);
796 * Function irlan_set_multicast_filter (self, status)
798 * Make IrLAN provider accept ethernet frames addressed to the multicast
799 * address.
802 void irlan_set_multicast_filter(struct irlan_cb *self, int status)
804 struct sk_buff *skb;
805 __u8 *frame;
807 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
809 ASSERT(self != NULL, return;);
810 ASSERT(self->magic == IRLAN_MAGIC, return;);
812 skb = dev_alloc_skb(128);
813 if (!skb)
814 return;
816 /* Reserve space for TTP, LMP, and LAP header */
817 skb_reserve(skb, self->client.max_header_size);
818 skb_put(skb, 2);
820 frame = skb->data;
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", "MULTICAST");
826 if (status)
827 irlan_insert_string_param(skb, "FILTER_MODE", "ALL");
828 else
829 irlan_insert_string_param(skb, "FILTER_MODE", "NONE");
831 irlan_ctrl_data_request(self, skb);
835 * Function irlan_get_unicast_addr (self)
837 * Retrieves the unicast address from the IrLAN provider. This address
838 * will be inserted into the devices structure, so the ethernet layer
839 * can construct its packets.
842 void irlan_get_unicast_addr(struct irlan_cb *self)
844 struct sk_buff *skb;
845 __u8 *frame;
847 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
849 ASSERT(self != NULL, return;);
850 ASSERT(self->magic == IRLAN_MAGIC, return;);
852 skb = dev_alloc_skb(128);
853 if (!skb)
854 return;
856 /* Reserve space for TTP, LMP, and LAP header */
857 skb_reserve(skb, self->client.max_header_size);
858 skb_put(skb, 2);
860 frame = skb->data;
862 frame[0] = CMD_FILTER_OPERATION;
863 frame[1] = 0x03; /* Three parameters */
864 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
865 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
866 irlan_insert_string_param(skb, "FILTER_OPERATION", "DYNAMIC");
868 irlan_ctrl_data_request(self, skb);
872 * Function irlan_get_media_char (self)
877 void irlan_get_media_char(struct irlan_cb *self)
879 struct sk_buff *skb;
880 __u8 *frame;
882 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
884 ASSERT(self != NULL, return;);
885 ASSERT(self->magic == IRLAN_MAGIC, return;);
887 skb = dev_alloc_skb(64);
888 if (!skb)
889 return;
891 /* Reserve space for TTP, LMP, and LAP header */
892 skb_reserve(skb, self->client.max_header_size);
893 skb_put(skb, 2);
895 frame = skb->data;
897 /* Build frame */
898 frame[0] = CMD_GET_MEDIA_CHAR;
899 frame[1] = 0x01; /* One parameter */
901 irlan_insert_string_param(skb, "MEDIA", "802.3");
902 irlan_ctrl_data_request(self, skb);
906 * Function insert_byte_param (skb, param, value)
908 * Insert byte parameter into frame
911 int irlan_insert_byte_param(struct sk_buff *skb, char *param, __u8 value)
913 return __irlan_insert_param(skb, param, IRLAN_BYTE, value, 0, NULL, 0);
916 int irlan_insert_short_param(struct sk_buff *skb, char *param, __u16 value)
918 return __irlan_insert_param(skb, param, IRLAN_SHORT, 0, value, NULL, 0);
922 * Function insert_string (skb, param, value)
924 * Insert string parameter into frame
927 int irlan_insert_string_param(struct sk_buff *skb, char *param, char *string)
929 int string_len = strlen(string);
931 return __irlan_insert_param(skb, param, IRLAN_ARRAY, 0, 0, string,
932 string_len);
936 * Function insert_array_param(skb, param, value, len_value)
938 * Insert array parameter into frame
941 int irlan_insert_array_param(struct sk_buff *skb, char *name, __u8 *array,
942 __u16 array_len)
944 return __irlan_insert_param(skb, name, IRLAN_ARRAY, 0, 0, array,
945 array_len);
949 * Function insert_param (skb, param, value, byte)
951 * Insert parameter at end of buffer, structure of a parameter is:
953 * -----------------------------------------------------------------------
954 * | Name Length[1] | Param Name[1..255] | Val Length[2] | Value[0..1016]|
955 * -----------------------------------------------------------------------
957 static int __irlan_insert_param(struct sk_buff *skb, char *param, int type,
958 __u8 value_byte, __u16 value_short,
959 __u8 *value_array, __u16 value_len)
961 __u8 *frame;
962 __u8 param_len;
963 __u16 tmp_le; /* Temporary value in little endian format */
964 int n=0;
966 if (skb == NULL) {
967 IRDA_DEBUG(2, "%s(), Got NULL skb\n", __FUNCTION__ );
968 return 0;
971 param_len = strlen(param);
972 switch (type) {
973 case IRLAN_BYTE:
974 value_len = 1;
975 break;
976 case IRLAN_SHORT:
977 value_len = 2;
978 break;
979 case IRLAN_ARRAY:
980 ASSERT(value_array != NULL, return 0;);
981 ASSERT(value_len > 0, return 0;);
982 break;
983 default:
984 IRDA_DEBUG(2, "%s(), Unknown parameter type!\n", __FUNCTION__ );
985 return 0;
986 break;
989 /* Insert at end of sk-buffer */
990 frame = skb->tail;
992 /* Make space for data */
993 if (skb_tailroom(skb) < (param_len+value_len+3)) {
994 IRDA_DEBUG(2, "%s(), No more space at end of skb\n", __FUNCTION__ );
995 return 0;
997 skb_put(skb, param_len+value_len+3);
999 /* Insert parameter length */
1000 frame[n++] = param_len;
1002 /* Insert parameter */
1003 memcpy(frame+n, param, param_len); n += param_len;
1005 /* Insert value length (2 byte little endian format, LSB first) */
1006 tmp_le = cpu_to_le16(value_len);
1007 memcpy(frame+n, &tmp_le, 2); n += 2; /* To avoid alignment problems */
1009 /* Insert value */
1010 switch (type) {
1011 case IRLAN_BYTE:
1012 frame[n++] = value_byte;
1013 break;
1014 case IRLAN_SHORT:
1015 tmp_le = cpu_to_le16(value_short);
1016 memcpy(frame+n, &tmp_le, 2); n += 2;
1017 break;
1018 case IRLAN_ARRAY:
1019 memcpy(frame+n, value_array, value_len); n+=value_len;
1020 break;
1021 default:
1022 break;
1024 ASSERT(n == (param_len+value_len+3), return 0;);
1026 return param_len+value_len+3;
1030 * Function irlan_extract_param (buf, name, value, len)
1032 * Extracts a single parameter name/value pair from buffer and updates
1033 * the buffer pointer to point to the next name/value pair.
1035 int irlan_extract_param(__u8 *buf, char *name, char *value, __u16 *len)
1037 __u8 name_len;
1038 __u16 val_len;
1039 int n=0;
1041 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
1043 /* get length of parameter name (1 byte) */
1044 name_len = buf[n++];
1046 if (name_len > 254) {
1047 IRDA_DEBUG(2, "%s(), name_len > 254\n", __FUNCTION__ );
1048 return -RSP_INVALID_COMMAND_FORMAT;
1051 /* get parameter name */
1052 memcpy(name, buf+n, name_len);
1053 name[name_len] = '\0';
1054 n+=name_len;
1057 * Get length of parameter value (2 bytes in little endian
1058 * format)
1060 memcpy(&val_len, buf+n, 2); /* To avoid alignment problems */
1061 le16_to_cpus(&val_len); n+=2;
1063 if (val_len > 1016) {
1064 IRDA_DEBUG(2, "%s(), parameter length to long\n", __FUNCTION__ );
1065 return -RSP_INVALID_COMMAND_FORMAT;
1067 *len = val_len;
1069 /* get parameter value */
1070 memcpy(value, buf+n, val_len);
1071 value[val_len] = '\0';
1072 n+=val_len;
1074 IRDA_DEBUG(4, "Parameter: %s ", name);
1075 IRDA_DEBUG(4, "Value: %s\n", value);
1077 return n;
1080 #ifdef CONFIG_PROC_FS
1083 * Start of reading /proc entries.
1084 * Return entry at pos,
1085 * or start_token to indicate print header line
1086 * or NULL if end of file
1088 static void *irlan_seq_start(struct seq_file *seq, loff_t *pos)
1090 int i = 1;
1091 struct irlan_cb *self;
1093 rcu_read_lock();
1094 if (*pos == 0)
1095 return SEQ_START_TOKEN;
1097 list_for_each_entry(self, &irlans, dev_list) {
1098 if (*pos == i)
1099 return self;
1100 ++i;
1102 return NULL;
1105 /* Return entry after v, and increment pos */
1106 static void *irlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1108 struct list_head *nxt;
1110 ++*pos;
1111 if (v == SEQ_START_TOKEN)
1112 nxt = irlans.next;
1113 else
1114 nxt = ((struct irlan_cb *)v)->dev_list.next;
1116 return (nxt == &irlans) ? NULL
1117 : list_entry(nxt, struct irlan_cb, dev_list);
1120 /* End of reading /proc file */
1121 static void irlan_seq_stop(struct seq_file *seq, void *v)
1123 rcu_read_unlock();
1128 * Show one entry in /proc file.
1130 static int irlan_seq_show(struct seq_file *seq, void *v)
1132 if (v == SEQ_START_TOKEN)
1133 seq_puts(seq, "IrLAN instances:\n");
1134 else {
1135 struct irlan_cb *self = v;
1137 ASSERT(self != NULL, return -1;);
1138 ASSERT(self->magic == IRLAN_MAGIC, return -1;);
1140 seq_printf(seq,"ifname: %s,\n",
1141 self->dev->name);
1142 seq_printf(seq,"client state: %s, ",
1143 irlan_state[ self->client.state]);
1144 seq_printf(seq,"provider state: %s,\n",
1145 irlan_state[ self->provider.state]);
1146 seq_printf(seq,"saddr: %#08x, ",
1147 self->saddr);
1148 seq_printf(seq,"daddr: %#08x\n",
1149 self->daddr);
1150 seq_printf(seq,"version: %d.%d,\n",
1151 self->version[1], self->version[0]);
1152 seq_printf(seq,"access type: %s\n",
1153 irlan_access[self->client.access_type]);
1154 seq_printf(seq,"media: %s\n",
1155 irlan_media[self->media]);
1157 seq_printf(seq,"local filter:\n");
1158 seq_printf(seq,"remote filter: ");
1159 irlan_print_filter(seq, self->client.filter_type);
1160 seq_printf(seq,"tx busy: %s\n",
1161 netif_queue_stopped(self->dev) ? "TRUE" : "FALSE");
1163 seq_putc(seq,'\n');
1165 return 0;
1168 static struct seq_operations irlan_seq_ops = {
1169 .start = irlan_seq_start,
1170 .next = irlan_seq_next,
1171 .stop = irlan_seq_stop,
1172 .show = irlan_seq_show,
1175 static int irlan_seq_open(struct inode *inode, struct file *file)
1177 return seq_open(file, &irlan_seq_ops);
1179 #endif
1181 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1182 MODULE_DESCRIPTION("The Linux IrDA LAN protocol");
1183 MODULE_LICENSE("GPL");
1185 module_param(eth, bool, 0);
1186 MODULE_PARM_DESC(eth, "Name devices ethX (0) or irlanX (1)");
1187 module_param(access, int, 0);
1188 MODULE_PARM_DESC(access, "Access type DIRECT=1, PEER=2, HOSTED=3");
1190 module_init(irlan_init);
1191 module_exit(irlan_cleanup);