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