1 /*********************************************************************
3 * Filename: ircomm_core.c
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>
12 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
13 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * 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.
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/>.
28 ********************************************************************/
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>
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>
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>
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);
54 extern struct proc_dir_entry *proc_irda;
55 static int ircomm_seq_open(struct inode *, struct file *);
57 static const struct file_operations ircomm_proc_fops = {
59 .open = ircomm_seq_open,
62 .release = seq_release,
64 #endif /* CONFIG_PROC_FS */
66 hashbin_t *ircomm = NULL;
68 static int __init ircomm_init(void)
70 ircomm = hashbin_new(HB_LOCK);
72 net_err_ratelimited("%s(), can't allocate hashbin!\n",
78 { struct proc_dir_entry *ent;
79 ent = proc_create("ircomm", 0, proc_irda, &ircomm_proc_fops);
81 printk(KERN_ERR "ircomm_init: can't create /proc entry!\n");
85 #endif /* CONFIG_PROC_FS */
87 net_info_ratelimited("IrCOMM protocol (Dag Brattli)\n");
92 static void __exit ircomm_cleanup(void)
94 IRDA_DEBUG(2, "%s()\n", __func__ );
96 hashbin_delete(ircomm, (FREE_FUNC) __ircomm_close);
99 remove_proc_entry("ircomm", proc_irda);
100 #endif /* CONFIG_PROC_FS */
104 * Function ircomm_open (client_notify)
106 * Start a new IrCOMM instance
109 struct ircomm_cb *ircomm_open(notify_t *notify, __u8 service_type, int line)
111 struct ircomm_cb *self = NULL;
114 IRDA_DEBUG(2, "%s(), service_type=0x%02x\n", __func__ ,
117 IRDA_ASSERT(ircomm != NULL, return NULL;);
119 self = kzalloc(sizeof(struct ircomm_cb), GFP_KERNEL);
123 self->notify = *notify;
124 self->magic = IRCOMM_MAGIC;
126 /* Check if we should use IrLMP or IrTTP */
127 if (service_type & IRCOMM_3_WIRE_RAW) {
128 self->flow_status = FLOW_START;
129 ret = ircomm_open_lsap(self);
131 ret = ircomm_open_tsap(self);
138 self->service_type = service_type;
141 hashbin_insert(ircomm, (irda_queue_t *) self, line, NULL);
143 ircomm_next_state(self, IRCOMM_IDLE);
148 EXPORT_SYMBOL(ircomm_open);
151 * Function ircomm_close_instance (self)
153 * Remove IrCOMM instance
156 static int __ircomm_close(struct ircomm_cb *self)
158 IRDA_DEBUG(2, "%s()\n", __func__ );
160 /* Disconnect link if any */
161 ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, NULL, NULL);
165 irttp_close_tsap(self->tsap);
171 irlmp_close_lsap(self->lsap);
182 * Function ircomm_close (self)
184 * Closes and removes the specified IrCOMM instance
187 int ircomm_close(struct ircomm_cb *self)
189 struct ircomm_cb *entry;
191 IRDA_ASSERT(self != NULL, return -EIO;);
192 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EIO;);
194 IRDA_DEBUG(0, "%s()\n", __func__ );
196 entry = hashbin_remove(ircomm, self->line, NULL);
198 IRDA_ASSERT(entry == self, return -1;);
200 return __ircomm_close(self);
203 EXPORT_SYMBOL(ircomm_close);
206 * Function ircomm_connect_request (self, service_type)
208 * Impl. of this function is differ from one of the reference. This
209 * function does discovery as well as sending connect request
212 int ircomm_connect_request(struct ircomm_cb *self, __u8 dlsap_sel,
213 __u32 saddr, __u32 daddr, struct sk_buff *skb,
216 struct ircomm_info info;
219 IRDA_DEBUG(2 , "%s()\n", __func__ );
221 IRDA_ASSERT(self != NULL, return -1;);
222 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
224 self->service_type= service_type;
226 info.dlsap_sel = dlsap_sel;
230 ret = ircomm_do_event(self, IRCOMM_CONNECT_REQUEST, skb, &info);
235 EXPORT_SYMBOL(ircomm_connect_request);
238 * Function ircomm_connect_indication (self, qos, skb)
240 * Notify user layer about the incoming connection
243 void ircomm_connect_indication(struct ircomm_cb *self, struct sk_buff *skb,
244 struct ircomm_info *info)
246 IRDA_DEBUG(2, "%s()\n", __func__ );
249 * If there are any data hiding in the control channel, we must
250 * deliver it first. The side effect is that the control channel
251 * will be removed from the skb
253 if (self->notify.connect_indication)
254 self->notify.connect_indication(self->notify.instance, self,
255 info->qos, info->max_data_size,
256 info->max_header_size, skb);
258 IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
263 * Function ircomm_connect_response (self, userdata, max_sdu_size)
265 * User accepts connection
268 int ircomm_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
272 IRDA_ASSERT(self != NULL, return -1;);
273 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
275 IRDA_DEBUG(4, "%s()\n", __func__ );
277 ret = ircomm_do_event(self, IRCOMM_CONNECT_RESPONSE, userdata, NULL);
282 EXPORT_SYMBOL(ircomm_connect_response);
285 * Function connect_confirm (self, skb)
287 * Notify user layer that the link is now connected
290 void ircomm_connect_confirm(struct ircomm_cb *self, struct sk_buff *skb,
291 struct ircomm_info *info)
293 IRDA_DEBUG(4, "%s()\n", __func__ );
295 if (self->notify.connect_confirm )
296 self->notify.connect_confirm(self->notify.instance,
299 info->max_header_size, skb);
301 IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
306 * Function ircomm_data_request (self, userdata)
308 * Send IrCOMM data to peer device
311 int ircomm_data_request(struct ircomm_cb *self, struct sk_buff *skb)
315 IRDA_DEBUG(4, "%s()\n", __func__ );
317 IRDA_ASSERT(self != NULL, return -EFAULT;);
318 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
319 IRDA_ASSERT(skb != NULL, return -EFAULT;);
321 ret = ircomm_do_event(self, IRCOMM_DATA_REQUEST, skb, NULL);
326 EXPORT_SYMBOL(ircomm_data_request);
329 * Function ircomm_data_indication (self, skb)
331 * Data arrived, so deliver it to user
334 void ircomm_data_indication(struct ircomm_cb *self, struct sk_buff *skb)
336 IRDA_DEBUG(4, "%s()\n", __func__ );
338 IRDA_ASSERT(skb->len > 0, return;);
340 if (self->notify.data_indication)
341 self->notify.data_indication(self->notify.instance, self, skb);
343 IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
348 * Function ircomm_process_data (self, skb)
350 * Data arrived which may contain control channel data
353 void ircomm_process_data(struct ircomm_cb *self, struct sk_buff *skb)
357 IRDA_ASSERT(skb->len > 0, return;);
362 * Input validation check: a stir4200/mcp2150 combinations sometimes
363 * results in frames with clen > remaining packet size. These are
364 * illegal; if we throw away just this frame then it seems to carry on
367 if (unlikely(skb->len < (clen + 1))) {
368 IRDA_DEBUG(2, "%s() throwing away illegal frame\n",
374 * If there are any data hiding in the control channel, we must
375 * deliver it first. The side effect is that the control channel
376 * will be removed from the skb
379 ircomm_control_indication(self, skb, clen);
381 /* Remove control channel from data channel */
382 skb_pull(skb, clen+1);
385 ircomm_data_indication(self, skb);
387 IRDA_DEBUG(4, "%s(), data was control info only!\n",
393 * Function ircomm_control_request (self, params)
395 * Send control data to peer device
398 int ircomm_control_request(struct ircomm_cb *self, struct sk_buff *skb)
402 IRDA_DEBUG(2, "%s()\n", __func__ );
404 IRDA_ASSERT(self != NULL, return -EFAULT;);
405 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
406 IRDA_ASSERT(skb != NULL, return -EFAULT;);
408 ret = ircomm_do_event(self, IRCOMM_CONTROL_REQUEST, skb, NULL);
413 EXPORT_SYMBOL(ircomm_control_request);
416 * Function ircomm_control_indication (self, skb)
418 * Data has arrived on the control channel
421 static void ircomm_control_indication(struct ircomm_cb *self,
422 struct sk_buff *skb, int clen)
424 IRDA_DEBUG(2, "%s()\n", __func__ );
426 /* Use udata for delivering data on the control channel */
427 if (self->notify.udata_indication) {
428 struct sk_buff *ctrl_skb;
430 /* We don't own the skb, so clone it */
431 ctrl_skb = skb_clone(skb, GFP_ATOMIC);
435 /* Remove data channel from control channel */
436 skb_trim(ctrl_skb, clen+1);
438 self->notify.udata_indication(self->notify.instance, self,
441 /* Drop reference count -
442 * see ircomm_tty_control_indication(). */
443 dev_kfree_skb(ctrl_skb);
445 IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
450 * Function ircomm_disconnect_request (self, userdata, priority)
452 * User layer wants to disconnect the IrCOMM connection
455 int ircomm_disconnect_request(struct ircomm_cb *self, struct sk_buff *userdata)
457 struct ircomm_info info;
460 IRDA_DEBUG(2, "%s()\n", __func__ );
462 IRDA_ASSERT(self != NULL, return -1;);
463 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
465 ret = ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, userdata,
470 EXPORT_SYMBOL(ircomm_disconnect_request);
473 * Function disconnect_indication (self, skb)
475 * Tell user that the link has been disconnected
478 void ircomm_disconnect_indication(struct ircomm_cb *self, struct sk_buff *skb,
479 struct ircomm_info *info)
481 IRDA_DEBUG(2, "%s()\n", __func__ );
483 IRDA_ASSERT(info != NULL, return;);
485 if (self->notify.disconnect_indication) {
486 self->notify.disconnect_indication(self->notify.instance, self,
489 IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
494 * Function ircomm_flow_request (self, flow)
499 void ircomm_flow_request(struct ircomm_cb *self, LOCAL_FLOW flow)
501 IRDA_DEBUG(2, "%s()\n", __func__ );
503 IRDA_ASSERT(self != NULL, return;);
504 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
506 if (self->service_type == IRCOMM_3_WIRE_RAW)
509 irttp_flow_request(self->tsap, flow);
512 EXPORT_SYMBOL(ircomm_flow_request);
514 #ifdef CONFIG_PROC_FS
515 static void *ircomm_seq_start(struct seq_file *seq, loff_t *pos)
517 struct ircomm_cb *self;
520 spin_lock_irq(&ircomm->hb_spinlock);
522 for (self = (struct ircomm_cb *) hashbin_get_first(ircomm);
524 self = (struct ircomm_cb *) hashbin_get_next(ircomm)) {
532 static void *ircomm_seq_next(struct seq_file *seq, void *v, loff_t *pos)
536 return (void *) hashbin_get_next(ircomm);
539 static void ircomm_seq_stop(struct seq_file *seq, void *v)
541 spin_unlock_irq(&ircomm->hb_spinlock);
544 static int ircomm_seq_show(struct seq_file *seq, void *v)
546 const struct ircomm_cb *self = v;
548 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EINVAL; );
550 if(self->line < 0x10)
551 seq_printf(seq, "ircomm%d", self->line);
553 seq_printf(seq, "irlpt%d", self->line - 0x10);
556 " state: %s, slsap_sel: %#02x, dlsap_sel: %#02x, mode:",
557 ircomm_state[ self->state],
558 self->slsap_sel, self->dlsap_sel);
560 if(self->service_type & IRCOMM_3_WIRE_RAW)
561 seq_printf(seq, " 3-wire-raw");
562 if(self->service_type & IRCOMM_3_WIRE)
563 seq_printf(seq, " 3-wire");
564 if(self->service_type & IRCOMM_9_WIRE)
565 seq_printf(seq, " 9-wire");
566 if(self->service_type & IRCOMM_CENTRONICS)
567 seq_printf(seq, " Centronics");
573 static const struct seq_operations ircomm_seq_ops = {
574 .start = ircomm_seq_start,
575 .next = ircomm_seq_next,
576 .stop = ircomm_seq_stop,
577 .show = ircomm_seq_show,
580 static int ircomm_seq_open(struct inode *inode, struct file *file)
582 return seq_open(file, &ircomm_seq_ops);
584 #endif /* CONFIG_PROC_FS */
586 MODULE_AUTHOR("Dag Brattli <dag@brattli.net>");
587 MODULE_DESCRIPTION("IrCOMM protocol");
588 MODULE_LICENSE("GPL");
590 module_init(ircomm_init);
591 module_exit(ircomm_cleanup);