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 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 *
29 ********************************************************************/
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/sched.h>
34 #include <linux/proc_fs.h>
35 #include <linux/init.h>
36
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>
43
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>
49
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);
53
54 #ifdef CONFIG_PROC_FS
55 static int ircomm_proc_read(char *buf, char **start, off_t offset, int len);
56
57 extern struct proc_dir_entry *proc_irda;
58 #endif /* CONFIG_PROC_FS */
59
60 hashbin_t *ircomm = NULL;
61
ircomm_init(void)62 int __init ircomm_init(void)
63 {
64 ircomm = hashbin_new(HB_LOCAL);
65 if (ircomm == NULL) {
66 ERROR("%s(), can't allocate hashbin!\n", __FUNCTION__);
67 return -ENOMEM;
68 }
69
70 #ifdef CONFIG_PROC_FS
71 create_proc_info_entry("ircomm", 0, proc_irda, ircomm_proc_read);
72 #endif /* CONFIG_PROC_FS */
73
74 MESSAGE("IrCOMM protocol (Dag Brattli)\n");
75
76 return 0;
77 }
78
79 #ifdef MODULE
ircomm_cleanup(void)80 void ircomm_cleanup(void)
81 {
82 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
83
84 hashbin_delete(ircomm, (FREE_FUNC) __ircomm_close);
85
86 #ifdef CONFIG_PROC_FS
87 remove_proc_entry("ircomm", proc_irda);
88 #endif /* CONFIG_PROC_FS */
89 }
90 #endif /* MODULE */
91
92 /*
93 * Function ircomm_open (client_notify)
94 *
95 * Start a new IrCOMM instance
96 *
97 */
ircomm_open(notify_t * notify,__u8 service_type,int line)98 struct ircomm_cb *ircomm_open(notify_t *notify, __u8 service_type, int line)
99 {
100 struct ircomm_cb *self = NULL;
101 int ret;
102
103 IRDA_DEBUG(2, "%s(), service_type=0x%02x\n", __FUNCTION__,
104 service_type);
105
106 ASSERT(ircomm != NULL, return NULL;);
107
108 self = kmalloc(sizeof(struct ircomm_cb), GFP_ATOMIC);
109 if (self == NULL)
110 return NULL;
111
112 memset(self, 0, sizeof(struct ircomm_cb));
113
114 self->notify = *notify;
115 self->magic = IRCOMM_MAGIC;
116
117 /* Check if we should use IrLMP or IrTTP */
118 if (service_type & IRCOMM_3_WIRE_RAW) {
119 self->flow_status = FLOW_START;
120 ret = ircomm_open_lsap(self);
121 } else
122 ret = ircomm_open_tsap(self);
123
124 if (ret < 0) {
125 kfree(self);
126 return NULL;
127 }
128
129 self->service_type = service_type;
130 self->line = line;
131
132 hashbin_insert(ircomm, (irda_queue_t *) self, line, NULL);
133
134 ircomm_next_state(self, IRCOMM_IDLE);
135
136 return self;
137 }
138
139 /*
140 * Function ircomm_close_instance (self)
141 *
142 * Remove IrCOMM instance
143 *
144 */
__ircomm_close(struct ircomm_cb * self)145 static int __ircomm_close(struct ircomm_cb *self)
146 {
147 IRDA_DEBUG(2,"%s()\n", __FUNCTION__);
148
149 /* Disconnect link if any */
150 ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, NULL, NULL);
151
152 /* Remove TSAP */
153 if (self->tsap) {
154 irttp_close_tsap(self->tsap);
155 self->tsap = NULL;
156 }
157
158 /* Remove LSAP */
159 if (self->lsap) {
160 irlmp_close_lsap(self->lsap);
161 self->lsap = NULL;
162 }
163 self->magic = 0;
164
165 kfree(self);
166
167 return 0;
168 }
169
170 /*
171 * Function ircomm_close (self)
172 *
173 * Closes and removes the specified IrCOMM instance
174 *
175 */
ircomm_close(struct ircomm_cb * self)176 int ircomm_close(struct ircomm_cb *self)
177 {
178 struct ircomm_cb *entry;
179
180 ASSERT(self != NULL, return -EIO;);
181 ASSERT(self->magic == IRCOMM_MAGIC, return -EIO;);
182
183 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
184
185 entry = hashbin_remove(ircomm, self->line, NULL);
186
187 ASSERT(entry == self, return -1;);
188
189 return __ircomm_close(self);
190 }
191
192 /*
193 * Function ircomm_connect_request (self, service_type)
194 *
195 * Impl. of this function is differ from one of the reference. This
196 * function does discovery as well as sending connect request
197 *
198 */
ircomm_connect_request(struct ircomm_cb * self,__u8 dlsap_sel,__u32 saddr,__u32 daddr,struct sk_buff * skb,__u8 service_type)199 int ircomm_connect_request(struct ircomm_cb *self, __u8 dlsap_sel,
200 __u32 saddr, __u32 daddr, struct sk_buff *skb,
201 __u8 service_type)
202 {
203 struct ircomm_info info;
204 int ret;
205
206 IRDA_DEBUG(2 ,"%s()\n", __FUNCTION__);
207
208 ASSERT(self != NULL, return -1;);
209 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
210
211 self->service_type= service_type;
212
213 info.dlsap_sel = dlsap_sel;
214 info.saddr = saddr;
215 info.daddr = daddr;
216
217 ret = ircomm_do_event(self, IRCOMM_CONNECT_REQUEST, skb, &info);
218
219 return ret;
220 }
221
222 /*
223 * Function ircomm_connect_indication (self, qos, skb)
224 *
225 * Notify user layer about the incoming connection
226 *
227 */
ircomm_connect_indication(struct ircomm_cb * self,struct sk_buff * skb,struct ircomm_info * info)228 void ircomm_connect_indication(struct ircomm_cb *self, struct sk_buff *skb,
229 struct ircomm_info *info)
230 {
231 int clen = 0;
232
233 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
234
235 /* Check if the packet contains data on the control channel */
236 if (skb->len > 0)
237 clen = skb->data[0];
238
239 /*
240 * If there are any data hiding in the control channel, we must
241 * deliver it first. The side effect is that the control channel
242 * will be removed from the skb
243 */
244 if (self->notify.connect_indication)
245 self->notify.connect_indication(self->notify.instance, self,
246 info->qos, info->max_data_size,
247 info->max_header_size, skb);
248 else {
249 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__);
250 dev_kfree_skb(skb);
251 }
252 }
253
254 /*
255 * Function ircomm_connect_response (self, userdata, max_sdu_size)
256 *
257 * User accepts connection
258 *
259 */
ircomm_connect_response(struct ircomm_cb * self,struct sk_buff * userdata)260 int ircomm_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
261 {
262 int ret;
263
264 ASSERT(self != NULL, return -1;);
265 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
266
267 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
268
269 ret = ircomm_do_event(self, IRCOMM_CONNECT_RESPONSE, userdata, NULL);
270
271 return ret;
272 }
273
274 /*
275 * Function connect_confirm (self, skb)
276 *
277 * Notify user layer that the link is now connected
278 *
279 */
ircomm_connect_confirm(struct ircomm_cb * self,struct sk_buff * skb,struct ircomm_info * info)280 void ircomm_connect_confirm(struct ircomm_cb *self, struct sk_buff *skb,
281 struct ircomm_info *info)
282 {
283 IRDA_DEBUG(4,"%s()\n", __FUNCTION__);
284
285 if (self->notify.connect_confirm )
286 self->notify.connect_confirm(self->notify.instance,
287 self, info->qos,
288 info->max_data_size,
289 info->max_header_size, skb);
290 else {
291 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__);
292 dev_kfree_skb(skb);
293 }
294 }
295
296 /*
297 * Function ircomm_data_request (self, userdata)
298 *
299 * Send IrCOMM data to peer device
300 *
301 */
ircomm_data_request(struct ircomm_cb * self,struct sk_buff * skb)302 int ircomm_data_request(struct ircomm_cb *self, struct sk_buff *skb)
303 {
304 int ret;
305
306 IRDA_DEBUG(4,"%s()\n", __FUNCTION__);
307
308 ASSERT(self != NULL, return -EFAULT;);
309 ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
310 ASSERT(skb != NULL, return -EFAULT;);
311
312 ret = ircomm_do_event(self, IRCOMM_DATA_REQUEST, skb, NULL);
313
314 return ret;
315 }
316
317 /*
318 * Function ircomm_data_indication (self, skb)
319 *
320 * Data arrived, so deliver it to user
321 *
322 */
ircomm_data_indication(struct ircomm_cb * self,struct sk_buff * skb)323 void ircomm_data_indication(struct ircomm_cb *self, struct sk_buff *skb)
324 {
325 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
326
327 ASSERT(skb->len > 0, return;);
328
329 if (self->notify.data_indication)
330 self->notify.data_indication(self->notify.instance, self, skb);
331 else {
332 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__);
333 dev_kfree_skb(skb);
334 }
335 }
336
337 /*
338 * Function ircomm_process_data (self, skb)
339 *
340 * Data arrived which may contain control channel data
341 *
342 */
ircomm_process_data(struct ircomm_cb * self,struct sk_buff * skb)343 void ircomm_process_data(struct ircomm_cb *self, struct sk_buff *skb)
344 {
345 int clen;
346
347 ASSERT(skb->len > 0, return;);
348
349 clen = skb->data[0];
350
351 /*
352 * If there are any data hiding in the control channel, we must
353 * deliver it first. The side effect is that the control channel
354 * will be removed from the skb
355 */
356 if (clen > 0)
357 ircomm_control_indication(self, skb, clen);
358
359 /* Remove control channel from data channel */
360 skb_pull(skb, clen+1);
361
362 if (skb->len)
363 ircomm_data_indication(self, skb);
364 else {
365 IRDA_DEBUG(4, "%s(), data was control info only!\n", __FUNCTION__);
366 dev_kfree_skb(skb);
367 }
368 }
369
370 /*
371 * Function ircomm_control_request (self, params)
372 *
373 * Send control data to peer device
374 *
375 */
ircomm_control_request(struct ircomm_cb * self,struct sk_buff * skb)376 int ircomm_control_request(struct ircomm_cb *self, struct sk_buff *skb)
377 {
378 int ret;
379
380 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
381
382 ASSERT(self != NULL, return -EFAULT;);
383 ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
384 ASSERT(skb != NULL, return -EFAULT;);
385
386 ret = ircomm_do_event(self, IRCOMM_CONTROL_REQUEST, skb, NULL);
387
388 return ret;
389 }
390
391 /*
392 * Function ircomm_control_indication (self, skb)
393 *
394 * Data has arrived on the control channel
395 *
396 */
ircomm_control_indication(struct ircomm_cb * self,struct sk_buff * skb,int clen)397 static void ircomm_control_indication(struct ircomm_cb *self,
398 struct sk_buff *skb, int clen)
399 {
400 struct sk_buff *ctrl_skb;
401
402 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
403
404 ctrl_skb = skb_clone(skb, GFP_ATOMIC);
405 if (!ctrl_skb)
406 return;
407
408 /* Remove data channel from control channel */
409 skb_trim(ctrl_skb, clen+1);
410
411 /* Use udata for delivering data on the control channel */
412 if (self->notify.udata_indication)
413 self->notify.udata_indication(self->notify.instance, self,
414 ctrl_skb);
415 else {
416 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__);
417 dev_kfree_skb(skb);
418 }
419 }
420
421 /*
422 * Function ircomm_disconnect_request (self, userdata, priority)
423 *
424 * User layer wants to disconnect the IrCOMM connection
425 *
426 */
ircomm_disconnect_request(struct ircomm_cb * self,struct sk_buff * userdata)427 int ircomm_disconnect_request(struct ircomm_cb *self, struct sk_buff *userdata)
428 {
429 struct ircomm_info info;
430 int ret;
431
432 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
433
434 ASSERT(self != NULL, return -1;);
435 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
436
437 ret = ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, userdata,
438 &info);
439 return ret;
440 }
441
442 /*
443 * Function disconnect_indication (self, skb)
444 *
445 * Tell user that the link has been disconnected
446 *
447 */
ircomm_disconnect_indication(struct ircomm_cb * self,struct sk_buff * skb,struct ircomm_info * info)448 void ircomm_disconnect_indication(struct ircomm_cb *self, struct sk_buff *skb,
449 struct ircomm_info *info)
450 {
451 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
452
453 ASSERT(info != NULL, return;);
454
455 if (self->notify.disconnect_indication) {
456 self->notify.disconnect_indication(self->notify.instance, self,
457 info->reason, skb);
458 } else {
459 IRDA_DEBUG(0, "%s(), missing handler\n", __FUNCTION__);
460 dev_kfree_skb(skb);
461 }
462 }
463
464 /*
465 * Function ircomm_flow_request (self, flow)
466 *
467 *
468 *
469 */
ircomm_flow_request(struct ircomm_cb * self,LOCAL_FLOW flow)470 void ircomm_flow_request(struct ircomm_cb *self, LOCAL_FLOW flow)
471 {
472 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
473
474 ASSERT(self != NULL, return;);
475 ASSERT(self->magic == IRCOMM_MAGIC, return;);
476
477 if (self->service_type == IRCOMM_3_WIRE_RAW)
478 return;
479
480 irttp_flow_request(self->tsap, flow);
481 }
482
483 #ifdef CONFIG_PROC_FS
484 /*
485 * Function ircomm_proc_read (buf, start, offset, len, unused)
486 *
487 *
488 *
489 */
ircomm_proc_read(char * buf,char ** start,off_t offset,int len)490 int ircomm_proc_read(char *buf, char **start, off_t offset, int len)
491 {
492 struct ircomm_cb *self;
493 unsigned long flags;
494
495 save_flags(flags);
496 cli();
497
498 len = 0;
499
500 self = (struct ircomm_cb *) hashbin_get_first(ircomm);
501 while (self != NULL) {
502 ASSERT(self->magic == IRCOMM_MAGIC, break;);
503
504 if(self->line < 0x10)
505 len += sprintf(buf+len, "ircomm%d", self->line);
506 else
507 len += sprintf(buf+len, "irlpt%d", self->line - 0x10);
508 len += sprintf(buf+len, " state: %s, ",
509 ircomm_state[ self->state]);
510 len += sprintf(buf+len,
511 "slsap_sel: %#02x, dlsap_sel: %#02x, mode:",
512 self->slsap_sel, self->dlsap_sel);
513 if(self->service_type & IRCOMM_3_WIRE_RAW)
514 len += sprintf(buf+len, " 3-wire-raw");
515 if(self->service_type & IRCOMM_3_WIRE)
516 len += sprintf(buf+len, " 3-wire");
517 if(self->service_type & IRCOMM_9_WIRE)
518 len += sprintf(buf+len, " 9-wire");
519 if(self->service_type & IRCOMM_CENTRONICS)
520 len += sprintf(buf+len, " Centronics");
521 len += sprintf(buf+len, "\n");
522
523 self = (struct ircomm_cb *) hashbin_get_next(ircomm);
524 }
525 restore_flags(flags);
526
527 return len;
528 }
529 #endif /* CONFIG_PROC_FS */
530
531 #ifdef MODULE
532 MODULE_AUTHOR("Dag Brattli <dag@brattli.net>");
533 MODULE_DESCRIPTION("IrCOMM protocol");
534 MODULE_LICENSE("GPL");
535
init_module(void)536 int init_module(void)
537 {
538 return ircomm_init();
539 }
540
cleanup_module(void)541 void cleanup_module(void)
542 {
543 ircomm_cleanup();
544 }
545 #endif /* MODULE */
546
547