2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <machine/bus_pio.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
42 #include <dev/kbd/atkbdcreg.h>
44 #include <isa/isareg.h>
45 #include <isa/isavar.h>
47 static MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
50 typedef struct atkbdc_device {
51 struct resource_list resources;
60 static devclass_t atkbdc_devclass;
62 static int atkbdc_probe(device_t dev);
63 static int atkbdc_attach(device_t dev);
64 static device_t atkbdc_add_child(device_t bus, int order, char *name,
66 static int atkbdc_print_child(device_t bus, device_t dev);
67 static int atkbdc_read_ivar(device_t bus, device_t dev, int index,
69 static int atkbdc_write_ivar(device_t bus, device_t dev, int index,
71 static struct resource_list
72 *atkbdc_get_resource_list (device_t bus, device_t dev);
73 static struct resource
74 *atkbdc_alloc_resource(device_t bus, device_t dev, int type,
75 int *rid, u_long start, u_long end,
76 u_long count, u_int flags);
77 static int atkbdc_release_resource(device_t bus, device_t dev, int type,
78 int rid, struct resource *res);
80 static device_method_t atkbdc_methods[] = {
81 DEVMETHOD(device_probe, atkbdc_probe),
82 DEVMETHOD(device_attach, atkbdc_attach),
83 DEVMETHOD(device_suspend, bus_generic_suspend),
84 DEVMETHOD(device_resume, bus_generic_resume),
86 DEVMETHOD(bus_add_child, atkbdc_add_child),
87 DEVMETHOD(bus_print_child, atkbdc_print_child),
88 DEVMETHOD(bus_read_ivar, atkbdc_read_ivar),
89 DEVMETHOD(bus_write_ivar, atkbdc_write_ivar),
90 DEVMETHOD(bus_get_resource_list,atkbdc_get_resource_list),
91 DEVMETHOD(bus_alloc_resource, atkbdc_alloc_resource),
92 DEVMETHOD(bus_release_resource, atkbdc_release_resource),
93 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
94 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
95 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
96 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
97 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
98 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
99 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
104 static driver_t atkbdc_driver = {
107 sizeof(atkbdc_softc_t *),
110 static struct isa_pnp_id atkbdc_ids[] = {
111 { 0x0303d041, "Keyboard controller (i8042)" }, /* PNP0303 */
116 atkbdc_probe(device_t dev)
118 struct resource *port0;
119 struct resource *port1;
126 if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
129 device_set_desc(dev, "Keyboard controller (i8042)");
132 * Adjust I/O port resources.
133 * The AT keyboard controller uses two ports (a command/data port
134 * 0x60 and a status port 0x64), which may be given to us in
135 * one resource (0x60 through 0x64) or as two separate resources
136 * (0x60 and 0x64). Furthermore, /boot/device.hints may contain
137 * just one port, 0x60. We shall adjust resource settings
138 * so that these two ports are available as two separate resources.
142 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &start, &count) != 0)
144 if (count > 1) /* adjust the count */
145 bus_set_resource(dev, SYS_RES_IOPORT, rid, start, 1);
146 port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
150 if (bus_get_resource(dev, SYS_RES_IOPORT, rid, NULL, NULL) != 0)
151 bus_set_resource(dev, SYS_RES_IOPORT, 1,
152 start + KBD_STATUS_PORT, 1);
153 port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
155 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
160 error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
162 bus_generic_probe(dev);
164 bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
165 bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
171 atkbdc_attach(device_t dev)
178 unit = device_get_unit(dev);
179 sc = *(atkbdc_softc_t **)device_get_softc(dev);
182 * We have to maintain two copies of the kbdc_softc struct,
183 * as the low-level console needs to have access to the
184 * keyboard controller before kbdc is probed and attached.
185 * kbdc_soft[] contains the default entry for that purpose.
188 sc = atkbdc_get_softc(unit);
194 sc->port0 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
196 if (sc->port0 == NULL)
199 sc->port1 = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
201 if (sc->port1 == NULL) {
202 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
206 error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
208 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
209 bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
212 *(atkbdc_softc_t **)device_get_softc(dev) = sc;
214 bus_generic_attach(dev);
220 atkbdc_add_child(device_t bus, int order, char *name, int unit)
222 atkbdc_device_t *ivar;
226 ivar = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV,
231 child = device_add_child_ordered(bus, order, name, unit);
233 free(ivar, M_ATKBDDEV);
237 resource_list_init(&ivar->resources);
241 * If the device is not created by the PnP BIOS or ACPI,
242 * refer to device hints for IRQ.
244 if (ISA_PNP_PROBE(device_get_parent(bus), bus, atkbdc_ids) != 0) {
245 if (resource_int_value(name, unit, "irq", &t) != 0)
248 t = bus_get_resource_start(bus, SYS_RES_IRQ, ivar->rid);
251 resource_list_add(&ivar->resources, SYS_RES_IRQ, ivar->rid,
254 if (resource_int_value(name, unit, "flags", &t) == 0)
255 device_set_flags(child, t);
256 if (resource_disabled(name, unit))
257 device_disable(child);
259 device_set_ivars(child, ivar);
265 atkbdc_print_child(device_t bus, device_t dev)
267 atkbdc_device_t *kbdcdev;
272 kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
274 retval += bus_print_child_header(bus, dev);
275 flags = device_get_flags(dev);
277 retval += printf(" flags 0x%x", flags);
278 irq = bus_get_resource_start(dev, SYS_RES_IRQ, kbdcdev->rid);
280 retval += printf(" irq %ld", irq);
281 retval += bus_print_child_footer(bus, dev);
287 atkbdc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val)
289 atkbdc_device_t *ivar;
291 ivar = (atkbdc_device_t *)device_get_ivars(dev);
293 case KBDC_IVAR_VENDORID:
294 *val = (u_long)ivar->vendorid;
296 case KBDC_IVAR_SERIAL:
297 *val = (u_long)ivar->serial;
299 case KBDC_IVAR_LOGICALID:
300 *val = (u_long)ivar->logicalid;
302 case KBDC_IVAR_COMPATID:
303 *val = (u_long)ivar->compatid;
312 atkbdc_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
314 atkbdc_device_t *ivar;
316 ivar = (atkbdc_device_t *)device_get_ivars(dev);
318 case KBDC_IVAR_VENDORID:
319 ivar->vendorid = (u_int32_t)val;
321 case KBDC_IVAR_SERIAL:
322 ivar->serial = (u_int32_t)val;
324 case KBDC_IVAR_LOGICALID:
325 ivar->logicalid = (u_int32_t)val;
327 case KBDC_IVAR_COMPATID:
328 ivar->compatid = (u_int32_t)val;
336 static struct resource_list
337 *atkbdc_get_resource_list (device_t bus, device_t dev)
339 atkbdc_device_t *ivar;
341 ivar = (atkbdc_device_t *)device_get_ivars(dev);
342 return &ivar->resources;
345 static struct resource
346 *atkbdc_alloc_resource(device_t bus, device_t dev, int type, int *rid,
347 u_long start, u_long end, u_long count, u_int flags)
349 atkbdc_device_t *ivar;
351 ivar = (atkbdc_device_t *)device_get_ivars(dev);
352 return resource_list_alloc(&ivar->resources, bus, dev, type, rid,
353 start, end, count, flags);
357 atkbdc_release_resource(device_t bus, device_t dev, int type, int rid,
358 struct resource *res)
360 atkbdc_device_t *ivar;
362 ivar = (atkbdc_device_t *)device_get_ivars(dev);
363 return resource_list_release(&ivar->resources, bus, dev, type, rid,
367 DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);
368 DRIVER_MODULE(atkbdc, acpi, atkbdc_driver, atkbdc_devclass, 0, 0);