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>
13 /* standard resource tags */
14 #define SMALL_TAG_PNPVERNO 0x01
15 #define SMALL_TAG_LOGDEVID 0x02
16 #define SMALL_TAG_COMPATDEVID 0x03
17 #define SMALL_TAG_IRQ 0x04
18 #define SMALL_TAG_DMA 0x05
19 #define SMALL_TAG_STARTDEP 0x06
20 #define SMALL_TAG_ENDDEP 0x07
21 #define SMALL_TAG_PORT 0x08
22 #define SMALL_TAG_FIXEDPORT 0x09
23 #define SMALL_TAG_VENDOR 0x0e
24 #define SMALL_TAG_END 0x0f
25 #define LARGE_TAG 0x80
26 #define LARGE_TAG_MEM 0x81
27 #define LARGE_TAG_ANSISTR 0x82
28 #define LARGE_TAG_UNICODESTR 0x83
29 #define LARGE_TAG_VENDOR 0x84
30 #define LARGE_TAG_MEM32 0x85
31 #define LARGE_TAG_FIXEDMEM32 0x86
34 * Resource Data Stream Format:
36 * Allocated Resources (required)
38 * Resource Configuration Options (optional)
40 * Compitable Device IDs (optional)
49 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
52 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
53 if (i < PNP_MAX_IRQ) {
54 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
56 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
59 res->irq_resource[i].start =
60 res->irq_resource[i].end = (unsigned long) irq;
65 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
68 while (!(res->dma_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_DMA) i++;
69 if (i < PNP_MAX_DMA) {
70 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
72 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
75 res->dma_resource[i].start =
76 res->dma_resource[i].end = (unsigned long) dma;
81 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
84 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
85 if (i < PNP_MAX_PORT) {
86 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
87 if (len <= 0 || (io + len -1) >= 0x10003) {
88 res->port_resource[i].flags |= IORESOURCE_DISABLED;
91 res->port_resource[i].start = (unsigned long) io;
92 res->port_resource[i].end = (unsigned long)(io + len - 1);
97 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
100 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
101 if (i < PNP_MAX_MEM) {
102 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
104 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
107 res->mem_resource[i].start = (unsigned long) mem;
108 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
112 static unsigned char *
113 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
115 unsigned int len, tag;
116 int io, size, mask, i;
121 /* Blank the resource table values */
122 pnp_init_resource_table(res);
124 while ((char *)p < (char *)end) {
126 /* determine the type of tag */
127 if (p[0] & LARGE_TAG) { /* large tag */
128 len = (p[2] << 8) | p[1];
130 } else { /* small tag */
132 tag = ((p[0]>>3) & 0x0f);
140 io = *(short *) &p[4];
141 size = *(short *) &p[10];
142 pnpbios_parse_allocated_memresource(res, io, size);
145 case LARGE_TAG_ANSISTR:
146 /* ignore this for now */
149 case LARGE_TAG_VENDOR:
153 case LARGE_TAG_MEM32:
157 size = *(int *) &p[16];
158 pnpbios_parse_allocated_memresource(res, io, size);
161 case LARGE_TAG_FIXEDMEM32:
165 size = *(int *) &p[8];
166 pnpbios_parse_allocated_memresource(res, io, size);
170 if (len < 2 || len > 3)
173 mask= p[1] + p[2]*256;
174 for (i=0;i<16;i++, mask=mask>>1)
175 if(mask & 0x01) io=i;
176 pnpbios_parse_allocated_irqresource(res, io);
184 for (i=0;i<8;i++, mask = mask>>1)
185 if(mask & 0x01) io=i;
186 pnpbios_parse_allocated_dmaresource(res, io);
192 io = p[2] + p[3] *256;
194 pnpbios_parse_allocated_ioresource(res, io, size);
197 case SMALL_TAG_VENDOR:
201 case SMALL_TAG_FIXEDPORT:
204 io = p[1] + p[2] * 256;
206 pnpbios_parse_allocated_ioresource(res, io, size);
211 return (unsigned char *)p;
214 default: /* an unkown tag */
216 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
220 /* continue to the next tag */
221 if (p[0] & LARGE_TAG)
227 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
234 * Resource Configuration Options
238 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
240 struct pnp_mem * mem;
241 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
244 mem->min = ((p[5] << 8) | p[4]) << 8;
245 mem->max = ((p[7] << 8) | p[6]) << 8;
246 mem->align = (p[9] << 8) | p[8];
247 mem->size = ((p[11] << 8) | p[10]) << 8;
249 pnp_register_mem_resource(option,mem);
254 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
256 struct pnp_mem * mem;
257 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
260 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
261 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
262 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
263 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
265 pnp_register_mem_resource(option,mem);
270 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
272 struct pnp_mem * mem;
273 mem = pnpbios_kmalloc(sizeof(struct pnp_mem), GFP_KERNEL);
276 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
277 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
280 pnp_register_mem_resource(option,mem);
285 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
287 struct pnp_irq * irq;
288 irq = pnpbios_kmalloc(sizeof(struct pnp_irq), GFP_KERNEL);
291 irq->map = (p[2] << 8) | p[1];
295 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
296 pnp_register_irq_resource(option,irq);
301 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
303 struct pnp_dma * dma;
304 dma = pnpbios_kmalloc(sizeof(struct pnp_dma), GFP_KERNEL);
309 pnp_register_dma_resource(option,dma);
314 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
316 struct pnp_port * port;
317 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
320 port->min = (p[3] << 8) | p[2];
321 port->max = (p[5] << 8) | p[4];
324 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
325 pnp_register_port_resource(option,port);
330 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
332 struct pnp_port * port;
333 port = pnpbios_kmalloc(sizeof(struct pnp_port), GFP_KERNEL);
336 port->min = port->max = (p[2] << 8) | p[1];
339 port->flags = PNP_PORT_FLAG_FIXED;
340 pnp_register_port_resource(option,port);
344 static unsigned char *
345 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
347 unsigned int len, tag;
349 struct pnp_option *option;
354 option = pnp_register_independent_option(dev);
358 while ((char *)p < (char *)end) {
360 /* determine the type of tag */
361 if (p[0] & LARGE_TAG) { /* large tag */
362 len = (p[2] << 8) | p[1];
364 } else { /* small tag */
366 tag = ((p[0]>>3) & 0x0f);
374 pnpbios_parse_mem_option(p, len, option);
377 case LARGE_TAG_MEM32:
380 pnpbios_parse_mem32_option(p, len, option);
383 case LARGE_TAG_FIXEDMEM32:
386 pnpbios_parse_fixed_mem32_option(p, len, option);
390 if (len < 2 || len > 3)
392 pnpbios_parse_irq_option(p, len, option);
398 pnpbios_parse_dma_option(p, len, option);
404 pnpbios_parse_port_option(p, len, option);
407 case SMALL_TAG_VENDOR:
411 case SMALL_TAG_FIXEDPORT:
414 pnpbios_parse_fixed_port_option(p, len, option);
417 case SMALL_TAG_STARTDEP:
420 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
422 priority = 0x100 | p[1];
423 option = pnp_register_dependent_option(dev, priority);
428 case SMALL_TAG_ENDDEP:
435 return (unsigned char *)p;
438 default: /* an unkown tag */
440 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
444 /* continue to the next tag */
445 if (p[0] & LARGE_TAG)
451 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
458 * Compatible Device IDs
461 #define HEX(id,a) hex[((id)>>a) & 15]
462 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
465 void pnpid32_to_pnpid(u32 id, char *str)
467 const char *hex = "0123456789abcdef";
469 id = be32_to_cpu(id);
470 str[0] = CHAR(id, 26);
471 str[1] = CHAR(id, 21);
472 str[2] = CHAR(id,16);
473 str[3] = HEX(id, 12);
485 static unsigned char *
486 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
490 struct pnp_id *dev_id;
495 while ((char *)p < (char *)end) {
497 /* determine the type of tag */
498 if (p[0] & LARGE_TAG) { /* large tag */
499 len = (p[2] << 8) | p[1];
501 } else { /* small tag */
503 tag = ((p[0]>>3) & 0x0f);
508 case LARGE_TAG_ANSISTR:
509 strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
510 dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
513 case SMALL_TAG_COMPATDEVID: /* compatible ID */
516 dev_id = pnpbios_kmalloc(sizeof (struct pnp_id), GFP_KERNEL);
519 memset(dev_id, 0, sizeof(struct pnp_id));
520 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
521 memcpy(&dev_id->id, id, 7);
522 pnp_add_id(dev_id, dev);
527 return (unsigned char *)p;
530 default: /* an unkown tag */
532 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
536 /* continue to the next tag */
537 if (p[0] & LARGE_TAG)
543 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
550 * Allocated Resource Encoding
553 static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
555 unsigned long base = res->start;
556 unsigned long len = res->end - res->start + 1;
557 p[4] = (base >> 8) & 0xff;
558 p[5] = ((base >> 8) >> 8) & 0xff;
559 p[6] = (base >> 8) & 0xff;
560 p[7] = ((base >> 8) >> 8) & 0xff;
561 p[10] = (len >> 8) & 0xff;
562 p[11] = ((len >> 8) >> 8) & 0xff;
566 static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
568 unsigned long base = res->start;
569 unsigned long len = res->end - res->start + 1;
571 p[5] = (base >> 8) & 0xff;
572 p[6] = (base >> 16) & 0xff;
573 p[7] = (base >> 24) & 0xff;
575 p[9] = (base >> 8) & 0xff;
576 p[10] = (base >> 16) & 0xff;
577 p[11] = (base >> 24) & 0xff;
579 p[17] = (len >> 8) & 0xff;
580 p[18] = (len >> 16) & 0xff;
581 p[19] = (len >> 24) & 0xff;
585 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
586 { unsigned long base = res->start;
587 unsigned long len = res->end - res->start + 1;
589 p[5] = (base >> 8) & 0xff;
590 p[6] = (base >> 16) & 0xff;
591 p[7] = (base >> 24) & 0xff;
593 p[9] = (len >> 8) & 0xff;
594 p[10] = (len >> 16) & 0xff;
595 p[11] = (len >> 24) & 0xff;
599 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
601 unsigned long map = 0;
602 map = 1 << res->start;
604 p[2] = (map >> 8) & 0xff;
608 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
610 unsigned long map = 0;
611 map = 1 << res->start;
616 static void pnpbios_encode_port(unsigned char *p, struct resource * res)
618 unsigned long base = res->start;
619 unsigned long len = res->end - res->start + 1;
621 p[3] = (base >> 8) & 0xff;
623 p[5] = (base >> 8) & 0xff;
628 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
630 unsigned long base = res->start;
631 unsigned long len = res->end - res->start + 1;
633 p[2] = (base >> 8) & 0xff;
638 static unsigned char *
639 pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
641 unsigned int len, tag;
642 int port = 0, irq = 0, dma = 0, mem = 0;
647 while ((char *)p < (char *)end) {
649 /* determine the type of tag */
650 if (p[0] & LARGE_TAG) { /* large tag */
651 len = (p[2] << 8) | p[1];
653 } else { /* small tag */
655 tag = ((p[0]>>3) & 0x0f);
663 pnpbios_encode_mem(p, &res->mem_resource[mem]);
667 case LARGE_TAG_MEM32:
670 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
674 case LARGE_TAG_FIXEDMEM32:
677 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
682 if (len < 2 || len > 3)
684 pnpbios_encode_irq(p, &res->irq_resource[irq]);
691 pnpbios_encode_dma(p, &res->dma_resource[dma]);
698 pnpbios_encode_port(p, &res->port_resource[port]);
702 case SMALL_TAG_VENDOR:
706 case SMALL_TAG_FIXEDPORT:
709 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
715 return (unsigned char *)p;
718 default: /* an unkown tag */
720 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
724 /* continue to the next tag */
725 if (p[0] & LARGE_TAG)
731 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
738 * Core Parsing Functions
742 pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
744 unsigned char * p = (char *)node->data;
745 unsigned char * end = (char *)(node->data + node->size);
746 p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
749 p = pnpbios_parse_resource_option_data(p,end,dev);
752 p = pnpbios_parse_compatible_ids(p,end,dev);
759 pnpbios_read_resources_from_node(struct pnp_resource_table *res,
760 struct pnp_bios_node * node)
762 unsigned char * p = (char *)node->data;
763 unsigned char * end = (char *)(node->data + node->size);
764 p = pnpbios_parse_allocated_resource_data(p,end,res);
771 pnpbios_write_resources_to_node(struct pnp_resource_table *res,
772 struct pnp_bios_node * node)
774 unsigned char * p = (char *)node->data;
775 unsigned char * end = (char *)(node->data + node->size);
776 p = pnpbios_encode_allocated_resource_data(p,end,res);