1 /*********************************************************************
5 * Description: IrLAP QoS parameter negotiation
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Sep 9 00:00:26 1997
9 * Modified at: Sun Jan 30 14:29:16 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
14 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 ********************************************************************/
33 #include <linux/config.h>
34 #include <asm/byteorder.h>
36 #include <net/irda/irda.h>
37 #include <net/irda/parameters.h>
38 #include <net/irda/qos.h>
39 #include <net/irda/irlap.h>
42 * Maximum values of the baud rate we negociate with the other end.
43 * Most often, you don't have to change that, because Linux-IrDA will
44 * use the maximum offered by the link layer, which usually works fine.
45 * In some very rare cases, you may want to limit it to lower speeds...
47 int sysctl_max_baud_rate = 16000000;
49 * Maximum value of the lap disconnect timer we negociate with the other end.
50 * Most often, the value below represent the best compromise, but some user
51 * may want to keep the LAP alive longuer or shorter in case of link failure.
52 * Remember that the threshold time (early warning) is fixed to 3s...
54 int sysctl_max_noreply_time = 12;
56 * Minimum turn time to be applied before transmitting to the peer.
57 * Nonzero values (usec) are used as lower limit to the per-connection
58 * mtt value which was announced by the other end during negotiation.
59 * Might be helpful if the peer device provides too short mtt.
60 * Default is 10us which means using the unmodified value given by the
61 * peer except if it's 0 (0 is likely a bug in the other stack).
63 unsigned sysctl_min_tx_turn_time = 10;
65 * Maximum data size to be used in transmission in payload of LAP frame.
66 * There is a bit of confusion in the IrDA spec :
67 * The LAP spec defines the payload of a LAP frame (I field) to be
68 * 2048 bytes max (IrLAP 1.1, chapt 6.6.5, p40).
69 * On the other hand, the PHY mention frames of 2048 bytes max (IrPHY
70 * 1.2, chapt 5.3.2.1, p41). But, this number includes the LAP header
71 * (2 bytes), and CRC (32 bits at 4 Mb/s). So, for the I field (LAP
72 * payload), that's only 2042 bytes. Oups !
73 * My nsc-ircc hardware has troubles receiving 2048 bytes frames at 4 Mb/s,
74 * so adjust to 2042... I don't know if this bug applies only for 2048
75 * bytes frames or all negotiated frame sizes, but you can use the sysctl
76 * to play with this value anyway.
78 unsigned sysctl_max_tx_data_size = 2042;
80 * Maximum transmit window, i.e. number of LAP frames between turn-around.
81 * This allow to override what the peer told us. Some peers are buggy and
82 * don't always support what they tell us.
84 unsigned sysctl_max_tx_window = 7;
86 static int irlap_param_baud_rate(void *instance, irda_param_t *param, int get);
87 static int irlap_param_link_disconnect(void *instance, irda_param_t *parm,
89 static int irlap_param_max_turn_time(void *instance, irda_param_t *param,
91 static int irlap_param_data_size(void *instance, irda_param_t *param, int get);
92 static int irlap_param_window_size(void *instance, irda_param_t *param,
94 static int irlap_param_additional_bofs(void *instance, irda_param_t *parm,
96 static int irlap_param_min_turn_time(void *instance, irda_param_t *param,
99 static __u32 min_turn_times[] = { 10000, 5000, 1000, 500, 100, 50, 10, 0 }; /* us */
100 static __u32 baud_rates[] = { 2400, 9600, 19200, 38400, 57600, 115200, 576000,
101 1152000, 4000000, 16000000 }; /* bps */
102 static __u32 data_sizes[] = { 64, 128, 256, 512, 1024, 2048 }; /* bytes */
103 static __u32 add_bofs[] = { 48, 24, 12, 5, 3, 2, 1, 0 }; /* bytes */
104 static __u32 max_turn_times[] = { 500, 250, 100, 50 }; /* ms */
105 static __u32 link_disc_times[] = { 3, 8, 12, 16, 20, 25, 30, 40 }; /* secs */
107 static __u32 max_line_capacities[10][4] = {
108 /* 500 ms 250 ms 100 ms 50 ms (max turn time) */
109 { 100, 0, 0, 0 }, /* 2400 bps */
110 { 400, 0, 0, 0 }, /* 9600 bps */
111 { 800, 0, 0, 0 }, /* 19200 bps */
112 { 1600, 0, 0, 0 }, /* 38400 bps */
113 { 2360, 0, 0, 0 }, /* 57600 bps */
114 { 4800, 2400, 960, 480 }, /* 115200 bps */
115 { 28800, 11520, 5760, 2880 }, /* 576000 bps */
116 { 57600, 28800, 11520, 5760 }, /* 1152000 bps */
117 { 200000, 100000, 40000, 20000 }, /* 4000000 bps */
118 { 800000, 400000, 160000, 80000 }, /* 16000000 bps */
121 static pi_minor_info_t pi_minor_call_table_type_0[] = {
123 /* 01 */{ irlap_param_baud_rate, PV_INTEGER | PV_LITTLE_ENDIAN },
130 /* 08 */{ irlap_param_link_disconnect, PV_INT_8_BITS }
133 static pi_minor_info_t pi_minor_call_table_type_1[] = {
136 /* 82 */{ irlap_param_max_turn_time, PV_INT_8_BITS },
137 /* 83 */{ irlap_param_data_size, PV_INT_8_BITS },
138 /* 84 */{ irlap_param_window_size, PV_INT_8_BITS },
139 /* 85 */{ irlap_param_additional_bofs, PV_INT_8_BITS },
140 /* 86 */{ irlap_param_min_turn_time, PV_INT_8_BITS },
143 static pi_major_info_t pi_major_call_table[] = {
144 { pi_minor_call_table_type_0, 9 },
145 { pi_minor_call_table_type_1, 7 },
148 static pi_param_info_t irlap_param_info = { pi_major_call_table, 2, 0x7f, 7 };
150 /* ---------------------- LOCAL SUBROUTINES ---------------------- */
151 /* Note : we start with a bunch of local subroutines.
152 * As the compiler is "one pass", this is the only way to get them to
157 * Function value_index (value, array, size)
159 * Returns the index to the value in the specified array
161 static inline int value_index(__u32 value, __u32 *array, int size)
165 for (i=0; i < size; i++)
166 if (array[i] == value)
172 * Function index_value (index, array)
174 * Returns value to index in array, easy!
177 static inline __u32 index_value(int index, __u32 *array)
183 * Function msb_index (word)
185 * Returns index to most significant bit (MSB) in word
188 static int msb_index (__u16 word)
191 int index = 15; /* Current MSB */
193 /* Check for buggy peers.
194 * Note : there is a small probability that it could be us, but I
195 * would expect driver authors to catch that pretty early and be
196 * able to check precisely what's going on. If a end user sees this,
197 * it's very likely the peer. - Jean II */
199 WARNING("%s(), Detected buggy peer, adjust null PV to 0x1!\n",
201 /* The only safe choice (we don't know the array size) */
207 break; /* Found it! */
214 static inline __u32 byte_value(__u8 byte, __u32 *array)
218 ASSERT(array != NULL, return -1;);
220 index = msb_index(byte);
222 return index_value(index, array);
226 * Function value_lower_bits (value, array)
228 * Returns a bit field marking all possibility lower than value.
230 static inline int value_lower_bits(__u32 value, __u32 *array, int size, __u16 *field)
236 for (i=0; i < size; i++) {
237 /* Add the current value to the bit field, shift mask */
241 if (array[i] >= value)
244 /* Send back a valid index */
246 i = size - 1; /* Last item */
252 * Function value_highest_bit (value, array)
254 * Returns a bit field marking the highest possibility lower than value.
256 static inline int value_highest_bit(__u32 value, __u32 *array, int size, __u16 *field)
262 for (i=0; i < size; i++) {
264 if (array[i] <= value)
269 /* Set the current value to the bit field */
271 /* Send back a valid index */
273 i = size - 1; /* Last item */
278 /* -------------------------- MAIN CALLS -------------------------- */
281 * Function irda_qos_compute_intersection (qos, new)
283 * Compute the intersection of the old QoS capabilities with new ones
286 void irda_qos_compute_intersection(struct qos_info *qos, struct qos_info *new)
288 ASSERT(qos != NULL, return;);
289 ASSERT(new != NULL, return;);
292 qos->baud_rate.bits &= new->baud_rate.bits;
293 qos->window_size.bits &= new->window_size.bits;
294 qos->min_turn_time.bits &= new->min_turn_time.bits;
295 qos->max_turn_time.bits &= new->max_turn_time.bits;
296 qos->data_size.bits &= new->data_size.bits;
297 qos->link_disc_time.bits &= new->link_disc_time.bits;
298 qos->additional_bofs.bits &= new->additional_bofs.bits;
300 irda_qos_bits_to_value(qos);
304 * Function irda_init_max_qos_capabilies (qos)
306 * The purpose of this function is for layers and drivers to be able to
307 * set the maximum QoS possible and then "and in" their own limitations
310 void irda_init_max_qos_capabilies(struct qos_info *qos)
314 * These are the maximum supported values as specified on pages
318 /* Use sysctl to set some configurable values... */
319 /* Set configured max speed */
320 i = value_lower_bits(sysctl_max_baud_rate, baud_rates, 10,
321 &qos->baud_rate.bits);
322 sysctl_max_baud_rate = index_value(i, baud_rates);
324 /* Set configured max disc time */
325 i = value_lower_bits(sysctl_max_noreply_time, link_disc_times, 8,
326 &qos->link_disc_time.bits);
327 sysctl_max_noreply_time = index_value(i, link_disc_times);
329 /* LSB is first byte, MSB is second byte */
330 qos->baud_rate.bits &= 0x03ff;
332 qos->window_size.bits = 0x7f;
333 qos->min_turn_time.bits = 0xff;
334 qos->max_turn_time.bits = 0x0f;
335 qos->data_size.bits = 0x3f;
336 qos->link_disc_time.bits &= 0xff;
337 qos->additional_bofs.bits = 0xff;
339 EXPORT_SYMBOL(irda_init_max_qos_capabilies);
342 * Function irlap_adjust_qos_settings (qos)
344 * Adjust QoS settings in case some values are not possible to use because
347 void irlap_adjust_qos_settings(struct qos_info *qos)
352 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
355 * Make sure the mintt is sensible.
356 * Main culprit : Ericsson T39. - Jean II
358 if (sysctl_min_tx_turn_time > qos->min_turn_time.value) {
361 WARNING("%s(), Detected buggy peer, adjust mtt to %dus!\n",
362 __FUNCTION__, sysctl_min_tx_turn_time);
364 /* We don't really need bits, but easier this way */
365 i = value_highest_bit(sysctl_min_tx_turn_time, min_turn_times,
366 8, &qos->min_turn_time.bits);
367 sysctl_min_tx_turn_time = index_value(i, min_turn_times);
368 qos->min_turn_time.value = sysctl_min_tx_turn_time;
372 * Not allowed to use a max turn time less than 500 ms if the baudrate
373 * is less than 115200
375 if ((qos->baud_rate.value < 115200) &&
376 (qos->max_turn_time.value < 500))
379 "%s(), adjusting max turn time from %d to 500 ms\n",
380 __FUNCTION__, qos->max_turn_time.value);
381 qos->max_turn_time.value = 500;
385 * The data size must be adjusted according to the baud rate and max
388 index = value_index(qos->data_size.value, data_sizes, 6);
389 line_capacity = irlap_max_line_capacity(qos->baud_rate.value,
390 qos->max_turn_time.value);
392 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
393 while ((qos->data_size.value > line_capacity) && (index > 0)) {
394 qos->data_size.value = data_sizes[index--];
395 IRDA_DEBUG(2, "%s(), reducing data size to %d\n",
396 __FUNCTION__, qos->data_size.value);
398 #else /* Use method described in section 6.6.11 of IrLAP */
399 while (irlap_requested_line_capacity(qos) > line_capacity) {
400 ASSERT(index != 0, return;);
402 /* Must be able to send at least one frame */
403 if (qos->window_size.value > 1) {
404 qos->window_size.value--;
405 IRDA_DEBUG(2, "%s(), reducing window size to %d\n",
406 __FUNCTION__, qos->window_size.value);
407 } else if (index > 1) {
408 qos->data_size.value = data_sizes[index--];
409 IRDA_DEBUG(2, "%s(), reducing data size to %d\n",
410 __FUNCTION__, qos->data_size.value);
412 WARNING("%s(), nothing more we can do!\n",
416 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
418 * Fix tx data size according to user limits - Jean II
420 if (qos->data_size.value > sysctl_max_tx_data_size)
421 /* Allow non discrete adjustement to avoid loosing capacity */
422 qos->data_size.value = sysctl_max_tx_data_size;
424 * Override Tx window if user request it. - Jean II
426 if (qos->window_size.value > sysctl_max_tx_window)
427 qos->window_size.value = sysctl_max_tx_window;
431 * Function irlap_negotiate (qos_device, qos_session, skb)
433 * Negotiate QoS values, not really that much negotiation :-)
434 * We just set the QoS capabilities for the peer station
437 int irlap_qos_negotiate(struct irlap_cb *self, struct sk_buff *skb)
441 ret = irda_param_extract_all(self, skb->data, skb->len,
444 /* Convert the negotiated bits to values */
445 irda_qos_bits_to_value(&self->qos_tx);
446 irda_qos_bits_to_value(&self->qos_rx);
448 irlap_adjust_qos_settings(&self->qos_tx);
450 IRDA_DEBUG(2, "Setting BAUD_RATE to %d bps.\n",
451 self->qos_tx.baud_rate.value);
452 IRDA_DEBUG(2, "Setting DATA_SIZE to %d bytes\n",
453 self->qos_tx.data_size.value);
454 IRDA_DEBUG(2, "Setting WINDOW_SIZE to %d\n",
455 self->qos_tx.window_size.value);
456 IRDA_DEBUG(2, "Setting XBOFS to %d\n",
457 self->qos_tx.additional_bofs.value);
458 IRDA_DEBUG(2, "Setting MAX_TURN_TIME to %d ms.\n",
459 self->qos_tx.max_turn_time.value);
460 IRDA_DEBUG(2, "Setting MIN_TURN_TIME to %d usecs.\n",
461 self->qos_tx.min_turn_time.value);
462 IRDA_DEBUG(2, "Setting LINK_DISC to %d secs.\n",
463 self->qos_tx.link_disc_time.value);
468 * Function irlap_insert_negotiation_params (qos, fp)
470 * Insert QoS negotiaion pararameters into frame
473 int irlap_insert_qos_negotiation_params(struct irlap_cb *self,
478 /* Insert data rate */
479 ret = irda_param_insert(self, PI_BAUD_RATE, skb->tail,
480 skb_tailroom(skb), &irlap_param_info);
485 /* Insert max turnaround time */
486 ret = irda_param_insert(self, PI_MAX_TURN_TIME, skb->tail,
487 skb_tailroom(skb), &irlap_param_info);
492 /* Insert data size */
493 ret = irda_param_insert(self, PI_DATA_SIZE, skb->tail,
494 skb_tailroom(skb), &irlap_param_info);
499 /* Insert window size */
500 ret = irda_param_insert(self, PI_WINDOW_SIZE, skb->tail,
501 skb_tailroom(skb), &irlap_param_info);
506 /* Insert additional BOFs */
507 ret = irda_param_insert(self, PI_ADD_BOFS, skb->tail,
508 skb_tailroom(skb), &irlap_param_info);
513 /* Insert minimum turnaround time */
514 ret = irda_param_insert(self, PI_MIN_TURN_TIME, skb->tail,
515 skb_tailroom(skb), &irlap_param_info);
520 /* Insert link disconnect/threshold time */
521 ret = irda_param_insert(self, PI_LINK_DISC, skb->tail,
522 skb_tailroom(skb), &irlap_param_info);
531 * Function irlap_param_baud_rate (instance, param, get)
533 * Negotiate data-rate
536 static int irlap_param_baud_rate(void *instance, irda_param_t *param, int get)
540 struct irlap_cb *self = (struct irlap_cb *) instance;
542 ASSERT(self != NULL, return -1;);
543 ASSERT(self->magic == LAP_MAGIC, return -1;);
546 param->pv.i = self->qos_rx.baud_rate.bits;
547 IRDA_DEBUG(2, "%s(), baud rate = 0x%02x\n",
548 __FUNCTION__, param->pv.i);
551 * Stations must agree on baud rate, so calculate
554 IRDA_DEBUG(2, "Requested BAUD_RATE: 0x%04x\n", (__u16) param->pv.i);
555 final = (__u16) param->pv.i & self->qos_rx.baud_rate.bits;
557 IRDA_DEBUG(2, "Final BAUD_RATE: 0x%04x\n", final);
558 self->qos_tx.baud_rate.bits = final;
559 self->qos_rx.baud_rate.bits = final;
566 * Function irlap_param_link_disconnect (instance, param, get)
568 * Negotiate link disconnect/threshold time.
571 static int irlap_param_link_disconnect(void *instance, irda_param_t *param,
576 struct irlap_cb *self = (struct irlap_cb *) instance;
578 ASSERT(self != NULL, return -1;);
579 ASSERT(self->magic == LAP_MAGIC, return -1;);
582 param->pv.i = self->qos_rx.link_disc_time.bits;
585 * Stations must agree on link disconnect/threshold
588 IRDA_DEBUG(2, "LINK_DISC: %02x\n", (__u8) param->pv.i);
589 final = (__u8) param->pv.i & self->qos_rx.link_disc_time.bits;
591 IRDA_DEBUG(2, "Final LINK_DISC: %02x\n", final);
592 self->qos_tx.link_disc_time.bits = final;
593 self->qos_rx.link_disc_time.bits = final;
599 * Function irlap_param_max_turn_time (instance, param, get)
601 * Negotiate the maximum turnaround time. This is a type 1 parameter and
602 * will be negotiated independently for each station
605 static int irlap_param_max_turn_time(void *instance, irda_param_t *param,
608 struct irlap_cb *self = (struct irlap_cb *) instance;
610 ASSERT(self != NULL, return -1;);
611 ASSERT(self->magic == LAP_MAGIC, return -1;);
614 param->pv.i = self->qos_rx.max_turn_time.bits;
616 self->qos_tx.max_turn_time.bits = (__u8) param->pv.i;
622 * Function irlap_param_data_size (instance, param, get)
624 * Negotiate the data size. This is a type 1 parameter and
625 * will be negotiated independently for each station
628 static int irlap_param_data_size(void *instance, irda_param_t *param, int get)
630 struct irlap_cb *self = (struct irlap_cb *) instance;
632 ASSERT(self != NULL, return -1;);
633 ASSERT(self->magic == LAP_MAGIC, return -1;);
636 param->pv.i = self->qos_rx.data_size.bits;
638 self->qos_tx.data_size.bits = (__u8) param->pv.i;
644 * Function irlap_param_window_size (instance, param, get)
646 * Negotiate the window size. This is a type 1 parameter and
647 * will be negotiated independently for each station
650 static int irlap_param_window_size(void *instance, irda_param_t *param,
653 struct irlap_cb *self = (struct irlap_cb *) instance;
655 ASSERT(self != NULL, return -1;);
656 ASSERT(self->magic == LAP_MAGIC, return -1;);
659 param->pv.i = self->qos_rx.window_size.bits;
661 self->qos_tx.window_size.bits = (__u8) param->pv.i;
667 * Function irlap_param_additional_bofs (instance, param, get)
669 * Negotiate additional BOF characters. This is a type 1 parameter and
670 * will be negotiated independently for each station.
672 static int irlap_param_additional_bofs(void *instance, irda_param_t *param, int get)
674 struct irlap_cb *self = (struct irlap_cb *) instance;
676 ASSERT(self != NULL, return -1;);
677 ASSERT(self->magic == LAP_MAGIC, return -1;);
680 param->pv.i = self->qos_rx.additional_bofs.bits;
682 self->qos_tx.additional_bofs.bits = (__u8) param->pv.i;
688 * Function irlap_param_min_turn_time (instance, param, get)
690 * Negotiate the minimum turn around time. This is a type 1 parameter and
691 * will be negotiated independently for each station
693 static int irlap_param_min_turn_time(void *instance, irda_param_t *param,
696 struct irlap_cb *self = (struct irlap_cb *) instance;
698 ASSERT(self != NULL, return -1;);
699 ASSERT(self->magic == LAP_MAGIC, return -1;);
702 param->pv.i = self->qos_rx.min_turn_time.bits;
704 self->qos_tx.min_turn_time.bits = (__u8) param->pv.i;
710 * Function irlap_max_line_capacity (speed, max_turn_time, min_turn_time)
712 * Calculate the maximum line capacity
715 __u32 irlap_max_line_capacity(__u32 speed, __u32 max_turn_time)
720 IRDA_DEBUG(2, "%s(), speed=%d, max_turn_time=%d\n",
721 __FUNCTION__, speed, max_turn_time);
723 i = value_index(speed, baud_rates, 10);
724 j = value_index(max_turn_time, max_turn_times, 4);
726 ASSERT(((i >=0) && (i <10)), return 0;);
727 ASSERT(((j >=0) && (j <4)), return 0;);
729 line_capacity = max_line_capacities[i][j];
731 IRDA_DEBUG(2, "%s(), line capacity=%d bytes\n",
732 __FUNCTION__, line_capacity);
734 return line_capacity;
737 __u32 irlap_requested_line_capacity(struct qos_info *qos)
738 { __u32 line_capacity;
740 line_capacity = qos->window_size.value *
741 (qos->data_size.value + 6 + qos->additional_bofs.value) +
742 irlap_min_turn_time_in_bytes(qos->baud_rate.value,
743 qos->min_turn_time.value);
745 IRDA_DEBUG(2, "%s(), requested line capacity=%d\n",
746 __FUNCTION__, line_capacity);
748 return line_capacity;
751 void irda_qos_bits_to_value(struct qos_info *qos)
755 ASSERT(qos != NULL, return;);
757 index = msb_index(qos->baud_rate.bits);
758 qos->baud_rate.value = baud_rates[index];
760 index = msb_index(qos->data_size.bits);
761 qos->data_size.value = data_sizes[index];
763 index = msb_index(qos->window_size.bits);
764 qos->window_size.value = index+1;
766 index = msb_index(qos->min_turn_time.bits);
767 qos->min_turn_time.value = min_turn_times[index];
769 index = msb_index(qos->max_turn_time.bits);
770 qos->max_turn_time.value = max_turn_times[index];
772 index = msb_index(qos->link_disc_time.bits);
773 qos->link_disc_time.value = link_disc_times[index];
775 index = msb_index(qos->additional_bofs.bits);
776 qos->additional_bofs.value = add_bofs[index];
778 EXPORT_SYMBOL(irda_qos_bits_to_value);