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, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 ********************************************************************/
33 #include <linux/sched.h>
34 #include <linux/init.h>
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 */
41 #include <net/irda/ircomm_event.h>
42 #include <net/irda/ircomm_lmp.h>
45 * Function ircomm_open_lsap (self)
47 * Open LSAP. This function will only be used when using "raw" services
50 int ircomm_open_lsap(struct ircomm_cb *self)
54 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
56 /* Register callbacks */
57 irda_notify_init(¬ify);
58 notify.data_indication = ircomm_lmp_data_indication;
59 notify.connect_confirm = ircomm_lmp_connect_confirm;
60 notify.connect_indication = ircomm_lmp_connect_indication;
61 notify.disconnect_indication = ircomm_lmp_disconnect_indication;
62 notify.instance = self;
63 strlcpy(notify.name, "IrCOMM", sizeof(notify.name));
65 self->lsap = irlmp_open_lsap(LSAP_ANY, ¬ify, 0);
67 IRDA_DEBUG(0,"%sfailed to allocate tsap\n", __FUNCTION__ );
70 self->slsap_sel = self->lsap->slsap_sel;
73 * Initialize the call-table for issuing commands
75 self->issue.data_request = ircomm_lmp_data_request;
76 self->issue.connect_request = ircomm_lmp_connect_request;
77 self->issue.connect_response = ircomm_lmp_connect_response;
78 self->issue.disconnect_request = ircomm_lmp_disconnect_request;
84 * Function ircomm_lmp_connect_request (self, userdata)
89 int ircomm_lmp_connect_request(struct ircomm_cb *self,
90 struct sk_buff *userdata,
91 struct ircomm_info *info)
95 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
97 /* Don't forget to refcount it - should be NULL anyway */
101 ret = irlmp_connect_request(self->lsap, info->dlsap_sel,
102 info->saddr, info->daddr, NULL, userdata);
107 * Function ircomm_lmp_connect_response (self, skb)
112 int ircomm_lmp_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
114 struct sk_buff *tx_skb;
117 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
119 /* Any userdata supplied? */
120 if (userdata == NULL) {
121 tx_skb = dev_alloc_skb(64);
125 /* Reserve space for MUX and LAP header */
126 skb_reserve(tx_skb, LMP_MAX_HEADER);
129 * Check that the client has reserved enough space for
132 ASSERT(skb_headroom(userdata) >= LMP_MAX_HEADER, return -1;);
134 /* Don't forget to refcount it - should be NULL anyway */
139 ret = irlmp_connect_response(self->lsap, tx_skb);
144 int ircomm_lmp_disconnect_request(struct ircomm_cb *self,
145 struct sk_buff *userdata,
146 struct ircomm_info *info)
148 struct sk_buff *tx_skb;
151 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
154 tx_skb = dev_alloc_skb(64);
158 /* Reserve space for MUX and LAP header */
159 skb_reserve(tx_skb, LMP_MAX_HEADER);
162 /* Don't forget to refcount it - should be NULL anyway */
166 ret = irlmp_disconnect_request(self->lsap, userdata);
172 * Function ircomm_lmp_flow_control (skb)
174 * This function is called when a data frame we have sent to IrLAP has
175 * been deallocated. We do this to make sure we don't flood IrLAP with
176 * frames, since we are not using the IrTTP flow control mechanism
178 void ircomm_lmp_flow_control(struct sk_buff *skb)
180 struct irda_skb_cb *cb;
181 struct ircomm_cb *self;
184 ASSERT(skb != NULL, return;);
186 cb = (struct irda_skb_cb *) skb->cb;
188 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
192 self = (struct ircomm_cb *) hashbin_lock_find(ircomm, line, NULL);
194 IRDA_DEBUG(2, "%s(), didn't find myself\n", __FUNCTION__ );
198 ASSERT(self != NULL, return;);
199 ASSERT(self->magic == IRCOMM_MAGIC, return;);
203 if ((self->pkt_count < 2) && (self->flow_status == FLOW_STOP)) {
204 IRDA_DEBUG(2, "%s(), asking TTY to start again!\n", __FUNCTION__ );
205 self->flow_status = FLOW_START;
206 if (self->notify.flow_indication)
207 self->notify.flow_indication(self->notify.instance,
213 * Function ircomm_lmp_data_request (self, userdata)
215 * Send data frame to peer device
218 int ircomm_lmp_data_request(struct ircomm_cb *self, struct sk_buff *skb,
221 struct irda_skb_cb *cb;
224 ASSERT(skb != NULL, return -1;);
226 cb = (struct irda_skb_cb *) skb->cb;
228 cb->line = self->line;
230 IRDA_DEBUG(4, "%s(), sending frame\n", __FUNCTION__ );
232 /* Don't forget to refcount it - see ircomm_tty_do_softint() */
235 skb->destructor = ircomm_lmp_flow_control;
237 if ((self->pkt_count++ > 7) && (self->flow_status == FLOW_START)) {
238 IRDA_DEBUG(2, "%s(), asking TTY to slow down!\n", __FUNCTION__ );
239 self->flow_status = FLOW_STOP;
240 if (self->notify.flow_indication)
241 self->notify.flow_indication(self->notify.instance,
244 ret = irlmp_data_request(self->lsap, skb);
246 ERROR("%s(), failed\n", __FUNCTION__);
247 /* irlmp_data_request already free the packet */
254 * Function ircomm_lmp_data_indication (instance, sap, skb)
256 * Incoming data which we must deliver to the state machine, to check
257 * we are still connected.
259 int ircomm_lmp_data_indication(void *instance, void *sap,
262 struct ircomm_cb *self = (struct ircomm_cb *) instance;
264 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
266 ASSERT(self != NULL, return -1;);
267 ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
268 ASSERT(skb != NULL, return -1;);
270 ircomm_do_event(self, IRCOMM_LMP_DATA_INDICATION, skb, NULL);
272 /* Drop reference count - see ircomm_tty_data_indication(). */
279 * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
280 * max_header_size, skb)
282 * Connection has been confirmed by peer device
285 void ircomm_lmp_connect_confirm(void *instance, void *sap,
286 struct qos_info *qos,
288 __u8 max_header_size,
291 struct ircomm_cb *self = (struct ircomm_cb *) instance;
292 struct ircomm_info info;
294 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
296 ASSERT(self != NULL, return;);
297 ASSERT(self->magic == IRCOMM_MAGIC, return;);
298 ASSERT(skb != NULL, return;);
299 ASSERT(qos != NULL, return;);
301 info.max_data_size = max_seg_size;
302 info.max_header_size = max_header_size;
305 ircomm_do_event(self, IRCOMM_LMP_CONNECT_CONFIRM, skb, &info);
307 /* Drop reference count - see ircomm_tty_connect_confirm(). */
312 * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
313 * max_header_size, skb)
315 * Peer device wants to make a connection with us
318 void ircomm_lmp_connect_indication(void *instance, void *sap,
319 struct qos_info *qos,
321 __u8 max_header_size,
324 struct ircomm_cb *self = (struct ircomm_cb *)instance;
325 struct ircomm_info info;
327 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
329 ASSERT(self != NULL, return;);
330 ASSERT(self->magic == IRCOMM_MAGIC, return;);
331 ASSERT(skb != NULL, return;);
332 ASSERT(qos != NULL, return;);
334 info.max_data_size = max_seg_size;
335 info.max_header_size = max_header_size;
338 ircomm_do_event(self, IRCOMM_LMP_CONNECT_INDICATION, skb, &info);
340 /* Drop reference count - see ircomm_tty_connect_indication(). */
345 * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
347 * Peer device has closed the connection, or the link went down for some
350 void ircomm_lmp_disconnect_indication(void *instance, void *sap,
354 struct ircomm_cb *self = (struct ircomm_cb *) instance;
355 struct ircomm_info info;
357 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
359 ASSERT(self != NULL, return;);
360 ASSERT(self->magic == IRCOMM_MAGIC, return;);
362 info.reason = reason;
364 ircomm_do_event(self, IRCOMM_LMP_DISCONNECT_INDICATION, skb, &info);
366 /* Drop reference count - see ircomm_tty_disconnect_indication(). */