1/********************************************************************* 2 * 3 * Filename: irttp.c 4 * Version: 1.2 5 * Description: Tiny Transport Protocol (TTP) implementation 6 * Status: Stable 7 * Author: Dag Brattli <dagb@cs.uit.no> 8 * Created at: Sun Aug 31 20:14:31 1997 9 * Modified at: Wed Jan 5 11:31:27 2000 10 * Modified by: Dag Brattli <dagb@cs.uit.no> 11 * 12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>, 13 * All Rights Reserved. 14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com> 15 * 16 * This program is free software; you can redistribute it and/or 17 * modify it under the terms of the GNU General Public License as 18 * published by the Free Software Foundation; either version 2 of 19 * the License, or (at your option) any later version. 20 * 21 * Neither Dag Brattli nor University of Troms�� admit liability nor 22 * provide warranty for any of this software. This material is 23 * provided "AS-IS" and at no charge. 24 * 25 ********************************************************************/ 26 27#include <linux/skbuff.h> 28#include <linux/init.h> 29#include <linux/fs.h> 30#include <linux/seq_file.h> 31#include <linux/slab.h> 32 33#include <asm/byteorder.h> 34#include <asm/unaligned.h> 35 36#include <net/irda/irda.h> 37#include <net/irda/irlap.h> 38#include <net/irda/irlmp.h> 39#include <net/irda/parameters.h> 40#include <net/irda/irttp.h> 41 42static struct irttp_cb *irttp; 43 44static void __irttp_close_tsap(struct tsap_cb *self); 45 46static int irttp_data_indication(void *instance, void *sap, 47 struct sk_buff *skb); 48static int irttp_udata_indication(void *instance, void *sap, 49 struct sk_buff *skb); 50static void irttp_disconnect_indication(void *instance, void *sap, 51 LM_REASON reason, struct sk_buff *); 52static void irttp_connect_indication(void *instance, void *sap, 53 struct qos_info *qos, __u32 max_sdu_size, 54 __u8 header_size, struct sk_buff *skb); 55static void irttp_connect_confirm(void *instance, void *sap, 56 struct qos_info *qos, __u32 max_sdu_size, 57 __u8 header_size, struct sk_buff *skb); 58static void irttp_run_tx_queue(struct tsap_cb *self); 59static void irttp_run_rx_queue(struct tsap_cb *self); 60 61static void irttp_flush_queues(struct tsap_cb *self); 62static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb); 63static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self); 64static void irttp_todo_expired(unsigned long data); 65static int irttp_param_max_sdu_size(void *instance, irda_param_t *param, 66 int get); 67 68static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow); 69static void irttp_status_indication(void *instance, 70 LINK_STATUS link, LOCK_STATUS lock); 71 72/* Information for parsing parameters in IrTTP */ 73static pi_minor_info_t pi_minor_call_table[] = { 74 { NULL, 0 }, /* 0x00 */ 75 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */ 76}; 77static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }}; 78static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 }; 79 80/************************ GLOBAL PROCEDURES ************************/ 81 82/* 83 * Function irttp_init (void) 84 * 85 * Initialize the IrTTP layer. Called by module initialization code 86 * 87 */ 88int __init irttp_init(void) 89{ 90 irttp = kzalloc(sizeof(struct irttp_cb), GFP_KERNEL); 91 if (irttp == NULL) 92 return -ENOMEM; 93 94 irttp->magic = TTP_MAGIC; 95 96 irttp->tsaps = hashbin_new(HB_LOCK); 97 if (!irttp->tsaps) { 98 IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n", 99 __func__); 100 kfree(irttp); 101 return -ENOMEM; 102 } 103 104 return 0; 105} 106 107/* 108 * Function irttp_cleanup (void) 109 * 110 * Called by module destruction/cleanup code 111 * 112 */ 113void irttp_cleanup(void) 114{ 115 /* Check for main structure */ 116 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;); 117 118 /* 119 * Delete hashbin and close all TSAP instances in it 120 */ 121 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap); 122 123 irttp->magic = 0; 124 125 /* De-allocate main structure */ 126 kfree(irttp); 127 128 irttp = NULL; 129} 130 131/*************************** SUBROUTINES ***************************/ 132 133/* 134 * Function irttp_start_todo_timer (self, timeout) 135 * 136 * Start todo timer. 137 * 138 * Made it more effient and unsensitive to race conditions - Jean II 139 */ 140static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout) 141{ 142 /* Set new value for timer */ 143 mod_timer(&self->todo_timer, jiffies + timeout); 144} 145 146/* 147 * Function irttp_todo_expired (data) 148 * 149 * Todo timer has expired! 150 * 151 * One of the restriction of the timer is that it is run only on the timer 152 * interrupt which run every 10ms. This mean that even if you set the timer 153 * with a delay of 0, it may take up to 10ms before it's run. 154 * So, to minimise latency and keep cache fresh, we try to avoid using 155 * it as much as possible. 156 * Note : we can't use tasklets, because they can't be asynchronously 157 * killed (need user context), and we can't guarantee that here... 158 * Jean II 159 */ 160static void irttp_todo_expired(unsigned long data) 161{ 162 struct tsap_cb *self = (struct tsap_cb *) data; 163 164 /* Check that we still exist */ 165 if (!self || self->magic != TTP_TSAP_MAGIC) 166 return; 167 168 IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self); 169 170 /* Try to make some progress, especially on Tx side - Jean II */ 171 irttp_run_rx_queue(self); 172 irttp_run_tx_queue(self); 173 174 /* Check if time for disconnect */ 175 if (test_bit(0, &self->disconnect_pend)) { 176 /* Check if it's possible to disconnect yet */ 177 if (skb_queue_empty(&self->tx_queue)) { 178 /* Make sure disconnect is not pending anymore */ 179 clear_bit(0, &self->disconnect_pend); /* FALSE */ 180 181 /* Note : self->disconnect_skb may be NULL */ 182 irttp_disconnect_request(self, self->disconnect_skb, 183 P_NORMAL); 184 self->disconnect_skb = NULL; 185 } else { 186 /* Try again later */ 187 irttp_start_todo_timer(self, HZ/10); 188 189 /* No reason to try and close now */ 190 return; 191 } 192 } 193 194 /* Check if it's closing time */ 195 if (self->close_pend) 196 /* Finish cleanup */ 197 irttp_close_tsap(self); 198} 199 200/* 201 * Function irttp_flush_queues (self) 202 * 203 * Flushes (removes all frames) in transitt-buffer (tx_list) 204 */ 205static void irttp_flush_queues(struct tsap_cb *self) 206{ 207 struct sk_buff* skb; 208 209 IRDA_DEBUG(4, "%s()\n", __func__); 210 211 IRDA_ASSERT(self != NULL, return;); 212 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 213 214 /* Deallocate frames waiting to be sent */ 215 while ((skb = skb_dequeue(&self->tx_queue)) != NULL) 216 dev_kfree_skb(skb); 217 218 /* Deallocate received frames */ 219 while ((skb = skb_dequeue(&self->rx_queue)) != NULL) 220 dev_kfree_skb(skb); 221 222 /* Deallocate received fragments */ 223 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL) 224 dev_kfree_skb(skb); 225} 226 227/* 228 * Function irttp_reassemble (self) 229 * 230 * Makes a new (continuous) skb of all the fragments in the fragment 231 * queue 232 * 233 */ 234static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self) 235{ 236 struct sk_buff *skb, *frag; 237 int n = 0; /* Fragment index */ 238 239 IRDA_ASSERT(self != NULL, return NULL;); 240 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;); 241 242 IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __func__, 243 self->rx_sdu_size); 244 245 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size); 246 if (!skb) 247 return NULL; 248 249 /* 250 * Need to reserve space for TTP header in case this skb needs to 251 * be requeued in case delivery failes 252 */ 253 skb_reserve(skb, TTP_HEADER); 254 skb_put(skb, self->rx_sdu_size); 255 256 /* 257 * Copy all fragments to a new buffer 258 */ 259 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) { 260 skb_copy_to_linear_data_offset(skb, n, frag->data, frag->len); 261 n += frag->len; 262 263 dev_kfree_skb(frag); 264 } 265 266 IRDA_DEBUG(2, 267 "%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n", 268 __func__, n, self->rx_sdu_size, self->rx_max_sdu_size); 269 /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size 270 * by summing the size of all fragments, so we should always 271 * have n == self->rx_sdu_size, except in cases where we 272 * droped the last fragment (when self->rx_sdu_size exceed 273 * self->rx_max_sdu_size), where n < self->rx_sdu_size. 274 * Jean II */ 275 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;); 276 277 /* Set the new length */ 278 skb_trim(skb, n); 279 280 self->rx_sdu_size = 0; 281 282 return skb; 283} 284 285/* 286 * Function irttp_fragment_skb (skb) 287 * 288 * Fragments a frame and queues all the fragments for transmission 289 * 290 */ 291static inline void irttp_fragment_skb(struct tsap_cb *self, 292 struct sk_buff *skb) 293{ 294 struct sk_buff *frag; 295 __u8 *frame; 296 297 IRDA_DEBUG(2, "%s()\n", __func__); 298 299 IRDA_ASSERT(self != NULL, return;); 300 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 301 IRDA_ASSERT(skb != NULL, return;); 302 303 /* 304 * Split frame into a number of segments 305 */ 306 while (skb->len > self->max_seg_size) { 307 IRDA_DEBUG(2, "%s(), fragmenting ...\n", __func__); 308 309 /* Make new segment */ 310 frag = alloc_skb(self->max_seg_size+self->max_header_size, 311 GFP_ATOMIC); 312 if (!frag) 313 return; 314 315 skb_reserve(frag, self->max_header_size); 316 317 /* Copy data from the original skb into this fragment. */ 318 skb_copy_from_linear_data(skb, skb_put(frag, self->max_seg_size), 319 self->max_seg_size); 320 321 /* Insert TTP header, with the more bit set */ 322 frame = skb_push(frag, TTP_HEADER); 323 frame[0] = TTP_MORE; 324 325 /* Hide the copied data from the original skb */ 326 skb_pull(skb, self->max_seg_size); 327 328 /* Queue fragment */ 329 skb_queue_tail(&self->tx_queue, frag); 330 } 331 /* Queue what is left of the original skb */ 332 IRDA_DEBUG(2, "%s(), queuing last segment\n", __func__); 333 334 frame = skb_push(skb, TTP_HEADER); 335 frame[0] = 0x00; /* Clear more bit */ 336 337 /* Queue fragment */ 338 skb_queue_tail(&self->tx_queue, skb); 339} 340 341/* 342 * Function irttp_param_max_sdu_size (self, param) 343 * 344 * Handle the MaxSduSize parameter in the connect frames, this function 345 * will be called both when this parameter needs to be inserted into, and 346 * extracted from the connect frames 347 */ 348static int irttp_param_max_sdu_size(void *instance, irda_param_t *param, 349 int get) 350{ 351 struct tsap_cb *self; 352 353 self = (struct tsap_cb *) instance; 354 355 IRDA_ASSERT(self != NULL, return -1;); 356 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 357 358 if (get) 359 param->pv.i = self->tx_max_sdu_size; 360 else 361 self->tx_max_sdu_size = param->pv.i; 362 363 IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __func__, param->pv.i); 364 365 return 0; 366} 367 368/*************************** CLIENT CALLS ***************************/ 369/************************** LMP CALLBACKS **************************/ 370/* Everything is happily mixed up. Waiting for next clean up - Jean II */ 371 372/* 373 * Initialization, that has to be done on new tsap 374 * instance allocation and on duplication 375 */ 376static void irttp_init_tsap(struct tsap_cb *tsap) 377{ 378 spin_lock_init(&tsap->lock); 379 init_timer(&tsap->todo_timer); 380 381 skb_queue_head_init(&tsap->rx_queue); 382 skb_queue_head_init(&tsap->tx_queue); 383 skb_queue_head_init(&tsap->rx_fragments); 384} 385 386/* 387 * Function irttp_open_tsap (stsap, notify) 388 * 389 * Create TSAP connection endpoint, 390 */ 391struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify) 392{ 393 struct tsap_cb *self; 394 struct lsap_cb *lsap; 395 notify_t ttp_notify; 396 397 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;); 398 399 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to 400 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well. 401 * JeanII */ 402 if((stsap_sel != LSAP_ANY) && 403 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) { 404 IRDA_DEBUG(0, "%s(), invalid tsap!\n", __func__); 405 return NULL; 406 } 407 408 self = kzalloc(sizeof(struct tsap_cb), GFP_ATOMIC); 409 if (self == NULL) { 410 IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __func__); 411 return NULL; 412 } 413 414 /* Initialize internal objects */ 415 irttp_init_tsap(self); 416 417 /* Initialise todo timer */ 418 self->todo_timer.data = (unsigned long) self; 419 self->todo_timer.function = &irttp_todo_expired; 420 421 /* Initialize callbacks for IrLMP to use */ 422 irda_notify_init(&ttp_notify); 423 ttp_notify.connect_confirm = irttp_connect_confirm; 424 ttp_notify.connect_indication = irttp_connect_indication; 425 ttp_notify.disconnect_indication = irttp_disconnect_indication; 426 ttp_notify.data_indication = irttp_data_indication; 427 ttp_notify.udata_indication = irttp_udata_indication; 428 ttp_notify.flow_indication = irttp_flow_indication; 429 if(notify->status_indication != NULL) 430 ttp_notify.status_indication = irttp_status_indication; 431 ttp_notify.instance = self; 432 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME); 433 434 self->magic = TTP_TSAP_MAGIC; 435 self->connected = FALSE; 436 437 /* 438 * Create LSAP at IrLMP layer 439 */ 440 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0); 441 if (lsap == NULL) { 442 IRDA_WARNING("%s: unable to allocate LSAP!!\n", __func__); 443 return NULL; 444 } 445 446 /* 447 * If user specified LSAP_ANY as source TSAP selector, then IrLMP 448 * will replace it with whatever source selector which is free, so 449 * the stsap_sel we have might not be valid anymore 450 */ 451 self->stsap_sel = lsap->slsap_sel; 452 IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __func__, self->stsap_sel); 453 454 self->notify = *notify; 455 self->lsap = lsap; 456 457 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL); 458 459 if (credit > TTP_RX_MAX_CREDIT) 460 self->initial_credit = TTP_RX_MAX_CREDIT; 461 else 462 self->initial_credit = credit; 463 464 return self; 465} 466EXPORT_SYMBOL(irttp_open_tsap); 467 468/* 469 * Function irttp_close (handle) 470 * 471 * Remove an instance of a TSAP. This function should only deal with the 472 * deallocation of the TSAP, and resetting of the TSAPs values; 473 * 474 */ 475static void __irttp_close_tsap(struct tsap_cb *self) 476{ 477 /* First make sure we're connected. */ 478 IRDA_ASSERT(self != NULL, return;); 479 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 480 481 irttp_flush_queues(self); 482 483 del_timer(&self->todo_timer); 484 485 /* This one won't be cleaned up if we are disconnect_pend + close_pend 486 * and we receive a disconnect_indication */ 487 if (self->disconnect_skb) 488 dev_kfree_skb(self->disconnect_skb); 489 490 self->connected = FALSE; 491 self->magic = ~TTP_TSAP_MAGIC; 492 493 kfree(self); 494} 495 496/* 497 * Function irttp_close (self) 498 * 499 * Remove TSAP from list of all TSAPs and then deallocate all resources 500 * associated with this TSAP 501 * 502 * Note : because we *free* the tsap structure, it is the responsibility 503 * of the caller to make sure we are called only once and to deal with 504 * possible race conditions. - Jean II 505 */ 506int irttp_close_tsap(struct tsap_cb *self) 507{ 508 struct tsap_cb *tsap; 509 510 IRDA_DEBUG(4, "%s()\n", __func__); 511 512 IRDA_ASSERT(self != NULL, return -1;); 513 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 514 515 /* Make sure tsap has been disconnected */ 516 if (self->connected) { 517 /* Check if disconnect is not pending */ 518 if (!test_bit(0, &self->disconnect_pend)) { 519 IRDA_WARNING("%s: TSAP still connected!\n", 520 __func__); 521 irttp_disconnect_request(self, NULL, P_NORMAL); 522 } 523 self->close_pend = TRUE; 524 irttp_start_todo_timer(self, HZ/10); 525 526 return 0; /* Will be back! */ 527 } 528 529 tsap = hashbin_remove(irttp->tsaps, (long) self, NULL); 530 531 IRDA_ASSERT(tsap == self, return -1;); 532 533 /* Close corresponding LSAP */ 534 if (self->lsap) { 535 irlmp_close_lsap(self->lsap); 536 self->lsap = NULL; 537 } 538 539 __irttp_close_tsap(self); 540 541 return 0; 542} 543EXPORT_SYMBOL(irttp_close_tsap); 544 545/* 546 * Function irttp_udata_request (self, skb) 547 * 548 * Send unreliable data on this TSAP 549 * 550 */ 551int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb) 552{ 553 IRDA_ASSERT(self != NULL, return -1;); 554 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 555 IRDA_ASSERT(skb != NULL, return -1;); 556 557 IRDA_DEBUG(4, "%s()\n", __func__); 558 559 /* Check that nothing bad happens */ 560 if ((skb->len == 0) || (!self->connected)) { 561 IRDA_DEBUG(1, "%s(), No data, or not connected\n", 562 __func__); 563 goto err; 564 } 565 566 if (skb->len > self->max_seg_size) { 567 IRDA_DEBUG(1, "%s(), UData is too large for IrLAP!\n", 568 __func__); 569 goto err; 570 } 571 572 irlmp_udata_request(self->lsap, skb); 573 self->stats.tx_packets++; 574 575 return 0; 576 577err: 578 dev_kfree_skb(skb); 579 return -1; 580} 581EXPORT_SYMBOL(irttp_udata_request); 582 583 584/* 585 * Function irttp_data_request (handle, skb) 586 * 587 * Queue frame for transmission. If SAR is enabled, fragement the frame 588 * and queue the fragments for transmission 589 */ 590int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb) 591{ 592 __u8 *frame; 593 int ret; 594 595 IRDA_ASSERT(self != NULL, return -1;); 596 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 597 IRDA_ASSERT(skb != NULL, return -1;); 598 599 IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__, 600 skb_queue_len(&self->tx_queue)); 601 602 /* Check that nothing bad happens */ 603 if ((skb->len == 0) || (!self->connected)) { 604 IRDA_WARNING("%s: No data, or not connected\n", __func__); 605 ret = -ENOTCONN; 606 goto err; 607 } 608 609 /* 610 * Check if SAR is disabled, and the frame is larger than what fits 611 * inside an IrLAP frame 612 */ 613 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) { 614 IRDA_ERROR("%s: SAR disabled, and data is too large for IrLAP!\n", 615 __func__); 616 ret = -EMSGSIZE; 617 goto err; 618 } 619 620 /* 621 * Check if SAR is enabled, and the frame is larger than the 622 * TxMaxSduSize 623 */ 624 if ((self->tx_max_sdu_size != 0) && 625 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) && 626 (skb->len > self->tx_max_sdu_size)) 627 { 628 IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n", 629 __func__); 630 ret = -EMSGSIZE; 631 goto err; 632 } 633 /* 634 * Check if transmit queue is full 635 */ 636 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) { 637 /* 638 * Give it a chance to empty itself 639 */ 640 irttp_run_tx_queue(self); 641 642 /* Drop packet. This error code should trigger the caller 643 * to resend the data in the client code - Jean II */ 644 ret = -ENOBUFS; 645 goto err; 646 } 647 648 /* Queue frame, or queue frame segments */ 649 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) { 650 /* Queue frame */ 651 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;); 652 frame = skb_push(skb, TTP_HEADER); 653 frame[0] = 0x00; /* Clear more bit */ 654 655 skb_queue_tail(&self->tx_queue, skb); 656 } else { 657 /* 658 * Fragment the frame, this function will also queue the 659 * fragments, we don't care about the fact the transmit 660 * queue may be overfilled by all the segments for a little 661 * while 662 */ 663 irttp_fragment_skb(self, skb); 664 } 665 666 /* Check if we can accept more data from client */ 667 if ((!self->tx_sdu_busy) && 668 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) { 669 /* Tx queue filling up, so stop client. */ 670 if (self->notify.flow_indication) { 671 self->notify.flow_indication(self->notify.instance, 672 self, FLOW_STOP); 673 } 674 /* self->tx_sdu_busy is the state of the client. 675 * Update state after notifying client to avoid 676 * race condition with irttp_flow_indication(). 677 * If the queue empty itself after our test but before 678 * we set the flag, we will fix ourselves below in 679 * irttp_run_tx_queue(). 680 * Jean II */ 681 self->tx_sdu_busy = TRUE; 682 } 683 684 /* Try to make some progress */ 685 irttp_run_tx_queue(self); 686 687 return 0; 688 689err: 690 dev_kfree_skb(skb); 691 return ret; 692} 693EXPORT_SYMBOL(irttp_data_request); 694 695/* 696 * Function irttp_run_tx_queue (self) 697 * 698 * Transmit packets queued for transmission (if possible) 699 * 700 */ 701static void irttp_run_tx_queue(struct tsap_cb *self) 702{ 703 struct sk_buff *skb; 704 unsigned long flags; 705 int n; 706 707 IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n", 708 __func__, 709 self->send_credit, skb_queue_len(&self->tx_queue)); 710 711 /* Get exclusive access to the tx queue, otherwise don't touch it */ 712 if (irda_lock(&self->tx_queue_lock) == FALSE) 713 return; 714 715 /* Try to send out frames as long as we have credits 716 * and as long as LAP is not full. If LAP is full, it will 717 * poll us through irttp_flow_indication() - Jean II */ 718 while ((self->send_credit > 0) && 719 (!irlmp_lap_tx_queue_full(self->lsap)) && 720 (skb = skb_dequeue(&self->tx_queue))) 721 { 722 /* 723 * Since we can transmit and receive frames concurrently, 724 * the code below is a critical region and we must assure that 725 * nobody messes with the credits while we update them. 726 */ 727 spin_lock_irqsave(&self->lock, flags); 728 729 n = self->avail_credit; 730 self->avail_credit = 0; 731 732 /* Only room for 127 credits in frame */ 733 if (n > 127) { 734 self->avail_credit = n-127; 735 n = 127; 736 } 737 self->remote_credit += n; 738 self->send_credit--; 739 740 spin_unlock_irqrestore(&self->lock, flags); 741 742 /* 743 * More bit must be set by the data_request() or fragment() 744 * functions 745 */ 746 skb->data[0] |= (n & 0x7f); 747 748 /* Detach from socket. 749 * The current skb has a reference to the socket that sent 750 * it (skb->sk). When we pass it to IrLMP, the skb will be 751 * stored in in IrLAP (self->wx_list). When we are within 752 * IrLAP, we lose the notion of socket, so we should not 753 * have a reference to a socket. So, we drop it here. 754 * 755 * Why does it matter ? 756 * When the skb is freed (kfree_skb), if it is associated 757 * with a socket, it release buffer space on the socket 758 * (through sock_wfree() and sock_def_write_space()). 759 * If the socket no longer exist, we may crash. Hard. 760 * When we close a socket, we make sure that associated packets 761 * in IrTTP are freed. However, we have no way to cancel 762 * the packet that we have passed to IrLAP. So, if a packet 763 * remains in IrLAP (retry on the link or else) after we 764 * close the socket, we are dead ! 765 * Jean II */ 766 if (skb->sk != NULL) { 767 /* IrSOCK application, IrOBEX, ... */ 768 skb_orphan(skb); 769 } 770 /* IrCOMM over IrTTP, IrLAN, ... */ 771 772 /* Pass the skb to IrLMP - done */ 773 irlmp_data_request(self->lsap, skb); 774 self->stats.tx_packets++; 775 } 776 777 /* Check if we can accept more frames from client. 778 * We don't want to wait until the todo timer to do that, and we 779 * can't use tasklets (grr...), so we are obliged to give control 780 * to client. That's ok, this test will be true not too often 781 * (max once per LAP window) and we are called from places 782 * where we can spend a bit of time doing stuff. - Jean II */ 783 if ((self->tx_sdu_busy) && 784 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) && 785 (!self->close_pend)) 786 { 787 if (self->notify.flow_indication) 788 self->notify.flow_indication(self->notify.instance, 789 self, FLOW_START); 790 791 /* self->tx_sdu_busy is the state of the client. 792 * We don't really have a race here, but it's always safer 793 * to update our state after the client - Jean II */ 794 self->tx_sdu_busy = FALSE; 795 } 796 797 /* Reset lock */ 798 self->tx_queue_lock = 0; 799} 800 801/* 802 * Function irttp_give_credit (self) 803 * 804 * Send a dataless flowdata TTP-PDU and give available credit to peer 805 * TSAP 806 */ 807static inline void irttp_give_credit(struct tsap_cb *self) 808{ 809 struct sk_buff *tx_skb = NULL; 810 unsigned long flags; 811 int n; 812 813 IRDA_ASSERT(self != NULL, return;); 814 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 815 816 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", 817 __func__, 818 self->send_credit, self->avail_credit, self->remote_credit); 819 820 /* Give credit to peer */ 821 tx_skb = alloc_skb(TTP_MAX_HEADER, GFP_ATOMIC); 822 if (!tx_skb) 823 return; 824 825 /* Reserve space for LMP, and LAP header */ 826 skb_reserve(tx_skb, LMP_MAX_HEADER); 827 828 /* 829 * Since we can transmit and receive frames concurrently, 830 * the code below is a critical region and we must assure that 831 * nobody messes with the credits while we update them. 832 */ 833 spin_lock_irqsave(&self->lock, flags); 834 835 n = self->avail_credit; 836 self->avail_credit = 0; 837 838 /* Only space for 127 credits in frame */ 839 if (n > 127) { 840 self->avail_credit = n - 127; 841 n = 127; 842 } 843 self->remote_credit += n; 844 845 spin_unlock_irqrestore(&self->lock, flags); 846 847 skb_put(tx_skb, 1); 848 tx_skb->data[0] = (__u8) (n & 0x7f); 849 850 irlmp_data_request(self->lsap, tx_skb); 851 self->stats.tx_packets++; 852} 853 854/* 855 * Function irttp_udata_indication (instance, sap, skb) 856 * 857 * Received some unit-data (unreliable) 858 * 859 */ 860static int irttp_udata_indication(void *instance, void *sap, 861 struct sk_buff *skb) 862{ 863 struct tsap_cb *self; 864 int err; 865 866 IRDA_DEBUG(4, "%s()\n", __func__); 867 868 self = (struct tsap_cb *) instance; 869 870 IRDA_ASSERT(self != NULL, return -1;); 871 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 872 IRDA_ASSERT(skb != NULL, return -1;); 873 874 self->stats.rx_packets++; 875 876 /* Just pass data to layer above */ 877 if (self->notify.udata_indication) { 878 err = self->notify.udata_indication(self->notify.instance, 879 self,skb); 880 /* Same comment as in irttp_do_data_indication() */ 881 if (!err) 882 return 0; 883 } 884 /* Either no handler, or handler returns an error */ 885 dev_kfree_skb(skb); 886 887 return 0; 888} 889 890/* 891 * Function irttp_data_indication (instance, sap, skb) 892 * 893 * Receive segment from IrLMP. 894 * 895 */ 896static int irttp_data_indication(void *instance, void *sap, 897 struct sk_buff *skb) 898{ 899 struct tsap_cb *self; 900 unsigned long flags; 901 int n; 902 903 self = (struct tsap_cb *) instance; 904 905 n = skb->data[0] & 0x7f; /* Extract the credits */ 906 907 self->stats.rx_packets++; 908 909 /* Deal with inbound credit 910 * Since we can transmit and receive frames concurrently, 911 * the code below is a critical region and we must assure that 912 * nobody messes with the credits while we update them. 913 */ 914 spin_lock_irqsave(&self->lock, flags); 915 self->send_credit += n; 916 if (skb->len > 1) 917 self->remote_credit--; 918 spin_unlock_irqrestore(&self->lock, flags); 919 920 /* 921 * Data or dataless packet? Dataless frames contains only the 922 * TTP_HEADER. 923 */ 924 if (skb->len > 1) { 925 /* 926 * We don't remove the TTP header, since we must preserve the 927 * more bit, so the defragment routing knows what to do 928 */ 929 skb_queue_tail(&self->rx_queue, skb); 930 } else { 931 /* Dataless flowdata TTP-PDU */ 932 dev_kfree_skb(skb); 933 } 934 935 936 /* Push data to the higher layer. 937 * We do it synchronously because running the todo timer for each 938 * receive packet would be too much overhead and latency. 939 * By passing control to the higher layer, we run the risk that 940 * it may take time or grab a lock. Most often, the higher layer 941 * will only put packet in a queue. 942 * Anyway, packets are only dripping through the IrDA, so we can 943 * have time before the next packet. 944 * Further, we are run from NET_BH, so the worse that can happen is 945 * us missing the optimal time to send back the PF bit in LAP. 946 * Jean II */ 947 irttp_run_rx_queue(self); 948 949 /* We now give credits to peer in irttp_run_rx_queue(). 950 * We need to send credit *NOW*, otherwise we are going 951 * to miss the next Tx window. The todo timer may take 952 * a while before it's run... - Jean II */ 953 954 /* 955 * If the peer device has given us some credits and we didn't have 956 * anyone from before, then we need to shedule the tx queue. 957 * We need to do that because our Tx have stopped (so we may not 958 * get any LAP flow indication) and the user may be stopped as 959 * well. - Jean II 960 */ 961 if (self->send_credit == n) { 962 /* Restart pushing stuff to LAP */ 963 irttp_run_tx_queue(self); 964 /* Note : we don't want to schedule the todo timer 965 * because it has horrible latency. No tasklets 966 * because the tasklet API is broken. - Jean II */ 967 } 968 969 return 0; 970} 971 972/* 973 * Function irttp_status_indication (self, reason) 974 * 975 * Status_indication, just pass to the higher layer... 976 * 977 */ 978static void irttp_status_indication(void *instance, 979 LINK_STATUS link, LOCK_STATUS lock) 980{ 981 struct tsap_cb *self; 982 983 IRDA_DEBUG(4, "%s()\n", __func__); 984 985 self = (struct tsap_cb *) instance; 986 987 IRDA_ASSERT(self != NULL, return;); 988 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 989 990 /* Check if client has already closed the TSAP and gone away */ 991 if (self->close_pend) 992 return; 993 994 /* 995 * Inform service user if he has requested it 996 */ 997 if (self->notify.status_indication != NULL) 998 self->notify.status_indication(self->notify.instance, 999 link, lock); 1000 else 1001 IRDA_DEBUG(2, "%s(), no handler\n", __func__); 1002} 1003 1004/* 1005 * Function irttp_flow_indication (self, reason) 1006 * 1007 * Flow_indication : IrLAP tells us to send more data. 1008 * 1009 */ 1010static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow) 1011{ 1012 struct tsap_cb *self; 1013 1014 self = (struct tsap_cb *) instance; 1015 1016 IRDA_ASSERT(self != NULL, return;); 1017 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 1018 1019 IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self); 1020 1021 /* We are "polled" directly from LAP, and the LAP want to fill 1022 * its Tx window. We want to do our best to send it data, so that 1023 * we maximise the window. On the other hand, we want to limit the 1024 * amount of work here so that LAP doesn't hang forever waiting 1025 * for packets. - Jean II */ 1026 1027 /* Try to send some packets. Currently, LAP calls us every time 1028 * there is one free slot, so we will send only one packet. 1029 * This allow the scheduler to do its round robin - Jean II */ 1030 irttp_run_tx_queue(self); 1031 1032 /* Note regarding the interraction with higher layer. 1033 * irttp_run_tx_queue() may call the client when its queue 1034 * start to empty, via notify.flow_indication(). Initially. 1035 * I wanted this to happen in a tasklet, to avoid client 1036 * grabbing the CPU, but we can't use tasklets safely. And timer 1037 * is definitely too slow. 1038 * This will happen only once per LAP window, and usually at 1039 * the third packet (unless window is smaller). LAP is still 1040 * doing mtt and sending first packet so it's sort of OK 1041 * to do that. Jean II */ 1042 1043 /* If we need to send disconnect. try to do it now */ 1044 if(self->disconnect_pend) 1045 irttp_start_todo_timer(self, 0); 1046} 1047 1048/* 1049 * Function irttp_flow_request (self, command) 1050 * 1051 * This function could be used by the upper layers to tell IrTTP to stop 1052 * delivering frames if the receive queues are starting to get full, or 1053 * to tell IrTTP to start delivering frames again. 1054 */ 1055void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow) 1056{ 1057 IRDA_DEBUG(1, "%s()\n", __func__); 1058 1059 IRDA_ASSERT(self != NULL, return;); 1060 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 1061 1062 switch (flow) { 1063 case FLOW_STOP: 1064 IRDA_DEBUG(1, "%s(), flow stop\n", __func__); 1065 self->rx_sdu_busy = TRUE; 1066 break; 1067 case FLOW_START: 1068 IRDA_DEBUG(1, "%s(), flow start\n", __func__); 1069 self->rx_sdu_busy = FALSE; 1070 1071 /* Client say he can accept more data, try to free our 1072 * queues ASAP - Jean II */ 1073 irttp_run_rx_queue(self); 1074 1075 break; 1076 default: 1077 IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __func__); 1078 } 1079} 1080EXPORT_SYMBOL(irttp_flow_request); 1081 1082/* 1083 * Function irttp_connect_request (self, dtsap_sel, daddr, qos) 1084 * 1085 * Try to connect to remote destination TSAP selector 1086 * 1087 */ 1088int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel, 1089 __u32 saddr, __u32 daddr, 1090 struct qos_info *qos, __u32 max_sdu_size, 1091 struct sk_buff *userdata) 1092{ 1093 struct sk_buff *tx_skb; 1094 __u8 *frame; 1095 __u8 n; 1096 1097 IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __func__, max_sdu_size); 1098 1099 IRDA_ASSERT(self != NULL, return -EBADR;); 1100 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;); 1101 1102 if (self->connected) { 1103 if(userdata) 1104 dev_kfree_skb(userdata); 1105 return -EISCONN; 1106 } 1107 1108 /* Any userdata supplied? */ 1109 if (userdata == NULL) { 1110 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER, 1111 GFP_ATOMIC); 1112 if (!tx_skb) 1113 return -ENOMEM; 1114 1115 /* Reserve space for MUX_CONTROL and LAP header */ 1116 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER); 1117 } else { 1118 tx_skb = userdata; 1119 /* 1120 * Check that the client has reserved enough space for 1121 * headers 1122 */ 1123 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER, 1124 { dev_kfree_skb(userdata); return -1; } ); 1125 } 1126 1127 /* Initialize connection parameters */ 1128 self->connected = FALSE; 1129 self->avail_credit = 0; 1130 self->rx_max_sdu_size = max_sdu_size; 1131 self->rx_sdu_size = 0; 1132 self->rx_sdu_busy = FALSE; 1133 self->dtsap_sel = dtsap_sel; 1134 1135 n = self->initial_credit; 1136 1137 self->remote_credit = 0; 1138 self->send_credit = 0; 1139 1140 /* 1141 * Give away max 127 credits for now 1142 */ 1143 if (n > 127) { 1144 self->avail_credit=n-127; 1145 n = 127; 1146 } 1147 1148 self->remote_credit = n; 1149 1150 /* SAR enabled? */ 1151 if (max_sdu_size > 0) { 1152 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER), 1153 { dev_kfree_skb(tx_skb); return -1; } ); 1154 1155 /* Insert SAR parameters */ 1156 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER); 1157 1158 frame[0] = TTP_PARAMETERS | n; 1159 frame[1] = 0x04; /* Length */ 1160 frame[2] = 0x01; /* MaxSduSize */ 1161 frame[3] = 0x02; /* Value length */ 1162 1163 put_unaligned(cpu_to_be16((__u16) max_sdu_size), 1164 (__be16 *)(frame+4)); 1165 } else { 1166 /* Insert plain TTP header */ 1167 frame = skb_push(tx_skb, TTP_HEADER); 1168 1169 /* Insert initial credit in frame */ 1170 frame[0] = n & 0x7f; 1171 } 1172 1173 /* Connect with IrLMP. No QoS parameters for now */ 1174 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos, 1175 tx_skb); 1176} 1177EXPORT_SYMBOL(irttp_connect_request); 1178 1179/* 1180 * Function irttp_connect_confirm (handle, qos, skb) 1181 * 1182 * Sevice user confirms TSAP connection with peer. 1183 * 1184 */ 1185static void irttp_connect_confirm(void *instance, void *sap, 1186 struct qos_info *qos, __u32 max_seg_size, 1187 __u8 max_header_size, struct sk_buff *skb) 1188{ 1189 struct tsap_cb *self; 1190 int parameters; 1191 int ret; 1192 __u8 plen; 1193 __u8 n; 1194 1195 IRDA_DEBUG(4, "%s()\n", __func__); 1196 1197 self = (struct tsap_cb *) instance; 1198 1199 IRDA_ASSERT(self != NULL, return;); 1200 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 1201 IRDA_ASSERT(skb != NULL, return;); 1202 1203 self->max_seg_size = max_seg_size - TTP_HEADER; 1204 self->max_header_size = max_header_size + TTP_HEADER; 1205 1206 /* 1207 * Check if we have got some QoS parameters back! This should be the 1208 * negotiated QoS for the link. 1209 */ 1210 if (qos) { 1211 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n", 1212 qos->baud_rate.bits); 1213 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n", 1214 qos->baud_rate.value); 1215 } 1216 1217 n = skb->data[0] & 0x7f; 1218 1219 IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __func__, n); 1220 1221 self->send_credit = n; 1222 self->tx_max_sdu_size = 0; 1223 self->connected = TRUE; 1224 1225 parameters = skb->data[0] & 0x80; 1226 1227 IRDA_ASSERT(skb->len >= TTP_HEADER, return;); 1228 skb_pull(skb, TTP_HEADER); 1229 1230 if (parameters) { 1231 plen = skb->data[0]; 1232 1233 ret = irda_param_extract_all(self, skb->data+1, 1234 IRDA_MIN(skb->len-1, plen), 1235 ¶m_info); 1236 1237 /* Any errors in the parameter list? */ 1238 if (ret < 0) { 1239 IRDA_WARNING("%s: error extracting parameters\n", 1240 __func__); 1241 dev_kfree_skb(skb); 1242 1243 /* Do not accept this connection attempt */ 1244 return; 1245 } 1246 /* Remove parameters */ 1247 skb_pull(skb, IRDA_MIN(skb->len, plen+1)); 1248 } 1249 1250 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __func__, 1251 self->send_credit, self->avail_credit, self->remote_credit); 1252 1253 IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __func__, 1254 self->tx_max_sdu_size); 1255 1256 if (self->notify.connect_confirm) { 1257 self->notify.connect_confirm(self->notify.instance, self, qos, 1258 self->tx_max_sdu_size, 1259 self->max_header_size, skb); 1260 } else 1261 dev_kfree_skb(skb); 1262} 1263 1264/* 1265 * Function irttp_connect_indication (handle, skb) 1266 * 1267 * Some other device is connecting to this TSAP 1268 * 1269 */ 1270static void irttp_connect_indication(void *instance, void *sap, 1271 struct qos_info *qos, __u32 max_seg_size, __u8 max_header_size, 1272 struct sk_buff *skb) 1273{ 1274 struct tsap_cb *self; 1275 struct lsap_cb *lsap; 1276 int parameters; 1277 int ret; 1278 __u8 plen; 1279 __u8 n; 1280 1281 self = (struct tsap_cb *) instance; 1282 1283 IRDA_ASSERT(self != NULL, return;); 1284 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 1285 IRDA_ASSERT(skb != NULL, return;); 1286 1287 lsap = (struct lsap_cb *) sap; 1288 1289 self->max_seg_size = max_seg_size - TTP_HEADER; 1290 self->max_header_size = max_header_size+TTP_HEADER; 1291 1292 IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __func__, self->stsap_sel); 1293 1294 /* Need to update dtsap_sel if its equal to LSAP_ANY */ 1295 self->dtsap_sel = lsap->dlsap_sel; 1296 1297 n = skb->data[0] & 0x7f; 1298 1299 self->send_credit = n; 1300 self->tx_max_sdu_size = 0; 1301 1302 parameters = skb->data[0] & 0x80; 1303 1304 IRDA_ASSERT(skb->len >= TTP_HEADER, return;); 1305 skb_pull(skb, TTP_HEADER); 1306 1307 if (parameters) { 1308 plen = skb->data[0]; 1309 1310 ret = irda_param_extract_all(self, skb->data+1, 1311 IRDA_MIN(skb->len-1, plen), 1312 ¶m_info); 1313 1314 /* Any errors in the parameter list? */ 1315 if (ret < 0) { 1316 IRDA_WARNING("%s: error extracting parameters\n", 1317 __func__); 1318 dev_kfree_skb(skb); 1319 1320 /* Do not accept this connection attempt */ 1321 return; 1322 } 1323 1324 /* Remove parameters */ 1325 skb_pull(skb, IRDA_MIN(skb->len, plen+1)); 1326 } 1327 1328 if (self->notify.connect_indication) { 1329 self->notify.connect_indication(self->notify.instance, self, 1330 qos, self->tx_max_sdu_size, 1331 self->max_header_size, skb); 1332 } else 1333 dev_kfree_skb(skb); 1334} 1335 1336/* 1337 * Function irttp_connect_response (handle, userdata) 1338 * 1339 * Service user is accepting the connection, just pass it down to 1340 * IrLMP! 1341 * 1342 */ 1343int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size, 1344 struct sk_buff *userdata) 1345{ 1346 struct sk_buff *tx_skb; 1347 __u8 *frame; 1348 int ret; 1349 __u8 n; 1350 1351 IRDA_ASSERT(self != NULL, return -1;); 1352 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 1353 1354 IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __func__, 1355 self->stsap_sel); 1356 1357 /* Any userdata supplied? */ 1358 if (userdata == NULL) { 1359 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER, 1360 GFP_ATOMIC); 1361 if (!tx_skb) 1362 return -ENOMEM; 1363 1364 /* Reserve space for MUX_CONTROL and LAP header */ 1365 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER); 1366 } else { 1367 tx_skb = userdata; 1368 /* 1369 * Check that the client has reserved enough space for 1370 * headers 1371 */ 1372 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER, 1373 { dev_kfree_skb(userdata); return -1; } ); 1374 } 1375 1376 self->avail_credit = 0; 1377 self->remote_credit = 0; 1378 self->rx_max_sdu_size = max_sdu_size; 1379 self->rx_sdu_size = 0; 1380 self->rx_sdu_busy = FALSE; 1381 1382 n = self->initial_credit; 1383 1384 /* Frame has only space for max 127 credits (7 bits) */ 1385 if (n > 127) { 1386 self->avail_credit = n - 127; 1387 n = 127; 1388 } 1389 1390 self->remote_credit = n; 1391 self->connected = TRUE; 1392 1393 /* SAR enabled? */ 1394 if (max_sdu_size > 0) { 1395 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER), 1396 { dev_kfree_skb(tx_skb); return -1; } ); 1397 1398 /* Insert TTP header with SAR parameters */ 1399 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER); 1400 1401 frame[0] = TTP_PARAMETERS | n; 1402 frame[1] = 0x04; /* Length */ 1403 1404 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */ 1405/* TTP_SAR_HEADER, ¶m_info) */ 1406 1407 frame[2] = 0x01; /* MaxSduSize */ 1408 frame[3] = 0x02; /* Value length */ 1409 1410 put_unaligned(cpu_to_be16((__u16) max_sdu_size), 1411 (__be16 *)(frame+4)); 1412 } else { 1413 /* Insert TTP header */ 1414 frame = skb_push(tx_skb, TTP_HEADER); 1415 1416 frame[0] = n & 0x7f; 1417 } 1418 1419 ret = irlmp_connect_response(self->lsap, tx_skb); 1420 1421 return ret; 1422} 1423EXPORT_SYMBOL(irttp_connect_response); 1424 1425/* 1426 * Function irttp_dup (self, instance) 1427 * 1428 * Duplicate TSAP, can be used by servers to confirm a connection on a 1429 * new TSAP so it can keep listening on the old one. 1430 */ 1431struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance) 1432{ 1433 struct tsap_cb *new; 1434 unsigned long flags; 1435 1436 IRDA_DEBUG(1, "%s()\n", __func__); 1437 1438 /* Protect our access to the old tsap instance */ 1439 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags); 1440 1441 /* Find the old instance */ 1442 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) { 1443 IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __func__); 1444 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags); 1445 return NULL; 1446 } 1447 1448 /* Allocate a new instance */ 1449 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC); 1450 if (!new) { 1451 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __func__); 1452 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags); 1453 return NULL; 1454 } 1455 /* Dup */ 1456 memcpy(new, orig, sizeof(struct tsap_cb)); 1457 spin_lock_init(&new->lock); 1458 1459 /* We don't need the old instance any more */ 1460 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags); 1461 1462 /* Try to dup the LSAP (may fail if we were too slow) */ 1463 new->lsap = irlmp_dup(orig->lsap, new); 1464 if (!new->lsap) { 1465 IRDA_DEBUG(0, "%s(), dup failed!\n", __func__); 1466 kfree(new); 1467 return NULL; 1468 } 1469 1470 /* Not everything should be copied */ 1471 new->notify.instance = instance; 1472 1473 /* Initialize internal objects */ 1474 irttp_init_tsap(new); 1475 1476 /* This is locked */ 1477 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL); 1478 1479 return new; 1480} 1481EXPORT_SYMBOL(irttp_dup); 1482 1483/* 1484 * Function irttp_disconnect_request (self) 1485 * 1486 * Close this connection please! If priority is high, the queued data 1487 * segments, if any, will be deallocated first 1488 * 1489 */ 1490int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata, 1491 int priority) 1492{ 1493 int ret; 1494 1495 IRDA_ASSERT(self != NULL, return -1;); 1496 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;); 1497 1498 /* Already disconnected? */ 1499 if (!self->connected) { 1500 IRDA_DEBUG(4, "%s(), already disconnected!\n", __func__); 1501 if (userdata) 1502 dev_kfree_skb(userdata); 1503 return -1; 1504 } 1505 1506 /* Disconnect already pending ? 1507 * We need to use an atomic operation to prevent reentry. This 1508 * function may be called from various context, like user, timer 1509 * for following a disconnect_indication() (i.e. net_bh). 1510 * Jean II */ 1511 if(test_and_set_bit(0, &self->disconnect_pend)) { 1512 IRDA_DEBUG(0, "%s(), disconnect already pending\n", 1513 __func__); 1514 if (userdata) 1515 dev_kfree_skb(userdata); 1516 1517 /* Try to make some progress */ 1518 irttp_run_tx_queue(self); 1519 return -1; 1520 } 1521 1522 /* 1523 * Check if there is still data segments in the transmit queue 1524 */ 1525 if (!skb_queue_empty(&self->tx_queue)) { 1526 if (priority == P_HIGH) { 1527 /* 1528 * No need to send the queued data, if we are 1529 * disconnecting right now since the data will 1530 * not have any usable connection to be sent on 1531 */ 1532 IRDA_DEBUG(1, "%s(): High priority!!()\n", __func__); 1533 irttp_flush_queues(self); 1534 } else if (priority == P_NORMAL) { 1535 /* 1536 * Must delay disconnect until after all data segments 1537 * have been sent and the tx_queue is empty 1538 */ 1539 /* We'll reuse this one later for the disconnect */ 1540 self->disconnect_skb = userdata; /* May be NULL */ 1541 1542 irttp_run_tx_queue(self); 1543 1544 irttp_start_todo_timer(self, HZ/10); 1545 return -1; 1546 } 1547 } 1548 /* Note : we don't need to check if self->rx_queue is full and the 1549 * state of self->rx_sdu_busy because the disconnect response will 1550 * be sent at the LMP level (so even if the peer has its Tx queue 1551 * full of data). - Jean II */ 1552 1553 IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __func__); 1554 self->connected = FALSE; 1555 1556 if (!userdata) { 1557 struct sk_buff *tx_skb; 1558 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC); 1559 if (!tx_skb) 1560 return -ENOMEM; 1561 1562 /* 1563 * Reserve space for MUX and LAP header 1564 */ 1565 skb_reserve(tx_skb, LMP_MAX_HEADER); 1566 1567 userdata = tx_skb; 1568 } 1569 ret = irlmp_disconnect_request(self->lsap, userdata); 1570 1571 /* The disconnect is no longer pending */ 1572 clear_bit(0, &self->disconnect_pend); /* FALSE */ 1573 1574 return ret; 1575} 1576EXPORT_SYMBOL(irttp_disconnect_request); 1577 1578/* 1579 * Function irttp_disconnect_indication (self, reason) 1580 * 1581 * Disconnect indication, TSAP disconnected by peer? 1582 * 1583 */ 1584static void irttp_disconnect_indication(void *instance, void *sap, 1585 LM_REASON reason, struct sk_buff *skb) 1586{ 1587 struct tsap_cb *self; 1588 1589 IRDA_DEBUG(4, "%s()\n", __func__); 1590 1591 self = (struct tsap_cb *) instance; 1592 1593 IRDA_ASSERT(self != NULL, return;); 1594 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 1595 1596 /* Prevent higher layer to send more data */ 1597 self->connected = FALSE; 1598 1599 /* Check if client has already tried to close the TSAP */ 1600 if (self->close_pend) { 1601 /* In this case, the higher layer is probably gone. Don't 1602 * bother it and clean up the remains - Jean II */ 1603 if (skb) 1604 dev_kfree_skb(skb); 1605 irttp_close_tsap(self); 1606 return; 1607 } 1608 1609 /* If we are here, we assume that is the higher layer is still 1610 * waiting for the disconnect notification and able to process it, 1611 * even if he tried to disconnect. Otherwise, it would have already 1612 * attempted to close the tsap and self->close_pend would be TRUE. 1613 * Jean II */ 1614 1615 /* No need to notify the client if has already tried to disconnect */ 1616 if(self->notify.disconnect_indication) 1617 self->notify.disconnect_indication(self->notify.instance, self, 1618 reason, skb); 1619 else 1620 if (skb) 1621 dev_kfree_skb(skb); 1622} 1623 1624/* 1625 * Function irttp_do_data_indication (self, skb) 1626 * 1627 * Try to deliver reassembled skb to layer above, and requeue it if that 1628 * for some reason should fail. We mark rx sdu as busy to apply back 1629 * pressure is necessary. 1630 */ 1631static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb) 1632{ 1633 int err; 1634 1635 /* Check if client has already closed the TSAP and gone away */ 1636 if (self->close_pend) { 1637 dev_kfree_skb(skb); 1638 return; 1639 } 1640 1641 err = self->notify.data_indication(self->notify.instance, self, skb); 1642 1643 /* Usually the layer above will notify that it's input queue is 1644 * starting to get filled by using the flow request, but this may 1645 * be difficult, so it can instead just refuse to eat it and just 1646 * give an error back 1647 */ 1648 if (err) { 1649 IRDA_DEBUG(0, "%s() requeueing skb!\n", __func__); 1650 1651 /* Make sure we take a break */ 1652 self->rx_sdu_busy = TRUE; 1653 1654 /* Need to push the header in again */ 1655 skb_push(skb, TTP_HEADER); 1656 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */ 1657 1658 /* Put skb back on queue */ 1659 skb_queue_head(&self->rx_queue, skb); 1660 } 1661} 1662 1663/* 1664 * Function irttp_run_rx_queue (self) 1665 * 1666 * Check if we have any frames to be transmitted, or if we have any 1667 * available credit to give away. 1668 */ 1669static void irttp_run_rx_queue(struct tsap_cb *self) 1670{ 1671 struct sk_buff *skb; 1672 int more = 0; 1673 1674 IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __func__, 1675 self->send_credit, self->avail_credit, self->remote_credit); 1676 1677 /* Get exclusive access to the rx queue, otherwise don't touch it */ 1678 if (irda_lock(&self->rx_queue_lock) == FALSE) 1679 return; 1680 1681 /* 1682 * Reassemble all frames in receive queue and deliver them 1683 */ 1684 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) { 1685 /* This bit will tell us if it's the last fragment or not */ 1686 more = skb->data[0] & 0x80; 1687 1688 /* Remove TTP header */ 1689 skb_pull(skb, TTP_HEADER); 1690 1691 /* Add the length of the remaining data */ 1692 self->rx_sdu_size += skb->len; 1693 1694 /* 1695 * If SAR is disabled, or user has requested no reassembly 1696 * of received fragments then we just deliver them 1697 * immediately. This can be requested by clients that 1698 * implements byte streams without any message boundaries 1699 */ 1700 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) { 1701 irttp_do_data_indication(self, skb); 1702 self->rx_sdu_size = 0; 1703 1704 continue; 1705 } 1706 1707 /* Check if this is a fragment, and not the last fragment */ 1708 if (more) { 1709 /* 1710 * Queue the fragment if we still are within the 1711 * limits of the maximum size of the rx_sdu 1712 */ 1713 if (self->rx_sdu_size <= self->rx_max_sdu_size) { 1714 IRDA_DEBUG(4, "%s(), queueing frag\n", 1715 __func__); 1716 skb_queue_tail(&self->rx_fragments, skb); 1717 } else { 1718 /* Free the part of the SDU that is too big */ 1719 dev_kfree_skb(skb); 1720 } 1721 continue; 1722 } 1723 /* 1724 * This is the last fragment, so time to reassemble! 1725 */ 1726 if ((self->rx_sdu_size <= self->rx_max_sdu_size) || 1727 (self->rx_max_sdu_size == TTP_SAR_UNBOUND)) 1728 { 1729 /* 1730 * A little optimizing. Only queue the fragment if 1731 * there are other fragments. Since if this is the 1732 * last and only fragment, there is no need to 1733 * reassemble :-) 1734 */ 1735 if (!skb_queue_empty(&self->rx_fragments)) { 1736 skb_queue_tail(&self->rx_fragments, 1737 skb); 1738 1739 skb = irttp_reassemble_skb(self); 1740 } 1741 1742 /* Now we can deliver the reassembled skb */ 1743 irttp_do_data_indication(self, skb); 1744 } else { 1745 IRDA_DEBUG(1, "%s(), Truncated frame\n", __func__); 1746 1747 /* Free the part of the SDU that is too big */ 1748 dev_kfree_skb(skb); 1749 1750 /* Deliver only the valid but truncated part of SDU */ 1751 skb = irttp_reassemble_skb(self); 1752 1753 irttp_do_data_indication(self, skb); 1754 } 1755 self->rx_sdu_size = 0; 1756 } 1757 1758 /* 1759 * It's not trivial to keep track of how many credits are available 1760 * by incrementing at each packet, because delivery may fail 1761 * (irttp_do_data_indication() may requeue the frame) and because 1762 * we need to take care of fragmentation. 1763 * We want the other side to send up to initial_credit packets. 1764 * We have some frames in our queues, and we have already allowed it 1765 * to send remote_credit. 1766 * No need to spinlock, write is atomic and self correcting... 1767 * Jean II 1768 */ 1769 self->avail_credit = (self->initial_credit - 1770 (self->remote_credit + 1771 skb_queue_len(&self->rx_queue) + 1772 skb_queue_len(&self->rx_fragments))); 1773 1774 /* Do we have too much credits to send to peer ? */ 1775 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) && 1776 (self->avail_credit > 0)) { 1777 /* Send explicit credit frame */ 1778 irttp_give_credit(self); 1779 /* Note : do *NOT* check if tx_queue is non-empty, that 1780 * will produce deadlocks. I repeat : send a credit frame 1781 * even if we have something to send in our Tx queue. 1782 * If we have credits, it means that our Tx queue is blocked. 1783 * 1784 * Let's suppose the peer can't keep up with our Tx. He will 1785 * flow control us by not sending us any credits, and we 1786 * will stop Tx and start accumulating credits here. 1787 * Up to the point where the peer will stop its Tx queue, 1788 * for lack of credits. 1789 * Let's assume the peer application is single threaded. 1790 * It will block on Tx and never consume any Rx buffer. 1791 * Deadlock. Guaranteed. - Jean II 1792 */ 1793 } 1794 1795 /* Reset lock */ 1796 self->rx_queue_lock = 0; 1797} 1798 1799#ifdef CONFIG_PROC_FS 1800struct irttp_iter_state { 1801 int id; 1802}; 1803 1804static void *irttp_seq_start(struct seq_file *seq, loff_t *pos) 1805{ 1806 struct irttp_iter_state *iter = seq->private; 1807 struct tsap_cb *self; 1808 1809 /* Protect our access to the tsap list */ 1810 spin_lock_irq(&irttp->tsaps->hb_spinlock); 1811 iter->id = 0; 1812 1813 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps); 1814 self != NULL; 1815 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) { 1816 if (iter->id == *pos) 1817 break; 1818 ++iter->id; 1819 } 1820 1821 return self; 1822} 1823 1824static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos) 1825{ 1826 struct irttp_iter_state *iter = seq->private; 1827 1828 ++*pos; 1829 ++iter->id; 1830 return (void *) hashbin_get_next(irttp->tsaps); 1831} 1832 1833static void irttp_seq_stop(struct seq_file *seq, void *v) 1834{ 1835 spin_unlock_irq(&irttp->tsaps->hb_spinlock); 1836} 1837 1838static int irttp_seq_show(struct seq_file *seq, void *v) 1839{ 1840 const struct irttp_iter_state *iter = seq->private; 1841 const struct tsap_cb *self = v; 1842 1843 seq_printf(seq, "TSAP %d, ", iter->id); 1844 seq_printf(seq, "stsap_sel: %02x, ", 1845 self->stsap_sel); 1846 seq_printf(seq, "dtsap_sel: %02x\n", 1847 self->dtsap_sel); 1848 seq_printf(seq, " connected: %s, ", 1849 self->connected? "TRUE":"FALSE"); 1850 seq_printf(seq, "avail credit: %d, ", 1851 self->avail_credit); 1852 seq_printf(seq, "remote credit: %d, ", 1853 self->remote_credit); 1854 seq_printf(seq, "send credit: %d\n", 1855 self->send_credit); 1856 seq_printf(seq, " tx packets: %lu, ", 1857 self->stats.tx_packets); 1858 seq_printf(seq, "rx packets: %lu, ", 1859 self->stats.rx_packets); 1860 seq_printf(seq, "tx_queue len: %u ", 1861 skb_queue_len(&self->tx_queue)); 1862 seq_printf(seq, "rx_queue len: %u\n", 1863 skb_queue_len(&self->rx_queue)); 1864 seq_printf(seq, " tx_sdu_busy: %s, ", 1865 self->tx_sdu_busy? "TRUE":"FALSE"); 1866 seq_printf(seq, "rx_sdu_busy: %s\n", 1867 self->rx_sdu_busy? "TRUE":"FALSE"); 1868 seq_printf(seq, " max_seg_size: %u, ", 1869 self->max_seg_size); 1870 seq_printf(seq, "tx_max_sdu_size: %u, ", 1871 self->tx_max_sdu_size); 1872 seq_printf(seq, "rx_max_sdu_size: %u\n", 1873 self->rx_max_sdu_size); 1874 1875 seq_printf(seq, " Used by (%s)\n\n", 1876 self->notify.name); 1877 return 0; 1878} 1879 1880static const struct seq_operations irttp_seq_ops = { 1881 .start = irttp_seq_start, 1882 .next = irttp_seq_next, 1883 .stop = irttp_seq_stop, 1884 .show = irttp_seq_show, 1885}; 1886 1887static int irttp_seq_open(struct inode *inode, struct file *file) 1888{ 1889 return seq_open_private(file, &irttp_seq_ops, 1890 sizeof(struct irttp_iter_state)); 1891} 1892 1893const struct file_operations irttp_seq_fops = { 1894 .owner = THIS_MODULE, 1895 .open = irttp_seq_open, 1896 .read = seq_read, 1897 .llseek = seq_lseek, 1898 .release = seq_release_private, 1899}; 1900 1901#endif /* PROC_FS */ 1902