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>
10 #include <linux/pci.h>
14 /* standard resource tags */
15 #define SMALL_TAG_PNPVERNO 0x01
16 #define SMALL_TAG_LOGDEVID 0x02
17 #define SMALL_TAG_COMPATDEVID 0x03
18 #define SMALL_TAG_IRQ 0x04
19 #define SMALL_TAG_DMA 0x05
20 #define SMALL_TAG_STARTDEP 0x06
21 #define SMALL_TAG_ENDDEP 0x07
22 #define SMALL_TAG_PORT 0x08
23 #define SMALL_TAG_FIXEDPORT 0x09
24 #define SMALL_TAG_VENDOR 0x0e
25 #define SMALL_TAG_END 0x0f
26 #define LARGE_TAG 0x80
27 #define LARGE_TAG_MEM 0x81
28 #define LARGE_TAG_ANSISTR 0x82
29 #define LARGE_TAG_UNICODESTR 0x83
30 #define LARGE_TAG_VENDOR 0x84
31 #define LARGE_TAG_MEM32 0x85
32 #define LARGE_TAG_FIXEDMEM32 0x86
35 * Resource Data Stream Format:
37 * Allocated Resources (required)
39 * Resource Configuration Options (optional)
41 * Compitable Device IDs (optional)
50 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
53 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
54 if (i < PNP_MAX_IRQ) {
55 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
57 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
60 res->irq_resource[i].start =
61 res->irq_resource[i].end = (unsigned long) irq;
62 pcibios_penalize_isa_irq(irq);
67 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
70 while (!(res->dma_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_DMA) i++;
71 if (i < PNP_MAX_DMA) {
72 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
74 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
77 res->dma_resource[i].start =
78 res->dma_resource[i].end = (unsigned long) dma;
83 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
86 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
87 if (i < PNP_MAX_PORT) {
88 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
89 if (len <= 0 || (io + len -1) >= 0x10003) {
90 res->port_resource[i].flags |= IORESOURCE_DISABLED;
93 res->port_resource[i].start = (unsigned long) io;
94 res->port_resource[i].end = (unsigned long)(io + len - 1);
99 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
102 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
103 if (i < PNP_MAX_MEM) {
104 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
106 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
109 res->mem_resource[i].start = (unsigned long) mem;
110 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
114 static unsigned char *
115 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
117 unsigned int len, tag;
118 int io, size, mask, i;
123 /* Blank the resource table values */
124 pnp_init_resource_table(res);
126 while ((char *)p < (char *)end) {
128 /* determine the type of tag */
129 if (p[0] & LARGE_TAG) { /* large tag */
130 len = (p[2] << 8) | p[1];
132 } else { /* small tag */
134 tag = ((p[0]>>3) & 0x0f);
142 io = *(short *) &p[4];
143 size = *(short *) &p[10];
144 pnpbios_parse_allocated_memresource(res, io, size);
147 case LARGE_TAG_ANSISTR:
148 /* ignore this for now */
151 case LARGE_TAG_VENDOR:
155 case LARGE_TAG_MEM32:
159 size = *(int *) &p[16];
160 pnpbios_parse_allocated_memresource(res, io, size);
163 case LARGE_TAG_FIXEDMEM32:
167 size = *(int *) &p[8];
168 pnpbios_parse_allocated_memresource(res, io, size);
172 if (len < 2 || len > 3)
175 mask= p[1] + p[2]*256;
176 for (i=0;i<16;i++, mask=mask>>1)
177 if(mask & 0x01) io=i;
178 pnpbios_parse_allocated_irqresource(res, io);
186 for (i=0;i<8;i++, mask = mask>>1)
187 if(mask & 0x01) io=i;
188 pnpbios_parse_allocated_dmaresource(res, io);
194 io = p[2] + p[3] *256;
196 pnpbios_parse_allocated_ioresource(res, io, size);
199 case SMALL_TAG_VENDOR:
203 case SMALL_TAG_FIXEDPORT:
206 io = p[1] + p[2] * 256;
208 pnpbios_parse_allocated_ioresource(res, io, size);
213 return (unsigned char *)p;
216 default: /* an unkown tag */
218 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
222 /* continue to the next tag */
223 if (p[0] & LARGE_TAG)
229 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
236 * Resource Configuration Options
240 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
242 struct pnp_mem * mem;
243 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
246 mem->min = ((p[5] << 8) | p[4]) << 8;
247 mem->max = ((p[7] << 8) | p[6]) << 8;
248 mem->align = (p[9] << 8) | p[8];
249 mem->size = ((p[11] << 8) | p[10]) << 8;
251 pnp_register_mem_resource(option,mem);
256 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
258 struct pnp_mem * mem;
259 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
262 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
263 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
264 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
265 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
267 pnp_register_mem_resource(option,mem);
272 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
274 struct pnp_mem * mem;
275 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
278 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
279 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
282 pnp_register_mem_resource(option,mem);
287 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
289 struct pnp_irq * irq;
292 irq = pnpbios_kmalloc(sizeof(struct pnp_irq), GFP_KERNEL);
295 bits = (p[2] << 8) | p[1];
296 bitmap_copy(irq->map, &bits, 16);
300 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
301 pnp_register_irq_resource(option,irq);
306 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
308 struct pnp_dma * dma;
309 dma = pnpbios_kmalloc(sizeof(struct pnp_dma), GFP_KERNEL);
314 pnp_register_dma_resource(option,dma);
319 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
321 struct pnp_port * port;
322 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
325 port->min = (p[3] << 8) | p[2];
326 port->max = (p[5] << 8) | p[4];
329 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
330 pnp_register_port_resource(option,port);
335 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
337 struct pnp_port * port;
338 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
341 port->min = port->max = (p[2] << 8) | p[1];
344 port->flags = PNP_PORT_FLAG_FIXED;
345 pnp_register_port_resource(option,port);
349 static unsigned char *
350 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
352 unsigned int len, tag;
354 struct pnp_option *option, *option_independent;
359 option_independent = option = pnp_register_independent_option(dev);
363 while ((char *)p < (char *)end) {
365 /* determine the type of tag */
366 if (p[0] & LARGE_TAG) { /* large tag */
367 len = (p[2] << 8) | p[1];
369 } else { /* small tag */
371 tag = ((p[0]>>3) & 0x0f);
379 pnpbios_parse_mem_option(p, len, option);
382 case LARGE_TAG_MEM32:
385 pnpbios_parse_mem32_option(p, len, option);
388 case LARGE_TAG_FIXEDMEM32:
391 pnpbios_parse_fixed_mem32_option(p, len, option);
395 if (len < 2 || len > 3)
397 pnpbios_parse_irq_option(p, len, option);
403 pnpbios_parse_dma_option(p, len, option);
409 pnpbios_parse_port_option(p, len, option);
412 case SMALL_TAG_VENDOR:
416 case SMALL_TAG_FIXEDPORT:
419 pnpbios_parse_fixed_port_option(p, len, option);
422 case SMALL_TAG_STARTDEP:
425 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
427 priority = 0x100 | p[1];
428 option = pnp_register_dependent_option(dev, priority);
433 case SMALL_TAG_ENDDEP:
436 if (option_independent == option)
437 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
438 option = option_independent;
442 if (option_independent != option)
443 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
445 return (unsigned char *)p;
448 default: /* an unkown tag */
450 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
454 /* continue to the next tag */
455 if (p[0] & LARGE_TAG)
461 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
468 * Compatible Device IDs
471 #define HEX(id,a) hex[((id)>>a) & 15]
472 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
475 void pnpid32_to_pnpid(u32 id, char *str)
477 const char *hex = "0123456789abcdef";
479 id = be32_to_cpu(id);
480 str[0] = CHAR(id, 26);
481 str[1] = CHAR(id, 21);
482 str[2] = CHAR(id,16);
483 str[3] = HEX(id, 12);
495 static unsigned char *
496 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
500 struct pnp_id *dev_id;
505 while ((char *)p < (char *)end) {
507 /* determine the type of tag */
508 if (p[0] & LARGE_TAG) { /* large tag */
509 len = (p[2] << 8) | p[1];
511 } else { /* small tag */
513 tag = ((p[0]>>3) & 0x0f);
518 case LARGE_TAG_ANSISTR:
519 strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
520 dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
523 case SMALL_TAG_COMPATDEVID: /* compatible ID */
526 dev_id = pnpbios_kmalloc(sizeof (struct pnp_id), GFP_KERNEL);
529 memset(dev_id, 0, sizeof(struct pnp_id));
530 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
531 memcpy(&dev_id->id, id, 7);
532 pnp_add_id(dev_id, dev);
537 return (unsigned char *)p;
540 default: /* an unkown tag */
542 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
546 /* continue to the next tag */
547 if (p[0] & LARGE_TAG)
553 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
560 * Allocated Resource Encoding
563 static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
565 unsigned long base = res->start;
566 unsigned long len = res->end - res->start + 1;
567 p[4] = (base >> 8) & 0xff;
568 p[5] = ((base >> 8) >> 8) & 0xff;
569 p[6] = (base >> 8) & 0xff;
570 p[7] = ((base >> 8) >> 8) & 0xff;
571 p[10] = (len >> 8) & 0xff;
572 p[11] = ((len >> 8) >> 8) & 0xff;
576 static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
578 unsigned long base = res->start;
579 unsigned long len = res->end - res->start + 1;
581 p[5] = (base >> 8) & 0xff;
582 p[6] = (base >> 16) & 0xff;
583 p[7] = (base >> 24) & 0xff;
585 p[9] = (base >> 8) & 0xff;
586 p[10] = (base >> 16) & 0xff;
587 p[11] = (base >> 24) & 0xff;
589 p[17] = (len >> 8) & 0xff;
590 p[18] = (len >> 16) & 0xff;
591 p[19] = (len >> 24) & 0xff;
595 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
596 { unsigned long base = res->start;
597 unsigned long len = res->end - res->start + 1;
599 p[5] = (base >> 8) & 0xff;
600 p[6] = (base >> 16) & 0xff;
601 p[7] = (base >> 24) & 0xff;
603 p[9] = (len >> 8) & 0xff;
604 p[10] = (len >> 16) & 0xff;
605 p[11] = (len >> 24) & 0xff;
609 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
611 unsigned long map = 0;
612 map = 1 << res->start;
614 p[2] = (map >> 8) & 0xff;
618 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
620 unsigned long map = 0;
621 map = 1 << res->start;
626 static void pnpbios_encode_port(unsigned char *p, struct resource * res)
628 unsigned long base = res->start;
629 unsigned long len = res->end - res->start + 1;
631 p[3] = (base >> 8) & 0xff;
633 p[5] = (base >> 8) & 0xff;
638 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
640 unsigned long base = res->start;
641 unsigned long len = res->end - res->start + 1;
643 p[2] = (base >> 8) & 0xff;
648 static unsigned char *
649 pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
651 unsigned int len, tag;
652 int port = 0, irq = 0, dma = 0, mem = 0;
657 while ((char *)p < (char *)end) {
659 /* determine the type of tag */
660 if (p[0] & LARGE_TAG) { /* large tag */
661 len = (p[2] << 8) | p[1];
663 } else { /* small tag */
665 tag = ((p[0]>>3) & 0x0f);
673 pnpbios_encode_mem(p, &res->mem_resource[mem]);
677 case LARGE_TAG_MEM32:
680 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
684 case LARGE_TAG_FIXEDMEM32:
687 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
692 if (len < 2 || len > 3)
694 pnpbios_encode_irq(p, &res->irq_resource[irq]);
701 pnpbios_encode_dma(p, &res->dma_resource[dma]);
708 pnpbios_encode_port(p, &res->port_resource[port]);
712 case SMALL_TAG_VENDOR:
716 case SMALL_TAG_FIXEDPORT:
719 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
725 return (unsigned char *)p;
728 default: /* an unkown tag */
730 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
734 /* continue to the next tag */
735 if (p[0] & LARGE_TAG)
741 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
748 * Core Parsing Functions
752 pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
754 unsigned char * p = (char *)node->data;
755 unsigned char * end = (char *)(node->data + node->size);
756 p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
759 p = pnpbios_parse_resource_option_data(p,end,dev);
762 p = pnpbios_parse_compatible_ids(p,end,dev);
769 pnpbios_read_resources_from_node(struct pnp_resource_table *res,
770 struct pnp_bios_node * node)
772 unsigned char * p = (char *)node->data;
773 unsigned char * end = (char *)(node->data + node->size);
774 p = pnpbios_parse_allocated_resource_data(p,end,res);
781 pnpbios_write_resources_to_node(struct pnp_resource_table *res,
782 struct pnp_bios_node * node)
784 unsigned char * p = (char *)node->data;
785 unsigned char * end = (char *)(node->data + node->size);
786 p = pnpbios_encode_allocated_resource_data(p,end,res);