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_irqresource(struct pnp_dev *dev, int irq)
59 struct pnp_resource_table *res = &dev->res;
62 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET)
65 if (i < PNP_MAX_IRQ) {
66 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
68 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
71 res->irq_resource[i].start =
72 res->irq_resource[i].end = (unsigned long)irq;
73 pcibios_penalize_isa_irq(irq, 1);
77 static void pnpbios_parse_allocated_dmaresource(struct pnp_dev *dev, int dma)
79 struct pnp_resource_table *res = &dev->res;
82 while (i < PNP_MAX_DMA &&
83 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
85 if (i < PNP_MAX_DMA) {
86 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
88 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
91 res->dma_resource[i].start =
92 res->dma_resource[i].end = (unsigned long)dma;
96 static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
99 struct pnp_resource_table *res = &dev->res;
102 while (!(res->port_resource[i].flags & IORESOURCE_UNSET)
105 if (i < PNP_MAX_PORT) {
106 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
107 if (len <= 0 || (io + len - 1) >= 0x10003) {
108 res->port_resource[i].flags |= IORESOURCE_DISABLED;
111 res->port_resource[i].start = (unsigned long)io;
112 res->port_resource[i].end = (unsigned long)(io + len - 1);
116 static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
119 struct pnp_resource_table *res = &dev->res;
122 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET)
125 if (i < PNP_MAX_MEM) {
126 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
128 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
131 res->mem_resource[i].start = (unsigned long)mem;
132 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
136 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
140 unsigned int len, tag;
141 int io, size, mask, i;
146 dev_dbg(&dev->dev, "parse allocated resources\n");
148 pnp_init_resources(dev);
150 while ((char *)p < (char *)end) {
152 /* determine the type of tag */
153 if (p[0] & LARGE_TAG) { /* large tag */
154 len = (p[2] << 8) | p[1];
156 } else { /* small tag */
158 tag = ((p[0] >> 3) & 0x0f);
166 io = *(short *)&p[4];
167 size = *(short *)&p[10];
168 pnpbios_parse_allocated_memresource(dev, io, size);
171 case LARGE_TAG_ANSISTR:
172 /* ignore this for now */
175 case LARGE_TAG_VENDOR:
179 case LARGE_TAG_MEM32:
183 size = *(int *)&p[16];
184 pnpbios_parse_allocated_memresource(dev, io, size);
187 case LARGE_TAG_FIXEDMEM32:
191 size = *(int *)&p[8];
192 pnpbios_parse_allocated_memresource(dev, io, size);
196 if (len < 2 || len > 3)
199 mask = p[1] + p[2] * 256;
200 for (i = 0; i < 16; i++, mask = mask >> 1)
203 pnpbios_parse_allocated_irqresource(dev, io);
211 for (i = 0; i < 8; i++, mask = mask >> 1)
214 pnpbios_parse_allocated_dmaresource(dev, io);
220 io = p[2] + p[3] * 256;
222 pnpbios_parse_allocated_ioresource(dev, io, size);
225 case SMALL_TAG_VENDOR:
229 case SMALL_TAG_FIXEDPORT:
232 io = p[1] + p[2] * 256;
234 pnpbios_parse_allocated_ioresource(dev, io, size);
239 return (unsigned char *)p;
242 default: /* an unkown tag */
244 dev_err(&dev->dev, "unknown tag %#x length %d\n",
249 /* continue to the next tag */
250 if (p[0] & LARGE_TAG)
256 dev_err(&dev->dev, "no end tag in resource structure\n");
262 * Resource Configuration Options
265 static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
266 unsigned char *p, int size,
267 struct pnp_option *option)
271 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
274 mem->min = ((p[5] << 8) | p[4]) << 8;
275 mem->max = ((p[7] << 8) | p[6]) << 8;
276 mem->align = (p[9] << 8) | p[8];
277 mem->size = ((p[11] << 8) | p[10]) << 8;
279 pnp_register_mem_resource(dev, option, mem);
282 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
283 unsigned char *p, int size,
284 struct pnp_option *option)
288 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
291 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
292 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
293 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
294 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
296 pnp_register_mem_resource(dev, option, mem);
299 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
300 unsigned char *p, int size,
301 struct pnp_option *option)
305 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
308 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
309 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
312 pnp_register_mem_resource(dev, option, mem);
315 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
316 unsigned char *p, int size,
317 struct pnp_option *option)
322 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
325 bits = (p[2] << 8) | p[1];
326 bitmap_copy(irq->map, &bits, 16);
330 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
331 pnp_register_irq_resource(dev, option, irq);
334 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
335 unsigned char *p, int size,
336 struct pnp_option *option)
340 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
345 pnp_register_dma_resource(dev, option, dma);
348 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
349 unsigned char *p, int size,
350 struct pnp_option *option)
352 struct pnp_port *port;
354 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
357 port->min = (p[3] << 8) | p[2];
358 port->max = (p[5] << 8) | p[4];
361 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
362 pnp_register_port_resource(dev, option, port);
365 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
366 unsigned char *p, int size,
367 struct pnp_option *option)
369 struct pnp_port *port;
371 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
374 port->min = port->max = (p[2] << 8) | p[1];
377 port->flags = PNP_PORT_FLAG_FIXED;
378 pnp_register_port_resource(dev, option, port);
381 static __init unsigned char *
382 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
385 unsigned int len, tag;
387 struct pnp_option *option, *option_independent;
392 dev_dbg(&dev->dev, "parse resource options\n");
394 option_independent = option = pnp_register_independent_option(dev);
398 while ((char *)p < (char *)end) {
400 /* determine the type of tag */
401 if (p[0] & LARGE_TAG) { /* large tag */
402 len = (p[2] << 8) | p[1];
404 } else { /* small tag */
406 tag = ((p[0] >> 3) & 0x0f);
414 pnpbios_parse_mem_option(dev, p, len, option);
417 case LARGE_TAG_MEM32:
420 pnpbios_parse_mem32_option(dev, p, len, option);
423 case LARGE_TAG_FIXEDMEM32:
426 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
430 if (len < 2 || len > 3)
432 pnpbios_parse_irq_option(dev, p, len, option);
438 pnpbios_parse_dma_option(dev, p, len, option);
444 pnpbios_parse_port_option(dev, p, len, option);
447 case SMALL_TAG_VENDOR:
451 case SMALL_TAG_FIXEDPORT:
454 pnpbios_parse_fixed_port_option(dev, p, len, option);
457 case SMALL_TAG_STARTDEP:
460 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
462 priority = 0x100 | p[1];
463 option = pnp_register_dependent_option(dev, priority);
468 case SMALL_TAG_ENDDEP:
471 if (option_independent == option)
472 dev_warn(&dev->dev, "missing "
473 "SMALL_TAG_STARTDEP tag\n");
474 option = option_independent;
475 dev_dbg(&dev->dev, "end dependent options\n");
481 default: /* an unkown tag */
483 dev_err(&dev->dev, "unknown tag %#x length %d\n",
488 /* continue to the next tag */
489 if (p[0] & LARGE_TAG)
495 dev_err(&dev->dev, "no end tag in resource structure\n");
501 * Compatible Device IDs
504 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
511 struct pnp_id *dev_id;
516 while ((char *)p < (char *)end) {
518 /* determine the type of tag */
519 if (p[0] & LARGE_TAG) { /* large tag */
520 len = (p[2] << 8) | p[1];
522 } else { /* small tag */
524 tag = ((p[0] >> 3) & 0x0f);
529 case LARGE_TAG_ANSISTR:
530 strncpy(dev->name, p + 3,
531 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
533 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
536 case SMALL_TAG_COMPATDEVID: /* compatible ID */
539 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
540 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
541 dev_id = pnp_add_id(dev, id);
548 return (unsigned char *)p;
551 default: /* an unkown tag */
553 dev_err(&dev->dev, "unknown tag %#x length %d\n",
558 /* continue to the next tag */
559 if (p[0] & LARGE_TAG)
565 dev_err(&dev->dev, "no end tag in resource structure\n");
571 * Allocated Resource Encoding
574 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
575 struct resource *res)
577 unsigned long base = res->start;
578 unsigned long len = res->end - res->start + 1;
580 p[4] = (base >> 8) & 0xff;
581 p[5] = ((base >> 8) >> 8) & 0xff;
582 p[6] = (base >> 8) & 0xff;
583 p[7] = ((base >> 8) >> 8) & 0xff;
584 p[10] = (len >> 8) & 0xff;
585 p[11] = ((len >> 8) >> 8) & 0xff;
587 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n",
588 (unsigned long long) res->start, (unsigned long long) res->end);
591 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
592 struct resource *res)
594 unsigned long base = res->start;
595 unsigned long len = res->end - res->start + 1;
598 p[5] = (base >> 8) & 0xff;
599 p[6] = (base >> 16) & 0xff;
600 p[7] = (base >> 24) & 0xff;
602 p[9] = (base >> 8) & 0xff;
603 p[10] = (base >> 16) & 0xff;
604 p[11] = (base >> 24) & 0xff;
606 p[17] = (len >> 8) & 0xff;
607 p[18] = (len >> 16) & 0xff;
608 p[19] = (len >> 24) & 0xff;
610 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n",
611 (unsigned long long) res->start, (unsigned long long) res->end);
614 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
615 struct resource *res)
617 unsigned long base = res->start;
618 unsigned long len = res->end - res->start + 1;
621 p[5] = (base >> 8) & 0xff;
622 p[6] = (base >> 16) & 0xff;
623 p[7] = (base >> 24) & 0xff;
625 p[9] = (len >> 8) & 0xff;
626 p[10] = (len >> 16) & 0xff;
627 p[11] = (len >> 24) & 0xff;
629 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n",
630 (unsigned long long) res->start, (unsigned long long) res->end);
633 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
634 struct resource *res)
636 unsigned long map = 0;
638 map = 1 << res->start;
640 p[2] = (map >> 8) & 0xff;
642 dev_dbg(&dev->dev, " encode irq %d\n", res->start);
645 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
646 struct resource *res)
648 unsigned long map = 0;
650 map = 1 << res->start;
653 dev_dbg(&dev->dev, " encode dma %d\n", res->start);
656 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
657 struct resource *res)
659 unsigned long base = res->start;
660 unsigned long len = res->end - res->start + 1;
663 p[3] = (base >> 8) & 0xff;
665 p[5] = (base >> 8) & 0xff;
668 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n",
669 (unsigned long long) res->start, (unsigned long long) res->end);
672 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
673 struct resource *res)
675 unsigned long base = res->start;
676 unsigned long len = res->end - res->start + 1;
679 p[2] = (base >> 8) & 0xff;
682 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
683 (unsigned long long) res->start, (unsigned long long) res->end);
686 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
691 unsigned int len, tag;
692 int port = 0, irq = 0, dma = 0, mem = 0;
697 while ((char *)p < (char *)end) {
699 /* determine the type of tag */
700 if (p[0] & LARGE_TAG) { /* large tag */
701 len = (p[2] << 8) | p[1];
703 } else { /* small tag */
705 tag = ((p[0] >> 3) & 0x0f);
713 pnpbios_encode_mem(dev, p,
714 pnp_get_resource(dev, IORESOURCE_MEM, mem));
718 case LARGE_TAG_MEM32:
721 pnpbios_encode_mem32(dev, p,
722 pnp_get_resource(dev, IORESOURCE_MEM, mem));
726 case LARGE_TAG_FIXEDMEM32:
729 pnpbios_encode_fixed_mem32(dev, p,
730 pnp_get_resource(dev, IORESOURCE_MEM, mem));
735 if (len < 2 || len > 3)
737 pnpbios_encode_irq(dev, p,
738 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
745 pnpbios_encode_dma(dev, p,
746 pnp_get_resource(dev, IORESOURCE_DMA, dma));
753 pnpbios_encode_port(dev, p,
754 pnp_get_resource(dev, IORESOURCE_IO, port));
758 case SMALL_TAG_VENDOR:
762 case SMALL_TAG_FIXEDPORT:
765 pnpbios_encode_fixed_port(dev, p,
766 pnp_get_resource(dev, IORESOURCE_IO, port));
772 return (unsigned char *)p;
775 default: /* an unkown tag */
777 dev_err(&dev->dev, "unknown tag %#x length %d\n",
782 /* continue to the next tag */
783 if (p[0] & LARGE_TAG)
789 dev_err(&dev->dev, "no end tag in resource structure\n");
795 * Core Parsing Functions
798 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
799 struct pnp_bios_node *node)
801 unsigned char *p = (char *)node->data;
802 unsigned char *end = (char *)(node->data + node->size);
804 p = pnpbios_parse_allocated_resource_data(dev, p, end);
807 p = pnpbios_parse_resource_option_data(p, end, dev);
810 p = pnpbios_parse_compatible_ids(p, end, dev);
816 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
817 struct pnp_bios_node *node)
819 unsigned char *p = (char *)node->data;
820 unsigned char *end = (char *)(node->data + node->size);
822 p = pnpbios_parse_allocated_resource_data(dev, p, end);
828 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
829 struct pnp_bios_node *node)
831 unsigned char *p = (char *)node->data;
832 unsigned char *end = (char *)(node->data + node->size);
834 p = pnpbios_encode_allocated_resource_data(dev, p, end);