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