xref: /freebsd/sys/dev/atkbdc/atkbdc_subr.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
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$
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>
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 
46 MALLOC_DEFINE(M_ATKBDDEV, "atkbddev", "AT Keyboard device");
47 
48 /* children */
49 typedef struct atkbdc_device {
50 	int flags;	/* configuration flags */
51 	int irq;	/* ISA IRQ mask */
52 	u_int32_t vendorid;
53 	u_int32_t serial;
54 	u_int32_t logicalid;
55 	u_int32_t compatid;
56 } atkbdc_device_t;
57 
58 /* kbdc */
59 devclass_t atkbdc_devclass;
60 
61 static int	atkbdc_probe(device_t dev);
62 static int	atkbdc_attach(device_t dev);
63 static int	atkbdc_print_child(device_t bus, device_t dev);
64 static int	atkbdc_read_ivar(device_t bus, device_t dev, int index,
65 				 uintptr_t *val);
66 static int	atkbdc_write_ivar(device_t bus, device_t dev, int index,
67 				  uintptr_t val);
68 
69 static device_method_t atkbdc_methods[] = {
70 	DEVMETHOD(device_probe,		atkbdc_probe),
71 	DEVMETHOD(device_attach,	atkbdc_attach),
72 	DEVMETHOD(device_suspend,	bus_generic_suspend),
73 	DEVMETHOD(device_resume,	bus_generic_resume),
74 
75 	DEVMETHOD(bus_print_child,	atkbdc_print_child),
76 	DEVMETHOD(bus_read_ivar,	atkbdc_read_ivar),
77 	DEVMETHOD(bus_write_ivar,	atkbdc_write_ivar),
78 	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
79 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
80 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
81 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
82 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
83 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
84 
85 	{ 0, 0 }
86 };
87 
88 static driver_t atkbdc_driver = {
89 	ATKBDC_DRIVER_NAME,
90 	atkbdc_methods,
91 	sizeof(atkbdc_softc_t *),
92 };
93 
94 static struct isa_pnp_id atkbdc_ids[] = {
95 	{ 0x0303d041, "Keyboard controller (i8042)" },	/* PNP0303 */
96 	{ 0 }
97 };
98 
99 static int
100 atkbdc_probe(device_t dev)
101 {
102 	struct resource	*port0;
103 	struct resource	*port1;
104 	int		error;
105 	int		rid;
106 
107 	/* check PnP IDs */
108 	if (ISA_PNP_PROBE(device_get_parent(dev), dev, atkbdc_ids) == ENXIO)
109 		return ENXIO;
110 
111 	device_set_desc(dev, "Keyboard controller (i8042)");
112 
113 	rid = 0;
114 	port0 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
115 				   RF_ACTIVE);
116 	if (port0 == NULL)
117 		return ENXIO;
118 	/* XXX */
119 	if (bus_get_resource_start(dev, SYS_RES_IOPORT, 1) <= 0) {
120 		bus_set_resource(dev, SYS_RES_IOPORT, 1,
121 				 rman_get_start(port0) + KBD_STATUS_PORT, 1);
122 	}
123 	rid = 1;
124 	port1 = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1,
125 				   RF_ACTIVE);
126 	if (port1 == NULL) {
127 		bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
128 		return ENXIO;
129 	}
130 
131 	error = atkbdc_probe_unit(device_get_unit(dev), port0, port1);
132 
133 	bus_release_resource(dev, SYS_RES_IOPORT, 0, port0);
134 	bus_release_resource(dev, SYS_RES_IOPORT, 1, port1);
135 
136 	return error;
137 }
138 
139 static void
140 atkbdc_add_device(device_t dev, const char *name, int unit)
141 {
142 	atkbdc_device_t	*kdev;
143 	device_t	child;
144 	int		t;
145 
146 	if (resource_int_value(name, unit, "disabled", &t) == 0 && t != 0)
147 		return;
148 
149 	kdev = malloc(sizeof(struct atkbdc_device), M_ATKBDDEV, M_NOWAIT);
150 	if (!kdev)
151 		return;
152 	bzero(kdev, sizeof *kdev);
153 
154 	if (resource_int_value(name, unit, "irq", &t) == 0)
155 		kdev->irq = t;
156 	else
157 		kdev->irq = -1;
158 
159 	if (resource_int_value(name, unit, "flags", &t) == 0)
160 		kdev->flags = t;
161 	else
162 		kdev->flags = 0;
163 
164 	child = device_add_child(dev, name, unit);
165 	device_set_ivars(child, kdev);
166 }
167 
168 static int
169 atkbdc_attach(device_t dev)
170 {
171 	atkbdc_softc_t	*sc;
172 	int		unit;
173 	int		error;
174 	int		rid;
175 	int		i;
176 	const char	*name;
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 	name = device_get_nameunit(dev);
218 	i = -1;
219 	while ((i = resource_query_string(i, "at", name)) != -1)
220 		atkbdc_add_device(dev, resource_query_name(i),
221 				  resource_query_unit(i));
222 
223 	/*
224 	 * and atkbdc?
225 	 */
226 	name = device_get_name(dev);
227 	i = -1;
228 	while ((i = resource_query_string(i, "at", name)) != -1)
229 		atkbdc_add_device(dev, resource_query_name(i),
230 				  resource_query_unit(i));
231 
232 	bus_generic_attach(dev);
233 
234 	return 0;
235 }
236 
237 static int
238 atkbdc_print_child(device_t bus, device_t dev)
239 {
240 	atkbdc_device_t *kbdcdev;
241 	int retval = 0;
242 
243 	kbdcdev = (atkbdc_device_t *)device_get_ivars(dev);
244 
245 	retval += bus_print_child_header(bus, dev);
246 	if (kbdcdev->flags != 0)
247 		retval += printf(" flags 0x%x", kbdcdev->flags);
248 	if (kbdcdev->irq != -1)
249 		retval += printf(" irq %d", kbdcdev->irq);
250 	retval += bus_print_child_footer(bus, dev);
251 
252 	return (retval);
253 }
254 
255 static int
256 atkbdc_read_ivar(device_t bus, device_t dev, int index, uintptr_t *val)
257 {
258 	atkbdc_device_t *ivar;
259 
260 	ivar = (atkbdc_device_t *)device_get_ivars(dev);
261 	switch (index) {
262 	case KBDC_IVAR_IRQ:
263 		*val = (u_long)ivar->irq;
264 		break;
265 	case KBDC_IVAR_FLAGS:
266 		*val = (u_long)ivar->flags;
267 		break;
268 	case KBDC_IVAR_VENDORID:
269 		*val = (u_long)ivar->vendorid;
270 		break;
271 	case KBDC_IVAR_SERIAL:
272 		*val = (u_long)ivar->serial;
273 		break;
274 	case KBDC_IVAR_LOGICALID:
275 		*val = (u_long)ivar->logicalid;
276 		break;
277 	case KBDC_IVAR_COMPATID:
278 		*val = (u_long)ivar->compatid;
279 		break;
280 	default:
281 		return ENOENT;
282 	}
283 	return 0;
284 }
285 
286 static int
287 atkbdc_write_ivar(device_t bus, device_t dev, int index, uintptr_t val)
288 {
289 	atkbdc_device_t *ivar;
290 
291 	ivar = (atkbdc_device_t *)device_get_ivars(dev);
292 	switch (index) {
293 	case KBDC_IVAR_IRQ:
294 		ivar->irq = (int)val;
295 		break;
296 	case KBDC_IVAR_FLAGS:
297 		ivar->flags = (int)val;
298 		break;
299 	case KBDC_IVAR_VENDORID:
300 		ivar->vendorid = (u_int32_t)val;
301 		break;
302 	case KBDC_IVAR_SERIAL:
303 		ivar->serial = (u_int32_t)val;
304 		break;
305 	case KBDC_IVAR_LOGICALID:
306 		ivar->logicalid = (u_int32_t)val;
307 		break;
308 	case KBDC_IVAR_COMPATID:
309 		ivar->compatid = (u_int32_t)val;
310 		break;
311 	default:
312 		return ENOENT;
313 	}
314 	return 0;
315 }
316 
317 DRIVER_MODULE(atkbdc, isa, atkbdc_driver, atkbdc_devclass, 0, 0);
318