xref: /freebsd/sys/dev/atkbdc/atkbd_atkbdc.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
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 void	atkbdidentify(driver_t *driver, device_t dev);
54 static int	atkbdprobe(device_t dev);
55 static int	atkbdattach(device_t dev);
56 static int	atkbdresume(device_t dev);
57 static void	atkbdintr(void *arg);
58 
59 static device_method_t atkbd_methods[] = {
60 	DEVMETHOD(device_identify,	atkbdidentify),
61 	DEVMETHOD(device_probe,		atkbdprobe),
62 	DEVMETHOD(device_attach,	atkbdattach),
63 	DEVMETHOD(device_resume,	atkbdresume),
64 	{ 0, 0 }
65 };
66 
67 static driver_t atkbd_driver = {
68 	ATKBD_DRIVER_NAME,
69 	atkbd_methods,
70 	sizeof(atkbd_softc_t),
71 };
72 
73 static void
atkbdidentify(driver_t * driver,device_t parent)74 atkbdidentify(driver_t *driver, device_t parent)
75 {
76 
77 	/* always add at least one child */
78 	BUS_ADD_CHILD(parent, KBDC_RID_KBD, driver->name, device_get_unit(parent));
79 }
80 
81 static int
atkbdprobe(device_t dev)82 atkbdprobe(device_t dev)
83 {
84 	struct resource *res;
85 	u_long irq;
86 	int flags;
87 	int rid;
88 
89 	device_set_desc(dev, "AT Keyboard");
90 
91 	/* obtain parameters */
92 	flags = device_get_flags(dev);
93 
94 	/* see if IRQ is available */
95 	rid = KBDC_RID_KBD;
96 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
97 	if (res == NULL) {
98 		if (bootverbose)
99 			device_printf(dev, "unable to allocate IRQ\n");
100 		return ENXIO;
101 	}
102 	irq = rman_get_start(res);
103 	bus_release_resource(dev, SYS_RES_IRQ, rid, res);
104 
105 	/* probe the device */
106 	return atkbd_probe_unit(dev, irq, flags);
107 }
108 
109 static int
atkbdattach(device_t dev)110 atkbdattach(device_t dev)
111 {
112 	atkbd_softc_t *sc;
113 	keyboard_t *kbd;
114 	u_long irq;
115 	int flags;
116 	int rid;
117 	int error;
118 
119 	sc = device_get_softc(dev);
120 
121 	rid = KBDC_RID_KBD;
122 	irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
123 	flags = device_get_flags(dev);
124 	error = atkbd_attach_unit(dev, &kbd, irq, flags);
125 	if (error)
126 		return error;
127 
128 	/* declare our interrupt handler */
129 	sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
130 	if (sc->intr == NULL)
131 		return ENXIO;
132 	error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, atkbdintr,
133 			       kbd, &sc->ih);
134 	if (error)
135 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
136 
137 	return error;
138 }
139 
140 static int
atkbdresume(device_t dev)141 atkbdresume(device_t dev)
142 {
143 	atkbd_softc_t *sc;
144 	keyboard_t *kbd;
145 	int args[2];
146 
147 	sc = device_get_softc(dev);
148 	kbd = kbd_get_keyboard(kbd_find_keyboard(ATKBD_DRIVER_NAME,
149 						 device_get_unit(dev)));
150 	if (kbd) {
151 		kbd->kb_flags &= ~KB_INITIALIZED;
152 		args[0] = device_get_unit(device_get_parent(dev));
153 		args[1] = rman_get_start(sc->intr);
154 		kbdd_init(kbd, device_get_unit(dev), &kbd, args,
155 		    device_get_flags(dev));
156 		kbdd_clear_state(kbd);
157 	}
158 	return 0;
159 }
160 
161 static void
atkbdintr(void * arg)162 atkbdintr(void *arg)
163 {
164 	keyboard_t *kbd;
165 
166 	kbd = (keyboard_t *)arg;
167 	kbdd_intr(kbd, NULL);
168 }
169 
170 DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, 0, 0);
171 #ifdef EVDEV_SUPPORT
172 MODULE_DEPEND(atkbd, evdev, 1, 1, 1);
173 #endif
174