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