blob: 845730c57edcec60a63dd5a23eed0198c9e3aee0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * rsparser.c - parses and encodes pnpbios resource data streams
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 */
4
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/ctype.h>
6#include <linux/pnp.h>
7#include <linux/pnpbios.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -08008#include <linux/string.h>
9#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#ifdef CONFIG_PCI
12#include <linux/pci.h>
13#else
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070014inline void pcibios_penalize_isa_irq(int irq, int active)
15{
16}
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070017#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Bjorn Helgaas772defc2008-04-28 16:33:52 -060019#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "pnpbios.h"
21
22/* standard resource tags */
23#define SMALL_TAG_PNPVERNO 0x01
24#define SMALL_TAG_LOGDEVID 0x02
25#define SMALL_TAG_COMPATDEVID 0x03
26#define SMALL_TAG_IRQ 0x04
27#define SMALL_TAG_DMA 0x05
28#define SMALL_TAG_STARTDEP 0x06
29#define SMALL_TAG_ENDDEP 0x07
30#define SMALL_TAG_PORT 0x08
31#define SMALL_TAG_FIXEDPORT 0x09
32#define SMALL_TAG_VENDOR 0x0e
33#define SMALL_TAG_END 0x0f
34#define LARGE_TAG 0x80
35#define LARGE_TAG_MEM 0x81
36#define LARGE_TAG_ANSISTR 0x82
37#define LARGE_TAG_UNICODESTR 0x83
38#define LARGE_TAG_VENDOR 0x84
39#define LARGE_TAG_MEM32 0x85
40#define LARGE_TAG_FIXEDMEM32 0x86
41
42/*
43 * Resource Data Stream Format:
44 *
45 * Allocated Resources (required)
46 * end tag ->
47 * Resource Configuration Options (optional)
48 * end tag ->
49 * Compitable Device IDs (optional)
50 * final end tag ->
51 */
52
53/*
54 * Allocated Resources
55 */
56
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060057static void pnpbios_parse_allocated_dmaresource(struct pnp_dev *dev, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060059 struct resource *res;
60 int i;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070061
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060062 for (i = 0; i < PNP_MAX_DMA; i++) {
63 res = pnp_get_resource(dev, IORESOURCE_DMA, i);
64 if (!pnp_resource_valid(res))
65 break;
66 }
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (i < PNP_MAX_DMA) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060069 res->flags = IORESOURCE_DMA; // Also clears _UNSET flag
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (dma == -1) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060071 res->flags |= IORESOURCE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return;
73 }
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060074 res->start = res->end = (unsigned long)dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76}
77
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060078static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070079 int io, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060081 struct resource *res;
82 int i;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070083
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060084 for (i = 0; i < PNP_MAX_PORT; i++) {
85 res = pnp_get_resource(dev, IORESOURCE_IO, i);
86 if (!pnp_resource_valid(res))
87 break;
88 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (i < PNP_MAX_PORT) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060091 res->flags = IORESOURCE_IO; // Also clears _UNSET flag
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070092 if (len <= 0 || (io + len - 1) >= 0x10003) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060093 res->flags |= IORESOURCE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return;
95 }
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060096 res->start = (unsigned long)io;
97 res->end = (unsigned long)(io + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99}
100
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600101static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700102 int mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -0600104 struct resource *res;
105 int i;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700106
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -0600107 for (i = 0; i < PNP_MAX_MEM; i++) {
108 res = pnp_get_resource(dev, IORESOURCE_MEM, i);
109 if (!pnp_resource_valid(res))
110 break;
111 }
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (i < PNP_MAX_MEM) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -0600114 res->flags = IORESOURCE_MEM; // Also clears _UNSET flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (len <= 0) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -0600116 res->flags |= IORESOURCE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return;
118 }
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -0600119 res->start = (unsigned long)mem;
120 res->end = (unsigned long)(mem + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122}
123
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600124static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
125 unsigned char *p,
126 unsigned char *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 unsigned int len, tag;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600129 int io, size, mask, i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (!p)
132 return NULL;
133
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600134 dev_dbg(&dev->dev, "parse allocated resources\n");
135
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600136 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 while ((char *)p < (char *)end) {
139
140 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700141 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 len = (p[2] << 8) | p[1];
143 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700144 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700146 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 switch (tag) {
150
151 case LARGE_TAG_MEM:
152 if (len != 9)
153 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700154 io = *(short *)&p[4];
155 size = *(short *)&p[10];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600156 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158
159 case LARGE_TAG_ANSISTR:
160 /* ignore this for now */
161 break;
162
163 case LARGE_TAG_VENDOR:
164 /* do nothing */
165 break;
166
167 case LARGE_TAG_MEM32:
168 if (len != 17)
169 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700170 io = *(int *)&p[4];
171 size = *(int *)&p[16];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600172 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174
175 case LARGE_TAG_FIXEDMEM32:
176 if (len != 9)
177 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700178 io = *(int *)&p[4];
179 size = *(int *)&p[8];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600180 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182
183 case SMALL_TAG_IRQ:
184 if (len < 2 || len > 3)
185 goto len_err;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600186 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 io = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700188 mask = p[1] + p[2] * 256;
189 for (i = 0; i < 16; i++, mask = mask >> 1)
190 if (mask & 0x01)
191 io = i;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600192 if (io != -1)
193 pcibios_penalize_isa_irq(io, 1);
194 else
195 flags = IORESOURCE_DISABLED;
196 pnp_add_irq_resource(dev, io, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198
199 case SMALL_TAG_DMA:
200 if (len != 2)
201 goto len_err;
202 io = -1;
203 mask = p[1];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700204 for (i = 0; i < 8; i++, mask = mask >> 1)
205 if (mask & 0x01)
206 io = i;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600207 pnpbios_parse_allocated_dmaresource(dev, io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209
210 case SMALL_TAG_PORT:
211 if (len != 7)
212 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700213 io = p[2] + p[3] * 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 size = p[7];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600215 pnpbios_parse_allocated_ioresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217
218 case SMALL_TAG_VENDOR:
219 /* do nothing */
220 break;
221
222 case SMALL_TAG_FIXEDPORT:
223 if (len != 3)
224 goto len_err;
225 io = p[1] + p[2] * 256;
226 size = p[3];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600227 pnpbios_parse_allocated_ioresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
229
230 case SMALL_TAG_END:
231 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700232 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700235 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600236len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600237 dev_err(&dev->dev, "unknown tag %#x length %d\n",
238 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 }
241
242 /* continue to the next tag */
243 if (p[0] & LARGE_TAG)
244 p += len + 3;
245 else
246 p += len + 1;
247 }
248
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600249 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 return NULL;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/*
255 * Resource Configuration Options
256 */
257
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600258static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
259 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800260 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700262 struct pnp_mem *mem;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700263
Robert P. J. Daycd861282006-12-13 00:34:52 -0800264 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!mem)
266 return;
267 mem->min = ((p[5] << 8) | p[4]) << 8;
268 mem->max = ((p[7] << 8) | p[6]) << 8;
269 mem->align = (p[9] << 8) | p[8];
270 mem->size = ((p[11] << 8) | p[10]) << 8;
271 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600272 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600275static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
276 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800277 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700279 struct pnp_mem *mem;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700280
Robert P. J. Daycd861282006-12-13 00:34:52 -0800281 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!mem)
283 return;
284 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
285 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
286 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
287 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
288 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600289 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600292static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
293 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800294 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700296 struct pnp_mem *mem;
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600297
Robert P. J. Daycd861282006-12-13 00:34:52 -0800298 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!mem)
300 return;
301 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
302 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
303 mem->align = 0;
304 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600305 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600308static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
309 unsigned char *p, int size,
310 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700312 struct pnp_irq *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned long bits;
314
Robert P. J. Daycd861282006-12-13 00:34:52 -0800315 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!irq)
317 return;
318 bits = (p[2] << 8) | p[1];
319 bitmap_copy(irq->map, &bits, 16);
320 if (size > 2)
321 irq->flags = p[3];
322 else
323 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600324 pnp_register_irq_resource(dev, option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600327static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
328 unsigned char *p, int size,
329 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700331 struct pnp_dma *dma;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700332
Robert P. J. Daycd861282006-12-13 00:34:52 -0800333 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!dma)
335 return;
336 dma->map = p[1];
337 dma->flags = p[2];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600338 pnp_register_dma_resource(dev, option, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600341static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
342 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800343 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700345 struct pnp_port *port;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700346
Robert P. J. Daycd861282006-12-13 00:34:52 -0800347 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (!port)
349 return;
350 port->min = (p[3] << 8) | p[2];
351 port->max = (p[5] << 8) | p[4];
352 port->align = p[6];
353 port->size = p[7];
354 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600355 pnp_register_port_resource(dev, option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600358static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
359 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800360 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700362 struct pnp_port *port;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700363
Robert P. J. Daycd861282006-12-13 00:34:52 -0800364 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 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 Helgaasc1caf062008-04-28 16:34:04 -0600371 pnp_register_port_resource(dev, option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800374static __init unsigned char *
375pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
376 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 unsigned int len, tag;
379 int priority = 0;
380 struct pnp_option *option, *option_independent;
381
382 if (!p)
383 return NULL;
384
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600385 dev_dbg(&dev->dev, "parse resource options\n");
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 option_independent = option = pnp_register_independent_option(dev);
388 if (!option)
389 return NULL;
390
391 while ((char *)p < (char *)end) {
392
393 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700394 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 len = (p[2] << 8) | p[1];
396 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700397 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700399 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 switch (tag) {
403
404 case LARGE_TAG_MEM:
405 if (len != 9)
406 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600407 pnpbios_parse_mem_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409
410 case LARGE_TAG_MEM32:
411 if (len != 17)
412 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600413 pnpbios_parse_mem32_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415
416 case LARGE_TAG_FIXEDMEM32:
417 if (len != 9)
418 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600419 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 break;
421
422 case SMALL_TAG_IRQ:
423 if (len < 2 || len > 3)
424 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600425 pnpbios_parse_irq_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427
428 case SMALL_TAG_DMA:
429 if (len != 2)
430 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600431 pnpbios_parse_dma_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 break;
433
434 case SMALL_TAG_PORT:
435 if (len != 7)
436 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600437 pnpbios_parse_port_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
439
440 case SMALL_TAG_VENDOR:
441 /* do nothing */
442 break;
443
444 case SMALL_TAG_FIXEDPORT:
445 if (len != 3)
446 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600447 pnpbios_parse_fixed_port_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449
450 case SMALL_TAG_STARTDEP:
451 if (len > 1)
452 goto len_err;
453 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
454 if (len > 0)
455 priority = 0x100 | p[1];
456 option = pnp_register_dependent_option(dev, priority);
457 if (!option)
458 return NULL;
459 break;
460
461 case SMALL_TAG_ENDDEP:
462 if (len != 0)
463 goto len_err;
464 if (option_independent == option)
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600465 dev_warn(&dev->dev, "missing "
466 "SMALL_TAG_STARTDEP tag\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 option = option_independent;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600468 dev_dbg(&dev->dev, "end dependent options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470
471 case SMALL_TAG_END:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700472 return p + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700474 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600475len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600476 dev_err(&dev->dev, "unknown tag %#x length %d\n",
477 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 }
480
481 /* continue to the next tag */
482 if (p[0] & LARGE_TAG)
483 p += len + 3;
484 else
485 p += len + 1;
486 }
487
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600488 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 return NULL;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * Compatible Device IDs
495 */
496
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700497static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
498 unsigned char *end,
499 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 int len, tag;
Bjorn Helgaas25eb8462008-04-28 16:33:53 -0600502 u32 eisa_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700512 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 len = (p[2] << 8) | p[1];
514 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700515 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700517 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 switch (tag) {
521
522 case LARGE_TAG_ANSISTR:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700523 strncpy(dev->name, p + 3,
524 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
525 dev->name[len >=
526 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 break;
528
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700529 case SMALL_TAG_COMPATDEVID: /* compatible ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (len != 4)
531 goto len_err;
Bjorn Helgaas25eb8462008-04-28 16:33:53 -0600532 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
533 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600534 dev_id = pnp_add_id(dev, id);
535 if (!dev_id)
536 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538
539 case SMALL_TAG_END:
540 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700541 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700544 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600545len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600546 dev_err(&dev->dev, "unknown tag %#x length %d\n",
547 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 }
550
551 /* continue to the next tag */
552 if (p[0] & LARGE_TAG)
553 p += len + 3;
554 else
555 p += len + 1;
556 }
557
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600558 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return NULL;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * Allocated Resource Encoding
565 */
566
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600567static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
568 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 unsigned long base = res->start;
571 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 p[4] = (base >> 8) & 0xff;
574 p[5] = ((base >> 8) >> 8) & 0xff;
575 p[6] = (base >> 8) & 0xff;
576 p[7] = ((base >> 8) >> 8) & 0xff;
577 p[10] = (len >> 8) & 0xff;
578 p[11] = ((len >> 8) >> 8) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600579
580 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n",
581 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600584static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
585 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 unsigned long base = res->start;
588 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 p[4] = base & 0xff;
591 p[5] = (base >> 8) & 0xff;
592 p[6] = (base >> 16) & 0xff;
593 p[7] = (base >> 24) & 0xff;
594 p[8] = base & 0xff;
595 p[9] = (base >> 8) & 0xff;
596 p[10] = (base >> 16) & 0xff;
597 p[11] = (base >> 24) & 0xff;
598 p[16] = len & 0xff;
599 p[17] = (len >> 8) & 0xff;
600 p[18] = (len >> 16) & 0xff;
601 p[19] = (len >> 24) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600602
603 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n",
604 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600607static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
608 struct resource *res)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700609{
610 unsigned long base = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 p[4] = base & 0xff;
614 p[5] = (base >> 8) & 0xff;
615 p[6] = (base >> 16) & 0xff;
616 p[7] = (base >> 24) & 0xff;
617 p[8] = len & 0xff;
618 p[9] = (len >> 8) & 0xff;
619 p[10] = (len >> 16) & 0xff;
620 p[11] = (len >> 24) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600621
622 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n",
623 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600626static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
627 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 unsigned long map = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 map = 1 << res->start;
632 p[1] = map & 0xff;
633 p[2] = (map >> 8) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600634
635 dev_dbg(&dev->dev, " encode irq %d\n", res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600638static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
639 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 unsigned long map = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 map = 1 << res->start;
644 p[1] = map & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600645
646 dev_dbg(&dev->dev, " encode dma %d\n", res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600649static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
650 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 unsigned long base = res->start;
653 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 p[2] = base & 0xff;
656 p[3] = (base >> 8) & 0xff;
657 p[4] = base & 0xff;
658 p[5] = (base >> 8) & 0xff;
659 p[7] = len & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600660
661 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n",
662 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600665static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
666 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 unsigned long base = res->start;
669 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 p[1] = base & 0xff;
672 p[2] = (base >> 8) & 0xff;
673 p[3] = len & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600674
675 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
676 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600679static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
680 *dev,
681 unsigned char *p,
682 unsigned char *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 unsigned int len, tag;
685 int port = 0, irq = 0, dma = 0, mem = 0;
686
687 if (!p)
688 return NULL;
689
690 while ((char *)p < (char *)end) {
691
692 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700693 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 len = (p[2] << 8) | p[1];
695 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700696 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700698 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 switch (tag) {
702
703 case LARGE_TAG_MEM:
704 if (len != 9)
705 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600706 pnpbios_encode_mem(dev, p,
707 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 mem++;
709 break;
710
711 case LARGE_TAG_MEM32:
712 if (len != 17)
713 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600714 pnpbios_encode_mem32(dev, p,
715 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 mem++;
717 break;
718
719 case LARGE_TAG_FIXEDMEM32:
720 if (len != 9)
721 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600722 pnpbios_encode_fixed_mem32(dev, p,
723 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 mem++;
725 break;
726
727 case SMALL_TAG_IRQ:
728 if (len < 2 || len > 3)
729 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600730 pnpbios_encode_irq(dev, p,
731 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 irq++;
733 break;
734
735 case SMALL_TAG_DMA:
736 if (len != 2)
737 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600738 pnpbios_encode_dma(dev, p,
739 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 dma++;
741 break;
742
743 case SMALL_TAG_PORT:
744 if (len != 7)
745 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600746 pnpbios_encode_port(dev, p,
747 pnp_get_resource(dev, IORESOURCE_IO, port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 port++;
749 break;
750
751 case SMALL_TAG_VENDOR:
752 /* do nothing */
753 break;
754
755 case SMALL_TAG_FIXEDPORT:
756 if (len != 3)
757 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600758 pnpbios_encode_fixed_port(dev, p,
759 pnp_get_resource(dev, IORESOURCE_IO, port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 port++;
761 break;
762
763 case SMALL_TAG_END:
764 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700765 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700768 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600769len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600770 dev_err(&dev->dev, "unknown tag %#x length %d\n",
771 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 break;
773 }
774
775 /* continue to the next tag */
776 if (p[0] & LARGE_TAG)
777 p += len + 3;
778 else
779 p += len + 1;
780 }
781
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600782 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 return NULL;
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
788 * Core Parsing Functions
789 */
790
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800791int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
792 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700794 unsigned char *p = (char *)node->data;
795 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700796
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600797 p = pnpbios_parse_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!p)
799 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700800 p = pnpbios_parse_resource_option_data(p, end, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!p)
802 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700803 p = pnpbios_parse_compatible_ids(p, end, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!p)
805 return -EIO;
806 return 0;
807}
808
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600809int pnpbios_read_resources_from_node(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700810 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700812 unsigned char *p = (char *)node->data;
813 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700814
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600815 p = pnpbios_parse_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!p)
817 return -EIO;
818 return 0;
819}
820
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600821int pnpbios_write_resources_to_node(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700822 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700824 unsigned char *p = (char *)node->data;
825 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700826
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600827 p = pnpbios_encode_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (!p)
829 return -EIO;
830 return 0;
831}