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
;
451 if (option_independent
!= option
)
452 printk(KERN_WARNING
"PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
454 return (unsigned char *)p
;
457 default: /* an unkown tag */
459 printk(KERN_ERR
"PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag
, len
);
463 /* continue to the next tag */
464 if (p
[0] & LARGE_TAG
)
470 printk(KERN_ERR
"PnPBIOS: Resource structure does not contain an end tag.\n");
477 * Compatible Device IDs
480 #define HEX(id,a) hex[((id)>>a) & 15]
481 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
484 void pnpid32_to_pnpid(u32 id
, char *str
)
486 const char *hex
= "0123456789abcdef";
488 id
= be32_to_cpu(id
);
489 str
[0] = CHAR(id
, 26);
490 str
[1] = CHAR(id
, 21);
491 str
[2] = CHAR(id
,16);
492 str
[3] = HEX(id
, 12);
504 static unsigned char *
505 pnpbios_parse_compatible_ids(unsigned char *p
, unsigned char *end
, struct pnp_dev
*dev
)
509 struct pnp_id
*dev_id
;
514 while ((char *)p
< (char *)end
) {
516 /* determine the type of tag */
517 if (p
[0] & LARGE_TAG
) { /* large tag */
518 len
= (p
[2] << 8) | p
[1];
520 } else { /* small tag */
522 tag
= ((p
[0]>>3) & 0x0f);
527 case LARGE_TAG_ANSISTR
:
528 strncpy(dev
->name
, p
+ 3, len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 2 : len
);
529 dev
->name
[len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 1 : len
] = '\0';
532 case SMALL_TAG_COMPATDEVID
: /* compatible ID */
535 dev_id
= kcalloc(1, sizeof (struct pnp_id
), GFP_KERNEL
);
538 memset(dev_id
, 0, sizeof(struct pnp_id
));
539 pnpid32_to_pnpid(p
[1] | p
[2] << 8 | p
[3] << 16 | p
[4] << 24,id
);
540 memcpy(&dev_id
->id
, id
, 7);
541 pnp_add_id(dev_id
, dev
);
546 return (unsigned char *)p
;
549 default: /* an unkown tag */
551 printk(KERN_ERR
"PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag
, len
);
555 /* continue to the next tag */
556 if (p
[0] & LARGE_TAG
)
562 printk(KERN_ERR
"PnPBIOS: Resource structure does not contain an end tag.\n");
569 * Allocated Resource Encoding
572 static void pnpbios_encode_mem(unsigned char *p
, struct resource
* res
)
574 unsigned long base
= res
->start
;
575 unsigned long len
= res
->end
- res
->start
+ 1;
576 p
[4] = (base
>> 8) & 0xff;
577 p
[5] = ((base
>> 8) >> 8) & 0xff;
578 p
[6] = (base
>> 8) & 0xff;
579 p
[7] = ((base
>> 8) >> 8) & 0xff;
580 p
[10] = (len
>> 8) & 0xff;
581 p
[11] = ((len
>> 8) >> 8) & 0xff;
585 static void pnpbios_encode_mem32(unsigned char *p
, struct resource
* res
)
587 unsigned long base
= res
->start
;
588 unsigned long len
= res
->end
- res
->start
+ 1;
590 p
[5] = (base
>> 8) & 0xff;
591 p
[6] = (base
>> 16) & 0xff;
592 p
[7] = (base
>> 24) & 0xff;
594 p
[9] = (base
>> 8) & 0xff;
595 p
[10] = (base
>> 16) & 0xff;
596 p
[11] = (base
>> 24) & 0xff;
598 p
[17] = (len
>> 8) & 0xff;
599 p
[18] = (len
>> 16) & 0xff;
600 p
[19] = (len
>> 24) & 0xff;
604 static void pnpbios_encode_fixed_mem32(unsigned char *p
, struct resource
* res
)
605 { unsigned long base
= res
->start
;
606 unsigned long len
= res
->end
- res
->start
+ 1;
608 p
[5] = (base
>> 8) & 0xff;
609 p
[6] = (base
>> 16) & 0xff;
610 p
[7] = (base
>> 24) & 0xff;
612 p
[9] = (len
>> 8) & 0xff;
613 p
[10] = (len
>> 16) & 0xff;
614 p
[11] = (len
>> 24) & 0xff;
618 static void pnpbios_encode_irq(unsigned char *p
, struct resource
* res
)
620 unsigned long map
= 0;
621 map
= 1 << res
->start
;
623 p
[2] = (map
>> 8) & 0xff;
627 static void pnpbios_encode_dma(unsigned char *p
, struct resource
* res
)
629 unsigned long map
= 0;
630 map
= 1 << res
->start
;
635 static void pnpbios_encode_port(unsigned char *p
, struct resource
* res
)
637 unsigned long base
= res
->start
;
638 unsigned long len
= res
->end
- res
->start
+ 1;
640 p
[3] = (base
>> 8) & 0xff;
642 p
[5] = (base
>> 8) & 0xff;
647 static void pnpbios_encode_fixed_port(unsigned char *p
, struct resource
* res
)
649 unsigned long base
= res
->start
;
650 unsigned long len
= res
->end
- res
->start
+ 1;
652 p
[2] = (base
>> 8) & 0xff;
657 static unsigned char *
658 pnpbios_encode_allocated_resource_data(unsigned char * p
, unsigned char * end
, struct pnp_resource_table
* res
)
660 unsigned int len
, tag
;
661 int port
= 0, irq
= 0, dma
= 0, mem
= 0;
666 while ((char *)p
< (char *)end
) {
668 /* determine the type of tag */
669 if (p
[0] & LARGE_TAG
) { /* large tag */
670 len
= (p
[2] << 8) | p
[1];
672 } else { /* small tag */
674 tag
= ((p
[0]>>3) & 0x0f);
682 pnpbios_encode_mem(p
, &res
->mem_resource
[mem
]);
686 case LARGE_TAG_MEM32
:
689 pnpbios_encode_mem32(p
, &res
->mem_resource
[mem
]);
693 case LARGE_TAG_FIXEDMEM32
:
696 pnpbios_encode_fixed_mem32(p
, &res
->mem_resource
[mem
]);
701 if (len
< 2 || len
> 3)
703 pnpbios_encode_irq(p
, &res
->irq_resource
[irq
]);
710 pnpbios_encode_dma(p
, &res
->dma_resource
[dma
]);
717 pnpbios_encode_port(p
, &res
->port_resource
[port
]);
721 case SMALL_TAG_VENDOR
:
725 case SMALL_TAG_FIXEDPORT
:
728 pnpbios_encode_fixed_port(p
, &res
->port_resource
[port
]);
734 return (unsigned char *)p
;
737 default: /* an unkown tag */
739 printk(KERN_ERR
"PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag
, len
);
743 /* continue to the next tag */
744 if (p
[0] & LARGE_TAG
)
750 printk(KERN_ERR
"PnPBIOS: Resource structure does not contain an end tag.\n");
757 * Core Parsing Functions
761 pnpbios_parse_data_stream(struct pnp_dev
*dev
, struct pnp_bios_node
* node
)
763 unsigned char * p
= (char *)node
->data
;
764 unsigned char * end
= (char *)(node
->data
+ node
->size
);
765 p
= pnpbios_parse_allocated_resource_data(p
,end
,&dev
->res
);
768 p
= pnpbios_parse_resource_option_data(p
,end
,dev
);
771 p
= pnpbios_parse_compatible_ids(p
,end
,dev
);
778 pnpbios_read_resources_from_node(struct pnp_resource_table
*res
,
779 struct pnp_bios_node
* node
)
781 unsigned char * p
= (char *)node
->data
;
782 unsigned char * end
= (char *)(node
->data
+ node
->size
);
783 p
= pnpbios_parse_allocated_resource_data(p
,end
,res
);
790 pnpbios_write_resources_to_node(struct pnp_resource_table
*res
,
791 struct pnp_bios_node
* node
)
793 unsigned char * p
= (char *)node
->data
;
794 unsigned char * end
= (char *)(node
->data
+ node
->size
);
795 p
= pnpbios_encode_allocated_resource_data(p
,end
,res
);