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 /* Don't forget to refcount it - should be NULL anyway */
59 ret
= irlmp_connect_request(self
->lsap
, info
->dlsap_sel
,
60 info
->saddr
, info
->daddr
, NULL
, userdata
);
65 * Function ircomm_lmp_connect_response (self, skb)
70 static int ircomm_lmp_connect_response(struct ircomm_cb
*self
,
71 struct sk_buff
*userdata
)
73 struct sk_buff
*tx_skb
;
75 /* Any userdata supplied? */
76 if (userdata
== NULL
) {
77 tx_skb
= alloc_skb(LMP_MAX_HEADER
, GFP_ATOMIC
);
81 /* Reserve space for MUX and LAP header */
82 skb_reserve(tx_skb
, LMP_MAX_HEADER
);
85 * Check that the client has reserved enough space for
88 IRDA_ASSERT(skb_headroom(userdata
) >= LMP_MAX_HEADER
,
91 /* Don't forget to refcount it - should be NULL anyway */
96 return irlmp_connect_response(self
->lsap
, tx_skb
);
99 static int ircomm_lmp_disconnect_request(struct ircomm_cb
*self
,
100 struct sk_buff
*userdata
,
101 struct ircomm_info
*info
)
103 struct sk_buff
*tx_skb
;
107 tx_skb
= alloc_skb(LMP_MAX_HEADER
, GFP_ATOMIC
);
111 /* Reserve space for MUX and LAP header */
112 skb_reserve(tx_skb
, LMP_MAX_HEADER
);
115 /* Don't forget to refcount it - should be NULL anyway */
119 ret
= irlmp_disconnect_request(self
->lsap
, userdata
);
125 * Function ircomm_lmp_flow_control (skb)
127 * This function is called when a data frame we have sent to IrLAP has
128 * been deallocated. We do this to make sure we don't flood IrLAP with
129 * frames, since we are not using the IrTTP flow control mechanism
131 static void ircomm_lmp_flow_control(struct sk_buff
*skb
)
133 struct irda_skb_cb
*cb
;
134 struct ircomm_cb
*self
;
137 IRDA_ASSERT(skb
!= NULL
, return;);
139 cb
= (struct irda_skb_cb
*) skb
->cb
;
143 self
= (struct ircomm_cb
*) hashbin_lock_find(ircomm
, line
, NULL
);
145 pr_debug("%s(), didn't find myself\n", __func__
);
149 IRDA_ASSERT(self
!= NULL
, return;);
150 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
154 if ((self
->pkt_count
< 2) && (self
->flow_status
== FLOW_STOP
)) {
155 pr_debug("%s(), asking TTY to start again!\n", __func__
);
156 self
->flow_status
= FLOW_START
;
157 if (self
->notify
.flow_indication
)
158 self
->notify
.flow_indication(self
->notify
.instance
,
164 * Function ircomm_lmp_data_request (self, userdata)
166 * Send data frame to peer device
169 static int ircomm_lmp_data_request(struct ircomm_cb
*self
,
173 struct irda_skb_cb
*cb
;
176 IRDA_ASSERT(skb
!= NULL
, return -1;);
178 cb
= (struct irda_skb_cb
*) skb
->cb
;
180 cb
->line
= self
->line
;
182 pr_debug("%s(), sending frame\n", __func__
);
184 /* Don't forget to refcount it - see ircomm_tty_do_softint() */
188 skb
->destructor
= ircomm_lmp_flow_control
;
190 if ((self
->pkt_count
++ > 7) && (self
->flow_status
== FLOW_START
)) {
191 pr_debug("%s(), asking TTY to slow down!\n", __func__
);
192 self
->flow_status
= FLOW_STOP
;
193 if (self
->notify
.flow_indication
)
194 self
->notify
.flow_indication(self
->notify
.instance
,
197 ret
= irlmp_data_request(self
->lsap
, skb
);
199 net_err_ratelimited("%s(), failed\n", __func__
);
200 /* irlmp_data_request already free the packet */
207 * Function ircomm_lmp_data_indication (instance, sap, skb)
209 * Incoming data which we must deliver to the state machine, to check
210 * we are still connected.
212 static int ircomm_lmp_data_indication(void *instance
, void *sap
,
215 struct ircomm_cb
*self
= (struct ircomm_cb
*) instance
;
217 IRDA_ASSERT(self
!= NULL
, return -1;);
218 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return -1;);
219 IRDA_ASSERT(skb
!= NULL
, return -1;);
221 ircomm_do_event(self
, IRCOMM_LMP_DATA_INDICATION
, skb
, NULL
);
223 /* Drop reference count - see ircomm_tty_data_indication(). */
230 * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
231 * max_header_size, skb)
233 * Connection has been confirmed by peer device
236 static void ircomm_lmp_connect_confirm(void *instance
, void *sap
,
237 struct qos_info
*qos
,
239 __u8 max_header_size
,
242 struct ircomm_cb
*self
= (struct ircomm_cb
*) instance
;
243 struct ircomm_info info
;
245 IRDA_ASSERT(self
!= NULL
, return;);
246 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
247 IRDA_ASSERT(skb
!= NULL
, return;);
248 IRDA_ASSERT(qos
!= NULL
, return;);
250 info
.max_data_size
= max_seg_size
;
251 info
.max_header_size
= max_header_size
;
254 ircomm_do_event(self
, IRCOMM_LMP_CONNECT_CONFIRM
, skb
, &info
);
256 /* Drop reference count - see ircomm_tty_connect_confirm(). */
261 * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
262 * max_header_size, skb)
264 * Peer device wants to make a connection with us
267 static void ircomm_lmp_connect_indication(void *instance
, void *sap
,
268 struct qos_info
*qos
,
270 __u8 max_header_size
,
273 struct ircomm_cb
*self
= (struct ircomm_cb
*)instance
;
274 struct ircomm_info info
;
276 IRDA_ASSERT(self
!= NULL
, return;);
277 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
278 IRDA_ASSERT(skb
!= NULL
, return;);
279 IRDA_ASSERT(qos
!= NULL
, return;);
281 info
.max_data_size
= max_seg_size
;
282 info
.max_header_size
= max_header_size
;
285 ircomm_do_event(self
, IRCOMM_LMP_CONNECT_INDICATION
, skb
, &info
);
287 /* Drop reference count - see ircomm_tty_connect_indication(). */
292 * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
294 * Peer device has closed the connection, or the link went down for some
297 static void ircomm_lmp_disconnect_indication(void *instance
, void *sap
,
301 struct ircomm_cb
*self
= (struct ircomm_cb
*) instance
;
302 struct ircomm_info info
;
304 IRDA_ASSERT(self
!= NULL
, return;);
305 IRDA_ASSERT(self
->magic
== IRCOMM_MAGIC
, return;);
307 info
.reason
= reason
;
309 ircomm_do_event(self
, IRCOMM_LMP_DISCONNECT_INDICATION
, skb
, &info
);
311 /* Drop reference count - see ircomm_tty_disconnect_indication(). */
316 * Function ircomm_open_lsap (self)
318 * Open LSAP. This function will only be used when using "raw" services
321 int ircomm_open_lsap(struct ircomm_cb
*self
)
325 /* Register callbacks */
326 irda_notify_init(¬ify
);
327 notify
.data_indication
= ircomm_lmp_data_indication
;
328 notify
.connect_confirm
= ircomm_lmp_connect_confirm
;
329 notify
.connect_indication
= ircomm_lmp_connect_indication
;
330 notify
.disconnect_indication
= ircomm_lmp_disconnect_indication
;
331 notify
.instance
= self
;
332 strlcpy(notify
.name
, "IrCOMM", sizeof(notify
.name
));
334 self
->lsap
= irlmp_open_lsap(LSAP_ANY
, ¬ify
, 0);
336 pr_debug("%sfailed to allocate tsap\n", __func__
);
339 self
->slsap_sel
= self
->lsap
->slsap_sel
;
342 * Initialize the call-table for issuing commands
344 self
->issue
.data_request
= ircomm_lmp_data_request
;
345 self
->issue
.connect_request
= ircomm_lmp_connect_request
;
346 self
->issue
.connect_response
= ircomm_lmp_connect_response
;
347 self
->issue
.disconnect_request
= ircomm_lmp_disconnect_request
;