Import 2.3.18pre1
[davej-history.git] / net / irda / irttp.c
blob9d1288779f8d379d3c3277376d7d8ff7b0779eef
1 /*********************************************************************
2 *
3 * Filename: irttp.c
4 * Version: 1.2
5 * Description: Tiny Transport Protocol (TTP) implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Aug 31 20:14:31 1997
9 * Modified at: Thu Jul 8 21:25:31 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/config.h>
27 #include <linux/skbuff.h>
28 #include <linux/init.h>
30 #include <asm/byteorder.h>
31 #include <asm/unaligned.h>
33 #include <net/irda/irda.h>
34 #include <net/irda/irmod.h>
35 #include <net/irda/irlap.h>
36 #include <net/irda/irlmp.h>
37 #include <net/irda/parameters.h>
38 #include <net/irda/irttp.h>
40 static struct irttp_cb *irttp = NULL;
42 static void __irttp_close_tsap(struct tsap_cb *self);
44 static int irttp_data_indication(void *instance, void *sap,
45 struct sk_buff *skb);
46 static int irttp_udata_indication(void *instance, void *sap,
47 struct sk_buff *skb);
48 static void irttp_disconnect_indication(void *instance, void *sap,
49 LM_REASON reason, struct sk_buff *);
50 static void irttp_connect_indication(void *instance, void *sap,
51 struct qos_info *qos, __u32 max_sdu_size,
52 __u8 header_size, struct sk_buff *skb);
53 static void irttp_connect_confirm(void *instance, void *sap,
54 struct qos_info *qos, __u32 max_sdu_size,
55 __u8 header_size, struct sk_buff *skb);
56 static void irttp_run_tx_queue(struct tsap_cb *self);
57 static void irttp_run_rx_queue(struct tsap_cb *self);
59 static void irttp_flush_queues(struct tsap_cb *self);
60 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
61 static void irttp_start_todo_timer(struct tsap_cb *self, int timeout);
62 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
63 static int irttp_param_max_sdu_size(void *instance, param_t *param, int get);
65 /* Information for parsing parameters in IrTTP */
66 static pi_minor_info_t pi_minor_call_table[] = {
67 { NULL, 0 }, /* 0x00 */
68 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
70 static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
71 static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
74 * Function irttp_init (void)
76 * Initialize the IrTTP layer. Called by module initialization code
79 int __init irttp_init(void)
81 /* Initialize the irttp structure. */
82 if (irttp == NULL) {
83 irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
84 if (irttp == NULL)
85 return -ENOMEM;
87 memset(irttp, 0, sizeof(struct irttp_cb));
89 irttp->magic = TTP_MAGIC;
91 irttp->tsaps = hashbin_new(HB_LOCAL);
92 if (!irttp->tsaps) {
93 printk(KERN_WARNING "IrDA: Can't allocate IrTTP hashbin!\n");
94 return -ENOMEM;
97 return 0;
101 * Function irttp_cleanup (void)
103 * Called by module destruction/cleanup code
106 #ifdef MODULE
107 void irttp_cleanup(void)
109 /* Check for main structure */
110 ASSERT(irttp != NULL, return;);
111 ASSERT(irttp->magic == TTP_MAGIC, return;);
114 * Delete hashbin and close all TSAP instances in it
116 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
118 irttp->magic = 0;
120 /* De-allocate main structure */
121 kfree(irttp);
123 irttp = NULL;
125 #endif
128 * Function irttp_open_tsap (stsap, notify)
130 * Create TSAP connection endpoint,
132 struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
134 struct tsap_cb *self;
135 struct lsap_cb *lsap;
136 notify_t ttp_notify;
138 ASSERT(irttp != NULL, return NULL;);
139 ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
141 self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
142 if (self == NULL) {
143 DEBUG(0, __FUNCTION__ "(), unable to kmalloc!\n");
144 return NULL;
146 memset(self, 0, sizeof(struct tsap_cb));
148 init_timer(&self->todo_timer);
150 /* Initialize callbacks for IrLMP to use */
151 irda_notify_init(&ttp_notify);
152 ttp_notify.connect_confirm = irttp_connect_confirm;
153 ttp_notify.connect_indication = irttp_connect_indication;
154 ttp_notify.disconnect_indication = irttp_disconnect_indication;
155 ttp_notify.data_indication = irttp_data_indication;
156 ttp_notify.udata_indication = irttp_udata_indication;
157 ttp_notify.instance = self;
158 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
160 self->magic = TTP_TSAP_MAGIC;
161 self->connected = FALSE;
163 skb_queue_head_init(&self->rx_queue);
164 skb_queue_head_init(&self->tx_queue);
165 skb_queue_head_init(&self->rx_fragments);
167 * Create LSAP at IrLMP layer
169 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify);
170 if (lsap == NULL) {
171 printk(KERN_ERR "IrTTP, Unable to get LSAP!!\n");
172 return NULL;
176 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
177 * will replace it with whatever source selector which is free, so
178 * the stsap_sel we have might not be valid anymore
180 self->stsap_sel = lsap->slsap_sel;
181 DEBUG(4, __FUNCTION__ "(), stsap_sel=%02x\n", self->stsap_sel);
183 self->notify = *notify;
184 self->lsap = lsap;
186 hashbin_insert(irttp->tsaps, (QUEUE *) self, (int) self, NULL);
188 if (credit > TTP_MAX_QUEUE)
189 self->initial_credit = TTP_MAX_QUEUE;
190 else
191 self->initial_credit = credit;
193 return self;
197 * Function irttp_close (handle)
199 * Remove an instance of a TSAP. This function should only deal with the
200 * deallocation of the TSAP, and resetting of the TSAPs values;
203 static void __irttp_close_tsap(struct tsap_cb *self)
205 /* First make sure we're connected. */
206 ASSERT(self != NULL, return;);
207 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
209 irttp_flush_queues(self);
211 del_timer(&self->todo_timer);
213 self->connected = FALSE;
214 self->magic = ~TTP_TSAP_MAGIC;
216 kfree(self);
220 * Function irttp_close (self)
222 * Remove TSAP from list of all TSAPs and then deallocate all resources
223 * associated with this TSAP
226 int irttp_close_tsap(struct tsap_cb *self)
228 struct tsap_cb *tsap;
230 DEBUG(4, __FUNCTION__ "()\n");
232 ASSERT(self != NULL, return -1;);
233 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
235 /* Make sure tsap has been disconnected */
236 if (self->connected) {
237 /* Check if disconnect is not pending */
238 if (!self->disconnect_pend) {
239 DEBUG(0, __FUNCTION__ "(), TSAP still connected!\n");
240 irttp_disconnect_request(self, NULL, P_NORMAL);
242 self->close_pend = TRUE;
243 irttp_start_todo_timer(self, 1*HZ);
245 return 0; /* Will be back! */
248 tsap = hashbin_remove(irttp->tsaps, (int) self, NULL);
250 ASSERT(tsap == self, return -1;);
252 /* Close corresponding LSAP */
253 if (self->lsap) {
254 irlmp_close_lsap(self->lsap);
255 self->lsap = NULL;
258 __irttp_close_tsap(self);
260 return 0;
264 * Function irttp_udata_request (self, skb)
266 * Send unreliable data on this TSAP
269 int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
271 ASSERT(self != NULL, return -1;);
272 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
273 ASSERT(skb != NULL, return -1;);
275 DEBUG(4, __FUNCTION__ "()\n");
277 /* Check that nothing bad happens */
278 if ((skb->len == 0) || (!self->connected)) {
279 DEBUG(1, __FUNCTION__ "(), No data, or not connected\n");
280 return -1;
283 if (skb->len > self->max_seg_size) {
284 DEBUG(1, __FUNCTION__ "(), UData is to large for IrLAP!\n");
285 return -1;
288 irlmp_udata_request(self->lsap, skb);
289 self->stats.tx_packets++;
291 return 0;
295 * Function irttp_data_request (handle, skb)
297 * Queue frame for transmission. If SAR is enabled, fragement the frame
298 * and queue the fragments for transmission
300 int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
302 __u8 *frame;
304 ASSERT(self != NULL, return -1;);
305 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
306 ASSERT(skb != NULL, return -1;);
308 /* Check that nothing bad happens */
309 if ((skb->len == 0) || (!self->connected)) {
310 WARNING(__FUNCTION__ "(), No data, or not connected\n");
311 return -ENOTCONN;
315 * Check if SAR is disabled, and the frame is larger than what fits
316 * inside an IrLAP frame
318 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
319 ERROR(__FUNCTION__
320 "(), SAR disabled, and data is to large for IrLAP!\n");
321 return -EMSGSIZE;
325 * Check if SAR is enabled, and the frame is larger than the
326 * TxMaxSduSize
328 if ((self->tx_max_sdu_size != 0) &&
329 (self->tx_max_sdu_size != SAR_UNBOUND) &&
330 (skb->len > self->tx_max_sdu_size))
332 ERROR(__FUNCTION__ "(), SAR enabled, "
333 "but data is larger than TxMaxSduSize!\n");
334 return -EMSGSIZE;
337 * Check if transmit queue is full
339 if (skb_queue_len(&self->tx_queue) >= TTP_MAX_QUEUE) {
341 * Give it a chance to empty itself
343 irttp_run_tx_queue(self);
345 return -ENOBUFS;
348 /* Queue frame, or queue frame segments */
349 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
350 /* Queue frame */
351 ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
352 frame = skb_push(skb, TTP_HEADER);
353 frame[0] = 0x00; /* Clear more bit */
355 skb_queue_tail(&self->tx_queue, skb);
356 } else {
358 * Fragment the frame, this function will also queue the
359 * fragments, we don't care about the fact the the transmit
360 * queue may be overfilled by all the segments for a little
361 * while
363 irttp_fragment_skb(self, skb);
366 /* Check if we can accept more data from client */
367 if ((!self->tx_sdu_busy) &&
368 (skb_queue_len(&self->tx_queue) > HIGH_THRESHOLD)) {
370 /* Tx queue filling up, so stop client */
371 self->tx_sdu_busy = TRUE;
373 if (self->notify.flow_indication) {
374 self->notify.flow_indication(self->notify.instance,
375 self, FLOW_STOP);
379 /* Try to make some progress */
380 irttp_run_tx_queue(self);
382 return 0;
386 * Function irttp_run_tx_queue (self)
388 * Transmit packets queued for transmission (if possible)
391 static void irttp_run_tx_queue(struct tsap_cb *self)
393 struct sk_buff *skb;
394 unsigned long flags;
395 int n;
397 if (irda_lock(&self->tx_queue_lock) == FALSE)
398 return;
400 /* Try to send out frames as long as we have credits */
401 while ((self->send_credit > 0) &&
402 (skb = skb_dequeue(&self->tx_queue)))
405 * Make sure we don't flood IrLAP with frames just because
406 * the remote device has given us a lot of credits
408 if (irlmp_get_lap_tx_queue_len(self->lsap) > LAP_MAX_QUEUE) {
409 /* Re-queue packet */
410 skb_queue_head(&self->tx_queue, skb);
412 /* Try later. Would be better if IrLAP could notify us */
413 irttp_start_todo_timer(self, MSECS_TO_JIFFIES(10));
415 break;
419 * Since we can transmit and receive frames concurrently,
420 * the code below is a critical region and we must assure that
421 * nobody messes with the credits while we update them.
423 spin_lock_irqsave(&self->lock, flags);
425 n = self->avail_credit;
426 self->avail_credit = 0;
428 /* Only room for 127 credits in frame */
429 if (n > 127) {
430 self->avail_credit = n-127;
431 n = 127;
433 self->remote_credit += n;
434 self->send_credit--;
436 spin_unlock_irqrestore(&self->lock, flags);
439 * More bit must be set by the data_request() or fragment()
440 * functions
442 skb->data[0] |= (n & 0x7f);
444 irlmp_data_request(self->lsap, skb);
445 self->stats.tx_packets++;
447 /* Check if we can accept more frames from client */
448 if ((self->tx_sdu_busy) &&
449 (skb_queue_len(&self->tx_queue) < LOW_THRESHOLD))
451 self->tx_sdu_busy = FALSE;
453 if (self->notify.flow_indication)
454 self->notify.flow_indication(
455 self->notify.instance, self,
456 FLOW_START);
460 /* Reset lock */
461 self->tx_queue_lock = 0;
465 * Function irttp_give_credit (self)
467 * Send a dataless flowdata TTP-PDU and give available credit to peer
468 * TSAP
470 void irttp_give_credit(struct tsap_cb *self)
472 struct sk_buff *tx_skb = NULL;
473 unsigned long flags;
474 int n;
476 ASSERT(self != NULL, return;);
477 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
479 DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n",
480 self->send_credit, self->avail_credit, self->remote_credit);
482 /* Give credit to peer */
483 tx_skb = dev_alloc_skb(64);
484 if (!tx_skb)
485 return;
487 /* Reserve space for LMP, and LAP header */
488 skb_reserve(tx_skb, self->max_header_size);
491 * Since we can transmit and receive frames concurrently,
492 * the code below is a critical region and we must assure that
493 * nobody messes with the credits while we update them.
495 spin_lock_irqsave(&self->lock, flags);
497 n = self->avail_credit;
498 self->avail_credit = 0;
500 /* Only space for 127 credits in frame */
501 if (n > 127) {
502 self->avail_credit = n - 127;
503 n = 127;
505 self->remote_credit += n;
507 spin_unlock_irqrestore(&self->lock, flags);
509 skb_put(tx_skb, 1);
510 tx_skb->data[0] = (__u8) (n & 0x7f);
512 irlmp_data_request(self->lsap, tx_skb);
513 self->stats.tx_packets++;
517 * Function irttp_udata_indication (instance, sap, skb)
519 * Received some unit-data (unreliable)
522 static int irttp_udata_indication(void *instance, void *sap,
523 struct sk_buff *skb)
525 struct tsap_cb *self;
527 DEBUG(4, __FUNCTION__ "()\n");
529 self = (struct tsap_cb *) instance;
531 ASSERT(self != NULL, return -1;);
532 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
533 ASSERT(skb != NULL, return -1;);
535 /* Just pass data to layer above */
536 if (self->notify.udata_indication) {
537 self->notify.udata_indication(self->notify.instance, self, skb);
539 self->stats.rx_packets++;
541 return 0;
545 * Function irttp_data_indication (instance, sap, skb)
547 * Receive segment from IrLMP.
550 static int irttp_data_indication(void *instance, void *sap,
551 struct sk_buff *skb)
553 struct tsap_cb *self;
554 int n;
556 self = (struct tsap_cb *) instance;
558 n = skb->data[0] & 0x7f; /* Extract the credits */
560 self->stats.rx_packets++;
563 * Data or dataless packet? Dataless frames contains only the
564 * TTP_HEADER.
566 if (skb->len > 1) {
567 /* Deal with inbound credit */
568 self->send_credit += n;
569 self->remote_credit--;
572 * We don't remove the TTP header, since we must preserve the
573 * more bit, so the defragment routing knows what to do
575 skb_queue_tail(&self->rx_queue, skb);
576 } else
577 self->send_credit += n; /* Dataless flowdata TTP-PDU */
579 irttp_run_rx_queue(self);
582 * Give avay some credits to peer?
584 if ((skb_queue_empty(&self->tx_queue)) &&
585 (self->remote_credit < LOW_THRESHOLD) &&
586 (self->avail_credit > 0))
588 /* Schedule to start immediately after this thread */
589 irttp_start_todo_timer(self, 0);
590 /*irttp_give_credit(self);*/
594 * If the peer device has given us some credits and we didn't have
595 * anyone from before, the we need to shedule the tx queue?
597 if (self->send_credit == n) {
598 /*irttp_run_tx_queue(self);*/
599 irttp_start_todo_timer(self, 0);
601 return 0;
605 * Function irttp_flow_request (self, command)
607 * This funtion could be used by the upper layers to tell IrTTP to stop
608 * delivering frames if the receive queues are starting to get full, or
609 * to tell IrTTP to start delivering frames again.
611 void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
613 DEBUG(1, __FUNCTION__ "()\n");
615 ASSERT(self != NULL, return;);
616 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
618 switch (flow) {
619 case FLOW_STOP:
620 DEBUG(1, __FUNCTION__ "(), flow stop\n");
621 self->rx_sdu_busy = TRUE;
622 break;
623 case FLOW_START:
624 DEBUG(1, __FUNCTION__ "(), flow start\n");
625 self->rx_sdu_busy = FALSE;
627 irttp_run_rx_queue(self);
628 break;
629 default:
630 DEBUG(1, __FUNCTION__ "(), Unknown flow command!\n");
635 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
637 * Try to connect to remote destination TSAP selector
640 int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
641 __u32 saddr, __u32 daddr,
642 struct qos_info *qos, __u32 max_sdu_size,
643 struct sk_buff *userdata)
645 struct sk_buff *skb;
646 __u8 *frame;
647 __u8 n;
649 DEBUG(4, __FUNCTION__ "(), max_sdu_size=%d\n", max_sdu_size);
651 ASSERT(self != NULL, return -EBADR;);
652 ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
654 if (self->connected)
655 return -EISCONN;
657 /* Any userdata supplied? */
658 if (userdata == NULL) {
659 skb = dev_alloc_skb(64);
660 if (!skb)
661 return -ENOMEM;
663 /* Reserve space for MUX_CONTROL and LAP header */
664 skb_reserve(skb, TTP_MAX_HEADER);
665 } else {
666 skb = userdata;
668 * Check that the client has reserved enough space for
669 * headers
671 ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER, return -1;);
674 /* Initialize connection parameters */
675 self->connected = FALSE;
676 self->avail_credit = 0;
677 self->rx_max_sdu_size = max_sdu_size;
678 self->rx_sdu_size = 0;
679 self->rx_sdu_busy = FALSE;
680 self->dtsap_sel = dtsap_sel;
682 n = self->initial_credit;
684 self->remote_credit = 0;
685 self->send_credit = 0;
688 * Give away max 127 credits for now
690 if (n > 127) {
691 self->avail_credit=n-127;
692 n = 127;
695 self->remote_credit = n;
697 /* SAR enabled? */
698 if (max_sdu_size > 0) {
699 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
700 return -1;);
702 /* Insert SAR parameters */
703 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
705 frame[0] = TTP_PARAMETERS | n;
706 frame[1] = 0x04; /* Length */
707 frame[2] = 0x01; /* MaxSduSize */
708 frame[3] = 0x02; /* Value length */
710 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
711 (__u16 *)(frame+4));
712 } else {
713 /* Insert plain TTP header */
714 frame = skb_push(skb, TTP_HEADER);
716 /* Insert initial credit in frame */
717 frame[0] = n & 0x7f;
720 /* Connect with IrLMP. No QoS parameters for now */
721 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
722 skb);
726 * Function irttp_connect_confirm (handle, qos, skb)
728 * Sevice user confirms TSAP connection with peer.
731 static void irttp_connect_confirm(void *instance, void *sap,
732 struct qos_info *qos,
733 __u32 max_seg_size, __u8 max_header_size,
734 struct sk_buff *skb)
736 struct tsap_cb *self;
737 int parameters;
738 int ret;
739 __u8 plen;
740 __u8 n;
742 DEBUG(4, __FUNCTION__ "()\n");
744 self = (struct tsap_cb *) instance;
746 ASSERT(self != NULL, return;);
747 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
748 ASSERT(skb != NULL, return;);
750 self->max_seg_size = max_seg_size;
751 self->max_header_size = max_header_size + TTP_HEADER;
754 * Check if we have got some QoS parameters back! This should be the
755 * negotiated QoS for the link.
757 if (qos) {
758 DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
759 qos->baud_rate.bits);
760 DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
761 qos->baud_rate.value);
764 n = skb->data[0] & 0x7f;
766 DEBUG(4, __FUNCTION__ "(), Initial send_credit=%d\n", n);
768 self->send_credit = n;
769 self->tx_max_sdu_size = 0;
770 self->connected = TRUE;
772 parameters = skb->data[0] & 0x80;
774 ASSERT(skb->len >= TTP_HEADER, return;);
775 skb_pull(skb, TTP_HEADER);
777 if (parameters) {
778 plen = skb->data[0];
780 ret = irda_param_extract_all(self, skb->data+1,
781 MIN(skb->len-1, plen),
782 &param_info);
784 /* Any errors in the parameter list? */
785 if (ret < 0) {
786 WARNING(__FUNCTION__
787 "(), error extracting parameters\n");
788 dev_kfree_skb(skb);
790 /* Do not accept this connection attempt */
791 return;
793 /* Remove parameters */
794 skb_pull(skb, MIN(skb->len, plen+1));
797 DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n",
798 self->send_credit, self->avail_credit, self->remote_credit);
800 DEBUG(0, __FUNCTION__ "(), MaxSduSize=%d\n", self->tx_max_sdu_size);
802 if (self->notify.connect_confirm) {
803 self->notify.connect_confirm(self->notify.instance, self, qos,
804 self->tx_max_sdu_size,
805 self->max_header_size, skb);
810 * Function irttp_connect_indication (handle, skb)
812 * Some other device is connecting to this TSAP
815 void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
816 __u32 max_seg_size, __u8 max_header_size,
817 struct sk_buff *skb)
819 struct tsap_cb *self;
820 struct lsap_cb *lsap;
821 int parameters;
822 int ret;
823 __u8 plen;
824 __u8 n;
826 self = (struct tsap_cb *) instance;
828 ASSERT(self != NULL, return;);
829 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
830 ASSERT(skb != NULL, return;);
832 lsap = (struct lsap_cb *) sap;
834 self->max_seg_size = max_seg_size;
835 self->max_header_size = max_header_size+TTP_HEADER;
837 DEBUG(4, __FUNCTION__ "(), TSAP sel=%02x\n", self->stsap_sel);
839 /* Need to update dtsap_sel if its equal to LSAP_ANY */
840 self->dtsap_sel = lsap->dlsap_sel;
842 n = skb->data[0] & 0x7f;
844 self->send_credit = n;
845 self->tx_max_sdu_size = 0;
847 parameters = skb->data[0] & 0x80;
849 ASSERT(skb->len >= TTP_HEADER, return;);
850 skb_pull(skb, TTP_HEADER);
852 if (parameters) {
853 plen = skb->data[0];
855 ret = irda_param_extract_all(self, skb->data+1,
856 MIN(skb->len-1, plen),
857 &param_info);
859 /* Any errors in the parameter list? */
860 if (ret < 0) {
861 WARNING(__FUNCTION__
862 "(), error extracting parameters\n");
863 dev_kfree_skb(skb);
865 /* Do not accept this connection attempt */
866 return;
869 /* Remove parameters */
870 skb_pull(skb, MIN(skb->len, plen+1));
873 DEBUG(4, __FUNCTION__ "(), initial send_credit=%d\n", n);
875 if (self->notify.connect_indication) {
876 self->notify.connect_indication(self->notify.instance, self,
877 qos, self->tx_max_sdu_size,
878 self->max_header_size, skb);
883 * Function irttp_connect_response (handle, userdata)
885 * Service user is accepting the connection, just pass it down to
886 * IrLMP!
889 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
890 struct sk_buff *userdata)
892 struct sk_buff *skb;
893 __u8 *frame;
894 int ret;
895 __u8 n;
897 ASSERT(self != NULL, return -1;);
898 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
900 DEBUG(4, __FUNCTION__ "(), Source TSAP selector=%02x\n",
901 self->stsap_sel);
903 /* Any userdata supplied? */
904 if (userdata == NULL) {
905 skb = dev_alloc_skb(64);
906 if (!skb)
907 return -ENOMEM;
909 /* Reserve space for MUX_CONTROL and LAP header */
910 skb_reserve(skb, TTP_MAX_HEADER);
911 } else {
912 skb = userdata;
914 * Check that the client has reserved enough space for
915 * headers
917 ASSERT(skb_headroom(skb) >= TTP_MAX_HEADER, return -1;);
920 self->avail_credit = 0;
921 self->remote_credit = 0;
922 self->rx_max_sdu_size = max_sdu_size;
923 self->rx_sdu_size = 0;
924 self->rx_sdu_busy = FALSE;
926 n = self->initial_credit;
928 /* Frame has only space for max 127 credits (7 bits) */
929 if (n > 127) {
930 self->avail_credit = n - 127;
931 n = 127;
934 self->remote_credit = n;
935 self->connected = TRUE;
937 /* SAR enabled? */
938 if (max_sdu_size > 0) {
939 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER+TTP_SAR_HEADER),
940 return -1;);
942 /* Insert TTP header with SAR parameters */
943 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
945 frame[0] = TTP_PARAMETERS | n;
946 frame[1] = 0x04; /* Length */
948 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
949 /* TTP_SAR_HEADER, &param_info) */
951 frame[2] = 0x01; /* MaxSduSize */
952 frame[3] = 0x02; /* Value length */
954 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
955 (__u16 *)(frame+4));
956 } else {
957 /* Insert TTP header */
958 frame = skb_push(skb, TTP_HEADER);
960 frame[0] = n & 0x7f;
963 ret = irlmp_connect_response(self->lsap, skb);
965 return ret;
969 * Function irttp_dup (self, instance)
971 * Duplicate TSAP, can be used by servers to confirm a connection on a
972 * new TSAP so it can keep listening on the old one.
974 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
976 struct tsap_cb *new;
978 DEBUG(1, __FUNCTION__ "()\n");
980 if (!hashbin_find(irttp->tsaps, (int) orig, NULL)) {
981 DEBUG(0, __FUNCTION__ "(), unable to find TSAP\n");
982 return NULL;
984 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
985 if (!new) {
986 DEBUG(0, __FUNCTION__ "(), unable to kmalloc\n");
987 return NULL;
989 /* Dup */
990 memcpy(new, orig, sizeof(struct tsap_cb));
991 new->notify.instance = instance;
992 new->lsap = irlmp_dup(orig->lsap, new);
994 /* Not everything should be copied */
995 init_timer(&new->todo_timer);
997 skb_queue_head_init(&new->rx_queue);
998 skb_queue_head_init(&new->tx_queue);
999 skb_queue_head_init(&new->rx_fragments);
1001 hashbin_insert(irttp->tsaps, (QUEUE *) new, (int) new, NULL);
1003 return new;
1007 * Function irttp_disconnect_request (self)
1009 * Close this connection please! If priority is high, the queued data
1010 * segments, if any, will be deallocated first
1013 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1014 int priority)
1016 struct sk_buff *skb;
1017 int ret;
1019 ASSERT(self != NULL, return -1;);
1020 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1022 /* Already disconnected? */
1023 if (!self->connected) {
1024 DEBUG(4, __FUNCTION__ "(), already disconnected!\n");
1025 return -1;
1028 /* Disconnect already pending? */
1029 if (self->disconnect_pend) {
1030 DEBUG(1, __FUNCTION__ "(), disconnect already pending\n");
1031 if (userdata) {
1032 dev_kfree_skb(userdata);
1035 /* Try to make some progress */
1036 irttp_run_rx_queue(self);
1037 return -1;
1041 * Check if there is still data segments in the transmit queue
1043 if (skb_queue_len(&self->tx_queue) > 0) {
1044 if (priority == P_HIGH) {
1045 DEBUG(1, __FUNCTION__ "High priority!!()\n" );
1048 * No need to send the queued data, if we are
1049 * disconnecting right now since the data will
1050 * not have any usable connection to be sent on
1052 irttp_flush_queues(self);
1053 } else if (priority == P_NORMAL) {
1055 * Must delay disconnect til after all data segments
1056 * have been sent an the tx_queue is empty
1058 if (userdata)
1059 self->disconnect_skb = userdata;
1060 else
1061 self->disconnect_skb = NULL;
1063 self->disconnect_pend = TRUE;
1065 irttp_run_tx_queue(self);
1067 irttp_start_todo_timer(self, MSECS_TO_JIFFIES(1000));
1068 return -1;
1071 DEBUG(1, __FUNCTION__ "(), Disconnecting ...\n");
1073 self->connected = FALSE;
1075 if (!userdata) {
1076 skb = dev_alloc_skb(64);
1077 if (!skb)
1078 return -ENOMEM;
1081 * Reserve space for MUX and LAP header
1083 skb_reserve(skb, TTP_MAX_HEADER);
1085 userdata = skb;
1087 ret = irlmp_disconnect_request(self->lsap, userdata);
1089 return ret;
1093 * Function irttp_disconnect_indication (self, reason)
1095 * Disconnect indication, TSAP disconnected by peer?
1098 void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason,
1099 struct sk_buff *userdata)
1101 struct tsap_cb *self;
1103 DEBUG(4, __FUNCTION__ "()\n");
1105 self = (struct tsap_cb *) instance;
1107 ASSERT(self != NULL, return;);
1108 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1110 self->connected = FALSE;
1112 if (!self->notify.disconnect_indication)
1113 return;
1115 self->notify.disconnect_indication(self->notify.instance, self, reason,
1116 userdata);
1120 * Function irttp_do_data_indication (self, skb)
1122 * Try to deliver reassebled skb to layer above, and requeue it if that
1123 * for some reason should fail. We mark rx sdu as busy to apply back
1124 * pressure is necessary.
1126 void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1128 int err;
1130 err = self->notify.data_indication(self->notify.instance, self, skb);
1132 /* Usually the layer above will notify that it's input queue is
1133 * starting to get filled by using the flow request, but this may
1134 * be difficult, so it can instead just refuse to eat it and just
1135 * give an error back
1137 if (err == -ENOMEM) {
1138 DEBUG(0, __FUNCTION__ "() requeueing skb!\n");
1140 /* Make sure we take a break */
1141 self->rx_sdu_busy = TRUE;
1143 /* Need to push the header in again */
1144 skb_push(skb, TTP_HEADER);
1145 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1147 /* Put skb back on queue */
1148 skb_queue_head(&self->rx_queue, skb);
1153 * Function irttp_run_rx_queue (self)
1155 * Check if we have any frames to be transmitted, or if we have any
1156 * available credit to give away.
1158 void irttp_run_rx_queue(struct tsap_cb *self)
1160 struct sk_buff *skb;
1161 int more = 0;
1163 DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n",
1164 self->send_credit, self->avail_credit, self->remote_credit);
1166 if (irda_lock(&self->rx_queue_lock) == FALSE)
1167 return;
1170 * Reassemble all frames in receive queue and deliver them
1172 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1173 self->avail_credit++;
1175 more = skb->data[0] & 0x80;
1177 /* Remove TTP header */
1178 skb_pull(skb, TTP_HEADER);
1180 /* Add the length of the remaining data */
1181 self->rx_sdu_size += skb->len;
1184 * If SAR is disabled, or user has requested no reassembly
1185 * of received fragements then we just deliver them
1186 * immediately. This can be requested by clients that
1187 * implements byte streams without any message boundaries
1189 if (self->rx_max_sdu_size == SAR_DISABLE) {
1190 irttp_do_data_indication(self, skb);
1191 self->rx_sdu_size = 0;
1193 continue;
1196 /* Check if this is a fragment, and not the last fragment */
1197 if (more) {
1199 * Queue the fragment if we still are within the
1200 * limits of the maximum size of the rx_sdu
1202 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1203 DEBUG(4, __FUNCTION__ "(), queueing frag\n");
1204 skb_queue_tail(&self->rx_fragments, skb);
1205 } else {
1206 /* Free the part of the SDU that is too big */
1207 dev_kfree_skb(skb);
1209 continue;
1212 * This is the last fragment, so time to reassemble!
1214 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1215 (self->rx_max_sdu_size == SAR_UNBOUND))
1218 * A little optimizing. Only queue the fragment if
1219 * there are other fragments. Since if this is the
1220 * last and only fragment, there is no need to
1221 * reassemble :-)
1223 if (!skb_queue_empty(&self->rx_fragments)) {
1224 skb_queue_tail(&self->rx_fragments,
1225 skb);
1227 skb = irttp_reassemble_skb(self);
1230 /* Now we can deliver the reassembled skb */
1231 irttp_do_data_indication(self, skb);
1232 } else {
1233 DEBUG(1, __FUNCTION__ "(), Truncated frame\n");
1235 /* Free the part of the SDU that is too big */
1236 dev_kfree_skb(skb);
1238 /* Deliver only the valid but truncated part of SDU */
1239 skb = irttp_reassemble_skb(self);
1241 irttp_do_data_indication(self, skb);
1243 self->rx_sdu_size = 0;
1245 /* Reset lock */
1246 self->rx_queue_lock = 0;
1250 * Function irttp_flush_queues (self)
1252 * Flushes (removes all frames) in transitt-buffer (tx_list)
1254 void irttp_flush_queues(struct tsap_cb *self)
1256 struct sk_buff* skb;
1258 DEBUG(4, __FUNCTION__ "()\n");
1260 ASSERT(self != NULL, return;);
1261 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1263 /* Deallocate frames waiting to be sent */
1264 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
1265 dev_kfree_skb(skb);
1267 /* Deallocate received frames */
1268 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
1269 dev_kfree_skb(skb);
1271 /* Deallocate received fragments */
1272 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
1273 dev_kfree_skb(skb);
1277 * Function irttp_reasseble (self)
1279 * Makes a new (continuous) skb of all the fragments in the fragment
1280 * queue
1283 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
1285 struct sk_buff *skb, *frag;
1286 int n = 0; /* Fragment index */
1288 ASSERT(self != NULL, return NULL;);
1289 ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
1291 DEBUG(4, __FUNCTION__ "(), self->rx_sdu_size=%d\n",
1292 self->rx_sdu_size);
1294 skb = dev_alloc_skb(self->rx_sdu_size);
1295 if (!skb)
1296 return NULL;
1299 * Need to reserve space for TTP header in case this skb needs to
1300 * be requeued in case delivery failes
1302 skb_reserve(skb, TTP_HEADER);
1303 skb_put(skb, self->rx_sdu_size);
1306 * Copy all fragments to a new buffer
1308 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
1309 memcpy(skb->data+n, frag->data, frag->len);
1310 n += frag->len;
1312 dev_kfree_skb(frag);
1314 DEBUG(4, __FUNCTION__ "(), frame len=%d\n", n);
1315 /* Set the new length */
1317 DEBUG(4, __FUNCTION__ "(), rx_sdu_size=%d\n", self->rx_sdu_size);
1318 ASSERT(n <= self->rx_sdu_size, return NULL;);
1319 skb_trim(skb, n);
1321 self->rx_sdu_size = 0;
1323 return skb;
1327 * Function irttp_fragment_skb (skb)
1329 * Fragments a frame and queues all the fragments for transmission
1332 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb)
1334 struct sk_buff *frag;
1335 __u8 *frame;
1337 DEBUG(4, __FUNCTION__ "()\n");
1339 ASSERT(self != NULL, return;);
1340 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1341 ASSERT(skb != NULL, return;);
1344 * Split frame into a number of segments
1346 while (skb->len > 0) {
1348 * Instead of making the last segment, we just
1349 * queue what is left of the original skb
1351 if (skb->len < self->max_seg_size) {
1352 DEBUG(4, __FUNCTION__
1353 "(), queuing last segment\n");
1355 frame = skb_push(skb, TTP_HEADER);
1356 frame[0] = 0x00; /* Clear more bit */
1357 skb_queue_tail(&self->tx_queue, skb);
1359 return;
1362 /* Make new segment */
1363 frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
1364 if (!frag)
1365 return;
1367 skb_reserve(frag, self->max_header_size);
1370 * Copy data from the original skb into this fragment. We
1371 * first insert the TTP header with the more bit set
1373 frame = skb_put(frag, self->max_seg_size+TTP_HEADER);
1374 frame[0] = TTP_MORE;
1375 memcpy(frag->data+1, skb->data, self->max_seg_size);
1377 /* Hide the copied data from the original skb */
1378 skb_pull(skb, self->max_seg_size);
1380 skb_queue_tail(&self->tx_queue, frag);
1385 * Function irttp_param_max_sdu_size (self, param)
1387 * Handle the MaxSduSize parameter in the connect frames, this function
1388 * will be called both when this parameter needs to be inserted into, and
1389 * extracted from the connect frames
1391 static int irttp_param_max_sdu_size(void *instance, param_t *param, int get)
1393 struct tsap_cb *self;
1395 self = (struct tsap_cb *) instance;
1397 ASSERT(self != NULL, return -1;);
1398 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1400 if (get)
1401 param->pv.i = self->tx_max_sdu_size;
1402 else
1403 self->tx_max_sdu_size = param->pv.i;
1405 DEBUG(0, __FUNCTION__ "(), MaxSduSize=%d\n", param->pv.i);
1407 return 0;
1411 * Function irttp_todo_expired (data)
1413 * Todo timer has expired!
1416 static void irttp_todo_expired(unsigned long data)
1418 struct tsap_cb *self = (struct tsap_cb *) data;
1420 /* Check that we still exist */
1421 if (!self || self->magic != TTP_TSAP_MAGIC)
1422 return;
1424 irttp_run_rx_queue(self);
1425 irttp_run_tx_queue(self);
1427 /* Give avay some credits to peer? */
1428 if ((self->remote_credit < LOW_THRESHOLD) &&
1429 (self->avail_credit > 0) && (skb_queue_empty(&self->tx_queue)))
1431 irttp_give_credit(self);
1434 /* Check if time for disconnect */
1435 if (self->disconnect_pend) {
1436 /* Check if it's possible to disconnect yet */
1437 if (skb_queue_empty(&self->tx_queue)) {
1439 /* Make sure disconnect is not pending anymore */
1440 self->disconnect_pend = FALSE;
1441 if (self->disconnect_skb) {
1442 irttp_disconnect_request(
1443 self, self->disconnect_skb, P_NORMAL);
1444 self->disconnect_skb = NULL;
1445 } else
1446 irttp_disconnect_request(self, NULL, P_NORMAL);
1447 } else {
1448 /* Try again later */
1449 irttp_start_todo_timer(self, 1*HZ);
1451 /* No reason to try and close now */
1452 return;
1456 /* Check if it's closing time */
1457 if (self->close_pend)
1458 irttp_close_tsap(self);
1462 * Function irttp_start_todo_timer (self, timeout)
1464 * Start todo timer.
1467 static void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
1469 ASSERT(self != NULL, return;);
1470 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1472 del_timer(&self->todo_timer);
1474 self->todo_timer.data = (unsigned long) self;
1475 self->todo_timer.function = &irttp_todo_expired;
1476 self->todo_timer.expires = jiffies + timeout;
1478 add_timer(&self->todo_timer);
1481 #ifdef CONFIG_PROC_FS
1483 * Function irttp_proc_read (buf, start, offset, len, unused)
1485 * Give some info to the /proc file system
1487 int irttp_proc_read(char *buf, char **start, off_t offset, int len, int unused)
1489 struct tsap_cb *self;
1490 unsigned long flags;
1491 int i = 0;
1493 ASSERT(irttp != NULL, return 0;);
1495 len = 0;
1497 save_flags(flags);
1498 cli();
1500 self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1501 while (self != NULL) {
1502 if (!self || self->magic != TTP_TSAP_MAGIC)
1503 return len;
1505 len += sprintf(buf+len, "TSAP %d, ", i++);
1506 len += sprintf(buf+len, "stsap_sel: %02x, ",
1507 self->stsap_sel);
1508 len += sprintf(buf+len, "dtsap_sel: %02x\n",
1509 self->dtsap_sel);
1510 len += sprintf(buf+len, " connected: %s, ",
1511 self->connected? "TRUE":"FALSE");
1512 len += sprintf(buf+len, "avail credit: %d, ",
1513 self->avail_credit);
1514 len += sprintf(buf+len, "remote credit: %d, ",
1515 self->remote_credit);
1516 len += sprintf(buf+len, "send credit: %d\n",
1517 self->send_credit);
1518 len += sprintf(buf+len, " tx packets: %d, ",
1519 self->stats.tx_packets);
1520 len += sprintf(buf+len, "rx packets: %d, ",
1521 self->stats.rx_packets);
1522 len += sprintf(buf+len, "tx_queue len: %d ",
1523 skb_queue_len(&self->tx_queue));
1524 len += sprintf(buf+len, "rx_queue len: %d\n",
1525 skb_queue_len(&self->rx_queue));
1526 len += sprintf(buf+len, " tx_sdu_busy: %s, ",
1527 self->tx_sdu_busy? "TRUE":"FALSE");
1528 len += sprintf(buf+len, "rx_sdu_busy: %s\n",
1529 self->rx_sdu_busy? "TRUE":"FALSE");
1530 len += sprintf(buf+len, " max_seg_size: %d, ",
1531 self->max_seg_size);
1532 len += sprintf(buf+len, "tx_max_sdu_size: %d, ",
1533 self->tx_max_sdu_size);
1534 len += sprintf(buf+len, "rx_max_sdu_size: %d\n",
1535 self->rx_max_sdu_size);
1537 len += sprintf(buf+len, " Used by (%s)\n",
1538 self->notify.name);
1540 len += sprintf(buf+len, "\n");
1542 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps);
1544 restore_flags(flags);
1546 return len;
1549 #endif /* PROC_FS */