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