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
26 * $FreeBSD: src/sys/isa/pnpparse.c,v 1.2.2.3 2000/11/07 05:53:55 msmith Exp $
27 * $DragonFly: src/sys/bus/isa/pnpparse.c,v 1.2 2003/06/17 04:28:40 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
36 #include <isa/isavar.h>
37 #include <isa/pnpreg.h>
38 #include <isa/pnpvar.h>
42 #define I16(p) ((p)[0] + ((p)[1] << 8))
43 #define I32(p) (I16(p) + (I16(p+2) << 16))
46 * Parse resource data for Logical Devices.
48 * This function exits as soon as it gets an error reading *ANY*
49 * Resource Data or it reaches the end of Resource Data.
52 pnp_parse_resources(device_t dev, u_char *resources, int len)
54 device_t parent = device_get_parent(dev);
55 u_char tag, *resp, *resinfo;
56 int large_len, scanning = len;
57 u_int32_t id, compat_id;
58 struct isa_config *config;
60 int priorities[1 + MAXDEP];
61 struct isa_config *configs;
65 id = isa_get_logicalid(dev);
66 configs = (struct isa_config *)malloc(sizeof(*configs) * (1 + MAXDEP),
68 if (configs == NULL) {
69 device_printf(dev, "No memory to parse PNP data\n");
72 bzero(configs, sizeof(*configs) * (1 + MAXDEP));
76 while (scanning > 0) {
79 if (PNP_RES_TYPE(tag) == 0) {
81 if (scanning < PNP_SRES_LEN(tag)) {
86 resp += PNP_SRES_LEN(tag);
87 scanning -= PNP_SRES_LEN(tag);;
89 switch (PNP_SRES_NUM(tag)) {
90 case PNP_TAG_COMPAT_DEVICE:
92 * Got a compatible device id
93 * resource. Should keep a list of
94 * compat ids in the device.
96 bcopy(resinfo, &compat_id, 4);
97 isa_set_compatid(dev, compat_id);
100 case PNP_TAG_IRQ_FORMAT:
102 printf("%s: adding irq mask %#04x\n",
106 if (config->ic_nirq == ISA_NIRQ) {
107 device_printf(parent, "too many irqs\n");
111 config->ic_irqmask[config->ic_nirq] =
116 case PNP_TAG_DMA_FORMAT:
118 printf("%s: adding dma mask %#02x\n",
122 if (config->ic_ndrq == ISA_NDRQ) {
123 device_printf(parent, "too many drqs\n");
127 config->ic_drqmask[config->ic_ndrq] =
132 case PNP_TAG_START_DEPENDANT:
134 printf("%s: start dependant\n",
137 if (ncfgs >= MAXDEP) {
138 device_printf(parent, "too many dependant configs (%d)\n", MAXDEP);
142 config = &configs[ncfgs];
144 * If the priority is not specified,
145 * then use the default of
148 if (PNP_SRES_LEN(tag) > 0)
149 priorities[ncfgs] = resinfo[0];
151 priorities[ncfgs] = 1;
155 case PNP_TAG_END_DEPENDANT:
157 printf("%s: end dependant\n",
160 config = &configs[0]; /* back to main config */
163 case PNP_TAG_IO_RANGE:
165 printf("%s: adding io range "
166 "%#x-%#x, size=%#x, "
170 I16(resinfo + 3) + resinfo[6]-1,
174 if (config->ic_nport == ISA_NPORT) {
175 device_printf(parent, "too many ports\n");
179 config->ic_port[config->ic_nport].ir_start =
181 config->ic_port[config->ic_nport].ir_end =
182 I16(resinfo + 3) + resinfo[6] - 1;
183 config->ic_port[config->ic_nport].ir_size =
185 if (resinfo[5] == 0) {
186 /* Make sure align is at least one */
189 config->ic_port[config->ic_nport].ir_align =
194 case PNP_TAG_IO_FIXED:
196 printf("%s: adding fixed io range "
197 "%#x-%#x, size=%#x, "
201 I16(resinfo) + resinfo[2] - 1,
205 if (config->ic_nport == ISA_NPORT) {
206 device_printf(parent, "too many ports\n");
210 config->ic_port[config->ic_nport].ir_start =
212 config->ic_port[config->ic_nport].ir_end =
213 I16(resinfo) + resinfo[2] - 1;
214 config->ic_port[config->ic_nport].ir_size
216 config->ic_port[config->ic_nport].ir_align = 1;
222 printf("%s: end config\n",
229 /* Skip this resource */
230 device_printf(parent, "unexpected small tag %d\n",
240 large_len = I16(resp);
244 if (scanning < large_len) {
250 scanning -= large_len;
252 switch (PNP_LRES_NUM(tag)) {
253 case PNP_TAG_ID_ANSI:
254 if (large_len > sizeof(buf) - 1)
255 large_len = sizeof(buf) - 1;
256 bcopy(resinfo, buf, large_len);
259 * Trim trailing spaces and garbage.
261 while (large_len > 0 && buf[large_len - 1] <= ' ')
263 buf[large_len] = '\0';
264 device_set_desc_copy(dev, buf);
267 case PNP_TAG_MEMORY_RANGE:
269 int temp = I16(resinfo + 7) << 8;
271 printf("%s: adding memory range "
272 "%#x-%#x, size=%#x, "
276 (I16(resinfo + 3)<<8) + temp - 1,
281 if (config->ic_nmem == ISA_NMEM) {
282 device_printf(parent, "too many memory ranges\n");
287 config->ic_mem[config->ic_nmem].ir_start =
289 config->ic_mem[config->ic_nmem].ir_end =
290 (I16(resinfo + 3)<<8)
291 + (I16(resinfo + 7) << 8) - 1;
292 config->ic_mem[config->ic_nmem].ir_size =
293 I16(resinfo + 7) << 8;
294 config->ic_mem[config->ic_nmem].ir_align =
296 if (!config->ic_mem[config->ic_nmem].ir_align)
297 config->ic_mem[config->ic_nmem]
302 case PNP_TAG_MEMORY32_RANGE:
303 if (I32(resinfo + 13) == 0) {
305 printf("%s: skipping empty range\n",
311 printf("%s: adding memory32 range "
312 "%#x-%#x, size=%#x, "
317 + I32(resinfo + 13) - 1,
322 if (config->ic_nmem == ISA_NMEM) {
323 device_printf(parent, "too many memory ranges\n");
328 config->ic_mem[config->ic_nmem].ir_start =
330 config->ic_mem[config->ic_nmem].ir_end =
332 + I32(resinfo + 13) - 1;
333 config->ic_mem[config->ic_nmem].ir_size =
335 config->ic_mem[config->ic_nmem].ir_align =
340 case PNP_TAG_MEMORY32_FIXED:
341 if (I32(resinfo + 5) == 0) {
343 printf("%s: skipping empty range\n",
349 printf("%s: adding fixed memory32 range "
350 "%#x-%#x, size=%#x\n",
354 + I32(resinfo + 5) - 1,
358 if (config->ic_nmem == ISA_NMEM) {
359 device_printf(parent, "too many memory ranges\n");
364 config->ic_mem[config->ic_nmem].ir_start =
366 config->ic_mem[config->ic_nmem].ir_end =
368 + I32(resinfo + 5) - 1;
369 config->ic_mem[config->ic_nmem].ir_size =
371 config->ic_mem[config->ic_nmem].ir_align = 1;
376 /* Skip this resource */
377 device_printf(parent, "unexpected large tag %d\n",
383 /* Single config without dependants */
384 (void)ISA_ADD_CONFIG(parent, dev, priorities[0], &configs[0]);
385 free(configs, M_DEVBUF);
388 /* Cycle through dependant configs merging primary details */
389 for(i = 1; i < ncfgs; i++) {
391 config = &configs[i];
392 for(j = 0; j < configs[0].ic_nmem; j++) {
393 if (config->ic_nmem == ISA_NMEM) {
394 device_printf(parent, "too many memory ranges\n");
395 free(configs, M_DEVBUF);
398 config->ic_mem[config->ic_nmem] = configs[0].ic_mem[j];
401 for(j = 0; j < configs[0].ic_nport; j++) {
402 if (config->ic_nport == ISA_NPORT) {
403 device_printf(parent, "too many port ranges\n");
404 free(configs, M_DEVBUF);
407 config->ic_port[config->ic_nport] = configs[0].ic_port[j];
410 for(j = 0; j < configs[0].ic_nirq; j++) {
411 if (config->ic_nirq == ISA_NIRQ) {
412 device_printf(parent, "too many irq ranges\n");
413 free(configs, M_DEVBUF);
416 config->ic_irqmask[config->ic_nirq] = configs[0].ic_irqmask[j];
419 for(j = 0; j < configs[0].ic_ndrq; j++) {
420 if (config->ic_ndrq == ISA_NDRQ) {
421 device_printf(parent, "too many drq ranges\n");
422 free(configs, M_DEVBUF);
425 config->ic_drqmask[config->ic_ndrq] = configs[0].ic_drqmask[j];
428 (void)ISA_ADD_CONFIG(parent, dev, priorities[i], &configs[i]);
430 free(configs, M_DEVBUF);