1 /*********************************************************************
3 * Filename: parameters.c
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, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 ********************************************************************/
31 #include <linux/types.h>
32 #include <linux/module.h>
34 #include <asm/unaligned.h>
35 #include <asm/byteorder.h>
37 #include <net/irda/irda.h>
38 #include <net/irda/parameters.h>
40 static int irda_extract_integer(void *self
, __u8
*buf
, int len
, __u8 pi
,
41 PV_TYPE type
, PI_HANDLER func
);
42 static int irda_extract_string(void *self
, __u8
*buf
, int len
, __u8 pi
,
43 PV_TYPE type
, PI_HANDLER func
);
44 static int irda_extract_octseq(void *self
, __u8
*buf
, int len
, __u8 pi
,
45 PV_TYPE type
, PI_HANDLER func
);
46 static int irda_extract_no_value(void *self
, __u8
*buf
, int len
, __u8 pi
,
47 PV_TYPE type
, PI_HANDLER func
);
49 static int irda_insert_integer(void *self
, __u8
*buf
, int len
, __u8 pi
,
50 PV_TYPE type
, PI_HANDLER func
);
51 static int irda_insert_no_value(void *self
, __u8
*buf
, int len
, __u8 pi
,
52 PV_TYPE type
, PI_HANDLER func
);
54 static int irda_param_unpack(__u8
*buf
, char *fmt
, ...);
56 /* Parameter value call table. Must match PV_TYPE */
57 static PV_HANDLER pv_extract_table
[] = {
58 irda_extract_integer
, /* Handler for any length integers */
59 irda_extract_integer
, /* Handler for 8 bits integers */
60 irda_extract_integer
, /* Handler for 16 bits integers */
61 irda_extract_string
, /* Handler for strings */
62 irda_extract_integer
, /* Handler for 32 bits integers */
63 irda_extract_octseq
, /* Handler for octet sequences */
64 irda_extract_no_value
/* Handler for no value parameters */
67 static PV_HANDLER pv_insert_table
[] = {
68 irda_insert_integer
, /* Handler for any length integers */
69 irda_insert_integer
, /* Handler for 8 bits integers */
70 irda_insert_integer
, /* Handler for 16 bits integers */
71 NULL
, /* Handler for strings */
72 irda_insert_integer
, /* Handler for 32 bits integers */
73 NULL
, /* Handler for octet sequences */
74 irda_insert_no_value
/* Handler for no value parameters */
78 * Function irda_insert_no_value (self, buf, len, pi, type, func)
80 static int irda_insert_no_value(void *self
, __u8
*buf
, int len
, __u8 pi
,
81 PV_TYPE type
, PI_HANDLER func
)
89 /* Call handler for this parameter */
90 ret
= (*func
)(self
, &p
, PV_GET
);
92 /* Extract values anyway, since handler may need them */
93 irda_param_pack(buf
, "bb", p
.pi
, p
.pl
);
98 return 2; /* Inserted pl+2 bytes */
102 * Function irda_extract_no_value (self, buf, len, type, func)
104 * Extracts a parameter without a pv field (pl=0)
107 static int irda_extract_no_value(void *self
, __u8
*buf
, int len
, __u8 pi
,
108 PV_TYPE type
, PI_HANDLER func
)
113 /* Extract values anyway, since handler may need them */
114 irda_param_unpack(buf
, "bb", &p
.pi
, &p
.pl
);
116 /* Call handler for this parameter */
117 ret
= (*func
)(self
, &p
, PV_PUT
);
122 return 2; /* Extracted pl+2 bytes */
126 * Function irda_insert_integer (self, buf, len, pi, type, func)
128 static int irda_insert_integer(void *self
, __u8
*buf
, int len
, __u8 pi
,
129 PV_TYPE type
, PI_HANDLER func
)
135 p
.pi
= pi
; /* In case handler needs to know */
136 p
.pl
= type
& PV_MASK
; /* The integer type codes the length as well */
137 p
.pv
.i
= 0; /* Clear value */
139 /* Call handler for this parameter */
140 err
= (*func
)(self
, &p
, PV_GET
);
145 * If parameter length is still 0, then (1) this is an any length
146 * integer, and (2) the handler function does not care which length
147 * we choose to use, so we pick the one the gives the fewest bytes.
151 IRDA_DEBUG(2, "%s(), using 1 byte\n", __func__
);
153 } else if (p
.pv
.i
< 0xffff) {
154 IRDA_DEBUG(2, "%s(), using 2 bytes\n", __func__
);
157 IRDA_DEBUG(2, "%s(), using 4 bytes\n", __func__
);
158 p
.pl
= 4; /* Default length */
161 /* Check if buffer is long enough for insertion */
162 if (len
< (2+p
.pl
)) {
163 IRDA_WARNING("%s: buffer too short for insertion!\n",
167 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __func__
,
171 n
+= irda_param_pack(buf
, "bbb", p
.pi
, p
.pl
, (__u8
) p
.pv
.i
);
174 if (type
& PV_BIG_ENDIAN
)
175 p
.pv
.i
= cpu_to_be16((__u16
) p
.pv
.i
);
177 p
.pv
.i
= cpu_to_le16((__u16
) p
.pv
.i
);
178 n
+= irda_param_pack(buf
, "bbs", p
.pi
, p
.pl
, (__u16
) p
.pv
.i
);
181 if (type
& PV_BIG_ENDIAN
)
182 cpu_to_be32s(&p
.pv
.i
);
184 cpu_to_le32s(&p
.pv
.i
);
185 n
+= irda_param_pack(buf
, "bbi", p
.pi
, p
.pl
, p
.pv
.i
);
189 IRDA_WARNING("%s: length %d not supported\n",
195 return p
.pl
+2; /* Inserted pl+2 bytes */
199 * Function irda_extract integer (self, buf, len, pi, type, func)
201 * Extract a possibly variable length integer from buffer, and call
202 * handler for processing of the parameter
204 static int irda_extract_integer(void *self
, __u8
*buf
, int len
, __u8 pi
,
205 PV_TYPE type
, PI_HANDLER func
)
209 int extract_len
; /* Real length we extract */
212 p
.pi
= pi
; /* In case handler needs to know */
213 p
.pl
= buf
[1]; /* Extract length of value */
214 p
.pv
.i
= 0; /* Clear value */
215 extract_len
= p
.pl
; /* Default : extract all */
217 /* Check if buffer is long enough for parsing */
218 if (len
< (2+p
.pl
)) {
219 IRDA_WARNING("%s: buffer too short for parsing! "
220 "Need %d bytes, but len is only %d\n",
221 __func__
, p
.pl
, len
);
226 * Check that the integer length is what we expect it to be. If the
227 * handler want a 16 bits integer then a 32 bits is not good enough
228 * PV_INTEGER means that the handler is flexible.
230 if (((type
& PV_MASK
) != PV_INTEGER
) && ((type
& PV_MASK
) != p
.pl
)) {
231 IRDA_ERROR("%s: invalid parameter length! "
232 "Expected %d bytes, but value had %d bytes!\n",
233 __func__
, type
& PV_MASK
, p
.pl
);
235 /* Most parameters are bit/byte fields or little endian,
236 * so it's ok to only extract a subset of it (the subset
237 * that the handler expect). This is necessary, as some
238 * broken implementations seems to add extra undefined bits.
239 * If the parameter is shorter than we expect or is big
240 * endian, we can't play those tricks. Jean II */
241 if((p
.pl
< (type
& PV_MASK
)) || (type
& PV_BIG_ENDIAN
)) {
245 /* Extract subset of it, fallthrough */
246 extract_len
= type
& PV_MASK
;
251 switch (extract_len
) {
253 n
+= irda_param_unpack(buf
+2, "b", &p
.pv
.i
);
256 n
+= irda_param_unpack(buf
+2, "s", &p
.pv
.i
);
257 if (type
& PV_BIG_ENDIAN
)
258 p
.pv
.i
= be16_to_cpu((__u16
) p
.pv
.i
);
260 p
.pv
.i
= le16_to_cpu((__u16
) p
.pv
.i
);
263 n
+= irda_param_unpack(buf
+2, "i", &p
.pv
.i
);
264 if (type
& PV_BIG_ENDIAN
)
265 be32_to_cpus(&p
.pv
.i
);
267 le32_to_cpus(&p
.pv
.i
);
270 IRDA_WARNING("%s: length %d not supported\n",
277 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __func__
,
279 /* Call handler for this parameter */
280 err
= (*func
)(self
, &p
, PV_PUT
);
284 return p
.pl
+2; /* Extracted pl+2 bytes */
288 * Function irda_extract_string (self, buf, len, type, func)
290 static int irda_extract_string(void *self
, __u8
*buf
, int len
, __u8 pi
,
291 PV_TYPE type
, PI_HANDLER func
)
297 IRDA_DEBUG(2, "%s()\n", __func__
);
299 p
.pi
= pi
; /* In case handler needs to know */
300 p
.pl
= buf
[1]; /* Extract length of value */
304 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d\n", __func__
,
307 /* Check if buffer is long enough for parsing */
308 if (len
< (2+p
.pl
)) {
309 IRDA_WARNING("%s: buffer too short for parsing! "
310 "Need %d bytes, but len is only %d\n",
311 __func__
, p
.pl
, len
);
315 /* Should be safe to copy string like this since we have already
316 * checked that the buffer is long enough */
317 strncpy(str
, buf
+2, p
.pl
);
319 IRDA_DEBUG(2, "%s(), str=0x%02x 0x%02x\n", __func__
,
320 (__u8
) str
[0], (__u8
) str
[1]);
322 /* Null terminate string */
325 p
.pv
.c
= str
; /* Handler will need to take a copy */
327 /* Call handler for this parameter */
328 err
= (*func
)(self
, &p
, PV_PUT
);
332 return p
.pl
+2; /* Extracted pl+2 bytes */
336 * Function irda_extract_octseq (self, buf, len, type, func)
338 static int irda_extract_octseq(void *self
, __u8
*buf
, int len
, __u8 pi
,
339 PV_TYPE type
, PI_HANDLER func
)
343 p
.pi
= pi
; /* In case handler needs to know */
344 p
.pl
= buf
[1]; /* Extract length of value */
346 /* Check if buffer is long enough for parsing */
347 if (len
< (2+p
.pl
)) {
348 IRDA_WARNING("%s: buffer too short for parsing! "
349 "Need %d bytes, but len is only %d\n",
350 __func__
, p
.pl
, len
);
354 IRDA_DEBUG(0, "%s(), not impl\n", __func__
);
356 return p
.pl
+2; /* Extracted pl+2 bytes */
360 * Function irda_param_pack (skb, fmt, ...)
363 * 'i' = 32 bits integer
367 int irda_param_pack(__u8
*buf
, char *fmt
, ...)
376 for (p
= fmt
; *p
!= '\0'; p
++) {
378 case 'b': /* 8 bits unsigned byte */
379 buf
[n
++] = (__u8
)va_arg(args
, int);
381 case 's': /* 16 bits unsigned short */
382 arg
.i
= (__u16
)va_arg(args
, int);
383 put_unaligned((__u16
)arg
.i
, (__u16
*)(buf
+n
)); n
+=2;
385 case 'i': /* 32 bits unsigned integer */
386 arg
.i
= va_arg(args
, __u32
);
387 put_unaligned(arg
.i
, (__u32
*)(buf
+n
)); n
+=4;
390 case 'c': /* \0 terminated string */
391 arg
.c
= va_arg(args
, char *);
392 strcpy(buf
+n
, arg
.c
);
393 n
+= strlen(arg
.c
) + 1;
405 EXPORT_SYMBOL(irda_param_pack
);
408 * Function irda_param_unpack (skb, fmt, ...)
410 static int irda_param_unpack(__u8
*buf
, char *fmt
, ...)
419 for (p
= fmt
; *p
!= '\0'; p
++) {
421 case 'b': /* 8 bits byte */
422 arg
.ip
= va_arg(args
, __u32
*);
425 case 's': /* 16 bits short */
426 arg
.ip
= va_arg(args
, __u32
*);
427 *arg
.ip
= get_unaligned((__u16
*)(buf
+n
)); n
+=2;
429 case 'i': /* 32 bits unsigned integer */
430 arg
.ip
= va_arg(args
, __u32
*);
431 *arg
.ip
= get_unaligned((__u32
*)(buf
+n
)); n
+=4;
434 case 'c': /* \0 terminated string */
435 arg
.c
= va_arg(args
, char *);
436 strcpy(arg
.c
, buf
+n
);
437 n
+= strlen(arg
.c
) + 1;
452 * Function irda_param_insert (self, pi, buf, len, info)
454 * Insert the specified parameter (pi) into buffer. Returns number of
457 int irda_param_insert(void *self
, __u8 pi
, __u8
*buf
, int len
,
458 pi_param_info_t
*info
)
460 pi_minor_info_t
*pi_minor_info
;
467 IRDA_ASSERT(buf
!= NULL
, return ret
;);
468 IRDA_ASSERT(info
!= NULL
, return ret
;);
470 pi_minor
= pi
& info
->pi_mask
;
471 pi_major
= pi
>> info
->pi_major_offset
;
473 /* Check if the identifier value (pi) is valid */
474 if ((pi_major
> info
->len
-1) ||
475 (pi_minor
> info
->tables
[pi_major
].len
-1))
477 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
480 /* Skip this parameter */
484 /* Lookup the info on how to parse this parameter */
485 pi_minor_info
= &info
->tables
[pi_major
].pi_minor_call_table
[pi_minor
];
487 /* Find expected data type for this parameter identifier (pi)*/
488 type
= pi_minor_info
->type
;
490 /* Check if handler has been implemented */
491 if (!pi_minor_info
->func
) {
492 IRDA_MESSAGE("%s: no handler for pi=%#x\n", __func__
, pi
);
493 /* Skip this parameter */
497 /* Insert parameter value */
498 ret
= (*pv_insert_table
[type
& PV_MASK
])(self
, buf
+n
, len
, pi
, type
,
499 pi_minor_info
->func
);
502 EXPORT_SYMBOL(irda_param_insert
);
505 * Function irda_param_extract (self, buf, len, info)
507 * Parse all parameters. If len is correct, then everything should be
508 * safe. Returns the number of bytes that was parsed
511 static int irda_param_extract(void *self
, __u8
*buf
, int len
,
512 pi_param_info_t
*info
)
514 pi_minor_info_t
*pi_minor_info
;
521 IRDA_ASSERT(buf
!= NULL
, return ret
;);
522 IRDA_ASSERT(info
!= NULL
, return ret
;);
524 pi_minor
= buf
[n
] & info
->pi_mask
;
525 pi_major
= buf
[n
] >> info
->pi_major_offset
;
527 /* Check if the identifier value (pi) is valid */
528 if ((pi_major
> info
->len
-1) ||
529 (pi_minor
> info
->tables
[pi_major
].len
-1))
531 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
534 /* Skip this parameter */
535 return 2 + buf
[n
+ 1]; /* Continue */
538 /* Lookup the info on how to parse this parameter */
539 pi_minor_info
= &info
->tables
[pi_major
].pi_minor_call_table
[pi_minor
];
541 /* Find expected data type for this parameter identifier (pi)*/
542 type
= pi_minor_info
->type
;
544 IRDA_DEBUG(3, "%s(), pi=[%d,%d], type=%d\n", __func__
,
545 pi_major
, pi_minor
, type
);
547 /* Check if handler has been implemented */
548 if (!pi_minor_info
->func
) {
549 IRDA_MESSAGE("%s: no handler for pi=%#x\n",
551 /* Skip this parameter */
552 return 2 + buf
[n
+ 1]; /* Continue */
555 /* Parse parameter value */
556 ret
= (*pv_extract_table
[type
& PV_MASK
])(self
, buf
+n
, len
, buf
[n
],
557 type
, pi_minor_info
->func
);
562 * Function irda_param_extract_all (self, buf, len, info)
564 * Parse all parameters. If len is correct, then everything should be
565 * safe. Returns the number of bytes that was parsed
568 int irda_param_extract_all(void *self
, __u8
*buf
, int len
,
569 pi_param_info_t
*info
)
574 IRDA_ASSERT(buf
!= NULL
, return ret
;);
575 IRDA_ASSERT(info
!= NULL
, return ret
;);
578 * Parse all parameters. Each parameter must be at least two bytes
579 * long or else there is no point in trying to parse it
582 ret
= irda_param_extract(self
, buf
+n
, len
, info
);
591 EXPORT_SYMBOL(irda_param_extract_all
);