2 * rsparser.c - parses and encodes pnpbios resource data streams
5 #include <linux/ctype.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
11 #include <linux/pci.h>
13 inline void pcibios_penalize_isa_irq(int irq, int active)
16 #endif /* CONFIG_PCI */
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)
46 * Resource Configuration Options (optional)
48 * Compitable Device IDs (optional)
56 static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
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,
72 int end = start + len - 1;
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,
84 unsigned int len, tag;
85 int io, size, mask, i, flags;
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];
100 } else { /* small tag */
102 tag = ((p[0] >> 3) & 0x0f);
110 io = *(short *)&p[4];
111 size = *(short *)&p[10];
112 pnpbios_parse_allocated_memresource(dev, io, size);
115 case LARGE_TAG_ANSISTR:
116 /* ignore this for now */
119 case LARGE_TAG_VENDOR:
123 case LARGE_TAG_MEM32:
127 size = *(int *)&p[16];
128 pnpbios_parse_allocated_memresource(dev, io, size);
131 case LARGE_TAG_FIXEDMEM32:
135 size = *(int *)&p[8];
136 pnpbios_parse_allocated_memresource(dev, io, size);
140 if (len < 2 || len > 3)
144 mask = p[1] + p[2] * 256;
145 for (i = 0; i < 16; i++, mask = mask >> 1)
149 pcibios_penalize_isa_irq(io, 1);
151 flags = IORESOURCE_DISABLED;
152 pnp_add_irq_resource(dev, io, flags);
161 for (i = 0; i < 8; i++, mask = mask >> 1)
165 flags = IORESOURCE_DISABLED;
166 pnp_add_dma_resource(dev, io, flags);
172 io = p[2] + p[3] * 256;
174 pnpbios_parse_allocated_ioresource(dev, io, size);
177 case SMALL_TAG_VENDOR:
181 case SMALL_TAG_FIXEDPORT:
184 io = p[1] + p[2] * 256;
186 pnpbios_parse_allocated_ioresource(dev, io, size);
191 return (unsigned char *)p;
194 default: /* an unkown tag */
196 dev_err(&dev->dev, "unknown tag %#x length %d\n",
201 /* continue to the next tag */
202 if (p[0] & LARGE_TAG)
208 dev_err(&dev->dev, "no end tag in resource structure\n");
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;
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;
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;
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];
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;
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];
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)
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);
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;
293 min = (p[3] << 8) | p[2];
294 max = (p[5] << 8) | p[4];
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];
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,
317 unsigned int len, tag;
319 struct pnp_option *option, *option_independent;
324 dev_dbg(&dev->dev, "parse resource options\n");
326 option_independent = option = pnp_register_independent_option(dev);
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];
336 } else { /* small tag */
338 tag = ((p[0] >> 3) & 0x0f);
346 pnpbios_parse_mem_option(dev, p, len, option);
349 case LARGE_TAG_MEM32:
352 pnpbios_parse_mem32_option(dev, p, len, option);
355 case LARGE_TAG_FIXEDMEM32:
358 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
362 if (len < 2 || len > 3)
364 pnpbios_parse_irq_option(dev, p, len, option);
370 pnpbios_parse_dma_option(dev, p, len, option);
376 pnpbios_parse_port_option(dev, p, len, option);
379 case SMALL_TAG_VENDOR:
383 case SMALL_TAG_FIXEDPORT:
386 pnpbios_parse_fixed_port_option(dev, p, len, option);
389 case SMALL_TAG_STARTDEP:
392 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
394 priority = 0x100 | p[1];
395 option = pnp_register_dependent_option(dev, priority);
400 case SMALL_TAG_ENDDEP:
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");
413 default: /* an unkown tag */
415 dev_err(&dev->dev, "unknown tag %#x length %d\n",
420 /* continue to the next tag */
421 if (p[0] & LARGE_TAG)
427 dev_err(&dev->dev, "no end tag in resource structure\n");
433 * Compatible Device IDs
436 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
443 struct pnp_id *dev_id;
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];
454 } else { /* small tag */
456 tag = ((p[0] >> 3) & 0x0f);
461 case LARGE_TAG_ANSISTR:
462 strncpy(dev->name, p + 3,
463 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
465 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
468 case SMALL_TAG_COMPATDEVID: /* compatible ID */
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);
480 return (unsigned char *)p;
483 default: /* an unkown tag */
485 dev_err(&dev->dev, "unknown tag %#x length %d\n",
490 /* continue to the next tag */
491 if (p[0] & LARGE_TAG)
497 dev_err(&dev->dev, "no end tag in resource structure\n");
503 * Allocated Resource Encoding
506 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
507 struct resource *res)
512 if (pnp_resource_enabled(res)) {
514 len = res->end - res->start + 1;
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)
536 if (pnp_resource_enabled(res)) {
538 len = res->end - res->start + 1;
545 p[5] = (base >> 8) & 0xff;
546 p[6] = (base >> 16) & 0xff;
547 p[7] = (base >> 24) & 0xff;
549 p[9] = (base >> 8) & 0xff;
550 p[10] = (base >> 16) & 0xff;
551 p[11] = (base >> 24) & 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)
566 if (pnp_resource_enabled(res)) {
568 len = res->end - res->start + 1;
575 p[5] = (base >> 8) & 0xff;
576 p[6] = (base >> 16) & 0xff;
577 p[7] = (base >> 24) & 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,
587 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
588 struct resource *res)
592 if (pnp_resource_enabled(res))
593 map = 1 << res->start;
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)
608 if (pnp_resource_enabled(res))
609 map = 1 << res->start;
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)
624 if (pnp_resource_enabled(res)) {
626 len = res->end - res->start + 1;
633 p[3] = (base >> 8) & 0xff;
635 p[5] = (base >> 8) & 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)) {
649 len = res->end - res->start + 1;
656 p[2] = (base >> 8) & 0xff;
659 dev_dbg(&dev->dev, " encode fixed_io %#lx-%#lx\n", base,
663 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
668 unsigned int len, tag;
669 int port = 0, irq = 0, dma = 0, mem = 0;
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];
680 } else { /* small tag */
682 tag = ((p[0] >> 3) & 0x0f);
690 pnpbios_encode_mem(dev, p,
691 pnp_get_resource(dev, IORESOURCE_MEM, mem));
695 case LARGE_TAG_MEM32:
698 pnpbios_encode_mem32(dev, p,
699 pnp_get_resource(dev, IORESOURCE_MEM, mem));
703 case LARGE_TAG_FIXEDMEM32:
706 pnpbios_encode_fixed_mem32(dev, p,
707 pnp_get_resource(dev, IORESOURCE_MEM, mem));
712 if (len < 2 || len > 3)
714 pnpbios_encode_irq(dev, p,
715 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
722 pnpbios_encode_dma(dev, p,
723 pnp_get_resource(dev, IORESOURCE_DMA, dma));
730 pnpbios_encode_port(dev, p,
731 pnp_get_resource(dev, IORESOURCE_IO, port));
735 case SMALL_TAG_VENDOR:
739 case SMALL_TAG_FIXEDPORT:
742 pnpbios_encode_fixed_port(dev, p,
743 pnp_get_resource(dev, IORESOURCE_IO, port));
749 return (unsigned char *)p;
752 default: /* an unkown tag */
754 dev_err(&dev->dev, "unknown tag %#x length %d\n",
759 /* continue to the next tag */
760 if (p[0] & LARGE_TAG)
766 dev_err(&dev->dev, "no end tag in resource structure\n");
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);
784 p = pnpbios_parse_resource_option_data(p, end, dev);
787 p = pnpbios_parse_compatible_ids(p, end, dev);
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);
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);