Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rsparser.c - parses and encodes pnpbios resource data streams |
| 3 | * |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/ctype.h> |
| 7 | #include <linux/pnp.h> |
| 8 | #include <linux/pnpbios.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 9 | #include <linux/string.h> |
| 10 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | #ifdef CONFIG_PCI |
| 13 | #include <linux/pci.h> |
| 14 | #else |
Adrian Bunk | 9a14d4c | 2005-07-27 11:43:41 -0700 | [diff] [blame] | 15 | inline void pcibios_penalize_isa_irq(int irq, int active) {} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #endif /* CONFIG_PCI */ |
| 17 | |
| 18 | #include "pnpbios.h" |
| 19 | |
| 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 |
| 39 | |
| 40 | /* |
| 41 | * Resource Data Stream Format: |
| 42 | * |
| 43 | * Allocated Resources (required) |
| 44 | * end tag -> |
| 45 | * Resource Configuration Options (optional) |
| 46 | * end tag -> |
| 47 | * Compitable Device IDs (optional) |
| 48 | * final end tag -> |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * Allocated Resources |
| 53 | */ |
| 54 | |
| 55 | static void |
| 56 | pnpbios_parse_allocated_irqresource(struct pnp_resource_table * res, int irq) |
| 57 | { |
| 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; |
| 65 | } |
| 66 | res->irq_resource[i].start = |
| 67 | res->irq_resource[i].end = (unsigned long) irq; |
David Shaohua Li | c9c3e45 | 2005-04-01 00:07:31 -0500 | [diff] [blame] | 68 | pcibios_penalize_isa_irq(irq, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | pnpbios_parse_allocated_dmaresource(struct pnp_resource_table * res, int dma) |
| 74 | { |
| 75 | int i = 0; |
Adrian Bunk | 6e3e98d | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 76 | while (i < PNP_MAX_DMA && |
| 77 | !(res->dma_resource[i].flags & IORESOURCE_UNSET)) |
| 78 | i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 84 | } |
| 85 | res->dma_resource[i].start = |
| 86 | res->dma_resource[i].end = (unsigned long) dma; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | pnpbios_parse_allocated_ioresource(struct pnp_resource_table * res, int io, int len) |
| 92 | { |
| 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; |
| 100 | } |
| 101 | res->port_resource[i].start = (unsigned long) io; |
| 102 | res->port_resource[i].end = (unsigned long)(io + len - 1); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | pnpbios_parse_allocated_memresource(struct pnp_resource_table * res, int mem, int len) |
| 108 | { |
| 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; |
| 116 | } |
| 117 | res->mem_resource[i].start = (unsigned long) mem; |
| 118 | res->mem_resource[i].end = (unsigned long)(mem + len - 1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static unsigned char * |
| 123 | pnpbios_parse_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res) |
| 124 | { |
| 125 | unsigned int len, tag; |
| 126 | int io, size, mask, i; |
| 127 | |
| 128 | if (!p) |
| 129 | return NULL; |
| 130 | |
| 131 | /* Blank the resource table values */ |
| 132 | pnp_init_resource_table(res); |
| 133 | |
| 134 | while ((char *)p < (char *)end) { |
| 135 | |
| 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); |
| 143 | } |
| 144 | |
| 145 | switch (tag) { |
| 146 | |
| 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; |
| 154 | |
| 155 | case LARGE_TAG_ANSISTR: |
| 156 | /* ignore this for now */ |
| 157 | break; |
| 158 | |
| 159 | case LARGE_TAG_VENDOR: |
| 160 | /* do nothing */ |
| 161 | break; |
| 162 | |
| 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; |
| 170 | |
| 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; |
| 178 | |
| 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; |
| 188 | |
| 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; |
| 198 | |
| 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; |
| 206 | |
| 207 | case SMALL_TAG_VENDOR: |
| 208 | /* do nothing */ |
| 209 | break; |
| 210 | |
| 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; |
| 218 | |
| 219 | case SMALL_TAG_END: |
| 220 | p = p + 2; |
| 221 | return (unsigned char *)p; |
| 222 | break; |
| 223 | |
| 224 | default: /* an unkown tag */ |
| 225 | len_err: |
| 226 | printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | /* continue to the next tag */ |
| 231 | if (p[0] & LARGE_TAG) |
| 232 | p += len + 3; |
| 233 | else |
| 234 | p += len + 1; |
| 235 | } |
| 236 | |
| 237 | printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n"); |
| 238 | |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /* |
| 244 | * Resource Configuration Options |
| 245 | */ |
| 246 | |
| 247 | static void |
| 248 | pnpbios_parse_mem_option(unsigned char *p, int size, struct pnp_option *option) |
| 249 | { |
| 250 | struct pnp_mem * mem; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 251 | mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 261 | } |
| 262 | |
| 263 | static void |
| 264 | pnpbios_parse_mem32_option(unsigned char *p, int size, struct pnp_option *option) |
| 265 | { |
| 266 | struct pnp_mem * mem; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 267 | mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 277 | } |
| 278 | |
| 279 | static void |
| 280 | pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, struct pnp_option *option) |
| 281 | { |
| 282 | struct pnp_mem * mem; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 283 | mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | pnpbios_parse_irq_option(unsigned char *p, int size, struct pnp_option *option) |
| 296 | { |
| 297 | struct pnp_irq * irq; |
| 298 | unsigned long bits; |
| 299 | |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 300 | irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | pnpbios_parse_dma_option(unsigned char *p, int size, struct pnp_option *option) |
| 315 | { |
| 316 | struct pnp_dma * dma; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 317 | dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (!dma) |
| 319 | return; |
| 320 | dma->map = p[1]; |
| 321 | dma->flags = p[2]; |
| 322 | pnp_register_dma_resource(option,dma); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | static void |
| 327 | pnpbios_parse_port_option(unsigned char *p, int size, struct pnp_option *option) |
| 328 | { |
| 329 | struct pnp_port * port; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 330 | port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | pnpbios_parse_fixed_port_option(unsigned char *p, int size, struct pnp_option *option) |
| 344 | { |
| 345 | struct pnp_port * port; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 346 | port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 355 | } |
| 356 | |
| 357 | static unsigned char * |
| 358 | pnpbios_parse_resource_option_data(unsigned char * p, unsigned char * end, struct pnp_dev *dev) |
| 359 | { |
| 360 | unsigned int len, tag; |
| 361 | int priority = 0; |
| 362 | struct pnp_option *option, *option_independent; |
| 363 | |
| 364 | if (!p) |
| 365 | return NULL; |
| 366 | |
| 367 | option_independent = option = pnp_register_independent_option(dev); |
| 368 | if (!option) |
| 369 | return NULL; |
| 370 | |
| 371 | while ((char *)p < (char *)end) { |
| 372 | |
| 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); |
| 380 | } |
| 381 | |
| 382 | switch (tag) { |
| 383 | |
| 384 | case LARGE_TAG_MEM: |
| 385 | if (len != 9) |
| 386 | goto len_err; |
| 387 | pnpbios_parse_mem_option(p, len, option); |
| 388 | break; |
| 389 | |
| 390 | case LARGE_TAG_MEM32: |
| 391 | if (len != 17) |
| 392 | goto len_err; |
| 393 | pnpbios_parse_mem32_option(p, len, option); |
| 394 | break; |
| 395 | |
| 396 | case LARGE_TAG_FIXEDMEM32: |
| 397 | if (len != 9) |
| 398 | goto len_err; |
| 399 | pnpbios_parse_fixed_mem32_option(p, len, option); |
| 400 | break; |
| 401 | |
| 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; |
| 407 | |
| 408 | case SMALL_TAG_DMA: |
| 409 | if (len != 2) |
| 410 | goto len_err; |
| 411 | pnpbios_parse_dma_option(p, len, option); |
| 412 | break; |
| 413 | |
| 414 | case SMALL_TAG_PORT: |
| 415 | if (len != 7) |
| 416 | goto len_err; |
| 417 | pnpbios_parse_port_option(p, len, option); |
| 418 | break; |
| 419 | |
| 420 | case SMALL_TAG_VENDOR: |
| 421 | /* do nothing */ |
| 422 | break; |
| 423 | |
| 424 | case SMALL_TAG_FIXEDPORT: |
| 425 | if (len != 3) |
| 426 | goto len_err; |
| 427 | pnpbios_parse_fixed_port_option(p, len, option); |
| 428 | break; |
| 429 | |
| 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; |
| 440 | |
| 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; |
| 448 | |
| 449 | case SMALL_TAG_END: |
Rene Herman | eb8782e | 2006-03-23 03:00:57 -0800 | [diff] [blame] | 450 | return p + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | default: /* an unkown tag */ |
| 453 | len_err: |
| 454 | printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len); |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | /* continue to the next tag */ |
| 459 | if (p[0] & LARGE_TAG) |
| 460 | p += len + 3; |
| 461 | else |
| 462 | p += len + 1; |
| 463 | } |
| 464 | |
| 465 | printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n"); |
| 466 | |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /* |
| 472 | * Compatible Device IDs |
| 473 | */ |
| 474 | |
| 475 | #define HEX(id,a) hex[((id)>>a) & 15] |
| 476 | #define CHAR(id,a) (0x40 + (((id)>>a) & 31)) |
| 477 | // |
| 478 | |
| 479 | void pnpid32_to_pnpid(u32 id, char *str) |
| 480 | { |
| 481 | const char *hex = "0123456789abcdef"; |
| 482 | |
| 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'; |
| 492 | |
| 493 | return; |
| 494 | } |
| 495 | // |
| 496 | #undef CHAR |
| 497 | #undef HEX |
| 498 | |
| 499 | static unsigned char * |
| 500 | pnpbios_parse_compatible_ids(unsigned char *p, unsigned char *end, struct pnp_dev *dev) |
| 501 | { |
| 502 | int len, tag; |
| 503 | char id[8]; |
| 504 | struct pnp_id *dev_id; |
| 505 | |
| 506 | if (!p) |
| 507 | return NULL; |
| 508 | |
| 509 | while ((char *)p < (char *)end) { |
| 510 | |
| 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); |
| 518 | } |
| 519 | |
| 520 | switch (tag) { |
| 521 | |
| 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; |
| 526 | |
| 527 | case SMALL_TAG_COMPATDEVID: /* compatible ID */ |
| 528 | if (len != 4) |
| 529 | goto len_err; |
Bjorn Helgaas | a2822e7 | 2005-09-06 15:16:51 -0700 | [diff] [blame] | 530 | dev_id = kcalloc(1, sizeof (struct pnp_id), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (!dev_id) |
| 532 | return NULL; |
| 533 | memset(dev_id, 0, sizeof(struct pnp_id)); |
| 534 | pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24,id); |
| 535 | memcpy(&dev_id->id, id, 7); |
| 536 | pnp_add_id(dev_id, dev); |
| 537 | break; |
| 538 | |
| 539 | case SMALL_TAG_END: |
| 540 | p = p + 2; |
| 541 | return (unsigned char *)p; |
| 542 | break; |
| 543 | |
| 544 | default: /* an unkown tag */ |
| 545 | len_err: |
| 546 | printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len); |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | /* continue to the next tag */ |
| 551 | if (p[0] & LARGE_TAG) |
| 552 | p += len + 3; |
| 553 | else |
| 554 | p += len + 1; |
| 555 | } |
| 556 | |
| 557 | printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n"); |
| 558 | |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | /* |
| 564 | * Allocated Resource Encoding |
| 565 | */ |
| 566 | |
| 567 | static void pnpbios_encode_mem(unsigned char *p, struct resource * res) |
| 568 | { |
| 569 | unsigned long base = res->start; |
| 570 | unsigned long len = res->end - res->start + 1; |
| 571 | p[4] = (base >> 8) & 0xff; |
| 572 | p[5] = ((base >> 8) >> 8) & 0xff; |
| 573 | p[6] = (base >> 8) & 0xff; |
| 574 | p[7] = ((base >> 8) >> 8) & 0xff; |
| 575 | p[10] = (len >> 8) & 0xff; |
| 576 | p[11] = ((len >> 8) >> 8) & 0xff; |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | static void pnpbios_encode_mem32(unsigned char *p, struct resource * res) |
| 581 | { |
| 582 | unsigned long base = res->start; |
| 583 | unsigned long len = res->end - res->start + 1; |
| 584 | p[4] = base & 0xff; |
| 585 | p[5] = (base >> 8) & 0xff; |
| 586 | p[6] = (base >> 16) & 0xff; |
| 587 | p[7] = (base >> 24) & 0xff; |
| 588 | p[8] = base & 0xff; |
| 589 | p[9] = (base >> 8) & 0xff; |
| 590 | p[10] = (base >> 16) & 0xff; |
| 591 | p[11] = (base >> 24) & 0xff; |
| 592 | p[16] = len & 0xff; |
| 593 | p[17] = (len >> 8) & 0xff; |
| 594 | p[18] = (len >> 16) & 0xff; |
| 595 | p[19] = (len >> 24) & 0xff; |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource * res) |
| 600 | { unsigned long base = res->start; |
| 601 | unsigned long len = res->end - res->start + 1; |
| 602 | p[4] = base & 0xff; |
| 603 | p[5] = (base >> 8) & 0xff; |
| 604 | p[6] = (base >> 16) & 0xff; |
| 605 | p[7] = (base >> 24) & 0xff; |
| 606 | p[8] = len & 0xff; |
| 607 | p[9] = (len >> 8) & 0xff; |
| 608 | p[10] = (len >> 16) & 0xff; |
| 609 | p[11] = (len >> 24) & 0xff; |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | static void pnpbios_encode_irq(unsigned char *p, struct resource * res) |
| 614 | { |
| 615 | unsigned long map = 0; |
| 616 | map = 1 << res->start; |
| 617 | p[1] = map & 0xff; |
| 618 | p[2] = (map >> 8) & 0xff; |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | static void pnpbios_encode_dma(unsigned char *p, struct resource * res) |
| 623 | { |
| 624 | unsigned long map = 0; |
| 625 | map = 1 << res->start; |
| 626 | p[1] = map & 0xff; |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | static void pnpbios_encode_port(unsigned char *p, struct resource * res) |
| 631 | { |
| 632 | unsigned long base = res->start; |
| 633 | unsigned long len = res->end - res->start + 1; |
| 634 | p[2] = base & 0xff; |
| 635 | p[3] = (base >> 8) & 0xff; |
| 636 | p[4] = base & 0xff; |
| 637 | p[5] = (base >> 8) & 0xff; |
| 638 | p[7] = len & 0xff; |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | static void pnpbios_encode_fixed_port(unsigned char *p, struct resource * res) |
| 643 | { |
| 644 | unsigned long base = res->start; |
| 645 | unsigned long len = res->end - res->start + 1; |
| 646 | p[1] = base & 0xff; |
| 647 | p[2] = (base >> 8) & 0xff; |
| 648 | p[3] = len & 0xff; |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | static unsigned char * |
| 653 | pnpbios_encode_allocated_resource_data(unsigned char * p, unsigned char * end, struct pnp_resource_table * res) |
| 654 | { |
| 655 | unsigned int len, tag; |
| 656 | int port = 0, irq = 0, dma = 0, mem = 0; |
| 657 | |
| 658 | if (!p) |
| 659 | return NULL; |
| 660 | |
| 661 | while ((char *)p < (char *)end) { |
| 662 | |
| 663 | /* determine the type of tag */ |
| 664 | if (p[0] & LARGE_TAG) { /* large tag */ |
| 665 | len = (p[2] << 8) | p[1]; |
| 666 | tag = p[0]; |
| 667 | } else { /* small tag */ |
| 668 | len = p[0] & 0x07; |
| 669 | tag = ((p[0]>>3) & 0x0f); |
| 670 | } |
| 671 | |
| 672 | switch (tag) { |
| 673 | |
| 674 | case LARGE_TAG_MEM: |
| 675 | if (len != 9) |
| 676 | goto len_err; |
| 677 | pnpbios_encode_mem(p, &res->mem_resource[mem]); |
| 678 | mem++; |
| 679 | break; |
| 680 | |
| 681 | case LARGE_TAG_MEM32: |
| 682 | if (len != 17) |
| 683 | goto len_err; |
| 684 | pnpbios_encode_mem32(p, &res->mem_resource[mem]); |
| 685 | mem++; |
| 686 | break; |
| 687 | |
| 688 | case LARGE_TAG_FIXEDMEM32: |
| 689 | if (len != 9) |
| 690 | goto len_err; |
| 691 | pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]); |
| 692 | mem++; |
| 693 | break; |
| 694 | |
| 695 | case SMALL_TAG_IRQ: |
| 696 | if (len < 2 || len > 3) |
| 697 | goto len_err; |
| 698 | pnpbios_encode_irq(p, &res->irq_resource[irq]); |
| 699 | irq++; |
| 700 | break; |
| 701 | |
| 702 | case SMALL_TAG_DMA: |
| 703 | if (len != 2) |
| 704 | goto len_err; |
| 705 | pnpbios_encode_dma(p, &res->dma_resource[dma]); |
| 706 | dma++; |
| 707 | break; |
| 708 | |
| 709 | case SMALL_TAG_PORT: |
| 710 | if (len != 7) |
| 711 | goto len_err; |
| 712 | pnpbios_encode_port(p, &res->port_resource[port]); |
| 713 | port++; |
| 714 | break; |
| 715 | |
| 716 | case SMALL_TAG_VENDOR: |
| 717 | /* do nothing */ |
| 718 | break; |
| 719 | |
| 720 | case SMALL_TAG_FIXEDPORT: |
| 721 | if (len != 3) |
| 722 | goto len_err; |
| 723 | pnpbios_encode_fixed_port(p, &res->port_resource[port]); |
| 724 | port++; |
| 725 | break; |
| 726 | |
| 727 | case SMALL_TAG_END: |
| 728 | p = p + 2; |
| 729 | return (unsigned char *)p; |
| 730 | break; |
| 731 | |
| 732 | default: /* an unkown tag */ |
| 733 | len_err: |
| 734 | printk(KERN_ERR "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", tag, len); |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | /* continue to the next tag */ |
| 739 | if (p[0] & LARGE_TAG) |
| 740 | p += len + 3; |
| 741 | else |
| 742 | p += len + 1; |
| 743 | } |
| 744 | |
| 745 | printk(KERN_ERR "PnPBIOS: Resource structure does not contain an end tag.\n"); |
| 746 | |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | /* |
| 752 | * Core Parsing Functions |
| 753 | */ |
| 754 | |
| 755 | int |
| 756 | pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node * node) |
| 757 | { |
| 758 | unsigned char * p = (char *)node->data; |
| 759 | unsigned char * end = (char *)(node->data + node->size); |
| 760 | p = pnpbios_parse_allocated_resource_data(p,end,&dev->res); |
| 761 | if (!p) |
| 762 | return -EIO; |
| 763 | p = pnpbios_parse_resource_option_data(p,end,dev); |
| 764 | if (!p) |
| 765 | return -EIO; |
| 766 | p = pnpbios_parse_compatible_ids(p,end,dev); |
| 767 | if (!p) |
| 768 | return -EIO; |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | int |
| 773 | pnpbios_read_resources_from_node(struct pnp_resource_table *res, |
| 774 | struct pnp_bios_node * node) |
| 775 | { |
| 776 | unsigned char * p = (char *)node->data; |
| 777 | unsigned char * end = (char *)(node->data + node->size); |
| 778 | p = pnpbios_parse_allocated_resource_data(p,end,res); |
| 779 | if (!p) |
| 780 | return -EIO; |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | int |
| 785 | pnpbios_write_resources_to_node(struct pnp_resource_table *res, |
| 786 | struct pnp_bios_node * node) |
| 787 | { |
| 788 | unsigned char * p = (char *)node->data; |
| 789 | unsigned char * end = (char *)(node->data + node->size); |
| 790 | p = pnpbios_encode_allocated_resource_data(p,end,res); |
| 791 | if (!p) |
| 792 | return -EIO; |
| 793 | return 0; |
| 794 | } |