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 * $Id: atkbdc_isa.c,v 1.9 1999/06/29 17:35:09 yokota Exp $ 27 */ 28 29 #include "atkbdc.h" 30 #include "opt_kbd.h" 31 32 #if NATKBDC > 0 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/bus.h> 38 #include <sys/malloc.h> 39 #include <machine/bus.h> 40 #include <machine/resource.h> 41 #include <sys/rman.h> 42 43 #include <dev/kbd/atkbdcreg.h> 44 45 #include <isa/isareg.h> 46 #include <isa/isavar.h> 47 48 MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device"); 49 50 /* children */ 51 typedef struct atkbdc_device { 52 int flags; /* configuration flags */ 53 int port; /* port number (same as the controller's) */ 54 int irq; /* ISA IRQ mask */ 55 } atkbdc_device_t; 56 57 /* kbdc */ 58 devclass_t atkbdc_devclass; 59 60 static int atkbdc_probe(device_t dev); 61 static int atkbdc_attach(device_t dev); 62 static int atkbdc_print_child(device_t bus, device_t dev); 63 static int atkbdc_read_ivar(device_t bus, device_t dev, int index, 64 u_long *val); 65 static int atkbdc_write_ivar(device_t bus, device_t dev, int index, 66 u_long val); 67 68 static device_method_t atkbdc_methods[] = { 69 DEVMETHOD(device_probe, atkbdc_probe), 70 DEVMETHOD(device_attach, atkbdc_attach), 71 DEVMETHOD(device_suspend, bus_generic_suspend), 72 DEVMETHOD(device_resume, bus_generic_resume), 73 74 DEVMETHOD(bus_print_child, atkbdc_print_child), 75 DEVMETHOD(bus_read_ivar, atkbdc_read_ivar), 76 DEVMETHOD(bus_write_ivar, atkbdc_write_ivar), 77 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 78 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 79 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 80 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 81 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 82 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 83 84 { 0, 0 } 85 }; 86 87 static driver_t atkbdc_driver = { 88 ATKBDC_DRIVER_NAME, 89 atkbdc_methods, 90 sizeof(atkbdc_softc_t *), 91 }; 92 93 static int 94 atkbdc_probe(device_t dev) 95 { 96 int error; 97 int rid; 98 struct resource *port; 99 100 /* Check isapnp ids */ 101 if (isa_get_vendorid(dev)) 102 return (ENXIO); 103 104 device_set_desc(dev, "keyboard controller (i8042)"); 105 rid = 0; 106 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 107 0, ~0, IO_KBDSIZE, RF_ACTIVE); 108 if (!port) 109 return ENXIO; 110 error = atkbdc_probe_unit(device_get_unit(dev), rman_get_start(port)); 111 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 112 return error; 113 } 114 115 static void 116 atkbdc_add_device(device_t dev, const char *name, int unit) 117 { 118 atkbdc_softc_t *sc = *(atkbdc_softc_t **)device_get_softc(dev); 119 atkbdc_device_t *kdev; 120 device_t child; 121 int t; 122 123 kdev = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, M_NOWAIT); 124 if (!kdev) 125 return; 126 bzero(kdev, sizeof *kdev); 127 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, kdev); 141 } 142 143 static int 144 atkbdc_attach(device_t dev) 145 { 146 atkbdc_softc_t *sc; 147 struct resource *port; 148 int unit; 149 int error; 150 int rid; 151 int i; 152 153 unit = device_get_unit(dev); 154 sc = *(atkbdc_softc_t **)device_get_softc(dev); 155 if (sc == NULL) { 156 /* 157 * We have to maintain two copies of the kbdc_softc struct, 158 * as the low-level console needs to have access to the 159 * keyboard controller before kbdc is probed and attached. 160 * kbdc_soft[] contains the default entry for that purpose. 161 * See atkbdc.c. XXX 162 */ 163 sc = atkbdc_get_softc(unit); 164 if (sc == NULL) 165 return ENOMEM; 166 } 167 168 /* XXX should track resource in softc */ 169 rid = 0; 170 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 171 0, ~0, IO_KBDSIZE, RF_ACTIVE); 172 if (!port) 173 return ENXIO; 174 error = atkbdc_attach_unit(unit, sc, rman_get_start(port)); 175 if (error) 176 return error; 177 *(atkbdc_softc_t **)device_get_softc(dev) = sc; 178 179 /* 180 * Add all devices configured to be attached to atkbdc0. 181 */ 182 for (i = resource_query_string(-1, "at", "atkbdc0"); 183 i != -1; 184 i = resource_query_string(i, "at", "atkbdc0")) { 185 atkbdc_add_device(dev, resource_query_name(i), 186 resource_query_unit(i)); 187 } 188 189 /* 190 * and atkbdc? 191 */ 192 for (i = resource_query_string(-1, "at", "atkbdc"); 193 i != -1; 194 i = resource_query_string(i, "at", "atkbdc")) { 195 atkbdc_add_device(dev, resource_query_name(i), 196 resource_query_unit(i)); 197 } 198 199 bus_generic_attach(dev); 200 201 return 0; 202 } 203 204 static int 205 atkbdc_print_child(device_t bus, device_t dev) 206 { 207 atkbdc_device_t *kbdcdev; 208 int retval = 0; 209 210 kbdcdev = (atkbdc_device_t *)device_get_ivars(dev); 211 212 retval += bus_print_child_header(bus, dev); 213 if (kbdcdev->flags != 0) 214 retval += printf(" flags 0x%x", kbdcdev->flags); 215 if (kbdcdev->irq != -1) 216 retval += printf(" irq %d", kbdcdev->irq); 217 retval += bus_print_child_footer(bus, dev); 218 219 return (retval); 220 } 221 222 static int 223 atkbdc_read_ivar(device_t bus, device_t dev, int index, u_long *val) 224 { 225 atkbdc_device_t *ivar; 226 227 ivar = (atkbdc_device_t *)device_get_ivars(dev); 228 switch (index) { 229 case KBDC_IVAR_PORT: 230 *val = (u_long)ivar->port; 231 break; 232 case KBDC_IVAR_IRQ: 233 *val = (u_long)ivar->irq; 234 break; 235 case KBDC_IVAR_FLAGS: 236 *val = (u_long)ivar->flags; 237 break; 238 default: 239 return ENOENT; 240 } 241 return 0; 242 } 243 244 static int 245 atkbdc_write_ivar(device_t bus, device_t dev, int index, u_long val) 246 { 247 atkbdc_device_t *ivar; 248 249 ivar = (atkbdc_device_t *)device_get_ivars(dev); 250 switch (index) { 251 case KBDC_IVAR_PORT: 252 ivar->port = (int)val; 253 break; 254 case KBDC_IVAR_IRQ: 255 ivar->irq = (int)val; 256 break; 257 case KBDC_IVAR_FLAGS: 258 ivar->flags = (int)val; 259 break; 260 default: 261 return ENOENT; 262 } 263 return 0; 264 } 265 266 DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0); 267 268 #endif /* NATKBDC > 0 */ 269