1 /*********************************************************************
3 * Filename: irlmp_frame.c
5 * Description: IrLMP frame implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Aug 19 02:09:59 1997
9 * Modified at: Mon Dec 13 13:41:12 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>
13 * All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/config.h>
27 #include <linux/skbuff.h>
28 #include <linux/kernel.h>
30 #include <net/irda/irda.h>
31 #include <net/irda/irlap.h>
32 #include <net/irda/timer.h>
33 #include <net/irda/irlmp.h>
34 #include <net/irda/irlmp_frame.h>
35 #include <net/irda/discovery.h>
37 #define DISCO_SMALL_DELAY 250 /* Delay for some discoveries in ms */
38 struct timer_list disco_delay
; /* The timer associated */
40 static struct lsap_cb
*irlmp_find_lsap(struct lap_cb
*self
, __u8 dlsap
,
41 __u8 slsap
, int status
, hashbin_t
*);
43 inline void irlmp_send_data_pdu(struct lap_cb
*self
, __u8 dlsap
, __u8 slsap
,
44 int expedited
, struct sk_buff
*skb
)
50 IRDA_DEBUG(4, __FUNCTION__
"(), sending expedited data\n");
51 irlap_data_request(self
->irlap
, skb
, TRUE
);
53 irlap_data_request(self
->irlap
, skb
, FALSE
);
57 * Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)
59 * Send Link Control Frame to IrLAP
61 void irlmp_send_lcf_pdu(struct lap_cb
*self
, __u8 dlsap
, __u8 slsap
,
62 __u8 opcode
, struct sk_buff
*skb
)
66 IRDA_DEBUG(2, __FUNCTION__
"()\n");
68 ASSERT(self
!= NULL
, return;);
69 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
70 ASSERT(skb
!= NULL
, return;);
74 frame
[0] = dlsap
| CONTROL_BIT
;
79 if (opcode
== DISCONNECT
)
80 frame
[3] = 0x01; /* Service user request */
82 frame
[3] = 0x00; /* rsvd */
84 irlap_data_request(self
->irlap
, skb
, FALSE
);
88 * Function irlmp_input (skb)
90 * Used by IrLAP to pass received data frames to IrLMP layer
93 void irlmp_link_data_indication(struct lap_cb
*self
, struct sk_buff
*skb
,
97 __u8 slsap_sel
; /* Source (this) LSAP address */
98 __u8 dlsap_sel
; /* Destination LSAP address */
101 IRDA_DEBUG(4, __FUNCTION__
"()\n");
103 ASSERT(self
!= NULL
, return;);
104 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
105 ASSERT(skb
->len
> 2, return;);
110 * The next statements may be confusing, but we do this so that
111 * destination LSAP of received frame is source LSAP in our view
113 slsap_sel
= fp
[0] & LSAP_MASK
;
117 * Check if this is an incoming connection, since we must deal with
118 * it in a different way than other established connections.
120 if ((fp
[0] & CONTROL_BIT
) && (fp
[2] == CONNECT_CMD
)) {
121 IRDA_DEBUG(3, __FUNCTION__
"(), incoming connection, "
122 "source LSAP=%d, dest LSAP=%d\n",
123 slsap_sel
, dlsap_sel
);
125 /* Try to find LSAP among the unconnected LSAPs */
126 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, CONNECT_CMD
,
127 irlmp
->unconnected_lsaps
);
129 /* Maybe LSAP was already connected, so try one more time */
131 IRDA_DEBUG(1, __FUNCTION__
"(), incoming connection for LSAP already connected\n");
132 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, 0,
136 lsap
= irlmp_find_lsap(self
, dlsap_sel
, slsap_sel
, 0,
140 IRDA_DEBUG(2, "IrLMP, Sorry, no LSAP for received frame!\n");
141 IRDA_DEBUG(2, __FUNCTION__
142 "(), slsap_sel = %02x, dlsap_sel = %02x\n", slsap_sel
,
144 if (fp
[0] & CONTROL_BIT
) {
145 IRDA_DEBUG(2, __FUNCTION__
146 "(), received control frame %02x\n", fp
[2]);
148 IRDA_DEBUG(2, __FUNCTION__
"(), received data frame\n");
155 * Check if we received a control frame?
157 if (fp
[0] & CONTROL_BIT
) {
161 irlmp_do_lsap_event(lsap
, LM_CONNECT_INDICATION
, skb
);
164 irlmp_do_lsap_event(lsap
, LM_CONNECT_CONFIRM
, skb
);
167 IRDA_DEBUG(4, __FUNCTION__
168 "(), Disconnect indication!\n");
169 irlmp_do_lsap_event(lsap
, LM_DISCONNECT_INDICATION
,
173 IRDA_DEBUG(0, "Access mode cmd not implemented!\n");
177 IRDA_DEBUG(0, "Access mode cnf not implemented!\n");
181 IRDA_DEBUG(0, __FUNCTION__
182 "(), Unknown control frame %02x\n", fp
[2]);
186 } else if (unreliable
) {
187 /* Optimize and bypass the state machine if possible */
188 if (lsap
->lsap_state
== LSAP_DATA_TRANSFER_READY
)
189 irlmp_udata_indication(lsap
, skb
);
191 irlmp_do_lsap_event(lsap
, LM_UDATA_INDICATION
, skb
);
193 /* Optimize and bypass the state machine if possible */
194 if (lsap
->lsap_state
== LSAP_DATA_TRANSFER_READY
)
195 irlmp_data_indication(lsap
, skb
);
197 irlmp_do_lsap_event(lsap
, LM_DATA_INDICATION
, skb
);
202 * Function irlmp_link_unitdata_indication (self, skb)
207 #ifdef CONFIG_IRDA_ULTRA
208 void irlmp_link_unitdata_indication(struct lap_cb
*self
, struct sk_buff
*skb
)
210 struct lsap_cb
*lsap
;
211 __u8 slsap_sel
; /* Source (this) LSAP address */
212 __u8 dlsap_sel
; /* Destination LSAP address */
213 __u8 pid
; /* Protocol identifier */
216 IRDA_DEBUG(4, __FUNCTION__
"()\n");
218 ASSERT(self
!= NULL
, return;);
219 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
220 ASSERT(skb
->len
> 2, return;);
225 * The next statements may be confusing, but we do this so that
226 * destination LSAP of received frame is source LSAP in our view
228 slsap_sel
= fp
[0] & LSAP_MASK
;
233 IRDA_DEBUG(0, __FUNCTION__
"(), extension in PID not supp!\n");
239 /* Check if frame is addressed to the connectionless LSAP */
240 if ((slsap_sel
!= LSAP_CONNLESS
) || (dlsap_sel
!= LSAP_CONNLESS
)) {
241 IRDA_DEBUG(0, __FUNCTION__
"(), dropping frame!\n");
247 lsap
= (struct lsap_cb
*) hashbin_get_first(irlmp
->unconnected_lsaps
);
248 while (lsap
!= NULL
) {
250 * Check if source LSAP and dest LSAP selectors and PID match.
252 if ((lsap
->slsap_sel
== slsap_sel
) &&
253 (lsap
->dlsap_sel
== dlsap_sel
) &&
258 lsap
= (struct lsap_cb
*) hashbin_get_next(irlmp
->unconnected_lsaps
);
261 irlmp_connless_data_indication(lsap
, skb
);
263 IRDA_DEBUG(0, __FUNCTION__
"(), found no matching LSAP!\n");
267 #endif /* CONFIG_IRDA_ULTRA */
270 * Function irlmp_link_disconnect_indication (reason, userdata)
272 * IrLAP has disconnected
275 void irlmp_link_disconnect_indication(struct lap_cb
*lap
,
276 struct irlap_cb
*irlap
,
278 struct sk_buff
*userdata
)
280 IRDA_DEBUG(2, __FUNCTION__
"()\n");
282 ASSERT(lap
!= NULL
, return;);
283 ASSERT(lap
->magic
== LMP_LAP_MAGIC
, return;);
285 lap
->reason
= reason
;
286 lap
->daddr
= DEV_ADDR_ANY
;
288 /* FIXME: must do something with the userdata if any */
290 dev_kfree_skb(userdata
);
293 * Inform station state machine
295 irlmp_do_lap_event(lap
, LM_LAP_DISCONNECT_INDICATION
, NULL
);
299 * Function irlmp_link_connect_indication (qos)
301 * Incoming LAP connection!
304 void irlmp_link_connect_indication(struct lap_cb
*self
, __u32 saddr
,
305 __u32 daddr
, struct qos_info
*qos
,
308 IRDA_DEBUG(4, __FUNCTION__
"()\n");
310 /* Copy QoS settings for this session */
313 /* Update destination device address */
315 ASSERT(self
->saddr
== saddr
, return;);
317 irlmp_do_lap_event(self
, LM_LAP_CONNECT_INDICATION
, skb
);
321 * Function irlmp_link_connect_confirm (qos)
323 * LAP connection confirmed!
326 void irlmp_link_connect_confirm(struct lap_cb
*self
, struct qos_info
*qos
,
327 struct sk_buff
*userdata
)
329 IRDA_DEBUG(4, __FUNCTION__
"()\n");
331 ASSERT(self
!= NULL
, return;);
332 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
333 ASSERT(qos
!= NULL
, return;);
335 /* Don't need use the userdata for now */
337 dev_kfree_skb(userdata
);
339 /* Copy QoS settings for this session */
342 irlmp_do_lap_event(self
, LM_LAP_CONNECT_CONFIRM
, NULL
);
346 * Function irlmp_discovery_timeout (priv)
348 * Create a discovery event to the state machine (called after a delay)
350 * Note : irlmp_do_lap_event will handle the very rare case where the LAP
351 * is destroyed while we were sleeping.
353 static void irlmp_discovery_timeout(u_long priv
)
357 IRDA_DEBUG(2, __FUNCTION__
"()\n");
359 self
= (struct lap_cb
*) priv
;
360 ASSERT(self
!= NULL
, return;);
362 /* Just handle it the same way as a discovery confirm */
363 irlmp_do_lap_event(self
, LM_LAP_DISCOVERY_CONFIRM
, NULL
);
367 * Function irlmp_link_discovery_indication (self, log)
369 * Device is discovering us
371 * It's not an answer to our own discoveries, just another device trying
372 * to perform discovery, but we don't want to miss the opportunity
373 * to exploit this information, because :
374 * o We may not actively perform discovery (just passive discovery)
375 * o This type of discovery is much more reliable. In some cases, it
376 * seem that less than 50% of our discoveries get an answer, while
377 * we always get ~100% of these.
378 * o Make faster discovery, statistically divide time of discovery
379 * events by 2 (important for the latency aspect and user feel)
380 * However, when both devices discover each other, they might attempt to
381 * connect to each other, and it would create collisions on the medium.
382 * The trick here is to defer the event by a little delay to avoid both
383 * devices to jump in exactly at the same time...
385 * The delay is currently set to 0.25s, which leave enough time to perform
386 * a connection and don't interfer with next discovery (the lowest discovery
387 * period/timeout that may be set is 1s). The message triggering this
388 * event was the last of the discovery, so the medium is now free...
389 * Maybe more testing is needed to get the value right...
391 void irlmp_link_discovery_indication(struct lap_cb
*self
,
392 discovery_t
*discovery
)
394 ASSERT(self
!= NULL
, return;);
395 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
397 irlmp_add_discovery(irlmp
->cachelog
, discovery
);
399 /* If delay was activated, kill it! */
400 if(timer_pending(&disco_delay
))
401 del_timer(&disco_delay
);
402 /* Set delay timer to expire in 0.25s. */
403 disco_delay
.expires
= jiffies
+ (DISCO_SMALL_DELAY
* HZ
/1000);
404 disco_delay
.function
= irlmp_discovery_timeout
;
405 disco_delay
.data
= (unsigned long) self
;
406 add_timer(&disco_delay
);
410 * Function irlmp_link_discovery_confirm (self, log)
412 * Called by IrLAP with a list of discoveries after the discovery
413 * request has been carried out. A NULL log is received if IrLAP
414 * was unable to carry out the discovery request
417 void irlmp_link_discovery_confirm(struct lap_cb
*self
, hashbin_t
*log
)
419 IRDA_DEBUG(4, __FUNCTION__
"()\n");
421 ASSERT(self
!= NULL
, return;);
422 ASSERT(self
->magic
== LMP_LAP_MAGIC
, return;);
424 irlmp_add_discovery_log(irlmp
->cachelog
, log
);
426 /* If discovery delay was activated, kill it! */
427 if(timer_pending(&disco_delay
))
428 del_timer(&disco_delay
);
430 /* Propagate event to the state machine */
431 irlmp_do_lap_event(self
, LM_LAP_DISCOVERY_CONFIRM
, NULL
);
434 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
435 inline void irlmp_update_cache(struct lsap_cb
*self
)
437 /* Update cache entry */
438 irlmp
->cache
.dlsap_sel
= self
->dlsap_sel
;
439 irlmp
->cache
.slsap_sel
= self
->slsap_sel
;
440 irlmp
->cache
.lsap
= self
;
441 irlmp
->cache
.valid
= TRUE
;
446 * Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)
448 * Find handle assosiated with destination and source LSAP
451 static struct lsap_cb
*irlmp_find_lsap(struct lap_cb
*self
, __u8 dlsap_sel
,
452 __u8 slsap_sel
, int status
,
455 struct lsap_cb
*lsap
;
458 * Optimize for the common case. We assume that the last frame
459 * received is in the same connection as the last one, so check in
460 * cache first to avoid the linear search
462 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
463 if ((irlmp
->cache
.valid
) &&
464 (irlmp
->cache
.slsap_sel
== slsap_sel
) &&
465 (irlmp
->cache
.dlsap_sel
== dlsap_sel
))
467 return (irlmp
->cache
.lsap
);
470 lsap
= (struct lsap_cb
*) hashbin_get_first(queue
);
471 while (lsap
!= NULL
) {
473 * If this is an incomming connection, then the destination
474 * LSAP selector may have been specified as LM_ANY so that
475 * any client can connect. In that case we only need to check
476 * if the source LSAP (in our view!) match!
478 if ((status
== CONNECT_CMD
) &&
479 (lsap
->slsap_sel
== slsap_sel
) &&
480 (lsap
->dlsap_sel
== LSAP_ANY
))
482 lsap
->dlsap_sel
= dlsap_sel
;
484 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
485 irlmp_update_cache(lsap
);
490 * Check if source LSAP and dest LSAP selectors match.
492 if ((lsap
->slsap_sel
== slsap_sel
) &&
493 (lsap
->dlsap_sel
== dlsap_sel
))
495 #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
496 irlmp_update_cache(lsap
);
500 lsap
= (struct lsap_cb
*) hashbin_get_next(queue
);
503 /* Sorry not found! */