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