blob: 1df6487609e1e440a0918f85d48da0e0dbcbe7bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
2 *
3 * Filename: irttp.c
4 * Version: 1.2
5 * Description: Tiny Transport Protocol (TTP) implementation
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Aug 31 20:14:31 1997
9 * Modified at: Wed Jan 5 11:31:27 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 *
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>
15 *
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.
20 *
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.
24 *
25 ********************************************************************/
26
27#include <linux/config.h>
28#include <linux/skbuff.h>
29#include <linux/init.h>
30#include <linux/seq_file.h>
31
32#include <asm/byteorder.h>
33#include <asm/unaligned.h>
34
35#include <net/irda/irda.h>
36#include <net/irda/irlap.h>
37#include <net/irda/irlmp.h>
38#include <net/irda/parameters.h>
39#include <net/irda/irttp.h>
40
41static struct irttp_cb *irttp = NULL;
42
43static void __irttp_close_tsap(struct tsap_cb *self);
44
45static int irttp_data_indication(void *instance, void *sap,
46 struct sk_buff *skb);
47static int irttp_udata_indication(void *instance, void *sap,
48 struct sk_buff *skb);
49static void irttp_disconnect_indication(void *instance, void *sap,
50 LM_REASON reason, struct sk_buff *);
51static void irttp_connect_indication(void *instance, void *sap,
52 struct qos_info *qos, __u32 max_sdu_size,
53 __u8 header_size, struct sk_buff *skb);
54static void irttp_connect_confirm(void *instance, void *sap,
55 struct qos_info *qos, __u32 max_sdu_size,
56 __u8 header_size, struct sk_buff *skb);
57static void irttp_run_tx_queue(struct tsap_cb *self);
58static void irttp_run_rx_queue(struct tsap_cb *self);
59
60static void irttp_flush_queues(struct tsap_cb *self);
61static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
62static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
63static void irttp_todo_expired(unsigned long data);
64static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
65 int get);
66
67static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow);
68static void irttp_status_indication(void *instance,
69 LINK_STATUS link, LOCK_STATUS lock);
70
71/* Information for parsing parameters in IrTTP */
72static pi_minor_info_t pi_minor_call_table[] = {
73 { NULL, 0 }, /* 0x00 */
74 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
75};
76static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
77static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
78
79/************************ GLOBAL PROCEDURES ************************/
80
81/*
82 * Function irttp_init (void)
83 *
84 * Initialize the IrTTP layer. Called by module initialization code
85 *
86 */
87int __init irttp_init(void)
88{
89 /* Initialize the irttp structure. */
90 if (irttp == NULL) {
91 irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
92 if (irttp == NULL)
93 return -ENOMEM;
94 }
95 memset(irttp, 0, sizeof(struct irttp_cb));
96
97 irttp->magic = TTP_MAGIC;
98
99 irttp->tsaps = hashbin_new(HB_LOCK);
100 if (!irttp->tsaps) {
101 IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n",
102 __FUNCTION__);
Alexey Dobriyan15166fadb2005-09-24 16:54:50 -0700103 kfree(irttp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENOMEM;
105 }
106
107 return 0;
108}
109
110/*
111 * Function irttp_cleanup (void)
112 *
113 * Called by module destruction/cleanup code
114 *
115 */
116void __exit irttp_cleanup(void)
117{
118 /* Check for main structure */
119 IRDA_ASSERT(irttp != NULL, return;);
120 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;);
121
122 /*
123 * Delete hashbin and close all TSAP instances in it
124 */
125 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
126
127 irttp->magic = 0;
128
129 /* De-allocate main structure */
130 kfree(irttp);
131
132 irttp = NULL;
133}
134
135/*************************** SUBROUTINES ***************************/
136
137/*
138 * Function irttp_start_todo_timer (self, timeout)
139 *
140 * Start todo timer.
141 *
142 * Made it more effient and unsensitive to race conditions - Jean II
143 */
144static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
145{
146 /* Set new value for timer */
147 mod_timer(&self->todo_timer, jiffies + timeout);
148}
149
150/*
151 * Function irttp_todo_expired (data)
152 *
153 * Todo timer has expired!
154 *
155 * One of the restriction of the timer is that it is run only on the timer
156 * interrupt which run every 10ms. This mean that even if you set the timer
157 * with a delay of 0, it may take up to 10ms before it's run.
158 * So, to minimise latency and keep cache fresh, we try to avoid using
159 * it as much as possible.
160 * Note : we can't use tasklets, because they can't be asynchronously
161 * killed (need user context), and we can't guarantee that here...
162 * Jean II
163 */
164static void irttp_todo_expired(unsigned long data)
165{
166 struct tsap_cb *self = (struct tsap_cb *) data;
167
168 /* Check that we still exist */
169 if (!self || self->magic != TTP_TSAP_MAGIC)
170 return;
171
172 IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
173
174 /* Try to make some progress, especially on Tx side - Jean II */
175 irttp_run_rx_queue(self);
176 irttp_run_tx_queue(self);
177
178 /* Check if time for disconnect */
179 if (test_bit(0, &self->disconnect_pend)) {
180 /* Check if it's possible to disconnect yet */
181 if (skb_queue_empty(&self->tx_queue)) {
182 /* Make sure disconnect is not pending anymore */
183 clear_bit(0, &self->disconnect_pend); /* FALSE */
184
185 /* Note : self->disconnect_skb may be NULL */
186 irttp_disconnect_request(self, self->disconnect_skb,
187 P_NORMAL);
188 self->disconnect_skb = NULL;
189 } else {
190 /* Try again later */
191 irttp_start_todo_timer(self, HZ/10);
192
193 /* No reason to try and close now */
194 return;
195 }
196 }
197
198 /* Check if it's closing time */
199 if (self->close_pend)
200 /* Finish cleanup */
201 irttp_close_tsap(self);
202}
203
204/*
205 * Function irttp_flush_queues (self)
206 *
207 * Flushes (removes all frames) in transitt-buffer (tx_list)
208 */
209void irttp_flush_queues(struct tsap_cb *self)
210{
211 struct sk_buff* skb;
212
213 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
214
215 IRDA_ASSERT(self != NULL, return;);
216 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
217
218 /* Deallocate frames waiting to be sent */
219 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
220 dev_kfree_skb(skb);
221
222 /* Deallocate received frames */
223 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
224 dev_kfree_skb(skb);
225
226 /* Deallocate received fragments */
227 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
228 dev_kfree_skb(skb);
229}
230
231/*
232 * Function irttp_reassemble (self)
233 *
234 * Makes a new (continuous) skb of all the fragments in the fragment
235 * queue
236 *
237 */
238static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
239{
240 struct sk_buff *skb, *frag;
241 int n = 0; /* Fragment index */
242
243 IRDA_ASSERT(self != NULL, return NULL;);
244 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
245
246 IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __FUNCTION__,
247 self->rx_sdu_size);
248
249 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
250 if (!skb)
251 return NULL;
252
253 /*
254 * Need to reserve space for TTP header in case this skb needs to
255 * be requeued in case delivery failes
256 */
257 skb_reserve(skb, TTP_HEADER);
258 skb_put(skb, self->rx_sdu_size);
259
260 /*
261 * Copy all fragments to a new buffer
262 */
263 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
264 memcpy(skb->data+n, frag->data, frag->len);
265 n += frag->len;
266
267 dev_kfree_skb(frag);
268 }
269
270 IRDA_DEBUG(2,
271 "%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
272 __FUNCTION__, n, self->rx_sdu_size, self->rx_max_sdu_size);
273 /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size
274 * by summing the size of all fragments, so we should always
275 * have n == self->rx_sdu_size, except in cases where we
276 * droped the last fragment (when self->rx_sdu_size exceed
277 * self->rx_max_sdu_size), where n < self->rx_sdu_size.
278 * Jean II */
279 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
280
281 /* Set the new length */
282 skb_trim(skb, n);
283
284 self->rx_sdu_size = 0;
285
286 return skb;
287}
288
289/*
290 * Function irttp_fragment_skb (skb)
291 *
292 * Fragments a frame and queues all the fragments for transmission
293 *
294 */
295static inline void irttp_fragment_skb(struct tsap_cb *self,
296 struct sk_buff *skb)
297{
298 struct sk_buff *frag;
299 __u8 *frame;
300
301 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
302
303 IRDA_ASSERT(self != NULL, return;);
304 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
305 IRDA_ASSERT(skb != NULL, return;);
306
307 /*
308 * Split frame into a number of segments
309 */
310 while (skb->len > self->max_seg_size) {
311 IRDA_DEBUG(2, "%s(), fragmenting ...\n", __FUNCTION__);
312
313 /* Make new segment */
314 frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
315 if (!frag)
316 return;
317
318 skb_reserve(frag, self->max_header_size);
319
320 /* Copy data from the original skb into this fragment. */
321 memcpy(skb_put(frag, self->max_seg_size), skb->data,
322 self->max_seg_size);
323
324 /* Insert TTP header, with the more bit set */
325 frame = skb_push(frag, TTP_HEADER);
326 frame[0] = TTP_MORE;
327
328 /* Hide the copied data from the original skb */
329 skb_pull(skb, self->max_seg_size);
330
331 /* Queue fragment */
332 skb_queue_tail(&self->tx_queue, frag);
333 }
334 /* Queue what is left of the original skb */
335 IRDA_DEBUG(2, "%s(), queuing last segment\n", __FUNCTION__);
336
337 frame = skb_push(skb, TTP_HEADER);
338 frame[0] = 0x00; /* Clear more bit */
339
340 /* Queue fragment */
341 skb_queue_tail(&self->tx_queue, skb);
342}
343
344/*
345 * Function irttp_param_max_sdu_size (self, param)
346 *
347 * Handle the MaxSduSize parameter in the connect frames, this function
348 * will be called both when this parameter needs to be inserted into, and
349 * extracted from the connect frames
350 */
351static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
352 int get)
353{
354 struct tsap_cb *self;
355
356 self = (struct tsap_cb *) instance;
357
358 IRDA_ASSERT(self != NULL, return -1;);
359 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
360
361 if (get)
362 param->pv.i = self->tx_max_sdu_size;
363 else
364 self->tx_max_sdu_size = param->pv.i;
365
366 IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __FUNCTION__, param->pv.i);
367
368 return 0;
369}
370
371/*************************** CLIENT CALLS ***************************/
372/************************** LMP CALLBACKS **************************/
373/* Everything is happily mixed up. Waiting for next clean up - Jean II */
374
375/*
376 * Function irttp_open_tsap (stsap, notify)
377 *
378 * Create TSAP connection endpoint,
379 */
380struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
381{
382 struct tsap_cb *self;
383 struct lsap_cb *lsap;
384 notify_t ttp_notify;
385
386 IRDA_ASSERT(irttp != NULL, return NULL;);
387 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
388
389 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
390 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
391 * JeanII */
392 if((stsap_sel != LSAP_ANY) &&
393 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
394 IRDA_DEBUG(0, "%s(), invalid tsap!\n", __FUNCTION__);
395 return NULL;
396 }
397
398 self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
399 if (self == NULL) {
400 IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __FUNCTION__);
401 return NULL;
402 }
403 memset(self, 0, sizeof(struct tsap_cb));
404 spin_lock_init(&self->lock);
405
406 /* Initialise todo timer */
407 init_timer(&self->todo_timer);
408 self->todo_timer.data = (unsigned long) self;
409 self->todo_timer.function = &irttp_todo_expired;
410
411 /* Initialize callbacks for IrLMP to use */
412 irda_notify_init(&ttp_notify);
413 ttp_notify.connect_confirm = irttp_connect_confirm;
414 ttp_notify.connect_indication = irttp_connect_indication;
415 ttp_notify.disconnect_indication = irttp_disconnect_indication;
416 ttp_notify.data_indication = irttp_data_indication;
417 ttp_notify.udata_indication = irttp_udata_indication;
418 ttp_notify.flow_indication = irttp_flow_indication;
419 if(notify->status_indication != NULL)
420 ttp_notify.status_indication = irttp_status_indication;
421 ttp_notify.instance = self;
422 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
423
424 self->magic = TTP_TSAP_MAGIC;
425 self->connected = FALSE;
426
427 skb_queue_head_init(&self->rx_queue);
428 skb_queue_head_init(&self->tx_queue);
429 skb_queue_head_init(&self->rx_fragments);
430 /*
431 * Create LSAP at IrLMP layer
432 */
433 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
434 if (lsap == NULL) {
435 IRDA_WARNING("%s: unable to allocate LSAP!!\n", __FUNCTION__);
436 return NULL;
437 }
438
439 /*
440 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
441 * will replace it with whatever source selector which is free, so
442 * the stsap_sel we have might not be valid anymore
443 */
444 self->stsap_sel = lsap->slsap_sel;
445 IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __FUNCTION__, self->stsap_sel);
446
447 self->notify = *notify;
448 self->lsap = lsap;
449
450 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
451
452 if (credit > TTP_RX_MAX_CREDIT)
453 self->initial_credit = TTP_RX_MAX_CREDIT;
454 else
455 self->initial_credit = credit;
456
457 return self;
458}
459EXPORT_SYMBOL(irttp_open_tsap);
460
461/*
462 * Function irttp_close (handle)
463 *
464 * Remove an instance of a TSAP. This function should only deal with the
465 * deallocation of the TSAP, and resetting of the TSAPs values;
466 *
467 */
468static void __irttp_close_tsap(struct tsap_cb *self)
469{
470 /* First make sure we're connected. */
471 IRDA_ASSERT(self != NULL, return;);
472 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
473
474 irttp_flush_queues(self);
475
476 del_timer(&self->todo_timer);
477
478 /* This one won't be cleaned up if we are disconnect_pend + close_pend
479 * and we receive a disconnect_indication */
480 if (self->disconnect_skb)
481 dev_kfree_skb(self->disconnect_skb);
482
483 self->connected = FALSE;
484 self->magic = ~TTP_TSAP_MAGIC;
485
486 kfree(self);
487}
488
489/*
490 * Function irttp_close (self)
491 *
492 * Remove TSAP from list of all TSAPs and then deallocate all resources
493 * associated with this TSAP
494 *
495 * Note : because we *free* the tsap structure, it is the responsibility
496 * of the caller to make sure we are called only once and to deal with
497 * possible race conditions. - Jean II
498 */
499int irttp_close_tsap(struct tsap_cb *self)
500{
501 struct tsap_cb *tsap;
502
503 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
504
505 IRDA_ASSERT(self != NULL, return -1;);
506 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
507
508 /* Make sure tsap has been disconnected */
509 if (self->connected) {
510 /* Check if disconnect is not pending */
511 if (!test_bit(0, &self->disconnect_pend)) {
512 IRDA_WARNING("%s: TSAP still connected!\n",
513 __FUNCTION__);
514 irttp_disconnect_request(self, NULL, P_NORMAL);
515 }
516 self->close_pend = TRUE;
517 irttp_start_todo_timer(self, HZ/10);
518
519 return 0; /* Will be back! */
520 }
521
522 tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);
523
524 IRDA_ASSERT(tsap == self, return -1;);
525
526 /* Close corresponding LSAP */
527 if (self->lsap) {
528 irlmp_close_lsap(self->lsap);
529 self->lsap = NULL;
530 }
531
532 __irttp_close_tsap(self);
533
534 return 0;
535}
536EXPORT_SYMBOL(irttp_close_tsap);
537
538/*
539 * Function irttp_udata_request (self, skb)
540 *
541 * Send unreliable data on this TSAP
542 *
543 */
544int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
545{
546 IRDA_ASSERT(self != NULL, return -1;);
547 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
548 IRDA_ASSERT(skb != NULL, return -1;);
549
550 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
551
552 /* Check that nothing bad happens */
553 if ((skb->len == 0) || (!self->connected)) {
554 IRDA_DEBUG(1, "%s(), No data, or not connected\n",
555 __FUNCTION__);
556 goto err;
557 }
558
559 if (skb->len > self->max_seg_size) {
560 IRDA_DEBUG(1, "%s(), UData is to large for IrLAP!\n",
561 __FUNCTION__);
562 goto err;
563 }
564
565 irlmp_udata_request(self->lsap, skb);
566 self->stats.tx_packets++;
567
568 return 0;
569
570err:
571 dev_kfree_skb(skb);
572 return -1;
573}
574EXPORT_SYMBOL(irttp_udata_request);
575
576
577/*
578 * Function irttp_data_request (handle, skb)
579 *
580 * Queue frame for transmission. If SAR is enabled, fragement the frame
581 * and queue the fragments for transmission
582 */
583int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
584{
585 __u8 *frame;
586 int ret;
587
588 IRDA_ASSERT(self != NULL, return -1;);
589 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
590 IRDA_ASSERT(skb != NULL, return -1;);
591
592 IRDA_DEBUG(2, "%s() : queue len = %d\n", __FUNCTION__,
593 skb_queue_len(&self->tx_queue));
594
595 /* Check that nothing bad happens */
596 if ((skb->len == 0) || (!self->connected)) {
597 IRDA_WARNING("%s: No data, or not connected\n", __FUNCTION__);
598 ret = -ENOTCONN;
599 goto err;
600 }
601
602 /*
603 * Check if SAR is disabled, and the frame is larger than what fits
604 * inside an IrLAP frame
605 */
606 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
607 IRDA_ERROR("%s: SAR disabled, and data is to large for IrLAP!\n",
608 __FUNCTION__);
609 ret = -EMSGSIZE;
610 goto err;
611 }
612
613 /*
614 * Check if SAR is enabled, and the frame is larger than the
615 * TxMaxSduSize
616 */
617 if ((self->tx_max_sdu_size != 0) &&
618 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
619 (skb->len > self->tx_max_sdu_size))
620 {
621 IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
622 __FUNCTION__);
623 ret = -EMSGSIZE;
624 goto err;
625 }
626 /*
627 * Check if transmit queue is full
628 */
629 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
630 /*
631 * Give it a chance to empty itself
632 */
633 irttp_run_tx_queue(self);
634
635 /* Drop packet. This error code should trigger the caller
636 * to resend the data in the client code - Jean II */
637 ret = -ENOBUFS;
638 goto err;
639 }
640
641 /* Queue frame, or queue frame segments */
642 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
643 /* Queue frame */
644 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
645 frame = skb_push(skb, TTP_HEADER);
646 frame[0] = 0x00; /* Clear more bit */
647
648 skb_queue_tail(&self->tx_queue, skb);
649 } else {
650 /*
651 * Fragment the frame, this function will also queue the
652 * fragments, we don't care about the fact the transmit
653 * queue may be overfilled by all the segments for a little
654 * while
655 */
656 irttp_fragment_skb(self, skb);
657 }
658
659 /* Check if we can accept more data from client */
660 if ((!self->tx_sdu_busy) &&
661 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
662 /* Tx queue filling up, so stop client. */
663 if (self->notify.flow_indication) {
664 self->notify.flow_indication(self->notify.instance,
665 self, FLOW_STOP);
666 }
667 /* self->tx_sdu_busy is the state of the client.
668 * Update state after notifying client to avoid
669 * race condition with irttp_flow_indication().
670 * If the queue empty itself after our test but before
671 * we set the flag, we will fix ourselves below in
672 * irttp_run_tx_queue().
673 * Jean II */
674 self->tx_sdu_busy = TRUE;
675 }
676
677 /* Try to make some progress */
678 irttp_run_tx_queue(self);
679
680 return 0;
681
682err:
683 dev_kfree_skb(skb);
684 return ret;
685}
686EXPORT_SYMBOL(irttp_data_request);
687
688/*
689 * Function irttp_run_tx_queue (self)
690 *
691 * Transmit packets queued for transmission (if possible)
692 *
693 */
694static void irttp_run_tx_queue(struct tsap_cb *self)
695{
696 struct sk_buff *skb;
697 unsigned long flags;
698 int n;
699
700 IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n",
701 __FUNCTION__,
702 self->send_credit, skb_queue_len(&self->tx_queue));
703
704 /* Get exclusive access to the tx queue, otherwise don't touch it */
705 if (irda_lock(&self->tx_queue_lock) == FALSE)
706 return;
707
708 /* Try to send out frames as long as we have credits
709 * and as long as LAP is not full. If LAP is full, it will
710 * poll us through irttp_flow_indication() - Jean II */
711 while ((self->send_credit > 0) &&
712 (!irlmp_lap_tx_queue_full(self->lsap)) &&
713 (skb = skb_dequeue(&self->tx_queue)))
714 {
715 /*
716 * Since we can transmit and receive frames concurrently,
717 * the code below is a critical region and we must assure that
718 * nobody messes with the credits while we update them.
719 */
720 spin_lock_irqsave(&self->lock, flags);
721
722 n = self->avail_credit;
723 self->avail_credit = 0;
724
725 /* Only room for 127 credits in frame */
726 if (n > 127) {
727 self->avail_credit = n-127;
728 n = 127;
729 }
730 self->remote_credit += n;
731 self->send_credit--;
732
733 spin_unlock_irqrestore(&self->lock, flags);
734
735 /*
736 * More bit must be set by the data_request() or fragment()
737 * functions
738 */
739 skb->data[0] |= (n & 0x7f);
740
741 /* Detach from socket.
742 * The current skb has a reference to the socket that sent
743 * it (skb->sk). When we pass it to IrLMP, the skb will be
744 * stored in in IrLAP (self->wx_list). When we are within
745 * IrLAP, we lose the notion of socket, so we should not
746 * have a reference to a socket. So, we drop it here.
747 *
748 * Why does it matter ?
749 * When the skb is freed (kfree_skb), if it is associated
750 * with a socket, it release buffer space on the socket
751 * (through sock_wfree() and sock_def_write_space()).
752 * If the socket no longer exist, we may crash. Hard.
753 * When we close a socket, we make sure that associated packets
754 * in IrTTP are freed. However, we have no way to cancel
755 * the packet that we have passed to IrLAP. So, if a packet
756 * remains in IrLAP (retry on the link or else) after we
757 * close the socket, we are dead !
758 * Jean II */
759 if (skb->sk != NULL) {
760 /* IrSOCK application, IrOBEX, ... */
761 skb_orphan(skb);
762 }
763 /* IrCOMM over IrTTP, IrLAN, ... */
764
765 /* Pass the skb to IrLMP - done */
766 irlmp_data_request(self->lsap, skb);
767 self->stats.tx_packets++;
768 }
769
770 /* Check if we can accept more frames from client.
771 * We don't want to wait until the todo timer to do that, and we
772 * can't use tasklets (grr...), so we are obliged to give control
773 * to client. That's ok, this test will be true not too often
774 * (max once per LAP window) and we are called from places
775 * where we can spend a bit of time doing stuff. - Jean II */
776 if ((self->tx_sdu_busy) &&
777 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
778 (!self->close_pend))
779 {
780 if (self->notify.flow_indication)
781 self->notify.flow_indication(self->notify.instance,
782 self, FLOW_START);
783
784 /* self->tx_sdu_busy is the state of the client.
785 * We don't really have a race here, but it's always safer
786 * to update our state after the client - Jean II */
787 self->tx_sdu_busy = FALSE;
788 }
789
790 /* Reset lock */
791 self->tx_queue_lock = 0;
792}
793
794/*
795 * Function irttp_give_credit (self)
796 *
797 * Send a dataless flowdata TTP-PDU and give available credit to peer
798 * TSAP
799 */
800static inline void irttp_give_credit(struct tsap_cb *self)
801{
802 struct sk_buff *tx_skb = NULL;
803 unsigned long flags;
804 int n;
805
806 IRDA_ASSERT(self != NULL, return;);
807 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
808
809 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n",
810 __FUNCTION__,
811 self->send_credit, self->avail_credit, self->remote_credit);
812
813 /* Give credit to peer */
814 tx_skb = dev_alloc_skb(64);
815 if (!tx_skb)
816 return;
817
818 /* Reserve space for LMP, and LAP header */
819 skb_reserve(tx_skb, self->max_header_size);
820
821 /*
822 * Since we can transmit and receive frames concurrently,
823 * the code below is a critical region and we must assure that
824 * nobody messes with the credits while we update them.
825 */
826 spin_lock_irqsave(&self->lock, flags);
827
828 n = self->avail_credit;
829 self->avail_credit = 0;
830
831 /* Only space for 127 credits in frame */
832 if (n > 127) {
833 self->avail_credit = n - 127;
834 n = 127;
835 }
836 self->remote_credit += n;
837
838 spin_unlock_irqrestore(&self->lock, flags);
839
840 skb_put(tx_skb, 1);
841 tx_skb->data[0] = (__u8) (n & 0x7f);
842
843 irlmp_data_request(self->lsap, tx_skb);
844 self->stats.tx_packets++;
845}
846
847/*
848 * Function irttp_udata_indication (instance, sap, skb)
849 *
850 * Received some unit-data (unreliable)
851 *
852 */
853static int irttp_udata_indication(void *instance, void *sap,
854 struct sk_buff *skb)
855{
856 struct tsap_cb *self;
857 int err;
858
859 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
860
861 self = (struct tsap_cb *) instance;
862
863 IRDA_ASSERT(self != NULL, return -1;);
864 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
865 IRDA_ASSERT(skb != NULL, return -1;);
866
867 self->stats.rx_packets++;
868
869 /* Just pass data to layer above */
870 if (self->notify.udata_indication) {
871 err = self->notify.udata_indication(self->notify.instance,
872 self,skb);
873 /* Same comment as in irttp_do_data_indication() */
874 if (!err)
875 return 0;
876 }
877 /* Either no handler, or handler returns an error */
878 dev_kfree_skb(skb);
879
880 return 0;
881}
882
883/*
884 * Function irttp_data_indication (instance, sap, skb)
885 *
886 * Receive segment from IrLMP.
887 *
888 */
889static int irttp_data_indication(void *instance, void *sap,
890 struct sk_buff *skb)
891{
892 struct tsap_cb *self;
893 unsigned long flags;
894 int n;
895
896 self = (struct tsap_cb *) instance;
897
898 n = skb->data[0] & 0x7f; /* Extract the credits */
899
900 self->stats.rx_packets++;
901
902 /* Deal with inbound credit
903 * Since we can transmit and receive frames concurrently,
904 * the code below is a critical region and we must assure that
905 * nobody messes with the credits while we update them.
906 */
907 spin_lock_irqsave(&self->lock, flags);
908 self->send_credit += n;
909 if (skb->len > 1)
910 self->remote_credit--;
911 spin_unlock_irqrestore(&self->lock, flags);
912
913 /*
914 * Data or dataless packet? Dataless frames contains only the
915 * TTP_HEADER.
916 */
917 if (skb->len > 1) {
918 /*
919 * We don't remove the TTP header, since we must preserve the
920 * more bit, so the defragment routing knows what to do
921 */
922 skb_queue_tail(&self->rx_queue, skb);
923 } else {
924 /* Dataless flowdata TTP-PDU */
925 dev_kfree_skb(skb);
926 }
927
928
929 /* Push data to the higher layer.
930 * We do it synchronously because running the todo timer for each
931 * receive packet would be too much overhead and latency.
932 * By passing control to the higher layer, we run the risk that
933 * it may take time or grab a lock. Most often, the higher layer
934 * will only put packet in a queue.
935 * Anyway, packets are only dripping through the IrDA, so we can
936 * have time before the next packet.
937 * Further, we are run from NET_BH, so the worse that can happen is
938 * us missing the optimal time to send back the PF bit in LAP.
939 * Jean II */
940 irttp_run_rx_queue(self);
941
942 /* We now give credits to peer in irttp_run_rx_queue().
943 * We need to send credit *NOW*, otherwise we are going
944 * to miss the next Tx window. The todo timer may take
945 * a while before it's run... - Jean II */
946
947 /*
948 * If the peer device has given us some credits and we didn't have
949 * anyone from before, then we need to shedule the tx queue.
950 * We need to do that because our Tx have stopped (so we may not
951 * get any LAP flow indication) and the user may be stopped as
952 * well. - Jean II
953 */
954 if (self->send_credit == n) {
955 /* Restart pushing stuff to LAP */
956 irttp_run_tx_queue(self);
957 /* Note : we don't want to schedule the todo timer
958 * because it has horrible latency. No tasklets
959 * because the tasklet API is broken. - Jean II */
960 }
961
962 return 0;
963}
964
965/*
966 * Function irttp_status_indication (self, reason)
967 *
968 * Status_indication, just pass to the higher layer...
969 *
970 */
971static void irttp_status_indication(void *instance,
972 LINK_STATUS link, LOCK_STATUS lock)
973{
974 struct tsap_cb *self;
975
976 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
977
978 self = (struct tsap_cb *) instance;
979
980 IRDA_ASSERT(self != NULL, return;);
981 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
982
983 /* Check if client has already closed the TSAP and gone away */
984 if (self->close_pend)
985 return;
986
987 /*
988 * Inform service user if he has requested it
989 */
990 if (self->notify.status_indication != NULL)
991 self->notify.status_indication(self->notify.instance,
992 link, lock);
993 else
994 IRDA_DEBUG(2, "%s(), no handler\n", __FUNCTION__);
995}
996
997/*
998 * Function irttp_flow_indication (self, reason)
999 *
1000 * Flow_indication : IrLAP tells us to send more data.
1001 *
1002 */
1003static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
1004{
1005 struct tsap_cb *self;
1006
1007 self = (struct tsap_cb *) instance;
1008
1009 IRDA_ASSERT(self != NULL, return;);
1010 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1011
1012 IRDA_DEBUG(4, "%s(instance=%p)\n", __FUNCTION__, self);
1013
1014 /* We are "polled" directly from LAP, and the LAP want to fill
1015 * its Tx window. We want to do our best to send it data, so that
1016 * we maximise the window. On the other hand, we want to limit the
1017 * amount of work here so that LAP doesn't hang forever waiting
1018 * for packets. - Jean II */
1019
1020 /* Try to send some packets. Currently, LAP calls us every time
1021 * there is one free slot, so we will send only one packet.
1022 * This allow the scheduler to do its round robin - Jean II */
1023 irttp_run_tx_queue(self);
1024
1025 /* Note regarding the interraction with higher layer.
1026 * irttp_run_tx_queue() may call the client when its queue
1027 * start to empty, via notify.flow_indication(). Initially.
1028 * I wanted this to happen in a tasklet, to avoid client
1029 * grabbing the CPU, but we can't use tasklets safely. And timer
1030 * is definitely too slow.
1031 * This will happen only once per LAP window, and usually at
1032 * the third packet (unless window is smaller). LAP is still
1033 * doing mtt and sending first packet so it's sort of OK
1034 * to do that. Jean II */
1035
1036 /* If we need to send disconnect. try to do it now */
1037 if(self->disconnect_pend)
1038 irttp_start_todo_timer(self, 0);
1039}
1040
1041/*
1042 * Function irttp_flow_request (self, command)
1043 *
1044 * This function could be used by the upper layers to tell IrTTP to stop
1045 * delivering frames if the receive queues are starting to get full, or
1046 * to tell IrTTP to start delivering frames again.
1047 */
1048void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1049{
1050 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1051
1052 IRDA_ASSERT(self != NULL, return;);
1053 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1054
1055 switch (flow) {
1056 case FLOW_STOP:
1057 IRDA_DEBUG(1, "%s(), flow stop\n", __FUNCTION__);
1058 self->rx_sdu_busy = TRUE;
1059 break;
1060 case FLOW_START:
1061 IRDA_DEBUG(1, "%s(), flow start\n", __FUNCTION__);
1062 self->rx_sdu_busy = FALSE;
1063
1064 /* Client say he can accept more data, try to free our
1065 * queues ASAP - Jean II */
1066 irttp_run_rx_queue(self);
1067
1068 break;
1069 default:
1070 IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __FUNCTION__);
1071 }
1072}
1073EXPORT_SYMBOL(irttp_flow_request);
1074
1075/*
1076 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
1077 *
1078 * Try to connect to remote destination TSAP selector
1079 *
1080 */
1081int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1082 __u32 saddr, __u32 daddr,
1083 struct qos_info *qos, __u32 max_sdu_size,
1084 struct sk_buff *userdata)
1085{
1086 struct sk_buff *tx_skb;
1087 __u8 *frame;
1088 __u8 n;
1089
1090 IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __FUNCTION__, max_sdu_size);
1091
1092 IRDA_ASSERT(self != NULL, return -EBADR;);
1093 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1094
1095 if (self->connected) {
1096 if(userdata)
1097 dev_kfree_skb(userdata);
1098 return -EISCONN;
1099 }
1100
1101 /* Any userdata supplied? */
1102 if (userdata == NULL) {
1103 tx_skb = dev_alloc_skb(64);
1104 if (!tx_skb)
1105 return -ENOMEM;
1106
1107 /* Reserve space for MUX_CONTROL and LAP header */
1108 skb_reserve(tx_skb, TTP_MAX_HEADER);
1109 } else {
1110 tx_skb = userdata;
1111 /*
1112 * Check that the client has reserved enough space for
1113 * headers
1114 */
1115 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1116 { dev_kfree_skb(userdata); return -1; } );
1117 }
1118
1119 /* Initialize connection parameters */
1120 self->connected = FALSE;
1121 self->avail_credit = 0;
1122 self->rx_max_sdu_size = max_sdu_size;
1123 self->rx_sdu_size = 0;
1124 self->rx_sdu_busy = FALSE;
1125 self->dtsap_sel = dtsap_sel;
1126
1127 n = self->initial_credit;
1128
1129 self->remote_credit = 0;
1130 self->send_credit = 0;
1131
1132 /*
1133 * Give away max 127 credits for now
1134 */
1135 if (n > 127) {
1136 self->avail_credit=n-127;
1137 n = 127;
1138 }
1139
1140 self->remote_credit = n;
1141
1142 /* SAR enabled? */
1143 if (max_sdu_size > 0) {
1144 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1145 { dev_kfree_skb(tx_skb); return -1; } );
1146
1147 /* Insert SAR parameters */
1148 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1149
1150 frame[0] = TTP_PARAMETERS | n;
1151 frame[1] = 0x04; /* Length */
1152 frame[2] = 0x01; /* MaxSduSize */
1153 frame[3] = 0x02; /* Value length */
1154
1155 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1156 (__u16 *)(frame+4));
1157 } else {
1158 /* Insert plain TTP header */
1159 frame = skb_push(tx_skb, TTP_HEADER);
1160
1161 /* Insert initial credit in frame */
1162 frame[0] = n & 0x7f;
1163 }
1164
1165 /* Connect with IrLMP. No QoS parameters for now */
1166 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1167 tx_skb);
1168}
1169EXPORT_SYMBOL(irttp_connect_request);
1170
1171/*
1172 * Function irttp_connect_confirm (handle, qos, skb)
1173 *
1174 * Sevice user confirms TSAP connection with peer.
1175 *
1176 */
1177static void irttp_connect_confirm(void *instance, void *sap,
1178 struct qos_info *qos, __u32 max_seg_size,
1179 __u8 max_header_size, struct sk_buff *skb)
1180{
1181 struct tsap_cb *self;
1182 int parameters;
1183 int ret;
1184 __u8 plen;
1185 __u8 n;
1186
1187 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1188
1189 self = (struct tsap_cb *) instance;
1190
1191 IRDA_ASSERT(self != NULL, return;);
1192 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1193 IRDA_ASSERT(skb != NULL, return;);
1194
1195 self->max_seg_size = max_seg_size - TTP_HEADER;
1196 self->max_header_size = max_header_size + TTP_HEADER;
1197
1198 /*
1199 * Check if we have got some QoS parameters back! This should be the
1200 * negotiated QoS for the link.
1201 */
1202 if (qos) {
1203 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",
1204 qos->baud_rate.bits);
1205 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1206 qos->baud_rate.value);
1207 }
1208
1209 n = skb->data[0] & 0x7f;
1210
1211 IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __FUNCTION__, n);
1212
1213 self->send_credit = n;
1214 self->tx_max_sdu_size = 0;
1215 self->connected = TRUE;
1216
1217 parameters = skb->data[0] & 0x80;
1218
1219 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1220 skb_pull(skb, TTP_HEADER);
1221
1222 if (parameters) {
1223 plen = skb->data[0];
1224
1225 ret = irda_param_extract_all(self, skb->data+1,
1226 IRDA_MIN(skb->len-1, plen),
1227 &param_info);
1228
1229 /* Any errors in the parameter list? */
1230 if (ret < 0) {
1231 IRDA_WARNING("%s: error extracting parameters\n",
1232 __FUNCTION__);
1233 dev_kfree_skb(skb);
1234
1235 /* Do not accept this connection attempt */
1236 return;
1237 }
1238 /* Remove parameters */
1239 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1240 }
1241
1242 IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
1243 self->send_credit, self->avail_credit, self->remote_credit);
1244
1245 IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __FUNCTION__,
1246 self->tx_max_sdu_size);
1247
1248 if (self->notify.connect_confirm) {
1249 self->notify.connect_confirm(self->notify.instance, self, qos,
1250 self->tx_max_sdu_size,
1251 self->max_header_size, skb);
1252 } else
1253 dev_kfree_skb(skb);
1254}
1255
1256/*
1257 * Function irttp_connect_indication (handle, skb)
1258 *
1259 * Some other device is connecting to this TSAP
1260 *
1261 */
1262void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
1263 __u32 max_seg_size, __u8 max_header_size,
1264 struct sk_buff *skb)
1265{
1266 struct tsap_cb *self;
1267 struct lsap_cb *lsap;
1268 int parameters;
1269 int ret;
1270 __u8 plen;
1271 __u8 n;
1272
1273 self = (struct tsap_cb *) instance;
1274
1275 IRDA_ASSERT(self != NULL, return;);
1276 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1277 IRDA_ASSERT(skb != NULL, return;);
1278
1279 lsap = (struct lsap_cb *) sap;
1280
1281 self->max_seg_size = max_seg_size - TTP_HEADER;
1282 self->max_header_size = max_header_size+TTP_HEADER;
1283
1284 IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __FUNCTION__, self->stsap_sel);
1285
1286 /* Need to update dtsap_sel if its equal to LSAP_ANY */
1287 self->dtsap_sel = lsap->dlsap_sel;
1288
1289 n = skb->data[0] & 0x7f;
1290
1291 self->send_credit = n;
1292 self->tx_max_sdu_size = 0;
1293
1294 parameters = skb->data[0] & 0x80;
1295
1296 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1297 skb_pull(skb, TTP_HEADER);
1298
1299 if (parameters) {
1300 plen = skb->data[0];
1301
1302 ret = irda_param_extract_all(self, skb->data+1,
1303 IRDA_MIN(skb->len-1, plen),
1304 &param_info);
1305
1306 /* Any errors in the parameter list? */
1307 if (ret < 0) {
1308 IRDA_WARNING("%s: error extracting parameters\n",
1309 __FUNCTION__);
1310 dev_kfree_skb(skb);
1311
1312 /* Do not accept this connection attempt */
1313 return;
1314 }
1315
1316 /* Remove parameters */
1317 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1318 }
1319
1320 if (self->notify.connect_indication) {
1321 self->notify.connect_indication(self->notify.instance, self,
1322 qos, self->tx_max_sdu_size,
1323 self->max_header_size, skb);
1324 } else
1325 dev_kfree_skb(skb);
1326}
1327
1328/*
1329 * Function irttp_connect_response (handle, userdata)
1330 *
1331 * Service user is accepting the connection, just pass it down to
1332 * IrLMP!
1333 *
1334 */
1335int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1336 struct sk_buff *userdata)
1337{
1338 struct sk_buff *tx_skb;
1339 __u8 *frame;
1340 int ret;
1341 __u8 n;
1342
1343 IRDA_ASSERT(self != NULL, return -1;);
1344 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1345
1346 IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __FUNCTION__,
1347 self->stsap_sel);
1348
1349 /* Any userdata supplied? */
1350 if (userdata == NULL) {
1351 tx_skb = dev_alloc_skb(64);
1352 if (!tx_skb)
1353 return -ENOMEM;
1354
1355 /* Reserve space for MUX_CONTROL and LAP header */
1356 skb_reserve(tx_skb, TTP_MAX_HEADER);
1357 } else {
1358 tx_skb = userdata;
1359 /*
1360 * Check that the client has reserved enough space for
1361 * headers
1362 */
1363 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1364 { dev_kfree_skb(userdata); return -1; } );
1365 }
1366
1367 self->avail_credit = 0;
1368 self->remote_credit = 0;
1369 self->rx_max_sdu_size = max_sdu_size;
1370 self->rx_sdu_size = 0;
1371 self->rx_sdu_busy = FALSE;
1372
1373 n = self->initial_credit;
1374
1375 /* Frame has only space for max 127 credits (7 bits) */
1376 if (n > 127) {
1377 self->avail_credit = n - 127;
1378 n = 127;
1379 }
1380
1381 self->remote_credit = n;
1382 self->connected = TRUE;
1383
1384 /* SAR enabled? */
1385 if (max_sdu_size > 0) {
1386 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1387 { dev_kfree_skb(tx_skb); return -1; } );
1388
1389 /* Insert TTP header with SAR parameters */
1390 frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);
1391
1392 frame[0] = TTP_PARAMETERS | n;
1393 frame[1] = 0x04; /* Length */
1394
1395 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
1396/* TTP_SAR_HEADER, &param_info) */
1397
1398 frame[2] = 0x01; /* MaxSduSize */
1399 frame[3] = 0x02; /* Value length */
1400
1401 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1402 (__u16 *)(frame+4));
1403 } else {
1404 /* Insert TTP header */
1405 frame = skb_push(tx_skb, TTP_HEADER);
1406
1407 frame[0] = n & 0x7f;
1408 }
1409
1410 ret = irlmp_connect_response(self->lsap, tx_skb);
1411
1412 return ret;
1413}
1414EXPORT_SYMBOL(irttp_connect_response);
1415
1416/*
1417 * Function irttp_dup (self, instance)
1418 *
1419 * Duplicate TSAP, can be used by servers to confirm a connection on a
1420 * new TSAP so it can keep listening on the old one.
1421 */
1422struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1423{
1424 struct tsap_cb *new;
1425 unsigned long flags;
1426
1427 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
1428
1429 /* Protect our access to the old tsap instance */
1430 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
1431
1432 /* Find the old instance */
1433 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
1434 IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __FUNCTION__);
1435 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1436 return NULL;
1437 }
1438
1439 /* Allocate a new instance */
1440 new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
1441 if (!new) {
1442 IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __FUNCTION__);
1443 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1444 return NULL;
1445 }
1446 /* Dup */
1447 memcpy(new, orig, sizeof(struct tsap_cb));
1448
1449 /* We don't need the old instance any more */
1450 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1451
1452 /* Try to dup the LSAP (may fail if we were too slow) */
1453 new->lsap = irlmp_dup(orig->lsap, new);
1454 if (!new->lsap) {
1455 IRDA_DEBUG(0, "%s(), dup failed!\n", __FUNCTION__);
1456 kfree(new);
1457 return NULL;
1458 }
1459
1460 /* Not everything should be copied */
1461 new->notify.instance = instance;
1462 init_timer(&new->todo_timer);
1463
1464 skb_queue_head_init(&new->rx_queue);
1465 skb_queue_head_init(&new->tx_queue);
1466 skb_queue_head_init(&new->rx_fragments);
1467
1468 /* This is locked */
1469 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
1470
1471 return new;
1472}
1473EXPORT_SYMBOL(irttp_dup);
1474
1475/*
1476 * Function irttp_disconnect_request (self)
1477 *
1478 * Close this connection please! If priority is high, the queued data
1479 * segments, if any, will be deallocated first
1480 *
1481 */
1482int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1483 int priority)
1484{
1485 int ret;
1486
1487 IRDA_ASSERT(self != NULL, return -1;);
1488 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1489
1490 /* Already disconnected? */
1491 if (!self->connected) {
1492 IRDA_DEBUG(4, "%s(), already disconnected!\n", __FUNCTION__);
1493 if (userdata)
1494 dev_kfree_skb(userdata);
1495 return -1;
1496 }
1497
1498 /* Disconnect already pending ?
1499 * We need to use an atomic operation to prevent reentry. This
1500 * function may be called from various context, like user, timer
1501 * for following a disconnect_indication() (i.e. net_bh).
1502 * Jean II */
1503 if(test_and_set_bit(0, &self->disconnect_pend)) {
1504 IRDA_DEBUG(0, "%s(), disconnect already pending\n",
1505 __FUNCTION__);
1506 if (userdata)
1507 dev_kfree_skb(userdata);
1508
1509 /* Try to make some progress */
1510 irttp_run_tx_queue(self);
1511 return -1;
1512 }
1513
1514 /*
1515 * Check if there is still data segments in the transmit queue
1516 */
David S. Millerb03efcf2005-07-08 14:57:23 -07001517 if (!skb_queue_empty(&self->tx_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (priority == P_HIGH) {
1519 /*
1520 * No need to send the queued data, if we are
1521 * disconnecting right now since the data will
1522 * not have any usable connection to be sent on
1523 */
1524 IRDA_DEBUG(1, "%s(): High priority!!()\n", __FUNCTION__);
1525 irttp_flush_queues(self);
1526 } else if (priority == P_NORMAL) {
1527 /*
1528 * Must delay disconnect until after all data segments
1529 * have been sent and the tx_queue is empty
1530 */
1531 /* We'll reuse this one later for the disconnect */
1532 self->disconnect_skb = userdata; /* May be NULL */
1533
1534 irttp_run_tx_queue(self);
1535
1536 irttp_start_todo_timer(self, HZ/10);
1537 return -1;
1538 }
1539 }
1540 /* Note : we don't need to check if self->rx_queue is full and the
1541 * state of self->rx_sdu_busy because the disconnect response will
1542 * be sent at the LMP level (so even if the peer has its Tx queue
1543 * full of data). - Jean II */
1544
1545 IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __FUNCTION__);
1546 self->connected = FALSE;
1547
1548 if (!userdata) {
1549 struct sk_buff *tx_skb;
1550 tx_skb = dev_alloc_skb(64);
1551 if (!tx_skb)
1552 return -ENOMEM;
1553
1554 /*
1555 * Reserve space for MUX and LAP header
1556 */
1557 skb_reserve(tx_skb, TTP_MAX_HEADER);
1558
1559 userdata = tx_skb;
1560 }
1561 ret = irlmp_disconnect_request(self->lsap, userdata);
1562
1563 /* The disconnect is no longer pending */
1564 clear_bit(0, &self->disconnect_pend); /* FALSE */
1565
1566 return ret;
1567}
1568EXPORT_SYMBOL(irttp_disconnect_request);
1569
1570/*
1571 * Function irttp_disconnect_indication (self, reason)
1572 *
1573 * Disconnect indication, TSAP disconnected by peer?
1574 *
1575 */
1576void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason,
1577 struct sk_buff *skb)
1578{
1579 struct tsap_cb *self;
1580
1581 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1582
1583 self = (struct tsap_cb *) instance;
1584
1585 IRDA_ASSERT(self != NULL, return;);
1586 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1587
1588 /* Prevent higher layer to send more data */
1589 self->connected = FALSE;
1590
1591 /* Check if client has already tried to close the TSAP */
1592 if (self->close_pend) {
1593 /* In this case, the higher layer is probably gone. Don't
1594 * bother it and clean up the remains - Jean II */
1595 if (skb)
1596 dev_kfree_skb(skb);
1597 irttp_close_tsap(self);
1598 return;
1599 }
1600
1601 /* If we are here, we assume that is the higher layer is still
1602 * waiting for the disconnect notification and able to process it,
1603 * even if he tried to disconnect. Otherwise, it would have already
1604 * attempted to close the tsap and self->close_pend would be TRUE.
1605 * Jean II */
1606
1607 /* No need to notify the client if has already tried to disconnect */
1608 if(self->notify.disconnect_indication)
1609 self->notify.disconnect_indication(self->notify.instance, self,
1610 reason, skb);
1611 else
1612 if (skb)
1613 dev_kfree_skb(skb);
1614}
1615
1616/*
1617 * Function irttp_do_data_indication (self, skb)
1618 *
1619 * Try to deliver reassembled skb to layer above, and requeue it if that
1620 * for some reason should fail. We mark rx sdu as busy to apply back
1621 * pressure is necessary.
1622 */
1623static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1624{
1625 int err;
1626
1627 /* Check if client has already closed the TSAP and gone away */
1628 if (self->close_pend) {
1629 dev_kfree_skb(skb);
1630 return;
1631 }
1632
1633 err = self->notify.data_indication(self->notify.instance, self, skb);
1634
1635 /* Usually the layer above will notify that it's input queue is
1636 * starting to get filled by using the flow request, but this may
1637 * be difficult, so it can instead just refuse to eat it and just
1638 * give an error back
1639 */
1640 if (err) {
1641 IRDA_DEBUG(0, "%s() requeueing skb!\n", __FUNCTION__);
1642
1643 /* Make sure we take a break */
1644 self->rx_sdu_busy = TRUE;
1645
1646 /* Need to push the header in again */
1647 skb_push(skb, TTP_HEADER);
1648 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1649
1650 /* Put skb back on queue */
1651 skb_queue_head(&self->rx_queue, skb);
1652 }
1653}
1654
1655/*
1656 * Function irttp_run_rx_queue (self)
1657 *
1658 * Check if we have any frames to be transmitted, or if we have any
1659 * available credit to give away.
1660 */
1661void irttp_run_rx_queue(struct tsap_cb *self)
1662{
1663 struct sk_buff *skb;
1664 int more = 0;
1665
1666 IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __FUNCTION__,
1667 self->send_credit, self->avail_credit, self->remote_credit);
1668
1669 /* Get exclusive access to the rx queue, otherwise don't touch it */
1670 if (irda_lock(&self->rx_queue_lock) == FALSE)
1671 return;
1672
1673 /*
1674 * Reassemble all frames in receive queue and deliver them
1675 */
1676 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1677 /* This bit will tell us if it's the last fragment or not */
1678 more = skb->data[0] & 0x80;
1679
1680 /* Remove TTP header */
1681 skb_pull(skb, TTP_HEADER);
1682
1683 /* Add the length of the remaining data */
1684 self->rx_sdu_size += skb->len;
1685
1686 /*
1687 * If SAR is disabled, or user has requested no reassembly
1688 * of received fragments then we just deliver them
1689 * immediately. This can be requested by clients that
1690 * implements byte streams without any message boundaries
1691 */
1692 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1693 irttp_do_data_indication(self, skb);
1694 self->rx_sdu_size = 0;
1695
1696 continue;
1697 }
1698
1699 /* Check if this is a fragment, and not the last fragment */
1700 if (more) {
1701 /*
1702 * Queue the fragment if we still are within the
1703 * limits of the maximum size of the rx_sdu
1704 */
1705 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1706 IRDA_DEBUG(4, "%s(), queueing frag\n",
1707 __FUNCTION__);
1708 skb_queue_tail(&self->rx_fragments, skb);
1709 } else {
1710 /* Free the part of the SDU that is too big */
1711 dev_kfree_skb(skb);
1712 }
1713 continue;
1714 }
1715 /*
1716 * This is the last fragment, so time to reassemble!
1717 */
1718 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1719 (self->rx_max_sdu_size == TTP_SAR_UNBOUND))
1720 {
1721 /*
1722 * A little optimizing. Only queue the fragment if
1723 * there are other fragments. Since if this is the
1724 * last and only fragment, there is no need to
1725 * reassemble :-)
1726 */
1727 if (!skb_queue_empty(&self->rx_fragments)) {
1728 skb_queue_tail(&self->rx_fragments,
1729 skb);
1730
1731 skb = irttp_reassemble_skb(self);
1732 }
1733
1734 /* Now we can deliver the reassembled skb */
1735 irttp_do_data_indication(self, skb);
1736 } else {
1737 IRDA_DEBUG(1, "%s(), Truncated frame\n", __FUNCTION__);
1738
1739 /* Free the part of the SDU that is too big */
1740 dev_kfree_skb(skb);
1741
1742 /* Deliver only the valid but truncated part of SDU */
1743 skb = irttp_reassemble_skb(self);
1744
1745 irttp_do_data_indication(self, skb);
1746 }
1747 self->rx_sdu_size = 0;
1748 }
1749
1750 /*
1751 * It's not trivial to keep track of how many credits are available
1752 * by incrementing at each packet, because delivery may fail
1753 * (irttp_do_data_indication() may requeue the frame) and because
1754 * we need to take care of fragmentation.
1755 * We want the other side to send up to initial_credit packets.
1756 * We have some frames in our queues, and we have already allowed it
1757 * to send remote_credit.
1758 * No need to spinlock, write is atomic and self correcting...
1759 * Jean II
1760 */
1761 self->avail_credit = (self->initial_credit -
1762 (self->remote_credit +
1763 skb_queue_len(&self->rx_queue) +
1764 skb_queue_len(&self->rx_fragments)));
1765
1766 /* Do we have too much credits to send to peer ? */
1767 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1768 (self->avail_credit > 0)) {
1769 /* Send explicit credit frame */
1770 irttp_give_credit(self);
1771 /* Note : do *NOT* check if tx_queue is non-empty, that
1772 * will produce deadlocks. I repeat : send a credit frame
1773 * even if we have something to send in our Tx queue.
1774 * If we have credits, it means that our Tx queue is blocked.
1775 *
1776 * Let's suppose the peer can't keep up with our Tx. He will
1777 * flow control us by not sending us any credits, and we
1778 * will stop Tx and start accumulating credits here.
1779 * Up to the point where the peer will stop its Tx queue,
1780 * for lack of credits.
1781 * Let's assume the peer application is single threaded.
1782 * It will block on Tx and never consume any Rx buffer.
1783 * Deadlock. Guaranteed. - Jean II
1784 */
1785 }
1786
1787 /* Reset lock */
1788 self->rx_queue_lock = 0;
1789}
1790
1791#ifdef CONFIG_PROC_FS
1792struct irttp_iter_state {
1793 int id;
1794};
1795
1796static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
1797{
1798 struct irttp_iter_state *iter = seq->private;
1799 struct tsap_cb *self;
1800
1801 /* Protect our access to the tsap list */
1802 spin_lock_irq(&irttp->tsaps->hb_spinlock);
1803 iter->id = 0;
1804
1805 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1806 self != NULL;
1807 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
1808 if (iter->id == *pos)
1809 break;
1810 ++iter->id;
1811 }
1812
1813 return self;
1814}
1815
1816static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1817{
1818 struct irttp_iter_state *iter = seq->private;
1819
1820 ++*pos;
1821 ++iter->id;
1822 return (void *) hashbin_get_next(irttp->tsaps);
1823}
1824
1825static void irttp_seq_stop(struct seq_file *seq, void *v)
1826{
1827 spin_unlock_irq(&irttp->tsaps->hb_spinlock);
1828}
1829
1830static int irttp_seq_show(struct seq_file *seq, void *v)
1831{
1832 const struct irttp_iter_state *iter = seq->private;
1833 const struct tsap_cb *self = v;
1834
1835 seq_printf(seq, "TSAP %d, ", iter->id);
1836 seq_printf(seq, "stsap_sel: %02x, ",
1837 self->stsap_sel);
1838 seq_printf(seq, "dtsap_sel: %02x\n",
1839 self->dtsap_sel);
1840 seq_printf(seq, " connected: %s, ",
1841 self->connected? "TRUE":"FALSE");
1842 seq_printf(seq, "avail credit: %d, ",
1843 self->avail_credit);
1844 seq_printf(seq, "remote credit: %d, ",
1845 self->remote_credit);
1846 seq_printf(seq, "send credit: %d\n",
1847 self->send_credit);
1848 seq_printf(seq, " tx packets: %ld, ",
1849 self->stats.tx_packets);
1850 seq_printf(seq, "rx packets: %ld, ",
1851 self->stats.rx_packets);
1852 seq_printf(seq, "tx_queue len: %d ",
1853 skb_queue_len(&self->tx_queue));
1854 seq_printf(seq, "rx_queue len: %d\n",
1855 skb_queue_len(&self->rx_queue));
1856 seq_printf(seq, " tx_sdu_busy: %s, ",
1857 self->tx_sdu_busy? "TRUE":"FALSE");
1858 seq_printf(seq, "rx_sdu_busy: %s\n",
1859 self->rx_sdu_busy? "TRUE":"FALSE");
1860 seq_printf(seq, " max_seg_size: %d, ",
1861 self->max_seg_size);
1862 seq_printf(seq, "tx_max_sdu_size: %d, ",
1863 self->tx_max_sdu_size);
1864 seq_printf(seq, "rx_max_sdu_size: %d\n",
1865 self->rx_max_sdu_size);
1866
1867 seq_printf(seq, " Used by (%s)\n\n",
1868 self->notify.name);
1869 return 0;
1870}
1871
1872static struct seq_operations irttp_seq_ops = {
1873 .start = irttp_seq_start,
1874 .next = irttp_seq_next,
1875 .stop = irttp_seq_stop,
1876 .show = irttp_seq_show,
1877};
1878
1879static int irttp_seq_open(struct inode *inode, struct file *file)
1880{
1881 struct seq_file *seq;
1882 int rc = -ENOMEM;
1883 struct irttp_iter_state *s;
1884
1885 IRDA_ASSERT(irttp != NULL, return -EINVAL;);
1886
1887 s = kmalloc(sizeof(*s), GFP_KERNEL);
1888 if (!s)
1889 goto out;
1890
1891 rc = seq_open(file, &irttp_seq_ops);
1892 if (rc)
1893 goto out_kfree;
1894
1895 seq = file->private_data;
1896 seq->private = s;
1897 memset(s, 0, sizeof(*s));
1898out:
1899 return rc;
1900out_kfree:
1901 kfree(s);
1902 goto out;
1903}
1904
1905struct file_operations irttp_seq_fops = {
1906 .owner = THIS_MODULE,
1907 .open = irttp_seq_open,
1908 .read = seq_read,
1909 .llseek = seq_lseek,
1910 .release = seq_release_private,
1911};
1912
1913#endif /* PROC_FS */