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