xref: /freebsd/sys/dev/atkbdc/atkbd_atkbdc.c (revision 396c556d77189a5c474d35cec6f44a762e310b7d)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_kbd.h"
31 #include "opt_evdev.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <sys/kbio.h>
44 #include <dev/kbd/kbdreg.h>
45 #include <dev/atkbdc/atkbdreg.h>
46 #include <dev/atkbdc/atkbdcreg.h>
47 
48 typedef struct {
49 	struct resource	*intr;
50 	void		*ih;
51 } atkbd_softc_t;
52 
53 static devclass_t	atkbd_devclass;
54 
55 static void	atkbdidentify(driver_t *driver, device_t dev);
56 static int	atkbdprobe(device_t dev);
57 static int	atkbdattach(device_t dev);
58 static int	atkbdresume(device_t dev);
59 static void	atkbdintr(void *arg);
60 
61 static device_method_t atkbd_methods[] = {
62 	DEVMETHOD(device_identify,	atkbdidentify),
63 	DEVMETHOD(device_probe,		atkbdprobe),
64 	DEVMETHOD(device_attach,	atkbdattach),
65 	DEVMETHOD(device_resume,	atkbdresume),
66 	{ 0, 0 }
67 };
68 
69 static driver_t atkbd_driver = {
70 	ATKBD_DRIVER_NAME,
71 	atkbd_methods,
72 	sizeof(atkbd_softc_t),
73 };
74 
75 static void
76 atkbdidentify(driver_t *driver, device_t parent)
77 {
78 
79 	/* always add at least one child */
80 	BUS_ADD_CHILD(parent, KBDC_RID_KBD, driver->name, device_get_unit(parent));
81 }
82 
83 static int
84 atkbdprobe(device_t dev)
85 {
86 	struct resource *res;
87 	u_long irq;
88 	int flags;
89 	int rid;
90 
91 	device_set_desc(dev, "AT Keyboard");
92 
93 	/* obtain parameters */
94 	flags = device_get_flags(dev);
95 
96 	/* see if IRQ is available */
97 	rid = KBDC_RID_KBD;
98 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
99 	if (res == NULL) {
100 		if (bootverbose)
101 			device_printf(dev, "unable to allocate IRQ\n");
102 		return ENXIO;
103 	}
104 	irq = rman_get_start(res);
105 	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
106 
107 	/* probe the device */
108 	return atkbd_probe_unit(dev, irq, flags);
109 }
110 
111 static int
112 atkbdattach(device_t dev)
113 {
114 	atkbd_softc_t *sc;
115 	keyboard_t *kbd;
116 	u_long irq;
117 	int flags;
118 	int rid;
119 	int error;
120 
121 	sc = device_get_softc(dev);
122 
123 	rid = KBDC_RID_KBD;
124 	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
125 	flags = device_get_flags(dev);
126 	error = atkbd_attach_unit(dev, &kbd, irq, flags);
127 	if (error)
128 		return error;
129 
130 	/* declare our interrupt handler */
131 	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
132 	if (sc->intr == NULL)
133 		return ENXIO;
134 	error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, atkbdintr,
135 			       kbd, &sc->ih);
136 	if (error)
137 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
138 
139 	return error;
140 }
141 
142 static int
143 atkbdresume(device_t dev)
144 {
145 	atkbd_softc_t *sc;
146 	keyboard_t *kbd;
147 	int args[2];
148 
149 	sc = device_get_softc(dev);
150 	kbd = kbd_get_keyboard(kbd_find_keyboard(ATKBD_DRIVER_NAME,
151 						 device_get_unit(dev)));
152 	if (kbd) {
153 		kbd->kb_flags &= ~KB_INITIALIZED;
154 		args[0] = device_get_unit(device_get_parent(dev));
155 		args[1] = rman_get_start(sc->intr);
156 		kbdd_init(kbd, device_get_unit(dev), &kbd, args,
157 		    device_get_flags(dev));
158 		kbdd_clear_state(kbd);
159 	}
160 	return 0;
161 }
162 
163 static void
164 atkbdintr(void *arg)
165 {
166 	keyboard_t *kbd;
167 
168 	kbd = (keyboard_t *)arg;
169 	kbdd_intr(kbd, NULL);
170 }
171 
172 DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, atkbd_devclass, 0, 0);
173 #ifdef EVDEV_SUPPORT
174 MODULE_DEPEND(atkbd, evdev, 1, 1, 1);
175 #endif
176