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,
61 int end = start + len - 1;
63 if (len <= 0 || end >= 0x10003)
64 flags |= IORESOURCE_DISABLED;
66 pnp_add_io_resource(dev, start, end, flags);
69 static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
75 for (i = 0; i < PNP_MAX_MEM; i++) {
76 res = pnp_get_resource(dev, IORESOURCE_MEM, i);
77 if (!pnp_resource_valid(res))
81 if (i < PNP_MAX_MEM) {
82 res->flags = IORESOURCE_MEM; // Also clears _UNSET flag
84 res->flags |= IORESOURCE_DISABLED;
87 res->start = (unsigned long)mem;
88 res->end = (unsigned long)(mem + len - 1);
92 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
96 unsigned int len, tag;
97 int io, size, mask, i, flags;
102 dev_dbg(&dev->dev, "parse allocated resources\n");
104 pnp_init_resources(dev);
106 while ((char *)p < (char *)end) {
108 /* determine the type of tag */
109 if (p[0] & LARGE_TAG) { /* large tag */
110 len = (p[2] << 8) | p[1];
112 } else { /* small tag */
114 tag = ((p[0] >> 3) & 0x0f);
122 io = *(short *)&p[4];
123 size = *(short *)&p[10];
124 pnpbios_parse_allocated_memresource(dev, io, size);
127 case LARGE_TAG_ANSISTR:
128 /* ignore this for now */
131 case LARGE_TAG_VENDOR:
135 case LARGE_TAG_MEM32:
139 size = *(int *)&p[16];
140 pnpbios_parse_allocated_memresource(dev, io, size);
143 case LARGE_TAG_FIXEDMEM32:
147 size = *(int *)&p[8];
148 pnpbios_parse_allocated_memresource(dev, io, size);
152 if (len < 2 || len > 3)
156 mask = p[1] + p[2] * 256;
157 for (i = 0; i < 16; i++, mask = mask >> 1)
161 pcibios_penalize_isa_irq(io, 1);
163 flags = IORESOURCE_DISABLED;
164 pnp_add_irq_resource(dev, io, flags);
173 for (i = 0; i < 8; i++, mask = mask >> 1)
177 flags = IORESOURCE_DISABLED;
178 pnp_add_dma_resource(dev, io, flags);
184 io = p[2] + p[3] * 256;
186 pnpbios_parse_allocated_ioresource(dev, io, size);
189 case SMALL_TAG_VENDOR:
193 case SMALL_TAG_FIXEDPORT:
196 io = p[1] + p[2] * 256;
198 pnpbios_parse_allocated_ioresource(dev, io, size);
203 return (unsigned char *)p;
206 default: /* an unkown tag */
208 dev_err(&dev->dev, "unknown tag %#x length %d\n",
213 /* continue to the next tag */
214 if (p[0] & LARGE_TAG)
220 dev_err(&dev->dev, "no end tag in resource structure\n");
226 * Resource Configuration Options
229 static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
230 unsigned char *p, int size,
231 struct pnp_option *option)
235 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
238 mem->min = ((p[5] << 8) | p[4]) << 8;
239 mem->max = ((p[7] << 8) | p[6]) << 8;
240 mem->align = (p[9] << 8) | p[8];
241 mem->size = ((p[11] << 8) | p[10]) << 8;
243 pnp_register_mem_resource(dev, option, mem);
246 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
247 unsigned char *p, int size,
248 struct pnp_option *option)
252 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
255 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
256 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
257 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
258 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
260 pnp_register_mem_resource(dev, option, mem);
263 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
264 unsigned char *p, int size,
265 struct pnp_option *option)
269 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
272 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
273 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
276 pnp_register_mem_resource(dev, option, mem);
279 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
280 unsigned char *p, int size,
281 struct pnp_option *option)
286 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
289 bits = (p[2] << 8) | p[1];
290 bitmap_copy(irq->map, &bits, 16);
294 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
295 pnp_register_irq_resource(dev, option, irq);
298 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
299 unsigned char *p, int size,
300 struct pnp_option *option)
304 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
309 pnp_register_dma_resource(dev, option, dma);
312 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
313 unsigned char *p, int size,
314 struct pnp_option *option)
316 struct pnp_port *port;
318 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
321 port->min = (p[3] << 8) | p[2];
322 port->max = (p[5] << 8) | p[4];
325 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
326 pnp_register_port_resource(dev, option, port);
329 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
330 unsigned char *p, int size,
331 struct pnp_option *option)
333 struct pnp_port *port;
335 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
338 port->min = port->max = (p[2] << 8) | p[1];
341 port->flags = PNP_PORT_FLAG_FIXED;
342 pnp_register_port_resource(dev, option, port);
345 static __init unsigned char *
346 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
349 unsigned int len, tag;
351 struct pnp_option *option, *option_independent;
356 dev_dbg(&dev->dev, "parse resource options\n");
358 option_independent = option = pnp_register_independent_option(dev);
362 while ((char *)p < (char *)end) {
364 /* determine the type of tag */
365 if (p[0] & LARGE_TAG) { /* large tag */
366 len = (p[2] << 8) | p[1];
368 } else { /* small tag */
370 tag = ((p[0] >> 3) & 0x0f);
378 pnpbios_parse_mem_option(dev, p, len, option);
381 case LARGE_TAG_MEM32:
384 pnpbios_parse_mem32_option(dev, p, len, option);
387 case LARGE_TAG_FIXEDMEM32:
390 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
394 if (len < 2 || len > 3)
396 pnpbios_parse_irq_option(dev, p, len, option);
402 pnpbios_parse_dma_option(dev, p, len, option);
408 pnpbios_parse_port_option(dev, p, len, option);
411 case SMALL_TAG_VENDOR:
415 case SMALL_TAG_FIXEDPORT:
418 pnpbios_parse_fixed_port_option(dev, p, len, option);
421 case SMALL_TAG_STARTDEP:
424 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
426 priority = 0x100 | p[1];
427 option = pnp_register_dependent_option(dev, priority);
432 case SMALL_TAG_ENDDEP:
435 if (option_independent == option)
436 dev_warn(&dev->dev, "missing "
437 "SMALL_TAG_STARTDEP tag\n");
438 option = option_independent;
439 dev_dbg(&dev->dev, "end dependent options\n");
445 default: /* an unkown tag */
447 dev_err(&dev->dev, "unknown tag %#x length %d\n",
452 /* continue to the next tag */
453 if (p[0] & LARGE_TAG)
459 dev_err(&dev->dev, "no end tag in resource structure\n");
465 * Compatible Device IDs
468 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
475 struct pnp_id *dev_id;
480 while ((char *)p < (char *)end) {
482 /* determine the type of tag */
483 if (p[0] & LARGE_TAG) { /* large tag */
484 len = (p[2] << 8) | p[1];
486 } else { /* small tag */
488 tag = ((p[0] >> 3) & 0x0f);
493 case LARGE_TAG_ANSISTR:
494 strncpy(dev->name, p + 3,
495 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
497 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
500 case SMALL_TAG_COMPATDEVID: /* compatible ID */
503 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
504 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
505 dev_id = pnp_add_id(dev, id);
512 return (unsigned char *)p;
515 default: /* an unkown tag */
517 dev_err(&dev->dev, "unknown tag %#x length %d\n",
522 /* continue to the next tag */
523 if (p[0] & LARGE_TAG)
529 dev_err(&dev->dev, "no end tag in resource structure\n");
535 * Allocated Resource Encoding
538 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
539 struct resource *res)
541 unsigned long base = res->start;
542 unsigned long len = res->end - res->start + 1;
544 p[4] = (base >> 8) & 0xff;
545 p[5] = ((base >> 8) >> 8) & 0xff;
546 p[6] = (base >> 8) & 0xff;
547 p[7] = ((base >> 8) >> 8) & 0xff;
548 p[10] = (len >> 8) & 0xff;
549 p[11] = ((len >> 8) >> 8) & 0xff;
551 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n",
552 (unsigned long long) res->start, (unsigned long long) res->end);
555 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
556 struct resource *res)
558 unsigned long base = res->start;
559 unsigned long len = res->end - res->start + 1;
562 p[5] = (base >> 8) & 0xff;
563 p[6] = (base >> 16) & 0xff;
564 p[7] = (base >> 24) & 0xff;
566 p[9] = (base >> 8) & 0xff;
567 p[10] = (base >> 16) & 0xff;
568 p[11] = (base >> 24) & 0xff;
570 p[17] = (len >> 8) & 0xff;
571 p[18] = (len >> 16) & 0xff;
572 p[19] = (len >> 24) & 0xff;
574 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n",
575 (unsigned long long) res->start, (unsigned long long) res->end);
578 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
579 struct resource *res)
581 unsigned long base = res->start;
582 unsigned long len = res->end - res->start + 1;
585 p[5] = (base >> 8) & 0xff;
586 p[6] = (base >> 16) & 0xff;
587 p[7] = (base >> 24) & 0xff;
589 p[9] = (len >> 8) & 0xff;
590 p[10] = (len >> 16) & 0xff;
591 p[11] = (len >> 24) & 0xff;
593 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n",
594 (unsigned long long) res->start, (unsigned long long) res->end);
597 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
598 struct resource *res)
600 unsigned long map = 0;
602 map = 1 << res->start;
604 p[2] = (map >> 8) & 0xff;
606 dev_dbg(&dev->dev, " encode irq %d\n", res->start);
609 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
610 struct resource *res)
612 unsigned long map = 0;
614 map = 1 << res->start;
617 dev_dbg(&dev->dev, " encode dma %d\n", res->start);
620 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
621 struct resource *res)
623 unsigned long base = res->start;
624 unsigned long len = res->end - res->start + 1;
627 p[3] = (base >> 8) & 0xff;
629 p[5] = (base >> 8) & 0xff;
632 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n",
633 (unsigned long long) res->start, (unsigned long long) res->end);
636 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
637 struct resource *res)
639 unsigned long base = res->start;
640 unsigned long len = res->end - res->start + 1;
643 p[2] = (base >> 8) & 0xff;
646 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
647 (unsigned long long) res->start, (unsigned long long) res->end);
650 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
655 unsigned int len, tag;
656 int port = 0, irq = 0, dma = 0, mem = 0;
661 while ((char *)p < (char *)end) {
663 /* determine the type of tag */
664 if (p[0] & LARGE_TAG) { /* large tag */
665 len = (p[2] << 8) | p[1];
667 } else { /* small tag */
669 tag = ((p[0] >> 3) & 0x0f);
677 pnpbios_encode_mem(dev, p,
678 pnp_get_resource(dev, IORESOURCE_MEM, mem));
682 case LARGE_TAG_MEM32:
685 pnpbios_encode_mem32(dev, p,
686 pnp_get_resource(dev, IORESOURCE_MEM, mem));
690 case LARGE_TAG_FIXEDMEM32:
693 pnpbios_encode_fixed_mem32(dev, p,
694 pnp_get_resource(dev, IORESOURCE_MEM, mem));
699 if (len < 2 || len > 3)
701 pnpbios_encode_irq(dev, p,
702 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
709 pnpbios_encode_dma(dev, p,
710 pnp_get_resource(dev, IORESOURCE_DMA, dma));
717 pnpbios_encode_port(dev, p,
718 pnp_get_resource(dev, IORESOURCE_IO, port));
722 case SMALL_TAG_VENDOR:
726 case SMALL_TAG_FIXEDPORT:
729 pnpbios_encode_fixed_port(dev, p,
730 pnp_get_resource(dev, IORESOURCE_IO, port));
736 return (unsigned char *)p;
739 default: /* an unkown tag */
741 dev_err(&dev->dev, "unknown tag %#x length %d\n",
746 /* continue to the next tag */
747 if (p[0] & LARGE_TAG)
753 dev_err(&dev->dev, "no end tag in resource structure\n");
759 * Core Parsing Functions
762 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
763 struct pnp_bios_node *node)
765 unsigned char *p = (char *)node->data;
766 unsigned char *end = (char *)(node->data + node->size);
768 p = pnpbios_parse_allocated_resource_data(dev, p, end);
771 p = pnpbios_parse_resource_option_data(p, end, dev);
774 p = pnpbios_parse_compatible_ids(p, end, dev);
780 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
781 struct pnp_bios_node *node)
783 unsigned char *p = (char *)node->data;
784 unsigned char *end = (char *)(node->data + node->size);
786 p = pnpbios_parse_allocated_resource_data(dev, p, end);
792 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
793 struct pnp_bios_node *node)
795 unsigned char *p = (char *)node->data;
796 unsigned char *end = (char *)(node->data + node->size);
798 p = pnpbios_encode_allocated_resource_data(dev, p, end);