blob: 481bbc2a4349300d51c66452a5362b4e1b4b4c81 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#ifdef CONFIG_PROC_FS
122 { struct proc_dir_entry *proc;
Wang Chen5e478792008-02-29 10:34:45 -0800123 proc = proc_create("irlan", 0, proc_irda, &irlan_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!proc) {
125 printk(KERN_ERR "irlan_init: can't create /proc entry!\n");
126 return -ENODEV;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129#endif /* CONFIG_PROC_FS */
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 hints = irlmp_service_to_hint(S_LAN);
132
133 /* Register with IrLMP as a client */
134 ckey = irlmp_register_client(hints, &irlan_client_discovery_indication,
135 NULL, NULL);
Akinobu Mita719647e2007-02-07 00:12:13 -0800136 if (!ckey)
137 goto err_ckey;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Register with IrLMP as a service */
Akinobu Mita719647e2007-02-07 00:12:13 -0800140 skey = irlmp_register_service(hints);
141 if (!skey)
142 goto err_skey;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* Start the master IrLAN instance (the only one for now) */
Akinobu Mita719647e2007-02-07 00:12:13 -0800145 new = irlan_open(DEV_ADDR_ANY, DEV_ADDR_ANY);
146 if (!new)
147 goto err_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
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;
Akinobu Mita719647e2007-02-07 00:12:13 -0800156
157err_open:
158 irlmp_unregister_service(skey);
159err_skey:
160 irlmp_unregister_client(ckey);
161err_ckey:
162#ifdef CONFIG_PROC_FS
163 remove_proc_entry("irlan", proc_irda);
164#endif /* CONFIG_PROC_FS */
165
166 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900169static void __exit irlan_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct irlan_cb *self, *next;
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 irlmp_unregister_client(ckey);
174 irlmp_unregister_service(skey);
175
176#ifdef CONFIG_PROC_FS
177 remove_proc_entry("irlan", proc_irda);
178#endif /* CONFIG_PROC_FS */
179
180 /* Cleanup any leftover network devices */
181 rtnl_lock();
182 list_for_each_entry_safe(self, next, &irlans, dev_list) {
183 __irlan_close(self);
184 }
185 rtnl_unlock();
186}
187
188/*
189 * Function irlan_open (void)
190 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900191 * Open new instance of a client/provider, we should only register the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * network device if this instance is ment for a particular client/provider
193 */
Fabian Frederick0d4a2f92014-09-30 22:24:04 +0200194static struct irlan_cb __init *irlan_open(__u32 saddr, __u32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct net_device *dev;
197 struct irlan_cb *self;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Create network device with irlan */
200 dev = alloc_irlandev(eth ? "eth%d" : "irlan%d");
201 if (!dev)
202 return NULL;
203
Wang Chen524ad0a2008-11-12 23:39:10 -0800204 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 self->dev = dev;
206
207 /*
208 * Initialize local device structure
209 */
210 self->magic = IRLAN_MAGIC;
211 self->saddr = saddr;
212 self->daddr = daddr;
213
214 /* Provider access can only be PEER, DIRECT, or HOSTED */
215 self->provider.access_type = access;
216 if (access == ACCESS_DIRECT) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900217 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Since we are emulating an IrLAN sever we will have to
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900219 * give ourself an ethernet address!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
221 dev->dev_addr[0] = 0x40;
222 dev->dev_addr[1] = 0x00;
223 dev->dev_addr[2] = 0x00;
224 dev->dev_addr[3] = 0x00;
225 get_random_bytes(dev->dev_addr+4, 1);
226 get_random_bytes(dev->dev_addr+5, 1);
227 }
228
229 self->media = MEDIA_802_3;
230 self->disconnect_reason = LM_USER_REQUEST;
231 init_timer(&self->watchdog_timer);
232 init_timer(&self->client.kick_timer);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900233 init_waitqueue_head(&self->open_wait);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 skb_queue_head_init(&self->client.txq);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 irlan_next_client_state(self, IRLAN_IDLE);
238 irlan_next_provider_state(self, IRLAN_IDLE);
239
240 if (register_netdev(dev)) {
Joe Perches955a9d202014-11-11 14:44:57 -0800241 pr_debug("%s(), register_netdev() failed!\n",
242 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 self = NULL;
244 free_netdev(dev);
245 } else {
246 rtnl_lock();
247 list_add_rcu(&self->dev_list, &irlans);
248 rtnl_unlock();
249 }
250
251 return self;
252}
253/*
254 * Function __irlan_close (self)
255 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900256 * This function closes and deallocates the IrLAN client instances. Be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * aware that other functions which calls client_close() must
258 * remove self from irlans list first.
259 */
260static void __irlan_close(struct irlan_cb *self)
261{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ASSERT_RTNL();
263 IRDA_ASSERT(self != NULL, return;);
264 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
265
266 del_timer_sync(&self->watchdog_timer);
267 del_timer_sync(&self->client.kick_timer);
268
269 /* Close all open connections and remove TSAPs */
270 irlan_close_tsaps(self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900271
272 if (self->client.iriap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 iriap_close(self->client.iriap);
274
275 /* Remove frames queued on the control channel */
276 skb_queue_purge(&self->client.txq);
277
278 /* Unregister and free self via destructor */
279 unregister_netdevice(self->dev);
280}
281
282/* Find any instance of irlan, used for client discovery wakeup */
283struct irlan_cb *irlan_get_any(void)
284{
285 struct irlan_cb *self;
286
287 list_for_each_entry_rcu(self, &irlans, dev_list) {
288 return self;
289 }
290 return NULL;
291}
292
293/*
294 * Function irlan_connect_indication (instance, sap, qos, max_sdu_size, skb)
295 *
296 * Here we receive the connect indication for the data channel
297 *
298 */
299static void irlan_connect_indication(void *instance, void *sap,
300 struct qos_info *qos,
301 __u32 max_sdu_size,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900302 __u8 max_header_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct sk_buff *skb)
304{
305 struct irlan_cb *self;
306 struct tsap_cb *tsap;
307
Joe Perchesea110732011-06-13 16:21:26 +0000308 self = instance;
309 tsap = sap;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 IRDA_ASSERT(self != NULL, return;);
312 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
313 IRDA_ASSERT(tsap == self->tsap_data,return;);
314
315 self->max_sdu_size = max_sdu_size;
316 self->max_header_size = max_header_size;
317
Joe Perches955a9d202014-11-11 14:44:57 -0800318 pr_debug("%s: We are now connected!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 del_timer(&self->watchdog_timer);
321
322 /* If you want to pass the skb to *both* state machines, you will
323 * need to skb_clone() it, so that you don't free it twice.
324 * As the state machines don't need it, git rid of it here...
325 * Jean II */
326 if (skb)
327 dev_kfree_skb(skb);
328
329 irlan_do_provider_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
330 irlan_do_client_event(self, IRLAN_DATA_CONNECT_INDICATION, NULL);
331
332 if (self->provider.access_type == ACCESS_PEER) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900333 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * Data channel is open, so we are now allowed to
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900335 * configure the remote filter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337 irlan_get_unicast_addr(self);
338 irlan_open_unicast_addr(self);
339 }
340 /* Ready to transfer Ethernet frames (at last) */
341 netif_start_queue(self->dev); /* Clear reason */
342}
343
344static void irlan_connect_confirm(void *instance, void *sap,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900345 struct qos_info *qos,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 __u32 max_sdu_size,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900347 __u8 max_header_size,
348 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct irlan_cb *self;
351
Joe Perchesea110732011-06-13 16:21:26 +0000352 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 IRDA_ASSERT(self != NULL, return;);
355 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
356
357 self->max_sdu_size = max_sdu_size;
358 self->max_header_size = max_header_size;
359
360 /* TODO: we could set the MTU depending on the max_sdu_size */
361
Joe Perches955a9d202014-11-11 14:44:57 -0800362 pr_debug("%s: We are now connected!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 del_timer(&self->watchdog_timer);
364
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900365 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Data channel is open, so we are now allowed to configure the remote
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900367 * filter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
369 irlan_get_unicast_addr(self);
370 irlan_open_unicast_addr(self);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* Open broadcast and multicast filter by default */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900373 irlan_set_broadcast_filter(self, TRUE);
374 irlan_set_multicast_filter(self, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* Ready to transfer Ethernet frames */
377 netif_start_queue(self->dev);
378 self->disconnect_reason = 0; /* Clear reason */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 wake_up_interruptible(&self->open_wait);
380}
381
382/*
383 * Function irlan_client_disconnect_indication (handle)
384 *
385 * Callback function for the IrTTP layer. Indicates a disconnection of
386 * the specified connection (handle)
387 */
388static void irlan_disconnect_indication(void *instance,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900389 void *sap, LM_REASON reason,
390 struct sk_buff *userdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct irlan_cb *self;
393 struct tsap_cb *tsap;
394
Joe Perches955a9d202014-11-11 14:44:57 -0800395 pr_debug("%s(), reason=%d\n", __func__ , reason);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900396
Joe Perchesea110732011-06-13 16:21:26 +0000397 self = instance;
398 tsap = sap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 IRDA_ASSERT(self != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900401 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 IRDA_ASSERT(tsap != NULL, return;);
403 IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 IRDA_ASSERT(tsap == self->tsap_data, return;);
406
Joe Perches955a9d202014-11-11 14:44:57 -0800407 pr_debug("IrLAN, data channel disconnected by peer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* Save reason so we know if we should try to reconnect or not */
410 self->disconnect_reason = reason;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 switch (reason) {
413 case LM_USER_REQUEST: /* User request */
Joe Perches955a9d202014-11-11 14:44:57 -0800414 pr_debug("%s(), User requested\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 case LM_LAP_DISCONNECT: /* Unexpected IrLAP disconnect */
Joe Perches955a9d202014-11-11 14:44:57 -0800417 pr_debug("%s(), Unexpected IrLAP disconnect\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 case LM_CONNECT_FAILURE: /* Failed to establish IrLAP connection */
Joe Perches955a9d202014-11-11 14:44:57 -0800420 pr_debug("%s(), IrLAP connect failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422 case LM_LAP_RESET: /* IrLAP reset */
Joe Perches955a9d202014-11-11 14:44:57 -0800423 pr_debug("%s(), IrLAP reset\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 case LM_INIT_DISCONNECT:
Joe Perches955a9d202014-11-11 14:44:57 -0800426 pr_debug("%s(), IrLMP connect failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 default:
Joe Perches6c910232014-11-11 13:37:30 -0800429 net_err_ratelimited("%s(), Unknown disconnect reason\n",
430 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* If you want to pass the skb to *both* state machines, you will
435 * need to skb_clone() it, so that you don't free it twice.
436 * As the state machines don't need it, git rid of it here...
437 * Jean II */
438 if (userdata)
439 dev_kfree_skb(userdata);
440
441 irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
442 irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 wake_up_interruptible(&self->open_wait);
445}
446
447void irlan_open_data_tsap(struct irlan_cb *self)
448{
449 struct tsap_cb *tsap;
450 notify_t notify;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 IRDA_ASSERT(self != NULL, return;);
453 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
454
455 /* Check if already open */
456 if (self->tsap_data)
457 return;
458
459 irda_notify_init(&notify);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 notify.data_indication = irlan_eth_receive;
462 notify.udata_indication = irlan_eth_receive;
463 notify.connect_indication = irlan_connect_indication;
464 notify.connect_confirm = irlan_connect_confirm;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900465 notify.flow_indication = irlan_eth_flow_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 notify.disconnect_indication = irlan_disconnect_indication;
467 notify.instance = self;
468 strlcpy(notify.name, "IrLAN data", sizeof(notify.name));
469
470 tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
471 if (!tsap) {
Joe Perches955a9d202014-11-11 14:44:57 -0800472 pr_debug("%s(), Got no tsap!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return;
474 }
475 self->tsap_data = tsap;
476
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900477 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * This is the data TSAP selector which we will pass to the client
479 * when the client ask for it.
480 */
481 self->stsap_sel_data = self->tsap_data->stsap_sel;
482}
483
484void irlan_close_tsaps(struct irlan_cb *self)
485{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 IRDA_ASSERT(self != NULL, return;);
487 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
488
489 /* Disconnect and close all open TSAP connections */
490 if (self->tsap_data) {
491 irttp_disconnect_request(self->tsap_data, NULL, P_NORMAL);
492 irttp_close_tsap(self->tsap_data);
493 self->tsap_data = NULL;
494 }
495 if (self->client.tsap_ctrl) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900496 irttp_disconnect_request(self->client.tsap_ctrl, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 P_NORMAL);
498 irttp_close_tsap(self->client.tsap_ctrl);
499 self->client.tsap_ctrl = NULL;
500 }
501 if (self->provider.tsap_ctrl) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900502 irttp_disconnect_request(self->provider.tsap_ctrl, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 P_NORMAL);
504 irttp_close_tsap(self->provider.tsap_ctrl);
505 self->provider.tsap_ctrl = NULL;
506 }
507 self->disconnect_reason = LM_USER_REQUEST;
508}
509
510/*
511 * Function irlan_ias_register (self, tsap_sel)
512 *
513 * Register with LM-IAS
514 *
515 */
516void irlan_ias_register(struct irlan_cb *self, __u8 tsap_sel)
517{
518 struct ias_object *obj;
519 struct ias_value *new_value;
520
521 IRDA_ASSERT(self != NULL, return;);
522 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900523
524 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * Check if object has already been registered by a previous provider.
526 * If that is the case, we just change the value of the attribute
527 */
528 if (!irias_find_object("IrLAN")) {
529 obj = irias_new_object("IrLAN", IAS_IRLAN_ID);
530 irias_add_integer_attrib(obj, "IrDA:TinyTP:LsapSel", tsap_sel,
531 IAS_KERNEL_ATTR);
532 irias_insert_object(obj);
533 } else {
534 new_value = irias_new_integer_value(tsap_sel);
535 irias_object_change_attribute("IrLAN", "IrDA:TinyTP:LsapSel",
536 new_value);
537 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900538
539 /* Register PnP object only if not registered before */
540 if (!irias_find_object("PnP")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 obj = irias_new_object("PnP", IAS_PNP_ID);
542#if 0
543 irias_add_string_attrib(obj, "Name", sysctl_devname,
544 IAS_KERNEL_ATTR);
545#else
546 irias_add_string_attrib(obj, "Name", "Linux", IAS_KERNEL_ATTR);
547#endif
548 irias_add_string_attrib(obj, "DeviceID", "HWP19F0",
549 IAS_KERNEL_ATTR);
550 irias_add_integer_attrib(obj, "CompCnt", 1, IAS_KERNEL_ATTR);
551 if (self->provider.access_type == ACCESS_PEER)
552 irias_add_string_attrib(obj, "Comp#01", "PNP8389",
553 IAS_KERNEL_ATTR);
554 else
555 irias_add_string_attrib(obj, "Comp#01", "PNP8294",
556 IAS_KERNEL_ATTR);
557
558 irias_add_string_attrib(obj, "Manufacturer",
559 "Linux-IrDA Project", IAS_KERNEL_ATTR);
560 irias_insert_object(obj);
561 }
562}
563
564/*
565 * Function irlan_run_ctrl_tx_queue (self)
566 *
567 * Try to send the next command in the control transmit queue
568 *
569 */
570int irlan_run_ctrl_tx_queue(struct irlan_cb *self)
571{
572 struct sk_buff *skb;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (irda_lock(&self->client.tx_busy) == FALSE)
575 return -EBUSY;
576
577 skb = skb_dequeue(&self->client.txq);
578 if (!skb) {
579 self->client.tx_busy = FALSE;
580 return 0;
581 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Check that it's really possible to send commands */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900584 if ((self->client.tsap_ctrl == NULL) ||
585 (self->client.state == IRLAN_IDLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 {
587 self->client.tx_busy = FALSE;
588 dev_kfree_skb(skb);
589 return -1;
590 }
Joe Perches955a9d202014-11-11 14:44:57 -0800591 pr_debug("%s(), sending ...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 return irttp_data_request(self->client.tsap_ctrl, skb);
594}
595
596/*
597 * Function irlan_ctrl_data_request (self, skb)
598 *
599 * This function makes sure that commands on the control channel is being
600 * sent in a command/response fashion
601 */
602static void irlan_ctrl_data_request(struct irlan_cb *self, struct sk_buff *skb)
603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 IRDA_ASSERT(self != NULL, return;);
623 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
624
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700625 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER,
626 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!skb)
628 return;
629
630 /* Reserve space for TTP, LMP, and LAP header */
631 skb_reserve(skb, self->client.max_header_size);
632 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900635
636 frame[0] = CMD_GET_PROVIDER_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 frame[1] = 0x00; /* Zero parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 irlan_ctrl_data_request(self, skb);
640}
641
642/*
643 * Function irlan_open_data_channel (self)
644 *
645 * Send an Open Data Command to provider
646 *
647 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900648void irlan_open_data_channel(struct irlan_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 struct sk_buff *skb;
651 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 IRDA_ASSERT(self != NULL, return;);
654 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900655
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700656 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
657 IRLAN_STRING_PARAMETER_LEN("MEDIA", "802.3") +
658 IRLAN_STRING_PARAMETER_LEN("ACCESS_TYPE", "DIRECT"),
659 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!skb)
661 return;
662
663 skb_reserve(skb, self->client.max_header_size);
664 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* Build frame */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900669 frame[0] = CMD_OPEN_DATA_CHANNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 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
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900681void irlan_close_data_channel(struct irlan_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct sk_buff *skb;
684 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 IRDA_ASSERT(self != NULL, return;);
687 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
688
689 /* Check if the TSAP is still there */
690 if (self->client.tsap_ctrl == NULL)
691 return;
692
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700693 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
694 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN"),
695 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!skb)
697 return;
698
699 skb_reserve(skb, self->client.max_header_size);
700 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* Build frame */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900705 frame[0] = CMD_CLOSE_DATA_CHAN;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700706 frame[1] = 0x01; /* One parameter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
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 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900716 * Make IrLAN provider accept ethernet frames addressed to the unicast
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * address.
718 *
719 */
720static void irlan_open_unicast_addr(struct irlan_cb *self)
721{
722 struct sk_buff *skb;
723 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 IRDA_ASSERT(self != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900726 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
727
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700728 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
729 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
730 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
731 IRLAN_STRING_PARAMETER_LEN("FILTER_MODE", "FILTER"),
732 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!skb)
734 return;
735
736 /* Reserve space for TTP, LMP, and LAP header */
737 skb_reserve(skb, self->max_header_size);
738 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900741
742 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900744 irlan_insert_byte_param(skb, "DATA_CHAN" , self->dtsap_sel_data);
745 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
746 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER");
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 irlan_ctrl_data_request(self, skb);
749}
750
751/*
752 * Function irlan_set_broadcast_filter (self, status)
753 *
754 * Make IrLAN provider accept ethernet frames addressed to the broadcast
755 * address. Be careful with the use of this one, since there may be a lot
756 * of broadcast traffic out there. We can still function without this
757 * one but then _we_ have to initiate all communication with other
758 * hosts, since ARP request for this host will not be answered.
759 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900760void irlan_set_broadcast_filter(struct irlan_cb *self, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct sk_buff *skb;
763 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 IRDA_ASSERT(self != NULL, return;);
766 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900767
768 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700769 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
770 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "BROADCAST") +
771 /* We may waste one byte here...*/
772 IRLAN_STRING_PARAMETER_LEN("FILTER_MODE", "FILTER"),
773 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!skb)
775 return;
776
777 /* Reserve space for TTP, LMP, and LAP header */
778 skb_reserve(skb, self->client.max_header_size);
779 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900782
783 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900785 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
786 irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (status)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900788 irlan_insert_string_param(skb, "FILTER_MODE", "FILTER");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 else
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900790 irlan_insert_string_param(skb, "FILTER_MODE", "NONE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 irlan_ctrl_data_request(self, skb);
793}
794
795/*
796 * Function irlan_set_multicast_filter (self, status)
797 *
798 * Make IrLAN provider accept ethernet frames addressed to the multicast
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900799 * address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
801 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900802void irlan_set_multicast_filter(struct irlan_cb *self, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 struct sk_buff *skb;
805 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 IRDA_ASSERT(self != NULL, return;);
808 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
809
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700810 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
811 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
812 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "MULTICAST") +
813 /* We may waste one byte here...*/
814 IRLAN_STRING_PARAMETER_LEN("FILTER_MODE", "NONE"),
815 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!skb)
817 return;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* Reserve space for TTP, LMP, and LAP header */
820 skb_reserve(skb, self->client.max_header_size);
821 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900824
825 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900827 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
828 irlan_insert_string_param(skb, "FILTER_TYPE", "MULTICAST");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (status)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900830 irlan_insert_string_param(skb, "FILTER_MODE", "ALL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 else
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900832 irlan_insert_string_param(skb, "FILTER_MODE", "NONE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 irlan_ctrl_data_request(self, skb);
835}
836
837/*
838 * Function irlan_get_unicast_addr (self)
839 *
840 * Retrieves the unicast address from the IrLAN provider. This address
841 * will be inserted into the devices structure, so the ethernet layer
842 * can construct its packets.
843 *
844 */
845static void irlan_get_unicast_addr(struct irlan_cb *self)
846{
847 struct sk_buff *skb;
848 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 IRDA_ASSERT(self != NULL, return;);
851 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900852
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700853 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
854 IRLAN_BYTE_PARAMETER_LEN("DATA_CHAN") +
855 IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
856 IRLAN_STRING_PARAMETER_LEN("FILTER_OPERATION",
857 "DYNAMIC"),
858 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!skb)
860 return;
861
862 /* Reserve space for TTP, LMP, and LAP header */
863 skb_reserve(skb, self->client.max_header_size);
864 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900867
868 frame[0] = CMD_FILTER_OPERATION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 frame[1] = 0x03; /* Three parameters */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900870 irlan_insert_byte_param(skb, "DATA_CHAN", self->dtsap_sel_data);
871 irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
872 irlan_insert_string_param(skb, "FILTER_OPERATION", "DYNAMIC");
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 irlan_ctrl_data_request(self, skb);
875}
876
877/*
878 * Function irlan_get_media_char (self)
879 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 *
882 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900883void irlan_get_media_char(struct irlan_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct sk_buff *skb;
886 __u8 *frame;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 IRDA_ASSERT(self != NULL, return;);
889 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900890
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700891 skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
892 IRLAN_STRING_PARAMETER_LEN("MEDIA", "802.3"),
893 GFP_ATOMIC);
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!skb)
896 return;
897
898 /* Reserve space for TTP, LMP, and LAP header */
899 skb_reserve(skb, self->client.max_header_size);
900 skb_put(skb, 2);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 frame = skb->data;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Build frame */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900905 frame[0] = CMD_GET_MEDIA_CHAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 frame[1] = 0x01; /* One parameter */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 irlan_insert_string_param(skb, "MEDIA", "802.3");
909 irlan_ctrl_data_request(self, skb);
910}
911
912/*
913 * Function insert_byte_param (skb, param, value)
914 *
915 * Insert byte parameter into frame
916 *
917 */
918int irlan_insert_byte_param(struct sk_buff *skb, char *param, __u8 value)
919{
920 return __irlan_insert_param(skb, param, IRLAN_BYTE, value, 0, NULL, 0);
921}
922
923int irlan_insert_short_param(struct sk_buff *skb, char *param, __u16 value)
924{
925 return __irlan_insert_param(skb, param, IRLAN_SHORT, 0, value, NULL, 0);
926}
927
928/*
929 * Function insert_string (skb, param, value)
930 *
931 * Insert string parameter into frame
932 *
933 */
934int irlan_insert_string_param(struct sk_buff *skb, char *param, char *string)
935{
936 int string_len = strlen(string);
937
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900938 return __irlan_insert_param(skb, param, IRLAN_ARRAY, 0, 0, string,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 string_len);
940}
941
942/*
943 * Function insert_array_param(skb, param, value, len_value)
944 *
945 * Insert array parameter into frame
946 *
947 */
948int irlan_insert_array_param(struct sk_buff *skb, char *name, __u8 *array,
949 __u16 array_len)
950{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900951 return __irlan_insert_param(skb, name, IRLAN_ARRAY, 0, 0, array,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 array_len);
953}
954
955/*
956 * Function insert_param (skb, param, value, byte)
957 *
958 * Insert parameter at end of buffer, structure of a parameter is:
959 *
960 * -----------------------------------------------------------------------
961 * | Name Length[1] | Param Name[1..255] | Val Length[2] | Value[0..1016]|
962 * -----------------------------------------------------------------------
963 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900964static int __irlan_insert_param(struct sk_buff *skb, char *param, int type,
965 __u8 value_byte, __u16 value_short,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 __u8 *value_array, __u16 value_len)
967{
968 __u8 *frame;
969 __u8 param_len;
Al Viro448c31a2006-11-14 20:47:46 -0800970 __le16 tmp_le; /* Temporary value in little endian format */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int n=0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (skb == NULL) {
Joe Perches955a9d202014-11-11 14:44:57 -0800974 pr_debug("%s(), Got NULL skb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 param_len = strlen(param);
979 switch (type) {
980 case IRLAN_BYTE:
981 value_len = 1;
982 break;
983 case IRLAN_SHORT:
984 value_len = 2;
985 break;
986 case IRLAN_ARRAY:
987 IRDA_ASSERT(value_array != NULL, return 0;);
988 IRDA_ASSERT(value_len > 0, return 0;);
989 break;
990 default:
Joe Perches955a9d202014-11-11 14:44:57 -0800991 pr_debug("%s(), Unknown parameter type!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* Insert at end of sk-buffer */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700996 frame = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /* Make space for data */
999 if (skb_tailroom(skb) < (param_len+value_len+3)) {
Joe Perches955a9d202014-11-11 14:44:57 -08001000 pr_debug("%s(), No more space at end of skb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 skb_put(skb, param_len+value_len+3);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* Insert parameter length */
1006 frame[n++] = param_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Insert parameter */
1009 memcpy(frame+n, param, param_len); n += param_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Insert value length (2 byte little endian format, LSB first) */
1012 tmp_le = cpu_to_le16(value_len);
1013 memcpy(frame+n, &tmp_le, 2); n += 2; /* To avoid alignment problems */
1014
1015 /* Insert value */
1016 switch (type) {
1017 case IRLAN_BYTE:
1018 frame[n++] = value_byte;
1019 break;
1020 case IRLAN_SHORT:
1021 tmp_le = cpu_to_le16(value_short);
1022 memcpy(frame+n, &tmp_le, 2); n += 2;
1023 break;
1024 case IRLAN_ARRAY:
1025 memcpy(frame+n, value_array, value_len); n+=value_len;
1026 break;
1027 default:
1028 break;
1029 }
1030 IRDA_ASSERT(n == (param_len+value_len+3), return 0;);
1031
1032 return param_len+value_len+3;
1033}
1034
1035/*
1036 * Function irlan_extract_param (buf, name, value, len)
1037 *
1038 * Extracts a single parameter name/value pair from buffer and updates
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001039 * the buffer pointer to point to the next name/value pair.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
1041int irlan_extract_param(__u8 *buf, char *name, char *value, __u16 *len)
1042{
1043 __u8 name_len;
1044 __u16 val_len;
1045 int n=0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /* get length of parameter name (1 byte) */
1048 name_len = buf[n++];
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (name_len > 254) {
Joe Perches955a9d202014-11-11 14:44:57 -08001051 pr_debug("%s(), name_len > 254\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return -RSP_INVALID_COMMAND_FORMAT;
1053 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* get parameter name */
1056 memcpy(name, buf+n, name_len);
1057 name[name_len] = '\0';
1058 n+=name_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001059
1060 /*
1061 * Get length of parameter value (2 bytes in little endian
1062 * format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
1064 memcpy(&val_len, buf+n, 2); /* To avoid alignment problems */
1065 le16_to_cpus(&val_len); n+=2;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001066
Dan Carpentercf9b94f2010-09-04 03:14:35 +00001067 if (val_len >= 1016) {
Joe Perches955a9d202014-11-11 14:44:57 -08001068 pr_debug("%s(), parameter length to long\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -RSP_INVALID_COMMAND_FORMAT;
1070 }
1071 *len = val_len;
1072
1073 /* get parameter value */
1074 memcpy(value, buf+n, val_len);
1075 value[val_len] = '\0';
1076 n+=val_len;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001077
Joe Perches955a9d202014-11-11 14:44:57 -08001078 pr_debug("Parameter: %s ", name);
1079 pr_debug("Value: %s\n", value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 return n;
1082}
1083
1084#ifdef CONFIG_PROC_FS
1085
1086/*
1087 * Start of reading /proc entries.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001088 * Return entry at pos,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * or start_token to indicate print header line
1090 * or NULL if end of file
1091 */
1092static void *irlan_seq_start(struct seq_file *seq, loff_t *pos)
1093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 rcu_read_lock();
Li Zefan216437e2010-02-08 23:21:05 +00001095 return seq_list_start_head(&irlans, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098/* Return entry after v, and increment pos */
1099static void *irlan_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1100{
Li Zefan216437e2010-02-08 23:21:05 +00001101 return seq_list_next(v, &irlans, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/* End of reading /proc file */
1105static void irlan_seq_stop(struct seq_file *seq, void *v)
1106{
1107 rcu_read_unlock();
1108}
1109
1110
1111/*
1112 * Show one entry in /proc file.
1113 */
1114static int irlan_seq_show(struct seq_file *seq, void *v)
1115{
Li Zefan216437e2010-02-08 23:21:05 +00001116 if (v == &irlans)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 seq_puts(seq, "IrLAN instances:\n");
1118 else {
Li Zefan216437e2010-02-08 23:21:05 +00001119 struct irlan_cb *self = list_entry(v, struct irlan_cb, dev_list);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 IRDA_ASSERT(self != NULL, return -1;);
1122 IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
1123
1124 seq_printf(seq,"ifname: %s,\n",
1125 self->dev->name);
1126 seq_printf(seq,"client state: %s, ",
1127 irlan_state[ self->client.state]);
1128 seq_printf(seq,"provider state: %s,\n",
1129 irlan_state[ self->provider.state]);
1130 seq_printf(seq,"saddr: %#08x, ",
1131 self->saddr);
1132 seq_printf(seq,"daddr: %#08x\n",
1133 self->daddr);
1134 seq_printf(seq,"version: %d.%d,\n",
1135 self->version[1], self->version[0]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001136 seq_printf(seq,"access type: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 irlan_access[self->client.access_type]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001138 seq_printf(seq,"media: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 irlan_media[self->media]);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 seq_printf(seq,"local filter:\n");
1142 seq_printf(seq,"remote filter: ");
1143 irlan_print_filter(seq, self->client.filter_type);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001144 seq_printf(seq,"tx busy: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 netif_queue_stopped(self->dev) ? "TRUE" : "FALSE");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 seq_putc(seq,'\n');
1148 }
1149 return 0;
1150}
1151
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001152static const struct seq_operations irlan_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 .start = irlan_seq_start,
1154 .next = irlan_seq_next,
1155 .stop = irlan_seq_stop,
1156 .show = irlan_seq_show,
1157};
1158
1159static int irlan_seq_open(struct inode *inode, struct file *file)
1160{
1161 return seq_open(file, &irlan_seq_ops);
1162}
1163#endif
1164
1165MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001166MODULE_DESCRIPTION("The Linux IrDA LAN protocol");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167MODULE_LICENSE("GPL");
1168
1169module_param(eth, bool, 0);
1170MODULE_PARM_DESC(eth, "Name devices ethX (0) or irlanX (1)");
1171module_param(access, int, 0);
1172MODULE_PARM_DESC(access, "Access type DIRECT=1, PEER=2, HOSTED=3");
1173
1174module_init(irlan_init);
1175module_exit(irlan_cleanup);
1176