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
, *option_independent
;
354 option_independent
= 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
:
431 if (option_independent
== option
)
432 printk(KERN_WARNING
"PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
433 option
= option_independent
;
437 if (option_independent
!= option
)
438 printk(KERN_WARNING
"PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
440 return (unsigned char *)p
;
443 default: /* an unkown tag */
445 printk(KERN_ERR
"PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag
, len
);
449 /* continue to the next tag */
450 if (p
[0] & LARGE_TAG
)
456 printk(KERN_ERR
"PnPBIOS: Resource structure does not contain an end tag.\n");
463 * Compatible Device IDs
466 #define HEX(id,a) hex[((id)>>a) & 15]
467 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
470 void pnpid32_to_pnpid(u32 id
, char *str
)
472 const char *hex
= "0123456789abcdef";
474 id
= be32_to_cpu(id
);
475 str
[0] = CHAR(id
, 26);
476 str
[1] = CHAR(id
, 21);
477 str
[2] = CHAR(id
,16);
478 str
[3] = HEX(id
, 12);
490 static unsigned char *
491 pnpbios_parse_compatible_ids(unsigned char *p
, unsigned char *end
, struct pnp_dev
*dev
)
495 struct pnp_id
*dev_id
;
500 while ((char *)p
< (char *)end
) {
502 /* determine the type of tag */
503 if (p
[0] & LARGE_TAG
) { /* large tag */
504 len
= (p
[2] << 8) | p
[1];
506 } else { /* small tag */
508 tag
= ((p
[0]>>3) & 0x0f);
513 case LARGE_TAG_ANSISTR
:
514 strncpy(dev
->name
, p
+ 3, len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 2 : len
);
515 dev
->name
[len
>= PNP_NAME_LEN
? PNP_NAME_LEN
- 1 : len
] = '\0';
518 case SMALL_TAG_COMPATDEVID
: /* compatible ID */
521 dev_id
= pnpbios_kmalloc(sizeof (struct pnp_id
), GFP_KERNEL
);
524 memset(dev_id
, 0, sizeof(struct pnp_id
));
525 pnpid32_to_pnpid(p
[1] | p
[2] << 8 | p
[3] << 16 | p
[4] << 24,id
);
526 memcpy(&dev_id
->id
, id
, 7);
527 pnp_add_id(dev_id
, dev
);
532 return (unsigned char *)p
;
535 default: /* an unkown tag */
537 printk(KERN_ERR
"PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag
, len
);
541 /* continue to the next tag */
542 if (p
[0] & LARGE_TAG
)
548 printk(KERN_ERR
"PnPBIOS: Resource structure does not contain an end tag.\n");
555 * Allocated Resource Encoding
558 static void pnpbios_encode_mem(unsigned char *p
, struct resource
* res
)
560 unsigned long base
= res
->start
;
561 unsigned long len
= res
->end
- res
->start
+ 1;
562 p
[4] = (base
>> 8) & 0xff;
563 p
[5] = ((base
>> 8) >> 8) & 0xff;
564 p
[6] = (base
>> 8) & 0xff;
565 p
[7] = ((base
>> 8) >> 8) & 0xff;
566 p
[10] = (len
>> 8) & 0xff;
567 p
[11] = ((len
>> 8) >> 8) & 0xff;
571 static void pnpbios_encode_mem32(unsigned char *p
, struct resource
* res
)
573 unsigned long base
= res
->start
;
574 unsigned long len
= res
->end
- res
->start
+ 1;
576 p
[5] = (base
>> 8) & 0xff;
577 p
[6] = (base
>> 16) & 0xff;
578 p
[7] = (base
>> 24) & 0xff;
580 p
[9] = (base
>> 8) & 0xff;
581 p
[10] = (base
>> 16) & 0xff;
582 p
[11] = (base
>> 24) & 0xff;
584 p
[17] = (len
>> 8) & 0xff;
585 p
[18] = (len
>> 16) & 0xff;
586 p
[19] = (len
>> 24) & 0xff;
590 static void pnpbios_encode_fixed_mem32(unsigned char *p
, struct resource
* res
)
591 { unsigned long base
= res
->start
;
592 unsigned long len
= res
->end
- res
->start
+ 1;
594 p
[5] = (base
>> 8) & 0xff;
595 p
[6] = (base
>> 16) & 0xff;
596 p
[7] = (base
>> 24) & 0xff;
598 p
[9] = (len
>> 8) & 0xff;
599 p
[10] = (len
>> 16) & 0xff;
600 p
[11] = (len
>> 24) & 0xff;
604 static void pnpbios_encode_irq(unsigned char *p
, struct resource
* res
)
606 unsigned long map
= 0;
607 map
= 1 << res
->start
;
609 p
[2] = (map
>> 8) & 0xff;
613 static void pnpbios_encode_dma(unsigned char *p
, struct resource
* res
)
615 unsigned long map
= 0;
616 map
= 1 << res
->start
;
621 static void pnpbios_encode_port(unsigned char *p
, struct resource
* res
)
623 unsigned long base
= res
->start
;
624 unsigned long len
= res
->end
- res
->start
+ 1;
626 p
[3] = (base
>> 8) & 0xff;
628 p
[5] = (base
>> 8) & 0xff;
633 static void pnpbios_encode_fixed_port(unsigned char *p
, struct resource
* res
)
635 unsigned long base
= res
->start
;
636 unsigned long len
= res
->end
- res
->start
+ 1;
638 p
[2] = (base
>> 8) & 0xff;
643 static unsigned char *
644 pnpbios_encode_allocated_resource_data(unsigned char * p
, unsigned char * end
, struct pnp_resource_table
* res
)
646 unsigned int len
, tag
;
647 int port
= 0, irq
= 0, dma
= 0, mem
= 0;
652 while ((char *)p
< (char *)end
) {
654 /* determine the type of tag */
655 if (p
[0] & LARGE_TAG
) { /* large tag */
656 len
= (p
[2] << 8) | p
[1];
658 } else { /* small tag */
660 tag
= ((p
[0]>>3) & 0x0f);
668 pnpbios_encode_mem(p
, &res
->mem_resource
[mem
]);
672 case LARGE_TAG_MEM32
:
675 pnpbios_encode_mem32(p
, &res
->mem_resource
[mem
]);
679 case LARGE_TAG_FIXEDMEM32
:
682 pnpbios_encode_fixed_mem32(p
, &res
->mem_resource
[mem
]);
687 if (len
< 2 || len
> 3)
689 pnpbios_encode_irq(p
, &res
->irq_resource
[irq
]);
696 pnpbios_encode_dma(p
, &res
->dma_resource
[dma
]);
703 pnpbios_encode_port(p
, &res
->port_resource
[port
]);
707 case SMALL_TAG_VENDOR
:
711 case SMALL_TAG_FIXEDPORT
:
714 pnpbios_encode_fixed_port(p
, &res
->port_resource
[port
]);
720 return (unsigned char *)p
;
723 default: /* an unkown tag */
725 printk(KERN_ERR
"PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag
, len
);
729 /* continue to the next tag */
730 if (p
[0] & LARGE_TAG
)
736 printk(KERN_ERR
"PnPBIOS: Resource structure does not contain an end tag.\n");
743 * Core Parsing Functions
747 pnpbios_parse_data_stream(struct pnp_dev
*dev
, struct pnp_bios_node
* node
)
749 unsigned char * p
= (char *)node
->data
;
750 unsigned char * end
= (char *)(node
->data
+ node
->size
);
751 p
= pnpbios_parse_allocated_resource_data(p
,end
,&dev
->res
);
754 p
= pnpbios_parse_resource_option_data(p
,end
,dev
);
757 p
= pnpbios_parse_compatible_ids(p
,end
,dev
);
764 pnpbios_read_resources_from_node(struct pnp_resource_table
*res
,
765 struct pnp_bios_node
* node
)
767 unsigned char * p
= (char *)node
->data
;
768 unsigned char * end
= (char *)(node
->data
+ node
->size
);
769 p
= pnpbios_parse_allocated_resource_data(p
,end
,res
);
776 pnpbios_write_resources_to_node(struct pnp_resource_table
*res
,
777 struct pnp_bios_node
* node
)
779 unsigned char * p
= (char *)node
->data
;
780 unsigned char * end
= (char *)(node
->data
+ node
->size
);
781 p
= pnpbios_encode_allocated_resource_data(p
,end
,res
);