aboutsummaryrefslogtreecommitdiff
path: root/sys/isa/isa_common.c
blob: ec3d01d547ed968d4dbad0d0d282bdf6e3c1b561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND MIT
 *
 * Copyright (c) 1999 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * Modifications for Intel architecture by Garrett A. Wollman.
 * Copyright 1998 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that both the above copyright notice and this
 * permission notice appear in all copies, that both the above
 * copyright notice and this permission notice appear in all
 * supporting documentation, and that the name of M.I.T. not be used
 * in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  M.I.T. makes
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 * 
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Parts of the ISA bus implementation common to all architectures.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_isa.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>

#include <machine/resource.h>

#include <isa/isavar.h>
#include <isa/isa_common.h>

static int	isa_print_child(device_t bus, device_t dev);

static MALLOC_DEFINE(M_ISADEV, "isadev", "ISA device");

static int isa_running;

/*
 * At 'probe' time, we add all the devices which we know about to the
 * bus.  The generic attach routine will probe and attach them if they
 * are alive.
 */
static int
isa_probe(device_t dev)
{
	device_set_desc(dev, "ISA bus");
	isa_init(dev);		/* Allow machdep code to initialise */
	return (0);
}

extern device_t isa_bus_device;

static int
isa_attach(device_t dev)
{
	/*
	 * Arrange for isa_probe_children(dev) to be called later. XXX
	 */
	isa_bus_device = dev;
	return (0);
}

/*
 * Find a working set of memory regions for a child using the ranges
 * in *config  and return the regions in *result. Returns non-zero if
 * a set of ranges was found.
 */
static int
isa_find_memory(device_t child, struct isa_config *config,
    struct isa_config *result)
{
	int success, i;
	struct resource *res[ISA_NMEM];

	/*
	 * First clear out any existing resource definitions.
	 */
	for (i = 0; i < ISA_NMEM; i++) {
		bus_delete_resource(child, SYS_RES_MEMORY, i);
		res[i] = NULL;
	}

	success = 1;
	result->ic_nmem = config->ic_nmem;
	for (i = 0; i < config->ic_nmem; i++) {
		uint32_t start, end, size, align;

		size = config->ic_mem[i].ir_size;

		/* the PnP device may have a null resource as filler */
		if (size == 0) {
			result->ic_mem[i].ir_start = 0;
			result->ic_mem[i].ir_end = 0;
			result->ic_mem[i].ir_size = 0;
			result->ic_mem[i].ir_align = 0;
			continue;
		}

		for (start = config->ic_mem[i].ir_start,
			     end = config->ic_mem[i].ir_end,
			     align = config->ic_mem[i].ir_align;
		     start + size - 1 <= end && start + size > start;
		     start += MAX(align, 1)) {
			bus_set_resource(child, SYS_RES_MEMORY, i,
					 start, size);
			res[i] = bus_alloc_resource_any(child,
			    SYS_RES_MEMORY, &i,
			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
			if (res[i]) {
				result->ic_mem[i].ir_start = start;
				result->ic_mem[i].ir_end = start + size - 1;
				result->ic_mem[i].ir_size = size;
				result->ic_mem[i].ir_align = align;
				break;
			}
		}

		/*
		 * If we didn't find a place for memory range i, then 
		 * give up now.
		 */
		if (!res[i]) {
			success = 0;
			break;
		}
	}

	for (i = 0; i < ISA_NMEM; i++) {
		if (res[i])
			bus_release_resource(child, SYS_RES_MEMORY,
					     i, res[i]);
	}

	return (success);
}

/*
 * Find a working set of port regions for a child using the ranges
 * in *config  and return the regions in *result. Returns non-zero if
 * a set of ranges was found.
 */
static int
isa_find_port(device_t child, struct isa_config *config,
    struct isa_config *result)
{
	int success, i;
	struct resource *res[ISA_NPORT];

	/*
	 * First clear out any existing resource definitions.
	 */
	for (i = 0; i < ISA_NPORT; i++) {
		bus_delete_resource(child, SYS_RES_IOPORT, i);
		res[i] = NULL;
	}

	success = 1;
	result->ic_nport = config->ic_nport;
	for (i = 0; i < config->ic_nport; i++) {
		uint32_t start, end, size, align;

		size = config->ic_port[i].ir_size;

		/* the PnP device may have a null resource as filler */
		if (size == 0) {
			result->ic_port[i].ir_start = 0;
			result->ic_port[i].ir_end = 0;
			result->ic_port[i].ir_size = 0;
			result->ic_port[i].ir_align = 0;
			continue;
		}

		for (start = config->ic_port[i].ir_start,
			     end = config->ic_port[i].ir_end,
			     align = config->ic_port[i].ir_align;
		     start + size - 1 <= end;
		     start += align) {
			bus_set_resource(child, SYS_RES_IOPORT, i,
					 start, size);
			res[i] = bus_alloc_resource_any(child,
			    SYS_RES_IOPORT, &i,
			    rman_make_alignment_flags(align) /* !RF_ACTIVE */);
			if (res[i]) {
				result->ic_port[i].ir_start = start;
				result->ic_port[i].ir_end = start + size - 1;
				result->ic_port[i].ir_size = size;
				result->ic_port[i].ir_align = align;
				break;
			}
		}

		/*
		 * If we didn't find a place for port range i, then 
		 * give up now.
		 */
		if (!res[i]) {
			success = 0;
			break;
		}
	}

	for (i = 0; i < ISA_NPORT; i++) {
		if (res[i])
			bus_release_resource(child, SYS_RES_IOPORT,
					     i, res[i]);
	}

	return success;
}

/*
 * Return the index of the first bit in the mask (or -1 if mask is empty.
 */
static int
find_first_bit(uint32_t mask)
{
	return (ffs(mask) - 1);
}

/*
 * Return the index of the next bit in the mask, or -1 if there are no more.
 */
static int
find_next_bit(uint32_t mask, int bit)
{
	bit++;
	while (bit < 32 && !(mask & (1 << bit)))
		bit++;
	if (bit != 32)
		return (bit);
	return (-1);
}

/*
 * Find a working set of irqs for a child using the masks in *config
 * and return the regions in *result. Returns non-zero if a set of
 * irqs was found.
 */
static int
isa_find_irq(device_t child, struct isa_config *config,
    struct isa_config *result)
{
	int success, i;
	struct resource *res[ISA_NIRQ];

	/*
	 * First clear out any existing resource definitions.
	 */
	for (i = 0; i < ISA_NIRQ; i++) {
		bus_delete_resource(child, SYS_RES_IRQ, i);
		res[i] = NULL;
	}

	success = 1;
	result->ic_nirq = config->ic_nirq;
	for (i = 0; i < config->ic_nirq; i++) {
		uint32_t mask = config->ic_irqmask[i];
		int irq;

		/* the PnP device may have a null resource as filler */
		if (mask == 0) {
			result->ic_irqmask[i] = 0;
			continue;
		}

		for (irq = find_first_bit(mask);
		     irq != -1;
		     irq = find_next_bit(mask, irq)) {
			bus_set_resource(child, SYS_RES_IRQ, i,
					 irq, 1);
			res[i] = bus_alloc_resource_any(child,
							SYS_RES_IRQ, &i,
							0 /* !RF_ACTIVE */ );
			if (res[i]) {
				result->ic_irqmask[i] = (1 << irq);
				break;
			}
		}

		/*
		 * If we didn't find a place for irq range i, then 
		 * give up now.
		 */
		if (!res[i]) {
			success = 0;
			break;
		}
	}

	for (i = 0; i < ISA_NIRQ; i++) {
		if (res[i])
			bus_release_resource(child, SYS_RES_IRQ,
					     i, res[i]);
	}

	return (success);
}

/*
 * Find a working set of drqs for a child using the masks in *config
 * and return the regions in *result. Returns non-zero if a set of
 * drqs was found.
 */
static int
isa_find_drq(device_t child, struct isa_config *config,
    struct isa_config *result)
{
	int success, i;
	struct resource *res[ISA_NDRQ];

	/*
	 * First clear out any existing resource definitions.
	 */
	for (i = 0; i < ISA_NDRQ; i++) {
		bus_delete_resource(child, SYS_RES_DRQ, i);
		res[i] = NULL;
	}

	success = 1;
	result->ic_ndrq = config->ic_ndrq;
	for (i = 0; i < config->ic_ndrq; i++) {
		uint32_t mask = config->ic_drqmask[i];
		int drq;

		/* the PnP device may have a null resource as filler */
		if (mask == 0) {
			result->ic_drqmask[i] = 0;
			continue;
		}

		for (drq = find_first_bit(mask);
		     drq != -1;
		     drq = find_next_bit(mask, drq)) {
			bus_set_resource(child, SYS_RES_DRQ, i,
					 drq, 1);
			res[i] = bus_alloc_resource_any(child,
							SYS_RES_DRQ, &i,
							0 /* !RF_ACTIVE */);
			if (res[i]) {
				result->ic_drqmask[i] = (1 << drq);
				break;
			}
		}

		/*
		 * If we didn't find a place for drq range i, then 
		 * give up now.
		 */
		if (!res[i]) {
			success = 0;
			break;
		}
	}

	for (i = 0; i < ISA_NDRQ; i++) {
		if (res[i])
			bus_release_resource(child, SYS_RES_DRQ,
					     i, res[i]);
	}

	return (success);
}

/*
 * Attempt to find a working set of resources for a device. Return
 * non-zero if a working configuration is found.
 */
static int
isa_assign_resources(device_t child)
{
	struct isa_device *idev = DEVTOISA(child);
	struct isa_config_entry *ice;
	struct isa_config *cfg;
	const char *reason;
	
	reason = "Empty ISA id_configs";
	cfg = malloc(sizeof(struct isa_config), M_TEMP, M_NOWAIT|M_ZERO);
	if (cfg == NULL)
		return(0);
	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
		reason = "memory";
		if (!isa_find_memory(child, &ice->ice_config, cfg))
			continue;
		reason = "port";
		if (!isa_find_port(child, &ice->ice_config, cfg))
			continue;
		reason = "irq";
		if (!isa_find_irq(child, &ice->ice_config, cfg))
			continue;
		reason = "drq";
		if (!isa_find_drq(child, &ice->ice_config, cfg))
			continue;

		/*
		 * A working configuration was found enable the device 
		 * with this configuration.
		 */
		reason = "no callback";
		if (idev->id_config_cb) {
			idev->id_config_cb(idev->id_config_arg,
					   cfg, 1);
			free(cfg, M_TEMP);
			return (1);
		}
	}

	/*
	 * Disable the device.
	 */
	bus_print_child_header(device_get_parent(child), child);
	printf(" can't assign resources (%s)\n", reason);
	if (bootverbose)
		isa_print_child(device_get_parent(child), child);
	bzero(cfg, sizeof (*cfg));
	if (idev->id_config_cb)
		idev->id_config_cb(idev->id_config_arg, cfg, 0);
	device_disable(child);

	free(cfg, M_TEMP);
	return (0);
}

/*
 * Claim any unallocated resources to keep other devices from using
 * them.
 */
static void
isa_claim_resources(device_t dev, device_t child)
{
	struct isa_device *idev = DEVTOISA(child);
	struct resource_list *rl = &idev->id_resources;
	struct resource_list_entry *rle;
	int rid;

	STAILQ_FOREACH(rle, rl, link) {
		if (!rle->res) {
			rid = rle->rid;
			resource_list_alloc(rl, dev, child, rle->type, &rid,
			    0, ~0, 1, 0);
		}
	}
}

/*
 * Called after other devices have initialised to probe for isa devices.
 */
void
isa_probe_children(device_t dev)
{
	struct isa_device *idev;
	device_t *children, child;
	struct isa_config *cfg;
	int nchildren, i, err;

	/*
	 * Create all the non-hinted children by calling drivers'
	 * identify methods.
	 */
	bus_generic_probe(dev);

	if (device_get_children(dev, &children, &nchildren))
		return;

	/*
	 * First disable all pnp devices so that they don't get
	 * matched by legacy probes.
	 */
	if (bootverbose)
		printf("isa_probe_children: disabling PnP devices\n");

	cfg = malloc(sizeof(*cfg), M_TEMP, M_NOWAIT|M_ZERO);
	if (cfg == NULL) {
		free(children, M_TEMP);
		return;
	}

	for (i = 0; i < nchildren; i++) {
		idev = DEVTOISA(children[i]);

		bzero(cfg, sizeof(*cfg));
		if (idev->id_config_cb)
			idev->id_config_cb(idev->id_config_arg, cfg, 0);
	}

	free(cfg, M_TEMP);

	/*
	 * Next, probe all the PnP BIOS devices so they can subsume any
	 * hints.
	 */
	for (i = 0; i < nchildren; i++) {
		child = children[i];
		idev = DEVTOISA(child);

		if (idev->id_order > ISA_ORDER_PNPBIOS)
			continue;
		if (!TAILQ_EMPTY(&idev->id_configs) &&
		    !isa_assign_resources(child))
			continue;

		if (device_probe_and_attach(child) == 0)
			isa_claim_resources(dev, child);
	}
	free(children, M_TEMP);

	/*
	 * Next, enumerate hinted devices and probe all non-pnp devices so
	 * that they claim their resources first.
	 */
	bus_enumerate_hinted_children(dev);
	if (device_get_children(dev, &children, &nchildren))
		return;
	if (bootverbose)
		printf("isa_probe_children: probing non-PnP devices\n");
	for (i = 0; i < nchildren; i++) {
		child = children[i];
		idev = DEVTOISA(child);

		if (device_is_attached(child) ||
		    !TAILQ_EMPTY(&idev->id_configs))
			continue;

		err = device_probe_and_attach(child);
		if (err == 0 && idev->id_vendorid == 0 &&
		    strcmp(kern_ident, "GENERIC") == 0 &&
		    device_is_attached(child))
			device_printf(child,
			    "non-PNP ISA device will be removed from GENERIC in FreeBSD 14.\n");
	}

	/*
	 * Finally assign resource to pnp devices and probe them.
	 */
	if (bootverbose)
		printf("isa_probe_children: probing PnP devices\n");
	for (i = 0; i < nchildren; i++) {
		child = children[i];
		idev = DEVTOISA(child);

		if (device_is_attached(child) || TAILQ_EMPTY(&idev->id_configs))
			continue;

		if (isa_assign_resources(child)) {
			device_probe_and_attach(child);
			isa_claim_resources(dev, child);
		}
	}

	free(children, M_TEMP);

	isa_running = 1;
}

/*
 * Add a new child with default ivars.
 */
static device_t
isa_add_child(device_t dev, u_int order, const char *name, int unit)
{
	device_t child;
	struct	isa_device *idev;

	child = device_add_child_ordered(dev, order, name, unit);
	if (child == NULL) 
		return (child);
	
	idev = malloc(sizeof(struct isa_device), M_ISADEV, M_NOWAIT | M_ZERO);
	if (!idev)
		return (0);

	resource_list_init(&idev->id_resources);
	TAILQ_INIT(&idev->id_configs);
	idev->id_order = order;

	device_set_ivars(child, idev);

	return (child);
}

static int
isa_print_all_resources(device_t dev)
{
	struct	isa_device *idev = DEVTOISA(dev);
	struct resource_list *rl = &idev->id_resources;
	int retval = 0;

	if (STAILQ_FIRST(rl) || device_get_flags(dev))
		retval += printf(" at");
	
	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
	retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#jx");
	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
	retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%jd");
	if (device_get_flags(dev))
		retval += printf(" flags %#x", device_get_flags(dev));
	if (idev->id_vendorid)
		retval += printf(" pnpid %s", pnp_eisaformat(idev->id_vendorid));

	return (retval);
}

static int
isa_print_child(device_t bus, device_t dev)
{
	int retval = 0;

	retval += bus_print_child_header(bus, dev);
	retval += isa_print_all_resources(dev);
	retval += bus_print_child_footer(bus, dev);

	return (retval);
}

static void
isa_probe_nomatch(device_t dev, device_t child)
{
	if (bootverbose) {
		bus_print_child_header(dev, child);
		printf(" failed to probe");
		isa_print_all_resources(child);
		bus_print_child_footer(dev, child);
	}
                                      
	return;
}

static int
isa_read_ivar(device_t bus, device_t dev, int index, uintptr_t * result)
{
	struct isa_device* idev = DEVTOISA(dev);
	struct resource_list *rl = &idev->id_resources;
	struct resource_list_entry *rle;

	switch (index) {
	case ISA_IVAR_PORT_0:
		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_PORT_1:
		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_PORTSIZE_0:
		rle = resource_list_find(rl, SYS_RES_IOPORT, 0);
		if (rle)
			*result = rle->count;
		else
			*result = 0;
		break;

	case ISA_IVAR_PORTSIZE_1:
		rle = resource_list_find(rl, SYS_RES_IOPORT, 1);
		if (rle)
			*result = rle->count;
		else
			*result = 0;
		break;

	case ISA_IVAR_MADDR_0:
		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_MADDR_1:
		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_MEMSIZE_0:
		rle = resource_list_find(rl, SYS_RES_MEMORY, 0);
		if (rle)
			*result = rle->count;
		else
			*result = 0;
		break;

	case ISA_IVAR_MEMSIZE_1:
		rle = resource_list_find(rl, SYS_RES_MEMORY, 1);
		if (rle)
			*result = rle->count;
		else
			*result = 0;
		break;

	case ISA_IVAR_IRQ_0:
		rle = resource_list_find(rl, SYS_RES_IRQ, 0);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_IRQ_1:
		rle = resource_list_find(rl, SYS_RES_IRQ, 1);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_DRQ_0:
		rle = resource_list_find(rl, SYS_RES_DRQ, 0);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_DRQ_1:
		rle = resource_list_find(rl, SYS_RES_DRQ, 1);
		if (rle)
			*result = rle->start;
		else
			*result = -1;
		break;

	case ISA_IVAR_VENDORID:
		*result = idev->id_vendorid;
		break;

	case ISA_IVAR_SERIAL:
		*result = idev->id_serial;
		break;

	case ISA_IVAR_LOGICALID:
		*result = idev->id_logicalid;
		break;

	case ISA_IVAR_COMPATID:
		*result = idev->id_compatid;
		break;

	case ISA_IVAR_CONFIGATTR:
		*result = idev->id_config_attr;
		break;

	case ISA_IVAR_PNP_CSN:
		*result = idev->id_pnp_csn;
		break;

	case ISA_IVAR_PNP_LDN:
		*result = idev->id_pnp_ldn;
		break;

	case ISA_IVAR_PNPBIOS_HANDLE:
		*result = idev->id_pnpbios_handle;
		break;

	default:
		return (ENOENT);
	}

	return (0);
}

static int
isa_write_ivar(device_t bus, device_t dev, int index, uintptr_t value)
{
	struct isa_device* idev = DEVTOISA(dev);

	switch (index) {
	case ISA_IVAR_PORT_0:
	case ISA_IVAR_PORT_1:
	case ISA_IVAR_PORTSIZE_0:
	case ISA_IVAR_PORTSIZE_1:
	case ISA_IVAR_MADDR_0:
	case ISA_IVAR_MADDR_1:
	case ISA_IVAR_MEMSIZE_0:
	case ISA_IVAR_MEMSIZE_1:
	case ISA_IVAR_IRQ_0:
	case ISA_IVAR_IRQ_1:
	case ISA_IVAR_DRQ_0:
	case ISA_IVAR_DRQ_1:
		return (EINVAL);

	case ISA_IVAR_VENDORID:
		idev->id_vendorid = value;
		break;

	case ISA_IVAR_SERIAL:
		idev->id_serial = value;
		break;

	case ISA_IVAR_LOGICALID:
		idev->id_logicalid = value;
		break;

	case ISA_IVAR_COMPATID:
		idev->id_compatid = value;
		break;

	case ISA_IVAR_CONFIGATTR:
		idev->id_config_attr = value;
		break;

	default:
		return (ENOENT);
	}

	return (0);
}

/*
 * Free any resources which the driver missed or which we were holding for
 * it (see isa_probe_children).
 */
static void
isa_child_detached(device_t dev, device_t child)
{
	struct isa_device* idev = DEVTOISA(child);

	if (TAILQ_FIRST(&idev->id_configs))
		isa_claim_resources(dev, child);
}

static void
isa_driver_added(device_t dev, driver_t *driver)
{
	device_t *children;
	int nchildren, i;

	/*
	 * Don't do anything if drivers are dynamically
	 * added during autoconfiguration (cf. ymf724).
	 * since that would end up calling identify
	 * twice.
	 */
	if (!isa_running)
		return;

	DEVICE_IDENTIFY(driver, dev);
	if (device_get_children(dev, &children, &nchildren))
		return;

	for (i = 0; i < nchildren; i++) {
		device_t child = children[i];
		struct isa_device *idev = DEVTOISA(child);
		struct resource_list *rl = &idev->id_resources;
		struct resource_list_entry *rle;

		if (device_get_state(child) != DS_NOTPRESENT)
			continue;
		if (!device_is_enabled(child))
			continue;

		/*
		 * Free resources which we were holding on behalf of
		 * the device.
		 */
		STAILQ_FOREACH(rle, &idev->id_resources, link) {
			if (rle->res)
				resource_list_release(rl, dev, child,
						      rle->type,
						      rle->rid,
						      rle->res);
		}

		if (TAILQ_FIRST(&idev->id_configs))
			if (!isa_assign_resources(child))
				continue;

		device_probe_and_attach(child);

		if (TAILQ_FIRST(&idev->id_configs))
			isa_claim_resources(dev, child);
	}

	free(children, M_TEMP);
}

static int
isa_set_resource(device_t dev, device_t child, int type, int rid,
    rman_res_t start, rman_res_t count)
{
	struct isa_device* idev = DEVTOISA(child);
	struct resource_list *rl = &idev->id_resources;

	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY
	    && type != SYS_RES_IRQ && type != SYS_RES_DRQ)
		return (EINVAL);
	if (rid < 0)
		return (EINVAL);
	if (type == SYS_RES_IOPORT && rid >= ISA_NPORT)
		return (EINVAL);
	if (type == SYS_RES_MEMORY && rid >= ISA_NMEM)
		return (EINVAL);
	if (type == SYS_RES_IRQ && rid >= ISA_NIRQ)
		return (EINVAL);
	if (type == SYS_RES_DRQ && rid >= ISA_NDRQ)
		return (EINVAL);

	resource_list_add(rl, type, rid, start, start + count - 1, count);

	return (0);
}

static struct resource_list *
isa_get_resource_list (device_t dev, device_t child)
{
	struct isa_device* idev = DEVTOISA(child);
	struct resource_list *rl = &idev->id_resources;

	if (!rl)
		return (NULL);

	return (rl);
}

static int
isa_add_config(device_t dev, device_t child, int priority,
    struct isa_config *config)
{
	struct isa_device* idev = DEVTOISA(child);
	struct isa_config_entry *newice, *ice;

	newice = malloc(sizeof *ice, M_DEVBUF, M_NOWAIT);
	if (!newice)
		return (ENOMEM);

	newice->ice_priority = priority;
	newice->ice_config = *config;
	
	TAILQ_FOREACH(ice, &idev->id_configs, ice_link) {
		if (ice->ice_priority > priority)
			break;
	}
	if (ice)
		TAILQ_INSERT_BEFORE(ice, newice, ice_link);
	else
		TAILQ_INSERT_TAIL(&idev->id_configs, newice, ice_link);

	return (0);
}

static void
isa_set_config_callback(device_t dev, device_t child, isa_config_cb *fn,
    void *arg)
{
	struct isa_device* idev = DEVTOISA(child);

	idev->id_config_cb = fn;
	idev->id_config_arg = arg;
}

static int
isa_pnp_probe(device_t dev, device_t child, struct isa_pnp_id *ids)
{
	struct isa_device* idev = DEVTOISA(child);

	if (!idev->id_vendorid)
		return (ENOENT);

	while (ids && ids->ip_id) {
		/*
		 * Really ought to support >1 compat id per device.
		 */
		if (idev->id_logicalid == ids->ip_id
		    || idev->id_compatid == ids->ip_id) {
			if (ids->ip_desc)
				device_set_desc(child, ids->ip_desc);
			return (0);
		}
		ids++;
	}

	return (ENXIO);
}

static int
isa_child_pnpinfo(device_t bus, device_t child, struct sbuf *sb)
{
	struct isa_device *idev = DEVTOISA(child);

	if (idev->id_vendorid)
		sbuf_printf(sb, "pnpid=%s",
		    pnp_eisaformat(idev->id_vendorid));
	return (0);
}

static int
isa_child_location(device_t bus, device_t child, struct sbuf *sb)
{
#if 0
	/* id_pnphandle isn't there yet */
	struct isa_device *idev = DEVTOISA(child);

	if (idev->id_vendorid)
		sbuf_printf(sbuf, "pnphandle=%d", idev->id_pnphandle);
#endif
	return (0);
}

static device_method_t isa_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		isa_probe),
	DEVMETHOD(device_attach,	isa_attach),
	DEVMETHOD(device_detach,	bus_generic_detach),
	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
	DEVMETHOD(device_suspend,	bus_generic_suspend),
	DEVMETHOD(device_resume,	bus_generic_resume),

	/* Bus interface */
	DEVMETHOD(bus_add_child,	isa_add_child),
	DEVMETHOD(bus_print_child,	isa_print_child),
	DEVMETHOD(bus_probe_nomatch,	isa_probe_nomatch),
	DEVMETHOD(bus_read_ivar,	isa_read_ivar),
	DEVMETHOD(bus_write_ivar,	isa_write_ivar),
	DEVMETHOD(bus_child_detached,	isa_child_detached),
	DEVMETHOD(bus_driver_added,	isa_driver_added),
	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),

	DEVMETHOD(bus_get_resource_list,isa_get_resource_list),
	DEVMETHOD(bus_alloc_resource,	isa_alloc_resource),
	DEVMETHOD(bus_release_resource,	isa_release_resource),
	DEVMETHOD(bus_set_resource,	isa_set_resource),
	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
	DEVMETHOD(bus_child_pnpinfo,	isa_child_pnpinfo),
	DEVMETHOD(bus_child_location,	isa_child_location),
	DEVMETHOD(bus_hinted_child,	isa_hinted_child),
	DEVMETHOD(bus_hint_device_unit,	isa_hint_device_unit),

	/* ISA interface */
	DEVMETHOD(isa_add_config,	isa_add_config),
	DEVMETHOD(isa_set_config_callback, isa_set_config_callback),
	DEVMETHOD(isa_pnp_probe,	isa_pnp_probe),

	{ 0, 0 }
};

DEFINE_CLASS_0(isa, isa_driver, isa_methods, 0);

/*
 * ISA can be attached to a PCI-ISA bridge, or other locations on some
 * platforms.
 */
DRIVER_MODULE(isa, isab, isa_driver, 0, 0);
DRIVER_MODULE(isa, eisab, isa_driver, 0, 0);
MODULE_VERSION(isa, 1);

/*
 * Code common to ISA bridges.
 */

int
isab_attach(device_t dev)
{
	device_t child;

	child = device_add_child(dev, "isa", 0);
	if (child != NULL)
		return (bus_generic_attach(dev));
	return (ENXIO);
}

char *
pnp_eisaformat(uint32_t id)
{
	uint8_t *data;
	static char idbuf[8];
	const char  hextoascii[] = "0123456789abcdef";

	id = htole32(id);
	data = (uint8_t *)&id;
	idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
	idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
	idbuf[2] = '@' + (data[1] & 0x1f);
	idbuf[3] = hextoascii[(data[2] >> 4)];
	idbuf[4] = hextoascii[(data[2] & 0xf)];
	idbuf[5] = hextoascii[(data[3] >> 4)];
	idbuf[6] = hextoascii[(data[3] & 0xf)];
	idbuf[7] = 0;
	return(idbuf);
}