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, int active) {}
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, 1);
72 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
75 while (i < PNP_MAX_DMA &&
76 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
78 if (i < PNP_MAX_DMA) {
79 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
81 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
84 res->dma_resource[i].start =
85 res->dma_resource[i].end = (unsigned long) dma;
90 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
93 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
94 if (i < PNP_MAX_PORT) {
95 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
96 if (len <= 0 || (io + len -1) >= 0x10003) {
97 res->port_resource[i].flags |= IORESOURCE_DISABLED;
100 res->port_resource[i].start = (unsigned long) io;
101 res->port_resource[i].end = (unsigned long)(io + len - 1);
106 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
109 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
110 if (i < PNP_MAX_MEM) {
111 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
113 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
116 res->mem_resource[i].start = (unsigned long) mem;
117 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
121 static unsigned char *
122 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
124 unsigned int len, tag;
125 int io, size, mask, i;
130 /* Blank the resource table values */
131 pnp_init_resource_table(res);
133 while ((char *)p < (char *)end) {
135 /* determine the type of tag */
136 if (p[0] & LARGE_TAG) { /* large tag */
137 len = (p[2] << 8) | p[1];
139 } else { /* small tag */
141 tag = ((p[0]>>3) & 0x0f);
149 io = *(short *) &p[4];
150 size = *(short *) &p[10];
151 pnpbios_parse_allocated_memresource(res, io, size);
154 case LARGE_TAG_ANSISTR:
155 /* ignore this for now */
158 case LARGE_TAG_VENDOR:
162 case LARGE_TAG_MEM32:
166 size = *(int *) &p[16];
167 pnpbios_parse_allocated_memresource(res, io, size);
170 case LARGE_TAG_FIXEDMEM32:
174 size = *(int *) &p[8];
175 pnpbios_parse_allocated_memresource(res, io, size);
179 if (len < 2 || len > 3)
182 mask= p[1] + p[2]*256;
183 for (i=0;i<16;i++, mask=mask>>1)
184 if(mask & 0x01) io=i;
185 pnpbios_parse_allocated_irqresource(res, io);
193 for (i=0;i<8;i++, mask = mask>>1)
194 if(mask & 0x01) io=i;
195 pnpbios_parse_allocated_dmaresource(res, io);
201 io = p[2] + p[3] *256;
203 pnpbios_parse_allocated_ioresource(res, io, size);
206 case SMALL_TAG_VENDOR:
210 case SMALL_TAG_FIXEDPORT:
213 io = p[1] + p[2] * 256;
215 pnpbios_parse_allocated_ioresource(res, io, size);
220 return (unsigned char *)p;
223 default: /* an unkown tag */
225 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
229 /* continue to the next tag */
230 if (p[0] & LARGE_TAG)
236 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
243 * Resource Configuration Options
247 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
249 struct pnp_mem * mem;
250 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
253 mem->min = ((p[5] << 8) | p[4]) << 8;
254 mem->max = ((p[7] << 8) | p[6]) << 8;
255 mem->align = (p[9] << 8) | p[8];
256 mem->size = ((p[11] << 8) | p[10]) << 8;
258 pnp_register_mem_resource(option,mem);
263 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
265 struct pnp_mem * mem;
266 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
269 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
270 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
271 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
272 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
274 pnp_register_mem_resource(option,mem);
279 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
281 struct pnp_mem * mem;
282 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
285 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
286 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
289 pnp_register_mem_resource(option,mem);
294 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
296 struct pnp_irq * irq;
299 irq = pnpbios_kmalloc(sizeof(struct pnp_irq), GFP_KERNEL);
302 bits = (p[2] << 8) | p[1];
303 bitmap_copy(irq->map, &bits, 16);
307 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
308 pnp_register_irq_resource(option,irq);
313 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
315 struct pnp_dma * dma;
316 dma = pnpbios_kmalloc(sizeof(struct pnp_dma), GFP_KERNEL);
321 pnp_register_dma_resource(option,dma);
326 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
328 struct pnp_port * port;
329 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
332 port->min = (p[3] << 8) | p[2];
333 port->max = (p[5] << 8) | p[4];
336 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
337 pnp_register_port_resource(option,port);
342 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
344 struct pnp_port * port;
345 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
348 port->min = port->max = (p[2] << 8) | p[1];
351 port->flags = PNP_PORT_FLAG_FIXED;
352 pnp_register_port_resource(option,port);
356 static unsigned char *
357 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
359 unsigned int len, tag;
361 struct pnp_option *option, *option_independent;
366 option_independent = option = pnp_register_independent_option(dev);
370 while ((char *)p < (char *)end) {
372 /* determine the type of tag */
373 if (p[0] & LARGE_TAG) { /* large tag */
374 len = (p[2] << 8) | p[1];
376 } else { /* small tag */
378 tag = ((p[0]>>3) & 0x0f);
386 pnpbios_parse_mem_option(p, len, option);
389 case LARGE_TAG_MEM32:
392 pnpbios_parse_mem32_option(p, len, option);
395 case LARGE_TAG_FIXEDMEM32:
398 pnpbios_parse_fixed_mem32_option(p, len, option);
402 if (len < 2 || len > 3)
404 pnpbios_parse_irq_option(p, len, option);
410 pnpbios_parse_dma_option(p, len, option);
416 pnpbios_parse_port_option(p, len, option);
419 case SMALL_TAG_VENDOR:
423 case SMALL_TAG_FIXEDPORT:
426 pnpbios_parse_fixed_port_option(p, len, option);
429 case SMALL_TAG_STARTDEP:
432 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
434 priority = 0x100 | p[1];
435 option = pnp_register_dependent_option(dev, priority);
440 case SMALL_TAG_ENDDEP:
443 if (option_independent == option)
444 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
445 option = option_independent;
449 if (option_independent != option)
450 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
452 return (unsigned char *)p;
455 default: /* an unkown tag */
457 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
461 /* continue to the next tag */
462 if (p[0] & LARGE_TAG)
468 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
475 * Compatible Device IDs
478 #define HEX(id,a) hex[((id)>>a) & 15]
479 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
482 void pnpid32_to_pnpid(u32 id, char *str)
484 const char *hex = "0123456789abcdef";
486 id = be32_to_cpu(id);
487 str[0] = CHAR(id, 26);
488 str[1] = CHAR(id, 21);
489 str[2] = CHAR(id,16);
490 str[3] = HEX(id, 12);
502 static unsigned char *
503 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
507 struct pnp_id *dev_id;
512 while ((char *)p < (char *)end) {
514 /* determine the type of tag */
515 if (p[0] & LARGE_TAG) { /* large tag */
516 len = (p[2] << 8) | p[1];
518 } else { /* small tag */
520 tag = ((p[0]>>3) & 0x0f);
525 case LARGE_TAG_ANSISTR:
526 strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
527 dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
530 case SMALL_TAG_COMPATDEVID: /* compatible ID */
533 dev_id = pnpbios_kmalloc(sizeof (struct pnp_id), GFP_KERNEL);
536 memset(dev_id, 0, sizeof(struct pnp_id));
537 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
538 memcpy(&dev_id->id, id, 7);
539 pnp_add_id(dev_id, dev);
544 return (unsigned char *)p;
547 default: /* an unkown tag */
549 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
553 /* continue to the next tag */
554 if (p[0] & LARGE_TAG)
560 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
567 * Allocated Resource Encoding
570 static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
572 unsigned long base = res->start;
573 unsigned long len = res->end - res->start + 1;
574 p[4] = (base >> 8) & 0xff;
575 p[5] = ((base >> 8) >> 8) & 0xff;
576 p[6] = (base >> 8) & 0xff;
577 p[7] = ((base >> 8) >> 8) & 0xff;
578 p[10] = (len >> 8) & 0xff;
579 p[11] = ((len >> 8) >> 8) & 0xff;
583 static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
585 unsigned long base = res->start;
586 unsigned long len = res->end - res->start + 1;
588 p[5] = (base >> 8) & 0xff;
589 p[6] = (base >> 16) & 0xff;
590 p[7] = (base >> 24) & 0xff;
592 p[9] = (base >> 8) & 0xff;
593 p[10] = (base >> 16) & 0xff;
594 p[11] = (base >> 24) & 0xff;
596 p[17] = (len >> 8) & 0xff;
597 p[18] = (len >> 16) & 0xff;
598 p[19] = (len >> 24) & 0xff;
602 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
603 { unsigned long base = res->start;
604 unsigned long len = res->end - res->start + 1;
606 p[5] = (base >> 8) & 0xff;
607 p[6] = (base >> 16) & 0xff;
608 p[7] = (base >> 24) & 0xff;
610 p[9] = (len >> 8) & 0xff;
611 p[10] = (len >> 16) & 0xff;
612 p[11] = (len >> 24) & 0xff;
616 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
618 unsigned long map = 0;
619 map = 1 << res->start;
621 p[2] = (map >> 8) & 0xff;
625 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
627 unsigned long map = 0;
628 map = 1 << res->start;
633 static void pnpbios_encode_port(unsigned char *p, struct resource * res)
635 unsigned long base = res->start;
636 unsigned long len = res->end - res->start + 1;
638 p[3] = (base >> 8) & 0xff;
640 p[5] = (base >> 8) & 0xff;
645 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
647 unsigned long base = res->start;
648 unsigned long len = res->end - res->start + 1;
650 p[2] = (base >> 8) & 0xff;
655 static unsigned char *
656 pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
658 unsigned int len, tag;
659 int port = 0, irq = 0, dma = 0, mem = 0;
664 while ((char *)p < (char *)end) {
666 /* determine the type of tag */
667 if (p[0] & LARGE_TAG) { /* large tag */
668 len = (p[2] << 8) | p[1];
670 } else { /* small tag */
672 tag = ((p[0]>>3) & 0x0f);
680 pnpbios_encode_mem(p, &res->mem_resource[mem]);
684 case LARGE_TAG_MEM32:
687 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
691 case LARGE_TAG_FIXEDMEM32:
694 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
699 if (len < 2 || len > 3)
701 pnpbios_encode_irq(p, &res->irq_resource[irq]);
708 pnpbios_encode_dma(p, &res->dma_resource[dma]);
715 pnpbios_encode_port(p, &res->port_resource[port]);
719 case SMALL_TAG_VENDOR:
723 case SMALL_TAG_FIXEDPORT:
726 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
732 return (unsigned char *)p;
735 default: /* an unkown tag */
737 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
741 /* continue to the next tag */
742 if (p[0] & LARGE_TAG)
748 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
755 * Core Parsing Functions
759 pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
761 unsigned char * p = (char *)node->data;
762 unsigned char * end = (char *)(node->data + node->size);
763 p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
766 p = pnpbios_parse_resource_option_data(p,end,dev);
769 p = pnpbios_parse_compatible_ids(p,end,dev);
776 pnpbios_read_resources_from_node(struct pnp_resource_table *res,
777 struct pnp_bios_node * node)
779 unsigned char * p = (char *)node->data;
780 unsigned char * end = (char *)(node->data + node->size);
781 p = pnpbios_parse_allocated_resource_data(p,end,res);
788 pnpbios_write_resources_to_node(struct pnp_resource_table *res,
789 struct pnp_bios_node * node)
791 unsigned char * p = (char *)node->data;
792 unsigned char * end = (char *)(node->data + node->size);
793 p = pnpbios_encode_allocated_resource_data(p,end,res);