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