1 /*********************************************************************
5 * Description: IrLAP implementation for Linux
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.
13 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
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 * 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.
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,
30 ********************************************************************/
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>
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>
51 hashbin_t *irlap = NULL;
52 int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
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;
59 extern void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb);
60 static void __irlap_close(struct irlap_cb *self);
62 #ifdef CONFIG_IRDA_DEBUG
63 static char *lap_reasons[] = {
65 "LAP_DISC_INDICATION",
67 "LAP_RESET_INDICATION",
70 "LAP_PRIMARY_CONFLICT",
73 #endif /* CONFIG_IRDA_DEBUG */
76 int irlap_proc_read(char *, char **, off_t, int);
78 #endif /* CONFIG_PROC_FS */
80 int __init irlap_init(void)
82 /* Allocate master array */
83 irlap = hashbin_new(HB_LOCAL);
85 ERROR("%s(), can't allocate irlap hashbin!\n", __FUNCTION__);
92 void irlap_cleanup(void)
94 ASSERT(irlap != NULL, return;);
96 hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
100 * Function irlap_open (driver)
102 * Initialize IrLAP layer
105 struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos,
108 struct irlap_cb *self;
110 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
112 /* Initialize the irlap structure. */
113 self = kmalloc(sizeof(struct irlap_cb), GFP_KERNEL);
117 memset(self, 0, sizeof(struct irlap_cb));
118 self->magic = LAP_MAGIC;
120 /* Make a binding between the layers */
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';
128 self->hw_name[0] = '\0';
131 /* FIXME: should we get our own field? */
132 dev->atalk_ptr = self;
134 self->state = LAP_OFFLINE;
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);
141 /* My unique IrLAP device address! */
142 get_random_bytes(&self->saddr, sizeof(self->saddr));
143 memcpy(dev->dev_addr, &self->saddr, 4);
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);
154 irlap_apply_default_connection_parameters(self);
156 self->N3 = 3; /* # connections attemts to try before giving up */
158 self->state = LAP_NDM;
160 hashbin_insert(irlap, (irda_queue_t *) self, self->saddr, NULL);
162 irlmp_register_link(self, self->saddr, &self->notify);
168 * Function __irlap_close (self)
170 * Remove IrLAP and all allocated memory. Stop any pending timers.
173 static void __irlap_close(struct irlap_cb *self)
175 ASSERT(self != NULL, return;);
176 ASSERT(self->magic == LAP_MAGIC, return;);
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);
188 irlap_flush_all_queues(self);
196 * Function irlap_close (self)
198 * Remove IrLAP instance
201 void irlap_close(struct irlap_cb *self)
203 struct irlap_cb *lap;
205 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
207 ASSERT(self != NULL, return;);
208 ASSERT(self->magic == LAP_MAGIC, return;);
210 irlap_disconnect_indication(self, LAP_DISC_INDICATION);
212 irlmp_unregister_link(self->saddr);
213 self->notify.instance = NULL;
215 /* Be sure that we manage to remove ourself from the hash */
216 lap = hashbin_remove(irlap, self->saddr, NULL);
218 IRDA_DEBUG(1, "%s(), Didn't find myself!\n", __FUNCTION__);
225 * Function irlap_connect_indication (self, skb)
227 * Another device is attempting to make a connection
230 void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb)
232 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
234 ASSERT(self != NULL, return;);
235 ASSERT(self->magic == LAP_MAGIC, return;);
237 irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
239 skb_get(skb); /*LEVEL4*/
240 irlmp_link_connect_indication(self->notify.instance, self->saddr,
241 self->daddr, &self->qos_tx, skb);
245 * Function irlap_connect_response (self, skb)
247 * Service user has accepted incoming connection
250 void irlap_connect_response(struct irlap_cb *self, struct sk_buff *skb)
252 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
254 irlap_do_event(self, CONNECT_RESPONSE, skb, NULL);
259 * Function irlap_connect_request (self, daddr, qos_user, sniff)
261 * Request connection with another device, sniffing is not implemented
265 void irlap_connect_request(struct irlap_cb *self, __u32 daddr,
266 struct qos_info *qos_user, int sniff)
268 IRDA_DEBUG(3, "%s(), daddr=0x%08x\n", __FUNCTION__, daddr);
270 ASSERT(self != NULL, return;);
271 ASSERT(self->magic == LAP_MAGIC, return;);
276 * If the service user specifies QoS values for this connection,
279 irlap_init_qos_capabilities(self, qos_user);
281 if ((self->state == LAP_NDM) && !self->media_busy)
282 irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
284 self->connect_pending = TRUE;
288 * Function irlap_connect_confirm (self, skb)
290 * Connection request has been accepted
293 void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
295 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
297 ASSERT(self != NULL, return;);
298 ASSERT(self->magic == LAP_MAGIC, return;);
300 skb_get(skb); /*LEVEL4*/
301 irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
305 * Function irlap_data_indication (self, skb)
307 * Received data frames from IR-port, so we just pass them up to
308 * IrLMP for further processing
311 void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
314 /* Hide LAP header from IrLMP layer */
315 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
317 skb_get(skb); /*LEVEL4*/
318 irlmp_link_data_indication(self->notify.instance, skb, unreliable);
323 * Function irlap_data_request (self, skb)
325 * Queue data for transmission, must wait until XMIT state
328 void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb,
331 ASSERT(self != NULL, return;);
332 ASSERT(self->magic == LAP_MAGIC, return;);
334 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
336 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
338 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
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
345 skb->data[1] = UI_FRAME;
347 skb->data[1] = I_FRAME;
349 /* Add at the end of the queue (keep ordering) - Jean II */
350 skb_queue_tail(&self->txq, skb);
353 * Send event if this frame only if we are in the right state
354 * FIXME: udata should be sent first! (skb_queue_head?)
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 */
367 * Function irlap_unitdata_request (self, skb)
369 * Send Ultra data. This is data that must be sent outside any connection
372 #ifdef CONFIG_IRDA_ULTRA
373 void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
375 ASSERT(self != NULL, return;);
376 ASSERT(self->magic == LAP_MAGIC, return;);
378 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
380 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
382 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
384 skb->data[0] = CBROADCAST;
385 skb->data[1] = UI_FRAME;
387 skb_queue_tail(&self->txq_ultra, skb);
389 irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
391 #endif /*CONFIG_IRDA_ULTRA */
394 * Function irlap_udata_indication (self, skb)
396 * Receive Ultra data. This is data that is received outside any connection
399 #ifdef CONFIG_IRDA_ULTRA
400 void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
402 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
404 ASSERT(self != NULL, return;);
405 ASSERT(self->magic == LAP_MAGIC, return;);
406 ASSERT(skb != NULL, return;);
408 /* Hide LAP header from IrLMP layer */
409 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
411 skb_get(skb); /*LEVEL4*/
412 irlmp_link_unitdata_indication(self->notify.instance, skb);
414 #endif /* CONFIG_IRDA_ULTRA */
417 * Function irlap_disconnect_request (void)
419 * Request to disconnect connection by service user
421 void irlap_disconnect_request(struct irlap_cb *self)
423 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
425 ASSERT(self != NULL, return;);
426 ASSERT(self->magic == LAP_MAGIC, return;);
428 /* Don't disconnect until all data frames are successfully sent */
429 if (skb_queue_len(&self->txq) > 0) {
430 self->disconnect_pending = TRUE;
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);
445 IRDA_DEBUG(2, "%s(), disconnect pending!\n", __FUNCTION__);
446 self->disconnect_pending = TRUE;
452 * Function irlap_disconnect_indication (void)
454 * Disconnect request from other device
457 void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason)
459 IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, lap_reasons[reason]);
461 ASSERT(self != NULL, return;);
462 ASSERT(self->magic == LAP_MAGIC, return;);
465 irlap_flush_all_queues(self);
468 case LAP_RESET_INDICATION:
469 IRDA_DEBUG(1, "%s(), Sending reset request!\n", __FUNCTION__);
470 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
472 case LAP_NO_RESPONSE: /* FALLTROUGH */
473 case LAP_DISC_INDICATION: /* FALLTROUGH */
474 case LAP_FOUND_NONE: /* FALLTROUGH */
476 irlmp_link_disconnect_indication(self->notify.instance, self,
480 ERROR("%s(), Unknown reason %d\n", __FUNCTION__, reason);
485 * Function irlap_discovery_request (gen_addr_bit)
487 * Start one single discovery operation.
490 void irlap_discovery_request(struct irlap_cb *self, discovery_t *discovery)
492 struct irlap_info info;
494 ASSERT(self != NULL, return;);
495 ASSERT(self->magic == LAP_MAGIC, return;);
496 ASSERT(discovery != NULL, return;);
498 IRDA_DEBUG(4, "%s(), nslots = %d\n", __FUNCTION__, discovery->nslots);
500 ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
501 (discovery->nslots == 8) || (discovery->nslots == 16),
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",
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.
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;
525 self->discovery_log= hashbin_new(HB_LOCAL);
527 info.S = discovery->nslots; /* Number of slots */
528 info.s = 0; /* Current slot */
530 self->discovery_cmd = discovery;
531 info.discovery = discovery;
533 /* sysctl_slot_timeout bounds are checked in irsysctl.c - Jean II */
534 self->slot_timeout = sysctl_slot_timeout * HZ / 1000;
536 irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
540 * Function irlap_discovery_confirm (log)
542 * A device has been discovered in front of this station, we
543 * report directly to LMP.
545 void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log)
547 ASSERT(self != NULL, return;);
548 ASSERT(self->magic == LAP_MAGIC, return;);
550 ASSERT(self->notify.instance != NULL, return;);
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
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
564 irda_device_set_media_busy(self->netdev, FALSE);
567 irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
571 * Function irlap_discovery_indication (log)
573 * Somebody is trying to discover us!
576 void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery)
578 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
580 ASSERT(self != NULL, return;);
581 ASSERT(self->magic == LAP_MAGIC, return;);
582 ASSERT(discovery != NULL, return;);
584 ASSERT(self->notify.instance != NULL, return;);
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 !
595 irda_device_set_media_busy(self->netdev, SMALL);
597 irlmp_link_discovery_indication(self->notify.instance, discovery);
601 * Function irlap_status_indication (quality_of_link)
606 void irlap_status_indication(struct irlap_cb *self, int quality_of_link)
608 switch (quality_of_link) {
609 case STATUS_NO_ACTIVITY:
610 MESSAGE("IrLAP, no activity on link!\n");
613 MESSAGE("IrLAP, noisy link!\n");
618 irlmp_status_indication(self->notify.instance,
619 quality_of_link, LOCK_NO_CHANGE);
623 * Function irlap_reset_indication (void)
628 void irlap_reset_indication(struct irlap_cb *self)
630 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
632 ASSERT(self != NULL, return;);
633 ASSERT(self->magic == LAP_MAGIC, return;);
635 if (self->state == LAP_RESET_WAIT)
636 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
638 irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
642 * Function irlap_reset_confirm (void)
647 void irlap_reset_confirm(void)
649 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
653 * Function irlap_generate_rand_time_slot (S, s)
655 * Generate a random time slot between s and S-1 where
656 * S = Number of slots (0 -> S-1)
659 int irlap_generate_rand_time_slot(int S, int s)
664 ASSERT((S - s) > 0, return 0;);
667 rand ^= (rand << 12);
668 rand ^= (rand >> 20);
670 slot = s + rand % (S-s);
672 ASSERT((slot >= s) || (slot < S), return 0;);
678 * Function irlap_update_nr_received (nr)
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.
684 void irlap_update_nr_received(struct irlap_cb *self, int nr)
686 struct sk_buff *skb = NULL;
690 * Remove all the ack-ed frames from the window queue.
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.
698 if (nr == self->vs) {
699 while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
702 /* The last acked frame is the next to send minus one */
705 /* Remove all acknowledged frames in current window */
706 while ((skb_peek(&self->wx_list) != NULL) &&
707 (((self->va+1) % 8) != nr))
709 skb = skb_dequeue(&self->wx_list);
712 self->va = (self->va + 1) % 8;
718 self->window = self->window_size - skb_queue_len(&self->wx_list);
722 * Function irlap_validate_ns_received (ns)
724 * Validate the next to send (ns) field from received frame.
726 int irlap_validate_ns_received(struct irlap_cb *self, int ns)
728 /* ns as expected? */
732 * Stations are allowed to treat invalid NS as unexpected NS
733 * IrLAP, Recv ... with-invalid-Ns. p. 84
735 return NS_UNEXPECTED;
737 /* return NR_INVALID; */
740 * Function irlap_validate_nr_received (nr)
742 * Validate the next to receive (nr) field from received frame.
745 int irlap_validate_nr_received(struct irlap_cb *self, int nr)
747 /* nr as expected? */
748 if (nr == self->vs) {
749 IRDA_DEBUG(4, "%s(), expected!\n", __FUNCTION__);
754 * unexpected nr? (but within current window), first we check if the
755 * ns numbers of the frames in the current window wrap.
757 if (self->va < self->vs) {
758 if ((nr >= self->va) && (nr <= self->vs))
759 return NR_UNEXPECTED;
761 if ((nr >= self->va) || (nr <= self->vs))
762 return NR_UNEXPECTED;
770 * Function irlap_initiate_connection_state ()
772 * Initialize the connection state parameters
775 void irlap_initiate_connection_state(struct irlap_cb *self)
777 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
779 ASSERT(self != NULL, return;);
780 ASSERT(self->magic == LAP_MAGIC, return;);
782 /* Next to send and next to receive */
783 self->vs = self->vr = 0;
785 /* Last frame which got acked (0 - 1) % 8 */
790 self->remote_busy = FALSE;
791 self->retry_count = 0;
795 * Function irlap_wait_min_turn_around (self, qos)
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!
802 void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos)
807 /* Get QoS values. */
808 speed = qos->baud_rate.value;
809 min_turn_time = qos->min_turn_time.value;
811 /* No need to calculate XBOFs for speeds over 115200 bps */
812 if (speed > 115200) {
813 self->mtt_required = min_turn_time;
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)
822 self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
826 * Function irlap_flush_all_queues (void)
831 void irlap_flush_all_queues(struct irlap_cb *self)
835 ASSERT(self != NULL, return;);
836 ASSERT(self->magic == LAP_MAGIC, return;);
838 /* Free transmission queue */
839 while ((skb = skb_dequeue(&self->txq)) != NULL)
842 while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
845 /* Free sliding window buffered packets */
846 while ((skb = skb_dequeue(&self->wx_list)) != NULL)
851 * Function irlap_setspeed (self, speed)
853 * Change the speed of the IrDA port
856 void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
860 IRDA_DEBUG(0, "%s(), setting speed to %d\n", __FUNCTION__, speed);
862 ASSERT(self != NULL, return;);
863 ASSERT(self->magic == LAP_MAGIC, return;);
867 /* Change speed now, or just piggyback speed on frames */
869 /* Send down empty frame to trigger speed change */
870 skb = dev_alloc_skb(0);
871 irlap_queue_xmit(self, skb);
876 * Function irlap_init_qos_capabilities (self, qos)
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.
883 void irlap_init_qos_capabilities(struct irlap_cb *self,
884 struct qos_info *qos_user)
886 ASSERT(self != NULL, return;);
887 ASSERT(self->magic == LAP_MAGIC, return;);
888 ASSERT(self->netdev != NULL, return;);
890 /* Start out with the maximum QoS support possible */
891 irda_init_max_qos_capabilies(&self->qos_rx);
893 /* Apply drivers QoS capabilities */
894 irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
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.
902 IRDA_DEBUG(1, "%s(), Found user specified QoS!\n", __FUNCTION__);
904 if (qos_user->baud_rate.bits)
905 self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
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;
912 if (qos_user->link_disc_time.bits)
913 self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
916 /* Use 500ms in IrLAP for now */
917 self->qos_rx.max_turn_time.bits &= 0x01;
920 /*self->qos_rx.data_size.bits &= 0x03;*/
922 irda_qos_bits_to_value(&self->qos_rx);
926 * Function irlap_apply_default_connection_parameters (void, now)
928 * Use the default connection and transmission parameters
931 void irlap_apply_default_connection_parameters(struct irlap_cb *self)
933 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
935 ASSERT(self != NULL, return;);
936 ASSERT(self->magic == LAP_MAGIC, return;);
938 /* xbofs : Default value in NDM */
939 self->next_bofs = 12;
940 self->bofs_count = 12;
942 /* NDM Speed is 9600 */
943 irlap_change_speed(self, 9600, TRUE);
945 /* Set mbusy when going to NDM state */
946 irda_device_set_media_busy(self->netdev, TRUE);
949 * Generate random connection address for this session, which must
950 * be 7 bits wide and different from 0x00 and 0xfe
952 while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
953 get_random_bytes(&self->caddr, sizeof(self->caddr));
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;
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;
979 irlap_flush_all_queues(self);
981 self->disconnect_pending = FALSE;
982 self->connect_pending = FALSE;
986 * Function irlap_apply_connection_parameters (qos, now)
988 * Initialize IrLAP with the negotiated QoS values
990 * If 'now' is false, the speed and xbofs will be changed after the next
992 * If 'now' is true, the speed and xbofs is changed immediately
994 void irlap_apply_connection_parameters(struct irlap_cb *self, int now)
996 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
998 ASSERT(self != NULL, return;);
999 ASSERT(self->magic == LAP_MAGIC, return;);
1001 /* Set the negociated xbofs value */
1002 self->next_bofs = self->qos_tx.additional_bofs.value;
1004 self->bofs_count = self->next_bofs;
1006 /* Set the negociated link speed (may need the new xbofs value) */
1007 irlap_change_speed(self, self->qos_tx.baud_rate.value, now);
1009 self->window_size = self->qos_tx.window_size.value;
1010 self->window = self->qos_tx.window_size.value;
1012 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1014 * Calculate how many bytes it is possible to transmit before the
1015 * link must be turned around
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 */
1025 * Initialize timeout values, some of the rules are listed on
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.
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;
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.
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
1065 if (self->qos_tx.link_disc_time.value == sysctl_warn_noreply_time)
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,
1071 self->N1 = -2; /* Disable - Need to be multiple of 2*/
1073 self->N1 = sysctl_warn_noreply_time * 1000 /
1074 self->qos_rx.max_turn_time.value;
1076 IRDA_DEBUG(4, "Setting N1 = %d\n", self->N1);
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);
1084 #ifdef CONFIG_PROC_FS
1086 * Function irlap_proc_read (buf, start, offset, len, unused)
1088 * Give some info to the /proc file system
1091 int irlap_proc_read(char *buf, char **start, off_t offset, int len)
1093 struct irlap_cb *self;
1094 unsigned long flags;
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;);
1107 len += sprintf(buf+len, "irlap%d ", i++);
1108 len += sprintf(buf+len, "state: %s\n",
1109 irlap_state[self->state]);
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);
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);
1119 len += sprintf(buf+len, " win size: %d, ",
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 ?
1133 len += sprintf(buf+len, " mbusy: %s\n", self->media_busy ?
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);
1141 len += sprintf(buf+len, " qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
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");
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");
1175 self = (struct irlap_cb *) hashbin_get_next(irlap);
1177 restore_flags(flags);
1182 #endif /* CONFIG_PROC_FS */