ptrace: cleanup arch_ptrace() on m68k
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / irda / irttp.c
blob285761e77d90f98ee15c6b47b34c8ead10692e33
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>
31 #include <linux/slab.h>
33 #include <asm/byteorder.h>
34 #include <asm/unaligned.h>
36 #include <net/irda/irda.h>
37 #include <net/irda/irlap.h>
38 #include <net/irda/irlmp.h>
39 #include <net/irda/parameters.h>
40 #include <net/irda/irttp.h>
42 static struct irttp_cb *irttp;
44 static void __irttp_close_tsap(struct tsap_cb *self);
46 static int irttp_data_indication(void *instance, void *sap,
47 struct sk_buff *skb);
48 static int irttp_udata_indication(void *instance, void *sap,
49 struct sk_buff *skb);
50 static void irttp_disconnect_indication(void *instance, void *sap,
51 LM_REASON reason, struct sk_buff *);
52 static void irttp_connect_indication(void *instance, void *sap,
53 struct qos_info *qos, __u32 max_sdu_size,
54 __u8 header_size, struct sk_buff *skb);
55 static void irttp_connect_confirm(void *instance, void *sap,
56 struct qos_info *qos, __u32 max_sdu_size,
57 __u8 header_size, struct sk_buff *skb);
58 static void irttp_run_tx_queue(struct tsap_cb *self);
59 static void irttp_run_rx_queue(struct tsap_cb *self);
61 static void irttp_flush_queues(struct tsap_cb *self);
62 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
63 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
64 static void irttp_todo_expired(unsigned long data);
65 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
66 int get);
68 static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow);
69 static void irttp_status_indication(void *instance,
70 LINK_STATUS link, LOCK_STATUS lock);
72 /* Information for parsing parameters in IrTTP */
73 static pi_minor_info_t pi_minor_call_table[] = {
74 { NULL, 0 }, /* 0x00 */
75 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
77 static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
78 static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
80 /************************ GLOBAL PROCEDURES ************************/
83 * Function irttp_init (void)
85 * Initialize the IrTTP layer. Called by module initialization code
88 int __init irttp_init(void)
90 irttp = kzalloc(sizeof(struct irttp_cb), GFP_KERNEL);
91 if (irttp == NULL)
92 return -ENOMEM;
94 irttp->magic = TTP_MAGIC;
96 irttp->tsaps = hashbin_new(HB_LOCK);
97 if (!irttp->tsaps) {
98 IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n",
99 __func__);
100 kfree(irttp);
101 return -ENOMEM;
104 return 0;
108 * Function irttp_cleanup (void)
110 * Called by module destruction/cleanup code
113 void irttp_cleanup(void)
115 /* Check for main structure */
116 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;);
119 * Delete hashbin and close all TSAP instances in it
121 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
123 irttp->magic = 0;
125 /* De-allocate main structure */
126 kfree(irttp);
128 irttp = NULL;
131 /*************************** SUBROUTINES ***************************/
134 * Function irttp_start_todo_timer (self, timeout)
136 * Start todo timer.
138 * Made it more effient and unsensitive to race conditions - Jean II
140 static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
142 /* Set new value for timer */
143 mod_timer(&self->todo_timer, jiffies + timeout);
147 * Function irttp_todo_expired (data)
149 * Todo timer has expired!
151 * One of the restriction of the timer is that it is run only on the timer
152 * interrupt which run every 10ms. This mean that even if you set the timer
153 * with a delay of 0, it may take up to 10ms before it's run.
154 * So, to minimise latency and keep cache fresh, we try to avoid using
155 * it as much as possible.
156 * Note : we can't use tasklets, because they can't be asynchronously
157 * killed (need user context), and we can't guarantee that here...
158 * Jean II
160 static void irttp_todo_expired(unsigned long data)
162 struct tsap_cb *self = (struct tsap_cb *) data;
164 /* Check that we still exist */
165 if (!self || self->magic != TTP_TSAP_MAGIC)
166 return;
168 IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self);
170 /* Try to make some progress, especially on Tx side - Jean II */
171 irttp_run_rx_queue(self);
172 irttp_run_tx_queue(self);
174 /* Check if time for disconnect */
175 if (test_bit(0, &self->disconnect_pend)) {
176 /* Check if it's possible to disconnect yet */
177 if (skb_queue_empty(&self->tx_queue)) {
178 /* Make sure disconnect is not pending anymore */
179 clear_bit(0, &self->disconnect_pend); /* FALSE */
181 /* Note : self->disconnect_skb may be NULL */
182 irttp_disconnect_request(self, self->disconnect_skb,
183 P_NORMAL);
184 self->disconnect_skb = NULL;
185 } else {
186 /* Try again later */
187 irttp_start_todo_timer(self, HZ/10);
189 /* No reason to try and close now */
190 return;
194 /* Check if it's closing time */
195 if (self->close_pend)
196 /* Finish cleanup */
197 irttp_close_tsap(self);
201 * Function irttp_flush_queues (self)
203 * Flushes (removes all frames) in transitt-buffer (tx_list)
205 static void irttp_flush_queues(struct tsap_cb *self)
207 struct sk_buff* skb;
209 IRDA_DEBUG(4, "%s()\n", __func__);
211 IRDA_ASSERT(self != NULL, return;);
212 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
214 /* Deallocate frames waiting to be sent */
215 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
216 dev_kfree_skb(skb);
218 /* Deallocate received frames */
219 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
220 dev_kfree_skb(skb);
222 /* Deallocate received fragments */
223 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
224 dev_kfree_skb(skb);
228 * Function irttp_reassemble (self)
230 * Makes a new (continuous) skb of all the fragments in the fragment
231 * queue
234 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
236 struct sk_buff *skb, *frag;
237 int n = 0; /* Fragment index */
239 IRDA_ASSERT(self != NULL, return NULL;);
240 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
242 IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __func__,
243 self->rx_sdu_size);
245 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
246 if (!skb)
247 return NULL;
250 * Need to reserve space for TTP header in case this skb needs to
251 * be requeued in case delivery failes
253 skb_reserve(skb, TTP_HEADER);
254 skb_put(skb, self->rx_sdu_size);
257 * Copy all fragments to a new buffer
259 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
260 skb_copy_to_linear_data_offset(skb, n, frag->data, frag->len);
261 n += frag->len;
263 dev_kfree_skb(frag);
266 IRDA_DEBUG(2,
267 "%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
268 __func__, n, self->rx_sdu_size, self->rx_max_sdu_size);
269 /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size
270 * by summing the size of all fragments, so we should always
271 * have n == self->rx_sdu_size, except in cases where we
272 * droped the last fragment (when self->rx_sdu_size exceed
273 * self->rx_max_sdu_size), where n < self->rx_sdu_size.
274 * Jean II */
275 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
277 /* Set the new length */
278 skb_trim(skb, n);
280 self->rx_sdu_size = 0;
282 return skb;
286 * Function irttp_fragment_skb (skb)
288 * Fragments a frame and queues all the fragments for transmission
291 static inline void irttp_fragment_skb(struct tsap_cb *self,
292 struct sk_buff *skb)
294 struct sk_buff *frag;
295 __u8 *frame;
297 IRDA_DEBUG(2, "%s()\n", __func__);
299 IRDA_ASSERT(self != NULL, return;);
300 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
301 IRDA_ASSERT(skb != NULL, return;);
304 * Split frame into a number of segments
306 while (skb->len > self->max_seg_size) {
307 IRDA_DEBUG(2, "%s(), fragmenting ...\n", __func__);
309 /* Make new segment */
310 frag = alloc_skb(self->max_seg_size+self->max_header_size,
311 GFP_ATOMIC);
312 if (!frag)
313 return;
315 skb_reserve(frag, self->max_header_size);
317 /* Copy data from the original skb into this fragment. */
318 skb_copy_from_linear_data(skb, skb_put(frag, self->max_seg_size),
319 self->max_seg_size);
321 /* Insert TTP header, with the more bit set */
322 frame = skb_push(frag, TTP_HEADER);
323 frame[0] = TTP_MORE;
325 /* Hide the copied data from the original skb */
326 skb_pull(skb, self->max_seg_size);
328 /* Queue fragment */
329 skb_queue_tail(&self->tx_queue, frag);
331 /* Queue what is left of the original skb */
332 IRDA_DEBUG(2, "%s(), queuing last segment\n", __func__);
334 frame = skb_push(skb, TTP_HEADER);
335 frame[0] = 0x00; /* Clear more bit */
337 /* Queue fragment */
338 skb_queue_tail(&self->tx_queue, skb);
342 * Function irttp_param_max_sdu_size (self, param)
344 * Handle the MaxSduSize parameter in the connect frames, this function
345 * will be called both when this parameter needs to be inserted into, and
346 * extracted from the connect frames
348 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
349 int get)
351 struct tsap_cb *self;
353 self = (struct tsap_cb *) instance;
355 IRDA_ASSERT(self != NULL, return -1;);
356 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
358 if (get)
359 param->pv.i = self->tx_max_sdu_size;
360 else
361 self->tx_max_sdu_size = param->pv.i;
363 IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __func__, param->pv.i);
365 return 0;
368 /*************************** CLIENT CALLS ***************************/
369 /************************** LMP CALLBACKS **************************/
370 /* Everything is happily mixed up. Waiting for next clean up - Jean II */
373 * Initialization, that has to be done on new tsap
374 * instance allocation and on duplication
376 static void irttp_init_tsap(struct tsap_cb *tsap)
378 spin_lock_init(&tsap->lock);
379 init_timer(&tsap->todo_timer);
381 skb_queue_head_init(&tsap->rx_queue);
382 skb_queue_head_init(&tsap->tx_queue);
383 skb_queue_head_init(&tsap->rx_fragments);
387 * Function irttp_open_tsap (stsap, notify)
389 * Create TSAP connection endpoint,
391 struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
393 struct tsap_cb *self;
394 struct lsap_cb *lsap;
395 notify_t ttp_notify;
397 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
399 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
400 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
401 * JeanII */
402 if((stsap_sel != LSAP_ANY) &&
403 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
404 IRDA_DEBUG(0, "%s(), invalid tsap!\n", __func__);
405 return NULL;
408 self = kzalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
409 if (self == NULL) {
410 IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __func__);
411 return NULL;
414 /* Initialize internal objects */
415 irttp_init_tsap(self);
417 /* Initialise todo timer */
418 self->todo_timer.data = (unsigned long) self;
419 self->todo_timer.function = &irttp_todo_expired;
421 /* Initialize callbacks for IrLMP to use */
422 irda_notify_init(&ttp_notify);
423 ttp_notify.connect_confirm = irttp_connect_confirm;
424 ttp_notify.connect_indication = irttp_connect_indication;
425 ttp_notify.disconnect_indication = irttp_disconnect_indication;
426 ttp_notify.data_indication = irttp_data_indication;
427 ttp_notify.udata_indication = irttp_udata_indication;
428 ttp_notify.flow_indication = irttp_flow_indication;
429 if(notify->status_indication != NULL)
430 ttp_notify.status_indication = irttp_status_indication;
431 ttp_notify.instance = self;
432 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
434 self->magic = TTP_TSAP_MAGIC;
435 self->connected = FALSE;
438 * Create LSAP at IrLMP layer
440 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
441 if (lsap == NULL) {
442 IRDA_WARNING("%s: unable to allocate LSAP!!\n", __func__);
443 return NULL;
447 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
448 * will replace it with whatever source selector which is free, so
449 * the stsap_sel we have might not be valid anymore
451 self->stsap_sel = lsap->slsap_sel;
452 IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __func__, self->stsap_sel);
454 self->notify = *notify;
455 self->lsap = lsap;
457 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
459 if (credit > TTP_RX_MAX_CREDIT)
460 self->initial_credit = TTP_RX_MAX_CREDIT;
461 else
462 self->initial_credit = credit;
464 return self;
466 EXPORT_SYMBOL(irttp_open_tsap);
469 * Function irttp_close (handle)
471 * Remove an instance of a TSAP. This function should only deal with the
472 * deallocation of the TSAP, and resetting of the TSAPs values;
475 static void __irttp_close_tsap(struct tsap_cb *self)
477 /* First make sure we're connected. */
478 IRDA_ASSERT(self != NULL, return;);
479 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
481 irttp_flush_queues(self);
483 del_timer(&self->todo_timer);
485 /* This one won't be cleaned up if we are disconnect_pend + close_pend
486 * and we receive a disconnect_indication */
487 if (self->disconnect_skb)
488 dev_kfree_skb(self->disconnect_skb);
490 self->connected = FALSE;
491 self->magic = ~TTP_TSAP_MAGIC;
493 kfree(self);
497 * Function irttp_close (self)
499 * Remove TSAP from list of all TSAPs and then deallocate all resources
500 * associated with this TSAP
502 * Note : because we *free* the tsap structure, it is the responsibility
503 * of the caller to make sure we are called only once and to deal with
504 * possible race conditions. - Jean II
506 int irttp_close_tsap(struct tsap_cb *self)
508 struct tsap_cb *tsap;
510 IRDA_DEBUG(4, "%s()\n", __func__);
512 IRDA_ASSERT(self != NULL, return -1;);
513 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
515 /* Make sure tsap has been disconnected */
516 if (self->connected) {
517 /* Check if disconnect is not pending */
518 if (!test_bit(0, &self->disconnect_pend)) {
519 IRDA_WARNING("%s: TSAP still connected!\n",
520 __func__);
521 irttp_disconnect_request(self, NULL, P_NORMAL);
523 self->close_pend = TRUE;
524 irttp_start_todo_timer(self, HZ/10);
526 return 0; /* Will be back! */
529 tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);
531 IRDA_ASSERT(tsap == self, return -1;);
533 /* Close corresponding LSAP */
534 if (self->lsap) {
535 irlmp_close_lsap(self->lsap);
536 self->lsap = NULL;
539 __irttp_close_tsap(self);
541 return 0;
543 EXPORT_SYMBOL(irttp_close_tsap);
546 * Function irttp_udata_request (self, skb)
548 * Send unreliable data on this TSAP
551 int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
553 IRDA_ASSERT(self != NULL, return -1;);
554 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
555 IRDA_ASSERT(skb != NULL, return -1;);
557 IRDA_DEBUG(4, "%s()\n", __func__);
559 /* Check that nothing bad happens */
560 if ((skb->len == 0) || (!self->connected)) {
561 IRDA_DEBUG(1, "%s(), No data, or not connected\n",
562 __func__);
563 goto err;
566 if (skb->len > self->max_seg_size) {
567 IRDA_DEBUG(1, "%s(), UData is too large for IrLAP!\n",
568 __func__);
569 goto err;
572 irlmp_udata_request(self->lsap, skb);
573 self->stats.tx_packets++;
575 return 0;
577 err:
578 dev_kfree_skb(skb);
579 return -1;
581 EXPORT_SYMBOL(irttp_udata_request);
585 * Function irttp_data_request (handle, skb)
587 * Queue frame for transmission. If SAR is enabled, fragement the frame
588 * and queue the fragments for transmission
590 int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
592 __u8 *frame;
593 int ret;
595 IRDA_ASSERT(self != NULL, return -1;);
596 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
597 IRDA_ASSERT(skb != NULL, return -1;);
599 IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__,
600 skb_queue_len(&self->tx_queue));
602 /* Check that nothing bad happens */
603 if ((skb->len == 0) || (!self->connected)) {
604 IRDA_WARNING("%s: No data, or not connected\n", __func__);
605 ret = -ENOTCONN;
606 goto err;
610 * Check if SAR is disabled, and the frame is larger than what fits
611 * inside an IrLAP frame
613 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
614 IRDA_ERROR("%s: SAR disabled, and data is too large for IrLAP!\n",
615 __func__);
616 ret = -EMSGSIZE;
617 goto err;
621 * Check if SAR is enabled, and the frame is larger than the
622 * TxMaxSduSize
624 if ((self->tx_max_sdu_size != 0) &&
625 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
626 (skb->len > self->tx_max_sdu_size))
628 IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
629 __func__);
630 ret = -EMSGSIZE;
631 goto err;
634 * Check if transmit queue is full
636 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
638 * Give it a chance to empty itself
640 irttp_run_tx_queue(self);
642 /* Drop packet. This error code should trigger the caller
643 * to resend the data in the client code - Jean II */
644 ret = -ENOBUFS;
645 goto err;
648 /* Queue frame, or queue frame segments */
649 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
650 /* Queue frame */
651 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
652 frame = skb_push(skb, TTP_HEADER);
653 frame[0] = 0x00; /* Clear more bit */
655 skb_queue_tail(&self->tx_queue, skb);
656 } else {
658 * Fragment the frame, this function will also queue the
659 * fragments, we don't care about the fact the transmit
660 * queue may be overfilled by all the segments for a little
661 * while
663 irttp_fragment_skb(self, skb);
666 /* Check if we can accept more data from client */
667 if ((!self->tx_sdu_busy) &&
668 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
669 /* Tx queue filling up, so stop client. */
670 if (self->notify.flow_indication) {
671 self->notify.flow_indication(self->notify.instance,
672 self, FLOW_STOP);
674 /* self->tx_sdu_busy is the state of the client.
675 * Update state after notifying client to avoid
676 * race condition with irttp_flow_indication().
677 * If the queue empty itself after our test but before
678 * we set the flag, we will fix ourselves below in
679 * irttp_run_tx_queue().
680 * Jean II */
681 self->tx_sdu_busy = TRUE;
684 /* Try to make some progress */
685 irttp_run_tx_queue(self);
687 return 0;
689 err:
690 dev_kfree_skb(skb);
691 return ret;
693 EXPORT_SYMBOL(irttp_data_request);
696 * Function irttp_run_tx_queue (self)
698 * Transmit packets queued for transmission (if possible)
701 static void irttp_run_tx_queue(struct tsap_cb *self)
703 struct sk_buff *skb;
704 unsigned long flags;
705 int n;
707 IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n",
708 __func__,
709 self->send_credit, skb_queue_len(&self->tx_queue));
711 /* Get exclusive access to the tx queue, otherwise don't touch it */
712 if (irda_lock(&self->tx_queue_lock) == FALSE)
713 return;
715 /* Try to send out frames as long as we have credits
716 * and as long as LAP is not full. If LAP is full, it will
717 * poll us through irttp_flow_indication() - Jean II */
718 while ((self->send_credit > 0) &&
719 (!irlmp_lap_tx_queue_full(self->lsap)) &&
720 (skb = skb_dequeue(&self->tx_queue)))
723 * Since we can transmit and receive frames concurrently,
724 * the code below is a critical region and we must assure that
725 * nobody messes with the credits while we update them.
727 spin_lock_irqsave(&self->lock, flags);
729 n = self->avail_credit;
730 self->avail_credit = 0;
732 /* Only room for 127 credits in frame */
733 if (n > 127) {
734 self->avail_credit = n-127;
735 n = 127;
737 self->remote_credit += n;
738 self->send_credit--;
740 spin_unlock_irqrestore(&self->lock, flags);
743 * More bit must be set by the data_request() or fragment()
744 * functions
746 skb->data[0] |= (n & 0x7f);
748 /* Detach from socket.
749 * The current skb has a reference to the socket that sent
750 * it (skb->sk). When we pass it to IrLMP, the skb will be
751 * stored in in IrLAP (self->wx_list). When we are within
752 * IrLAP, we lose the notion of socket, so we should not
753 * have a reference to a socket. So, we drop it here.
755 * Why does it matter ?
756 * When the skb is freed (kfree_skb), if it is associated
757 * with a socket, it release buffer space on the socket
758 * (through sock_wfree() and sock_def_write_space()).
759 * If the socket no longer exist, we may crash. Hard.
760 * When we close a socket, we make sure that associated packets
761 * in IrTTP are freed. However, we have no way to cancel
762 * the packet that we have passed to IrLAP. So, if a packet
763 * remains in IrLAP (retry on the link or else) after we
764 * close the socket, we are dead !
765 * Jean II */
766 if (skb->sk != NULL) {
767 /* IrSOCK application, IrOBEX, ... */
768 skb_orphan(skb);
770 /* IrCOMM over IrTTP, IrLAN, ... */
772 /* Pass the skb to IrLMP - done */
773 irlmp_data_request(self->lsap, skb);
774 self->stats.tx_packets++;
777 /* Check if we can accept more frames from client.
778 * We don't want to wait until the todo timer to do that, and we
779 * can't use tasklets (grr...), so we are obliged to give control
780 * to client. That's ok, this test will be true not too often
781 * (max once per LAP window) and we are called from places
782 * where we can spend a bit of time doing stuff. - Jean II */
783 if ((self->tx_sdu_busy) &&
784 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
785 (!self->close_pend))
787 if (self->notify.flow_indication)
788 self->notify.flow_indication(self->notify.instance,
789 self, FLOW_START);
791 /* self->tx_sdu_busy is the state of the client.
792 * We don't really have a race here, but it's always safer
793 * to update our state after the client - Jean II */
794 self->tx_sdu_busy = FALSE;
797 /* Reset lock */
798 self->tx_queue_lock = 0;
802 * Function irttp_give_credit (self)
804 * Send a dataless flowdata TTP-PDU and give available credit to peer
805 * TSAP
807 static inline void irttp_give_credit(struct tsap_cb *self)
809 struct sk_buff *tx_skb = NULL;
810 unsigned long flags;
811 int n;
813 IRDA_ASSERT(self != NULL, return;);
814 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
816 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n",
817 __func__,
818 self->send_credit, self->avail_credit, self->remote_credit);
820 /* Give credit to peer */
821 tx_skb = alloc_skb(TTP_MAX_HEADER, GFP_ATOMIC);
822 if (!tx_skb)
823 return;
825 /* Reserve space for LMP, and LAP header */
826 skb_reserve(tx_skb, LMP_MAX_HEADER);
829 * Since we can transmit and receive frames concurrently,
830 * the code below is a critical region and we must assure that
831 * nobody messes with the credits while we update them.
833 spin_lock_irqsave(&self->lock, flags);
835 n = self->avail_credit;
836 self->avail_credit = 0;
838 /* Only space for 127 credits in frame */
839 if (n > 127) {
840 self->avail_credit = n - 127;
841 n = 127;
843 self->remote_credit += n;
845 spin_unlock_irqrestore(&self->lock, flags);
847 skb_put(tx_skb, 1);
848 tx_skb->data[0] = (__u8) (n & 0x7f);
850 irlmp_data_request(self->lsap, tx_skb);
851 self->stats.tx_packets++;
855 * Function irttp_udata_indication (instance, sap, skb)
857 * Received some unit-data (unreliable)
860 static int irttp_udata_indication(void *instance, void *sap,
861 struct sk_buff *skb)
863 struct tsap_cb *self;
864 int err;
866 IRDA_DEBUG(4, "%s()\n", __func__);
868 self = (struct tsap_cb *) instance;
870 IRDA_ASSERT(self != NULL, return -1;);
871 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
872 IRDA_ASSERT(skb != NULL, return -1;);
874 self->stats.rx_packets++;
876 /* Just pass data to layer above */
877 if (self->notify.udata_indication) {
878 err = self->notify.udata_indication(self->notify.instance,
879 self,skb);
880 /* Same comment as in irttp_do_data_indication() */
881 if (!err)
882 return 0;
884 /* Either no handler, or handler returns an error */
885 dev_kfree_skb(skb);
887 return 0;
891 * Function irttp_data_indication (instance, sap, skb)
893 * Receive segment from IrLMP.
896 static int irttp_data_indication(void *instance, void *sap,
897 struct sk_buff *skb)
899 struct tsap_cb *self;
900 unsigned long flags;
901 int n;
903 self = (struct tsap_cb *) instance;
905 n = skb->data[0] & 0x7f; /* Extract the credits */
907 self->stats.rx_packets++;
909 /* Deal with inbound credit
910 * Since we can transmit and receive frames concurrently,
911 * the code below is a critical region and we must assure that
912 * nobody messes with the credits while we update them.
914 spin_lock_irqsave(&self->lock, flags);
915 self->send_credit += n;
916 if (skb->len > 1)
917 self->remote_credit--;
918 spin_unlock_irqrestore(&self->lock, flags);
921 * Data or dataless packet? Dataless frames contains only the
922 * TTP_HEADER.
924 if (skb->len > 1) {
926 * We don't remove the TTP header, since we must preserve the
927 * more bit, so the defragment routing knows what to do
929 skb_queue_tail(&self->rx_queue, skb);
930 } else {
931 /* Dataless flowdata TTP-PDU */
932 dev_kfree_skb(skb);
936 /* Push data to the higher layer.
937 * We do it synchronously because running the todo timer for each
938 * receive packet would be too much overhead and latency.
939 * By passing control to the higher layer, we run the risk that
940 * it may take time or grab a lock. Most often, the higher layer
941 * will only put packet in a queue.
942 * Anyway, packets are only dripping through the IrDA, so we can
943 * have time before the next packet.
944 * Further, we are run from NET_BH, so the worse that can happen is
945 * us missing the optimal time to send back the PF bit in LAP.
946 * Jean II */
947 irttp_run_rx_queue(self);
949 /* We now give credits to peer in irttp_run_rx_queue().
950 * We need to send credit *NOW*, otherwise we are going
951 * to miss the next Tx window. The todo timer may take
952 * a while before it's run... - Jean II */
955 * If the peer device has given us some credits and we didn't have
956 * anyone from before, then we need to shedule the tx queue.
957 * We need to do that because our Tx have stopped (so we may not
958 * get any LAP flow indication) and the user may be stopped as
959 * well. - Jean II
961 if (self->send_credit == n) {
962 /* Restart pushing stuff to LAP */
963 irttp_run_tx_queue(self);
964 /* Note : we don't want to schedule the todo timer
965 * because it has horrible latency. No tasklets
966 * because the tasklet API is broken. - Jean II */
969 return 0;
973 * Function irttp_status_indication (self, reason)
975 * Status_indication, just pass to the higher layer...
978 static void irttp_status_indication(void *instance,
979 LINK_STATUS link, LOCK_STATUS lock)
981 struct tsap_cb *self;
983 IRDA_DEBUG(4, "%s()\n", __func__);
985 self = (struct tsap_cb *) instance;
987 IRDA_ASSERT(self != NULL, return;);
988 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
990 /* Check if client has already closed the TSAP and gone away */
991 if (self->close_pend)
992 return;
995 * Inform service user if he has requested it
997 if (self->notify.status_indication != NULL)
998 self->notify.status_indication(self->notify.instance,
999 link, lock);
1000 else
1001 IRDA_DEBUG(2, "%s(), no handler\n", __func__);
1005 * Function irttp_flow_indication (self, reason)
1007 * Flow_indication : IrLAP tells us to send more data.
1010 static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
1012 struct tsap_cb *self;
1014 self = (struct tsap_cb *) instance;
1016 IRDA_ASSERT(self != NULL, return;);
1017 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1019 IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self);
1021 /* We are "polled" directly from LAP, and the LAP want to fill
1022 * its Tx window. We want to do our best to send it data, so that
1023 * we maximise the window. On the other hand, we want to limit the
1024 * amount of work here so that LAP doesn't hang forever waiting
1025 * for packets. - Jean II */
1027 /* Try to send some packets. Currently, LAP calls us every time
1028 * there is one free slot, so we will send only one packet.
1029 * This allow the scheduler to do its round robin - Jean II */
1030 irttp_run_tx_queue(self);
1032 /* Note regarding the interraction with higher layer.
1033 * irttp_run_tx_queue() may call the client when its queue
1034 * start to empty, via notify.flow_indication(). Initially.
1035 * I wanted this to happen in a tasklet, to avoid client
1036 * grabbing the CPU, but we can't use tasklets safely. And timer
1037 * is definitely too slow.
1038 * This will happen only once per LAP window, and usually at
1039 * the third packet (unless window is smaller). LAP is still
1040 * doing mtt and sending first packet so it's sort of OK
1041 * to do that. Jean II */
1043 /* If we need to send disconnect. try to do it now */
1044 if(self->disconnect_pend)
1045 irttp_start_todo_timer(self, 0);
1049 * Function irttp_flow_request (self, command)
1051 * This function could be used by the upper layers to tell IrTTP to stop
1052 * delivering frames if the receive queues are starting to get full, or
1053 * to tell IrTTP to start delivering frames again.
1055 void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1057 IRDA_DEBUG(1, "%s()\n", __func__);
1059 IRDA_ASSERT(self != NULL, return;);
1060 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1062 switch (flow) {
1063 case FLOW_STOP:
1064 IRDA_DEBUG(1, "%s(), flow stop\n", __func__);
1065 self->rx_sdu_busy = TRUE;
1066 break;
1067 case FLOW_START:
1068 IRDA_DEBUG(1, "%s(), flow start\n", __func__);
1069 self->rx_sdu_busy = FALSE;
1071 /* Client say he can accept more data, try to free our
1072 * queues ASAP - Jean II */
1073 irttp_run_rx_queue(self);
1075 break;
1076 default:
1077 IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __func__);
1080 EXPORT_SYMBOL(irttp_flow_request);
1083 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
1085 * Try to connect to remote destination TSAP selector
1088 int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1089 __u32 saddr, __u32 daddr,
1090 struct qos_info *qos, __u32 max_sdu_size,
1091 struct sk_buff *userdata)
1093 struct sk_buff *tx_skb;
1094 __u8 *frame;
1095 __u8 n;
1097 IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __func__, max_sdu_size);
1099 IRDA_ASSERT(self != NULL, return -EBADR;);
1100 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1102 if (self->connected) {
1103 if(userdata)
1104 dev_kfree_skb(userdata);
1105 return -EISCONN;
1108 /* Any userdata supplied? */
1109 if (userdata == NULL) {
1110 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1111 GFP_ATOMIC);
1112 if (!tx_skb)
1113 return -ENOMEM;
1115 /* Reserve space for MUX_CONTROL and LAP header */
1116 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
1117 } else {
1118 tx_skb = userdata;
1120 * Check that the client has reserved enough space for
1121 * headers
1123 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1124 { dev_kfree_skb(userdata); return -1; } );
1127 /* Initialize connection parameters */
1128 self->connected = FALSE;
1129 self->avail_credit = 0;
1130 self->rx_max_sdu_size = max_sdu_size;
1131 self->rx_sdu_size = 0;
1132 self->rx_sdu_busy = FALSE;
1133 self->dtsap_sel = dtsap_sel;
1135 n = self->initial_credit;
1137 self->remote_credit = 0;
1138 self->send_credit = 0;
1141 * Give away max 127 credits for now
1143 if (n > 127) {
1144 self->avail_credit=n-127;
1145 n = 127;
1148 self->remote_credit = n;
1150 /* SAR enabled? */
1151 if (max_sdu_size > 0) {
1152 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1153 { dev_kfree_skb(tx_skb); return -1; } );
1155 /* Insert SAR parameters */
1156 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1158 frame[0] = TTP_PARAMETERS | n;
1159 frame[1] = 0x04; /* Length */
1160 frame[2] = 0x01; /* MaxSduSize */
1161 frame[3] = 0x02; /* Value length */
1163 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1164 (__be16 *)(frame+4));
1165 } else {
1166 /* Insert plain TTP header */
1167 frame = skb_push(tx_skb, TTP_HEADER);
1169 /* Insert initial credit in frame */
1170 frame[0] = n & 0x7f;
1173 /* Connect with IrLMP. No QoS parameters for now */
1174 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1175 tx_skb);
1177 EXPORT_SYMBOL(irttp_connect_request);
1180 * Function irttp_connect_confirm (handle, qos, skb)
1182 * Sevice user confirms TSAP connection with peer.
1185 static void irttp_connect_confirm(void *instance, void *sap,
1186 struct qos_info *qos, __u32 max_seg_size,
1187 __u8 max_header_size, struct sk_buff *skb)
1189 struct tsap_cb *self;
1190 int parameters;
1191 int ret;
1192 __u8 plen;
1193 __u8 n;
1195 IRDA_DEBUG(4, "%s()\n", __func__);
1197 self = (struct tsap_cb *) instance;
1199 IRDA_ASSERT(self != NULL, return;);
1200 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1201 IRDA_ASSERT(skb != NULL, return;);
1203 self->max_seg_size = max_seg_size - TTP_HEADER;
1204 self->max_header_size = max_header_size + TTP_HEADER;
1207 * Check if we have got some QoS parameters back! This should be the
1208 * negotiated QoS for the link.
1210 if (qos) {
1211 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
1212 qos->baud_rate.bits);
1213 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1214 qos->baud_rate.value);
1217 n = skb->data[0] & 0x7f;
1219 IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __func__, n);
1221 self->send_credit = n;
1222 self->tx_max_sdu_size = 0;
1223 self->connected = TRUE;
1225 parameters = skb->data[0] & 0x80;
1227 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1228 skb_pull(skb, TTP_HEADER);
1230 if (parameters) {
1231 plen = skb->data[0];
1233 ret = irda_param_extract_all(self, skb->data+1,
1234 IRDA_MIN(skb->len-1, plen),
1235 &param_info);
1237 /* Any errors in the parameter list? */
1238 if (ret < 0) {
1239 IRDA_WARNING("%s: error extracting parameters\n",
1240 __func__);
1241 dev_kfree_skb(skb);
1243 /* Do not accept this connection attempt */
1244 return;
1246 /* Remove parameters */
1247 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1250 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __func__,
1251 self->send_credit, self->avail_credit, self->remote_credit);
1253 IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __func__,
1254 self->tx_max_sdu_size);
1256 if (self->notify.connect_confirm) {
1257 self->notify.connect_confirm(self->notify.instance, self, qos,
1258 self->tx_max_sdu_size,
1259 self->max_header_size, skb);
1260 } else
1261 dev_kfree_skb(skb);
1265 * Function irttp_connect_indication (handle, skb)
1267 * Some other device is connecting to this TSAP
1270 static void irttp_connect_indication(void *instance, void *sap,
1271 struct qos_info *qos, __u32 max_seg_size, __u8 max_header_size,
1272 struct sk_buff *skb)
1274 struct tsap_cb *self;
1275 struct lsap_cb *lsap;
1276 int parameters;
1277 int ret;
1278 __u8 plen;
1279 __u8 n;
1281 self = (struct tsap_cb *) instance;
1283 IRDA_ASSERT(self != NULL, return;);
1284 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1285 IRDA_ASSERT(skb != NULL, return;);
1287 lsap = (struct lsap_cb *) sap;
1289 self->max_seg_size = max_seg_size - TTP_HEADER;
1290 self->max_header_size = max_header_size+TTP_HEADER;
1292 IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __func__, self->stsap_sel);
1294 /* Need to update dtsap_sel if its equal to LSAP_ANY */
1295 self->dtsap_sel = lsap->dlsap_sel;
1297 n = skb->data[0] & 0x7f;
1299 self->send_credit = n;
1300 self->tx_max_sdu_size = 0;
1302 parameters = skb->data[0] & 0x80;
1304 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1305 skb_pull(skb, TTP_HEADER);
1307 if (parameters) {
1308 plen = skb->data[0];
1310 ret = irda_param_extract_all(self, skb->data+1,
1311 IRDA_MIN(skb->len-1, plen),
1312 &param_info);
1314 /* Any errors in the parameter list? */
1315 if (ret < 0) {
1316 IRDA_WARNING("%s: error extracting parameters\n",
1317 __func__);
1318 dev_kfree_skb(skb);
1320 /* Do not accept this connection attempt */
1321 return;
1324 /* Remove parameters */
1325 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1328 if (self->notify.connect_indication) {
1329 self->notify.connect_indication(self->notify.instance, self,
1330 qos, self->tx_max_sdu_size,
1331 self->max_header_size, skb);
1332 } else
1333 dev_kfree_skb(skb);
1337 * Function irttp_connect_response (handle, userdata)
1339 * Service user is accepting the connection, just pass it down to
1340 * IrLMP!
1343 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1344 struct sk_buff *userdata)
1346 struct sk_buff *tx_skb;
1347 __u8 *frame;
1348 int ret;
1349 __u8 n;
1351 IRDA_ASSERT(self != NULL, return -1;);
1352 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1354 IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __func__,
1355 self->stsap_sel);
1357 /* Any userdata supplied? */
1358 if (userdata == NULL) {
1359 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1360 GFP_ATOMIC);
1361 if (!tx_skb)
1362 return -ENOMEM;
1364 /* Reserve space for MUX_CONTROL and LAP header */
1365 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
1366 } else {
1367 tx_skb = userdata;
1369 * Check that the client has reserved enough space for
1370 * headers
1372 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1373 { dev_kfree_skb(userdata); return -1; } );
1376 self->avail_credit = 0;
1377 self->remote_credit = 0;
1378 self->rx_max_sdu_size = max_sdu_size;
1379 self->rx_sdu_size = 0;
1380 self->rx_sdu_busy = FALSE;
1382 n = self->initial_credit;
1384 /* Frame has only space for max 127 credits (7 bits) */
1385 if (n > 127) {
1386 self->avail_credit = n - 127;
1387 n = 127;
1390 self->remote_credit = n;
1391 self->connected = TRUE;
1393 /* SAR enabled? */
1394 if (max_sdu_size > 0) {
1395 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1396 { dev_kfree_skb(tx_skb); return -1; } );
1398 /* Insert TTP header with SAR parameters */
1399 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1401 frame[0] = TTP_PARAMETERS | n;
1402 frame[1] = 0x04; /* Length */
1404 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
1405 /* TTP_SAR_HEADER, &param_info) */
1407 frame[2] = 0x01; /* MaxSduSize */
1408 frame[3] = 0x02; /* Value length */
1410 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1411 (__be16 *)(frame+4));
1412 } else {
1413 /* Insert TTP header */
1414 frame = skb_push(tx_skb, TTP_HEADER);
1416 frame[0] = n & 0x7f;
1419 ret = irlmp_connect_response(self->lsap, tx_skb);
1421 return ret;
1423 EXPORT_SYMBOL(irttp_connect_response);
1426 * Function irttp_dup (self, instance)
1428 * Duplicate TSAP, can be used by servers to confirm a connection on a
1429 * new TSAP so it can keep listening on the old one.
1431 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1433 struct tsap_cb *new;
1434 unsigned long flags;
1436 IRDA_DEBUG(1, "%s()\n", __func__);
1438 /* Protect our access to the old tsap instance */
1439 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
1441 /* Find the old instance */
1442 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
1443 IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __func__);
1444 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1445 return NULL;
1448 /* Allocate a new instance */
1449 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
1450 if (!new) {
1451 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __func__);
1452 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1453 return NULL;
1455 /* Dup */
1456 memcpy(new, orig, sizeof(struct tsap_cb));
1457 spin_lock_init(&new->lock);
1459 /* We don't need the old instance any more */
1460 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1462 /* Try to dup the LSAP (may fail if we were too slow) */
1463 new->lsap = irlmp_dup(orig->lsap, new);
1464 if (!new->lsap) {
1465 IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
1466 kfree(new);
1467 return NULL;
1470 /* Not everything should be copied */
1471 new->notify.instance = instance;
1473 /* Initialize internal objects */
1474 irttp_init_tsap(new);
1476 /* This is locked */
1477 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
1479 return new;
1481 EXPORT_SYMBOL(irttp_dup);
1484 * Function irttp_disconnect_request (self)
1486 * Close this connection please! If priority is high, the queued data
1487 * segments, if any, will be deallocated first
1490 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1491 int priority)
1493 int ret;
1495 IRDA_ASSERT(self != NULL, return -1;);
1496 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1498 /* Already disconnected? */
1499 if (!self->connected) {
1500 IRDA_DEBUG(4, "%s(), already disconnected!\n", __func__);
1501 if (userdata)
1502 dev_kfree_skb(userdata);
1503 return -1;
1506 /* Disconnect already pending ?
1507 * We need to use an atomic operation to prevent reentry. This
1508 * function may be called from various context, like user, timer
1509 * for following a disconnect_indication() (i.e. net_bh).
1510 * Jean II */
1511 if(test_and_set_bit(0, &self->disconnect_pend)) {
1512 IRDA_DEBUG(0, "%s(), disconnect already pending\n",
1513 __func__);
1514 if (userdata)
1515 dev_kfree_skb(userdata);
1517 /* Try to make some progress */
1518 irttp_run_tx_queue(self);
1519 return -1;
1523 * Check if there is still data segments in the transmit queue
1525 if (!skb_queue_empty(&self->tx_queue)) {
1526 if (priority == P_HIGH) {
1528 * No need to send the queued data, if we are
1529 * disconnecting right now since the data will
1530 * not have any usable connection to be sent on
1532 IRDA_DEBUG(1, "%s(): High priority!!()\n", __func__);
1533 irttp_flush_queues(self);
1534 } else if (priority == P_NORMAL) {
1536 * Must delay disconnect until after all data segments
1537 * have been sent and the tx_queue is empty
1539 /* We'll reuse this one later for the disconnect */
1540 self->disconnect_skb = userdata; /* May be NULL */
1542 irttp_run_tx_queue(self);
1544 irttp_start_todo_timer(self, HZ/10);
1545 return -1;
1548 /* Note : we don't need to check if self->rx_queue is full and the
1549 * state of self->rx_sdu_busy because the disconnect response will
1550 * be sent at the LMP level (so even if the peer has its Tx queue
1551 * full of data). - Jean II */
1553 IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __func__);
1554 self->connected = FALSE;
1556 if (!userdata) {
1557 struct sk_buff *tx_skb;
1558 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
1559 if (!tx_skb)
1560 return -ENOMEM;
1563 * Reserve space for MUX and LAP header
1565 skb_reserve(tx_skb, LMP_MAX_HEADER);
1567 userdata = tx_skb;
1569 ret = irlmp_disconnect_request(self->lsap, userdata);
1571 /* The disconnect is no longer pending */
1572 clear_bit(0, &self->disconnect_pend); /* FALSE */
1574 return ret;
1576 EXPORT_SYMBOL(irttp_disconnect_request);
1579 * Function irttp_disconnect_indication (self, reason)
1581 * Disconnect indication, TSAP disconnected by peer?
1584 static void irttp_disconnect_indication(void *instance, void *sap,
1585 LM_REASON reason, struct sk_buff *skb)
1587 struct tsap_cb *self;
1589 IRDA_DEBUG(4, "%s()\n", __func__);
1591 self = (struct tsap_cb *) instance;
1593 IRDA_ASSERT(self != NULL, return;);
1594 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1596 /* Prevent higher layer to send more data */
1597 self->connected = FALSE;
1599 /* Check if client has already tried to close the TSAP */
1600 if (self->close_pend) {
1601 /* In this case, the higher layer is probably gone. Don't
1602 * bother it and clean up the remains - Jean II */
1603 if (skb)
1604 dev_kfree_skb(skb);
1605 irttp_close_tsap(self);
1606 return;
1609 /* If we are here, we assume that is the higher layer is still
1610 * waiting for the disconnect notification and able to process it,
1611 * even if he tried to disconnect. Otherwise, it would have already
1612 * attempted to close the tsap and self->close_pend would be TRUE.
1613 * Jean II */
1615 /* No need to notify the client if has already tried to disconnect */
1616 if(self->notify.disconnect_indication)
1617 self->notify.disconnect_indication(self->notify.instance, self,
1618 reason, skb);
1619 else
1620 if (skb)
1621 dev_kfree_skb(skb);
1625 * Function irttp_do_data_indication (self, skb)
1627 * Try to deliver reassembled skb to layer above, and requeue it if that
1628 * for some reason should fail. We mark rx sdu as busy to apply back
1629 * pressure is necessary.
1631 static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1633 int err;
1635 /* Check if client has already closed the TSAP and gone away */
1636 if (self->close_pend) {
1637 dev_kfree_skb(skb);
1638 return;
1641 err = self->notify.data_indication(self->notify.instance, self, skb);
1643 /* Usually the layer above will notify that it's input queue is
1644 * starting to get filled by using the flow request, but this may
1645 * be difficult, so it can instead just refuse to eat it and just
1646 * give an error back
1648 if (err) {
1649 IRDA_DEBUG(0, "%s() requeueing skb!\n", __func__);
1651 /* Make sure we take a break */
1652 self->rx_sdu_busy = TRUE;
1654 /* Need to push the header in again */
1655 skb_push(skb, TTP_HEADER);
1656 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1658 /* Put skb back on queue */
1659 skb_queue_head(&self->rx_queue, skb);
1664 * Function irttp_run_rx_queue (self)
1666 * Check if we have any frames to be transmitted, or if we have any
1667 * available credit to give away.
1669 static void irttp_run_rx_queue(struct tsap_cb *self)
1671 struct sk_buff *skb;
1672 int more = 0;
1674 IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __func__,
1675 self->send_credit, self->avail_credit, self->remote_credit);
1677 /* Get exclusive access to the rx queue, otherwise don't touch it */
1678 if (irda_lock(&self->rx_queue_lock) == FALSE)
1679 return;
1682 * Reassemble all frames in receive queue and deliver them
1684 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1685 /* This bit will tell us if it's the last fragment or not */
1686 more = skb->data[0] & 0x80;
1688 /* Remove TTP header */
1689 skb_pull(skb, TTP_HEADER);
1691 /* Add the length of the remaining data */
1692 self->rx_sdu_size += skb->len;
1695 * If SAR is disabled, or user has requested no reassembly
1696 * of received fragments then we just deliver them
1697 * immediately. This can be requested by clients that
1698 * implements byte streams without any message boundaries
1700 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1701 irttp_do_data_indication(self, skb);
1702 self->rx_sdu_size = 0;
1704 continue;
1707 /* Check if this is a fragment, and not the last fragment */
1708 if (more) {
1710 * Queue the fragment if we still are within the
1711 * limits of the maximum size of the rx_sdu
1713 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1714 IRDA_DEBUG(4, "%s(), queueing frag\n",
1715 __func__);
1716 skb_queue_tail(&self->rx_fragments, skb);
1717 } else {
1718 /* Free the part of the SDU that is too big */
1719 dev_kfree_skb(skb);
1721 continue;
1724 * This is the last fragment, so time to reassemble!
1726 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1727 (self->rx_max_sdu_size == TTP_SAR_UNBOUND))
1730 * A little optimizing. Only queue the fragment if
1731 * there are other fragments. Since if this is the
1732 * last and only fragment, there is no need to
1733 * reassemble :-)
1735 if (!skb_queue_empty(&self->rx_fragments)) {
1736 skb_queue_tail(&self->rx_fragments,
1737 skb);
1739 skb = irttp_reassemble_skb(self);
1742 /* Now we can deliver the reassembled skb */
1743 irttp_do_data_indication(self, skb);
1744 } else {
1745 IRDA_DEBUG(1, "%s(), Truncated frame\n", __func__);
1747 /* Free the part of the SDU that is too big */
1748 dev_kfree_skb(skb);
1750 /* Deliver only the valid but truncated part of SDU */
1751 skb = irttp_reassemble_skb(self);
1753 irttp_do_data_indication(self, skb);
1755 self->rx_sdu_size = 0;
1759 * It's not trivial to keep track of how many credits are available
1760 * by incrementing at each packet, because delivery may fail
1761 * (irttp_do_data_indication() may requeue the frame) and because
1762 * we need to take care of fragmentation.
1763 * We want the other side to send up to initial_credit packets.
1764 * We have some frames in our queues, and we have already allowed it
1765 * to send remote_credit.
1766 * No need to spinlock, write is atomic and self correcting...
1767 * Jean II
1769 self->avail_credit = (self->initial_credit -
1770 (self->remote_credit +
1771 skb_queue_len(&self->rx_queue) +
1772 skb_queue_len(&self->rx_fragments)));
1774 /* Do we have too much credits to send to peer ? */
1775 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1776 (self->avail_credit > 0)) {
1777 /* Send explicit credit frame */
1778 irttp_give_credit(self);
1779 /* Note : do *NOT* check if tx_queue is non-empty, that
1780 * will produce deadlocks. I repeat : send a credit frame
1781 * even if we have something to send in our Tx queue.
1782 * If we have credits, it means that our Tx queue is blocked.
1784 * Let's suppose the peer can't keep up with our Tx. He will
1785 * flow control us by not sending us any credits, and we
1786 * will stop Tx and start accumulating credits here.
1787 * Up to the point where the peer will stop its Tx queue,
1788 * for lack of credits.
1789 * Let's assume the peer application is single threaded.
1790 * It will block on Tx and never consume any Rx buffer.
1791 * Deadlock. Guaranteed. - Jean II
1795 /* Reset lock */
1796 self->rx_queue_lock = 0;
1799 #ifdef CONFIG_PROC_FS
1800 struct irttp_iter_state {
1801 int id;
1804 static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
1806 struct irttp_iter_state *iter = seq->private;
1807 struct tsap_cb *self;
1809 /* Protect our access to the tsap list */
1810 spin_lock_irq(&irttp->tsaps->hb_spinlock);
1811 iter->id = 0;
1813 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1814 self != NULL;
1815 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
1816 if (iter->id == *pos)
1817 break;
1818 ++iter->id;
1821 return self;
1824 static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1826 struct irttp_iter_state *iter = seq->private;
1828 ++*pos;
1829 ++iter->id;
1830 return (void *) hashbin_get_next(irttp->tsaps);
1833 static void irttp_seq_stop(struct seq_file *seq, void *v)
1835 spin_unlock_irq(&irttp->tsaps->hb_spinlock);
1838 static int irttp_seq_show(struct seq_file *seq, void *v)
1840 const struct irttp_iter_state *iter = seq->private;
1841 const struct tsap_cb *self = v;
1843 seq_printf(seq, "TSAP %d, ", iter->id);
1844 seq_printf(seq, "stsap_sel: %02x, ",
1845 self->stsap_sel);
1846 seq_printf(seq, "dtsap_sel: %02x\n",
1847 self->dtsap_sel);
1848 seq_printf(seq, " connected: %s, ",
1849 self->connected? "TRUE":"FALSE");
1850 seq_printf(seq, "avail credit: %d, ",
1851 self->avail_credit);
1852 seq_printf(seq, "remote credit: %d, ",
1853 self->remote_credit);
1854 seq_printf(seq, "send credit: %d\n",
1855 self->send_credit);
1856 seq_printf(seq, " tx packets: %lu, ",
1857 self->stats.tx_packets);
1858 seq_printf(seq, "rx packets: %lu, ",
1859 self->stats.rx_packets);
1860 seq_printf(seq, "tx_queue len: %u ",
1861 skb_queue_len(&self->tx_queue));
1862 seq_printf(seq, "rx_queue len: %u\n",
1863 skb_queue_len(&self->rx_queue));
1864 seq_printf(seq, " tx_sdu_busy: %s, ",
1865 self->tx_sdu_busy? "TRUE":"FALSE");
1866 seq_printf(seq, "rx_sdu_busy: %s\n",
1867 self->rx_sdu_busy? "TRUE":"FALSE");
1868 seq_printf(seq, " max_seg_size: %u, ",
1869 self->max_seg_size);
1870 seq_printf(seq, "tx_max_sdu_size: %u, ",
1871 self->tx_max_sdu_size);
1872 seq_printf(seq, "rx_max_sdu_size: %u\n",
1873 self->rx_max_sdu_size);
1875 seq_printf(seq, " Used by (%s)\n\n",
1876 self->notify.name);
1877 return 0;
1880 static const struct seq_operations irttp_seq_ops = {
1881 .start = irttp_seq_start,
1882 .next = irttp_seq_next,
1883 .stop = irttp_seq_stop,
1884 .show = irttp_seq_show,
1887 static int irttp_seq_open(struct inode *inode, struct file *file)
1889 return seq_open_private(file, &irttp_seq_ops,
1890 sizeof(struct irttp_iter_state));
1893 const struct file_operations irttp_seq_fops = {
1894 .owner = THIS_MODULE,
1895 .open = irttp_seq_open,
1896 .read = seq_read,
1897 .llseek = seq_lseek,
1898 .release = seq_release_private,
1901 #endif /* PROC_FS */