Blame view

net/irda/parameters.c 15.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  /*********************************************************************
   *
   * Filename:      parameters.c
   * Version:       1.0
   * Description:   A more general way to handle (pi,pl,pv) parameters
   * Status:        Experimental.
   * Author:        Dag Brattli <dagb@cs.uit.no>
   * Created at:    Mon Jun  7 10:25:11 1999
   * Modified at:   Sun Jan 30 14:08:39 2000
   * Modified by:   Dag Brattli <dagb@cs.uit.no>
   *
   *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
   *
   *     This program is free software; you can redistribute it and/or
   *     modify it under the terms of the GNU General Public License as
   *     published by the Free Software Foundation; either version 2 of
   *     the License, or (at your option) any later version.
   *
   *     This program is distributed in the hope that it will be useful,
   *     but WITHOUT ANY WARRANTY; without even the implied warranty of
   *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   *     GNU General Public License for more details.
   *
   *     You should have received a copy of the GNU General Public License
   *     along with this program; if not, write to the Free Software
   *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   *     MA 02111-1307 USA
   *
   ********************************************************************/
  
  #include <linux/types.h>
  #include <linux/module.h>
  
  #include <asm/unaligned.h>
  #include <asm/byteorder.h>
  
  #include <net/irda/irda.h>
  #include <net/irda/parameters.h>
  
  static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
  				PV_TYPE type, PI_HANDLER func);
  static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
  			       PV_TYPE type, PI_HANDLER func);
  static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
  			       PV_TYPE type, PI_HANDLER func);
  static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
  				 PV_TYPE type, PI_HANDLER func);
  
  static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
  			       PV_TYPE type, PI_HANDLER func);
  static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
  				PV_TYPE type, PI_HANDLER func);
  
  static int irda_param_unpack(__u8 *buf, char *fmt, ...);
  
  /* Parameter value call table. Must match PV_TYPE */
  static PV_HANDLER pv_extract_table[] = {
  	irda_extract_integer, /* Handler for any length integers */
  	irda_extract_integer, /* Handler for 8  bits integers */
  	irda_extract_integer, /* Handler for 16 bits integers */
  	irda_extract_string,  /* Handler for strings */
  	irda_extract_integer, /* Handler for 32 bits integers */
  	irda_extract_octseq,  /* Handler for octet sequences */
  	irda_extract_no_value /* Handler for no value parameters */
  };
  
  static PV_HANDLER pv_insert_table[] = {
  	irda_insert_integer, /* Handler for any length integers */
  	irda_insert_integer, /* Handler for 8  bits integers */
  	irda_insert_integer, /* Handler for 16 bits integers */
  	NULL,                /* Handler for strings */
  	irda_insert_integer, /* Handler for 32 bits integers */
  	NULL,                /* Handler for octet sequences */
  	irda_insert_no_value /* Handler for no value parameters */
  };
  
  /*
   * Function irda_insert_no_value (self, buf, len, pi, type, func)
   */
  static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
  				PV_TYPE type, PI_HANDLER func)
  {
  	irda_param_t p;
  	int ret;
  
  	p.pi = pi;
  	p.pl = 0;
  
  	/* Call handler for this parameter */
  	ret = (*func)(self, &p, PV_GET);
  
  	/* Extract values anyway, since handler may need them */
  	irda_param_pack(buf, "bb", p.pi, p.pl);
  
  	if (ret < 0)
  		return ret;
  
  	return 2; /* Inserted pl+2 bytes */
  }
  
  /*
   * Function irda_extract_no_value (self, buf, len, type, func)
   *
   *    Extracts a parameter without a pv field (pl=0)
   *
   */
  static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
  				 PV_TYPE type, PI_HANDLER func)
  {
  	irda_param_t p;
  	int ret;
  
  	/* Extract values anyway, since handler may need them */
  	irda_param_unpack(buf, "bb", &p.pi, &p.pl);
  
  	/* Call handler for this parameter */
  	ret = (*func)(self, &p, PV_PUT);
  
  	if (ret < 0)
  		return ret;
  
  	return 2; /* Extracted pl+2 bytes */
  }
  
  /*
   * Function irda_insert_integer (self, buf, len, pi, type, func)
   */
  static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
  			       PV_TYPE type, PI_HANDLER func)
  {
  	irda_param_t p;
  	int n = 0;
  	int err;
  
  	p.pi = pi;             /* In case handler needs to know */
cc53ded27   Joe Perches   [IRDA]: Spelling ...
136
  	p.pl = type & PV_MASK; /* The integer type codes the length as well */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
139
140
141
142
143
144
  	p.pv.i = 0;            /* Clear value */
  
  	/* Call handler for this parameter */
  	err = (*func)(self, &p, PV_GET);
  	if (err < 0)
  		return err;
  
  	/*
cc53ded27   Joe Perches   [IRDA]: Spelling ...
145
  	 * If parameter length is still 0, then (1) this is an any length
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
  	 * integer, and (2) the handler function does not care which length
  	 * we choose to use, so we pick the one the gives the fewest bytes.
  	 */
  	if (p.pl == 0) {
  		if (p.pv.i < 0xff) {
0dc47877a   Harvey Harrison   net: replace rema...
151
152
  			IRDA_DEBUG(2, "%s(), using 1 byte
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
  			p.pl = 1;
  		} else if (p.pv.i < 0xffff) {
0dc47877a   Harvey Harrison   net: replace rema...
155
156
  			IRDA_DEBUG(2, "%s(), using 2 bytes
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
  			p.pl = 2;
  		} else {
0dc47877a   Harvey Harrison   net: replace rema...
159
160
  			IRDA_DEBUG(2, "%s(), using 4 bytes
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
163
164
165
  			p.pl = 4; /* Default length */
  		}
  	}
  	/* Check if buffer is long enough for insertion */
  	if (len < (2+p.pl)) {
b450777a5   G. Liakhovetski   [IrDA]: Misc spel...
166
167
  		IRDA_WARNING("%s: buffer too short for insertion!
  ",
0dc47877a   Harvey Harrison   net: replace rema...
168
  			     __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
  		return -1;
  	}
0dc47877a   Harvey Harrison   net: replace rema...
171
172
  	IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  		   p.pi, p.pl, p.pv.i);
  	switch (p.pl) {
  	case 1:
  		n += irda_param_pack(buf, "bbb", p.pi, p.pl, (__u8) p.pv.i);
  		break;
  	case 2:
  		if (type & PV_BIG_ENDIAN)
  			p.pv.i = cpu_to_be16((__u16) p.pv.i);
  		else
  			p.pv.i = cpu_to_le16((__u16) p.pv.i);
  		n += irda_param_pack(buf, "bbs", p.pi, p.pl, (__u16) p.pv.i);
  		break;
  	case 4:
  		if (type & PV_BIG_ENDIAN)
  			cpu_to_be32s(&p.pv.i);
  		else
  			cpu_to_le32s(&p.pv.i);
  		n += irda_param_pack(buf, "bbi", p.pi, p.pl, p.pv.i);
  
  		break;
  	default:
  		IRDA_WARNING("%s: length %d not supported
  ",
0dc47877a   Harvey Harrison   net: replace rema...
196
  			     __func__, p.pl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  		/* Skip parameter */
  		return -1;
  	}
  
  	return p.pl+2; /* Inserted pl+2 bytes */
  }
  
  /*
   * Function irda_extract integer (self, buf, len, pi, type, func)
   *
   *    Extract a possibly variable length integer from buffer, and call
   *    handler for processing of the parameter
   */
  static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
  				PV_TYPE type, PI_HANDLER func)
  {
  	irda_param_t p;
  	int n = 0;
cc53ded27   Joe Perches   [IRDA]: Spelling ...
215
  	int extract_len;	/* Real length we extract */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
217
218
  	int err;
  
  	p.pi = pi;     /* In case handler needs to know */
cc53ded27   Joe Perches   [IRDA]: Spelling ...
219
  	p.pl = buf[1]; /* Extract length of value */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
  	p.pv.i = 0;    /* Clear value */
  	extract_len = p.pl;	/* Default : extract all */
  
  	/* Check if buffer is long enough for parsing */
  	if (len < (2+p.pl)) {
b450777a5   G. Liakhovetski   [IrDA]: Misc spel...
225
  		IRDA_WARNING("%s: buffer too short for parsing! "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
  			     "Need %d bytes, but len is only %d
  ",
0dc47877a   Harvey Harrison   net: replace rema...
228
  			     __func__, p.pl, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
234
235
236
237
238
239
240
  		return -1;
  	}
  
  	/*
  	 * Check that the integer length is what we expect it to be. If the
  	 * handler want a 16 bits integer then a 32 bits is not good enough
  	 * PV_INTEGER means that the handler is flexible.
  	 */
  	if (((type & PV_MASK) != PV_INTEGER) && ((type & PV_MASK) != p.pl)) {
  		IRDA_ERROR("%s: invalid parameter length! "
  			   "Expected %d bytes, but value had %d bytes!
  ",
0dc47877a   Harvey Harrison   net: replace rema...
241
  			   __func__, type & PV_MASK, p.pl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  
  		/* Most parameters are bit/byte fields or little endian,
  		 * so it's ok to only extract a subset of it (the subset
  		 * that the handler expect). This is necessary, as some
  		 * broken implementations seems to add extra undefined bits.
  		 * If the parameter is shorter than we expect or is big
  		 * endian, we can't play those tricks. Jean II */
  		if((p.pl < (type & PV_MASK)) || (type & PV_BIG_ENDIAN)) {
  			/* Skip parameter */
  			return p.pl+2;
  		} else {
  			/* Extract subset of it, fallthrough */
  			extract_len = type & PV_MASK;
  		}
  	}
  
  
  	switch (extract_len) {
  	case 1:
  		n += irda_param_unpack(buf+2, "b", &p.pv.i);
  		break;
  	case 2:
  		n += irda_param_unpack(buf+2, "s", &p.pv.i);
  		if (type & PV_BIG_ENDIAN)
  			p.pv.i = be16_to_cpu((__u16) p.pv.i);
  		else
  			p.pv.i = le16_to_cpu((__u16) p.pv.i);
  		break;
  	case 4:
  		n += irda_param_unpack(buf+2, "i", &p.pv.i);
  		if (type & PV_BIG_ENDIAN)
  			be32_to_cpus(&p.pv.i);
  		else
  			le32_to_cpus(&p.pv.i);
  		break;
  	default:
  		IRDA_WARNING("%s: length %d not supported
  ",
0dc47877a   Harvey Harrison   net: replace rema...
280
  			     __func__, p.pl);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
  
  		/* Skip parameter */
  		return p.pl+2;
  	}
0dc47877a   Harvey Harrison   net: replace rema...
285
286
  	IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  		   p.pi, p.pl, p.pv.i);
  	/* Call handler for this parameter */
  	err = (*func)(self, &p, PV_PUT);
  	if (err < 0)
  		return err;
  
  	return p.pl+2; /* Extracted pl+2 bytes */
  }
  
  /*
   * Function irda_extract_string (self, buf, len, type, func)
   */
  static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
  			       PV_TYPE type, PI_HANDLER func)
  {
  	char str[33];
  	irda_param_t p;
  	int err;
0dc47877a   Harvey Harrison   net: replace rema...
305
306
  	IRDA_DEBUG(2, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
  
  	p.pi = pi;     /* In case handler needs to know */
cc53ded27   Joe Perches   [IRDA]: Spelling ...
309
  	p.pl = buf[1]; /* Extract length of value */
efc463eb5   Samuel Ortiz   irda: Fix paramet...
310
311
  	if (p.pl > 32)
  		p.pl = 32;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312

0dc47877a   Harvey Harrison   net: replace rema...
313
314
  	IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
316
317
318
  		   p.pi, p.pl);
  
  	/* Check if buffer is long enough for parsing */
  	if (len < (2+p.pl)) {
b450777a5   G. Liakhovetski   [IrDA]: Misc spel...
319
  		IRDA_WARNING("%s: buffer too short for parsing! "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
  			     "Need %d bytes, but len is only %d
  ",
0dc47877a   Harvey Harrison   net: replace rema...
322
  			     __func__, p.pl, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
326
327
328
  		return -1;
  	}
  
  	/* Should be safe to copy string like this since we have already
  	 * checked that the buffer is long enough */
  	strncpy(str, buf+2, p.pl);
0dc47877a   Harvey Harrison   net: replace rema...
329
330
  	IRDA_DEBUG(2, "%s(), str=0x%02x 0x%02x
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
331
332
333
  		   (__u8) str[0], (__u8) str[1]);
  
  	/* Null terminate string */
efc463eb5   Samuel Ortiz   irda: Fix paramet...
334
  	str[p.pl] = '\0';
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  
  	p.pv.c = str; /* Handler will need to take a copy */
  
  	/* Call handler for this parameter */
  	err = (*func)(self, &p, PV_PUT);
  	if (err < 0)
  		return err;
  
  	return p.pl+2; /* Extracted pl+2 bytes */
  }
  
  /*
   * Function irda_extract_octseq (self, buf, len, type, func)
   */
  static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
  			       PV_TYPE type, PI_HANDLER func)
  {
  	irda_param_t p;
  
  	p.pi = pi;     /* In case handler needs to know */
cc53ded27   Joe Perches   [IRDA]: Spelling ...
355
  	p.pl = buf[1]; /* Extract length of value */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
  
  	/* Check if buffer is long enough for parsing */
  	if (len < (2+p.pl)) {
b450777a5   G. Liakhovetski   [IrDA]: Misc spel...
359
  		IRDA_WARNING("%s: buffer too short for parsing! "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
361
  			     "Need %d bytes, but len is only %d
  ",
0dc47877a   Harvey Harrison   net: replace rema...
362
  			     __func__, p.pl, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
364
  		return -1;
  	}
0dc47877a   Harvey Harrison   net: replace rema...
365
366
  	IRDA_DEBUG(0, "%s(), not impl
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
  
  	return p.pl+2; /* Extracted pl+2 bytes */
  }
  
  /*
   * Function irda_param_pack (skb, fmt, ...)
   *
   *    Format:
   *        'i' = 32 bits integer
   *        's' = string
   *
   */
  int irda_param_pack(__u8 *buf, char *fmt, ...)
  {
  	irda_pv_t arg;
  	va_list args;
  	char *p;
  	int n = 0;
  
  	va_start(args, fmt);
  
  	for (p = fmt; *p != '\0'; p++) {
  		switch (*p) {
  		case 'b':  /* 8 bits unsigned byte */
  			buf[n++] = (__u8)va_arg(args, int);
  			break;
  		case 's':  /* 16 bits unsigned short */
  			arg.i = (__u16)va_arg(args, int);
  			put_unaligned((__u16)arg.i, (__u16 *)(buf+n)); n+=2;
  			break;
  		case 'i':  /* 32 bits unsigned integer */
  			arg.i = va_arg(args, __u32);
  			put_unaligned(arg.i, (__u32 *)(buf+n)); n+=4;
  			break;
  #if 0
  		case 'c': /* \0 terminated string */
  			arg.c = va_arg(args, char *);
  			strcpy(buf+n, arg.c);
  			n += strlen(arg.c) + 1;
  			break;
  #endif
  		default:
  			va_end(args);
  			return -1;
  		}
  	}
  	va_end(args);
  
  	return 0;
  }
  EXPORT_SYMBOL(irda_param_pack);
  
  /*
   * Function irda_param_unpack (skb, fmt, ...)
   */
  static int irda_param_unpack(__u8 *buf, char *fmt, ...)
  {
  	irda_pv_t arg;
  	va_list args;
  	char *p;
  	int n = 0;
  
  	va_start(args, fmt);
  
  	for (p = fmt; *p != '\0'; p++) {
  		switch (*p) {
  		case 'b':  /* 8 bits byte */
  			arg.ip = va_arg(args, __u32 *);
  			*arg.ip = buf[n++];
  			break;
  		case 's':  /* 16 bits short */
  			arg.ip = va_arg(args, __u32 *);
  			*arg.ip = get_unaligned((__u16 *)(buf+n)); n+=2;
  			break;
  		case 'i':  /* 32 bits unsigned integer */
  			arg.ip = va_arg(args, __u32 *);
  			*arg.ip = get_unaligned((__u32 *)(buf+n)); n+=4;
  			break;
  #if 0
  		case 'c':   /* \0 terminated string */
  			arg.c = va_arg(args, char *);
  			strcpy(arg.c, buf+n);
  			n += strlen(arg.c) + 1;
  			break;
  #endif
  		default:
  			va_end(args);
  			return -1;
  		}
  
  	}
  	va_end(args);
  
  	return 0;
  }
  
  /*
   * Function irda_param_insert (self, pi, buf, len, info)
   *
   *    Insert the specified parameter (pi) into buffer. Returns number of
   *    bytes inserted
   */
  int irda_param_insert(void *self, __u8 pi, __u8 *buf, int len,
  		      pi_param_info_t *info)
  {
  	pi_minor_info_t *pi_minor_info;
  	__u8 pi_minor;
  	__u8 pi_major;
  	int type;
  	int ret = -1;
  	int n = 0;
  
  	IRDA_ASSERT(buf != NULL, return ret;);
a26e01d71   Richard Knutsson   [IRDA]: irda para...
480
  	IRDA_ASSERT(info != NULL, return ret;);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
482
483
484
485
486
487
488
489
490
  
  	pi_minor = pi & info->pi_mask;
  	pi_major = pi >> info->pi_major_offset;
  
  	/* Check if the identifier value (pi) is valid */
  	if ((pi_major > info->len-1) ||
  	    (pi_minor > info->tables[pi_major].len-1))
  	{
  		IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x
  ",
0dc47877a   Harvey Harrison   net: replace rema...
491
  			   __func__, pi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
493
494
495
496
497
498
499
500
501
502
503
504
  
  		/* Skip this parameter */
  		return -1;
  	}
  
  	/* Lookup the info on how to parse this parameter */
  	pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
  
  	/* Find expected data type for this parameter identifier (pi)*/
  	type = pi_minor_info->type;
  
  	/*  Check if handler has been implemented */
  	if (!pi_minor_info->func) {
0dc47877a   Harvey Harrison   net: replace rema...
505
506
  		IRDA_MESSAGE("%s: no handler for pi=%#x
  ", __func__, pi);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  		/* Skip this parameter */
  		return -1;
  	}
  
  	/* Insert parameter value */
  	ret = (*pv_insert_table[type & PV_MASK])(self, buf+n, len, pi, type,
  						 pi_minor_info->func);
  	return ret;
  }
  EXPORT_SYMBOL(irda_param_insert);
  
  /*
   * Function irda_param_extract (self, buf, len, info)
   *
   *    Parse all parameters. If len is correct, then everything should be
   *    safe. Returns the number of bytes that was parsed
   *
   */
  static int irda_param_extract(void *self, __u8 *buf, int len,
  			      pi_param_info_t *info)
  {
  	pi_minor_info_t *pi_minor_info;
  	__u8 pi_minor;
  	__u8 pi_major;
  	int type;
  	int ret = -1;
  	int n = 0;
  
  	IRDA_ASSERT(buf != NULL, return ret;);
a26e01d71   Richard Knutsson   [IRDA]: irda para...
536
  	IRDA_ASSERT(info != NULL, return ret;);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537
538
539
540
541
542
543
544
545
546
  
  	pi_minor = buf[n] & info->pi_mask;
  	pi_major = buf[n] >> info->pi_major_offset;
  
  	/* Check if the identifier value (pi) is valid */
  	if ((pi_major > info->len-1) ||
  	    (pi_minor > info->tables[pi_major].len-1))
  	{
  		IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x
  ",
0dc47877a   Harvey Harrison   net: replace rema...
547
  			   __func__, buf[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548
549
550
551
552
553
554
555
556
557
  
  		/* Skip this parameter */
  		return 2 + buf[n + 1];  /* Continue */
  	}
  
  	/* Lookup the info on how to parse this parameter */
  	pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
  
  	/* Find expected data type for this parameter identifier (pi)*/
  	type = pi_minor_info->type;
0dc47877a   Harvey Harrison   net: replace rema...
558
559
  	IRDA_DEBUG(3, "%s(), pi=[%d,%d], type=%d
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
562
563
564
565
  		   pi_major, pi_minor, type);
  
  	/*  Check if handler has been implemented */
  	if (!pi_minor_info->func) {
  		IRDA_MESSAGE("%s: no handler for pi=%#x
  ",
0dc47877a   Harvey Harrison   net: replace rema...
566
  			     __func__, buf[n]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  		/* Skip this parameter */
  		return 2 + buf[n + 1]; /* Continue */
  	}
  
  	/* Parse parameter value */
  	ret = (*pv_extract_table[type & PV_MASK])(self, buf+n, len, buf[n],
  						  type, pi_minor_info->func);
  	return ret;
  }
  
  /*
   * Function irda_param_extract_all (self, buf, len, info)
   *
   *    Parse all parameters. If len is correct, then everything should be
   *    safe. Returns the number of bytes that was parsed
   *
   */
6819bc2e1   YOSHIFUJI Hideaki   [NET] IRDA: Fix w...
584
  int irda_param_extract_all(void *self, __u8 *buf, int len,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
586
587
588
589
590
  			   pi_param_info_t *info)
  {
  	int ret = -1;
  	int n = 0;
  
  	IRDA_ASSERT(buf != NULL, return ret;);
a26e01d71   Richard Knutsson   [IRDA]: irda para...
591
  	IRDA_ASSERT(info != NULL, return ret;);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
  
  	/*
  	 * Parse all parameters. Each parameter must be at least two bytes
  	 * long or else there is no point in trying to parse it
  	 */
  	while (len > 2) {
  		ret = irda_param_extract(self, buf+n, len, info);
  		if (ret < 0)
  			return ret;
  
  		n += ret;
  		len -= ret;
  	}
  	return n;
  }
  EXPORT_SYMBOL(irda_param_extract_all);