Linux 2.6.19-rc6
[cris-mirror.git] / net / irda / ircomm / ircomm_lmp.c
blobc8e0d89ee11fd3e9443767fd875d05372453bcc5
1 /*********************************************************************
2 *
3 * 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
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,
29 * MA 02111-1307 USA
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>
46 * Function ircomm_lmp_connect_request (self, userdata)
51 static int ircomm_lmp_connect_request(struct ircomm_cb *self,
52 struct sk_buff *userdata,
53 struct ircomm_info *info)
55 int ret = 0;
57 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
59 /* Don't forget to refcount it - should be NULL anyway */
60 if(userdata)
61 skb_get(userdata);
63 ret = irlmp_connect_request(self->lsap, info->dlsap_sel,
64 info->saddr, info->daddr, NULL, userdata);
65 return ret;
69 * Function ircomm_lmp_connect_response (self, skb)
74 static int ircomm_lmp_connect_response(struct ircomm_cb *self,
75 struct sk_buff *userdata)
77 struct sk_buff *tx_skb;
78 int ret;
80 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
82 /* Any userdata supplied? */
83 if (userdata == NULL) {
84 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
85 if (!tx_skb)
86 return -ENOMEM;
88 /* Reserve space for MUX and LAP header */
89 skb_reserve(tx_skb, LMP_MAX_HEADER);
90 } else {
91 /*
92 * Check that the client has reserved enough space for
93 * headers
95 IRDA_ASSERT(skb_headroom(userdata) >= LMP_MAX_HEADER,
96 return -1;);
98 /* Don't forget to refcount it - should be NULL anyway */
99 skb_get(userdata);
100 tx_skb = userdata;
103 ret = irlmp_connect_response(self->lsap, tx_skb);
105 return 0;
108 static int ircomm_lmp_disconnect_request(struct ircomm_cb *self,
109 struct sk_buff *userdata,
110 struct ircomm_info *info)
112 struct sk_buff *tx_skb;
113 int ret;
115 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
117 if (!userdata) {
118 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
119 if (!tx_skb)
120 return -ENOMEM;
122 /* Reserve space for MUX and LAP header */
123 skb_reserve(tx_skb, LMP_MAX_HEADER);
124 userdata = tx_skb;
125 } else {
126 /* Don't forget to refcount it - should be NULL anyway */
127 skb_get(userdata);
130 ret = irlmp_disconnect_request(self->lsap, userdata);
132 return ret;
136 * Function ircomm_lmp_flow_control (skb)
138 * This function is called when a data frame we have sent to IrLAP has
139 * been deallocated. We do this to make sure we don't flood IrLAP with
140 * frames, since we are not using the IrTTP flow control mechanism
142 static void ircomm_lmp_flow_control(struct sk_buff *skb)
144 struct irda_skb_cb *cb;
145 struct ircomm_cb *self;
146 int line;
148 IRDA_ASSERT(skb != NULL, return;);
150 cb = (struct irda_skb_cb *) skb->cb;
152 IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
154 line = cb->line;
156 self = (struct ircomm_cb *) hashbin_lock_find(ircomm, line, NULL);
157 if (!self) {
158 IRDA_DEBUG(2, "%s(), didn't find myself\n", __FUNCTION__ );
159 return;
162 IRDA_ASSERT(self != NULL, return;);
163 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
165 self->pkt_count--;
167 if ((self->pkt_count < 2) && (self->flow_status == FLOW_STOP)) {
168 IRDA_DEBUG(2, "%s(), asking TTY to start again!\n", __FUNCTION__ );
169 self->flow_status = FLOW_START;
170 if (self->notify.flow_indication)
171 self->notify.flow_indication(self->notify.instance,
172 self, FLOW_START);
177 * Function ircomm_lmp_data_request (self, userdata)
179 * Send data frame to peer device
182 static int ircomm_lmp_data_request(struct ircomm_cb *self,
183 struct sk_buff *skb,
184 int not_used)
186 struct irda_skb_cb *cb;
187 int ret;
189 IRDA_ASSERT(skb != NULL, return -1;);
191 cb = (struct irda_skb_cb *) skb->cb;
193 cb->line = self->line;
195 IRDA_DEBUG(4, "%s(), sending frame\n", __FUNCTION__ );
197 /* Don't forget to refcount it - see ircomm_tty_do_softint() */
198 skb_get(skb);
200 skb->destructor = ircomm_lmp_flow_control;
202 if ((self->pkt_count++ > 7) && (self->flow_status == FLOW_START)) {
203 IRDA_DEBUG(2, "%s(), asking TTY to slow down!\n", __FUNCTION__ );
204 self->flow_status = FLOW_STOP;
205 if (self->notify.flow_indication)
206 self->notify.flow_indication(self->notify.instance,
207 self, FLOW_STOP);
209 ret = irlmp_data_request(self->lsap, skb);
210 if (ret) {
211 IRDA_ERROR("%s(), failed\n", __FUNCTION__);
212 /* irlmp_data_request already free the packet */
215 return ret;
219 * Function ircomm_lmp_data_indication (instance, sap, skb)
221 * Incoming data which we must deliver to the state machine, to check
222 * we are still connected.
224 static int ircomm_lmp_data_indication(void *instance, void *sap,
225 struct sk_buff *skb)
227 struct ircomm_cb *self = (struct ircomm_cb *) instance;
229 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
231 IRDA_ASSERT(self != NULL, return -1;);
232 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
233 IRDA_ASSERT(skb != NULL, return -1;);
235 ircomm_do_event(self, IRCOMM_LMP_DATA_INDICATION, skb, NULL);
237 /* Drop reference count - see ircomm_tty_data_indication(). */
238 dev_kfree_skb(skb);
240 return 0;
244 * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
245 * max_header_size, skb)
247 * Connection has been confirmed by peer device
250 static void ircomm_lmp_connect_confirm(void *instance, void *sap,
251 struct qos_info *qos,
252 __u32 max_seg_size,
253 __u8 max_header_size,
254 struct sk_buff *skb)
256 struct ircomm_cb *self = (struct ircomm_cb *) instance;
257 struct ircomm_info info;
259 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
261 IRDA_ASSERT(self != NULL, return;);
262 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
263 IRDA_ASSERT(skb != NULL, return;);
264 IRDA_ASSERT(qos != NULL, return;);
266 info.max_data_size = max_seg_size;
267 info.max_header_size = max_header_size;
268 info.qos = qos;
270 ircomm_do_event(self, IRCOMM_LMP_CONNECT_CONFIRM, skb, &info);
272 /* Drop reference count - see ircomm_tty_connect_confirm(). */
273 dev_kfree_skb(skb);
277 * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
278 * max_header_size, skb)
280 * Peer device wants to make a connection with us
283 static void ircomm_lmp_connect_indication(void *instance, void *sap,
284 struct qos_info *qos,
285 __u32 max_seg_size,
286 __u8 max_header_size,
287 struct sk_buff *skb)
289 struct ircomm_cb *self = (struct ircomm_cb *)instance;
290 struct ircomm_info info;
292 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
294 IRDA_ASSERT(self != NULL, return;);
295 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
296 IRDA_ASSERT(skb != NULL, return;);
297 IRDA_ASSERT(qos != NULL, return;);
299 info.max_data_size = max_seg_size;
300 info.max_header_size = max_header_size;
301 info.qos = qos;
303 ircomm_do_event(self, IRCOMM_LMP_CONNECT_INDICATION, skb, &info);
305 /* Drop reference count - see ircomm_tty_connect_indication(). */
306 dev_kfree_skb(skb);
310 * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
312 * Peer device has closed the connection, or the link went down for some
313 * other reason
315 static void ircomm_lmp_disconnect_indication(void *instance, void *sap,
316 LM_REASON reason,
317 struct sk_buff *skb)
319 struct ircomm_cb *self = (struct ircomm_cb *) instance;
320 struct ircomm_info info;
322 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
324 IRDA_ASSERT(self != NULL, return;);
325 IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
327 info.reason = reason;
329 ircomm_do_event(self, IRCOMM_LMP_DISCONNECT_INDICATION, skb, &info);
331 /* Drop reference count - see ircomm_tty_disconnect_indication(). */
332 if(skb)
333 dev_kfree_skb(skb);
336 * Function ircomm_open_lsap (self)
338 * Open LSAP. This function will only be used when using "raw" services
341 int ircomm_open_lsap(struct ircomm_cb *self)
343 notify_t notify;
345 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
347 /* Register callbacks */
348 irda_notify_init(&notify);
349 notify.data_indication = ircomm_lmp_data_indication;
350 notify.connect_confirm = ircomm_lmp_connect_confirm;
351 notify.connect_indication = ircomm_lmp_connect_indication;
352 notify.disconnect_indication = ircomm_lmp_disconnect_indication;
353 notify.instance = self;
354 strlcpy(notify.name, "IrCOMM", sizeof(notify.name));
356 self->lsap = irlmp_open_lsap(LSAP_ANY, &notify, 0);
357 if (!self->lsap) {
358 IRDA_DEBUG(0,"%sfailed to allocate tsap\n", __FUNCTION__ );
359 return -1;
361 self->slsap_sel = self->lsap->slsap_sel;
364 * Initialize the call-table for issuing commands
366 self->issue.data_request = ircomm_lmp_data_request;
367 self->issue.connect_request = ircomm_lmp_connect_request;
368 self->issue.connect_response = ircomm_lmp_connect_response;
369 self->issue.disconnect_request = ircomm_lmp_disconnect_request;
371 return 0;