xref: /freebsd/sys/dev/atkbdc/atkbd_atkbdc.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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$
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 
36 #include <machine/bus.h>
37 #include <machine/resource.h>
38 #include <sys/rman.h>
39 
40 #include <dev/kbd/kbdreg.h>
41 #include <dev/kbd/atkbdreg.h>
42 #include <dev/kbd/atkbdcreg.h>
43 
44 #include <isa/isareg.h>
45 #include <isa/isavar.h>
46 
47 typedef struct {
48 	struct resource	*intr;
49 	void		*ih;
50 } atkbd_softc_t;
51 
52 devclass_t	atkbd_devclass;
53 
54 static int	atkbdprobe(device_t dev);
55 static int	atkbdattach(device_t dev);
56 static int	atkbdresume(device_t dev);
57 static void	atkbd_isa_intr(void *arg);
58 
59 static device_method_t atkbd_methods[] = {
60 	DEVMETHOD(device_probe,		atkbdprobe),
61 	DEVMETHOD(device_attach,	atkbdattach),
62 	DEVMETHOD(device_resume,	atkbdresume),
63 	{ 0, 0 }
64 };
65 
66 static driver_t atkbd_driver = {
67 	ATKBD_DRIVER_NAME,
68 	atkbd_methods,
69 	sizeof(atkbd_softc_t),
70 };
71 
72 static int
73 atkbdprobe(device_t dev)
74 {
75 	uintptr_t irq;
76 	uintptr_t flags;
77 
78 	device_set_desc(dev, "AT Keyboard");
79 
80 	/* obtain parameters */
81 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
82 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
83 
84 	/* probe the device */
85 	return atkbd_probe_unit(device_get_unit(dev),
86 				device_get_unit(device_get_parent(dev)),
87 				irq, flags);
88 }
89 
90 static int
91 atkbdattach(device_t dev)
92 {
93 	atkbd_softc_t *sc;
94 	keyboard_t *kbd;
95 	uintptr_t irq;
96 	uintptr_t flags;
97 	int rid;
98 	int error;
99 
100 	sc = device_get_softc(dev);
101 
102 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
103 	BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
104 
105 	error = atkbd_attach_unit(device_get_unit(dev), &kbd,
106 				  device_get_unit(device_get_parent(dev)),
107 				  irq, flags);
108 	if (error)
109 		return error;
110 
111 	/* declare our interrupt handler */
112 	rid = 0;
113 	sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
114 				      RF_SHAREABLE | RF_ACTIVE);
115 	BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr, INTR_TYPE_TTY,
116 		       atkbd_isa_intr, kbd, &sc->ih);
117 
118 	return 0;
119 }
120 
121 static int
122 atkbdresume(device_t dev)
123 {
124 	keyboard_t *kbd;
125 
126 	kbd = kbd_get_keyboard(kbd_find_keyboard(ATKBD_DRIVER_NAME,
127 						 device_get_unit(dev)));
128 	if (kbd)
129 		(*kbdsw[kbd->kb_index]->clear_state)(kbd);
130 	return 0;
131 }
132 
133 static void
134 atkbd_isa_intr(void *arg)
135 {
136 	keyboard_t *kbd;
137 
138 	kbd = (keyboard_t *)arg;
139 	(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
140 }
141 
142 DRIVER_MODULE(atkbd, atkbdc, atkbd_driver, atkbd_devclass, 0, 0);
143