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