2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
38 #include <machine/stdarg.h>
40 #include <isa/isavar.h>
41 #include <isa/pnpreg.h>
42 #include <isa/pnpvar.h>
46 #define I16(p) ((p)[0] + ((p)[1] << 8))
47 #define I32(p) (I16(p) + (I16((p)+2) << 16))
50 pnp_printf(u_int32_t id, char *fmt, ...)
55 printf("%s: ", pnp_eisaformat(id));
60 /* parse a single descriptor */
63 pnp_parse_desc(device_t dev, u_char tag, u_char *res, int len,
64 struct isa_config *config, int ldn)
71 id = isa_get_logicalid(dev);
73 if (PNP_RES_TYPE(tag) == 0) {
76 switch (PNP_SRES_NUM(tag)) {
80 /* these descriptors are quietly ignored */
83 case PNP_TAG_LOGICAL_DEVICE:
84 case PNP_TAG_START_DEPENDANT:
85 case PNP_TAG_END_DEPENDANT:
87 pnp_printf(id, "unexpected small tag %d\n",
89 /* shouldn't happen; quit now */
92 case PNP_TAG_COMPAT_DEVICE:
94 * Got a compatible device id resource.
95 * Should keep a list of compat ids in the device.
97 bcopy(res, &compat_id, 4);
98 if (isa_get_compatid(dev) == 0)
99 isa_set_compatid(dev, compat_id);
102 case PNP_TAG_IRQ_FORMAT:
103 if (config->ic_nirq == ISA_NIRQ) {
104 pnp_printf(id, "too many irqs\n");
108 /* a null descriptor */
109 config->ic_irqmask[config->ic_nirq] = 0;
114 pnp_printf(id, "adding irq mask %#02x\n",
116 config->ic_irqmask[config->ic_nirq] = I16(res);
120 case PNP_TAG_DMA_FORMAT:
121 if (config->ic_ndrq == ISA_NDRQ) {
122 pnp_printf(id, "too many drqs\n");
126 /* a null descriptor */
127 config->ic_drqmask[config->ic_ndrq] = 0;
132 pnp_printf(id, "adding dma mask %#02x\n",
134 config->ic_drqmask[config->ic_ndrq] = res[0];
138 case PNP_TAG_IO_RANGE:
139 if (config->ic_nport == ISA_NPORT) {
140 pnp_printf(id, "too many ports\n");
144 /* a null descriptor */
145 config->ic_port[config->ic_nport].ir_start = 0;
146 config->ic_port[config->ic_nport].ir_end = 0;
147 config->ic_port[config->ic_nport].ir_size = 0;
148 config->ic_port[config->ic_nport].ir_align = 0;
153 pnp_printf(id, "adding io range "
154 "%#x-%#x, size=%#x, "
157 I16(res + 3) + res[6]-1,
160 config->ic_port[config->ic_nport].ir_start =
162 config->ic_port[config->ic_nport].ir_end =
163 I16(res + 3) + res[6] - 1;
164 config->ic_port[config->ic_nport].ir_size = res[6];
166 /* Make sure align is at least one */
169 config->ic_port[config->ic_nport].ir_align = res[5];
171 pnp_check_quirks(isa_get_vendorid(dev),
172 isa_get_logicalid(dev), ldn, config);
175 case PNP_TAG_IO_FIXED:
176 if (config->ic_nport == ISA_NPORT) {
177 pnp_printf(id, "too many ports\n");
181 /* a null descriptor */
182 config->ic_port[config->ic_nport].ir_start = 0;
183 config->ic_port[config->ic_nport].ir_end = 0;
184 config->ic_port[config->ic_nport].ir_size = 0;
185 config->ic_port[config->ic_nport].ir_align = 0;
190 pnp_printf(id, "adding fixed io range "
191 "%#x-%#x, size=%#x, "
194 I16(res) + res[2] - 1,
197 config->ic_port[config->ic_nport].ir_start = I16(res);
198 config->ic_port[config->ic_nport].ir_end =
199 I16(res) + res[2] - 1;
200 config->ic_port[config->ic_nport].ir_size = res[2];
201 config->ic_port[config->ic_nport].ir_align = 1;
207 pnp_printf(id, "end config\n");
211 /* Skip this resource */
212 pnp_printf(id, "unexpected small tag %d\n",
218 switch (PNP_LRES_NUM(tag)) {
220 case PNP_TAG_ID_UNICODE:
221 case PNP_TAG_LARGE_VENDOR:
222 /* these descriptors are quietly ignored */
225 case PNP_TAG_ID_ANSI:
226 if (len > sizeof(buf) - 1)
227 len = sizeof(buf) - 1;
228 bcopy(res, buf, len);
231 * Trim trailing spaces and garbage.
233 while (len > 0 && buf[len - 1] <= ' ')
236 device_set_desc_copy(dev, buf);
239 case PNP_TAG_MEMORY_RANGE:
240 if (config->ic_nmem == ISA_NMEM) {
241 pnp_printf(id, "too many memory ranges\n");
244 if (I16(res + 7) == 0) {
245 /* a null descriptor */
246 config->ic_mem[config->ic_nmem].ir_start = 0;
247 config->ic_mem[config->ic_nmem].ir_end = 0;
248 config->ic_mem[config->ic_nmem].ir_size = 0;
249 config->ic_mem[config->ic_nmem].ir_align = 0;
254 temp = I16(res + 7) << 8;
255 pnp_printf(id, "adding memory range "
256 "%#x-%#x, size=%#x, "
259 (I16(res + 3) << 8) + temp - 1,
262 config->ic_mem[config->ic_nmem].ir_start =
264 config->ic_mem[config->ic_nmem].ir_end =
265 (I16(res + 3) << 8) + (I16(res + 7) << 8) - 1;
266 config->ic_mem[config->ic_nmem].ir_size =
268 config->ic_mem[config->ic_nmem].ir_align = I16(res + 5);
269 if (!config->ic_mem[config->ic_nmem].ir_align)
270 config->ic_mem[config->ic_nmem].ir_align =
275 case PNP_TAG_MEMORY32_RANGE:
276 if (config->ic_nmem == ISA_NMEM) {
277 pnp_printf(id, "too many memory ranges\n");
280 if (I32(res + 13) == 0) {
281 /* a null descriptor */
282 config->ic_mem[config->ic_nmem].ir_start = 0;
283 config->ic_mem[config->ic_nmem].ir_end = 0;
284 config->ic_mem[config->ic_nmem].ir_size = 0;
285 config->ic_mem[config->ic_nmem].ir_align = 0;
290 pnp_printf(id, "adding memory32 range "
291 "%#x-%#x, size=%#x, "
294 I32(res + 5) + I32(res + 13) - 1,
295 I32(res + 13), I32(res + 9));
297 config->ic_mem[config->ic_nmem].ir_start = I32(res + 1);
298 config->ic_mem[config->ic_nmem].ir_end =
299 I32(res + 5) + I32(res + 13) - 1;
300 config->ic_mem[config->ic_nmem].ir_size = I32(res + 13);
301 config->ic_mem[config->ic_nmem].ir_align = I32(res + 9);
305 case PNP_TAG_MEMORY32_FIXED:
306 if (config->ic_nmem == ISA_NMEM) {
307 pnp_printf(id, "too many memory ranges\n");
310 if (I32(res + 5) == 0) {
311 /* a null descriptor */
312 config->ic_mem[config->ic_nmem].ir_start = 0;
313 config->ic_mem[config->ic_nmem].ir_end = 0;
314 config->ic_mem[config->ic_nmem].ir_size = 0;
315 config->ic_mem[config->ic_nmem].ir_align = 0;
319 pnp_printf(id, "adding fixed memory32 range "
320 "%#x-%#x, size=%#x\n",
322 I32(res + 1) + I32(res + 5) - 1,
325 config->ic_mem[config->ic_nmem].ir_start = I32(res + 1);
326 config->ic_mem[config->ic_nmem].ir_end =
327 I32(res + 1) + I32(res + 5) - 1;
328 config->ic_mem[config->ic_nmem].ir_size = I32(res + 5);
329 config->ic_mem[config->ic_nmem].ir_align = 1;
334 /* Skip this resource */
335 pnp_printf(id, "unexpected large tag %d\n",
345 * Parse a single "dependent" resource combination.
349 *pnp_parse_dependant(device_t dev, u_char *resources, int len,
350 struct isa_config *config, int ldn)
353 return pnp_scan_resources(dev, resources, len, config, ldn,
358 pnp_merge_resources(device_t dev, struct isa_config *from,
359 struct isa_config *to)
364 parent = device_get_parent(dev);
365 for (i = 0; i < from->ic_nmem; i++) {
366 if (to->ic_nmem == ISA_NMEM) {
367 device_printf(parent, "too many memory ranges\n");
370 to->ic_mem[to->ic_nmem] = from->ic_mem[i];
373 for (i = 0; i < from->ic_nport; i++) {
374 if (to->ic_nport == ISA_NPORT) {
375 device_printf(parent, "too many port ranges\n");
378 to->ic_port[to->ic_nport] = from->ic_port[i];
381 for (i = 0; i < from->ic_nirq; i++) {
382 if (to->ic_nirq == ISA_NIRQ) {
383 device_printf(parent, "too many irq ranges\n");
386 to->ic_irqmask[to->ic_nirq] = from->ic_irqmask[i];
389 for (i = 0; i < from->ic_ndrq; i++) {
390 if (to->ic_ndrq == ISA_NDRQ) {
391 device_printf(parent, "too many drq ranges\n");
394 to->ic_drqmask[to->ic_ndrq] = from->ic_drqmask[i];
400 * Parse resource data for Logical Devices, make a list of available
401 * resource configurations, and add them to the device.
403 * This function exits as soon as it gets an error reading *ANY*
404 * Resource Data or it reaches the end of Resource Data.
408 pnp_parse_resources(device_t dev, u_char *resources, int len, int ldn)
410 struct isa_config *configs;
411 struct isa_config *config;
413 int priorities[1 + MAXDEP];
422 parent = device_get_parent(dev);
423 id = isa_get_logicalid(dev);
425 configs = (struct isa_config *)malloc(sizeof(*configs)*(1 + MAXDEP),
426 M_DEVBUF, M_NOWAIT | M_ZERO);
427 if (configs == NULL) {
428 device_printf(parent, "No memory to parse PNP data\n");
431 config = &configs[0];
440 if (PNP_RES_TYPE(tag) == 0) {
442 l = PNP_SRES_LEN(tag);
449 switch (PNP_SRES_NUM(tag)) {
451 case PNP_TAG_START_DEPENDANT:
454 * Copy the common resources first,
455 * then parse the "dependent" resources.
457 pnp_merge_resources(dev, &configs[0],
459 pnp_parse_dependant(dev, start,
464 if (ncfgs > MAXDEP) {
465 device_printf(parent, "too many dependent configs (%d)\n", MAXDEP);
469 config = &configs[ncfgs];
471 * If the priority is not specified,
472 * then use the default of 'acceptable'
475 priorities[ncfgs] = p[0];
477 priorities[ncfgs] = 1;
479 pnp_printf(id, "start dependent (%d)\n",
484 case PNP_TAG_END_DEPENDANT:
486 device_printf(parent,
487 "malformed resources\n");
492 * Copy the common resources first,
493 * then parse the "dependent" resources.
495 pnp_merge_resources(dev, &configs[0], config);
496 pnp_parse_dependant(dev, start, p - start - 1,
500 pnp_printf(id, "end dependent\n");
502 * Back to the common part; clear it
503 * as its contents has already been copied
506 config = &configs[0];
507 bzero(config, sizeof(*config));
512 device_printf(parent,
513 "malformed resources\n");
520 /* defer parsing a dependent section */
522 if (pnp_parse_desc(dev, tag, p, l, config, ldn))
542 pnp_parse_desc(dev, tag, p, l, config, ldn)) {
551 /* Single config without dependants */
552 ISA_ADD_CONFIG(parent, dev, priorities[0], &configs[0]);
553 free(configs, M_DEVBUF);
557 for (i = 1; i < ncfgs; i++) {
559 * Merge the remaining part of the common resources,
560 * if any. Strictly speaking, there shouldn't be common/main
561 * resources after the END_DEPENDENT tag.
563 pnp_merge_resources(dev, &configs[0], &configs[i]);
564 ISA_ADD_CONFIG(parent, dev, priorities[i], &configs[i]);
567 free(configs, M_DEVBUF);
571 *pnp_scan_resources(device_t dev, u_char *resources, int len,
572 struct isa_config *config, int ldn, pnp_scan_cb *cb)
582 if (PNP_RES_TYPE(tag) == 0) {
584 l = PNP_SRES_LEN(tag);
587 if ((*cb)(dev, tag, p, l, config, ldn))
589 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
600 if ((*cb)(dev, tag, p, l, config, ldn))