2 * rsparser.c - parses and encodes pnpbios resource data streams
5 #include <linux/ctype.h>
7 #include <linux/pnpbios.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
12 #include <linux/pci.h>
14 inline void pcibios_penalize_isa_irq(int irq
, int active
)
17 #endif /* CONFIG_PCI */
22 /* standard resource tags */
23 #define SMALL_TAG_PNPVERNO 0x01
24 #define SMALL_TAG_LOGDEVID 0x02
25 #define SMALL_TAG_COMPATDEVID 0x03
26 #define SMALL_TAG_IRQ 0x04
27 #define SMALL_TAG_DMA 0x05
28 #define SMALL_TAG_STARTDEP 0x06
29 #define SMALL_TAG_ENDDEP 0x07
30 #define SMALL_TAG_PORT 0x08
31 #define SMALL_TAG_FIXEDPORT 0x09
32 #define SMALL_TAG_VENDOR 0x0e
33 #define SMALL_TAG_END 0x0f
34 #define LARGE_TAG 0x80
35 #define LARGE_TAG_MEM 0x81
36 #define LARGE_TAG_ANSISTR 0x82
37 #define LARGE_TAG_UNICODESTR 0x83
38 #define LARGE_TAG_VENDOR 0x84
39 #define LARGE_TAG_MEM32 0x85
40 #define LARGE_TAG_FIXEDMEM32 0x86
43 * Resource Data Stream Format:
45 * Allocated Resources (required)
47 * Resource Configuration Options (optional)
49 * Compitable Device IDs (optional)
57 static void pnpbios_parse_allocated_irqresource(struct pnp_dev
*dev
, int irq
)
59 struct pnp_resource_table
*res
= &dev
->res
;
62 while (!(res
->irq_resource
[i
].flags
& IORESOURCE_UNSET
)
65 if (i
< PNP_MAX_IRQ
) {
66 res
->irq_resource
[i
].flags
= IORESOURCE_IRQ
; // Also clears _UNSET flag
68 res
->irq_resource
[i
].flags
|= IORESOURCE_DISABLED
;
71 res
->irq_resource
[i
].start
=
72 res
->irq_resource
[i
].end
= (unsigned long)irq
;
73 pcibios_penalize_isa_irq(irq
, 1);
77 static void pnpbios_parse_allocated_dmaresource(struct pnp_dev
*dev
, int dma
)
79 struct pnp_resource_table
*res
= &dev
->res
;
82 while (i
< PNP_MAX_DMA
&&
83 !(res
->dma_resource
[i
].flags
& IORESOURCE_UNSET
))
85 if (i
< PNP_MAX_DMA
) {
86 res
->dma_resource
[i
].flags
= IORESOURCE_DMA
; // Also clears _UNSET flag
88 res
->dma_resource
[i
].flags
|= IORESOURCE_DISABLED
;
91 res
->dma_resource
[i
].start
=
92 res
->dma_resource
[i
].end
= (unsigned long)dma
;
96 static void pnpbios_parse_allocated_ioresource(struct pnp_dev
*dev
,
99 struct pnp_resource_table
*res
= &dev
->res
;
102 while (!(res
->port_resource
[i
].flags
& IORESOURCE_UNSET
)
105 if (i
< PNP_MAX_PORT
) {
106 res
->port_resource
[i
].flags
= IORESOURCE_IO
; // Also clears _UNSET flag
107 if (len
<= 0 || (io
+ len
- 1) >= 0x10003) {
108 res
->port_resource
[i
].flags
|= IORESOURCE_DISABLED
;
111 res
->port_resource
[i
].start
= (unsigned long)io
;
112 res
->port_resource
[i
].end
= (unsigned long)(io
+ len
- 1);
116 static void pnpbios_parse_allocated_memresource(struct pnp_dev
*dev
,
119 struct pnp_resource_table
*res
= &dev
->res
;
122 while (!(res
->mem_resource
[i
].flags
& IORESOURCE_UNSET
)
125 if (i
< PNP_MAX_MEM
) {
126 res
->mem_resource
[i
].flags
= IORESOURCE_MEM
; // Also clears _UNSET flag
128 res
->mem_resource
[i
].flags
|= IORESOURCE_DISABLED
;
131 res
->mem_resource
[i
].start
= (unsigned long)mem
;
132 res
->mem_resource
[i
].end
= (unsigned long)(mem
+ len
- 1);
136 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev
*dev
,
140 unsigned int len
, tag
;
141 int io
, size
, mask
, i
;
146 dev_dbg(&dev
->dev
, "parse allocated resources\n");
148 pnp_init_resources(dev
);
150 while ((char *)p
< (char *)end
) {
152 /* determine the type of tag */
153 if (p
[0] & LARGE_TAG
) { /* large tag */
154 len
= (p
[2] << 8) | p
[1];
156 } else { /* small tag */
158 tag
= ((p
[0] >> 3) & 0x0f);
166 io
= *(short *)&p
[4];
167 size
= *(short *)&p
[10];
168 pnpbios_parse_allocated_memresource(dev
, io
, size
);
171 case LARGE_TAG_ANSISTR
:
172 /* ignore this for now */
175 case LARGE_TAG_VENDOR
:
179 case LARGE_TAG_MEM32
:
183 size
= *(int *)&p
[16];
184 pnpbios_parse_allocated_memresource(dev
, io
, size
);
187 case LARGE_TAG_FIXEDMEM32
:
191 size
= *(int *)&p
[8];
192 pnpbios_parse_allocated_memresource(dev
, io
, size
);
196 if (len
< 2 || len
> 3)
199 mask
= p
[1] + p
[2] * 256;
200 for (i
= 0; i
< 16; i
++, mask
= mask
>> 1)
203 pnpbios_parse_allocated_irqresource(dev
, io
);
211 for (i
= 0; i
< 8; i
++, mask
= mask
>> 1)
214 pnpbios_parse_allocated_dmaresource(dev
, io
);
220 io
= p
[2] + p
[3] * 256;
222 pnpbios_parse_allocated_ioresource(dev
, io
, size
);
225 case SMALL_TAG_VENDOR
:
229 case SMALL_TAG_FIXEDPORT
:
232 io
= p
[1] + p
[2] * 256;
234 pnpbios_parse_allocated_ioresource(dev
, io
, size
);
239 return (unsigned char *)p
;
242 default: /* an unkown tag */
245 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
250 /* continue to the next tag */
251 if (p
[0] & LARGE_TAG
)
258 "PnPBIOS: Resource structure does not contain an end tag.\n");
264 * Resource Configuration Options
267 static __init
void pnpbios_parse_mem_option(struct pnp_dev
*dev
,
268 unsigned char *p
, int size
,
269 struct pnp_option
*option
)
273 mem
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
276 mem
->min
= ((p
[5] << 8) | p
[4]) << 8;
277 mem
->max
= ((p
[7] << 8) | p
[6]) << 8;
278 mem
->align
= (p
[9] << 8) | p
[8];
279 mem
->size
= ((p
[11] << 8) | p
[10]) << 8;
281 pnp_register_mem_resource(dev
, option
, mem
);
284 static __init
void pnpbios_parse_mem32_option(struct pnp_dev
*dev
,
285 unsigned char *p
, int size
,
286 struct pnp_option
*option
)
290 mem
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
293 mem
->min
= (p
[7] << 24) | (p
[6] << 16) | (p
[5] << 8) | p
[4];
294 mem
->max
= (p
[11] << 24) | (p
[10] << 16) | (p
[9] << 8) | p
[8];
295 mem
->align
= (p
[15] << 24) | (p
[14] << 16) | (p
[13] << 8) | p
[12];
296 mem
->size
= (p
[19] << 24) | (p
[18] << 16) | (p
[17] << 8) | p
[16];
298 pnp_register_mem_resource(dev
, option
, mem
);
301 static __init
void pnpbios_parse_fixed_mem32_option(struct pnp_dev
*dev
,
302 unsigned char *p
, int size
,
303 struct pnp_option
*option
)
307 mem
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
310 mem
->min
= mem
->max
= (p
[7] << 24) | (p
[6] << 16) | (p
[5] << 8) | p
[4];
311 mem
->size
= (p
[11] << 24) | (p
[10] << 16) | (p
[9] << 8) | p
[8];
314 pnp_register_mem_resource(dev
, option
, mem
);
317 static __init
void pnpbios_parse_irq_option(struct pnp_dev
*dev
,
318 unsigned char *p
, int size
,
319 struct pnp_option
*option
)
324 irq
= kzalloc(sizeof(struct pnp_irq
), GFP_KERNEL
);
327 bits
= (p
[2] << 8) | p
[1];
328 bitmap_copy(irq
->map
, &bits
, 16);
332 irq
->flags
= IORESOURCE_IRQ_HIGHEDGE
;
333 pnp_register_irq_resource(dev
, option
, irq
);
336 static __init
void pnpbios_parse_dma_option(struct pnp_dev
*dev
,
337 unsigned char *p
, int size
,
338 struct pnp_option
*option
)
342 dma
= kzalloc(sizeof(struct pnp_dma
), GFP_KERNEL
);
347 pnp_register_dma_resource(dev
, option
, dma
);
350 static __init
void pnpbios_parse_port_option(struct pnp_dev
*dev
,
351 unsigned char *p
, int size
,
352 struct pnp_option
*option
)
354 struct pnp_port
*port
;
356 port
= kzalloc(sizeof(struct pnp_port
), GFP_KERNEL
);
359 port
->min
= (p
[3] << 8) | p
[2];
360 port
->max
= (p
[5] << 8) | p
[4];
363 port
->flags
= p
[1] ? PNP_PORT_FLAG_16BITADDR
: 0;
364 pnp_register_port_resource(dev
, option
, port
);
367 static __init
void pnpbios_parse_fixed_port_option(struct pnp_dev
*dev
,
368 unsigned char *p
, int size
,
369 struct pnp_option
*option
)
371 struct pnp_port
*port
;
373 port
= kzalloc(sizeof(struct pnp_port
), GFP_KERNEL
);
376 port
->min
= port
->max
= (p
[2] << 8) | p
[1];
379 port
->flags
= PNP_PORT_FLAG_FIXED
;
380 pnp_register_port_resource(dev
, option
, port
);
383 static __init
unsigned char *
384 pnpbios_parse_resource_option_data(unsigned char *p
, unsigned char *end
,
387 unsigned int len
, tag
;
389 struct pnp_option
*option
, *option_independent
;
394 dev_dbg(&dev
->dev
, "parse resource options\n");
396 option_independent
= option
= pnp_register_independent_option(dev
);
400 while ((char *)p
< (char *)end
) {
402 /* determine the type of tag */
403 if (p
[0] & LARGE_TAG
) { /* large tag */
404 len
= (p
[2] << 8) | p
[1];
406 } else { /* small tag */
408 tag
= ((p
[0] >> 3) & 0x0f);
416 pnpbios_parse_mem_option(dev
, p
, len
, option
);
419 case LARGE_TAG_MEM32
:
422 pnpbios_parse_mem32_option(dev
, p
, len
, option
);
425 case LARGE_TAG_FIXEDMEM32
:
428 pnpbios_parse_fixed_mem32_option(dev
, p
, len
, option
);
432 if (len
< 2 || len
> 3)
434 pnpbios_parse_irq_option(dev
, p
, len
, option
);
440 pnpbios_parse_dma_option(dev
, p
, len
, option
);
446 pnpbios_parse_port_option(dev
, p
, len
, option
);
449 case SMALL_TAG_VENDOR
:
453 case SMALL_TAG_FIXEDPORT
:
456 pnpbios_parse_fixed_port_option(dev
, p
, len
, option
);
459 case SMALL_TAG_STARTDEP
:
462 priority
= 0x100 | PNP_RES_PRIORITY_ACCEPTABLE
;
464 priority
= 0x100 | p
[1];
465 option
= pnp_register_dependent_option(dev
, priority
);
470 case SMALL_TAG_ENDDEP
:
473 if (option_independent
== option
)
475 "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
476 option
= option_independent
;
477 dev_dbg(&dev
->dev
, "end dependent options\n");
483 default: /* an unkown tag */
486 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
491 /* continue to the next tag */
492 if (p
[0] & LARGE_TAG
)
499 "PnPBIOS: Resource structure does not contain an end tag.\n");
505 * Compatible Device IDs
508 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p
,
515 struct pnp_id
*dev_id
;
520 while ((char *)p
< (char *)end
) {
522 /* determine the type of tag */
523 if (p
[0] & LARGE_TAG
) { /* large tag */
524 len
= (p
[2] << 8) | p
[1];
526 } else { /* small tag */
528 tag
= ((p
[0] >> 3) & 0x0f);
533 case LARGE_TAG_ANSISTR
:
534 strncpy(dev
->name
, p
+ 3,
535 len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 2 : len
);
537 PNP_NAME_LEN
? PNP_NAME_LEN
- 1 : len
] = '\0';
540 case SMALL_TAG_COMPATDEVID
: /* compatible ID */
543 eisa_id
= p
[1] | p
[2] << 8 | p
[3] << 16 | p
[4] << 24;
544 pnp_eisa_id_to_string(eisa_id
& PNP_EISA_ID_MASK
, id
);
545 dev_id
= pnp_add_id(dev
, id
);
552 return (unsigned char *)p
;
555 default: /* an unkown tag */
558 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
563 /* continue to the next tag */
564 if (p
[0] & LARGE_TAG
)
571 "PnPBIOS: Resource structure does not contain an end tag.\n");
577 * Allocated Resource Encoding
580 static void pnpbios_encode_mem(struct pnp_dev
*dev
, unsigned char *p
,
581 struct resource
*res
)
583 unsigned long base
= res
->start
;
584 unsigned long len
= res
->end
- res
->start
+ 1;
586 p
[4] = (base
>> 8) & 0xff;
587 p
[5] = ((base
>> 8) >> 8) & 0xff;
588 p
[6] = (base
>> 8) & 0xff;
589 p
[7] = ((base
>> 8) >> 8) & 0xff;
590 p
[10] = (len
>> 8) & 0xff;
591 p
[11] = ((len
>> 8) >> 8) & 0xff;
593 dev_dbg(&dev
->dev
, " encode mem %#llx-%#llx\n",
594 (unsigned long long) res
->start
, (unsigned long long) res
->end
);
597 static void pnpbios_encode_mem32(struct pnp_dev
*dev
, unsigned char *p
,
598 struct resource
*res
)
600 unsigned long base
= res
->start
;
601 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] = (base
>> 8) & 0xff;
609 p
[10] = (base
>> 16) & 0xff;
610 p
[11] = (base
>> 24) & 0xff;
612 p
[17] = (len
>> 8) & 0xff;
613 p
[18] = (len
>> 16) & 0xff;
614 p
[19] = (len
>> 24) & 0xff;
616 dev_dbg(&dev
->dev
, " encode mem32 %#llx-%#llx\n",
617 (unsigned long long) res
->start
, (unsigned long long) res
->end
);
620 static void pnpbios_encode_fixed_mem32(struct pnp_dev
*dev
, unsigned char *p
,
621 struct resource
*res
)
623 unsigned long base
= res
->start
;
624 unsigned long len
= res
->end
- res
->start
+ 1;
627 p
[5] = (base
>> 8) & 0xff;
628 p
[6] = (base
>> 16) & 0xff;
629 p
[7] = (base
>> 24) & 0xff;
631 p
[9] = (len
>> 8) & 0xff;
632 p
[10] = (len
>> 16) & 0xff;
633 p
[11] = (len
>> 24) & 0xff;
635 dev_dbg(&dev
->dev
, " encode fixed_mem32 %#llx-%#llx\n",
636 (unsigned long long) res
->start
, (unsigned long long) res
->end
);
639 static void pnpbios_encode_irq(struct pnp_dev
*dev
, unsigned char *p
,
640 struct resource
*res
)
642 unsigned long map
= 0;
644 map
= 1 << res
->start
;
646 p
[2] = (map
>> 8) & 0xff;
648 dev_dbg(&dev
->dev
, " encode irq %d\n", res
->start
);
651 static void pnpbios_encode_dma(struct pnp_dev
*dev
, unsigned char *p
,
652 struct resource
*res
)
654 unsigned long map
= 0;
656 map
= 1 << res
->start
;
659 dev_dbg(&dev
->dev
, " encode dma %d\n", res
->start
);
662 static void pnpbios_encode_port(struct pnp_dev
*dev
, unsigned char *p
,
663 struct resource
*res
)
665 unsigned long base
= res
->start
;
666 unsigned long len
= res
->end
- res
->start
+ 1;
669 p
[3] = (base
>> 8) & 0xff;
671 p
[5] = (base
>> 8) & 0xff;
674 dev_dbg(&dev
->dev
, " encode io %#llx-%#llx\n",
675 (unsigned long long) res
->start
, (unsigned long long) res
->end
);
678 static void pnpbios_encode_fixed_port(struct pnp_dev
*dev
, unsigned char *p
,
679 struct resource
*res
)
681 unsigned long base
= res
->start
;
682 unsigned long len
= res
->end
- res
->start
+ 1;
685 p
[2] = (base
>> 8) & 0xff;
688 dev_dbg(&dev
->dev
, " encode fixed_io %#llx-%#llx\n",
689 (unsigned long long) res
->start
, (unsigned long long) res
->end
);
692 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
697 struct pnp_resource_table
*res
= &dev
->res
;
698 unsigned int len
, tag
;
699 int port
= 0, irq
= 0, dma
= 0, mem
= 0;
704 while ((char *)p
< (char *)end
) {
706 /* determine the type of tag */
707 if (p
[0] & LARGE_TAG
) { /* large tag */
708 len
= (p
[2] << 8) | p
[1];
710 } else { /* small tag */
712 tag
= ((p
[0] >> 3) & 0x0f);
720 pnpbios_encode_mem(dev
, p
, &res
->mem_resource
[mem
]);
724 case LARGE_TAG_MEM32
:
727 pnpbios_encode_mem32(dev
, p
, &res
->mem_resource
[mem
]);
731 case LARGE_TAG_FIXEDMEM32
:
734 pnpbios_encode_fixed_mem32(dev
, p
, &res
->mem_resource
[mem
]);
739 if (len
< 2 || len
> 3)
741 pnpbios_encode_irq(dev
, p
, &res
->irq_resource
[irq
]);
748 pnpbios_encode_dma(dev
, p
, &res
->dma_resource
[dma
]);
755 pnpbios_encode_port(dev
, p
, &res
->port_resource
[port
]);
759 case SMALL_TAG_VENDOR
:
763 case SMALL_TAG_FIXEDPORT
:
766 pnpbios_encode_fixed_port(dev
, p
, &res
->port_resource
[port
]);
772 return (unsigned char *)p
;
775 default: /* an unkown tag */
778 "PnPBIOS: Unknown tag '0x%x', length '%d'.\n",
783 /* continue to the next tag */
784 if (p
[0] & LARGE_TAG
)
791 "PnPBIOS: Resource structure does not contain an end tag.\n");
797 * Core Parsing Functions
800 int __init
pnpbios_parse_data_stream(struct pnp_dev
*dev
,
801 struct pnp_bios_node
*node
)
803 unsigned char *p
= (char *)node
->data
;
804 unsigned char *end
= (char *)(node
->data
+ node
->size
);
806 p
= pnpbios_parse_allocated_resource_data(dev
, p
, end
);
809 p
= pnpbios_parse_resource_option_data(p
, end
, dev
);
812 p
= pnpbios_parse_compatible_ids(p
, end
, dev
);
818 int pnpbios_read_resources_from_node(struct pnp_dev
*dev
,
819 struct pnp_bios_node
*node
)
821 unsigned char *p
= (char *)node
->data
;
822 unsigned char *end
= (char *)(node
->data
+ node
->size
);
824 p
= pnpbios_parse_allocated_resource_data(dev
, p
, end
);
830 int pnpbios_write_resources_to_node(struct pnp_dev
*dev
,
831 struct pnp_bios_node
*node
)
833 unsigned char *p
= (char *)node
->data
;
834 unsigned char *end
= (char *)(node
->data
+ node
->size
);
836 p
= pnpbios_encode_allocated_resource_data(dev
, p
, end
);