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