blob: 5a2d0a69552993ce5e88bf7efa9ea8e3745424c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090011 *
12 * Copyright (c) 1997, 1999 Dag Brattli <dagb@cs.uit.no>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * All Rights Reserved.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090014 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * the License, or (at your option) any later version.
19 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020 * Neither Dag Brattli nor University of Tromsø admit liability nor
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090021 * provide warranty for any of this software. This material is
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * provided "AS-IS" and at no charge.
23 *
24 ********************************************************************/
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27
28#include <linux/kernel.h>
29#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/errno.h>
33#include <linux/proc_fs.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040034#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/seq_file.h>
36#include <linux/random.h>
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
39#include <linux/rtnetlink.h>
40#include <linux/moduleparam.h>
41#include <linux/bitops.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/byteorder.h>
44
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>
50
51#include <net/irda/irlan_common.h>
52#include <net/irda/irlan_client.h>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090053#include <net/irda/irlan_provider.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/irda/irlan_eth.h>
55#include <net/irda/irlan_filter.h>
56
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* extern char sysctl_devname[]; */
59
60/*
61 * Master structure
62 */
63static LIST_HEAD(irlans);
64
65static void *ckey;
66static void *skey;
67
68/* Module parameters */
Rusty Russelleb939922011-12-19 14:08:01 +000069static bool eth; /* Use "eth" or "irlan" name for devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int access = ACCESS_PEER; /* PEER, DIRECT or HOSTED */
71
72#ifdef CONFIG_PROC_FS
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070073static const char *const irlan_access[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 "UNKNOWN",
75 "DIRECT",
76 "PEER",
77 "HOSTED"
78};
79
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070080static const char *const irlan_media[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 "UNKNOWN",
82 "802.3",
83 "802.5"
84};
85
86extern struct proc_dir_entry *proc_irda;
87
88static int irlan_seq_open(struct inode *inode, struct file *file);
89
Arjan van de Venda7071d2007-02-12 00:55:36 -080090static const struct file_operations irlan_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .owner = THIS_MODULE,
92 .open = irlan_seq_open,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = seq_release,
96};
97
98extern struct proc_dir_entry *proc_irda;
99#endif /* CONFIG_PROC_FS */
100
Fabian Frederick0d4a2f92014-09-30 22:24:04 +0200101static struct irlan_cb __init *irlan_open(__u32 saddr, __u32 daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static void __irlan_close(struct irlan_cb *self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900103static int __irlan_insert_param(struct sk_buff *skb, char *param, int type,
104 __u8 value_byte, __u16 value_short,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 __u8 *value_array, __u16 value_len);
106static void irlan_open_unicast_addr(struct irlan_cb *self);
107static void irlan_get_unicast_addr(struct irlan_cb *self);
108void irlan_close_tsaps(struct irlan_cb *self);
109
110/*
111 * Function irlan_init (void)
112 *
113 * Initialize IrLAN layer
114 *
115 */
116static int __init irlan_init(void)
117{
118 struct irlan_cb *new;
119 __u16 hints;
120
Harvey Harrison0dc47872008-03-05 20:47:47 -0800121 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123#ifdef CONFIG_PROC_FS
124 { struct proc_dir_entry *proc;
Wang Chen5e478792008-02-29 10:34:45 -0800125 proc = proc_create("irlan", 0, proc_irda, &irlan_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!proc) {
127 printk(KERN_ERR "irlan_init: can't create /proc entry!\n");
128 return -ENODEV;
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131#endif /* CONFIG_PROC_FS */
132
Harvey Harrison0dc47872008-03-05 20:47:47 -0800133 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 hints = irlmp_service_to_hint(S_LAN);
135
136 /* Register with IrLMP as a client */
137 ckey = irlmp_register_client(hints, &irlan_client_discovery_indication,
138 NULL, NULL);
Akinobu Mita719647e2007-02-07 00:12:13 -0800139 if (!ckey)
140 goto err_ckey;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Register with IrLMP as a service */
Akinobu Mita719647e2007-02-07 00:12:13 -0800143 skey = irlmp_register_service(hints);
144 if (!skey)
145 goto err_skey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* Start the master IrLAN instance (the only one for now) */
Akinobu Mita719647e2007-02-07 00:12:13 -0800148 new = irlan_open(DEV_ADDR_ANY, DEV_ADDR_ANY);
149 if (!new)
150 goto err_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* The master will only open its (listen) control TSAP */
153 irlan_provider_open_ctrl_tsap(new);
154
155 /* Do some fast discovery! */
156 irlmp_discovery_request(DISCOVERY_DEFAULT_SLOTS);
157
158 return 0;
Akinobu Mita719647e2007-02-07 00:12:13 -0800159
160err_open:
161 irlmp_unregister_service(skey);
162err_skey:
163 irlmp_unregister_client(ckey);
164err_ckey:
165#ifdef CONFIG_PROC_FS
166 remove_proc_entry("irlan", proc_irda);
167#endif /* CONFIG_PROC_FS */
168
169 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900172static void __exit irlan_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct irlan_cb *self, *next;
175
Harvey Harrison0dc47872008-03-05 20:47:47 -0800176 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 irlmp_unregister_client(ckey);
179 irlmp_unregister_service(skey);
180
181#ifdef CONFIG_PROC_FS
182 remove_proc_entry("irlan", proc_irda);
183#endif /* CONFIG_PROC_FS */
184
185 /* Cleanup any leftover network devices */
186 rtnl_lock();
187 list_for_each_entry_safe(self, next, &irlans, dev_list) {
188 __irlan_close(self);
189 }
190 rtnl_unlock();
191}
192
193/*
194 * Function irlan_open (void)
195 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900196 * Open new instance of a client/provider, we should only register the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * network device if this instance is ment for a particular client/provider
198 */
Fabian Frederick0d4a2f92014-09-30 22:24:04 +0200199static struct irlan_cb __init *irlan_open(__u32 saddr, __u32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct net_device *dev;
202 struct irlan_cb *self;
203
Harvey Harrison0dc47872008-03-05 20:47:47 -0800204 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Create network device with irlan */
207 dev = alloc_irlandev(eth ? "eth%d" : "irlan%d");
208 if (!dev)
209 return NULL;
210
Wang Chen524ad0a2008-11-12 23:39:10 -0800211 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 self->dev = dev;
213
214 /*
215 * Initialize local device structure
216 */
217 self->magic = IRLAN_MAGIC;
218 self->saddr = saddr;
219 self->daddr = daddr;
220
221 /* Provider access can only be PEER, DIRECT, or HOSTED */
222 self->provider.access_type = access;
223 if (access == ACCESS_DIRECT) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900224 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * Since we are emulating an IrLAN sever we will have to
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900226 * give ourself an ethernet address!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
228 dev->dev_addr[0] = 0x40;
229 dev->dev_addr[1] = 0x00;
230 dev->dev_addr[2] = 0x00;
231 dev->dev_addr[3] = 0x00;
232 get_random_bytes(dev->dev_addr+4, 1);
233 get_random_bytes(dev->dev_addr+5, 1);
234 }
235
236 self->media = MEDIA_802_3;
237 self->disconnect_reason = LM_USER_REQUEST;
238 init_timer(&self->watchdog_timer);
239 init_timer(&self->client.kick_timer);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900240 init_waitqueue_head(&self->open_wait);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 skb_queue_head_init(&self->client.txq);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 irlan_next_client_state(self, IRLAN_IDLE);
245 irlan_next_provider_state(self, IRLAN_IDLE);
246
247 if (register_netdev(dev)) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900248 IRDA_DEBUG(2, "%s(), register_netdev() failed!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800249 __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 self = NULL;
251 free_netdev(dev);
252 } else {
253 rtnl_lock();
254 list_add_rcu(&self->dev_list, &irlans);
255 rtnl_unlock();
256 }
257
258 return self;
259}
260/*
261 * Function __irlan_close (self)
262 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900263 * This function closes and deallocates the IrLAN client instances. Be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * aware that other functions which calls client_close() must
265 * remove self from irlans list first.
266 */
267static void __irlan_close(struct irlan_cb *self)
268{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800269 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ASSERT_RTNL();
272 IRDA_ASSERT(self != NULL, return;);
273 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
274
275 del_timer_sync(&self->watchdog_timer);
276 del_timer_sync(&self->client.kick_timer);
277
278 /* Close all open connections and remove TSAPs */
279 irlan_close_tsaps(self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900280
281 if (self->client.iriap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 iriap_close(self->client.iriap);
283
284 /* Remove frames queued on the control channel */
285 skb_queue_purge(&self->client.txq);
286
287 /* Unregister and free self via destructor */
288 unregister_netdevice(self->dev);
289}
290
291/* Find any instance of irlan, used for client discovery wakeup */
292struct irlan_cb *irlan_get_any(void)
293{
294 struct irlan_cb *self;
295
296 list_for_each_entry_rcu(self, &irlans, dev_list) {
297 return self;
298 }
299 return NULL;
300}
301
302/*
303 * Function irlan_connect_indication (instance, sap, qos, max_sdu_size, skb)
304 *
305 * Here we receive the connect indication for the data channel
306 *
307 */
308static void irlan_connect_indication(void *instance, void *sap,
309 struct qos_info *qos,
310 __u32 max_sdu_size,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900311 __u8 max_header_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct sk_buff *skb)
313{
314 struct irlan_cb *self;
315 struct tsap_cb *tsap;
316
Harvey Harrison0dc47872008-03-05 20:47:47 -0800317 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900318
Joe Perchesea110732011-06-13 16:21:26 +0000319 self = instance;
320 tsap = sap;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 IRDA_ASSERT(self != NULL, return;);
323 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
324 IRDA_ASSERT(tsap == self->tsap_data,return;);
325
326 self->max_sdu_size = max_sdu_size;
327 self->max_header_size = max_header_size;
328
Harvey Harrison0dc47872008-03-05 20:47:47 -0800329 IRDA_DEBUG(0, "%s: We are now connected!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 del_timer(&self->watchdog_timer);
332
333 /* If you want to pass the skb to *both* state machines, you will
334 * need to skb_clone() it, so that you don't free it twice.
335 * As the state machines don't need it, git rid of it here...
336 * Jean II */
337 if (skb)
338 dev_kfree_skb(skb);
339
340 irlan_do_provider_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
341 irlan_do_client_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
342
343 if (self->provider.access_type == ACCESS_PEER) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900344 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * Data channel is open, so we are now allowed to
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900346 * configure the remote filter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348 irlan_get_unicast_addr(self);
349 irlan_open_unicast_addr(self);
350 }
351 /* Ready to transfer Ethernet frames (at last) */
352 netif_start_queue(self->dev); /* Clear reason */
353}
354
355static void irlan_connect_confirm(void *instance, void *sap,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900356 struct qos_info *qos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 __u32 max_sdu_size,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900358 __u8 max_header_size,
359 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct irlan_cb *self;
362
Joe Perchesea110732011-06-13 16:21:26 +0000363 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 IRDA_ASSERT(self != NULL, return;);
366 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
367
368 self->max_sdu_size = max_sdu_size;
369 self->max_header_size = max_header_size;
370
371 /* TODO: we could set the MTU depending on the max_sdu_size */
372
Harvey Harrison0dc47872008-03-05 20:47:47 -0800373 IRDA_DEBUG(0, "%s: We are now connected!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 del_timer(&self->watchdog_timer);
375
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900376 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * Data channel is open, so we are now allowed to configure the remote
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900378 * filter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380 irlan_get_unicast_addr(self);
381 irlan_open_unicast_addr(self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Open broadcast and multicast filter by default */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900384 irlan_set_broadcast_filter(self, TRUE);
385 irlan_set_multicast_filter(self, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Ready to transfer Ethernet frames */
388 netif_start_queue(self->dev);
389 self->disconnect_reason = 0; /* Clear reason */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 wake_up_interruptible(&self->open_wait);
391}
392
393/*
394 * Function irlan_client_disconnect_indication (handle)
395 *
396 * Callback function for the IrTTP layer. Indicates a disconnection of
397 * the specified connection (handle)
398 */
399static void irlan_disconnect_indication(void *instance,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900400 void *sap, LM_REASON reason,
401 struct sk_buff *userdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct irlan_cb *self;
404 struct tsap_cb *tsap;
405
Harvey Harrison0dc47872008-03-05 20:47:47 -0800406 IRDA_DEBUG(0, "%s(), reason=%d\n", __func__ , reason);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900407
Joe Perchesea110732011-06-13 16:21:26 +0000408 self = instance;
409 tsap = sap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 IRDA_ASSERT(self != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900412 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 IRDA_ASSERT(tsap != NULL, return;);
414 IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 IRDA_ASSERT(tsap == self->tsap_data, return;);
417
418 IRDA_DEBUG(2, "IrLAN, data channel disconnected by peer!\n");
419
420 /* Save reason so we know if we should try to reconnect or not */
421 self->disconnect_reason = reason;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 switch (reason) {
424 case LM_USER_REQUEST: /* User request */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800425 IRDA_DEBUG(2, "%s(), User requested\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 case LM_LAP_DISCONNECT: /* Unexpected IrLAP disconnect */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800428 IRDA_DEBUG(2, "%s(), Unexpected IrLAP disconnect\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430 case LM_CONNECT_FAILURE: /* Failed to establish IrLAP connection */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800431 IRDA_DEBUG(2, "%s(), IrLAP connect failed\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433 case LM_LAP_RESET: /* IrLAP reset */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800434 IRDA_DEBUG(2, "%s(), IrLAP reset\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436 case LM_INIT_DISCONNECT:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800437 IRDA_DEBUG(2, "%s(), IrLMP connect failed\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
439 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800440 IRDA_ERROR("%s(), Unknown disconnect reason\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* If you want to pass the skb to *both* state machines, you will
445 * need to skb_clone() it, so that you don't free it twice.
446 * As the state machines don't need it, git rid of it here...
447 * Jean II */
448 if (userdata)
449 dev_kfree_skb(userdata);
450
451 irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
452 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 wake_up_interruptible(&self->open_wait);
455}
456
457void irlan_open_data_tsap(struct irlan_cb *self)
458{
459 struct tsap_cb *tsap;
460 notify_t notify;
461
Harvey Harrison0dc47872008-03-05 20:47:47 -0800462 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 IRDA_ASSERT(self != NULL, return;);
465 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
466
467 /* Check if already open */
468 if (self->tsap_data)
469 return;
470
471 irda_notify_init(&notify);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 notify.data_indication = irlan_eth_receive;
474 notify.udata_indication = irlan_eth_receive;
475 notify.connect_indication = irlan_connect_indication;
476 notify.connect_confirm = irlan_connect_confirm;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900477 notify.flow_indication = irlan_eth_flow_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 notify.disconnect_indication = irlan_disconnect_indication;
479 notify.instance = self;
480 strlcpy(notify.name, "IrLAN data", sizeof(notify.name));
481
482 tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
483 if (!tsap) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800484 IRDA_DEBUG(2, "%s(), Got no tsap!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return;
486 }
487 self->tsap_data = tsap;
488
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900489 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * This is the data TSAP selector which we will pass to the client
491 * when the client ask for it.
492 */
493 self->stsap_sel_data = self->tsap_data->stsap_sel;
494}
495
496void irlan_close_tsaps(struct irlan_cb *self)
497{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800498 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 IRDA_ASSERT(self != NULL, return;);
501 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
502
503 /* Disconnect and close all open TSAP connections */
504 if (self->tsap_data) {
505 irttp_disconnect_request(self->tsap_data, NULL, P_NORMAL);
506 irttp_close_tsap(self->tsap_data);
507 self->tsap_data = NULL;
508 }
509 if (self->client.tsap_ctrl) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900510 irttp_disconnect_request(self->client.tsap_ctrl, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 P_NORMAL);
512 irttp_close_tsap(self->client.tsap_ctrl);
513 self->client.tsap_ctrl = NULL;
514 }
515 if (self->provider.tsap_ctrl) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900516 irttp_disconnect_request(self->provider.tsap_ctrl, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 P_NORMAL);
518 irttp_close_tsap(self->provider.tsap_ctrl);
519 self->provider.tsap_ctrl = NULL;
520 }
521 self->disconnect_reason = LM_USER_REQUEST;
522}
523
524/*
525 * Function irlan_ias_register (self, tsap_sel)
526 *
527 * Register with LM-IAS
528 *
529 */
530void irlan_ias_register(struct irlan_cb *self, __u8 tsap_sel)
531{
532 struct ias_object *obj;
533 struct ias_value *new_value;
534
535 IRDA_ASSERT(self != NULL, return;);
536 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900537
538 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * Check if object has already been registered by a previous provider.
540 * If that is the case, we just change the value of the attribute
541 */
542 if (!irias_find_object("IrLAN")) {
543 obj = irias_new_object("IrLAN", IAS_IRLAN_ID);
544 irias_add_integer_attrib(obj, "IrDA:TinyTP:LsapSel", tsap_sel,
545 IAS_KERNEL_ATTR);
546 irias_insert_object(obj);
547 } else {
548 new_value = irias_new_integer_value(tsap_sel);
549 irias_object_change_attribute("IrLAN", "IrDA:TinyTP:LsapSel",
550 new_value);
551 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900552
553 /* Register PnP object only if not registered before */
554 if (!irias_find_object("PnP")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 obj = irias_new_object("PnP", IAS_PNP_ID);
556#if 0
557 irias_add_string_attrib(obj, "Name", sysctl_devname,
558 IAS_KERNEL_ATTR);
559#else
560 irias_add_string_attrib(obj, "Name", "Linux", IAS_KERNEL_ATTR);
561#endif
562 irias_add_string_attrib(obj, "DeviceID", "HWP19F0",
563 IAS_KERNEL_ATTR);
564 irias_add_integer_attrib(obj, "CompCnt", 1, IAS_KERNEL_ATTR);
565 if (self->provider.access_type == ACCESS_PEER)
566 irias_add_string_attrib(obj, "Comp#01", "PNP8389",
567 IAS_KERNEL_ATTR);
568 else
569 irias_add_string_attrib(obj, "Comp#01", "PNP8294",
570 IAS_KERNEL_ATTR);
571
572 irias_add_string_attrib(obj, "Manufacturer",
573 "Linux-IrDA Project", IAS_KERNEL_ATTR);
574 irias_insert_object(obj);
575 }
576}
577
578/*
579 * Function irlan_run_ctrl_tx_queue (self)
580 *
581 * Try to send the next command in the control transmit queue
582 *
583 */
584int irlan_run_ctrl_tx_queue(struct irlan_cb *self)
585{
586 struct sk_buff *skb;
587
Harvey Harrison0dc47872008-03-05 20:47:47 -0800588 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (irda_lock(&self->client.tx_busy) == FALSE)
591 return -EBUSY;
592
593 skb = skb_dequeue(&self->client.txq);
594 if (!skb) {
595 self->client.tx_busy = FALSE;
596 return 0;
597 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Check that it's really possible to send commands */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900600 if ((self->client.tsap_ctrl == NULL) ||
601 (self->client.state == IRLAN_IDLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 {
603 self->client.tx_busy = FALSE;
604 dev_kfree_skb(skb);
605 return -1;
606 }
Harvey Harrison0dc47872008-03-05 20:47:47 -0800607 IRDA_DEBUG(2, "%s(), sending ...\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 return irttp_data_request(self->client.tsap_ctrl, skb);
610}
611
612/*
613 * Function irlan_ctrl_data_request (self, skb)
614 *
615 * This function makes sure that commands on the control channel is being
616 * sent in a command/response fashion
617 */
618static void irlan_ctrl_data_request(struct irlan_cb *self, struct sk_buff *skb)
619{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800620 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* Queue command */
623 skb_queue_tail(&self->client.txq, skb);
624
625 /* Try to send command */
626 irlan_run_ctrl_tx_queue(self);
627}
628
629/*
630 * Function irlan_get_provider_info (self)
631 *
632 * Send Get Provider Information command to peer IrLAN layer
633 *
634 */
635void irlan_get_provider_info(struct irlan_cb *self)
636{
637 struct sk_buff *skb;
638 __u8 *frame;
639
Harvey Harrison0dc47872008-03-05 20:47:47 -0800640 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 IRDA_ASSERT(self != NULL, return;);
643 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
644
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700645 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER,
646 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!skb)
648 return;
649
650 /* Reserve space for TTP, LMP, and LAP header */
651 skb_reserve(skb, self->client.max_header_size);
652 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900655
656 frame[0] = CMD_GET_PROVIDER_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 frame[1] = 0x00; /* Zero parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 irlan_ctrl_data_request(self, skb);
660}
661
662/*
663 * Function irlan_open_data_channel (self)
664 *
665 * Send an Open Data Command to provider
666 *
667 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900668void irlan_open_data_channel(struct irlan_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct sk_buff *skb;
671 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900672
Harvey Harrison0dc47872008-03-05 20:47:47 -0800673 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 IRDA_ASSERT(self != NULL, return;);
676 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900677
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700678 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
679 IRLAN_STRING_PARAMETER_LEN("MEDIA", "802.3") +
680 IRLAN_STRING_PARAMETER_LEN("ACCESS_TYPE", "DIRECT"),
681 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!skb)
683 return;
684
685 skb_reserve(skb, self->client.max_header_size);
686 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* Build frame */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900691 frame[0] = CMD_OPEN_DATA_CHANNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 frame[1] = 0x02; /* Two parameters */
693
694 irlan_insert_string_param(skb, "MEDIA", "802.3");
695 irlan_insert_string_param(skb, "ACCESS_TYPE", "DIRECT");
696 /* irlan_insert_string_param(skb, "MODE", "UNRELIABLE"); */
697
698/* self->use_udata = TRUE; */
699
700 irlan_ctrl_data_request(self, skb);
701}
702
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900703void irlan_close_data_channel(struct irlan_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct sk_buff *skb;
706 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900707
Harvey Harrison0dc47872008-03-05 20:47:47 -0800708 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 IRDA_ASSERT(self != NULL, return;);
711 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
712
713 /* Check if the TSAP is still there */
714 if (self->client.tsap_ctrl == NULL)
715 return;
716
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700717 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
718 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN"),
719 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!skb)
721 return;
722
723 skb_reserve(skb, self->client.max_header_size);
724 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Build frame */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900729 frame[0] = CMD_CLOSE_DATA_CHAN;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700730 frame[1] = 0x01; /* One parameter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
733
734 irlan_ctrl_data_request(self, skb);
735}
736
737/*
738 * Function irlan_open_unicast_addr (self)
739 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900740 * Make IrLAN provider accept ethernet frames addressed to the unicast
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * address.
742 *
743 */
744static void irlan_open_unicast_addr(struct irlan_cb *self)
745{
746 struct sk_buff *skb;
747 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900748
Harvey Harrison0dc47872008-03-05 20:47:47 -0800749 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 IRDA_ASSERT(self != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900752 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
753
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700754 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
755 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
756 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
757 IRLAN_STRING_PARAMETER_LEN("FILTER_MODE", "FILTER"),
758 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!skb)
760 return;
761
762 /* Reserve space for TTP, LMP, and LAP header */
763 skb_reserve(skb, self->max_header_size);
764 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900767
768 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900770 irlan_insert_byte_param(skb, "DATA_CHAN" , self->dtsap_sel_data);
771 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
772 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER");
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 irlan_ctrl_data_request(self, skb);
775}
776
777/*
778 * Function irlan_set_broadcast_filter (self, status)
779 *
780 * Make IrLAN provider accept ethernet frames addressed to the broadcast
781 * address. Be careful with the use of this one, since there may be a lot
782 * of broadcast traffic out there. We can still function without this
783 * one but then _we_ have to initiate all communication with other
784 * hosts, since ARP request for this host will not be answered.
785 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900786void irlan_set_broadcast_filter(struct irlan_cb *self, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct sk_buff *skb;
789 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900790
Harvey Harrison0dc47872008-03-05 20:47:47 -0800791 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 IRDA_ASSERT(self != NULL, return;);
794 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900795
796 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700797 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
798 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "BROADCAST") +
799 /* We may waste one byte here...*/
800 IRLAN_STRING_PARAMETER_LEN("FILTER_MODE", "FILTER"),
801 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!skb)
803 return;
804
805 /* Reserve space for TTP, LMP, and LAP header */
806 skb_reserve(skb, self->client.max_header_size);
807 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900810
811 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900813 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
814 irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (status)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900816 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 else
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900818 irlan_insert_string_param(skb, "FILTER_MODE", "NONE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 irlan_ctrl_data_request(self, skb);
821}
822
823/*
824 * Function irlan_set_multicast_filter (self, status)
825 *
826 * Make IrLAN provider accept ethernet frames addressed to the multicast
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900827 * address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 *
829 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900830void irlan_set_multicast_filter(struct irlan_cb *self, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 struct sk_buff *skb;
833 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900834
Harvey Harrison0dc47872008-03-05 20:47:47 -0800835 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 IRDA_ASSERT(self != NULL, return;);
838 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
839
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700840 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
841 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
842 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "MULTICAST") +
843 /* We may waste one byte here...*/
844 IRLAN_STRING_PARAMETER_LEN("FILTER_MODE", "NONE"),
845 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!skb)
847 return;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Reserve space for TTP, LMP, and LAP header */
850 skb_reserve(skb, self->client.max_header_size);
851 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900854
855 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900857 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
858 irlan_insert_string_param(skb, "FILTER_TYPE", "MULTICAST");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (status)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900860 irlan_insert_string_param(skb, "FILTER_MODE", "ALL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 else
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900862 irlan_insert_string_param(skb, "FILTER_MODE", "NONE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 irlan_ctrl_data_request(self, skb);
865}
866
867/*
868 * Function irlan_get_unicast_addr (self)
869 *
870 * Retrieves the unicast address from the IrLAN provider. This address
871 * will be inserted into the devices structure, so the ethernet layer
872 * can construct its packets.
873 *
874 */
875static void irlan_get_unicast_addr(struct irlan_cb *self)
876{
877 struct sk_buff *skb;
878 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900879
Harvey Harrison0dc47872008-03-05 20:47:47 -0800880 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 IRDA_ASSERT(self != NULL, return;);
883 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900884
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700885 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
886 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
887 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
888 IRLAN_STRING_PARAMETER_LEN("FILTER_OPERATION",
889 "DYNAMIC"),
890 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (!skb)
892 return;
893
894 /* Reserve space for TTP, LMP, and LAP header */
895 skb_reserve(skb, self->client.max_header_size);
896 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900899
900 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900902 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
903 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
904 irlan_insert_string_param(skb, "FILTER_OPERATION", "DYNAMIC");
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 irlan_ctrl_data_request(self, skb);
907}
908
909/*
910 * Function irlan_get_media_char (self)
911 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900912 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *
914 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900915void irlan_get_media_char(struct irlan_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct sk_buff *skb;
918 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900919
Harvey Harrison0dc47872008-03-05 20:47:47 -0800920 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 IRDA_ASSERT(self != NULL, return;);
923 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900924
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700925 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
926 IRLAN_STRING_PARAMETER_LEN("MEDIA", "802.3"),
927 GFP_ATOMIC);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!skb)
930 return;
931
932 /* Reserve space for TTP, LMP, and LAP header */
933 skb_reserve(skb, self->client.max_header_size);
934 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* Build frame */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900939 frame[0] = CMD_GET_MEDIA_CHAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 frame[1] = 0x01; /* One parameter */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 irlan_insert_string_param(skb, "MEDIA", "802.3");
943 irlan_ctrl_data_request(self, skb);
944}
945
946/*
947 * Function insert_byte_param (skb, param, value)
948 *
949 * Insert byte parameter into frame
950 *
951 */
952int irlan_insert_byte_param(struct sk_buff *skb, char *param, __u8 value)
953{
954 return __irlan_insert_param(skb, param, IRLAN_BYTE, value, 0, NULL, 0);
955}
956
957int irlan_insert_short_param(struct sk_buff *skb, char *param, __u16 value)
958{
959 return __irlan_insert_param(skb, param, IRLAN_SHORT, 0, value, NULL, 0);
960}
961
962/*
963 * Function insert_string (skb, param, value)
964 *
965 * Insert string parameter into frame
966 *
967 */
968int irlan_insert_string_param(struct sk_buff *skb, char *param, char *string)
969{
970 int string_len = strlen(string);
971
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900972 return __irlan_insert_param(skb, param, IRLAN_ARRAY, 0, 0, string,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 string_len);
974}
975
976/*
977 * Function insert_array_param(skb, param, value, len_value)
978 *
979 * Insert array parameter into frame
980 *
981 */
982int irlan_insert_array_param(struct sk_buff *skb, char *name, __u8 *array,
983 __u16 array_len)
984{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900985 return __irlan_insert_param(skb, name, IRLAN_ARRAY, 0, 0, array,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 array_len);
987}
988
989/*
990 * Function insert_param (skb, param, value, byte)
991 *
992 * Insert parameter at end of buffer, structure of a parameter is:
993 *
994 * -----------------------------------------------------------------------
995 * | Name Length[1] | Param Name[1..255] | Val Length[2] | Value[0..1016]|
996 * -----------------------------------------------------------------------
997 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900998static int __irlan_insert_param(struct sk_buff *skb, char *param, int type,
999 __u8 value_byte, __u16 value_short,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 __u8 *value_array, __u16 value_len)
1001{
1002 __u8 *frame;
1003 __u8 param_len;
Al Viro448c31a2006-11-14 20:47:46 -08001004 __le16 tmp_le; /* Temporary value in little endian format */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int n=0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (skb == NULL) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001008 IRDA_DEBUG(2, "%s(), Got NULL skb\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 param_len = strlen(param);
1013 switch (type) {
1014 case IRLAN_BYTE:
1015 value_len = 1;
1016 break;
1017 case IRLAN_SHORT:
1018 value_len = 2;
1019 break;
1020 case IRLAN_ARRAY:
1021 IRDA_ASSERT(value_array != NULL, return 0;);
1022 IRDA_ASSERT(value_len > 0, return 0;);
1023 break;
1024 default:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001025 IRDA_DEBUG(2, "%s(), Unknown parameter type!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* Insert at end of sk-buffer */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001030 frame = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 /* Make space for data */
1033 if (skb_tailroom(skb) < (param_len+value_len+3)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001034 IRDA_DEBUG(2, "%s(), No more space at end of skb\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 skb_put(skb, param_len+value_len+3);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Insert parameter length */
1040 frame[n++] = param_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* Insert parameter */
1043 memcpy(frame+n, param, param_len); n += param_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* Insert value length (2 byte little endian format, LSB first) */
1046 tmp_le = cpu_to_le16(value_len);
1047 memcpy(frame+n, &tmp_le, 2); n += 2; /* To avoid alignment problems */
1048
1049 /* Insert value */
1050 switch (type) {
1051 case IRLAN_BYTE:
1052 frame[n++] = value_byte;
1053 break;
1054 case IRLAN_SHORT:
1055 tmp_le = cpu_to_le16(value_short);
1056 memcpy(frame+n, &tmp_le, 2); n += 2;
1057 break;
1058 case IRLAN_ARRAY:
1059 memcpy(frame+n, value_array, value_len); n+=value_len;
1060 break;
1061 default:
1062 break;
1063 }
1064 IRDA_ASSERT(n == (param_len+value_len+3), return 0;);
1065
1066 return param_len+value_len+3;
1067}
1068
1069/*
1070 * Function irlan_extract_param (buf, name, value, len)
1071 *
1072 * Extracts a single parameter name/value pair from buffer and updates
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001073 * the buffer pointer to point to the next name/value pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
1075int irlan_extract_param(__u8 *buf, char *name, char *value, __u16 *len)
1076{
1077 __u8 name_len;
1078 __u16 val_len;
1079 int n=0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001080
Harvey Harrison0dc47872008-03-05 20:47:47 -08001081 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* get length of parameter name (1 byte) */
1084 name_len = buf[n++];
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (name_len > 254) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001087 IRDA_DEBUG(2, "%s(), name_len > 254\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -RSP_INVALID_COMMAND_FORMAT;
1089 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* get parameter name */
1092 memcpy(name, buf+n, name_len);
1093 name[name_len] = '\0';
1094 n+=name_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001095
1096 /*
1097 * Get length of parameter value (2 bytes in little endian
1098 * format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
1100 memcpy(&val_len, buf+n, 2); /* To avoid alignment problems */
1101 le16_to_cpus(&val_len); n+=2;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001102
Dan Carpentercf9b94f2010-09-04 03:14:35 +00001103 if (val_len >= 1016) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001104 IRDA_DEBUG(2, "%s(), parameter length to long\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -RSP_INVALID_COMMAND_FORMAT;
1106 }
1107 *len = val_len;
1108
1109 /* get parameter value */
1110 memcpy(value, buf+n, val_len);
1111 value[val_len] = '\0';
1112 n+=val_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001113
1114 IRDA_DEBUG(4, "Parameter: %s ", name);
1115 IRDA_DEBUG(4, "Value: %s\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return n;
1118}
1119
1120#ifdef CONFIG_PROC_FS
1121
1122/*
1123 * Start of reading /proc entries.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001124 * Return entry at pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * or start_token to indicate print header line
1126 * or NULL if end of file
1127 */
1128static void *irlan_seq_start(struct seq_file *seq, loff_t *pos)
1129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 rcu_read_lock();
Li Zefan216437e2010-02-08 23:21:05 +00001131 return seq_list_start_head(&irlans, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134/* Return entry after v, and increment pos */
1135static void *irlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1136{
Li Zefan216437e2010-02-08 23:21:05 +00001137 return seq_list_next(v, &irlans, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140/* End of reading /proc file */
1141static void irlan_seq_stop(struct seq_file *seq, void *v)
1142{
1143 rcu_read_unlock();
1144}
1145
1146
1147/*
1148 * Show one entry in /proc file.
1149 */
1150static int irlan_seq_show(struct seq_file *seq, void *v)
1151{
Li Zefan216437e2010-02-08 23:21:05 +00001152 if (v == &irlans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 seq_puts(seq, "IrLAN instances:\n");
1154 else {
Li Zefan216437e2010-02-08 23:21:05 +00001155 struct irlan_cb *self = list_entry(v, struct irlan_cb, dev_list);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 IRDA_ASSERT(self != NULL, return -1;);
1158 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
1159
1160 seq_printf(seq,"ifname: %s,\n",
1161 self->dev->name);
1162 seq_printf(seq,"client state: %s, ",
1163 irlan_state[ self->client.state]);
1164 seq_printf(seq,"provider state: %s,\n",
1165 irlan_state[ self->provider.state]);
1166 seq_printf(seq,"saddr: %#08x, ",
1167 self->saddr);
1168 seq_printf(seq,"daddr: %#08x\n",
1169 self->daddr);
1170 seq_printf(seq,"version: %d.%d,\n",
1171 self->version[1], self->version[0]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001172 seq_printf(seq,"access type: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 irlan_access[self->client.access_type]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001174 seq_printf(seq,"media: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 irlan_media[self->media]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 seq_printf(seq,"local filter:\n");
1178 seq_printf(seq,"remote filter: ");
1179 irlan_print_filter(seq, self->client.filter_type);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001180 seq_printf(seq,"tx busy: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 netif_queue_stopped(self->dev) ? "TRUE" : "FALSE");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 seq_putc(seq,'\n');
1184 }
1185 return 0;
1186}
1187
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001188static const struct seq_operations irlan_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 .start = irlan_seq_start,
1190 .next = irlan_seq_next,
1191 .stop = irlan_seq_stop,
1192 .show = irlan_seq_show,
1193};
1194
1195static int irlan_seq_open(struct inode *inode, struct file *file)
1196{
1197 return seq_open(file, &irlan_seq_ops);
1198}
1199#endif
1200
1201MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001202MODULE_DESCRIPTION("The Linux IrDA LAN protocol");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203MODULE_LICENSE("GPL");
1204
1205module_param(eth, bool, 0);
1206MODULE_PARM_DESC(eth, "Name devices ethX (0) or irlanX (1)");
1207module_param(access, int, 0);
1208MODULE_PARM_DESC(access, "Access type DIRECT=1, PEER=2, HOSTED=3");
1209
1210module_init(irlan_init);
1211module_exit(irlan_cleanup);
1212