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-2003 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>
40 #include <linux/module.h>
41 #include <linux/seq_file.h>
43 #include <net/irda/irda.h>
44 #include <net/irda/irda_device.h>
45 #include <net/irda/irqueue.h>
46 #include <net/irda/irlmp.h>
47 #include <net/irda/irlmp_frame.h>
48 #include <net/irda/irlap_frame.h>
49 #include <net/irda/irlap.h>
50 #include <net/irda/timer.h>
51 #include <net/irda/qos.h>
53 static hashbin_t *irlap = NULL;
54 int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
56 /* This is the delay of missed pf period before generating an event
57 * to the application. The spec mandate 3 seconds, but in some cases
58 * it's way too long. - Jean II */
59 int sysctl_warn_noreply_time = 3;
61 extern void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb);
62 static void __irlap_close(struct irlap_cb *self);
64 #ifdef CONFIG_IRDA_DEBUG
65 static char *lap_reasons[] = {
67 "LAP_DISC_INDICATION",
69 "LAP_RESET_INDICATION",
72 "LAP_PRIMARY_CONFLICT",
75 #endif /* CONFIG_IRDA_DEBUG */
77 int __init irlap_init(void)
79 /* Check if the compiler did its job properly.
80 * May happen on some ARM configuration, check with Russell King. */
81 ASSERT(sizeof(struct xid_frame) == 14, ;);
82 ASSERT(sizeof(struct test_frame) == 10, ;);
83 ASSERT(sizeof(struct ua_frame) == 10, ;);
84 ASSERT(sizeof(struct snrm_frame) == 11, ;);
86 /* Allocate master array */
87 irlap = hashbin_new(HB_LOCK);
89 ERROR("%s: can't allocate irlap hashbin!\n", __FUNCTION__);
96 void __exit irlap_cleanup(void)
98 ASSERT(irlap != NULL, return;);
100 hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
104 * Function irlap_open (driver)
106 * Initialize IrLAP layer
109 struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos,
112 struct irlap_cb *self;
114 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
116 /* Initialize the irlap structure. */
117 self = kmalloc(sizeof(struct irlap_cb), GFP_KERNEL);
121 memset(self, 0, sizeof(struct irlap_cb));
122 self->magic = LAP_MAGIC;
124 /* Make a binding between the layers */
127 /* Copy hardware name */
128 if(hw_name != NULL) {
129 strlcpy(self->hw_name, hw_name, sizeof(self->hw_name));
131 self->hw_name[0] = '\0';
134 /* FIXME: should we get our own field? */
135 dev->atalk_ptr = self;
137 self->state = LAP_OFFLINE;
139 /* Initialize transmit queue */
140 skb_queue_head_init(&self->txq);
141 skb_queue_head_init(&self->txq_ultra);
142 skb_queue_head_init(&self->wx_list);
144 /* My unique IrLAP device address! */
145 /* We don't want the broadcast address, neither the NULL address
146 * (most often used to signify "invalid"), and we don't want an
147 * address already in use (otherwise connect won't be able
148 * to select the proper link). - Jean II */
150 get_random_bytes(&self->saddr, sizeof(self->saddr));
151 } while ((self->saddr == 0x0) || (self->saddr == BROADCAST) ||
152 (hashbin_lock_find(irlap, self->saddr, NULL)) );
153 /* Copy to the driver */
154 memcpy(dev->dev_addr, &self->saddr, 4);
156 init_timer(&self->slot_timer);
157 init_timer(&self->query_timer);
158 init_timer(&self->discovery_timer);
159 init_timer(&self->final_timer);
160 init_timer(&self->poll_timer);
161 init_timer(&self->wd_timer);
162 init_timer(&self->backoff_timer);
163 init_timer(&self->media_busy_timer);
165 irlap_apply_default_connection_parameters(self);
167 self->N3 = 3; /* # connections attemts to try before giving up */
169 self->state = LAP_NDM;
171 hashbin_insert(irlap, (irda_queue_t *) self, self->saddr, NULL);
173 irlmp_register_link(self, self->saddr, &self->notify);
177 EXPORT_SYMBOL(irlap_open);
180 * Function __irlap_close (self)
182 * Remove IrLAP and all allocated memory. Stop any pending timers.
185 static void __irlap_close(struct irlap_cb *self)
187 ASSERT(self != NULL, return;);
188 ASSERT(self->magic == LAP_MAGIC, return;);
191 del_timer(&self->slot_timer);
192 del_timer(&self->query_timer);
193 del_timer(&self->discovery_timer);
194 del_timer(&self->final_timer);
195 del_timer(&self->poll_timer);
196 del_timer(&self->wd_timer);
197 del_timer(&self->backoff_timer);
198 del_timer(&self->media_busy_timer);
200 irlap_flush_all_queues(self);
208 * Function irlap_close (self)
210 * Remove IrLAP instance
213 void irlap_close(struct irlap_cb *self)
215 struct irlap_cb *lap;
217 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
219 ASSERT(self != NULL, return;);
220 ASSERT(self->magic == LAP_MAGIC, return;);
222 /* We used to send a LAP_DISC_INDICATION here, but this was
223 * racy. This has been move within irlmp_unregister_link()
226 /* Kill the LAP and all LSAPs on top of it */
227 irlmp_unregister_link(self->saddr);
228 self->notify.instance = NULL;
230 /* Be sure that we manage to remove ourself from the hash */
231 lap = hashbin_remove(irlap, self->saddr, NULL);
233 IRDA_DEBUG(1, "%s(), Didn't find myself!\n", __FUNCTION__);
238 EXPORT_SYMBOL(irlap_close);
241 * Function irlap_connect_indication (self, skb)
243 * Another device is attempting to make a connection
246 void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb)
248 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
250 ASSERT(self != NULL, return;);
251 ASSERT(self->magic == LAP_MAGIC, return;);
253 irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
255 irlmp_link_connect_indication(self->notify.instance, self->saddr,
256 self->daddr, &self->qos_tx, skb);
260 * Function irlap_connect_response (self, skb)
262 * Service user has accepted incoming connection
265 void irlap_connect_response(struct irlap_cb *self, struct sk_buff *userdata)
267 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
269 irlap_do_event(self, CONNECT_RESPONSE, userdata, NULL);
273 * Function irlap_connect_request (self, daddr, qos_user, sniff)
275 * Request connection with another device, sniffing is not implemented
279 void irlap_connect_request(struct irlap_cb *self, __u32 daddr,
280 struct qos_info *qos_user, int sniff)
282 IRDA_DEBUG(3, "%s(), daddr=0x%08x\n", __FUNCTION__, daddr);
284 ASSERT(self != NULL, return;);
285 ASSERT(self->magic == LAP_MAGIC, return;);
290 * If the service user specifies QoS values for this connection,
293 irlap_init_qos_capabilities(self, qos_user);
295 if ((self->state == LAP_NDM) && !self->media_busy)
296 irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
298 self->connect_pending = TRUE;
302 * Function irlap_connect_confirm (self, skb)
304 * Connection request has been accepted
307 void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
309 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
311 ASSERT(self != NULL, return;);
312 ASSERT(self->magic == LAP_MAGIC, return;);
314 irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
318 * Function irlap_data_indication (self, skb)
320 * Received data frames from IR-port, so we just pass them up to
321 * IrLMP for further processing
324 void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
327 /* Hide LAP header from IrLMP layer */
328 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
330 irlmp_link_data_indication(self->notify.instance, skb, unreliable);
335 * Function irlap_data_request (self, skb)
337 * Queue data for transmission, must wait until XMIT state
340 void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb,
343 ASSERT(self != NULL, return;);
344 ASSERT(self->magic == LAP_MAGIC, return;);
346 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
348 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
350 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
353 * Must set frame format now so that the rest of the code knows
354 * if its dealing with an I or an UI frame
357 skb->data[1] = UI_FRAME;
359 skb->data[1] = I_FRAME;
361 /* Don't forget to refcount it - see irlmp_connect_request(). */
364 /* Add at the end of the queue (keep ordering) - Jean II */
365 skb_queue_tail(&self->txq, skb);
368 * Send event if this frame only if we are in the right state
369 * FIXME: udata should be sent first! (skb_queue_head?)
371 if ((self->state == LAP_XMIT_P) || (self->state == LAP_XMIT_S)) {
372 /* If we are not already processing the Tx queue, trigger
373 * transmission immediately - Jean II */
374 if((skb_queue_len(&self->txq) <= 1) && (!self->local_busy))
375 irlap_do_event(self, DATA_REQUEST, skb, NULL);
376 /* Otherwise, the packets will be sent normally at the
377 * next pf-poll - Jean II */
382 * Function irlap_unitdata_request (self, skb)
384 * Send Ultra data. This is data that must be sent outside any connection
387 #ifdef CONFIG_IRDA_ULTRA
388 void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
390 ASSERT(self != NULL, return;);
391 ASSERT(self->magic == LAP_MAGIC, return;);
393 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
395 ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
397 skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
399 skb->data[0] = CBROADCAST;
400 skb->data[1] = UI_FRAME;
402 /* Don't need to refcount, see irlmp_connless_data_request() */
404 skb_queue_tail(&self->txq_ultra, skb);
406 irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
408 #endif /*CONFIG_IRDA_ULTRA */
411 * Function irlap_udata_indication (self, skb)
413 * Receive Ultra data. This is data that is received outside any connection
416 #ifdef CONFIG_IRDA_ULTRA
417 void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
419 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
421 ASSERT(self != NULL, return;);
422 ASSERT(self->magic == LAP_MAGIC, return;);
423 ASSERT(skb != NULL, return;);
425 /* Hide LAP header from IrLMP layer */
426 skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
428 irlmp_link_unitdata_indication(self->notify.instance, skb);
430 #endif /* CONFIG_IRDA_ULTRA */
433 * Function irlap_disconnect_request (void)
435 * Request to disconnect connection by service user
437 void irlap_disconnect_request(struct irlap_cb *self)
439 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
441 ASSERT(self != NULL, return;);
442 ASSERT(self->magic == LAP_MAGIC, return;);
444 /* Don't disconnect until all data frames are successfully sent */
445 if (skb_queue_len(&self->txq) > 0) {
446 self->disconnect_pending = TRUE;
451 /* Check if we are in the right state for disconnecting */
452 switch (self->state) {
453 case LAP_XMIT_P: /* FALLTROUGH */
454 case LAP_XMIT_S: /* FALLTROUGH */
455 case LAP_CONN: /* FALLTROUGH */
456 case LAP_RESET_WAIT: /* FALLTROUGH */
457 case LAP_RESET_CHECK:
458 irlap_do_event(self, DISCONNECT_REQUEST, NULL, NULL);
461 IRDA_DEBUG(2, "%s(), disconnect pending!\n", __FUNCTION__);
462 self->disconnect_pending = TRUE;
468 * Function irlap_disconnect_indication (void)
470 * Disconnect request from other device
473 void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason)
475 IRDA_DEBUG(1, "%s(), reason=%s\n", __FUNCTION__, lap_reasons[reason]);
477 ASSERT(self != NULL, return;);
478 ASSERT(self->magic == LAP_MAGIC, return;);
481 irlap_flush_all_queues(self);
484 case LAP_RESET_INDICATION:
485 IRDA_DEBUG(1, "%s(), Sending reset request!\n", __FUNCTION__);
486 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
488 case LAP_NO_RESPONSE: /* FALLTROUGH */
489 case LAP_DISC_INDICATION: /* FALLTROUGH */
490 case LAP_FOUND_NONE: /* FALLTROUGH */
492 irlmp_link_disconnect_indication(self->notify.instance, self,
496 ERROR("%s: Unknown reason %d\n", __FUNCTION__, 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, "%s(), nslots = %d\n", __FUNCTION__, discovery->nslots);
516 ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
517 (discovery->nslots == 8) || (discovery->nslots == 16),
520 /* Discovery is only possible in NDM mode */
521 if (self->state != LAP_NDM) {
522 IRDA_DEBUG(4, "%s(), discovery only possible in NDM mode\n",
524 irlap_discovery_confirm(self, NULL);
525 /* Note : in theory, if we are not in NDM, we could postpone
526 * the discovery like we do for connection request.
527 * In practice, it's not worth it. If the media was busy,
528 * it's likely next time around it won't be busy. If we are
529 * in REPLY state, we will get passive discovery info & event.
534 /* Check if last discovery request finished in time, or if
535 * it was aborted due to the media busy flag. */
536 if (self->discovery_log != NULL) {
537 hashbin_delete(self->discovery_log, (FREE_FUNC) kfree);
538 self->discovery_log = NULL;
541 /* All operations will occur at predictable time, no need to lock */
542 self->discovery_log = hashbin_new(HB_NOLOCK);
544 if (self->discovery_log == NULL) {
545 WARNING("%s(), Unable to allocate discovery log!\n",
550 info.S = discovery->nslots; /* Number of slots */
551 info.s = 0; /* Current slot */
553 self->discovery_cmd = discovery;
554 info.discovery = discovery;
556 /* sysctl_slot_timeout bounds are checked in irsysctl.c - Jean II */
557 self->slot_timeout = sysctl_slot_timeout * HZ / 1000;
559 irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
563 * Function irlap_discovery_confirm (log)
565 * A device has been discovered in front of this station, we
566 * report directly to LMP.
568 void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log)
570 ASSERT(self != NULL, return;);
571 ASSERT(self->magic == LAP_MAGIC, return;);
573 ASSERT(self->notify.instance != NULL, return;);
576 * Check for successful discovery, since we are then allowed to clear
577 * the media busy condition (IrLAP 6.13.4 - p.94). This should allow
578 * us to make connection attempts much faster and easier (i.e. no
580 * Setting media busy to false will also generate an event allowing
581 * to process pending events in NDM state machine.
582 * Note : the spec doesn't define what's a successful discovery is.
583 * If we want Ultra to work, it's successful even if there is
584 * nobody discovered - Jean II
587 irda_device_set_media_busy(self->netdev, FALSE);
590 irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
594 * Function irlap_discovery_indication (log)
596 * Somebody is trying to discover us!
599 void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery)
601 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
603 ASSERT(self != NULL, return;);
604 ASSERT(self->magic == LAP_MAGIC, return;);
605 ASSERT(discovery != NULL, return;);
607 ASSERT(self->notify.instance != NULL, return;);
609 /* A device is very likely to connect immediately after it performs
610 * a successful discovery. This means that in our case, we are much
611 * more likely to receive a connection request over the medium.
612 * So, we backoff to avoid collisions.
613 * IrLAP spec 6.13.4 suggest 100ms...
614 * Note : this little trick actually make a *BIG* difference. If I set
615 * my Linux box with discovery enabled and one Ultra frame sent every
616 * second, my Palm has no trouble connecting to it every time !
618 irda_device_set_media_busy(self->netdev, SMALL);
620 irlmp_link_discovery_indication(self->notify.instance, discovery);
624 * Function irlap_status_indication (quality_of_link)
626 void irlap_status_indication(struct irlap_cb *self, int quality_of_link)
628 switch (quality_of_link) {
629 case STATUS_NO_ACTIVITY:
630 MESSAGE("IrLAP, no activity on link!\n");
633 MESSAGE("IrLAP, noisy link!\n");
638 irlmp_status_indication(self->notify.instance,
639 quality_of_link, LOCK_NO_CHANGE);
643 * Function irlap_reset_indication (void)
645 void irlap_reset_indication(struct irlap_cb *self)
647 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
649 ASSERT(self != NULL, return;);
650 ASSERT(self->magic == LAP_MAGIC, return;);
652 if (self->state == LAP_RESET_WAIT)
653 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
655 irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
659 * Function irlap_reset_confirm (void)
661 void irlap_reset_confirm(void)
663 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
667 * Function irlap_generate_rand_time_slot (S, s)
669 * Generate a random time slot between s and S-1 where
670 * S = Number of slots (0 -> S-1)
673 int irlap_generate_rand_time_slot(int S, int s)
678 ASSERT((S - s) > 0, return 0;);
681 rand ^= (rand << 12);
682 rand ^= (rand >> 20);
684 slot = s + rand % (S-s);
686 ASSERT((slot >= s) || (slot < S), return 0;);
692 * Function irlap_update_nr_received (nr)
694 * Remove all acknowledged frames in current window queue. This code is
695 * not intuitive and you should not try to change it. If you think it
696 * contains bugs, please mail a patch to the author instead.
698 void irlap_update_nr_received(struct irlap_cb *self, int nr)
700 struct sk_buff *skb = NULL;
704 * Remove all the ack-ed frames from the window queue.
708 * Optimize for the common case. It is most likely that the receiver
709 * will acknowledge all the frames we have sent! So in that case we
710 * delete all frames stored in window.
712 if (nr == self->vs) {
713 while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
716 /* The last acked frame is the next to send minus one */
719 /* Remove all acknowledged frames in current window */
720 while ((skb_peek(&self->wx_list) != NULL) &&
721 (((self->va+1) % 8) != nr))
723 skb = skb_dequeue(&self->wx_list);
726 self->va = (self->va + 1) % 8;
732 self->window = self->window_size - skb_queue_len(&self->wx_list);
736 * Function irlap_validate_ns_received (ns)
738 * Validate the next to send (ns) field from received frame.
740 int irlap_validate_ns_received(struct irlap_cb *self, int ns)
742 /* ns as expected? */
746 * Stations are allowed to treat invalid NS as unexpected NS
747 * IrLAP, Recv ... with-invalid-Ns. p. 84
749 return NS_UNEXPECTED;
751 /* return NR_INVALID; */
754 * Function irlap_validate_nr_received (nr)
756 * Validate the next to receive (nr) field from received frame.
759 int irlap_validate_nr_received(struct irlap_cb *self, int nr)
761 /* nr as expected? */
762 if (nr == self->vs) {
763 IRDA_DEBUG(4, "%s(), expected!\n", __FUNCTION__);
768 * unexpected nr? (but within current window), first we check if the
769 * ns numbers of the frames in the current window wrap.
771 if (self->va < self->vs) {
772 if ((nr >= self->va) && (nr <= self->vs))
773 return NR_UNEXPECTED;
775 if ((nr >= self->va) || (nr <= self->vs))
776 return NR_UNEXPECTED;
784 * Function irlap_initiate_connection_state ()
786 * Initialize the connection state parameters
789 void irlap_initiate_connection_state(struct irlap_cb *self)
791 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
793 ASSERT(self != NULL, return;);
794 ASSERT(self->magic == LAP_MAGIC, return;);
796 /* Next to send and next to receive */
797 self->vs = self->vr = 0;
799 /* Last frame which got acked (0 - 1) % 8 */
804 self->remote_busy = FALSE;
805 self->retry_count = 0;
809 * Function irlap_wait_min_turn_around (self, qos)
811 * Wait negotiated minimum turn around time, this function actually sets
812 * the number of BOS's that must be sent before the next transmitted
813 * frame in order to delay for the specified amount of time. This is
814 * done to avoid using timers, and the forbidden udelay!
816 void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos)
821 /* Get QoS values. */
822 speed = qos->baud_rate.value;
823 min_turn_time = qos->min_turn_time.value;
825 /* No need to calculate XBOFs for speeds over 115200 bps */
826 if (speed > 115200) {
827 self->mtt_required = min_turn_time;
832 * Send additional BOF's for the next frame for the requested
833 * min turn time, so now we must calculate how many chars (XBOF's) we
834 * must send for the requested time period (min turn time)
836 self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
840 * Function irlap_flush_all_queues (void)
845 void irlap_flush_all_queues(struct irlap_cb *self)
849 ASSERT(self != NULL, return;);
850 ASSERT(self->magic == LAP_MAGIC, return;);
852 /* Free transmission queue */
853 while ((skb = skb_dequeue(&self->txq)) != NULL)
856 while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
859 /* Free sliding window buffered packets */
860 while ((skb = skb_dequeue(&self->wx_list)) != NULL)
865 * Function irlap_setspeed (self, speed)
867 * Change the speed of the IrDA port
870 void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
874 IRDA_DEBUG(0, "%s(), setting speed to %d\n", __FUNCTION__, speed);
876 ASSERT(self != NULL, return;);
877 ASSERT(self->magic == LAP_MAGIC, return;);
881 /* Change speed now, or just piggyback speed on frames */
883 /* Send down empty frame to trigger speed change */
884 skb = dev_alloc_skb(0);
885 irlap_queue_xmit(self, skb);
890 * Function irlap_init_qos_capabilities (self, qos)
892 * Initialize QoS for this IrLAP session, What we do is to compute the
893 * intersection of the QoS capabilities for the user, driver and for
894 * IrLAP itself. Normally, IrLAP will not specify any values, but it can
895 * be used to restrict certain values.
897 void irlap_init_qos_capabilities(struct irlap_cb *self,
898 struct qos_info *qos_user)
900 ASSERT(self != NULL, return;);
901 ASSERT(self->magic == LAP_MAGIC, return;);
902 ASSERT(self->netdev != NULL, return;);
904 /* Start out with the maximum QoS support possible */
905 irda_init_max_qos_capabilies(&self->qos_rx);
907 /* Apply drivers QoS capabilities */
908 irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
911 * Check for user supplied QoS parameters. The service user is only
912 * allowed to supply these values. We check each parameter since the
913 * user may not have set all of them.
916 IRDA_DEBUG(1, "%s(), Found user specified QoS!\n", __FUNCTION__);
918 if (qos_user->baud_rate.bits)
919 self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
921 if (qos_user->max_turn_time.bits)
922 self->qos_rx.max_turn_time.bits &= qos_user->max_turn_time.bits;
923 if (qos_user->data_size.bits)
924 self->qos_rx.data_size.bits &= qos_user->data_size.bits;
926 if (qos_user->link_disc_time.bits)
927 self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
930 /* Use 500ms in IrLAP for now */
931 self->qos_rx.max_turn_time.bits &= 0x01;
934 /*self->qos_rx.data_size.bits &= 0x03;*/
936 irda_qos_bits_to_value(&self->qos_rx);
940 * Function irlap_apply_default_connection_parameters (void, now)
942 * Use the default connection and transmission parameters
944 void irlap_apply_default_connection_parameters(struct irlap_cb *self)
946 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
948 ASSERT(self != NULL, return;);
949 ASSERT(self->magic == LAP_MAGIC, return;);
951 /* xbofs : Default value in NDM */
952 self->next_bofs = 12;
953 self->bofs_count = 12;
955 /* NDM Speed is 9600 */
956 irlap_change_speed(self, 9600, TRUE);
958 /* Set mbusy when going to NDM state */
959 irda_device_set_media_busy(self->netdev, TRUE);
962 * Generate random connection address for this session, which must
963 * be 7 bits wide and different from 0x00 and 0xfe
965 while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
966 get_random_bytes(&self->caddr, sizeof(self->caddr));
970 /* Use default values until connection has been negitiated */
971 self->slot_timeout = sysctl_slot_timeout;
972 self->final_timeout = FINAL_TIMEOUT;
973 self->poll_timeout = POLL_TIMEOUT;
974 self->wd_timeout = WD_TIMEOUT;
976 /* Set some default values */
977 self->qos_tx.baud_rate.value = 9600;
978 self->qos_rx.baud_rate.value = 9600;
979 self->qos_tx.max_turn_time.value = 0;
980 self->qos_rx.max_turn_time.value = 0;
981 self->qos_tx.min_turn_time.value = 0;
982 self->qos_rx.min_turn_time.value = 0;
983 self->qos_tx.data_size.value = 64;
984 self->qos_rx.data_size.value = 64;
985 self->qos_tx.window_size.value = 1;
986 self->qos_rx.window_size.value = 1;
987 self->qos_tx.additional_bofs.value = 12;
988 self->qos_rx.additional_bofs.value = 12;
989 self->qos_tx.link_disc_time.value = 0;
990 self->qos_rx.link_disc_time.value = 0;
992 irlap_flush_all_queues(self);
994 self->disconnect_pending = FALSE;
995 self->connect_pending = FALSE;
999 * Function irlap_apply_connection_parameters (qos, now)
1001 * Initialize IrLAP with the negotiated QoS values
1003 * If 'now' is false, the speed and xbofs will be changed after the next
1005 * If 'now' is true, the speed and xbofs is changed immediately
1007 void irlap_apply_connection_parameters(struct irlap_cb *self, int now)
1009 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1011 ASSERT(self != NULL, return;);
1012 ASSERT(self->magic == LAP_MAGIC, return;);
1014 /* Set the negotiated xbofs value */
1015 self->next_bofs = self->qos_tx.additional_bofs.value;
1017 self->bofs_count = self->next_bofs;
1019 /* Set the negotiated link speed (may need the new xbofs value) */
1020 irlap_change_speed(self, self->qos_tx.baud_rate.value, now);
1022 self->window_size = self->qos_tx.window_size.value;
1023 self->window = self->qos_tx.window_size.value;
1025 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1027 * Calculate how many bytes it is possible to transmit before the
1028 * link must be turned around
1030 self->line_capacity =
1031 irlap_max_line_capacity(self->qos_tx.baud_rate.value,
1032 self->qos_tx.max_turn_time.value);
1033 self->bytes_left = self->line_capacity;
1034 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1038 * Initialize timeout values, some of the rules are listed on
1041 ASSERT(self->qos_tx.max_turn_time.value != 0, return;);
1042 ASSERT(self->qos_rx.max_turn_time.value != 0, return;);
1043 /* The poll timeout applies only to the primary station.
1044 * It defines the maximum time the primary stay in XMIT mode
1045 * before timeout and turning the link around (sending a RR).
1046 * Or, this is how much we can keep the pf bit in primary mode.
1047 * Therefore, it must be lower or equal than our *OWN* max turn around.
1049 self->poll_timeout = self->qos_tx.max_turn_time.value * HZ / 1000;
1050 /* The Final timeout applies only to the primary station.
1051 * It defines the maximum time the primary wait (mostly in RECV mode)
1052 * for an answer from the secondary station before polling it again.
1053 * Therefore, it must be greater or equal than our *PARTNER*
1054 * max turn around time - Jean II */
1055 self->final_timeout = self->qos_rx.max_turn_time.value * HZ / 1000;
1056 /* The Watchdog Bit timeout applies only to the secondary station.
1057 * It defines the maximum time the secondary wait (mostly in RECV mode)
1058 * for poll from the primary station before getting annoyed.
1059 * Therefore, it must be greater or equal than our *PARTNER*
1060 * max turn around time - Jean II */
1061 self->wd_timeout = self->final_timeout * 2;
1064 * N1 and N2 are maximum retry count for *both* the final timer
1065 * and the wd timer (with a factor 2) as defined above.
1066 * After N1 retry of a timer, we give a warning to the user.
1067 * After N2 retry, we consider the link dead and disconnect it.
1072 * Set N1 to 0 if Link Disconnect/Threshold Time = 3 and set it to
1073 * 3 seconds otherwise. See page 71 in IrLAP for more details.
1074 * Actually, it's not always 3 seconds, as we allow to set
1075 * it via sysctl... Max maxtt is 500ms, and N1 need to be multiple
1076 * of 2, so 1 second is minimum we can allow. - Jean II
1078 if (self->qos_tx.link_disc_time.value == sysctl_warn_noreply_time)
1080 * If we set N1 to 0, it will trigger immediately, which is
1081 * not what we want. What we really want is to disable it,
1084 self->N1 = -2; /* Disable - Need to be multiple of 2*/
1086 self->N1 = sysctl_warn_noreply_time * 1000 /
1087 self->qos_rx.max_turn_time.value;
1089 IRDA_DEBUG(4, "Setting N1 = %d\n", self->N1);
1091 /* Set N2 to match our own disconnect time */
1092 self->N2 = self->qos_tx.link_disc_time.value * 1000 /
1093 self->qos_rx.max_turn_time.value;
1094 IRDA_DEBUG(4, "Setting N2 = %d\n", self->N2);
1097 #ifdef CONFIG_PROC_FS
1098 struct irlap_iter_state {
1102 static void *irlap_seq_start(struct seq_file *seq, loff_t *pos)
1104 struct irlap_iter_state *iter = seq->private;
1105 struct irlap_cb *self;
1107 /* Protect our access to the tsap list */
1108 spin_lock_irq(&irlap->hb_spinlock);
1111 for (self = (struct irlap_cb *) hashbin_get_first(irlap);
1112 self; self = (struct irlap_cb *) hashbin_get_next(irlap)) {
1113 if (iter->id == *pos)
1121 static void *irlap_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1123 struct irlap_iter_state *iter = seq->private;
1127 return (void *) hashbin_get_next(irlap);
1130 static void irlap_seq_stop(struct seq_file *seq, void *v)
1132 spin_unlock_irq(&irlap->hb_spinlock);
1135 static int irlap_seq_show(struct seq_file *seq, void *v)
1137 const struct irlap_iter_state *iter = seq->private;
1138 const struct irlap_cb *self = v;
1140 ASSERT(self->magic == LAP_MAGIC, return -EINVAL;);
1142 seq_printf(seq, "irlap%d ", iter->id);
1143 seq_printf(seq, "state: %s\n",
1144 irlap_state[self->state]);
1146 seq_printf(seq, " device name: %s, ",
1147 (self->netdev) ? self->netdev->name : "bug");
1148 seq_printf(seq, "hardware name: %s\n", self->hw_name);
1150 seq_printf(seq, " caddr: %#02x, ", self->caddr);
1151 seq_printf(seq, "saddr: %#08x, ", self->saddr);
1152 seq_printf(seq, "daddr: %#08x\n", self->daddr);
1154 seq_printf(seq, " win size: %d, ",
1156 seq_printf(seq, "win: %d, ", self->window);
1157 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
1158 seq_printf(seq, "line capacity: %d, ",
1159 self->line_capacity);
1160 seq_printf(seq, "bytes left: %d\n", self->bytes_left);
1161 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1162 seq_printf(seq, " tx queue len: %d ",
1163 skb_queue_len(&self->txq));
1164 seq_printf(seq, "win queue len: %d ",
1165 skb_queue_len(&self->wx_list));
1166 seq_printf(seq, "rbusy: %s", self->remote_busy ?
1168 seq_printf(seq, " mbusy: %s\n", self->media_busy ?
1171 seq_printf(seq, " retrans: %d ", self->retry_count);
1172 seq_printf(seq, "vs: %d ", self->vs);
1173 seq_printf(seq, "vr: %d ", self->vr);
1174 seq_printf(seq, "va: %d\n", self->va);
1176 seq_printf(seq, " qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
1178 seq_printf(seq, " tx\t%d\t",
1179 self->qos_tx.baud_rate.value);
1180 seq_printf(seq, "%d\t",
1181 self->qos_tx.max_turn_time.value);
1182 seq_printf(seq, "%d\t",
1183 self->qos_tx.data_size.value);
1184 seq_printf(seq, "%d\t",
1185 self->qos_tx.window_size.value);
1186 seq_printf(seq, "%d\t",
1187 self->qos_tx.additional_bofs.value);
1188 seq_printf(seq, "%d\t",
1189 self->qos_tx.min_turn_time.value);
1190 seq_printf(seq, "%d\t",
1191 self->qos_tx.link_disc_time.value);
1192 seq_printf(seq, "\n");
1194 seq_printf(seq, " rx\t%d\t",
1195 self->qos_rx.baud_rate.value);
1196 seq_printf(seq, "%d\t",
1197 self->qos_rx.max_turn_time.value);
1198 seq_printf(seq, "%d\t",
1199 self->qos_rx.data_size.value);
1200 seq_printf(seq, "%d\t",
1201 self->qos_rx.window_size.value);
1202 seq_printf(seq, "%d\t",
1203 self->qos_rx.additional_bofs.value);
1204 seq_printf(seq, "%d\t",
1205 self->qos_rx.min_turn_time.value);
1206 seq_printf(seq, "%d\n",
1207 self->qos_rx.link_disc_time.value);
1212 static struct seq_operations irlap_seq_ops = {
1213 .start = irlap_seq_start,
1214 .next = irlap_seq_next,
1215 .stop = irlap_seq_stop,
1216 .show = irlap_seq_show,
1219 static int irlap_seq_open(struct inode *inode, struct file *file)
1221 struct seq_file *seq;
1223 struct irlap_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
1228 if (irlap == NULL) {
1233 rc = seq_open(file, &irlap_seq_ops);
1237 seq = file->private_data;
1239 memset(s, 0, sizeof(*s));
1247 struct file_operations irlap_seq_fops = {
1248 .owner = THIS_MODULE,
1249 .open = irlap_seq_open,
1251 .llseek = seq_lseek,
1252 .release = seq_release_private,
1255 #endif /* CONFIG_PROC_FS */