- Peter Anvin: more P4 configuration parsing
[davej-history.git] / net / irda / ircomm / ircomm_core.c
blob7ae03b0a623e55b59190f02101f0b32767ae60c4
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>
12 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
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.
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.
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
29 ********************************************************************/
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>
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 static int ircomm_proc_read(char *buf, char **start, off_t offset, int len);
57 extern struct proc_dir_entry *proc_irda;
58 #endif /* CONFIG_PROC_FS */
60 hashbin_t *ircomm = NULL;
62 int __init ircomm_init(void)
64 ircomm = hashbin_new(HB_LOCAL);
65 if (ircomm == NULL) {
66 ERROR(__FUNCTION__ "(), can't allocate hashbin!\n");
67 return -ENOMEM;
70 #ifdef CONFIG_PROC_FS
71 create_proc_info_entry("ircomm", 0, proc_irda, ircomm_proc_read);
72 #endif /* CONFIG_PROC_FS */
74 MESSAGE("IrCOMM protocol (Dag Brattli)\n");
76 return 0;
79 #ifdef MODULE
80 void ircomm_cleanup(void)
82 IRDA_DEBUG(2, __FUNCTION__ "()\n");
84 hashbin_delete(ircomm, (FREE_FUNC) __ircomm_close);
86 #ifdef CONFIG_PROC_FS
87 remove_proc_entry("ircomm", proc_irda);
88 #endif /* CONFIG_PROC_FS */
90 #endif /* MODULE */
93 * Function ircomm_open (client_notify)
95 * Start a new IrCOMM instance
98 struct ircomm_cb *ircomm_open(notify_t *notify, __u8 service_type, int line)
100 struct ircomm_cb *self = NULL;
101 int ret;
103 IRDA_DEBUG(2, __FUNCTION__ "(), service_type=0x%02x\n",
104 service_type);
106 ASSERT(ircomm != NULL, return NULL;);
108 self = kmalloc(sizeof(struct ircomm_cb), GFP_ATOMIC);
109 if (self == NULL)
110 return NULL;
112 memset(self, 0, sizeof(struct ircomm_cb));
114 self->notify = *notify;
115 self->magic = IRCOMM_MAGIC;
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);
124 if (ret < 0)
125 return NULL;
127 self->service_type = service_type;
128 self->line = line;
130 hashbin_insert(ircomm, (irda_queue_t *) self, line, NULL);
132 ircomm_next_state(self, IRCOMM_IDLE);
134 return self;
138 * Function ircomm_close_instance (self)
140 * Remove IrCOMM instance
143 static int __ircomm_close(struct ircomm_cb *self)
145 IRDA_DEBUG(2, __FUNCTION__"()\n");
147 /* Disconnect link if any */
148 ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, NULL, NULL);
150 /* Remove TSAP */
151 if (self->tsap) {
152 irttp_close_tsap(self->tsap);
153 self->tsap = NULL;
156 /* Remove LSAP */
157 if (self->lsap) {
158 irlmp_close_lsap(self->lsap);
159 self->lsap = NULL;
161 self->magic = 0;
163 kfree(self);
165 return 0;
169 * Function ircomm_close (self)
171 * Closes and removes the specified IrCOMM instance
174 int ircomm_close(struct ircomm_cb *self)
176 struct ircomm_cb *entry;
178 ASSERT(self != NULL, return -EIO;);
179 ASSERT(self->magic == IRCOMM_MAGIC, return -EIO;);
181 IRDA_DEBUG(0, __FUNCTION__ "()\n");
183 entry = hashbin_remove(ircomm, self->line, NULL);
185 ASSERT(entry == self, return -1;);
187 return __ircomm_close(self);
191 * Function ircomm_connect_request (self, service_type)
193 * Impl. of this function is differ from one of the reference. This
194 * function does discovery as well as sending connect request
197 int ircomm_connect_request(struct ircomm_cb *self, __u8 dlsap_sel,
198 __u32 saddr, __u32 daddr, struct sk_buff *skb,
199 __u8 service_type)
201 struct ircomm_info info;
202 int ret;
204 IRDA_DEBUG(2 , __FUNCTION__"()\n");
206 ASSERT(self != NULL, return -1;);
207 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
209 self->service_type= service_type;
211 info.dlsap_sel = dlsap_sel;
212 info.saddr = saddr;
213 info.daddr = daddr;
215 ret = ircomm_do_event(self, IRCOMM_CONNECT_REQUEST, skb, &info);
217 return ret;
221 * Function ircomm_connect_indication (self, qos, skb)
223 * Notify user layer about the incomming connection
226 void ircomm_connect_indication(struct ircomm_cb *self, struct sk_buff *skb,
227 struct ircomm_info *info)
229 int clen = 0;
231 IRDA_DEBUG(2, __FUNCTION__ "()\n");
233 /* Check if the packet contains data on the control channel */
234 if (skb->len > 0)
235 clen = skb->data[0];
238 * If there are any data hiding in the control channel, we must
239 * deliver it first. The side effect is that the control channel
240 * will be removed from the skb
242 if (self->notify.connect_indication)
243 self->notify.connect_indication(self->notify.instance, self,
244 info->qos, info->max_data_size,
245 info->max_header_size, skb);
246 else {
247 IRDA_DEBUG(0, __FUNCTION__ "(), missing handler\n");
248 dev_kfree_skb(skb);
253 * Function ircomm_connect_response (self, userdata, max_sdu_size)
255 * User accepts connection
258 int ircomm_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
260 int ret;
262 ASSERT(self != NULL, return -1;);
263 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
265 IRDA_DEBUG(4, __FUNCTION__ "()\n");
267 ret = ircomm_do_event(self, IRCOMM_CONNECT_RESPONSE, userdata, NULL);
269 return ret;
273 * Function connect_confirm (self, skb)
275 * Notify user layer that the link is now connected
278 void ircomm_connect_confirm(struct ircomm_cb *self, struct sk_buff *skb,
279 struct ircomm_info *info)
281 IRDA_DEBUG(4, __FUNCTION__"()\n");
283 if (self->notify.connect_confirm )
284 self->notify.connect_confirm(self->notify.instance,
285 self, info->qos,
286 info->max_data_size,
287 info->max_header_size, skb);
288 else {
289 IRDA_DEBUG(0, __FUNCTION__ "(), missing handler\n");
290 dev_kfree_skb(skb);
295 * Function ircomm_data_request (self, userdata)
297 * Send IrCOMM data to peer device
300 int ircomm_data_request(struct ircomm_cb *self, struct sk_buff *skb)
302 int ret;
304 IRDA_DEBUG(4, __FUNCTION__"()\n");
306 ASSERT(self != NULL, return -EFAULT;);
307 ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
308 ASSERT(skb != NULL, return -EFAULT;);
310 ret = ircomm_do_event(self, IRCOMM_DATA_REQUEST, skb, NULL);
312 return ret;
316 * Function ircomm_data_indication (self, skb)
318 * Data arrived, so deliver it to user
321 void ircomm_data_indication(struct ircomm_cb *self, struct sk_buff *skb)
323 IRDA_DEBUG(4, __FUNCTION__"()\n");
325 ASSERT(skb->len > 0, return;);
327 if (self->notify.data_indication)
328 self->notify.data_indication(self->notify.instance, self, skb);
329 else {
330 IRDA_DEBUG(0, __FUNCTION__ "(), missing handler\n");
331 dev_kfree_skb(skb);
336 * Function ircomm_process_data (self, skb)
338 * Data arrived which may contain control channel data
341 void ircomm_process_data(struct ircomm_cb *self, struct sk_buff *skb)
343 int clen;
345 ASSERT(skb->len > 0, return;);
347 clen = skb->data[0];
350 * If there are any data hiding in the control channel, we must
351 * deliver it first. The side effect is that the control channel
352 * will be removed from the skb
354 if (clen > 0)
355 ircomm_control_indication(self, skb, clen);
357 /* Remove control channel from data channel */
358 skb_pull(skb, clen+1);
360 if (skb->len)
361 ircomm_data_indication(self, skb);
362 else {
363 IRDA_DEBUG(4, __FUNCTION__
364 "(), data was control info only!\n");
365 dev_kfree_skb(skb);
370 * Function ircomm_control_request (self, params)
372 * Send control data to peer device
375 int ircomm_control_request(struct ircomm_cb *self, struct sk_buff *skb)
377 int ret;
379 IRDA_DEBUG(2, __FUNCTION__"()\n");
381 ASSERT(self != NULL, return -EFAULT;);
382 ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
383 ASSERT(skb != NULL, return -EFAULT;);
385 ret = ircomm_do_event(self, IRCOMM_CONTROL_REQUEST, skb, NULL);
387 return ret;
391 * Function ircomm_control_indication (self, skb)
393 * Data has arrived on the control channel
396 static void ircomm_control_indication(struct ircomm_cb *self,
397 struct sk_buff *skb, int clen)
399 struct sk_buff *ctrl_skb;
401 IRDA_DEBUG(2, __FUNCTION__"()\n");
403 ctrl_skb = skb_clone(skb, GFP_ATOMIC);
404 if (!ctrl_skb)
405 return;
407 /* Remove data channel from control channel */
408 skb_trim(ctrl_skb, clen+1);
410 /* Use udata for delivering data on the control channel */
411 if (self->notify.udata_indication)
412 self->notify.udata_indication(self->notify.instance, self,
413 ctrl_skb);
414 else {
415 IRDA_DEBUG(0, __FUNCTION__ "(), missing handler\n");
416 dev_kfree_skb(skb);
421 * Function ircomm_disconnect_request (self, userdata, priority)
423 * User layer wants to disconnect the IrCOMM connection
426 int ircomm_disconnect_request(struct ircomm_cb *self, struct sk_buff *userdata)
428 struct ircomm_info info;
429 int ret;
431 IRDA_DEBUG(2, __FUNCTION__"()\n");
433 ASSERT(self != NULL, return -1;);
434 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
436 ret = ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, userdata,
437 &info);
438 return ret;
442 * Function disconnect_indication (self, skb)
444 * Tell user that the link has been disconnected
447 void ircomm_disconnect_indication(struct ircomm_cb *self, struct sk_buff *skb,
448 struct ircomm_info *info)
450 IRDA_DEBUG(2, __FUNCTION__ "()\n");
452 ASSERT(info != NULL, return;);
454 if (self->notify.disconnect_indication) {
455 self->notify.disconnect_indication(self->notify.instance, self,
456 info->reason, skb);
457 } else {
458 IRDA_DEBUG(0, __FUNCTION__ "(), missing handler\n");
459 dev_kfree_skb(skb);
464 * Function ircomm_flow_request (self, flow)
469 void ircomm_flow_request(struct ircomm_cb *self, LOCAL_FLOW flow)
471 IRDA_DEBUG(2, __FUNCTION__ "()\n");
473 ASSERT(self != NULL, return;);
474 ASSERT(self->magic == IRCOMM_MAGIC, return;);
476 if (self->service_type == IRCOMM_3_WIRE_RAW)
477 return;
479 irttp_flow_request(self->tsap, flow);
482 #ifdef CONFIG_PROC_FS
484 * Function ircomm_proc_read (buf, start, offset, len, unused)
489 int ircomm_proc_read(char *buf, char **start, off_t offset, int len)
491 struct ircomm_cb *self;
492 unsigned long flags;
493 int i=0;
495 save_flags(flags);
496 cli();
498 len = 0;
500 len += sprintf(buf+len, "Instance %d:\n", i++);
502 self = (struct ircomm_cb *) hashbin_get_first(ircomm);
503 while (self != NULL) {
504 ASSERT(self->magic == IRCOMM_MAGIC, return len;);
506 self = (struct ircomm_cb *) hashbin_get_next(ircomm);
508 restore_flags(flags);
510 return len;
512 #endif /* CONFIG_PROC_FS */
514 #ifdef MODULE
515 MODULE_AUTHOR("Dag Brattli <dag@brattli.net>");
516 MODULE_DESCRIPTION("IrCOMM protocol");
518 int init_module(void)
520 return ircomm_init();
523 void cleanup_module(void)
525 ircomm_cleanup();
527 #endif /* MODULE */