PNP: centralize resource option allocations
[linux-2.6/sactl.git] / drivers / pnp / pnpbios / rsparser.c
blob26fb04cc12bbab31d938d8cd67642b2e6269c97a
1 /*
2 * rsparser.c - parses and encodes pnpbios resource data streams
3 */
5 #include <linux/ctype.h>
6 #include <linux/pnp.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
10 #ifdef CONFIG_PCI
11 #include <linux/pci.h>
12 #else
13 inline void pcibios_penalize_isa_irq(int irq, int active)
16 #endif /* CONFIG_PCI */
18 #include "../base.h"
19 #include "pnpbios.h"
21 /* standard resource tags */
22 #define SMALL_TAG_PNPVERNO 0x01
23 #define SMALL_TAG_LOGDEVID 0x02
24 #define SMALL_TAG_COMPATDEVID 0x03
25 #define SMALL_TAG_IRQ 0x04
26 #define SMALL_TAG_DMA 0x05
27 #define SMALL_TAG_STARTDEP 0x06
28 #define SMALL_TAG_ENDDEP 0x07
29 #define SMALL_TAG_PORT 0x08
30 #define SMALL_TAG_FIXEDPORT 0x09
31 #define SMALL_TAG_VENDOR 0x0e
32 #define SMALL_TAG_END 0x0f
33 #define LARGE_TAG 0x80
34 #define LARGE_TAG_MEM 0x81
35 #define LARGE_TAG_ANSISTR 0x82
36 #define LARGE_TAG_UNICODESTR 0x83
37 #define LARGE_TAG_VENDOR 0x84
38 #define LARGE_TAG_MEM32 0x85
39 #define LARGE_TAG_FIXEDMEM32 0x86
42 * Resource Data Stream Format:
44 * Allocated Resources (required)
45 * end tag ->
46 * Resource Configuration Options (optional)
47 * end tag ->
48 * Compitable Device IDs (optional)
49 * final end tag ->
53 * Allocated Resources
56 static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
57 int start, int len)
59 int flags = 0;
60 int end = start + len - 1;
62 if (len <= 0 || end >= 0x10003)
63 flags |= IORESOURCE_DISABLED;
65 pnp_add_io_resource(dev, start, end, flags);
68 static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
69 int start, int len)
71 int flags = 0;
72 int end = start + len - 1;
74 if (len <= 0)
75 flags |= IORESOURCE_DISABLED;
77 pnp_add_mem_resource(dev, start, end, flags);
80 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
81 unsigned char *p,
82 unsigned char *end)
84 unsigned int len, tag;
85 int io, size, mask, i, flags;
87 if (!p)
88 return NULL;
90 dev_dbg(&dev->dev, "parse allocated resources\n");
92 pnp_init_resources(dev);
94 while ((char *)p < (char *)end) {
96 /* determine the type of tag */
97 if (p[0] & LARGE_TAG) { /* large tag */
98 len = (p[2] << 8) | p[1];
99 tag = p[0];
100 } else { /* small tag */
101 len = p[0] & 0x07;
102 tag = ((p[0] >> 3) & 0x0f);
105 switch (tag) {
107 case LARGE_TAG_MEM:
108 if (len != 9)
109 goto len_err;
110 io = *(short *)&p[4];
111 size = *(short *)&p[10];
112 pnpbios_parse_allocated_memresource(dev, io, size);
113 break;
115 case LARGE_TAG_ANSISTR:
116 /* ignore this for now */
117 break;
119 case LARGE_TAG_VENDOR:
120 /* do nothing */
121 break;
123 case LARGE_TAG_MEM32:
124 if (len != 17)
125 goto len_err;
126 io = *(int *)&p[4];
127 size = *(int *)&p[16];
128 pnpbios_parse_allocated_memresource(dev, io, size);
129 break;
131 case LARGE_TAG_FIXEDMEM32:
132 if (len != 9)
133 goto len_err;
134 io = *(int *)&p[4];
135 size = *(int *)&p[8];
136 pnpbios_parse_allocated_memresource(dev, io, size);
137 break;
139 case SMALL_TAG_IRQ:
140 if (len < 2 || len > 3)
141 goto len_err;
142 flags = 0;
143 io = -1;
144 mask = p[1] + p[2] * 256;
145 for (i = 0; i < 16; i++, mask = mask >> 1)
146 if (mask & 0x01)
147 io = i;
148 if (io != -1)
149 pcibios_penalize_isa_irq(io, 1);
150 else
151 flags = IORESOURCE_DISABLED;
152 pnp_add_irq_resource(dev, io, flags);
153 break;
155 case SMALL_TAG_DMA:
156 if (len != 2)
157 goto len_err;
158 flags = 0;
159 io = -1;
160 mask = p[1];
161 for (i = 0; i < 8; i++, mask = mask >> 1)
162 if (mask & 0x01)
163 io = i;
164 if (io == -1)
165 flags = IORESOURCE_DISABLED;
166 pnp_add_dma_resource(dev, io, flags);
167 break;
169 case SMALL_TAG_PORT:
170 if (len != 7)
171 goto len_err;
172 io = p[2] + p[3] * 256;
173 size = p[7];
174 pnpbios_parse_allocated_ioresource(dev, io, size);
175 break;
177 case SMALL_TAG_VENDOR:
178 /* do nothing */
179 break;
181 case SMALL_TAG_FIXEDPORT:
182 if (len != 3)
183 goto len_err;
184 io = p[1] + p[2] * 256;
185 size = p[3];
186 pnpbios_parse_allocated_ioresource(dev, io, size);
187 break;
189 case SMALL_TAG_END:
190 p = p + 2;
191 return (unsigned char *)p;
192 break;
194 default: /* an unkown tag */
195 len_err:
196 dev_err(&dev->dev, "unknown tag %#x length %d\n",
197 tag, len);
198 break;
201 /* continue to the next tag */
202 if (p[0] & LARGE_TAG)
203 p += len + 3;
204 else
205 p += len + 1;
208 dev_err(&dev->dev, "no end tag in resource structure\n");
210 return NULL;
214 * Resource Configuration Options
217 static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
218 unsigned char *p, int size,
219 struct pnp_option *option)
221 resource_size_t min, max, align, len;
222 unsigned char flags;
224 min = ((p[5] << 8) | p[4]) << 8;
225 max = ((p[7] << 8) | p[6]) << 8;
226 align = (p[9] << 8) | p[8];
227 len = ((p[11] << 8) | p[10]) << 8;
228 flags = p[3];
229 pnp_register_mem_resource(dev, option, min, max, align, len, flags);
232 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
233 unsigned char *p, int size,
234 struct pnp_option *option)
236 resource_size_t min, max, align, len;
237 unsigned char flags;
239 min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
240 max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
241 align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
242 len = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
243 flags = p[3];
244 pnp_register_mem_resource(dev, option, min, max, align, len, flags);
247 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
248 unsigned char *p, int size,
249 struct pnp_option *option)
251 resource_size_t base, len;
252 unsigned char flags;
254 base = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
255 len = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
256 flags = p[3];
257 pnp_register_mem_resource(dev, option, base, base, 0, len, flags);
260 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
261 unsigned char *p, int size,
262 struct pnp_option *option)
264 unsigned long bits;
265 pnp_irq_mask_t map;
266 unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
268 bits = (p[2] << 8) | p[1];
270 bitmap_zero(map.bits, PNP_IRQ_NR);
271 bitmap_copy(map.bits, &bits, 16);
273 if (size > 2)
274 flags = p[3];
276 pnp_register_irq_resource(dev, option, &map, flags);
279 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
280 unsigned char *p, int size,
281 struct pnp_option *option)
283 pnp_register_dma_resource(dev, option, p[1], p[2]);
286 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
287 unsigned char *p, int size,
288 struct pnp_option *option)
290 resource_size_t min, max, align, len;
291 unsigned char flags;
293 min = (p[3] << 8) | p[2];
294 max = (p[5] << 8) | p[4];
295 align = p[6];
296 len = p[7];
297 flags = p[1] ? IORESOURCE_IO_16BIT_ADDR : 0;
298 pnp_register_port_resource(dev, option, min, max, align, len, flags);
301 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
302 unsigned char *p, int size,
303 struct pnp_option *option)
305 resource_size_t base, len;
307 base = (p[2] << 8) | p[1];
308 len = p[3];
309 pnp_register_port_resource(dev, option, base, base, 0, len,
310 IORESOURCE_IO_FIXED);
313 static __init unsigned char *
314 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
315 struct pnp_dev *dev)
317 unsigned int len, tag;
318 int priority = 0;
319 struct pnp_option *option, *option_independent;
321 if (!p)
322 return NULL;
324 dev_dbg(&dev->dev, "parse resource options\n");
326 option_independent = option = pnp_register_independent_option(dev);
327 if (!option)
328 return NULL;
330 while ((char *)p < (char *)end) {
332 /* determine the type of tag */
333 if (p[0] & LARGE_TAG) { /* large tag */
334 len = (p[2] << 8) | p[1];
335 tag = p[0];
336 } else { /* small tag */
337 len = p[0] & 0x07;
338 tag = ((p[0] >> 3) & 0x0f);
341 switch (tag) {
343 case LARGE_TAG_MEM:
344 if (len != 9)
345 goto len_err;
346 pnpbios_parse_mem_option(dev, p, len, option);
347 break;
349 case LARGE_TAG_MEM32:
350 if (len != 17)
351 goto len_err;
352 pnpbios_parse_mem32_option(dev, p, len, option);
353 break;
355 case LARGE_TAG_FIXEDMEM32:
356 if (len != 9)
357 goto len_err;
358 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
359 break;
361 case SMALL_TAG_IRQ:
362 if (len < 2 || len > 3)
363 goto len_err;
364 pnpbios_parse_irq_option(dev, p, len, option);
365 break;
367 case SMALL_TAG_DMA:
368 if (len != 2)
369 goto len_err;
370 pnpbios_parse_dma_option(dev, p, len, option);
371 break;
373 case SMALL_TAG_PORT:
374 if (len != 7)
375 goto len_err;
376 pnpbios_parse_port_option(dev, p, len, option);
377 break;
379 case SMALL_TAG_VENDOR:
380 /* do nothing */
381 break;
383 case SMALL_TAG_FIXEDPORT:
384 if (len != 3)
385 goto len_err;
386 pnpbios_parse_fixed_port_option(dev, p, len, option);
387 break;
389 case SMALL_TAG_STARTDEP:
390 if (len > 1)
391 goto len_err;
392 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
393 if (len > 0)
394 priority = 0x100 | p[1];
395 option = pnp_register_dependent_option(dev, priority);
396 if (!option)
397 return NULL;
398 break;
400 case SMALL_TAG_ENDDEP:
401 if (len != 0)
402 goto len_err;
403 if (option_independent == option)
404 dev_warn(&dev->dev, "missing "
405 "SMALL_TAG_STARTDEP tag\n");
406 option = option_independent;
407 dev_dbg(&dev->dev, "end dependent options\n");
408 break;
410 case SMALL_TAG_END:
411 return p + 2;
413 default: /* an unkown tag */
414 len_err:
415 dev_err(&dev->dev, "unknown tag %#x length %d\n",
416 tag, len);
417 break;
420 /* continue to the next tag */
421 if (p[0] & LARGE_TAG)
422 p += len + 3;
423 else
424 p += len + 1;
427 dev_err(&dev->dev, "no end tag in resource structure\n");
429 return NULL;
433 * Compatible Device IDs
436 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
437 unsigned char *end,
438 struct pnp_dev *dev)
440 int len, tag;
441 u32 eisa_id;
442 char id[8];
443 struct pnp_id *dev_id;
445 if (!p)
446 return NULL;
448 while ((char *)p < (char *)end) {
450 /* determine the type of tag */
451 if (p[0] & LARGE_TAG) { /* large tag */
452 len = (p[2] << 8) | p[1];
453 tag = p[0];
454 } else { /* small tag */
455 len = p[0] & 0x07;
456 tag = ((p[0] >> 3) & 0x0f);
459 switch (tag) {
461 case LARGE_TAG_ANSISTR:
462 strncpy(dev->name, p + 3,
463 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
464 dev->name[len >=
465 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
466 break;
468 case SMALL_TAG_COMPATDEVID: /* compatible ID */
469 if (len != 4)
470 goto len_err;
471 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
472 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
473 dev_id = pnp_add_id(dev, id);
474 if (!dev_id)
475 return NULL;
476 break;
478 case SMALL_TAG_END:
479 p = p + 2;
480 return (unsigned char *)p;
481 break;
483 default: /* an unkown tag */
484 len_err:
485 dev_err(&dev->dev, "unknown tag %#x length %d\n",
486 tag, len);
487 break;
490 /* continue to the next tag */
491 if (p[0] & LARGE_TAG)
492 p += len + 3;
493 else
494 p += len + 1;
497 dev_err(&dev->dev, "no end tag in resource structure\n");
499 return NULL;
503 * Allocated Resource Encoding
506 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
507 struct resource *res)
509 unsigned long base;
510 unsigned long len;
512 if (pnp_resource_enabled(res)) {
513 base = res->start;
514 len = res->end - res->start + 1;
515 } else {
516 base = 0;
517 len = 0;
520 p[4] = (base >> 8) & 0xff;
521 p[5] = ((base >> 8) >> 8) & 0xff;
522 p[6] = (base >> 8) & 0xff;
523 p[7] = ((base >> 8) >> 8) & 0xff;
524 p[10] = (len >> 8) & 0xff;
525 p[11] = ((len >> 8) >> 8) & 0xff;
527 dev_dbg(&dev->dev, " encode mem %#lx-%#lx\n", base, base + len - 1);
530 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
531 struct resource *res)
533 unsigned long base;
534 unsigned long len;
536 if (pnp_resource_enabled(res)) {
537 base = res->start;
538 len = res->end - res->start + 1;
539 } else {
540 base = 0;
541 len = 0;
544 p[4] = base & 0xff;
545 p[5] = (base >> 8) & 0xff;
546 p[6] = (base >> 16) & 0xff;
547 p[7] = (base >> 24) & 0xff;
548 p[8] = base & 0xff;
549 p[9] = (base >> 8) & 0xff;
550 p[10] = (base >> 16) & 0xff;
551 p[11] = (base >> 24) & 0xff;
552 p[16] = len & 0xff;
553 p[17] = (len >> 8) & 0xff;
554 p[18] = (len >> 16) & 0xff;
555 p[19] = (len >> 24) & 0xff;
557 dev_dbg(&dev->dev, " encode mem32 %#lx-%#lx\n", base, base + len - 1);
560 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
561 struct resource *res)
563 unsigned long base;
564 unsigned long len;
566 if (pnp_resource_enabled(res)) {
567 base = res->start;
568 len = res->end - res->start + 1;
569 } else {
570 base = 0;
571 len = 0;
574 p[4] = base & 0xff;
575 p[5] = (base >> 8) & 0xff;
576 p[6] = (base >> 16) & 0xff;
577 p[7] = (base >> 24) & 0xff;
578 p[8] = len & 0xff;
579 p[9] = (len >> 8) & 0xff;
580 p[10] = (len >> 16) & 0xff;
581 p[11] = (len >> 24) & 0xff;
583 dev_dbg(&dev->dev, " encode fixed_mem32 %#lx-%#lx\n", base,
584 base + len - 1);
587 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
588 struct resource *res)
590 unsigned long map;
592 if (pnp_resource_enabled(res))
593 map = 1 << res->start;
594 else
595 map = 0;
597 p[1] = map & 0xff;
598 p[2] = (map >> 8) & 0xff;
600 dev_dbg(&dev->dev, " encode irq mask %#lx\n", map);
603 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
604 struct resource *res)
606 unsigned long map;
608 if (pnp_resource_enabled(res))
609 map = 1 << res->start;
610 else
611 map = 0;
613 p[1] = map & 0xff;
615 dev_dbg(&dev->dev, " encode dma mask %#lx\n", map);
618 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
619 struct resource *res)
621 unsigned long base;
622 unsigned long len;
624 if (pnp_resource_enabled(res)) {
625 base = res->start;
626 len = res->end - res->start + 1;
627 } else {
628 base = 0;
629 len = 0;
632 p[2] = base & 0xff;
633 p[3] = (base >> 8) & 0xff;
634 p[4] = base & 0xff;
635 p[5] = (base >> 8) & 0xff;
636 p[7] = len & 0xff;
638 dev_dbg(&dev->dev, " encode io %#lx-%#lx\n", base, base + len - 1);
641 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
642 struct resource *res)
644 unsigned long base = res->start;
645 unsigned long len = res->end - res->start + 1;
647 if (pnp_resource_enabled(res)) {
648 base = res->start;
649 len = res->end - res->start + 1;
650 } else {
651 base = 0;
652 len = 0;
655 p[1] = base & 0xff;
656 p[2] = (base >> 8) & 0xff;
657 p[3] = len & 0xff;
659 dev_dbg(&dev->dev, " encode fixed_io %#lx-%#lx\n", base,
660 base + len - 1);
663 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
664 *dev,
665 unsigned char *p,
666 unsigned char *end)
668 unsigned int len, tag;
669 int port = 0, irq = 0, dma = 0, mem = 0;
671 if (!p)
672 return NULL;
674 while ((char *)p < (char *)end) {
676 /* determine the type of tag */
677 if (p[0] & LARGE_TAG) { /* large tag */
678 len = (p[2] << 8) | p[1];
679 tag = p[0];
680 } else { /* small tag */
681 len = p[0] & 0x07;
682 tag = ((p[0] >> 3) & 0x0f);
685 switch (tag) {
687 case LARGE_TAG_MEM:
688 if (len != 9)
689 goto len_err;
690 pnpbios_encode_mem(dev, p,
691 pnp_get_resource(dev, IORESOURCE_MEM, mem));
692 mem++;
693 break;
695 case LARGE_TAG_MEM32:
696 if (len != 17)
697 goto len_err;
698 pnpbios_encode_mem32(dev, p,
699 pnp_get_resource(dev, IORESOURCE_MEM, mem));
700 mem++;
701 break;
703 case LARGE_TAG_FIXEDMEM32:
704 if (len != 9)
705 goto len_err;
706 pnpbios_encode_fixed_mem32(dev, p,
707 pnp_get_resource(dev, IORESOURCE_MEM, mem));
708 mem++;
709 break;
711 case SMALL_TAG_IRQ:
712 if (len < 2 || len > 3)
713 goto len_err;
714 pnpbios_encode_irq(dev, p,
715 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
716 irq++;
717 break;
719 case SMALL_TAG_DMA:
720 if (len != 2)
721 goto len_err;
722 pnpbios_encode_dma(dev, p,
723 pnp_get_resource(dev, IORESOURCE_DMA, dma));
724 dma++;
725 break;
727 case SMALL_TAG_PORT:
728 if (len != 7)
729 goto len_err;
730 pnpbios_encode_port(dev, p,
731 pnp_get_resource(dev, IORESOURCE_IO, port));
732 port++;
733 break;
735 case SMALL_TAG_VENDOR:
736 /* do nothing */
737 break;
739 case SMALL_TAG_FIXEDPORT:
740 if (len != 3)
741 goto len_err;
742 pnpbios_encode_fixed_port(dev, p,
743 pnp_get_resource(dev, IORESOURCE_IO, port));
744 port++;
745 break;
747 case SMALL_TAG_END:
748 p = p + 2;
749 return (unsigned char *)p;
750 break;
752 default: /* an unkown tag */
753 len_err:
754 dev_err(&dev->dev, "unknown tag %#x length %d\n",
755 tag, len);
756 break;
759 /* continue to the next tag */
760 if (p[0] & LARGE_TAG)
761 p += len + 3;
762 else
763 p += len + 1;
766 dev_err(&dev->dev, "no end tag in resource structure\n");
768 return NULL;
772 * Core Parsing Functions
775 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
776 struct pnp_bios_node *node)
778 unsigned char *p = (char *)node->data;
779 unsigned char *end = (char *)(node->data + node->size);
781 p = pnpbios_parse_allocated_resource_data(dev, p, end);
782 if (!p)
783 return -EIO;
784 p = pnpbios_parse_resource_option_data(p, end, dev);
785 if (!p)
786 return -EIO;
787 p = pnpbios_parse_compatible_ids(p, end, dev);
788 if (!p)
789 return -EIO;
790 return 0;
793 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
794 struct pnp_bios_node *node)
796 unsigned char *p = (char *)node->data;
797 unsigned char *end = (char *)(node->data + node->size);
799 p = pnpbios_parse_allocated_resource_data(dev, p, end);
800 if (!p)
801 return -EIO;
802 return 0;
805 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
806 struct pnp_bios_node *node)
808 unsigned char *p = (char *)node->data;
809 unsigned char *end = (char *)(node->data + node->size);
811 p = pnpbios_encode_allocated_resource_data(dev, p, end);
812 if (!p)
813 return -EIO;
814 return 0;