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