1 /*********************************************************************
2 *
3 * Filename: irlap.c
4 * Version: 1.0
5 * Description: IrLAP implementation for Linux
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Mon Aug 4 20:40:53 1997
9 * Modified at: Tue Dec 14 09:26:44 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 *
12 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
13 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
14 *
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.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
29 *
30 ********************************************************************/
31
32 #include <linux/config.h>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/skbuff.h>
36 #include <linux/delay.h>
37 #include <linux/proc_fs.h>
38 #include <linux/init.h>
39 #include <linux/random.h>
40
41 #include <net/irda/irda.h>
42 #include <net/irda/irda_device.h>
43 #include <net/irda/irqueue.h>
44 #include <net/irda/irlmp.h>
45 #include <net/irda/irlmp_frame.h>
46 #include <net/irda/irlap_frame.h>
47 #include <net/irda/irlap.h>
48 #include <net/irda/timer.h>
49 #include <net/irda/qos.h>
50
51 hashbin_t *irlap = NULL;
52 int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
53
54 /* This is the delay of missed pf period before generating an event
55 * to the application. The spec mandate 3 seconds, but in some cases
56 * it's way too long. - Jean II */
57 int sysctl_warn_noreply_time = 3;
58
59 extern void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb);
60 static void __irlap_close(struct irlap_cb *self);
61
62 #ifdef CONFIG_IRDA_DEBUG
63 static char *lap_reasons[] = {
64 "ERROR, NOT USED",
65 "LAP_DISC_INDICATION",
66 "LAP_NO_RESPONSE",
67 "LAP_RESET_INDICATION",
68 "LAP_FOUND_NONE",
69 "LAP_MEDIA_BUSY",
70 "LAP_PRIMARY_CONFLICT",
71 "ERROR, NOT USED",
72 };
73 #endif /* CONFIG_IRDA_DEBUG */
74
75 #ifdef CONFIG_PROC_FS
76 int irlap_proc_read(char *, char **, off_t, int);
77
78 #endif /* CONFIG_PROC_FS */
79
irlap_init(void)80 int __init irlap_init(void)
81 {
82 /* Allocate master array */
83 irlap = hashbin_new(HB_LOCAL);
84 if (irlap == NULL) {
85 ERROR("%s(), can't allocate irlap hashbin!\n", __FUNCTION__);
86 return -ENOMEM;
87 }
88
89 return 0;
90 }
91
irlap_cleanup(void)92 void irlap_cleanup(void)
93 {
94 ASSERT(irlap != NULL, return;);
95
96 hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
97 }
98
99 /*
100 * Function irlap_open (driver)
101 *
102 * Initialize IrLAP layer
103 *
104 */
irlap_open(struct net_device * dev,struct qos_info * qos,char * hw_name)105 struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos,
106 char * hw_name)
107 {
108 struct irlap_cb *self;
109
110 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
111
112 /* Initialize the irlap structure. */
113 self = kmalloc(sizeof(struct irlap_cb), GFP_KERNEL);
114 if (self == NULL)
115 return NULL;
116
117 memset(self, 0, sizeof(struct irlap_cb));
118 self->magic = LAP_MAGIC;
119
120 /* Make a binding between the layers */
121 self->netdev = dev;
122 self->qos_dev = qos;
123 /* Copy hardware name */
124 if(hw_name != NULL) {
125 strncpy(self->hw_name, hw_name, 2*IFNAMSIZ);
126 self->hw_name[2*IFNAMSIZ] = '\0';
127 } else {
128 self->hw_name[0] = '\0';
129 }
130
131 /* FIXME: should we get our own field? */
132 dev->atalk_ptr = self;
133
134 self->state = LAP_OFFLINE;
135
136 /* Initialize transmit queue */
137 skb_queue_head_init(&self->txq);
138 skb_queue_head_init(&self->txq_ultra);
139 skb_queue_head_init(&self->wx_list);
140
141 /* My unique IrLAP device address! */
142 get_random_bytes(&self->saddr, sizeof(self->saddr));
143 memcpy(dev->dev_addr, &self->saddr, 4);
144
145 init_timer(&self->slot_timer);
146 init_timer(&self->query_timer);
147 init_timer(&self->discovery_timer);
148 init_timer(&self->final_timer);
149 init_timer(&self->poll_timer);
150 init_timer(&self->wd_timer);
151 init_timer(&self->backoff_timer);
152 init_timer(&self->media_busy_timer);
153
154 irlap_apply_default_connection_parameters(self);
155
156 self->N3 = 3; /* # connections attemts to try before giving up */
157
158 self->state = LAP_NDM;
159
160 hashbin_insert(irlap, (irda_queue_t *) self, self->saddr, NULL);
161
162 irlmp_register_link(self, self->saddr, &self->notify);
163
164 return self;
165 }
166
167 /*
168 * Function __irlap_close (self)
169 *
170 * Remove IrLAP and all allocated memory. Stop any pending timers.
171 *
172 */
__irlap_close(struct irlap_cb * self)173 static void __irlap_close(struct irlap_cb *self)
174 {
175 ASSERT(self != NULL, return;);
176 ASSERT(self->magic == LAP_MAGIC, return;);
177
178 /* Stop timers */
179 del_timer(&self->slot_timer);
180 del_timer(&self->query_timer);
181 del_timer(&self->discovery_timer);
182 del_timer(&self->final_timer);
183 del_timer(&self->poll_timer);
184 del_timer(&self->wd_timer);
185 del_timer(&self->backoff_timer);
186 del_timer(&self->media_busy_timer);
187
188 irlap_flush_all_queues(self);
189
190 self->magic = 0;
191
192 kfree(self);
193 }
194
195 /*
196 * Function irlap_close (self)
197 *
198 * Remove IrLAP instance
199 *
200 */
irlap_close(struct irlap_cb * self)201 void irlap_close(struct irlap_cb *self)
202 {
203 struct irlap_cb *lap;
204
205 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
206
207 ASSERT(self != NULL, return;);
208 ASSERT(self->magic == LAP_MAGIC, return;);
209
210 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
211
212 irlmp_unregister_link(self->saddr);
213 self->notify.instance = NULL;
214
215 /* Be sure that we manage to remove ourself from the hash */
216 lap = hashbin_remove(irlap, self->saddr, NULL);
217 if (!lap) {
218 IRDA_DEBUG(1, "%s(), Didn't find myself!\n", __FUNCTION__);
219 return;
220 }
221 __irlap_close(lap);
222 }
223
224 /*
225 * Function irlap_connect_indication (self, skb)
226 *
227 * Another device is attempting to make a connection
228 *
229 */
irlap_connect_indication(struct irlap_cb * self,struct sk_buff * skb)230 void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb)
231 {
232 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
233
234 ASSERT(self != NULL, return;);
235 ASSERT(self->magic == LAP_MAGIC, return;);
236
237 irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
238
239 skb_get(skb); /*LEVEL4*/
240 irlmp_link_connect_indication(self->notify.instance, self->saddr,
241 self->daddr, &self->qos_tx, skb);
242 }
243
244 /*
245 * Function irlap_connect_response (self, skb)
246 *
247 * Service user has accepted incoming connection
248 *
249 */
irlap_connect_response(struct irlap_cb * self,struct sk_buff * skb)250 void irlap_connect_response(struct irlap_cb *self, struct sk_buff *skb)
251 {
252 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
253
254 irlap_do_event(self, CONNECT_RESPONSE, skb, NULL);
255 kfree_skb(skb);
256 }
257
258 /*
259 * Function irlap_connect_request (self, daddr, qos_user, sniff)
260 *
261 * Request connection with another device, sniffing is not implemented
262 * yet.
263 *
264 */
irlap_connect_request(struct irlap_cb * self,__u32 daddr,struct qos_info * qos_user,int sniff)265 void irlap_connect_request(struct irlap_cb *self, __u32 daddr,
266 struct qos_info *qos_user, int sniff)
267 {
268 IRDA_DEBUG(3, "%s(), daddr=0x%08x\n", __FUNCTION__, daddr);
269
270 ASSERT(self != NULL, return;);
271 ASSERT(self->magic == LAP_MAGIC, return;);
272
273 self->daddr = daddr;
274
275 /*
276 * If the service user specifies QoS values for this connection,
277 * then use them
278 */
279 irlap_init_qos_capabilities(self, qos_user);
280
281 if ((self->state == LAP_NDM) && !self->media_busy)
282 irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
283 else
284 self->connect_pending = TRUE;
285 }
286
287 /*
288 * Function irlap_connect_confirm (self, skb)
289 *
290 * Connection request has been accepted
291 *
292 */
irlap_connect_confirm(struct irlap_cb * self,struct sk_buff * skb)293 void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
294 {
295 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
296
297 ASSERT(self != NULL, return;);
298 ASSERT(self->magic == LAP_MAGIC, return;);
299
300 skb_get(skb); /*LEVEL4*/
301 irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
302 }
303
304 /*
305 * Function irlap_data_indication (self, skb)
306 *
307 * Received data frames from IR-port, so we just pass them up to
308 * IrLMP for further processing
309 *
310 */
irlap_data_indication(struct irlap_cb * self,struct sk_buff * skb,int unreliable)311 void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
312 int unreliable)
313 {
314 /* Hide LAP header from IrLMP layer */
315 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
316
317 skb_get(skb); /*LEVEL4*/
318 irlmp_link_data_indication(self->notify.instance, skb, unreliable);
319 }
320
321
322 /*
323 * Function irlap_data_request (self, skb)
324 *
325 * Queue data for transmission, must wait until XMIT state
326 *
327 */
irlap_data_request(struct irlap_cb * self,struct sk_buff * skb,int unreliable)328 void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb,
329 int unreliable)
330 {
331 ASSERT(self != NULL, return;);
332 ASSERT(self->magic == LAP_MAGIC, return;);
333
334 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
335
336 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
337 return;);
338 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
339
340 /*
341 * Must set frame format now so that the rest of the code knows
342 * if its dealing with an I or an UI frame
343 */
344 if (unreliable)
345 skb->data[1] = UI_FRAME;
346 else
347 skb->data[1] = I_FRAME;
348
349 /* Add at the end of the queue (keep ordering) - Jean II */
350 skb_queue_tail(&self->txq, skb);
351
352 /*
353 * Send event if this frame only if we are in the right state
354 * FIXME: udata should be sent first! (skb_queue_head?)
355 */
356 if ((self->state == LAP_XMIT_P) || (self->state == LAP_XMIT_S)) {
357 /* If we are not already processing the Tx queue, trigger
358 * transmission immediately - Jean II */
359 if((skb_queue_len(&self->txq) <= 1) && (!self->local_busy))
360 irlap_do_event(self, DATA_REQUEST, skb, NULL);
361 /* Otherwise, the packets will be sent normally at the
362 * next pf-poll - Jean II */
363 }
364 }
365
366 /*
367 * Function irlap_unitdata_request (self, skb)
368 *
369 * Send Ultra data. This is data that must be sent outside any connection
370 *
371 */
372 #ifdef CONFIG_IRDA_ULTRA
irlap_unitdata_request(struct irlap_cb * self,struct sk_buff * skb)373 void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
374 {
375 ASSERT(self != NULL, return;);
376 ASSERT(self->magic == LAP_MAGIC, return;);
377
378 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
379
380 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
381 return;);
382 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
383
384 skb->data[0] = CBROADCAST;
385 skb->data[1] = UI_FRAME;
386
387 skb_queue_tail(&self->txq_ultra, skb);
388
389 irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
390 }
391 #endif /*CONFIG_IRDA_ULTRA */
392
393 /*
394 * Function irlap_udata_indication (self, skb)
395 *
396 * Receive Ultra data. This is data that is received outside any connection
397 *
398 */
399 #ifdef CONFIG_IRDA_ULTRA
irlap_unitdata_indication(struct irlap_cb * self,struct sk_buff * skb)400 void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
401 {
402 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
403
404 ASSERT(self != NULL, return;);
405 ASSERT(self->magic == LAP_MAGIC, return;);
406 ASSERT(skb != NULL, return;);
407
408 /* Hide LAP header from IrLMP layer */
409 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
410
411 skb_get(skb); /*LEVEL4*/
412 irlmp_link_unitdata_indication(self->notify.instance, skb);
413 }
414 #endif /* CONFIG_IRDA_ULTRA */
415
416 /*
417 * Function irlap_disconnect_request (void)
418 *
419 * Request to disconnect connection by service user
420 */
irlap_disconnect_request(struct irlap_cb * self)421 void irlap_disconnect_request(struct irlap_cb *self)
422 {
423 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
424
425 ASSERT(self != NULL, return;);
426 ASSERT(self->magic == LAP_MAGIC, return;);
427
428 /* Don't disconnect until all data frames are successfully sent */
429 if (skb_queue_len(&self->txq) > 0) {
430 self->disconnect_pending = TRUE;
431
432 return;
433 }
434
435 /* Check if we are in the right state for disconnecting */
436 switch (self->state) {
437 case LAP_XMIT_P: /* FALLTROUGH */
438 case LAP_XMIT_S: /* FALLTROUGH */
439 case LAP_CONN: /* FALLTROUGH */
440 case LAP_RESET_WAIT: /* FALLTROUGH */
441 case LAP_RESET_CHECK:
442 irlap_do_event(self, DISCONNECT_REQUEST, NULL, NULL);
443 break;
444 default:
445 IRDA_DEBUG(2, "%s(), disconnect pending!\n", __FUNCTION__);
446 self->disconnect_pending = TRUE;
447 break;
448 }
449 }
450
451 /*
452 * Function irlap_disconnect_indication (void)
453 *
454 * Disconnect request from other device
455 *
456 */
irlap_disconnect_indication(struct irlap_cb * self,LAP_REASON reason)457 void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason)
458 {
459 IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, lap_reasons[reason]);
460
461 ASSERT(self != NULL, return;);
462 ASSERT(self->magic == LAP_MAGIC, return;);
463
464 /* Flush queues */
465 irlap_flush_all_queues(self);
466
467 switch (reason) {
468 case LAP_RESET_INDICATION:
469 IRDA_DEBUG(1, "%s(), Sending reset request!\n", __FUNCTION__);
470 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
471 break;
472 case LAP_NO_RESPONSE: /* FALLTROUGH */
473 case LAP_DISC_INDICATION: /* FALLTROUGH */
474 case LAP_FOUND_NONE: /* FALLTROUGH */
475 case LAP_MEDIA_BUSY:
476 irlmp_link_disconnect_indication(self->notify.instance, self,
477 reason, NULL);
478 break;
479 default:
480 ERROR("%s(), Unknown reason %d\n", __FUNCTION__, reason);
481 }
482 }
483
484 /*
485 * Function irlap_discovery_request (gen_addr_bit)
486 *
487 * Start one single discovery operation.
488 *
489 */
irlap_discovery_request(struct irlap_cb * self,discovery_t * discovery)490 void irlap_discovery_request(struct irlap_cb *self, discovery_t *discovery)
491 {
492 struct irlap_info info;
493
494 ASSERT(self != NULL, return;);
495 ASSERT(self->magic == LAP_MAGIC, return;);
496 ASSERT(discovery != NULL, return;);
497
498 IRDA_DEBUG(4, "%s(), nslots = %d\n", __FUNCTION__, discovery->nslots);
499
500 ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
501 (discovery->nslots == 8) || (discovery->nslots == 16),
502 return;);
503
504 /* Discovery is only possible in NDM mode */
505 if (self->state != LAP_NDM) {
506 IRDA_DEBUG(4, "%s(), discovery only possible in NDM mode\n",
507 __FUNCTION__);
508 irlap_discovery_confirm(self, NULL);
509 /* Note : in theory, if we are not in NDM, we could postpone
510 * the discovery like we do for connection request.
511 * In practice, it's not worth it. If the media was busy,
512 * it's likely next time around it won't be busy. If we are
513 * in REPLY state, we will get passive discovery info & event.
514 * Jean II */
515 return;
516 }
517
518 /* Check if last discovery request finished in time, or if
519 * it was aborted due to the media busy flag. */
520 if (self->discovery_log != NULL) {
521 hashbin_delete(self->discovery_log, (FREE_FUNC) kfree);
522 self->discovery_log = NULL;
523 }
524
525 self->discovery_log= hashbin_new(HB_LOCAL);
526
527 info.S = discovery->nslots; /* Number of slots */
528 info.s = 0; /* Current slot */
529
530 self->discovery_cmd = discovery;
531 info.discovery = discovery;
532
533 /* sysctl_slot_timeout bounds are checked in irsysctl.c - Jean II */
534 self->slot_timeout = sysctl_slot_timeout * HZ / 1000;
535
536 irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
537 }
538
539 /*
540 * Function irlap_discovery_confirm (log)
541 *
542 * A device has been discovered in front of this station, we
543 * report directly to LMP.
544 */
irlap_discovery_confirm(struct irlap_cb * self,hashbin_t * discovery_log)545 void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log)
546 {
547 ASSERT(self != NULL, return;);
548 ASSERT(self->magic == LAP_MAGIC, return;);
549
550 ASSERT(self->notify.instance != NULL, return;);
551
552 /*
553 * Check for successful discovery, since we are then allowed to clear
554 * the media busy condition (IrLAP 6.13.4 - p.94). This should allow
555 * us to make connection attempts much faster and easier (i.e. no
556 * collisions).
557 * Setting media busy to false will also generate an event allowing
558 * to process pending events in NDM state machine.
559 * Note : the spec doesn't define what's a successful discovery is.
560 * If we want Ultra to work, it's successful even if there is
561 * nobody discovered - Jean II
562 */
563 if (discovery_log)
564 irda_device_set_media_busy(self->netdev, FALSE);
565
566 /* Inform IrLMP */
567 irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
568 }
569
570 /*
571 * Function irlap_discovery_indication (log)
572 *
573 * Somebody is trying to discover us!
574 *
575 */
irlap_discovery_indication(struct irlap_cb * self,discovery_t * discovery)576 void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery)
577 {
578 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
579
580 ASSERT(self != NULL, return;);
581 ASSERT(self->magic == LAP_MAGIC, return;);
582 ASSERT(discovery != NULL, return;);
583
584 ASSERT(self->notify.instance != NULL, return;);
585
586 /* A device is very likely to connect immediately after it performs
587 * a successful discovery. This means that in our case, we are much
588 * more likely to receive a connection request over the medium.
589 * So, we backoff to avoid collisions.
590 * IrLAP spec 6.13.4 suggest 100ms...
591 * Note : this little trick actually make a *BIG* difference. If I set
592 * my Linux box with discovery enabled and one Ultra frame sent every
593 * second, my Palm has no trouble connecting to it every time !
594 * Jean II */
595 irda_device_set_media_busy(self->netdev, SMALL);
596
597 irlmp_link_discovery_indication(self->notify.instance, discovery);
598 }
599
600 /*
601 * Function irlap_status_indication (quality_of_link)
602 *
603 *
604 *
605 */
irlap_status_indication(struct irlap_cb * self,int quality_of_link)606 void irlap_status_indication(struct irlap_cb *self, int quality_of_link)
607 {
608 switch (quality_of_link) {
609 case STATUS_NO_ACTIVITY:
610 MESSAGE("IrLAP, no activity on link!\n");
611 break;
612 case STATUS_NOISY:
613 MESSAGE("IrLAP, noisy link!\n");
614 break;
615 default:
616 break;
617 }
618 irlmp_status_indication(self->notify.instance,
619 quality_of_link, LOCK_NO_CHANGE);
620 }
621
622 /*
623 * Function irlap_reset_indication (void)
624 *
625 *
626 *
627 */
irlap_reset_indication(struct irlap_cb * self)628 void irlap_reset_indication(struct irlap_cb *self)
629 {
630 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
631
632 ASSERT(self != NULL, return;);
633 ASSERT(self->magic == LAP_MAGIC, return;);
634
635 if (self->state == LAP_RESET_WAIT)
636 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
637 else
638 irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
639 }
640
641 /*
642 * Function irlap_reset_confirm (void)
643 *
644 *
645 *
646 */
irlap_reset_confirm(void)647 void irlap_reset_confirm(void)
648 {
649 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
650 }
651
652 /*
653 * Function irlap_generate_rand_time_slot (S, s)
654 *
655 * Generate a random time slot between s and S-1 where
656 * S = Number of slots (0 -> S-1)
657 * s = Current slot
658 */
irlap_generate_rand_time_slot(int S,int s)659 int irlap_generate_rand_time_slot(int S, int s)
660 {
661 static int rand;
662 int slot;
663
664 ASSERT((S - s) > 0, return 0;);
665
666 rand += jiffies;
667 rand ^= (rand << 12);
668 rand ^= (rand >> 20);
669
670 slot = s + rand % (S-s);
671
672 ASSERT((slot >= s) || (slot < S), return 0;);
673
674 return slot;
675 }
676
677 /*
678 * Function irlap_update_nr_received (nr)
679 *
680 * Remove all acknowledged frames in current window queue. This code is
681 * not intuitive and you should not try to change it. If you think it
682 * contains bugs, please mail a patch to the author instead.
683 */
irlap_update_nr_received(struct irlap_cb * self,int nr)684 void irlap_update_nr_received(struct irlap_cb *self, int nr)
685 {
686 struct sk_buff *skb = NULL;
687 int count = 0;
688
689 /*
690 * Remove all the ack-ed frames from the window queue.
691 */
692
693 /*
694 * Optimize for the common case. It is most likely that the receiver
695 * will acknowledge all the frames we have sent! So in that case we
696 * delete all frames stored in window.
697 */
698 if (nr == self->vs) {
699 while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
700 dev_kfree_skb(skb);
701 }
702 /* The last acked frame is the next to send minus one */
703 self->va = nr - 1;
704 } else {
705 /* Remove all acknowledged frames in current window */
706 while ((skb_peek(&self->wx_list) != NULL) &&
707 (((self->va+1) % 8) != nr))
708 {
709 skb = skb_dequeue(&self->wx_list);
710 dev_kfree_skb(skb);
711
712 self->va = (self->va + 1) % 8;
713 count++;
714 }
715 }
716
717 /* Advance window */
718 self->window = self->window_size - skb_queue_len(&self->wx_list);
719 }
720
721 /*
722 * Function irlap_validate_ns_received (ns)
723 *
724 * Validate the next to send (ns) field from received frame.
725 */
irlap_validate_ns_received(struct irlap_cb * self,int ns)726 int irlap_validate_ns_received(struct irlap_cb *self, int ns)
727 {
728 /* ns as expected? */
729 if (ns == self->vr)
730 return NS_EXPECTED;
731 /*
732 * Stations are allowed to treat invalid NS as unexpected NS
733 * IrLAP, Recv ... with-invalid-Ns. p. 84
734 */
735 return NS_UNEXPECTED;
736
737 /* return NR_INVALID; */
738 }
739 /*
740 * Function irlap_validate_nr_received (nr)
741 *
742 * Validate the next to receive (nr) field from received frame.
743 *
744 */
irlap_validate_nr_received(struct irlap_cb * self,int nr)745 int irlap_validate_nr_received(struct irlap_cb *self, int nr)
746 {
747 /* nr as expected? */
748 if (nr == self->vs) {
749 IRDA_DEBUG(4, "%s(), expected!\n", __FUNCTION__);
750 return NR_EXPECTED;
751 }
752
753 /*
754 * unexpected nr? (but within current window), first we check if the
755 * ns numbers of the frames in the current window wrap.
756 */
757 if (self->va < self->vs) {
758 if ((nr >= self->va) && (nr <= self->vs))
759 return NR_UNEXPECTED;
760 } else {
761 if ((nr >= self->va) || (nr <= self->vs))
762 return NR_UNEXPECTED;
763 }
764
765 /* Invalid nr! */
766 return NR_INVALID;
767 }
768
769 /*
770 * Function irlap_initiate_connection_state ()
771 *
772 * Initialize the connection state parameters
773 *
774 */
irlap_initiate_connection_state(struct irlap_cb * self)775 void irlap_initiate_connection_state(struct irlap_cb *self)
776 {
777 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
778
779 ASSERT(self != NULL, return;);
780 ASSERT(self->magic == LAP_MAGIC, return;);
781
782 /* Next to send and next to receive */
783 self->vs = self->vr = 0;
784
785 /* Last frame which got acked (0 - 1) % 8 */
786 self->va = 7;
787
788 self->window = 1;
789
790 self->remote_busy = FALSE;
791 self->retry_count = 0;
792 }
793
794 /*
795 * Function irlap_wait_min_turn_around (self, qos)
796 *
797 * Wait negotiated minimum turn around time, this function actually sets
798 * the number of BOS's that must be sent before the next transmitted
799 * frame in order to delay for the specified amount of time. This is
800 * done to avoid using timers, and the forbidden udelay!
801 */
irlap_wait_min_turn_around(struct irlap_cb * self,struct qos_info * qos)802 void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos)
803 {
804 __u32 min_turn_time;
805 __u32 speed;
806
807 /* Get QoS values. */
808 speed = qos->baud_rate.value;
809 min_turn_time = qos->min_turn_time.value;
810
811 /* No need to calculate XBOFs for speeds over 115200 bps */
812 if (speed > 115200) {
813 self->mtt_required = min_turn_time;
814 return;
815 }
816
817 /*
818 * Send additional BOF's for the next frame for the requested
819 * min turn time, so now we must calculate how many chars (XBOF's) we
820 * must send for the requested time period (min turn time)
821 */
822 self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
823 }
824
825 /*
826 * Function irlap_flush_all_queues (void)
827 *
828 * Flush all queues
829 *
830 */
irlap_flush_all_queues(struct irlap_cb * self)831 void irlap_flush_all_queues(struct irlap_cb *self)
832 {
833 struct sk_buff* skb;
834
835 ASSERT(self != NULL, return;);
836 ASSERT(self->magic == LAP_MAGIC, return;);
837
838 /* Free transmission queue */
839 while ((skb = skb_dequeue(&self->txq)) != NULL)
840 dev_kfree_skb(skb);
841
842 while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
843 dev_kfree_skb(skb);
844
845 /* Free sliding window buffered packets */
846 while ((skb = skb_dequeue(&self->wx_list)) != NULL)
847 dev_kfree_skb(skb);
848 }
849
850 /*
851 * Function irlap_setspeed (self, speed)
852 *
853 * Change the speed of the IrDA port
854 *
855 */
irlap_change_speed(struct irlap_cb * self,__u32 speed,int now)856 void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
857 {
858 struct sk_buff *skb;
859
860 IRDA_DEBUG(0, "%s(), setting speed to %d\n", __FUNCTION__, speed);
861
862 ASSERT(self != NULL, return;);
863 ASSERT(self->magic == LAP_MAGIC, return;);
864
865 self->speed = speed;
866
867 /* Change speed now, or just piggyback speed on frames */
868 if (now) {
869 /* Send down empty frame to trigger speed change */
870 skb = dev_alloc_skb(0);
871 irlap_queue_xmit(self, skb);
872 }
873 }
874
875 /*
876 * Function irlap_init_qos_capabilities (self, qos)
877 *
878 * Initialize QoS for this IrLAP session, What we do is to compute the
879 * intersection of the QoS capabilities for the user, driver and for
880 * IrLAP itself. Normally, IrLAP will not specify any values, but it can
881 * be used to restrict certain values.
882 */
irlap_init_qos_capabilities(struct irlap_cb * self,struct qos_info * qos_user)883 void irlap_init_qos_capabilities(struct irlap_cb *self,
884 struct qos_info *qos_user)
885 {
886 ASSERT(self != NULL, return;);
887 ASSERT(self->magic == LAP_MAGIC, return;);
888 ASSERT(self->netdev != NULL, return;);
889
890 /* Start out with the maximum QoS support possible */
891 irda_init_max_qos_capabilies(&self->qos_rx);
892
893 /* Apply drivers QoS capabilities */
894 irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
895
896 /*
897 * Check for user supplied QoS parameters. The service user is only
898 * allowed to supply these values. We check each parameter since the
899 * user may not have set all of them.
900 */
901 if (qos_user) {
902 IRDA_DEBUG(1, "%s(), Found user specified QoS!\n", __FUNCTION__);
903
904 if (qos_user->baud_rate.bits)
905 self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
906
907 if (qos_user->max_turn_time.bits)
908 self->qos_rx.max_turn_time.bits &= qos_user->max_turn_time.bits;
909 if (qos_user->data_size.bits)
910 self->qos_rx.data_size.bits &= qos_user->data_size.bits;
911
912 if (qos_user->link_disc_time.bits)
913 self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
914 }
915
916 /* Use 500ms in IrLAP for now */
917 self->qos_rx.max_turn_time.bits &= 0x01;
918
919 /* Set data size */
920 /*self->qos_rx.data_size.bits &= 0x03;*/
921
922 irda_qos_bits_to_value(&self->qos_rx);
923 }
924
925 /*
926 * Function irlap_apply_default_connection_parameters (void, now)
927 *
928 * Use the default connection and transmission parameters
929 *
930 */
irlap_apply_default_connection_parameters(struct irlap_cb * self)931 void irlap_apply_default_connection_parameters(struct irlap_cb *self)
932 {
933 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
934
935 ASSERT(self != NULL, return;);
936 ASSERT(self->magic == LAP_MAGIC, return;);
937
938 /* xbofs : Default value in NDM */
939 self->next_bofs = 12;
940 self->bofs_count = 12;
941
942 /* NDM Speed is 9600 */
943 irlap_change_speed(self, 9600, TRUE);
944
945 /* Set mbusy when going to NDM state */
946 irda_device_set_media_busy(self->netdev, TRUE);
947
948 /*
949 * Generate random connection address for this session, which must
950 * be 7 bits wide and different from 0x00 and 0xfe
951 */
952 while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
953 get_random_bytes(&self->caddr, sizeof(self->caddr));
954 self->caddr &= 0xfe;
955 }
956
957 /* Use default values until connection has been negitiated */
958 self->slot_timeout = sysctl_slot_timeout;
959 self->final_timeout = FINAL_TIMEOUT;
960 self->poll_timeout = POLL_TIMEOUT;
961 self->wd_timeout = WD_TIMEOUT;
962
963 /* Set some default values */
964 self->qos_tx.baud_rate.value = 9600;
965 self->qos_rx.baud_rate.value = 9600;
966 self->qos_tx.max_turn_time.value = 0;
967 self->qos_rx.max_turn_time.value = 0;
968 self->qos_tx.min_turn_time.value = 0;
969 self->qos_rx.min_turn_time.value = 0;
970 self->qos_tx.data_size.value = 64;
971 self->qos_rx.data_size.value = 64;
972 self->qos_tx.window_size.value = 1;
973 self->qos_rx.window_size.value = 1;
974 self->qos_tx.additional_bofs.value = 12;
975 self->qos_rx.additional_bofs.value = 12;
976 self->qos_tx.link_disc_time.value = 0;
977 self->qos_rx.link_disc_time.value = 0;
978
979 irlap_flush_all_queues(self);
980
981 self->disconnect_pending = FALSE;
982 self->connect_pending = FALSE;
983 }
984
985 /*
986 * Function irlap_apply_connection_parameters (qos, now)
987 *
988 * Initialize IrLAP with the negotiated QoS values
989 *
990 * If 'now' is false, the speed and xbofs will be changed after the next
991 * frame is sent.
992 * If 'now' is true, the speed and xbofs is changed immediately
993 */
irlap_apply_connection_parameters(struct irlap_cb * self,int now)994 void irlap_apply_connection_parameters(struct irlap_cb *self, int now)
995 {
996 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
997
998 ASSERT(self != NULL, return;);
999 ASSERT(self->magic == LAP_MAGIC, return;);
1000
1001 /* Set the negociated xbofs value */
1002 self->next_bofs = self->qos_tx.additional_bofs.value;
1003 if (now)
1004 self->bofs_count = self->next_bofs;
1005
1006 /* Set the negociated link speed (may need the new xbofs value) */
1007 irlap_change_speed(self, self->qos_tx.baud_rate.value, now);
1008
1009 self->window_size = self->qos_tx.window_size.value;
1010 self->window = self->qos_tx.window_size.value;
1011
1012 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1013 /*
1014 * Calculate how many bytes it is possible to transmit before the
1015 * link must be turned around
1016 */
1017 self->line_capacity =
1018 irlap_max_line_capacity(self->qos_tx.baud_rate.value,
1019 self->qos_tx.max_turn_time.value);
1020 self->bytes_left = self->line_capacity;
1021 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1022
1023
1024 /*
1025 * Initialize timeout values, some of the rules are listed on
1026 * page 92 in IrLAP.
1027 */
1028 ASSERT(self->qos_tx.max_turn_time.value != 0, return;);
1029 ASSERT(self->qos_rx.max_turn_time.value != 0, return;);
1030 /* The poll timeout applies only to the primary station.
1031 * It defines the maximum time the primary stay in XMIT mode
1032 * before timeout and turning the link around (sending a RR).
1033 * Or, this is how much we can keep the pf bit in primary mode.
1034 * Therefore, it must be lower or equal than our *OWN* max turn around.
1035 * Jean II */
1036 self->poll_timeout = self->qos_tx.max_turn_time.value * HZ / 1000;
1037 /* The Final timeout applies only to the primary station.
1038 * It defines the maximum time the primary wait (mostly in RECV mode)
1039 * for an answer from the secondary station before polling it again.
1040 * Therefore, it must be greater or equal than our *PARTNER*
1041 * max turn around time - Jean II */
1042 self->final_timeout = self->qos_rx.max_turn_time.value * HZ / 1000;
1043 /* The Watchdog Bit timeout applies only to the secondary station.
1044 * It defines the maximum time the secondary wait (mostly in RECV mode)
1045 * for poll from the primary station before getting annoyed.
1046 * Therefore, it must be greater or equal than our *PARTNER*
1047 * max turn around time - Jean II */
1048 self->wd_timeout = self->final_timeout * 2;
1049
1050 /*
1051 * N1 and N2 are maximum retry count for *both* the final timer
1052 * and the wd timer (with a factor 2) as defined above.
1053 * After N1 retry of a timer, we give a warning to the user.
1054 * After N2 retry, we consider the link dead and disconnect it.
1055 * Jean II
1056 */
1057
1058 /*
1059 * Set N1 to 0 if Link Disconnect/Threshold Time = 3 and set it to
1060 * 3 seconds otherwise. See page 71 in IrLAP for more details.
1061 * Actually, it's not always 3 seconds, as we allow to set
1062 * it via sysctl... Max maxtt is 500ms, and N1 need to be multiple
1063 * of 2, so 1 second is minimum we can allow. - Jean II
1064 */
1065 if (self->qos_tx.link_disc_time.value == sysctl_warn_noreply_time)
1066 /*
1067 * If we set N1 to 0, it will trigger immediately, which is
1068 * not what we want. What we really want is to disable it,
1069 * Jean II
1070 */
1071 self->N1 = -2; /* Disable - Need to be multiple of 2*/
1072 else
1073 self->N1 = sysctl_warn_noreply_time * 1000 /
1074 self->qos_rx.max_turn_time.value;
1075
1076 IRDA_DEBUG(4, "Setting N1 = %d\n", self->N1);
1077
1078 /* Set N2 to match our own disconnect time */
1079 self->N2 = self->qos_tx.link_disc_time.value * 1000 /
1080 self->qos_rx.max_turn_time.value;
1081 IRDA_DEBUG(4, "Setting N2 = %d\n", self->N2);
1082 }
1083
1084 #ifdef CONFIG_PROC_FS
1085 /*
1086 * Function irlap_proc_read (buf, start, offset, len, unused)
1087 *
1088 * Give some info to the /proc file system
1089 *
1090 */
irlap_proc_read(char * buf,char ** start,off_t offset,int len)1091 int irlap_proc_read(char *buf, char **start, off_t offset, int len)
1092 {
1093 struct irlap_cb *self;
1094 unsigned long flags;
1095 int i = 0;
1096
1097 save_flags(flags);
1098 cli();
1099
1100 len = 0;
1101
1102 self = (struct irlap_cb *) hashbin_get_first(irlap);
1103 while (self != NULL) {
1104 ASSERT(self != NULL, break;);
1105 ASSERT(self->magic == LAP_MAGIC, break;);
1106
1107 len += sprintf(buf+len, "irlap%d ", i++);
1108 len += sprintf(buf+len, "state: %s\n",
1109 irlap_state[self->state]);
1110
1111 len += sprintf(buf+len, " device name: %s, ",
1112 (self->netdev) ? self->netdev->name : "bug");
1113 len += sprintf(buf+len, "hardware name: %s\n", self->hw_name);
1114
1115 len += sprintf(buf+len, " caddr: %#02x, ", self->caddr);
1116 len += sprintf(buf+len, "saddr: %#08x, ", self->saddr);
1117 len += sprintf(buf+len, "daddr: %#08x\n", self->daddr);
1118
1119 len += sprintf(buf+len, " win size: %d, ",
1120 self->window_size);
1121 len += sprintf(buf+len, "win: %d, ", self->window);
1122 #if CONFIG_IRDA_DYNAMIC_WINDOW
1123 len += sprintf(buf+len, "line capacity: %d, ",
1124 self->line_capacity);
1125 len += sprintf(buf+len, "bytes left: %d\n", self->bytes_left);
1126 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1127 len += sprintf(buf+len, " tx queue len: %d ",
1128 skb_queue_len(&self->txq));
1129 len += sprintf(buf+len, "win queue len: %d ",
1130 skb_queue_len(&self->wx_list));
1131 len += sprintf(buf+len, "rbusy: %s", self->remote_busy ?
1132 "TRUE" : "FALSE");
1133 len += sprintf(buf+len, " mbusy: %s\n", self->media_busy ?
1134 "TRUE" : "FALSE");
1135
1136 len += sprintf(buf+len, " retrans: %d ", self->retry_count);
1137 len += sprintf(buf+len, "vs: %d ", self->vs);
1138 len += sprintf(buf+len, "vr: %d ", self->vr);
1139 len += sprintf(buf+len, "va: %d\n", self->va);
1140
1141 len += sprintf(buf+len, " qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
1142
1143 len += sprintf(buf+len, " tx\t%d\t",
1144 self->qos_tx.baud_rate.value);
1145 len += sprintf(buf+len, "%d\t",
1146 self->qos_tx.max_turn_time.value);
1147 len += sprintf(buf+len, "%d\t",
1148 self->qos_tx.data_size.value);
1149 len += sprintf(buf+len, "%d\t",
1150 self->qos_tx.window_size.value);
1151 len += sprintf(buf+len, "%d\t",
1152 self->qos_tx.additional_bofs.value);
1153 len += sprintf(buf+len, "%d\t",
1154 self->qos_tx.min_turn_time.value);
1155 len += sprintf(buf+len, "%d\t",
1156 self->qos_tx.link_disc_time.value);
1157 len += sprintf(buf+len, "\n");
1158
1159 len += sprintf(buf+len, " rx\t%d\t",
1160 self->qos_rx.baud_rate.value);
1161 len += sprintf(buf+len, "%d\t",
1162 self->qos_rx.max_turn_time.value);
1163 len += sprintf(buf+len, "%d\t",
1164 self->qos_rx.data_size.value);
1165 len += sprintf(buf+len, "%d\t",
1166 self->qos_rx.window_size.value);
1167 len += sprintf(buf+len, "%d\t",
1168 self->qos_rx.additional_bofs.value);
1169 len += sprintf(buf+len, "%d\t",
1170 self->qos_rx.min_turn_time.value);
1171 len += sprintf(buf+len, "%d\t",
1172 self->qos_rx.link_disc_time.value);
1173 len += sprintf(buf+len, "\n");
1174
1175 self = (struct irlap_cb *) hashbin_get_next(irlap);
1176 }
1177 restore_flags(flags);
1178
1179 return len;
1180 }
1181
1182 #endif /* CONFIG_PROC_FS */
1183
1184