Import 2.3.25pre1
[davej-history.git] / net / irda / irttp.c
blob304f39e1075638d6cf9c9e82ff938cb8be4726d2
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: Tue Oct 19 21:40:00 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 ERROR(__FUNCTION__ "(), 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 IRDA_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 WARNING(__FUNCTION__ "(), unable to allocate 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 IRDA_DEBUG(4, __FUNCTION__ "(), stsap_sel=%02x\n", self->stsap_sel);
183 self->notify = *notify;
184 self->lsap = lsap;
186 hashbin_insert(irttp->tsaps, (queue_t *) 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 IRDA_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 IRDA_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 IRDA_DEBUG(4, __FUNCTION__ "()\n");
277 /* Check that nothing bad happens */
278 if ((skb->len == 0) || (!self->connected)) {
279 IRDA_DEBUG(1, __FUNCTION__ "(), No data, or not connected\n");
280 return -1;
283 if (skb->len > self->max_seg_size) {
284 IRDA_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 IRDA_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 IRDA_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 IRDA_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 IRDA_DEBUG(1, __FUNCTION__ "(), flow stop\n");
621 self->rx_sdu_busy = TRUE;
622 break;
623 case FLOW_START:
624 IRDA_DEBUG(1, __FUNCTION__ "(), flow start\n");
625 self->rx_sdu_busy = FALSE;
627 irttp_run_rx_queue(self);
628 break;
629 default:
630 IRDA_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 IRDA_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, __u32 max_seg_size,
733 __u8 max_header_size, struct sk_buff *skb)
735 struct tsap_cb *self;
736 int parameters;
737 int ret;
738 __u8 plen;
739 __u8 n;
741 IRDA_DEBUG(4, __FUNCTION__ "()\n");
743 self = (struct tsap_cb *) instance;
745 ASSERT(self != NULL, return;);
746 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
747 ASSERT(skb != NULL, return;);
749 self->max_seg_size = max_seg_size - TTP_HEADER;
750 self->max_header_size = max_header_size + TTP_HEADER;
753 * Check if we have got some QoS parameters back! This should be the
754 * negotiated QoS for the link.
756 if (qos) {
757 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
758 qos->baud_rate.bits);
759 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
760 qos->baud_rate.value);
763 n = skb->data[0] & 0x7f;
765 IRDA_DEBUG(4, __FUNCTION__ "(), Initial send_credit=%d\n", n);
767 self->send_credit = n;
768 self->tx_max_sdu_size = 0;
769 self->connected = TRUE;
771 parameters = skb->data[0] & 0x80;
773 ASSERT(skb->len >= TTP_HEADER, return;);
774 skb_pull(skb, TTP_HEADER);
776 if (parameters) {
777 plen = skb->data[0];
779 ret = irda_param_extract_all(self, skb->data+1,
780 IRDA_MIN(skb->len-1, plen),
781 &param_info);
783 /* Any errors in the parameter list? */
784 if (ret < 0) {
785 WARNING(__FUNCTION__
786 "(), error extracting parameters\n");
787 dev_kfree_skb(skb);
789 /* Do not accept this connection attempt */
790 return;
792 /* Remove parameters */
793 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
796 IRDA_DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n",
797 self->send_credit, self->avail_credit, self->remote_credit);
799 IRDA_DEBUG(2, __FUNCTION__ "(), MaxSduSize=%d\n", self->tx_max_sdu_size);
801 if (self->notify.connect_confirm) {
802 self->notify.connect_confirm(self->notify.instance, self, qos,
803 self->tx_max_sdu_size,
804 self->max_header_size, skb);
809 * Function irttp_connect_indication (handle, skb)
811 * Some other device is connecting to this TSAP
814 void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
815 __u32 max_seg_size, __u8 max_header_size,
816 struct sk_buff *skb)
818 struct tsap_cb *self;
819 struct lsap_cb *lsap;
820 int parameters;
821 int ret;
822 __u8 plen;
823 __u8 n;
825 self = (struct tsap_cb *) instance;
827 ASSERT(self != NULL, return;);
828 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
829 ASSERT(skb != NULL, return;);
831 lsap = (struct lsap_cb *) sap;
833 self->max_seg_size = max_seg_size;
834 self->max_header_size = max_header_size+TTP_HEADER;
836 IRDA_DEBUG(4, __FUNCTION__ "(), TSAP sel=%02x\n", self->stsap_sel);
838 /* Need to update dtsap_sel if its equal to LSAP_ANY */
839 self->dtsap_sel = lsap->dlsap_sel;
841 n = skb->data[0] & 0x7f;
843 self->send_credit = n;
844 self->tx_max_sdu_size = 0;
846 parameters = skb->data[0] & 0x80;
848 ASSERT(skb->len >= TTP_HEADER, return;);
849 skb_pull(skb, TTP_HEADER);
851 if (parameters) {
852 plen = skb->data[0];
854 ret = irda_param_extract_all(self, skb->data+1,
855 IRDA_MIN(skb->len-1, plen),
856 &param_info);
858 /* Any errors in the parameter list? */
859 if (ret < 0) {
860 WARNING(__FUNCTION__
861 "(), error extracting parameters\n");
862 dev_kfree_skb(skb);
864 /* Do not accept this connection attempt */
865 return;
868 /* Remove parameters */
869 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
872 if (self->notify.connect_indication) {
873 self->notify.connect_indication(self->notify.instance, self,
874 qos, self->tx_max_sdu_size,
875 self->max_header_size, skb);
880 * Function irttp_connect_response (handle, userdata)
882 * Service user is accepting the connection, just pass it down to
883 * IrLMP!
886 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
887 struct sk_buff *userdata)
889 struct sk_buff *skb;
890 __u8 *frame;
891 int ret;
892 __u8 n;
894 ASSERT(self != NULL, return -1;);
895 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
897 IRDA_DEBUG(4, __FUNCTION__ "(), Source TSAP selector=%02x\n",
898 self->stsap_sel);
900 /* Any userdata supplied? */
901 if (userdata == NULL) {
902 skb = dev_alloc_skb(64);
903 if (!skb)
904 return -ENOMEM;
906 /* Reserve space for MUX_CONTROL and LAP header */
907 skb_reserve(skb, TTP_MAX_HEADER);
908 } else {
909 skb = userdata;
911 * Check that the client has reserved enough space for
912 * headers
914 ASSERT(skb_headroom(skb) >= TTP_MAX_HEADER, return -1;);
917 self->avail_credit = 0;
918 self->remote_credit = 0;
919 self->rx_max_sdu_size = max_sdu_size;
920 self->rx_sdu_size = 0;
921 self->rx_sdu_busy = FALSE;
923 n = self->initial_credit;
925 /* Frame has only space for max 127 credits (7 bits) */
926 if (n > 127) {
927 self->avail_credit = n - 127;
928 n = 127;
931 self->remote_credit = n;
932 self->connected = TRUE;
934 /* SAR enabled? */
935 if (max_sdu_size > 0) {
936 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER+TTP_SAR_HEADER),
937 return -1;);
939 /* Insert TTP header with SAR parameters */
940 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
942 frame[0] = TTP_PARAMETERS | n;
943 frame[1] = 0x04; /* Length */
945 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
946 /* TTP_SAR_HEADER, &param_info) */
948 frame[2] = 0x01; /* MaxSduSize */
949 frame[3] = 0x02; /* Value length */
951 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
952 (__u16 *)(frame+4));
953 } else {
954 /* Insert TTP header */
955 frame = skb_push(skb, TTP_HEADER);
957 frame[0] = n & 0x7f;
960 ret = irlmp_connect_response(self->lsap, skb);
962 return ret;
966 * Function irttp_dup (self, instance)
968 * Duplicate TSAP, can be used by servers to confirm a connection on a
969 * new TSAP so it can keep listening on the old one.
971 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
973 struct tsap_cb *new;
975 IRDA_DEBUG(1, __FUNCTION__ "()\n");
977 if (!hashbin_find(irttp->tsaps, (int) orig, NULL)) {
978 IRDA_DEBUG(0, __FUNCTION__ "(), unable to find TSAP\n");
979 return NULL;
981 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
982 if (!new) {
983 IRDA_DEBUG(0, __FUNCTION__ "(), unable to kmalloc\n");
984 return NULL;
986 /* Dup */
987 memcpy(new, orig, sizeof(struct tsap_cb));
988 new->notify.instance = instance;
989 new->lsap = irlmp_dup(orig->lsap, new);
991 /* Not everything should be copied */
992 init_timer(&new->todo_timer);
994 skb_queue_head_init(&new->rx_queue);
995 skb_queue_head_init(&new->tx_queue);
996 skb_queue_head_init(&new->rx_fragments);
998 hashbin_insert(irttp->tsaps, (queue_t *) new, (int) new, NULL);
1000 return new;
1004 * Function irttp_disconnect_request (self)
1006 * Close this connection please! If priority is high, the queued data
1007 * segments, if any, will be deallocated first
1010 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1011 int priority)
1013 struct sk_buff *skb;
1014 int ret;
1016 ASSERT(self != NULL, return -1;);
1017 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1019 /* Already disconnected? */
1020 if (!self->connected) {
1021 IRDA_DEBUG(4, __FUNCTION__ "(), already disconnected!\n");
1022 return -1;
1025 /* Disconnect already pending? */
1026 if (self->disconnect_pend) {
1027 IRDA_DEBUG(1, __FUNCTION__ "(), disconnect already pending\n");
1028 if (userdata) {
1029 dev_kfree_skb(userdata);
1032 /* Try to make some progress */
1033 irttp_run_rx_queue(self);
1034 return -1;
1038 * Check if there is still data segments in the transmit queue
1040 if (skb_queue_len(&self->tx_queue) > 0) {
1041 if (priority == P_HIGH) {
1042 IRDA_DEBUG(1, __FUNCTION__ "High priority!!()\n" );
1045 * No need to send the queued data, if we are
1046 * disconnecting right now since the data will
1047 * not have any usable connection to be sent on
1049 irttp_flush_queues(self);
1050 } else if (priority == P_NORMAL) {
1052 * Must delay disconnect til after all data segments
1053 * have been sent an the tx_queue is empty
1055 if (userdata)
1056 self->disconnect_skb = userdata;
1057 else
1058 self->disconnect_skb = NULL;
1060 self->disconnect_pend = TRUE;
1062 irttp_run_tx_queue(self);
1064 irttp_start_todo_timer(self, MSECS_TO_JIFFIES(1000));
1065 return -1;
1068 IRDA_DEBUG(1, __FUNCTION__ "(), Disconnecting ...\n");
1070 self->connected = FALSE;
1072 if (!userdata) {
1073 skb = dev_alloc_skb(64);
1074 if (!skb)
1075 return -ENOMEM;
1078 * Reserve space for MUX and LAP header
1080 skb_reserve(skb, TTP_MAX_HEADER);
1082 userdata = skb;
1084 ret = irlmp_disconnect_request(self->lsap, userdata);
1086 return ret;
1090 * Function irttp_disconnect_indication (self, reason)
1092 * Disconnect indication, TSAP disconnected by peer?
1095 void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason,
1096 struct sk_buff *userdata)
1098 struct tsap_cb *self;
1100 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1102 self = (struct tsap_cb *) instance;
1104 ASSERT(self != NULL, return;);
1105 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1107 self->connected = FALSE;
1109 if (!self->notify.disconnect_indication)
1110 return;
1112 self->notify.disconnect_indication(self->notify.instance, self, reason,
1113 userdata);
1117 * Function irttp_do_data_indication (self, skb)
1119 * Try to deliver reassebled skb to layer above, and requeue it if that
1120 * for some reason should fail. We mark rx sdu as busy to apply back
1121 * pressure is necessary.
1123 void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1125 int err;
1127 err = self->notify.data_indication(self->notify.instance, self, skb);
1129 /* Usually the layer above will notify that it's input queue is
1130 * starting to get filled by using the flow request, but this may
1131 * be difficult, so it can instead just refuse to eat it and just
1132 * give an error back
1134 if (err == -ENOMEM) {
1135 IRDA_DEBUG(0, __FUNCTION__ "() requeueing skb!\n");
1137 /* Make sure we take a break */
1138 self->rx_sdu_busy = TRUE;
1140 /* Need to push the header in again */
1141 skb_push(skb, TTP_HEADER);
1142 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1144 /* Put skb back on queue */
1145 skb_queue_head(&self->rx_queue, skb);
1150 * Function irttp_run_rx_queue (self)
1152 * Check if we have any frames to be transmitted, or if we have any
1153 * available credit to give away.
1155 void irttp_run_rx_queue(struct tsap_cb *self)
1157 struct sk_buff *skb;
1158 int more = 0;
1160 IRDA_DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n",
1161 self->send_credit, self->avail_credit, self->remote_credit);
1163 if (irda_lock(&self->rx_queue_lock) == FALSE)
1164 return;
1167 * Reassemble all frames in receive queue and deliver them
1169 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1170 self->avail_credit++;
1172 more = skb->data[0] & 0x80;
1174 /* Remove TTP header */
1175 skb_pull(skb, TTP_HEADER);
1177 /* Add the length of the remaining data */
1178 self->rx_sdu_size += skb->len;
1181 * If SAR is disabled, or user has requested no reassembly
1182 * of received fragements then we just deliver them
1183 * immediately. This can be requested by clients that
1184 * implements byte streams without any message boundaries
1186 if (self->rx_max_sdu_size == SAR_DISABLE) {
1187 irttp_do_data_indication(self, skb);
1188 self->rx_sdu_size = 0;
1190 continue;
1193 /* Check if this is a fragment, and not the last fragment */
1194 if (more) {
1196 * Queue the fragment if we still are within the
1197 * limits of the maximum size of the rx_sdu
1199 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1200 IRDA_DEBUG(4, __FUNCTION__ "(), queueing frag\n");
1201 skb_queue_tail(&self->rx_fragments, skb);
1202 } else {
1203 /* Free the part of the SDU that is too big */
1204 dev_kfree_skb(skb);
1206 continue;
1209 * This is the last fragment, so time to reassemble!
1211 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1212 (self->rx_max_sdu_size == SAR_UNBOUND))
1215 * A little optimizing. Only queue the fragment if
1216 * there are other fragments. Since if this is the
1217 * last and only fragment, there is no need to
1218 * reassemble :-)
1220 if (!skb_queue_empty(&self->rx_fragments)) {
1221 skb_queue_tail(&self->rx_fragments,
1222 skb);
1224 skb = irttp_reassemble_skb(self);
1227 /* Now we can deliver the reassembled skb */
1228 irttp_do_data_indication(self, skb);
1229 } else {
1230 IRDA_DEBUG(1, __FUNCTION__ "(), Truncated frame\n");
1232 /* Free the part of the SDU that is too big */
1233 dev_kfree_skb(skb);
1235 /* Deliver only the valid but truncated part of SDU */
1236 skb = irttp_reassemble_skb(self);
1238 irttp_do_data_indication(self, skb);
1240 self->rx_sdu_size = 0;
1242 /* Reset lock */
1243 self->rx_queue_lock = 0;
1247 * Function irttp_flush_queues (self)
1249 * Flushes (removes all frames) in transitt-buffer (tx_list)
1251 void irttp_flush_queues(struct tsap_cb *self)
1253 struct sk_buff* skb;
1255 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1257 ASSERT(self != NULL, return;);
1258 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1260 /* Deallocate frames waiting to be sent */
1261 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
1262 dev_kfree_skb(skb);
1264 /* Deallocate received frames */
1265 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
1266 dev_kfree_skb(skb);
1268 /* Deallocate received fragments */
1269 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
1270 dev_kfree_skb(skb);
1274 * Function irttp_reasseble (self)
1276 * Makes a new (continuous) skb of all the fragments in the fragment
1277 * queue
1280 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
1282 struct sk_buff *skb, *frag;
1283 int n = 0; /* Fragment index */
1285 ASSERT(self != NULL, return NULL;);
1286 ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
1288 IRDA_DEBUG(4, __FUNCTION__ "(), self->rx_sdu_size=%d\n",
1289 self->rx_sdu_size);
1291 skb = dev_alloc_skb(self->rx_sdu_size);
1292 if (!skb)
1293 return NULL;
1296 * Need to reserve space for TTP header in case this skb needs to
1297 * be requeued in case delivery failes
1299 skb_reserve(skb, TTP_HEADER);
1300 skb_put(skb, self->rx_sdu_size);
1303 * Copy all fragments to a new buffer
1305 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
1306 memcpy(skb->data+n, frag->data, frag->len);
1307 n += frag->len;
1309 dev_kfree_skb(frag);
1311 IRDA_DEBUG(4, __FUNCTION__ "(), frame len=%d\n", n);
1312 /* Set the new length */
1314 IRDA_DEBUG(4, __FUNCTION__ "(), rx_sdu_size=%d\n", self->rx_sdu_size);
1315 ASSERT(n <= self->rx_sdu_size, return NULL;);
1316 skb_trim(skb, n);
1318 self->rx_sdu_size = 0;
1320 return skb;
1324 * Function irttp_fragment_skb (skb)
1326 * Fragments a frame and queues all the fragments for transmission
1329 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb)
1331 struct sk_buff *frag;
1332 __u8 *frame;
1334 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1336 ASSERT(self != NULL, return;);
1337 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1338 ASSERT(skb != NULL, return;);
1341 * Split frame into a number of segments
1343 while (skb->len > 0) {
1345 * Instead of making the last segment, we just
1346 * queue what is left of the original skb
1348 if (skb->len < self->max_seg_size) {
1349 IRDA_DEBUG(4, __FUNCTION__
1350 "(), queuing last segment\n");
1352 frame = skb_push(skb, TTP_HEADER);
1353 frame[0] = 0x00; /* Clear more bit */
1354 skb_queue_tail(&self->tx_queue, skb);
1356 return;
1359 /* Make new segment */
1360 frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
1361 if (!frag)
1362 return;
1364 skb_reserve(frag, self->max_header_size);
1367 * Copy data from the original skb into this fragment. We
1368 * first insert the TTP header with the more bit set
1370 frame = skb_put(frag, self->max_seg_size+TTP_HEADER);
1371 frame[0] = TTP_MORE;
1372 memcpy(frag->data+1, skb->data, self->max_seg_size);
1374 /* Hide the copied data from the original skb */
1375 skb_pull(skb, self->max_seg_size);
1377 skb_queue_tail(&self->tx_queue, frag);
1382 * Function irttp_param_max_sdu_size (self, param)
1384 * Handle the MaxSduSize parameter in the connect frames, this function
1385 * will be called both when this parameter needs to be inserted into, and
1386 * extracted from the connect frames
1388 static int irttp_param_max_sdu_size(void *instance, param_t *param, int get)
1390 struct tsap_cb *self;
1392 self = (struct tsap_cb *) instance;
1394 ASSERT(self != NULL, return -1;);
1395 ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1397 if (get)
1398 param->pv.i = self->tx_max_sdu_size;
1399 else
1400 self->tx_max_sdu_size = param->pv.i;
1402 IRDA_DEBUG(0, __FUNCTION__ "(), MaxSduSize=%d\n", param->pv.i);
1404 return 0;
1408 * Function irttp_todo_expired (data)
1410 * Todo timer has expired!
1413 static void irttp_todo_expired(unsigned long data)
1415 struct tsap_cb *self = (struct tsap_cb *) data;
1417 /* Check that we still exist */
1418 if (!self || self->magic != TTP_TSAP_MAGIC)
1419 return;
1421 irttp_run_rx_queue(self);
1422 irttp_run_tx_queue(self);
1424 /* Give avay some credits to peer? */
1425 if ((self->remote_credit < LOW_THRESHOLD) &&
1426 (self->avail_credit > 0) && (skb_queue_empty(&self->tx_queue)))
1428 irttp_give_credit(self);
1431 /* Check if time for disconnect */
1432 if (self->disconnect_pend) {
1433 /* Check if it's possible to disconnect yet */
1434 if (skb_queue_empty(&self->tx_queue)) {
1436 /* Make sure disconnect is not pending anymore */
1437 self->disconnect_pend = FALSE;
1438 if (self->disconnect_skb) {
1439 irttp_disconnect_request(
1440 self, self->disconnect_skb, P_NORMAL);
1441 self->disconnect_skb = NULL;
1442 } else
1443 irttp_disconnect_request(self, NULL, P_NORMAL);
1444 } else {
1445 /* Try again later */
1446 irttp_start_todo_timer(self, 1*HZ);
1448 /* No reason to try and close now */
1449 return;
1453 /* Check if it's closing time */
1454 if (self->close_pend)
1455 irttp_close_tsap(self);
1459 * Function irttp_start_todo_timer (self, timeout)
1461 * Start todo timer.
1464 static void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
1466 ASSERT(self != NULL, return;);
1467 ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1469 del_timer(&self->todo_timer);
1471 self->todo_timer.data = (unsigned long) self;
1472 self->todo_timer.function = &irttp_todo_expired;
1473 self->todo_timer.expires = jiffies + timeout;
1475 add_timer(&self->todo_timer);
1478 #ifdef CONFIG_PROC_FS
1480 * Function irttp_proc_read (buf, start, offset, len, unused)
1482 * Give some info to the /proc file system
1484 int irttp_proc_read(char *buf, char **start, off_t offset, int len, int unused)
1486 struct tsap_cb *self;
1487 unsigned long flags;
1488 int i = 0;
1490 ASSERT(irttp != NULL, return 0;);
1492 len = 0;
1494 save_flags(flags);
1495 cli();
1497 self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1498 while (self != NULL) {
1499 if (!self || self->magic != TTP_TSAP_MAGIC)
1500 return len;
1502 len += sprintf(buf+len, "TSAP %d, ", i++);
1503 len += sprintf(buf+len, "stsap_sel: %02x, ",
1504 self->stsap_sel);
1505 len += sprintf(buf+len, "dtsap_sel: %02x\n",
1506 self->dtsap_sel);
1507 len += sprintf(buf+len, " connected: %s, ",
1508 self->connected? "TRUE":"FALSE");
1509 len += sprintf(buf+len, "avail credit: %d, ",
1510 self->avail_credit);
1511 len += sprintf(buf+len, "remote credit: %d, ",
1512 self->remote_credit);
1513 len += sprintf(buf+len, "send credit: %d\n",
1514 self->send_credit);
1515 len += sprintf(buf+len, " tx packets: %d, ",
1516 self->stats.tx_packets);
1517 len += sprintf(buf+len, "rx packets: %d, ",
1518 self->stats.rx_packets);
1519 len += sprintf(buf+len, "tx_queue len: %d ",
1520 skb_queue_len(&self->tx_queue));
1521 len += sprintf(buf+len, "rx_queue len: %d\n",
1522 skb_queue_len(&self->rx_queue));
1523 len += sprintf(buf+len, " tx_sdu_busy: %s, ",
1524 self->tx_sdu_busy? "TRUE":"FALSE");
1525 len += sprintf(buf+len, "rx_sdu_busy: %s\n",
1526 self->rx_sdu_busy? "TRUE":"FALSE");
1527 len += sprintf(buf+len, " max_seg_size: %d, ",
1528 self->max_seg_size);
1529 len += sprintf(buf+len, "tx_max_sdu_size: %d, ",
1530 self->tx_max_sdu_size);
1531 len += sprintf(buf+len, "rx_max_sdu_size: %d\n",
1532 self->rx_max_sdu_size);
1534 len += sprintf(buf+len, " Used by (%s)\n",
1535 self->notify.name);
1537 len += sprintf(buf+len, "\n");
1539 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps);
1541 restore_flags(flags);
1543 return len;
1546 #endif /* PROC_FS */