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