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 */
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_irqresource(struct pnp_resource_table *res,
61 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET)
64 if (i < PNP_MAX_IRQ) {
65 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
67 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
70 res->irq_resource[i].start =
71 res->irq_resource[i].end = (unsigned long)irq;
72 pcibios_penalize_isa_irq(irq, 1);
76 static void pnpbios_parse_allocated_dmaresource(struct pnp_resource_table *res,
81 while (i < PNP_MAX_DMA &&
82 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
84 if (i < PNP_MAX_DMA) {
85 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
87 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
90 res->dma_resource[i].start =
91 res->dma_resource[i].end = (unsigned long)dma;
95 static void pnpbios_parse_allocated_ioresource(struct pnp_resource_table *res,
100 while (!(res->port_resource[i].flags & IORESOURCE_UNSET)
103 if (i < PNP_MAX_PORT) {
104 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
105 if (len <= 0 || (io + len - 1) >= 0x10003) {
106 res->port_resource[i].flags |= IORESOURCE_DISABLED;
109 res->port_resource[i].start = (unsigned long)io;
110 res->port_resource[i].end = (unsigned long)(io + len - 1);
114 static void pnpbios_parse_allocated_memresource(struct pnp_resource_table *res,
119 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET)
122 if (i < PNP_MAX_MEM) {
123 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
125 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
128 res->mem_resource[i].start = (unsigned long)mem;
129 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
133 static unsigned char *pnpbios_parse_allocated_resource_data(unsigned char *p,
139 unsigned int len, tag;
140 int io, size, mask, i;
145 /* Blank the resource table values */
146 pnp_init_resource_table(res);
148 while ((char *)p < (char *)end) {
150 /* determine the type of tag */
151 if (p[0] & LARGE_TAG) { /* large tag */
152 len = (p[2] << 8) | p[1];
154 } else { /* small tag */
156 tag = ((p[0] >> 3) & 0x0f);
164 io = *(short *)&p[4];
165 size = *(short *)&p[10];
166 pnpbios_parse_allocated_memresource(res, io, size);
169 case LARGE_TAG_ANSISTR:
170 /* ignore this for now */
173 case LARGE_TAG_VENDOR:
177 case LARGE_TAG_MEM32:
181 size = *(int *)&p[16];
182 pnpbios_parse_allocated_memresource(res, io, size);
185 case LARGE_TAG_FIXEDMEM32:
189 size = *(int *)&p[8];
190 pnpbios_parse_allocated_memresource(res, io, size);
194 if (len < 2 || len > 3)
197 mask = p[1] + p[2] * 256;
198 for (i = 0; i < 16; i++, mask = mask >> 1)
201 pnpbios_parse_allocated_irqresource(res, io);
209 for (i = 0; i < 8; i++, mask = mask >> 1)
212 pnpbios_parse_allocated_dmaresource(res, io);
218 io = p[2] + p[3] * 256;
220 pnpbios_parse_allocated_ioresource(res, io, size);
223 case SMALL_TAG_VENDOR:
227 case SMALL_TAG_FIXEDPORT:
230 io = p[1] + p[2] * 256;
232 pnpbios_parse_allocated_ioresource(res, io, size);
237 return (unsigned char *)p;
240 default: /* an unkown tag */
243 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
248 /* continue to the next tag */
249 if (p[0] & LARGE_TAG)
256 "PnPBIOS: Resource structure does not contain an end tag.\n");
262 * Resource Configuration Options
265 static void pnpbios_parse_mem_option(unsigned char *p, int size,
266 struct pnp_option *option)
270 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
273 mem->min = ((p[5] << 8) | p[4]) << 8;
274 mem->max = ((p[7] << 8) | p[6]) << 8;
275 mem->align = (p[9] << 8) | p[8];
276 mem->size = ((p[11] << 8) | p[10]) << 8;
278 pnp_register_mem_resource(option, mem);
281 static void pnpbios_parse_mem32_option(unsigned char *p, int size,
282 struct pnp_option *option)
286 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
289 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
290 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
291 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
292 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
294 pnp_register_mem_resource(option, mem);
297 static void pnpbios_parse_fixed_mem32_option(unsigned char *p, int size,
298 struct pnp_option *option)
302 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
305 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
306 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
309 pnp_register_mem_resource(option, mem);
312 static void pnpbios_parse_irq_option(unsigned char *p, int size,
313 struct pnp_option *option)
318 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
321 bits = (p[2] << 8) | p[1];
322 bitmap_copy(irq->map, &bits, 16);
326 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
327 pnp_register_irq_resource(option, irq);
330 static void pnpbios_parse_dma_option(unsigned char *p, int size,
331 struct pnp_option *option)
335 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
340 pnp_register_dma_resource(option, dma);
343 static void pnpbios_parse_port_option(unsigned char *p, int size,
344 struct pnp_option *option)
346 struct pnp_port *port;
348 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
351 port->min = (p[3] << 8) | p[2];
352 port->max = (p[5] << 8) | p[4];
355 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
356 pnp_register_port_resource(option, port);
359 static void pnpbios_parse_fixed_port_option(unsigned char *p, int size,
360 struct pnp_option *option)
362 struct pnp_port *port;
364 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
367 port->min = port->max = (p[2] << 8) | p[1];
370 port->flags = PNP_PORT_FLAG_FIXED;
371 pnp_register_port_resource(option, port);
374 static unsigned char *pnpbios_parse_resource_option_data(unsigned char *p,
378 unsigned int len, tag;
380 struct pnp_option *option, *option_independent;
385 option_independent = option = pnp_register_independent_option(dev);
389 while ((char *)p < (char *)end) {
391 /* determine the type of tag */
392 if (p[0] & LARGE_TAG) { /* large tag */
393 len = (p[2] << 8) | p[1];
395 } else { /* small tag */
397 tag = ((p[0] >> 3) & 0x0f);
405 pnpbios_parse_mem_option(p, len, option);
408 case LARGE_TAG_MEM32:
411 pnpbios_parse_mem32_option(p, len, option);
414 case LARGE_TAG_FIXEDMEM32:
417 pnpbios_parse_fixed_mem32_option(p, len, option);
421 if (len < 2 || len > 3)
423 pnpbios_parse_irq_option(p, len, option);
429 pnpbios_parse_dma_option(p, len, option);
435 pnpbios_parse_port_option(p, len, option);
438 case SMALL_TAG_VENDOR:
442 case SMALL_TAG_FIXEDPORT:
445 pnpbios_parse_fixed_port_option(p, len, option);
448 case SMALL_TAG_STARTDEP:
451 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
453 priority = 0x100 | p[1];
454 option = pnp_register_dependent_option(dev, priority);
459 case SMALL_TAG_ENDDEP:
462 if (option_independent == option)
464 "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
465 option = option_independent;
471 default: /* an unkown tag */
474 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
479 /* continue to the next tag */
480 if (p[0] & LARGE_TAG)
487 "PnPBIOS: Resource structure does not contain an end tag.\n");
493 * Compatible Device IDs
496 #define HEX(id,a) hex[((id)>>a) & 15]
497 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
499 void pnpid32_to_pnpid(u32 id, char *str)
501 const char *hex = "0123456789abcdef";
503 id = be32_to_cpu(id);
504 str[0] = CHAR(id, 26);
505 str[1] = CHAR(id, 21);
506 str[2] = CHAR(id, 16);
507 str[3] = HEX(id, 12);
517 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
523 struct pnp_id *dev_id;
528 while ((char *)p < (char *)end) {
530 /* determine the type of tag */
531 if (p[0] & LARGE_TAG) { /* large tag */
532 len = (p[2] << 8) | p[1];
534 } else { /* small tag */
536 tag = ((p[0] >> 3) & 0x0f);
541 case LARGE_TAG_ANSISTR:
542 strncpy(dev->name, p + 3,
543 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
545 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
548 case SMALL_TAG_COMPATDEVID: /* compatible ID */
551 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
554 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] <<
556 memcpy(&dev_id->id, id, 7);
557 pnp_add_id(dev_id, dev);
562 return (unsigned char *)p;
565 default: /* an unkown tag */
568 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
573 /* continue to the next tag */
574 if (p[0] & LARGE_TAG)
581 "PnPBIOS: Resource structure does not contain an end tag.\n");
587 * Allocated Resource Encoding
590 static void pnpbios_encode_mem(unsigned char *p, struct resource *res)
592 unsigned long base = res->start;
593 unsigned long len = res->end - res->start + 1;
595 p[4] = (base >> 8) & 0xff;
596 p[5] = ((base >> 8) >> 8) & 0xff;
597 p[6] = (base >> 8) & 0xff;
598 p[7] = ((base >> 8) >> 8) & 0xff;
599 p[10] = (len >> 8) & 0xff;
600 p[11] = ((len >> 8) >> 8) & 0xff;
603 static void pnpbios_encode_mem32(unsigned char *p, struct resource *res)
605 unsigned long base = res->start;
606 unsigned long len = res->end - res->start + 1;
609 p[5] = (base >> 8) & 0xff;
610 p[6] = (base >> 16) & 0xff;
611 p[7] = (base >> 24) & 0xff;
613 p[9] = (base >> 8) & 0xff;
614 p[10] = (base >> 16) & 0xff;
615 p[11] = (base >> 24) & 0xff;
617 p[17] = (len >> 8) & 0xff;
618 p[18] = (len >> 16) & 0xff;
619 p[19] = (len >> 24) & 0xff;
622 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource *res)
624 unsigned long base = res->start;
625 unsigned long len = res->end - res->start + 1;
628 p[5] = (base >> 8) & 0xff;
629 p[6] = (base >> 16) & 0xff;
630 p[7] = (base >> 24) & 0xff;
632 p[9] = (len >> 8) & 0xff;
633 p[10] = (len >> 16) & 0xff;
634 p[11] = (len >> 24) & 0xff;
637 static void pnpbios_encode_irq(unsigned char *p, struct resource *res)
639 unsigned long map = 0;
641 map = 1 << res->start;
643 p[2] = (map >> 8) & 0xff;
646 static void pnpbios_encode_dma(unsigned char *p, struct resource *res)
648 unsigned long map = 0;
650 map = 1 << res->start;
654 static void pnpbios_encode_port(unsigned char *p, struct resource *res)
656 unsigned long base = res->start;
657 unsigned long len = res->end - res->start + 1;
660 p[3] = (base >> 8) & 0xff;
662 p[5] = (base >> 8) & 0xff;
666 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource *res)
668 unsigned long base = res->start;
669 unsigned long len = res->end - res->start + 1;
672 p[2] = (base >> 8) & 0xff;
676 static unsigned char *pnpbios_encode_allocated_resource_data(unsigned char *p,
682 unsigned int len, tag;
683 int port = 0, irq = 0, dma = 0, mem = 0;
688 while ((char *)p < (char *)end) {
690 /* determine the type of tag */
691 if (p[0] & LARGE_TAG) { /* large tag */
692 len = (p[2] << 8) | p[1];
694 } else { /* small tag */
696 tag = ((p[0] >> 3) & 0x0f);
704 pnpbios_encode_mem(p, &res->mem_resource[mem]);
708 case LARGE_TAG_MEM32:
711 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
715 case LARGE_TAG_FIXEDMEM32:
718 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
723 if (len < 2 || len > 3)
725 pnpbios_encode_irq(p, &res->irq_resource[irq]);
732 pnpbios_encode_dma(p, &res->dma_resource[dma]);
739 pnpbios_encode_port(p, &res->port_resource[port]);
743 case SMALL_TAG_VENDOR:
747 case SMALL_TAG_FIXEDPORT:
750 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
756 return (unsigned char *)p;
759 default: /* an unkown tag */
762 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
767 /* continue to the next tag */
768 if (p[0] & LARGE_TAG)
775 "PnPBIOS: Resource structure does not contain an end tag.\n");
781 * Core Parsing Functions
784 int pnpbios_parse_data_stream(struct pnp_dev *dev, 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);