isa_common.c revision 51052
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 51052 1999-09-07 08:42:49Z dfr $
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;
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 */
88static int
89isa_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
96extern device_t isa_bus_device;
97
98static int
99isa_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 */
113static int
114isa_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 */
178static int
179isa_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 */
241static int
242find_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 */
250static int
251find_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 */
266static int
267isa_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 */
326static int
327isa_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 */
385static int
386isa_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 */
428void
429isa_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 */
489static device_t
490isa_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	TAILQ_INIT(&idev->id_configs);
501
502	return device_add_child_ordered(dev, order, name, unit, idev);
503}
504
505static void
506isa_print_resources(struct resource_list *rl, const char *name, int type,
507		    const char *format)
508{
509	struct resource_list_entry *rle;
510	int printed;
511	int i;
512
513	printed = 0;
514	for (i = 0; i < 16; i++) {
515		rle = resource_list_find(rl, type, i);
516		if (rle) {
517			if (printed == 0)
518				printf(" %s ", name);
519			else if (printed > 0)
520				printf(",");
521			printed++;
522			printf(format, rle->start);
523			if (rle->count > 1) {
524				printf("-");
525				printf(format, rle->start + rle->count - 1);
526			}
527		} else if (i > 3) {
528			/* check the first few regardless */
529			break;
530		}
531	}
532}
533
534static int
535isa_print_child(device_t bus, device_t dev)
536{
537	struct	isa_device *idev = DEVTOISA(dev);
538	struct resource_list *rl = &idev->id_resources;
539	int retval = 0;
540
541	retval += bus_print_child_header(bus, dev);
542
543	if (SLIST_FIRST(rl) || device_get_flags(dev))
544		retval += printf(" at");
545
546	isa_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
547	isa_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx");
548	isa_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
549	isa_print_resources(rl, "drq", SYS_RES_DRQ, "%ld");
550	if (device_get_flags(dev))
551		retval += printf(" flags %#x", device_get_flags(dev));
552
553	retval += bus_print_child_footer(bus, dev);
554
555	return (retval);
556}
557
558static int
559isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
560{
561	struct isa_device* idev = DEVTOISA(dev);
562	struct resource_list *rl = &idev->id_resources;
563	struct resource_list_entry *rle;
564
565	switch (index) {
566	case ISA_IVAR_PORT_0:
567		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
568		if (rle)
569			*result = rle->start;
570		else
571			*result = -1;
572		break;
573
574	case ISA_IVAR_PORT_1:
575		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
576		if (rle)
577			*result = rle->start;
578		else
579			*result = -1;
580		break;
581
582	case ISA_IVAR_PORTSIZE_0:
583		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
584		if (rle)
585			*result = rle->count;
586		else
587			*result = 0;
588		break;
589
590	case ISA_IVAR_PORTSIZE_1:
591		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
592		if (rle)
593			*result = rle->count;
594		else
595			*result = 0;
596		break;
597
598	case ISA_IVAR_MADDR_0:
599		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
600		if (rle)
601			*result = rle->start;
602		else
603			*result = -1;
604		break;
605
606	case ISA_IVAR_MADDR_1:
607		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
608		if (rle)
609			*result = rle->start;
610		else
611			*result = -1;
612		break;
613
614	case ISA_IVAR_MSIZE_0:
615		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
616		if (rle)
617			*result = rle->count;
618		else
619			*result = 0;
620		break;
621
622	case ISA_IVAR_MSIZE_1:
623		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
624		if (rle)
625			*result = rle->count;
626		else
627			*result = 0;
628		break;
629
630	case ISA_IVAR_IRQ_0:
631		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
632		if (rle)
633			*result = rle->start;
634		else
635			*result = -1;
636		break;
637
638	case ISA_IVAR_IRQ_1:
639		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
640		if (rle)
641			*result = rle->start;
642		else
643			*result = -1;
644		break;
645
646	case ISA_IVAR_DRQ_0:
647		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
648		if (rle)
649			*result = rle->start;
650		else
651			*result = -1;
652		break;
653
654	case ISA_IVAR_DRQ_1:
655		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
656		if (rle)
657			*result = rle->start;
658		else
659			*result = -1;
660		break;
661
662	case ISA_IVAR_VENDORID:
663		*result = idev->id_vendorid;
664		break;
665
666	case ISA_IVAR_SERIAL:
667		*result = idev->id_serial;
668		break;
669
670	case ISA_IVAR_LOGICALID:
671		*result = idev->id_logicalid;
672		break;
673
674	case ISA_IVAR_COMPATID:
675		*result = idev->id_compatid;
676		break;
677
678	default:
679		return ENOENT;
680	}
681
682	return 0;
683}
684
685static int
686isa_write_ivar(device_t bus, device_t dev,
687	       int index, uintptr_t value)
688{
689	struct isa_device* idev = DEVTOISA(dev);
690
691	switch (index) {
692	case ISA_IVAR_PORT_0:
693	case ISA_IVAR_PORT_1:
694	case ISA_IVAR_PORTSIZE_0:
695	case ISA_IVAR_PORTSIZE_1:
696	case ISA_IVAR_MADDR_0:
697	case ISA_IVAR_MADDR_1:
698	case ISA_IVAR_MSIZE_0:
699	case ISA_IVAR_MSIZE_1:
700	case ISA_IVAR_IRQ_0:
701	case ISA_IVAR_IRQ_1:
702	case ISA_IVAR_DRQ_0:
703	case ISA_IVAR_DRQ_1:
704		return EINVAL;
705
706	case ISA_IVAR_VENDORID:
707		idev->id_vendorid = value;
708		break;
709
710	case ISA_IVAR_SERIAL:
711		idev->id_serial = value;
712		break;
713
714	case ISA_IVAR_LOGICALID:
715		idev->id_logicalid = value;
716		break;
717
718	case ISA_IVAR_COMPATID:
719		idev->id_compatid = value;
720		break;
721
722	default:
723		return (ENOENT);
724	}
725
726	return (0);
727}
728
729/*
730 * Free any resources which the driver missed or which we were holding for
731 * it (see isa_probe_children).
732 */
733static void
734isa_child_detached(device_t dev, device_t child)
735{
736	struct isa_device* idev = DEVTOISA(child);
737	struct resource_list_entry *rle;
738
739	SLIST_FOREACH(rle, &idev->id_resources, link) {
740		if (rle->res)
741			resource_list_release(dev, child,
742					      rle->type,
743					      rle->rid,
744					      rle->res);
745	}
746}
747
748static int
749isa_set_resource(device_t dev, device_t child, int type, int rid,
750		 u_long start, u_long count)
751{
752	struct isa_device* idev = DEVTOISA(child);
753	struct resource_list *rl = &idev->id_resources;
754
755	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
756	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
757		return EINVAL;
758	if (rid < 0)
759		return EINVAL;
760	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
761		return EINVAL;
762	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
763		return EINVAL;
764	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
765		return EINVAL;
766	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
767		return EINVAL;
768
769	resource_list_add(rl, type, rid, start, start + count - 1, count);
770
771	return 0;
772}
773
774static int
775isa_get_resource(device_t dev, device_t child, int type, int rid,
776		 u_long *startp, u_long *countp)
777{
778	struct isa_device* idev = DEVTOISA(child);
779	struct resource_list *rl = &idev->id_resources;
780	struct resource_list_entry *rle;
781
782	rle = resource_list_find(rl, type, rid);
783	if (!rle)
784		return ENOENT;
785
786	*startp = rle->start;
787	*countp = rle->count;
788
789	return 0;
790}
791
792static void
793isa_delete_resource(device_t dev, device_t child, int type, int rid)
794{
795	struct isa_device* idev = DEVTOISA(child);
796	struct resource_list *rl = &idev->id_resources;
797	resource_list_delete(rl, type, rid);
798}
799
800static int
801isa_add_config(device_t dev, device_t child,
802	       int priority, struct isa_config *config)
803{
804	struct isa_device* idev = DEVTOISA(child);
805	struct isa_config_entry *newice, *ice;
806
807	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
808	if (!newice)
809		return ENOMEM;
810
811	newice->ice_priority = priority;
812	newice->ice_config = *config;
813
814	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
815		if (ice->ice_priority > priority)
816			break;
817	}
818	if (ice)
819		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
820	else
821		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);
822
823	return 0;
824}
825
826static void
827isa_set_config_callback(device_t dev, device_t child,
828			isa_config_cb *fn, void *arg)
829{
830	struct isa_device* idev = DEVTOISA(child);
831
832	idev->id_config_cb = fn;
833	idev->id_config_arg = arg;
834}
835
836static int
837isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
838{
839	struct isa_device* idev = DEVTOISA(child);
840
841	if (!idev->id_vendorid)
842		return ENOENT;
843
844	while (ids->ip_id) {
845		/*
846		 * Really ought to support >1 compat id per device.
847		 */
848		if (idev->id_logicalid == ids->ip_id
849		    || idev->id_compatid == ids->ip_id) {
850			if (ids->ip_desc)
851				device_set_desc(child, ids->ip_desc);
852			return 0;
853		}
854		ids++;
855	}
856
857	return ENXIO;
858}
859
860static device_method_t isa_methods[] = {
861	/* Device interface */
862	DEVMETHOD(device_probe,		isa_probe),
863	DEVMETHOD(device_attach,	isa_attach),
864	DEVMETHOD(device_detach,	bus_generic_detach),
865	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
866	DEVMETHOD(device_suspend,	bus_generic_suspend),
867	DEVMETHOD(device_resume,	bus_generic_resume),
868
869	/* Bus interface */
870	DEVMETHOD(bus_add_child,	isa_add_child),
871	DEVMETHOD(bus_print_child,	isa_print_child),
872	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
873	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
874	DEVMETHOD(bus_child_detached,	isa_child_detached),
875	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
876	DEVMETHOD(bus_release_resource,	isa_release_resource),
877	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
878	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
879	DEVMETHOD(bus_setup_intr,	isa_setup_intr),
880	DEVMETHOD(bus_teardown_intr,	isa_teardown_intr),
881
882	/* ISA interface */
883	DEVMETHOD(isa_set_resource,	isa_set_resource),
884	DEVMETHOD(isa_get_resource,	isa_get_resource),
885	DEVMETHOD(isa_delete_resource,	isa_delete_resource),
886	DEVMETHOD(isa_add_config,	isa_add_config),
887	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
888	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),
889
890	{ 0, 0 }
891};
892
893static driver_t isa_driver = {
894	"isa",
895	isa_methods,
896	1,			/* no softc */
897};
898
899/*
900 * ISA can be attached to a PCI-ISA bridge or directly to the nexus.
901 */
902DRIVER_MODULE(isa, isab, isa_driver, isa_devclass, 0, 0);
903#ifdef __i386__
904DRIVER_MODULE(isa, nexus, isa_driver, isa_devclass, 0, 0);
905#endif
906
907/*
908 * A fallback driver for reporting un-matched pnp devices.
909 */
910
911static int
912unknown_probe(device_t dev)
913{
914	/*
915	 * Only match pnp devices.
916	 */
917	if (isa_get_vendorid(dev) != 0)
918		return -100;
919	return ENXIO;
920}
921
922static int
923unknown_attach(device_t dev)
924{
925	return 0;
926}
927
928static int
929unknown_detach(device_t dev)
930{
931	return 0;
932}
933
934static device_method_t unknown_methods[] = {
935	/* Device interface */
936	DEVMETHOD(device_probe,		unknown_probe),
937	DEVMETHOD(device_attach,	unknown_attach),
938	DEVMETHOD(device_detach,	unknown_detach),
939
940	{ 0, 0 }
941};
942
943static driver_t unknown_driver = {
944	"unknown",
945	unknown_methods,
946	1,			/* no softc */
947};
948
949static devclass_t unknown_devclass;
950
951DRIVER_MODULE(unknown, isa, unknown_driver, unknown_devclass, 0, 0);
952