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.14 2003/06/11 00:32:45 obrien Exp $
27 * $DragonFly: src/sys/bus/isa/pnpparse.c,v 1.11 2008/07/21 23:42:02 swildner Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
36 #include <machine/stdarg.h>
44 #define I16(p) ((p)[0] + ((p)[1] << 8))
45 #define I32(p) (I16(p) + (I16((p)+2) << 16))
48 pnp_printf(u_int32_t id
, char *fmt
, ...)
53 kprintf("%s: ", pnp_eisaformat(id
));
58 /* parse a single descriptor */
61 pnp_parse_desc(device_t dev
, u_char tag
, u_char
*res
, int len
,
62 struct isa_config
*config
, int ldn
)
69 id
= isa_get_logicalid(dev
);
71 if (PNP_RES_TYPE(tag
) == 0) {
74 switch (PNP_SRES_NUM(tag
)) {
78 /* these descriptors are quietly ignored */
81 case PNP_TAG_LOGICAL_DEVICE
:
82 case PNP_TAG_START_DEPENDANT
:
83 case PNP_TAG_END_DEPENDANT
:
85 pnp_printf(id
, "unexpected small tag %d\n",
87 /* shouldn't happen; quit now */
90 case PNP_TAG_COMPAT_DEVICE
:
92 * Got a compatible device id resource.
93 * Should keep a list of compat ids in the device.
95 bcopy(res
, &compat_id
, 4);
96 if (isa_get_compatid(dev
) == 0)
97 isa_set_compatid(dev
, compat_id
);
100 case PNP_TAG_IRQ_FORMAT
:
101 if (config
->ic_nirq
== ISA_NIRQ
) {
102 pnp_printf(id
, "too many irqs\n");
106 /* a null descriptor */
107 config
->ic_irqmask
[config
->ic_nirq
] = 0;
112 pnp_printf(id
, "adding irq mask %#02x\n",
114 config
->ic_irqmask
[config
->ic_nirq
] = I16(res
);
118 case PNP_TAG_DMA_FORMAT
:
119 if (config
->ic_ndrq
== ISA_NDRQ
) {
120 pnp_printf(id
, "too many drqs\n");
124 /* a null descriptor */
125 config
->ic_drqmask
[config
->ic_ndrq
] = 0;
130 pnp_printf(id
, "adding dma mask %#02x\n",
132 config
->ic_drqmask
[config
->ic_ndrq
] = res
[0];
136 case PNP_TAG_IO_RANGE
:
137 if (config
->ic_nport
== ISA_NPORT
) {
138 pnp_printf(id
, "too many ports\n");
142 /* a null descriptor */
143 config
->ic_port
[config
->ic_nport
].ir_start
= 0;
144 config
->ic_port
[config
->ic_nport
].ir_end
= 0;
145 config
->ic_port
[config
->ic_nport
].ir_size
= 0;
146 config
->ic_port
[config
->ic_nport
].ir_align
= 0;
151 pnp_printf(id
, "adding io range "
152 "%#x-%#x, size=%#x, "
155 I16(res
+ 3) + res
[6]-1,
158 config
->ic_port
[config
->ic_nport
].ir_start
=
160 config
->ic_port
[config
->ic_nport
].ir_end
=
161 I16(res
+ 3) + res
[6] - 1;
162 config
->ic_port
[config
->ic_nport
].ir_size
= res
[6];
164 /* Make sure align is at least one */
167 config
->ic_port
[config
->ic_nport
].ir_align
= res
[5];
169 pnp_check_quirks(isa_get_vendorid(dev
),
170 isa_get_logicalid(dev
), ldn
, config
);
173 case PNP_TAG_IO_FIXED
:
174 if (config
->ic_nport
== ISA_NPORT
) {
175 pnp_printf(id
, "too many ports\n");
179 /* a null descriptor */
180 config
->ic_port
[config
->ic_nport
].ir_start
= 0;
181 config
->ic_port
[config
->ic_nport
].ir_end
= 0;
182 config
->ic_port
[config
->ic_nport
].ir_size
= 0;
183 config
->ic_port
[config
->ic_nport
].ir_align
= 0;
188 pnp_printf(id
, "adding fixed io range "
189 "%#x-%#x, size=%#x, "
192 I16(res
) + res
[2] - 1,
195 config
->ic_port
[config
->ic_nport
].ir_start
= I16(res
);
196 config
->ic_port
[config
->ic_nport
].ir_end
=
197 I16(res
) + res
[2] - 1;
198 config
->ic_port
[config
->ic_nport
].ir_size
= res
[2];
199 config
->ic_port
[config
->ic_nport
].ir_align
= 1;
205 pnp_printf(id
, "end config\n");
209 /* Skip this resource */
210 pnp_printf(id
, "unexpected small tag %d\n",
216 switch (PNP_LRES_NUM(tag
)) {
218 case PNP_TAG_ID_UNICODE
:
219 case PNP_TAG_LARGE_VENDOR
:
220 /* these descriptors are quietly ignored */
223 case PNP_TAG_ID_ANSI
:
224 if (len
> sizeof(buf
) - 1)
225 len
= sizeof(buf
) - 1;
226 bcopy(res
, buf
, len
);
229 * Trim trailing spaces and garbage.
231 while (len
> 0 && buf
[len
- 1] <= ' ')
234 device_set_desc_copy(dev
, buf
);
237 case PNP_TAG_MEMORY_RANGE
:
238 if (config
->ic_nmem
== ISA_NMEM
) {
239 pnp_printf(id
, "too many memory ranges\n");
242 if (I16(res
+ 7) == 0) {
243 /* a null descriptor */
244 config
->ic_mem
[config
->ic_nmem
].ir_start
= 0;
245 config
->ic_mem
[config
->ic_nmem
].ir_end
= 0;
246 config
->ic_mem
[config
->ic_nmem
].ir_size
= 0;
247 config
->ic_mem
[config
->ic_nmem
].ir_align
= 0;
252 temp
= I16(res
+ 7) << 8;
253 pnp_printf(id
, "adding memory range "
254 "%#x-%#x, size=%#x, "
257 (I16(res
+ 3) << 8) + temp
- 1,
260 config
->ic_mem
[config
->ic_nmem
].ir_start
=
262 config
->ic_mem
[config
->ic_nmem
].ir_end
=
263 (I16(res
+ 3) << 8) + (I16(res
+ 7) << 8) - 1;
264 config
->ic_mem
[config
->ic_nmem
].ir_size
=
266 config
->ic_mem
[config
->ic_nmem
].ir_align
= I16(res
+ 5);
267 if (!config
->ic_mem
[config
->ic_nmem
].ir_align
)
268 config
->ic_mem
[config
->ic_nmem
].ir_align
=
273 case PNP_TAG_MEMORY32_RANGE
:
274 if (config
->ic_nmem
== ISA_NMEM
) {
275 pnp_printf(id
, "too many memory ranges\n");
278 if (I32(res
+ 13) == 0) {
279 /* a null descriptor */
280 config
->ic_mem
[config
->ic_nmem
].ir_start
= 0;
281 config
->ic_mem
[config
->ic_nmem
].ir_end
= 0;
282 config
->ic_mem
[config
->ic_nmem
].ir_size
= 0;
283 config
->ic_mem
[config
->ic_nmem
].ir_align
= 0;
288 pnp_printf(id
, "adding memory32 range "
289 "%#x-%#x, size=%#x, "
292 I32(res
+ 5) + I32(res
+ 13) - 1,
293 I32(res
+ 13), I32(res
+ 9));
295 config
->ic_mem
[config
->ic_nmem
].ir_start
= I32(res
+ 1);
296 config
->ic_mem
[config
->ic_nmem
].ir_end
=
297 I32(res
+ 5) + I32(res
+ 13) - 1;
298 config
->ic_mem
[config
->ic_nmem
].ir_size
= I32(res
+ 13);
299 config
->ic_mem
[config
->ic_nmem
].ir_align
= I32(res
+ 9);
303 case PNP_TAG_MEMORY32_FIXED
:
304 if (config
->ic_nmem
== ISA_NMEM
) {
305 pnp_printf(id
, "too many memory ranges\n");
308 if (I32(res
+ 5) == 0) {
309 /* a null descriptor */
310 config
->ic_mem
[config
->ic_nmem
].ir_start
= 0;
311 config
->ic_mem
[config
->ic_nmem
].ir_end
= 0;
312 config
->ic_mem
[config
->ic_nmem
].ir_size
= 0;
313 config
->ic_mem
[config
->ic_nmem
].ir_align
= 0;
317 pnp_printf(id
, "adding fixed memory32 range "
318 "%#x-%#x, size=%#x\n",
320 I32(res
+ 1) + I32(res
+ 5) - 1,
323 config
->ic_mem
[config
->ic_nmem
].ir_start
= I32(res
+ 1);
324 config
->ic_mem
[config
->ic_nmem
].ir_end
=
325 I32(res
+ 1) + I32(res
+ 5) - 1;
326 config
->ic_mem
[config
->ic_nmem
].ir_size
= I32(res
+ 5);
327 config
->ic_mem
[config
->ic_nmem
].ir_align
= 1;
332 /* Skip this resource */
333 pnp_printf(id
, "unexpected large tag %d\n",
343 * Parse a single "dependent" resource combination.
347 pnp_parse_dependant(device_t dev
, u_char
*resources
, int len
,
348 struct isa_config
*config
, int ldn
)
352 res
= pnp_scan_resources(dev
, resources
, len
, config
,
353 ldn
, pnp_parse_desc
);
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
= kmalloc(sizeof(*configs
)*(1 + MAXDEP
),
426 M_DEVBUF
, M_WAITOK
| M_ZERO
);
427 config
= &configs
[0];
436 if (PNP_RES_TYPE(tag
) == 0) {
438 l
= PNP_SRES_LEN(tag
);
445 switch (PNP_SRES_NUM(tag
)) {
447 case PNP_TAG_START_DEPENDANT
:
450 * Copy the common resources first,
451 * then parse the "dependent" resources.
453 pnp_merge_resources(dev
, &configs
[0],
455 pnp_parse_dependant(dev
, start
,
460 if (ncfgs
> MAXDEP
) {
461 device_printf(parent
, "too many dependent configs (%d)\n", MAXDEP
);
465 config
= &configs
[ncfgs
];
467 * If the priority is not specified,
468 * then use the default of 'acceptable'
471 priorities
[ncfgs
] = p
[0];
473 priorities
[ncfgs
] = 1;
475 pnp_printf(id
, "start dependent (%d)\n",
480 case PNP_TAG_END_DEPENDANT
:
482 device_printf(parent
,
483 "malformed resources\n");
488 * Copy the common resources first,
489 * then parse the "dependent" resources.
491 pnp_merge_resources(dev
, &configs
[0], config
);
492 pnp_parse_dependant(dev
, start
, p
- start
- 1,
496 pnp_printf(id
, "end dependent\n");
498 * Back to the common part; clear it
499 * as its contents has already been copied
502 config
= &configs
[0];
503 bzero(config
, sizeof(*config
));
508 device_printf(parent
,
509 "malformed resources\n");
516 /* defer parsing a dependent section */
518 if (pnp_parse_desc(dev
, tag
, p
, l
, config
, ldn
))
538 pnp_parse_desc(dev
, tag
, p
, l
, config
, ldn
)) {
547 /* Single config without dependants */
548 ISA_ADD_CONFIG(parent
, dev
, priorities
[0], &configs
[0]);
549 kfree(configs
, M_DEVBUF
);
553 for (i
= 1; i
< ncfgs
; i
++) {
555 * Merge the remaining part of the common resources,
556 * if any. Strictly speaking, there shouldn't be common/main
557 * resources after the END_DEPENDENT tag.
559 pnp_merge_resources(dev
, &configs
[0], &configs
[i
]);
560 ISA_ADD_CONFIG(parent
, dev
, priorities
[i
], &configs
[i
]);
563 kfree(configs
, M_DEVBUF
);
567 pnp_scan_resources(device_t dev
, u_char
*resources
, int len
,
568 struct isa_config
*config
, int ldn
, pnp_scan_cb
*cb
)
578 if (PNP_RES_TYPE(tag
) == 0) {
580 l
= PNP_SRES_LEN(tag
);
583 if ((*cb
)(dev
, tag
, p
, l
, config
, ldn
))
585 if (PNP_SRES_NUM(tag
) == PNP_TAG_END
)
596 if ((*cb
)(dev
, tag
, p
, l
, config
, ldn
))