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)
223 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
226 mem->min = ((p[5] << 8) | p[4]) << 8;
227 mem->max = ((p[7] << 8) | p[6]) << 8;
228 mem->align = (p[9] << 8) | p[8];
229 mem->size = ((p[11] << 8) | p[10]) << 8;
231 pnp_register_mem_resource(dev, option, mem);
234 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
235 unsigned char *p, int size,
236 struct pnp_option *option)
240 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
243 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
244 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
245 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
246 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
248 pnp_register_mem_resource(dev, option, mem);
251 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
252 unsigned char *p, int size,
253 struct pnp_option *option)
257 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
260 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
261 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
264 pnp_register_mem_resource(dev, option, mem);
267 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
268 unsigned char *p, int size,
269 struct pnp_option *option)
274 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
277 bits = (p[2] << 8) | p[1];
278 bitmap_copy(irq->map, &bits, 16);
282 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
283 pnp_register_irq_resource(dev, option, irq);
286 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
287 unsigned char *p, int size,
288 struct pnp_option *option)
292 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
297 pnp_register_dma_resource(dev, option, dma);
300 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
301 unsigned char *p, int size,
302 struct pnp_option *option)
304 struct pnp_port *port;
306 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
309 port->min = (p[3] << 8) | p[2];
310 port->max = (p[5] << 8) | p[4];
313 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
314 pnp_register_port_resource(dev, option, port);
317 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
318 unsigned char *p, int size,
319 struct pnp_option *option)
321 struct pnp_port *port;
323 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
326 port->min = port->max = (p[2] << 8) | p[1];
329 port->flags = PNP_PORT_FLAG_FIXED;
330 pnp_register_port_resource(dev, option, port);
333 static __init unsigned char *
334 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
337 unsigned int len, tag;
339 struct pnp_option *option, *option_independent;
344 dev_dbg(&dev->dev, "parse resource options\n");
346 option_independent = option = pnp_register_independent_option(dev);
350 while ((char *)p < (char *)end) {
352 /* determine the type of tag */
353 if (p[0] & LARGE_TAG) { /* large tag */
354 len = (p[2] << 8) | p[1];
356 } else { /* small tag */
358 tag = ((p[0] >> 3) & 0x0f);
366 pnpbios_parse_mem_option(dev, p, len, option);
369 case LARGE_TAG_MEM32:
372 pnpbios_parse_mem32_option(dev, p, len, option);
375 case LARGE_TAG_FIXEDMEM32:
378 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
382 if (len < 2 || len > 3)
384 pnpbios_parse_irq_option(dev, p, len, option);
390 pnpbios_parse_dma_option(dev, p, len, option);
396 pnpbios_parse_port_option(dev, p, len, option);
399 case SMALL_TAG_VENDOR:
403 case SMALL_TAG_FIXEDPORT:
406 pnpbios_parse_fixed_port_option(dev, p, len, option);
409 case SMALL_TAG_STARTDEP:
412 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
414 priority = 0x100 | p[1];
415 option = pnp_register_dependent_option(dev, priority);
420 case SMALL_TAG_ENDDEP:
423 if (option_independent == option)
424 dev_warn(&dev->dev, "missing "
425 "SMALL_TAG_STARTDEP tag\n");
426 option = option_independent;
427 dev_dbg(&dev->dev, "end dependent options\n");
433 default: /* an unkown tag */
435 dev_err(&dev->dev, "unknown tag %#x length %d\n",
440 /* continue to the next tag */
441 if (p[0] & LARGE_TAG)
447 dev_err(&dev->dev, "no end tag in resource structure\n");
453 * Compatible Device IDs
456 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
463 struct pnp_id *dev_id;
468 while ((char *)p < (char *)end) {
470 /* determine the type of tag */
471 if (p[0] & LARGE_TAG) { /* large tag */
472 len = (p[2] << 8) | p[1];
474 } else { /* small tag */
476 tag = ((p[0] >> 3) & 0x0f);
481 case LARGE_TAG_ANSISTR:
482 strncpy(dev->name, p + 3,
483 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
485 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
488 case SMALL_TAG_COMPATDEVID: /* compatible ID */
491 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
492 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
493 dev_id = pnp_add_id(dev, id);
500 return (unsigned char *)p;
503 default: /* an unkown tag */
505 dev_err(&dev->dev, "unknown tag %#x length %d\n",
510 /* continue to the next tag */
511 if (p[0] & LARGE_TAG)
517 dev_err(&dev->dev, "no end tag in resource structure\n");
523 * Allocated Resource Encoding
526 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
527 struct resource *res)
529 unsigned long base = res->start;
530 unsigned long len = res->end - res->start + 1;
532 p[4] = (base >> 8) & 0xff;
533 p[5] = ((base >> 8) >> 8) & 0xff;
534 p[6] = (base >> 8) & 0xff;
535 p[7] = ((base >> 8) >> 8) & 0xff;
536 p[10] = (len >> 8) & 0xff;
537 p[11] = ((len >> 8) >> 8) & 0xff;
539 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n",
540 (unsigned long long) res->start, (unsigned long long) res->end);
543 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
544 struct resource *res)
546 unsigned long base = res->start;
547 unsigned long len = res->end - res->start + 1;
550 p[5] = (base >> 8) & 0xff;
551 p[6] = (base >> 16) & 0xff;
552 p[7] = (base >> 24) & 0xff;
554 p[9] = (base >> 8) & 0xff;
555 p[10] = (base >> 16) & 0xff;
556 p[11] = (base >> 24) & 0xff;
558 p[17] = (len >> 8) & 0xff;
559 p[18] = (len >> 16) & 0xff;
560 p[19] = (len >> 24) & 0xff;
562 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n",
563 (unsigned long long) res->start, (unsigned long long) res->end);
566 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
567 struct resource *res)
569 unsigned long base = res->start;
570 unsigned long len = res->end - res->start + 1;
573 p[5] = (base >> 8) & 0xff;
574 p[6] = (base >> 16) & 0xff;
575 p[7] = (base >> 24) & 0xff;
577 p[9] = (len >> 8) & 0xff;
578 p[10] = (len >> 16) & 0xff;
579 p[11] = (len >> 24) & 0xff;
581 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n",
582 (unsigned long long) res->start, (unsigned long long) res->end);
585 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
586 struct resource *res)
588 unsigned long map = 0;
590 map = 1 << res->start;
592 p[2] = (map >> 8) & 0xff;
594 dev_dbg(&dev->dev, " encode irq %llu\n",
595 (unsigned long long)res->start);
598 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
599 struct resource *res)
601 unsigned long map = 0;
603 map = 1 << res->start;
606 dev_dbg(&dev->dev, " encode dma %llu\n",
607 (unsigned long long)res->start);
610 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
611 struct resource *res)
613 unsigned long base = res->start;
614 unsigned long len = res->end - res->start + 1;
617 p[3] = (base >> 8) & 0xff;
619 p[5] = (base >> 8) & 0xff;
622 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n",
623 (unsigned long long) res->start, (unsigned long long) res->end);
626 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
627 struct resource *res)
629 unsigned long base = res->start;
630 unsigned long len = res->end - res->start + 1;
633 p[2] = (base >> 8) & 0xff;
636 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
637 (unsigned long long) res->start, (unsigned long long) res->end);
640 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
645 unsigned int len, tag;
646 int port = 0, irq = 0, dma = 0, mem = 0;
651 while ((char *)p < (char *)end) {
653 /* determine the type of tag */
654 if (p[0] & LARGE_TAG) { /* large tag */
655 len = (p[2] << 8) | p[1];
657 } else { /* small tag */
659 tag = ((p[0] >> 3) & 0x0f);
667 pnpbios_encode_mem(dev, p,
668 pnp_get_resource(dev, IORESOURCE_MEM, mem));
672 case LARGE_TAG_MEM32:
675 pnpbios_encode_mem32(dev, p,
676 pnp_get_resource(dev, IORESOURCE_MEM, mem));
680 case LARGE_TAG_FIXEDMEM32:
683 pnpbios_encode_fixed_mem32(dev, p,
684 pnp_get_resource(dev, IORESOURCE_MEM, mem));
689 if (len < 2 || len > 3)
691 pnpbios_encode_irq(dev, p,
692 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
699 pnpbios_encode_dma(dev, p,
700 pnp_get_resource(dev, IORESOURCE_DMA, dma));
707 pnpbios_encode_port(dev, p,
708 pnp_get_resource(dev, IORESOURCE_IO, port));
712 case SMALL_TAG_VENDOR:
716 case SMALL_TAG_FIXEDPORT:
719 pnpbios_encode_fixed_port(dev, p,
720 pnp_get_resource(dev, IORESOURCE_IO, port));
726 return (unsigned char *)p;
729 default: /* an unkown tag */
731 dev_err(&dev->dev, "unknown tag %#x length %d\n",
736 /* continue to the next tag */
737 if (p[0] & LARGE_TAG)
743 dev_err(&dev->dev, "no end tag in resource structure\n");
749 * Core Parsing Functions
752 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
753 struct pnp_bios_node *node)
755 unsigned char *p = (char *)node->data;
756 unsigned char *end = (char *)(node->data + node->size);
758 p = pnpbios_parse_allocated_resource_data(dev, p, end);
761 p = pnpbios_parse_resource_option_data(p, end, dev);
764 p = pnpbios_parse_compatible_ids(p, end, dev);
770 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
771 struct pnp_bios_node *node)
773 unsigned char *p = (char *)node->data;
774 unsigned char *end = (char *)(node->data + node->size);
776 p = pnpbios_parse_allocated_resource_data(dev, p, end);
782 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
783 struct pnp_bios_node *node)
785 unsigned char *p = (char *)node->data;
786 unsigned char *end = (char *)(node->data + node->size);
788 p = pnpbios_encode_allocated_resource_data(dev, p, end);