xref: /freebsd/sys/dev/proto/proto_bus_isa.c (revision f3c5273d315a64826d2149ac453ff8c4583ddbe8)
1 /*-
2  * Copyright (c) 2015 Marcel Moolenaar
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <machine/bus.h>
36 #include <sys/rman.h>
37 #include <machine/resource.h>
38 #include <sys/sbuf.h>
39 
40 #include <isa/isavar.h>
41 #include <isa/pnpvar.h>
42 
43 #include <dev/proto/proto.h>
44 
45 static int proto_isa_probe(device_t dev);
46 static int proto_isa_attach(device_t dev);
47 
48 static device_method_t proto_isa_methods[] = {
49 	/* Device interface */
50 	DEVMETHOD(device_probe,		proto_isa_probe),
51 	DEVMETHOD(device_attach,	proto_isa_attach),
52 	DEVMETHOD(device_detach,	proto_detach),
53 	DEVMETHOD_END
54 };
55 
56 static driver_t proto_isa_driver = {
57 	proto_driver_name,
58 	proto_isa_methods,
59 	sizeof(struct proto_softc),
60 };
61 
62 static char proto_isa_prefix[] = "isa";
63 static char **proto_isa_devnames;
64 
65 static int
66 proto_isa_probe(device_t dev)
67 {
68 	struct sbuf *sb;
69 	struct resource *res;
70 	int rid, type;
71 
72 	rid = 0;
73 	type = SYS_RES_IOPORT;
74 	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
75 	if (res == NULL) {
76 		type = SYS_RES_MEMORY;
77 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
78 	}
79 	if (res == NULL)
80 		return (ENODEV);
81 
82 	sb = sbuf_new_auto();
83 	sbuf_printf(sb, "%s:%#jx", proto_isa_prefix, rman_get_start(res));
84 	sbuf_finish(sb);
85 	device_set_desc_copy(dev, sbuf_data(sb));
86 	sbuf_delete(sb);
87 	bus_release_resource(dev, type, rid, res);
88 	return (proto_probe(dev, proto_isa_prefix, &proto_isa_devnames));
89 }
90 
91 static int
92 proto_isa_alloc(device_t dev, int type, int nrids)
93 {
94 	struct resource *res;
95 	struct proto_softc *sc;
96 	int count, rid;
97 
98 	sc = device_get_softc(dev);
99 	count = 0;
100 	for (rid = 0; rid < nrids; rid++) {
101 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
102 		if (res == NULL)
103 			break;
104 		proto_add_resource(sc, type, rid, res);
105 		count++;
106 	}
107 	if (type == SYS_RES_DRQ && count > 0)
108 		proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
109 	return (count);
110 }
111 
112 static int
113 proto_isa_attach(device_t dev)
114 {
115 
116 	proto_isa_alloc(dev, SYS_RES_IRQ, ISA_NIRQ);
117 	proto_isa_alloc(dev, SYS_RES_DRQ, ISA_NDRQ);
118 	proto_isa_alloc(dev, SYS_RES_IOPORT, ISA_NPORT);
119 	proto_isa_alloc(dev, SYS_RES_MEMORY, ISA_NMEM);
120 	return (proto_attach(dev));
121 }
122 
123 DRIVER_MODULE(proto, acpi, proto_isa_driver, proto_devclass, NULL, NULL);
124 DRIVER_MODULE(proto, isa, proto_isa_driver, proto_devclass, NULL, NULL);
125