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