- pre1:
[davej-history.git] / net / irda / irlap.c
blob1c2958c4be72c34aa7fd0decea8e19c0ba671b9b
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>
12 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
29 ********************************************************************/
31 #include <linux/config.h>
32 #include <linux/malloc.h>
33 #include <linux/string.h>
34 #include <linux/skbuff.h>
35 #include <linux/delay.h>
36 #include <linux/proc_fs.h>
37 #include <linux/init.h>
38 #include <linux/random.h>
40 #include <net/irda/irda.h>
41 #include <net/irda/irda_device.h>
42 #include <net/irda/irqueue.h>
43 #include <net/irda/irlmp.h>
44 #include <net/irda/irlmp_frame.h>
45 #include <net/irda/irlap_frame.h>
46 #include <net/irda/irlap.h>
47 #include <net/irda/timer.h>
48 #include <net/irda/qos.h>
49 #include <net/irda/irlap_comp.h>
51 hashbin_t *irlap = NULL;
52 int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
54 static void __irlap_close(struct irlap_cb *self);
56 static char *lap_reasons[] = {
57 "ERROR, NOT USED",
58 "LAP_DISC_INDICATION",
59 "LAP_NO_RESPONSE",
60 "LAP_RESET_INDICATION",
61 "LAP_FOUND_NONE",
62 "LAP_MEDIA_BUSY",
63 "LAP_PRIMARY_CONFLICT",
64 "ERROR, NOT USED",
67 #ifdef CONFIG_PROC_FS
68 int irlap_proc_read(char *, char **, off_t, int);
70 #endif /* CONFIG_PROC_FS */
72 int __init irlap_init(void)
74 /* Allocate master array */
75 irlap = hashbin_new(HB_LOCAL);
76 if (irlap == NULL) {
77 ERROR(__FUNCTION__ "(), can't allocate irlap hashbin!\n");
78 return -ENOMEM;
81 #ifdef CONFIG_IRDA_COMPRESSION
82 irlap_compressors = hashbin_new(HB_LOCAL);
83 if (irlap_compressors == NULL) {
84 WARNING(__FUNCTION__
85 "(), can't allocate compressors hashbin!\n");
86 return -ENOMEM;
88 #endif
90 return 0;
93 void irlap_cleanup(void)
95 ASSERT(irlap != NULL, return;);
97 hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
99 #ifdef CONFIG_IRDA_COMPRESSION
100 hashbin_delete(irlap_compressors, (FREE_FUNC) kfree);
101 #endif
105 * Function irlap_open (driver)
107 * Initialize IrLAP layer
110 struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos)
112 struct irlap_cb *self;
114 IRDA_DEBUG(4, __FUNCTION__ "()\n");
116 /* Initialize the irlap structure. */
117 self = kmalloc(sizeof(struct irlap_cb), GFP_KERNEL);
118 if (self == NULL)
119 return NULL;
121 memset(self, 0, sizeof(struct irlap_cb));
122 self->magic = LAP_MAGIC;
124 /* Make a binding between the layers */
125 self->netdev = dev;
126 self->qos_dev = qos;
128 /* FIXME: should we get our own field? */
129 dev->atalk_ptr = self;
131 irlap_next_state(self, LAP_OFFLINE);
133 /* Initialize transmit queue */
134 skb_queue_head_init(&self->txq);
135 skb_queue_head_init(&self->txq_ultra);
136 skb_queue_head_init(&self->wx_list);
138 /* My unique IrLAP device address! */
139 get_random_bytes(&self->saddr, sizeof(self->saddr));
140 memcpy(dev->dev_addr, &self->saddr, 4);
142 init_timer(&self->slot_timer);
143 init_timer(&self->query_timer);
144 init_timer(&self->discovery_timer);
145 init_timer(&self->final_timer);
146 init_timer(&self->poll_timer);
147 init_timer(&self->wd_timer);
148 init_timer(&self->backoff_timer);
149 init_timer(&self->media_busy_timer);
151 irlap_apply_default_connection_parameters(self);
153 self->N3 = 3; /* # connections attemts to try before giving up */
155 irlap_next_state(self, LAP_NDM);
157 hashbin_insert(irlap, (queue_t *) self, self->saddr, NULL);
159 irlmp_register_link(self, self->saddr, &self->notify);
161 return self;
165 * Function __irlap_close (self)
167 * Remove IrLAP and all allocated memory. Stop any pending timers.
170 static void __irlap_close(struct irlap_cb *self)
172 ASSERT(self != NULL, return;);
173 ASSERT(self->magic == LAP_MAGIC, return;);
175 /* Stop timers */
176 del_timer(&self->slot_timer);
177 del_timer(&self->query_timer);
178 del_timer(&self->discovery_timer);
179 del_timer(&self->final_timer);
180 del_timer(&self->poll_timer);
181 del_timer(&self->wd_timer);
182 del_timer(&self->backoff_timer);
183 del_timer(&self->media_busy_timer);
185 irlap_flush_all_queues(self);
187 self->magic = 0;
189 kfree(self);
193 * Function irlap_close (self)
195 * Remove IrLAP instance
198 void irlap_close(struct irlap_cb *self)
200 struct irlap_cb *lap;
202 IRDA_DEBUG(4, __FUNCTION__ "()\n");
204 ASSERT(self != NULL, return;);
205 ASSERT(self->magic == LAP_MAGIC, return;);
207 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
209 irlmp_unregister_link(self->saddr);
210 self->notify.instance = NULL;
212 /* Be sure that we manage to remove ourself from the hash */
213 lap = hashbin_remove(irlap, self->saddr, NULL);
214 if (!lap) {
215 IRDA_DEBUG(1, __FUNCTION__ "(), Didn't find myself!\n");
216 return;
218 __irlap_close(lap);
222 * Function irlap_connect_indication (self, skb)
224 * Another device is attempting to make a connection
227 void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb)
229 IRDA_DEBUG(4, __FUNCTION__ "()\n");
231 ASSERT(self != NULL, return;);
232 ASSERT(self->magic == LAP_MAGIC, return;);
234 irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
236 irlmp_link_connect_indication(self->notify.instance, self->saddr,
237 self->daddr, &self->qos_tx, skb);
241 * Function irlap_connect_response (self, skb)
243 * Service user has accepted incomming connection
246 void irlap_connect_response(struct irlap_cb *self, struct sk_buff *skb)
248 IRDA_DEBUG(4, __FUNCTION__ "()\n");
250 irlap_do_event(self, CONNECT_RESPONSE, skb, NULL);
254 * Function irlap_connect_request (self, daddr, qos_user, sniff)
256 * Request connection with another device, sniffing is not implemented
257 * yet.
260 void irlap_connect_request(struct irlap_cb *self, __u32 daddr,
261 struct qos_info *qos_user, int sniff)
263 IRDA_DEBUG(3, __FUNCTION__ "(), daddr=0x%08x\n", daddr);
265 ASSERT(self != NULL, return;);
266 ASSERT(self->magic == LAP_MAGIC, return;);
268 self->daddr = daddr;
271 * If the service user specifies QoS values for this connection,
272 * then use them
274 irlap_init_qos_capabilities(self, qos_user);
276 if ((self->state == LAP_NDM) && !self->media_busy)
277 irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
278 else
279 self->connect_pending = TRUE;
283 * Function irlap_connect_confirm (self, skb)
285 * Connection request has been accepted
288 void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
290 IRDA_DEBUG(4, __FUNCTION__ "()\n");
292 ASSERT(self != NULL, return;);
293 ASSERT(self->magic == LAP_MAGIC, return;);
295 irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
299 * Function irlap_data_indication (self, skb)
301 * Received data frames from IR-port, so we just pass them up to
302 * IrLMP for further processing
305 void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
306 int unreliable)
308 /* Hide LAP header from IrLMP layer */
309 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
311 #ifdef CONFIG_IRDA_COMPRESSION
312 if (self->qos_tx.compression.value) {
313 skb = irlap_decompress_frame(self, skb);
314 if (!skb) {
315 IRDA_DEBUG(1, __FUNCTION__ "(), Decompress error!\n");
316 return;
319 #endif
320 irlmp_link_data_indication(self->notify.instance, skb, unreliable);
325 * Function irlap_data_request (self, skb)
327 * Queue data for transmission, must wait until XMIT state
330 void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb,
331 int unreliable)
333 ASSERT(self != NULL, return;);
334 ASSERT(self->magic == LAP_MAGIC, return;);
336 IRDA_DEBUG(3, __FUNCTION__ "()\n");
338 #ifdef CONFIG_IRDA_COMPRESSION
339 if (self->qos_tx.compression.value) {
340 skb = irlap_compress_frame(self, skb);
341 if (!skb) {
342 IRDA_DEBUG(1, __FUNCTION__ "(), Compress error!\n");
343 return;
346 #endif
347 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
348 return;);
349 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
352 * Must set frame format now so that the rest of the code knows
353 * if its dealing with an I or an UI frame
355 if (unreliable)
356 skb->data[1] = UI_FRAME;
357 else
358 skb->data[1] = I_FRAME;
361 * Send event if this frame only if we are in the right state
362 * FIXME: udata should be sent first! (skb_queue_head?)
364 if ((self->state == LAP_XMIT_P) || (self->state == LAP_XMIT_S)) {
366 * Check if the transmit queue contains some unsent frames,
367 * and if so, make sure they are sent first
369 if (!skb_queue_empty(&self->txq)) {
370 skb_queue_tail(&self->txq, skb);
371 skb = skb_dequeue(&self->txq);
373 ASSERT(skb != NULL, return;);
375 irlap_do_event(self, SEND_I_CMD, skb, NULL);
376 } else
377 skb_queue_tail(&self->txq, skb);
381 * Function irlap_unitdata_request (self, skb)
383 * Send Ultra data. This is data that must be sent outside any connection
386 #ifdef CONFIG_IRDA_ULTRA
387 void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
389 ASSERT(self != NULL, return;);
390 ASSERT(self->magic == LAP_MAGIC, return;);
392 IRDA_DEBUG(3, __FUNCTION__ "()\n");
394 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
395 return;);
396 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
398 skb->data[0] = CBROADCAST;
399 skb->data[1] = UI_FRAME;
401 skb_queue_tail(&self->txq_ultra, skb);
403 irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
405 #endif /*CONFIG_IRDA_ULTRA */
408 * Function irlap_udata_indication (self, skb)
410 * Receive Ultra data. This is data that is received outside any connection
413 #ifdef CONFIG_IRDA_ULTRA
414 void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
416 IRDA_DEBUG(1, __FUNCTION__ "()\n");
418 ASSERT(self != NULL, return;);
419 ASSERT(self->magic == LAP_MAGIC, return;);
420 ASSERT(skb != NULL, return;);
422 /* Hide LAP header from IrLMP layer */
423 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
425 irlmp_link_unitdata_indication(self->notify.instance, skb);
427 #endif /* CONFIG_IRDA_ULTRA */
430 * Function irlap_disconnect_request (void)
432 * Request to disconnect connection by service user
434 void irlap_disconnect_request(struct irlap_cb *self)
436 IRDA_DEBUG(3, __FUNCTION__ "()\n");
438 ASSERT(self != NULL, return;);
439 ASSERT(self->magic == LAP_MAGIC, return;);
441 /* Don't disconnect until all data frames are successfully sent */
442 if (skb_queue_len(&self->txq) > 0) {
443 self->disconnect_pending = TRUE;
445 return;
448 /* Check if we are in the right state for disconnecting */
449 switch (self->state) {
450 case LAP_XMIT_P: /* FALLTROUGH */
451 case LAP_XMIT_S: /* FALLTROUGH */
452 case LAP_CONN: /* FALLTROUGH */
453 case LAP_RESET_WAIT: /* FALLTROUGH */
454 case LAP_RESET_CHECK:
455 irlap_do_event(self, DISCONNECT_REQUEST, NULL, NULL);
456 break;
457 default:
458 IRDA_DEBUG(2, __FUNCTION__ "(), disconnect pending!\n");
459 self->disconnect_pending = TRUE;
460 break;
465 * Function irlap_disconnect_indication (void)
467 * Disconnect request from other device
470 void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason)
472 IRDA_DEBUG(1, __FUNCTION__ "(), reason=%s\n", lap_reasons[reason]);
474 ASSERT(self != NULL, return;);
475 ASSERT(self->magic == LAP_MAGIC, return;);
477 #ifdef CONFIG_IRDA_COMPRESSION
478 irda_free_compression(self);
479 #endif
480 /* Flush queues */
481 irlap_flush_all_queues(self);
483 switch (reason) {
484 case LAP_RESET_INDICATION:
485 IRDA_DEBUG(1, __FUNCTION__ "(), Sending reset request!\n");
486 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
487 break;
488 case LAP_NO_RESPONSE: /* FALLTROUGH */
489 case LAP_DISC_INDICATION: /* FALLTROUGH */
490 case LAP_FOUND_NONE: /* FALLTROUGH */
491 case LAP_MEDIA_BUSY:
492 irlmp_link_disconnect_indication(self->notify.instance, self,
493 reason, NULL);
494 break;
495 default:
496 ERROR(__FUNCTION__ "(), Unknown reason %d\n", reason);
501 * Function irlap_discovery_request (gen_addr_bit)
503 * Start one single discovery operation.
506 void irlap_discovery_request(struct irlap_cb *self, discovery_t *discovery)
508 struct irlap_info info;
510 ASSERT(self != NULL, return;);
511 ASSERT(self->magic == LAP_MAGIC, return;);
512 ASSERT(discovery != NULL, return;);
514 IRDA_DEBUG(4, __FUNCTION__ "(), nslots = %d\n", discovery->nslots);
516 ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
517 (discovery->nslots == 8) || (discovery->nslots == 16),
518 return;);
520 /* Discovery is only possible in NDM mode */
521 if (self->state != LAP_NDM) {
522 IRDA_DEBUG(4, __FUNCTION__
523 "(), discovery only possible in NDM mode\n");
524 irlap_discovery_confirm(self, NULL);
525 return;
528 /* Check if last discovery request finished in time */
529 if (self->discovery_log != NULL) {
530 hashbin_delete(self->discovery_log, (FREE_FUNC) kfree);
531 self->discovery_log = NULL;
534 self->discovery_log= hashbin_new(HB_LOCAL);
536 info.S = discovery->nslots; /* Number of slots */
537 info.s = 0; /* Current slot */
539 self->discovery_cmd = discovery;
540 info.discovery = discovery;
542 /* Check if the slot timeout is within limits */
543 if (sysctl_slot_timeout < 20) {
544 ERROR(__FUNCTION__
545 "(), to low value for slot timeout!\n");
546 sysctl_slot_timeout = 20;
549 * Highest value is actually 8, but we allow higher since
550 * some devices seems to require it.
552 if (sysctl_slot_timeout > 160) {
553 ERROR(__FUNCTION__
554 "(), to high value for slot timeout!\n");
555 sysctl_slot_timeout = 160;
558 self->slot_timeout = sysctl_slot_timeout * HZ / 1000;
560 irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
564 * Function irlap_discovery_confirm (log)
566 * A device has been discovered in front of this station, we
567 * report directly to LMP.
569 void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log)
571 ASSERT(self != NULL, return;);
572 ASSERT(self->magic == LAP_MAGIC, return;);
574 ASSERT(self->notify.instance != NULL, return;);
577 * Check for successful discovery, since we are then allowed to clear
578 * the media busy condition (irlap p.94). This should allow us to make
579 * connection attempts much easier.
581 if (discovery_log && HASHBIN_GET_SIZE(discovery_log) > 0)
582 irda_device_set_media_busy(self->netdev, FALSE);
584 /* Inform IrLMP */
585 irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
589 * Function irlap_discovery_indication (log)
591 * Somebody is trying to discover us!
594 void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery)
596 IRDA_DEBUG(4, __FUNCTION__ "()\n");
598 ASSERT(self != NULL, return;);
599 ASSERT(self->magic == LAP_MAGIC, return;);
600 ASSERT(discovery != NULL, return;);
602 ASSERT(self->notify.instance != NULL, return;);
604 irlmp_link_discovery_indication(self->notify.instance, discovery);
608 * Function irlap_status_indication (quality_of_link)
613 void irlap_status_indication(int quality_of_link)
615 switch (quality_of_link) {
616 case STATUS_NO_ACTIVITY:
617 MESSAGE("IrLAP, no activity on link!\n");
618 break;
619 case STATUS_NOISY:
620 MESSAGE("IrLAP, noisy link!\n");
621 break;
622 default:
623 break;
625 irlmp_status_indication(quality_of_link, LOCK_NO_CHANGE);
629 * Function irlap_reset_indication (void)
634 void irlap_reset_indication(struct irlap_cb *self)
636 IRDA_DEBUG(1, __FUNCTION__ "()\n");
638 ASSERT(self != NULL, return;);
639 ASSERT(self->magic == LAP_MAGIC, return;);
641 if (self->state == LAP_RESET_WAIT)
642 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
643 else
644 irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
648 * Function irlap_reset_confirm (void)
653 void irlap_reset_confirm(void)
655 IRDA_DEBUG(1, __FUNCTION__ "()\n");
659 * Function irlap_generate_rand_time_slot (S, s)
661 * Generate a random time slot between s and S-1 where
662 * S = Number of slots (0 -> S-1)
663 * s = Current slot
665 int irlap_generate_rand_time_slot(int S, int s)
667 int slot;
669 ASSERT((S - s) > 0, return 0;);
671 slot = s + jiffies % (S-s);
673 ASSERT((slot >= s) || (slot < S), return 0;);
675 return slot;
679 * Function irlap_update_nr_received (nr)
681 * Remove all acknowledged frames in current window queue. This code is
682 * not intuitive and you should not try to change it. If you think it
683 * contains bugs, please mail a patch to the author instead.
685 void irlap_update_nr_received(struct irlap_cb *self, int nr)
687 struct sk_buff *skb = NULL;
688 int count = 0;
691 * Remove all the ack-ed frames from the window queue.
695 * Optimize for the common case. It is most likely that the receiver
696 * will acknowledge all the frames we have sent! So in that case we
697 * delete all frames stored in window.
699 if (nr == self->vs) {
700 while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
701 dev_kfree_skb(skb);
703 /* The last acked frame is the next to send minus one */
704 self->va = nr - 1;
705 } else {
706 /* Remove all acknowledged frames in current window */
707 while ((skb_peek(&self->wx_list) != NULL) &&
708 (((self->va+1) % 8) != nr))
710 skb = skb_dequeue(&self->wx_list);
711 dev_kfree_skb(skb);
713 self->va = (self->va + 1) % 8;
714 count++;
718 /* Advance window */
719 self->window = self->window_size - skb_queue_len(&self->wx_list);
723 * Function irlap_validate_ns_received (ns)
725 * Validate the next to send (ns) field from received frame.
727 int irlap_validate_ns_received(struct irlap_cb *self, int ns)
729 /* ns as expected? */
730 if (ns == self->vr)
731 return NS_EXPECTED;
733 * Stations are allowed to treat invalid NS as unexpected NS
734 * IrLAP, Recv ... with-invalid-Ns. p. 84
736 return NS_UNEXPECTED;
738 /* return NR_INVALID; */
741 * Function irlap_validate_nr_received (nr)
743 * Validate the next to receive (nr) field from received frame.
746 int irlap_validate_nr_received(struct irlap_cb *self, int nr)
748 /* nr as expected? */
749 if (nr == self->vs) {
750 IRDA_DEBUG(4, __FUNCTION__ "(), expected!\n");
751 return NR_EXPECTED;
755 * unexpected nr? (but within current window), first we check if the
756 * ns numbers of the frames in the current window wrap.
758 if (self->va < self->vs) {
759 if ((nr >= self->va) && (nr <= self->vs))
760 return NR_UNEXPECTED;
761 } else {
762 if ((nr >= self->va) || (nr <= self->vs))
763 return NR_UNEXPECTED;
766 /* Invalid nr! */
767 return NR_INVALID;
771 * Function irlap_initiate_connection_state ()
773 * Initialize the connection state parameters
776 void irlap_initiate_connection_state(struct irlap_cb *self)
778 IRDA_DEBUG(4, __FUNCTION__ "()\n");
780 ASSERT(self != NULL, return;);
781 ASSERT(self->magic == LAP_MAGIC, return;);
783 /* Next to send and next to receive */
784 self->vs = self->vr = 0;
786 /* Last frame which got acked (0 - 1) % 8 */
787 self->va = 7;
789 self->window = 1;
791 self->remote_busy = FALSE;
792 self->retry_count = 0;
796 * Function irlap_wait_min_turn_around (self, qos)
798 * Wait negotiated minimum turn around time, this function actually sets
799 * the number of BOS's that must be sent before the next transmitted
800 * frame in order to delay for the specified amount of time. This is
801 * done to avoid using timers, and the forbidden udelay!
803 void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos)
805 __u32 min_turn_time;
806 __u32 speed;
808 /* Get QoS values. */
809 speed = qos->baud_rate.value;
810 min_turn_time = qos->min_turn_time.value;
812 /* No need to calculate XBOFs for speeds over 115200 bps */
813 if (speed > 115200) {
814 self->mtt_required = min_turn_time;
815 return;
819 * Send additional BOF's for the next frame for the requested
820 * min turn time, so now we must calculate how many chars (XBOF's) we
821 * must send for the requested time period (min turn time)
823 self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
827 * Function irlap_flush_all_queues (void)
829 * Flush all queues
832 void irlap_flush_all_queues(struct irlap_cb *self)
834 struct sk_buff* skb;
836 ASSERT(self != NULL, return;);
837 ASSERT(self->magic == LAP_MAGIC, return;);
839 /* Free transmission queue */
840 while ((skb = skb_dequeue(&self->txq)) != NULL)
841 dev_kfree_skb(skb);
843 while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
844 dev_kfree_skb(skb);
846 /* Free sliding window buffered packets */
847 while ((skb = skb_dequeue(&self->wx_list)) != NULL)
848 dev_kfree_skb(skb);
850 #ifdef CONFIG_IRDA_RECYCLE_RR
851 if (self->recycle_rr_skb) {
852 dev_kfree_skb(self->recycle_rr_skb);
853 self->recycle_rr_skb = NULL;
855 #endif
859 * Function irlap_setspeed (self, speed)
861 * Change the speed of the IrDA port
864 void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
866 IRDA_DEBUG(0, __FUNCTION__ "(), setting speed to %d\n", speed);
868 ASSERT(self != NULL, return;);
869 ASSERT(self->magic == LAP_MAGIC, return;);
871 self->speed = speed;
873 /* Change speed now, or just piggyback speed on frames */
874 if (now)
875 irda_device_change_speed(self->netdev, speed);
878 #ifdef CONFIG_IRDA_COMPRESSION
879 void irlap_init_comp_qos_capabilities(struct irlap_cb *self)
881 struct irda_compressor *comp;
882 __u8 mask; /* Current bit tested */
883 int i;
885 ASSERT(self != NULL, return;);
886 ASSERT(self->magic == LAP_MAGIC, return;);
889 * Find out which compressors we support. We do this be checking that
890 * the corresponding compressor for each bit set in the QoS bits has
891 * actually been loaded. Ths is sort of hairy code but that is what
892 * you get when you do a little bit flicking :-)
894 IRDA_DEBUG(4, __FUNCTION__ "(), comp bits 0x%02x\n",
895 self->qos_rx.compression.bits);
896 mask = 0x80; /* Start with testing MSB */
897 for (i=0;i<8;i++) {
898 IRDA_DEBUG(4, __FUNCTION__ "(), testing bit %d\n", 8-i);
899 if (self->qos_rx.compression.bits & mask) {
900 IRDA_DEBUG(4, __FUNCTION__
901 "(), bit %d is set by defalt\n", 8-i);
902 comp = hashbin_find(irlap_compressors,
903 compressions[msb_index(mask)],
904 NULL);
905 if (!comp) {
906 /* Protocol not supported, so clear the bit */
907 IRDA_DEBUG(4, __FUNCTION__ "(), Compression "
908 "protocol %d has not been loaded!\n",
909 compressions[msb_index(mask)]);
910 self->qos_rx.compression.bits &= ~mask;
911 IRDA_DEBUG(4, __FUNCTION__
912 "(), comp bits 0x%02x\n",
913 self->qos_rx.compression.bits);
916 /* Try the next bit */
917 mask >>= 1;
920 #endif
923 * Function irlap_init_qos_capabilities (self, qos)
925 * Initialize QoS for this IrLAP session, What we do is to compute the
926 * intersection of the QoS capabilities for the user, driver and for
927 * IrLAP itself. Normally, IrLAP will not specify any values, but it can
928 * be used to restrict certain values.
930 void irlap_init_qos_capabilities(struct irlap_cb *self,
931 struct qos_info *qos_user)
933 ASSERT(self != NULL, return;);
934 ASSERT(self->magic == LAP_MAGIC, return;);
935 ASSERT(self->netdev != NULL, return;);
937 /* Start out with the maximum QoS support possible */
938 irda_init_max_qos_capabilies(&self->qos_rx);
940 #ifdef CONFIG_IRDA_COMPRESSION
941 irlap_init_comp_qos_capabilities(self);
942 #endif
944 /* Apply drivers QoS capabilities */
945 irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
948 * Check for user supplied QoS parameters. The service user is only
949 * allowed to supply these values. We check each parameter since the
950 * user may not have set all of them.
952 if (qos_user) {
953 IRDA_DEBUG(1, __FUNCTION__ "(), Found user specified QoS!\n");
955 if (qos_user->baud_rate.bits)
956 self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
958 if (qos_user->max_turn_time.bits)
959 self->qos_rx.max_turn_time.bits &= qos_user->max_turn_time.bits;
960 if (qos_user->data_size.bits)
961 self->qos_rx.data_size.bits &= qos_user->data_size.bits;
963 if (qos_user->link_disc_time.bits)
964 self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
965 #ifdef CONFIG_IRDA_COMPRESSION
966 self->qos_rx.compression.bits &= qos_user->compression.bits;
967 #endif
970 /* Use 500ms in IrLAP for now */
971 self->qos_rx.max_turn_time.bits &= 0x01;
973 /* Set data size */
974 /*self->qos_rx.data_size.bits &= 0x03;*/
976 /* Set disconnect time */
977 self->qos_rx.link_disc_time.bits &= 0x07;
979 irda_qos_bits_to_value(&self->qos_rx);
983 * Function irlap_apply_default_connection_parameters (void)
985 * Use the default connection and transmission parameters
988 void irlap_apply_default_connection_parameters(struct irlap_cb *self)
990 IRDA_DEBUG(4, __FUNCTION__ "()\n");
992 ASSERT(self != NULL, return;);
993 ASSERT(self->magic == LAP_MAGIC, return;);
995 irlap_change_speed(self, 9600, TRUE);
997 /* Set mbusy when going to NDM state */
998 irda_device_set_media_busy(self->netdev, TRUE);
1000 /* Default value in NDM */
1001 self->bofs_count = 11;
1004 * Generate random connection address for this session, which must
1005 * be 7 bits wide and different from 0x00 and 0xfe
1007 while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
1008 get_random_bytes(&self->caddr, sizeof(self->caddr));
1009 self->caddr &= 0xfe;
1012 /* Use default values until connection has been negitiated */
1013 self->slot_timeout = sysctl_slot_timeout;
1014 self->final_timeout = FINAL_TIMEOUT;
1015 self->poll_timeout = POLL_TIMEOUT;
1016 self->wd_timeout = WD_TIMEOUT;
1018 /* Set some default values */
1019 self->qos_tx.baud_rate.value = 9600;
1020 self->qos_rx.baud_rate.value = 9600;
1021 self->qos_tx.max_turn_time.value = 0;
1022 self->qos_rx.max_turn_time.value = 0;
1023 self->qos_tx.min_turn_time.value = 0;
1024 self->qos_rx.min_turn_time.value = 0;
1025 self->qos_tx.data_size.value = 64;
1026 self->qos_rx.data_size.value = 64;
1027 self->qos_tx.window_size.value = 1;
1028 self->qos_rx.window_size.value = 1;
1029 self->qos_tx.additional_bofs.value = 11;
1030 self->qos_rx.additional_bofs.value = 11;
1031 self->qos_tx.link_disc_time.value = 0;
1032 self->qos_rx.link_disc_time.value = 0;
1034 irlap_flush_all_queues(self);
1036 self->disconnect_pending = FALSE;
1037 self->connect_pending = FALSE;
1041 * Function irlap_apply_connection_parameters (qos)
1043 * Initialize IrLAP with the negotiated QoS values
1046 void irlap_apply_connection_parameters(struct irlap_cb *self)
1048 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1050 ASSERT(self != NULL, return;);
1051 ASSERT(self->magic == LAP_MAGIC, return;);
1053 irlap_change_speed(self, self->qos_tx.baud_rate.value, FALSE);
1055 self->window_size = self->qos_tx.window_size.value;
1056 self->window = self->qos_tx.window_size.value;
1057 self->bofs_count = self->qos_tx.additional_bofs.value;
1060 * Calculate how many bytes it is possible to transmit before the
1061 * link must be turned around
1063 self->line_capacity =
1064 irlap_max_line_capacity(self->qos_tx.baud_rate.value,
1065 self->qos_tx.max_turn_time.value);
1067 * Set N1 to 0 if Link Disconnect/Threshold Time = 3 and set it to
1068 * 3 seconds otherwise. See page 71 in IrLAP for more details.
1069 * TODO: these values should be calculated from the final timer
1070 * as well
1072 ASSERT(self->qos_tx.max_turn_time.value != 0, return;);
1073 if (self->qos_tx.link_disc_time.value == 3)
1074 self->N1 = 0;
1075 else
1076 self->N1 = 3000 / self->qos_tx.max_turn_time.value;
1078 IRDA_DEBUG(4, "Setting N1 = %d\n", self->N1);
1081 self->N2 = self->qos_tx.link_disc_time.value * 1000 /
1082 self->qos_tx.max_turn_time.value;
1083 IRDA_DEBUG(4, "Setting N2 = %d\n", self->N2);
1086 * Initialize timeout values, some of the rules are listed on
1087 * page 92 in IrLAP.
1089 self->poll_timeout = self->qos_tx.max_turn_time.value * HZ / 1000;
1090 self->wd_timeout = self->poll_timeout * 2;
1093 * Be careful to keep our promises to the peer device about how long
1094 * time it can keep the pf bit. So here we must use the rx_qos value
1096 self->final_timeout = self->qos_rx.max_turn_time.value * HZ / 1000;
1098 #ifdef CONFIG_IRDA_COMPRESSION
1099 if (self->qos_tx.compression.value) {
1100 IRDA_DEBUG(1, __FUNCTION__ "(), Initializing compression\n");
1101 irda_set_compression(self, self->qos_tx.compression.value);
1103 irlap_compressor_init(self, 0);
1105 #endif
1109 * Function irlap_set_local_busy (self, status)
1114 void irlap_set_local_busy(struct irlap_cb *self, int status)
1116 IRDA_DEBUG(0, __FUNCTION__ "()\n");
1118 self->local_busy = status;
1120 if (status)
1121 IRDA_DEBUG(0, __FUNCTION__ "(), local busy ON\n");
1122 else
1123 IRDA_DEBUG(0, __FUNCTION__ "(), local busy OFF\n");
1126 #ifdef CONFIG_PROC_FS
1128 * Function irlap_proc_read (buf, start, offset, len, unused)
1130 * Give some info to the /proc file system
1133 int irlap_proc_read(char *buf, char **start, off_t offset, int len)
1135 struct irlap_cb *self;
1136 unsigned long flags;
1137 int i = 0;
1139 save_flags(flags);
1140 cli();
1142 len = 0;
1144 self = (struct irlap_cb *) hashbin_get_first(irlap);
1145 while (self != NULL) {
1146 ASSERT(self != NULL, return -ENODEV;);
1147 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1149 len += sprintf(buf+len, "irlap%d ", i++);
1150 len += sprintf(buf+len, "state: %s\n",
1151 irlap_state[self->state]);
1153 len += sprintf(buf+len, " caddr: %#02x, ", self->caddr);
1154 len += sprintf(buf+len, "saddr: %#08x, ", self->saddr);
1155 len += sprintf(buf+len, "daddr: %#08x\n", self->daddr);
1157 len += sprintf(buf+len, " win size: %d, ",
1158 self->window_size);
1159 len += sprintf(buf+len, "win: %d, ", self->window);
1160 #if CONFIG_IRDA_DYNAMIC_WINDOW
1161 len += sprintf(buf+len, "line capacity: %d, ",
1162 self->line_capacity);
1163 len += sprintf(buf+len, "bytes left: %d\n", self->bytes_left);
1164 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1165 len += sprintf(buf+len, " tx queue len: %d ",
1166 skb_queue_len(&self->txq));
1167 len += sprintf(buf+len, "win queue len: %d ",
1168 skb_queue_len(&self->wx_list));
1169 len += sprintf(buf+len, "rbusy: %s", self->remote_busy ?
1170 "TRUE" : "FALSE");
1171 len += sprintf(buf+len, " mbusy: %s\n", self->media_busy ?
1172 "TRUE" : "FALSE");
1174 len += sprintf(buf+len, " retrans: %d ", self->retry_count);
1175 len += sprintf(buf+len, "vs: %d ", self->vs);
1176 len += sprintf(buf+len, "vr: %d ", self->vr);
1177 len += sprintf(buf+len, "va: %d\n", self->va);
1179 len += sprintf(buf+len, " qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
1181 len += sprintf(buf+len, " tx\t%d\t",
1182 self->qos_tx.baud_rate.value);
1183 len += sprintf(buf+len, "%d\t",
1184 self->qos_tx.max_turn_time.value);
1185 len += sprintf(buf+len, "%d\t",
1186 self->qos_tx.data_size.value);
1187 len += sprintf(buf+len, "%d\t",
1188 self->qos_tx.window_size.value);
1189 len += sprintf(buf+len, "%d\t",
1190 self->qos_tx.additional_bofs.value);
1191 len += sprintf(buf+len, "%d\t",
1192 self->qos_tx.min_turn_time.value);
1193 len += sprintf(buf+len, "%d\t",
1194 self->qos_tx.link_disc_time.value);
1195 #ifdef CONFIG_IRDA_COMPRESSION
1196 len += sprintf(buf+len, "%d",
1197 self->qos_tx.compression.value);
1198 #endif
1199 len += sprintf(buf+len, "\n");
1201 len += sprintf(buf+len, " rx\t%d\t",
1202 self->qos_rx.baud_rate.value);
1203 len += sprintf(buf+len, "%d\t",
1204 self->qos_rx.max_turn_time.value);
1205 len += sprintf(buf+len, "%d\t",
1206 self->qos_rx.data_size.value);
1207 len += sprintf(buf+len, "%d\t",
1208 self->qos_rx.window_size.value);
1209 len += sprintf(buf+len, "%d\t",
1210 self->qos_rx.additional_bofs.value);
1211 len += sprintf(buf+len, "%d\t",
1212 self->qos_rx.min_turn_time.value);
1213 len += sprintf(buf+len, "%d\t",
1214 self->qos_rx.link_disc_time.value);
1215 #ifdef CONFIG_IRDA_COMPRESSION
1216 len += sprintf(buf+len, "%d",
1217 self->qos_rx.compression.value);
1218 #endif
1219 len += sprintf(buf+len, "\n");
1221 self = (struct irlap_cb *) hashbin_get_next(irlap);
1223 restore_flags(flags);
1225 return len;
1228 #endif /* CONFIG_PROC_FS */