kb_ap.c revision 1.8 1 /* $NetBSD: kb_ap.c,v 1.8 2007/03/04 06:00:26 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: kb_ap.c,v 1.8 2007/03/04 06:00:26 christos Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35
36 #include <dev/wscons/wsconsio.h>
37 #include <dev/wscons/wskbdvar.h>
38 #include <dev/wscons/wsksymdef.h>
39
40 #include <machine/adrsmap.h>
41 #include <newsmips/apbus/apbusvar.h>
42
43 struct kbreg {
44 u_int kb_rx_data;
45 u_int kb_rx_stat;
46 u_int kb_rx_intr_en;
47 u_int kb_rx_reset;
48 u_int kb_rx_speed;
49
50 u_int ms_rx_data;
51 u_int ms_rx_stat;
52 u_int ms_rx_intr_en;
53 u_int ms_rx_reset;
54 u_int ms_rx_speed;
55
56 u_int kb_buzzf;
57 u_int kb_buzz;
58
59 u_int kb_tx_data;
60 u_int kb_tx_stat;
61 u_int kb_tx_intr_en;
62 u_int kb_tx_reset;
63 u_int kb_tx_speed;
64 };
65
66 struct kb_ap_softc {
67 struct device sc_dev;
68 volatile struct kbreg *sc_reg;
69 struct device *sc_wskbddev;
70 };
71
72 int kb_ap_match(struct device *, struct cfdata *, void *);
73 void kb_ap_attach(struct device *, struct device *, void *);
74 int kb_ap_intr(void *);
75
76 void kb_ap_cnattach(void);
77 void kb_ap_cngetc(void *, u_int *, int *);
78 void kb_ap_cnpollc(void *, int);
79
80 int kb_ap_enable(void *, int);
81 void kb_ap_setleds(void *, int);
82 int kb_ap_ioctl(void *, u_long, void *, int, struct lwp *);
83
84 extern struct wscons_keydesc newskb_keydesctab[];
85
86 CFATTACH_DECL(kb_ap, sizeof(struct kb_ap_softc),
87 kb_ap_match, kb_ap_attach, NULL, NULL);
88
89 struct wskbd_accessops kb_ap_accessops = {
90 kb_ap_enable,
91 kb_ap_setleds,
92 kb_ap_ioctl,
93 };
94
95 struct wskbd_consops kb_ap_consops = {
96 kb_ap_cngetc,
97 kb_ap_cnpollc,
98 };
99
100 struct wskbd_mapdata kb_ap_keymapdata = {
101 newskb_keydesctab,
102 KB_JP,
103 };
104
105 int
106 kb_ap_match(struct device *parent, struct cfdata *cf, void *aux)
107 {
108 struct apbus_attach_args *apa = aux;
109
110 if (strcmp(apa->apa_name, "kb") == 0)
111 return 1;
112
113 return 0;
114 }
115
116 void
117 kb_ap_attach(struct device *parent, struct device *self, void *aux)
118 {
119 struct kb_ap_softc *sc = (void *)self;
120 struct apbus_attach_args *apa = aux;
121 volatile struct kbreg *reg = (void *)apa->apa_hwbase;
122 volatile int *dipsw = (void *)NEWS5000_DIP_SWITCH;
123 struct wskbddev_attach_args waa;
124 int cons = 0;
125
126 printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
127
128 sc->sc_reg = reg;
129
130 if (*dipsw & 7) {
131 printf(" (console)");
132 cons = 1;
133 }
134 printf("\n");
135
136 reg->kb_rx_reset = 0x03;
137 reg->kb_tx_reset = 0x03;
138
139 reg->kb_rx_speed = 0x04;
140 reg->kb_tx_speed = 0x04;
141
142 reg->kb_rx_intr_en = 1;
143 reg->kb_tx_intr_en = 0;
144
145 apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, kb_ap_intr, sc,
146 "", apa->apa_ctlnum);
147
148 waa.console = cons;
149 waa.keymap = &kb_ap_keymapdata;
150 waa.accessops = &kb_ap_accessops;
151 waa.accesscookie = sc;
152
153 sc->sc_wskbddev = config_found(self, &waa, wskbddevprint);
154 }
155
156 int
157 kb_ap_intr(void *v)
158 {
159 struct kb_ap_softc *sc = v;
160 volatile struct kbreg *reg = sc->sc_reg;
161 int key, val, type, release;
162 int rv = 0;
163
164 while (reg->kb_rx_stat & RX_KBRDY) {
165 key = reg->kb_rx_data;
166 val = key & 0x7f;
167 release = key & 0x80;
168 type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
169
170 if (sc->sc_wskbddev)
171 wskbd_input(sc->sc_wskbddev, type, val);
172
173 rv = 1;
174 }
175
176 return rv;
177 }
178
179 void
180 kb_ap_cnattach(void)
181 {
182
183 wskbd_cnattach(&kb_ap_consops, (void *)0xbf900000, &kb_ap_keymapdata);
184 }
185
186 void
187 kb_ap_cngetc(void *v, u_int *type, int *data)
188 {
189 volatile struct kbreg *reg = v;
190 int key, release, ointr;
191
192 /* Disable keyboard interrupt. */
193 ointr = reg->kb_rx_intr_en;
194 reg->kb_rx_intr_en = 0;
195
196 /* Wait for key data. */
197 while ((reg->kb_rx_stat & RX_KBRDY) == 0);
198
199 key = reg->kb_rx_data;
200 release = key & 0x80;
201 *data = key & 0x7f;
202 *type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
203
204 reg->kb_rx_intr_en = ointr;
205 }
206
207 void
208 kb_ap_cnpollc(void *v, int on)
209 {
210 }
211
212 int
213 kb_ap_enable(void *v, int on)
214 {
215
216 return 0;
217 }
218
219 void
220 kb_ap_setleds(void *v, int on)
221 {
222 }
223
224 int
225 kb_ap_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
226 {
227
228 switch (cmd) {
229 case WSKBDIO_GTYPE:
230 *(int *)data = 0; /* XXX */
231 return 0;
232 case WSKBDIO_SETLEDS:
233 return 0;
234 case WSKBDIO_GETLEDS:
235 *(int *)data = 0;
236 return 0;
237 }
238
239 return EPASSTHROUGH;
240 }
241