atkbdc_subr.c revision 46743
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.3 1999/05/08 20:25:38 peter 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 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 void 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 69 DEVMETHOD(bus_print_child, atkbdc_print_child), 70 DEVMETHOD(bus_read_ivar, atkbdc_read_ivar), 71 DEVMETHOD(bus_write_ivar, atkbdc_write_ivar), 72 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource), 73 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 74 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 75 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 76 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 77 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 78 79 { 0, 0 } 80}; 81 82static driver_t atkbdc_driver = { 83 ATKBDC_DRIVER_NAME, 84 atkbdc_methods, 85 sizeof(atkbdc_softc_t *), 86}; 87 88static int 89atkbdc_probe(device_t dev) 90{ 91 atkbdc_softc_t *sc; 92 int unit; 93 int error; 94 95 unit = device_get_unit(dev); 96 sc = *(atkbdc_softc_t **)device_get_softc(dev); 97 if (sc == NULL) { 98 /* 99 * We have to maintain two copies of the kbdc_softc struct, 100 * as the low-level console needs to have access to the 101 * keyboard controller before kbdc is probed and attached. 102 * kbdc_soft[] contains the default entry for that purpose. 103 * See atkbdc.c. XXX 104 */ 105 sc = atkbdc_get_softc(unit); 106 if (sc == NULL) 107 return ENOMEM; 108 } 109 110 device_set_desc(dev, "keyboard controller (i8042)"); 111 112 error = atkbdc_probe_unit(sc, unit, isa_get_port(dev)); 113 if (error == 0) 114 *(atkbdc_softc_t **)device_get_softc(dev) = sc; 115 116 return error; 117} 118 119static void 120atkbdc_add_device(device_t dev, const char *name, int unit) 121{ 122 atkbdc_softc_t *sc = *(atkbdc_softc_t **)device_get_softc(dev); 123 atkbdc_device_t *kdev; 124 device_t child; 125 int t; 126 127 kdev = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, M_NOWAIT); 128 if (!kdev) 129 return; 130 bzero(kdev, sizeof *kdev); 131 132 kdev->port = sc->port; 133 134 if (resource_int_value(name, unit, "irq", &t) == 0) 135 kdev->irq = t; 136 else 137 kdev->irq = -1; 138 139 if (resource_int_value(name, unit, "flags", &t) == 0) 140 kdev->flags = t; 141 else 142 kdev->flags = 0; 143 144 child = device_add_child(dev, name, unit, kdev); 145} 146 147static int 148atkbdc_attach(device_t dev) 149{ 150 atkbdc_softc_t *sc; 151 int i; 152 153 sc = *(atkbdc_softc_t **)device_get_softc(dev); 154 if ((sc == NULL) || (sc->port <= 0)) 155 return ENXIO; 156 157 /* 158 * Add all devices configured to be attached to atkbdc0. 159 */ 160 for (i = resource_query_string(-1, "at", "atkbdc0"); 161 i != -1; 162 i = resource_query_string(i, "at", "atkbdc0")) { 163 atkbdc_add_device(dev, resource_query_name(i), 164 resource_query_unit(i)); 165 } 166 167 /* 168 * and atkbdc? 169 */ 170 for (i = resource_query_string(-1, "at", "atkbdc"); 171 i != -1; 172 i = resource_query_string(i, "at", "atkbdc")) { 173 atkbdc_add_device(dev, resource_query_name(i), 174 resource_query_unit(i)); 175 } 176 177#ifdef __i386__ 178 /* 179 * Foot protection... 180 */ 181 for (i = resource_locate(-1, "atkbd"); 182 i != -1; 183 i = resource_locate(i, "atkbd")) { 184 if (device_find_child(dev, resource_query_name(i), 185 resource_query_unit(i)) == NULL) { 186 printf("WARNING: Update your atkbdc config!\n"); 187 atkbdc_add_device(dev, resource_query_name(i), 188 resource_query_unit(i)); 189 } 190 } 191#endif 192 193 bus_generic_attach(dev); 194 195 return 0; 196} 197 198static void 199atkbdc_print_child(device_t bus, device_t dev) 200{ 201 atkbdc_device_t *kbdcdev; 202 203 kbdcdev = (atkbdc_device_t *)device_get_ivars(dev); 204 205 if (kbdcdev->flags != 0) 206 printf(" flags 0x%x", kbdcdev->flags); 207 if (kbdcdev->irq != -1) 208 printf(" irq %d", kbdcdev->irq); 209 210 printf(" on %s%d", device_get_name(bus), device_get_unit(bus)); 211} 212 213static int 214atkbdc_read_ivar(device_t bus, device_t dev, int index, u_long *val) 215{ 216 atkbdc_device_t *ivar; 217 218 ivar = (atkbdc_device_t *)device_get_ivars(dev); 219 switch (index) { 220 case KBDC_IVAR_PORT: 221 *val = (u_long)ivar->port; 222 break; 223 case KBDC_IVAR_IRQ: 224 *val = (u_long)ivar->irq; 225 break; 226 case KBDC_IVAR_FLAGS: 227 *val = (u_long)ivar->flags; 228 break; 229 default: 230 return ENOENT; 231 } 232 return 0; 233} 234 235static int 236atkbdc_write_ivar(device_t bus, device_t dev, int index, u_long val) 237{ 238 atkbdc_device_t *ivar; 239 240 ivar = (atkbdc_device_t *)device_get_ivars(dev); 241 switch (index) { 242 case KBDC_IVAR_PORT: 243 ivar->port = (int)val; 244 break; 245 case KBDC_IVAR_IRQ: 246 ivar->irq = (int)val; 247 break; 248 case KBDC_IVAR_FLAGS: 249 ivar->flags = (int)val; 250 break; 251 default: 252 return ENOENT; 253 } 254 return 0; 255} 256 257DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0); 258 259#endif /* NATKBDC > 0 */ 260