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: src/sys/isa/atkbdc_isa.c,v 1.14.2.1 2000/03/31 12:52:05 yokota Exp $
27  * $DragonFly: src/sys/dev/misc/atkbdc_layer/atkbdc_isa.c,v 1.3 2003/08/07 21:16:56 dillon Exp $
28  */
29 
30 #include "opt_kbd.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include <machine/bus_pio.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41 
42 #include <dev/misc/kbd/atkbdcreg.h>
43 
44 #include <bus/isa/isareg.h>
45 #include <bus/isa/isavar.h>
46 
47 MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
48 
49 /* children */
50 typedef struct atkbdc_device {
51 	int flags;	/* configuration flags */
52 	int irq;	/* ISA IRQ mask */
53 	u_int32_t vendorid;
54 	u_int32_t serial;
55 	u_int32_t logicalid;
56 	u_int32_t compatid;
57 } atkbdc_device_t;
58 
59 /* kbdc */
60 devclass_t atkbdc_devclass;
61 
62 static int	atkbdc_probe(device_t dev);
63 static int	atkbdc_attach(device_t dev);
64 static int	atkbdc_print_child(device_t bus, device_t dev);
65 static int	atkbdc_read_ivar(device_t bus, device_t dev, int index,
66 				 u_long *val);
67 static int	atkbdc_write_ivar(device_t bus, device_t dev, int index,
68 				  u_long val);
69 
70 static device_method_t atkbdc_methods[] = {
71 	DEVMETHOD(device_probe,		atkbdc_probe),
72 	DEVMETHOD(device_attach,	atkbdc_attach),
73 	DEVMETHOD(device_suspend,	bus_generic_suspend),
74 	DEVMETHOD(device_resume,	bus_generic_resume),
75 
76 	DEVMETHOD(bus_print_child,	atkbdc_print_child),
77 	DEVMETHOD(bus_read_ivar,	atkbdc_read_ivar),
78 	DEVMETHOD(bus_write_ivar,	atkbdc_write_ivar),
79 	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
80 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
81 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
82 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
83 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
84 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
85 
86 	{ 0, 0 }
87 };
88 
89 static driver_t atkbdc_driver = {
90 	ATKBDC_DRIVER_NAME,
91 	atkbdc_methods,
92 	sizeof(atkbdc_softc_t *),
93 };
94 
95 static struct isa_pnp_id atkbdc_ids[] = {
96 	{ 0x0303d041, "Keyboard controller (i8042)" },	/* PNP0303 */
97 	{ 0 }
98 };
99 
100 static int
101 atkbdc_probe(device_t dev)
102 {
103 	struct resource	*port0;
104 	struct resource	*port1;
105 	int		error;
106 	int		rid;
107 
108 	/* check PnP IDs */
109 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
110 		return ENXIO;
111 
112 	device_set_desc(dev, "Keyboard controller (i8042)");
113 
114 	rid = 0;
115 	port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
116 				   RF_ACTIVE);
117 	if (port0 == NULL)
118 		return ENXIO;
119 	/* XXX */
120 	if (bus_get_resource_start(dev, SYS_RES_IOPORT, 1) <= 0) {
121 		bus_set_resource(dev, SYS_RES_IOPORT, 1,
122 				 rman_get_start(port0) + KBD_STATUS_PORT, 1);
123 	}
124 	rid = 1;
125 	port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
126 				   RF_ACTIVE);
127 	if (port1 == NULL) {
128 		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
129 		return ENXIO;
130 	}
131 
132 	error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
133 
134 	bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
135 	bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
136 
137 	return error;
138 }
139 
140 static void
141 atkbdc_add_device(device_t dev, const char *name, int unit)
142 {
143 	atkbdc_device_t	*kdev;
144 	device_t	child;
145 	int		t;
146 
147 	if (resource_int_value(name, unit, "disabled", &t) == 0 && t != 0)
148 		return;
149 
150 	kdev = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, M_NOWAIT);
151 	if (!kdev)
152 		return;
153 	bzero(kdev, sizeof *kdev);
154 
155 	if (resource_int_value(name, unit, "irq", &t) == 0)
156 		kdev->irq = t;
157 	else
158 		kdev->irq = -1;
159 
160 	if (resource_int_value(name, unit, "flags", &t) == 0)
161 		kdev->flags = t;
162 	else
163 		kdev->flags = 0;
164 
165 	child = device_add_child(dev, name, unit);
166 	device_set_ivars(child, kdev);
167 }
168 
169 static int
170 atkbdc_attach(device_t dev)
171 {
172 	atkbdc_softc_t	*sc;
173 	int		unit;
174 	int		error;
175 	int		rid;
176 	int		i;
177 
178 	unit = device_get_unit(dev);
179 	sc = *(atkbdc_softc_t **)device_get_softc(dev);
180 	if (sc == NULL) {
181 		/*
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.
186 		 * See atkbdc.c. XXX
187 		 */
188 		sc = atkbdc_get_softc(unit);
189 		if (sc == NULL)
190 			return ENOMEM;
191 	}
192 
193 	rid = 0;
194 	sc->port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
195 				       RF_ACTIVE);
196 	if (sc->port0 == NULL)
197 		return ENXIO;
198 	rid = 1;
199 	sc->port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
200 				       RF_ACTIVE);
201 	if (sc->port1 == NULL) {
202 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
203 		return ENXIO;
204 	}
205 
206 	error = atkbdc_attach_unit(unit, sc, sc->port0, sc->port1);
207 	if (error) {
208 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->port0);
209 		bus_release_resource(dev, SYS_RES_IOPORT, 1, sc->port1);
210 		return error;
211 	}
212 	*(atkbdc_softc_t **)device_get_softc(dev) = sc;
213 
214 	/*
215 	 * Add all devices configured to be attached to atkbdc0.
216 	 */
217 	for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
218 	     i != -1;
219 	     i = resource_query_string(i, "at", device_get_nameunit(dev))) {
220 		atkbdc_add_device(dev, resource_query_name(i),
221 				  resource_query_unit(i));
222 	}
223 
224 	/*
225 	 * and atkbdc?
226 	 */
227 	for (i = resource_query_string(-1, "at", device_get_name(dev));
228 	     i != -1;
229 	     i = resource_query_string(i, "at", device_get_name(dev))) {
230 		atkbdc_add_device(dev, resource_query_name(i),
231 				  resource_query_unit(i));
232 	}
233 
234 	bus_generic_attach(dev);
235 
236 	return 0;
237 }
238 
239 static int
240 atkbdc_print_child(device_t bus, device_t dev)
241 {
242 	atkbdc_device_t *kbdcdev;
243 	int retval = 0;
244 
245 	kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
246 
247 	retval += bus_print_child_header(bus, dev);
248 	if (kbdcdev->flags != 0)
249 		retval += printf(" flags 0x%x", kbdcdev->flags);
250 	if (kbdcdev->irq != -1)
251 		retval += printf(" irq %d", kbdcdev->irq);
252 	retval += bus_print_child_footer(bus, dev);
253 
254 	return (retval);
255 }
256 
257 static int
258 atkbdc_read_ivar(device_t bus, device_t dev, int index, u_long *val)
259 {
260 	atkbdc_device_t *ivar;
261 
262 	ivar = (atkbdc_device_t *)device_get_ivars(dev);
263 	switch (index) {
264 	case KBDC_IVAR_IRQ:
265 		*val = (u_long)ivar->irq;
266 		break;
267 	case KBDC_IVAR_FLAGS:
268 		*val = (u_long)ivar->flags;
269 		break;
270 	case KBDC_IVAR_VENDORID:
271 		*val = (u_long)ivar->vendorid;
272 		break;
273 	case KBDC_IVAR_SERIAL:
274 		*val = (u_long)ivar->serial;
275 		break;
276 	case KBDC_IVAR_LOGICALID:
277 		*val = (u_long)ivar->logicalid;
278 		break;
279 	case KBDC_IVAR_COMPATID:
280 		*val = (u_long)ivar->compatid;
281 		break;
282 	default:
283 		return ENOENT;
284 	}
285 	return 0;
286 }
287 
288 static int
289 atkbdc_write_ivar(device_t bus, device_t dev, int index, u_long val)
290 {
291 	atkbdc_device_t *ivar;
292 
293 	ivar = (atkbdc_device_t *)device_get_ivars(dev);
294 	switch (index) {
295 	case KBDC_IVAR_IRQ:
296 		ivar->irq = (int)val;
297 		break;
298 	case KBDC_IVAR_FLAGS:
299 		ivar->flags = (int)val;
300 		break;
301 	case KBDC_IVAR_VENDORID:
302 		ivar->vendorid = (u_int32_t)val;
303 		break;
304 	case KBDC_IVAR_SERIAL:
305 		ivar->serial = (u_int32_t)val;
306 		break;
307 	case KBDC_IVAR_LOGICALID:
308 		ivar->logicalid = (u_int32_t)val;
309 		break;
310 	case KBDC_IVAR_COMPATID:
311 		ivar->compatid = (u_int32_t)val;
312 		break;
313 	default:
314 		return ENOENT;
315 	}
316 	return 0;
317 }
318 
319 DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);
320