blob: 01f0c3dd1b08ef259bc554c7ff2acb661e1b7e47 [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>
Tim Schmielau4e57b682005-10-30 15:03:48 -08007#include <linux/string.h>
8#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#ifdef CONFIG_PCI
11#include <linux/pci.h>
12#else
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070013inline void pcibios_penalize_isa_irq(int irq, int active)
14{
15}
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070016#endif /* CONFIG_PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Bjorn Helgaas772defc2008-04-28 16:33:52 -060018#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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 Helgaas4ab55d82008-04-28 16:34:06 -060056static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060057 int start, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060059 int flags = 0;
60 int end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070061
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060062 if (len <= 0 || end >= 0x10003)
63 flags |= IORESOURCE_DISABLED;
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060064
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -060065 pnp_add_io_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060068static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
Bjorn Helgaasd6180f32008-04-28 16:34:37 -060069 int start, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Bjorn Helgaasd6180f32008-04-28 16:34:37 -060071 int flags = 0;
72 int end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070073
Bjorn Helgaasd6180f32008-04-28 16:34:37 -060074 if (len <= 0)
75 flags |= IORESOURCE_DISABLED;
Bjorn Helgaas06cb58a2008-04-28 16:34:29 -060076
Bjorn Helgaasd6180f32008-04-28 16:34:37 -060077 pnp_add_mem_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060080static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
81 unsigned char *p,
82 unsigned char *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 unsigned int len, tag;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -060085 int io, size, mask, i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (!p)
88 return NULL;
89
Bjorn Helgaas72dcc882008-04-28 16:34:07 -060090 dev_dbg(&dev->dev, "parse allocated resources\n");
91
Bjorn Helgaasf4490002008-04-28 16:34:09 -060092 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 while ((char *)p < (char *)end) {
95
96 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070097 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 len = (p[2] << 8) | p[1];
99 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700100 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700102 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
105 switch (tag) {
106
107 case LARGE_TAG_MEM:
108 if (len != 9)
109 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700110 io = *(short *)&p[4];
111 size = *(short *)&p[10];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600112 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114
115 case LARGE_TAG_ANSISTR:
116 /* ignore this for now */
117 break;
118
119 case LARGE_TAG_VENDOR:
120 /* do nothing */
121 break;
122
123 case LARGE_TAG_MEM32:
124 if (len != 17)
125 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700126 io = *(int *)&p[4];
127 size = *(int *)&p[16];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600128 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
130
131 case LARGE_TAG_FIXEDMEM32:
132 if (len != 9)
133 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700134 io = *(int *)&p[4];
135 size = *(int *)&p[8];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600136 pnpbios_parse_allocated_memresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 break;
138
139 case SMALL_TAG_IRQ:
140 if (len < 2 || len > 3)
141 goto len_err;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600142 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 io = -1;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700144 mask = p[1] + p[2] * 256;
145 for (i = 0; i < 16; i++, mask = mask >> 1)
146 if (mask & 0x01)
147 io = i;
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600148 if (io != -1)
149 pcibios_penalize_isa_irq(io, 1);
150 else
151 flags = IORESOURCE_DISABLED;
152 pnp_add_irq_resource(dev, io, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154
155 case SMALL_TAG_DMA:
156 if (len != 2)
157 goto len_err;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600158 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 io = -1;
160 mask = p[1];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700161 for (i = 0; i < 8; i++, mask = mask >> 1)
162 if (mask & 0x01)
163 io = i;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600164 if (io == -1)
165 flags = IORESOURCE_DISABLED;
166 pnp_add_dma_resource(dev, io, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168
169 case SMALL_TAG_PORT:
170 if (len != 7)
171 goto len_err;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700172 io = p[2] + p[3] * 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 size = p[7];
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600174 pnpbios_parse_allocated_ioresource(dev, io, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
176
177 case SMALL_TAG_VENDOR:
178 /* do nothing */
179 break;
180
181 case SMALL_TAG_FIXEDPORT:
182 if (len != 3)
183 goto len_err;
184 io = p[1] + p[2] * 256;
185 size = p[3];
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_END:
190 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700191 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700194 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600195len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600196 dev_err(&dev->dev, "unknown tag %#x length %d\n",
197 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 }
200
201 /* continue to the next tag */
202 if (p[0] & LARGE_TAG)
203 p += len + 3;
204 else
205 p += len + 1;
206 }
207
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600208 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 return NULL;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * Resource Configuration Options
215 */
216
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600217static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
218 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800219 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700221 struct pnp_mem *mem;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700222
Robert P. J. Daycd861282006-12-13 00:34:52 -0800223 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!mem)
225 return;
226 mem->min = ((p[5] << 8) | p[4]) << 8;
227 mem->max = ((p[7] << 8) | p[6]) << 8;
228 mem->align = (p[9] << 8) | p[8];
229 mem->size = ((p[11] << 8) | p[10]) << 8;
230 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600231 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600234static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
235 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800236 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700238 struct pnp_mem *mem;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700239
Robert P. J. Daycd861282006-12-13 00:34:52 -0800240 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!mem)
242 return;
243 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
244 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
245 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
246 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
247 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600248 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600251static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
252 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800253 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700255 struct pnp_mem *mem;
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600256
Robert P. J. Daycd861282006-12-13 00:34:52 -0800257 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!mem)
259 return;
260 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
261 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
262 mem->align = 0;
263 mem->flags = p[3];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600264 pnp_register_mem_resource(dev, option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600267static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
268 unsigned char *p, int size,
269 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700271 struct pnp_irq *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 unsigned long bits;
273
Robert P. J. Daycd861282006-12-13 00:34:52 -0800274 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!irq)
276 return;
277 bits = (p[2] << 8) | p[1];
278 bitmap_copy(irq->map, &bits, 16);
279 if (size > 2)
280 irq->flags = p[3];
281 else
282 irq->flags = IORESOURCE_IRQ_HIGHEDGE;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600283 pnp_register_irq_resource(dev, option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600286static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
287 unsigned char *p, int size,
288 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700290 struct pnp_dma *dma;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700291
Robert P. J. Daycd861282006-12-13 00:34:52 -0800292 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!dma)
294 return;
295 dma->map = p[1];
296 dma->flags = p[2];
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600297 pnp_register_dma_resource(dev, option, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600300static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
301 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800302 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700304 struct pnp_port *port;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700305
Robert P. J. Daycd861282006-12-13 00:34:52 -0800306 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!port)
308 return;
309 port->min = (p[3] << 8) | p[2];
310 port->max = (p[5] << 8) | p[4];
311 port->align = p[6];
312 port->size = p[7];
313 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600314 pnp_register_port_resource(dev, option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600317static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
318 unsigned char *p, int size,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800319 struct pnp_option *option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700321 struct pnp_port *port;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700322
Robert P. J. Daycd861282006-12-13 00:34:52 -0800323 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!port)
325 return;
326 port->min = port->max = (p[2] << 8) | p[1];
327 port->size = p[3];
328 port->align = 0;
329 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600330 pnp_register_port_resource(dev, option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800333static __init unsigned char *
334pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
335 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 unsigned int len, tag;
338 int priority = 0;
339 struct pnp_option *option, *option_independent;
340
341 if (!p)
342 return NULL;
343
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600344 dev_dbg(&dev->dev, "parse resource options\n");
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 option_independent = option = pnp_register_independent_option(dev);
347 if (!option)
348 return NULL;
349
350 while ((char *)p < (char *)end) {
351
352 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700353 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 len = (p[2] << 8) | p[1];
355 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700356 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700358 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 switch (tag) {
362
363 case LARGE_TAG_MEM:
364 if (len != 9)
365 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600366 pnpbios_parse_mem_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368
369 case LARGE_TAG_MEM32:
370 if (len != 17)
371 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600372 pnpbios_parse_mem32_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374
375 case LARGE_TAG_FIXEDMEM32:
376 if (len != 9)
377 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600378 pnpbios_parse_fixed_mem32_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380
381 case SMALL_TAG_IRQ:
382 if (len < 2 || len > 3)
383 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600384 pnpbios_parse_irq_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386
387 case SMALL_TAG_DMA:
388 if (len != 2)
389 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600390 pnpbios_parse_dma_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
392
393 case SMALL_TAG_PORT:
394 if (len != 7)
395 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600396 pnpbios_parse_port_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398
399 case SMALL_TAG_VENDOR:
400 /* do nothing */
401 break;
402
403 case SMALL_TAG_FIXEDPORT:
404 if (len != 3)
405 goto len_err;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600406 pnpbios_parse_fixed_port_option(dev, p, len, option);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
408
409 case SMALL_TAG_STARTDEP:
410 if (len > 1)
411 goto len_err;
412 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
413 if (len > 0)
414 priority = 0x100 | p[1];
415 option = pnp_register_dependent_option(dev, priority);
416 if (!option)
417 return NULL;
418 break;
419
420 case SMALL_TAG_ENDDEP:
421 if (len != 0)
422 goto len_err;
423 if (option_independent == option)
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600424 dev_warn(&dev->dev, "missing "
425 "SMALL_TAG_STARTDEP tag\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 option = option_independent;
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600427 dev_dbg(&dev->dev, "end dependent options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429
430 case SMALL_TAG_END:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700431 return p + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700433 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600434len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600435 dev_err(&dev->dev, "unknown tag %#x length %d\n",
436 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 }
439
440 /* continue to the next tag */
441 if (p[0] & LARGE_TAG)
442 p += len + 3;
443 else
444 p += len + 1;
445 }
446
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600447 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return NULL;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * Compatible Device IDs
454 */
455
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700456static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
457 unsigned char *end,
458 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 int len, tag;
Bjorn Helgaas25eb8462008-04-28 16:33:53 -0600461 u32 eisa_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char id[8];
463 struct pnp_id *dev_id;
464
465 if (!p)
466 return NULL;
467
468 while ((char *)p < (char *)end) {
469
470 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700471 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 len = (p[2] << 8) | p[1];
473 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700474 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700476 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 switch (tag) {
480
481 case LARGE_TAG_ANSISTR:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700482 strncpy(dev->name, p + 3,
483 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
484 dev->name[len >=
485 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700488 case SMALL_TAG_COMPATDEVID: /* compatible ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (len != 4)
490 goto len_err;
Bjorn Helgaas25eb8462008-04-28 16:33:53 -0600491 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
492 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
Bjorn Helgaas772defc2008-04-28 16:33:52 -0600493 dev_id = pnp_add_id(dev, id);
494 if (!dev_id)
495 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497
498 case SMALL_TAG_END:
499 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700500 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700503 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600504len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600505 dev_err(&dev->dev, "unknown tag %#x length %d\n",
506 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 }
509
510 /* continue to the next tag */
511 if (p[0] & LARGE_TAG)
512 p += len + 3;
513 else
514 p += len + 1;
515 }
516
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600517 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 return NULL;
520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
523 * Allocated Resource Encoding
524 */
525
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600526static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
527 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600529 unsigned long base;
530 unsigned long len;
531
532 if (pnp_resource_enabled(res)) {
533 base = res->start;
534 len = res->end - res->start + 1;
535 } else {
536 base = 0;
537 len = 0;
538 }
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 p[4] = (base >> 8) & 0xff;
541 p[5] = ((base >> 8) >> 8) & 0xff;
542 p[6] = (base >> 8) & 0xff;
543 p[7] = ((base >> 8) >> 8) & 0xff;
544 p[10] = (len >> 8) & 0xff;
545 p[11] = ((len >> 8) >> 8) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600546
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600547 dev_dbg(&dev->dev, " encode mem %#lx-%#lx\n", base, base + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600550static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
551 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600553 unsigned long base;
554 unsigned long len;
555
556 if (pnp_resource_enabled(res)) {
557 base = res->start;
558 len = res->end - res->start + 1;
559 } else {
560 base = 0;
561 len = 0;
562 }
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 p[4] = base & 0xff;
565 p[5] = (base >> 8) & 0xff;
566 p[6] = (base >> 16) & 0xff;
567 p[7] = (base >> 24) & 0xff;
568 p[8] = base & 0xff;
569 p[9] = (base >> 8) & 0xff;
570 p[10] = (base >> 16) & 0xff;
571 p[11] = (base >> 24) & 0xff;
572 p[16] = len & 0xff;
573 p[17] = (len >> 8) & 0xff;
574 p[18] = (len >> 16) & 0xff;
575 p[19] = (len >> 24) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600576
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600577 dev_dbg(&dev->dev, " encode mem32 %#lx-%#lx\n", base, base + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600580static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
581 struct resource *res)
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700582{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600583 unsigned long base;
584 unsigned long len;
585
586 if (pnp_resource_enabled(res)) {
587 base = res->start;
588 len = res->end - res->start + 1;
589 } else {
590 base = 0;
591 len = 0;
592 }
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 p[4] = base & 0xff;
595 p[5] = (base >> 8) & 0xff;
596 p[6] = (base >> 16) & 0xff;
597 p[7] = (base >> 24) & 0xff;
598 p[8] = len & 0xff;
599 p[9] = (len >> 8) & 0xff;
600 p[10] = (len >> 16) & 0xff;
601 p[11] = (len >> 24) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600602
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600603 dev_dbg(&dev->dev, " encode fixed_mem32 %#lx-%#lx\n", base,
604 base + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600607static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
608 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600610 unsigned long map;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700611
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600612 if (pnp_resource_enabled(res))
613 map = 1 << res->start;
614 else
615 map = 0;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 p[1] = map & 0xff;
618 p[2] = (map >> 8) & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600619
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600620 dev_dbg(&dev->dev, " encode irq mask %#lx\n", map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600623static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
624 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600626 unsigned long map;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700627
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600628 if (pnp_resource_enabled(res))
629 map = 1 << res->start;
630 else
631 map = 0;
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 p[1] = map & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600634
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600635 dev_dbg(&dev->dev, " encode dma mask %#lx\n", map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600638static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
639 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600641 unsigned long base;
642 unsigned long len;
643
644 if (pnp_resource_enabled(res)) {
645 base = res->start;
646 len = res->end - res->start + 1;
647 } else {
648 base = 0;
649 len = 0;
650 }
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 p[2] = base & 0xff;
653 p[3] = (base >> 8) & 0xff;
654 p[4] = base & 0xff;
655 p[5] = (base >> 8) & 0xff;
656 p[7] = len & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600657
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600658 dev_dbg(&dev->dev, " encode io %#lx-%#lx\n", base, base + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600661static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
662 struct resource *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 unsigned long base = res->start;
665 unsigned long len = res->end - res->start + 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700666
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600667 if (pnp_resource_enabled(res)) {
668 base = res->start;
669 len = res->end - res->start + 1;
670 } else {
671 base = 0;
672 len = 0;
673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 p[1] = base & 0xff;
676 p[2] = (base >> 8) & 0xff;
677 p[3] = len & 0xff;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600678
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600679 dev_dbg(&dev->dev, " encode fixed_io %#lx-%#lx\n", base,
680 base + len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600683static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
684 *dev,
685 unsigned char *p,
686 unsigned char *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 unsigned int len, tag;
689 int port = 0, irq = 0, dma = 0, mem = 0;
690
691 if (!p)
692 return NULL;
693
694 while ((char *)p < (char *)end) {
695
696 /* determine the type of tag */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700697 if (p[0] & LARGE_TAG) { /* large tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 len = (p[2] << 8) | p[1];
699 tag = p[0];
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700700 } else { /* small tag */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 len = p[0] & 0x07;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700702 tag = ((p[0] >> 3) & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 switch (tag) {
706
707 case LARGE_TAG_MEM:
708 if (len != 9)
709 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600710 pnpbios_encode_mem(dev, p,
711 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 mem++;
713 break;
714
715 case LARGE_TAG_MEM32:
716 if (len != 17)
717 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600718 pnpbios_encode_mem32(dev, p,
719 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 mem++;
721 break;
722
723 case LARGE_TAG_FIXEDMEM32:
724 if (len != 9)
725 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600726 pnpbios_encode_fixed_mem32(dev, p,
727 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 mem++;
729 break;
730
731 case SMALL_TAG_IRQ:
732 if (len < 2 || len > 3)
733 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600734 pnpbios_encode_irq(dev, p,
735 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 irq++;
737 break;
738
739 case SMALL_TAG_DMA:
740 if (len != 2)
741 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600742 pnpbios_encode_dma(dev, p,
743 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dma++;
745 break;
746
747 case SMALL_TAG_PORT:
748 if (len != 7)
749 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600750 pnpbios_encode_port(dev, p,
751 pnp_get_resource(dev, IORESOURCE_IO, port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 port++;
753 break;
754
755 case SMALL_TAG_VENDOR:
756 /* do nothing */
757 break;
758
759 case SMALL_TAG_FIXEDPORT:
760 if (len != 3)
761 goto len_err;
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -0600762 pnpbios_encode_fixed_port(dev, p,
763 pnp_get_resource(dev, IORESOURCE_IO, port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 port++;
765 break;
766
767 case SMALL_TAG_END:
768 p = p + 2;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700769 return (unsigned char *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700772 default: /* an unkown tag */
Bjorn Helgaas1e0aa9a2007-08-15 10:32:08 -0600773len_err:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600774 dev_err(&dev->dev, "unknown tag %#x length %d\n",
775 tag, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 }
778
779 /* continue to the next tag */
780 if (p[0] & LARGE_TAG)
781 p += len + 3;
782 else
783 p += len + 1;
784 }
785
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600786 dev_err(&dev->dev, "no end tag in resource structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 return NULL;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
792 * Core Parsing Functions
793 */
794
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800795int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
796 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700798 unsigned char *p = (char *)node->data;
799 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700800
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600801 p = pnpbios_parse_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!p)
803 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700804 p = pnpbios_parse_resource_option_data(p, end, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!p)
806 return -EIO;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700807 p = pnpbios_parse_compatible_ids(p, end, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!p)
809 return -EIO;
810 return 0;
811}
812
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600813int pnpbios_read_resources_from_node(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700814 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700816 unsigned char *p = (char *)node->data;
817 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700818
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600819 p = pnpbios_parse_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!p)
821 return -EIO;
822 return 0;
823}
824
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600825int pnpbios_write_resources_to_node(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700826 struct pnp_bios_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700828 unsigned char *p = (char *)node->data;
829 unsigned char *end = (char *)(node->data + node->size);
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700830
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600831 p = pnpbios_encode_allocated_resource_data(dev, p, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!p)
833 return -EIO;
834 return 0;
835}