blob: 8c83bc16a9be7b6ff0aa5faf11e84a4138ef30d3 [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_ioresource(struct pnp_dev *dev,
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060058 int start, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060060 int flags = 0;
61 int end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070062
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060063 if (len <= 0 || end >= 0x10003)
64 flags |= IORESOURCE_DISABLED;
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060065
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060066 pnp_add_io_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060069static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070070 int mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060072 struct resource *res;
73 int i;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070074
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060075 for (i = 0; i < PNP_MAX_MEM; i++) {
76 res = pnp_get_resource(dev, IORESOURCE_MEM, i);
77 if (!pnp_resource_valid(res))
78 break;
79 }
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (i < PNP_MAX_MEM) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060082 res->flags = IORESOURCE_MEM; // Also clears _UNSET flag
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (len <= 0) {
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060084 res->flags |= IORESOURCE_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return;
86 }
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060087 res->start = (unsigned long)mem;
88 res->end = (unsigned long)(mem + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90}
91
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060092static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
93 unsigned char *p,
94 unsigned char *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned int len, tag;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -060097 int io, size, mask, i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if (!p)
100 return NULL;
101
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600102 dev_dbg(&dev->dev, "parse allocated resources\n");
103
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600104 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 while ((char *)p < (char *)end) {
107
108 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700109 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 len = (p[2] << 8) | p[1];
111 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700112 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700114 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 switch (tag) {
118
119 case LARGE_TAG_MEM:
120 if (len != 9)
121 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700122 io = *(short *)&p[4];
123 size = *(short *)&p[10];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600124 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
126
127 case LARGE_TAG_ANSISTR:
128 /* ignore this for now */
129 break;
130
131 case LARGE_TAG_VENDOR:
132 /* do nothing */
133 break;
134
135 case LARGE_TAG_MEM32:
136 if (len != 17)
137 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700138 io = *(int *)&p[4];
139 size = *(int *)&p[16];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600140 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142
143 case LARGE_TAG_FIXEDMEM32:
144 if (len != 9)
145 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700146 io = *(int *)&p[4];
147 size = *(int *)&p[8];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600148 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150
151 case SMALL_TAG_IRQ:
152 if (len < 2 || len > 3)
153 goto len_err;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600154 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 io = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700156 mask = p[1] + p[2] * 256;
157 for (i = 0; i < 16; i++, mask = mask >> 1)
158 if (mask & 0x01)
159 io = i;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600160 if (io != -1)
161 pcibios_penalize_isa_irq(io, 1);
162 else
163 flags = IORESOURCE_DISABLED;
164 pnp_add_irq_resource(dev, io, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break;
166
167 case SMALL_TAG_DMA:
168 if (len != 2)
169 goto len_err;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600170 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 io = -1;
172 mask = p[1];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700173 for (i = 0; i < 8; i++, mask = mask >> 1)
174 if (mask & 0x01)
175 io = i;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600176 if (io == -1)
177 flags = IORESOURCE_DISABLED;
178 pnp_add_dma_resource(dev, io, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180
181 case SMALL_TAG_PORT:
182 if (len != 7)
183 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700184 io = p[2] + p[3] * 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 size = p[7];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600186 pnpbios_parse_allocated_ioresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188
189 case SMALL_TAG_VENDOR:
190 /* do nothing */
191 break;
192
193 case SMALL_TAG_FIXEDPORT:
194 if (len != 3)
195 goto len_err;
196 io = p[1] + p[2] * 256;
197 size = p[3];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600198 pnpbios_parse_allocated_ioresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200
201 case SMALL_TAG_END:
202 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700203 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700206 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600207len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600208 dev_err(&dev->dev, "unknown tag %#x length %d\n",
209 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211 }
212
213 /* continue to the next tag */
214 if (p[0] & LARGE_TAG)
215 p += len + 3;
216 else
217 p += len + 1;
218 }
219
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600220 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 return NULL;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * Resource Configuration Options
227 */
228
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600229static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
230 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800231 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700233 struct pnp_mem *mem;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700234
Robert P. J. Daycd861282006-12-13 00:34:52 -0800235 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!mem)
237 return;
238 mem->min = ((p[5] << 8) | p[4]) << 8;
239 mem->max = ((p[7] << 8) | p[6]) << 8;
240 mem->align = (p[9] << 8) | p[8];
241 mem->size = ((p[11] << 8) | p[10]) << 8;
242 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600243 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600246static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
247 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800248 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700250 struct pnp_mem *mem;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700251
Robert P. J. Daycd861282006-12-13 00:34:52 -0800252 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!mem)
254 return;
255 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
256 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
257 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
258 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
259 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600260 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600263static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
264 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800265 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700267 struct pnp_mem *mem;
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600268
Robert P. J. Daycd861282006-12-13 00:34:52 -0800269 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!mem)
271 return;
272 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
273 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
274 mem->align = 0;
275 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600276 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600279static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
280 unsigned char *p, int size,
281 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700283 struct pnp_irq *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned long bits;
285
Robert P. J. Daycd861282006-12-13 00:34:52 -0800286 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!irq)
288 return;
289 bits = (p[2] << 8) | p[1];
290 bitmap_copy(irq->map, &bits, 16);
291 if (size > 2)
292 irq->flags = p[3];
293 else
294 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600295 pnp_register_irq_resource(dev, option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600298static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
299 unsigned char *p, int size,
300 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700302 struct pnp_dma *dma;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700303
Robert P. J. Daycd861282006-12-13 00:34:52 -0800304 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!dma)
306 return;
307 dma->map = p[1];
308 dma->flags = p[2];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600309 pnp_register_dma_resource(dev, option, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600312static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
313 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800314 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700316 struct pnp_port *port;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700317
Robert P. J. Daycd861282006-12-13 00:34:52 -0800318 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!port)
320 return;
321 port->min = (p[3] << 8) | p[2];
322 port->max = (p[5] << 8) | p[4];
323 port->align = p[6];
324 port->size = p[7];
325 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600326 pnp_register_port_resource(dev, option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600329static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
330 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800331 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700333 struct pnp_port *port;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700334
Robert P. J. Daycd861282006-12-13 00:34:52 -0800335 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!port)
337 return;
338 port->min = port->max = (p[2] << 8) | p[1];
339 port->size = p[3];
340 port->align = 0;
341 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600342 pnp_register_port_resource(dev, option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800345static __init unsigned char *
346pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
347 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 unsigned int len, tag;
350 int priority = 0;
351 struct pnp_option *option, *option_independent;
352
353 if (!p)
354 return NULL;
355
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600356 dev_dbg(&dev->dev, "parse resource options\n");
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 option_independent = option = pnp_register_independent_option(dev);
359 if (!option)
360 return NULL;
361
362 while ((char *)p < (char *)end) {
363
364 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700365 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 len = (p[2] << 8) | p[1];
367 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700368 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700370 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 switch (tag) {
374
375 case LARGE_TAG_MEM:
376 if (len != 9)
377 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600378 pnpbios_parse_mem_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380
381 case LARGE_TAG_MEM32:
382 if (len != 17)
383 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600384 pnpbios_parse_mem32_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386
387 case LARGE_TAG_FIXEDMEM32:
388 if (len != 9)
389 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600390 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
392
393 case SMALL_TAG_IRQ:
394 if (len < 2 || len > 3)
395 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600396 pnpbios_parse_irq_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398
399 case SMALL_TAG_DMA:
400 if (len != 2)
401 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600402 pnpbios_parse_dma_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404
405 case SMALL_TAG_PORT:
406 if (len != 7)
407 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600408 pnpbios_parse_port_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
410
411 case SMALL_TAG_VENDOR:
412 /* do nothing */
413 break;
414
415 case SMALL_TAG_FIXEDPORT:
416 if (len != 3)
417 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600418 pnpbios_parse_fixed_port_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420
421 case SMALL_TAG_STARTDEP:
422 if (len > 1)
423 goto len_err;
424 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
425 if (len > 0)
426 priority = 0x100 | p[1];
427 option = pnp_register_dependent_option(dev, priority);
428 if (!option)
429 return NULL;
430 break;
431
432 case SMALL_TAG_ENDDEP:
433 if (len != 0)
434 goto len_err;
435 if (option_independent == option)
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600436 dev_warn(&dev->dev, "missing "
437 "SMALL_TAG_STARTDEP tag\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 option = option_independent;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600439 dev_dbg(&dev->dev, "end dependent options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441
442 case SMALL_TAG_END:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700443 return p + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700445 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600446len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600447 dev_err(&dev->dev, "unknown tag %#x length %d\n",
448 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 }
451
452 /* continue to the next tag */
453 if (p[0] & LARGE_TAG)
454 p += len + 3;
455 else
456 p += len + 1;
457 }
458
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600459 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 return NULL;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * Compatible Device IDs
466 */
467
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700468static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
469 unsigned char *end,
470 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int len, tag;
Bjorn Helgaas25eb8462008-04-28 16:33:53 -0600473 u32 eisa_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 char id[8];
475 struct pnp_id *dev_id;
476
477 if (!p)
478 return NULL;
479
480 while ((char *)p < (char *)end) {
481
482 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700483 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 len = (p[2] << 8) | p[1];
485 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700486 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700488 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
491 switch (tag) {
492
493 case LARGE_TAG_ANSISTR:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700494 strncpy(dev->name, p + 3,
495 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
496 dev->name[len >=
497 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700500 case SMALL_TAG_COMPATDEVID: /* compatible ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (len != 4)
502 goto len_err;
Bjorn Helgaas25eb8462008-04-28 16:33:53 -0600503 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
504 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600505 dev_id = pnp_add_id(dev, id);
506 if (!dev_id)
507 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509
510 case SMALL_TAG_END:
511 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700512 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
514
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700515 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600516len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600517 dev_err(&dev->dev, "unknown tag %#x length %d\n",
518 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520 }
521
522 /* continue to the next tag */
523 if (p[0] & LARGE_TAG)
524 p += len + 3;
525 else
526 p += len + 1;
527 }
528
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600529 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 return NULL;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
535 * Allocated Resource Encoding
536 */
537
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600538static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
539 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned long base = res->start;
542 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 p[4] = (base >> 8) & 0xff;
545 p[5] = ((base >> 8) >> 8) & 0xff;
546 p[6] = (base >> 8) & 0xff;
547 p[7] = ((base >> 8) >> 8) & 0xff;
548 p[10] = (len >> 8) & 0xff;
549 p[11] = ((len >> 8) >> 8) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600550
551 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n",
552 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600555static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
556 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned long base = res->start;
559 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 p[4] = base & 0xff;
562 p[5] = (base >> 8) & 0xff;
563 p[6] = (base >> 16) & 0xff;
564 p[7] = (base >> 24) & 0xff;
565 p[8] = base & 0xff;
566 p[9] = (base >> 8) & 0xff;
567 p[10] = (base >> 16) & 0xff;
568 p[11] = (base >> 24) & 0xff;
569 p[16] = len & 0xff;
570 p[17] = (len >> 8) & 0xff;
571 p[18] = (len >> 16) & 0xff;
572 p[19] = (len >> 24) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600573
574 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n",
575 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600578static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
579 struct resource *res)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700580{
581 unsigned long base = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 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] = len & 0xff;
589 p[9] = (len >> 8) & 0xff;
590 p[10] = (len >> 16) & 0xff;
591 p[11] = (len >> 24) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600592
593 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n",
594 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600597static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
598 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 unsigned long map = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 map = 1 << res->start;
603 p[1] = map & 0xff;
604 p[2] = (map >> 8) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600605
606 dev_dbg(&dev->dev, " encode irq %d\n", res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600609static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
610 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 unsigned long map = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 map = 1 << res->start;
615 p[1] = map & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600616
617 dev_dbg(&dev->dev, " encode dma %d\n", res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600620static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
621 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 unsigned long base = res->start;
624 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 p[2] = base & 0xff;
627 p[3] = (base >> 8) & 0xff;
628 p[4] = base & 0xff;
629 p[5] = (base >> 8) & 0xff;
630 p[7] = len & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600631
632 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n",
633 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600636static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
637 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 unsigned long base = res->start;
640 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 p[1] = base & 0xff;
643 p[2] = (base >> 8) & 0xff;
644 p[3] = len & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600645
646 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
647 (unsigned long long) res->start, (unsigned long long) res->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600650static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
651 *dev,
652 unsigned char *p,
653 unsigned char *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
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 */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700664 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 len = (p[2] << 8) | p[1];
666 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700667 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700669 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 switch (tag) {
673
674 case LARGE_TAG_MEM:
675 if (len != 9)
676 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600677 pnpbios_encode_mem(dev, p,
678 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 mem++;
680 break;
681
682 case LARGE_TAG_MEM32:
683 if (len != 17)
684 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600685 pnpbios_encode_mem32(dev, p,
686 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mem++;
688 break;
689
690 case LARGE_TAG_FIXEDMEM32:
691 if (len != 9)
692 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600693 pnpbios_encode_fixed_mem32(dev, p,
694 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 mem++;
696 break;
697
698 case SMALL_TAG_IRQ:
699 if (len < 2 || len > 3)
700 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600701 pnpbios_encode_irq(dev, p,
702 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 irq++;
704 break;
705
706 case SMALL_TAG_DMA:
707 if (len != 2)
708 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600709 pnpbios_encode_dma(dev, p,
710 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dma++;
712 break;
713
714 case SMALL_TAG_PORT:
715 if (len != 7)
716 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600717 pnpbios_encode_port(dev, p,
718 pnp_get_resource(dev, IORESOURCE_IO, port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 port++;
720 break;
721
722 case SMALL_TAG_VENDOR:
723 /* do nothing */
724 break;
725
726 case SMALL_TAG_FIXEDPORT:
727 if (len != 3)
728 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600729 pnpbios_encode_fixed_port(dev, p,
730 pnp_get_resource(dev, IORESOURCE_IO, port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 port++;
732 break;
733
734 case SMALL_TAG_END:
735 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700736 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700739 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600740len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600741 dev_err(&dev->dev, "unknown tag %#x length %d\n",
742 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744 }
745
746 /* continue to the next tag */
747 if (p[0] & LARGE_TAG)
748 p += len + 3;
749 else
750 p += len + 1;
751 }
752
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600753 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 return NULL;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * Core Parsing Functions
760 */
761
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800762int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
763 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700765 unsigned char *p = (char *)node->data;
766 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700767
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600768 p = pnpbios_parse_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (!p)
770 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700771 p = pnpbios_parse_resource_option_data(p, end, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!p)
773 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700774 p = pnpbios_parse_compatible_ids(p, end, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!p)
776 return -EIO;
777 return 0;
778}
779
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600780int pnpbios_read_resources_from_node(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700781 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700783 unsigned char *p = (char *)node->data;
784 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700785
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600786 p = pnpbios_parse_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!p)
788 return -EIO;
789 return 0;
790}
791
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600792int pnpbios_write_resources_to_node(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700793 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700795 unsigned char *p = (char *)node->data;
796 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700797
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600798 p = pnpbios_encode_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!p)
800 return -EIO;
801 return 0;
802}