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/string.h>
11 #include <linux/slab.h>
14 #include <linux/pci.h>
16 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)
57 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
60 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
61 if (i < PNP_MAX_IRQ) {
62 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
64 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
67 res->irq_resource[i].start =
68 res->irq_resource[i].end = (unsigned long) irq;
69 pcibios_penalize_isa_irq(irq, 1);
74 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
77 while (i < PNP_MAX_DMA &&
78 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
80 if (i < PNP_MAX_DMA) {
81 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
83 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
86 res->dma_resource[i].start =
87 res->dma_resource[i].end = (unsigned long) dma;
92 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
95 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
96 if (i < PNP_MAX_PORT) {
97 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
98 if (len <= 0 || (io + len -1) >= 0x10003) {
99 res->port_resource[i].flags |= IORESOURCE_DISABLED;
102 res->port_resource[i].start = (unsigned long) io;
103 res->port_resource[i].end = (unsigned long)(io + len - 1);
108 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
111 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
112 if (i < PNP_MAX_MEM) {
113 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
115 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
118 res->mem_resource[i].start = (unsigned long) mem;
119 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
123 static unsigned char *
124 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
126 unsigned int len, tag;
127 int io, size, mask, i;
132 /* Blank the resource table values */
133 pnp_init_resource_table(res);
135 while ((char *)p < (char *)end) {
137 /* determine the type of tag */
138 if (p[0] & LARGE_TAG) { /* large tag */
139 len = (p[2] << 8) | p[1];
141 } else { /* small tag */
143 tag = ((p[0]>>3) & 0x0f);
151 io = *(short *) &p[4];
152 size = *(short *) &p[10];
153 pnpbios_parse_allocated_memresource(res, io, size);
156 case LARGE_TAG_ANSISTR:
157 /* ignore this for now */
160 case LARGE_TAG_VENDOR:
164 case LARGE_TAG_MEM32:
168 size = *(int *) &p[16];
169 pnpbios_parse_allocated_memresource(res, io, size);
172 case LARGE_TAG_FIXEDMEM32:
176 size = *(int *) &p[8];
177 pnpbios_parse_allocated_memresource(res, io, size);
181 if (len < 2 || len > 3)
184 mask= p[1] + p[2]*256;
185 for (i=0;i<16;i++, mask=mask>>1)
186 if(mask & 0x01) io=i;
187 pnpbios_parse_allocated_irqresource(res, io);
195 for (i=0;i<8;i++, mask = mask>>1)
196 if(mask & 0x01) io=i;
197 pnpbios_parse_allocated_dmaresource(res, io);
203 io = p[2] + p[3] *256;
205 pnpbios_parse_allocated_ioresource(res, io, size);
208 case SMALL_TAG_VENDOR:
212 case SMALL_TAG_FIXEDPORT:
215 io = p[1] + p[2] * 256;
217 pnpbios_parse_allocated_ioresource(res, io, size);
222 return (unsigned char *)p;
225 default: /* an unkown tag */
227 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
231 /* continue to the next tag */
232 if (p[0] & LARGE_TAG)
238 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
245 * Resource Configuration Options
249 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
251 struct pnp_mem * mem;
252 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
255 mem->min = ((p[5] << 8) | p[4]) << 8;
256 mem->max = ((p[7] << 8) | p[6]) << 8;
257 mem->align = (p[9] << 8) | p[8];
258 mem->size = ((p[11] << 8) | p[10]) << 8;
260 pnp_register_mem_resource(option,mem);
265 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
267 struct pnp_mem * mem;
268 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
271 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
272 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
273 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
274 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
276 pnp_register_mem_resource(option,mem);
281 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
283 struct pnp_mem * mem;
284 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
287 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
288 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
291 pnp_register_mem_resource(option,mem);
296 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
298 struct pnp_irq * irq;
301 irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
304 bits = (p[2] << 8) | p[1];
305 bitmap_copy(irq->map, &bits, 16);
309 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
310 pnp_register_irq_resource(option,irq);
315 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
317 struct pnp_dma * dma;
318 dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL);
323 pnp_register_dma_resource(option,dma);
328 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
330 struct pnp_port * port;
331 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
334 port->min = (p[3] << 8) | p[2];
335 port->max = (p[5] << 8) | p[4];
338 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
339 pnp_register_port_resource(option,port);
344 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
346 struct pnp_port * port;
347 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
350 port->min = port->max = (p[2] << 8) | p[1];
353 port->flags = PNP_PORT_FLAG_FIXED;
354 pnp_register_port_resource(option,port);
358 static unsigned char *
359 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
361 unsigned int len, tag;
363 struct pnp_option *option, *option_independent;
368 option_independent = option = pnp_register_independent_option(dev);
372 while ((char *)p < (char *)end) {
374 /* determine the type of tag */
375 if (p[0] & LARGE_TAG) { /* large tag */
376 len = (p[2] << 8) | p[1];
378 } else { /* small tag */
380 tag = ((p[0]>>3) & 0x0f);
388 pnpbios_parse_mem_option(p, len, option);
391 case LARGE_TAG_MEM32:
394 pnpbios_parse_mem32_option(p, len, option);
397 case LARGE_TAG_FIXEDMEM32:
400 pnpbios_parse_fixed_mem32_option(p, len, option);
404 if (len < 2 || len > 3)
406 pnpbios_parse_irq_option(p, len, option);
412 pnpbios_parse_dma_option(p, len, option);
418 pnpbios_parse_port_option(p, len, option);
421 case SMALL_TAG_VENDOR:
425 case SMALL_TAG_FIXEDPORT:
428 pnpbios_parse_fixed_port_option(p, len, option);
431 case SMALL_TAG_STARTDEP:
434 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
436 priority = 0x100 | p[1];
437 option = pnp_register_dependent_option(dev, priority);
442 case SMALL_TAG_ENDDEP:
445 if (option_independent == option)
446 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
447 option = option_independent;
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 = kcalloc(1, 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);