2 * rsparser.c - parses and encodes pnpbios resource data streams
6 #include <linux/config.h>
7 #include <linux/ctype.h>
9 #include <linux/pnpbios.h>
12 #include <linux/pci.h>
14 inline void pcibios_penalize_isa_irq(int irq) {}
15 #endif /* CONFIG_PCI */
19 /* standard resource tags */
20 #define SMALL_TAG_PNPVERNO 0x01
21 #define SMALL_TAG_LOGDEVID 0x02
22 #define SMALL_TAG_COMPATDEVID 0x03
23 #define SMALL_TAG_IRQ 0x04
24 #define SMALL_TAG_DMA 0x05
25 #define SMALL_TAG_STARTDEP 0x06
26 #define SMALL_TAG_ENDDEP 0x07
27 #define SMALL_TAG_PORT 0x08
28 #define SMALL_TAG_FIXEDPORT 0x09
29 #define SMALL_TAG_VENDOR 0x0e
30 #define SMALL_TAG_END 0x0f
31 #define LARGE_TAG 0x80
32 #define LARGE_TAG_MEM 0x81
33 #define LARGE_TAG_ANSISTR 0x82
34 #define LARGE_TAG_UNICODESTR 0x83
35 #define LARGE_TAG_VENDOR 0x84
36 #define LARGE_TAG_MEM32 0x85
37 #define LARGE_TAG_FIXEDMEM32 0x86
40 * Resource Data Stream Format:
42 * Allocated Resources (required)
44 * Resource Configuration Options (optional)
46 * Compitable Device IDs (optional)
55 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
58 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
59 if (i < PNP_MAX_IRQ) {
60 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
62 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
65 res->irq_resource[i].start =
66 res->irq_resource[i].end = (unsigned long) irq;
67 pcibios_penalize_isa_irq(irq);
72 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
75 while (!(res->dma_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_DMA) i++;
76 if (i < PNP_MAX_DMA) {
77 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
79 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
82 res->dma_resource[i].start =
83 res->dma_resource[i].end = (unsigned long) dma;
88 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
91 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
92 if (i < PNP_MAX_PORT) {
93 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
94 if (len <= 0 || (io + len -1) >= 0x10003) {
95 res->port_resource[i].flags |= IORESOURCE_DISABLED;
98 res->port_resource[i].start = (unsigned long) io;
99 res->port_resource[i].end = (unsigned long)(io + len - 1);
104 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
107 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
108 if (i < PNP_MAX_MEM) {
109 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
111 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
114 res->mem_resource[i].start = (unsigned long) mem;
115 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
119 static unsigned char *
120 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
122 unsigned int len, tag;
123 int io, size, mask, i;
128 /* Blank the resource table values */
129 pnp_init_resource_table(res);
131 while ((char *)p < (char *)end) {
133 /* determine the type of tag */
134 if (p[0] & LARGE_TAG) { /* large tag */
135 len = (p[2] << 8) | p[1];
137 } else { /* small tag */
139 tag = ((p[0]>>3) & 0x0f);
147 io = *(short *) &p[4];
148 size = *(short *) &p[10];
149 pnpbios_parse_allocated_memresource(res, io, size);
152 case LARGE_TAG_ANSISTR:
153 /* ignore this for now */
156 case LARGE_TAG_VENDOR:
160 case LARGE_TAG_MEM32:
164 size = *(int *) &p[16];
165 pnpbios_parse_allocated_memresource(res, io, size);
168 case LARGE_TAG_FIXEDMEM32:
172 size = *(int *) &p[8];
173 pnpbios_parse_allocated_memresource(res, io, size);
177 if (len < 2 || len > 3)
180 mask= p[1] + p[2]*256;
181 for (i=0;i<16;i++, mask=mask>>1)
182 if(mask & 0x01) io=i;
183 pnpbios_parse_allocated_irqresource(res, io);
191 for (i=0;i<8;i++, mask = mask>>1)
192 if(mask & 0x01) io=i;
193 pnpbios_parse_allocated_dmaresource(res, io);
199 io = p[2] + p[3] *256;
201 pnpbios_parse_allocated_ioresource(res, io, size);
204 case SMALL_TAG_VENDOR:
208 case SMALL_TAG_FIXEDPORT:
211 io = p[1] + p[2] * 256;
213 pnpbios_parse_allocated_ioresource(res, io, size);
218 return (unsigned char *)p;
221 default: /* an unkown tag */
223 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
227 /* continue to the next tag */
228 if (p[0] & LARGE_TAG)
234 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
241 * Resource Configuration Options
245 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
247 struct pnp_mem * mem;
248 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
251 mem->min = ((p[5] << 8) | p[4]) << 8;
252 mem->max = ((p[7] << 8) | p[6]) << 8;
253 mem->align = (p[9] << 8) | p[8];
254 mem->size = ((p[11] << 8) | p[10]) << 8;
256 pnp_register_mem_resource(option,mem);
261 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
263 struct pnp_mem * mem;
264 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
267 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
268 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
269 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
270 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
272 pnp_register_mem_resource(option,mem);
277 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
279 struct pnp_mem * mem;
280 mem = pnpbios_kmalloc(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(option,mem);
292 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
294 struct pnp_irq * irq;
297 irq = pnpbios_kmalloc(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(option,irq);
311 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
313 struct pnp_dma * dma;
314 dma = pnpbios_kmalloc(sizeof(struct pnp_dma), GFP_KERNEL);
319 pnp_register_dma_resource(option,dma);
324 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
326 struct pnp_port * port;
327 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
330 port->min = (p[3] << 8) | p[2];
331 port->max = (p[5] << 8) | p[4];
334 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
335 pnp_register_port_resource(option,port);
340 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
342 struct pnp_port * port;
343 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
346 port->min = port->max = (p[2] << 8) | p[1];
349 port->flags = PNP_PORT_FLAG_FIXED;
350 pnp_register_port_resource(option,port);
354 static unsigned char *
355 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
357 unsigned int len, tag;
359 struct pnp_option *option, *option_independent;
364 option_independent = option = pnp_register_independent_option(dev);
368 while ((char *)p < (char *)end) {
370 /* determine the type of tag */
371 if (p[0] & LARGE_TAG) { /* large tag */
372 len = (p[2] << 8) | p[1];
374 } else { /* small tag */
376 tag = ((p[0]>>3) & 0x0f);
384 pnpbios_parse_mem_option(p, len, option);
387 case LARGE_TAG_MEM32:
390 pnpbios_parse_mem32_option(p, len, option);
393 case LARGE_TAG_FIXEDMEM32:
396 pnpbios_parse_fixed_mem32_option(p, len, option);
400 if (len < 2 || len > 3)
402 pnpbios_parse_irq_option(p, len, option);
408 pnpbios_parse_dma_option(p, len, option);
414 pnpbios_parse_port_option(p, len, option);
417 case SMALL_TAG_VENDOR:
421 case SMALL_TAG_FIXEDPORT:
424 pnpbios_parse_fixed_port_option(p, len, option);
427 case SMALL_TAG_STARTDEP:
430 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
432 priority = 0x100 | p[1];
433 option = pnp_register_dependent_option(dev, priority);
438 case SMALL_TAG_ENDDEP:
441 if (option_independent == option)
442 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
443 option = option_independent;
447 if (option_independent != option)
448 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
450 return (unsigned char *)p;
453 default: /* an unkown tag */
455 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
459 /* continue to the next tag */
460 if (p[0] & LARGE_TAG)
466 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
473 * Compatible Device IDs
476 #define HEX(id,a) hex[((id)>>a) & 15]
477 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
480 void pnpid32_to_pnpid(u32 id, char *str)
482 const char *hex = "0123456789abcdef";
484 id = be32_to_cpu(id);
485 str[0] = CHAR(id, 26);
486 str[1] = CHAR(id, 21);
487 str[2] = CHAR(id,16);
488 str[3] = HEX(id, 12);
500 static unsigned char *
501 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
505 struct pnp_id *dev_id;
510 while ((char *)p < (char *)end) {
512 /* determine the type of tag */
513 if (p[0] & LARGE_TAG) { /* large tag */
514 len = (p[2] << 8) | p[1];
516 } else { /* small tag */
518 tag = ((p[0]>>3) & 0x0f);
523 case LARGE_TAG_ANSISTR:
524 strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
525 dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
528 case SMALL_TAG_COMPATDEVID: /* compatible ID */
531 dev_id = pnpbios_kmalloc(sizeof (struct pnp_id), GFP_KERNEL);
534 memset(dev_id, 0, sizeof(struct pnp_id));
535 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
536 memcpy(&dev_id->id, id, 7);
537 pnp_add_id(dev_id, dev);
542 return (unsigned char *)p;
545 default: /* an unkown tag */
547 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
551 /* continue to the next tag */
552 if (p[0] & LARGE_TAG)
558 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
565 * Allocated Resource Encoding
568 static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
570 unsigned long base = res->start;
571 unsigned long len = res->end - res->start + 1;
572 p[4] = (base >> 8) & 0xff;
573 p[5] = ((base >> 8) >> 8) & 0xff;
574 p[6] = (base >> 8) & 0xff;
575 p[7] = ((base >> 8) >> 8) & 0xff;
576 p[10] = (len >> 8) & 0xff;
577 p[11] = ((len >> 8) >> 8) & 0xff;
581 static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
583 unsigned long base = res->start;
584 unsigned long len = res->end - res->start + 1;
586 p[5] = (base >> 8) & 0xff;
587 p[6] = (base >> 16) & 0xff;
588 p[7] = (base >> 24) & 0xff;
590 p[9] = (base >> 8) & 0xff;
591 p[10] = (base >> 16) & 0xff;
592 p[11] = (base >> 24) & 0xff;
594 p[17] = (len >> 8) & 0xff;
595 p[18] = (len >> 16) & 0xff;
596 p[19] = (len >> 24) & 0xff;
600 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
601 { unsigned long base = res->start;
602 unsigned long len = res->end - res->start + 1;
604 p[5] = (base >> 8) & 0xff;
605 p[6] = (base >> 16) & 0xff;
606 p[7] = (base >> 24) & 0xff;
608 p[9] = (len >> 8) & 0xff;
609 p[10] = (len >> 16) & 0xff;
610 p[11] = (len >> 24) & 0xff;
614 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
616 unsigned long map = 0;
617 map = 1 << res->start;
619 p[2] = (map >> 8) & 0xff;
623 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
625 unsigned long map = 0;
626 map = 1 << res->start;
631 static void pnpbios_encode_port(unsigned char *p, struct resource * res)
633 unsigned long base = res->start;
634 unsigned long len = res->end - res->start + 1;
636 p[3] = (base >> 8) & 0xff;
638 p[5] = (base >> 8) & 0xff;
643 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
645 unsigned long base = res->start;
646 unsigned long len = res->end - res->start + 1;
648 p[2] = (base >> 8) & 0xff;
653 static unsigned char *
654 pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
656 unsigned int len, tag;
657 int port = 0, irq = 0, dma = 0, mem = 0;
662 while ((char *)p < (char *)end) {
664 /* determine the type of tag */
665 if (p[0] & LARGE_TAG) { /* large tag */
666 len = (p[2] << 8) | p[1];
668 } else { /* small tag */
670 tag = ((p[0]>>3) & 0x0f);
678 pnpbios_encode_mem(p, &res->mem_resource[mem]);
682 case LARGE_TAG_MEM32:
685 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
689 case LARGE_TAG_FIXEDMEM32:
692 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
697 if (len < 2 || len > 3)
699 pnpbios_encode_irq(p, &res->irq_resource[irq]);
706 pnpbios_encode_dma(p, &res->dma_resource[dma]);
713 pnpbios_encode_port(p, &res->port_resource[port]);
717 case SMALL_TAG_VENDOR:
721 case SMALL_TAG_FIXEDPORT:
724 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
730 return (unsigned char *)p;
733 default: /* an unkown tag */
735 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
739 /* continue to the next tag */
740 if (p[0] & LARGE_TAG)
746 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
753 * Core Parsing Functions
757 pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
759 unsigned char * p = (char *)node->data;
760 unsigned char * end = (char *)(node->data + node->size);
761 p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
764 p = pnpbios_parse_resource_option_data(p,end,dev);
767 p = pnpbios_parse_compatible_ids(p,end,dev);
774 pnpbios_read_resources_from_node(struct pnp_resource_table *res,
775 struct pnp_bios_node * node)
777 unsigned char * p = (char *)node->data;
778 unsigned char * end = (char *)(node->data + node->size);
779 p = pnpbios_parse_allocated_resource_data(p,end,res);
786 pnpbios_write_resources_to_node(struct pnp_resource_table *res,
787 struct pnp_bios_node * node)
789 unsigned char * p = (char *)node->data;
790 unsigned char * end = (char *)(node->data + node->size);
791 p = pnpbios_encode_allocated_resource_data(p,end,res);