xref: /freebsd/sys/isa/isa_common.c (revision f9ce010afdd3136fc73e2b500f2ed916bf9cfa59)
1 /*-
2  * Copyright (c) 1999 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 /*
29  * Modifications for Intel architecture by Garrett A. Wollman.
30  * Copyright 1998 Massachusetts Institute of Technology
31  *
32  * Permission to use, copy, modify, and distribute this software and
33  * its documentation for any purpose and without fee is hereby
34  * granted, provided that both the above copyright notice and this
35  * permission notice appear in all copies, that both the above
36  * copyright notice and this permission notice appear in all
37  * supporting documentation, and that the name of M.I.T. not be used
38  * in advertising or publicity pertaining to distribution of the
39  * software without specific, written prior permission.  M.I.T. makes
40  * no representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied
42  * warranty.
43  *
44  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
45  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
46  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
48  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 /*
59  * Parts of the ISA bus implementation common to all architectures.
60  */
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/bus.h>
66 #include <sys/malloc.h>
67 #include <sys/module.h>
68 #include <machine/bus.h>
69 #include <sys/rman.h>
70 
71 #include <machine/resource.h>
72 
73 #include <isa/isavar.h>
74 #include <isa/isa_common.h>
75 #ifdef __alpha__		/* XXX workaround a stupid warning */
76 #include <alpha/isa/isavar.h>
77 #endif
78 
79 MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
80 
81 static devclass_t isa_devclass;
82 
83 /*
84  * At 'probe' time, we add all the devices which we know about to the
85  * bus.  The generic attach routine will probe and attach them if they
86  * are alive.
87  */
88 static int
89 isa_probe(device_t dev)
90 {
91 	device_set_desc(dev, "ISA bus");
92 	isa_init();		/* Allow machdep code to initialise */
93 	return bus_generic_probe(dev);
94 }
95 
96 extern device_t isa_bus_device;
97 
98 static int
99 isa_attach(device_t dev)
100 {
101 	/*
102 	 * Arrange for isa_probe_children(dev) to be called later. XXX
103 	 */
104 	isa_bus_device = dev;
105 	return 0;
106 }
107 
108 /*
109  * Find a working set of memory regions for a child using the ranges
110  * in *config  and return the regions in *result. Returns non-zero if
111  * a set of ranges was found.
112  */
113 static int
114 isa_find_memory(device_t child,
115 		struct isa_config *config,
116 		struct isa_config *result)
117 {
118 	device_t dev = device_get_parent(child);
119 	int success, i;
120 	struct resource *res[ISA_NMEM];
121 
122 	/*
123 	 * First clear out any existing resource definitions.
124 	 */
125 	for (i = 0; i < ISA_NMEM; i++) {
126 		ISA_DELETE_RESOURCE(dev, child, SYS_RES_MEMORY, i);
127 		res[i] = NULL;
128 	}
129 
130 	success = 1;
131 	result->ic_nmem = config->ic_nmem;
132 	for (i = 0; i < config->ic_nmem; i++) {
133 		u_int32_t start, end, size, align;
134 		for (start = config->ic_mem[i].ir_start,
135 			     end = config->ic_mem[i].ir_end,
136 			     size = config->ic_mem[i].ir_size,
137 			     align = config->ic_mem[i].ir_align;
138 		     start + size - 1 <= end;
139 		     start += align) {
140 			ISA_SET_RESOURCE(dev, child, SYS_RES_MEMORY, i,
141 					 start, size);
142 			res[i] = bus_alloc_resource(child,
143 						    SYS_RES_MEMORY, &i,
144 						    0, ~0, 1, RF_ACTIVE);
145 			if (res[i]) {
146 				result->ic_mem[i].ir_start = start;
147 				result->ic_mem[i].ir_end = start + size - 1;
148 				result->ic_mem[i].ir_size = size;
149 				result->ic_mem[i].ir_align = align;
150 				break;
151 			}
152 		}
153 
154 		/*
155 		 * If we didn't find a place for memory range i, then
156 		 * give up now.
157 		 */
158 		if (!res[i]) {
159 			success = 0;
160 			break;
161 		}
162 	}
163 
164 	for (i = 0; i < ISA_NMEM; i++) {
165 		if (res[i])
166 			bus_release_resource(child, SYS_RES_MEMORY,
167 					     i, res[i]);
168 	}
169 
170 	return success;
171 }
172 
173 /*
174  * Find a working set of port regions for a child using the ranges
175  * in *config  and return the regions in *result. Returns non-zero if
176  * a set of ranges was found.
177  */
178 static int
179 isa_find_port(device_t child,
180 	      struct isa_config *config,
181 	      struct isa_config *result)
182 {
183 	device_t dev = device_get_parent(child);
184 	int success, i;
185 	struct resource *res[ISA_NPORT];
186 
187 	/*
188 	 * First clear out any existing resource definitions.
189 	 */
190 	for (i = 0; i < ISA_NPORT; i++) {
191 		ISA_DELETE_RESOURCE(dev, child, SYS_RES_IOPORT, i);
192 		res[i] = NULL;
193 	}
194 
195 	success = 1;
196 	result->ic_nport = config->ic_nport;
197 	for (i = 0; i < config->ic_nport; i++) {
198 		u_int32_t start, end, size, align;
199 		for (start = config->ic_port[i].ir_start,
200 			     end = config->ic_port[i].ir_end,
201 			     size = config->ic_port[i].ir_size,
202 			     align = config->ic_port[i].ir_align;
203 		     start + size - 1 <= end;
204 		     start += align) {
205 			ISA_SET_RESOURCE(dev, child, SYS_RES_IOPORT, i,
206 					 start, size);
207 			res[i] = bus_alloc_resource(child,
208 						    SYS_RES_IOPORT, &i,
209 						    0, ~0, 1, RF_ACTIVE);
210 			if (res[i]) {
211 				result->ic_port[i].ir_start = start;
212 				result->ic_port[i].ir_end = start + size - 1;
213 				result->ic_port[i].ir_size = size;
214 				result->ic_port[i].ir_align = align;
215 				break;
216 			}
217 		}
218 
219 		/*
220 		 * If we didn't find a place for port range i, then
221 		 * give up now.
222 		 */
223 		if (!res[i]) {
224 			success = 0;
225 			break;
226 		}
227 	}
228 
229 	for (i = 0; i < ISA_NPORT; i++) {
230 		if (res[i])
231 			bus_release_resource(child, SYS_RES_IOPORT,
232 					     i, res[i]);
233 	}
234 
235 	return success;
236 }
237 
238 /*
239  * Return the index of the first bit in the mask (or -1 if mask is empty.
240  */
241 static int
242 find_first_bit(u_int32_t mask)
243 {
244 	return ffs(mask) - 1;
245 }
246 
247 /*
248  * Return the index of the next bit in the mask, or -1 if there are no more.
249  */
250 static int
251 find_next_bit(u_int32_t mask, int bit)
252 {
253 	bit++;
254 	while (bit < 32 && !(mask & (1 << bit)))
255 		bit++;
256 	if (bit != 32)
257 		return bit;
258 	return -1;
259 }
260 
261 /*
262  * Find a working set of irqs for a child using the masks in *config
263  * and return the regions in *result. Returns non-zero if a set of
264  * irqs was found.
265  */
266 static int
267 isa_find_irq(device_t child,
268 	     struct isa_config *config,
269 	     struct isa_config *result)
270 {
271 	device_t dev = device_get_parent(child);
272 	int success, i;
273 	struct resource *res[ISA_NIRQ];
274 
275 	/*
276 	 * First clear out any existing resource definitions.
277 	 */
278 	for (i = 0; i < ISA_NIRQ; i++) {
279 		ISA_DELETE_RESOURCE(dev, child, SYS_RES_IRQ, i);
280 		res[i] = NULL;
281 	}
282 
283 	success = 1;
284 	result->ic_nirq = config->ic_nirq;
285 	for (i = 0; i < config->ic_nirq; i++) {
286 		u_int32_t mask = config->ic_irqmask[i];
287 		int irq;
288 		for (irq = find_first_bit(mask);
289 		     irq != -1;
290 		     irq = find_next_bit(mask, irq)) {
291 			ISA_SET_RESOURCE(dev, child, SYS_RES_IRQ, i,
292 					 irq, 1);
293 			res[i] = bus_alloc_resource(child,
294 						    SYS_RES_IRQ, &i,
295 						    0, ~0, 1, RF_ACTIVE);
296 			if (res[i]) {
297 				result->ic_irqmask[i] = (1 << irq);
298 				break;
299 			}
300 		}
301 
302 		/*
303 		 * If we didn't find a place for irq range i, then
304 		 * give up now.
305 		 */
306 		if (!res[i]) {
307 			success = 0;
308 			break;
309 		}
310 	}
311 
312 	for (i = 0; i < ISA_NIRQ; i++) {
313 		if (res[i])
314 			bus_release_resource(child, SYS_RES_IRQ,
315 					     i, res[i]);
316 	}
317 
318 	return success;
319 }
320 
321 /*
322  * Find a working set of drqs for a child using the masks in *config
323  * and return the regions in *result. Returns non-zero if a set of
324  * drqs was found.
325  */
326 static int
327 isa_find_drq(device_t child,
328 	     struct isa_config *config,
329 	     struct isa_config *result)
330 {
331 	device_t dev = device_get_parent(child);
332 	int success, i;
333 	struct resource *res[ISA_NDRQ];
334 
335 	/*
336 	 * First clear out any existing resource definitions.
337 	 */
338 	for (i = 0; i < ISA_NDRQ; i++) {
339 		ISA_DELETE_RESOURCE(dev, child, SYS_RES_DRQ, i);
340 		res[i] = NULL;
341 	}
342 
343 	success = 1;
344 	result->ic_ndrq = config->ic_ndrq;
345 	for (i = 0; i < config->ic_ndrq; i++) {
346 		u_int32_t mask = config->ic_drqmask[i];
347 		int drq;
348 		for (drq = find_first_bit(mask);
349 		     drq != -1;
350 		     drq = find_next_bit(mask, drq)) {
351 			ISA_SET_RESOURCE(dev, child, SYS_RES_DRQ, i,
352 					 drq, 1);
353 			res[i] = bus_alloc_resource(child,
354 						    SYS_RES_DRQ, &i,
355 						    0, ~0, 1, RF_ACTIVE);
356 			if (res[i]) {
357 				result->ic_drqmask[i] = (1 << drq);
358 				break;
359 			}
360 		}
361 
362 		/*
363 		 * If we didn't find a place for drq range i, then
364 		 * give up now.
365 		 */
366 		if (!res[i]) {
367 			success = 0;
368 			break;
369 		}
370 	}
371 
372 	for (i = 0; i < ISA_NDRQ; i++) {
373 		if (res[i])
374 			bus_release_resource(child, SYS_RES_DRQ,
375 					     i, res[i]);
376 	}
377 
378 	return success;
379 }
380 
381 /*
382  * Attempt to find a working set of resources for a device. Return
383  * non-zero if a working configuration is found.
384  */
385 static int
386 isa_assign_resources(device_t child)
387 {
388 	struct isa_device *idev = DEVTOISA(child);
389 	struct isa_config_entry *ice;
390 	struct isa_config config;
391 
392 	bzero(&config, sizeof config);
393 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
394 		if (!isa_find_memory(child, &ice->ice_config, &config))
395 			continue;
396 		if (!isa_find_port(child, &ice->ice_config, &config))
397 			continue;
398 		if (!isa_find_irq(child, &ice->ice_config, &config))
399 			continue;
400 		if (!isa_find_drq(child, &ice->ice_config, &config))
401 			continue;
402 
403 		/*
404 		 * A working configuration was found enable the device
405 		 * with this configuration.
406 		 */
407 		if (idev->id_config_cb) {
408 			idev->id_config_cb(idev->id_config_arg,
409 					   &config, 1);
410 			return 1;
411 		}
412 	}
413 
414 	/*
415 	 * Disable the device.
416 	 */
417 	bzero(&config, sizeof config);
418 	if (idev->id_config_cb)
419 		idev->id_config_cb(idev->id_config_arg, &config, 0);
420 	device_disable(child);
421 
422 	return 0;
423 }
424 
425 /*
426  * Called after other devices have initialised to probe for isa devices.
427  */
428 void
429 isa_probe_children(device_t dev)
430 {
431 	device_t *children;
432 	int nchildren, i;
433 
434 	if (device_get_children(dev, &children, &nchildren))
435 		return;
436 
437 	/*
438 	 * First probe all non-pnp devices so that they claim their
439 	 * resources first.
440 	 */
441 	for (i = 0; i < nchildren; i++) {
442 		device_t child = children[i];
443 		struct isa_device *idev = DEVTOISA(child);
444 
445 		if (TAILQ_FIRST(&idev->id_configs))
446 			continue;
447 
448 		device_probe_and_attach(child);
449 	}
450 
451 	/*
452 	 * Next assign resource to pnp devices and probe them.
453 	 */
454 	for (i = 0; i < nchildren; i++) {
455 		device_t child = children[i];
456 		struct isa_device* idev = DEVTOISA(child);
457 
458 		if (!TAILQ_FIRST(&idev->id_configs))
459 			continue;
460 
461 		if (isa_assign_resources(child)) {
462 			struct resource_list_entry *rle;
463 
464 			device_probe_and_attach(child);
465 
466 			/*
467 			 * Claim any unallocated resources to keep other
468 			 * devices from using them.
469 			 */
470 			SLIST_FOREACH(rle, &idev->id_resources, link) {
471 				if (!rle->res) {
472 					int rid = rle->rid;
473 					resource_list_alloc(dev, child,
474 							    rle->type,
475 							    &rid,
476 							    0, ~0, 1,
477 							    RF_ACTIVE);
478 				}
479 			}
480 		}
481 	}
482 
483 	free(children, M_TEMP);
484 }
485 
486 /*
487  * Add a new child with default ivars.
488  */
489 static device_t
490 isa_add_child(device_t dev, int order, const char *name, int unit)
491 {
492 	struct	isa_device *idev;
493 
494 	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
495 	if (!idev)
496 		return 0;
497 	bzero(idev, sizeof *idev);
498 
499 	resource_list_init(&idev->id_resources);
500 	idev->id_flags = 0;
501 	TAILQ_INIT(&idev->id_configs);
502 
503 	return device_add_child_ordered(dev, order, name, unit, idev);
504 }
505 
506 static void
507 isa_print_resources(struct resource_list *rl, const char *name, int type,
508 		    const char *format)
509 {
510 	struct resource_list_entry *rle;
511 	int printed;
512 	int i;
513 
514 	printed = 0;
515 	for (i = 0; i < 16; i++) {
516 		rle = resource_list_find(rl, type, i);
517 		if (rle) {
518 			if (printed == 0)
519 				printf(" %s ", name);
520 			else if (printed > 0)
521 				printf(",");
522 			printed++;
523 			printf(format, rle->start);
524 			if (rle->count > 1) {
525 				printf("-");
526 				printf(format, rle->start + rle->count - 1);
527 			}
528 		} else if (i > 3) {
529 			/* check the first few regardless */
530 			break;
531 		}
532 	}
533 }
534 
535 static int
536 isa_print_child(device_t bus, device_t dev)
537 {
538 	struct	isa_device *idev = DEVTOISA(dev);
539 	struct resource_list *rl = &idev->id_resources;
540 	int retval = 0;
541 
542 	retval += bus_print_child_header(bus, dev);
543 
544 	if (SLIST_FIRST(rl) || idev->id_flags)
545 		retval += printf(" at");
546 
547 	isa_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
548 	isa_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
549 	isa_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
550 	isa_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
551 	if (idev->id_flags)
552 		retval += printf(" flags %#x", idev->id_flags);
553 
554 	retval += bus_print_child_footer(bus, dev);
555 
556 	return (retval);
557 }
558 
559 static int
560 isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
561 {
562 	struct isa_device* idev = DEVTOISA(dev);
563 	struct resource_list *rl = &idev->id_resources;
564 	struct resource_list_entry *rle;
565 
566 	switch (index) {
567 	case ISA_IVAR_PORT_0:
568 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
569 		if (rle)
570 			*result = rle->start;
571 		else
572 			*result = -1;
573 		break;
574 
575 	case ISA_IVAR_PORT_1:
576 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
577 		if (rle)
578 			*result = rle->start;
579 		else
580 			*result = -1;
581 		break;
582 
583 	case ISA_IVAR_PORTSIZE_0:
584 		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
585 		if (rle)
586 			*result = rle->count;
587 		else
588 			*result = 0;
589 		break;
590 
591 	case ISA_IVAR_PORTSIZE_1:
592 		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
593 		if (rle)
594 			*result = rle->count;
595 		else
596 			*result = 0;
597 		break;
598 
599 	case ISA_IVAR_MADDR_0:
600 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
601 		if (rle)
602 			*result = rle->start;
603 		else
604 			*result = -1;
605 		break;
606 
607 	case ISA_IVAR_MADDR_1:
608 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
609 		if (rle)
610 			*result = rle->start;
611 		else
612 			*result = -1;
613 		break;
614 
615 	case ISA_IVAR_MSIZE_0:
616 		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
617 		if (rle)
618 			*result = rle->count;
619 		else
620 			*result = 0;
621 		break;
622 
623 	case ISA_IVAR_MSIZE_1:
624 		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
625 		if (rle)
626 			*result = rle->count;
627 		else
628 			*result = 0;
629 		break;
630 
631 	case ISA_IVAR_IRQ_0:
632 		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
633 		if (rle)
634 			*result = rle->start;
635 		else
636 			*result = -1;
637 		break;
638 
639 	case ISA_IVAR_IRQ_1:
640 		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
641 		if (rle)
642 			*result = rle->start;
643 		else
644 			*result = -1;
645 		break;
646 
647 	case ISA_IVAR_DRQ_0:
648 		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
649 		if (rle)
650 			*result = rle->start;
651 		else
652 			*result = -1;
653 		break;
654 
655 	case ISA_IVAR_DRQ_1:
656 		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
657 		if (rle)
658 			*result = rle->start;
659 		else
660 			*result = -1;
661 		break;
662 
663 	case ISA_IVAR_FLAGS:
664 		*result = idev->id_flags;
665 		break;
666 
667 	case ISA_IVAR_VENDORID:
668 		*result = idev->id_vendorid;
669 		break;
670 
671 	case ISA_IVAR_SERIAL:
672 		*result = idev->id_serial;
673 		break;
674 
675 	case ISA_IVAR_LOGICALID:
676 		*result = idev->id_logicalid;
677 		break;
678 
679 	case ISA_IVAR_COMPATID:
680 		*result = idev->id_compatid;
681 		break;
682 
683 	default:
684 		return ENOENT;
685 	}
686 
687 	return 0;
688 }
689 
690 static int
691 isa_write_ivar(device_t bus, device_t dev,
692 	       int index, uintptr_t value)
693 {
694 	struct isa_device* idev = DEVTOISA(dev);
695 
696 	switch (index) {
697 	case ISA_IVAR_PORT_0:
698 	case ISA_IVAR_PORT_1:
699 	case ISA_IVAR_PORTSIZE_0:
700 	case ISA_IVAR_PORTSIZE_1:
701 	case ISA_IVAR_MADDR_0:
702 	case ISA_IVAR_MADDR_1:
703 	case ISA_IVAR_MSIZE_0:
704 	case ISA_IVAR_MSIZE_1:
705 	case ISA_IVAR_IRQ_0:
706 	case ISA_IVAR_IRQ_1:
707 	case ISA_IVAR_DRQ_0:
708 	case ISA_IVAR_DRQ_1:
709 		return EINVAL;
710 
711 	case ISA_IVAR_FLAGS:
712 		idev->id_flags = value;
713 		break;
714 
715 	case ISA_IVAR_VENDORID:
716 		idev->id_vendorid = value;
717 		break;
718 
719 	case ISA_IVAR_SERIAL:
720 		idev->id_serial = value;
721 		break;
722 
723 	case ISA_IVAR_LOGICALID:
724 		idev->id_logicalid = value;
725 		break;
726 
727 	case ISA_IVAR_COMPATID:
728 		idev->id_compatid = value;
729 		break;
730 
731 	default:
732 		return (ENOENT);
733 	}
734 
735 	return (0);
736 }
737 
738 /*
739  * Free any resources which the driver missed or which we were holding for
740  * it (see isa_probe_children).
741  */
742 static void
743 isa_child_detached(device_t dev, device_t child)
744 {
745 	struct isa_device* idev = DEVTOISA(child);
746 	struct resource_list_entry *rle;
747 
748 	SLIST_FOREACH(rle, &idev->id_resources, link) {
749 		if (rle->res)
750 			resource_list_release(dev, child,
751 					      rle->type,
752 					      rle->rid,
753 					      rle->res);
754 	}
755 }
756 
757 static int
758 isa_set_resource(device_t dev, device_t child, int type, int rid,
759 		 u_long start, u_long count)
760 {
761 	struct isa_device* idev = DEVTOISA(child);
762 	struct resource_list *rl = &idev->id_resources;
763 
764 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
765 	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
766 		return EINVAL;
767 	if (rid < 0)
768 		return EINVAL;
769 	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
770 		return EINVAL;
771 	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
772 		return EINVAL;
773 	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
774 		return EINVAL;
775 	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
776 		return EINVAL;
777 
778 	resource_list_add(rl, type, rid, start, start + count - 1, count);
779 
780 	return 0;
781 }
782 
783 static int
784 isa_get_resource(device_t dev, device_t child, int type, int rid,
785 		 u_long *startp, u_long *countp)
786 {
787 	struct isa_device* idev = DEVTOISA(child);
788 	struct resource_list *rl = &idev->id_resources;
789 	struct resource_list_entry *rle;
790 
791 	rle = resource_list_find(rl, type, rid);
792 	if (!rle)
793 		return ENOENT;
794 
795 	*startp = rle->start;
796 	*countp = rle->count;
797 
798 	return 0;
799 }
800 
801 static void
802 isa_delete_resource(device_t dev, device_t child, int type, int rid)
803 {
804 	struct isa_device* idev = DEVTOISA(child);
805 	struct resource_list *rl = &idev->id_resources;
806 	resource_list_delete(rl, type, rid);
807 }
808 
809 static int
810 isa_add_config(device_t dev, device_t child,
811 	       int priority, struct isa_config *config)
812 {
813 	struct isa_device* idev = DEVTOISA(child);
814 	struct isa_config_entry *newice, *ice;
815 
816 	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
817 	if (!newice)
818 		return ENOMEM;
819 
820 	newice->ice_priority = priority;
821 	newice->ice_config = *config;
822 
823 	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
824 		if (ice->ice_priority > priority)
825 			break;
826 	}
827 	if (ice)
828 		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
829 	else
830 		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
831 
832 	return 0;
833 }
834 
835 static void
836 isa_set_config_callback(device_t dev, device_t child,
837 			isa_config_cb *fn, void *arg)
838 {
839 	struct isa_device* idev = DEVTOISA(child);
840 
841 	idev->id_config_cb = fn;
842 	idev->id_config_arg = arg;
843 }
844 
845 static int
846 isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
847 {
848 	struct isa_device* idev = DEVTOISA(child);
849 
850 	if (!idev->id_vendorid)
851 		return ENOENT;
852 
853 	while (ids->ip_id) {
854 		/*
855 		 * Really ought to support >1 compat id per device.
856 		 */
857 		if (idev->id_logicalid == ids->ip_id
858 		    || idev->id_compatid == ids->ip_id) {
859 			device_set_desc(child, ids->ip_desc);
860 			return 0;
861 		}
862 		ids++;
863 	}
864 
865 	return ENXIO;
866 }
867 
868 static device_method_t isa_methods[] = {
869 	/* Device interface */
870 	DEVMETHOD(device_probe,		isa_probe),
871 	DEVMETHOD(device_attach,	isa_attach),
872 	DEVMETHOD(device_detach,	bus_generic_detach),
873 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
874 	DEVMETHOD(device_suspend,	bus_generic_suspend),
875 	DEVMETHOD(device_resume,	bus_generic_resume),
876 
877 	/* Bus interface */
878 	DEVMETHOD(bus_add_child,	isa_add_child),
879 	DEVMETHOD(bus_print_child,	isa_print_child),
880 	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
881 	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
882 	DEVMETHOD(bus_child_detached,	isa_child_detached),
883 	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
884 	DEVMETHOD(bus_release_resource,	isa_release_resource),
885 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
886 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
887 	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
888 	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
889 
890 	/* ISA interface */
891 	DEVMETHOD(isa_set_resource,	isa_set_resource),
892 	DEVMETHOD(isa_get_resource,	isa_get_resource),
893 	DEVMETHOD(isa_delete_resource,	isa_delete_resource),
894 	DEVMETHOD(isa_add_config,	isa_add_config),
895 	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
896 	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
897 
898 	{ 0, 0 }
899 };
900 
901 static driver_t isa_driver = {
902 	"isa",
903 	isa_methods,
904 	1,			/* no softc */
905 };
906 
907 /*
908  * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
909  */
910 DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
911 #ifdef __i386__
912 DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
913 #endif
914 
915 /*
916  * A fallback driver for reporting un-matched pnp devices.
917  */
918 
919 static int
920 unknown_probe(device_t dev)
921 {
922 	/*
923 	 * Only match pnp devices.
924 	 */
925 	if (isa_get_vendorid(dev) != 0)
926 		return -100;
927 	return ENXIO;
928 }
929 
930 static int
931 unknown_attach(device_t dev)
932 {
933 	return 0;
934 }
935 
936 static int
937 unknown_detach(device_t dev)
938 {
939 	return 0;
940 }
941 
942 static device_method_t unknown_methods[] = {
943 	/* Device interface */
944 	DEVMETHOD(device_probe,		unknown_probe),
945 	DEVMETHOD(device_attach,	unknown_attach),
946 	DEVMETHOD(device_detach,	unknown_detach),
947 
948 	{ 0, 0 }
949 };
950 
951 static driver_t unknown_driver = {
952 	"unknown",
953 	unknown_methods,
954 	1,			/* no softc */
955 };
956 
957 static devclass_t unknown_devclass;
958 
959 DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);
960