2 * rsparser.c - parses and encodes pnpbios resource data streams
5 #include <linux/ctype.h>
7 #include <linux/pnpbios.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
12 #include <linux/pci.h>
14 inline void pcibios_penalize_isa_irq(int irq, int active)
17 #endif /* CONFIG_PCI */
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
43 * Resource Data Stream Format:
45 * Allocated Resources (required)
47 * Resource Configuration Options (optional)
49 * Compitable Device IDs (optional)
57 static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
63 for (i = 0; i < PNP_MAX_PORT; i++) {
64 res = pnp_get_resource(dev, IORESOURCE_IO, i);
65 if (!pnp_resource_valid(res))
69 if (i < PNP_MAX_PORT) {
70 res->flags = IORESOURCE_IO; // Also clears _UNSET flag
71 if (len <= 0 || (io + len - 1) >= 0x10003) {
72 res->flags |= IORESOURCE_DISABLED;
75 res->start = (unsigned long)io;
76 res->end = (unsigned long)(io + len - 1);
80 static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
86 for (i = 0; i < PNP_MAX_MEM; i++) {
87 res = pnp_get_resource(dev, IORESOURCE_MEM, i);
88 if (!pnp_resource_valid(res))
92 if (i < PNP_MAX_MEM) {
93 res->flags = IORESOURCE_MEM; // Also clears _UNSET flag
95 res->flags |= IORESOURCE_DISABLED;
98 res->start = (unsigned long)mem;
99 res->end = (unsigned long)(mem + len - 1);
103 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
107 unsigned int len, tag;
108 int io, size, mask, i, flags;
113 dev_dbg(&dev->dev, "parse allocated resources\n");
115 pnp_init_resources(dev);
117 while ((char *)p < (char *)end) {
119 /* determine the type of tag */
120 if (p[0] & LARGE_TAG) { /* large tag */
121 len = (p[2] << 8) | p[1];
123 } else { /* small tag */
125 tag = ((p[0] >> 3) & 0x0f);
133 io = *(short *)&p[4];
134 size = *(short *)&p[10];
135 pnpbios_parse_allocated_memresource(dev, io, size);
138 case LARGE_TAG_ANSISTR:
139 /* ignore this for now */
142 case LARGE_TAG_VENDOR:
146 case LARGE_TAG_MEM32:
150 size = *(int *)&p[16];
151 pnpbios_parse_allocated_memresource(dev, io, size);
154 case LARGE_TAG_FIXEDMEM32:
158 size = *(int *)&p[8];
159 pnpbios_parse_allocated_memresource(dev, io, size);
163 if (len < 2 || len > 3)
167 mask = p[1] + p[2] * 256;
168 for (i = 0; i < 16; i++, mask = mask >> 1)
172 pcibios_penalize_isa_irq(io, 1);
174 flags = IORESOURCE_DISABLED;
175 pnp_add_irq_resource(dev, io, flags);
184 for (i = 0; i < 8; i++, mask = mask >> 1)
188 flags = IORESOURCE_DISABLED;
189 pnp_add_dma_resource(dev, io, flags);
195 io = p[2] + p[3] * 256;
197 pnpbios_parse_allocated_ioresource(dev, io, size);
200 case SMALL_TAG_VENDOR:
204 case SMALL_TAG_FIXEDPORT:
207 io = p[1] + p[2] * 256;
209 pnpbios_parse_allocated_ioresource(dev, io, size);
214 return (unsigned char *)p;
217 default: /* an unkown tag */
219 dev_err(&dev->dev, "unknown tag %#x length %d\n",
224 /* continue to the next tag */
225 if (p[0] & LARGE_TAG)
231 dev_err(&dev->dev, "no end tag in resource structure\n");
237 * Resource Configuration Options
240 static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
241 unsigned char *p, int size,
242 struct pnp_option *option)
246 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
249 mem->min = ((p[5] << 8) | p[4]) << 8;
250 mem->max = ((p[7] << 8) | p[6]) << 8;
251 mem->align = (p[9] << 8) | p[8];
252 mem->size = ((p[11] << 8) | p[10]) << 8;
254 pnp_register_mem_resource(dev, option, mem);
257 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
258 unsigned char *p, int size,
259 struct pnp_option *option)
263 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
266 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
267 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
268 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
269 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
271 pnp_register_mem_resource(dev, option, mem);
274 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
275 unsigned char *p, int size,
276 struct pnp_option *option)
280 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
283 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
284 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
287 pnp_register_mem_resource(dev, option, mem);
290 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
291 unsigned char *p, int size,
292 struct pnp_option *option)
297 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
300 bits = (p[2] << 8) | p[1];
301 bitmap_copy(irq->map, &bits, 16);
305 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
306 pnp_register_irq_resource(dev, option, irq);
309 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
310 unsigned char *p, int size,
311 struct pnp_option *option)
315 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
320 pnp_register_dma_resource(dev, option, dma);
323 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
324 unsigned char *p, int size,
325 struct pnp_option *option)
327 struct pnp_port *port;
329 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
332 port->min = (p[3] << 8) | p[2];
333 port->max = (p[5] << 8) | p[4];
336 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
337 pnp_register_port_resource(dev, option, port);
340 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
341 unsigned char *p, int size,
342 struct pnp_option *option)
344 struct pnp_port *port;
346 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
349 port->min = port->max = (p[2] << 8) | p[1];
352 port->flags = PNP_PORT_FLAG_FIXED;
353 pnp_register_port_resource(dev, option, port);
356 static __init unsigned char *
357 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
360 unsigned int len, tag;
362 struct pnp_option *option, *option_independent;
367 dev_dbg(&dev->dev, "parse resource options\n");
369 option_independent = option = pnp_register_independent_option(dev);
373 while ((char *)p < (char *)end) {
375 /* determine the type of tag */
376 if (p[0] & LARGE_TAG) { /* large tag */
377 len = (p[2] << 8) | p[1];
379 } else { /* small tag */
381 tag = ((p[0] >> 3) & 0x0f);
389 pnpbios_parse_mem_option(dev, p, len, option);
392 case LARGE_TAG_MEM32:
395 pnpbios_parse_mem32_option(dev, p, len, option);
398 case LARGE_TAG_FIXEDMEM32:
401 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
405 if (len < 2 || len > 3)
407 pnpbios_parse_irq_option(dev, p, len, option);
413 pnpbios_parse_dma_option(dev, p, len, option);
419 pnpbios_parse_port_option(dev, p, len, option);
422 case SMALL_TAG_VENDOR:
426 case SMALL_TAG_FIXEDPORT:
429 pnpbios_parse_fixed_port_option(dev, p, len, option);
432 case SMALL_TAG_STARTDEP:
435 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
437 priority = 0x100 | p[1];
438 option = pnp_register_dependent_option(dev, priority);
443 case SMALL_TAG_ENDDEP:
446 if (option_independent == option)
447 dev_warn(&dev->dev, "missing "
448 "SMALL_TAG_STARTDEP tag\n");
449 option = option_independent;
450 dev_dbg(&dev->dev, "end dependent options\n");
456 default: /* an unkown tag */
458 dev_err(&dev->dev, "unknown tag %#x length %d\n",
463 /* continue to the next tag */
464 if (p[0] & LARGE_TAG)
470 dev_err(&dev->dev, "no end tag in resource structure\n");
476 * Compatible Device IDs
479 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
486 struct pnp_id *dev_id;
491 while ((char *)p < (char *)end) {
493 /* determine the type of tag */
494 if (p[0] & LARGE_TAG) { /* large tag */
495 len = (p[2] << 8) | p[1];
497 } else { /* small tag */
499 tag = ((p[0] >> 3) & 0x0f);
504 case LARGE_TAG_ANSISTR:
505 strncpy(dev->name, p + 3,
506 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
508 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
511 case SMALL_TAG_COMPATDEVID: /* compatible ID */
514 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
515 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
516 dev_id = pnp_add_id(dev, id);
523 return (unsigned char *)p;
526 default: /* an unkown tag */
528 dev_err(&dev->dev, "unknown tag %#x length %d\n",
533 /* continue to the next tag */
534 if (p[0] & LARGE_TAG)
540 dev_err(&dev->dev, "no end tag in resource structure\n");
546 * Allocated Resource Encoding
549 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
550 struct resource *res)
552 unsigned long base = res->start;
553 unsigned long len = res->end - res->start + 1;
555 p[4] = (base >> 8) & 0xff;
556 p[5] = ((base >> 8) >> 8) & 0xff;
557 p[6] = (base >> 8) & 0xff;
558 p[7] = ((base >> 8) >> 8) & 0xff;
559 p[10] = (len >> 8) & 0xff;
560 p[11] = ((len >> 8) >> 8) & 0xff;
562 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n",
563 (unsigned long long) res->start, (unsigned long long) res->end);
566 static void pnpbios_encode_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] = (base >> 8) & 0xff;
578 p[10] = (base >> 16) & 0xff;
579 p[11] = (base >> 24) & 0xff;
581 p[17] = (len >> 8) & 0xff;
582 p[18] = (len >> 16) & 0xff;
583 p[19] = (len >> 24) & 0xff;
585 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n",
586 (unsigned long long) res->start, (unsigned long long) res->end);
589 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
590 struct resource *res)
592 unsigned long base = res->start;
593 unsigned long len = res->end - res->start + 1;
596 p[5] = (base >> 8) & 0xff;
597 p[6] = (base >> 16) & 0xff;
598 p[7] = (base >> 24) & 0xff;
600 p[9] = (len >> 8) & 0xff;
601 p[10] = (len >> 16) & 0xff;
602 p[11] = (len >> 24) & 0xff;
604 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n",
605 (unsigned long long) res->start, (unsigned long long) res->end);
608 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
609 struct resource *res)
611 unsigned long map = 0;
613 map = 1 << res->start;
615 p[2] = (map >> 8) & 0xff;
617 dev_dbg(&dev->dev, " encode irq %d\n", res->start);
620 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
621 struct resource *res)
623 unsigned long map = 0;
625 map = 1 << res->start;
628 dev_dbg(&dev->dev, " encode dma %d\n", res->start);
631 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
632 struct resource *res)
634 unsigned long base = res->start;
635 unsigned long len = res->end - res->start + 1;
638 p[3] = (base >> 8) & 0xff;
640 p[5] = (base >> 8) & 0xff;
643 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n",
644 (unsigned long long) res->start, (unsigned long long) res->end);
647 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
648 struct resource *res)
650 unsigned long base = res->start;
651 unsigned long len = res->end - res->start + 1;
654 p[2] = (base >> 8) & 0xff;
657 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
658 (unsigned long long) res->start, (unsigned long long) res->end);
661 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
666 unsigned int len, tag;
667 int port = 0, irq = 0, dma = 0, mem = 0;
672 while ((char *)p < (char *)end) {
674 /* determine the type of tag */
675 if (p[0] & LARGE_TAG) { /* large tag */
676 len = (p[2] << 8) | p[1];
678 } else { /* small tag */
680 tag = ((p[0] >> 3) & 0x0f);
688 pnpbios_encode_mem(dev, p,
689 pnp_get_resource(dev, IORESOURCE_MEM, mem));
693 case LARGE_TAG_MEM32:
696 pnpbios_encode_mem32(dev, p,
697 pnp_get_resource(dev, IORESOURCE_MEM, mem));
701 case LARGE_TAG_FIXEDMEM32:
704 pnpbios_encode_fixed_mem32(dev, p,
705 pnp_get_resource(dev, IORESOURCE_MEM, mem));
710 if (len < 2 || len > 3)
712 pnpbios_encode_irq(dev, p,
713 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
720 pnpbios_encode_dma(dev, p,
721 pnp_get_resource(dev, IORESOURCE_DMA, dma));
728 pnpbios_encode_port(dev, p,
729 pnp_get_resource(dev, IORESOURCE_IO, port));
733 case SMALL_TAG_VENDOR:
737 case SMALL_TAG_FIXEDPORT:
740 pnpbios_encode_fixed_port(dev, p,
741 pnp_get_resource(dev, IORESOURCE_IO, port));
747 return (unsigned char *)p;
750 default: /* an unkown tag */
752 dev_err(&dev->dev, "unknown tag %#x length %d\n",
757 /* continue to the next tag */
758 if (p[0] & LARGE_TAG)
764 dev_err(&dev->dev, "no end tag in resource structure\n");
770 * Core Parsing Functions
773 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
774 struct pnp_bios_node *node)
776 unsigned char *p = (char *)node->data;
777 unsigned char *end = (char *)(node->data + node->size);
779 p = pnpbios_parse_allocated_resource_data(dev, p, end);
782 p = pnpbios_parse_resource_option_data(p, end, dev);
785 p = pnpbios_parse_compatible_ids(p, end, dev);
791 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
792 struct pnp_bios_node *node)
794 unsigned char *p = (char *)node->data;
795 unsigned char *end = (char *)(node->data + node->size);
797 p = pnpbios_parse_allocated_resource_data(dev, p, end);
803 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
804 struct pnp_bios_node *node)
806 unsigned char *p = (char *)node->data;
807 unsigned char *end = (char *)(node->data + node->size);
809 p = pnpbios_encode_allocated_resource_data(dev, p, end);