Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rsparser.c - parses and encodes pnpbios resource data streams |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | */ |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/ctype.h> |
| 6 | #include <linux/pnp.h> |
| 7 | #include <linux/pnpbios.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 8 | #include <linux/string.h> |
| 9 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | #ifdef CONFIG_PCI |
| 12 | #include <linux/pci.h> |
| 13 | #else |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 14 | inline void pcibios_penalize_isa_irq(int irq, int active) |
| 15 | { |
| 16 | } |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 17 | #endif /* CONFIG_PCI */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include "pnpbios.h" |
| 20 | |
| 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 |
| 40 | |
| 41 | /* |
| 42 | * Resource Data Stream Format: |
| 43 | * |
| 44 | * Allocated Resources (required) |
| 45 | * end tag -> |
| 46 | * Resource Configuration Options (optional) |
| 47 | * end tag -> |
| 48 | * Compitable Device IDs (optional) |
| 49 | * final end tag -> |
| 50 | */ |
| 51 | |
| 52 | /* |
| 53 | * Allocated Resources |
| 54 | */ |
| 55 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 56 | static void pnpbios_parse_allocated_irqresource(struct pnp_resource_table *res, |
| 57 | int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | int i = 0; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 60 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 61 | while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) |
| 62 | && i < PNP_MAX_IRQ) |
| 63 | i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (i < PNP_MAX_IRQ) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 65 | res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (irq == -1) { |
| 67 | res->irq_resource[i].flags |= IORESOURCE_DISABLED; |
| 68 | return; |
| 69 | } |
| 70 | res->irq_resource[i].start = |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 71 | res->irq_resource[i].end = (unsigned long)irq; |
David Shaohua Li | c9c3e45 | 2005-04-01 00:07:31 -0500 | [diff] [blame] | 72 | pcibios_penalize_isa_irq(irq, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 76 | static void pnpbios_parse_allocated_dmaresource(struct pnp_resource_table *res, |
| 77 | int dma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | int i = 0; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 80 | |
Adrian Bunk | 6e3e98d | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 81 | while (i < PNP_MAX_DMA && |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 82 | !(res->dma_resource[i].flags & IORESOURCE_UNSET)) |
Adrian Bunk | 6e3e98d | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 83 | i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (i < PNP_MAX_DMA) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 85 | res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (dma == -1) { |
| 87 | res->dma_resource[i].flags |= IORESOURCE_DISABLED; |
| 88 | return; |
| 89 | } |
| 90 | res->dma_resource[i].start = |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 91 | res->dma_resource[i].end = (unsigned long)dma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 95 | static void pnpbios_parse_allocated_ioresource(struct pnp_resource_table *res, |
| 96 | int io, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
| 98 | int i = 0; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 99 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 100 | while (!(res->port_resource[i].flags & IORESOURCE_UNSET) |
| 101 | && i < PNP_MAX_PORT) |
| 102 | i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (i < PNP_MAX_PORT) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 104 | res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag |
| 105 | if (len <= 0 || (io + len - 1) >= 0x10003) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | res->port_resource[i].flags |= IORESOURCE_DISABLED; |
| 107 | return; |
| 108 | } |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 109 | res->port_resource[i].start = (unsigned long)io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | res->port_resource[i].end = (unsigned long)(io + len - 1); |
| 111 | } |
| 112 | } |
| 113 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 114 | static void pnpbios_parse_allocated_memresource(struct pnp_resource_table *res, |
| 115 | int mem, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | int i = 0; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 118 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 119 | while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) |
| 120 | && i < PNP_MAX_MEM) |
| 121 | i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (i < PNP_MAX_MEM) { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 123 | res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (len <= 0) { |
| 125 | res->mem_resource[i].flags |= IORESOURCE_DISABLED; |
| 126 | return; |
| 127 | } |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 128 | res->mem_resource[i].start = (unsigned long)mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | res->mem_resource[i].end = (unsigned long)(mem + len - 1); |
| 130 | } |
| 131 | } |
| 132 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 133 | static unsigned char *pnpbios_parse_allocated_resource_data(unsigned char *p, |
| 134 | unsigned char *end, |
| 135 | struct |
| 136 | pnp_resource_table |
| 137 | *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | unsigned int len, tag; |
| 140 | int io, size, mask, i; |
| 141 | |
| 142 | if (!p) |
| 143 | return NULL; |
| 144 | |
| 145 | /* Blank the resource table values */ |
| 146 | pnp_init_resource_table(res); |
| 147 | |
| 148 | while ((char *)p < (char *)end) { |
| 149 | |
| 150 | /* determine the type of tag */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 151 | if (p[0] & LARGE_TAG) { /* large tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | len = (p[2] << 8) | p[1]; |
| 153 | tag = p[0]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 154 | } else { /* small tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | len = p[0] & 0x07; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 156 | tag = ((p[0] >> 3) & 0x0f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | switch (tag) { |
| 160 | |
| 161 | case LARGE_TAG_MEM: |
| 162 | if (len != 9) |
| 163 | goto len_err; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 164 | io = *(short *)&p[4]; |
| 165 | size = *(short *)&p[10]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | pnpbios_parse_allocated_memresource(res, io, size); |
| 167 | break; |
| 168 | |
| 169 | case LARGE_TAG_ANSISTR: |
| 170 | /* ignore this for now */ |
| 171 | break; |
| 172 | |
| 173 | case LARGE_TAG_VENDOR: |
| 174 | /* do nothing */ |
| 175 | break; |
| 176 | |
| 177 | case LARGE_TAG_MEM32: |
| 178 | if (len != 17) |
| 179 | goto len_err; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 180 | io = *(int *)&p[4]; |
| 181 | size = *(int *)&p[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | pnpbios_parse_allocated_memresource(res, io, size); |
| 183 | break; |
| 184 | |
| 185 | case LARGE_TAG_FIXEDMEM32: |
| 186 | if (len != 9) |
| 187 | goto len_err; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 188 | io = *(int *)&p[4]; |
| 189 | size = *(int *)&p[8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | pnpbios_parse_allocated_memresource(res, io, size); |
| 191 | break; |
| 192 | |
| 193 | case SMALL_TAG_IRQ: |
| 194 | if (len < 2 || len > 3) |
| 195 | goto len_err; |
| 196 | io = -1; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 197 | mask = p[1] + p[2] * 256; |
| 198 | for (i = 0; i < 16; i++, mask = mask >> 1) |
| 199 | if (mask & 0x01) |
| 200 | io = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | pnpbios_parse_allocated_irqresource(res, io); |
| 202 | break; |
| 203 | |
| 204 | case SMALL_TAG_DMA: |
| 205 | if (len != 2) |
| 206 | goto len_err; |
| 207 | io = -1; |
| 208 | mask = p[1]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 209 | for (i = 0; i < 8; i++, mask = mask >> 1) |
| 210 | if (mask & 0x01) |
| 211 | io = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | pnpbios_parse_allocated_dmaresource(res, io); |
| 213 | break; |
| 214 | |
| 215 | case SMALL_TAG_PORT: |
| 216 | if (len != 7) |
| 217 | goto len_err; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 218 | io = p[2] + p[3] * 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | size = p[7]; |
| 220 | pnpbios_parse_allocated_ioresource(res, io, size); |
| 221 | break; |
| 222 | |
| 223 | case SMALL_TAG_VENDOR: |
| 224 | /* do nothing */ |
| 225 | break; |
| 226 | |
| 227 | case SMALL_TAG_FIXEDPORT: |
| 228 | if (len != 3) |
| 229 | goto len_err; |
| 230 | io = p[1] + p[2] * 256; |
| 231 | size = p[3]; |
| 232 | pnpbios_parse_allocated_ioresource(res, io, size); |
| 233 | break; |
| 234 | |
| 235 | case SMALL_TAG_END: |
| 236 | p = p + 2; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 237 | return (unsigned char *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | break; |
| 239 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 240 | default: /* an unkown tag */ |
Bjorn Helgaas | 1e0aa9a | 2007-08-15 10:32:08 -0600 | [diff] [blame] | 241 | len_err: |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 242 | printk(KERN_ERR |
| 243 | "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", |
| 244 | tag, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | |
| 248 | /* continue to the next tag */ |
| 249 | if (p[0] & LARGE_TAG) |
| 250 | p += len + 3; |
| 251 | else |
| 252 | p += len + 1; |
| 253 | } |
| 254 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 255 | printk(KERN_ERR |
| 256 | "PnPBIOS: Resource structure does not contain an end tag.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | /* |
| 262 | * Resource Configuration Options |
| 263 | */ |
| 264 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 265 | static void pnpbios_parse_mem_option(unsigned char *p, int size, |
| 266 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 268 | struct pnp_mem *mem; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 269 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 270 | mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (!mem) |
| 272 | return; |
| 273 | mem->min = ((p[5] << 8) | p[4]) << 8; |
| 274 | mem->max = ((p[7] << 8) | p[6]) << 8; |
| 275 | mem->align = (p[9] << 8) | p[8]; |
| 276 | mem->size = ((p[11] << 8) | p[10]) << 8; |
| 277 | mem->flags = p[3]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 278 | pnp_register_mem_resource(option, mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 281 | static void pnpbios_parse_mem32_option(unsigned char *p, int size, |
| 282 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 284 | struct pnp_mem *mem; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 285 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 286 | mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (!mem) |
| 288 | return; |
| 289 | mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4]; |
| 290 | mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8]; |
| 291 | mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12]; |
| 292 | mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16]; |
| 293 | mem->flags = p[3]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 294 | pnp_register_mem_resource(option, mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 297 | static void pnpbios_parse_fixed_mem32_option(unsigned char *p, int size, |
| 298 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 300 | struct pnp_mem *mem; |
Bjorn Helgaas | 1e0aa9a | 2007-08-15 10:32:08 -0600 | [diff] [blame] | 301 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 302 | mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (!mem) |
| 304 | return; |
| 305 | mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4]; |
| 306 | mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8]; |
| 307 | mem->align = 0; |
| 308 | mem->flags = p[3]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 309 | pnp_register_mem_resource(option, mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 312 | static void pnpbios_parse_irq_option(unsigned char *p, int size, |
| 313 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 315 | struct pnp_irq *irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | unsigned long bits; |
| 317 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 318 | irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (!irq) |
| 320 | return; |
| 321 | bits = (p[2] << 8) | p[1]; |
| 322 | bitmap_copy(irq->map, &bits, 16); |
| 323 | if (size > 2) |
| 324 | irq->flags = p[3]; |
| 325 | else |
| 326 | irq->flags = IORESOURCE_IRQ_HIGHEDGE; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 327 | pnp_register_irq_resource(option, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 330 | static void pnpbios_parse_dma_option(unsigned char *p, int size, |
| 331 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 333 | struct pnp_dma *dma; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 334 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 335 | dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (!dma) |
| 337 | return; |
| 338 | dma->map = p[1]; |
| 339 | dma->flags = p[2]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 340 | pnp_register_dma_resource(option, dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 343 | static void pnpbios_parse_port_option(unsigned char *p, int size, |
| 344 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 346 | struct pnp_port *port; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 347 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 348 | port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (!port) |
| 350 | return; |
| 351 | port->min = (p[3] << 8) | p[2]; |
| 352 | port->max = (p[5] << 8) | p[4]; |
| 353 | port->align = p[6]; |
| 354 | port->size = p[7]; |
| 355 | port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 356 | pnp_register_port_resource(option, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 359 | static void pnpbios_parse_fixed_port_option(unsigned char *p, int size, |
| 360 | struct pnp_option *option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 362 | struct pnp_port *port; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 363 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 364 | port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (!port) |
| 366 | return; |
| 367 | port->min = port->max = (p[2] << 8) | p[1]; |
| 368 | port->size = p[3]; |
| 369 | port->align = 0; |
| 370 | port->flags = PNP_PORT_FLAG_FIXED; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 371 | pnp_register_port_resource(option, port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 374 | static unsigned char *pnpbios_parse_resource_option_data(unsigned char *p, |
| 375 | unsigned char *end, |
| 376 | struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
| 378 | unsigned int len, tag; |
| 379 | int priority = 0; |
| 380 | struct pnp_option *option, *option_independent; |
| 381 | |
| 382 | if (!p) |
| 383 | return NULL; |
| 384 | |
| 385 | option_independent = option = pnp_register_independent_option(dev); |
| 386 | if (!option) |
| 387 | return NULL; |
| 388 | |
| 389 | while ((char *)p < (char *)end) { |
| 390 | |
| 391 | /* determine the type of tag */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 392 | if (p[0] & LARGE_TAG) { /* large tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | len = (p[2] << 8) | p[1]; |
| 394 | tag = p[0]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 395 | } else { /* small tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | len = p[0] & 0x07; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 397 | tag = ((p[0] >> 3) & 0x0f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | switch (tag) { |
| 401 | |
| 402 | case LARGE_TAG_MEM: |
| 403 | if (len != 9) |
| 404 | goto len_err; |
| 405 | pnpbios_parse_mem_option(p, len, option); |
| 406 | break; |
| 407 | |
| 408 | case LARGE_TAG_MEM32: |
| 409 | if (len != 17) |
| 410 | goto len_err; |
| 411 | pnpbios_parse_mem32_option(p, len, option); |
| 412 | break; |
| 413 | |
| 414 | case LARGE_TAG_FIXEDMEM32: |
| 415 | if (len != 9) |
| 416 | goto len_err; |
| 417 | pnpbios_parse_fixed_mem32_option(p, len, option); |
| 418 | break; |
| 419 | |
| 420 | case SMALL_TAG_IRQ: |
| 421 | if (len < 2 || len > 3) |
| 422 | goto len_err; |
| 423 | pnpbios_parse_irq_option(p, len, option); |
| 424 | break; |
| 425 | |
| 426 | case SMALL_TAG_DMA: |
| 427 | if (len != 2) |
| 428 | goto len_err; |
| 429 | pnpbios_parse_dma_option(p, len, option); |
| 430 | break; |
| 431 | |
| 432 | case SMALL_TAG_PORT: |
| 433 | if (len != 7) |
| 434 | goto len_err; |
| 435 | pnpbios_parse_port_option(p, len, option); |
| 436 | break; |
| 437 | |
| 438 | case SMALL_TAG_VENDOR: |
| 439 | /* do nothing */ |
| 440 | break; |
| 441 | |
| 442 | case SMALL_TAG_FIXEDPORT: |
| 443 | if (len != 3) |
| 444 | goto len_err; |
| 445 | pnpbios_parse_fixed_port_option(p, len, option); |
| 446 | break; |
| 447 | |
| 448 | case SMALL_TAG_STARTDEP: |
| 449 | if (len > 1) |
| 450 | goto len_err; |
| 451 | priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE; |
| 452 | if (len > 0) |
| 453 | priority = 0x100 | p[1]; |
| 454 | option = pnp_register_dependent_option(dev, priority); |
| 455 | if (!option) |
| 456 | return NULL; |
| 457 | break; |
| 458 | |
| 459 | case SMALL_TAG_ENDDEP: |
| 460 | if (len != 0) |
| 461 | goto len_err; |
| 462 | if (option_independent == option) |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 463 | printk(KERN_WARNING |
| 464 | "PnPBIOS: Missing SMALL_TAG_STARTDEP tag\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | option = option_independent; |
| 466 | break; |
| 467 | |
| 468 | case SMALL_TAG_END: |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 469 | return p + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 471 | default: /* an unkown tag */ |
Bjorn Helgaas | 1e0aa9a | 2007-08-15 10:32:08 -0600 | [diff] [blame] | 472 | len_err: |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 473 | printk(KERN_ERR |
| 474 | "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", |
| 475 | tag, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | |
| 479 | /* continue to the next tag */ |
| 480 | if (p[0] & LARGE_TAG) |
| 481 | p += len + 3; |
| 482 | else |
| 483 | p += len + 1; |
| 484 | } |
| 485 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 486 | printk(KERN_ERR |
| 487 | "PnPBIOS: Resource structure does not contain an end tag.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /* |
| 493 | * Compatible Device IDs |
| 494 | */ |
| 495 | |
| 496 | #define HEX(id,a) hex[((id)>>a) & 15] |
| 497 | #define CHAR(id,a) (0x40 + (((id)>>a) & 31)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
| 499 | void pnpid32_to_pnpid(u32 id, char *str) |
| 500 | { |
| 501 | const char *hex = "0123456789abcdef"; |
| 502 | |
| 503 | id = be32_to_cpu(id); |
| 504 | str[0] = CHAR(id, 26); |
| 505 | str[1] = CHAR(id, 21); |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 506 | str[2] = CHAR(id, 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | str[3] = HEX(id, 12); |
| 508 | str[4] = HEX(id, 8); |
| 509 | str[5] = HEX(id, 4); |
| 510 | str[6] = HEX(id, 0); |
| 511 | str[7] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | #undef CHAR |
| 515 | #undef HEX |
| 516 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 517 | static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p, |
| 518 | unsigned char *end, |
| 519 | struct pnp_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
| 521 | int len, tag; |
| 522 | char id[8]; |
| 523 | struct pnp_id *dev_id; |
| 524 | |
| 525 | if (!p) |
| 526 | return NULL; |
| 527 | |
| 528 | while ((char *)p < (char *)end) { |
| 529 | |
| 530 | /* determine the type of tag */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 531 | if (p[0] & LARGE_TAG) { /* large tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | len = (p[2] << 8) | p[1]; |
| 533 | tag = p[0]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 534 | } else { /* small tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | len = p[0] & 0x07; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 536 | tag = ((p[0] >> 3) & 0x0f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | switch (tag) { |
| 540 | |
| 541 | case LARGE_TAG_ANSISTR: |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 542 | strncpy(dev->name, p + 3, |
| 543 | len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len); |
| 544 | dev->name[len >= |
| 545 | PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | break; |
| 547 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 548 | case SMALL_TAG_COMPATDEVID: /* compatible ID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | if (len != 4) |
| 550 | goto len_err; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 551 | dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (!dev_id) |
| 553 | return NULL; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 554 | pnpid32_to_pnpid(p[1] | p[2] << 8 | p[3] << 16 | p[4] << |
| 555 | 24, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | memcpy(&dev_id->id, id, 7); |
| 557 | pnp_add_id(dev_id, dev); |
| 558 | break; |
| 559 | |
| 560 | case SMALL_TAG_END: |
| 561 | p = p + 2; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 562 | return (unsigned char *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | break; |
| 564 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 565 | default: /* an unkown tag */ |
Bjorn Helgaas | 1e0aa9a | 2007-08-15 10:32:08 -0600 | [diff] [blame] | 566 | len_err: |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 567 | printk(KERN_ERR |
| 568 | "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", |
| 569 | tag, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | break; |
| 571 | } |
| 572 | |
| 573 | /* continue to the next tag */ |
| 574 | if (p[0] & LARGE_TAG) |
| 575 | p += len + 3; |
| 576 | else |
| 577 | p += len + 1; |
| 578 | } |
| 579 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 580 | printk(KERN_ERR |
| 581 | "PnPBIOS: Resource structure does not contain an end tag.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | return NULL; |
| 584 | } |
| 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | /* |
| 587 | * Allocated Resource Encoding |
| 588 | */ |
| 589 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 590 | static void pnpbios_encode_mem(unsigned char *p, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
| 592 | unsigned long base = res->start; |
| 593 | unsigned long len = res->end - res->start + 1; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | p[4] = (base >> 8) & 0xff; |
| 596 | p[5] = ((base >> 8) >> 8) & 0xff; |
| 597 | p[6] = (base >> 8) & 0xff; |
| 598 | p[7] = ((base >> 8) >> 8) & 0xff; |
| 599 | p[10] = (len >> 8) & 0xff; |
| 600 | p[11] = ((len >> 8) >> 8) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 603 | static void pnpbios_encode_mem32(unsigned char *p, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
| 605 | unsigned long base = res->start; |
| 606 | unsigned long len = res->end - res->start + 1; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | p[4] = base & 0xff; |
| 609 | p[5] = (base >> 8) & 0xff; |
| 610 | p[6] = (base >> 16) & 0xff; |
| 611 | p[7] = (base >> 24) & 0xff; |
| 612 | p[8] = base & 0xff; |
| 613 | p[9] = (base >> 8) & 0xff; |
| 614 | p[10] = (base >> 16) & 0xff; |
| 615 | p[11] = (base >> 24) & 0xff; |
| 616 | p[16] = len & 0xff; |
| 617 | p[17] = (len >> 8) & 0xff; |
| 618 | p[18] = (len >> 16) & 0xff; |
| 619 | p[19] = (len >> 24) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 622 | static void pnpbios_encode_fixed_mem32(unsigned char *p, struct resource *res) |
| 623 | { |
| 624 | unsigned long base = res->start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | unsigned long len = res->end - res->start + 1; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | p[4] = base & 0xff; |
| 628 | p[5] = (base >> 8) & 0xff; |
| 629 | p[6] = (base >> 16) & 0xff; |
| 630 | p[7] = (base >> 24) & 0xff; |
| 631 | p[8] = len & 0xff; |
| 632 | p[9] = (len >> 8) & 0xff; |
| 633 | p[10] = (len >> 16) & 0xff; |
| 634 | p[11] = (len >> 24) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 637 | static void pnpbios_encode_irq(unsigned char *p, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | { |
| 639 | unsigned long map = 0; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | map = 1 << res->start; |
| 642 | p[1] = map & 0xff; |
| 643 | p[2] = (map >> 8) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 646 | static void pnpbios_encode_dma(unsigned char *p, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
| 648 | unsigned long map = 0; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | map = 1 << res->start; |
| 651 | p[1] = map & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 654 | static void pnpbios_encode_port(unsigned char *p, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
| 656 | unsigned long base = res->start; |
| 657 | unsigned long len = res->end - res->start + 1; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | p[2] = base & 0xff; |
| 660 | p[3] = (base >> 8) & 0xff; |
| 661 | p[4] = base & 0xff; |
| 662 | p[5] = (base >> 8) & 0xff; |
| 663 | p[7] = len & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 666 | static void pnpbios_encode_fixed_port(unsigned char *p, struct resource *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | unsigned long base = res->start; |
| 669 | unsigned long len = res->end - res->start + 1; |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | p[1] = base & 0xff; |
| 672 | p[2] = (base >> 8) & 0xff; |
| 673 | p[3] = len & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 676 | static unsigned char *pnpbios_encode_allocated_resource_data(unsigned char *p, |
| 677 | unsigned char *end, |
| 678 | struct |
| 679 | pnp_resource_table |
| 680 | *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
| 682 | unsigned int len, tag; |
| 683 | int port = 0, irq = 0, dma = 0, mem = 0; |
| 684 | |
| 685 | if (!p) |
| 686 | return NULL; |
| 687 | |
| 688 | while ((char *)p < (char *)end) { |
| 689 | |
| 690 | /* determine the type of tag */ |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 691 | if (p[0] & LARGE_TAG) { /* large tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | len = (p[2] << 8) | p[1]; |
| 693 | tag = p[0]; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 694 | } else { /* small tag */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | len = p[0] & 0x07; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 696 | tag = ((p[0] >> 3) & 0x0f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | switch (tag) { |
| 700 | |
| 701 | case LARGE_TAG_MEM: |
| 702 | if (len != 9) |
| 703 | goto len_err; |
| 704 | pnpbios_encode_mem(p, &res->mem_resource[mem]); |
| 705 | mem++; |
| 706 | break; |
| 707 | |
| 708 | case LARGE_TAG_MEM32: |
| 709 | if (len != 17) |
| 710 | goto len_err; |
| 711 | pnpbios_encode_mem32(p, &res->mem_resource[mem]); |
| 712 | mem++; |
| 713 | break; |
| 714 | |
| 715 | case LARGE_TAG_FIXEDMEM32: |
| 716 | if (len != 9) |
| 717 | goto len_err; |
| 718 | pnpbios_encode_fixed_mem32(p, &res->mem_resource[mem]); |
| 719 | mem++; |
| 720 | break; |
| 721 | |
| 722 | case SMALL_TAG_IRQ: |
| 723 | if (len < 2 || len > 3) |
| 724 | goto len_err; |
| 725 | pnpbios_encode_irq(p, &res->irq_resource[irq]); |
| 726 | irq++; |
| 727 | break; |
| 728 | |
| 729 | case SMALL_TAG_DMA: |
| 730 | if (len != 2) |
| 731 | goto len_err; |
| 732 | pnpbios_encode_dma(p, &res->dma_resource[dma]); |
| 733 | dma++; |
| 734 | break; |
| 735 | |
| 736 | case SMALL_TAG_PORT: |
| 737 | if (len != 7) |
| 738 | goto len_err; |
| 739 | pnpbios_encode_port(p, &res->port_resource[port]); |
| 740 | port++; |
| 741 | break; |
| 742 | |
| 743 | case SMALL_TAG_VENDOR: |
| 744 | /* do nothing */ |
| 745 | break; |
| 746 | |
| 747 | case SMALL_TAG_FIXEDPORT: |
| 748 | if (len != 3) |
| 749 | goto len_err; |
| 750 | pnpbios_encode_fixed_port(p, &res->port_resource[port]); |
| 751 | port++; |
| 752 | break; |
| 753 | |
| 754 | case SMALL_TAG_END: |
| 755 | p = p + 2; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 756 | return (unsigned char *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | break; |
| 758 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 759 | default: /* an unkown tag */ |
Bjorn Helgaas | 1e0aa9a | 2007-08-15 10:32:08 -0600 | [diff] [blame] | 760 | len_err: |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 761 | printk(KERN_ERR |
| 762 | "PnPBIOS: Unknown tag '0x%x', length '%d'.\n", |
| 763 | tag, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | break; |
| 765 | } |
| 766 | |
| 767 | /* continue to the next tag */ |
| 768 | if (p[0] & LARGE_TAG) |
| 769 | p += len + 3; |
| 770 | else |
| 771 | p += len + 1; |
| 772 | } |
| 773 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 774 | printk(KERN_ERR |
| 775 | "PnPBIOS: Resource structure does not contain an end tag.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | return NULL; |
| 778 | } |
| 779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* |
| 781 | * Core Parsing Functions |
| 782 | */ |
| 783 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 784 | int pnpbios_parse_data_stream(struct pnp_dev *dev, struct pnp_bios_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 786 | unsigned char *p = (char *)node->data; |
| 787 | unsigned char *end = (char *)(node->data + node->size); |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 788 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 789 | p = pnpbios_parse_allocated_resource_data(p, end, &dev->res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | if (!p) |
| 791 | return -EIO; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 792 | p = pnpbios_parse_resource_option_data(p, end, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | if (!p) |
| 794 | return -EIO; |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 795 | p = pnpbios_parse_compatible_ids(p, end, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | if (!p) |
| 797 | return -EIO; |
| 798 | return 0; |
| 799 | } |
| 800 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 801 | int pnpbios_read_resources_from_node(struct pnp_resource_table *res, |
| 802 | struct pnp_bios_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 804 | unsigned char *p = (char *)node->data; |
| 805 | unsigned char *end = (char *)(node->data + node->size); |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 806 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 807 | p = pnpbios_parse_allocated_resource_data(p, end, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if (!p) |
| 809 | return -EIO; |
| 810 | return 0; |
| 811 | } |
| 812 | |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 813 | int pnpbios_write_resources_to_node(struct pnp_resource_table *res, |
| 814 | struct pnp_bios_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 816 | unsigned char *p = (char *)node->data; |
| 817 | unsigned char *end = (char *)(node->data + node->size); |
Bjorn Helgaas | 07d4e9a | 2007-07-26 10:41:21 -0700 | [diff] [blame] | 818 | |
Bjorn Helgaas | 9dd7846 | 2007-07-26 10:41:20 -0700 | [diff] [blame] | 819 | p = pnpbios_encode_allocated_resource_data(p, end, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (!p) |
| 821 | return -EIO; |
| 822 | return 0; |
| 823 | } |