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_resource_table *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_resource_table *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_resource_table *res,
101 while (!(res->port_resource[i].flags & IORESOURCE_UNSET)
104 if (i < PNP_MAX_PORT) {
105 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
106 if (len <= 0 || (io + len - 1) >= 0x10003) {
107 res->port_resource[i].flags |= IORESOURCE_DISABLED;
110 res->port_resource[i].start = (unsigned long)io;
111 res->port_resource[i].end = (unsigned long)(io + len - 1);
115 static void pnpbios_parse_allocated_memresource(struct pnp_resource_table *res,
120 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET)
123 if (i < PNP_MAX_MEM) {
124 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
126 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
129 res->mem_resource[i].start = (unsigned long)mem;
130 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
134 static unsigned char *pnpbios_parse_allocated_resource_data(unsigned char *p,
140 unsigned int len, tag;
141 int io, size, mask, i;
146 /* Blank the resource table values */
147 pnp_init_resource_table(res);
149 while ((char *)p < (char *)end) {
151 /* determine the type of tag */
152 if (p[0] & LARGE_TAG) { /* large tag */
153 len = (p[2] << 8) | p[1];
155 } else { /* small tag */
157 tag = ((p[0] >> 3) & 0x0f);
165 io = *(short *)&p[4];
166 size = *(short *)&p[10];
167 pnpbios_parse_allocated_memresource(res, io, size);
170 case LARGE_TAG_ANSISTR:
171 /* ignore this for now */
174 case LARGE_TAG_VENDOR:
178 case LARGE_TAG_MEM32:
182 size = *(int *)&p[16];
183 pnpbios_parse_allocated_memresource(res, io, size);
186 case LARGE_TAG_FIXEDMEM32:
190 size = *(int *)&p[8];
191 pnpbios_parse_allocated_memresource(res, io, size);
195 if (len < 2 || len > 3)
198 mask = p[1] + p[2] * 256;
199 for (i = 0; i < 16; i++, mask = mask >> 1)
202 pnpbios_parse_allocated_irqresource(res, io);
210 for (i = 0; i < 8; i++, mask = mask >> 1)
213 pnpbios_parse_allocated_dmaresource(res, io);
219 io = p[2] + p[3] * 256;
221 pnpbios_parse_allocated_ioresource(res, io, size);
224 case SMALL_TAG_VENDOR:
228 case SMALL_TAG_FIXEDPORT:
231 io = p[1] + p[2] * 256;
233 pnpbios_parse_allocated_ioresource(res, io, size);
238 return (unsigned char *)p;
241 default: /* an unkown tag */
244 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
249 /* continue to the next tag */
250 if (p[0] & LARGE_TAG)
257 "PnPBIOS: Resource structure does not contain an end tag.\n");
263 * Resource Configuration Options
266 static __init void pnpbios_parse_mem_option(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(option, mem);
282 static __init void pnpbios_parse_mem32_option(unsigned char *p, int size,
283 struct pnp_option *option)
287 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
290 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
291 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
292 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
293 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
295 pnp_register_mem_resource(option, mem);
298 static __init void pnpbios_parse_fixed_mem32_option(unsigned char *p, int size,
299 struct pnp_option *option)
303 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
306 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
307 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
310 pnp_register_mem_resource(option, mem);
313 static __init void pnpbios_parse_irq_option(unsigned char *p, int size,
314 struct pnp_option *option)
319 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
322 bits = (p[2] << 8) | p[1];
323 bitmap_copy(irq->map, &bits, 16);
327 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
328 pnp_register_irq_resource(option, irq);
331 static __init void pnpbios_parse_dma_option(unsigned char *p, int size,
332 struct pnp_option *option)
336 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
341 pnp_register_dma_resource(option, dma);
344 static __init void pnpbios_parse_port_option(unsigned char *p, int size,
345 struct pnp_option *option)
347 struct pnp_port *port;
349 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
352 port->min = (p[3] << 8) | p[2];
353 port->max = (p[5] << 8) | p[4];
356 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
357 pnp_register_port_resource(option, port);
360 static __init void pnpbios_parse_fixed_port_option(unsigned char *p, int size,
361 struct pnp_option *option)
363 struct pnp_port *port;
365 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
368 port->min = port->max = (p[2] << 8) | p[1];
371 port->flags = PNP_PORT_FLAG_FIXED;
372 pnp_register_port_resource(option, port);
375 static __init unsigned char *
376 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
379 unsigned int len, tag;
381 struct pnp_option *option, *option_independent;
386 option_independent = option = pnp_register_independent_option(dev);
390 while ((char *)p < (char *)end) {
392 /* determine the type of tag */
393 if (p[0] & LARGE_TAG) { /* large tag */
394 len = (p[2] << 8) | p[1];
396 } else { /* small tag */
398 tag = ((p[0] >> 3) & 0x0f);
406 pnpbios_parse_mem_option(p, len, option);
409 case LARGE_TAG_MEM32:
412 pnpbios_parse_mem32_option(p, len, option);
415 case LARGE_TAG_FIXEDMEM32:
418 pnpbios_parse_fixed_mem32_option(p, len, option);
422 if (len < 2 || len > 3)
424 pnpbios_parse_irq_option(p, len, option);
430 pnpbios_parse_dma_option(p, len, option);
436 pnpbios_parse_port_option(p, len, option);
439 case SMALL_TAG_VENDOR:
443 case SMALL_TAG_FIXEDPORT:
446 pnpbios_parse_fixed_port_option(p, len, option);
449 case SMALL_TAG_STARTDEP:
452 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
454 priority = 0x100 | p[1];
455 option = pnp_register_dependent_option(dev, priority);
460 case SMALL_TAG_ENDDEP:
463 if (option_independent == option)
465 "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
466 option = option_independent;
472 default: /* an unkown tag */
475 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
480 /* continue to the next tag */
481 if (p[0] & LARGE_TAG)
488 "PnPBIOS: Resource structure does not contain an end tag.\n");
494 * Compatible Device IDs
497 #define HEX(id,a) hex[((id)>>a) & 15]
498 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
500 void pnpid32_to_pnpid(u32 id, char *str)
502 const char *hex = "0123456789abcdef";
504 id = be32_to_cpu(id);
505 str[0] = CHAR(id, 26);
506 str[1] = CHAR(id, 21);
507 str[2] = CHAR(id, 16);
508 str[3] = HEX(id, 12);
518 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
524 struct pnp_id *dev_id;
529 while ((char *)p < (char *)end) {
531 /* determine the type of tag */
532 if (p[0] & LARGE_TAG) { /* large tag */
533 len = (p[2] << 8) | p[1];
535 } else { /* small tag */
537 tag = ((p[0] >> 3) & 0x0f);
542 case LARGE_TAG_ANSISTR:
543 strncpy(dev->name, p + 3,
544 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
546 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
549 case SMALL_TAG_COMPATDEVID: /* compatible ID */
552 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] <<
554 dev_id = pnp_add_id(dev, id);
561 return (unsigned char *)p;
564 default: /* an unkown tag */
567 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
572 /* continue to the next tag */
573 if (p[0] & LARGE_TAG)
580 "PnPBIOS: Resource structure does not contain an end tag.\n");
586 * Allocated Resource Encoding
589 static void pnpbios_encode_mem(unsigned char *p, struct resource *res)
591 unsigned long base = res->start;
592 unsigned long len = res->end - res->start + 1;
594 p[4] = (base >> 8) & 0xff;
595 p[5] = ((base >> 8) >> 8) & 0xff;
596 p[6] = (base >> 8) & 0xff;
597 p[7] = ((base >> 8) >> 8) & 0xff;
598 p[10] = (len >> 8) & 0xff;
599 p[11] = ((len >> 8) >> 8) & 0xff;
602 static void pnpbios_encode_mem32(unsigned char *p, struct resource *res)
604 unsigned long base = res->start;
605 unsigned long len = res->end - res->start + 1;
608 p[5] = (base >> 8) & 0xff;
609 p[6] = (base >> 16) & 0xff;
610 p[7] = (base >> 24) & 0xff;
612 p[9] = (base >> 8) & 0xff;
613 p[10] = (base >> 16) & 0xff;
614 p[11] = (base >> 24) & 0xff;
616 p[17] = (len >> 8) & 0xff;
617 p[18] = (len >> 16) & 0xff;
618 p[19] = (len >> 24) & 0xff;
621 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource *res)
623 unsigned long base = res->start;
624 unsigned long len = res->end - res->start + 1;
627 p[5] = (base >> 8) & 0xff;
628 p[6] = (base >> 16) & 0xff;
629 p[7] = (base >> 24) & 0xff;
631 p[9] = (len >> 8) & 0xff;
632 p[10] = (len >> 16) & 0xff;
633 p[11] = (len >> 24) & 0xff;
636 static void pnpbios_encode_irq(unsigned char *p, struct resource *res)
638 unsigned long map = 0;
640 map = 1 << res->start;
642 p[2] = (map >> 8) & 0xff;
645 static void pnpbios_encode_dma(unsigned char *p, struct resource *res)
647 unsigned long map = 0;
649 map = 1 << res->start;
653 static void pnpbios_encode_port(unsigned char *p, struct resource *res)
655 unsigned long base = res->start;
656 unsigned long len = res->end - res->start + 1;
659 p[3] = (base >> 8) & 0xff;
661 p[5] = (base >> 8) & 0xff;
665 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource *res)
667 unsigned long base = res->start;
668 unsigned long len = res->end - res->start + 1;
671 p[2] = (base >> 8) & 0xff;
675 static unsigned char *pnpbios_encode_allocated_resource_data(unsigned char *p,
681 unsigned int len, tag;
682 int port = 0, irq = 0, dma = 0, mem = 0;
687 while ((char *)p < (char *)end) {
689 /* determine the type of tag */
690 if (p[0] & LARGE_TAG) { /* large tag */
691 len = (p[2] << 8) | p[1];
693 } else { /* small tag */
695 tag = ((p[0] >> 3) & 0x0f);
703 pnpbios_encode_mem(p, &res->mem_resource[mem]);
707 case LARGE_TAG_MEM32:
710 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
714 case LARGE_TAG_FIXEDMEM32:
717 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
722 if (len < 2 || len > 3)
724 pnpbios_encode_irq(p, &res->irq_resource[irq]);
731 pnpbios_encode_dma(p, &res->dma_resource[dma]);
738 pnpbios_encode_port(p, &res->port_resource[port]);
742 case SMALL_TAG_VENDOR:
746 case SMALL_TAG_FIXEDPORT:
749 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
755 return (unsigned char *)p;
758 default: /* an unkown tag */
761 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
766 /* continue to the next tag */
767 if (p[0] & LARGE_TAG)
774 "PnPBIOS: Resource structure does not contain an end tag.\n");
780 * Core Parsing Functions
783 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
784 struct pnp_bios_node *node)
786 unsigned char *p = (char *)node->data;
787 unsigned char *end = (char *)(node->data + node->size);
789 p = pnpbios_parse_allocated_resource_data(p, end, &dev->res);
792 p = pnpbios_parse_resource_option_data(p, end, dev);
795 p = pnpbios_parse_compatible_ids(p, end, dev);
801 int pnpbios_read_resources_from_node(struct pnp_resource_table *res,
802 struct pnp_bios_node *node)
804 unsigned char *p = (char *)node->data;
805 unsigned char *end = (char *)(node->data + node->size);
807 p = pnpbios_parse_allocated_resource_data(p, end, res);
813 int pnpbios_write_resources_to_node(struct pnp_resource_table *res,
814 struct pnp_bios_node *node)
816 unsigned char *p = (char *)node->data;
817 unsigned char *end = (char *)(node->data + node->size);
819 p = pnpbios_encode_allocated_resource_data(p, end, res);