ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / irda / ircomm / ircomm_lmp.c
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
12  * 
13  *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
14  *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15  *     
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.
20  * 
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.
25  * 
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
30  *     
31  ********************************************************************/
32
33 #include <linux/sched.h>
34 #include <linux/init.h>
35
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  * Function ircomm_open_lsap (self)
46  *
47  *    Open LSAP. This function will only be used when using "raw" services
48  *
49  */
50 int ircomm_open_lsap(struct ircomm_cb *self)
51 {
52         notify_t notify;
53         
54         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
55         
56         /* Register callbacks */
57         irda_notify_init(&notify);
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));
64
65         self->lsap = irlmp_open_lsap(LSAP_ANY, &notify, 0);
66         if (!self->lsap) {
67                 IRDA_DEBUG(0,"%sfailed to allocate tsap\n", __FUNCTION__ );
68                 return -1;
69         }
70         self->slsap_sel = self->lsap->slsap_sel;
71
72         /*
73          *  Initialize the call-table for issuing commands
74          */
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;
79
80         return 0;
81 }
82
83 /*
84  * Function ircomm_lmp_connect_request (self, userdata)
85  *
86  *    
87  *
88  */
89 int ircomm_lmp_connect_request(struct ircomm_cb *self, 
90                                struct sk_buff *userdata, 
91                                struct ircomm_info *info)
92 {
93         int ret = 0;
94
95         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
96
97         /* Don't forget to refcount it - should be NULL anyway */
98         if(userdata)
99                 skb_get(userdata);
100
101         ret = irlmp_connect_request(self->lsap, info->dlsap_sel,
102                                     info->saddr, info->daddr, NULL, userdata); 
103         return ret;
104 }       
105
106 /*
107  * Function ircomm_lmp_connect_response (self, skb)
108  *
109  *    
110  *
111  */
112 int ircomm_lmp_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
113 {
114         struct sk_buff *tx_skb;
115         int ret;
116
117         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
118         
119         /* Any userdata supplied? */
120         if (userdata == NULL) {
121                 tx_skb = dev_alloc_skb(64);
122                 if (!tx_skb)
123                         return -ENOMEM;
124
125                 /* Reserve space for MUX and LAP header */
126                 skb_reserve(tx_skb, LMP_MAX_HEADER);
127         } else {
128                 /*  
129                  *  Check that the client has reserved enough space for 
130                  *  headers
131                  */
132                 ASSERT(skb_headroom(userdata) >= LMP_MAX_HEADER, return -1;);
133
134                 /* Don't forget to refcount it - should be NULL anyway */
135                 skb_get(userdata);
136                 tx_skb = userdata;
137         }
138
139         ret = irlmp_connect_response(self->lsap, tx_skb);
140
141         return 0;
142 }
143
144 int ircomm_lmp_disconnect_request(struct ircomm_cb *self, 
145                                   struct sk_buff *userdata, 
146                                   struct ircomm_info *info)
147 {
148         struct sk_buff *tx_skb;
149         int ret;
150
151         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
152
153         if (!userdata) {
154                 tx_skb = dev_alloc_skb(64);
155                 if (!tx_skb)
156                         return -ENOMEM;
157                 
158                 /*  Reserve space for MUX and LAP header */
159                 skb_reserve(tx_skb, LMP_MAX_HEADER);            
160                 userdata = tx_skb;
161         } else {
162                 /* Don't forget to refcount it - should be NULL anyway */
163                 skb_get(userdata);
164         }
165
166         ret = irlmp_disconnect_request(self->lsap, userdata);
167
168         return ret;
169 }
170
171 /*
172  * Function ircomm_lmp_flow_control (skb)
173  *
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
177  */
178 void ircomm_lmp_flow_control(struct sk_buff *skb)
179 {
180         struct irda_skb_cb *cb;
181         struct ircomm_cb *self;
182         int line;
183
184         ASSERT(skb != NULL, return;);
185
186         cb = (struct irda_skb_cb *) skb->cb;
187
188         IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
189  
190         line = cb->line;
191
192         self = (struct ircomm_cb *) hashbin_lock_find(ircomm, line, NULL);
193         if (!self) {
194                 IRDA_DEBUG(2, "%s(), didn't find myself\n", __FUNCTION__ );
195                 return;
196         }
197
198         ASSERT(self != NULL, return;);
199         ASSERT(self->magic == IRCOMM_MAGIC, return;);
200
201         self->pkt_count--;
202
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, 
208                                                      self, FLOW_START);
209         }
210 }
211     
212 /*
213  * Function ircomm_lmp_data_request (self, userdata)
214  *
215  *    Send data frame to peer device
216  *
217  */
218 int ircomm_lmp_data_request(struct ircomm_cb *self, struct sk_buff *skb, 
219                             int not_used)
220 {
221         struct irda_skb_cb *cb;
222         int ret;
223
224         ASSERT(skb != NULL, return -1;);
225
226         cb = (struct irda_skb_cb *) skb->cb;
227         
228         cb->line = self->line;
229
230         IRDA_DEBUG(4, "%s(), sending frame\n", __FUNCTION__ );
231
232         /* Don't forget to refcount it - see ircomm_tty_do_softint() */
233         skb_get(skb);
234
235         skb->destructor = ircomm_lmp_flow_control;
236
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, 
242                                                      self, FLOW_STOP);
243         }
244         ret = irlmp_data_request(self->lsap, skb);
245         if (ret) {
246                 ERROR("%s(), failed\n", __FUNCTION__);
247                 /* irlmp_data_request already free the packet */
248         }
249
250         return ret;
251 }
252
253 /*
254  * Function ircomm_lmp_data_indication (instance, sap, skb)
255  *
256  *    Incoming data which we must deliver to the state machine, to check
257  *    we are still connected.
258  */
259 int ircomm_lmp_data_indication(void *instance, void *sap,
260                                struct sk_buff *skb)
261 {
262         struct ircomm_cb *self = (struct ircomm_cb *) instance;
263
264         IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
265         
266         ASSERT(self != NULL, return -1;);
267         ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
268         ASSERT(skb != NULL, return -1;);
269         
270         ircomm_do_event(self, IRCOMM_LMP_DATA_INDICATION, skb, NULL);
271
272         /* Drop reference count - see ircomm_tty_data_indication(). */
273         dev_kfree_skb(skb);
274
275         return 0;
276 }
277
278 /*
279  * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size, 
280  *                                       max_header_size, skb)
281  *
282  *    Connection has been confirmed by peer device
283  *
284  */
285 void ircomm_lmp_connect_confirm(void *instance, void *sap,
286                                 struct qos_info *qos, 
287                                 __u32 max_seg_size, 
288                                 __u8 max_header_size,
289                                 struct sk_buff *skb)
290 {
291         struct ircomm_cb *self = (struct ircomm_cb *) instance;
292         struct ircomm_info info;
293
294         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
295
296         ASSERT(self != NULL, return;);
297         ASSERT(self->magic == IRCOMM_MAGIC, return;);
298         ASSERT(skb != NULL, return;);
299         ASSERT(qos != NULL, return;);
300
301         info.max_data_size = max_seg_size;
302         info.max_header_size = max_header_size;
303         info.qos = qos;
304
305         ircomm_do_event(self, IRCOMM_LMP_CONNECT_CONFIRM, skb, &info);
306
307         /* Drop reference count - see ircomm_tty_connect_confirm(). */
308         dev_kfree_skb(skb);
309 }
310
311 /*
312  * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
313  *                                         max_header_size, skb)
314  *
315  *    Peer device wants to make a connection with us
316  *
317  */
318 void ircomm_lmp_connect_indication(void *instance, void *sap,
319                                    struct qos_info *qos,
320                                    __u32 max_seg_size,
321                                    __u8 max_header_size,
322                                    struct sk_buff *skb)
323 {
324         struct ircomm_cb *self = (struct ircomm_cb *)instance;
325         struct ircomm_info info;
326
327         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
328
329         ASSERT(self != NULL, return;);
330         ASSERT(self->magic == IRCOMM_MAGIC, return;);
331         ASSERT(skb != NULL, return;);
332         ASSERT(qos != NULL, return;);
333
334         info.max_data_size = max_seg_size;
335         info.max_header_size = max_header_size;
336         info.qos = qos;
337
338         ircomm_do_event(self, IRCOMM_LMP_CONNECT_INDICATION, skb, &info);
339
340         /* Drop reference count - see ircomm_tty_connect_indication(). */
341         dev_kfree_skb(skb);
342 }
343
344 /*
345  * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
346  *
347  *    Peer device has closed the connection, or the link went down for some
348  *    other reason
349  */
350 void ircomm_lmp_disconnect_indication(void *instance, void *sap, 
351                                       LM_REASON reason,
352                                       struct sk_buff *skb)
353 {
354         struct ircomm_cb *self = (struct ircomm_cb *) instance;
355         struct ircomm_info info;
356
357         IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
358
359         ASSERT(self != NULL, return;);
360         ASSERT(self->magic == IRCOMM_MAGIC, return;);
361
362         info.reason = reason;
363
364         ircomm_do_event(self, IRCOMM_LMP_DISCONNECT_INDICATION, skb, &info);
365
366         /* Drop reference count - see ircomm_tty_disconnect_indication(). */
367         if(skb)
368                 dev_kfree_skb(skb);
369 }