2 * rsparser.c - parses and encodes pnpbios resource data streams
5 #include <linux/ctype.h>
7 #include <linux/string.h>
10 #include <linux/pci.h>
12 inline void pcibios_penalize_isa_irq(int irq
, int active
)
15 #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)
55 static void pnpbios_parse_allocated_ioresource(struct pnp_dev
*dev
,
59 int end
= start
+ len
- 1;
61 if (len
<= 0 || end
>= 0x10003)
62 flags
|= IORESOURCE_DISABLED
;
64 pnp_add_io_resource(dev
, start
, end
, flags
);
67 static void pnpbios_parse_allocated_memresource(struct pnp_dev
*dev
,
71 int end
= start
+ len
- 1;
74 flags
|= IORESOURCE_DISABLED
;
76 pnp_add_mem_resource(dev
, start
, end
, flags
);
79 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev
*dev
,
83 unsigned int len
, tag
;
84 int io
, size
, mask
, i
, flags
;
89 pnp_dbg(&dev
->dev
, "parse allocated resources\n");
91 pnp_init_resources(dev
);
93 while ((char *)p
< (char *)end
) {
95 /* determine the type of tag */
96 if (p
[0] & LARGE_TAG
) { /* large tag */
97 len
= (p
[2] << 8) | p
[1];
99 } else { /* small tag */
101 tag
= ((p
[0] >> 3) & 0x0f);
109 io
= *(short *)&p
[4];
110 size
= *(short *)&p
[10];
111 pnpbios_parse_allocated_memresource(dev
, io
, size
);
114 case LARGE_TAG_ANSISTR
:
115 /* ignore this for now */
118 case LARGE_TAG_VENDOR
:
122 case LARGE_TAG_MEM32
:
126 size
= *(int *)&p
[16];
127 pnpbios_parse_allocated_memresource(dev
, io
, size
);
130 case LARGE_TAG_FIXEDMEM32
:
134 size
= *(int *)&p
[8];
135 pnpbios_parse_allocated_memresource(dev
, io
, size
);
139 if (len
< 2 || len
> 3)
143 mask
= p
[1] + p
[2] * 256;
144 for (i
= 0; i
< 16; i
++, mask
= mask
>> 1)
148 pcibios_penalize_isa_irq(io
, 1);
150 flags
= IORESOURCE_DISABLED
;
151 pnp_add_irq_resource(dev
, io
, flags
);
160 for (i
= 0; i
< 8; i
++, mask
= mask
>> 1)
164 flags
= IORESOURCE_DISABLED
;
165 pnp_add_dma_resource(dev
, io
, flags
);
171 io
= p
[2] + p
[3] * 256;
173 pnpbios_parse_allocated_ioresource(dev
, io
, size
);
176 case SMALL_TAG_VENDOR
:
180 case SMALL_TAG_FIXEDPORT
:
183 io
= p
[1] + p
[2] * 256;
185 pnpbios_parse_allocated_ioresource(dev
, io
, size
);
190 return (unsigned char *)p
;
193 default: /* an unknown tag */
195 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
200 /* continue to the next tag */
201 if (p
[0] & LARGE_TAG
)
207 dev_err(&dev
->dev
, "no end tag in resource structure\n");
213 * Resource Configuration Options
216 static __init
void pnpbios_parse_mem_option(struct pnp_dev
*dev
,
217 unsigned char *p
, int size
,
218 unsigned int option_flags
)
220 resource_size_t min
, max
, align
, len
;
223 min
= ((p
[5] << 8) | p
[4]) << 8;
224 max
= ((p
[7] << 8) | p
[6]) << 8;
225 align
= (p
[9] << 8) | p
[8];
226 len
= ((p
[11] << 8) | p
[10]) << 8;
228 pnp_register_mem_resource(dev
, option_flags
, min
, max
, align
, len
,
232 static __init
void pnpbios_parse_mem32_option(struct pnp_dev
*dev
,
233 unsigned char *p
, int size
,
234 unsigned int option_flags
)
236 resource_size_t min
, max
, align
, len
;
239 min
= (p
[7] << 24) | (p
[6] << 16) | (p
[5] << 8) | p
[4];
240 max
= (p
[11] << 24) | (p
[10] << 16) | (p
[9] << 8) | p
[8];
241 align
= (p
[15] << 24) | (p
[14] << 16) | (p
[13] << 8) | p
[12];
242 len
= (p
[19] << 24) | (p
[18] << 16) | (p
[17] << 8) | p
[16];
244 pnp_register_mem_resource(dev
, option_flags
, min
, max
, align
, len
,
248 static __init
void pnpbios_parse_fixed_mem32_option(struct pnp_dev
*dev
,
249 unsigned char *p
, int size
,
250 unsigned int option_flags
)
252 resource_size_t base
, len
;
255 base
= (p
[7] << 24) | (p
[6] << 16) | (p
[5] << 8) | p
[4];
256 len
= (p
[11] << 24) | (p
[10] << 16) | (p
[9] << 8) | p
[8];
258 pnp_register_mem_resource(dev
, option_flags
, base
, base
, 0, len
, flags
);
261 static __init
void pnpbios_parse_irq_option(struct pnp_dev
*dev
,
262 unsigned char *p
, int size
,
263 unsigned int option_flags
)
267 unsigned char flags
= IORESOURCE_IRQ_HIGHEDGE
;
269 bits
= (p
[2] << 8) | p
[1];
271 bitmap_zero(map
.bits
, PNP_IRQ_NR
);
272 bitmap_copy(map
.bits
, &bits
, 16);
277 pnp_register_irq_resource(dev
, option_flags
, &map
, flags
);
280 static __init
void pnpbios_parse_dma_option(struct pnp_dev
*dev
,
281 unsigned char *p
, int size
,
282 unsigned int option_flags
)
284 pnp_register_dma_resource(dev
, option_flags
, p
[1], p
[2]);
287 static __init
void pnpbios_parse_port_option(struct pnp_dev
*dev
,
288 unsigned char *p
, int size
,
289 unsigned int option_flags
)
291 resource_size_t min
, max
, align
, len
;
294 min
= (p
[3] << 8) | p
[2];
295 max
= (p
[5] << 8) | p
[4];
298 flags
= p
[1] ? IORESOURCE_IO_16BIT_ADDR
: 0;
299 pnp_register_port_resource(dev
, option_flags
, min
, max
, align
, len
,
303 static __init
void pnpbios_parse_fixed_port_option(struct pnp_dev
*dev
,
304 unsigned char *p
, int size
,
305 unsigned int option_flags
)
307 resource_size_t base
, len
;
309 base
= (p
[2] << 8) | p
[1];
311 pnp_register_port_resource(dev
, option_flags
, base
, base
, 0, len
,
312 IORESOURCE_IO_FIXED
);
315 static __init
unsigned char *
316 pnpbios_parse_resource_option_data(unsigned char *p
, unsigned char *end
,
319 unsigned int len
, tag
;
321 unsigned int option_flags
;
326 pnp_dbg(&dev
->dev
, "parse resource options\n");
328 while ((char *)p
< (char *)end
) {
330 /* determine the type of tag */
331 if (p
[0] & LARGE_TAG
) { /* large tag */
332 len
= (p
[2] << 8) | p
[1];
334 } else { /* small tag */
336 tag
= ((p
[0] >> 3) & 0x0f);
344 pnpbios_parse_mem_option(dev
, p
, len
, option_flags
);
347 case LARGE_TAG_MEM32
:
350 pnpbios_parse_mem32_option(dev
, p
, len
, option_flags
);
353 case LARGE_TAG_FIXEDMEM32
:
356 pnpbios_parse_fixed_mem32_option(dev
, p
, len
,
361 if (len
< 2 || len
> 3)
363 pnpbios_parse_irq_option(dev
, p
, len
, option_flags
);
369 pnpbios_parse_dma_option(dev
, p
, len
, option_flags
);
375 pnpbios_parse_port_option(dev
, p
, len
, option_flags
);
378 case SMALL_TAG_VENDOR
:
382 case SMALL_TAG_FIXEDPORT
:
385 pnpbios_parse_fixed_port_option(dev
, p
, len
,
389 case SMALL_TAG_STARTDEP
:
392 priority
= PNP_RES_PRIORITY_ACCEPTABLE
;
395 option_flags
= pnp_new_dependent_set(dev
, priority
);
398 case SMALL_TAG_ENDDEP
:
407 default: /* an unknown tag */
409 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
414 /* continue to the next tag */
415 if (p
[0] & LARGE_TAG
)
421 dev_err(&dev
->dev
, "no end tag in resource structure\n");
427 * Compatible Device IDs
430 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p
,
437 struct pnp_id
*dev_id
;
442 while ((char *)p
< (char *)end
) {
444 /* determine the type of tag */
445 if (p
[0] & LARGE_TAG
) { /* large tag */
446 len
= (p
[2] << 8) | p
[1];
448 } else { /* small tag */
450 tag
= ((p
[0] >> 3) & 0x0f);
455 case LARGE_TAG_ANSISTR
:
456 strncpy(dev
->name
, p
+ 3,
457 len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 2 : len
);
459 PNP_NAME_LEN
? PNP_NAME_LEN
- 1 : len
] = '\0';
462 case SMALL_TAG_COMPATDEVID
: /* compatible ID */
465 eisa_id
= p
[1] | p
[2] << 8 | p
[3] << 16 | p
[4] << 24;
466 pnp_eisa_id_to_string(eisa_id
& PNP_EISA_ID_MASK
, id
);
467 dev_id
= pnp_add_id(dev
, id
);
474 return (unsigned char *)p
;
477 default: /* an unknown tag */
479 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
484 /* continue to the next tag */
485 if (p
[0] & LARGE_TAG
)
491 dev_err(&dev
->dev
, "no end tag in resource structure\n");
497 * Allocated Resource Encoding
500 static void pnpbios_encode_mem(struct pnp_dev
*dev
, unsigned char *p
,
501 struct resource
*res
)
506 if (pnp_resource_enabled(res
)) {
508 len
= resource_size(res
);
514 p
[4] = (base
>> 8) & 0xff;
515 p
[5] = ((base
>> 8) >> 8) & 0xff;
516 p
[6] = (base
>> 8) & 0xff;
517 p
[7] = ((base
>> 8) >> 8) & 0xff;
518 p
[10] = (len
>> 8) & 0xff;
519 p
[11] = ((len
>> 8) >> 8) & 0xff;
521 pnp_dbg(&dev
->dev
, " encode mem %#lx-%#lx\n", base
, base
+ len
- 1);
524 static void pnpbios_encode_mem32(struct pnp_dev
*dev
, unsigned char *p
,
525 struct resource
*res
)
530 if (pnp_resource_enabled(res
)) {
532 len
= resource_size(res
);
539 p
[5] = (base
>> 8) & 0xff;
540 p
[6] = (base
>> 16) & 0xff;
541 p
[7] = (base
>> 24) & 0xff;
543 p
[9] = (base
>> 8) & 0xff;
544 p
[10] = (base
>> 16) & 0xff;
545 p
[11] = (base
>> 24) & 0xff;
547 p
[17] = (len
>> 8) & 0xff;
548 p
[18] = (len
>> 16) & 0xff;
549 p
[19] = (len
>> 24) & 0xff;
551 pnp_dbg(&dev
->dev
, " encode mem32 %#lx-%#lx\n", base
, base
+ len
- 1);
554 static void pnpbios_encode_fixed_mem32(struct pnp_dev
*dev
, unsigned char *p
,
555 struct resource
*res
)
560 if (pnp_resource_enabled(res
)) {
562 len
= resource_size(res
);
569 p
[5] = (base
>> 8) & 0xff;
570 p
[6] = (base
>> 16) & 0xff;
571 p
[7] = (base
>> 24) & 0xff;
573 p
[9] = (len
>> 8) & 0xff;
574 p
[10] = (len
>> 16) & 0xff;
575 p
[11] = (len
>> 24) & 0xff;
577 pnp_dbg(&dev
->dev
, " encode fixed_mem32 %#lx-%#lx\n", base
,
581 static void pnpbios_encode_irq(struct pnp_dev
*dev
, unsigned char *p
,
582 struct resource
*res
)
586 if (pnp_resource_enabled(res
))
587 map
= 1 << res
->start
;
592 p
[2] = (map
>> 8) & 0xff;
594 pnp_dbg(&dev
->dev
, " encode irq mask %#lx\n", map
);
597 static void pnpbios_encode_dma(struct pnp_dev
*dev
, unsigned char *p
,
598 struct resource
*res
)
602 if (pnp_resource_enabled(res
))
603 map
= 1 << res
->start
;
609 pnp_dbg(&dev
->dev
, " encode dma mask %#lx\n", map
);
612 static void pnpbios_encode_port(struct pnp_dev
*dev
, unsigned char *p
,
613 struct resource
*res
)
618 if (pnp_resource_enabled(res
)) {
620 len
= resource_size(res
);
627 p
[3] = (base
>> 8) & 0xff;
629 p
[5] = (base
>> 8) & 0xff;
632 pnp_dbg(&dev
->dev
, " encode io %#lx-%#lx\n", base
, base
+ len
- 1);
635 static void pnpbios_encode_fixed_port(struct pnp_dev
*dev
, unsigned char *p
,
636 struct resource
*res
)
638 unsigned long base
= res
->start
;
639 unsigned long len
= resource_size(res
);
641 if (pnp_resource_enabled(res
)) {
643 len
= resource_size(res
);
650 p
[2] = (base
>> 8) & 0xff;
653 pnp_dbg(&dev
->dev
, " encode fixed_io %#lx-%#lx\n", base
,
657 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
662 unsigned int len
, tag
;
663 int port
= 0, irq
= 0, dma
= 0, mem
= 0;
668 while ((char *)p
< (char *)end
) {
670 /* determine the type of tag */
671 if (p
[0] & LARGE_TAG
) { /* large tag */
672 len
= (p
[2] << 8) | p
[1];
674 } else { /* small tag */
676 tag
= ((p
[0] >> 3) & 0x0f);
684 pnpbios_encode_mem(dev
, p
,
685 pnp_get_resource(dev
, IORESOURCE_MEM
, mem
));
689 case LARGE_TAG_MEM32
:
692 pnpbios_encode_mem32(dev
, p
,
693 pnp_get_resource(dev
, IORESOURCE_MEM
, mem
));
697 case LARGE_TAG_FIXEDMEM32
:
700 pnpbios_encode_fixed_mem32(dev
, p
,
701 pnp_get_resource(dev
, IORESOURCE_MEM
, mem
));
706 if (len
< 2 || len
> 3)
708 pnpbios_encode_irq(dev
, p
,
709 pnp_get_resource(dev
, IORESOURCE_IRQ
, irq
));
716 pnpbios_encode_dma(dev
, p
,
717 pnp_get_resource(dev
, IORESOURCE_DMA
, dma
));
724 pnpbios_encode_port(dev
, p
,
725 pnp_get_resource(dev
, IORESOURCE_IO
, port
));
729 case SMALL_TAG_VENDOR
:
733 case SMALL_TAG_FIXEDPORT
:
736 pnpbios_encode_fixed_port(dev
, p
,
737 pnp_get_resource(dev
, IORESOURCE_IO
, port
));
743 return (unsigned char *)p
;
746 default: /* an unknown tag */
748 dev_err(&dev
->dev
, "unknown tag %#x length %d\n",
753 /* continue to the next tag */
754 if (p
[0] & LARGE_TAG
)
760 dev_err(&dev
->dev
, "no end tag in resource structure\n");
766 * Core Parsing Functions
769 int __init
pnpbios_parse_data_stream(struct pnp_dev
*dev
,
770 struct pnp_bios_node
*node
)
772 unsigned char *p
= (char *)node
->data
;
773 unsigned char *end
= (char *)(node
->data
+ node
->size
);
775 p
= pnpbios_parse_allocated_resource_data(dev
, p
, end
);
778 p
= pnpbios_parse_resource_option_data(p
, end
, dev
);
781 p
= pnpbios_parse_compatible_ids(p
, end
, dev
);
787 int pnpbios_read_resources_from_node(struct pnp_dev
*dev
,
788 struct pnp_bios_node
*node
)
790 unsigned char *p
= (char *)node
->data
;
791 unsigned char *end
= (char *)(node
->data
+ node
->size
);
793 p
= pnpbios_parse_allocated_resource_data(dev
, p
, end
);
799 int pnpbios_write_resources_to_node(struct pnp_dev
*dev
,
800 struct pnp_bios_node
*node
)
802 unsigned char *p
= (char *)node
->data
;
803 unsigned char *end
= (char *)(node
->data
+ node
->size
);
805 p
= pnpbios_encode_allocated_resource_data(dev
, p
, end
);