01f0c3dd1b08ef259bc554c7ff2acb661e1b7e47
[linux-2.6.git] / drivers / pnp / pnpbios / rsparser.c
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;
530         unsigned long len;
531
532         if (pnp_resource_enabled(res)) {
533                 base = res->start;
534                 len = res->end - res->start + 1;
535         } else {
536                 base = 0;
537                 len = 0;
538         }
539
540         p[4] = (base >> 8) & 0xff;
541         p[5] = ((base >> 8) >> 8) & 0xff;
542         p[6] = (base >> 8) & 0xff;
543         p[7] = ((base >> 8) >> 8) & 0xff;
544         p[10] = (len >> 8) & 0xff;
545         p[11] = ((len >> 8) >> 8) & 0xff;
546
547         dev_dbg(&dev->dev, "  encode mem %#lx-%#lx\n", base, base + len - 1);
548 }
549
550 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
551                                  struct resource *res)
552 {
553         unsigned long base;
554         unsigned long len;
555
556         if (pnp_resource_enabled(res)) {
557                 base = res->start;
558                 len = res->end - res->start + 1;
559         } else {
560                 base = 0;
561                 len = 0;
562         }
563
564         p[4] = base & 0xff;
565         p[5] = (base >> 8) & 0xff;
566         p[6] = (base >> 16) & 0xff;
567         p[7] = (base >> 24) & 0xff;
568         p[8] = base & 0xff;
569         p[9] = (base >> 8) & 0xff;
570         p[10] = (base >> 16) & 0xff;
571         p[11] = (base >> 24) & 0xff;
572         p[16] = len & 0xff;
573         p[17] = (len >> 8) & 0xff;
574         p[18] = (len >> 16) & 0xff;
575         p[19] = (len >> 24) & 0xff;
576
577         dev_dbg(&dev->dev, "  encode mem32 %#lx-%#lx\n", base, base + len - 1);
578 }
579
580 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
581                                        struct resource *res)
582 {
583         unsigned long base;
584         unsigned long len;
585
586         if (pnp_resource_enabled(res)) {
587                 base = res->start;
588                 len = res->end - res->start + 1;
589         } else {
590                 base = 0;
591                 len = 0;
592         }
593
594         p[4] = base & 0xff;
595         p[5] = (base >> 8) & 0xff;
596         p[6] = (base >> 16) & 0xff;
597         p[7] = (base >> 24) & 0xff;
598         p[8] = len & 0xff;
599         p[9] = (len >> 8) & 0xff;
600         p[10] = (len >> 16) & 0xff;
601         p[11] = (len >> 24) & 0xff;
602
603         dev_dbg(&dev->dev, "  encode fixed_mem32 %#lx-%#lx\n", base,
604                 base + len - 1);
605 }
606
607 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
608                                struct resource *res)
609 {
610         unsigned long map;
611
612         if (pnp_resource_enabled(res))
613                 map = 1 << res->start;
614         else
615                 map = 0;
616
617         p[1] = map & 0xff;
618         p[2] = (map >> 8) & 0xff;
619
620         dev_dbg(&dev->dev, "  encode irq mask %#lx\n", map);
621 }
622
623 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
624                                struct resource *res)
625 {
626         unsigned long map;
627
628         if (pnp_resource_enabled(res))
629                 map = 1 << res->start;
630         else
631                 map = 0;
632
633         p[1] = map & 0xff;
634
635         dev_dbg(&dev->dev, "  encode dma mask %#lx\n", map);
636 }
637
638 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
639                                 struct resource *res)
640 {
641         unsigned long base;
642         unsigned long len;
643
644         if (pnp_resource_enabled(res)) {
645                 base = res->start;
646                 len = res->end - res->start + 1;
647         } else {
648                 base = 0;
649                 len = 0;
650         }
651
652         p[2] = base & 0xff;
653         p[3] = (base >> 8) & 0xff;
654         p[4] = base & 0xff;
655         p[5] = (base >> 8) & 0xff;
656         p[7] = len & 0xff;
657
658         dev_dbg(&dev->dev, "  encode io %#lx-%#lx\n", base, base + len - 1);
659 }
660
661 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
662                                       struct resource *res)
663 {
664         unsigned long base = res->start;
665         unsigned long len = res->end - res->start + 1;
666
667         if (pnp_resource_enabled(res)) {
668                 base = res->start;
669                 len = res->end - res->start + 1;
670         } else {
671                 base = 0;
672                 len = 0;
673         }
674
675         p[1] = base & 0xff;
676         p[2] = (base >> 8) & 0xff;
677         p[3] = len & 0xff;
678
679         dev_dbg(&dev->dev, "  encode fixed_io %#lx-%#lx\n", base,
680                 base + len - 1);
681 }
682
683 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
684                                                                 *dev,
685                                                              unsigned char *p,
686                                                              unsigned char *end)
687 {
688         unsigned int len, tag;
689         int port = 0, irq = 0, dma = 0, mem = 0;
690
691         if (!p)
692                 return NULL;
693
694         while ((char *)p < (char *)end) {
695
696                 /* determine the type of tag */
697                 if (p[0] & LARGE_TAG) { /* large tag */
698                         len = (p[2] << 8) | p[1];
699                         tag = p[0];
700                 } else {        /* small tag */
701                         len = p[0] & 0x07;
702                         tag = ((p[0] >> 3) & 0x0f);
703                 }
704
705                 switch (tag) {
706
707                 case LARGE_TAG_MEM:
708                         if (len != 9)
709                                 goto len_err;
710                         pnpbios_encode_mem(dev, p,
711                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
712                         mem++;
713                         break;
714
715                 case LARGE_TAG_MEM32:
716                         if (len != 17)
717                                 goto len_err;
718                         pnpbios_encode_mem32(dev, p,
719                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
720                         mem++;
721                         break;
722
723                 case LARGE_TAG_FIXEDMEM32:
724                         if (len != 9)
725                                 goto len_err;
726                         pnpbios_encode_fixed_mem32(dev, p,
727                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
728                         mem++;
729                         break;
730
731                 case SMALL_TAG_IRQ:
732                         if (len < 2 || len > 3)
733                                 goto len_err;
734                         pnpbios_encode_irq(dev, p,
735                                 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
736                         irq++;
737                         break;
738
739                 case SMALL_TAG_DMA:
740                         if (len != 2)
741                                 goto len_err;
742                         pnpbios_encode_dma(dev, p,
743                                 pnp_get_resource(dev, IORESOURCE_DMA, dma));
744                         dma++;
745                         break;
746
747                 case SMALL_TAG_PORT:
748                         if (len != 7)
749                                 goto len_err;
750                         pnpbios_encode_port(dev, p,
751                                 pnp_get_resource(dev, IORESOURCE_IO, port));
752                         port++;
753                         break;
754
755                 case SMALL_TAG_VENDOR:
756                         /* do nothing */
757                         break;
758
759                 case SMALL_TAG_FIXEDPORT:
760                         if (len != 3)
761                                 goto len_err;
762                         pnpbios_encode_fixed_port(dev, p,
763                                 pnp_get_resource(dev, IORESOURCE_IO, port));
764                         port++;
765                         break;
766
767                 case SMALL_TAG_END:
768                         p = p + 2;
769                         return (unsigned char *)p;
770                         break;
771
772                 default:        /* an unkown tag */
773 len_err:
774                         dev_err(&dev->dev, "unknown tag %#x length %d\n",
775                                 tag, len);
776                         break;
777                 }
778
779                 /* continue to the next tag */
780                 if (p[0] & LARGE_TAG)
781                         p += len + 3;
782                 else
783                         p += len + 1;
784         }
785
786         dev_err(&dev->dev, "no end tag in resource structure\n");
787
788         return NULL;
789 }
790
791 /*
792  * Core Parsing Functions
793  */
794
795 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
796                                         struct pnp_bios_node *node)
797 {
798         unsigned char *p = (char *)node->data;
799         unsigned char *end = (char *)(node->data + node->size);
800
801         p = pnpbios_parse_allocated_resource_data(dev, p, end);
802         if (!p)
803                 return -EIO;
804         p = pnpbios_parse_resource_option_data(p, end, dev);
805         if (!p)
806                 return -EIO;
807         p = pnpbios_parse_compatible_ids(p, end, dev);
808         if (!p)
809                 return -EIO;
810         return 0;
811 }
812
813 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
814                                      struct pnp_bios_node *node)
815 {
816         unsigned char *p = (char *)node->data;
817         unsigned char *end = (char *)(node->data + node->size);
818
819         p = pnpbios_parse_allocated_resource_data(dev, p, end);
820         if (!p)
821                 return -EIO;
822         return 0;
823 }
824
825 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
826                                     struct pnp_bios_node *node)
827 {
828         unsigned char *p = (char *)node->data;
829         unsigned char *end = (char *)(node->data + node->size);
830
831         p = pnpbios_encode_allocated_resource_data(dev, p, end);
832         if (!p)
833                 return -EIO;
834         return 0;
835 }