blob: d362d711b79c8cb36283667387e142538b1e515a [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: ircomm_ttp.c
4 * Version: 1.0
5 * Description: Interface between IrCOMM and IrTTP
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Jun 6 20:48:27 1999
9 * Modified at: Mon Dec 13 11:35:13 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
13 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090024 *
25 * You should have received a copy of the GNU General Public License
Jeff Kirsherd3770502013-12-06 09:13:43 -080026 * along with this program; if not, see <http://www.gnu.org/licenses/>.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 ********************************************************************/
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/init.h>
31
32#include <net/irda/irda.h>
33#include <net/irda/irlmp.h>
34#include <net/irda/iriap.h>
35#include <net/irda/irttp.h>
36
37#include <net/irda/ircomm_event.h>
38#include <net/irda/ircomm_ttp.h>
39
40static int ircomm_ttp_data_indication(void *instance, void *sap,
41 struct sk_buff *skb);
42static void ircomm_ttp_connect_confirm(void *instance, void *sap,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090043 struct qos_info *qos,
44 __u32 max_sdu_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 __u8 max_header_size,
46 struct sk_buff *skb);
47static void ircomm_ttp_connect_indication(void *instance, void *sap,
48 struct qos_info *qos,
49 __u32 max_sdu_size,
50 __u8 max_header_size,
51 struct sk_buff *skb);
52static void ircomm_ttp_flow_indication(void *instance, void *sap,
53 LOCAL_FLOW cmd);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090054static void ircomm_ttp_disconnect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 LM_REASON reason,
56 struct sk_buff *skb);
57static int ircomm_ttp_data_request(struct ircomm_cb *self,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090058 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int clen);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090060static int ircomm_ttp_connect_request(struct ircomm_cb *self,
61 struct sk_buff *userdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct ircomm_info *info);
63static int ircomm_ttp_connect_response(struct ircomm_cb *self,
64 struct sk_buff *userdata);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090065static int ircomm_ttp_disconnect_request(struct ircomm_cb *self,
66 struct sk_buff *userdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct ircomm_info *info);
68
69/*
70 * Function ircomm_open_tsap (self)
71 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 */
75int ircomm_open_tsap(struct ircomm_cb *self)
76{
77 notify_t notify;
78
Harvey Harrison0dc47872008-03-05 20:47:47 -080079 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* Register callbacks */
82 irda_notify_init(&notify);
83 notify.data_indication = ircomm_ttp_data_indication;
84 notify.connect_confirm = ircomm_ttp_connect_confirm;
85 notify.connect_indication = ircomm_ttp_connect_indication;
86 notify.flow_indication = ircomm_ttp_flow_indication;
87 notify.disconnect_indication = ircomm_ttp_disconnect_indication;
88 notify.instance = self;
89 strlcpy(notify.name, "IrCOMM", sizeof(notify.name));
90
91 self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
92 &notify);
93 if (!self->tsap) {
Harvey Harrison0dc47872008-03-05 20:47:47 -080094 IRDA_DEBUG(0, "%sfailed to allocate tsap\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -1;
96 }
97 self->slsap_sel = self->tsap->stsap_sel;
98
99 /*
100 * Initialize the call-table for issuing commands
101 */
102 self->issue.data_request = ircomm_ttp_data_request;
103 self->issue.connect_request = ircomm_ttp_connect_request;
104 self->issue.connect_response = ircomm_ttp_connect_response;
105 self->issue.disconnect_request = ircomm_ttp_disconnect_request;
106
107 return 0;
108}
109
110/*
111 * Function ircomm_ttp_connect_request (self, userdata)
112 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
115 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900116static int ircomm_ttp_connect_request(struct ircomm_cb *self,
117 struct sk_buff *userdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct ircomm_info *info)
119{
120 int ret = 0;
121
Harvey Harrison0dc47872008-03-05 20:47:47 -0800122 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /* Don't forget to refcount it - should be NULL anyway */
125 if(userdata)
126 skb_get(userdata);
127
128 ret = irttp_connect_request(self->tsap, info->dlsap_sel,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900129 info->saddr, info->daddr, NULL,
130 TTP_SAR_DISABLE, userdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return ret;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/*
136 * Function ircomm_ttp_connect_response (self, skb)
137 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900138 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 */
141static int ircomm_ttp_connect_response(struct ircomm_cb *self,
142 struct sk_buff *userdata)
143{
144 int ret;
145
Harvey Harrison0dc47872008-03-05 20:47:47 -0800146 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Don't forget to refcount it - should be NULL anyway */
149 if(userdata)
150 skb_get(userdata);
151
152 ret = irttp_connect_response(self->tsap, TTP_SAR_DISABLE, userdata);
153
154 return ret;
155}
156
157/*
158 * Function ircomm_ttp_data_request (self, userdata)
159 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900160 * Send IrCOMM data to IrTTP layer. Currently we do not try to combine
161 * control data with pure data, so they will be sent as separate frames.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * Should not be a big problem though, since control frames are rare. But
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900163 * some of them are sent after connection establishment, so this can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * increase the latency a bit.
165 */
166static int ircomm_ttp_data_request(struct ircomm_cb *self,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900167 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int clen)
169{
170 int ret;
171
172 IRDA_ASSERT(skb != NULL, return -1;);
173
Harvey Harrison0dc47872008-03-05 20:47:47 -0800174 IRDA_DEBUG(2, "%s(), clen=%d\n", __func__ , clen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900176 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * Insert clen field, currently we either send data only, or control
178 * only frames, to make things easier and avoid queueing
179 */
180 IRDA_ASSERT(skb_headroom(skb) >= IRCOMM_HEADER_SIZE, return -1;);
181
182 /* Don't forget to refcount it - see ircomm_tty_do_softint() */
183 skb_get(skb);
184
185 skb_push(skb, IRCOMM_HEADER_SIZE);
186
187 skb->data[0] = clen;
188
189 ret = irttp_data_request(self->tsap, skb);
190 if (ret) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800191 IRDA_ERROR("%s(), failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* irttp_data_request already free the packet */
193 }
194
195 return ret;
196}
197
198/*
199 * Function ircomm_ttp_data_indication (instance, sap, skb)
200 *
201 * Incoming data
202 *
203 */
204static int ircomm_ttp_data_indication(void *instance, void *sap,
205 struct sk_buff *skb)
206{
207 struct ircomm_cb *self = (struct ircomm_cb *) instance;
208
Harvey Harrison0dc47872008-03-05 20:47:47 -0800209 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 IRDA_ASSERT(self != NULL, return -1;);
212 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
213 IRDA_ASSERT(skb != NULL, return -1;);
214
215 ircomm_do_event(self, IRCOMM_TTP_DATA_INDICATION, skb, NULL);
216
217 /* Drop reference count - see ircomm_tty_data_indication(). */
218 dev_kfree_skb(skb);
219
220 return 0;
221}
222
223static void ircomm_ttp_connect_confirm(void *instance, void *sap,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900224 struct qos_info *qos,
225 __u32 max_sdu_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 __u8 max_header_size,
227 struct sk_buff *skb)
228{
229 struct ircomm_cb *self = (struct ircomm_cb *) instance;
230 struct ircomm_info info;
231
Harvey Harrison0dc47872008-03-05 20:47:47 -0800232 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 IRDA_ASSERT(self != NULL, return;);
235 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
236 IRDA_ASSERT(skb != NULL, return;);
237 IRDA_ASSERT(qos != NULL, goto out;);
238
239 if (max_sdu_size != TTP_SAR_DISABLE) {
240 IRDA_ERROR("%s(), SAR not allowed for IrCOMM!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800241 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out;
243 }
244
245 info.max_data_size = irttp_get_max_seg_size(self->tsap)
246 - IRCOMM_HEADER_SIZE;
247 info.max_header_size = max_header_size + IRCOMM_HEADER_SIZE;
248 info.qos = qos;
249
250 ircomm_do_event(self, IRCOMM_TTP_CONNECT_CONFIRM, skb, &info);
251
252out:
253 /* Drop reference count - see ircomm_tty_connect_confirm(). */
254 dev_kfree_skb(skb);
255}
256
257/*
258 * Function ircomm_ttp_connect_indication (instance, sap, qos, max_sdu_size,
259 * max_header_size, skb)
260 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900261 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
263 */
264static void ircomm_ttp_connect_indication(void *instance, void *sap,
265 struct qos_info *qos,
266 __u32 max_sdu_size,
267 __u8 max_header_size,
268 struct sk_buff *skb)
269{
270 struct ircomm_cb *self = (struct ircomm_cb *)instance;
271 struct ircomm_info info;
272
Harvey Harrison0dc47872008-03-05 20:47:47 -0800273 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 IRDA_ASSERT(self != NULL, return;);
276 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
277 IRDA_ASSERT(skb != NULL, return;);
278 IRDA_ASSERT(qos != NULL, goto out;);
279
280 if (max_sdu_size != TTP_SAR_DISABLE) {
281 IRDA_ERROR("%s(), SAR not allowed for IrCOMM!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800282 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto out;
284 }
285
286 info.max_data_size = irttp_get_max_seg_size(self->tsap)
287 - IRCOMM_HEADER_SIZE;
288 info.max_header_size = max_header_size + IRCOMM_HEADER_SIZE;
289 info.qos = qos;
290
291 ircomm_do_event(self, IRCOMM_TTP_CONNECT_INDICATION, skb, &info);
292
293out:
294 /* Drop reference count - see ircomm_tty_connect_indication(). */
295 dev_kfree_skb(skb);
296}
297
298/*
299 * Function ircomm_ttp_disconnect_request (self, userdata, info)
300 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900301 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900304static int ircomm_ttp_disconnect_request(struct ircomm_cb *self,
305 struct sk_buff *userdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct ircomm_info *info)
307{
308 int ret;
309
310 /* Don't forget to refcount it - should be NULL anyway */
311 if(userdata)
312 skb_get(userdata);
313
314 ret = irttp_disconnect_request(self->tsap, userdata, P_NORMAL);
315
316 return ret;
317}
318
319/*
320 * Function ircomm_ttp_disconnect_indication (instance, sap, reason, skb)
321 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900322 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
324 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900325static void ircomm_ttp_disconnect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 LM_REASON reason,
327 struct sk_buff *skb)
328{
329 struct ircomm_cb *self = (struct ircomm_cb *) instance;
330 struct ircomm_info info;
331
Harvey Harrison0dc47872008-03-05 20:47:47 -0800332 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 IRDA_ASSERT(self != NULL, return;);
335 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
336
337 info.reason = reason;
338
339 ircomm_do_event(self, IRCOMM_TTP_DISCONNECT_INDICATION, skb, &info);
340
341 /* Drop reference count - see ircomm_tty_disconnect_indication(). */
342 if(skb)
343 dev_kfree_skb(skb);
344}
345
346/*
347 * Function ircomm_ttp_flow_indication (instance, sap, cmd)
348 *
349 * Layer below is telling us to start or stop the flow of data
350 *
351 */
352static void ircomm_ttp_flow_indication(void *instance, void *sap,
353 LOCAL_FLOW cmd)
354{
355 struct ircomm_cb *self = (struct ircomm_cb *) instance;
356
Harvey Harrison0dc47872008-03-05 20:47:47 -0800357 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 IRDA_ASSERT(self != NULL, return;);
360 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (self->notify.flow_indication)
363 self->notify.flow_indication(self->notify.instance, self, cmd);
364}
365
366