isa_common.c revision 53461
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: head/sys/isa/isa_common.c 53461 1999-11-20 14:56:55Z peter $
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
79MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");
80
81static devclass_t isa_devclass;
82static int isa_running;
83
84/*
85 * At 'probe' time, we add all the devices which we know about to the
86 * bus.  The generic attach routine will probe and attach them if they
87 * are alive.
88 */
89static int
90isa_probe(device_t dev)
91{
92	device_set_desc(dev, "ISA bus");
93	isa_init();		/* Allow machdep code to initialise */
94	return 0;
95}
96
97extern device_t isa_bus_device;
98
99static int
100isa_attach(device_t dev)
101{
102	/*
103	 * Arrange for isa_probe_children(dev) to be called later. XXX
104	 */
105	isa_bus_device = dev;
106	return 0;
107}
108
109/*
110 * Find a working set of memory regions for a child using the ranges
111 * in *config  and return the regions in *result. Returns non-zero if
112 * a set of ranges was found.
113 */
114static int
115isa_find_memory(device_t child,
116		struct isa_config *config,
117		struct isa_config *result)
118{
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		bus_delete_resource(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			bus_set_resource(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 */
178static int
179isa_find_port(device_t child,
180	      struct isa_config *config,
181	      struct isa_config *result)
182{
183	int success, i;
184	struct resource *res[ISA_NPORT];
185
186	/*
187	 * First clear out any existing resource definitions.
188	 */
189	for (i = 0; i < ISA_NPORT; i++) {
190		bus_delete_resource(child, SYS_RES_IOPORT, i);
191		res[i] = NULL;
192	}
193
194	success = 1;
195	result->ic_nport = config->ic_nport;
196	for (i = 0; i < config->ic_nport; i++) {
197		u_int32_t start, end, size, align;
198		for (start = config->ic_port[i].ir_start,
199			     end = config->ic_port[i].ir_end,
200			     size = config->ic_port[i].ir_size,
201			     align = config->ic_port[i].ir_align;
202		     start + size - 1 <= end;
203		     start += align) {
204			bus_set_resource(child, SYS_RES_IOPORT, i,
205					 start, size);
206			res[i] = bus_alloc_resource(child,
207						    SYS_RES_IOPORT, &i,
208						    0, ~0, 1, RF_ACTIVE);
209			if (res[i]) {
210				result->ic_port[i].ir_start = start;
211				result->ic_port[i].ir_end = start + size - 1;
212				result->ic_port[i].ir_size = size;
213				result->ic_port[i].ir_align = align;
214				break;
215			}
216		}
217
218		/*
219		 * If we didn't find a place for port range i, then
220		 * give up now.
221		 */
222		if (!res[i]) {
223			success = 0;
224			break;
225		}
226	}
227
228	for (i = 0; i < ISA_NPORT; i++) {
229		if (res[i])
230			bus_release_resource(child, SYS_RES_IOPORT,
231					     i, res[i]);
232	}
233
234	return success;
235}
236
237/*
238 * Return the index of the first bit in the mask (or -1 if mask is empty.
239 */
240static int
241find_first_bit(u_int32_t mask)
242{
243	return ffs(mask) - 1;
244}
245
246/*
247 * Return the index of the next bit in the mask, or -1 if there are no more.
248 */
249static int
250find_next_bit(u_int32_t mask, int bit)
251{
252	bit++;
253	while (bit < 32 && !(mask & (1 << bit)))
254		bit++;
255	if (bit != 32)
256		return bit;
257	return -1;
258}
259
260/*
261 * Find a working set of irqs for a child using the masks in *config
262 * and return the regions in *result. Returns non-zero if a set of
263 * irqs was found.
264 */
265static int
266isa_find_irq(device_t child,
267	     struct isa_config *config,
268	     struct isa_config *result)
269{
270	int success, i;
271	struct resource *res[ISA_NIRQ];
272
273	/*
274	 * First clear out any existing resource definitions.
275	 */
276	for (i = 0; i < ISA_NIRQ; i++) {
277		bus_delete_resource(child, SYS_RES_IRQ, i);
278		res[i] = NULL;
279	}
280
281	success = 1;
282	result->ic_nirq = config->ic_nirq;
283	for (i = 0; i < config->ic_nirq; i++) {
284		u_int32_t mask = config->ic_irqmask[i];
285		int irq;
286		for (irq = find_first_bit(mask);
287		     irq != -1;
288		     irq = find_next_bit(mask, irq)) {
289			bus_set_resource(child, SYS_RES_IRQ, i,
290					 irq, 1);
291			res[i] = bus_alloc_resource(child,
292						    SYS_RES_IRQ, &i,
293						    0, ~0, 1, RF_ACTIVE);
294			if (res[i]) {
295				result->ic_irqmask[i] = (1 << irq);
296				break;
297			}
298		}
299
300		/*
301		 * If we didn't find a place for irq range i, then
302		 * give up now.
303		 */
304		if (!res[i]) {
305			success = 0;
306			break;
307		}
308	}
309
310	for (i = 0; i < ISA_NIRQ; i++) {
311		if (res[i])
312			bus_release_resource(child, SYS_RES_IRQ,
313					     i, res[i]);
314	}
315
316	return success;
317}
318
319/*
320 * Find a working set of drqs for a child using the masks in *config
321 * and return the regions in *result. Returns non-zero if a set of
322 * drqs was found.
323 */
324static int
325isa_find_drq(device_t child,
326	     struct isa_config *config,
327	     struct isa_config *result)
328{
329	int success, i;
330	struct resource *res[ISA_NDRQ];
331
332	/*
333	 * First clear out any existing resource definitions.
334	 */
335	for (i = 0; i < ISA_NDRQ; i++) {
336		bus_delete_resource(child, SYS_RES_DRQ, i);
337		res[i] = NULL;
338	}
339
340	success = 1;
341	result->ic_ndrq = config->ic_ndrq;
342	for (i = 0; i < config->ic_ndrq; i++) {
343		u_int32_t mask = config->ic_drqmask[i];
344		int drq;
345		for (drq = find_first_bit(mask);
346		     drq != -1;
347		     drq = find_next_bit(mask, drq)) {
348			bus_set_resource(child, SYS_RES_DRQ, i,
349					 drq, 1);
350			res[i] = bus_alloc_resource(child,
351						    SYS_RES_DRQ, &i,
352						    0, ~0, 1, RF_ACTIVE);
353			if (res[i]) {
354				result->ic_drqmask[i] = (1 << drq);
355				break;
356			}
357		}
358
359		/*
360		 * If we didn't find a place for drq range i, then
361		 * give up now.
362		 */
363		if (!res[i]) {
364			success = 0;
365			break;
366		}
367	}
368
369	for (i = 0; i < ISA_NDRQ; i++) {
370		if (res[i])
371			bus_release_resource(child, SYS_RES_DRQ,
372					     i, res[i]);
373	}
374
375	return success;
376}
377
378/*
379 * Attempt to find a working set of resources for a device. Return
380 * non-zero if a working configuration is found.
381 */
382static int
383isa_assign_resources(device_t child)
384{
385	struct isa_device *idev = DEVTOISA(child);
386	struct isa_config_entry *ice;
387	struct isa_config config;
388
389	bzero(&config, sizeof config);
390	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
391		if (!isa_find_memory(child, &ice->ice_config, &config))
392			continue;
393		if (!isa_find_port(child, &ice->ice_config, &config))
394			continue;
395		if (!isa_find_irq(child, &ice->ice_config, &config))
396			continue;
397		if (!isa_find_drq(child, &ice->ice_config, &config))
398			continue;
399
400		/*
401		 * A working configuration was found enable the device
402		 * with this configuration.
403		 */
404		if (idev->id_config_cb) {
405			idev->id_config_cb(idev->id_config_arg,
406					   &config, 1);
407			return 1;
408		}
409	}
410
411	/*
412	 * Disable the device.
413	 */
414	if (device_get_desc(child))
415	    device_printf(child, "<%s> can't assign resources\n",
416			  device_get_desc(child));
417	else
418	    device_printf(child, "can't assign resources\n");
419	bzero(&config, sizeof config);
420	if (idev->id_config_cb)
421		idev->id_config_cb(idev->id_config_arg, &config, 0);
422	device_disable(child);
423
424	return 0;
425}
426
427/*
428 * Called after other devices have initialised to probe for isa devices.
429 */
430void
431isa_probe_children(device_t dev)
432{
433	device_t *children;
434	int nchildren, i;
435
436	/*
437	 * Create all the children by calling driver's identify methods.
438	 */
439	bus_generic_probe(dev);
440
441	if (device_get_children(dev, &children, &nchildren))
442		return;
443
444	/*
445	 * First disable all pnp devices so that they don't get
446	 * matched by legacy probes.
447	 */
448	if (bootverbose)
449		printf("isa_probe_children: disabling PnP devices\n");
450	for (i = 0; i < nchildren; i++) {
451		device_t child = children[i];
452		struct isa_device *idev = DEVTOISA(child);
453		struct isa_config config;
454
455		bzero(&config, sizeof config);
456		if (idev->id_config_cb)
457			idev->id_config_cb(idev->id_config_arg, &config, 0);
458	}
459
460	/*
461	 * Next probe all non-pnp devices so that they claim their
462	 * resources first.
463	 */
464	if (bootverbose)
465		printf("isa_probe_children: probing non-PnP devices\n");
466	for (i = 0; i < nchildren; i++) {
467		device_t child = children[i];
468		struct isa_device *idev = DEVTOISA(child);
469
470		if (TAILQ_FIRST(&idev->id_configs))
471			continue;
472
473		device_probe_and_attach(child);
474	}
475
476	/*
477	 * Finally assign resource to pnp devices and probe them.
478	 */
479	if (bootverbose)
480		printf("isa_probe_children: probing PnP devices\n");
481	for (i = 0; i < nchildren; i++) {
482		device_t child = children[i];
483		struct isa_device* idev = DEVTOISA(child);
484
485		if (!TAILQ_FIRST(&idev->id_configs))
486			continue;
487
488		if (isa_assign_resources(child)) {
489			struct resource_list *rl = &idev->id_resources;
490			struct resource_list_entry *rle;
491
492			device_probe_and_attach(child);
493
494			/*
495			 * Claim any unallocated resources to keep other
496			 * devices from using them.
497			 */
498			SLIST_FOREACH(rle, rl, link) {
499				if (!rle->res) {
500					int rid = rle->rid;
501					resource_list_alloc(rl, dev, child,
502							    rle->type,
503							    &rid,
504							    0, ~0, 1,
505							    RF_ACTIVE);
506				}
507			}
508		}
509	}
510
511	free(children, M_TEMP);
512
513	isa_running = 1;
514}
515
516/*
517 * Add a new child with default ivars.
518 */
519static device_t
520isa_add_child(device_t dev, int order, const char *name, int unit)
521{
522	struct	isa_device *idev;
523
524	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT);
525	if (!idev)
526		return 0;
527	bzero(idev, sizeof *idev);
528
529	resource_list_init(&idev->id_resources);
530	TAILQ_INIT(&idev->id_configs);
531
532	return device_add_child_ordered(dev, order, name, unit, idev);
533}
534
535static void
536isa_print_resources(struct resource_list *rl, const char *name, int type,
537		    int count, const char *format)
538{
539	struct resource_list_entry *rle;
540	int printed;
541	int i;
542
543	printed = 0;
544	for (i = 0; i < count; i++) {
545		rle = resource_list_find(rl, type, i);
546		if (rle) {
547			if (printed == 0)
548				printf(" %s ", name);
549			else if (printed > 0)
550				printf(",");
551			printed++;
552			printf(format, rle->start);
553			if (rle->count > 1) {
554				printf("-");
555				printf(format, rle->start + rle->count - 1);
556			}
557		} else if (i > 3) {
558			/* check the first few regardless */
559			break;
560		}
561	}
562}
563
564static int
565isa_print_child(device_t bus, device_t dev)
566{
567	struct	isa_device *idev = DEVTOISA(dev);
568	struct resource_list *rl = &idev->id_resources;
569	int retval = 0;
570
571	retval += bus_print_child_header(bus, dev);
572
573	if (SLIST_FIRST(rl) || device_get_flags(dev))
574		retval += printf(" at");
575
576	isa_print_resources(rl, "port", SYS_RES_IOPORT, ISA_NPORT, "%#lx");
577	isa_print_resources(rl, "iomem", SYS_RES_MEMORY, ISA_NMEM, "%#lx");
578	isa_print_resources(rl, "irq", SYS_RES_IRQ, ISA_NIRQ, "%ld");
579	isa_print_resources(rl, "drq", SYS_RES_DRQ, ISA_NDRQ, "%ld");
580	if (device_get_flags(dev))
581		retval += printf(" flags %#x", device_get_flags(dev));
582
583	retval += bus_print_child_footer(bus, dev);
584
585	return (retval);
586}
587
588static int
589isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
590{
591	struct isa_device* idev = DEVTOISA(dev);
592	struct resource_list *rl = &idev->id_resources;
593	struct resource_list_entry *rle;
594
595	switch (index) {
596	case ISA_IVAR_PORT_0:
597		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
598		if (rle)
599			*result = rle->start;
600		else
601			*result = -1;
602		break;
603
604	case ISA_IVAR_PORT_1:
605		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
606		if (rle)
607			*result = rle->start;
608		else
609			*result = -1;
610		break;
611
612	case ISA_IVAR_PORTSIZE_0:
613		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
614		if (rle)
615			*result = rle->count;
616		else
617			*result = 0;
618		break;
619
620	case ISA_IVAR_PORTSIZE_1:
621		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
622		if (rle)
623			*result = rle->count;
624		else
625			*result = 0;
626		break;
627
628	case ISA_IVAR_MADDR_0:
629		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
630		if (rle)
631			*result = rle->start;
632		else
633			*result = -1;
634		break;
635
636	case ISA_IVAR_MADDR_1:
637		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
638		if (rle)
639			*result = rle->start;
640		else
641			*result = -1;
642		break;
643
644	case ISA_IVAR_MSIZE_0:
645		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
646		if (rle)
647			*result = rle->count;
648		else
649			*result = 0;
650		break;
651
652	case ISA_IVAR_MSIZE_1:
653		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
654		if (rle)
655			*result = rle->count;
656		else
657			*result = 0;
658		break;
659
660	case ISA_IVAR_IRQ_0:
661		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
662		if (rle)
663			*result = rle->start;
664		else
665			*result = -1;
666		break;
667
668	case ISA_IVAR_IRQ_1:
669		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
670		if (rle)
671			*result = rle->start;
672		else
673			*result = -1;
674		break;
675
676	case ISA_IVAR_DRQ_0:
677		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
678		if (rle)
679			*result = rle->start;
680		else
681			*result = -1;
682		break;
683
684	case ISA_IVAR_DRQ_1:
685		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
686		if (rle)
687			*result = rle->start;
688		else
689			*result = -1;
690		break;
691
692	case ISA_IVAR_VENDORID:
693		*result = idev->id_vendorid;
694		break;
695
696	case ISA_IVAR_SERIAL:
697		*result = idev->id_serial;
698		break;
699
700	case ISA_IVAR_LOGICALID:
701		*result = idev->id_logicalid;
702		break;
703
704	case ISA_IVAR_COMPATID:
705		*result = idev->id_compatid;
706		break;
707
708	default:
709		return ENOENT;
710	}
711
712	return 0;
713}
714
715static int
716isa_write_ivar(device_t bus, device_t dev,
717	       int index, uintptr_t value)
718{
719	struct isa_device* idev = DEVTOISA(dev);
720
721	switch (index) {
722	case ISA_IVAR_PORT_0:
723	case ISA_IVAR_PORT_1:
724	case ISA_IVAR_PORTSIZE_0:
725	case ISA_IVAR_PORTSIZE_1:
726	case ISA_IVAR_MADDR_0:
727	case ISA_IVAR_MADDR_1:
728	case ISA_IVAR_MSIZE_0:
729	case ISA_IVAR_MSIZE_1:
730	case ISA_IVAR_IRQ_0:
731	case ISA_IVAR_IRQ_1:
732	case ISA_IVAR_DRQ_0:
733	case ISA_IVAR_DRQ_1:
734		return EINVAL;
735
736	case ISA_IVAR_VENDORID:
737		idev->id_vendorid = value;
738		break;
739
740	case ISA_IVAR_SERIAL:
741		idev->id_serial = value;
742		break;
743
744	case ISA_IVAR_LOGICALID:
745		idev->id_logicalid = value;
746		break;
747
748	case ISA_IVAR_COMPATID:
749		idev->id_compatid = value;
750		break;
751
752	default:
753		return (ENOENT);
754	}
755
756	return (0);
757}
758
759/*
760 * Free any resources which the driver missed or which we were holding for
761 * it (see isa_probe_children).
762 */
763static void
764isa_child_detached(device_t dev, device_t child)
765{
766	struct isa_device* idev = DEVTOISA(child);
767	struct resource_list *rl = &idev->id_resources;
768	struct resource_list_entry *rle;
769
770	SLIST_FOREACH(rle, &idev->id_resources, link) {
771		if (rle->res)
772			resource_list_release(rl, dev, child,
773					      rle->type,
774					      rle->rid,
775					      rle->res);
776	}
777}
778
779static void
780isa_driver_added(device_t dev, driver_t *driver)
781{
782	device_t *children;
783	int nchildren, i;
784
785	/*
786	 * Don't do anything if drivers are dynamically
787	 * added during autoconfiguration (cf. ymf724).
788	 * since that would end up calling identify
789	 * twice.
790	 */
791	if (!isa_running)
792		return;
793
794	DEVICE_IDENTIFY(driver, dev);
795	if (device_get_children(dev, &children, &nchildren))
796		return;
797
798	for (i = 0; i < nchildren; i++) {
799		device_t child = children[i];
800		struct isa_device *idev = DEVTOISA(child);
801		struct resource_list *rl = &idev->id_resources;
802		struct resource_list_entry *rle;
803
804		if (device_get_state(child) != DS_NOTPRESENT)
805			continue;
806
807		if (TAILQ_FIRST(&idev->id_configs))
808			if (!isa_assign_resources(child))
809				continue;
810
811		device_probe_and_attach(child);
812
813		if (TAILQ_FIRST(&idev->id_configs)) {
814			/*
815			 * Claim any unallocated resources to keep other
816			 * devices from using them.
817			 */
818			SLIST_FOREACH(rle, rl, link) {
819				if (!rle->res) {
820					int rid = rle->rid;
821					resource_list_alloc(rl, dev, child,
822							    rle->type,
823							    &rid,
824							    0, ~0, 1,
825							    RF_ACTIVE);
826				}
827			}
828		}
829	}
830
831	free(children, M_TEMP);
832}
833
834static int
835isa_set_resource(device_t dev, device_t child, int type, int rid,
836		 u_long start, u_long count)
837{
838	struct isa_device* idev = DEVTOISA(child);
839	struct resource_list *rl = &idev->id_resources;
840
841	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
842	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
843		return EINVAL;
844	if (rid < 0)
845		return EINVAL;
846	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
847		return EINVAL;
848	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
849		return EINVAL;
850	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
851		return EINVAL;
852	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
853		return EINVAL;
854
855	resource_list_add(rl, type, rid, start, start + count - 1, count);
856
857	return 0;
858}
859
860static int
861isa_get_resource(device_t dev, device_t child, int type, int rid,
862		 u_long *startp, u_long *countp)
863{
864	struct isa_device* idev = DEVTOISA(child);
865	struct resource_list *rl = &idev->id_resources;
866	struct resource_list_entry *rle;
867
868	rle = resource_list_find(rl, type, rid);
869	if (!rle)
870		return ENOENT;
871
872	if (startp)
873		*startp = rle->start;
874	if (countp)
875		*countp = rle->count;
876
877	return 0;
878}
879
880static void
881isa_delete_resource(device_t dev, device_t child, int type, int rid)
882{
883	struct isa_device* idev = DEVTOISA(child);
884	struct resource_list *rl = &idev->id_resources;
885	resource_list_delete(rl, type, rid);
886}
887
888static int
889isa_add_config(device_t dev, device_t child,
890	       int priority, struct isa_config *config)
891{
892	struct isa_device* idev = DEVTOISA(child);
893	struct isa_config_entry *newice, *ice;
894
895	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
896	if (!newice)
897		return ENOMEM;
898
899	newice->ice_priority = priority;
900	newice->ice_config = *config;
901
902	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
903		if (ice->ice_priority > priority)
904			break;
905	}
906	if (ice)
907		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
908	else
909		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
910
911	return 0;
912}
913
914static void
915isa_set_config_callback(device_t dev, device_t child,
916			isa_config_cb *fn, void *arg)
917{
918	struct isa_device* idev = DEVTOISA(child);
919
920	idev->id_config_cb = fn;
921	idev->id_config_arg = arg;
922}
923
924static int
925isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
926{
927	struct isa_device* idev = DEVTOISA(child);
928
929	if (!idev->id_vendorid)
930		return ENOENT;
931
932	while (ids->ip_id) {
933		/*
934		 * Really ought to support >1 compat id per device.
935		 */
936		if (idev->id_logicalid == ids->ip_id
937		    || idev->id_compatid == ids->ip_id) {
938			if (ids->ip_desc)
939				device_set_desc(child, ids->ip_desc);
940			return 0;
941		}
942		ids++;
943	}
944
945	return ENXIO;
946}
947
948static device_method_t isa_methods[] = {
949	/* Device interface */
950	DEVMETHOD(device_probe,		isa_probe),
951	DEVMETHOD(device_attach,	isa_attach),
952	DEVMETHOD(device_detach,	bus_generic_detach),
953	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
954	DEVMETHOD(device_suspend,	bus_generic_suspend),
955	DEVMETHOD(device_resume,	bus_generic_resume),
956
957	/* Bus interface */
958	DEVMETHOD(bus_add_child,	isa_add_child),
959	DEVMETHOD(bus_print_child,	isa_print_child),
960	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
961	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
962	DEVMETHOD(bus_child_detached,	isa_child_detached),
963	DEVMETHOD(bus_driver_added,	isa_driver_added),
964	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
965	DEVMETHOD(bus_release_resource,	isa_release_resource),
966	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
967	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
968	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
969	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
970	DEVMETHOD(bus_set_resource,	isa_set_resource),
971	DEVMETHOD(bus_get_resource,	isa_get_resource),
972	DEVMETHOD(bus_delete_resource,	isa_delete_resource),
973
974	/* ISA interface */
975	DEVMETHOD(isa_add_config,	isa_add_config),
976	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
977	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
978
979	{ 0, 0 }
980};
981
982static driver_t isa_driver = {
983	"isa",
984	isa_methods,
985	1,			/* no softc */
986};
987
988/*
989 * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
990 */
991DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
992#ifdef __i386__
993DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
994#endif
995
996/*
997 * A fallback driver for reporting un-matched pnp devices.
998 */
999
1000static int
1001unknown_probe(device_t dev)
1002{
1003	/*
1004	 * Only match pnp devices.
1005	 */
1006	if (isa_get_vendorid(dev) != 0)
1007		return -100;
1008	return ENXIO;
1009}
1010
1011static int
1012unknown_attach(device_t dev)
1013{
1014	return 0;
1015}
1016
1017static int
1018unknown_detach(device_t dev)
1019{
1020	return 0;
1021}
1022
1023static device_method_t unknown_methods[] = {
1024	/* Device interface */
1025	DEVMETHOD(device_probe,		unknown_probe),
1026	DEVMETHOD(device_attach,	unknown_attach),
1027	DEVMETHOD(device_detach,	unknown_detach),
1028
1029	{ 0, 0 }
1030};
1031
1032static driver_t unknown_driver = {
1033	"unknown",
1034	unknown_methods,
1035	1,			/* no softc */
1036};
1037
1038static devclass_t unknown_devclass;
1039
1040DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);
1041