MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / pnp / pnpbios / rsparser.c
blob7aafbf80daf3e15faf1a1d779a6fab05e23f9ad3
1 /*
2 * rsparser.c - parses and encodes pnpbios resource data streams
4 */
6 #include <linux/config.h>
7 #include <linux/ctype.h>
8 #include <linux/pnp.h>
9 #include <linux/pnpbios.h>
11 #include "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)
37 * end tag ->
38 * Resource Configuration Options (optional)
39 * end tag ->
40 * Compitable Device IDs (optional)
41 * final end tag ->
45 * Allocated Resources
48 static void
49 pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq)
51 int i = 0;
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
55 if (irq == -1) {
56 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
57 return;
59 res->irq_resource[i].start =
60 res->irq_resource[i].end = (unsigned long) irq;
64 static void
65 pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma)
67 int i = 0;
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
71 if (dma == -1) {
72 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
73 return;
75 res->dma_resource[i].start =
76 res->dma_resource[i].end = (unsigned long) dma;
80 static void
81 pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len)
83 int i = 0;
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;
89 return;
91 res->port_resource[i].start = (unsigned long) io;
92 res->port_resource[i].end = (unsigned long)(io + len - 1);
96 static void
97 pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len)
99 int i = 0;
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
103 if (len <= 0) {
104 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
105 return;
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;
118 if (!p)
119 return NULL;
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];
129 tag = p[0];
130 } else { /* small tag */
131 len = p[0] & 0x07;
132 tag = ((p[0]>>3) & 0x0f);
135 switch (tag) {
137 case LARGE_TAG_MEM:
138 if (len != 9)
139 goto len_err;
140 io = *(short *) &p[4];
141 size = *(short *) &p[10];
142 pnpbios_parse_allocated_memresource(res, io, size);
143 break;
145 case LARGE_TAG_ANSISTR:
146 /* ignore this for now */
147 break;
149 case LARGE_TAG_VENDOR:
150 /* do nothing */
151 break;
153 case LARGE_TAG_MEM32:
154 if (len != 17)
155 goto len_err;
156 io = *(int *) &p[4];
157 size = *(int *) &p[16];
158 pnpbios_parse_allocated_memresource(res, io, size);
159 break;
161 case LARGE_TAG_FIXEDMEM32:
162 if (len != 9)
163 goto len_err;
164 io = *(int *) &p[4];
165 size = *(int *) &p[8];
166 pnpbios_parse_allocated_memresource(res, io, size);
167 break;
169 case SMALL_TAG_IRQ:
170 if (len < 2 || len > 3)
171 goto len_err;
172 io = -1;
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);
177 break;
179 case SMALL_TAG_DMA:
180 if (len != 2)
181 goto len_err;
182 io = -1;
183 mask = p[1];
184 for (i=0;i<8;i++, mask = mask>>1)
185 if(mask & 0x01) io=i;
186 pnpbios_parse_allocated_dmaresource(res, io);
187 break;
189 case SMALL_TAG_PORT:
190 if (len != 7)
191 goto len_err;
192 io = p[2] + p[3] *256;
193 size = p[7];
194 pnpbios_parse_allocated_ioresource(res, io, size);
195 break;
197 case SMALL_TAG_VENDOR:
198 /* do nothing */
199 break;
201 case SMALL_TAG_FIXEDPORT:
202 if (len != 3)
203 goto len_err;
204 io = p[1] + p[2] * 256;
205 size = p[3];
206 pnpbios_parse_allocated_ioresource(res, io, size);
207 break;
209 case SMALL_TAG_END:
210 p = p + 2;
211 return (unsigned char *)p;
212 break;
214 default: /* an unkown tag */
215 len_err:
216 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
217 break;
220 /* continue to the next tag */
221 if (p[0] & LARGE_TAG)
222 p += len + 3;
223 else
224 p += len + 1;
227 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
229 return NULL;
234 * Resource Configuration Options
237 static void
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);
242 if (!mem)
243 return;
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;
248 mem->flags = p[3];
249 pnp_register_mem_resource(option,mem);
250 return;
253 static void
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);
258 if (!mem)
259 return;
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];
264 mem->flags = p[3];
265 pnp_register_mem_resource(option,mem);
266 return;
269 static void
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);
274 if (!mem)
275 return;
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];
278 mem->align = 0;
279 mem->flags = p[3];
280 pnp_register_mem_resource(option,mem);
281 return;
284 static void
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);
289 if (!irq)
290 return;
291 irq->map = (p[2] << 8) | p[1];
292 if (size > 2)
293 irq->flags = p[3];
294 else
295 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
296 pnp_register_irq_resource(option,irq);
297 return;
300 static void
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);
305 if (!dma)
306 return;
307 dma->map = p[1];
308 dma->flags = p[2];
309 pnp_register_dma_resource(option,dma);
310 return;
313 static void
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);
318 if (!port)
319 return;
320 port->min = (p[3] << 8) | p[2];
321 port->max = (p[5] << 8) | p[4];
322 port->align = p[6];
323 port->size = p[7];
324 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
325 pnp_register_port_resource(option,port);
326 return;
329 static void
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);
334 if (!port)
335 return;
336 port->min = port->max = (p[2] << 8) | p[1];
337 port->size = p[3];
338 port->align = 0;
339 port->flags = PNP_PORT_FLAG_FIXED;
340 pnp_register_port_resource(option,port);
341 return;
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;
348 int priority = 0;
349 struct pnp_option *option, *option_independent;
351 if (!p)
352 return NULL;
354 option_independent = option = pnp_register_independent_option(dev);
355 if (!option)
356 return NULL;
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];
363 tag = p[0];
364 } else { /* small tag */
365 len = p[0] & 0x07;
366 tag = ((p[0]>>3) & 0x0f);
369 switch (tag) {
371 case LARGE_TAG_MEM:
372 if (len != 9)
373 goto len_err;
374 pnpbios_parse_mem_option(p, len, option);
375 break;
377 case LARGE_TAG_MEM32:
378 if (len != 17)
379 goto len_err;
380 pnpbios_parse_mem32_option(p, len, option);
381 break;
383 case LARGE_TAG_FIXEDMEM32:
384 if (len != 9)
385 goto len_err;
386 pnpbios_parse_fixed_mem32_option(p, len, option);
387 break;
389 case SMALL_TAG_IRQ:
390 if (len < 2 || len > 3)
391 goto len_err;
392 pnpbios_parse_irq_option(p, len, option);
393 break;
395 case SMALL_TAG_DMA:
396 if (len != 2)
397 goto len_err;
398 pnpbios_parse_dma_option(p, len, option);
399 break;
401 case SMALL_TAG_PORT:
402 if (len != 7)
403 goto len_err;
404 pnpbios_parse_port_option(p, len, option);
405 break;
407 case SMALL_TAG_VENDOR:
408 /* do nothing */
409 break;
411 case SMALL_TAG_FIXEDPORT:
412 if (len != 3)
413 goto len_err;
414 pnpbios_parse_fixed_port_option(p, len, option);
415 break;
417 case SMALL_TAG_STARTDEP:
418 if (len > 1)
419 goto len_err;
420 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
421 if (len > 0)
422 priority = 0x100 | p[1];
423 option = pnp_register_dependent_option(dev, priority);
424 if (!option)
425 return NULL;
426 break;
428 case SMALL_TAG_ENDDEP:
429 if (len != 0)
430 goto len_err;
431 if (option_independent == option)
432 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n");
433 option = option_independent;
434 break;
436 case SMALL_TAG_END:
437 if (option_independent != option)
438 printk(KERN_WARNING "PnPBIOS: Missing SMALL_TAG_ENDDEP tag\n");
439 p = p + 2;
440 return (unsigned char *)p;
441 break;
443 default: /* an unkown tag */
444 len_err:
445 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
446 break;
449 /* continue to the next tag */
450 if (p[0] & LARGE_TAG)
451 p += len + 3;
452 else
453 p += len + 1;
456 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
458 return NULL;
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);
479 str[4] = HEX(id, 8);
480 str[5] = HEX(id, 4);
481 str[6] = HEX(id, 0);
482 str[7] = '\0';
484 return;
487 #undef CHAR
488 #undef HEX
490 static unsigned char *
491 pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev)
493 int len, tag;
494 char id[8];
495 struct pnp_id *dev_id;
497 if (!p)
498 return NULL;
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];
505 tag = p[0];
506 } else { /* small tag */
507 len = p[0] & 0x07;
508 tag = ((p[0]>>3) & 0x0f);
511 switch (tag) {
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';
516 break;
518 case SMALL_TAG_COMPATDEVID: /* compatible ID */
519 if (len != 4)
520 goto len_err;
521 dev_id = pnpbios_kmalloc(sizeof (struct pnp_id), GFP_KERNEL);
522 if (!dev_id)
523 return NULL;
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);
528 break;
530 case SMALL_TAG_END:
531 p = p + 2;
532 return (unsigned char *)p;
533 break;
535 default: /* an unkown tag */
536 len_err:
537 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
538 break;
541 /* continue to the next tag */
542 if (p[0] & LARGE_TAG)
543 p += len + 3;
544 else
545 p += len + 1;
548 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
550 return NULL;
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;
568 return;
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;
575 p[4] = base & 0xff;
576 p[5] = (base >> 8) & 0xff;
577 p[6] = (base >> 16) & 0xff;
578 p[7] = (base >> 24) & 0xff;
579 p[8] = base & 0xff;
580 p[9] = (base >> 8) & 0xff;
581 p[10] = (base >> 16) & 0xff;
582 p[11] = (base >> 24) & 0xff;
583 p[16] = len & 0xff;
584 p[17] = (len >> 8) & 0xff;
585 p[18] = (len >> 16) & 0xff;
586 p[19] = (len >> 24) & 0xff;
587 return;
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;
593 p[4] = base & 0xff;
594 p[5] = (base >> 8) & 0xff;
595 p[6] = (base >> 16) & 0xff;
596 p[7] = (base >> 24) & 0xff;
597 p[8] = len & 0xff;
598 p[9] = (len >> 8) & 0xff;
599 p[10] = (len >> 16) & 0xff;
600 p[11] = (len >> 24) & 0xff;
601 return;
604 static void pnpbios_encode_irq(unsigned char *p, struct resource * res)
606 unsigned long map = 0;
607 map = 1 << res->start;
608 p[1] = map & 0xff;
609 p[2] = (map >> 8) & 0xff;
610 return;
613 static void pnpbios_encode_dma(unsigned char *p, struct resource * res)
615 unsigned long map = 0;
616 map = 1 << res->start;
617 p[1] = map & 0xff;
618 return;
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;
625 p[2] = base & 0xff;
626 p[3] = (base >> 8) & 0xff;
627 p[4] = base & 0xff;
628 p[5] = (base >> 8) & 0xff;
629 p[7] = len & 0xff;
630 return;
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;
637 p[1] = base & 0xff;
638 p[2] = (base >> 8) & 0xff;
639 p[3] = len & 0xff;
640 return;
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;
649 if (!p)
650 return NULL;
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];
657 tag = p[0];
658 } else { /* small tag */
659 len = p[0] & 0x07;
660 tag = ((p[0]>>3) & 0x0f);
663 switch (tag) {
665 case LARGE_TAG_MEM:
666 if (len != 9)
667 goto len_err;
668 pnpbios_encode_mem(p, &res->mem_resource[mem]);
669 mem++;
670 break;
672 case LARGE_TAG_MEM32:
673 if (len != 17)
674 goto len_err;
675 pnpbios_encode_mem32(p, &res->mem_resource[mem]);
676 mem++;
677 break;
679 case LARGE_TAG_FIXEDMEM32:
680 if (len != 9)
681 goto len_err;
682 pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]);
683 mem++;
684 break;
686 case SMALL_TAG_IRQ:
687 if (len < 2 || len > 3)
688 goto len_err;
689 pnpbios_encode_irq(p, &res->irq_resource[irq]);
690 irq++;
691 break;
693 case SMALL_TAG_DMA:
694 if (len != 2)
695 goto len_err;
696 pnpbios_encode_dma(p, &res->dma_resource[dma]);
697 dma++;
698 break;
700 case SMALL_TAG_PORT:
701 if (len != 7)
702 goto len_err;
703 pnpbios_encode_port(p, &res->port_resource[port]);
704 port++;
705 break;
707 case SMALL_TAG_VENDOR:
708 /* do nothing */
709 break;
711 case SMALL_TAG_FIXEDPORT:
712 if (len != 3)
713 goto len_err;
714 pnpbios_encode_fixed_port(p, &res->port_resource[port]);
715 port++;
716 break;
718 case SMALL_TAG_END:
719 p = p + 2;
720 return (unsigned char *)p;
721 break;
723 default: /* an unkown tag */
724 len_err:
725 printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len);
726 break;
729 /* continue to the next tag */
730 if (p[0] & LARGE_TAG)
731 p += len + 3;
732 else
733 p += len + 1;
736 printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n");
738 return NULL;
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);
752 if (!p)
753 return -EIO;
754 p = pnpbios_parse_resource_option_data(p,end,dev);
755 if (!p)
756 return -EIO;
757 p = pnpbios_parse_compatible_ids(p,end,dev);
758 if (!p)
759 return -EIO;
760 return 0;
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);
770 if (!p)
771 return -EIO;
772 return 0;
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);
782 if (!p)
783 return -EIO;
784 return 0;