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