[IRDA]: Fix memory leak in irttp_init()
[linux-2.6/verdex.git] / net / irda / irttp.c
blob1df6487609e1e440a0918f85d48da0e0dbcbe7bd
1 /*********************************************************************
2 *
3 * Filename: irttp.c
4 * Version: 1.2
5 * Description: Tiny Transport Protocol (TTP) implementation
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Aug 31 20:14:31 1997
9 * Modified at: Wed Jan 5 11:31:27 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
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/config.h>
28 #include <linux/skbuff.h>
29 #include <linux/init.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 = NULL;
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 /* Initialize the irttp structure. */
90 if (irttp == NULL) {
91 irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
92 if (irttp == NULL)
93 return -ENOMEM;
95 memset(irttp, 0, sizeof(struct irttp_cb));
97 irttp->magic = TTP_MAGIC;
99 irttp->tsaps = hashbin_new(HB_LOCK);
100 if (!irttp->tsaps) {
101 IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n",
102 __FUNCTION__);
103 kfree(irttp);
104 return -ENOMEM;
107 return 0;
111 * Function irttp_cleanup (void)
113 * Called by module destruction/cleanup code
116 void __exit irttp_cleanup(void)
118 /* Check for main structure */
119 IRDA_ASSERT(irttp != NULL, return;);
120 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;);
123 * Delete hashbin and close all TSAP instances in it
125 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
127 irttp->magic = 0;
129 /* De-allocate main structure */
130 kfree(irttp);
132 irttp = NULL;
135 /*************************** SUBROUTINES ***************************/
138 * Function irttp_start_todo_timer (self, timeout)
140 * Start todo timer.
142 * Made it more effient and unsensitive to race conditions - Jean II
144 static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
146 /* Set new value for timer */
147 mod_timer(&self->todo_timer, jiffies + timeout);
151 * Function irttp_todo_expired (data)
153 * Todo timer has expired!
155 * One of the restriction of the timer is that it is run only on the timer
156 * interrupt which run every 10ms. This mean that even if you set the timer
157 * with a delay of 0, it may take up to 10ms before it's run.
158 * So, to minimise latency and keep cache fresh, we try to avoid using
159 * it as much as possible.
160 * Note : we can't use tasklets, because they can't be asynchronously
161 * killed (need user context), and we can't guarantee that here...
162 * Jean II
164 static void irttp_todo_expired(unsigned long data)
166 struct tsap_cb *self = (struct tsap_cb *) data;
168 /* Check that we still exist */
169 if (!self || self->magic != TTP_TSAP_MAGIC)
170 return;
172 IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
174 /* Try to make some progress, especially on Tx side - Jean II */
175 irttp_run_rx_queue(self);
176 irttp_run_tx_queue(self);
178 /* Check if time for disconnect */
179 if (test_bit(0, &self->disconnect_pend)) {
180 /* Check if it's possible to disconnect yet */
181 if (skb_queue_empty(&self->tx_queue)) {
182 /* Make sure disconnect is not pending anymore */
183 clear_bit(0, &self->disconnect_pend); /* FALSE */
185 /* Note : self->disconnect_skb may be NULL */
186 irttp_disconnect_request(self, self->disconnect_skb,
187 P_NORMAL);
188 self->disconnect_skb = NULL;
189 } else {
190 /* Try again later */
191 irttp_start_todo_timer(self, HZ/10);
193 /* No reason to try and close now */
194 return;
198 /* Check if it's closing time */
199 if (self->close_pend)
200 /* Finish cleanup */
201 irttp_close_tsap(self);
205 * Function irttp_flush_queues (self)
207 * Flushes (removes all frames) in transitt-buffer (tx_list)
209 void irttp_flush_queues(struct tsap_cb *self)
211 struct sk_buff* skb;
213 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
215 IRDA_ASSERT(self != NULL, return;);
216 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
218 /* Deallocate frames waiting to be sent */
219 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
220 dev_kfree_skb(skb);
222 /* Deallocate received frames */
223 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
224 dev_kfree_skb(skb);
226 /* Deallocate received fragments */
227 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
228 dev_kfree_skb(skb);
232 * Function irttp_reassemble (self)
234 * Makes a new (continuous) skb of all the fragments in the fragment
235 * queue
238 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
240 struct sk_buff *skb, *frag;
241 int n = 0; /* Fragment index */
243 IRDA_ASSERT(self != NULL, return NULL;);
244 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
246 IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __FUNCTION__,
247 self->rx_sdu_size);
249 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
250 if (!skb)
251 return NULL;
254 * Need to reserve space for TTP header in case this skb needs to
255 * be requeued in case delivery failes
257 skb_reserve(skb, TTP_HEADER);
258 skb_put(skb, self->rx_sdu_size);
261 * Copy all fragments to a new buffer
263 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
264 memcpy(skb->data+n, frag->data, frag->len);
265 n += frag->len;
267 dev_kfree_skb(frag);
270 IRDA_DEBUG(2,
271 "%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
272 __FUNCTION__, n, self->rx_sdu_size, self->rx_max_sdu_size);
273 /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size
274 * by summing the size of all fragments, so we should always
275 * have n == self->rx_sdu_size, except in cases where we
276 * droped the last fragment (when self->rx_sdu_size exceed
277 * self->rx_max_sdu_size), where n < self->rx_sdu_size.
278 * Jean II */
279 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
281 /* Set the new length */
282 skb_trim(skb, n);
284 self->rx_sdu_size = 0;
286 return skb;
290 * Function irttp_fragment_skb (skb)
292 * Fragments a frame and queues all the fragments for transmission
295 static inline void irttp_fragment_skb(struct tsap_cb *self,
296 struct sk_buff *skb)
298 struct sk_buff *frag;
299 __u8 *frame;
301 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
303 IRDA_ASSERT(self != NULL, return;);
304 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
305 IRDA_ASSERT(skb != NULL, return;);
308 * Split frame into a number of segments
310 while (skb->len > self->max_seg_size) {
311 IRDA_DEBUG(2, "%s(), fragmenting ...\n", __FUNCTION__);
313 /* Make new segment */
314 frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
315 if (!frag)
316 return;
318 skb_reserve(frag, self->max_header_size);
320 /* Copy data from the original skb into this fragment. */
321 memcpy(skb_put(frag, self->max_seg_size), skb->data,
322 self->max_seg_size);
324 /* Insert TTP header, with the more bit set */
325 frame = skb_push(frag, TTP_HEADER);
326 frame[0] = TTP_MORE;
328 /* Hide the copied data from the original skb */
329 skb_pull(skb, self->max_seg_size);
331 /* Queue fragment */
332 skb_queue_tail(&self->tx_queue, frag);
334 /* Queue what is left of the original skb */
335 IRDA_DEBUG(2, "%s(), queuing last segment\n", __FUNCTION__);
337 frame = skb_push(skb, TTP_HEADER);
338 frame[0] = 0x00; /* Clear more bit */
340 /* Queue fragment */
341 skb_queue_tail(&self->tx_queue, skb);
345 * Function irttp_param_max_sdu_size (self, param)
347 * Handle the MaxSduSize parameter in the connect frames, this function
348 * will be called both when this parameter needs to be inserted into, and
349 * extracted from the connect frames
351 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
352 int get)
354 struct tsap_cb *self;
356 self = (struct tsap_cb *) instance;
358 IRDA_ASSERT(self != NULL, return -1;);
359 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
361 if (get)
362 param->pv.i = self->tx_max_sdu_size;
363 else
364 self->tx_max_sdu_size = param->pv.i;
366 IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __FUNCTION__, param->pv.i);
368 return 0;
371 /*************************** CLIENT CALLS ***************************/
372 /************************** LMP CALLBACKS **************************/
373 /* Everything is happily mixed up. Waiting for next clean up - Jean II */
376 * Function irttp_open_tsap (stsap, notify)
378 * Create TSAP connection endpoint,
380 struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
382 struct tsap_cb *self;
383 struct lsap_cb *lsap;
384 notify_t ttp_notify;
386 IRDA_ASSERT(irttp != NULL, return NULL;);
387 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
389 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
390 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
391 * JeanII */
392 if((stsap_sel != LSAP_ANY) &&
393 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
394 IRDA_DEBUG(0, "%s(), invalid tsap!\n", __FUNCTION__);
395 return NULL;
398 self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
399 if (self == NULL) {
400 IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __FUNCTION__);
401 return NULL;
403 memset(self, 0, sizeof(struct tsap_cb));
404 spin_lock_init(&self->lock);
406 /* Initialise todo timer */
407 init_timer(&self->todo_timer);
408 self->todo_timer.data = (unsigned long) self;
409 self->todo_timer.function = &irttp_todo_expired;
411 /* Initialize callbacks for IrLMP to use */
412 irda_notify_init(&ttp_notify);
413 ttp_notify.connect_confirm = irttp_connect_confirm;
414 ttp_notify.connect_indication = irttp_connect_indication;
415 ttp_notify.disconnect_indication = irttp_disconnect_indication;
416 ttp_notify.data_indication = irttp_data_indication;
417 ttp_notify.udata_indication = irttp_udata_indication;
418 ttp_notify.flow_indication = irttp_flow_indication;
419 if(notify->status_indication != NULL)
420 ttp_notify.status_indication = irttp_status_indication;
421 ttp_notify.instance = self;
422 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
424 self->magic = TTP_TSAP_MAGIC;
425 self->connected = FALSE;
427 skb_queue_head_init(&self->rx_queue);
428 skb_queue_head_init(&self->tx_queue);
429 skb_queue_head_init(&self->rx_fragments);
431 * Create LSAP at IrLMP layer
433 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
434 if (lsap == NULL) {
435 IRDA_WARNING("%s: unable to allocate LSAP!!\n", __FUNCTION__);
436 return NULL;
440 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
441 * will replace it with whatever source selector which is free, so
442 * the stsap_sel we have might not be valid anymore
444 self->stsap_sel = lsap->slsap_sel;
445 IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __FUNCTION__, self->stsap_sel);
447 self->notify = *notify;
448 self->lsap = lsap;
450 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
452 if (credit > TTP_RX_MAX_CREDIT)
453 self->initial_credit = TTP_RX_MAX_CREDIT;
454 else
455 self->initial_credit = credit;
457 return self;
459 EXPORT_SYMBOL(irttp_open_tsap);
462 * Function irttp_close (handle)
464 * Remove an instance of a TSAP. This function should only deal with the
465 * deallocation of the TSAP, and resetting of the TSAPs values;
468 static void __irttp_close_tsap(struct tsap_cb *self)
470 /* First make sure we're connected. */
471 IRDA_ASSERT(self != NULL, return;);
472 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
474 irttp_flush_queues(self);
476 del_timer(&self->todo_timer);
478 /* This one won't be cleaned up if we are disconnect_pend + close_pend
479 * and we receive a disconnect_indication */
480 if (self->disconnect_skb)
481 dev_kfree_skb(self->disconnect_skb);
483 self->connected = FALSE;
484 self->magic = ~TTP_TSAP_MAGIC;
486 kfree(self);
490 * Function irttp_close (self)
492 * Remove TSAP from list of all TSAPs and then deallocate all resources
493 * associated with this TSAP
495 * Note : because we *free* the tsap structure, it is the responsibility
496 * of the caller to make sure we are called only once and to deal with
497 * possible race conditions. - Jean II
499 int irttp_close_tsap(struct tsap_cb *self)
501 struct tsap_cb *tsap;
503 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
505 IRDA_ASSERT(self != NULL, return -1;);
506 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
508 /* Make sure tsap has been disconnected */
509 if (self->connected) {
510 /* Check if disconnect is not pending */
511 if (!test_bit(0, &self->disconnect_pend)) {
512 IRDA_WARNING("%s: TSAP still connected!\n",
513 __FUNCTION__);
514 irttp_disconnect_request(self, NULL, P_NORMAL);
516 self->close_pend = TRUE;
517 irttp_start_todo_timer(self, HZ/10);
519 return 0; /* Will be back! */
522 tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);
524 IRDA_ASSERT(tsap == self, return -1;);
526 /* Close corresponding LSAP */
527 if (self->lsap) {
528 irlmp_close_lsap(self->lsap);
529 self->lsap = NULL;
532 __irttp_close_tsap(self);
534 return 0;
536 EXPORT_SYMBOL(irttp_close_tsap);
539 * Function irttp_udata_request (self, skb)
541 * Send unreliable data on this TSAP
544 int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
546 IRDA_ASSERT(self != NULL, return -1;);
547 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
548 IRDA_ASSERT(skb != NULL, return -1;);
550 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
552 /* Check that nothing bad happens */
553 if ((skb->len == 0) || (!self->connected)) {
554 IRDA_DEBUG(1, "%s(), No data, or not connected\n",
555 __FUNCTION__);
556 goto err;
559 if (skb->len > self->max_seg_size) {
560 IRDA_DEBUG(1, "%s(), UData is to large for IrLAP!\n",
561 __FUNCTION__);
562 goto err;
565 irlmp_udata_request(self->lsap, skb);
566 self->stats.tx_packets++;
568 return 0;
570 err:
571 dev_kfree_skb(skb);
572 return -1;
574 EXPORT_SYMBOL(irttp_udata_request);
578 * Function irttp_data_request (handle, skb)
580 * Queue frame for transmission. If SAR is enabled, fragement the frame
581 * and queue the fragments for transmission
583 int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
585 __u8 *frame;
586 int ret;
588 IRDA_ASSERT(self != NULL, return -1;);
589 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
590 IRDA_ASSERT(skb != NULL, return -1;);
592 IRDA_DEBUG(2, "%s() : queue len = %d\n", __FUNCTION__,
593 skb_queue_len(&self->tx_queue));
595 /* Check that nothing bad happens */
596 if ((skb->len == 0) || (!self->connected)) {
597 IRDA_WARNING("%s: No data, or not connected\n", __FUNCTION__);
598 ret = -ENOTCONN;
599 goto err;
603 * Check if SAR is disabled, and the frame is larger than what fits
604 * inside an IrLAP frame
606 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
607 IRDA_ERROR("%s: SAR disabled, and data is to large for IrLAP!\n",
608 __FUNCTION__);
609 ret = -EMSGSIZE;
610 goto err;
614 * Check if SAR is enabled, and the frame is larger than the
615 * TxMaxSduSize
617 if ((self->tx_max_sdu_size != 0) &&
618 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
619 (skb->len > self->tx_max_sdu_size))
621 IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
622 __FUNCTION__);
623 ret = -EMSGSIZE;
624 goto err;
627 * Check if transmit queue is full
629 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
631 * Give it a chance to empty itself
633 irttp_run_tx_queue(self);
635 /* Drop packet. This error code should trigger the caller
636 * to resend the data in the client code - Jean II */
637 ret = -ENOBUFS;
638 goto err;
641 /* Queue frame, or queue frame segments */
642 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
643 /* Queue frame */
644 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
645 frame = skb_push(skb, TTP_HEADER);
646 frame[0] = 0x00; /* Clear more bit */
648 skb_queue_tail(&self->tx_queue, skb);
649 } else {
651 * Fragment the frame, this function will also queue the
652 * fragments, we don't care about the fact the transmit
653 * queue may be overfilled by all the segments for a little
654 * while
656 irttp_fragment_skb(self, skb);
659 /* Check if we can accept more data from client */
660 if ((!self->tx_sdu_busy) &&
661 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
662 /* Tx queue filling up, so stop client. */
663 if (self->notify.flow_indication) {
664 self->notify.flow_indication(self->notify.instance,
665 self, FLOW_STOP);
667 /* self->tx_sdu_busy is the state of the client.
668 * Update state after notifying client to avoid
669 * race condition with irttp_flow_indication().
670 * If the queue empty itself after our test but before
671 * we set the flag, we will fix ourselves below in
672 * irttp_run_tx_queue().
673 * Jean II */
674 self->tx_sdu_busy = TRUE;
677 /* Try to make some progress */
678 irttp_run_tx_queue(self);
680 return 0;
682 err:
683 dev_kfree_skb(skb);
684 return ret;
686 EXPORT_SYMBOL(irttp_data_request);
689 * Function irttp_run_tx_queue (self)
691 * Transmit packets queued for transmission (if possible)
694 static void irttp_run_tx_queue(struct tsap_cb *self)
696 struct sk_buff *skb;
697 unsigned long flags;
698 int n;
700 IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n",
701 __FUNCTION__,
702 self->send_credit, skb_queue_len(&self->tx_queue));
704 /* Get exclusive access to the tx queue, otherwise don't touch it */
705 if (irda_lock(&self->tx_queue_lock) == FALSE)
706 return;
708 /* Try to send out frames as long as we have credits
709 * and as long as LAP is not full. If LAP is full, it will
710 * poll us through irttp_flow_indication() - Jean II */
711 while ((self->send_credit > 0) &&
712 (!irlmp_lap_tx_queue_full(self->lsap)) &&
713 (skb = skb_dequeue(&self->tx_queue)))
716 * Since we can transmit and receive frames concurrently,
717 * the code below is a critical region and we must assure that
718 * nobody messes with the credits while we update them.
720 spin_lock_irqsave(&self->lock, flags);
722 n = self->avail_credit;
723 self->avail_credit = 0;
725 /* Only room for 127 credits in frame */
726 if (n > 127) {
727 self->avail_credit = n-127;
728 n = 127;
730 self->remote_credit += n;
731 self->send_credit--;
733 spin_unlock_irqrestore(&self->lock, flags);
736 * More bit must be set by the data_request() or fragment()
737 * functions
739 skb->data[0] |= (n & 0x7f);
741 /* Detach from socket.
742 * The current skb has a reference to the socket that sent
743 * it (skb->sk). When we pass it to IrLMP, the skb will be
744 * stored in in IrLAP (self->wx_list). When we are within
745 * IrLAP, we lose the notion of socket, so we should not
746 * have a reference to a socket. So, we drop it here.
748 * Why does it matter ?
749 * When the skb is freed (kfree_skb), if it is associated
750 * with a socket, it release buffer space on the socket
751 * (through sock_wfree() and sock_def_write_space()).
752 * If the socket no longer exist, we may crash. Hard.
753 * When we close a socket, we make sure that associated packets
754 * in IrTTP are freed. However, we have no way to cancel
755 * the packet that we have passed to IrLAP. So, if a packet
756 * remains in IrLAP (retry on the link or else) after we
757 * close the socket, we are dead !
758 * Jean II */
759 if (skb->sk != NULL) {
760 /* IrSOCK application, IrOBEX, ... */
761 skb_orphan(skb);
763 /* IrCOMM over IrTTP, IrLAN, ... */
765 /* Pass the skb to IrLMP - done */
766 irlmp_data_request(self->lsap, skb);
767 self->stats.tx_packets++;
770 /* Check if we can accept more frames from client.
771 * We don't want to wait until the todo timer to do that, and we
772 * can't use tasklets (grr...), so we are obliged to give control
773 * to client. That's ok, this test will be true not too often
774 * (max once per LAP window) and we are called from places
775 * where we can spend a bit of time doing stuff. - Jean II */
776 if ((self->tx_sdu_busy) &&
777 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
778 (!self->close_pend))
780 if (self->notify.flow_indication)
781 self->notify.flow_indication(self->notify.instance,
782 self, FLOW_START);
784 /* self->tx_sdu_busy is the state of the client.
785 * We don't really have a race here, but it's always safer
786 * to update our state after the client - Jean II */
787 self->tx_sdu_busy = FALSE;
790 /* Reset lock */
791 self->tx_queue_lock = 0;
795 * Function irttp_give_credit (self)
797 * Send a dataless flowdata TTP-PDU and give available credit to peer
798 * TSAP
800 static inline void irttp_give_credit(struct tsap_cb *self)
802 struct sk_buff *tx_skb = NULL;
803 unsigned long flags;
804 int n;
806 IRDA_ASSERT(self != NULL, return;);
807 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
809 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n",
810 __FUNCTION__,
811 self->send_credit, self->avail_credit, self->remote_credit);
813 /* Give credit to peer */
814 tx_skb = dev_alloc_skb(64);
815 if (!tx_skb)
816 return;
818 /* Reserve space for LMP, and LAP header */
819 skb_reserve(tx_skb, self->max_header_size);
822 * Since we can transmit and receive frames concurrently,
823 * the code below is a critical region and we must assure that
824 * nobody messes with the credits while we update them.
826 spin_lock_irqsave(&self->lock, flags);
828 n = self->avail_credit;
829 self->avail_credit = 0;
831 /* Only space for 127 credits in frame */
832 if (n > 127) {
833 self->avail_credit = n - 127;
834 n = 127;
836 self->remote_credit += n;
838 spin_unlock_irqrestore(&self->lock, flags);
840 skb_put(tx_skb, 1);
841 tx_skb->data[0] = (__u8) (n & 0x7f);
843 irlmp_data_request(self->lsap, tx_skb);
844 self->stats.tx_packets++;
848 * Function irttp_udata_indication (instance, sap, skb)
850 * Received some unit-data (unreliable)
853 static int irttp_udata_indication(void *instance, void *sap,
854 struct sk_buff *skb)
856 struct tsap_cb *self;
857 int err;
859 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
861 self = (struct tsap_cb *) instance;
863 IRDA_ASSERT(self != NULL, return -1;);
864 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
865 IRDA_ASSERT(skb != NULL, return -1;);
867 self->stats.rx_packets++;
869 /* Just pass data to layer above */
870 if (self->notify.udata_indication) {
871 err = self->notify.udata_indication(self->notify.instance,
872 self,skb);
873 /* Same comment as in irttp_do_data_indication() */
874 if (!err)
875 return 0;
877 /* Either no handler, or handler returns an error */
878 dev_kfree_skb(skb);
880 return 0;
884 * Function irttp_data_indication (instance, sap, skb)
886 * Receive segment from IrLMP.
889 static int irttp_data_indication(void *instance, void *sap,
890 struct sk_buff *skb)
892 struct tsap_cb *self;
893 unsigned long flags;
894 int n;
896 self = (struct tsap_cb *) instance;
898 n = skb->data[0] & 0x7f; /* Extract the credits */
900 self->stats.rx_packets++;
902 /* Deal with inbound credit
903 * Since we can transmit and receive frames concurrently,
904 * the code below is a critical region and we must assure that
905 * nobody messes with the credits while we update them.
907 spin_lock_irqsave(&self->lock, flags);
908 self->send_credit += n;
909 if (skb->len > 1)
910 self->remote_credit--;
911 spin_unlock_irqrestore(&self->lock, flags);
914 * Data or dataless packet? Dataless frames contains only the
915 * TTP_HEADER.
917 if (skb->len > 1) {
919 * We don't remove the TTP header, since we must preserve the
920 * more bit, so the defragment routing knows what to do
922 skb_queue_tail(&self->rx_queue, skb);
923 } else {
924 /* Dataless flowdata TTP-PDU */
925 dev_kfree_skb(skb);
929 /* Push data to the higher layer.
930 * We do it synchronously because running the todo timer for each
931 * receive packet would be too much overhead and latency.
932 * By passing control to the higher layer, we run the risk that
933 * it may take time or grab a lock. Most often, the higher layer
934 * will only put packet in a queue.
935 * Anyway, packets are only dripping through the IrDA, so we can
936 * have time before the next packet.
937 * Further, we are run from NET_BH, so the worse that can happen is
938 * us missing the optimal time to send back the PF bit in LAP.
939 * Jean II */
940 irttp_run_rx_queue(self);
942 /* We now give credits to peer in irttp_run_rx_queue().
943 * We need to send credit *NOW*, otherwise we are going
944 * to miss the next Tx window. The todo timer may take
945 * a while before it's run... - Jean II */
948 * If the peer device has given us some credits and we didn't have
949 * anyone from before, then we need to shedule the tx queue.
950 * We need to do that because our Tx have stopped (so we may not
951 * get any LAP flow indication) and the user may be stopped as
952 * well. - Jean II
954 if (self->send_credit == n) {
955 /* Restart pushing stuff to LAP */
956 irttp_run_tx_queue(self);
957 /* Note : we don't want to schedule the todo timer
958 * because it has horrible latency. No tasklets
959 * because the tasklet API is broken. - Jean II */
962 return 0;
966 * Function irttp_status_indication (self, reason)
968 * Status_indication, just pass to the higher layer...
971 static void irttp_status_indication(void *instance,
972 LINK_STATUS link, LOCK_STATUS lock)
974 struct tsap_cb *self;
976 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
978 self = (struct tsap_cb *) instance;
980 IRDA_ASSERT(self != NULL, return;);
981 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
983 /* Check if client has already closed the TSAP and gone away */
984 if (self->close_pend)
985 return;
988 * Inform service user if he has requested it
990 if (self->notify.status_indication != NULL)
991 self->notify.status_indication(self->notify.instance,
992 link, lock);
993 else
994 IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
998 * Function irttp_flow_indication (self, reason)
1000 * Flow_indication : IrLAP tells us to send more data.
1003 static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
1005 struct tsap_cb *self;
1007 self = (struct tsap_cb *) instance;
1009 IRDA_ASSERT(self != NULL, return;);
1010 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1012 IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
1014 /* We are "polled" directly from LAP, and the LAP want to fill
1015 * its Tx window. We want to do our best to send it data, so that
1016 * we maximise the window. On the other hand, we want to limit the
1017 * amount of work here so that LAP doesn't hang forever waiting
1018 * for packets. - Jean II */
1020 /* Try to send some packets. Currently, LAP calls us every time
1021 * there is one free slot, so we will send only one packet.
1022 * This allow the scheduler to do its round robin - Jean II */
1023 irttp_run_tx_queue(self);
1025 /* Note regarding the interraction with higher layer.
1026 * irttp_run_tx_queue() may call the client when its queue
1027 * start to empty, via notify.flow_indication(). Initially.
1028 * I wanted this to happen in a tasklet, to avoid client
1029 * grabbing the CPU, but we can't use tasklets safely. And timer
1030 * is definitely too slow.
1031 * This will happen only once per LAP window, and usually at
1032 * the third packet (unless window is smaller). LAP is still
1033 * doing mtt and sending first packet so it's sort of OK
1034 * to do that. Jean II */
1036 /* If we need to send disconnect. try to do it now */
1037 if(self->disconnect_pend)
1038 irttp_start_todo_timer(self, 0);
1042 * Function irttp_flow_request (self, command)
1044 * This function could be used by the upper layers to tell IrTTP to stop
1045 * delivering frames if the receive queues are starting to get full, or
1046 * to tell IrTTP to start delivering frames again.
1048 void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1050 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1052 IRDA_ASSERT(self != NULL, return;);
1053 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1055 switch (flow) {
1056 case FLOW_STOP:
1057 IRDA_DEBUG(1, "%s(), flow stop\n", __FUNCTION__);
1058 self->rx_sdu_busy = TRUE;
1059 break;
1060 case FLOW_START:
1061 IRDA_DEBUG(1, "%s(), flow start\n", __FUNCTION__);
1062 self->rx_sdu_busy = FALSE;
1064 /* Client say he can accept more data, try to free our
1065 * queues ASAP - Jean II */
1066 irttp_run_rx_queue(self);
1068 break;
1069 default:
1070 IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __FUNCTION__);
1073 EXPORT_SYMBOL(irttp_flow_request);
1076 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
1078 * Try to connect to remote destination TSAP selector
1081 int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1082 __u32 saddr, __u32 daddr,
1083 struct qos_info *qos, __u32 max_sdu_size,
1084 struct sk_buff *userdata)
1086 struct sk_buff *tx_skb;
1087 __u8 *frame;
1088 __u8 n;
1090 IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __FUNCTION__, max_sdu_size);
1092 IRDA_ASSERT(self != NULL, return -EBADR;);
1093 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1095 if (self->connected) {
1096 if(userdata)
1097 dev_kfree_skb(userdata);
1098 return -EISCONN;
1101 /* Any userdata supplied? */
1102 if (userdata == NULL) {
1103 tx_skb = dev_alloc_skb(64);
1104 if (!tx_skb)
1105 return -ENOMEM;
1107 /* Reserve space for MUX_CONTROL and LAP header */
1108 skb_reserve(tx_skb, TTP_MAX_HEADER);
1109 } else {
1110 tx_skb = userdata;
1112 * Check that the client has reserved enough space for
1113 * headers
1115 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1116 { dev_kfree_skb(userdata); return -1; } );
1119 /* Initialize connection parameters */
1120 self->connected = FALSE;
1121 self->avail_credit = 0;
1122 self->rx_max_sdu_size = max_sdu_size;
1123 self->rx_sdu_size = 0;
1124 self->rx_sdu_busy = FALSE;
1125 self->dtsap_sel = dtsap_sel;
1127 n = self->initial_credit;
1129 self->remote_credit = 0;
1130 self->send_credit = 0;
1133 * Give away max 127 credits for now
1135 if (n > 127) {
1136 self->avail_credit=n-127;
1137 n = 127;
1140 self->remote_credit = n;
1142 /* SAR enabled? */
1143 if (max_sdu_size > 0) {
1144 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1145 { dev_kfree_skb(tx_skb); return -1; } );
1147 /* Insert SAR parameters */
1148 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1150 frame[0] = TTP_PARAMETERS | n;
1151 frame[1] = 0x04; /* Length */
1152 frame[2] = 0x01; /* MaxSduSize */
1153 frame[3] = 0x02; /* Value length */
1155 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1156 (__u16 *)(frame+4));
1157 } else {
1158 /* Insert plain TTP header */
1159 frame = skb_push(tx_skb, TTP_HEADER);
1161 /* Insert initial credit in frame */
1162 frame[0] = n & 0x7f;
1165 /* Connect with IrLMP. No QoS parameters for now */
1166 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1167 tx_skb);
1169 EXPORT_SYMBOL(irttp_connect_request);
1172 * Function irttp_connect_confirm (handle, qos, skb)
1174 * Sevice user confirms TSAP connection with peer.
1177 static void irttp_connect_confirm(void *instance, void *sap,
1178 struct qos_info *qos, __u32 max_seg_size,
1179 __u8 max_header_size, struct sk_buff *skb)
1181 struct tsap_cb *self;
1182 int parameters;
1183 int ret;
1184 __u8 plen;
1185 __u8 n;
1187 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1189 self = (struct tsap_cb *) instance;
1191 IRDA_ASSERT(self != NULL, return;);
1192 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1193 IRDA_ASSERT(skb != NULL, return;);
1195 self->max_seg_size = max_seg_size - TTP_HEADER;
1196 self->max_header_size = max_header_size + TTP_HEADER;
1199 * Check if we have got some QoS parameters back! This should be the
1200 * negotiated QoS for the link.
1202 if (qos) {
1203 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
1204 qos->baud_rate.bits);
1205 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1206 qos->baud_rate.value);
1209 n = skb->data[0] & 0x7f;
1211 IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __FUNCTION__, n);
1213 self->send_credit = n;
1214 self->tx_max_sdu_size = 0;
1215 self->connected = TRUE;
1217 parameters = skb->data[0] & 0x80;
1219 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1220 skb_pull(skb, TTP_HEADER);
1222 if (parameters) {
1223 plen = skb->data[0];
1225 ret = irda_param_extract_all(self, skb->data+1,
1226 IRDA_MIN(skb->len-1, plen),
1227 &param_info);
1229 /* Any errors in the parameter list? */
1230 if (ret < 0) {
1231 IRDA_WARNING("%s: error extracting parameters\n",
1232 __FUNCTION__);
1233 dev_kfree_skb(skb);
1235 /* Do not accept this connection attempt */
1236 return;
1238 /* Remove parameters */
1239 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1242 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
1243 self->send_credit, self->avail_credit, self->remote_credit);
1245 IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __FUNCTION__,
1246 self->tx_max_sdu_size);
1248 if (self->notify.connect_confirm) {
1249 self->notify.connect_confirm(self->notify.instance, self, qos,
1250 self->tx_max_sdu_size,
1251 self->max_header_size, skb);
1252 } else
1253 dev_kfree_skb(skb);
1257 * Function irttp_connect_indication (handle, skb)
1259 * Some other device is connecting to this TSAP
1262 void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
1263 __u32 max_seg_size, __u8 max_header_size,
1264 struct sk_buff *skb)
1266 struct tsap_cb *self;
1267 struct lsap_cb *lsap;
1268 int parameters;
1269 int ret;
1270 __u8 plen;
1271 __u8 n;
1273 self = (struct tsap_cb *) instance;
1275 IRDA_ASSERT(self != NULL, return;);
1276 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1277 IRDA_ASSERT(skb != NULL, return;);
1279 lsap = (struct lsap_cb *) sap;
1281 self->max_seg_size = max_seg_size - TTP_HEADER;
1282 self->max_header_size = max_header_size+TTP_HEADER;
1284 IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __FUNCTION__, self->stsap_sel);
1286 /* Need to update dtsap_sel if its equal to LSAP_ANY */
1287 self->dtsap_sel = lsap->dlsap_sel;
1289 n = skb->data[0] & 0x7f;
1291 self->send_credit = n;
1292 self->tx_max_sdu_size = 0;
1294 parameters = skb->data[0] & 0x80;
1296 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1297 skb_pull(skb, TTP_HEADER);
1299 if (parameters) {
1300 plen = skb->data[0];
1302 ret = irda_param_extract_all(self, skb->data+1,
1303 IRDA_MIN(skb->len-1, plen),
1304 &param_info);
1306 /* Any errors in the parameter list? */
1307 if (ret < 0) {
1308 IRDA_WARNING("%s: error extracting parameters\n",
1309 __FUNCTION__);
1310 dev_kfree_skb(skb);
1312 /* Do not accept this connection attempt */
1313 return;
1316 /* Remove parameters */
1317 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1320 if (self->notify.connect_indication) {
1321 self->notify.connect_indication(self->notify.instance, self,
1322 qos, self->tx_max_sdu_size,
1323 self->max_header_size, skb);
1324 } else
1325 dev_kfree_skb(skb);
1329 * Function irttp_connect_response (handle, userdata)
1331 * Service user is accepting the connection, just pass it down to
1332 * IrLMP!
1335 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1336 struct sk_buff *userdata)
1338 struct sk_buff *tx_skb;
1339 __u8 *frame;
1340 int ret;
1341 __u8 n;
1343 IRDA_ASSERT(self != NULL, return -1;);
1344 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1346 IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __FUNCTION__,
1347 self->stsap_sel);
1349 /* Any userdata supplied? */
1350 if (userdata == NULL) {
1351 tx_skb = dev_alloc_skb(64);
1352 if (!tx_skb)
1353 return -ENOMEM;
1355 /* Reserve space for MUX_CONTROL and LAP header */
1356 skb_reserve(tx_skb, TTP_MAX_HEADER);
1357 } else {
1358 tx_skb = userdata;
1360 * Check that the client has reserved enough space for
1361 * headers
1363 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1364 { dev_kfree_skb(userdata); return -1; } );
1367 self->avail_credit = 0;
1368 self->remote_credit = 0;
1369 self->rx_max_sdu_size = max_sdu_size;
1370 self->rx_sdu_size = 0;
1371 self->rx_sdu_busy = FALSE;
1373 n = self->initial_credit;
1375 /* Frame has only space for max 127 credits (7 bits) */
1376 if (n > 127) {
1377 self->avail_credit = n - 127;
1378 n = 127;
1381 self->remote_credit = n;
1382 self->connected = TRUE;
1384 /* SAR enabled? */
1385 if (max_sdu_size > 0) {
1386 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1387 { dev_kfree_skb(tx_skb); return -1; } );
1389 /* Insert TTP header with SAR parameters */
1390 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1392 frame[0] = TTP_PARAMETERS | n;
1393 frame[1] = 0x04; /* Length */
1395 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
1396 /* TTP_SAR_HEADER, &param_info) */
1398 frame[2] = 0x01; /* MaxSduSize */
1399 frame[3] = 0x02; /* Value length */
1401 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1402 (__u16 *)(frame+4));
1403 } else {
1404 /* Insert TTP header */
1405 frame = skb_push(tx_skb, TTP_HEADER);
1407 frame[0] = n & 0x7f;
1410 ret = irlmp_connect_response(self->lsap, tx_skb);
1412 return ret;
1414 EXPORT_SYMBOL(irttp_connect_response);
1417 * Function irttp_dup (self, instance)
1419 * Duplicate TSAP, can be used by servers to confirm a connection on a
1420 * new TSAP so it can keep listening on the old one.
1422 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1424 struct tsap_cb *new;
1425 unsigned long flags;
1427 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1429 /* Protect our access to the old tsap instance */
1430 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
1432 /* Find the old instance */
1433 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
1434 IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __FUNCTION__);
1435 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1436 return NULL;
1439 /* Allocate a new instance */
1440 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
1441 if (!new) {
1442 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
1443 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1444 return NULL;
1446 /* Dup */
1447 memcpy(new, orig, sizeof(struct tsap_cb));
1449 /* We don't need the old instance any more */
1450 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1452 /* Try to dup the LSAP (may fail if we were too slow) */
1453 new->lsap = irlmp_dup(orig->lsap, new);
1454 if (!new->lsap) {
1455 IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
1456 kfree(new);
1457 return NULL;
1460 /* Not everything should be copied */
1461 new->notify.instance = instance;
1462 init_timer(&new->todo_timer);
1464 skb_queue_head_init(&new->rx_queue);
1465 skb_queue_head_init(&new->tx_queue);
1466 skb_queue_head_init(&new->rx_fragments);
1468 /* This is locked */
1469 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
1471 return new;
1473 EXPORT_SYMBOL(irttp_dup);
1476 * Function irttp_disconnect_request (self)
1478 * Close this connection please! If priority is high, the queued data
1479 * segments, if any, will be deallocated first
1482 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1483 int priority)
1485 int ret;
1487 IRDA_ASSERT(self != NULL, return -1;);
1488 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1490 /* Already disconnected? */
1491 if (!self->connected) {
1492 IRDA_DEBUG(4, "%s(), already disconnected!\n", __FUNCTION__);
1493 if (userdata)
1494 dev_kfree_skb(userdata);
1495 return -1;
1498 /* Disconnect already pending ?
1499 * We need to use an atomic operation to prevent reentry. This
1500 * function may be called from various context, like user, timer
1501 * for following a disconnect_indication() (i.e. net_bh).
1502 * Jean II */
1503 if(test_and_set_bit(0, &self->disconnect_pend)) {
1504 IRDA_DEBUG(0, "%s(), disconnect already pending\n",
1505 __FUNCTION__);
1506 if (userdata)
1507 dev_kfree_skb(userdata);
1509 /* Try to make some progress */
1510 irttp_run_tx_queue(self);
1511 return -1;
1515 * Check if there is still data segments in the transmit queue
1517 if (!skb_queue_empty(&self->tx_queue)) {
1518 if (priority == P_HIGH) {
1520 * No need to send the queued data, if we are
1521 * disconnecting right now since the data will
1522 * not have any usable connection to be sent on
1524 IRDA_DEBUG(1, "%s(): High priority!!()\n", __FUNCTION__);
1525 irttp_flush_queues(self);
1526 } else if (priority == P_NORMAL) {
1528 * Must delay disconnect until after all data segments
1529 * have been sent and the tx_queue is empty
1531 /* We'll reuse this one later for the disconnect */
1532 self->disconnect_skb = userdata; /* May be NULL */
1534 irttp_run_tx_queue(self);
1536 irttp_start_todo_timer(self, HZ/10);
1537 return -1;
1540 /* Note : we don't need to check if self->rx_queue is full and the
1541 * state of self->rx_sdu_busy because the disconnect response will
1542 * be sent at the LMP level (so even if the peer has its Tx queue
1543 * full of data). - Jean II */
1545 IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __FUNCTION__);
1546 self->connected = FALSE;
1548 if (!userdata) {
1549 struct sk_buff *tx_skb;
1550 tx_skb = dev_alloc_skb(64);
1551 if (!tx_skb)
1552 return -ENOMEM;
1555 * Reserve space for MUX and LAP header
1557 skb_reserve(tx_skb, TTP_MAX_HEADER);
1559 userdata = tx_skb;
1561 ret = irlmp_disconnect_request(self->lsap, userdata);
1563 /* The disconnect is no longer pending */
1564 clear_bit(0, &self->disconnect_pend); /* FALSE */
1566 return ret;
1568 EXPORT_SYMBOL(irttp_disconnect_request);
1571 * Function irttp_disconnect_indication (self, reason)
1573 * Disconnect indication, TSAP disconnected by peer?
1576 void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason,
1577 struct sk_buff *skb)
1579 struct tsap_cb *self;
1581 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1583 self = (struct tsap_cb *) instance;
1585 IRDA_ASSERT(self != NULL, return;);
1586 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1588 /* Prevent higher layer to send more data */
1589 self->connected = FALSE;
1591 /* Check if client has already tried to close the TSAP */
1592 if (self->close_pend) {
1593 /* In this case, the higher layer is probably gone. Don't
1594 * bother it and clean up the remains - Jean II */
1595 if (skb)
1596 dev_kfree_skb(skb);
1597 irttp_close_tsap(self);
1598 return;
1601 /* If we are here, we assume that is the higher layer is still
1602 * waiting for the disconnect notification and able to process it,
1603 * even if he tried to disconnect. Otherwise, it would have already
1604 * attempted to close the tsap and self->close_pend would be TRUE.
1605 * Jean II */
1607 /* No need to notify the client if has already tried to disconnect */
1608 if(self->notify.disconnect_indication)
1609 self->notify.disconnect_indication(self->notify.instance, self,
1610 reason, skb);
1611 else
1612 if (skb)
1613 dev_kfree_skb(skb);
1617 * Function irttp_do_data_indication (self, skb)
1619 * Try to deliver reassembled skb to layer above, and requeue it if that
1620 * for some reason should fail. We mark rx sdu as busy to apply back
1621 * pressure is necessary.
1623 static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1625 int err;
1627 /* Check if client has already closed the TSAP and gone away */
1628 if (self->close_pend) {
1629 dev_kfree_skb(skb);
1630 return;
1633 err = self->notify.data_indication(self->notify.instance, self, skb);
1635 /* Usually the layer above will notify that it's input queue is
1636 * starting to get filled by using the flow request, but this may
1637 * be difficult, so it can instead just refuse to eat it and just
1638 * give an error back
1640 if (err) {
1641 IRDA_DEBUG(0, "%s() requeueing skb!\n", __FUNCTION__);
1643 /* Make sure we take a break */
1644 self->rx_sdu_busy = TRUE;
1646 /* Need to push the header in again */
1647 skb_push(skb, TTP_HEADER);
1648 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1650 /* Put skb back on queue */
1651 skb_queue_head(&self->rx_queue, skb);
1656 * Function irttp_run_rx_queue (self)
1658 * Check if we have any frames to be transmitted, or if we have any
1659 * available credit to give away.
1661 void irttp_run_rx_queue(struct tsap_cb *self)
1663 struct sk_buff *skb;
1664 int more = 0;
1666 IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
1667 self->send_credit, self->avail_credit, self->remote_credit);
1669 /* Get exclusive access to the rx queue, otherwise don't touch it */
1670 if (irda_lock(&self->rx_queue_lock) == FALSE)
1671 return;
1674 * Reassemble all frames in receive queue and deliver them
1676 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1677 /* This bit will tell us if it's the last fragment or not */
1678 more = skb->data[0] & 0x80;
1680 /* Remove TTP header */
1681 skb_pull(skb, TTP_HEADER);
1683 /* Add the length of the remaining data */
1684 self->rx_sdu_size += skb->len;
1687 * If SAR is disabled, or user has requested no reassembly
1688 * of received fragments then we just deliver them
1689 * immediately. This can be requested by clients that
1690 * implements byte streams without any message boundaries
1692 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1693 irttp_do_data_indication(self, skb);
1694 self->rx_sdu_size = 0;
1696 continue;
1699 /* Check if this is a fragment, and not the last fragment */
1700 if (more) {
1702 * Queue the fragment if we still are within the
1703 * limits of the maximum size of the rx_sdu
1705 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1706 IRDA_DEBUG(4, "%s(), queueing frag\n",
1707 __FUNCTION__);
1708 skb_queue_tail(&self->rx_fragments, skb);
1709 } else {
1710 /* Free the part of the SDU that is too big */
1711 dev_kfree_skb(skb);
1713 continue;
1716 * This is the last fragment, so time to reassemble!
1718 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1719 (self->rx_max_sdu_size == TTP_SAR_UNBOUND))
1722 * A little optimizing. Only queue the fragment if
1723 * there are other fragments. Since if this is the
1724 * last and only fragment, there is no need to
1725 * reassemble :-)
1727 if (!skb_queue_empty(&self->rx_fragments)) {
1728 skb_queue_tail(&self->rx_fragments,
1729 skb);
1731 skb = irttp_reassemble_skb(self);
1734 /* Now we can deliver the reassembled skb */
1735 irttp_do_data_indication(self, skb);
1736 } else {
1737 IRDA_DEBUG(1, "%s(), Truncated frame\n", __FUNCTION__);
1739 /* Free the part of the SDU that is too big */
1740 dev_kfree_skb(skb);
1742 /* Deliver only the valid but truncated part of SDU */
1743 skb = irttp_reassemble_skb(self);
1745 irttp_do_data_indication(self, skb);
1747 self->rx_sdu_size = 0;
1751 * It's not trivial to keep track of how many credits are available
1752 * by incrementing at each packet, because delivery may fail
1753 * (irttp_do_data_indication() may requeue the frame) and because
1754 * we need to take care of fragmentation.
1755 * We want the other side to send up to initial_credit packets.
1756 * We have some frames in our queues, and we have already allowed it
1757 * to send remote_credit.
1758 * No need to spinlock, write is atomic and self correcting...
1759 * Jean II
1761 self->avail_credit = (self->initial_credit -
1762 (self->remote_credit +
1763 skb_queue_len(&self->rx_queue) +
1764 skb_queue_len(&self->rx_fragments)));
1766 /* Do we have too much credits to send to peer ? */
1767 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1768 (self->avail_credit > 0)) {
1769 /* Send explicit credit frame */
1770 irttp_give_credit(self);
1771 /* Note : do *NOT* check if tx_queue is non-empty, that
1772 * will produce deadlocks. I repeat : send a credit frame
1773 * even if we have something to send in our Tx queue.
1774 * If we have credits, it means that our Tx queue is blocked.
1776 * Let's suppose the peer can't keep up with our Tx. He will
1777 * flow control us by not sending us any credits, and we
1778 * will stop Tx and start accumulating credits here.
1779 * Up to the point where the peer will stop its Tx queue,
1780 * for lack of credits.
1781 * Let's assume the peer application is single threaded.
1782 * It will block on Tx and never consume any Rx buffer.
1783 * Deadlock. Guaranteed. - Jean II
1787 /* Reset lock */
1788 self->rx_queue_lock = 0;
1791 #ifdef CONFIG_PROC_FS
1792 struct irttp_iter_state {
1793 int id;
1796 static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
1798 struct irttp_iter_state *iter = seq->private;
1799 struct tsap_cb *self;
1801 /* Protect our access to the tsap list */
1802 spin_lock_irq(&irttp->tsaps->hb_spinlock);
1803 iter->id = 0;
1805 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1806 self != NULL;
1807 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
1808 if (iter->id == *pos)
1809 break;
1810 ++iter->id;
1813 return self;
1816 static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1818 struct irttp_iter_state *iter = seq->private;
1820 ++*pos;
1821 ++iter->id;
1822 return (void *) hashbin_get_next(irttp->tsaps);
1825 static void irttp_seq_stop(struct seq_file *seq, void *v)
1827 spin_unlock_irq(&irttp->tsaps->hb_spinlock);
1830 static int irttp_seq_show(struct seq_file *seq, void *v)
1832 const struct irttp_iter_state *iter = seq->private;
1833 const struct tsap_cb *self = v;
1835 seq_printf(seq, "TSAP %d, ", iter->id);
1836 seq_printf(seq, "stsap_sel: %02x, ",
1837 self->stsap_sel);
1838 seq_printf(seq, "dtsap_sel: %02x\n",
1839 self->dtsap_sel);
1840 seq_printf(seq, " connected: %s, ",
1841 self->connected? "TRUE":"FALSE");
1842 seq_printf(seq, "avail credit: %d, ",
1843 self->avail_credit);
1844 seq_printf(seq, "remote credit: %d, ",
1845 self->remote_credit);
1846 seq_printf(seq, "send credit: %d\n",
1847 self->send_credit);
1848 seq_printf(seq, " tx packets: %ld, ",
1849 self->stats.tx_packets);
1850 seq_printf(seq, "rx packets: %ld, ",
1851 self->stats.rx_packets);
1852 seq_printf(seq, "tx_queue len: %d ",
1853 skb_queue_len(&self->tx_queue));
1854 seq_printf(seq, "rx_queue len: %d\n",
1855 skb_queue_len(&self->rx_queue));
1856 seq_printf(seq, " tx_sdu_busy: %s, ",
1857 self->tx_sdu_busy? "TRUE":"FALSE");
1858 seq_printf(seq, "rx_sdu_busy: %s\n",
1859 self->rx_sdu_busy? "TRUE":"FALSE");
1860 seq_printf(seq, " max_seg_size: %d, ",
1861 self->max_seg_size);
1862 seq_printf(seq, "tx_max_sdu_size: %d, ",
1863 self->tx_max_sdu_size);
1864 seq_printf(seq, "rx_max_sdu_size: %d\n",
1865 self->rx_max_sdu_size);
1867 seq_printf(seq, " Used by (%s)\n\n",
1868 self->notify.name);
1869 return 0;
1872 static struct seq_operations irttp_seq_ops = {
1873 .start = irttp_seq_start,
1874 .next = irttp_seq_next,
1875 .stop = irttp_seq_stop,
1876 .show = irttp_seq_show,
1879 static int irttp_seq_open(struct inode *inode, struct file *file)
1881 struct seq_file *seq;
1882 int rc = -ENOMEM;
1883 struct irttp_iter_state *s;
1885 IRDA_ASSERT(irttp != NULL, return -EINVAL;);
1887 s = kmalloc(sizeof(*s), GFP_KERNEL);
1888 if (!s)
1889 goto out;
1891 rc = seq_open(file, &irttp_seq_ops);
1892 if (rc)
1893 goto out_kfree;
1895 seq = file->private_data;
1896 seq->private = s;
1897 memset(s, 0, sizeof(*s));
1898 out:
1899 return rc;
1900 out_kfree:
1901 kfree(s);
1902 goto out;
1905 struct file_operations irttp_seq_fops = {
1906 .owner = THIS_MODULE,
1907 .open = irttp_seq_open,
1908 .read = seq_read,
1909 .llseek = seq_lseek,
1910 .release = seq_release_private,
1913 #endif /* PROC_FS */