blob: 3ef0b08b6bf5b703ecc82beb33f5b950ec88ffcb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090011 *
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090015 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * the License, or (at your option) any later version.
20 *
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021 * Neither Dag Brattli nor University of Tromsø admit liability nor
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090022 * provide warranty for any of this software. This material is
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * provided "AS-IS" and at no charge.
24 *
25 ********************************************************************/
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/skbuff.h>
28#include <linux/init.h>
Al Virod7fe0f22006-12-03 23:15:30 -050029#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040032#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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
Alexey Dobriyan8689c072005-09-24 16:55:17 -070043static struct irttp_cb *irttp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static void __irttp_close_tsap(struct tsap_cb *self);
46
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090047static int irttp_data_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct sk_buff *skb);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090049static int irttp_udata_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct sk_buff *skb);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090051static void irttp_disconnect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 LM_REASON reason, struct sk_buff *);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090053static void irttp_connect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct qos_info *qos, __u32 max_sdu_size,
55 __u8 header_size, struct sk_buff *skb);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090056static void irttp_connect_confirm(void *instance, void *sap,
57 struct qos_info *qos, __u32 max_sdu_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 __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);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090066static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 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};
Dragos Foianuaafee332013-07-17 12:25:38 +010078static pi_major_info_t pi_major_call_table[] = { { pi_minor_call_table, 2 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static 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{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070091 irttp = kzalloc(sizeof(struct irttp_cb), GFP_KERNEL);
Alexey Dobriyan8689c072005-09-24 16:55:17 -070092 if (irttp == NULL)
93 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 irttp->magic = TTP_MAGIC;
96
97 irttp->tsaps = hashbin_new(HB_LOCK);
98 if (!irttp->tsaps) {
Joe Perches6c910232014-11-11 13:37:30 -080099 net_err_ratelimited("%s: can't allocate IrTTP hashbin!\n",
100 __func__);
Alexey Dobriyan15166fadb2005-09-24 16:54:50 -0700101 kfree(irttp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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 */
Samuel Ortiz75a69ac2007-07-18 02:16:30 -0700114void irttp_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 /* Check for main structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 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
Joe Perches955a9d202014-11-11 14:44:57 -0800169 pr_debug("%s(instance=%p)\n", __func__, self);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
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 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800206static void irttp_flush_queues(struct tsap_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Dragos Foianuaafee332013-07-17 12:25:38 +0100208 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 IRDA_ASSERT(self != NULL, return;);
211 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
212
213 /* Deallocate frames waiting to be sent */
214 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
215 dev_kfree_skb(skb);
216
217 /* Deallocate received frames */
218 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
219 dev_kfree_skb(skb);
220
221 /* Deallocate received fragments */
222 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
223 dev_kfree_skb(skb);
224}
225
226/*
227 * Function irttp_reassemble (self)
228 *
229 * Makes a new (continuous) skb of all the fragments in the fragment
230 * queue
231 *
232 */
233static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
234{
235 struct sk_buff *skb, *frag;
236 int n = 0; /* Fragment index */
237
238 IRDA_ASSERT(self != NULL, return NULL;);
239 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
240
Joe Perches955a9d202014-11-11 14:44:57 -0800241 pr_debug("%s(), self->rx_sdu_size=%d\n", __func__,
242 self->rx_sdu_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
245 if (!skb)
246 return NULL;
247
248 /*
249 * Need to reserve space for TTP header in case this skb needs to
250 * be requeued in case delivery failes
251 */
252 skb_reserve(skb, TTP_HEADER);
253 skb_put(skb, self->rx_sdu_size);
254
255 /*
256 * Copy all fragments to a new buffer
257 */
258 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300259 skb_copy_to_linear_data_offset(skb, n, frag->data, frag->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 n += frag->len;
261
262 dev_kfree_skb(frag);
263 }
264
Joe Perches955a9d202014-11-11 14:44:57 -0800265 pr_debug("%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
266 __func__, n, self->rx_sdu_size, self->rx_max_sdu_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* Note : irttp_run_rx_queue() calculate self->rx_sdu_size
268 * by summing the size of all fragments, so we should always
269 * have n == self->rx_sdu_size, except in cases where we
270 * droped the last fragment (when self->rx_sdu_size exceed
271 * self->rx_max_sdu_size), where n < self->rx_sdu_size.
272 * Jean II */
273 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
274
275 /* Set the new length */
276 skb_trim(skb, n);
277
278 self->rx_sdu_size = 0;
279
280 return skb;
281}
282
283/*
284 * Function irttp_fragment_skb (skb)
285 *
286 * Fragments a frame and queues all the fragments for transmission
287 *
288 */
289static inline void irttp_fragment_skb(struct tsap_cb *self,
290 struct sk_buff *skb)
291{
292 struct sk_buff *frag;
293 __u8 *frame;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 IRDA_ASSERT(self != NULL, return;);
296 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
297 IRDA_ASSERT(skb != NULL, return;);
298
299 /*
300 * Split frame into a number of segments
301 */
302 while (skb->len > self->max_seg_size) {
Joe Perches955a9d202014-11-11 14:44:57 -0800303 pr_debug("%s(), fragmenting ...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Make new segment */
Samuel Ortiz485fb2c2006-07-21 14:50:41 -0700306 frag = alloc_skb(self->max_seg_size+self->max_header_size,
307 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!frag)
309 return;
310
311 skb_reserve(frag, self->max_header_size);
312
313 /* Copy data from the original skb into this fragment. */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300314 skb_copy_from_linear_data(skb, skb_put(frag, self->max_seg_size),
315 self->max_seg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Insert TTP header, with the more bit set */
318 frame = skb_push(frag, TTP_HEADER);
319 frame[0] = TTP_MORE;
320
321 /* Hide the copied data from the original skb */
322 skb_pull(skb, self->max_seg_size);
323
324 /* Queue fragment */
325 skb_queue_tail(&self->tx_queue, frag);
326 }
327 /* Queue what is left of the original skb */
Joe Perches955a9d202014-11-11 14:44:57 -0800328 pr_debug("%s(), queuing last segment\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 frame = skb_push(skb, TTP_HEADER);
331 frame[0] = 0x00; /* Clear more bit */
332
333 /* Queue fragment */
334 skb_queue_tail(&self->tx_queue, skb);
335}
336
337/*
338 * Function irttp_param_max_sdu_size (self, param)
339 *
340 * Handle the MaxSduSize parameter in the connect frames, this function
341 * will be called both when this parameter needs to be inserted into, and
342 * extracted from the connect frames
343 */
344static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
345 int get)
346{
347 struct tsap_cb *self;
348
Joe Perchesea110732011-06-13 16:21:26 +0000349 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 IRDA_ASSERT(self != NULL, return -1;);
352 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
353
354 if (get)
355 param->pv.i = self->tx_max_sdu_size;
356 else
357 self->tx_max_sdu_size = param->pv.i;
358
Joe Perches955a9d202014-11-11 14:44:57 -0800359 pr_debug("%s(), MaxSduSize=%d\n", __func__, param->pv.i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 return 0;
362}
363
364/*************************** CLIENT CALLS ***************************/
365/************************** LMP CALLBACKS **************************/
366/* Everything is happily mixed up. Waiting for next clean up - Jean II */
367
368/*
G. Liakhovetski93cce3d2007-07-02 22:56:57 -0700369 * Initialization, that has to be done on new tsap
370 * instance allocation and on duplication
371 */
372static void irttp_init_tsap(struct tsap_cb *tsap)
373{
374 spin_lock_init(&tsap->lock);
375 init_timer(&tsap->todo_timer);
376
377 skb_queue_head_init(&tsap->rx_queue);
378 skb_queue_head_init(&tsap->tx_queue);
379 skb_queue_head_init(&tsap->rx_fragments);
380}
381
382/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * Function irttp_open_tsap (stsap, notify)
384 *
385 * Create TSAP connection endpoint,
386 */
387struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
388{
389 struct tsap_cb *self;
390 struct lsap_cb *lsap;
391 notify_t ttp_notify;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
394
395 /* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
396 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
397 * JeanII */
Dragos Foianuaafee332013-07-17 12:25:38 +0100398 if ((stsap_sel != LSAP_ANY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
Joe Perches955a9d202014-11-11 14:44:57 -0800400 pr_debug("%s(), invalid tsap!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return NULL;
402 }
403
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700404 self = kzalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
Joe Perches955a9d202014-11-11 14:44:57 -0800405 if (self == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return NULL;
G. Liakhovetski93cce3d2007-07-02 22:56:57 -0700407
408 /* Initialize internal objects */
409 irttp_init_tsap(self);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* Initialise todo timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 self->todo_timer.data = (unsigned long) self;
413 self->todo_timer.function = &irttp_todo_expired;
414
415 /* Initialize callbacks for IrLMP to use */
416 irda_notify_init(&ttp_notify);
417 ttp_notify.connect_confirm = irttp_connect_confirm;
418 ttp_notify.connect_indication = irttp_connect_indication;
419 ttp_notify.disconnect_indication = irttp_disconnect_indication;
420 ttp_notify.data_indication = irttp_data_indication;
421 ttp_notify.udata_indication = irttp_udata_indication;
422 ttp_notify.flow_indication = irttp_flow_indication;
Dragos Foianuaafee332013-07-17 12:25:38 +0100423 if (notify->status_indication != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ttp_notify.status_indication = irttp_status_indication;
425 ttp_notify.instance = self;
426 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
427
428 self->magic = TTP_TSAP_MAGIC;
429 self->connected = FALSE;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * Create LSAP at IrLMP layer
433 */
434 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
435 if (lsap == NULL) {
Joe Perches955a9d202014-11-11 14:44:57 -0800436 pr_debug("%s: unable to allocate LSAP!!\n", __func__);
Tommi Rantalac3b2c252012-11-26 04:16:44 +0000437 __irttp_close_tsap(self);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return NULL;
439 }
440
441 /*
442 * If user specified LSAP_ANY as source TSAP selector, then IrLMP
443 * will replace it with whatever source selector which is free, so
444 * the stsap_sel we have might not be valid anymore
445 */
446 self->stsap_sel = lsap->slsap_sel;
Joe Perches955a9d202014-11-11 14:44:57 -0800447 pr_debug("%s(), stsap_sel=%02x\n", __func__, self->stsap_sel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 self->notify = *notify;
450 self->lsap = lsap;
451
452 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
453
454 if (credit > TTP_RX_MAX_CREDIT)
455 self->initial_credit = TTP_RX_MAX_CREDIT;
456 else
457 self->initial_credit = credit;
458
459 return self;
460}
461EXPORT_SYMBOL(irttp_open_tsap);
462
463/*
464 * Function irttp_close (handle)
465 *
466 * Remove an instance of a TSAP. This function should only deal with the
467 * deallocation of the TSAP, and resetting of the TSAPs values;
468 *
469 */
470static void __irttp_close_tsap(struct tsap_cb *self)
471{
472 /* First make sure we're connected. */
473 IRDA_ASSERT(self != NULL, return;);
474 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
475
476 irttp_flush_queues(self);
477
478 del_timer(&self->todo_timer);
479
480 /* This one won't be cleaned up if we are disconnect_pend + close_pend
481 * and we receive a disconnect_indication */
482 if (self->disconnect_skb)
483 dev_kfree_skb(self->disconnect_skb);
484
485 self->connected = FALSE;
486 self->magic = ~TTP_TSAP_MAGIC;
487
488 kfree(self);
489}
490
491/*
492 * Function irttp_close (self)
493 *
494 * Remove TSAP from list of all TSAPs and then deallocate all resources
495 * associated with this TSAP
496 *
497 * Note : because we *free* the tsap structure, it is the responsibility
498 * of the caller to make sure we are called only once and to deal with
499 * possible race conditions. - Jean II
500 */
501int irttp_close_tsap(struct tsap_cb *self)
502{
503 struct tsap_cb *tsap;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 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)) {
Joe Perches6c910232014-11-11 13:37:30 -0800512 net_warn_ratelimited("%s: TSAP still connected!\n",
513 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 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{
Wolfram Sang925e2772010-11-16 09:40:02 +0000546 int ret;
Wolfram Sang4c62ab9c2010-11-16 09:50:47 -0800547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 IRDA_ASSERT(self != NULL, return -1;);
549 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
550 IRDA_ASSERT(skb != NULL, return -1;);
551
Wolfram Sang4c62ab9c2010-11-16 09:50:47 -0800552 /* Take shortcut on zero byte packets */
553 if (skb->len == 0) {
554 ret = 0;
555 goto err;
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Check that nothing bad happens */
Wolfram Sang4c62ab9c2010-11-16 09:50:47 -0800559 if (!self->connected) {
Joe Perches6c910232014-11-11 13:37:30 -0800560 net_warn_ratelimited("%s(), Not connected\n", __func__);
Wolfram Sang925e2772010-11-16 09:40:02 +0000561 ret = -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto err;
563 }
564
565 if (skb->len > self->max_seg_size) {
Joe Perches6c910232014-11-11 13:37:30 -0800566 net_err_ratelimited("%s(), UData is too large for IrLAP!\n",
567 __func__);
Wolfram Sang925e2772010-11-16 09:40:02 +0000568 ret = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto err;
570 }
571
572 irlmp_udata_request(self->lsap, skb);
573 self->stats.tx_packets++;
574
575 return 0;
576
577err:
578 dev_kfree_skb(skb);
Wolfram Sang4c62ab9c2010-11-16 09:50:47 -0800579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581EXPORT_SYMBOL(irttp_udata_request);
582
583
584/*
585 * Function irttp_data_request (handle, skb)
586 *
587 * Queue frame for transmission. If SAR is enabled, fragement the frame
588 * and queue the fragments for transmission
589 */
590int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
591{
592 __u8 *frame;
593 int ret;
594
595 IRDA_ASSERT(self != NULL, return -1;);
596 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
597 IRDA_ASSERT(skb != NULL, return -1;);
598
Joe Perches955a9d202014-11-11 14:44:57 -0800599 pr_debug("%s() : queue len = %d\n", __func__,
600 skb_queue_len(&self->tx_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Wolfram Sang4c62ab9c2010-11-16 09:50:47 -0800602 /* Take shortcut on zero byte packets */
603 if (skb->len == 0) {
604 ret = 0;
605 goto err;
606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Check that nothing bad happens */
Wolfram Sang4c62ab9c2010-11-16 09:50:47 -0800609 if (!self->connected) {
Joe Perches6c910232014-11-11 13:37:30 -0800610 net_warn_ratelimited("%s: Not connected\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ret = -ENOTCONN;
612 goto err;
613 }
614
615 /*
616 * Check if SAR is disabled, and the frame is larger than what fits
617 * inside an IrLAP frame
618 */
619 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
Joe Perches6c910232014-11-11 13:37:30 -0800620 net_err_ratelimited("%s: SAR disabled, and data is too large for IrLAP!\n",
621 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 ret = -EMSGSIZE;
623 goto err;
624 }
625
626 /*
627 * Check if SAR is enabled, and the frame is larger than the
628 * TxMaxSduSize
629 */
630 if ((self->tx_max_sdu_size != 0) &&
631 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
Dragos Foianuaafee332013-07-17 12:25:38 +0100632 (skb->len > self->tx_max_sdu_size)) {
Joe Perches6c910232014-11-11 13:37:30 -0800633 net_err_ratelimited("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
634 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 ret = -EMSGSIZE;
636 goto err;
637 }
638 /*
639 * Check if transmit queue is full
640 */
641 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
642 /*
643 * Give it a chance to empty itself
644 */
645 irttp_run_tx_queue(self);
646
647 /* Drop packet. This error code should trigger the caller
648 * to resend the data in the client code - Jean II */
649 ret = -ENOBUFS;
650 goto err;
651 }
652
653 /* Queue frame, or queue frame segments */
654 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
655 /* Queue frame */
656 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
657 frame = skb_push(skb, TTP_HEADER);
658 frame[0] = 0x00; /* Clear more bit */
659
660 skb_queue_tail(&self->tx_queue, skb);
661 } else {
662 /*
663 * Fragment the frame, this function will also queue the
664 * fragments, we don't care about the fact the transmit
665 * queue may be overfilled by all the segments for a little
666 * while
667 */
668 irttp_fragment_skb(self, skb);
669 }
670
671 /* Check if we can accept more data from client */
672 if ((!self->tx_sdu_busy) &&
673 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
674 /* Tx queue filling up, so stop client. */
675 if (self->notify.flow_indication) {
676 self->notify.flow_indication(self->notify.instance,
677 self, FLOW_STOP);
678 }
679 /* self->tx_sdu_busy is the state of the client.
680 * Update state after notifying client to avoid
681 * race condition with irttp_flow_indication().
682 * If the queue empty itself after our test but before
683 * we set the flag, we will fix ourselves below in
684 * irttp_run_tx_queue().
685 * Jean II */
686 self->tx_sdu_busy = TRUE;
687 }
688
689 /* Try to make some progress */
690 irttp_run_tx_queue(self);
691
692 return 0;
693
694err:
695 dev_kfree_skb(skb);
696 return ret;
697}
698EXPORT_SYMBOL(irttp_data_request);
699
700/*
701 * Function irttp_run_tx_queue (self)
702 *
703 * Transmit packets queued for transmission (if possible)
704 *
705 */
706static void irttp_run_tx_queue(struct tsap_cb *self)
707{
708 struct sk_buff *skb;
709 unsigned long flags;
710 int n;
711
Joe Perches955a9d202014-11-11 14:44:57 -0800712 pr_debug("%s() : send_credit = %d, queue_len = %d\n",
713 __func__,
714 self->send_credit, skb_queue_len(&self->tx_queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Get exclusive access to the tx queue, otherwise don't touch it */
717 if (irda_lock(&self->tx_queue_lock) == FALSE)
718 return;
719
720 /* Try to send out frames as long as we have credits
721 * and as long as LAP is not full. If LAP is full, it will
722 * poll us through irttp_flow_indication() - Jean II */
723 while ((self->send_credit > 0) &&
724 (!irlmp_lap_tx_queue_full(self->lsap)) &&
Dragos Foianuaafee332013-07-17 12:25:38 +0100725 (skb = skb_dequeue(&self->tx_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Since we can transmit and receive frames concurrently,
728 * the code below is a critical region and we must assure that
729 * nobody messes with the credits while we update them.
730 */
731 spin_lock_irqsave(&self->lock, flags);
732
733 n = self->avail_credit;
734 self->avail_credit = 0;
735
736 /* Only room for 127 credits in frame */
737 if (n > 127) {
738 self->avail_credit = n-127;
739 n = 127;
740 }
741 self->remote_credit += n;
742 self->send_credit--;
743
744 spin_unlock_irqrestore(&self->lock, flags);
745
746 /*
747 * More bit must be set by the data_request() or fragment()
748 * functions
749 */
750 skb->data[0] |= (n & 0x7f);
751
752 /* Detach from socket.
753 * The current skb has a reference to the socket that sent
754 * it (skb->sk). When we pass it to IrLMP, the skb will be
755 * stored in in IrLAP (self->wx_list). When we are within
756 * IrLAP, we lose the notion of socket, so we should not
757 * have a reference to a socket. So, we drop it here.
758 *
759 * Why does it matter ?
760 * When the skb is freed (kfree_skb), if it is associated
761 * with a socket, it release buffer space on the socket
762 * (through sock_wfree() and sock_def_write_space()).
763 * If the socket no longer exist, we may crash. Hard.
764 * When we close a socket, we make sure that associated packets
765 * in IrTTP are freed. However, we have no way to cancel
766 * the packet that we have passed to IrLAP. So, if a packet
767 * remains in IrLAP (retry on the link or else) after we
768 * close the socket, we are dead !
769 * Jean II */
770 if (skb->sk != NULL) {
771 /* IrSOCK application, IrOBEX, ... */
772 skb_orphan(skb);
773 }
774 /* IrCOMM over IrTTP, IrLAN, ... */
775
776 /* Pass the skb to IrLMP - done */
777 irlmp_data_request(self->lsap, skb);
778 self->stats.tx_packets++;
779 }
780
781 /* Check if we can accept more frames from client.
782 * We don't want to wait until the todo timer to do that, and we
783 * can't use tasklets (grr...), so we are obliged to give control
784 * to client. That's ok, this test will be true not too often
785 * (max once per LAP window) and we are called from places
786 * where we can spend a bit of time doing stuff. - Jean II */
787 if ((self->tx_sdu_busy) &&
788 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
Dragos Foianuaafee332013-07-17 12:25:38 +0100789 (!self->close_pend)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (self->notify.flow_indication)
791 self->notify.flow_indication(self->notify.instance,
792 self, FLOW_START);
793
794 /* self->tx_sdu_busy is the state of the client.
795 * We don't really have a race here, but it's always safer
796 * to update our state after the client - Jean II */
797 self->tx_sdu_busy = FALSE;
798 }
799
800 /* Reset lock */
801 self->tx_queue_lock = 0;
802}
803
804/*
805 * Function irttp_give_credit (self)
806 *
807 * Send a dataless flowdata TTP-PDU and give available credit to peer
808 * TSAP
809 */
810static inline void irttp_give_credit(struct tsap_cb *self)
811{
812 struct sk_buff *tx_skb = NULL;
813 unsigned long flags;
814 int n;
815
816 IRDA_ASSERT(self != NULL, return;);
817 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
818
Joe Perches955a9d202014-11-11 14:44:57 -0800819 pr_debug("%s() send=%d,avail=%d,remote=%d\n",
820 __func__,
821 self->send_credit, self->avail_credit, self->remote_credit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* Give credit to peer */
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700824 tx_skb = alloc_skb(TTP_MAX_HEADER, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!tx_skb)
826 return;
827
828 /* Reserve space for LMP, and LAP header */
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700829 skb_reserve(tx_skb, LMP_MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
832 * Since we can transmit and receive frames concurrently,
833 * the code below is a critical region and we must assure that
834 * nobody messes with the credits while we update them.
835 */
836 spin_lock_irqsave(&self->lock, flags);
837
838 n = self->avail_credit;
839 self->avail_credit = 0;
840
841 /* Only space for 127 credits in frame */
842 if (n > 127) {
843 self->avail_credit = n - 127;
844 n = 127;
845 }
846 self->remote_credit += n;
847
848 spin_unlock_irqrestore(&self->lock, flags);
849
850 skb_put(tx_skb, 1);
851 tx_skb->data[0] = (__u8) (n & 0x7f);
852
853 irlmp_data_request(self->lsap, tx_skb);
854 self->stats.tx_packets++;
855}
856
857/*
858 * Function irttp_udata_indication (instance, sap, skb)
859 *
860 * Received some unit-data (unreliable)
861 *
862 */
863static int irttp_udata_indication(void *instance, void *sap,
864 struct sk_buff *skb)
865{
866 struct tsap_cb *self;
867 int err;
868
Joe Perchesea110732011-06-13 16:21:26 +0000869 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 IRDA_ASSERT(self != NULL, return -1;);
872 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
873 IRDA_ASSERT(skb != NULL, return -1;);
874
875 self->stats.rx_packets++;
876
877 /* Just pass data to layer above */
878 if (self->notify.udata_indication) {
879 err = self->notify.udata_indication(self->notify.instance,
Dragos Foianuaafee332013-07-17 12:25:38 +0100880 self, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Same comment as in irttp_do_data_indication() */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900882 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 0;
884 }
885 /* Either no handler, or handler returns an error */
886 dev_kfree_skb(skb);
887
888 return 0;
889}
890
891/*
892 * Function irttp_data_indication (instance, sap, skb)
893 *
894 * Receive segment from IrLMP.
895 *
896 */
897static int irttp_data_indication(void *instance, void *sap,
898 struct sk_buff *skb)
899{
900 struct tsap_cb *self;
901 unsigned long flags;
902 int n;
903
Joe Perchesea110732011-06-13 16:21:26 +0000904 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 n = skb->data[0] & 0x7f; /* Extract the credits */
907
908 self->stats.rx_packets++;
909
910 /* Deal with inbound credit
911 * Since we can transmit and receive frames concurrently,
912 * the code below is a critical region and we must assure that
913 * nobody messes with the credits while we update them.
914 */
915 spin_lock_irqsave(&self->lock, flags);
916 self->send_credit += n;
917 if (skb->len > 1)
918 self->remote_credit--;
919 spin_unlock_irqrestore(&self->lock, flags);
920
921 /*
922 * Data or dataless packet? Dataless frames contains only the
923 * TTP_HEADER.
924 */
925 if (skb->len > 1) {
926 /*
927 * We don't remove the TTP header, since we must preserve the
928 * more bit, so the defragment routing knows what to do
929 */
930 skb_queue_tail(&self->rx_queue, skb);
931 } else {
932 /* Dataless flowdata TTP-PDU */
933 dev_kfree_skb(skb);
934 }
935
936
937 /* Push data to the higher layer.
938 * We do it synchronously because running the todo timer for each
939 * receive packet would be too much overhead and latency.
940 * By passing control to the higher layer, we run the risk that
941 * it may take time or grab a lock. Most often, the higher layer
942 * will only put packet in a queue.
943 * Anyway, packets are only dripping through the IrDA, so we can
944 * have time before the next packet.
945 * Further, we are run from NET_BH, so the worse that can happen is
946 * us missing the optimal time to send back the PF bit in LAP.
947 * Jean II */
948 irttp_run_rx_queue(self);
949
950 /* We now give credits to peer in irttp_run_rx_queue().
951 * We need to send credit *NOW*, otherwise we are going
952 * to miss the next Tx window. The todo timer may take
953 * a while before it's run... - Jean II */
954
955 /*
956 * If the peer device has given us some credits and we didn't have
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900957 * anyone from before, then we need to shedule the tx queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * We need to do that because our Tx have stopped (so we may not
959 * get any LAP flow indication) and the user may be stopped as
960 * well. - Jean II
961 */
962 if (self->send_credit == n) {
963 /* Restart pushing stuff to LAP */
964 irttp_run_tx_queue(self);
965 /* Note : we don't want to schedule the todo timer
966 * because it has horrible latency. No tasklets
967 * because the tasklet API is broken. - Jean II */
968 }
969
970 return 0;
971}
972
973/*
974 * Function irttp_status_indication (self, reason)
975 *
976 * Status_indication, just pass to the higher layer...
977 *
978 */
979static void irttp_status_indication(void *instance,
980 LINK_STATUS link, LOCK_STATUS lock)
981{
982 struct tsap_cb *self;
983
Joe Perchesea110732011-06-13 16:21:26 +0000984 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 IRDA_ASSERT(self != NULL, return;);
987 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
988
989 /* Check if client has already closed the TSAP and gone away */
990 if (self->close_pend)
991 return;
992
993 /*
994 * Inform service user if he has requested it
995 */
996 if (self->notify.status_indication != NULL)
997 self->notify.status_indication(self->notify.instance,
998 link, lock);
999 else
Joe Perches955a9d202014-11-11 14:44:57 -08001000 pr_debug("%s(), no handler\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003/*
1004 * Function irttp_flow_indication (self, reason)
1005 *
1006 * Flow_indication : IrLAP tells us to send more data.
1007 *
1008 */
1009static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
1010{
1011 struct tsap_cb *self;
1012
Joe Perchesea110732011-06-13 16:21:26 +00001013 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 IRDA_ASSERT(self != NULL, return;);
1016 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1017
Joe Perches955a9d202014-11-11 14:44:57 -08001018 pr_debug("%s(instance=%p)\n", __func__, self);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /* We are "polled" directly from LAP, and the LAP want to fill
1021 * its Tx window. We want to do our best to send it data, so that
1022 * we maximise the window. On the other hand, we want to limit the
1023 * amount of work here so that LAP doesn't hang forever waiting
1024 * for packets. - Jean II */
1025
1026 /* Try to send some packets. Currently, LAP calls us every time
1027 * there is one free slot, so we will send only one packet.
1028 * This allow the scheduler to do its round robin - Jean II */
1029 irttp_run_tx_queue(self);
1030
1031 /* Note regarding the interraction with higher layer.
1032 * irttp_run_tx_queue() may call the client when its queue
1033 * start to empty, via notify.flow_indication(). Initially.
1034 * I wanted this to happen in a tasklet, to avoid client
1035 * grabbing the CPU, but we can't use tasklets safely. And timer
1036 * is definitely too slow.
1037 * This will happen only once per LAP window, and usually at
1038 * the third packet (unless window is smaller). LAP is still
1039 * doing mtt and sending first packet so it's sort of OK
1040 * to do that. Jean II */
1041
1042 /* If we need to send disconnect. try to do it now */
Dragos Foianuaafee332013-07-17 12:25:38 +01001043 if (self->disconnect_pend)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 irttp_start_todo_timer(self, 0);
1045}
1046
1047/*
1048 * Function irttp_flow_request (self, command)
1049 *
1050 * This function could be used by the upper layers to tell IrTTP to stop
1051 * delivering frames if the receive queues are starting to get full, or
1052 * to tell IrTTP to start delivering frames again.
1053 */
1054void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 IRDA_ASSERT(self != NULL, return;);
1057 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1058
1059 switch (flow) {
1060 case FLOW_STOP:
Joe Perches955a9d202014-11-11 14:44:57 -08001061 pr_debug("%s(), flow stop\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 self->rx_sdu_busy = TRUE;
1063 break;
1064 case FLOW_START:
Joe Perches955a9d202014-11-11 14:44:57 -08001065 pr_debug("%s(), flow start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 self->rx_sdu_busy = FALSE;
1067
1068 /* Client say he can accept more data, try to free our
1069 * queues ASAP - Jean II */
1070 irttp_run_rx_queue(self);
1071
1072 break;
1073 default:
Joe Perches955a9d202014-11-11 14:44:57 -08001074 pr_debug("%s(), Unknown flow command!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076}
1077EXPORT_SYMBOL(irttp_flow_request);
1078
1079/*
1080 * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
1081 *
1082 * Try to connect to remote destination TSAP selector
1083 *
1084 */
1085int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1086 __u32 saddr, __u32 daddr,
1087 struct qos_info *qos, __u32 max_sdu_size,
1088 struct sk_buff *userdata)
1089{
1090 struct sk_buff *tx_skb;
1091 __u8 *frame;
1092 __u8 n;
1093
Joe Perches955a9d202014-11-11 14:44:57 -08001094 pr_debug("%s(), max_sdu_size=%d\n", __func__, max_sdu_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 IRDA_ASSERT(self != NULL, return -EBADR;);
1097 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1098
1099 if (self->connected) {
Dragos Foianuaafee332013-07-17 12:25:38 +01001100 if (userdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 dev_kfree_skb(userdata);
1102 return -EISCONN;
1103 }
1104
1105 /* Any userdata supplied? */
1106 if (userdata == NULL) {
Samuel Ortiz1b0fee72006-09-27 20:06:44 -07001107 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1108 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!tx_skb)
1110 return -ENOMEM;
1111
1112 /* Reserve space for MUX_CONTROL and LAP header */
Jeet Chaudhurie694ba42006-12-06 20:08:45 -08001113 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 } else {
1115 tx_skb = userdata;
1116 /*
1117 * Check that the client has reserved enough space for
1118 * headers
1119 */
1120 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
Dragos Foianuaafee332013-07-17 12:25:38 +01001121 { dev_kfree_skb(userdata); return -1; });
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
1124 /* Initialize connection parameters */
1125 self->connected = FALSE;
1126 self->avail_credit = 0;
1127 self->rx_max_sdu_size = max_sdu_size;
1128 self->rx_sdu_size = 0;
1129 self->rx_sdu_busy = FALSE;
1130 self->dtsap_sel = dtsap_sel;
1131
1132 n = self->initial_credit;
1133
1134 self->remote_credit = 0;
1135 self->send_credit = 0;
1136
1137 /*
1138 * Give away max 127 credits for now
1139 */
1140 if (n > 127) {
Dragos Foianuaafee332013-07-17 12:25:38 +01001141 self->avail_credit = n - 127;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 n = 127;
1143 }
1144
1145 self->remote_credit = n;
1146
1147 /* SAR enabled? */
1148 if (max_sdu_size > 0) {
1149 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
Dragos Foianuaafee332013-07-17 12:25:38 +01001150 { dev_kfree_skb(tx_skb); return -1; });
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* Insert SAR parameters */
Dragos Foianuaafee332013-07-17 12:25:38 +01001153 frame = skb_push(tx_skb, TTP_HEADER + TTP_SAR_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 frame[0] = TTP_PARAMETERS | n;
1156 frame[1] = 0x04; /* Length */
1157 frame[2] = 0x01; /* MaxSduSize */
1158 frame[3] = 0x02; /* Value length */
1159
1160 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
Al Viro448c31a2006-11-14 20:47:46 -08001161 (__be16 *)(frame+4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else {
1163 /* Insert plain TTP header */
1164 frame = skb_push(tx_skb, TTP_HEADER);
1165
1166 /* Insert initial credit in frame */
1167 frame[0] = n & 0x7f;
1168 }
1169
1170 /* Connect with IrLMP. No QoS parameters for now */
1171 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1172 tx_skb);
1173}
1174EXPORT_SYMBOL(irttp_connect_request);
1175
1176/*
1177 * Function irttp_connect_confirm (handle, qos, skb)
1178 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001179 * Service user confirms TSAP connection with peer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 *
1181 */
1182static void irttp_connect_confirm(void *instance, void *sap,
1183 struct qos_info *qos, __u32 max_seg_size,
1184 __u8 max_header_size, struct sk_buff *skb)
1185{
1186 struct tsap_cb *self;
1187 int parameters;
1188 int ret;
1189 __u8 plen;
1190 __u8 n;
1191
Joe Perchesea110732011-06-13 16:21:26 +00001192 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 IRDA_ASSERT(self != NULL, return;);
1195 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1196 IRDA_ASSERT(skb != NULL, return;);
1197
1198 self->max_seg_size = max_seg_size - TTP_HEADER;
1199 self->max_header_size = max_header_size + TTP_HEADER;
1200
1201 /*
1202 * Check if we have got some QoS parameters back! This should be the
1203 * negotiated QoS for the link.
1204 */
1205 if (qos) {
Joe Perches955a9d202014-11-11 14:44:57 -08001206 pr_debug("IrTTP, Negotiated BAUD_RATE: %02x\n",
1207 qos->baud_rate.bits);
1208 pr_debug("IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1209 qos->baud_rate.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
1212 n = skb->data[0] & 0x7f;
1213
Joe Perches955a9d202014-11-11 14:44:57 -08001214 pr_debug("%s(), Initial send_credit=%d\n", __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 self->send_credit = n;
1217 self->tx_max_sdu_size = 0;
1218 self->connected = TRUE;
1219
1220 parameters = skb->data[0] & 0x80;
1221
1222 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1223 skb_pull(skb, TTP_HEADER);
1224
1225 if (parameters) {
1226 plen = skb->data[0];
1227
1228 ret = irda_param_extract_all(self, skb->data+1,
1229 IRDA_MIN(skb->len-1, plen),
1230 &param_info);
1231
1232 /* Any errors in the parameter list? */
1233 if (ret < 0) {
Joe Perches6c910232014-11-11 13:37:30 -08001234 net_warn_ratelimited("%s: error extracting parameters\n",
1235 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 dev_kfree_skb(skb);
1237
1238 /* Do not accept this connection attempt */
1239 return;
1240 }
1241 /* Remove parameters */
1242 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1243 }
1244
Joe Perches955a9d202014-11-11 14:44:57 -08001245 pr_debug("%s() send=%d,avail=%d,remote=%d\n", __func__,
1246 self->send_credit, self->avail_credit, self->remote_credit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Joe Perches955a9d202014-11-11 14:44:57 -08001248 pr_debug("%s(), MaxSduSize=%d\n", __func__,
1249 self->tx_max_sdu_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 if (self->notify.connect_confirm) {
1252 self->notify.connect_confirm(self->notify.instance, self, qos,
1253 self->tx_max_sdu_size,
1254 self->max_header_size, skb);
1255 } else
1256 dev_kfree_skb(skb);
1257}
1258
1259/*
1260 * Function irttp_connect_indication (handle, skb)
1261 *
1262 * Some other device is connecting to this TSAP
1263 *
1264 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001265static void irttp_connect_indication(void *instance, void *sap,
1266 struct qos_info *qos, __u32 max_seg_size, __u8 max_header_size,
1267 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct tsap_cb *self;
1270 struct lsap_cb *lsap;
1271 int parameters;
1272 int ret;
1273 __u8 plen;
1274 __u8 n;
1275
Joe Perchesea110732011-06-13 16:21:26 +00001276 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 IRDA_ASSERT(self != NULL, return;);
1279 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1280 IRDA_ASSERT(skb != NULL, return;);
1281
Joe Perchesea110732011-06-13 16:21:26 +00001282 lsap = sap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 self->max_seg_size = max_seg_size - TTP_HEADER;
1285 self->max_header_size = max_header_size+TTP_HEADER;
1286
Joe Perches955a9d202014-11-11 14:44:57 -08001287 pr_debug("%s(), TSAP sel=%02x\n", __func__, self->stsap_sel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /* Need to update dtsap_sel if its equal to LSAP_ANY */
1290 self->dtsap_sel = lsap->dlsap_sel;
1291
1292 n = skb->data[0] & 0x7f;
1293
1294 self->send_credit = n;
1295 self->tx_max_sdu_size = 0;
1296
1297 parameters = skb->data[0] & 0x80;
1298
1299 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1300 skb_pull(skb, TTP_HEADER);
1301
1302 if (parameters) {
1303 plen = skb->data[0];
1304
1305 ret = irda_param_extract_all(self, skb->data+1,
1306 IRDA_MIN(skb->len-1, plen),
1307 &param_info);
1308
1309 /* Any errors in the parameter list? */
1310 if (ret < 0) {
Joe Perches6c910232014-11-11 13:37:30 -08001311 net_warn_ratelimited("%s: error extracting parameters\n",
1312 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 dev_kfree_skb(skb);
1314
1315 /* Do not accept this connection attempt */
1316 return;
1317 }
1318
1319 /* Remove parameters */
1320 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1321 }
1322
1323 if (self->notify.connect_indication) {
1324 self->notify.connect_indication(self->notify.instance, self,
1325 qos, self->tx_max_sdu_size,
1326 self->max_header_size, skb);
1327 } else
1328 dev_kfree_skb(skb);
1329}
1330
1331/*
1332 * Function irttp_connect_response (handle, userdata)
1333 *
1334 * Service user is accepting the connection, just pass it down to
1335 * IrLMP!
1336 *
1337 */
1338int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1339 struct sk_buff *userdata)
1340{
1341 struct sk_buff *tx_skb;
1342 __u8 *frame;
1343 int ret;
1344 __u8 n;
1345
1346 IRDA_ASSERT(self != NULL, return -1;);
1347 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1348
Joe Perches955a9d202014-11-11 14:44:57 -08001349 pr_debug("%s(), Source TSAP selector=%02x\n", __func__,
1350 self->stsap_sel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* Any userdata supplied? */
1353 if (userdata == NULL) {
Samuel Ortiz1b0fee72006-09-27 20:06:44 -07001354 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1355 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (!tx_skb)
1357 return -ENOMEM;
1358
1359 /* Reserve space for MUX_CONTROL and LAP header */
Jeet Chaudhurie694ba42006-12-06 20:08:45 -08001360 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 } else {
1362 tx_skb = userdata;
1363 /*
1364 * Check that the client has reserved enough space for
1365 * headers
1366 */
1367 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
Dragos Foianuaafee332013-07-17 12:25:38 +01001368 { dev_kfree_skb(userdata); return -1; });
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 self->avail_credit = 0;
1372 self->remote_credit = 0;
1373 self->rx_max_sdu_size = max_sdu_size;
1374 self->rx_sdu_size = 0;
1375 self->rx_sdu_busy = FALSE;
1376
1377 n = self->initial_credit;
1378
1379 /* Frame has only space for max 127 credits (7 bits) */
1380 if (n > 127) {
1381 self->avail_credit = n - 127;
1382 n = 127;
1383 }
1384
1385 self->remote_credit = n;
1386 self->connected = TRUE;
1387
1388 /* SAR enabled? */
1389 if (max_sdu_size > 0) {
1390 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
Dragos Foianuaafee332013-07-17 12:25:38 +01001391 { dev_kfree_skb(tx_skb); return -1; });
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* Insert TTP header with SAR parameters */
Dragos Foianuaafee332013-07-17 12:25:38 +01001394 frame = skb_push(tx_skb, TTP_HEADER + TTP_SAR_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 frame[0] = TTP_PARAMETERS | n;
1397 frame[1] = 0x04; /* Length */
1398
1399 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */
1400/* TTP_SAR_HEADER, &param_info) */
1401
1402 frame[2] = 0x01; /* MaxSduSize */
1403 frame[3] = 0x02; /* Value length */
1404
1405 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
Al Viro448c31a2006-11-14 20:47:46 -08001406 (__be16 *)(frame+4));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 } else {
1408 /* Insert TTP header */
1409 frame = skb_push(tx_skb, TTP_HEADER);
1410
1411 frame[0] = n & 0x7f;
1412 }
1413
1414 ret = irlmp_connect_response(self->lsap, tx_skb);
1415
1416 return ret;
1417}
1418EXPORT_SYMBOL(irttp_connect_response);
1419
1420/*
1421 * Function irttp_dup (self, instance)
1422 *
1423 * Duplicate TSAP, can be used by servers to confirm a connection on a
1424 * new TSAP so it can keep listening on the old one.
1425 */
1426struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1427{
1428 struct tsap_cb *new;
1429 unsigned long flags;
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* Protect our access to the old tsap instance */
1432 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
1433
1434 /* Find the old instance */
1435 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
Joe Perches955a9d202014-11-11 14:44:57 -08001436 pr_debug("%s(), unable to find TSAP\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1438 return NULL;
1439 }
1440
1441 /* Allocate a new instance */
Thomas Meyer8524b002011-11-17 12:43:40 +00001442 new = kmemdup(orig, sizeof(struct tsap_cb), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (!new) {
Joe Perches955a9d202014-11-11 14:44:57 -08001444 pr_debug("%s(), unable to kmalloc\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1446 return NULL;
1447 }
Deepak Saxena0cbb0a72009-07-27 10:49:44 -07001448 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /* We don't need the old instance any more */
1451 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1452
1453 /* Try to dup the LSAP (may fail if we were too slow) */
1454 new->lsap = irlmp_dup(orig->lsap, new);
1455 if (!new->lsap) {
Joe Perches955a9d202014-11-11 14:44:57 -08001456 pr_debug("%s(), dup failed!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 kfree(new);
1458 return NULL;
1459 }
1460
1461 /* Not everything should be copied */
1462 new->notify.instance = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
G. Liakhovetski93cce3d2007-07-02 22:56:57 -07001464 /* Initialize internal objects */
1465 irttp_init_tsap(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 /* This is locked */
1468 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
1469
1470 return new;
1471}
1472EXPORT_SYMBOL(irttp_dup);
1473
1474/*
1475 * Function irttp_disconnect_request (self)
1476 *
1477 * Close this connection please! If priority is high, the queued data
1478 * segments, if any, will be deallocated first
1479 *
1480 */
1481int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1482 int priority)
1483{
1484 int ret;
1485
1486 IRDA_ASSERT(self != NULL, return -1;);
1487 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1488
1489 /* Already disconnected? */
1490 if (!self->connected) {
Joe Perches955a9d202014-11-11 14:44:57 -08001491 pr_debug("%s(), already disconnected!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (userdata)
1493 dev_kfree_skb(userdata);
1494 return -1;
1495 }
1496
1497 /* Disconnect already pending ?
1498 * We need to use an atomic operation to prevent reentry. This
1499 * function may be called from various context, like user, timer
1500 * for following a disconnect_indication() (i.e. net_bh).
1501 * Jean II */
Dragos Foianuaafee332013-07-17 12:25:38 +01001502 if (test_and_set_bit(0, &self->disconnect_pend)) {
Joe Perches955a9d202014-11-11 14:44:57 -08001503 pr_debug("%s(), disconnect already pending\n",
1504 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (userdata)
1506 dev_kfree_skb(userdata);
1507
1508 /* Try to make some progress */
1509 irttp_run_tx_queue(self);
1510 return -1;
1511 }
1512
1513 /*
1514 * Check if there is still data segments in the transmit queue
1515 */
David S. Millerb03efcf2005-07-08 14:57:23 -07001516 if (!skb_queue_empty(&self->tx_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (priority == P_HIGH) {
1518 /*
1519 * No need to send the queued data, if we are
1520 * disconnecting right now since the data will
1521 * not have any usable connection to be sent on
1522 */
Joe Perches955a9d202014-11-11 14:44:57 -08001523 pr_debug("%s(): High priority!!()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 irttp_flush_queues(self);
1525 } else if (priority == P_NORMAL) {
1526 /*
1527 * Must delay disconnect until after all data segments
1528 * have been sent and the tx_queue is empty
1529 */
1530 /* We'll reuse this one later for the disconnect */
1531 self->disconnect_skb = userdata; /* May be NULL */
1532
1533 irttp_run_tx_queue(self);
1534
1535 irttp_start_todo_timer(self, HZ/10);
1536 return -1;
1537 }
1538 }
1539 /* Note : we don't need to check if self->rx_queue is full and the
1540 * state of self->rx_sdu_busy because the disconnect response will
1541 * be sent at the LMP level (so even if the peer has its Tx queue
1542 * full of data). - Jean II */
1543
Joe Perches955a9d202014-11-11 14:44:57 -08001544 pr_debug("%s(), Disconnecting ...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 self->connected = FALSE;
1546
1547 if (!userdata) {
1548 struct sk_buff *tx_skb;
Samuel Ortiz1b0fee72006-09-27 20:06:44 -07001549 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (!tx_skb)
1551 return -ENOMEM;
1552
1553 /*
1554 * Reserve space for MUX and LAP header
1555 */
Samuel Ortiz1b0fee72006-09-27 20:06:44 -07001556 skb_reserve(tx_skb, LMP_MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 userdata = tx_skb;
1559 }
1560 ret = irlmp_disconnect_request(self->lsap, userdata);
1561
1562 /* The disconnect is no longer pending */
1563 clear_bit(0, &self->disconnect_pend); /* FALSE */
1564
1565 return ret;
1566}
1567EXPORT_SYMBOL(irttp_disconnect_request);
1568
1569/*
1570 * Function irttp_disconnect_indication (self, reason)
1571 *
1572 * Disconnect indication, TSAP disconnected by peer?
1573 *
1574 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001575static void irttp_disconnect_indication(void *instance, void *sap,
1576 LM_REASON reason, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct tsap_cb *self;
1579
Joe Perchesea110732011-06-13 16:21:26 +00001580 self = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 IRDA_ASSERT(self != NULL, return;);
1583 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1584
1585 /* Prevent higher layer to send more data */
1586 self->connected = FALSE;
1587
1588 /* Check if client has already tried to close the TSAP */
1589 if (self->close_pend) {
1590 /* In this case, the higher layer is probably gone. Don't
1591 * bother it and clean up the remains - Jean II */
1592 if (skb)
1593 dev_kfree_skb(skb);
1594 irttp_close_tsap(self);
1595 return;
1596 }
1597
1598 /* If we are here, we assume that is the higher layer is still
1599 * waiting for the disconnect notification and able to process it,
1600 * even if he tried to disconnect. Otherwise, it would have already
1601 * attempted to close the tsap and self->close_pend would be TRUE.
1602 * Jean II */
1603
1604 /* No need to notify the client if has already tried to disconnect */
Dragos Foianuaafee332013-07-17 12:25:38 +01001605 if (self->notify.disconnect_indication)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 self->notify.disconnect_indication(self->notify.instance, self,
1607 reason, skb);
1608 else
1609 if (skb)
1610 dev_kfree_skb(skb);
1611}
1612
1613/*
1614 * Function irttp_do_data_indication (self, skb)
1615 *
1616 * Try to deliver reassembled skb to layer above, and requeue it if that
1617 * for some reason should fail. We mark rx sdu as busy to apply back
1618 * pressure is necessary.
1619 */
1620static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1621{
1622 int err;
1623
1624 /* Check if client has already closed the TSAP and gone away */
1625 if (self->close_pend) {
1626 dev_kfree_skb(skb);
1627 return;
1628 }
1629
1630 err = self->notify.data_indication(self->notify.instance, self, skb);
1631
1632 /* Usually the layer above will notify that it's input queue is
1633 * starting to get filled by using the flow request, but this may
1634 * be difficult, so it can instead just refuse to eat it and just
1635 * give an error back
1636 */
1637 if (err) {
Joe Perches955a9d202014-11-11 14:44:57 -08001638 pr_debug("%s() requeueing skb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /* Make sure we take a break */
1641 self->rx_sdu_busy = TRUE;
1642
1643 /* Need to push the header in again */
1644 skb_push(skb, TTP_HEADER);
1645 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1646
1647 /* Put skb back on queue */
1648 skb_queue_head(&self->rx_queue, skb);
1649 }
1650}
1651
1652/*
1653 * Function irttp_run_rx_queue (self)
1654 *
1655 * Check if we have any frames to be transmitted, or if we have any
1656 * available credit to give away.
1657 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -08001658static void irttp_run_rx_queue(struct tsap_cb *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct sk_buff *skb;
1661 int more = 0;
1662
Joe Perches955a9d202014-11-11 14:44:57 -08001663 pr_debug("%s() send=%d,avail=%d,remote=%d\n", __func__,
1664 self->send_credit, self->avail_credit, self->remote_credit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /* Get exclusive access to the rx queue, otherwise don't touch it */
1667 if (irda_lock(&self->rx_queue_lock) == FALSE)
1668 return;
1669
1670 /*
1671 * Reassemble all frames in receive queue and deliver them
1672 */
1673 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1674 /* This bit will tell us if it's the last fragment or not */
1675 more = skb->data[0] & 0x80;
1676
1677 /* Remove TTP header */
1678 skb_pull(skb, TTP_HEADER);
1679
1680 /* Add the length of the remaining data */
1681 self->rx_sdu_size += skb->len;
1682
1683 /*
1684 * If SAR is disabled, or user has requested no reassembly
1685 * of received fragments then we just deliver them
1686 * immediately. This can be requested by clients that
1687 * implements byte streams without any message boundaries
1688 */
1689 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1690 irttp_do_data_indication(self, skb);
1691 self->rx_sdu_size = 0;
1692
1693 continue;
1694 }
1695
1696 /* Check if this is a fragment, and not the last fragment */
1697 if (more) {
1698 /*
1699 * Queue the fragment if we still are within the
1700 * limits of the maximum size of the rx_sdu
1701 */
1702 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
Joe Perches955a9d202014-11-11 14:44:57 -08001703 pr_debug("%s(), queueing frag\n",
1704 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 skb_queue_tail(&self->rx_fragments, skb);
1706 } else {
1707 /* Free the part of the SDU that is too big */
1708 dev_kfree_skb(skb);
1709 }
1710 continue;
1711 }
1712 /*
1713 * This is the last fragment, so time to reassemble!
1714 */
1715 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
Dragos Foianuaafee332013-07-17 12:25:38 +01001716 (self->rx_max_sdu_size == TTP_SAR_UNBOUND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /*
1718 * A little optimizing. Only queue the fragment if
1719 * there are other fragments. Since if this is the
1720 * last and only fragment, there is no need to
1721 * reassemble :-)
1722 */
1723 if (!skb_queue_empty(&self->rx_fragments)) {
1724 skb_queue_tail(&self->rx_fragments,
1725 skb);
1726
1727 skb = irttp_reassemble_skb(self);
1728 }
1729
1730 /* Now we can deliver the reassembled skb */
1731 irttp_do_data_indication(self, skb);
1732 } else {
Joe Perches955a9d202014-11-11 14:44:57 -08001733 pr_debug("%s(), Truncated frame\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* Free the part of the SDU that is too big */
1736 dev_kfree_skb(skb);
1737
1738 /* Deliver only the valid but truncated part of SDU */
1739 skb = irttp_reassemble_skb(self);
1740
1741 irttp_do_data_indication(self, skb);
1742 }
1743 self->rx_sdu_size = 0;
1744 }
1745
1746 /*
1747 * It's not trivial to keep track of how many credits are available
1748 * by incrementing at each packet, because delivery may fail
1749 * (irttp_do_data_indication() may requeue the frame) and because
1750 * we need to take care of fragmentation.
1751 * We want the other side to send up to initial_credit packets.
1752 * We have some frames in our queues, and we have already allowed it
1753 * to send remote_credit.
1754 * No need to spinlock, write is atomic and self correcting...
1755 * Jean II
1756 */
1757 self->avail_credit = (self->initial_credit -
1758 (self->remote_credit +
1759 skb_queue_len(&self->rx_queue) +
1760 skb_queue_len(&self->rx_fragments)));
1761
1762 /* Do we have too much credits to send to peer ? */
1763 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1764 (self->avail_credit > 0)) {
1765 /* Send explicit credit frame */
1766 irttp_give_credit(self);
1767 /* Note : do *NOT* check if tx_queue is non-empty, that
1768 * will produce deadlocks. I repeat : send a credit frame
1769 * even if we have something to send in our Tx queue.
1770 * If we have credits, it means that our Tx queue is blocked.
1771 *
1772 * Let's suppose the peer can't keep up with our Tx. He will
1773 * flow control us by not sending us any credits, and we
1774 * will stop Tx and start accumulating credits here.
1775 * Up to the point where the peer will stop its Tx queue,
1776 * for lack of credits.
1777 * Let's assume the peer application is single threaded.
1778 * It will block on Tx and never consume any Rx buffer.
1779 * Deadlock. Guaranteed. - Jean II
1780 */
1781 }
1782
1783 /* Reset lock */
1784 self->rx_queue_lock = 0;
1785}
1786
1787#ifdef CONFIG_PROC_FS
1788struct irttp_iter_state {
1789 int id;
1790};
1791
1792static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
1793{
1794 struct irttp_iter_state *iter = seq->private;
1795 struct tsap_cb *self;
1796
1797 /* Protect our access to the tsap list */
1798 spin_lock_irq(&irttp->tsaps->hb_spinlock);
1799 iter->id = 0;
1800
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001801 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 self != NULL;
1803 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
1804 if (iter->id == *pos)
1805 break;
1806 ++iter->id;
1807 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return self;
1810}
1811
1812static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1813{
1814 struct irttp_iter_state *iter = seq->private;
1815
1816 ++*pos;
1817 ++iter->id;
1818 return (void *) hashbin_get_next(irttp->tsaps);
1819}
1820
1821static void irttp_seq_stop(struct seq_file *seq, void *v)
1822{
1823 spin_unlock_irq(&irttp->tsaps->hb_spinlock);
1824}
1825
1826static int irttp_seq_show(struct seq_file *seq, void *v)
1827{
1828 const struct irttp_iter_state *iter = seq->private;
1829 const struct tsap_cb *self = v;
1830
1831 seq_printf(seq, "TSAP %d, ", iter->id);
1832 seq_printf(seq, "stsap_sel: %02x, ",
1833 self->stsap_sel);
1834 seq_printf(seq, "dtsap_sel: %02x\n",
1835 self->dtsap_sel);
1836 seq_printf(seq, " connected: %s, ",
Dragos Foianuaafee332013-07-17 12:25:38 +01001837 self->connected ? "TRUE" : "FALSE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 seq_printf(seq, "avail credit: %d, ",
1839 self->avail_credit);
1840 seq_printf(seq, "remote credit: %d, ",
1841 self->remote_credit);
1842 seq_printf(seq, "send credit: %d\n",
1843 self->send_credit);
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001844 seq_printf(seq, " tx packets: %lu, ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 self->stats.tx_packets);
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001846 seq_printf(seq, "rx packets: %lu, ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 self->stats.rx_packets);
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001848 seq_printf(seq, "tx_queue len: %u ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 skb_queue_len(&self->tx_queue));
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001850 seq_printf(seq, "rx_queue len: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 skb_queue_len(&self->rx_queue));
1852 seq_printf(seq, " tx_sdu_busy: %s, ",
Dragos Foianuaafee332013-07-17 12:25:38 +01001853 self->tx_sdu_busy ? "TRUE" : "FALSE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 seq_printf(seq, "rx_sdu_busy: %s\n",
Dragos Foianuaafee332013-07-17 12:25:38 +01001855 self->rx_sdu_busy ? "TRUE" : "FALSE");
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001856 seq_printf(seq, " max_seg_size: %u, ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 self->max_seg_size);
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001858 seq_printf(seq, "tx_max_sdu_size: %u, ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 self->tx_max_sdu_size);
Ben Hutchings0b5c25e2010-06-08 08:23:01 +00001860 seq_printf(seq, "rx_max_sdu_size: %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 self->rx_max_sdu_size);
1862
1863 seq_printf(seq, " Used by (%s)\n\n",
1864 self->notify.name);
1865 return 0;
1866}
1867
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001868static const struct seq_operations irttp_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 .start = irttp_seq_start,
1870 .next = irttp_seq_next,
1871 .stop = irttp_seq_stop,
1872 .show = irttp_seq_show,
1873};
1874
1875static int irttp_seq_open(struct inode *inode, struct file *file)
1876{
Pavel Emelyanova662d4c2007-10-10 02:30:45 -07001877 return seq_open_private(file, &irttp_seq_ops,
1878 sizeof(struct irttp_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Arjan van de Venda7071d2007-02-12 00:55:36 -08001881const struct file_operations irttp_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 .owner = THIS_MODULE,
1883 .open = irttp_seq_open,
1884 .read = seq_read,
1885 .llseek = seq_lseek,
1886 .release = seq_release_private,
1887};
1888
1889#endif /* PROC_FS */