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