summaryrefslogtreecommitdiffstats
path: root/net/irda/ircomm/ircomm_ttp.c
blob: bb06ebaadd16f481741032ca39c8cd54cc55dbd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*********************************************************************
 *
 * Filename:      ircomm_ttp.c
 * Version:       1.0
 * Description:   Interface between IrCOMM and IrTTP
 * Status:        Stable
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Sun Jun  6 20:48:27 1999
 * Modified at:   Mon Dec 13 11:35:13 1999
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 *
 *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
 *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *     MA 02111-1307 USA
 *
 ********************************************************************/

#include <linux/sched.h>
#include <linux/init.h>

#include <net/irda/irda.h>
#include <net/irda/irlmp.h>
#include <net/irda/iriap.h>
#include <net/irda/irttp.h>

#include <net/irda/ircomm_event.h>
#include <net/irda/ircomm_ttp.h>

static int ircomm_ttp_data_indication(void *instance, void *sap,
				      struct sk_buff *skb);
static void ircomm_ttp_connect_confirm(void *instance, void *sap,
				       struct qos_info *qos,
				       __u32 max_sdu_size,
				       __u8 max_header_size,
				       struct sk_buff *skb);
static void ircomm_ttp_connect_indication(void *instance, void *sap,
					  struct qos_info *qos,
					  __u32 max_sdu_size,
					  __u8 max_header_size,
					  struct sk_buff *skb);
static void ircomm_ttp_flow_indication(void *instance, void *sap,
				       LOCAL_FLOW cmd);
static void ircomm_ttp_disconnect_indication(void *instance, void *sap,
					     LM_REASON reason,
					     struct sk_buff *skb);
static int ircomm_ttp_data_request(struct ircomm_cb *self,
				   struct sk_buff *skb,
				   int clen);
static int ircomm_ttp_connect_request(struct ircomm_cb *self,
				      struct sk_buff *userdata,
				      struct ircomm_info *info);
static int ircomm_ttp_connect_response(struct ircomm_cb *self,
				       struct sk_buff *userdata);
static int ircomm_ttp_disconnect_request(struct ircomm_cb *self,
					 struct sk_buff *userdata,
					 struct ircomm_info *info);

/*
 * Function ircomm_open_tsap (self)
 *
 *
 *
 */
int ircomm_open_tsap(struct ircomm_cb *self)
{
	notify_t notify;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	/* Register callbacks */
	irda_notify_init(&notify);
	notify.data_indication       = ircomm_ttp_data_indication;
	notify.connect_confirm       = ircomm_ttp_connect_confirm;
	notify.connect_indication    = ircomm_ttp_connect_indication;
	notify.flow_indication       = ircomm_ttp_flow_indication;
	notify.disconnect_indication = ircomm_ttp_disconnect_indication;
	notify.instance = self;
	strlcpy(notify.name, "IrCOMM", sizeof(notify.name));

	self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
				     &notify);
	if (!self->tsap) {
		IRDA_DEBUG(0, "%sfailed to allocate tsap\n", __FUNCTION__ );
		return -1;
	}
	self->slsap_sel = self->tsap->stsap_sel;

	/*
	 *  Initialize the call-table for issuing commands
	 */
	self->issue.data_request       = ircomm_ttp_data_request;
	self->issue.connect_request    = ircomm_ttp_connect_request;
	self->issue.connect_response   = ircomm_ttp_connect_response;
	self->issue.disconnect_request = ircomm_ttp_disconnect_request;

	return 0;
}

/*
 * Function ircomm_ttp_connect_request (self, userdata)
 *
 *
 *
 */
static int ircomm_ttp_connect_request(struct ircomm_cb *self,
				      struct sk_buff *userdata,
				      struct ircomm_info *info)
{
	int ret = 0;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	/* Don't forget to refcount it - should be NULL anyway */
	if(userdata)
		skb_get(userdata);

	ret = irttp_connect_request(self->tsap, info->dlsap_sel,
				    info->saddr, info->daddr, NULL,
				    TTP_SAR_DISABLE, userdata);

	return ret;
}

/*
 * Function ircomm_ttp_connect_response (self, skb)
 *
 *
 *
 */
static int ircomm_ttp_connect_response(struct ircomm_cb *self,
				       struct sk_buff *userdata)
{
	int ret;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	/* Don't forget to refcount it - should be NULL anyway */
	if(userdata)
		skb_get(userdata);

	ret = irttp_connect_response(self->tsap, TTP_SAR_DISABLE, userdata);

	return ret;
}

/*
 * Function ircomm_ttp_data_request (self, userdata)
 *
 *    Send IrCOMM data to IrTTP layer. Currently we do not try to combine
 *    control data with pure data, so they will be sent as separate frames.
 *    Should not be a big problem though, since control frames are rare. But
 *    some of them are sent after connection establishment, so this can
 *    increase the latency a bit.
 */
static int ircomm_ttp_data_request(struct ircomm_cb *self,
				   struct sk_buff *skb,
				   int clen)
{
	int ret;

	IRDA_ASSERT(skb != NULL, return -1;);

	IRDA_DEBUG(2, "%s(), clen=%d\n", __FUNCTION__ , clen);

	/*
	 * Insert clen field, currently we either send data only, or control
	 * only frames, to make things easier and avoid queueing
	 */
	IRDA_ASSERT(skb_headroom(skb) >= IRCOMM_HEADER_SIZE, return -1;);

	/* Don't forget to refcount it - see ircomm_tty_do_softint() */
	skb_get(skb);

	skb_push(skb, IRCOMM_HEADER_SIZE);

	skb->data[0] = clen;

	ret = irttp_data_request(self->tsap, skb);
	if (ret) {
		IRDA_ERROR("%s(), failed\n", __FUNCTION__);
		/* irttp_data_request already free the packet */
	}

	return ret;
}

/*
 * Function ircomm_ttp_data_indication (instance, sap, skb)
 *
 *    Incoming data
 *
 */
static int ircomm_ttp_data_indication(void *instance, void *sap,
				      struct sk_buff *skb)
{
	struct ircomm_cb *self = (struct ircomm_cb *) instance;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
	IRDA_ASSERT(skb != NULL, return -1;);

	ircomm_do_event(self, IRCOMM_TTP_DATA_INDICATION, skb, NULL);

	/* Drop reference count - see ircomm_tty_data_indication(). */
	dev_kfree_skb(skb);

	return 0;
}

static void ircomm_ttp_connect_confirm(void *instance, void *sap,
				       struct qos_info *qos,
				       __u32 max_sdu_size,
				       __u8 max_header_size,
				       struct sk_buff *skb)
{
	struct ircomm_cb *self = (struct ircomm_cb *) instance;
	struct ircomm_info info;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
	IRDA_ASSERT(skb != NULL, return;);
	IRDA_ASSERT(qos != NULL, goto out;);

	if (max_sdu_size != TTP_SAR_DISABLE) {
		IRDA_ERROR("%s(), SAR not allowed for IrCOMM!\n",
			   __FUNCTION__);
		goto out;
	}

	info.max_data_size = irttp_get_max_seg_size(self->tsap)
		- IRCOMM_HEADER_SIZE;
	info.max_header_size = max_header_size + IRCOMM_HEADER_SIZE;
	info.qos = qos;

	ircomm_do_event(self, IRCOMM_TTP_CONNECT_CONFIRM, skb, &info);

out:
	/* Drop reference count - see ircomm_tty_connect_confirm(). */
	dev_kfree_skb(skb);
}

/*
 * Function ircomm_ttp_connect_indication (instance, sap, qos, max_sdu_size,
 *                                         max_header_size, skb)
 *
 *
 *
 */
static void ircomm_ttp_connect_indication(void *instance, void *sap,
					  struct qos_info *qos,
					  __u32 max_sdu_size,
					  __u8 max_header_size,
					  struct sk_buff *skb)
{
	struct ircomm_cb *self = (struct ircomm_cb *)instance;
	struct ircomm_info info;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
	IRDA_ASSERT(skb != NULL, return;);
	IRDA_ASSERT(qos != NULL, goto out;);

	if (max_sdu_size != TTP_SAR_DISABLE) {
		IRDA_ERROR("%s(), SAR not allowed for IrCOMM!\n",
			   __FUNCTION__);
		goto out;
	}

	info.max_data_size = irttp_get_max_seg_size(self->tsap)
		- IRCOMM_HEADER_SIZE;
	info.max_header_size = max_header_size + IRCOMM_HEADER_SIZE;
	info.qos = qos;

	ircomm_do_event(self, IRCOMM_TTP_CONNECT_INDICATION, skb, &info);

out:
	/* Drop reference count - see ircomm_tty_connect_indication(). */
	dev_kfree_skb(skb);
}

/*
 * Function ircomm_ttp_disconnect_request (self, userdata, info)
 *
 *
 *
 */
static int ircomm_ttp_disconnect_request(struct ircomm_cb *self,
					 struct sk_buff *userdata,
					 struct ircomm_info *info)
{
	int ret;

	/* Don't forget to refcount it - should be NULL anyway */
	if(userdata)
		skb_get(userdata);

	ret = irttp_disconnect_request(self->tsap, userdata, P_NORMAL);

	return ret;
}

/*
 * Function ircomm_ttp_disconnect_indication (instance, sap, reason, skb)
 *
 *
 *
 */
static void ircomm_ttp_disconnect_indication(void *instance, void *sap,
					     LM_REASON reason,
					     struct sk_buff *skb)
{
	struct ircomm_cb *self = (struct ircomm_cb *) instance;
	struct ircomm_info info;

	IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);

	info.reason = reason;

	ircomm_do_event(self, IRCOMM_TTP_DISCONNECT_INDICATION, skb, &info);

	/* Drop reference count - see ircomm_tty_disconnect_indication(). */
	if(skb)
		dev_kfree_skb(skb);
}

/*
 * Function ircomm_ttp_flow_indication (instance, sap, cmd)
 *
 *    Layer below is telling us to start or stop the flow of data
 *
 */
static void ircomm_ttp_flow_indication(void *instance, void *sap,
				       LOCAL_FLOW cmd)
{
	struct ircomm_cb *self = (struct ircomm_cb *) instance;

	IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);

	if (self->notify.flow_indication)
		self->notify.flow_indication(self->notify.instance, self, cmd);
}