2 * rsparser.c - parses and encodes pnpbios resource data streams
6 #include <linux/ctype.h>
8 #include <linux/pnpbios.h>
9 #include <linux/string.h>
10 #include <linux/slab.h>
13 #include <linux/pci.h>
15 inline void pcibios_penalize_isa_irq(int irq, int active) {}
16 #endif /* CONFIG_PCI */
20 /* standard resource tags */
21 #define SMALL_TAG_PNPVERNO 0x01
22 #define SMALL_TAG_LOGDEVID 0x02
23 #define SMALL_TAG_COMPATDEVID 0x03
24 #define SMALL_TAG_IRQ 0x04
25 #define SMALL_TAG_DMA 0x05
26 #define SMALL_TAG_STARTDEP 0x06
27 #define SMALL_TAG_ENDDEP 0x07
28 #define SMALL_TAG_PORT 0x08
29 #define SMALL_TAG_FIXEDPORT 0x09
30 #define SMALL_TAG_VENDOR 0x0e
31 #define SMALL_TAG_END 0x0f
32 #define LARGE_TAG 0x80
33 #define LARGE_TAG_MEM 0x81
34 #define LARGE_TAG_ANSISTR 0x82
35 #define LARGE_TAG_UNICODESTR 0x83
36 #define LARGE_TAG_VENDOR 0x84
37 #define LARGE_TAG_MEM32 0x85
38 #define LARGE_TAG_FIXEDMEM32 0x86
41 * Resource Data Stream Format:
43 * Allocated Resources (required)
45 * Resource Configuration Options (optional)
47 * Compitable Device IDs (optional)
56 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
59 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
60 if (i < PNP_MAX_IRQ) {
61 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
63 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
66 res->irq_resource[i].start =
67 res->irq_resource[i].end = (unsigned long) irq;
68 pcibios_penalize_isa_irq(irq, 1);
73 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
76 while (i < PNP_MAX_DMA &&
77 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
79 if (i < PNP_MAX_DMA) {
80 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
82 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
85 res->dma_resource[i].start =
86 res->dma_resource[i].end = (unsigned long) dma;
91 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
94 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
95 if (i < PNP_MAX_PORT) {
96 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
97 if (len <= 0 || (io + len -1) >= 0x10003) {
98 res->port_resource[i].flags |= IORESOURCE_DISABLED;
101 res->port_resource[i].start = (unsigned long) io;
102 res->port_resource[i].end = (unsigned long)(io + len - 1);
107 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
110 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
111 if (i < PNP_MAX_MEM) {
112 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
114 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
117 res->mem_resource[i].start = (unsigned long) mem;
118 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
122 static unsigned char *
123 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
125 unsigned int len, tag;
126 int io, size, mask, i;
131 /* Blank the resource table values */
132 pnp_init_resource_table(res);
134 while ((char *)p < (char *)end) {
136 /* determine the type of tag */
137 if (p[0] & LARGE_TAG) { /* large tag */
138 len = (p[2] << 8) | p[1];
140 } else { /* small tag */
142 tag = ((p[0]>>3) & 0x0f);
150 io = *(short *) &p[4];
151 size = *(short *) &p[10];
152 pnpbios_parse_allocated_memresource(res, io, size);
155 case LARGE_TAG_ANSISTR:
156 /* ignore this for now */
159 case LARGE_TAG_VENDOR:
163 case LARGE_TAG_MEM32:
167 size = *(int *) &p[16];
168 pnpbios_parse_allocated_memresource(res, io, size);
171 case LARGE_TAG_FIXEDMEM32:
175 size = *(int *) &p[8];
176 pnpbios_parse_allocated_memresource(res, io, size);
180 if (len < 2 || len > 3)
183 mask= p[1] + p[2]*256;
184 for (i=0;i<16;i++, mask=mask>>1)
185 if(mask & 0x01) io=i;
186 pnpbios_parse_allocated_irqresource(res, io);
194 for (i=0;i<8;i++, mask = mask>>1)
195 if(mask & 0x01) io=i;
196 pnpbios_parse_allocated_dmaresource(res, io);
202 io = p[2] + p[3] *256;
204 pnpbios_parse_allocated_ioresource(res, io, size);
207 case SMALL_TAG_VENDOR:
211 case SMALL_TAG_FIXEDPORT:
214 io = p[1] + p[2] * 256;
216 pnpbios_parse_allocated_ioresource(res, io, size);
221 return (unsigned char *)p;
224 default: /* an unkown tag */
226 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
230 /* continue to the next tag */
231 if (p[0] & LARGE_TAG)
237 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
244 * Resource Configuration Options
248 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
250 struct pnp_mem * mem;
251 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
254 mem->min = ((p[5] << 8) | p[4]) << 8;
255 mem->max = ((p[7] << 8) | p[6]) << 8;
256 mem->align = (p[9] << 8) | p[8];
257 mem->size = ((p[11] << 8) | p[10]) << 8;
259 pnp_register_mem_resource(option,mem);
264 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
266 struct pnp_mem * mem;
267 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
270 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
271 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
272 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
273 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
275 pnp_register_mem_resource(option,mem);
280 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
282 struct pnp_mem * mem;
283 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
286 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
287 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
290 pnp_register_mem_resource(option,mem);
295 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
297 struct pnp_irq * irq;
300 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
303 bits = (p[2] << 8) | p[1];
304 bitmap_copy(irq->map, &bits, 16);
308 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
309 pnp_register_irq_resource(option,irq);
314 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
316 struct pnp_dma * dma;
317 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
322 pnp_register_dma_resource(option,dma);
327 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
329 struct pnp_port * port;
330 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
333 port->min = (p[3] << 8) | p[2];
334 port->max = (p[5] << 8) | p[4];
337 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
338 pnp_register_port_resource(option,port);
343 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
345 struct pnp_port * port;
346 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
349 port->min = port->max = (p[2] << 8) | p[1];
352 port->flags = PNP_PORT_FLAG_FIXED;
353 pnp_register_port_resource(option,port);
357 static unsigned char *
358 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
360 unsigned int len, tag;
362 struct pnp_option *option, *option_independent;
367 option_independent = option = pnp_register_independent_option(dev);
371 while ((char *)p < (char *)end) {
373 /* determine the type of tag */
374 if (p[0] & LARGE_TAG) { /* large tag */
375 len = (p[2] << 8) | p[1];
377 } else { /* small tag */
379 tag = ((p[0]>>3) & 0x0f);
387 pnpbios_parse_mem_option(p, len, option);
390 case LARGE_TAG_MEM32:
393 pnpbios_parse_mem32_option(p, len, option);
396 case LARGE_TAG_FIXEDMEM32:
399 pnpbios_parse_fixed_mem32_option(p, len, option);
403 if (len < 2 || len > 3)
405 pnpbios_parse_irq_option(p, len, option);
411 pnpbios_parse_dma_option(p, len, option);
417 pnpbios_parse_port_option(p, len, option);
420 case SMALL_TAG_VENDOR:
424 case SMALL_TAG_FIXEDPORT:
427 pnpbios_parse_fixed_port_option(p, len, option);
430 case SMALL_TAG_STARTDEP:
433 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
435 priority = 0x100 | p[1];
436 option = pnp_register_dependent_option(dev, priority);
441 case SMALL_TAG_ENDDEP:
444 if (option_independent == option)
445 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
446 option = option_independent;
452 default: /* an unkown tag */
454 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
458 /* continue to the next tag */
459 if (p[0] & LARGE_TAG)
465 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
472 * Compatible Device IDs
475 #define HEX(id,a) hex[((id)>>a) & 15]
476 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
479 void pnpid32_to_pnpid(u32 id, char *str)
481 const char *hex = "0123456789abcdef";
483 id = be32_to_cpu(id);
484 str[0] = CHAR(id, 26);
485 str[1] = CHAR(id, 21);
486 str[2] = CHAR(id,16);
487 str[3] = HEX(id, 12);
499 static unsigned char *
500 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
504 struct pnp_id *dev_id;
509 while ((char *)p < (char *)end) {
511 /* determine the type of tag */
512 if (p[0] & LARGE_TAG) { /* large tag */
513 len = (p[2] << 8) | p[1];
515 } else { /* small tag */
517 tag = ((p[0]>>3) & 0x0f);
522 case LARGE_TAG_ANSISTR:
523 strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
524 dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
527 case SMALL_TAG_COMPATDEVID: /* compatible ID */
530 dev_id = kzalloc(sizeof (struct pnp_id), GFP_KERNEL);
533 memset(dev_id, 0, sizeof(struct pnp_id));
534 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
535 memcpy(&dev_id->id, id, 7);
536 pnp_add_id(dev_id, dev);
541 return (unsigned char *)p;
544 default: /* an unkown tag */
546 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
550 /* continue to the next tag */
551 if (p[0] & LARGE_TAG)
557 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
564 * Allocated Resource Encoding
567 static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
569 unsigned long base = res->start;
570 unsigned long len = res->end - res->start + 1;
571 p[4] = (base >> 8) & 0xff;
572 p[5] = ((base >> 8) >> 8) & 0xff;
573 p[6] = (base >> 8) & 0xff;
574 p[7] = ((base >> 8) >> 8) & 0xff;
575 p[10] = (len >> 8) & 0xff;
576 p[11] = ((len >> 8) >> 8) & 0xff;
580 static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
582 unsigned long base = res->start;
583 unsigned long len = res->end - res->start + 1;
585 p[5] = (base >> 8) & 0xff;
586 p[6] = (base >> 16) & 0xff;
587 p[7] = (base >> 24) & 0xff;
589 p[9] = (base >> 8) & 0xff;
590 p[10] = (base >> 16) & 0xff;
591 p[11] = (base >> 24) & 0xff;
593 p[17] = (len >> 8) & 0xff;
594 p[18] = (len >> 16) & 0xff;
595 p[19] = (len >> 24) & 0xff;
599 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
600 { unsigned long base = res->start;
601 unsigned long len = res->end - res->start + 1;
603 p[5] = (base >> 8) & 0xff;
604 p[6] = (base >> 16) & 0xff;
605 p[7] = (base >> 24) & 0xff;
607 p[9] = (len >> 8) & 0xff;
608 p[10] = (len >> 16) & 0xff;
609 p[11] = (len >> 24) & 0xff;
613 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
615 unsigned long map = 0;
616 map = 1 << res->start;
618 p[2] = (map >> 8) & 0xff;
622 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
624 unsigned long map = 0;
625 map = 1 << res->start;
630 static void pnpbios_encode_port(unsigned char *p, struct resource * res)
632 unsigned long base = res->start;
633 unsigned long len = res->end - res->start + 1;
635 p[3] = (base >> 8) & 0xff;
637 p[5] = (base >> 8) & 0xff;
642 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
644 unsigned long base = res->start;
645 unsigned long len = res->end - res->start + 1;
647 p[2] = (base >> 8) & 0xff;
652 static unsigned char *
653 pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
655 unsigned int len, tag;
656 int port = 0, irq = 0, dma = 0, mem = 0;
661 while ((char *)p < (char *)end) {
663 /* determine the type of tag */
664 if (p[0] & LARGE_TAG) { /* large tag */
665 len = (p[2] << 8) | p[1];
667 } else { /* small tag */
669 tag = ((p[0]>>3) & 0x0f);
677 pnpbios_encode_mem(p, &res->mem_resource[mem]);
681 case LARGE_TAG_MEM32:
684 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
688 case LARGE_TAG_FIXEDMEM32:
691 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
696 if (len < 2 || len > 3)
698 pnpbios_encode_irq(p, &res->irq_resource[irq]);
705 pnpbios_encode_dma(p, &res->dma_resource[dma]);
712 pnpbios_encode_port(p, &res->port_resource[port]);
716 case SMALL_TAG_VENDOR:
720 case SMALL_TAG_FIXEDPORT:
723 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
729 return (unsigned char *)p;
732 default: /* an unkown tag */
734 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
738 /* continue to the next tag */
739 if (p[0] & LARGE_TAG)
745 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
752 * Core Parsing Functions
756 pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
758 unsigned char * p = (char *)node->data;
759 unsigned char * end = (char *)(node->data + node->size);
760 p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
763 p = pnpbios_parse_resource_option_data(p,end,dev);
766 p = pnpbios_parse_compatible_ids(p,end,dev);
773 pnpbios_read_resources_from_node(struct pnp_resource_table *res,
774 struct pnp_bios_node * node)
776 unsigned char * p = (char *)node->data;
777 unsigned char * end = (char *)(node->data + node->size);
778 p = pnpbios_parse_allocated_resource_data(p,end,res);
785 pnpbios_write_resources_to_node(struct pnp_resource_table *res,
786 struct pnp_bios_node * node)
788 unsigned char * p = (char *)node->data;
789 unsigned char * end = (char *)(node->data + node->size);
790 p = pnpbios_encode_allocated_resource_data(p,end,res);