1 /*********************************************************************
5 * Description: Tiny Transport Protocol (TTP) implementation
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>
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
14 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
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.
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.
25 ********************************************************************/
27 #include <linux/config.h>
28 #include <linux/skbuff.h>
29 #include <linux/init.h>
31 #include <asm/byteorder.h>
32 #include <asm/unaligned.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irmod.h>
36 #include <net/irda/irlap.h>
37 #include <net/irda/irlmp.h>
38 #include <net/irda/parameters.h>
39 #include <net/irda/irttp.h>
41 static struct irttp_cb *irttp = NULL;
43 static void __irttp_close_tsap(struct tsap_cb *self);
45 static int irttp_data_indication(void *instance, void *sap,
47 static int irttp_udata_indication(void *instance, void *sap,
49 static void irttp_disconnect_indication(void *instance, void *sap,
50 LM_REASON reason, struct sk_buff *);
51 static void irttp_connect_indication(void *instance, void *sap,
52 struct qos_info *qos, __u32 max_sdu_size,
53 __u8 header_size, struct sk_buff *skb);
54 static void irttp_connect_confirm(void *instance, void *sap,
55 struct qos_info *qos, __u32 max_sdu_size,
56 __u8 header_size, struct sk_buff *skb);
57 static void irttp_run_tx_queue(struct tsap_cb *self);
58 static void irttp_run_rx_queue(struct tsap_cb *self);
60 static void irttp_flush_queues(struct tsap_cb *self);
61 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
62 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
63 static void irttp_todo_expired(unsigned long data);
64 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
67 /* Information for parsing parameters in IrTTP */
68 static pi_minor_info_t pi_minor_call_table[] = {
69 { NULL, 0 }, /* 0x00 */
70 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
72 static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
73 static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
75 /************************ GLOBAL PROCEDURES ************************/
78 * Function irttp_init (void)
80 * Initialize the IrTTP layer. Called by module initialization code
83 int __init irttp_init(void)
85 /* Initialize the irttp structure. */
87 irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
91 memset(irttp, 0, sizeof(struct irttp_cb));
93 irttp->magic = TTP_MAGIC;
95 irttp->tsaps = hashbin_new(HB_LOCAL);
97 ERROR("%s(), can't allocate IrTTP hashbin!\n", __FUNCTION__);
105 * Function irttp_cleanup (void)
107 * Called by module destruction/cleanup code
111 void irttp_cleanup(void)
113 /* Check for main structure */
114 ASSERT(irttp != NULL, return;);
115 ASSERT(irttp->magic == TTP_MAGIC, return;);
118 * Delete hashbin and close all TSAP instances in it
120 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
124 /* De-allocate main structure */
131 /*************************** SUBROUTINES ***************************/
134 * Function irttp_start_todo_timer (self, timeout)
138 * Made it more effient and unsensitive to race conditions - Jean II
140 static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
142 /* Set new value for timer */
143 mod_timer(&self->todo_timer, jiffies + timeout);
147 * Function irttp_todo_expired (data)
149 * Todo timer has expired!
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...
160 static void irttp_todo_expired(unsigned long data)
162 struct tsap_cb *self = (struct tsap_cb *) data;
164 /* Check that we still exist */
165 if (!self || self->magic != TTP_TSAP_MAGIC)
168 IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
170 /* Try to make some progress, especially on Tx side - Jean II */
171 irttp_run_rx_queue(self);
172 irttp_run_tx_queue(self);
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 */
181 /* Note : self->disconnect_skb may be NULL */
182 irttp_disconnect_request(self, self->disconnect_skb,
184 self->disconnect_skb = NULL;
186 /* Try again later */
187 irttp_start_todo_timer(self, HZ/10);
189 /* No reason to try and close now */
194 /* Check if it's closing time */
195 if (self->close_pend)
197 irttp_close_tsap(self);
201 * Function irttp_flush_queues (self)
203 * Flushes (removes all frames) in transitt-buffer (tx_list)
205 void irttp_flush_queues(struct tsap_cb *self)
209 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
211 ASSERT(self != NULL, return;);
212 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
214 /* Deallocate frames waiting to be sent */
215 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
218 /* Deallocate received frames */
219 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
222 /* Deallocate received fragments */
223 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
228 * Function irttp_reassemble (self)
230 * Makes a new (continuous) skb of all the fragments in the fragment
234 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
236 struct sk_buff *skb, *frag;
237 int n = 0; /* Fragment index */
239 ASSERT(self != NULL, return NULL;);
240 ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
242 IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __FUNCTION__,
245 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
250 * Need to reserve space for TTP header in case this skb needs to
251 * be requeued in case delivery failes
253 skb_reserve(skb, TTP_HEADER);
254 skb_put(skb, self->rx_sdu_size);
257 * Copy all fragments to a new buffer
259 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
260 memcpy(skb->data+n, frag->data, frag->len);
265 IRDA_DEBUG(2, "%s(), frame len=%d\n", __FUNCTION__, n);
267 IRDA_DEBUG(2, "%s(), rx_sdu_size=%d\n", __FUNCTION__, self->rx_sdu_size);
268 ASSERT(n <= self->rx_sdu_size, return NULL;);
270 /* Set the new length */
273 self->rx_sdu_size = 0;
279 * Function irttp_fragment_skb (skb)
281 * Fragments a frame and queues all the fragments for transmission
284 static inline void irttp_fragment_skb(struct tsap_cb *self,
287 struct sk_buff *frag;
290 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
292 ASSERT(self != NULL, return;);
293 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
294 ASSERT(skb != NULL, return;);
297 * Split frame into a number of segments
299 while (skb->len > self->max_seg_size) {
300 IRDA_DEBUG(2, "%s(), fragmenting ...\n", __FUNCTION__);
302 /* Make new segment */
303 frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
307 skb_reserve(frag, self->max_header_size);
309 /* Copy data from the original skb into this fragment. */
310 memcpy(skb_put(frag, self->max_seg_size), skb->data,
313 /* Insert TTP header, with the more bit set */
314 frame = skb_push(frag, TTP_HEADER);
317 /* Hide the copied data from the original skb */
318 skb_pull(skb, self->max_seg_size);
321 skb_queue_tail(&self->tx_queue, frag);
323 /* Queue what is left of the original skb */
324 IRDA_DEBUG(2, "%s(), queuing last segment\n", __FUNCTION__);
326 frame = skb_push(skb, TTP_HEADER);
327 frame[0] = 0x00; /* Clear more bit */
330 skb_queue_tail(&self->tx_queue, skb);
334 * Function irttp_param_max_sdu_size (self, param)
336 * Handle the MaxSduSize parameter in the connect frames, this function
337 * will be called both when this parameter needs to be inserted into, and
338 * extracted from the connect frames
340 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
343 struct tsap_cb *self;
345 self = (struct tsap_cb *) instance;
347 ASSERT(self != NULL, return -1;);
348 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
351 param->pv.i = self->tx_max_sdu_size;
353 self->tx_max_sdu_size = param->pv.i;
355 IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __FUNCTION__, param->pv.i);
360 /*************************** CLIENT CALLS ***************************/
361 /************************** LMP CALLBACKS **************************/
362 /* Everything is happily mixed up. Waiting for next clean up - Jean II */
365 * Function irttp_open_tsap (stsap, notify)
367 * Create TSAP connection endpoint,
369 struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
371 struct tsap_cb *self;
372 struct lsap_cb *lsap;
375 ASSERT(irttp != NULL, return NULL;);
376 ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
378 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
379 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
381 if((stsap_sel != LSAP_ANY) &&
382 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
383 IRDA_DEBUG(0, "%s(), invalid tsap!\n", __FUNCTION__);
387 self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
389 IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __FUNCTION__);
392 memset(self, 0, sizeof(struct tsap_cb));
393 spin_lock_init(&self->lock);
395 /* Initialise todo timer */
396 init_timer(&self->todo_timer);
397 self->todo_timer.data = (unsigned long) self;
398 self->todo_timer.function = &irttp_todo_expired;
400 /* Initialize callbacks for IrLMP to use */
401 irda_notify_init(&ttp_notify);
402 ttp_notify.connect_confirm = irttp_connect_confirm;
403 ttp_notify.connect_indication = irttp_connect_indication;
404 ttp_notify.disconnect_indication = irttp_disconnect_indication;
405 ttp_notify.data_indication = irttp_data_indication;
406 ttp_notify.udata_indication = irttp_udata_indication;
407 ttp_notify.flow_indication = irttp_flow_indication;
408 if(notify->status_indication != NULL)
409 ttp_notify.status_indication = irttp_status_indication;
410 ttp_notify.instance = self;
411 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
413 self->magic = TTP_TSAP_MAGIC;
414 self->connected = FALSE;
416 skb_queue_head_init(&self->rx_queue);
417 skb_queue_head_init(&self->tx_queue);
418 skb_queue_head_init(&self->rx_fragments);
420 * Create LSAP at IrLMP layer
422 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
424 WARNING("%s(), unable to allocate LSAP!!\n", __FUNCTION__);
429 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
430 * will replace it with whatever source selector which is free, so
431 * the stsap_sel we have might not be valid anymore
433 self->stsap_sel = lsap->slsap_sel;
434 IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __FUNCTION__, self->stsap_sel);
436 self->notify = *notify;
439 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (int) self, NULL);
441 if (credit > TTP_RX_MAX_CREDIT)
442 self->initial_credit = TTP_RX_MAX_CREDIT;
444 self->initial_credit = credit;
450 * Function irttp_close (handle)
452 * Remove an instance of a TSAP. This function should only deal with the
453 * deallocation of the TSAP, and resetting of the TSAPs values;
456 static void __irttp_close_tsap(struct tsap_cb *self)
458 /* First make sure we're connected. */
459 ASSERT(self != NULL, return;);
460 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
462 irttp_flush_queues(self);
464 del_timer(&self->todo_timer);
466 /* This one won't be cleaned up if we are disconnect_pend + close_pend
467 * and we receive a disconnect_indication */
468 if (self->disconnect_skb)
469 dev_kfree_skb(self->disconnect_skb);
471 self->connected = FALSE;
472 self->magic = ~TTP_TSAP_MAGIC;
478 * Function irttp_close (self)
480 * Remove TSAP from list of all TSAPs and then deallocate all resources
481 * associated with this TSAP
483 * Note : because we *free* the tsap structure, it is the responsability
484 * of the caller to make sure we are called only once and to deal with
485 * possible race conditions. - Jean II
487 int irttp_close_tsap(struct tsap_cb *self)
489 struct tsap_cb *tsap;
491 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
493 ASSERT(self != NULL, return -1;);
494 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
496 /* Make sure tsap has been disconnected */
497 if (self->connected) {
498 /* Check if disconnect is not pending */
499 if (!test_bit(0, &self->disconnect_pend)) {
500 WARNING("%s(), TSAP still connected!\n", __FUNCTION__);
501 irttp_disconnect_request(self, NULL, P_NORMAL);
503 self->close_pend = TRUE;
504 irttp_start_todo_timer(self, HZ/10);
506 return 0; /* Will be back! */
509 tsap = hashbin_remove(irttp->tsaps, (int) self, NULL);
511 ASSERT(tsap == self, return -1;);
513 /* Close corresponding LSAP */
515 irlmp_close_lsap(self->lsap);
519 __irttp_close_tsap(self);
525 * Function irttp_udata_request (self, skb)
527 * Send unreliable data on this TSAP
530 int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
532 ASSERT(self != NULL, return -1;);
533 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
534 ASSERT(skb != NULL, return -1;);
536 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
538 /* Check that nothing bad happens */
539 if ((skb->len == 0) || (!self->connected)) {
540 IRDA_DEBUG(1, "%s(), No data, or not connected\n", __FUNCTION__);
544 if (skb->len > self->max_seg_size) {
545 IRDA_DEBUG(1, "%s(), UData is to large for IrLAP!\n", __FUNCTION__);
549 irlmp_udata_request(self->lsap, skb);
550 self->stats.tx_packets++;
556 * Function irttp_data_request (handle, skb)
558 * Queue frame for transmission. If SAR is enabled, fragement the frame
559 * and queue the fragments for transmission
561 int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
565 ASSERT(self != NULL, return -1;);
566 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
567 ASSERT(skb != NULL, return -1;);
569 IRDA_DEBUG(2, "%s : queue len = %d\n", __FUNCTION__,
570 skb_queue_len(&self->tx_queue));
572 /* Check that nothing bad happens */
573 if ((skb->len == 0) || (!self->connected)) {
574 WARNING("%s(), No data, or not connected\n", __FUNCTION__);
579 * Check if SAR is disabled, and the frame is larger than what fits
580 * inside an IrLAP frame
582 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
583 ERROR("%s(), SAR disabled, and data is to large for IrLAP!\n", __FUNCTION__);
588 * Check if SAR is enabled, and the frame is larger than the
591 if ((self->tx_max_sdu_size != 0) &&
592 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
593 (skb->len > self->tx_max_sdu_size))
595 ERROR("%s(), SAR enabled, "
596 "but data is larger than TxMaxSduSize!\n", __FUNCTION__);
600 * Check if transmit queue is full
602 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
604 * Give it a chance to empty itself
606 irttp_run_tx_queue(self);
608 /* Drop packet. This error code should trigger the caller
609 * to requeue the packet in the client code - Jean II */
613 /* Queue frame, or queue frame segments */
614 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
616 ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
617 frame = skb_push(skb, TTP_HEADER);
618 frame[0] = 0x00; /* Clear more bit */
620 skb_queue_tail(&self->tx_queue, skb);
623 * Fragment the frame, this function will also queue the
624 * fragments, we don't care about the fact the transmit
625 * queue may be overfilled by all the segments for a little
628 irttp_fragment_skb(self, skb);
631 /* Check if we can accept more data from client */
632 if ((!self->tx_sdu_busy) &&
633 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
634 /* Tx queue filling up, so stop client. */
635 if (self->notify.flow_indication) {
636 self->notify.flow_indication(self->notify.instance,
639 /* self->tx_sdu_busy is the state of the client.
640 * Update state after notifying client to avoid
641 * race condition with irttp_flow_indication().
642 * If the queue empty itself after our test but before
643 * we set the flag, we will fix ourselves below in
644 * irttp_run_tx_queue().
646 self->tx_sdu_busy = TRUE;
649 /* Try to make some progress */
650 irttp_run_tx_queue(self);
656 * Function irttp_run_tx_queue (self)
658 * Transmit packets queued for transmission (if possible)
661 static void irttp_run_tx_queue(struct tsap_cb *self)
667 IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n", __FUNCTION__,
668 self->send_credit, skb_queue_len(&self->tx_queue));
670 /* Get exclusive access to the tx queue, otherwise don't touch it */
671 if (irda_lock(&self->tx_queue_lock) == FALSE)
674 /* Try to send out frames as long as we have credits
675 * and as long as LAP is not full. If LAP is full, it will
676 * poll us through irttp_flow_indication() - Jean II */
677 while ((self->send_credit > 0) &&
678 (!irlmp_lap_tx_queue_full(self->lsap)) &&
679 (skb = skb_dequeue(&self->tx_queue)))
682 * Since we can transmit and receive frames concurrently,
683 * the code below is a critical region and we must assure that
684 * nobody messes with the credits while we update them.
686 spin_lock_irqsave(&self->lock, flags);
688 n = self->avail_credit;
689 self->avail_credit = 0;
691 /* Only room for 127 credits in frame */
693 self->avail_credit = n-127;
696 self->remote_credit += n;
699 spin_unlock_irqrestore(&self->lock, flags);
702 * More bit must be set by the data_request() or fragment()
705 skb->data[0] |= (n & 0x7f);
707 /* Detach from socket.
708 * The current skb has a reference to the socket that sent
709 * it (skb->sk). When we pass it to IrLMP, the skb will be
710 * stored in in IrLAP (self->wx_list). When we are within
711 * IrLAP, we loose the notion of socket, so we should not
712 * have a reference to a socket. So, we drop it here.
714 * Why does it matter ?
715 * When the skb is freed (kfree_skb), if it is associated
716 * with a socket, it release buffer space on the socket
717 * (through sock_wfree() and sock_def_write_space()).
718 * If the socket no longer exist, we may crash. Hard.
719 * When we close a socket, we make sure that associated packets
720 * in IrTTP are freed. However, we have no way to cancel
721 * the packet that we have passed to IrLAP. So, if a packet
722 * remains in IrLAP (retry on the link or else) after we
723 * close the socket, we are dead !
725 if (skb->sk != NULL) {
726 /* IrSOCK application, IrOBEX, ... */
729 /* IrCOMM over IrTTP, IrLAN, ... */
731 /* Pass the skb to IrLMP - done */
732 irlmp_data_request(self->lsap, skb);
733 self->stats.tx_packets++;
736 /* Check if we can accept more frames from client.
737 * We don't want to wait until the todo timer to do that, and we
738 * can't use tasklets (grr...), so we are obliged to give control
739 * to client. That's ok, this test will be true not too often
740 * (max once per LAP window) and we are called from places
741 * where we can spend a bit of time doing stuff. - Jean II */
742 if ((self->tx_sdu_busy) &&
743 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
746 if (self->notify.flow_indication)
747 self->notify.flow_indication(self->notify.instance,
750 /* self->tx_sdu_busy is the state of the client.
751 * We don't really have a race here, but it's always safer
752 * to update our state after the client - Jean II */
753 self->tx_sdu_busy = FALSE;
757 self->tx_queue_lock = 0;
761 * Function irttp_give_credit (self)
763 * Send a dataless flowdata TTP-PDU and give available credit to peer
766 static inline void irttp_give_credit(struct tsap_cb *self)
768 struct sk_buff *tx_skb = NULL;
772 ASSERT(self != NULL, return;);
773 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
775 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
776 self->send_credit, self->avail_credit, self->remote_credit);
778 /* Give credit to peer */
779 tx_skb = dev_alloc_skb(64);
783 /* Reserve space for LMP, and LAP header */
784 skb_reserve(tx_skb, self->max_header_size);
787 * Since we can transmit and receive frames concurrently,
788 * the code below is a critical region and we must assure that
789 * nobody messes with the credits while we update them.
791 spin_lock_irqsave(&self->lock, flags);
793 n = self->avail_credit;
794 self->avail_credit = 0;
796 /* Only space for 127 credits in frame */
798 self->avail_credit = n - 127;
801 self->remote_credit += n;
803 spin_unlock_irqrestore(&self->lock, flags);
806 tx_skb->data[0] = (__u8) (n & 0x7f);
808 irlmp_data_request(self->lsap, tx_skb);
809 self->stats.tx_packets++;
813 * Function irttp_udata_indication (instance, sap, skb)
815 * Received some unit-data (unreliable)
818 static int irttp_udata_indication(void *instance, void *sap,
821 struct tsap_cb *self;
823 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
825 self = (struct tsap_cb *) instance;
827 ASSERT(self != NULL, return -1;);
828 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
829 ASSERT(skb != NULL, return -1;);
831 /* Just pass data to layer above */
832 if (self->notify.udata_indication)
833 self->notify.udata_indication(self->notify.instance, self,skb);
837 self->stats.rx_packets++;
843 * Function irttp_data_indication (instance, sap, skb)
845 * Receive segment from IrLMP.
848 static int irttp_data_indication(void *instance, void *sap,
851 struct tsap_cb *self;
855 self = (struct tsap_cb *) instance;
857 n = skb->data[0] & 0x7f; /* Extract the credits */
859 self->stats.rx_packets++;
861 /* Deal with inbound credit
862 * Since we can transmit and receive frames concurrently,
863 * the code below is a critical region and we must assure that
864 * nobody messes with the credits while we update them.
866 spin_lock_irqsave(&self->lock, flags);
867 self->send_credit += n;
869 self->remote_credit--;
870 spin_unlock_irqrestore(&self->lock, flags);
873 * Data or dataless packet? Dataless frames contains only the
878 * We don't remove the TTP header, since we must preserve the
879 * more bit, so the defragment routing knows what to do
881 skb_queue_tail(&self->rx_queue, skb);
883 /* Dataless flowdata TTP-PDU */
888 /* Push data to the higher layer.
889 * We do it synchronously because running the todo timer for each
890 * receive packet would be too much overhead and latency.
891 * By passing control to the higher layer, we run the risk that
892 * it may take time or grab a lock. Most often, the higher layer
893 * will only put packet in a queue.
894 * Anyway, packets are only dripping through the IrDA, so we can
895 * have time before the next packet.
896 * Further, we are run from NET_BH, so the worse that can happen is
897 * us missing the optimal time to send back the PF bit in LAP.
899 irttp_run_rx_queue(self);
901 /* We now give credits to peer in irttp_run_rx_queue().
902 * We need to send credit *NOW*, otherwise we are going
903 * to miss the next Tx window. The todo timer may take
904 * a while before it's run... - Jean II */
907 * If the peer device has given us some credits and we didn't have
908 * anyone from before, then we need to shedule the tx queue.
909 * We need to do that because our Tx have stopped (so we may not
910 * get any LAP flow indication) and the user may be stopped as
913 if (self->send_credit == n) {
914 /* Restart pushing stuff to LAP */
915 irttp_run_tx_queue(self);
916 /* Note : we don't want to schedule the todo timer
917 * because it has horrible latency. No tasklets
918 * because the tasklet API is broken. - Jean II */
925 * Function irttp_status_indication (self, reason)
927 * Status_indication, just pass to the higher layer...
930 void irttp_status_indication(void *instance,
931 LINK_STATUS link, LOCK_STATUS lock)
933 struct tsap_cb *self;
935 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
937 self = (struct tsap_cb *) instance;
939 ASSERT(self != NULL, return;);
940 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
943 * Inform service user if he has requested it
945 if (self->notify.status_indication != NULL)
946 self->notify.status_indication(self->notify.instance,
949 IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
953 * Function irttp_flow_indication (self, reason)
955 * Flow_indication : IrLAP tells us to send more data.
958 void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
960 struct tsap_cb *self;
962 self = (struct tsap_cb *) instance;
964 ASSERT(self != NULL, return;);
965 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
967 IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
969 /* We are "polled" directly from LAP, and the LAP want to fill
970 * its Tx window. We want to do our best to send it data, so that
971 * we maximise the window. On the other hand, we want to limit the
972 * amount of work here so that LAP doesn't hang forever waiting
973 * for packets. - Jean II */
975 /* Try to send some packets. Currently, LAP calls us every time
976 * there is one free slot, so we will send only one packet.
977 * This allow the scheduler to do its round robin - Jean II */
978 irttp_run_tx_queue(self);
980 /* Note regarding the interraction with higher layer.
981 * irttp_run_tx_queue() may call the client when its queue
982 * start to empty, via notify.flow_indication(). Initially.
983 * I wanted this to happen in a tasklet, to avoid client
984 * grabbing the CPU, but we can't use tasklets safely. And timer
985 * is definitely too slow.
986 * This will happen only once per LAP window, and usually at
987 * the third packet (unless window is smaller). LAP is still
988 * doing mtt and sending first packet so it's sort of OK
989 * to do that. Jean II */
991 /* If we need to send disconnect. try to do it now */
992 if(self->disconnect_pend)
993 irttp_start_todo_timer(self, 0);
997 * Function irttp_flow_request (self, command)
999 * This funtion could be used by the upper layers to tell IrTTP to stop
1000 * delivering frames if the receive queues are starting to get full, or
1001 * to tell IrTTP to start delivering frames again.
1003 void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1005 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1007 ASSERT(self != NULL, return;);
1008 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1012 IRDA_DEBUG(1, "%s(), flow stop\n", __FUNCTION__);
1013 self->rx_sdu_busy = TRUE;
1016 IRDA_DEBUG(1, "%s(), flow start\n", __FUNCTION__);
1017 self->rx_sdu_busy = FALSE;
1019 /* Client say he can accept more data, try to free our
1020 * queues ASAP - Jean II */
1021 irttp_run_rx_queue(self);
1025 IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __FUNCTION__);
1030 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
1032 * Try to connect to remote destination TSAP selector
1035 int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1036 __u32 saddr, __u32 daddr,
1037 struct qos_info *qos, __u32 max_sdu_size,
1038 struct sk_buff *userdata)
1040 struct sk_buff *skb;
1044 IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __FUNCTION__, max_sdu_size);
1046 ASSERT(self != NULL, return -EBADR;);
1047 ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1049 if (self->connected)
1052 /* Any userdata supplied? */
1053 if (userdata == NULL) {
1054 skb = dev_alloc_skb(64);
1058 /* Reserve space for MUX_CONTROL and LAP header */
1059 skb_reserve(skb, TTP_MAX_HEADER);
1063 * Check that the client has reserved enough space for
1066 ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER, return -1;);
1069 /* Initialize connection parameters */
1070 self->connected = FALSE;
1071 self->avail_credit = 0;
1072 self->rx_max_sdu_size = max_sdu_size;
1073 self->rx_sdu_size = 0;
1074 self->rx_sdu_busy = FALSE;
1075 self->dtsap_sel = dtsap_sel;
1077 n = self->initial_credit;
1079 self->remote_credit = 0;
1080 self->send_credit = 0;
1083 * Give away max 127 credits for now
1086 self->avail_credit=n-127;
1090 self->remote_credit = n;
1093 if (max_sdu_size > 0) {
1094 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1097 /* Insert SAR parameters */
1098 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
1100 frame[0] = TTP_PARAMETERS | n;
1101 frame[1] = 0x04; /* Length */
1102 frame[2] = 0x01; /* MaxSduSize */
1103 frame[3] = 0x02; /* Value length */
1105 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1106 (__u16 *)(frame+4));
1108 /* Insert plain TTP header */
1109 frame = skb_push(skb, TTP_HEADER);
1111 /* Insert initial credit in frame */
1112 frame[0] = n & 0x7f;
1115 /* Connect with IrLMP. No QoS parameters for now */
1116 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1121 * Function irttp_connect_confirm (handle, qos, skb)
1123 * Sevice user confirms TSAP connection with peer.
1126 static void irttp_connect_confirm(void *instance, void *sap,
1127 struct qos_info *qos, __u32 max_seg_size,
1128 __u8 max_header_size, struct sk_buff *skb)
1130 struct tsap_cb *self;
1136 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1138 self = (struct tsap_cb *) instance;
1140 ASSERT(self != NULL, return;);
1141 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1142 ASSERT(skb != NULL, return;);
1144 self->max_seg_size = max_seg_size - TTP_HEADER;
1145 self->max_header_size = max_header_size + TTP_HEADER;
1148 * Check if we have got some QoS parameters back! This should be the
1149 * negotiated QoS for the link.
1152 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
1153 qos->baud_rate.bits);
1154 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1155 qos->baud_rate.value);
1158 n = skb->data[0] & 0x7f;
1160 IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __FUNCTION__, n);
1162 self->send_credit = n;
1163 self->tx_max_sdu_size = 0;
1164 self->connected = TRUE;
1166 parameters = skb->data[0] & 0x80;
1168 ASSERT(skb->len >= TTP_HEADER, return;);
1169 skb_pull(skb, TTP_HEADER);
1172 plen = skb->data[0];
1174 ret = irda_param_extract_all(self, skb->data+1,
1175 IRDA_MIN(skb->len-1, plen),
1178 /* Any errors in the parameter list? */
1180 WARNING("%s(), error extracting parameters\n", __FUNCTION__);
1183 /* Do not accept this connection attempt */
1186 /* Remove parameters */
1187 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1190 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
1191 self->send_credit, self->avail_credit, self->remote_credit);
1193 IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __FUNCTION__, self->tx_max_sdu_size);
1195 if (self->notify.connect_confirm) {
1196 self->notify.connect_confirm(self->notify.instance, self, qos,
1197 self->tx_max_sdu_size,
1198 self->max_header_size, skb);
1203 * Function irttp_connect_indication (handle, skb)
1205 * Some other device is connecting to this TSAP
1208 void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
1209 __u32 max_seg_size, __u8 max_header_size,
1210 struct sk_buff *skb)
1212 struct tsap_cb *self;
1213 struct lsap_cb *lsap;
1219 self = (struct tsap_cb *) instance;
1221 ASSERT(self != NULL, return;);
1222 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1223 ASSERT(skb != NULL, return;);
1225 lsap = (struct lsap_cb *) sap;
1227 self->max_seg_size = max_seg_size - TTP_HEADER;;
1228 self->max_header_size = max_header_size+TTP_HEADER;
1230 IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __FUNCTION__, self->stsap_sel);
1232 /* Need to update dtsap_sel if its equal to LSAP_ANY */
1233 self->dtsap_sel = lsap->dlsap_sel;
1235 n = skb->data[0] & 0x7f;
1237 self->send_credit = n;
1238 self->tx_max_sdu_size = 0;
1240 parameters = skb->data[0] & 0x80;
1242 ASSERT(skb->len >= TTP_HEADER, return;);
1243 skb_pull(skb, TTP_HEADER);
1246 plen = skb->data[0];
1248 ret = irda_param_extract_all(self, skb->data+1,
1249 IRDA_MIN(skb->len-1, plen),
1252 /* Any errors in the parameter list? */
1254 WARNING("%s(), error extracting parameters\n", __FUNCTION__);
1257 /* Do not accept this connection attempt */
1261 /* Remove parameters */
1262 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1265 if (self->notify.connect_indication) {
1266 self->notify.connect_indication(self->notify.instance, self,
1267 qos, self->tx_max_sdu_size,
1268 self->max_header_size, skb);
1274 * Function irttp_connect_response (handle, userdata)
1276 * Service user is accepting the connection, just pass it down to
1280 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1281 struct sk_buff *userdata)
1283 struct sk_buff *skb;
1288 ASSERT(self != NULL, return -1;);
1289 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1291 IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __FUNCTION__,
1294 /* Any userdata supplied? */
1295 if (userdata == NULL) {
1296 skb = dev_alloc_skb(64);
1300 /* Reserve space for MUX_CONTROL and LAP header */
1301 skb_reserve(skb, TTP_MAX_HEADER);
1305 * Check that the client has reserved enough space for
1308 ASSERT(skb_headroom(skb) >= TTP_MAX_HEADER, return -1;);
1311 self->avail_credit = 0;
1312 self->remote_credit = 0;
1313 self->rx_max_sdu_size = max_sdu_size;
1314 self->rx_sdu_size = 0;
1315 self->rx_sdu_busy = FALSE;
1317 n = self->initial_credit;
1319 /* Frame has only space for max 127 credits (7 bits) */
1321 self->avail_credit = n - 127;
1325 self->remote_credit = n;
1326 self->connected = TRUE;
1329 if (max_sdu_size > 0) {
1330 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER+TTP_SAR_HEADER),
1333 /* Insert TTP header with SAR parameters */
1334 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
1336 frame[0] = TTP_PARAMETERS | n;
1337 frame[1] = 0x04; /* Length */
1339 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
1340 /* TTP_SAR_HEADER, ¶m_info) */
1342 frame[2] = 0x01; /* MaxSduSize */
1343 frame[3] = 0x02; /* Value length */
1345 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1346 (__u16 *)(frame+4));
1348 /* Insert TTP header */
1349 frame = skb_push(skb, TTP_HEADER);
1351 frame[0] = n & 0x7f;
1354 ret = irlmp_connect_response(self->lsap, skb);
1360 * Function irttp_dup (self, instance)
1362 * Duplicate TSAP, can be used by servers to confirm a connection on a
1363 * new TSAP so it can keep listening on the old one.
1365 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1367 struct tsap_cb *new;
1369 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1371 if (!hashbin_find(irttp->tsaps, (int) orig, NULL)) {
1372 IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __FUNCTION__);
1375 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
1377 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
1381 memcpy(new, orig, sizeof(struct tsap_cb));
1382 new->notify.instance = instance;
1383 new->lsap = irlmp_dup(orig->lsap, new);
1385 /* Not everything should be copied */
1386 init_timer(&new->todo_timer);
1388 skb_queue_head_init(&new->rx_queue);
1389 skb_queue_head_init(&new->tx_queue);
1390 skb_queue_head_init(&new->rx_fragments);
1392 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (int) new, NULL);
1398 * Function irttp_disconnect_request (self)
1400 * Close this connection please! If priority is high, the queued data
1401 * segments, if any, will be deallocated first
1404 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1407 struct sk_buff *skb;
1410 ASSERT(self != NULL, return -1;);
1411 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1413 /* Already disconnected? */
1414 if (!self->connected) {
1415 IRDA_DEBUG(4, "%s(), already disconnected!\n", __FUNCTION__);
1417 dev_kfree_skb(userdata);
1421 /* Disconnect already pending ?
1422 * We need to use an atomic operation to prevent reentry. This
1423 * function may be called from various context, like user, timer
1424 * for following a disconnect_indication() (i.e. net_bh).
1426 if(test_and_set_bit(0, &self->disconnect_pend)) {
1427 IRDA_DEBUG(0, "%s(), disconnect already pending\n", __FUNCTION__);
1429 dev_kfree_skb(userdata);
1431 /* Try to make some progress */
1432 irttp_run_tx_queue(self);
1437 * Check if there is still data segments in the transmit queue
1439 if (skb_queue_len(&self->tx_queue) > 0) {
1440 if (priority == P_HIGH) {
1442 * No need to send the queued data, if we are
1443 * disconnecting right now since the data will
1444 * not have any usable connection to be sent on
1446 IRDA_DEBUG(1, "%s High priority!!()\n", __FUNCTION__);
1447 irttp_flush_queues(self);
1448 } else if (priority == P_NORMAL) {
1450 * Must delay disconnect until after all data segments
1451 * have been sent and the tx_queue is empty
1453 /* We'll reuse this one later for the disconnect */
1454 self->disconnect_skb = userdata; /* May be NULL */
1456 irttp_run_tx_queue(self);
1458 irttp_start_todo_timer(self, HZ/10);
1462 /* Note : we don't need to check if self->rx_queue is full and the
1463 * state of self->rx_sdu_busy because the disconnect response will
1464 * be sent at the LMP level (so even if the peer has its Tx queue
1465 * full of data). - Jean II */
1467 IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __FUNCTION__);
1468 self->connected = FALSE;
1471 skb = dev_alloc_skb(64);
1476 * Reserve space for MUX and LAP header
1478 skb_reserve(skb, TTP_MAX_HEADER);
1482 ret = irlmp_disconnect_request(self->lsap, userdata);
1484 /* The disconnect is no longer pending */
1485 clear_bit(0, &self->disconnect_pend); /* FALSE */
1491 * Function irttp_disconnect_indication (self, reason)
1493 * Disconnect indication, TSAP disconnected by peer?
1496 void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason,
1497 struct sk_buff *skb)
1499 struct tsap_cb *self;
1501 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1503 self = (struct tsap_cb *) instance;
1505 ASSERT(self != NULL, return;);
1506 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1508 /* Prevent higher layer to send more data */
1509 self->connected = FALSE;
1511 /* Check if client has already tried to close the TSAP */
1512 if (self->close_pend) {
1513 /* In this case, the higher layer is probably gone. Don't
1514 * bother it and clean up the remains - Jean II */
1517 irttp_close_tsap(self);
1521 /* If we are here, we assume that is the higher layer is still
1522 * waiting for the disconnect notification and able to process it,
1523 * even if he tried to disconnect. Otherwise, it would have already
1524 * attempted to close the tsap and self->close_pend would be TRUE.
1527 /* No need to notify the client if has already tried to disconnect */
1528 if(self->notify.disconnect_indication)
1529 self->notify.disconnect_indication(self->notify.instance, self,
1537 * Function irttp_do_data_indication (self, skb)
1539 * Try to deliver reassebled skb to layer above, and requeue it if that
1540 * for some reason should fail. We mark rx sdu as busy to apply back
1541 * pressure is necessary.
1543 void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1547 /* Check if client has already tried to close the TSAP */
1548 if (self->close_pend) {
1553 err = self->notify.data_indication(self->notify.instance, self, skb);
1555 /* Usually the layer above will notify that it's input queue is
1556 * starting to get filled by using the flow request, but this may
1557 * be difficult, so it can instead just refuse to eat it and just
1558 * give an error back
1560 if (err == -ENOMEM) {
1561 IRDA_DEBUG(0, "%s() requeueing skb!\n", __FUNCTION__);
1563 /* Make sure we take a break */
1564 self->rx_sdu_busy = TRUE;
1566 /* Need to push the header in again */
1567 skb_push(skb, TTP_HEADER);
1568 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1570 /* Put skb back on queue */
1571 skb_queue_head(&self->rx_queue, skb);
1576 * Function irttp_run_rx_queue (self)
1578 * Check if we have any frames to be transmitted, or if we have any
1579 * available credit to give away.
1581 void irttp_run_rx_queue(struct tsap_cb *self)
1583 struct sk_buff *skb;
1586 IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
1587 self->send_credit, self->avail_credit, self->remote_credit);
1589 /* Get exclusive access to the rx queue, otherwise don't touch it */
1590 if (irda_lock(&self->rx_queue_lock) == FALSE)
1594 * Reassemble all frames in receive queue and deliver them
1596 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1597 /* This bit will tell us if it's the last fragment or not */
1598 more = skb->data[0] & 0x80;
1600 /* Remove TTP header */
1601 skb_pull(skb, TTP_HEADER);
1603 /* Add the length of the remaining data */
1604 self->rx_sdu_size += skb->len;
1607 * If SAR is disabled, or user has requested no reassembly
1608 * of received fragments then we just deliver them
1609 * immediately. This can be requested by clients that
1610 * implements byte streams without any message boundaries
1612 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1613 irttp_do_data_indication(self, skb);
1614 self->rx_sdu_size = 0;
1619 /* Check if this is a fragment, and not the last fragment */
1622 * Queue the fragment if we still are within the
1623 * limits of the maximum size of the rx_sdu
1625 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1626 IRDA_DEBUG(4, "%s(), queueing frag\n", __FUNCTION__);
1627 skb_queue_tail(&self->rx_fragments, skb);
1629 /* Free the part of the SDU that is too big */
1635 * This is the last fragment, so time to reassemble!
1637 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1638 (self->rx_max_sdu_size == TTP_SAR_UNBOUND))
1641 * A little optimizing. Only queue the fragment if
1642 * there are other fragments. Since if this is the
1643 * last and only fragment, there is no need to
1646 if (!skb_queue_empty(&self->rx_fragments)) {
1647 skb_queue_tail(&self->rx_fragments,
1650 skb = irttp_reassemble_skb(self);
1653 /* Now we can deliver the reassembled skb */
1654 irttp_do_data_indication(self, skb);
1656 IRDA_DEBUG(1, "%s(), Truncated frame\n", __FUNCTION__);
1658 /* Free the part of the SDU that is too big */
1661 /* Deliver only the valid but truncated part of SDU */
1662 skb = irttp_reassemble_skb(self);
1664 irttp_do_data_indication(self, skb);
1666 self->rx_sdu_size = 0;
1670 * It's not trivial to keep track of how many credits are available
1671 * by incrementing at each packet, because delivery may fail
1672 * (irttp_do_data_indication() may requeue the frame) and because
1673 * we need to take care of fragmentation.
1674 * We want the other side to send up to initial_credit packets.
1675 * We have some frames in our queues, and we have already allowed it
1676 * to send remote_credit.
1677 * No need to spinlock, write is atomic and self correcting...
1680 self->avail_credit = (self->initial_credit -
1681 (self->remote_credit +
1682 skb_queue_len(&self->rx_queue) +
1683 skb_queue_len(&self->rx_fragments)));
1685 /* Do we have too much credits to send to peer ? */
1686 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1687 (self->avail_credit > 0)) {
1688 /* Send explicit credit frame */
1689 irttp_give_credit(self);
1690 /* Note : do *NOT* check if tx_queue is non-empty, that
1691 * will produce deadlocks. I repeat : send a credit frame
1692 * even if we have something to send in our Tx queue.
1693 * If we have credits, it means that our Tx queue is blocked.
1695 * Let's suppose the peer can't keep up with our Tx. He will
1696 * flow control us by not sending us any credits, and we
1697 * will stop Tx and start accumulating credits here.
1698 * Up to the point where the peer will stop its Tx queue,
1699 * for lack of credits.
1700 * Let's assume the peer application is single threaded.
1701 * It will block on Tx and never consume any Rx buffer.
1702 * Deadlock. Guaranteed. - Jean II
1707 self->rx_queue_lock = 0;
1710 #ifdef CONFIG_PROC_FS
1712 * Function irttp_proc_read (buf, start, offset, len, unused)
1714 * Give some info to the /proc file system
1716 int irttp_proc_read(char *buf, char **start, off_t offset, int len)
1718 struct tsap_cb *self;
1719 unsigned long flags;
1722 ASSERT(irttp != NULL, return 0;);
1729 self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1730 while (self != NULL) {
1731 if (!self || self->magic != TTP_TSAP_MAGIC)
1734 len += sprintf(buf+len, "TSAP %d, ", i++);
1735 len += sprintf(buf+len, "stsap_sel: %02x, ",
1737 len += sprintf(buf+len, "dtsap_sel: %02x\n",
1739 len += sprintf(buf+len, " connected: %s, ",
1740 self->connected? "TRUE":"FALSE");
1741 len += sprintf(buf+len, "avail credit: %d, ",
1742 self->avail_credit);
1743 len += sprintf(buf+len, "remote credit: %d, ",
1744 self->remote_credit);
1745 len += sprintf(buf+len, "send credit: %d\n",
1747 len += sprintf(buf+len, " tx packets: %ld, ",
1748 self->stats.tx_packets);
1749 len += sprintf(buf+len, "rx packets: %ld, ",
1750 self->stats.rx_packets);
1751 len += sprintf(buf+len, "tx_queue len: %d ",
1752 skb_queue_len(&self->tx_queue));
1753 len += sprintf(buf+len, "rx_queue len: %d\n",
1754 skb_queue_len(&self->rx_queue));
1755 len += sprintf(buf+len, " tx_sdu_busy: %s, ",
1756 self->tx_sdu_busy? "TRUE":"FALSE");
1757 len += sprintf(buf+len, "rx_sdu_busy: %s\n",
1758 self->rx_sdu_busy? "TRUE":"FALSE");
1759 len += sprintf(buf+len, " max_seg_size: %d, ",
1760 self->max_seg_size);
1761 len += sprintf(buf+len, "tx_max_sdu_size: %d, ",
1762 self->tx_max_sdu_size);
1763 len += sprintf(buf+len, "rx_max_sdu_size: %d\n",
1764 self->rx_max_sdu_size);
1766 len += sprintf(buf+len, " Used by (%s)\n",
1769 len += sprintf(buf+len, "\n");
1771 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps);
1773 restore_flags(flags);
1778 #endif /* PROC_FS */