ARM: dt: imx28-cfa10036: introduce a regulator for mmc0
[linux-2.6/btrfs-unstable.git] / net / irda / parameters.c
blob6d0869716bf680f91670bfa8e48f999137bb09fe
1 /*********************************************************************
3 * Filename: parameters.c
4 * Version: 1.0
5 * Description: A more general way to handle (pi,pl,pv) parameters
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Mon Jun 7 10:25:11 1999
9 * Modified at: Sun Jan 30 14:08:39 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 ********************************************************************/
29 #include <linux/types.h>
30 #include <linux/module.h>
32 #include <asm/unaligned.h>
33 #include <asm/byteorder.h>
35 #include <net/irda/irda.h>
36 #include <net/irda/parameters.h>
38 static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
39 PV_TYPE type, PI_HANDLER func);
40 static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
41 PV_TYPE type, PI_HANDLER func);
42 static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
43 PV_TYPE type, PI_HANDLER func);
44 static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
45 PV_TYPE type, PI_HANDLER func);
47 static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
48 PV_TYPE type, PI_HANDLER func);
49 static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
50 PV_TYPE type, PI_HANDLER func);
52 static int irda_param_unpack(__u8 *buf, char *fmt, ...);
54 /* Parameter value call table. Must match PV_TYPE */
55 static PV_HANDLER pv_extract_table[] = {
56 irda_extract_integer, /* Handler for any length integers */
57 irda_extract_integer, /* Handler for 8 bits integers */
58 irda_extract_integer, /* Handler for 16 bits integers */
59 irda_extract_string, /* Handler for strings */
60 irda_extract_integer, /* Handler for 32 bits integers */
61 irda_extract_octseq, /* Handler for octet sequences */
62 irda_extract_no_value /* Handler for no value parameters */
65 static PV_HANDLER pv_insert_table[] = {
66 irda_insert_integer, /* Handler for any length integers */
67 irda_insert_integer, /* Handler for 8 bits integers */
68 irda_insert_integer, /* Handler for 16 bits integers */
69 NULL, /* Handler for strings */
70 irda_insert_integer, /* Handler for 32 bits integers */
71 NULL, /* Handler for octet sequences */
72 irda_insert_no_value /* Handler for no value parameters */
76 * Function irda_insert_no_value (self, buf, len, pi, type, func)
78 static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
79 PV_TYPE type, PI_HANDLER func)
81 irda_param_t p;
82 int ret;
84 p.pi = pi;
85 p.pl = 0;
87 /* Call handler for this parameter */
88 ret = (*func)(self, &p, PV_GET);
90 /* Extract values anyway, since handler may need them */
91 irda_param_pack(buf, "bb", p.pi, p.pl);
93 if (ret < 0)
94 return ret;
96 return 2; /* Inserted pl+2 bytes */
100 * Function irda_extract_no_value (self, buf, len, type, func)
102 * Extracts a parameter without a pv field (pl=0)
105 static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
106 PV_TYPE type, PI_HANDLER func)
108 irda_param_t p;
109 int ret;
111 /* Extract values anyway, since handler may need them */
112 irda_param_unpack(buf, "bb", &p.pi, &p.pl);
114 /* Call handler for this parameter */
115 ret = (*func)(self, &p, PV_PUT);
117 if (ret < 0)
118 return ret;
120 return 2; /* Extracted pl+2 bytes */
124 * Function irda_insert_integer (self, buf, len, pi, type, func)
126 static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
127 PV_TYPE type, PI_HANDLER func)
129 irda_param_t p;
130 int n = 0;
131 int err;
133 p.pi = pi; /* In case handler needs to know */
134 p.pl = type & PV_MASK; /* The integer type codes the length as well */
135 p.pv.i = 0; /* Clear value */
137 /* Call handler for this parameter */
138 err = (*func)(self, &p, PV_GET);
139 if (err < 0)
140 return err;
143 * If parameter length is still 0, then (1) this is an any length
144 * integer, and (2) the handler function does not care which length
145 * we choose to use, so we pick the one the gives the fewest bytes.
147 if (p.pl == 0) {
148 if (p.pv.i < 0xff) {
149 IRDA_DEBUG(2, "%s(), using 1 byte\n", __func__);
150 p.pl = 1;
151 } else if (p.pv.i < 0xffff) {
152 IRDA_DEBUG(2, "%s(), using 2 bytes\n", __func__);
153 p.pl = 2;
154 } else {
155 IRDA_DEBUG(2, "%s(), using 4 bytes\n", __func__);
156 p.pl = 4; /* Default length */
159 /* Check if buffer is long enough for insertion */
160 if (len < (2+p.pl)) {
161 IRDA_WARNING("%s: buffer too short for insertion!\n",
162 __func__);
163 return -1;
165 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __func__,
166 p.pi, p.pl, p.pv.i);
167 switch (p.pl) {
168 case 1:
169 n += irda_param_pack(buf, "bbb", p.pi, p.pl, (__u8) p.pv.i);
170 break;
171 case 2:
172 if (type & PV_BIG_ENDIAN)
173 p.pv.i = cpu_to_be16((__u16) p.pv.i);
174 else
175 p.pv.i = cpu_to_le16((__u16) p.pv.i);
176 n += irda_param_pack(buf, "bbs", p.pi, p.pl, (__u16) p.pv.i);
177 break;
178 case 4:
179 if (type & PV_BIG_ENDIAN)
180 cpu_to_be32s(&p.pv.i);
181 else
182 cpu_to_le32s(&p.pv.i);
183 n += irda_param_pack(buf, "bbi", p.pi, p.pl, p.pv.i);
185 break;
186 default:
187 IRDA_WARNING("%s: length %d not supported\n",
188 __func__, p.pl);
189 /* Skip parameter */
190 return -1;
193 return p.pl+2; /* Inserted pl+2 bytes */
197 * Function irda_extract integer (self, buf, len, pi, type, func)
199 * Extract a possibly variable length integer from buffer, and call
200 * handler for processing of the parameter
202 static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
203 PV_TYPE type, PI_HANDLER func)
205 irda_param_t p;
206 int n = 0;
207 int extract_len; /* Real length we extract */
208 int err;
210 p.pi = pi; /* In case handler needs to know */
211 p.pl = buf[1]; /* Extract length of value */
212 p.pv.i = 0; /* Clear value */
213 extract_len = p.pl; /* Default : extract all */
215 /* Check if buffer is long enough for parsing */
216 if (len < (2+p.pl)) {
217 IRDA_WARNING("%s: buffer too short for parsing! "
218 "Need %d bytes, but len is only %d\n",
219 __func__, p.pl, len);
220 return -1;
224 * Check that the integer length is what we expect it to be. If the
225 * handler want a 16 bits integer then a 32 bits is not good enough
226 * PV_INTEGER means that the handler is flexible.
228 if (((type & PV_MASK) != PV_INTEGER) && ((type & PV_MASK) != p.pl)) {
229 IRDA_ERROR("%s: invalid parameter length! "
230 "Expected %d bytes, but value had %d bytes!\n",
231 __func__, type & PV_MASK, p.pl);
233 /* Most parameters are bit/byte fields or little endian,
234 * so it's ok to only extract a subset of it (the subset
235 * that the handler expect). This is necessary, as some
236 * broken implementations seems to add extra undefined bits.
237 * If the parameter is shorter than we expect or is big
238 * endian, we can't play those tricks. Jean II */
239 if((p.pl < (type & PV_MASK)) || (type & PV_BIG_ENDIAN)) {
240 /* Skip parameter */
241 return p.pl+2;
242 } else {
243 /* Extract subset of it, fallthrough */
244 extract_len = type & PV_MASK;
249 switch (extract_len) {
250 case 1:
251 n += irda_param_unpack(buf+2, "b", &p.pv.i);
252 break;
253 case 2:
254 n += irda_param_unpack(buf+2, "s", &p.pv.i);
255 if (type & PV_BIG_ENDIAN)
256 p.pv.i = be16_to_cpu((__u16) p.pv.i);
257 else
258 p.pv.i = le16_to_cpu((__u16) p.pv.i);
259 break;
260 case 4:
261 n += irda_param_unpack(buf+2, "i", &p.pv.i);
262 if (type & PV_BIG_ENDIAN)
263 be32_to_cpus(&p.pv.i);
264 else
265 le32_to_cpus(&p.pv.i);
266 break;
267 default:
268 IRDA_WARNING("%s: length %d not supported\n",
269 __func__, p.pl);
271 /* Skip parameter */
272 return p.pl+2;
275 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __func__,
276 p.pi, p.pl, p.pv.i);
277 /* Call handler for this parameter */
278 err = (*func)(self, &p, PV_PUT);
279 if (err < 0)
280 return err;
282 return p.pl+2; /* Extracted pl+2 bytes */
286 * Function irda_extract_string (self, buf, len, type, func)
288 static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
289 PV_TYPE type, PI_HANDLER func)
291 char str[33];
292 irda_param_t p;
293 int err;
295 IRDA_DEBUG(2, "%s()\n", __func__);
297 p.pi = pi; /* In case handler needs to know */
298 p.pl = buf[1]; /* Extract length of value */
299 if (p.pl > 32)
300 p.pl = 32;
302 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d\n", __func__,
303 p.pi, p.pl);
305 /* Check if buffer is long enough for parsing */
306 if (len < (2+p.pl)) {
307 IRDA_WARNING("%s: buffer too short for parsing! "
308 "Need %d bytes, but len is only %d\n",
309 __func__, p.pl, len);
310 return -1;
313 /* Should be safe to copy string like this since we have already
314 * checked that the buffer is long enough */
315 strncpy(str, buf+2, p.pl);
317 IRDA_DEBUG(2, "%s(), str=0x%02x 0x%02x\n", __func__,
318 (__u8) str[0], (__u8) str[1]);
320 /* Null terminate string */
321 str[p.pl] = '\0';
323 p.pv.c = str; /* Handler will need to take a copy */
325 /* Call handler for this parameter */
326 err = (*func)(self, &p, PV_PUT);
327 if (err < 0)
328 return err;
330 return p.pl+2; /* Extracted pl+2 bytes */
334 * Function irda_extract_octseq (self, buf, len, type, func)
336 static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
337 PV_TYPE type, PI_HANDLER func)
339 irda_param_t p;
341 p.pi = pi; /* In case handler needs to know */
342 p.pl = buf[1]; /* Extract length of value */
344 /* Check if buffer is long enough for parsing */
345 if (len < (2+p.pl)) {
346 IRDA_WARNING("%s: buffer too short for parsing! "
347 "Need %d bytes, but len is only %d\n",
348 __func__, p.pl, len);
349 return -1;
352 IRDA_DEBUG(0, "%s(), not impl\n", __func__);
354 return p.pl+2; /* Extracted pl+2 bytes */
358 * Function irda_param_pack (skb, fmt, ...)
360 * Format:
361 * 'i' = 32 bits integer
362 * 's' = string
365 int irda_param_pack(__u8 *buf, char *fmt, ...)
367 irda_pv_t arg;
368 va_list args;
369 char *p;
370 int n = 0;
372 va_start(args, fmt);
374 for (p = fmt; *p != '\0'; p++) {
375 switch (*p) {
376 case 'b': /* 8 bits unsigned byte */
377 buf[n++] = (__u8)va_arg(args, int);
378 break;
379 case 's': /* 16 bits unsigned short */
380 arg.i = (__u16)va_arg(args, int);
381 put_unaligned((__u16)arg.i, (__u16 *)(buf+n)); n+=2;
382 break;
383 case 'i': /* 32 bits unsigned integer */
384 arg.i = va_arg(args, __u32);
385 put_unaligned(arg.i, (__u32 *)(buf+n)); n+=4;
386 break;
387 #if 0
388 case 'c': /* \0 terminated string */
389 arg.c = va_arg(args, char *);
390 strcpy(buf+n, arg.c);
391 n += strlen(arg.c) + 1;
392 break;
393 #endif
394 default:
395 va_end(args);
396 return -1;
399 va_end(args);
401 return 0;
403 EXPORT_SYMBOL(irda_param_pack);
406 * Function irda_param_unpack (skb, fmt, ...)
408 static int irda_param_unpack(__u8 *buf, char *fmt, ...)
410 irda_pv_t arg;
411 va_list args;
412 char *p;
413 int n = 0;
415 va_start(args, fmt);
417 for (p = fmt; *p != '\0'; p++) {
418 switch (*p) {
419 case 'b': /* 8 bits byte */
420 arg.ip = va_arg(args, __u32 *);
421 *arg.ip = buf[n++];
422 break;
423 case 's': /* 16 bits short */
424 arg.ip = va_arg(args, __u32 *);
425 *arg.ip = get_unaligned((__u16 *)(buf+n)); n+=2;
426 break;
427 case 'i': /* 32 bits unsigned integer */
428 arg.ip = va_arg(args, __u32 *);
429 *arg.ip = get_unaligned((__u32 *)(buf+n)); n+=4;
430 break;
431 #if 0
432 case 'c': /* \0 terminated string */
433 arg.c = va_arg(args, char *);
434 strcpy(arg.c, buf+n);
435 n += strlen(arg.c) + 1;
436 break;
437 #endif
438 default:
439 va_end(args);
440 return -1;
444 va_end(args);
446 return 0;
450 * Function irda_param_insert (self, pi, buf, len, info)
452 * Insert the specified parameter (pi) into buffer. Returns number of
453 * bytes inserted
455 int irda_param_insert(void *self, __u8 pi, __u8 *buf, int len,
456 pi_param_info_t *info)
458 pi_minor_info_t *pi_minor_info;
459 __u8 pi_minor;
460 __u8 pi_major;
461 int type;
462 int ret = -1;
463 int n = 0;
465 IRDA_ASSERT(buf != NULL, return ret;);
466 IRDA_ASSERT(info != NULL, return ret;);
468 pi_minor = pi & info->pi_mask;
469 pi_major = pi >> info->pi_major_offset;
471 /* Check if the identifier value (pi) is valid */
472 if ((pi_major > info->len-1) ||
473 (pi_minor > info->tables[pi_major].len-1))
475 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
476 __func__, pi);
478 /* Skip this parameter */
479 return -1;
482 /* Lookup the info on how to parse this parameter */
483 pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
485 /* Find expected data type for this parameter identifier (pi)*/
486 type = pi_minor_info->type;
488 /* Check if handler has been implemented */
489 if (!pi_minor_info->func) {
490 IRDA_MESSAGE("%s: no handler for pi=%#x\n", __func__, pi);
491 /* Skip this parameter */
492 return -1;
495 /* Insert parameter value */
496 ret = (*pv_insert_table[type & PV_MASK])(self, buf+n, len, pi, type,
497 pi_minor_info->func);
498 return ret;
500 EXPORT_SYMBOL(irda_param_insert);
503 * Function irda_param_extract (self, buf, len, info)
505 * Parse all parameters. If len is correct, then everything should be
506 * safe. Returns the number of bytes that was parsed
509 static int irda_param_extract(void *self, __u8 *buf, int len,
510 pi_param_info_t *info)
512 pi_minor_info_t *pi_minor_info;
513 __u8 pi_minor;
514 __u8 pi_major;
515 int type;
516 int ret = -1;
517 int n = 0;
519 IRDA_ASSERT(buf != NULL, return ret;);
520 IRDA_ASSERT(info != NULL, return ret;);
522 pi_minor = buf[n] & info->pi_mask;
523 pi_major = buf[n] >> info->pi_major_offset;
525 /* Check if the identifier value (pi) is valid */
526 if ((pi_major > info->len-1) ||
527 (pi_minor > info->tables[pi_major].len-1))
529 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
530 __func__, buf[0]);
532 /* Skip this parameter */
533 return 2 + buf[n + 1]; /* Continue */
536 /* Lookup the info on how to parse this parameter */
537 pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
539 /* Find expected data type for this parameter identifier (pi)*/
540 type = pi_minor_info->type;
542 IRDA_DEBUG(3, "%s(), pi=[%d,%d], type=%d\n", __func__,
543 pi_major, pi_minor, type);
545 /* Check if handler has been implemented */
546 if (!pi_minor_info->func) {
547 IRDA_MESSAGE("%s: no handler for pi=%#x\n",
548 __func__, buf[n]);
549 /* Skip this parameter */
550 return 2 + buf[n + 1]; /* Continue */
553 /* Parse parameter value */
554 ret = (*pv_extract_table[type & PV_MASK])(self, buf+n, len, buf[n],
555 type, pi_minor_info->func);
556 return ret;
560 * Function irda_param_extract_all (self, buf, len, info)
562 * Parse all parameters. If len is correct, then everything should be
563 * safe. Returns the number of bytes that was parsed
566 int irda_param_extract_all(void *self, __u8 *buf, int len,
567 pi_param_info_t *info)
569 int ret = -1;
570 int n = 0;
572 IRDA_ASSERT(buf != NULL, return ret;);
573 IRDA_ASSERT(info != NULL, return ret;);
576 * Parse all parameters. Each parameter must be at least two bytes
577 * long or else there is no point in trying to parse it
579 while (len > 2) {
580 ret = irda_param_extract(self, buf+n, len, info);
581 if (ret < 0)
582 return ret;
584 n += ret;
585 len -= ret;
587 return n;
589 EXPORT_SYMBOL(irda_param_extract_all);