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