blob: 798ffd9a705ee014671248e71a213b557031ecac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Filename: qos.c
4 * Version: 1.0
5 * Description: IrLAP QoS parameter negotiation
6 * Status: Stable
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>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090011 *
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * All Rights Reserved.
14 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090015 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090020 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * 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.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090025 *
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * MA 02111-1307 USA
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 ********************************************************************/
32
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040033#include <linux/export.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/byteorder.h>
36
37#include <net/irda/irda.h>
38#include <net/irda/parameters.h>
39#include <net/irda/qos.h>
40#include <net/irda/irlap.h>
Adrian Bunk506e7be2005-09-05 18:08:11 -070041#include <net/irda/irlap_frame.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030044 * Maximum values of the baud rate we negotiate with the other end.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Most often, you don't have to change that, because Linux-IrDA will
46 * use the maximum offered by the link layer, which usually works fine.
47 * In some very rare cases, you may want to limit it to lower speeds...
48 */
49int sysctl_max_baud_rate = 16000000;
50/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030051 * Maximum value of the lap disconnect timer we negotiate with the other end.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Most often, the value below represent the best compromise, but some user
Lucas De Marchi25985ed2011-03-30 22:57:33 -030053 * may want to keep the LAP alive longer or shorter in case of link failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Remember that the threshold time (early warning) is fixed to 3s...
55 */
56int sysctl_max_noreply_time = 12;
57/*
58 * Minimum turn time to be applied before transmitting to the peer.
59 * Nonzero values (usec) are used as lower limit to the per-connection
60 * mtt value which was announced by the other end during negotiation.
61 * Might be helpful if the peer device provides too short mtt.
62 * Default is 10us which means using the unmodified value given by the
63 * peer except if it's 0 (0 is likely a bug in the other stack).
64 */
Andi Kleen95660422011-09-16 09:09:50 +000065unsigned int sysctl_min_tx_turn_time = 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Maximum data size to be used in transmission in payload of LAP frame.
68 * There is a bit of confusion in the IrDA spec :
69 * The LAP spec defines the payload of a LAP frame (I field) to be
70 * 2048 bytes max (IrLAP 1.1, chapt 6.6.5, p40).
71 * On the other hand, the PHY mention frames of 2048 bytes max (IrPHY
72 * 1.2, chapt 5.3.2.1, p41). But, this number includes the LAP header
73 * (2 bytes), and CRC (32 bits at 4 Mb/s). So, for the I field (LAP
74 * payload), that's only 2042 bytes. Oups !
75 * My nsc-ircc hardware has troubles receiving 2048 bytes frames at 4 Mb/s,
76 * so adjust to 2042... I don't know if this bug applies only for 2048
77 * bytes frames or all negotiated frame sizes, but you can use the sysctl
78 * to play with this value anyway.
79 * Jean II */
Andi Kleen95660422011-09-16 09:09:50 +000080unsigned int sysctl_max_tx_data_size = 2042;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Maximum transmit window, i.e. number of LAP frames between turn-around.
83 * This allow to override what the peer told us. Some peers are buggy and
84 * don't always support what they tell us.
85 * Jean II */
Andi Kleen95660422011-09-16 09:09:50 +000086unsigned int sysctl_max_tx_window = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static int irlap_param_baud_rate(void *instance, irda_param_t *param, int get);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090089static int irlap_param_link_disconnect(void *instance, irda_param_t *parm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int get);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090091static int irlap_param_max_turn_time(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int get);
93static int irlap_param_data_size(void *instance, irda_param_t *param, int get);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090094static int irlap_param_window_size(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int get);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090096static int irlap_param_additional_bofs(void *instance, irda_param_t *parm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int get);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090098static int irlap_param_min_turn_time(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int get);
100
101#ifndef CONFIG_IRDA_DYNAMIC_WINDOW
102static __u32 irlap_requested_line_capacity(struct qos_info *qos);
103#endif
104
105static __u32 min_turn_times[] = { 10000, 5000, 1000, 500, 100, 50, 10, 0 }; /* us */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900106static __u32 baud_rates[] = { 2400, 9600, 19200, 38400, 57600, 115200, 576000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 1152000, 4000000, 16000000 }; /* bps */
108static __u32 data_sizes[] = { 64, 128, 256, 512, 1024, 2048 }; /* bytes */
109static __u32 add_bofs[] = { 48, 24, 12, 5, 3, 2, 1, 0 }; /* bytes */
110static __u32 max_turn_times[] = { 500, 250, 100, 50 }; /* ms */
111static __u32 link_disc_times[] = { 3, 8, 12, 16, 20, 25, 30, 40 }; /* secs */
112
113static __u32 max_line_capacities[10][4] = {
114 /* 500 ms 250 ms 100 ms 50 ms (max turn time) */
115 { 100, 0, 0, 0 }, /* 2400 bps */
116 { 400, 0, 0, 0 }, /* 9600 bps */
117 { 800, 0, 0, 0 }, /* 19200 bps */
118 { 1600, 0, 0, 0 }, /* 38400 bps */
119 { 2360, 0, 0, 0 }, /* 57600 bps */
120 { 4800, 2400, 960, 480 }, /* 115200 bps */
121 { 28800, 11520, 5760, 2880 }, /* 576000 bps */
122 { 57600, 28800, 11520, 5760 }, /* 1152000 bps */
123 { 200000, 100000, 40000, 20000 }, /* 4000000 bps */
124 { 800000, 400000, 160000, 80000 }, /* 16000000 bps */
125};
126
127static pi_minor_info_t pi_minor_call_table_type_0[] = {
128 { NULL, 0 },
129/* 01 */{ irlap_param_baud_rate, PV_INTEGER | PV_LITTLE_ENDIAN },
130 { NULL, 0 },
131 { NULL, 0 },
132 { NULL, 0 },
133 { NULL, 0 },
134 { NULL, 0 },
135 { NULL, 0 },
136/* 08 */{ irlap_param_link_disconnect, PV_INT_8_BITS }
137};
138
139static pi_minor_info_t pi_minor_call_table_type_1[] = {
140 { NULL, 0 },
141 { NULL, 0 },
142/* 82 */{ irlap_param_max_turn_time, PV_INT_8_BITS },
143/* 83 */{ irlap_param_data_size, PV_INT_8_BITS },
144/* 84 */{ irlap_param_window_size, PV_INT_8_BITS },
145/* 85 */{ irlap_param_additional_bofs, PV_INT_8_BITS },
146/* 86 */{ irlap_param_min_turn_time, PV_INT_8_BITS },
147};
148
149static pi_major_info_t pi_major_call_table[] = {
150 { pi_minor_call_table_type_0, 9 },
151 { pi_minor_call_table_type_1, 7 },
152};
153
154static pi_param_info_t irlap_param_info = { pi_major_call_table, 2, 0x7f, 7 };
155
156/* ---------------------- LOCAL SUBROUTINES ---------------------- */
157/* Note : we start with a bunch of local subroutines.
158 * As the compiler is "one pass", this is the only way to get them to
159 * inline properly...
160 * Jean II
161 */
162/*
163 * Function value_index (value, array, size)
164 *
165 * Returns the index to the value in the specified array
166 */
167static inline int value_index(__u32 value, __u32 *array, int size)
168{
169 int i;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 for (i=0; i < size; i++)
172 if (array[i] == value)
173 break;
174 return i;
175}
176
177/*
178 * Function index_value (index, array)
179 *
180 * Returns value to index in array, easy!
181 *
182 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900183static inline __u32 index_value(int index, __u32 *array)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 return array[index];
186}
187
188/*
189 * Function msb_index (word)
190 *
191 * Returns index to most significant bit (MSB) in word
192 *
193 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900194static int msb_index (__u16 word)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 __u16 msb = 0x8000;
197 int index = 15; /* Current MSB */
198
199 /* Check for buggy peers.
200 * Note : there is a small probability that it could be us, but I
201 * would expect driver authors to catch that pretty early and be
202 * able to check precisely what's going on. If a end user sees this,
203 * it's very likely the peer. - Jean II */
204 if (word == 0) {
205 IRDA_WARNING("%s(), Detected buggy peer, adjust null PV to 0x1!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800206 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* The only safe choice (we don't know the array size) */
208 word = 0x1;
209 }
210
211 while (msb) {
212 if (word & msb)
213 break; /* Found it! */
214 msb >>=1;
215 index--;
216 }
217 return index;
218}
219
220/*
221 * Function value_lower_bits (value, array)
222 *
223 * Returns a bit field marking all possibility lower than value.
224 */
225static inline int value_lower_bits(__u32 value, __u32 *array, int size, __u16 *field)
226{
227 int i;
228 __u16 mask = 0x1;
229 __u16 result = 0x0;
230
231 for (i=0; i < size; i++) {
232 /* Add the current value to the bit field, shift mask */
233 result |= mask;
234 mask <<= 1;
235 /* Finished ? */
236 if (array[i] >= value)
237 break;
238 }
239 /* Send back a valid index */
240 if(i >= size)
241 i = size - 1; /* Last item */
242 *field = result;
243 return i;
244}
245
246/*
247 * Function value_highest_bit (value, array)
248 *
249 * Returns a bit field marking the highest possibility lower than value.
250 */
251static inline int value_highest_bit(__u32 value, __u32 *array, int size, __u16 *field)
252{
253 int i;
254 __u16 mask = 0x1;
255 __u16 result = 0x0;
256
257 for (i=0; i < size; i++) {
258 /* Finished ? */
259 if (array[i] <= value)
260 break;
261 /* Shift mask */
262 mask <<= 1;
263 }
264 /* Set the current value to the bit field */
265 result |= mask;
266 /* Send back a valid index */
267 if(i >= size)
268 i = size - 1; /* Last item */
269 *field = result;
270 return i;
271}
272
273/* -------------------------- MAIN CALLS -------------------------- */
274
275/*
276 * Function irda_qos_compute_intersection (qos, new)
277 *
278 * Compute the intersection of the old QoS capabilities with new ones
279 *
280 */
281void irda_qos_compute_intersection(struct qos_info *qos, struct qos_info *new)
282{
283 IRDA_ASSERT(qos != NULL, return;);
284 IRDA_ASSERT(new != NULL, return;);
285
286 /* Apply */
287 qos->baud_rate.bits &= new->baud_rate.bits;
288 qos->window_size.bits &= new->window_size.bits;
289 qos->min_turn_time.bits &= new->min_turn_time.bits;
290 qos->max_turn_time.bits &= new->max_turn_time.bits;
291 qos->data_size.bits &= new->data_size.bits;
292 qos->link_disc_time.bits &= new->link_disc_time.bits;
293 qos->additional_bofs.bits &= new->additional_bofs.bits;
294
295 irda_qos_bits_to_value(qos);
296}
297
298/*
299 * Function irda_init_max_qos_capabilies (qos)
300 *
301 * The purpose of this function is for layers and drivers to be able to
302 * set the maximum QoS possible and then "and in" their own limitations
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900303 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305void irda_init_max_qos_capabilies(struct qos_info *qos)
306{
307 int i;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900308 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * These are the maximum supported values as specified on pages
310 * 39-43 in IrLAP
311 */
312
313 /* Use sysctl to set some configurable values... */
314 /* Set configured max speed */
315 i = value_lower_bits(sysctl_max_baud_rate, baud_rates, 10,
316 &qos->baud_rate.bits);
317 sysctl_max_baud_rate = index_value(i, baud_rates);
318
319 /* Set configured max disc time */
320 i = value_lower_bits(sysctl_max_noreply_time, link_disc_times, 8,
321 &qos->link_disc_time.bits);
322 sysctl_max_noreply_time = index_value(i, link_disc_times);
323
324 /* LSB is first byte, MSB is second byte */
325 qos->baud_rate.bits &= 0x03ff;
326
327 qos->window_size.bits = 0x7f;
328 qos->min_turn_time.bits = 0xff;
329 qos->max_turn_time.bits = 0x0f;
330 qos->data_size.bits = 0x3f;
331 qos->link_disc_time.bits &= 0xff;
332 qos->additional_bofs.bits = 0xff;
333}
334EXPORT_SYMBOL(irda_init_max_qos_capabilies);
335
336/*
337 * Function irlap_adjust_qos_settings (qos)
338 *
339 * Adjust QoS settings in case some values are not possible to use because
340 * of other settings
341 */
342static void irlap_adjust_qos_settings(struct qos_info *qos)
343{
344 __u32 line_capacity;
345 int index;
346
Harvey Harrison0dc47872008-03-05 20:47:47 -0800347 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /*
350 * Make sure the mintt is sensible.
351 * Main culprit : Ericsson T39. - Jean II
352 */
353 if (sysctl_min_tx_turn_time > qos->min_turn_time.value) {
354 int i;
355
356 IRDA_WARNING("%s(), Detected buggy peer, adjust mtt to %dus!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800357 __func__, sysctl_min_tx_turn_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* We don't really need bits, but easier this way */
360 i = value_highest_bit(sysctl_min_tx_turn_time, min_turn_times,
361 8, &qos->min_turn_time.bits);
362 sysctl_min_tx_turn_time = index_value(i, min_turn_times);
363 qos->min_turn_time.value = sysctl_min_tx_turn_time;
364 }
365
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900366 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * Not allowed to use a max turn time less than 500 ms if the baudrate
368 * is less than 115200
369 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900370 if ((qos->baud_rate.value < 115200) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 (qos->max_turn_time.value < 500))
372 {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900373 IRDA_DEBUG(0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 "%s(), adjusting max turn time from %d to 500 ms\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800375 __func__, qos->max_turn_time.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 qos->max_turn_time.value = 500;
377 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900380 * The data size must be adjusted according to the baud rate and max
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * turn time
382 */
383 index = value_index(qos->data_size.value, data_sizes, 6);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900384 line_capacity = irlap_max_line_capacity(qos->baud_rate.value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 qos->max_turn_time.value);
386
387#ifdef CONFIG_IRDA_DYNAMIC_WINDOW
388 while ((qos->data_size.value > line_capacity) && (index > 0)) {
389 qos->data_size.value = data_sizes[index--];
390 IRDA_DEBUG(2, "%s(), reducing data size to %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800391 __func__, qos->data_size.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393#else /* Use method described in section 6.6.11 of IrLAP */
394 while (irlap_requested_line_capacity(qos) > line_capacity) {
395 IRDA_ASSERT(index != 0, return;);
396
397 /* Must be able to send at least one frame */
398 if (qos->window_size.value > 1) {
399 qos->window_size.value--;
400 IRDA_DEBUG(2, "%s(), reducing window size to %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800401 __func__, qos->window_size.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } else if (index > 1) {
403 qos->data_size.value = data_sizes[index--];
404 IRDA_DEBUG(2, "%s(), reducing data size to %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800405 __func__, qos->data_size.value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else {
407 IRDA_WARNING("%s(), nothing more we can do!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800408 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 }
411#endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
412 /*
413 * Fix tx data size according to user limits - Jean II
414 */
415 if (qos->data_size.value > sysctl_max_tx_data_size)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300416 /* Allow non discrete adjustement to avoid losing capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 qos->data_size.value = sysctl_max_tx_data_size;
418 /*
419 * Override Tx window if user request it. - Jean II
420 */
421 if (qos->window_size.value > sysctl_max_tx_window)
422 qos->window_size.value = sysctl_max_tx_window;
423}
424
425/*
426 * Function irlap_negotiate (qos_device, qos_session, skb)
427 *
428 * Negotiate QoS values, not really that much negotiation :-)
429 * We just set the QoS capabilities for the peer station
430 *
431 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900432int irlap_qos_negotiate(struct irlap_cb *self, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 int ret;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900435
436 ret = irda_param_extract_all(self, skb->data, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 &irlap_param_info);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* Convert the negotiated bits to values */
440 irda_qos_bits_to_value(&self->qos_tx);
441 irda_qos_bits_to_value(&self->qos_rx);
442
443 irlap_adjust_qos_settings(&self->qos_tx);
444
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900445 IRDA_DEBUG(2, "Setting BAUD_RATE to %d bps.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 self->qos_tx.baud_rate.value);
447 IRDA_DEBUG(2, "Setting DATA_SIZE to %d bytes\n",
448 self->qos_tx.data_size.value);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900449 IRDA_DEBUG(2, "Setting WINDOW_SIZE to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 self->qos_tx.window_size.value);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900451 IRDA_DEBUG(2, "Setting XBOFS to %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 self->qos_tx.additional_bofs.value);
453 IRDA_DEBUG(2, "Setting MAX_TURN_TIME to %d ms.\n",
454 self->qos_tx.max_turn_time.value);
455 IRDA_DEBUG(2, "Setting MIN_TURN_TIME to %d usecs.\n",
456 self->qos_tx.min_turn_time.value);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900457 IRDA_DEBUG(2, "Setting LINK_DISC to %d secs.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 self->qos_tx.link_disc_time.value);
459 return ret;
460}
461
462/*
463 * Function irlap_insert_negotiation_params (qos, fp)
464 *
465 * Insert QoS negotiaion pararameters into frame
466 *
467 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900468int irlap_insert_qos_negotiation_params(struct irlap_cb *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct sk_buff *skb)
470{
471 int ret;
472
473 /* Insert data rate */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700474 ret = irda_param_insert(self, PI_BAUD_RATE, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 skb_tailroom(skb), &irlap_param_info);
476 if (ret < 0)
477 return ret;
478 skb_put(skb, ret);
479
480 /* Insert max turnaround time */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700481 ret = irda_param_insert(self, PI_MAX_TURN_TIME, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 skb_tailroom(skb), &irlap_param_info);
483 if (ret < 0)
484 return ret;
485 skb_put(skb, ret);
486
487 /* Insert data size */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700488 ret = irda_param_insert(self, PI_DATA_SIZE, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 skb_tailroom(skb), &irlap_param_info);
490 if (ret < 0)
491 return ret;
492 skb_put(skb, ret);
493
494 /* Insert window size */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700495 ret = irda_param_insert(self, PI_WINDOW_SIZE, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 skb_tailroom(skb), &irlap_param_info);
497 if (ret < 0)
498 return ret;
499 skb_put(skb, ret);
500
501 /* Insert additional BOFs */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700502 ret = irda_param_insert(self, PI_ADD_BOFS, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 skb_tailroom(skb), &irlap_param_info);
504 if (ret < 0)
505 return ret;
506 skb_put(skb, ret);
507
508 /* Insert minimum turnaround time */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700509 ret = irda_param_insert(self, PI_MIN_TURN_TIME, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 skb_tailroom(skb), &irlap_param_info);
511 if (ret < 0)
512 return ret;
513 skb_put(skb, ret);
514
515 /* Insert link disconnect/threshold time */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700516 ret = irda_param_insert(self, PI_LINK_DISC, skb_tail_pointer(skb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 skb_tailroom(skb), &irlap_param_info);
518 if (ret < 0)
519 return ret;
520 skb_put(skb, ret);
521
522 return 0;
523}
524
525/*
526 * Function irlap_param_baud_rate (instance, param, get)
527 *
528 * Negotiate data-rate
529 *
530 */
531static int irlap_param_baud_rate(void *instance, irda_param_t *param, int get)
532{
533 __u16 final;
534
535 struct irlap_cb *self = (struct irlap_cb *) instance;
536
537 IRDA_ASSERT(self != NULL, return -1;);
538 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
539
540 if (get) {
541 param->pv.i = self->qos_rx.baud_rate.bits;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900542 IRDA_DEBUG(2, "%s(), baud rate = 0x%02x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800543 __func__, param->pv.i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900545 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * Stations must agree on baud rate, so calculate
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900547 * intersection
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549 IRDA_DEBUG(2, "Requested BAUD_RATE: 0x%04x\n", (__u16) param->pv.i);
550 final = (__u16) param->pv.i & self->qos_rx.baud_rate.bits;
551
552 IRDA_DEBUG(2, "Final BAUD_RATE: 0x%04x\n", final);
553 self->qos_tx.baud_rate.bits = final;
554 self->qos_rx.baud_rate.bits = final;
555 }
556
557 return 0;
558}
559
560/*
561 * Function irlap_param_link_disconnect (instance, param, get)
562 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900563 * Negotiate link disconnect/threshold time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *
565 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900566static int irlap_param_link_disconnect(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int get)
568{
569 __u16 final;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct irlap_cb *self = (struct irlap_cb *) instance;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 IRDA_ASSERT(self != NULL, return -1;);
574 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (get)
577 param->pv.i = self->qos_rx.link_disc_time.bits;
578 else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900579 /*
580 * Stations must agree on link disconnect/threshold
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * time.
582 */
583 IRDA_DEBUG(2, "LINK_DISC: %02x\n", (__u8) param->pv.i);
584 final = (__u8) param->pv.i & self->qos_rx.link_disc_time.bits;
585
586 IRDA_DEBUG(2, "Final LINK_DISC: %02x\n", final);
587 self->qos_tx.link_disc_time.bits = final;
588 self->qos_rx.link_disc_time.bits = final;
589 }
590 return 0;
591}
592
593/*
594 * Function irlap_param_max_turn_time (instance, param, get)
595 *
596 * Negotiate the maximum turnaround time. This is a type 1 parameter and
597 * will be negotiated independently for each station
598 *
599 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900600static int irlap_param_max_turn_time(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int get)
602{
603 struct irlap_cb *self = (struct irlap_cb *) instance;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 IRDA_ASSERT(self != NULL, return -1;);
606 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (get)
609 param->pv.i = self->qos_rx.max_turn_time.bits;
610 else
611 self->qos_tx.max_turn_time.bits = (__u8) param->pv.i;
612
613 return 0;
614}
615
616/*
617 * Function irlap_param_data_size (instance, param, get)
618 *
619 * Negotiate the data size. This is a type 1 parameter and
620 * will be negotiated independently for each station
621 *
622 */
623static int irlap_param_data_size(void *instance, irda_param_t *param, int get)
624{
625 struct irlap_cb *self = (struct irlap_cb *) instance;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 IRDA_ASSERT(self != NULL, return -1;);
628 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (get)
631 param->pv.i = self->qos_rx.data_size.bits;
632 else
633 self->qos_tx.data_size.bits = (__u8) param->pv.i;
634
635 return 0;
636}
637
638/*
639 * Function irlap_param_window_size (instance, param, get)
640 *
641 * Negotiate the window size. This is a type 1 parameter and
642 * will be negotiated independently for each station
643 *
644 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900645static int irlap_param_window_size(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int get)
647{
648 struct irlap_cb *self = (struct irlap_cb *) instance;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 IRDA_ASSERT(self != NULL, return -1;);
651 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (get)
654 param->pv.i = self->qos_rx.window_size.bits;
655 else
656 self->qos_tx.window_size.bits = (__u8) param->pv.i;
657
658 return 0;
659}
660
661/*
662 * Function irlap_param_additional_bofs (instance, param, get)
663 *
664 * Negotiate additional BOF characters. This is a type 1 parameter and
665 * will be negotiated independently for each station.
666 */
667static int irlap_param_additional_bofs(void *instance, irda_param_t *param, int get)
668{
669 struct irlap_cb *self = (struct irlap_cb *) instance;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 IRDA_ASSERT(self != NULL, return -1;);
672 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (get)
675 param->pv.i = self->qos_rx.additional_bofs.bits;
676 else
677 self->qos_tx.additional_bofs.bits = (__u8) param->pv.i;
678
679 return 0;
680}
681
682/*
683 * Function irlap_param_min_turn_time (instance, param, get)
684 *
685 * Negotiate the minimum turn around time. This is a type 1 parameter and
686 * will be negotiated independently for each station
687 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900688static int irlap_param_min_turn_time(void *instance, irda_param_t *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int get)
690{
691 struct irlap_cb *self = (struct irlap_cb *) instance;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 IRDA_ASSERT(self != NULL, return -1;);
694 IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (get)
697 param->pv.i = self->qos_rx.min_turn_time.bits;
698 else
699 self->qos_tx.min_turn_time.bits = (__u8) param->pv.i;
700
701 return 0;
702}
703
704/*
705 * Function irlap_max_line_capacity (speed, max_turn_time, min_turn_time)
706 *
707 * Calculate the maximum line capacity
708 *
709 */
710__u32 irlap_max_line_capacity(__u32 speed, __u32 max_turn_time)
711{
712 __u32 line_capacity;
713 int i,j;
714
715 IRDA_DEBUG(2, "%s(), speed=%d, max_turn_time=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800716 __func__, speed, max_turn_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 i = value_index(speed, baud_rates, 10);
719 j = value_index(max_turn_time, max_turn_times, 4);
720
721 IRDA_ASSERT(((i >=0) && (i <10)), return 0;);
722 IRDA_ASSERT(((j >=0) && (j <4)), return 0;);
723
724 line_capacity = max_line_capacities[i][j];
725
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900726 IRDA_DEBUG(2, "%s(), line capacity=%d bytes\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800727 __func__, line_capacity);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return line_capacity;
730}
731
732#ifndef CONFIG_IRDA_DYNAMIC_WINDOW
733static __u32 irlap_requested_line_capacity(struct qos_info *qos)
734{
735 __u32 line_capacity;
736
737 line_capacity = qos->window_size.value *
738 (qos->data_size.value + 6 + qos->additional_bofs.value) +
739 irlap_min_turn_time_in_bytes(qos->baud_rate.value,
740 qos->min_turn_time.value);
741
742 IRDA_DEBUG(2, "%s(), requested line capacity=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800743 __func__, line_capacity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 return line_capacity;
746}
747#endif
748
749void irda_qos_bits_to_value(struct qos_info *qos)
750{
751 int index;
752
753 IRDA_ASSERT(qos != NULL, return;);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 index = msb_index(qos->baud_rate.bits);
756 qos->baud_rate.value = baud_rates[index];
757
758 index = msb_index(qos->data_size.bits);
759 qos->data_size.value = data_sizes[index];
760
761 index = msb_index(qos->window_size.bits);
762 qos->window_size.value = index+1;
763
764 index = msb_index(qos->min_turn_time.bits);
765 qos->min_turn_time.value = min_turn_times[index];
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 index = msb_index(qos->max_turn_time.bits);
768 qos->max_turn_time.value = max_turn_times[index];
769
770 index = msb_index(qos->link_disc_time.bits);
771 qos->link_disc_time.value = link_disc_times[index];
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 index = msb_index(qos->additional_bofs.bits);
774 qos->additional_bofs.value = add_bofs[index];
775}
776EXPORT_SYMBOL(irda_qos_bits_to_value);