blob: 08fb54dc8c412cbcb04721ca7df9f034ff06dcbf [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_lmp.c
4 * Version: 1.0
5 * Description: Interface between IrCOMM and IrLMP
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Jun 6 20:48:27 1999
9 * Modified at: Sun Dec 12 13:44:17 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: Previous IrLPT work by Thomas Davis
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Copyright (c) 1999 Dag Brattli, 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.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090020 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090025 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * MA 02111-1307 USA
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 ********************************************************************/
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <net/irda/irda.h>
37#include <net/irda/irlmp.h>
38#include <net/irda/iriap.h>
39#include <net/irda/irda_device.h> /* struct irda_skb_cb */
40
41#include <net/irda/ircomm_event.h>
42#include <net/irda/ircomm_lmp.h>
43
44
45/*
46 * Function ircomm_lmp_connect_request (self, userdata)
47 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090048 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 *
50 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090051static int ircomm_lmp_connect_request(struct ircomm_cb *self,
52 struct sk_buff *userdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct ircomm_info *info)
54{
55 int ret = 0;
56
Harvey Harrison0dc47872008-03-05 20:47:47 -080057 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /* Don't forget to refcount it - should be NULL anyway */
60 if(userdata)
61 skb_get(userdata);
62
63 ret = irlmp_connect_request(self->lsap, info->dlsap_sel,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090064 info->saddr, info->daddr, NULL, userdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return ret;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090066}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * Function ircomm_lmp_connect_response (self, skb)
70 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090071 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 */
74static int ircomm_lmp_connect_response(struct ircomm_cb *self,
75 struct sk_buff *userdata)
76{
77 struct sk_buff *tx_skb;
78 int ret;
79
Harvey Harrison0dc47872008-03-05 20:47:47 -080080 IRDA_DEBUG(0, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /* Any userdata supplied? */
83 if (userdata == NULL) {
Samuel Ortiz1b0fee72006-09-27 20:06:44 -070084 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (!tx_skb)
86 return -ENOMEM;
87
88 /* Reserve space for MUX and LAP header */
89 skb_reserve(tx_skb, LMP_MAX_HEADER);
90 } else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090091 /*
92 * Check that the client has reserved enough space for
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * headers
94 */
95 IRDA_ASSERT(skb_headroom(userdata) >= LMP_MAX_HEADER,
96 return -1;);
97
98 /* Don't forget to refcount it - should be NULL anyway */
99 skb_get(userdata);
100 tx_skb = userdata;
101 }
102
103 ret = irlmp_connect_response(self->lsap, tx_skb);
104
105 return 0;
106}
107
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900108static int ircomm_lmp_disconnect_request(struct ircomm_cb *self,
109 struct sk_buff *userdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct ircomm_info *info)
111{
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900112 struct sk_buff *tx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int ret;
114
Harvey Harrison0dc47872008-03-05 20:47:47 -0800115 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900117 if (!userdata) {
Samuel Ortiz1b0fee72006-09-27 20:06:44 -0700118 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!tx_skb)
120 return -ENOMEM;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* Reserve space for MUX and LAP header */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900123 skb_reserve(tx_skb, LMP_MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 userdata = tx_skb;
125 } else {
126 /* Don't forget to refcount it - should be NULL anyway */
127 skb_get(userdata);
128 }
129
130 ret = irlmp_disconnect_request(self->lsap, userdata);
131
132 return ret;
133}
134
135/*
136 * Function ircomm_lmp_flow_control (skb)
137 *
138 * This function is called when a data frame we have sent to IrLAP has
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900139 * been deallocated. We do this to make sure we don't flood IrLAP with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * frames, since we are not using the IrTTP flow control mechanism
141 */
142static void ircomm_lmp_flow_control(struct sk_buff *skb)
143{
144 struct irda_skb_cb *cb;
145 struct ircomm_cb *self;
146 int line;
147
148 IRDA_ASSERT(skb != NULL, return;);
149
150 cb = (struct irda_skb_cb *) skb->cb;
151
Harvey Harrison0dc47872008-03-05 20:47:47 -0800152 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900153
154 line = cb->line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 self = (struct ircomm_cb *) hashbin_lock_find(ircomm, line, NULL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900157 if (!self) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800158 IRDA_DEBUG(2, "%s(), didn't find myself\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900159 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900162 IRDA_ASSERT(self != NULL, return;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
164
165 self->pkt_count--;
166
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900167 if ((self->pkt_count < 2) && (self->flow_status == FLOW_STOP)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800168 IRDA_DEBUG(2, "%s(), asking TTY to start again!\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900169 self->flow_status = FLOW_START;
170 if (self->notify.flow_indication)
171 self->notify.flow_indication(self->notify.instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 self, FLOW_START);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
177 * Function ircomm_lmp_data_request (self, userdata)
178 *
179 * Send data frame to peer device
180 *
181 */
182static int ircomm_lmp_data_request(struct ircomm_cb *self,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900183 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int not_used)
185{
186 struct irda_skb_cb *cb;
187 int ret;
188
189 IRDA_ASSERT(skb != NULL, return -1;);
190
191 cb = (struct irda_skb_cb *) skb->cb;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900192
193 cb->line = self->line;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Harvey Harrison0dc47872008-03-05 20:47:47 -0800195 IRDA_DEBUG(4, "%s(), sending frame\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Don't forget to refcount it - see ircomm_tty_do_softint() */
198 skb_get(skb);
199
Herbert Xud55d87f2009-06-22 02:25:25 +0000200 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 skb->destructor = ircomm_lmp_flow_control;
202
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900203 if ((self->pkt_count++ > 7) && (self->flow_status == FLOW_START)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800204 IRDA_DEBUG(2, "%s(), asking TTY to slow down!\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900205 self->flow_status = FLOW_STOP;
206 if (self->notify.flow_indication)
207 self->notify.flow_indication(self->notify.instance,
208 self, FLOW_STOP);
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 ret = irlmp_data_request(self->lsap, skb);
211 if (ret) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800212 IRDA_ERROR("%s(), failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* irlmp_data_request already free the packet */
214 }
215
216 return ret;
217}
218
219/*
220 * Function ircomm_lmp_data_indication (instance, sap, skb)
221 *
222 * Incoming data which we must deliver to the state machine, to check
223 * we are still connected.
224 */
225static int ircomm_lmp_data_indication(void *instance, void *sap,
226 struct sk_buff *skb)
227{
228 struct ircomm_cb *self = (struct ircomm_cb *) instance;
229
Harvey Harrison0dc47872008-03-05 20:47:47 -0800230 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 IRDA_ASSERT(self != NULL, return -1;);
233 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
234 IRDA_ASSERT(skb != NULL, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ircomm_do_event(self, IRCOMM_LMP_DATA_INDICATION, skb, NULL);
237
238 /* Drop reference count - see ircomm_tty_data_indication(). */
239 dev_kfree_skb(skb);
240
241 return 0;
242}
243
244/*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900245 * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * max_header_size, skb)
247 *
248 * Connection has been confirmed by peer device
249 *
250 */
251static void ircomm_lmp_connect_confirm(void *instance, void *sap,
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900252 struct qos_info *qos,
253 __u32 max_seg_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 __u8 max_header_size,
255 struct sk_buff *skb)
256{
257 struct ircomm_cb *self = (struct ircomm_cb *) instance;
258 struct ircomm_info info;
259
Harvey Harrison0dc47872008-03-05 20:47:47 -0800260 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 IRDA_ASSERT(self != NULL, return;);
263 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
264 IRDA_ASSERT(skb != NULL, return;);
265 IRDA_ASSERT(qos != NULL, return;);
266
267 info.max_data_size = max_seg_size;
268 info.max_header_size = max_header_size;
269 info.qos = qos;
270
271 ircomm_do_event(self, IRCOMM_LMP_CONNECT_CONFIRM, skb, &info);
272
273 /* Drop reference count - see ircomm_tty_connect_confirm(). */
274 dev_kfree_skb(skb);
275}
276
277/*
278 * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
279 * max_header_size, skb)
280 *
281 * Peer device wants to make a connection with us
282 *
283 */
284static void ircomm_lmp_connect_indication(void *instance, void *sap,
285 struct qos_info *qos,
286 __u32 max_seg_size,
287 __u8 max_header_size,
288 struct sk_buff *skb)
289{
290 struct ircomm_cb *self = (struct ircomm_cb *)instance;
291 struct ircomm_info info;
292
Harvey Harrison0dc47872008-03-05 20:47:47 -0800293 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 IRDA_ASSERT(self != NULL, return;);
296 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
297 IRDA_ASSERT(skb != NULL, return;);
298 IRDA_ASSERT(qos != NULL, return;);
299
300 info.max_data_size = max_seg_size;
301 info.max_header_size = max_header_size;
302 info.qos = qos;
303
304 ircomm_do_event(self, IRCOMM_LMP_CONNECT_INDICATION, skb, &info);
305
306 /* Drop reference count - see ircomm_tty_connect_indication(). */
307 dev_kfree_skb(skb);
308}
309
310/*
311 * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
312 *
313 * Peer device has closed the connection, or the link went down for some
314 * other reason
315 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900316static void ircomm_lmp_disconnect_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 LM_REASON reason,
318 struct sk_buff *skb)
319{
320 struct ircomm_cb *self = (struct ircomm_cb *) instance;
321 struct ircomm_info info;
322
Harvey Harrison0dc47872008-03-05 20:47:47 -0800323 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 IRDA_ASSERT(self != NULL, return;);
326 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
327
328 info.reason = reason;
329
330 ircomm_do_event(self, IRCOMM_LMP_DISCONNECT_INDICATION, skb, &info);
331
332 /* Drop reference count - see ircomm_tty_disconnect_indication(). */
333 if(skb)
334 dev_kfree_skb(skb);
335}
336/*
337 * Function ircomm_open_lsap (self)
338 *
339 * Open LSAP. This function will only be used when using "raw" services
340 *
341 */
342int ircomm_open_lsap(struct ircomm_cb *self)
343{
344 notify_t notify;
345
Harvey Harrison0dc47872008-03-05 20:47:47 -0800346 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Register callbacks */
349 irda_notify_init(&notify);
350 notify.data_indication = ircomm_lmp_data_indication;
351 notify.connect_confirm = ircomm_lmp_connect_confirm;
352 notify.connect_indication = ircomm_lmp_connect_indication;
353 notify.disconnect_indication = ircomm_lmp_disconnect_indication;
354 notify.instance = self;
355 strlcpy(notify.name, "IrCOMM", sizeof(notify.name));
356
357 self->lsap = irlmp_open_lsap(LSAP_ANY, &notify, 0);
358 if (!self->lsap) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800359 IRDA_DEBUG(0,"%sfailed to allocate tsap\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -1;
361 }
362 self->slsap_sel = self->lsap->slsap_sel;
363
364 /*
365 * Initialize the call-table for issuing commands
366 */
367 self->issue.data_request = ircomm_lmp_data_request;
368 self->issue.connect_request = ircomm_lmp_connect_request;
369 self->issue.connect_response = ircomm_lmp_connect_response;
370 self->issue.disconnect_request = ircomm_lmp_disconnect_request;
371
372 return 0;
373}