Deleted Added
full compact
atkbdc_isa.c (57481) atkbdc_isa.c (58271)
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
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 as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
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 as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/atkbdc/atkbdc_isa.c 57481 2000-02-25 11:40:31Z yokota $
26 * $FreeBSD: head/sys/dev/atkbdc/atkbdc_isa.c 58271 2000-03-19 03:25:13Z yokota $
27 */
28
29#include "opt_kbd.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/bus.h>
35#include <sys/malloc.h>
27 */
28
29#include "opt_kbd.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/bus.h>
35#include <sys/malloc.h>
36#include <machine/bus_pio.h>
36#include <machine/bus.h>
37#include <machine/resource.h>
38#include <sys/rman.h>
39
40#include <dev/kbd/atkbdcreg.h>
41
42#include <isa/isareg.h>
43#include <isa/isavar.h>
44
45MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
46
47/* children */
48typedef struct atkbdc_device {
49 int flags; /* configuration flags */
50 int port; /* port number (same as the controller's) */
51 int irq; /* ISA IRQ mask */
52} atkbdc_device_t;
53
54/* kbdc */
55devclass_t atkbdc_devclass;
56
57static int atkbdc_probe(device_t dev);
58static int atkbdc_attach(device_t dev);
59static int atkbdc_print_child(device_t bus, device_t dev);
60static int atkbdc_read_ivar(device_t bus, device_t dev, int index,
61 u_long *val);
62static int atkbdc_write_ivar(device_t bus, device_t dev, int index,
63 u_long val);
64
65static device_method_t atkbdc_methods[] = {
66 DEVMETHOD(device_probe, atkbdc_probe),
67 DEVMETHOD(device_attach, atkbdc_attach),
68 DEVMETHOD(device_suspend, bus_generic_suspend),
69 DEVMETHOD(device_resume, bus_generic_resume),
70
71 DEVMETHOD(bus_print_child, atkbdc_print_child),
72 DEVMETHOD(bus_read_ivar, atkbdc_read_ivar),
73 DEVMETHOD(bus_write_ivar, atkbdc_write_ivar),
74 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
75 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
76 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
77 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
78 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
79 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
80
81 { 0, 0 }
82};
83
84static driver_t atkbdc_driver = {
85 ATKBDC_DRIVER_NAME,
86 atkbdc_methods,
87 sizeof(atkbdc_softc_t *),
88};
89
37#include <machine/bus.h>
38#include <machine/resource.h>
39#include <sys/rman.h>
40
41#include <dev/kbd/atkbdcreg.h>
42
43#include <isa/isareg.h>
44#include <isa/isavar.h>
45
46MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
47
48/* children */
49typedef struct atkbdc_device {
50 int flags; /* configuration flags */
51 int port; /* port number (same as the controller's) */
52 int irq; /* ISA IRQ mask */
53} atkbdc_device_t;
54
55/* kbdc */
56devclass_t atkbdc_devclass;
57
58static int atkbdc_probe(device_t dev);
59static int atkbdc_attach(device_t dev);
60static int atkbdc_print_child(device_t bus, device_t dev);
61static int atkbdc_read_ivar(device_t bus, device_t dev, int index,
62 u_long *val);
63static int atkbdc_write_ivar(device_t bus, device_t dev, int index,
64 u_long val);
65
66static device_method_t atkbdc_methods[] = {
67 DEVMETHOD(device_probe, atkbdc_probe),
68 DEVMETHOD(device_attach, atkbdc_attach),
69 DEVMETHOD(device_suspend, bus_generic_suspend),
70 DEVMETHOD(device_resume, bus_generic_resume),
71
72 DEVMETHOD(bus_print_child, atkbdc_print_child),
73 DEVMETHOD(bus_read_ivar, atkbdc_read_ivar),
74 DEVMETHOD(bus_write_ivar, atkbdc_write_ivar),
75 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
76 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
77 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
78 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
79 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
80 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
81
82 { 0, 0 }
83};
84
85static driver_t atkbdc_driver = {
86 ATKBDC_DRIVER_NAME,
87 atkbdc_methods,
88 sizeof(atkbdc_softc_t *),
89};
90
91static struct isa_pnp_id atkbdc_ids[] = {
92 { 0x0303d041, "Keyboard controller (i8042)" }, /* PNP0303 */
93 { 0 }
94};
95
90static int
91atkbdc_probe(device_t dev)
92{
96static int
97atkbdc_probe(device_t dev)
98{
93 int error;
94 int rid;
95 struct resource *port;
99 struct resource *port0;
100 struct resource *port1;
101 int error;
102 int rid;
96
103
97 /* Check isapnp ids */
98 if (isa_get_vendorid(dev))
99 return (ENXIO);
104 /* check PnP IDs */
105 if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
106 return ENXIO;
100
107
101 device_set_desc(dev, "keyboard controller (i8042)");
108 device_set_desc(dev, "Keyboard controller (i8042)");
109
102 rid = 0;
110 rid = 0;
103 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
104 0, ~0, IO_KBDSIZE, RF_ACTIVE);
105 if (!port)
111 port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
112 RF_ACTIVE);
113 if (port0 == NULL)
106 return ENXIO;
114 return ENXIO;
107 error = atkbdc_probe_unit(device_get_unit(dev), rman_get_start(port));
108 bus_release_resource(dev, SYS_RES_IOPORT, rid, port);
115 /* XXX */
116 if (bus_get_resource_start(dev, SYS_RES_IOPORT, 1) <= 0) {
117 bus_set_resource(dev, SYS_RES_IOPORT, 1,
118 rman_get_start(port0) + KBD_STATUS_PORT, 1);
119 }
120 rid = 1;
121 port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
122 RF_ACTIVE);
123 if (port1 == NULL) {
124 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
125 return ENXIO;
126 }
127
128 error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
129
130 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
131 bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
132
109 return error;
110}
111
112static void
113atkbdc_add_device(device_t dev, const char *name, int unit)
114{
133 return error;
134}
135
136static void
137atkbdc_add_device(device_t dev, const char *name, int unit)
138{
115 atkbdc_softc_t *sc = *(atkbdc_softc_t **)device_get_softc(dev);
116 atkbdc_device_t *kdev;
117 device_t child;
118 int t;
119
120 if (resource_int_value(name, unit, "disabled", &t) == 0 && t != 0)
121 return;
122
123 kdev = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, M_NOWAIT);
124 if (!kdev)
125 return;
126 bzero(kdev, sizeof *kdev);
127
139 atkbdc_device_t *kdev;
140 device_t child;
141 int t;
142
143 if (resource_int_value(name, unit, "disabled", &t) == 0 && t != 0)
144 return;
145
146 kdev = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, M_NOWAIT);
147 if (!kdev)
148 return;
149 bzero(kdev, sizeof *kdev);
150
128 kdev->port = sc->port;
129
130 if (resource_int_value(name, unit, "irq", &t) == 0)
131 kdev->irq = t;
132 else
133 kdev->irq = -1;
134
135 if (resource_int_value(name, unit, "flags", &t) == 0)
136 kdev->flags = t;
137 else
138 kdev->flags = 0;
139
140 child = device_add_child(dev, name, unit);
141 device_set_ivars(child, kdev);
142}
143
144static int
145atkbdc_attach(device_t dev)
146{
147 atkbdc_softc_t *sc;
151 if (resource_int_value(name, unit, "irq", &t) == 0)
152 kdev->irq = t;
153 else
154 kdev->irq = -1;
155
156 if (resource_int_value(name, unit, "flags", &t) == 0)
157 kdev->flags = t;
158 else
159 kdev->flags = 0;
160
161 child = device_add_child(dev, name, unit);
162 device_set_ivars(child, kdev);
163}
164
165static int
166atkbdc_attach(device_t dev)
167{
168 atkbdc_softc_t *sc;
148 struct resource *port;
149 int unit;
150 int error;
151 int rid;
152 int i;
153
154 unit = device_get_unit(dev);
155 sc = *(atkbdc_softc_t **)device_get_softc(dev);
156 if (sc == NULL) {
157 /*
158 * We have to maintain two copies of the kbdc_softc struct,
159 * as the low-level console needs to have access to the
160 * keyboard controller before kbdc is probed and attached.
161 * kbdc_soft[] contains the default entry for that purpose.
162 * See atkbdc.c. XXX
163 */
164 sc = atkbdc_get_softc(unit);
165 if (sc == NULL)
166 return ENOMEM;
167 }
168
169 int unit;
170 int error;
171 int rid;
172 int i;
173
174 unit = device_get_unit(dev);
175 sc = *(atkbdc_softc_t **)device_get_softc(dev);
176 if (sc == NULL) {
177 /*
178 * We have to maintain two copies of the kbdc_softc struct,
179 * as the low-level console needs to have access to the
180 * keyboard controller before kbdc is probed and attached.
181 * kbdc_soft[] contains the default entry for that purpose.
182 * See atkbdc.c. XXX
183 */
184 sc = atkbdc_get_softc(unit);
185 if (sc == NULL)
186 return ENOMEM;
187 }
188
169 /* XXX should track resource in softc */
170 rid = 0;
189 rid = 0;
171 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
172 0, ~0, IO_KBDSIZE, RF_ACTIVE);
173 if (!port)
190 sc->port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
191 RF_ACTIVE);
192 if (sc->port0 == NULL)
174 return ENXIO;
193 return ENXIO;
175 error = atkbdc_attach_unit(unit, sc, rman_get_start(port));
176 if (error)
194 rid = 1;
195 sc->port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
196 RF_ACTIVE);
197 if (sc->port1 == NULL) {
198 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
199 return ENXIO;
200 }
201
202 error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
203 if (error) {
204 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
205 bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
177 return error;
206 return error;
207 }
178 *(atkbdc_softc_t **)device_get_softc(dev) = sc;
179
180 /*
181 * Add all devices configured to be attached to atkbdc0.
182 */
183 for (i = resource_query_string(-1, "at", "atkbdc0");
184 i != -1;
185 i = resource_query_string(i, "at", "atkbdc0")) {
186 atkbdc_add_device(dev, resource_query_name(i),
187 resource_query_unit(i));
188 }
189
190 /*
191 * and atkbdc?
192 */
193 for (i = resource_query_string(-1, "at", "atkbdc");
194 i != -1;
195 i = resource_query_string(i, "at", "atkbdc")) {
196 atkbdc_add_device(dev, resource_query_name(i),
197 resource_query_unit(i));
198 }
199
200 bus_generic_attach(dev);
201
202 return 0;
203}
204
205static int
206atkbdc_print_child(device_t bus, device_t dev)
207{
208 atkbdc_device_t *kbdcdev;
209 int retval = 0;
210
211 kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
212
213 retval += bus_print_child_header(bus, dev);
214 if (kbdcdev->flags != 0)
215 retval += printf(" flags 0x%x", kbdcdev->flags);
216 if (kbdcdev->irq != -1)
217 retval += printf(" irq %d", kbdcdev->irq);
218 retval += bus_print_child_footer(bus, dev);
219
220 return (retval);
221}
222
223static int
224atkbdc_read_ivar(device_t bus, device_t dev, int index, u_long *val)
225{
226 atkbdc_device_t *ivar;
227
228 ivar = (atkbdc_device_t *)device_get_ivars(dev);
229 switch (index) {
230 case KBDC_IVAR_PORT:
231 *val = (u_long)ivar->port;
232 break;
233 case KBDC_IVAR_IRQ:
234 *val = (u_long)ivar->irq;
235 break;
236 case KBDC_IVAR_FLAGS:
237 *val = (u_long)ivar->flags;
238 break;
239 default:
240 return ENOENT;
241 }
242 return 0;
243}
244
245static int
246atkbdc_write_ivar(device_t bus, device_t dev, int index, u_long val)
247{
248 atkbdc_device_t *ivar;
249
250 ivar = (atkbdc_device_t *)device_get_ivars(dev);
251 switch (index) {
252 case KBDC_IVAR_PORT:
253 ivar->port = (int)val;
254 break;
255 case KBDC_IVAR_IRQ:
256 ivar->irq = (int)val;
257 break;
258 case KBDC_IVAR_FLAGS:
259 ivar->flags = (int)val;
260 break;
261 default:
262 return ENOENT;
263 }
264 return 0;
265}
266
267DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);
208 *(atkbdc_softc_t **)device_get_softc(dev) = sc;
209
210 /*
211 * Add all devices configured to be attached to atkbdc0.
212 */
213 for (i = resource_query_string(-1, "at", "atkbdc0");
214 i != -1;
215 i = resource_query_string(i, "at", "atkbdc0")) {
216 atkbdc_add_device(dev, resource_query_name(i),
217 resource_query_unit(i));
218 }
219
220 /*
221 * and atkbdc?
222 */
223 for (i = resource_query_string(-1, "at", "atkbdc");
224 i != -1;
225 i = resource_query_string(i, "at", "atkbdc")) {
226 atkbdc_add_device(dev, resource_query_name(i),
227 resource_query_unit(i));
228 }
229
230 bus_generic_attach(dev);
231
232 return 0;
233}
234
235static int
236atkbdc_print_child(device_t bus, device_t dev)
237{
238 atkbdc_device_t *kbdcdev;
239 int retval = 0;
240
241 kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
242
243 retval += bus_print_child_header(bus, dev);
244 if (kbdcdev->flags != 0)
245 retval += printf(" flags 0x%x", kbdcdev->flags);
246 if (kbdcdev->irq != -1)
247 retval += printf(" irq %d", kbdcdev->irq);
248 retval += bus_print_child_footer(bus, dev);
249
250 return (retval);
251}
252
253static int
254atkbdc_read_ivar(device_t bus, device_t dev, int index, u_long *val)
255{
256 atkbdc_device_t *ivar;
257
258 ivar = (atkbdc_device_t *)device_get_ivars(dev);
259 switch (index) {
260 case KBDC_IVAR_PORT:
261 *val = (u_long)ivar->port;
262 break;
263 case KBDC_IVAR_IRQ:
264 *val = (u_long)ivar->irq;
265 break;
266 case KBDC_IVAR_FLAGS:
267 *val = (u_long)ivar->flags;
268 break;
269 default:
270 return ENOENT;
271 }
272 return 0;
273}
274
275static int
276atkbdc_write_ivar(device_t bus, device_t dev, int index, u_long val)
277{
278 atkbdc_device_t *ivar;
279
280 ivar = (atkbdc_device_t *)device_get_ivars(dev);
281 switch (index) {
282 case KBDC_IVAR_PORT:
283 ivar->port = (int)val;
284 break;
285 case KBDC_IVAR_IRQ:
286 ivar->irq = (int)val;
287 break;
288 case KBDC_IVAR_FLAGS:
289 ivar->flags = (int)val;
290 break;
291 default:
292 return ENOENT;
293 }
294 return 0;
295}
296
297DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);