2 * Copyright (c) 1999 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/module.h>
35 #include <machine/stdarg.h>
37 #include <isa/isavar.h>
38 #include <isa/pnpreg.h>
39 #include <isa/pnpvar.h>
43 #define I16(p) ((p)[0] + ((p)[1] << 8))
44 #define I32(p) (I16(p) + (I16(p+2) << 16))
47 pnp_printf(u_int32_t id, char *fmt, ...)
52 printf("%s: ", pnp_eisaformat(id));
57 /* parse a single descriptor */
60 pnp_parse_desc(device_t dev, u_char tag, u_char *res, int len,
61 struct isa_config *config, int ldn)
68 id = isa_get_logicalid(dev);
70 if (PNP_RES_TYPE(tag) == 0) {
73 switch (PNP_SRES_NUM(tag)) {
77 /* these descriptors are quietly ignored */
80 case PNP_TAG_LOGICAL_DEVICE:
81 case PNP_TAG_START_DEPENDANT:
82 case PNP_TAG_END_DEPENDANT:
84 pnp_printf(id, "unexpected small tag %d\n",
86 /* shouldn't happen; quit now */
89 case PNP_TAG_COMPAT_DEVICE:
91 * Got a compatible device id resource.
92 * Should keep a list of compat ids in the device.
94 bcopy(res, &compat_id, 4);
95 if (isa_get_compatid(dev) == 0)
96 isa_set_compatid(dev, compat_id);
99 case PNP_TAG_IRQ_FORMAT:
100 if (config->ic_nirq == ISA_NIRQ) {
101 pnp_printf(id, "too many irqs\n");
105 /* a null descriptor */
106 config->ic_irqmask[config->ic_nirq] = 0;
111 pnp_printf(id, "adding irq mask %#02x\n",
113 config->ic_irqmask[config->ic_nirq] = I16(res);
117 case PNP_TAG_DMA_FORMAT:
118 if (config->ic_ndrq == ISA_NDRQ) {
119 pnp_printf(id, "too many drqs\n");
123 /* a null descriptor */
124 config->ic_drqmask[config->ic_ndrq] = 0;
129 pnp_printf(id, "adding dma mask %#02x\n",
131 config->ic_drqmask[config->ic_ndrq] = res[0];
135 case PNP_TAG_IO_RANGE:
136 if (config->ic_nport == ISA_NPORT) {
137 pnp_printf(id, "too many ports\n");
141 /* a null descriptor */
142 config->ic_port[config->ic_nport].ir_start = 0;
143 config->ic_port[config->ic_nport].ir_end = 0;
144 config->ic_port[config->ic_nport].ir_size = 0;
145 config->ic_port[config->ic_nport].ir_align = 0;
150 pnp_printf(id, "adding io range "
151 "%#x-%#x, size=%#x, "
154 I16(res + 3) + res[6]-1,
157 config->ic_port[config->ic_nport].ir_start =
159 config->ic_port[config->ic_nport].ir_end =
160 I16(res + 3) + res[6] - 1;
161 config->ic_port[config->ic_nport].ir_size = res[6];
163 /* Make sure align is at least one */
166 config->ic_port[config->ic_nport].ir_align = res[5];
168 pnp_check_quirks(isa_get_vendorid(dev),
169 isa_get_logicalid(dev), ldn, config);
172 case PNP_TAG_IO_FIXED:
173 if (config->ic_nport == ISA_NPORT) {
174 pnp_printf(id, "too many ports\n");
178 /* a null descriptor */
179 config->ic_port[config->ic_nport].ir_start = 0;
180 config->ic_port[config->ic_nport].ir_end = 0;
181 config->ic_port[config->ic_nport].ir_size = 0;
182 config->ic_port[config->ic_nport].ir_align = 0;
187 pnp_printf(id, "adding fixed io range "
188 "%#x-%#x, size=%#x, "
191 I16(res) + res[2] - 1,
194 config->ic_port[config->ic_nport].ir_start = I16(res);
195 config->ic_port[config->ic_nport].ir_end =
196 I16(res) + res[2] - 1;
197 config->ic_port[config->ic_nport].ir_size = res[2];
198 config->ic_port[config->ic_nport].ir_align = 1;
204 pnp_printf(id, "end config\n");
208 /* Skip this resource */
209 pnp_printf(id, "unexpected small tag %d\n",
215 switch (PNP_LRES_NUM(tag)) {
217 case PNP_TAG_ID_UNICODE:
218 case PNP_TAG_LARGE_VENDOR:
219 /* these descriptors are quietly ignored */
222 case PNP_TAG_ID_ANSI:
223 if (len > sizeof(buf) - 1)
224 len = sizeof(buf) - 1;
225 bcopy(res, buf, len);
228 * Trim trailing spaces and garbage.
230 while (len > 0 && buf[len - 1] <= ' ')
233 device_set_desc_copy(dev, buf);
236 case PNP_TAG_MEMORY_RANGE:
237 if (config->ic_nmem == ISA_NMEM) {
238 pnp_printf(id, "too many memory ranges\n");
241 if (I16(res + 7) == 0) {
242 /* a null descriptor */
243 config->ic_mem[config->ic_nmem].ir_start = 0;
244 config->ic_mem[config->ic_nmem].ir_end = 0;
245 config->ic_mem[config->ic_nmem].ir_size = 0;
246 config->ic_mem[config->ic_nmem].ir_align = 0;
251 temp = I16(res + 7) << 8;
252 pnp_printf(id, "adding memory range "
253 "%#x-%#x, size=%#x, "
256 (I16(res + 3) << 8) + temp - 1,
259 config->ic_mem[config->ic_nmem].ir_start =
261 config->ic_mem[config->ic_nmem].ir_end =
262 (I16(res + 3) << 8) + (I16(res + 7) << 8) - 1;
263 config->ic_mem[config->ic_nmem].ir_size =
265 config->ic_mem[config->ic_nmem].ir_align = I16(res + 5);
266 if (!config->ic_mem[config->ic_nmem].ir_align)
267 config->ic_mem[config->ic_nmem].ir_align =
272 case PNP_TAG_MEMORY32_RANGE:
273 if (config->ic_nmem == ISA_NMEM) {
274 pnp_printf(id, "too many memory ranges\n");
277 if (I32(res + 13) == 0) {
278 /* a null descriptor */
279 config->ic_mem[config->ic_nmem].ir_start = 0;
280 config->ic_mem[config->ic_nmem].ir_end = 0;
281 config->ic_mem[config->ic_nmem].ir_size = 0;
282 config->ic_mem[config->ic_nmem].ir_align = 0;
287 pnp_printf(id, "adding memory32 range "
288 "%#x-%#x, size=%#x, "
291 I32(res + 5) + I32(res + 13) - 1,
292 I32(res + 13), I32(res + 9));
294 config->ic_mem[config->ic_nmem].ir_start = I32(res + 1);
295 config->ic_mem[config->ic_nmem].ir_end =
296 I32(res + 5) + I32(res + 13) - 1;
297 config->ic_mem[config->ic_nmem].ir_size = I32(res + 13);
298 config->ic_mem[config->ic_nmem].ir_align = I32(res + 9);
302 case PNP_TAG_MEMORY32_FIXED:
303 if (config->ic_nmem == ISA_NMEM) {
304 pnp_printf(id, "too many memory ranges\n");
307 if (I32(res + 5) == 0) {
308 /* a null descriptor */
309 config->ic_mem[config->ic_nmem].ir_start = 0;
310 config->ic_mem[config->ic_nmem].ir_end = 0;
311 config->ic_mem[config->ic_nmem].ir_size = 0;
312 config->ic_mem[config->ic_nmem].ir_align = 0;
316 pnp_printf(id, "adding fixed memory32 range "
317 "%#x-%#x, size=%#x\n",
319 I32(res + 1) + I32(res + 5) - 1,
322 config->ic_mem[config->ic_nmem].ir_start = I32(res + 1);
323 config->ic_mem[config->ic_nmem].ir_end =
324 I32(res + 1) + I32(res + 5) - 1;
325 config->ic_mem[config->ic_nmem].ir_size = I32(res + 5);
326 config->ic_mem[config->ic_nmem].ir_align = 1;
331 /* Skip this resource */
332 pnp_printf(id, "unexpected large tag %d\n",
342 * Parse a single "dependent" resource combination.
346 *pnp_parse_dependant(device_t dev, u_char *resources, int len,
347 struct isa_config *config, int ldn)
350 return pnp_scan_resources(dev, resources, len, config, ldn,
355 pnp_merge_resources(device_t dev, struct isa_config *from,
356 struct isa_config *to)
361 parent = device_get_parent(dev);
362 for (i = 0; i < from->ic_nmem; i++) {
363 if (to->ic_nmem == ISA_NMEM) {
364 device_printf(parent, "too many memory ranges\n");
367 to->ic_mem[to->ic_nmem] = from->ic_mem[i];
370 for (i = 0; i < from->ic_nport; i++) {
371 if (to->ic_nport == ISA_NPORT) {
372 device_printf(parent, "too many port ranges\n");
375 to->ic_port[to->ic_nport] = from->ic_port[i];
378 for (i = 0; i < from->ic_nirq; i++) {
379 if (to->ic_nirq == ISA_NIRQ) {
380 device_printf(parent, "too many irq ranges\n");
383 to->ic_irqmask[to->ic_nirq] = from->ic_irqmask[i];
386 for (i = 0; i < from->ic_ndrq; i++) {
387 if (to->ic_ndrq == ISA_NDRQ) {
388 device_printf(parent, "too many drq ranges\n");
391 to->ic_drqmask[to->ic_ndrq] = from->ic_drqmask[i];
397 * Parse resource data for Logical Devices, make a list of available
398 * resource configurations, and add them to the device.
400 * This function exits as soon as it gets an error reading *ANY*
401 * Resource Data or it reaches the end of Resource Data.
405 pnp_parse_resources(device_t dev, u_char *resources, int len, int ldn)
407 struct isa_config *configs;
408 struct isa_config *config;
410 int priorities[1 + MAXDEP];
419 parent = device_get_parent(dev);
420 id = isa_get_logicalid(dev);
422 configs = (struct isa_config *)malloc(sizeof(*configs)*(1 + MAXDEP),
423 M_DEVBUF, M_NOWAIT | M_ZERO);
424 if (configs == NULL) {
425 device_printf(parent, "No memory to parse PNP data\n");
428 config = &configs[0];
437 if (PNP_RES_TYPE(tag) == 0) {
439 l = PNP_SRES_LEN(tag);
446 switch (PNP_SRES_NUM(tag)) {
448 case PNP_TAG_START_DEPENDANT:
451 * Copy the common resources first,
452 * then parse the "dependent" resources.
454 pnp_merge_resources(dev, &configs[0],
456 pnp_parse_dependant(dev, start,
461 if (ncfgs > MAXDEP) {
462 device_printf(parent, "too many dependant configs (%d)\n", MAXDEP);
466 config = &configs[ncfgs];
468 * If the priority is not specified,
469 * then use the default of 'acceptable'
472 priorities[ncfgs] = p[0];
474 priorities[ncfgs] = 1;
476 pnp_printf(id, "start dependent (%d)\n",
481 case PNP_TAG_END_DEPENDANT:
483 device_printf(parent,
484 "malformed resources\n");
489 * Copy the common resources first,
490 * then parse the "dependent" resources.
492 pnp_merge_resources(dev, &configs[0], config);
493 pnp_parse_dependant(dev, start, p - start - 1,
497 pnp_printf(id, "end dependent\n");
499 * Back to the common part; clear it
500 * as its contents has already been copied
503 config = &configs[0];
504 bzero(config, sizeof(*config));
509 device_printf(parent,
510 "malformed resources\n");
517 /* defer parsing a dependent section */
519 if (pnp_parse_desc(dev, tag, p, l, config, ldn))
539 pnp_parse_desc(dev, tag, p, l, config, ldn)) {
548 /* Single config without dependants */
549 ISA_ADD_CONFIG(parent, dev, priorities[0], &configs[0]);
550 free(configs, M_DEVBUF);
554 for (i = 1; i < ncfgs; i++) {
556 * Merge the remaining part of the common resources,
557 * if any. Strictly speaking, there shouldn't be common/main
558 * resources after the END_DEPENDENT tag.
560 pnp_merge_resources(dev, &configs[0], &configs[i]);
561 ISA_ADD_CONFIG(parent, dev, priorities[i], &configs[i]);
564 free(configs, M_DEVBUF);
568 *pnp_scan_resources(device_t dev, u_char *resources, int len,
569 struct isa_config *config, int ldn, pnp_scan_cb *cb)
579 if (PNP_RES_TYPE(tag) == 0) {
581 l = PNP_SRES_LEN(tag);
584 if ((*cb)(dev, tag, p, l, config, ldn))
586 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
597 if ((*cb)(dev, tag, p, l, config, ldn))