1 /*********************************************************************
3 * Filename: ircomm_lmp.c
5 * Description: Interface between IrCOMM and IrLMP
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
13 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
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
19 * the License, or (at your option) any later version.
21 * 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.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 ********************************************************************/
31 #include <linux/init.h>
32 #include <linux/gfp.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/irlmp.h>
36 #include <net/irda/iriap.h>
37 #include <net/irda/irda_device.h> /* struct irda_skb_cb */
39 #include <net/irda/ircomm_event.h>
40 #include <net/irda/ircomm_lmp.h>
44 * Function ircomm_lmp_connect_request (self, userdata)
49 static int ircomm_lmp_connect_request(struct ircomm_cb
*self
,
50 struct sk_buff
*userdata
,
51 struct ircomm_info
*info
)
55 IRDA_DEBUG(0, "%s()\n", __func__
);
57 /* Don't forget to refcount it - should be NULL anyway */
61 ret
= irlmp_connect_request(self
->lsap
, info
->dlsap_sel
,
62 info
->saddr
, info
->daddr
, NULL
, userdata
);
67 * Function ircomm_lmp_connect_response (self, skb)
72 static int ircomm_lmp_connect_response(struct ircomm_cb
*self
,
73 struct sk_buff
*userdata
)
75 struct sk_buff
*tx_skb
;
77 IRDA_DEBUG(0, "%s()\n", __func__
);
79 /* Any userdata supplied? */
80 if (userdata
== NULL
) {
81 tx_skb
= alloc_skb(LMP_MAX_HEADER
, GFP_ATOMIC
);
85 /* Reserve space for MUX and LAP header */
86 skb_reserve(tx_skb
, LMP_MAX_HEADER
);
89 * Check that the client has reserved enough space for
92 IRDA_ASSERT(skb_headroom(userdata
) >= LMP_MAX_HEADER
,
95 /* Don't forget to refcount it - should be NULL anyway */
100 return irlmp_connect_response(self
->lsap
, tx_skb
);
103 static int ircomm_lmp_disconnect_request(struct ircomm_cb
*self
,
104 struct sk_buff
*userdata
,
105 struct ircomm_info
*info
)
107 struct sk_buff
*tx_skb
;
110 IRDA_DEBUG(0, "%s()\n", __func__
);
113 tx_skb
= alloc_skb(LMP_MAX_HEADER
, GFP_ATOMIC
);
117 /* Reserve space for MUX and LAP header */
118 skb_reserve(tx_skb
, LMP_MAX_HEADER
);
121 /* Don't forget to refcount it - should be NULL anyway */
125 ret
= irlmp_disconnect_request(self
->lsap
, userdata
);
131 * Function ircomm_lmp_flow_control (skb)
133 * This function is called when a data frame we have sent to IrLAP has
134 * been deallocated. We do this to make sure we don't flood IrLAP with
135 * frames, since we are not using the IrTTP flow control mechanism
137 static void ircomm_lmp_flow_control(struct sk_buff
*skb
)
139 struct irda_skb_cb
*cb
;
140 struct ircomm_cb
*self
;
143 IRDA_ASSERT(skb
!= NULL
, return;);
145 cb
= (struct irda_skb_cb
*) skb
->cb
;
147 IRDA_DEBUG(2, "%s()\n", __func__
);
151 self
= (struct ircomm_cb
*) hashbin_lock_find(ircomm
, line
, NULL
);
153 IRDA_DEBUG(2, "%s(), didn't find myself\n", __func__
);
157 IRDA_ASSERT(self
!= NULL
, return;);
158 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
162 if ((self
->pkt_count
< 2) && (self
->flow_status
== FLOW_STOP
)) {
163 IRDA_DEBUG(2, "%s(), asking TTY to start again!\n", __func__
);
164 self
->flow_status
= FLOW_START
;
165 if (self
->notify
.flow_indication
)
166 self
->notify
.flow_indication(self
->notify
.instance
,
172 * Function ircomm_lmp_data_request (self, userdata)
174 * Send data frame to peer device
177 static int ircomm_lmp_data_request(struct ircomm_cb
*self
,
181 struct irda_skb_cb
*cb
;
184 IRDA_ASSERT(skb
!= NULL
, return -1;);
186 cb
= (struct irda_skb_cb
*) skb
->cb
;
188 cb
->line
= self
->line
;
190 IRDA_DEBUG(4, "%s(), sending frame\n", __func__
);
192 /* Don't forget to refcount it - see ircomm_tty_do_softint() */
196 skb
->destructor
= ircomm_lmp_flow_control
;
198 if ((self
->pkt_count
++ > 7) && (self
->flow_status
== FLOW_START
)) {
199 IRDA_DEBUG(2, "%s(), asking TTY to slow down!\n", __func__
);
200 self
->flow_status
= FLOW_STOP
;
201 if (self
->notify
.flow_indication
)
202 self
->notify
.flow_indication(self
->notify
.instance
,
205 ret
= irlmp_data_request(self
->lsap
, skb
);
207 IRDA_ERROR("%s(), failed\n", __func__
);
208 /* irlmp_data_request already free the packet */
215 * Function ircomm_lmp_data_indication (instance, sap, skb)
217 * Incoming data which we must deliver to the state machine, to check
218 * we are still connected.
220 static int ircomm_lmp_data_indication(void *instance
, void *sap
,
223 struct ircomm_cb
*self
= (struct ircomm_cb
*) instance
;
225 IRDA_DEBUG(4, "%s()\n", __func__
);
227 IRDA_ASSERT(self
!= NULL
, return -1;);
228 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return -1;);
229 IRDA_ASSERT(skb
!= NULL
, return -1;);
231 ircomm_do_event(self
, IRCOMM_LMP_DATA_INDICATION
, skb
, NULL
);
233 /* Drop reference count - see ircomm_tty_data_indication(). */
240 * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
241 * max_header_size, skb)
243 * Connection has been confirmed by peer device
246 static void ircomm_lmp_connect_confirm(void *instance
, void *sap
,
247 struct qos_info
*qos
,
249 __u8 max_header_size
,
252 struct ircomm_cb
*self
= (struct ircomm_cb
*) instance
;
253 struct ircomm_info info
;
255 IRDA_DEBUG(0, "%s()\n", __func__
);
257 IRDA_ASSERT(self
!= NULL
, return;);
258 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
259 IRDA_ASSERT(skb
!= NULL
, return;);
260 IRDA_ASSERT(qos
!= NULL
, return;);
262 info
.max_data_size
= max_seg_size
;
263 info
.max_header_size
= max_header_size
;
266 ircomm_do_event(self
, IRCOMM_LMP_CONNECT_CONFIRM
, skb
, &info
);
268 /* Drop reference count - see ircomm_tty_connect_confirm(). */
273 * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
274 * max_header_size, skb)
276 * Peer device wants to make a connection with us
279 static void ircomm_lmp_connect_indication(void *instance
, void *sap
,
280 struct qos_info
*qos
,
282 __u8 max_header_size
,
285 struct ircomm_cb
*self
= (struct ircomm_cb
*)instance
;
286 struct ircomm_info info
;
288 IRDA_DEBUG(0, "%s()\n", __func__
);
290 IRDA_ASSERT(self
!= NULL
, return;);
291 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
292 IRDA_ASSERT(skb
!= NULL
, return;);
293 IRDA_ASSERT(qos
!= NULL
, return;);
295 info
.max_data_size
= max_seg_size
;
296 info
.max_header_size
= max_header_size
;
299 ircomm_do_event(self
, IRCOMM_LMP_CONNECT_INDICATION
, skb
, &info
);
301 /* Drop reference count - see ircomm_tty_connect_indication(). */
306 * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
308 * Peer device has closed the connection, or the link went down for some
311 static void ircomm_lmp_disconnect_indication(void *instance
, void *sap
,
315 struct ircomm_cb
*self
= (struct ircomm_cb
*) instance
;
316 struct ircomm_info info
;
318 IRDA_DEBUG(0, "%s()\n", __func__
);
320 IRDA_ASSERT(self
!= NULL
, return;);
321 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
323 info
.reason
= reason
;
325 ircomm_do_event(self
, IRCOMM_LMP_DISCONNECT_INDICATION
, skb
, &info
);
327 /* Drop reference count - see ircomm_tty_disconnect_indication(). */
332 * Function ircomm_open_lsap (self)
334 * Open LSAP. This function will only be used when using "raw" services
337 int ircomm_open_lsap(struct ircomm_cb
*self
)
341 IRDA_DEBUG(0, "%s()\n", __func__
);
343 /* Register callbacks */
344 irda_notify_init(¬ify
);
345 notify
.data_indication
= ircomm_lmp_data_indication
;
346 notify
.connect_confirm
= ircomm_lmp_connect_confirm
;
347 notify
.connect_indication
= ircomm_lmp_connect_indication
;
348 notify
.disconnect_indication
= ircomm_lmp_disconnect_indication
;
349 notify
.instance
= self
;
350 strlcpy(notify
.name
, "IrCOMM", sizeof(notify
.name
));
352 self
->lsap
= irlmp_open_lsap(LSAP_ANY
, ¬ify
, 0);
354 IRDA_DEBUG(0,"%sfailed to allocate tsap\n", __func__
);
357 self
->slsap_sel
= self
->lsap
->slsap_sel
;
360 * Initialize the call-table for issuing commands
362 self
->issue
.data_request
= ircomm_lmp_data_request
;
363 self
->issue
.connect_request
= ircomm_lmp_connect_request
;
364 self
->issue
.connect_response
= ircomm_lmp_connect_response
;
365 self
->issue
.disconnect_request
= ircomm_lmp_disconnect_request
;