]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/irda/parameters.c
irda: Remove IRDA_<TYPE> logging macros
[karo-tx-linux.git] / net / irda / parameters.c
1 /*********************************************************************
2  *
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>
11  *
12  *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
13  *
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.
18  *
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.
23  *
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/>.
26  *
27  ********************************************************************/
28
29 #include <linux/types.h>
30 #include <linux/module.h>
31
32 #include <asm/unaligned.h>
33 #include <asm/byteorder.h>
34
35 #include <net/irda/irda.h>
36 #include <net/irda/parameters.h>
37
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);
46
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);
51
52 static int irda_param_unpack(__u8 *buf, char *fmt, ...);
53
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 */
63 };
64
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 */
73 };
74
75 /*
76  * Function irda_insert_no_value (self, buf, len, pi, type, func)
77  */
78 static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
79                                 PV_TYPE type, PI_HANDLER func)
80 {
81         irda_param_t p;
82         int ret;
83
84         p.pi = pi;
85         p.pl = 0;
86
87         /* Call handler for this parameter */
88         ret = (*func)(self, &p, PV_GET);
89
90         /* Extract values anyway, since handler may need them */
91         irda_param_pack(buf, "bb", p.pi, p.pl);
92
93         if (ret < 0)
94                 return ret;
95
96         return 2; /* Inserted pl+2 bytes */
97 }
98
99 /*
100  * Function irda_extract_no_value (self, buf, len, type, func)
101  *
102  *    Extracts a parameter without a pv field (pl=0)
103  *
104  */
105 static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
106                                  PV_TYPE type, PI_HANDLER func)
107 {
108         irda_param_t p;
109         int ret;
110
111         /* Extract values anyway, since handler may need them */
112         irda_param_unpack(buf, "bb", &p.pi, &p.pl);
113
114         /* Call handler for this parameter */
115         ret = (*func)(self, &p, PV_PUT);
116
117         if (ret < 0)
118                 return ret;
119
120         return 2; /* Extracted pl+2 bytes */
121 }
122
123 /*
124  * Function irda_insert_integer (self, buf, len, pi, type, func)
125  */
126 static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
127                                PV_TYPE type, PI_HANDLER func)
128 {
129         irda_param_t p;
130         int n = 0;
131         int err;
132
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 */
136
137         /* Call handler for this parameter */
138         err = (*func)(self, &p, PV_GET);
139         if (err < 0)
140                 return err;
141
142         /*
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.
146          */
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 */
157                 }
158         }
159         /* Check if buffer is long enough for insertion */
160         if (len < (2+p.pl)) {
161                 net_warn_ratelimited("%s: buffer too short for insertion!\n",
162                                      __func__);
163                 return -1;
164         }
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);
184
185                 break;
186         default:
187                 net_warn_ratelimited("%s: length %d not supported\n",
188                                      __func__, p.pl);
189                 /* Skip parameter */
190                 return -1;
191         }
192
193         return p.pl+2; /* Inserted pl+2 bytes */
194 }
195
196 /*
197  * Function irda_extract integer (self, buf, len, pi, type, func)
198  *
199  *    Extract a possibly variable length integer from buffer, and call
200  *    handler for processing of the parameter
201  */
202 static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
203                                 PV_TYPE type, PI_HANDLER func)
204 {
205         irda_param_t p;
206         int n = 0;
207         int extract_len;        /* Real length we extract */
208         int err;
209
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 */
214
215         /* Check if buffer is long enough for parsing */
216         if (len < (2+p.pl)) {
217                 net_warn_ratelimited("%s: buffer too short for parsing! Need %d bytes, but len is only %d\n",
218                                      __func__, p.pl, len);
219                 return -1;
220         }
221
222         /*
223          * Check that the integer length is what we expect it to be. If the
224          * handler want a 16 bits integer then a 32 bits is not good enough
225          * PV_INTEGER means that the handler is flexible.
226          */
227         if (((type & PV_MASK) != PV_INTEGER) && ((type & PV_MASK) != p.pl)) {
228                 net_err_ratelimited("%s: invalid parameter length! Expected %d bytes, but value had %d bytes!\n",
229                                     __func__, type & PV_MASK, p.pl);
230
231                 /* Most parameters are bit/byte fields or little endian,
232                  * so it's ok to only extract a subset of it (the subset
233                  * that the handler expect). This is necessary, as some
234                  * broken implementations seems to add extra undefined bits.
235                  * If the parameter is shorter than we expect or is big
236                  * endian, we can't play those tricks. Jean II */
237                 if((p.pl < (type & PV_MASK)) || (type & PV_BIG_ENDIAN)) {
238                         /* Skip parameter */
239                         return p.pl+2;
240                 } else {
241                         /* Extract subset of it, fallthrough */
242                         extract_len = type & PV_MASK;
243                 }
244         }
245
246
247         switch (extract_len) {
248         case 1:
249                 n += irda_param_unpack(buf+2, "b", &p.pv.i);
250                 break;
251         case 2:
252                 n += irda_param_unpack(buf+2, "s", &p.pv.i);
253                 if (type & PV_BIG_ENDIAN)
254                         p.pv.i = be16_to_cpu((__u16) p.pv.i);
255                 else
256                         p.pv.i = le16_to_cpu((__u16) p.pv.i);
257                 break;
258         case 4:
259                 n += irda_param_unpack(buf+2, "i", &p.pv.i);
260                 if (type & PV_BIG_ENDIAN)
261                         be32_to_cpus(&p.pv.i);
262                 else
263                         le32_to_cpus(&p.pv.i);
264                 break;
265         default:
266                 net_warn_ratelimited("%s: length %d not supported\n",
267                                      __func__, p.pl);
268
269                 /* Skip parameter */
270                 return p.pl+2;
271         }
272
273         IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __func__,
274                    p.pi, p.pl, p.pv.i);
275         /* Call handler for this parameter */
276         err = (*func)(self, &p, PV_PUT);
277         if (err < 0)
278                 return err;
279
280         return p.pl+2; /* Extracted pl+2 bytes */
281 }
282
283 /*
284  * Function irda_extract_string (self, buf, len, type, func)
285  */
286 static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
287                                PV_TYPE type, PI_HANDLER func)
288 {
289         char str[33];
290         irda_param_t p;
291         int err;
292
293         IRDA_DEBUG(2, "%s()\n", __func__);
294
295         p.pi = pi;     /* In case handler needs to know */
296         p.pl = buf[1]; /* Extract length of value */
297         if (p.pl > 32)
298                 p.pl = 32;
299
300         IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d\n", __func__,
301                    p.pi, p.pl);
302
303         /* Check if buffer is long enough for parsing */
304         if (len < (2+p.pl)) {
305                 net_warn_ratelimited("%s: buffer too short for parsing! Need %d bytes, but len is only %d\n",
306                                      __func__, p.pl, len);
307                 return -1;
308         }
309
310         /* Should be safe to copy string like this since we have already
311          * checked that the buffer is long enough */
312         strncpy(str, buf+2, p.pl);
313
314         IRDA_DEBUG(2, "%s(), str=0x%02x 0x%02x\n", __func__,
315                    (__u8) str[0], (__u8) str[1]);
316
317         /* Null terminate string */
318         str[p.pl] = '\0';
319
320         p.pv.c = str; /* Handler will need to take a copy */
321
322         /* Call handler for this parameter */
323         err = (*func)(self, &p, PV_PUT);
324         if (err < 0)
325                 return err;
326
327         return p.pl+2; /* Extracted pl+2 bytes */
328 }
329
330 /*
331  * Function irda_extract_octseq (self, buf, len, type, func)
332  */
333 static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
334                                PV_TYPE type, PI_HANDLER func)
335 {
336         irda_param_t p;
337
338         p.pi = pi;     /* In case handler needs to know */
339         p.pl = buf[1]; /* Extract length of value */
340
341         /* Check if buffer is long enough for parsing */
342         if (len < (2+p.pl)) {
343                 net_warn_ratelimited("%s: buffer too short for parsing! Need %d bytes, but len is only %d\n",
344                                      __func__, p.pl, len);
345                 return -1;
346         }
347
348         IRDA_DEBUG(0, "%s(), not impl\n", __func__);
349
350         return p.pl+2; /* Extracted pl+2 bytes */
351 }
352
353 /*
354  * Function irda_param_pack (skb, fmt, ...)
355  *
356  *    Format:
357  *        'i' = 32 bits integer
358  *        's' = string
359  *
360  */
361 int irda_param_pack(__u8 *buf, char *fmt, ...)
362 {
363         irda_pv_t arg;
364         va_list args;
365         char *p;
366         int n = 0;
367
368         va_start(args, fmt);
369
370         for (p = fmt; *p != '\0'; p++) {
371                 switch (*p) {
372                 case 'b':  /* 8 bits unsigned byte */
373                         buf[n++] = (__u8)va_arg(args, int);
374                         break;
375                 case 's':  /* 16 bits unsigned short */
376                         arg.i = (__u16)va_arg(args, int);
377                         put_unaligned((__u16)arg.i, (__u16 *)(buf+n)); n+=2;
378                         break;
379                 case 'i':  /* 32 bits unsigned integer */
380                         arg.i = va_arg(args, __u32);
381                         put_unaligned(arg.i, (__u32 *)(buf+n)); n+=4;
382                         break;
383 #if 0
384                 case 'c': /* \0 terminated string */
385                         arg.c = va_arg(args, char *);
386                         strcpy(buf+n, arg.c);
387                         n += strlen(arg.c) + 1;
388                         break;
389 #endif
390                 default:
391                         va_end(args);
392                         return -1;
393                 }
394         }
395         va_end(args);
396
397         return 0;
398 }
399 EXPORT_SYMBOL(irda_param_pack);
400
401 /*
402  * Function irda_param_unpack (skb, fmt, ...)
403  */
404 static int irda_param_unpack(__u8 *buf, char *fmt, ...)
405 {
406         irda_pv_t arg;
407         va_list args;
408         char *p;
409         int n = 0;
410
411         va_start(args, fmt);
412
413         for (p = fmt; *p != '\0'; p++) {
414                 switch (*p) {
415                 case 'b':  /* 8 bits byte */
416                         arg.ip = va_arg(args, __u32 *);
417                         *arg.ip = buf[n++];
418                         break;
419                 case 's':  /* 16 bits short */
420                         arg.ip = va_arg(args, __u32 *);
421                         *arg.ip = get_unaligned((__u16 *)(buf+n)); n+=2;
422                         break;
423                 case 'i':  /* 32 bits unsigned integer */
424                         arg.ip = va_arg(args, __u32 *);
425                         *arg.ip = get_unaligned((__u32 *)(buf+n)); n+=4;
426                         break;
427 #if 0
428                 case 'c':   /* \0 terminated string */
429                         arg.c = va_arg(args, char *);
430                         strcpy(arg.c, buf+n);
431                         n += strlen(arg.c) + 1;
432                         break;
433 #endif
434                 default:
435                         va_end(args);
436                         return -1;
437                 }
438
439         }
440         va_end(args);
441
442         return 0;
443 }
444
445 /*
446  * Function irda_param_insert (self, pi, buf, len, info)
447  *
448  *    Insert the specified parameter (pi) into buffer. Returns number of
449  *    bytes inserted
450  */
451 int irda_param_insert(void *self, __u8 pi, __u8 *buf, int len,
452                       pi_param_info_t *info)
453 {
454         pi_minor_info_t *pi_minor_info;
455         __u8 pi_minor;
456         __u8 pi_major;
457         int type;
458         int ret = -1;
459         int n = 0;
460
461         IRDA_ASSERT(buf != NULL, return ret;);
462         IRDA_ASSERT(info != NULL, return ret;);
463
464         pi_minor = pi & info->pi_mask;
465         pi_major = pi >> info->pi_major_offset;
466
467         /* Check if the identifier value (pi) is valid */
468         if ((pi_major > info->len-1) ||
469             (pi_minor > info->tables[pi_major].len-1))
470         {
471                 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
472                            __func__, pi);
473
474                 /* Skip this parameter */
475                 return -1;
476         }
477
478         /* Lookup the info on how to parse this parameter */
479         pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
480
481         /* Find expected data type for this parameter identifier (pi)*/
482         type = pi_minor_info->type;
483
484         /*  Check if handler has been implemented */
485         if (!pi_minor_info->func) {
486                 net_info_ratelimited("%s: no handler for pi=%#x\n",
487                                      __func__, pi);
488                 /* Skip this parameter */
489                 return -1;
490         }
491
492         /* Insert parameter value */
493         ret = (*pv_insert_table[type & PV_MASK])(self, buf+n, len, pi, type,
494                                                  pi_minor_info->func);
495         return ret;
496 }
497 EXPORT_SYMBOL(irda_param_insert);
498
499 /*
500  * Function irda_param_extract (self, buf, len, info)
501  *
502  *    Parse all parameters. If len is correct, then everything should be
503  *    safe. Returns the number of bytes that was parsed
504  *
505  */
506 static int irda_param_extract(void *self, __u8 *buf, int len,
507                               pi_param_info_t *info)
508 {
509         pi_minor_info_t *pi_minor_info;
510         __u8 pi_minor;
511         __u8 pi_major;
512         int type;
513         int ret = -1;
514         int n = 0;
515
516         IRDA_ASSERT(buf != NULL, return ret;);
517         IRDA_ASSERT(info != NULL, return ret;);
518
519         pi_minor = buf[n] & info->pi_mask;
520         pi_major = buf[n] >> info->pi_major_offset;
521
522         /* Check if the identifier value (pi) is valid */
523         if ((pi_major > info->len-1) ||
524             (pi_minor > info->tables[pi_major].len-1))
525         {
526                 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
527                            __func__, buf[0]);
528
529                 /* Skip this parameter */
530                 return 2 + buf[n + 1];  /* Continue */
531         }
532
533         /* Lookup the info on how to parse this parameter */
534         pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
535
536         /* Find expected data type for this parameter identifier (pi)*/
537         type = pi_minor_info->type;
538
539         IRDA_DEBUG(3, "%s(), pi=[%d,%d], type=%d\n", __func__,
540                    pi_major, pi_minor, type);
541
542         /*  Check if handler has been implemented */
543         if (!pi_minor_info->func) {
544                 net_info_ratelimited("%s: no handler for pi=%#x\n",
545                                      __func__, buf[n]);
546                 /* Skip this parameter */
547                 return 2 + buf[n + 1]; /* Continue */
548         }
549
550         /* Parse parameter value */
551         ret = (*pv_extract_table[type & PV_MASK])(self, buf+n, len, buf[n],
552                                                   type, pi_minor_info->func);
553         return ret;
554 }
555
556 /*
557  * Function irda_param_extract_all (self, buf, len, info)
558  *
559  *    Parse all parameters. If len is correct, then everything should be
560  *    safe. Returns the number of bytes that was parsed
561  *
562  */
563 int irda_param_extract_all(void *self, __u8 *buf, int len,
564                            pi_param_info_t *info)
565 {
566         int ret = -1;
567         int n = 0;
568
569         IRDA_ASSERT(buf != NULL, return ret;);
570         IRDA_ASSERT(info != NULL, return ret;);
571
572         /*
573          * Parse all parameters. Each parameter must be at least two bytes
574          * long or else there is no point in trying to parse it
575          */
576         while (len > 2) {
577                 ret = irda_param_extract(self, buf+n, len, info);
578                 if (ret < 0)
579                         return ret;
580
581                 n += ret;
582                 len -= ret;
583         }
584         return n;
585 }
586 EXPORT_SYMBOL(irda_param_extract_all);