xref: /openbmc/linux/drivers/pnp/pnpbios/rsparser.c (revision 22246614)
1 /*
2  * rsparser.c - parses and encodes pnpbios resource data streams
3  */
4 
5 #include <linux/ctype.h>
6 #include <linux/pnp.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 
10 #ifdef CONFIG_PCI
11 #include <linux/pci.h>
12 #else
13 inline void pcibios_penalize_isa_irq(int irq, int active)
14 {
15 }
16 #endif				/* CONFIG_PCI */
17 
18 #include "../base.h"
19 #include "pnpbios.h"
20 
21 /* standard resource tags */
22 #define SMALL_TAG_PNPVERNO		0x01
23 #define SMALL_TAG_LOGDEVID		0x02
24 #define SMALL_TAG_COMPATDEVID		0x03
25 #define SMALL_TAG_IRQ			0x04
26 #define SMALL_TAG_DMA			0x05
27 #define SMALL_TAG_STARTDEP		0x06
28 #define SMALL_TAG_ENDDEP		0x07
29 #define SMALL_TAG_PORT			0x08
30 #define SMALL_TAG_FIXEDPORT		0x09
31 #define SMALL_TAG_VENDOR		0x0e
32 #define SMALL_TAG_END			0x0f
33 #define LARGE_TAG			0x80
34 #define LARGE_TAG_MEM			0x81
35 #define LARGE_TAG_ANSISTR		0x82
36 #define LARGE_TAG_UNICODESTR		0x83
37 #define LARGE_TAG_VENDOR		0x84
38 #define LARGE_TAG_MEM32			0x85
39 #define LARGE_TAG_FIXEDMEM32		0x86
40 
41 /*
42  * Resource Data Stream Format:
43  *
44  * Allocated Resources (required)
45  * end tag ->
46  * Resource Configuration Options (optional)
47  * end tag ->
48  * Compitable Device IDs (optional)
49  * final end tag ->
50  */
51 
52 /*
53  * Allocated Resources
54  */
55 
56 static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
57 					       int start, int len)
58 {
59 	int flags = 0;
60 	int end = start + len - 1;
61 
62 	if (len <= 0 || end >= 0x10003)
63 		flags |= IORESOURCE_DISABLED;
64 
65 	pnp_add_io_resource(dev, start, end, flags);
66 }
67 
68 static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
69 						int start, int len)
70 {
71 	int flags = 0;
72 	int end = start + len - 1;
73 
74 	if (len <= 0)
75 		flags |= IORESOURCE_DISABLED;
76 
77 	pnp_add_mem_resource(dev, start, end, flags);
78 }
79 
80 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
81 							    unsigned char *p,
82 							    unsigned char *end)
83 {
84 	unsigned int len, tag;
85 	int io, size, mask, i, flags;
86 
87 	if (!p)
88 		return NULL;
89 
90 	dev_dbg(&dev->dev, "parse allocated resources\n");
91 
92 	pnp_init_resources(dev);
93 
94 	while ((char *)p < (char *)end) {
95 
96 		/* determine the type of tag */
97 		if (p[0] & LARGE_TAG) {	/* large tag */
98 			len = (p[2] << 8) | p[1];
99 			tag = p[0];
100 		} else {	/* small tag */
101 			len = p[0] & 0x07;
102 			tag = ((p[0] >> 3) & 0x0f);
103 		}
104 
105 		switch (tag) {
106 
107 		case LARGE_TAG_MEM:
108 			if (len != 9)
109 				goto len_err;
110 			io = *(short *)&p[4];
111 			size = *(short *)&p[10];
112 			pnpbios_parse_allocated_memresource(dev, io, size);
113 			break;
114 
115 		case LARGE_TAG_ANSISTR:
116 			/* ignore this for now */
117 			break;
118 
119 		case LARGE_TAG_VENDOR:
120 			/* do nothing */
121 			break;
122 
123 		case LARGE_TAG_MEM32:
124 			if (len != 17)
125 				goto len_err;
126 			io = *(int *)&p[4];
127 			size = *(int *)&p[16];
128 			pnpbios_parse_allocated_memresource(dev, io, size);
129 			break;
130 
131 		case LARGE_TAG_FIXEDMEM32:
132 			if (len != 9)
133 				goto len_err;
134 			io = *(int *)&p[4];
135 			size = *(int *)&p[8];
136 			pnpbios_parse_allocated_memresource(dev, io, size);
137 			break;
138 
139 		case SMALL_TAG_IRQ:
140 			if (len < 2 || len > 3)
141 				goto len_err;
142 			flags = 0;
143 			io = -1;
144 			mask = p[1] + p[2] * 256;
145 			for (i = 0; i < 16; i++, mask = mask >> 1)
146 				if (mask & 0x01)
147 					io = i;
148 			if (io != -1)
149 				pcibios_penalize_isa_irq(io, 1);
150 			else
151 				flags = IORESOURCE_DISABLED;
152 			pnp_add_irq_resource(dev, io, flags);
153 			break;
154 
155 		case SMALL_TAG_DMA:
156 			if (len != 2)
157 				goto len_err;
158 			flags = 0;
159 			io = -1;
160 			mask = p[1];
161 			for (i = 0; i < 8; i++, mask = mask >> 1)
162 				if (mask & 0x01)
163 					io = i;
164 			if (io == -1)
165 				flags = IORESOURCE_DISABLED;
166 			pnp_add_dma_resource(dev, io, flags);
167 			break;
168 
169 		case SMALL_TAG_PORT:
170 			if (len != 7)
171 				goto len_err;
172 			io = p[2] + p[3] * 256;
173 			size = p[7];
174 			pnpbios_parse_allocated_ioresource(dev, io, size);
175 			break;
176 
177 		case SMALL_TAG_VENDOR:
178 			/* do nothing */
179 			break;
180 
181 		case SMALL_TAG_FIXEDPORT:
182 			if (len != 3)
183 				goto len_err;
184 			io = p[1] + p[2] * 256;
185 			size = p[3];
186 			pnpbios_parse_allocated_ioresource(dev, io, size);
187 			break;
188 
189 		case SMALL_TAG_END:
190 			p = p + 2;
191 			return (unsigned char *)p;
192 			break;
193 
194 		default:	/* an unkown tag */
195 len_err:
196 			dev_err(&dev->dev, "unknown tag %#x length %d\n",
197 				tag, len);
198 			break;
199 		}
200 
201 		/* continue to the next tag */
202 		if (p[0] & LARGE_TAG)
203 			p += len + 3;
204 		else
205 			p += len + 1;
206 	}
207 
208 	dev_err(&dev->dev, "no end tag in resource structure\n");
209 
210 	return NULL;
211 }
212 
213 /*
214  * Resource Configuration Options
215  */
216 
217 static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
218 					    unsigned char *p, int size,
219 					    struct pnp_option *option)
220 {
221 	struct pnp_mem *mem;
222 
223 	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
224 	if (!mem)
225 		return;
226 	mem->min = ((p[5] << 8) | p[4]) << 8;
227 	mem->max = ((p[7] << 8) | p[6]) << 8;
228 	mem->align = (p[9] << 8) | p[8];
229 	mem->size = ((p[11] << 8) | p[10]) << 8;
230 	mem->flags = p[3];
231 	pnp_register_mem_resource(dev, option, mem);
232 }
233 
234 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
235 					      unsigned char *p, int size,
236 					      struct pnp_option *option)
237 {
238 	struct pnp_mem *mem;
239 
240 	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
241 	if (!mem)
242 		return;
243 	mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
244 	mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
245 	mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
246 	mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
247 	mem->flags = p[3];
248 	pnp_register_mem_resource(dev, option, mem);
249 }
250 
251 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
252 						    unsigned char *p, int size,
253 						    struct pnp_option *option)
254 {
255 	struct pnp_mem *mem;
256 
257 	mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
258 	if (!mem)
259 		return;
260 	mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
261 	mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
262 	mem->align = 0;
263 	mem->flags = p[3];
264 	pnp_register_mem_resource(dev, option, mem);
265 }
266 
267 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
268 					    unsigned char *p, int size,
269 					    struct pnp_option *option)
270 {
271 	struct pnp_irq *irq;
272 	unsigned long bits;
273 
274 	irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
275 	if (!irq)
276 		return;
277 	bits = (p[2] << 8) | p[1];
278 	bitmap_copy(irq->map, &bits, 16);
279 	if (size > 2)
280 		irq->flags = p[3];
281 	else
282 		irq->flags = IORESOURCE_IRQ_HIGHEDGE;
283 	pnp_register_irq_resource(dev, option, irq);
284 }
285 
286 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
287 					    unsigned char *p, int size,
288 					    struct pnp_option *option)
289 {
290 	struct pnp_dma *dma;
291 
292 	dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
293 	if (!dma)
294 		return;
295 	dma->map = p[1];
296 	dma->flags = p[2];
297 	pnp_register_dma_resource(dev, option, dma);
298 }
299 
300 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
301 					     unsigned char *p, int size,
302 					     struct pnp_option *option)
303 {
304 	struct pnp_port *port;
305 
306 	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
307 	if (!port)
308 		return;
309 	port->min = (p[3] << 8) | p[2];
310 	port->max = (p[5] << 8) | p[4];
311 	port->align = p[6];
312 	port->size = p[7];
313 	port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
314 	pnp_register_port_resource(dev, option, port);
315 }
316 
317 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
318 						   unsigned char *p, int size,
319 						   struct pnp_option *option)
320 {
321 	struct pnp_port *port;
322 
323 	port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
324 	if (!port)
325 		return;
326 	port->min = port->max = (p[2] << 8) | p[1];
327 	port->size = p[3];
328 	port->align = 0;
329 	port->flags = PNP_PORT_FLAG_FIXED;
330 	pnp_register_port_resource(dev, option, port);
331 }
332 
333 static __init unsigned char *
334 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
335 					struct pnp_dev *dev)
336 {
337 	unsigned int len, tag;
338 	int priority = 0;
339 	struct pnp_option *option, *option_independent;
340 
341 	if (!p)
342 		return NULL;
343 
344 	dev_dbg(&dev->dev, "parse resource options\n");
345 
346 	option_independent = option = pnp_register_independent_option(dev);
347 	if (!option)
348 		return NULL;
349 
350 	while ((char *)p < (char *)end) {
351 
352 		/* determine the type of tag */
353 		if (p[0] & LARGE_TAG) {	/* large tag */
354 			len = (p[2] << 8) | p[1];
355 			tag = p[0];
356 		} else {	/* small tag */
357 			len = p[0] & 0x07;
358 			tag = ((p[0] >> 3) & 0x0f);
359 		}
360 
361 		switch (tag) {
362 
363 		case LARGE_TAG_MEM:
364 			if (len != 9)
365 				goto len_err;
366 			pnpbios_parse_mem_option(dev, p, len, option);
367 			break;
368 
369 		case LARGE_TAG_MEM32:
370 			if (len != 17)
371 				goto len_err;
372 			pnpbios_parse_mem32_option(dev, p, len, option);
373 			break;
374 
375 		case LARGE_TAG_FIXEDMEM32:
376 			if (len != 9)
377 				goto len_err;
378 			pnpbios_parse_fixed_mem32_option(dev, p, len, option);
379 			break;
380 
381 		case SMALL_TAG_IRQ:
382 			if (len < 2 || len > 3)
383 				goto len_err;
384 			pnpbios_parse_irq_option(dev, p, len, option);
385 			break;
386 
387 		case SMALL_TAG_DMA:
388 			if (len != 2)
389 				goto len_err;
390 			pnpbios_parse_dma_option(dev, p, len, option);
391 			break;
392 
393 		case SMALL_TAG_PORT:
394 			if (len != 7)
395 				goto len_err;
396 			pnpbios_parse_port_option(dev, p, len, option);
397 			break;
398 
399 		case SMALL_TAG_VENDOR:
400 			/* do nothing */
401 			break;
402 
403 		case SMALL_TAG_FIXEDPORT:
404 			if (len != 3)
405 				goto len_err;
406 			pnpbios_parse_fixed_port_option(dev, p, len, option);
407 			break;
408 
409 		case SMALL_TAG_STARTDEP:
410 			if (len > 1)
411 				goto len_err;
412 			priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
413 			if (len > 0)
414 				priority = 0x100 | p[1];
415 			option = pnp_register_dependent_option(dev, priority);
416 			if (!option)
417 				return NULL;
418 			break;
419 
420 		case SMALL_TAG_ENDDEP:
421 			if (len != 0)
422 				goto len_err;
423 			if (option_independent == option)
424 				dev_warn(&dev->dev, "missing "
425 					 "SMALL_TAG_STARTDEP tag\n");
426 			option = option_independent;
427 			dev_dbg(&dev->dev, "end dependent options\n");
428 			break;
429 
430 		case SMALL_TAG_END:
431 			return p + 2;
432 
433 		default:	/* an unkown tag */
434 len_err:
435 			dev_err(&dev->dev, "unknown tag %#x length %d\n",
436 				tag, len);
437 			break;
438 		}
439 
440 		/* continue to the next tag */
441 		if (p[0] & LARGE_TAG)
442 			p += len + 3;
443 		else
444 			p += len + 1;
445 	}
446 
447 	dev_err(&dev->dev, "no end tag in resource structure\n");
448 
449 	return NULL;
450 }
451 
452 /*
453  * Compatible Device IDs
454  */
455 
456 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
457 						   unsigned char *end,
458 						   struct pnp_dev *dev)
459 {
460 	int len, tag;
461 	u32 eisa_id;
462 	char id[8];
463 	struct pnp_id *dev_id;
464 
465 	if (!p)
466 		return NULL;
467 
468 	while ((char *)p < (char *)end) {
469 
470 		/* determine the type of tag */
471 		if (p[0] & LARGE_TAG) {	/* large tag */
472 			len = (p[2] << 8) | p[1];
473 			tag = p[0];
474 		} else {	/* small tag */
475 			len = p[0] & 0x07;
476 			tag = ((p[0] >> 3) & 0x0f);
477 		}
478 
479 		switch (tag) {
480 
481 		case LARGE_TAG_ANSISTR:
482 			strncpy(dev->name, p + 3,
483 				len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
484 			dev->name[len >=
485 				  PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
486 			break;
487 
488 		case SMALL_TAG_COMPATDEVID:	/* compatible ID */
489 			if (len != 4)
490 				goto len_err;
491 			eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
492 			pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
493 			dev_id = pnp_add_id(dev, id);
494 			if (!dev_id)
495 				return NULL;
496 			break;
497 
498 		case SMALL_TAG_END:
499 			p = p + 2;
500 			return (unsigned char *)p;
501 			break;
502 
503 		default:	/* an unkown tag */
504 len_err:
505 			dev_err(&dev->dev, "unknown tag %#x length %d\n",
506 				tag, len);
507 			break;
508 		}
509 
510 		/* continue to the next tag */
511 		if (p[0] & LARGE_TAG)
512 			p += len + 3;
513 		else
514 			p += len + 1;
515 	}
516 
517 	dev_err(&dev->dev, "no end tag in resource structure\n");
518 
519 	return NULL;
520 }
521 
522 /*
523  * Allocated Resource Encoding
524  */
525 
526 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
527 			       struct resource *res)
528 {
529 	unsigned long base = res->start;
530 	unsigned long len = res->end - res->start + 1;
531 
532 	p[4] = (base >> 8) & 0xff;
533 	p[5] = ((base >> 8) >> 8) & 0xff;
534 	p[6] = (base >> 8) & 0xff;
535 	p[7] = ((base >> 8) >> 8) & 0xff;
536 	p[10] = (len >> 8) & 0xff;
537 	p[11] = ((len >> 8) >> 8) & 0xff;
538 
539 	dev_dbg(&dev->dev, "  encode mem %#llx-%#llx\n",
540 		(unsigned long long) res->start, (unsigned long long) res->end);
541 }
542 
543 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
544 				 struct resource *res)
545 {
546 	unsigned long base = res->start;
547 	unsigned long len = res->end - res->start + 1;
548 
549 	p[4] = base & 0xff;
550 	p[5] = (base >> 8) & 0xff;
551 	p[6] = (base >> 16) & 0xff;
552 	p[7] = (base >> 24) & 0xff;
553 	p[8] = base & 0xff;
554 	p[9] = (base >> 8) & 0xff;
555 	p[10] = (base >> 16) & 0xff;
556 	p[11] = (base >> 24) & 0xff;
557 	p[16] = len & 0xff;
558 	p[17] = (len >> 8) & 0xff;
559 	p[18] = (len >> 16) & 0xff;
560 	p[19] = (len >> 24) & 0xff;
561 
562 	dev_dbg(&dev->dev, "  encode mem32 %#llx-%#llx\n",
563 		(unsigned long long) res->start, (unsigned long long) res->end);
564 }
565 
566 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
567 				       struct resource *res)
568 {
569 	unsigned long base = res->start;
570 	unsigned long len = res->end - res->start + 1;
571 
572 	p[4] = base & 0xff;
573 	p[5] = (base >> 8) & 0xff;
574 	p[6] = (base >> 16) & 0xff;
575 	p[7] = (base >> 24) & 0xff;
576 	p[8] = len & 0xff;
577 	p[9] = (len >> 8) & 0xff;
578 	p[10] = (len >> 16) & 0xff;
579 	p[11] = (len >> 24) & 0xff;
580 
581 	dev_dbg(&dev->dev, "  encode fixed_mem32 %#llx-%#llx\n",
582 		(unsigned long long) res->start, (unsigned long long) res->end);
583 }
584 
585 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
586 			       struct resource *res)
587 {
588 	unsigned long map = 0;
589 
590 	map = 1 << res->start;
591 	p[1] = map & 0xff;
592 	p[2] = (map >> 8) & 0xff;
593 
594 	dev_dbg(&dev->dev, "  encode irq %llu\n",
595 		(unsigned long long)res->start);
596 }
597 
598 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
599 			       struct resource *res)
600 {
601 	unsigned long map = 0;
602 
603 	map = 1 << res->start;
604 	p[1] = map & 0xff;
605 
606 	dev_dbg(&dev->dev, "  encode dma %llu\n",
607 		(unsigned long long)res->start);
608 }
609 
610 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
611 				struct resource *res)
612 {
613 	unsigned long base = res->start;
614 	unsigned long len = res->end - res->start + 1;
615 
616 	p[2] = base & 0xff;
617 	p[3] = (base >> 8) & 0xff;
618 	p[4] = base & 0xff;
619 	p[5] = (base >> 8) & 0xff;
620 	p[7] = len & 0xff;
621 
622 	dev_dbg(&dev->dev, "  encode io %#llx-%#llx\n",
623 		(unsigned long long) res->start, (unsigned long long) res->end);
624 }
625 
626 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
627 				      struct resource *res)
628 {
629 	unsigned long base = res->start;
630 	unsigned long len = res->end - res->start + 1;
631 
632 	p[1] = base & 0xff;
633 	p[2] = (base >> 8) & 0xff;
634 	p[3] = len & 0xff;
635 
636 	dev_dbg(&dev->dev, "  encode fixed_io %#llx-%#llx\n",
637 		(unsigned long long) res->start, (unsigned long long) res->end);
638 }
639 
640 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
641 								*dev,
642 							     unsigned char *p,
643 							     unsigned char *end)
644 {
645 	unsigned int len, tag;
646 	int port = 0, irq = 0, dma = 0, mem = 0;
647 
648 	if (!p)
649 		return NULL;
650 
651 	while ((char *)p < (char *)end) {
652 
653 		/* determine the type of tag */
654 		if (p[0] & LARGE_TAG) {	/* large tag */
655 			len = (p[2] << 8) | p[1];
656 			tag = p[0];
657 		} else {	/* small tag */
658 			len = p[0] & 0x07;
659 			tag = ((p[0] >> 3) & 0x0f);
660 		}
661 
662 		switch (tag) {
663 
664 		case LARGE_TAG_MEM:
665 			if (len != 9)
666 				goto len_err;
667 			pnpbios_encode_mem(dev, p,
668 				pnp_get_resource(dev, IORESOURCE_MEM, mem));
669 			mem++;
670 			break;
671 
672 		case LARGE_TAG_MEM32:
673 			if (len != 17)
674 				goto len_err;
675 			pnpbios_encode_mem32(dev, p,
676 				pnp_get_resource(dev, IORESOURCE_MEM, mem));
677 			mem++;
678 			break;
679 
680 		case LARGE_TAG_FIXEDMEM32:
681 			if (len != 9)
682 				goto len_err;
683 			pnpbios_encode_fixed_mem32(dev, p,
684 				pnp_get_resource(dev, IORESOURCE_MEM, mem));
685 			mem++;
686 			break;
687 
688 		case SMALL_TAG_IRQ:
689 			if (len < 2 || len > 3)
690 				goto len_err;
691 			pnpbios_encode_irq(dev, p,
692 				pnp_get_resource(dev, IORESOURCE_IRQ, irq));
693 			irq++;
694 			break;
695 
696 		case SMALL_TAG_DMA:
697 			if (len != 2)
698 				goto len_err;
699 			pnpbios_encode_dma(dev, p,
700 				pnp_get_resource(dev, IORESOURCE_DMA, dma));
701 			dma++;
702 			break;
703 
704 		case SMALL_TAG_PORT:
705 			if (len != 7)
706 				goto len_err;
707 			pnpbios_encode_port(dev, p,
708 				pnp_get_resource(dev, IORESOURCE_IO, port));
709 			port++;
710 			break;
711 
712 		case SMALL_TAG_VENDOR:
713 			/* do nothing */
714 			break;
715 
716 		case SMALL_TAG_FIXEDPORT:
717 			if (len != 3)
718 				goto len_err;
719 			pnpbios_encode_fixed_port(dev, p,
720 				pnp_get_resource(dev, IORESOURCE_IO, port));
721 			port++;
722 			break;
723 
724 		case SMALL_TAG_END:
725 			p = p + 2;
726 			return (unsigned char *)p;
727 			break;
728 
729 		default:	/* an unkown tag */
730 len_err:
731 			dev_err(&dev->dev, "unknown tag %#x length %d\n",
732 				tag, len);
733 			break;
734 		}
735 
736 		/* continue to the next tag */
737 		if (p[0] & LARGE_TAG)
738 			p += len + 3;
739 		else
740 			p += len + 1;
741 	}
742 
743 	dev_err(&dev->dev, "no end tag in resource structure\n");
744 
745 	return NULL;
746 }
747 
748 /*
749  * Core Parsing Functions
750  */
751 
752 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
753 					struct pnp_bios_node *node)
754 {
755 	unsigned char *p = (char *)node->data;
756 	unsigned char *end = (char *)(node->data + node->size);
757 
758 	p = pnpbios_parse_allocated_resource_data(dev, p, end);
759 	if (!p)
760 		return -EIO;
761 	p = pnpbios_parse_resource_option_data(p, end, dev);
762 	if (!p)
763 		return -EIO;
764 	p = pnpbios_parse_compatible_ids(p, end, dev);
765 	if (!p)
766 		return -EIO;
767 	return 0;
768 }
769 
770 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
771 				     struct pnp_bios_node *node)
772 {
773 	unsigned char *p = (char *)node->data;
774 	unsigned char *end = (char *)(node->data + node->size);
775 
776 	p = pnpbios_parse_allocated_resource_data(dev, p, end);
777 	if (!p)
778 		return -EIO;
779 	return 0;
780 }
781 
782 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
783 				    struct pnp_bios_node *node)
784 {
785 	unsigned char *p = (char *)node->data;
786 	unsigned char *end = (char *)(node->data + node->size);
787 
788 	p = pnpbios_encode_allocated_resource_data(dev, p, end);
789 	if (!p)
790 		return -EIO;
791 	return 0;
792 }
793