• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*********************************************************************
2  *
3  * Filename:      ircomm_core.c
4  * Version:       1.0
5  * Description:   IrCOMM service interface
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sun Jun  6 20:37:34 1999
9  * Modified at:   Tue Dec 21 13:26:41 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  *
12  *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
13  *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public License
26  *     along with this program; if not, see <http://www.gnu.org/licenses/>.
27  *
28  ********************************************************************/
29 
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 
36 #include <net/irda/irda.h>
37 #include <net/irda/irmod.h>
38 #include <net/irda/irlmp.h>
39 #include <net/irda/iriap.h>
40 #include <net/irda/irttp.h>
41 #include <net/irda/irias_object.h>
42 
43 #include <net/irda/ircomm_event.h>
44 #include <net/irda/ircomm_lmp.h>
45 #include <net/irda/ircomm_ttp.h>
46 #include <net/irda/ircomm_param.h>
47 #include <net/irda/ircomm_core.h>
48 
49 static int __ircomm_close(struct ircomm_cb *self);
50 static void ircomm_control_indication(struct ircomm_cb *self,
51 				      struct sk_buff *skb, int clen);
52 
53 #ifdef CONFIG_PROC_FS
54 extern struct proc_dir_entry *proc_irda;
55 static int ircomm_seq_open(struct inode *, struct file *);
56 
57 static const struct file_operations ircomm_proc_fops = {
58 	.owner		= THIS_MODULE,
59 	.open           = ircomm_seq_open,
60 	.read           = seq_read,
61 	.llseek         = seq_lseek,
62 	.release	= seq_release,
63 };
64 #endif /* CONFIG_PROC_FS */
65 
66 hashbin_t *ircomm = NULL;
67 
ircomm_init(void)68 static int __init ircomm_init(void)
69 {
70 	ircomm = hashbin_new(HB_LOCK);
71 	if (ircomm == NULL) {
72 		net_err_ratelimited("%s(), can't allocate hashbin!\n",
73 				    __func__);
74 		return -ENOMEM;
75 	}
76 
77 #ifdef CONFIG_PROC_FS
78 	{ struct proc_dir_entry *ent;
79 	ent = proc_create("ircomm", 0, proc_irda, &ircomm_proc_fops);
80 	if (!ent) {
81 		printk(KERN_ERR "ircomm_init: can't create /proc entry!\n");
82 		return -ENODEV;
83 	}
84 	}
85 #endif /* CONFIG_PROC_FS */
86 
87 	net_info_ratelimited("IrCOMM protocol (Dag Brattli)\n");
88 
89 	return 0;
90 }
91 
ircomm_cleanup(void)92 static void __exit ircomm_cleanup(void)
93 {
94 	hashbin_delete(ircomm, (FREE_FUNC) __ircomm_close);
95 
96 #ifdef CONFIG_PROC_FS
97 	remove_proc_entry("ircomm", proc_irda);
98 #endif /* CONFIG_PROC_FS */
99 }
100 
101 /*
102  * Function ircomm_open (client_notify)
103  *
104  *    Start a new IrCOMM instance
105  *
106  */
ircomm_open(notify_t * notify,__u8 service_type,int line)107 struct ircomm_cb *ircomm_open(notify_t *notify, __u8 service_type, int line)
108 {
109 	struct ircomm_cb *self = NULL;
110 	int ret;
111 
112 	pr_debug("%s(), service_type=0x%02x\n", __func__ ,
113 		 service_type);
114 
115 	IRDA_ASSERT(ircomm != NULL, return NULL;);
116 
117 	self = kzalloc(sizeof(struct ircomm_cb), GFP_KERNEL);
118 	if (self == NULL)
119 		return NULL;
120 
121 	self->notify = *notify;
122 	self->magic = IRCOMM_MAGIC;
123 
124 	/* Check if we should use IrLMP or IrTTP */
125 	if (service_type & IRCOMM_3_WIRE_RAW) {
126 		self->flow_status = FLOW_START;
127 		ret = ircomm_open_lsap(self);
128 	} else
129 		ret = ircomm_open_tsap(self);
130 
131 	if (ret < 0) {
132 		kfree(self);
133 		return NULL;
134 	}
135 
136 	self->service_type = service_type;
137 	self->line = line;
138 
139 	hashbin_insert(ircomm, (irda_queue_t *) self, line, NULL);
140 
141 	ircomm_next_state(self, IRCOMM_IDLE);
142 
143 	return self;
144 }
145 
146 EXPORT_SYMBOL(ircomm_open);
147 
148 /*
149  * Function ircomm_close_instance (self)
150  *
151  *    Remove IrCOMM instance
152  *
153  */
__ircomm_close(struct ircomm_cb * self)154 static int __ircomm_close(struct ircomm_cb *self)
155 {
156 	/* Disconnect link if any */
157 	ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, NULL, NULL);
158 
159 	/* Remove TSAP */
160 	if (self->tsap) {
161 		irttp_close_tsap(self->tsap);
162 		self->tsap = NULL;
163 	}
164 
165 	/* Remove LSAP */
166 	if (self->lsap) {
167 		irlmp_close_lsap(self->lsap);
168 		self->lsap = NULL;
169 	}
170 	self->magic = 0;
171 
172 	kfree(self);
173 
174 	return 0;
175 }
176 
177 /*
178  * Function ircomm_close (self)
179  *
180  *    Closes and removes the specified IrCOMM instance
181  *
182  */
ircomm_close(struct ircomm_cb * self)183 int ircomm_close(struct ircomm_cb *self)
184 {
185 	struct ircomm_cb *entry;
186 
187 	IRDA_ASSERT(self != NULL, return -EIO;);
188 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EIO;);
189 
190 	entry = hashbin_remove(ircomm, self->line, NULL);
191 
192 	IRDA_ASSERT(entry == self, return -1;);
193 
194 	return __ircomm_close(self);
195 }
196 
197 EXPORT_SYMBOL(ircomm_close);
198 
199 /*
200  * Function ircomm_connect_request (self, service_type)
201  *
202  *    Impl. of this function is differ from one of the reference. This
203  *    function does discovery as well as sending connect request
204  *
205  */
ircomm_connect_request(struct ircomm_cb * self,__u8 dlsap_sel,__u32 saddr,__u32 daddr,struct sk_buff * skb,__u8 service_type)206 int ircomm_connect_request(struct ircomm_cb *self, __u8 dlsap_sel,
207 			   __u32 saddr, __u32 daddr, struct sk_buff *skb,
208 			   __u8 service_type)
209 {
210 	struct ircomm_info info;
211 	int ret;
212 
213 	IRDA_ASSERT(self != NULL, return -1;);
214 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
215 
216 	self->service_type= service_type;
217 
218 	info.dlsap_sel = dlsap_sel;
219 	info.saddr = saddr;
220 	info.daddr = daddr;
221 
222 	ret = ircomm_do_event(self, IRCOMM_CONNECT_REQUEST, skb, &info);
223 
224 	return ret;
225 }
226 
227 EXPORT_SYMBOL(ircomm_connect_request);
228 
229 /*
230  * Function ircomm_connect_indication (self, qos, skb)
231  *
232  *    Notify user layer about the incoming connection
233  *
234  */
ircomm_connect_indication(struct ircomm_cb * self,struct sk_buff * skb,struct ircomm_info * info)235 void ircomm_connect_indication(struct ircomm_cb *self, struct sk_buff *skb,
236 			       struct ircomm_info *info)
237 {
238 	/*
239 	 * If there are any data hiding in the control channel, we must
240 	 * deliver it first. The side effect is that the control channel
241 	 * will be removed from the skb
242 	 */
243 	if (self->notify.connect_indication)
244 		self->notify.connect_indication(self->notify.instance, self,
245 						info->qos, info->max_data_size,
246 						info->max_header_size, skb);
247 	else {
248 		pr_debug("%s(), missing handler\n", __func__);
249 	}
250 }
251 
252 /*
253  * Function ircomm_connect_response (self, userdata, max_sdu_size)
254  *
255  *    User accepts connection
256  *
257  */
ircomm_connect_response(struct ircomm_cb * self,struct sk_buff * userdata)258 int ircomm_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
259 {
260 	int ret;
261 
262 	IRDA_ASSERT(self != NULL, return -1;);
263 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
264 
265 	ret = ircomm_do_event(self, IRCOMM_CONNECT_RESPONSE, userdata, NULL);
266 
267 	return ret;
268 }
269 
270 EXPORT_SYMBOL(ircomm_connect_response);
271 
272 /*
273  * Function connect_confirm (self, skb)
274  *
275  *    Notify user layer that the link is now connected
276  *
277  */
ircomm_connect_confirm(struct ircomm_cb * self,struct sk_buff * skb,struct ircomm_info * info)278 void ircomm_connect_confirm(struct ircomm_cb *self, struct sk_buff *skb,
279 			    struct ircomm_info *info)
280 {
281 	if (self->notify.connect_confirm )
282 		self->notify.connect_confirm(self->notify.instance,
283 					     self, info->qos,
284 					     info->max_data_size,
285 					     info->max_header_size, skb);
286 	else {
287 		pr_debug("%s(), missing handler\n", __func__);
288 	}
289 }
290 
291 /*
292  * Function ircomm_data_request (self, userdata)
293  *
294  *    Send IrCOMM data to peer device
295  *
296  */
ircomm_data_request(struct ircomm_cb * self,struct sk_buff * skb)297 int ircomm_data_request(struct ircomm_cb *self, struct sk_buff *skb)
298 {
299 	int ret;
300 
301 	IRDA_ASSERT(self != NULL, return -EFAULT;);
302 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
303 	IRDA_ASSERT(skb != NULL, return -EFAULT;);
304 
305 	ret = ircomm_do_event(self, IRCOMM_DATA_REQUEST, skb, NULL);
306 
307 	return ret;
308 }
309 
310 EXPORT_SYMBOL(ircomm_data_request);
311 
312 /*
313  * Function ircomm_data_indication (self, skb)
314  *
315  *    Data arrived, so deliver it to user
316  *
317  */
ircomm_data_indication(struct ircomm_cb * self,struct sk_buff * skb)318 void ircomm_data_indication(struct ircomm_cb *self, struct sk_buff *skb)
319 {
320 	IRDA_ASSERT(skb->len > 0, return;);
321 
322 	if (self->notify.data_indication)
323 		self->notify.data_indication(self->notify.instance, self, skb);
324 	else {
325 		pr_debug("%s(), missing handler\n", __func__);
326 	}
327 }
328 
329 /*
330  * Function ircomm_process_data (self, skb)
331  *
332  *    Data arrived which may contain control channel data
333  *
334  */
ircomm_process_data(struct ircomm_cb * self,struct sk_buff * skb)335 void ircomm_process_data(struct ircomm_cb *self, struct sk_buff *skb)
336 {
337 	int clen;
338 
339 	IRDA_ASSERT(skb->len > 0, return;);
340 
341 	clen = skb->data[0];
342 
343 	/*
344 	 * Input validation check: a stir4200/mcp2150 combinations sometimes
345 	 * results in frames with clen > remaining packet size. These are
346 	 * illegal; if we throw away just this frame then it seems to carry on
347 	 * fine
348 	 */
349 	if (unlikely(skb->len < (clen + 1))) {
350 		pr_debug("%s() throwing away illegal frame\n",
351 			 __func__);
352 		return;
353 	}
354 
355 	/*
356 	 * If there are any data hiding in the control channel, we must
357 	 * deliver it first. The side effect is that the control channel
358 	 * will be removed from the skb
359 	 */
360 	if (clen > 0)
361 		ircomm_control_indication(self, skb, clen);
362 
363 	/* Remove control channel from data channel */
364 	skb_pull(skb, clen+1);
365 
366 	if (skb->len)
367 		ircomm_data_indication(self, skb);
368 	else {
369 		pr_debug("%s(), data was control info only!\n",
370 			 __func__);
371 	}
372 }
373 
374 /*
375  * Function ircomm_control_request (self, params)
376  *
377  *    Send control data to peer device
378  *
379  */
ircomm_control_request(struct ircomm_cb * self,struct sk_buff * skb)380 int ircomm_control_request(struct ircomm_cb *self, struct sk_buff *skb)
381 {
382 	int ret;
383 
384 	IRDA_ASSERT(self != NULL, return -EFAULT;);
385 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
386 	IRDA_ASSERT(skb != NULL, return -EFAULT;);
387 
388 	ret = ircomm_do_event(self, IRCOMM_CONTROL_REQUEST, skb, NULL);
389 
390 	return ret;
391 }
392 
393 EXPORT_SYMBOL(ircomm_control_request);
394 
395 /*
396  * Function ircomm_control_indication (self, skb)
397  *
398  *    Data has arrived on the control channel
399  *
400  */
ircomm_control_indication(struct ircomm_cb * self,struct sk_buff * skb,int clen)401 static void ircomm_control_indication(struct ircomm_cb *self,
402 				      struct sk_buff *skb, int clen)
403 {
404 	/* Use udata for delivering data on the control channel */
405 	if (self->notify.udata_indication) {
406 		struct sk_buff *ctrl_skb;
407 
408 		/* We don't own the skb, so clone it */
409 		ctrl_skb = skb_clone(skb, GFP_ATOMIC);
410 		if (!ctrl_skb)
411 			return;
412 
413 		/* Remove data channel from control channel */
414 		skb_trim(ctrl_skb, clen+1);
415 
416 		self->notify.udata_indication(self->notify.instance, self,
417 					      ctrl_skb);
418 
419 		/* Drop reference count -
420 		 * see ircomm_tty_control_indication(). */
421 		dev_kfree_skb(ctrl_skb);
422 	} else {
423 		pr_debug("%s(), missing handler\n", __func__);
424 	}
425 }
426 
427 /*
428  * Function ircomm_disconnect_request (self, userdata, priority)
429  *
430  *    User layer wants to disconnect the IrCOMM connection
431  *
432  */
ircomm_disconnect_request(struct ircomm_cb * self,struct sk_buff * userdata)433 int ircomm_disconnect_request(struct ircomm_cb *self, struct sk_buff *userdata)
434 {
435 	struct ircomm_info info;
436 	int ret;
437 
438 	IRDA_ASSERT(self != NULL, return -1;);
439 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
440 
441 	ret = ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, userdata,
442 			      &info);
443 	return ret;
444 }
445 
446 EXPORT_SYMBOL(ircomm_disconnect_request);
447 
448 /*
449  * Function disconnect_indication (self, skb)
450  *
451  *    Tell user that the link has been disconnected
452  *
453  */
ircomm_disconnect_indication(struct ircomm_cb * self,struct sk_buff * skb,struct ircomm_info * info)454 void ircomm_disconnect_indication(struct ircomm_cb *self, struct sk_buff *skb,
455 				  struct ircomm_info *info)
456 {
457 	IRDA_ASSERT(info != NULL, return;);
458 
459 	if (self->notify.disconnect_indication) {
460 		self->notify.disconnect_indication(self->notify.instance, self,
461 						   info->reason, skb);
462 	} else {
463 		pr_debug("%s(), missing handler\n", __func__);
464 	}
465 }
466 
467 /*
468  * Function ircomm_flow_request (self, flow)
469  *
470  *
471  *
472  */
ircomm_flow_request(struct ircomm_cb * self,LOCAL_FLOW flow)473 void ircomm_flow_request(struct ircomm_cb *self, LOCAL_FLOW flow)
474 {
475 	IRDA_ASSERT(self != NULL, return;);
476 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
477 
478 	if (self->service_type == IRCOMM_3_WIRE_RAW)
479 		return;
480 
481 	irttp_flow_request(self->tsap, flow);
482 }
483 
484 EXPORT_SYMBOL(ircomm_flow_request);
485 
486 #ifdef CONFIG_PROC_FS
ircomm_seq_start(struct seq_file * seq,loff_t * pos)487 static void *ircomm_seq_start(struct seq_file *seq, loff_t *pos)
488 {
489 	struct ircomm_cb *self;
490 	loff_t off = 0;
491 
492 	spin_lock_irq(&ircomm->hb_spinlock);
493 
494 	for (self = (struct ircomm_cb *) hashbin_get_first(ircomm);
495 	     self != NULL;
496 	     self = (struct ircomm_cb *) hashbin_get_next(ircomm)) {
497 		if (off++ == *pos)
498 			break;
499 
500 	}
501 	return self;
502 }
503 
ircomm_seq_next(struct seq_file * seq,void * v,loff_t * pos)504 static void *ircomm_seq_next(struct seq_file *seq, void *v, loff_t *pos)
505 {
506 	++*pos;
507 
508 	return (void *) hashbin_get_next(ircomm);
509 }
510 
ircomm_seq_stop(struct seq_file * seq,void * v)511 static void ircomm_seq_stop(struct seq_file *seq, void *v)
512 {
513 	spin_unlock_irq(&ircomm->hb_spinlock);
514 }
515 
ircomm_seq_show(struct seq_file * seq,void * v)516 static int ircomm_seq_show(struct seq_file *seq, void *v)
517 {
518 	const struct ircomm_cb *self = v;
519 
520 	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EINVAL; );
521 
522 	if(self->line < 0x10)
523 		seq_printf(seq, "ircomm%d", self->line);
524 	else
525 		seq_printf(seq, "irlpt%d", self->line - 0x10);
526 
527 	seq_printf(seq,
528 		   " state: %s, slsap_sel: %#02x, dlsap_sel: %#02x, mode:",
529 		   ircomm_state[ self->state],
530 		   self->slsap_sel, self->dlsap_sel);
531 
532 	if(self->service_type & IRCOMM_3_WIRE_RAW)
533 		seq_printf(seq, " 3-wire-raw");
534 	if(self->service_type & IRCOMM_3_WIRE)
535 		seq_printf(seq, " 3-wire");
536 	if(self->service_type & IRCOMM_9_WIRE)
537 		seq_printf(seq, " 9-wire");
538 	if(self->service_type & IRCOMM_CENTRONICS)
539 		seq_printf(seq, " Centronics");
540 	seq_putc(seq, '\n');
541 
542 	return 0;
543 }
544 
545 static const struct seq_operations ircomm_seq_ops = {
546 	.start  = ircomm_seq_start,
547 	.next   = ircomm_seq_next,
548 	.stop   = ircomm_seq_stop,
549 	.show   = ircomm_seq_show,
550 };
551 
ircomm_seq_open(struct inode * inode,struct file * file)552 static int ircomm_seq_open(struct inode *inode, struct file *file)
553 {
554 	return seq_open(file, &ircomm_seq_ops);
555 }
556 #endif /* CONFIG_PROC_FS */
557 
558 MODULE_AUTHOR("Dag Brattli <dag@brattli.net>");
559 MODULE_DESCRIPTION("IrCOMM protocol");
560 MODULE_LICENSE("GPL");
561 
562 module_init(ircomm_init);
563 module_exit(ircomm_cleanup);
564