Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / irda / ircomm / ircomm_core.c
blob44399a06246b0985d77483725f4f5305d3200694
1 /*********************************************************************
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>
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, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
30 ********************************************************************/
32 #include <linux/module.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/init.h>
37 #include <net/irda/irda.h>
38 #include <net/irda/irmod.h>
39 #include <net/irda/irlmp.h>
40 #include <net/irda/iriap.h>
41 #include <net/irda/irttp.h>
42 #include <net/irda/irias_object.h>
44 #include <net/irda/ircomm_event.h>
45 #include <net/irda/ircomm_lmp.h>
46 #include <net/irda/ircomm_ttp.h>
47 #include <net/irda/ircomm_param.h>
48 #include <net/irda/ircomm_core.h>
50 static int __ircomm_close(struct ircomm_cb *self);
51 static void ircomm_control_indication(struct ircomm_cb *self,
52 struct sk_buff *skb, int clen);
54 #ifdef CONFIG_PROC_FS
55 extern struct proc_dir_entry *proc_irda;
56 static int ircomm_seq_open(struct inode *, struct file *);
58 static const struct file_operations ircomm_proc_fops = {
59 .owner = THIS_MODULE,
60 .open = ircomm_seq_open,
61 .read = seq_read,
62 .llseek = seq_lseek,
63 .release = seq_release,
65 #endif /* CONFIG_PROC_FS */
67 hashbin_t *ircomm = NULL;
69 static int __init ircomm_init(void)
71 ircomm = hashbin_new(HB_LOCK);
72 if (ircomm == NULL) {
73 IRDA_ERROR("%s(), can't allocate hashbin!\n", __FUNCTION__);
74 return -ENOMEM;
77 #ifdef CONFIG_PROC_FS
78 { struct proc_dir_entry *ent;
79 <<<<<<< HEAD:net/irda/ircomm/ircomm_core.c
80 ent = create_proc_entry("ircomm", 0, proc_irda);
81 if (ent)
82 ent->proc_fops = &ircomm_proc_fops;
83 =======
84 ent = proc_create("ircomm", 0, proc_irda, &ircomm_proc_fops);
85 if (!ent) {
86 printk(KERN_ERR "ircomm_init: can't create /proc entry!\n");
87 return -ENODEV;
89 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:net/irda/ircomm/ircomm_core.c
91 #endif /* CONFIG_PROC_FS */
93 IRDA_MESSAGE("IrCOMM protocol (Dag Brattli)\n");
95 return 0;
98 static void __exit ircomm_cleanup(void)
100 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
102 hashbin_delete(ircomm, (FREE_FUNC) __ircomm_close);
104 #ifdef CONFIG_PROC_FS
105 remove_proc_entry("ircomm", proc_irda);
106 #endif /* CONFIG_PROC_FS */
110 * Function ircomm_open (client_notify)
112 * Start a new IrCOMM instance
115 struct ircomm_cb *ircomm_open(notify_t *notify, __u8 service_type, int line)
117 struct ircomm_cb *self = NULL;
118 int ret;
120 IRDA_DEBUG(2, "%s(), service_type=0x%02x\n", __FUNCTION__ ,
121 service_type);
123 IRDA_ASSERT(ircomm != NULL, return NULL;);
125 self = kzalloc(sizeof(struct ircomm_cb), GFP_ATOMIC);
126 if (self == NULL)
127 return NULL;
129 self->notify = *notify;
130 self->magic = IRCOMM_MAGIC;
132 /* Check if we should use IrLMP or IrTTP */
133 if (service_type & IRCOMM_3_WIRE_RAW) {
134 self->flow_status = FLOW_START;
135 ret = ircomm_open_lsap(self);
136 } else
137 ret = ircomm_open_tsap(self);
139 if (ret < 0) {
140 kfree(self);
141 return NULL;
144 self->service_type = service_type;
145 self->line = line;
147 hashbin_insert(ircomm, (irda_queue_t *) self, line, NULL);
149 ircomm_next_state(self, IRCOMM_IDLE);
151 return self;
154 EXPORT_SYMBOL(ircomm_open);
157 * Function ircomm_close_instance (self)
159 * Remove IrCOMM instance
162 static int __ircomm_close(struct ircomm_cb *self)
164 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
166 /* Disconnect link if any */
167 ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, NULL, NULL);
169 /* Remove TSAP */
170 if (self->tsap) {
171 irttp_close_tsap(self->tsap);
172 self->tsap = NULL;
175 /* Remove LSAP */
176 if (self->lsap) {
177 irlmp_close_lsap(self->lsap);
178 self->lsap = NULL;
180 self->magic = 0;
182 kfree(self);
184 return 0;
188 * Function ircomm_close (self)
190 * Closes and removes the specified IrCOMM instance
193 int ircomm_close(struct ircomm_cb *self)
195 struct ircomm_cb *entry;
197 IRDA_ASSERT(self != NULL, return -EIO;);
198 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EIO;);
200 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
202 entry = hashbin_remove(ircomm, self->line, NULL);
204 IRDA_ASSERT(entry == self, return -1;);
206 return __ircomm_close(self);
209 EXPORT_SYMBOL(ircomm_close);
212 * Function ircomm_connect_request (self, service_type)
214 * Impl. of this function is differ from one of the reference. This
215 * function does discovery as well as sending connect request
218 int ircomm_connect_request(struct ircomm_cb *self, __u8 dlsap_sel,
219 __u32 saddr, __u32 daddr, struct sk_buff *skb,
220 __u8 service_type)
222 struct ircomm_info info;
223 int ret;
225 IRDA_DEBUG(2 , "%s()\n", __FUNCTION__ );
227 IRDA_ASSERT(self != NULL, return -1;);
228 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
230 self->service_type= service_type;
232 info.dlsap_sel = dlsap_sel;
233 info.saddr = saddr;
234 info.daddr = daddr;
236 ret = ircomm_do_event(self, IRCOMM_CONNECT_REQUEST, skb, &info);
238 return ret;
241 EXPORT_SYMBOL(ircomm_connect_request);
244 * Function ircomm_connect_indication (self, qos, skb)
246 * Notify user layer about the incoming connection
249 void ircomm_connect_indication(struct ircomm_cb *self, struct sk_buff *skb,
250 struct ircomm_info *info)
252 int clen = 0;
254 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
256 /* Check if the packet contains data on the control channel */
257 if (skb->len > 0)
258 clen = skb->data[0];
261 * If there are any data hiding in the control channel, we must
262 * deliver it first. The side effect is that the control channel
263 * will be removed from the skb
265 if (self->notify.connect_indication)
266 self->notify.connect_indication(self->notify.instance, self,
267 info->qos, info->max_data_size,
268 info->max_header_size, skb);
269 else {
270 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__ );
275 * Function ircomm_connect_response (self, userdata, max_sdu_size)
277 * User accepts connection
280 int ircomm_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
282 int ret;
284 IRDA_ASSERT(self != NULL, return -1;);
285 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
287 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
289 ret = ircomm_do_event(self, IRCOMM_CONNECT_RESPONSE, userdata, NULL);
291 return ret;
294 EXPORT_SYMBOL(ircomm_connect_response);
297 * Function connect_confirm (self, skb)
299 * Notify user layer that the link is now connected
302 void ircomm_connect_confirm(struct ircomm_cb *self, struct sk_buff *skb,
303 struct ircomm_info *info)
305 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
307 if (self->notify.connect_confirm )
308 self->notify.connect_confirm(self->notify.instance,
309 self, info->qos,
310 info->max_data_size,
311 info->max_header_size, skb);
312 else {
313 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__ );
318 * Function ircomm_data_request (self, userdata)
320 * Send IrCOMM data to peer device
323 int ircomm_data_request(struct ircomm_cb *self, struct sk_buff *skb)
325 int ret;
327 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
329 IRDA_ASSERT(self != NULL, return -EFAULT;);
330 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
331 IRDA_ASSERT(skb != NULL, return -EFAULT;);
333 ret = ircomm_do_event(self, IRCOMM_DATA_REQUEST, skb, NULL);
335 return ret;
338 EXPORT_SYMBOL(ircomm_data_request);
341 * Function ircomm_data_indication (self, skb)
343 * Data arrived, so deliver it to user
346 void ircomm_data_indication(struct ircomm_cb *self, struct sk_buff *skb)
348 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
350 IRDA_ASSERT(skb->len > 0, return;);
352 if (self->notify.data_indication)
353 self->notify.data_indication(self->notify.instance, self, skb);
354 else {
355 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__ );
360 * Function ircomm_process_data (self, skb)
362 * Data arrived which may contain control channel data
365 void ircomm_process_data(struct ircomm_cb *self, struct sk_buff *skb)
367 int clen;
369 IRDA_ASSERT(skb->len > 0, return;);
371 clen = skb->data[0];
374 * Input validation check: a stir4200/mcp2150 combinations sometimes
375 * results in frames with clen > remaining packet size. These are
376 * illegal; if we throw away just this frame then it seems to carry on
377 * fine
379 if (unlikely(skb->len < (clen + 1))) {
380 IRDA_DEBUG(2, "%s() throwing away illegal frame\n",
381 __FUNCTION__ );
382 return;
386 * If there are any data hiding in the control channel, we must
387 * deliver it first. The side effect is that the control channel
388 * will be removed from the skb
390 if (clen > 0)
391 ircomm_control_indication(self, skb, clen);
393 /* Remove control channel from data channel */
394 skb_pull(skb, clen+1);
396 if (skb->len)
397 ircomm_data_indication(self, skb);
398 else {
399 IRDA_DEBUG(4, "%s(), data was control info only!\n",
400 __FUNCTION__ );
405 * Function ircomm_control_request (self, params)
407 * Send control data to peer device
410 int ircomm_control_request(struct ircomm_cb *self, struct sk_buff *skb)
412 int ret;
414 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
416 IRDA_ASSERT(self != NULL, return -EFAULT;);
417 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
418 IRDA_ASSERT(skb != NULL, return -EFAULT;);
420 ret = ircomm_do_event(self, IRCOMM_CONTROL_REQUEST, skb, NULL);
422 return ret;
425 EXPORT_SYMBOL(ircomm_control_request);
428 * Function ircomm_control_indication (self, skb)
430 * Data has arrived on the control channel
433 static void ircomm_control_indication(struct ircomm_cb *self,
434 struct sk_buff *skb, int clen)
436 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
438 /* Use udata for delivering data on the control channel */
439 if (self->notify.udata_indication) {
440 struct sk_buff *ctrl_skb;
442 /* We don't own the skb, so clone it */
443 ctrl_skb = skb_clone(skb, GFP_ATOMIC);
444 if (!ctrl_skb)
445 return;
447 /* Remove data channel from control channel */
448 skb_trim(ctrl_skb, clen+1);
450 self->notify.udata_indication(self->notify.instance, self,
451 ctrl_skb);
453 /* Drop reference count -
454 * see ircomm_tty_control_indication(). */
455 dev_kfree_skb(ctrl_skb);
456 } else {
457 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__ );
462 * Function ircomm_disconnect_request (self, userdata, priority)
464 * User layer wants to disconnect the IrCOMM connection
467 int ircomm_disconnect_request(struct ircomm_cb *self, struct sk_buff *userdata)
469 struct ircomm_info info;
470 int ret;
472 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
474 IRDA_ASSERT(self != NULL, return -1;);
475 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
477 ret = ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, userdata,
478 &info);
479 return ret;
482 EXPORT_SYMBOL(ircomm_disconnect_request);
485 * Function disconnect_indication (self, skb)
487 * Tell user that the link has been disconnected
490 void ircomm_disconnect_indication(struct ircomm_cb *self, struct sk_buff *skb,
491 struct ircomm_info *info)
493 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
495 IRDA_ASSERT(info != NULL, return;);
497 if (self->notify.disconnect_indication) {
498 self->notify.disconnect_indication(self->notify.instance, self,
499 info->reason, skb);
500 } else {
501 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__ );
506 * Function ircomm_flow_request (self, flow)
511 void ircomm_flow_request(struct ircomm_cb *self, LOCAL_FLOW flow)
513 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
515 IRDA_ASSERT(self != NULL, return;);
516 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
518 if (self->service_type == IRCOMM_3_WIRE_RAW)
519 return;
521 irttp_flow_request(self->tsap, flow);
524 EXPORT_SYMBOL(ircomm_flow_request);
526 #ifdef CONFIG_PROC_FS
527 static void *ircomm_seq_start(struct seq_file *seq, loff_t *pos)
529 struct ircomm_cb *self;
530 loff_t off = 0;
532 spin_lock_irq(&ircomm->hb_spinlock);
534 for (self = (struct ircomm_cb *) hashbin_get_first(ircomm);
535 self != NULL;
536 self = (struct ircomm_cb *) hashbin_get_next(ircomm)) {
537 if (off++ == *pos)
538 break;
541 return self;
544 static void *ircomm_seq_next(struct seq_file *seq, void *v, loff_t *pos)
546 ++*pos;
548 return (void *) hashbin_get_next(ircomm);
551 static void ircomm_seq_stop(struct seq_file *seq, void *v)
553 spin_unlock_irq(&ircomm->hb_spinlock);
556 static int ircomm_seq_show(struct seq_file *seq, void *v)
558 const struct ircomm_cb *self = v;
560 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EINVAL; );
562 if(self->line < 0x10)
563 seq_printf(seq, "ircomm%d", self->line);
564 else
565 seq_printf(seq, "irlpt%d", self->line - 0x10);
567 seq_printf(seq,
568 " state: %s, slsap_sel: %#02x, dlsap_sel: %#02x, mode:",
569 ircomm_state[ self->state],
570 self->slsap_sel, self->dlsap_sel);
572 if(self->service_type & IRCOMM_3_WIRE_RAW)
573 seq_printf(seq, " 3-wire-raw");
574 if(self->service_type & IRCOMM_3_WIRE)
575 seq_printf(seq, " 3-wire");
576 if(self->service_type & IRCOMM_9_WIRE)
577 seq_printf(seq, " 9-wire");
578 if(self->service_type & IRCOMM_CENTRONICS)
579 seq_printf(seq, " Centronics");
580 seq_putc(seq, '\n');
582 return 0;
585 static const struct seq_operations ircomm_seq_ops = {
586 .start = ircomm_seq_start,
587 .next = ircomm_seq_next,
588 .stop = ircomm_seq_stop,
589 .show = ircomm_seq_show,
592 static int ircomm_seq_open(struct inode *inode, struct file *file)
594 return seq_open(file, &ircomm_seq_ops);
596 #endif /* CONFIG_PROC_FS */
598 MODULE_AUTHOR("Dag Brattli <dag@brattli.net>");
599 MODULE_DESCRIPTION("IrCOMM protocol");
600 MODULE_LICENSE("GPL");
602 module_init(ircomm_init);
603 module_exit(ircomm_cleanup);