2 * rsparser.c - parses and encodes pnpbios resource data streams
5 #include <linux/ctype.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
11 #include <linux/pci.h>
13 inline void pcibios_penalize_isa_irq(int irq
, int active
)
16 #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)
56 static void pnpbios_parse_allocated_ioresource(struct pnp_dev
*dev
,
60 int end
= start
+ len
- 1;
62 if (len
<= 0 || end
>= 0x10003)
63 flags
|= IORESOURCE_DISABLED
;
65 pnp_add_io_resource(dev
, start
, end
, flags
);
68 static void pnpbios_parse_allocated_memresource(struct pnp_dev
*dev
,
72 int end
= start
+ len
- 1;
75 flags
|= IORESOURCE_DISABLED
;
77 pnp_add_mem_resource(dev
, start
, end
, flags
);
80 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev
*dev
,
84 unsigned int len
, tag
;
85 int io
, size
, mask
, i
, flags
;
90 dev_dbg(&dev
->dev
, "parse allocated resources\n");
92 pnp_init_resources(dev
);
94 while ((char *)p
< (char *)end
) {
96 /* determine the type of tag */
97 if (p
[0] & LARGE_TAG
) { /* large tag */
98 len
= (p
[2] << 8) | p
[1];
100 } else { /* small tag */
102 tag
= ((p
[0] >> 3) & 0x0f);
110 io
= *(short *)&p
[4];
111 size
= *(short *)&p
[10];
112 pnpbios_parse_allocated_memresource(dev
, io
, size
);
115 case LARGE_TAG_ANSISTR
:
116 /* ignore this for now */
119 case LARGE_TAG_VENDOR
:
123 case LARGE_TAG_MEM32
:
127 size
= *(int *)&p
[16];
128 pnpbios_parse_allocated_memresource(dev
, io
, size
);
131 case LARGE_TAG_FIXEDMEM32
:
135 size
= *(int *)&p
[8];
136 pnpbios_parse_allocated_memresource(dev
, io
, size
);
140 if (len
< 2 || len
> 3)
144 mask
= p
[1] + p
[2] * 256;
145 for (i
= 0; i
< 16; i
++, mask
= mask
>> 1)
149 pcibios_penalize_isa_irq(io
, 1);
151 flags
= IORESOURCE_DISABLED
;
152 pnp_add_irq_resource(dev
, io
, flags
);
161 for (i
= 0; i
< 8; i
++, mask
= mask
>> 1)
165 flags
= IORESOURCE_DISABLED
;
166 pnp_add_dma_resource(dev
, io
, flags
);
172 io
= p
[2] + p
[3] * 256;
174 pnpbios_parse_allocated_ioresource(dev
, io
, size
);
177 case SMALL_TAG_VENDOR
:
181 case SMALL_TAG_FIXEDPORT
:
184 io
= p
[1] + p
[2] * 256;
186 pnpbios_parse_allocated_ioresource(dev
, io
, size
);
191 return (unsigned char *)p
;
194 default: /* an unkown tag */
196 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
201 /* continue to the next tag */
202 if (p
[0] & LARGE_TAG
)
208 dev_err(&dev
->dev
, "no end tag in resource structure\n");
214 * Resource Configuration Options
217 static __init
void pnpbios_parse_mem_option(struct pnp_dev
*dev
,
218 unsigned char *p
, int size
,
219 struct pnp_option
*option
)
223 mem
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
226 mem
->min
= ((p
[5] << 8) | p
[4]) << 8;
227 mem
->max
= ((p
[7] << 8) | p
[6]) << 8;
228 mem
->align
= (p
[9] << 8) | p
[8];
229 mem
->size
= ((p
[11] << 8) | p
[10]) << 8;
231 pnp_register_mem_resource(dev
, option
, mem
);
234 static __init
void pnpbios_parse_mem32_option(struct pnp_dev
*dev
,
235 unsigned char *p
, int size
,
236 struct pnp_option
*option
)
240 mem
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
243 mem
->min
= (p
[7] << 24) | (p
[6] << 16) | (p
[5] << 8) | p
[4];
244 mem
->max
= (p
[11] << 24) | (p
[10] << 16) | (p
[9] << 8) | p
[8];
245 mem
->align
= (p
[15] << 24) | (p
[14] << 16) | (p
[13] << 8) | p
[12];
246 mem
->size
= (p
[19] << 24) | (p
[18] << 16) | (p
[17] << 8) | p
[16];
248 pnp_register_mem_resource(dev
, option
, mem
);
251 static __init
void pnpbios_parse_fixed_mem32_option(struct pnp_dev
*dev
,
252 unsigned char *p
, int size
,
253 struct pnp_option
*option
)
257 mem
= kzalloc(sizeof(struct pnp_mem
), GFP_KERNEL
);
260 mem
->min
= mem
->max
= (p
[7] << 24) | (p
[6] << 16) | (p
[5] << 8) | p
[4];
261 mem
->size
= (p
[11] << 24) | (p
[10] << 16) | (p
[9] << 8) | p
[8];
264 pnp_register_mem_resource(dev
, option
, mem
);
267 static __init
void pnpbios_parse_irq_option(struct pnp_dev
*dev
,
268 unsigned char *p
, int size
,
269 struct pnp_option
*option
)
274 irq
= kzalloc(sizeof(struct pnp_irq
), GFP_KERNEL
);
277 bits
= (p
[2] << 8) | p
[1];
278 bitmap_copy(irq
->map
.bits
, &bits
, 16);
282 irq
->flags
= IORESOURCE_IRQ_HIGHEDGE
;
283 pnp_register_irq_resource(dev
, option
, irq
);
286 static __init
void pnpbios_parse_dma_option(struct pnp_dev
*dev
,
287 unsigned char *p
, int size
,
288 struct pnp_option
*option
)
292 dma
= kzalloc(sizeof(struct pnp_dma
), GFP_KERNEL
);
297 pnp_register_dma_resource(dev
, option
, dma
);
300 static __init
void pnpbios_parse_port_option(struct pnp_dev
*dev
,
301 unsigned char *p
, int size
,
302 struct pnp_option
*option
)
304 struct pnp_port
*port
;
306 port
= kzalloc(sizeof(struct pnp_port
), GFP_KERNEL
);
309 port
->min
= (p
[3] << 8) | p
[2];
310 port
->max
= (p
[5] << 8) | p
[4];
313 port
->flags
= p
[1] ? IORESOURCE_IO_16BIT_ADDR
: 0;
314 pnp_register_port_resource(dev
, option
, port
);
317 static __init
void pnpbios_parse_fixed_port_option(struct pnp_dev
*dev
,
318 unsigned char *p
, int size
,
319 struct pnp_option
*option
)
321 struct pnp_port
*port
;
323 port
= kzalloc(sizeof(struct pnp_port
), GFP_KERNEL
);
326 port
->min
= port
->max
= (p
[2] << 8) | p
[1];
329 port
->flags
= IORESOURCE_IO_FIXED
;
330 pnp_register_port_resource(dev
, option
, port
);
333 static __init
unsigned char *
334 pnpbios_parse_resource_option_data(unsigned char *p
, unsigned char *end
,
337 unsigned int len
, tag
;
339 struct pnp_option
*option
, *option_independent
;
344 dev_dbg(&dev
->dev
, "parse resource options\n");
346 option_independent
= option
= pnp_register_independent_option(dev
);
350 while ((char *)p
< (char *)end
) {
352 /* determine the type of tag */
353 if (p
[0] & LARGE_TAG
) { /* large tag */
354 len
= (p
[2] << 8) | p
[1];
356 } else { /* small tag */
358 tag
= ((p
[0] >> 3) & 0x0f);
366 pnpbios_parse_mem_option(dev
, p
, len
, option
);
369 case LARGE_TAG_MEM32
:
372 pnpbios_parse_mem32_option(dev
, p
, len
, option
);
375 case LARGE_TAG_FIXEDMEM32
:
378 pnpbios_parse_fixed_mem32_option(dev
, p
, len
, option
);
382 if (len
< 2 || len
> 3)
384 pnpbios_parse_irq_option(dev
, p
, len
, option
);
390 pnpbios_parse_dma_option(dev
, p
, len
, option
);
396 pnpbios_parse_port_option(dev
, p
, len
, option
);
399 case SMALL_TAG_VENDOR
:
403 case SMALL_TAG_FIXEDPORT
:
406 pnpbios_parse_fixed_port_option(dev
, p
, len
, option
);
409 case SMALL_TAG_STARTDEP
:
412 priority
= 0x100 | PNP_RES_PRIORITY_ACCEPTABLE
;
414 priority
= 0x100 | p
[1];
415 option
= pnp_register_dependent_option(dev
, priority
);
420 case SMALL_TAG_ENDDEP
:
423 if (option_independent
== option
)
424 dev_warn(&dev
->dev
, "missing "
425 "SMALL_TAG_STARTDEP tag\n");
426 option
= option_independent
;
427 dev_dbg(&dev
->dev
, "end dependent options\n");
433 default: /* an unkown tag */
435 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
440 /* continue to the next tag */
441 if (p
[0] & LARGE_TAG
)
447 dev_err(&dev
->dev
, "no end tag in resource structure\n");
453 * Compatible Device IDs
456 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p
,
463 struct pnp_id
*dev_id
;
468 while ((char *)p
< (char *)end
) {
470 /* determine the type of tag */
471 if (p
[0] & LARGE_TAG
) { /* large tag */
472 len
= (p
[2] << 8) | p
[1];
474 } else { /* small tag */
476 tag
= ((p
[0] >> 3) & 0x0f);
481 case LARGE_TAG_ANSISTR
:
482 strncpy(dev
->name
, p
+ 3,
483 len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 2 : len
);
485 PNP_NAME_LEN
? PNP_NAME_LEN
- 1 : len
] = '\0';
488 case SMALL_TAG_COMPATDEVID
: /* compatible ID */
491 eisa_id
= p
[1] | p
[2] << 8 | p
[3] << 16 | p
[4] << 24;
492 pnp_eisa_id_to_string(eisa_id
& PNP_EISA_ID_MASK
, id
);
493 dev_id
= pnp_add_id(dev
, id
);
500 return (unsigned char *)p
;
503 default: /* an unkown tag */
505 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
510 /* continue to the next tag */
511 if (p
[0] & LARGE_TAG
)
517 dev_err(&dev
->dev
, "no end tag in resource structure\n");
523 * Allocated Resource Encoding
526 static void pnpbios_encode_mem(struct pnp_dev
*dev
, unsigned char *p
,
527 struct resource
*res
)
532 if (pnp_resource_enabled(res
)) {
534 len
= res
->end
- res
->start
+ 1;
540 p
[4] = (base
>> 8) & 0xff;
541 p
[5] = ((base
>> 8) >> 8) & 0xff;
542 p
[6] = (base
>> 8) & 0xff;
543 p
[7] = ((base
>> 8) >> 8) & 0xff;
544 p
[10] = (len
>> 8) & 0xff;
545 p
[11] = ((len
>> 8) >> 8) & 0xff;
547 dev_dbg(&dev
->dev
, " encode mem %#lx-%#lx\n", base
, base
+ len
- 1);
550 static void pnpbios_encode_mem32(struct pnp_dev
*dev
, unsigned char *p
,
551 struct resource
*res
)
556 if (pnp_resource_enabled(res
)) {
558 len
= res
->end
- res
->start
+ 1;
565 p
[5] = (base
>> 8) & 0xff;
566 p
[6] = (base
>> 16) & 0xff;
567 p
[7] = (base
>> 24) & 0xff;
569 p
[9] = (base
>> 8) & 0xff;
570 p
[10] = (base
>> 16) & 0xff;
571 p
[11] = (base
>> 24) & 0xff;
573 p
[17] = (len
>> 8) & 0xff;
574 p
[18] = (len
>> 16) & 0xff;
575 p
[19] = (len
>> 24) & 0xff;
577 dev_dbg(&dev
->dev
, " encode mem32 %#lx-%#lx\n", base
, base
+ len
- 1);
580 static void pnpbios_encode_fixed_mem32(struct pnp_dev
*dev
, unsigned char *p
,
581 struct resource
*res
)
586 if (pnp_resource_enabled(res
)) {
588 len
= res
->end
- res
->start
+ 1;
595 p
[5] = (base
>> 8) & 0xff;
596 p
[6] = (base
>> 16) & 0xff;
597 p
[7] = (base
>> 24) & 0xff;
599 p
[9] = (len
>> 8) & 0xff;
600 p
[10] = (len
>> 16) & 0xff;
601 p
[11] = (len
>> 24) & 0xff;
603 dev_dbg(&dev
->dev
, " encode fixed_mem32 %#lx-%#lx\n", base
,
607 static void pnpbios_encode_irq(struct pnp_dev
*dev
, unsigned char *p
,
608 struct resource
*res
)
612 if (pnp_resource_enabled(res
))
613 map
= 1 << res
->start
;
618 p
[2] = (map
>> 8) & 0xff;
620 dev_dbg(&dev
->dev
, " encode irq mask %#lx\n", map
);
623 static void pnpbios_encode_dma(struct pnp_dev
*dev
, unsigned char *p
,
624 struct resource
*res
)
628 if (pnp_resource_enabled(res
))
629 map
= 1 << res
->start
;
635 dev_dbg(&dev
->dev
, " encode dma mask %#lx\n", map
);
638 static void pnpbios_encode_port(struct pnp_dev
*dev
, unsigned char *p
,
639 struct resource
*res
)
644 if (pnp_resource_enabled(res
)) {
646 len
= res
->end
- res
->start
+ 1;
653 p
[3] = (base
>> 8) & 0xff;
655 p
[5] = (base
>> 8) & 0xff;
658 dev_dbg(&dev
->dev
, " encode io %#lx-%#lx\n", base
, base
+ len
- 1);
661 static void pnpbios_encode_fixed_port(struct pnp_dev
*dev
, unsigned char *p
,
662 struct resource
*res
)
664 unsigned long base
= res
->start
;
665 unsigned long len
= res
->end
- res
->start
+ 1;
667 if (pnp_resource_enabled(res
)) {
669 len
= res
->end
- res
->start
+ 1;
676 p
[2] = (base
>> 8) & 0xff;
679 dev_dbg(&dev
->dev
, " encode fixed_io %#lx-%#lx\n", base
,
683 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
688 unsigned int len
, tag
;
689 int port
= 0, irq
= 0, dma
= 0, mem
= 0;
694 while ((char *)p
< (char *)end
) {
696 /* determine the type of tag */
697 if (p
[0] & LARGE_TAG
) { /* large tag */
698 len
= (p
[2] << 8) | p
[1];
700 } else { /* small tag */
702 tag
= ((p
[0] >> 3) & 0x0f);
710 pnpbios_encode_mem(dev
, p
,
711 pnp_get_resource(dev
, IORESOURCE_MEM
, mem
));
715 case LARGE_TAG_MEM32
:
718 pnpbios_encode_mem32(dev
, p
,
719 pnp_get_resource(dev
, IORESOURCE_MEM
, mem
));
723 case LARGE_TAG_FIXEDMEM32
:
726 pnpbios_encode_fixed_mem32(dev
, p
,
727 pnp_get_resource(dev
, IORESOURCE_MEM
, mem
));
732 if (len
< 2 || len
> 3)
734 pnpbios_encode_irq(dev
, p
,
735 pnp_get_resource(dev
, IORESOURCE_IRQ
, irq
));
742 pnpbios_encode_dma(dev
, p
,
743 pnp_get_resource(dev
, IORESOURCE_DMA
, dma
));
750 pnpbios_encode_port(dev
, p
,
751 pnp_get_resource(dev
, IORESOURCE_IO
, port
));
755 case SMALL_TAG_VENDOR
:
759 case SMALL_TAG_FIXEDPORT
:
762 pnpbios_encode_fixed_port(dev
, p
,
763 pnp_get_resource(dev
, IORESOURCE_IO
, port
));
769 return (unsigned char *)p
;
772 default: /* an unkown tag */
774 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
779 /* continue to the next tag */
780 if (p
[0] & LARGE_TAG
)
786 dev_err(&dev
->dev
, "no end tag in resource structure\n");
792 * Core Parsing Functions
795 int __init
pnpbios_parse_data_stream(struct pnp_dev
*dev
,
796 struct pnp_bios_node
*node
)
798 unsigned char *p
= (char *)node
->data
;
799 unsigned char *end
= (char *)(node
->data
+ node
->size
);
801 p
= pnpbios_parse_allocated_resource_data(dev
, p
, end
);
804 p
= pnpbios_parse_resource_option_data(p
, end
, dev
);
807 p
= pnpbios_parse_compatible_ids(p
, end
, dev
);
813 int pnpbios_read_resources_from_node(struct pnp_dev
*dev
,
814 struct pnp_bios_node
*node
)
816 unsigned char *p
= (char *)node
->data
;
817 unsigned char *end
= (char *)(node
->data
+ node
->size
);
819 p
= pnpbios_parse_allocated_resource_data(dev
, p
, end
);
825 int pnpbios_write_resources_to_node(struct pnp_dev
*dev
,
826 struct pnp_bios_node
*node
)
828 unsigned char *p
= (char *)node
->data
;
829 unsigned char *end
= (char *)(node
->data
+ node
->size
);
831 p
= pnpbios_encode_allocated_resource_data(dev
, p
, end
);