per conn neighbor stall timeout removal
[cor_2_6_31.git] / net / irda / irttp.c
blob9cb79f95bf63953c9bcdf2b379b4ccbb37089ff5
1 /*********************************************************************
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>
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>
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/skbuff.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/seq_file.h>
32 #include <asm/byteorder.h>
33 #include <asm/unaligned.h>
35 #include <net/irda/irda.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;
43 static void __irttp_close_tsap(struct tsap_cb *self);
45 static int irttp_data_indication(void *instance, void *sap,
46 struct sk_buff *skb);
47 static int irttp_udata_indication(void *instance, void *sap,
48 struct sk_buff *skb);
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,
65 int get);
67 static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow);
68 static void irttp_status_indication(void *instance,
69 LINK_STATUS link, LOCK_STATUS lock);
71 /* Information for parsing parameters in IrTTP */
72 static pi_minor_info_t pi_minor_call_table[] = {
73 { NULL, 0 }, /* 0x00 */
74 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
76 static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
77 static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
79 /************************ GLOBAL PROCEDURES ************************/
82 * Function irttp_init (void)
84 * Initialize the IrTTP layer. Called by module initialization code
87 int __init irttp_init(void)
89 irttp = kzalloc(sizeof(struct irttp_cb), GFP_KERNEL);
90 if (irttp == NULL)
91 return -ENOMEM;
93 irttp->magic = TTP_MAGIC;
95 irttp->tsaps = hashbin_new(HB_LOCK);
96 if (!irttp->tsaps) {
97 IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n",
98 __func__);
99 kfree(irttp);
100 return -ENOMEM;
103 return 0;
107 * Function irttp_cleanup (void)
109 * Called by module destruction/cleanup code
112 void irttp_cleanup(void)
114 /* Check for main structure */
115 IRDA_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);
122 irttp->magic = 0;
124 /* De-allocate main structure */
125 kfree(irttp);
127 irttp = NULL;
130 /*************************** SUBROUTINES ***************************/
133 * Function irttp_start_todo_timer (self, timeout)
135 * Start todo timer.
137 * Made it more effient and unsensitive to race conditions - Jean II
139 static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
141 /* Set new value for timer */
142 mod_timer(&self->todo_timer, jiffies + timeout);
146 * Function irttp_todo_expired (data)
148 * Todo timer has expired!
150 * One of the restriction of the timer is that it is run only on the timer
151 * interrupt which run every 10ms. This mean that even if you set the timer
152 * with a delay of 0, it may take up to 10ms before it's run.
153 * So, to minimise latency and keep cache fresh, we try to avoid using
154 * it as much as possible.
155 * Note : we can't use tasklets, because they can't be asynchronously
156 * killed (need user context), and we can't guarantee that here...
157 * Jean II
159 static void irttp_todo_expired(unsigned long data)
161 struct tsap_cb *self = (struct tsap_cb *) data;
163 /* Check that we still exist */
164 if (!self || self->magic != TTP_TSAP_MAGIC)
165 return;
167 IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self);
169 /* Try to make some progress, especially on Tx side - Jean II */
170 irttp_run_rx_queue(self);
171 irttp_run_tx_queue(self);
173 /* Check if time for disconnect */
174 if (test_bit(0, &self->disconnect_pend)) {
175 /* Check if it's possible to disconnect yet */
176 if (skb_queue_empty(&self->tx_queue)) {
177 /* Make sure disconnect is not pending anymore */
178 clear_bit(0, &self->disconnect_pend); /* FALSE */
180 /* Note : self->disconnect_skb may be NULL */
181 irttp_disconnect_request(self, self->disconnect_skb,
182 P_NORMAL);
183 self->disconnect_skb = NULL;
184 } else {
185 /* Try again later */
186 irttp_start_todo_timer(self, HZ/10);
188 /* No reason to try and close now */
189 return;
193 /* Check if it's closing time */
194 if (self->close_pend)
195 /* Finish cleanup */
196 irttp_close_tsap(self);
200 * Function irttp_flush_queues (self)
202 * Flushes (removes all frames) in transitt-buffer (tx_list)
204 static void irttp_flush_queues(struct tsap_cb *self)
206 struct sk_buff* skb;
208 IRDA_DEBUG(4, "%s()\n", __func__);
210 IRDA_ASSERT(self != NULL, return;);
211 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
213 /* Deallocate frames waiting to be sent */
214 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
215 dev_kfree_skb(skb);
217 /* Deallocate received frames */
218 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
219 dev_kfree_skb(skb);
221 /* Deallocate received fragments */
222 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
223 dev_kfree_skb(skb);
227 * Function irttp_reassemble (self)
229 * Makes a new (continuous) skb of all the fragments in the fragment
230 * queue
233 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
235 struct sk_buff *skb, *frag;
236 int n = 0; /* Fragment index */
238 IRDA_ASSERT(self != NULL, return NULL;);
239 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
241 IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __func__,
242 self->rx_sdu_size);
244 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
245 if (!skb)
246 return NULL;
249 * Need to reserve space for TTP header in case this skb needs to
250 * be requeued in case delivery failes
252 skb_reserve(skb, TTP_HEADER);
253 skb_put(skb, self->rx_sdu_size);
256 * Copy all fragments to a new buffer
258 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
259 skb_copy_to_linear_data_offset(skb, n, frag->data, frag->len);
260 n += frag->len;
262 dev_kfree_skb(frag);
265 IRDA_DEBUG(2,
266 "%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
267 __func__, n, self->rx_sdu_size, self->rx_max_sdu_size);
268 /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size
269 * by summing the size of all fragments, so we should always
270 * have n == self->rx_sdu_size, except in cases where we
271 * droped the last fragment (when self->rx_sdu_size exceed
272 * self->rx_max_sdu_size), where n < self->rx_sdu_size.
273 * Jean II */
274 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
276 /* Set the new length */
277 skb_trim(skb, n);
279 self->rx_sdu_size = 0;
281 return skb;
285 * Function irttp_fragment_skb (skb)
287 * Fragments a frame and queues all the fragments for transmission
290 static inline void irttp_fragment_skb(struct tsap_cb *self,
291 struct sk_buff *skb)
293 struct sk_buff *frag;
294 __u8 *frame;
296 IRDA_DEBUG(2, "%s()\n", __func__);
298 IRDA_ASSERT(self != NULL, return;);
299 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
300 IRDA_ASSERT(skb != NULL, return;);
303 * Split frame into a number of segments
305 while (skb->len > self->max_seg_size) {
306 IRDA_DEBUG(2, "%s(), fragmenting ...\n", __func__);
308 /* Make new segment */
309 frag = alloc_skb(self->max_seg_size+self->max_header_size,
310 GFP_ATOMIC);
311 if (!frag)
312 return;
314 skb_reserve(frag, self->max_header_size);
316 /* Copy data from the original skb into this fragment. */
317 skb_copy_from_linear_data(skb, skb_put(frag, self->max_seg_size),
318 self->max_seg_size);
320 /* Insert TTP header, with the more bit set */
321 frame = skb_push(frag, TTP_HEADER);
322 frame[0] = TTP_MORE;
324 /* Hide the copied data from the original skb */
325 skb_pull(skb, self->max_seg_size);
327 /* Queue fragment */
328 skb_queue_tail(&self->tx_queue, frag);
330 /* Queue what is left of the original skb */
331 IRDA_DEBUG(2, "%s(), queuing last segment\n", __func__);
333 frame = skb_push(skb, TTP_HEADER);
334 frame[0] = 0x00; /* Clear more bit */
336 /* Queue fragment */
337 skb_queue_tail(&self->tx_queue, skb);
341 * Function irttp_param_max_sdu_size (self, param)
343 * Handle the MaxSduSize parameter in the connect frames, this function
344 * will be called both when this parameter needs to be inserted into, and
345 * extracted from the connect frames
347 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
348 int get)
350 struct tsap_cb *self;
352 self = (struct tsap_cb *) instance;
354 IRDA_ASSERT(self != NULL, return -1;);
355 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
357 if (get)
358 param->pv.i = self->tx_max_sdu_size;
359 else
360 self->tx_max_sdu_size = param->pv.i;
362 IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __func__, param->pv.i);
364 return 0;
367 /*************************** CLIENT CALLS ***************************/
368 /************************** LMP CALLBACKS **************************/
369 /* Everything is happily mixed up. Waiting for next clean up - Jean II */
372 * Initialization, that has to be done on new tsap
373 * instance allocation and on duplication
375 static void irttp_init_tsap(struct tsap_cb *tsap)
377 spin_lock_init(&tsap->lock);
378 init_timer(&tsap->todo_timer);
380 skb_queue_head_init(&tsap->rx_queue);
381 skb_queue_head_init(&tsap->tx_queue);
382 skb_queue_head_init(&tsap->rx_fragments);
386 * Function irttp_open_tsap (stsap, notify)
388 * Create TSAP connection endpoint,
390 struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
392 struct tsap_cb *self;
393 struct lsap_cb *lsap;
394 notify_t ttp_notify;
396 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
398 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
399 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
400 * JeanII */
401 if((stsap_sel != LSAP_ANY) &&
402 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
403 IRDA_DEBUG(0, "%s(), invalid tsap!\n", __func__);
404 return NULL;
407 self = kzalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
408 if (self == NULL) {
409 IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __func__);
410 return NULL;
413 /* Initialize internal objects */
414 irttp_init_tsap(self);
416 /* Initialise todo timer */
417 self->todo_timer.data = (unsigned long) self;
418 self->todo_timer.function = &irttp_todo_expired;
420 /* Initialize callbacks for IrLMP to use */
421 irda_notify_init(&ttp_notify);
422 ttp_notify.connect_confirm = irttp_connect_confirm;
423 ttp_notify.connect_indication = irttp_connect_indication;
424 ttp_notify.disconnect_indication = irttp_disconnect_indication;
425 ttp_notify.data_indication = irttp_data_indication;
426 ttp_notify.udata_indication = irttp_udata_indication;
427 ttp_notify.flow_indication = irttp_flow_indication;
428 if(notify->status_indication != NULL)
429 ttp_notify.status_indication = irttp_status_indication;
430 ttp_notify.instance = self;
431 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
433 self->magic = TTP_TSAP_MAGIC;
434 self->connected = FALSE;
437 * Create LSAP at IrLMP layer
439 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
440 if (lsap == NULL) {
441 IRDA_WARNING("%s: unable to allocate LSAP!!\n", __func__);
442 return NULL;
446 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
447 * will replace it with whatever source selector which is free, so
448 * the stsap_sel we have might not be valid anymore
450 self->stsap_sel = lsap->slsap_sel;
451 IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __func__, self->stsap_sel);
453 self->notify = *notify;
454 self->lsap = lsap;
456 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
458 if (credit > TTP_RX_MAX_CREDIT)
459 self->initial_credit = TTP_RX_MAX_CREDIT;
460 else
461 self->initial_credit = credit;
463 return self;
465 EXPORT_SYMBOL(irttp_open_tsap);
468 * Function irttp_close (handle)
470 * Remove an instance of a TSAP. This function should only deal with the
471 * deallocation of the TSAP, and resetting of the TSAPs values;
474 static void __irttp_close_tsap(struct tsap_cb *self)
476 /* First make sure we're connected. */
477 IRDA_ASSERT(self != NULL, return;);
478 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
480 irttp_flush_queues(self);
482 del_timer(&self->todo_timer);
484 /* This one won't be cleaned up if we are disconnect_pend + close_pend
485 * and we receive a disconnect_indication */
486 if (self->disconnect_skb)
487 dev_kfree_skb(self->disconnect_skb);
489 self->connected = FALSE;
490 self->magic = ~TTP_TSAP_MAGIC;
492 kfree(self);
496 * Function irttp_close (self)
498 * Remove TSAP from list of all TSAPs and then deallocate all resources
499 * associated with this TSAP
501 * Note : because we *free* the tsap structure, it is the responsibility
502 * of the caller to make sure we are called only once and to deal with
503 * possible race conditions. - Jean II
505 int irttp_close_tsap(struct tsap_cb *self)
507 struct tsap_cb *tsap;
509 IRDA_DEBUG(4, "%s()\n", __func__);
511 IRDA_ASSERT(self != NULL, return -1;);
512 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
514 /* Make sure tsap has been disconnected */
515 if (self->connected) {
516 /* Check if disconnect is not pending */
517 if (!test_bit(0, &self->disconnect_pend)) {
518 IRDA_WARNING("%s: TSAP still connected!\n",
519 __func__);
520 irttp_disconnect_request(self, NULL, P_NORMAL);
522 self->close_pend = TRUE;
523 irttp_start_todo_timer(self, HZ/10);
525 return 0; /* Will be back! */
528 tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);
530 IRDA_ASSERT(tsap == self, return -1;);
532 /* Close corresponding LSAP */
533 if (self->lsap) {
534 irlmp_close_lsap(self->lsap);
535 self->lsap = NULL;
538 __irttp_close_tsap(self);
540 return 0;
542 EXPORT_SYMBOL(irttp_close_tsap);
545 * Function irttp_udata_request (self, skb)
547 * Send unreliable data on this TSAP
550 int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
552 IRDA_ASSERT(self != NULL, return -1;);
553 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
554 IRDA_ASSERT(skb != NULL, return -1;);
556 IRDA_DEBUG(4, "%s()\n", __func__);
558 /* Check that nothing bad happens */
559 if ((skb->len == 0) || (!self->connected)) {
560 IRDA_DEBUG(1, "%s(), No data, or not connected\n",
561 __func__);
562 goto err;
565 if (skb->len > self->max_seg_size) {
566 IRDA_DEBUG(1, "%s(), UData is too large for IrLAP!\n",
567 __func__);
568 goto err;
571 irlmp_udata_request(self->lsap, skb);
572 self->stats.tx_packets++;
574 return 0;
576 err:
577 dev_kfree_skb(skb);
578 return -1;
580 EXPORT_SYMBOL(irttp_udata_request);
584 * Function irttp_data_request (handle, skb)
586 * Queue frame for transmission. If SAR is enabled, fragement the frame
587 * and queue the fragments for transmission
589 int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
591 __u8 *frame;
592 int ret;
594 IRDA_ASSERT(self != NULL, return -1;);
595 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
596 IRDA_ASSERT(skb != NULL, return -1;);
598 IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__,
599 skb_queue_len(&self->tx_queue));
601 /* Check that nothing bad happens */
602 if ((skb->len == 0) || (!self->connected)) {
603 IRDA_WARNING("%s: No data, or not connected\n", __func__);
604 ret = -ENOTCONN;
605 goto err;
609 * Check if SAR is disabled, and the frame is larger than what fits
610 * inside an IrLAP frame
612 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
613 IRDA_ERROR("%s: SAR disabled, and data is too large for IrLAP!\n",
614 __func__);
615 ret = -EMSGSIZE;
616 goto err;
620 * Check if SAR is enabled, and the frame is larger than the
621 * TxMaxSduSize
623 if ((self->tx_max_sdu_size != 0) &&
624 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
625 (skb->len > self->tx_max_sdu_size))
627 IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
628 __func__);
629 ret = -EMSGSIZE;
630 goto err;
633 * Check if transmit queue is full
635 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
637 * Give it a chance to empty itself
639 irttp_run_tx_queue(self);
641 /* Drop packet. This error code should trigger the caller
642 * to resend the data in the client code - Jean II */
643 ret = -ENOBUFS;
644 goto err;
647 /* Queue frame, or queue frame segments */
648 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
649 /* Queue frame */
650 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
651 frame = skb_push(skb, TTP_HEADER);
652 frame[0] = 0x00; /* Clear more bit */
654 skb_queue_tail(&self->tx_queue, skb);
655 } else {
657 * Fragment the frame, this function will also queue the
658 * fragments, we don't care about the fact the transmit
659 * queue may be overfilled by all the segments for a little
660 * while
662 irttp_fragment_skb(self, skb);
665 /* Check if we can accept more data from client */
666 if ((!self->tx_sdu_busy) &&
667 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
668 /* Tx queue filling up, so stop client. */
669 if (self->notify.flow_indication) {
670 self->notify.flow_indication(self->notify.instance,
671 self, FLOW_STOP);
673 /* self->tx_sdu_busy is the state of the client.
674 * Update state after notifying client to avoid
675 * race condition with irttp_flow_indication().
676 * If the queue empty itself after our test but before
677 * we set the flag, we will fix ourselves below in
678 * irttp_run_tx_queue().
679 * Jean II */
680 self->tx_sdu_busy = TRUE;
683 /* Try to make some progress */
684 irttp_run_tx_queue(self);
686 return 0;
688 err:
689 dev_kfree_skb(skb);
690 return ret;
692 EXPORT_SYMBOL(irttp_data_request);
695 * Function irttp_run_tx_queue (self)
697 * Transmit packets queued for transmission (if possible)
700 static void irttp_run_tx_queue(struct tsap_cb *self)
702 struct sk_buff *skb;
703 unsigned long flags;
704 int n;
706 IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n",
707 __func__,
708 self->send_credit, skb_queue_len(&self->tx_queue));
710 /* Get exclusive access to the tx queue, otherwise don't touch it */
711 if (irda_lock(&self->tx_queue_lock) == FALSE)
712 return;
714 /* Try to send out frames as long as we have credits
715 * and as long as LAP is not full. If LAP is full, it will
716 * poll us through irttp_flow_indication() - Jean II */
717 while ((self->send_credit > 0) &&
718 (!irlmp_lap_tx_queue_full(self->lsap)) &&
719 (skb = skb_dequeue(&self->tx_queue)))
722 * Since we can transmit and receive frames concurrently,
723 * the code below is a critical region and we must assure that
724 * nobody messes with the credits while we update them.
726 spin_lock_irqsave(&self->lock, flags);
728 n = self->avail_credit;
729 self->avail_credit = 0;
731 /* Only room for 127 credits in frame */
732 if (n > 127) {
733 self->avail_credit = n-127;
734 n = 127;
736 self->remote_credit += n;
737 self->send_credit--;
739 spin_unlock_irqrestore(&self->lock, flags);
742 * More bit must be set by the data_request() or fragment()
743 * functions
745 skb->data[0] |= (n & 0x7f);
747 /* Detach from socket.
748 * The current skb has a reference to the socket that sent
749 * it (skb->sk). When we pass it to IrLMP, the skb will be
750 * stored in in IrLAP (self->wx_list). When we are within
751 * IrLAP, we lose the notion of socket, so we should not
752 * have a reference to a socket. So, we drop it here.
754 * Why does it matter ?
755 * When the skb is freed (kfree_skb), if it is associated
756 * with a socket, it release buffer space on the socket
757 * (through sock_wfree() and sock_def_write_space()).
758 * If the socket no longer exist, we may crash. Hard.
759 * When we close a socket, we make sure that associated packets
760 * in IrTTP are freed. However, we have no way to cancel
761 * the packet that we have passed to IrLAP. So, if a packet
762 * remains in IrLAP (retry on the link or else) after we
763 * close the socket, we are dead !
764 * Jean II */
765 if (skb->sk != NULL) {
766 /* IrSOCK application, IrOBEX, ... */
767 skb_orphan(skb);
769 /* IrCOMM over IrTTP, IrLAN, ... */
771 /* Pass the skb to IrLMP - done */
772 irlmp_data_request(self->lsap, skb);
773 self->stats.tx_packets++;
776 /* Check if we can accept more frames from client.
777 * We don't want to wait until the todo timer to do that, and we
778 * can't use tasklets (grr...), so we are obliged to give control
779 * to client. That's ok, this test will be true not too often
780 * (max once per LAP window) and we are called from places
781 * where we can spend a bit of time doing stuff. - Jean II */
782 if ((self->tx_sdu_busy) &&
783 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
784 (!self->close_pend))
786 if (self->notify.flow_indication)
787 self->notify.flow_indication(self->notify.instance,
788 self, FLOW_START);
790 /* self->tx_sdu_busy is the state of the client.
791 * We don't really have a race here, but it's always safer
792 * to update our state after the client - Jean II */
793 self->tx_sdu_busy = FALSE;
796 /* Reset lock */
797 self->tx_queue_lock = 0;
801 * Function irttp_give_credit (self)
803 * Send a dataless flowdata TTP-PDU and give available credit to peer
804 * TSAP
806 static inline void irttp_give_credit(struct tsap_cb *self)
808 struct sk_buff *tx_skb = NULL;
809 unsigned long flags;
810 int n;
812 IRDA_ASSERT(self != NULL, return;);
813 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
815 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n",
816 __func__,
817 self->send_credit, self->avail_credit, self->remote_credit);
819 /* Give credit to peer */
820 tx_skb = alloc_skb(TTP_MAX_HEADER, GFP_ATOMIC);
821 if (!tx_skb)
822 return;
824 /* Reserve space for LMP, and LAP header */
825 skb_reserve(tx_skb, LMP_MAX_HEADER);
828 * Since we can transmit and receive frames concurrently,
829 * the code below is a critical region and we must assure that
830 * nobody messes with the credits while we update them.
832 spin_lock_irqsave(&self->lock, flags);
834 n = self->avail_credit;
835 self->avail_credit = 0;
837 /* Only space for 127 credits in frame */
838 if (n > 127) {
839 self->avail_credit = n - 127;
840 n = 127;
842 self->remote_credit += n;
844 spin_unlock_irqrestore(&self->lock, flags);
846 skb_put(tx_skb, 1);
847 tx_skb->data[0] = (__u8) (n & 0x7f);
849 irlmp_data_request(self->lsap, tx_skb);
850 self->stats.tx_packets++;
854 * Function irttp_udata_indication (instance, sap, skb)
856 * Received some unit-data (unreliable)
859 static int irttp_udata_indication(void *instance, void *sap,
860 struct sk_buff *skb)
862 struct tsap_cb *self;
863 int err;
865 IRDA_DEBUG(4, "%s()\n", __func__);
867 self = (struct tsap_cb *) instance;
869 IRDA_ASSERT(self != NULL, return -1;);
870 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
871 IRDA_ASSERT(skb != NULL, return -1;);
873 self->stats.rx_packets++;
875 /* Just pass data to layer above */
876 if (self->notify.udata_indication) {
877 err = self->notify.udata_indication(self->notify.instance,
878 self,skb);
879 /* Same comment as in irttp_do_data_indication() */
880 if (!err)
881 return 0;
883 /* Either no handler, or handler returns an error */
884 dev_kfree_skb(skb);
886 return 0;
890 * Function irttp_data_indication (instance, sap, skb)
892 * Receive segment from IrLMP.
895 static int irttp_data_indication(void *instance, void *sap,
896 struct sk_buff *skb)
898 struct tsap_cb *self;
899 unsigned long flags;
900 int n;
902 self = (struct tsap_cb *) instance;
904 n = skb->data[0] & 0x7f; /* Extract the credits */
906 self->stats.rx_packets++;
908 /* Deal with inbound credit
909 * Since we can transmit and receive frames concurrently,
910 * the code below is a critical region and we must assure that
911 * nobody messes with the credits while we update them.
913 spin_lock_irqsave(&self->lock, flags);
914 self->send_credit += n;
915 if (skb->len > 1)
916 self->remote_credit--;
917 spin_unlock_irqrestore(&self->lock, flags);
920 * Data or dataless packet? Dataless frames contains only the
921 * TTP_HEADER.
923 if (skb->len > 1) {
925 * We don't remove the TTP header, since we must preserve the
926 * more bit, so the defragment routing knows what to do
928 skb_queue_tail(&self->rx_queue, skb);
929 } else {
930 /* Dataless flowdata TTP-PDU */
931 dev_kfree_skb(skb);
935 /* Push data to the higher layer.
936 * We do it synchronously because running the todo timer for each
937 * receive packet would be too much overhead and latency.
938 * By passing control to the higher layer, we run the risk that
939 * it may take time or grab a lock. Most often, the higher layer
940 * will only put packet in a queue.
941 * Anyway, packets are only dripping through the IrDA, so we can
942 * have time before the next packet.
943 * Further, we are run from NET_BH, so the worse that can happen is
944 * us missing the optimal time to send back the PF bit in LAP.
945 * Jean II */
946 irttp_run_rx_queue(self);
948 /* We now give credits to peer in irttp_run_rx_queue().
949 * We need to send credit *NOW*, otherwise we are going
950 * to miss the next Tx window. The todo timer may take
951 * a while before it's run... - Jean II */
954 * If the peer device has given us some credits and we didn't have
955 * anyone from before, then we need to shedule the tx queue.
956 * We need to do that because our Tx have stopped (so we may not
957 * get any LAP flow indication) and the user may be stopped as
958 * well. - Jean II
960 if (self->send_credit == n) {
961 /* Restart pushing stuff to LAP */
962 irttp_run_tx_queue(self);
963 /* Note : we don't want to schedule the todo timer
964 * because it has horrible latency. No tasklets
965 * because the tasklet API is broken. - Jean II */
968 return 0;
972 * Function irttp_status_indication (self, reason)
974 * Status_indication, just pass to the higher layer...
977 static void irttp_status_indication(void *instance,
978 LINK_STATUS link, LOCK_STATUS lock)
980 struct tsap_cb *self;
982 IRDA_DEBUG(4, "%s()\n", __func__);
984 self = (struct tsap_cb *) instance;
986 IRDA_ASSERT(self != NULL, return;);
987 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
989 /* Check if client has already closed the TSAP and gone away */
990 if (self->close_pend)
991 return;
994 * Inform service user if he has requested it
996 if (self->notify.status_indication != NULL)
997 self->notify.status_indication(self->notify.instance,
998 link, lock);
999 else
1000 IRDA_DEBUG(2, "%s(), no handler\n", __func__);
1004 * Function irttp_flow_indication (self, reason)
1006 * Flow_indication : IrLAP tells us to send more data.
1009 static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
1011 struct tsap_cb *self;
1013 self = (struct tsap_cb *) instance;
1015 IRDA_ASSERT(self != NULL, return;);
1016 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1018 IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self);
1020 /* We are "polled" directly from LAP, and the LAP want to fill
1021 * its Tx window. We want to do our best to send it data, so that
1022 * we maximise the window. On the other hand, we want to limit the
1023 * amount of work here so that LAP doesn't hang forever waiting
1024 * for packets. - Jean II */
1026 /* Try to send some packets. Currently, LAP calls us every time
1027 * there is one free slot, so we will send only one packet.
1028 * This allow the scheduler to do its round robin - Jean II */
1029 irttp_run_tx_queue(self);
1031 /* Note regarding the interraction with higher layer.
1032 * irttp_run_tx_queue() may call the client when its queue
1033 * start to empty, via notify.flow_indication(). Initially.
1034 * I wanted this to happen in a tasklet, to avoid client
1035 * grabbing the CPU, but we can't use tasklets safely. And timer
1036 * is definitely too slow.
1037 * This will happen only once per LAP window, and usually at
1038 * the third packet (unless window is smaller). LAP is still
1039 * doing mtt and sending first packet so it's sort of OK
1040 * to do that. Jean II */
1042 /* If we need to send disconnect. try to do it now */
1043 if(self->disconnect_pend)
1044 irttp_start_todo_timer(self, 0);
1048 * Function irttp_flow_request (self, command)
1050 * This function could be used by the upper layers to tell IrTTP to stop
1051 * delivering frames if the receive queues are starting to get full, or
1052 * to tell IrTTP to start delivering frames again.
1054 void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1056 IRDA_DEBUG(1, "%s()\n", __func__);
1058 IRDA_ASSERT(self != NULL, return;);
1059 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1061 switch (flow) {
1062 case FLOW_STOP:
1063 IRDA_DEBUG(1, "%s(), flow stop\n", __func__);
1064 self->rx_sdu_busy = TRUE;
1065 break;
1066 case FLOW_START:
1067 IRDA_DEBUG(1, "%s(), flow start\n", __func__);
1068 self->rx_sdu_busy = FALSE;
1070 /* Client say he can accept more data, try to free our
1071 * queues ASAP - Jean II */
1072 irttp_run_rx_queue(self);
1074 break;
1075 default:
1076 IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __func__);
1079 EXPORT_SYMBOL(irttp_flow_request);
1082 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
1084 * Try to connect to remote destination TSAP selector
1087 int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1088 __u32 saddr, __u32 daddr,
1089 struct qos_info *qos, __u32 max_sdu_size,
1090 struct sk_buff *userdata)
1092 struct sk_buff *tx_skb;
1093 __u8 *frame;
1094 __u8 n;
1096 IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __func__, max_sdu_size);
1098 IRDA_ASSERT(self != NULL, return -EBADR;);
1099 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1101 if (self->connected) {
1102 if(userdata)
1103 dev_kfree_skb(userdata);
1104 return -EISCONN;
1107 /* Any userdata supplied? */
1108 if (userdata == NULL) {
1109 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1110 GFP_ATOMIC);
1111 if (!tx_skb)
1112 return -ENOMEM;
1114 /* Reserve space for MUX_CONTROL and LAP header */
1115 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
1116 } else {
1117 tx_skb = userdata;
1119 * Check that the client has reserved enough space for
1120 * headers
1122 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1123 { dev_kfree_skb(userdata); return -1; } );
1126 /* Initialize connection parameters */
1127 self->connected = FALSE;
1128 self->avail_credit = 0;
1129 self->rx_max_sdu_size = max_sdu_size;
1130 self->rx_sdu_size = 0;
1131 self->rx_sdu_busy = FALSE;
1132 self->dtsap_sel = dtsap_sel;
1134 n = self->initial_credit;
1136 self->remote_credit = 0;
1137 self->send_credit = 0;
1140 * Give away max 127 credits for now
1142 if (n > 127) {
1143 self->avail_credit=n-127;
1144 n = 127;
1147 self->remote_credit = n;
1149 /* SAR enabled? */
1150 if (max_sdu_size > 0) {
1151 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1152 { dev_kfree_skb(tx_skb); return -1; } );
1154 /* Insert SAR parameters */
1155 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1157 frame[0] = TTP_PARAMETERS | n;
1158 frame[1] = 0x04; /* Length */
1159 frame[2] = 0x01; /* MaxSduSize */
1160 frame[3] = 0x02; /* Value length */
1162 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1163 (__be16 *)(frame+4));
1164 } else {
1165 /* Insert plain TTP header */
1166 frame = skb_push(tx_skb, TTP_HEADER);
1168 /* Insert initial credit in frame */
1169 frame[0] = n & 0x7f;
1172 /* Connect with IrLMP. No QoS parameters for now */
1173 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1174 tx_skb);
1176 EXPORT_SYMBOL(irttp_connect_request);
1179 * Function irttp_connect_confirm (handle, qos, skb)
1181 * Sevice user confirms TSAP connection with peer.
1184 static void irttp_connect_confirm(void *instance, void *sap,
1185 struct qos_info *qos, __u32 max_seg_size,
1186 __u8 max_header_size, struct sk_buff *skb)
1188 struct tsap_cb *self;
1189 int parameters;
1190 int ret;
1191 __u8 plen;
1192 __u8 n;
1194 IRDA_DEBUG(4, "%s()\n", __func__);
1196 self = (struct tsap_cb *) instance;
1198 IRDA_ASSERT(self != NULL, return;);
1199 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1200 IRDA_ASSERT(skb != NULL, return;);
1202 self->max_seg_size = max_seg_size - TTP_HEADER;
1203 self->max_header_size = max_header_size + TTP_HEADER;
1206 * Check if we have got some QoS parameters back! This should be the
1207 * negotiated QoS for the link.
1209 if (qos) {
1210 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
1211 qos->baud_rate.bits);
1212 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1213 qos->baud_rate.value);
1216 n = skb->data[0] & 0x7f;
1218 IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __func__, n);
1220 self->send_credit = n;
1221 self->tx_max_sdu_size = 0;
1222 self->connected = TRUE;
1224 parameters = skb->data[0] & 0x80;
1226 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1227 skb_pull(skb, TTP_HEADER);
1229 if (parameters) {
1230 plen = skb->data[0];
1232 ret = irda_param_extract_all(self, skb->data+1,
1233 IRDA_MIN(skb->len-1, plen),
1234 &param_info);
1236 /* Any errors in the parameter list? */
1237 if (ret < 0) {
1238 IRDA_WARNING("%s: error extracting parameters\n",
1239 __func__);
1240 dev_kfree_skb(skb);
1242 /* Do not accept this connection attempt */
1243 return;
1245 /* Remove parameters */
1246 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1249 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __func__,
1250 self->send_credit, self->avail_credit, self->remote_credit);
1252 IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __func__,
1253 self->tx_max_sdu_size);
1255 if (self->notify.connect_confirm) {
1256 self->notify.connect_confirm(self->notify.instance, self, qos,
1257 self->tx_max_sdu_size,
1258 self->max_header_size, skb);
1259 } else
1260 dev_kfree_skb(skb);
1264 * Function irttp_connect_indication (handle, skb)
1266 * Some other device is connecting to this TSAP
1269 static void irttp_connect_indication(void *instance, void *sap,
1270 struct qos_info *qos, __u32 max_seg_size, __u8 max_header_size,
1271 struct sk_buff *skb)
1273 struct tsap_cb *self;
1274 struct lsap_cb *lsap;
1275 int parameters;
1276 int ret;
1277 __u8 plen;
1278 __u8 n;
1280 self = (struct tsap_cb *) instance;
1282 IRDA_ASSERT(self != NULL, return;);
1283 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1284 IRDA_ASSERT(skb != NULL, return;);
1286 lsap = (struct lsap_cb *) sap;
1288 self->max_seg_size = max_seg_size - TTP_HEADER;
1289 self->max_header_size = max_header_size+TTP_HEADER;
1291 IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __func__, self->stsap_sel);
1293 /* Need to update dtsap_sel if its equal to LSAP_ANY */
1294 self->dtsap_sel = lsap->dlsap_sel;
1296 n = skb->data[0] & 0x7f;
1298 self->send_credit = n;
1299 self->tx_max_sdu_size = 0;
1301 parameters = skb->data[0] & 0x80;
1303 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1304 skb_pull(skb, TTP_HEADER);
1306 if (parameters) {
1307 plen = skb->data[0];
1309 ret = irda_param_extract_all(self, skb->data+1,
1310 IRDA_MIN(skb->len-1, plen),
1311 &param_info);
1313 /* Any errors in the parameter list? */
1314 if (ret < 0) {
1315 IRDA_WARNING("%s: error extracting parameters\n",
1316 __func__);
1317 dev_kfree_skb(skb);
1319 /* Do not accept this connection attempt */
1320 return;
1323 /* Remove parameters */
1324 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1327 if (self->notify.connect_indication) {
1328 self->notify.connect_indication(self->notify.instance, self,
1329 qos, self->tx_max_sdu_size,
1330 self->max_header_size, skb);
1331 } else
1332 dev_kfree_skb(skb);
1336 * Function irttp_connect_response (handle, userdata)
1338 * Service user is accepting the connection, just pass it down to
1339 * IrLMP!
1342 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1343 struct sk_buff *userdata)
1345 struct sk_buff *tx_skb;
1346 __u8 *frame;
1347 int ret;
1348 __u8 n;
1350 IRDA_ASSERT(self != NULL, return -1;);
1351 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1353 IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __func__,
1354 self->stsap_sel);
1356 /* Any userdata supplied? */
1357 if (userdata == NULL) {
1358 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1359 GFP_ATOMIC);
1360 if (!tx_skb)
1361 return -ENOMEM;
1363 /* Reserve space for MUX_CONTROL and LAP header */
1364 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
1365 } else {
1366 tx_skb = userdata;
1368 * Check that the client has reserved enough space for
1369 * headers
1371 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1372 { dev_kfree_skb(userdata); return -1; } );
1375 self->avail_credit = 0;
1376 self->remote_credit = 0;
1377 self->rx_max_sdu_size = max_sdu_size;
1378 self->rx_sdu_size = 0;
1379 self->rx_sdu_busy = FALSE;
1381 n = self->initial_credit;
1383 /* Frame has only space for max 127 credits (7 bits) */
1384 if (n > 127) {
1385 self->avail_credit = n - 127;
1386 n = 127;
1389 self->remote_credit = n;
1390 self->connected = TRUE;
1392 /* SAR enabled? */
1393 if (max_sdu_size > 0) {
1394 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1395 { dev_kfree_skb(tx_skb); return -1; } );
1397 /* Insert TTP header with SAR parameters */
1398 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1400 frame[0] = TTP_PARAMETERS | n;
1401 frame[1] = 0x04; /* Length */
1403 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
1404 /* TTP_SAR_HEADER, &param_info) */
1406 frame[2] = 0x01; /* MaxSduSize */
1407 frame[3] = 0x02; /* Value length */
1409 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1410 (__be16 *)(frame+4));
1411 } else {
1412 /* Insert TTP header */
1413 frame = skb_push(tx_skb, TTP_HEADER);
1415 frame[0] = n & 0x7f;
1418 ret = irlmp_connect_response(self->lsap, tx_skb);
1420 return ret;
1422 EXPORT_SYMBOL(irttp_connect_response);
1425 * Function irttp_dup (self, instance)
1427 * Duplicate TSAP, can be used by servers to confirm a connection on a
1428 * new TSAP so it can keep listening on the old one.
1430 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1432 struct tsap_cb *new;
1433 unsigned long flags;
1435 IRDA_DEBUG(1, "%s()\n", __func__);
1437 /* Protect our access to the old tsap instance */
1438 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
1440 /* Find the old instance */
1441 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
1442 IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __func__);
1443 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1444 return NULL;
1447 /* Allocate a new instance */
1448 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
1449 if (!new) {
1450 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __func__);
1451 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1452 return NULL;
1454 /* Dup */
1455 memcpy(new, orig, sizeof(struct tsap_cb));
1456 spin_lock_init(&new->lock);
1458 /* We don't need the old instance any more */
1459 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1461 /* Try to dup the LSAP (may fail if we were too slow) */
1462 new->lsap = irlmp_dup(orig->lsap, new);
1463 if (!new->lsap) {
1464 IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
1465 kfree(new);
1466 return NULL;
1469 /* Not everything should be copied */
1470 new->notify.instance = instance;
1472 /* Initialize internal objects */
1473 irttp_init_tsap(new);
1475 /* This is locked */
1476 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
1478 return new;
1480 EXPORT_SYMBOL(irttp_dup);
1483 * Function irttp_disconnect_request (self)
1485 * Close this connection please! If priority is high, the queued data
1486 * segments, if any, will be deallocated first
1489 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1490 int priority)
1492 int ret;
1494 IRDA_ASSERT(self != NULL, return -1;);
1495 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1497 /* Already disconnected? */
1498 if (!self->connected) {
1499 IRDA_DEBUG(4, "%s(), already disconnected!\n", __func__);
1500 if (userdata)
1501 dev_kfree_skb(userdata);
1502 return -1;
1505 /* Disconnect already pending ?
1506 * We need to use an atomic operation to prevent reentry. This
1507 * function may be called from various context, like user, timer
1508 * for following a disconnect_indication() (i.e. net_bh).
1509 * Jean II */
1510 if(test_and_set_bit(0, &self->disconnect_pend)) {
1511 IRDA_DEBUG(0, "%s(), disconnect already pending\n",
1512 __func__);
1513 if (userdata)
1514 dev_kfree_skb(userdata);
1516 /* Try to make some progress */
1517 irttp_run_tx_queue(self);
1518 return -1;
1522 * Check if there is still data segments in the transmit queue
1524 if (!skb_queue_empty(&self->tx_queue)) {
1525 if (priority == P_HIGH) {
1527 * No need to send the queued data, if we are
1528 * disconnecting right now since the data will
1529 * not have any usable connection to be sent on
1531 IRDA_DEBUG(1, "%s(): High priority!!()\n", __func__);
1532 irttp_flush_queues(self);
1533 } else if (priority == P_NORMAL) {
1535 * Must delay disconnect until after all data segments
1536 * have been sent and the tx_queue is empty
1538 /* We'll reuse this one later for the disconnect */
1539 self->disconnect_skb = userdata; /* May be NULL */
1541 irttp_run_tx_queue(self);
1543 irttp_start_todo_timer(self, HZ/10);
1544 return -1;
1547 /* Note : we don't need to check if self->rx_queue is full and the
1548 * state of self->rx_sdu_busy because the disconnect response will
1549 * be sent at the LMP level (so even if the peer has its Tx queue
1550 * full of data). - Jean II */
1552 IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __func__);
1553 self->connected = FALSE;
1555 if (!userdata) {
1556 struct sk_buff *tx_skb;
1557 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
1558 if (!tx_skb)
1559 return -ENOMEM;
1562 * Reserve space for MUX and LAP header
1564 skb_reserve(tx_skb, LMP_MAX_HEADER);
1566 userdata = tx_skb;
1568 ret = irlmp_disconnect_request(self->lsap, userdata);
1570 /* The disconnect is no longer pending */
1571 clear_bit(0, &self->disconnect_pend); /* FALSE */
1573 return ret;
1575 EXPORT_SYMBOL(irttp_disconnect_request);
1578 * Function irttp_disconnect_indication (self, reason)
1580 * Disconnect indication, TSAP disconnected by peer?
1583 static void irttp_disconnect_indication(void *instance, void *sap,
1584 LM_REASON reason, struct sk_buff *skb)
1586 struct tsap_cb *self;
1588 IRDA_DEBUG(4, "%s()\n", __func__);
1590 self = (struct tsap_cb *) instance;
1592 IRDA_ASSERT(self != NULL, return;);
1593 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1595 /* Prevent higher layer to send more data */
1596 self->connected = FALSE;
1598 /* Check if client has already tried to close the TSAP */
1599 if (self->close_pend) {
1600 /* In this case, the higher layer is probably gone. Don't
1601 * bother it and clean up the remains - Jean II */
1602 if (skb)
1603 dev_kfree_skb(skb);
1604 irttp_close_tsap(self);
1605 return;
1608 /* If we are here, we assume that is the higher layer is still
1609 * waiting for the disconnect notification and able to process it,
1610 * even if he tried to disconnect. Otherwise, it would have already
1611 * attempted to close the tsap and self->close_pend would be TRUE.
1612 * Jean II */
1614 /* No need to notify the client if has already tried to disconnect */
1615 if(self->notify.disconnect_indication)
1616 self->notify.disconnect_indication(self->notify.instance, self,
1617 reason, skb);
1618 else
1619 if (skb)
1620 dev_kfree_skb(skb);
1624 * Function irttp_do_data_indication (self, skb)
1626 * Try to deliver reassembled skb to layer above, and requeue it if that
1627 * for some reason should fail. We mark rx sdu as busy to apply back
1628 * pressure is necessary.
1630 static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1632 int err;
1634 /* Check if client has already closed the TSAP and gone away */
1635 if (self->close_pend) {
1636 dev_kfree_skb(skb);
1637 return;
1640 err = self->notify.data_indication(self->notify.instance, self, skb);
1642 /* Usually the layer above will notify that it's input queue is
1643 * starting to get filled by using the flow request, but this may
1644 * be difficult, so it can instead just refuse to eat it and just
1645 * give an error back
1647 if (err) {
1648 IRDA_DEBUG(0, "%s() requeueing skb!\n", __func__);
1650 /* Make sure we take a break */
1651 self->rx_sdu_busy = TRUE;
1653 /* Need to push the header in again */
1654 skb_push(skb, TTP_HEADER);
1655 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1657 /* Put skb back on queue */
1658 skb_queue_head(&self->rx_queue, skb);
1663 * Function irttp_run_rx_queue (self)
1665 * Check if we have any frames to be transmitted, or if we have any
1666 * available credit to give away.
1668 static void irttp_run_rx_queue(struct tsap_cb *self)
1670 struct sk_buff *skb;
1671 int more = 0;
1673 IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __func__,
1674 self->send_credit, self->avail_credit, self->remote_credit);
1676 /* Get exclusive access to the rx queue, otherwise don't touch it */
1677 if (irda_lock(&self->rx_queue_lock) == FALSE)
1678 return;
1681 * Reassemble all frames in receive queue and deliver them
1683 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1684 /* This bit will tell us if it's the last fragment or not */
1685 more = skb->data[0] & 0x80;
1687 /* Remove TTP header */
1688 skb_pull(skb, TTP_HEADER);
1690 /* Add the length of the remaining data */
1691 self->rx_sdu_size += skb->len;
1694 * If SAR is disabled, or user has requested no reassembly
1695 * of received fragments then we just deliver them
1696 * immediately. This can be requested by clients that
1697 * implements byte streams without any message boundaries
1699 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1700 irttp_do_data_indication(self, skb);
1701 self->rx_sdu_size = 0;
1703 continue;
1706 /* Check if this is a fragment, and not the last fragment */
1707 if (more) {
1709 * Queue the fragment if we still are within the
1710 * limits of the maximum size of the rx_sdu
1712 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1713 IRDA_DEBUG(4, "%s(), queueing frag\n",
1714 __func__);
1715 skb_queue_tail(&self->rx_fragments, skb);
1716 } else {
1717 /* Free the part of the SDU that is too big */
1718 dev_kfree_skb(skb);
1720 continue;
1723 * This is the last fragment, so time to reassemble!
1725 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1726 (self->rx_max_sdu_size == TTP_SAR_UNBOUND))
1729 * A little optimizing. Only queue the fragment if
1730 * there are other fragments. Since if this is the
1731 * last and only fragment, there is no need to
1732 * reassemble :-)
1734 if (!skb_queue_empty(&self->rx_fragments)) {
1735 skb_queue_tail(&self->rx_fragments,
1736 skb);
1738 skb = irttp_reassemble_skb(self);
1741 /* Now we can deliver the reassembled skb */
1742 irttp_do_data_indication(self, skb);
1743 } else {
1744 IRDA_DEBUG(1, "%s(), Truncated frame\n", __func__);
1746 /* Free the part of the SDU that is too big */
1747 dev_kfree_skb(skb);
1749 /* Deliver only the valid but truncated part of SDU */
1750 skb = irttp_reassemble_skb(self);
1752 irttp_do_data_indication(self, skb);
1754 self->rx_sdu_size = 0;
1758 * It's not trivial to keep track of how many credits are available
1759 * by incrementing at each packet, because delivery may fail
1760 * (irttp_do_data_indication() may requeue the frame) and because
1761 * we need to take care of fragmentation.
1762 * We want the other side to send up to initial_credit packets.
1763 * We have some frames in our queues, and we have already allowed it
1764 * to send remote_credit.
1765 * No need to spinlock, write is atomic and self correcting...
1766 * Jean II
1768 self->avail_credit = (self->initial_credit -
1769 (self->remote_credit +
1770 skb_queue_len(&self->rx_queue) +
1771 skb_queue_len(&self->rx_fragments)));
1773 /* Do we have too much credits to send to peer ? */
1774 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1775 (self->avail_credit > 0)) {
1776 /* Send explicit credit frame */
1777 irttp_give_credit(self);
1778 /* Note : do *NOT* check if tx_queue is non-empty, that
1779 * will produce deadlocks. I repeat : send a credit frame
1780 * even if we have something to send in our Tx queue.
1781 * If we have credits, it means that our Tx queue is blocked.
1783 * Let's suppose the peer can't keep up with our Tx. He will
1784 * flow control us by not sending us any credits, and we
1785 * will stop Tx and start accumulating credits here.
1786 * Up to the point where the peer will stop its Tx queue,
1787 * for lack of credits.
1788 * Let's assume the peer application is single threaded.
1789 * It will block on Tx and never consume any Rx buffer.
1790 * Deadlock. Guaranteed. - Jean II
1794 /* Reset lock */
1795 self->rx_queue_lock = 0;
1798 #ifdef CONFIG_PROC_FS
1799 struct irttp_iter_state {
1800 int id;
1803 static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
1805 struct irttp_iter_state *iter = seq->private;
1806 struct tsap_cb *self;
1808 /* Protect our access to the tsap list */
1809 spin_lock_irq(&irttp->tsaps->hb_spinlock);
1810 iter->id = 0;
1812 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1813 self != NULL;
1814 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
1815 if (iter->id == *pos)
1816 break;
1817 ++iter->id;
1820 return self;
1823 static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1825 struct irttp_iter_state *iter = seq->private;
1827 ++*pos;
1828 ++iter->id;
1829 return (void *) hashbin_get_next(irttp->tsaps);
1832 static void irttp_seq_stop(struct seq_file *seq, void *v)
1834 spin_unlock_irq(&irttp->tsaps->hb_spinlock);
1837 static int irttp_seq_show(struct seq_file *seq, void *v)
1839 const struct irttp_iter_state *iter = seq->private;
1840 const struct tsap_cb *self = v;
1842 seq_printf(seq, "TSAP %d, ", iter->id);
1843 seq_printf(seq, "stsap_sel: %02x, ",
1844 self->stsap_sel);
1845 seq_printf(seq, "dtsap_sel: %02x\n",
1846 self->dtsap_sel);
1847 seq_printf(seq, " connected: %s, ",
1848 self->connected? "TRUE":"FALSE");
1849 seq_printf(seq, "avail credit: %d, ",
1850 self->avail_credit);
1851 seq_printf(seq, "remote credit: %d, ",
1852 self->remote_credit);
1853 seq_printf(seq, "send credit: %d\n",
1854 self->send_credit);
1855 seq_printf(seq, " tx packets: %ld, ",
1856 self->stats.tx_packets);
1857 seq_printf(seq, "rx packets: %ld, ",
1858 self->stats.rx_packets);
1859 seq_printf(seq, "tx_queue len: %d ",
1860 skb_queue_len(&self->tx_queue));
1861 seq_printf(seq, "rx_queue len: %d\n",
1862 skb_queue_len(&self->rx_queue));
1863 seq_printf(seq, " tx_sdu_busy: %s, ",
1864 self->tx_sdu_busy? "TRUE":"FALSE");
1865 seq_printf(seq, "rx_sdu_busy: %s\n",
1866 self->rx_sdu_busy? "TRUE":"FALSE");
1867 seq_printf(seq, " max_seg_size: %d, ",
1868 self->max_seg_size);
1869 seq_printf(seq, "tx_max_sdu_size: %d, ",
1870 self->tx_max_sdu_size);
1871 seq_printf(seq, "rx_max_sdu_size: %d\n",
1872 self->rx_max_sdu_size);
1874 seq_printf(seq, " Used by (%s)\n\n",
1875 self->notify.name);
1876 return 0;
1879 static const struct seq_operations irttp_seq_ops = {
1880 .start = irttp_seq_start,
1881 .next = irttp_seq_next,
1882 .stop = irttp_seq_stop,
1883 .show = irttp_seq_show,
1886 static int irttp_seq_open(struct inode *inode, struct file *file)
1888 return seq_open_private(file, &irttp_seq_ops,
1889 sizeof(struct irttp_iter_state));
1892 const struct file_operations irttp_seq_fops = {
1893 .owner = THIS_MODULE,
1894 .open = irttp_seq_open,
1895 .read = seq_read,
1896 .llseek = seq_lseek,
1897 .release = seq_release_private,
1900 #endif /* PROC_FS */