Linux 2.6.21
[linux/fpc-iii.git] / drivers / pnp / pnpbios / rsparser.c
blob3c2ab8394e3f987b71a555461e98c3c0b1a58e3e
1 /*
2 * rsparser.c - parses and encodes pnpbios resource data streams
4 */
6 #include <linux/ctype.h>
7 #include <linux/pnp.h>
8 #include <linux/pnpbios.h>
9 #include <linux/string.h>
10 #include <linux/slab.h>
12 #ifdef CONFIG_PCI
13 #include <linux/pci.h>
14 #else
15 inline void pcibios_penalize_isa_irq(int irq, int active) {}
16 #endif /* CONFIG_PCI */
18 #include "pnpbios.h"
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)
44 * end tag ->
45 * Resource Configuration Options (optional)
46 * end tag ->
47 * Compitable Device IDs (optional)
48 * final end tag ->
52 * Allocated Resources
55 static void
56 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
58 int i = 0;
59 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_IRQ) i++;
60 if (i < PNP_MAX_IRQ) {
61 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
62 if (irq == -1) {
63 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
64 return;
66 res->irq_resource[i].start =
67 res->irq_resource[i].end = (unsigned long) irq;
68 pcibios_penalize_isa_irq(irq, 1);
72 static void
73 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
75 int i = 0;
76 while (i < PNP_MAX_DMA &&
77 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
78 i++;
79 if (i < PNP_MAX_DMA) {
80 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
81 if (dma == -1) {
82 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
83 return;
85 res->dma_resource[i].start =
86 res->dma_resource[i].end = (unsigned long) dma;
90 static void
91 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
93 int i = 0;
94 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_PORT) i++;
95 if (i < PNP_MAX_PORT) {
96 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
97 if (len <= 0 || (io + len -1) >= 0x10003) {
98 res->port_resource[i].flags |= IORESOURCE_DISABLED;
99 return;
101 res->port_resource[i].start = (unsigned long) io;
102 res->port_resource[i].end = (unsigned long)(io + len - 1);
106 static void
107 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
109 int i = 0;
110 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) && i < PNP_MAX_MEM) i++;
111 if (i < PNP_MAX_MEM) {
112 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
113 if (len <= 0) {
114 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
115 return;
117 res->mem_resource[i].start = (unsigned long) mem;
118 res->mem_resource[i].end = (unsigned long)(mem + len - 1);
122 static unsigned char *
123 pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
125 unsigned int len, tag;
126 int io, size, mask, i;
128 if (!p)
129 return NULL;
131 /* Blank the resource table values */
132 pnp_init_resource_table(res);
134 while ((char *)p < (char *)end) {
136 /* determine the type of tag */
137 if (p[0] & LARGE_TAG) { /* large tag */
138 len = (p[2] << 8) | p[1];
139 tag = p[0];
140 } else { /* small tag */
141 len = p[0] & 0x07;
142 tag = ((p[0]>>3) & 0x0f);
145 switch (tag) {
147 case LARGE_TAG_MEM:
148 if (len != 9)
149 goto len_err;
150 io = *(short *) &p[4];
151 size = *(short *) &p[10];
152 pnpbios_parse_allocated_memresource(res, io, size);
153 break;
155 case LARGE_TAG_ANSISTR:
156 /* ignore this for now */
157 break;
159 case LARGE_TAG_VENDOR:
160 /* do nothing */
161 break;
163 case LARGE_TAG_MEM32:
164 if (len != 17)
165 goto len_err;
166 io = *(int *) &p[4];
167 size = *(int *) &p[16];
168 pnpbios_parse_allocated_memresource(res, io, size);
169 break;
171 case LARGE_TAG_FIXEDMEM32:
172 if (len != 9)
173 goto len_err;
174 io = *(int *) &p[4];
175 size = *(int *) &p[8];
176 pnpbios_parse_allocated_memresource(res, io, size);
177 break;
179 case SMALL_TAG_IRQ:
180 if (len < 2 || len > 3)
181 goto len_err;
182 io = -1;
183 mask= p[1] + p[2]*256;
184 for (i=0;i<16;i++, mask=mask>>1)
185 if(mask & 0x01) io=i;
186 pnpbios_parse_allocated_irqresource(res, io);
187 break;
189 case SMALL_TAG_DMA:
190 if (len != 2)
191 goto len_err;
192 io = -1;
193 mask = p[1];
194 for (i=0;i<8;i++, mask = mask>>1)
195 if(mask & 0x01) io=i;
196 pnpbios_parse_allocated_dmaresource(res, io);
197 break;
199 case SMALL_TAG_PORT:
200 if (len != 7)
201 goto len_err;
202 io = p[2] + p[3] *256;
203 size = p[7];
204 pnpbios_parse_allocated_ioresource(res, io, size);
205 break;
207 case SMALL_TAG_VENDOR:
208 /* do nothing */
209 break;
211 case SMALL_TAG_FIXEDPORT:
212 if (len != 3)
213 goto len_err;
214 io = p[1] + p[2] * 256;
215 size = p[3];
216 pnpbios_parse_allocated_ioresource(res, io, size);
217 break;
219 case SMALL_TAG_END:
220 p = p + 2;
221 return (unsigned char *)p;
222 break;
224 default: /* an unkown tag */
225 len_err:
226 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
227 break;
230 /* continue to the next tag */
231 if (p[0] & LARGE_TAG)
232 p += len + 3;
233 else
234 p += len + 1;
237 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
239 return NULL;
244 * Resource Configuration Options
247 static void
248 pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option)
250 struct pnp_mem * mem;
251 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
252 if (!mem)
253 return;
254 mem->min = ((p[5] << 8) | p[4]) << 8;
255 mem->max = ((p[7] << 8) | p[6]) << 8;
256 mem->align = (p[9] << 8) | p[8];
257 mem->size = ((p[11] << 8) | p[10]) << 8;
258 mem->flags = p[3];
259 pnp_register_mem_resource(option,mem);
260 return;
263 static void
264 pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option)
266 struct pnp_mem * mem;
267 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
268 if (!mem)
269 return;
270 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
271 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
272 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
273 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
274 mem->flags = p[3];
275 pnp_register_mem_resource(option,mem);
276 return;
279 static void
280 pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option)
282 struct pnp_mem * mem;
283 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
284 if (!mem)
285 return;
286 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
287 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
288 mem->align = 0;
289 mem->flags = p[3];
290 pnp_register_mem_resource(option,mem);
291 return;
294 static void
295 pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option)
297 struct pnp_irq * irq;
298 unsigned long bits;
300 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
301 if (!irq)
302 return;
303 bits = (p[2] << 8) | p[1];
304 bitmap_copy(irq->map, &bits, 16);
305 if (size > 2)
306 irq->flags = p[3];
307 else
308 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
309 pnp_register_irq_resource(option,irq);
310 return;
313 static void
314 pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option)
316 struct pnp_dma * dma;
317 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
318 if (!dma)
319 return;
320 dma->map = p[1];
321 dma->flags = p[2];
322 pnp_register_dma_resource(option,dma);
323 return;
326 static void
327 pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option)
329 struct pnp_port * port;
330 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
331 if (!port)
332 return;
333 port->min = (p[3] << 8) | p[2];
334 port->max = (p[5] << 8) | p[4];
335 port->align = p[6];
336 port->size = p[7];
337 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
338 pnp_register_port_resource(option,port);
339 return;
342 static void
343 pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option)
345 struct pnp_port * port;
346 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
347 if (!port)
348 return;
349 port->min = port->max = (p[2] << 8) | p[1];
350 port->size = p[3];
351 port->align = 0;
352 port->flags = PNP_PORT_FLAG_FIXED;
353 pnp_register_port_resource(option,port);
354 return;
357 static unsigned char *
358 pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev)
360 unsigned int len, tag;
361 int priority = 0;
362 struct pnp_option *option, *option_independent;
364 if (!p)
365 return NULL;
367 option_independent = option = pnp_register_independent_option(dev);
368 if (!option)
369 return NULL;
371 while ((char *)p < (char *)end) {
373 /* determine the type of tag */
374 if (p[0] & LARGE_TAG) { /* large tag */
375 len = (p[2] << 8) | p[1];
376 tag = p[0];
377 } else { /* small tag */
378 len = p[0] & 0x07;
379 tag = ((p[0]>>3) & 0x0f);
382 switch (tag) {
384 case LARGE_TAG_MEM:
385 if (len != 9)
386 goto len_err;
387 pnpbios_parse_mem_option(p, len, option);
388 break;
390 case LARGE_TAG_MEM32:
391 if (len != 17)
392 goto len_err;
393 pnpbios_parse_mem32_option(p, len, option);
394 break;
396 case LARGE_TAG_FIXEDMEM32:
397 if (len != 9)
398 goto len_err;
399 pnpbios_parse_fixed_mem32_option(p, len, option);
400 break;
402 case SMALL_TAG_IRQ:
403 if (len < 2 || len > 3)
404 goto len_err;
405 pnpbios_parse_irq_option(p, len, option);
406 break;
408 case SMALL_TAG_DMA:
409 if (len != 2)
410 goto len_err;
411 pnpbios_parse_dma_option(p, len, option);
412 break;
414 case SMALL_TAG_PORT:
415 if (len != 7)
416 goto len_err;
417 pnpbios_parse_port_option(p, len, option);
418 break;
420 case SMALL_TAG_VENDOR:
421 /* do nothing */
422 break;
424 case SMALL_TAG_FIXEDPORT:
425 if (len != 3)
426 goto len_err;
427 pnpbios_parse_fixed_port_option(p, len, option);
428 break;
430 case SMALL_TAG_STARTDEP:
431 if (len > 1)
432 goto len_err;
433 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
434 if (len > 0)
435 priority = 0x100 | p[1];
436 option = pnp_register_dependent_option(dev, priority);
437 if (!option)
438 return NULL;
439 break;
441 case SMALL_TAG_ENDDEP:
442 if (len != 0)
443 goto len_err;
444 if (option_independent == option)
445 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
446 option = option_independent;
447 break;
449 case SMALL_TAG_END:
450 return p + 2;
452 default: /* an unkown tag */
453 len_err:
454 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
455 break;
458 /* continue to the next tag */
459 if (p[0] & LARGE_TAG)
460 p += len + 3;
461 else
462 p += len + 1;
465 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
467 return NULL;
472 * Compatible Device IDs
475 #define HEX(id,a) hex[((id)>>a) & 15]
476 #define CHAR(id,a) (0x40 + (((id)>>a) & 31))
479 void pnpid32_to_pnpid(u32 id, char *str)
481 const char *hex = "0123456789abcdef";
483 id = be32_to_cpu(id);
484 str[0] = CHAR(id, 26);
485 str[1] = CHAR(id, 21);
486 str[2] = CHAR(id,16);
487 str[3] = HEX(id, 12);
488 str[4] = HEX(id, 8);
489 str[5] = HEX(id, 4);
490 str[6] = HEX(id, 0);
491 str[7] = '\0';
493 return;
496 #undef CHAR
497 #undef HEX
499 static unsigned char *
500 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
502 int len, tag;
503 char id[8];
504 struct pnp_id *dev_id;
506 if (!p)
507 return NULL;
509 while ((char *)p < (char *)end) {
511 /* determine the type of tag */
512 if (p[0] & LARGE_TAG) { /* large tag */
513 len = (p[2] << 8) | p[1];
514 tag = p[0];
515 } else { /* small tag */
516 len = p[0] & 0x07;
517 tag = ((p[0]>>3) & 0x0f);
520 switch (tag) {
522 case LARGE_TAG_ANSISTR:
523 strncpy(dev->name, p + 3, len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
524 dev->name[len >= PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
525 break;
527 case SMALL_TAG_COMPATDEVID: /* compatible ID */
528 if (len != 4)
529 goto len_err;
530 dev_id = kzalloc(sizeof (struct pnp_id), GFP_KERNEL);
531 if (!dev_id)
532 return NULL;
533 pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id);
534 memcpy(&dev_id->id, id, 7);
535 pnp_add_id(dev_id, dev);
536 break;
538 case SMALL_TAG_END:
539 p = p + 2;
540 return (unsigned char *)p;
541 break;
543 default: /* an unkown tag */
544 len_err:
545 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
546 break;
549 /* continue to the next tag */
550 if (p[0] & LARGE_TAG)
551 p += len + 3;
552 else
553 p += len + 1;
556 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
558 return NULL;
563 * Allocated Resource Encoding
566 static void pnpbios_encode_mem(unsigned char *p, struct resource * res)
568 unsigned long base = res->start;
569 unsigned long len = res->end - res->start + 1;
570 p[4] = (base >> 8) & 0xff;
571 p[5] = ((base >> 8) >> 8) & 0xff;
572 p[6] = (base >> 8) & 0xff;
573 p[7] = ((base >> 8) >> 8) & 0xff;
574 p[10] = (len >> 8) & 0xff;
575 p[11] = ((len >> 8) >> 8) & 0xff;
576 return;
579 static void pnpbios_encode_mem32(unsigned char *p, struct resource * res)
581 unsigned long base = res->start;
582 unsigned long len = res->end - res->start + 1;
583 p[4] = base & 0xff;
584 p[5] = (base >> 8) & 0xff;
585 p[6] = (base >> 16) & 0xff;
586 p[7] = (base >> 24) & 0xff;
587 p[8] = base & 0xff;
588 p[9] = (base >> 8) & 0xff;
589 p[10] = (base >> 16) & 0xff;
590 p[11] = (base >> 24) & 0xff;
591 p[16] = len & 0xff;
592 p[17] = (len >> 8) & 0xff;
593 p[18] = (len >> 16) & 0xff;
594 p[19] = (len >> 24) & 0xff;
595 return;
598 static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res)
599 { unsigned long base = res->start;
600 unsigned long len = res->end - res->start + 1;
601 p[4] = base & 0xff;
602 p[5] = (base >> 8) & 0xff;
603 p[6] = (base >> 16) & 0xff;
604 p[7] = (base >> 24) & 0xff;
605 p[8] = len & 0xff;
606 p[9] = (len >> 8) & 0xff;
607 p[10] = (len >> 16) & 0xff;
608 p[11] = (len >> 24) & 0xff;
609 return;
612 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
614 unsigned long map = 0;
615 map = 1 << res->start;
616 p[1] = map & 0xff;
617 p[2] = (map >> 8) & 0xff;
618 return;
621 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
623 unsigned long map = 0;
624 map = 1 << res->start;
625 p[1] = map & 0xff;
626 return;
629 static void pnpbios_encode_port(unsigned char *p, struct resource * res)
631 unsigned long base = res->start;
632 unsigned long len = res->end - res->start + 1;
633 p[2] = base & 0xff;
634 p[3] = (base >> 8) & 0xff;
635 p[4] = base & 0xff;
636 p[5] = (base >> 8) & 0xff;
637 p[7] = len & 0xff;
638 return;
641 static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res)
643 unsigned long base = res->start;
644 unsigned long len = res->end - res->start + 1;
645 p[1] = base & 0xff;
646 p[2] = (base >> 8) & 0xff;
647 p[3] = len & 0xff;
648 return;
651 static unsigned char *
652 pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res)
654 unsigned int len, tag;
655 int port = 0, irq = 0, dma = 0, mem = 0;
657 if (!p)
658 return NULL;
660 while ((char *)p < (char *)end) {
662 /* determine the type of tag */
663 if (p[0] & LARGE_TAG) { /* large tag */
664 len = (p[2] << 8) | p[1];
665 tag = p[0];
666 } else { /* small tag */
667 len = p[0] & 0x07;
668 tag = ((p[0]>>3) & 0x0f);
671 switch (tag) {
673 case LARGE_TAG_MEM:
674 if (len != 9)
675 goto len_err;
676 pnpbios_encode_mem(p, &res->mem_resource[mem]);
677 mem++;
678 break;
680 case LARGE_TAG_MEM32:
681 if (len != 17)
682 goto len_err;
683 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
684 mem++;
685 break;
687 case LARGE_TAG_FIXEDMEM32:
688 if (len != 9)
689 goto len_err;
690 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
691 mem++;
692 break;
694 case SMALL_TAG_IRQ:
695 if (len < 2 || len > 3)
696 goto len_err;
697 pnpbios_encode_irq(p, &res->irq_resource[irq]);
698 irq++;
699 break;
701 case SMALL_TAG_DMA:
702 if (len != 2)
703 goto len_err;
704 pnpbios_encode_dma(p, &res->dma_resource[dma]);
705 dma++;
706 break;
708 case SMALL_TAG_PORT:
709 if (len != 7)
710 goto len_err;
711 pnpbios_encode_port(p, &res->port_resource[port]);
712 port++;
713 break;
715 case SMALL_TAG_VENDOR:
716 /* do nothing */
717 break;
719 case SMALL_TAG_FIXEDPORT:
720 if (len != 3)
721 goto len_err;
722 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
723 port++;
724 break;
726 case SMALL_TAG_END:
727 p = p + 2;
728 return (unsigned char *)p;
729 break;
731 default: /* an unkown tag */
732 len_err:
733 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
734 break;
737 /* continue to the next tag */
738 if (p[0] & LARGE_TAG)
739 p += len + 3;
740 else
741 p += len + 1;
744 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
746 return NULL;
751 * Core Parsing Functions
755 pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node)
757 unsigned char * p = (char *)node->data;
758 unsigned char * end = (char *)(node->data + node->size);
759 p = pnpbios_parse_allocated_resource_data(p,end,&dev->res);
760 if (!p)
761 return -EIO;
762 p = pnpbios_parse_resource_option_data(p,end,dev);
763 if (!p)
764 return -EIO;
765 p = pnpbios_parse_compatible_ids(p,end,dev);
766 if (!p)
767 return -EIO;
768 return 0;
772 pnpbios_read_resources_from_node(struct pnp_resource_table *res,
773 struct pnp_bios_node * node)
775 unsigned char * p = (char *)node->data;
776 unsigned char * end = (char *)(node->data + node->size);
777 p = pnpbios_parse_allocated_resource_data(p,end,res);
778 if (!p)
779 return -EIO;
780 return 0;
784 pnpbios_write_resources_to_node(struct pnp_resource_table *res,
785 struct pnp_bios_node * node)
787 unsigned char * p = (char *)node->data;
788 unsigned char * end = (char *)(node->data + node->size);
789 p = pnpbios_encode_allocated_resource_data(p,end,res);
790 if (!p)
791 return -EIO;
792 return 0;