1 /* $NetBSD: kb_hb.c,v 1.15 2021/08/07 16:19:01 thorpej 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_hb.c,v 1.15 2021/08/07 16:19:01 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35
36 #include <dev/wscons/wsconsio.h>
37 #include <dev/wscons/wskbdvar.h>
38 #include <dev/wscons/wsksymdef.h>
39 #include <dev/wscons/wsksymvar.h>
40
41 #include <machine/adrsmap.h>
42
43 #include <newsmips/dev/hbvar.h>
44
45 struct kbreg {
46 volatile uint8_t kb_data;
47 volatile uint8_t kb_stat;
48 volatile uint8_t kb_reset;
49 volatile uint8_t kb_init;
50 };
51
52 struct kb_hb_softc {
53 device_t sc_dev;
54 struct kbreg *sc_reg;
55 device_t sc_wskbddev;
56 };
57
58 int kb_hb_match(device_t, cfdata_t, void *);
59 void kb_hb_attach(device_t, device_t, void *);
60 int kb_hb_intr(void *);
61
62 void kb_hb_cnattach(void);
63 void kb_hb_cngetc(void *, u_int *, int *);
64 void kb_hb_cnpollc(void *, int);
65
66 int kb_hb_enable(void *, int);
67 void kb_hb_setleds(void *, int);
68 int kb_hb_ioctl(void *, u_long, void *, int, struct lwp *);
69
70 extern struct wscons_keydesc newskb_keydesctab[];
71
72 CFATTACH_DECL_NEW(kb_hb, sizeof(struct kb_hb_softc),
73 kb_hb_match, kb_hb_attach, NULL, NULL);
74
75 struct wskbd_accessops kb_hb_accessops = {
76 kb_hb_enable,
77 kb_hb_setleds,
78 kb_hb_ioctl
79 };
80
81 struct wskbd_consops kb_hb_consops = {
82 kb_hb_cngetc,
83 kb_hb_cnpollc
84 };
85
86 struct wskbd_mapdata kb_hb_keymapdata = {
87 newskb_keydesctab,
88 KB_JP
89 };
90
91 int
kb_hb_match(device_t parent,cfdata_t cf,void * aux)92 kb_hb_match(device_t parent, cfdata_t cf, void *aux)
93 {
94 struct hb_attach_args *ha = aux;
95
96 if (strcmp(ha->ha_name, "kb") == 0)
97 return 1;
98
99 return 0;
100 }
101
102 void
kb_hb_attach(device_t parent,device_t self,void * aux)103 kb_hb_attach(device_t parent, device_t self, void *aux)
104 {
105 struct kb_hb_softc *sc = device_private(self);
106 struct hb_attach_args *ha = aux;
107 struct kbreg *reg;
108 volatile uint32_t *dipsw = (void *)DIP_SWITCH;
109 struct wskbddev_attach_args aa;
110 int intr, cons;
111
112 sc->sc_dev = self;
113
114 reg = (struct kbreg *)ha->ha_addr;
115 intr = ha->ha_level;
116
117 if (intr == -1)
118 intr = 2;
119
120 sc->sc_reg = reg;
121 reg->kb_reset = 0x01;
122 reg->kb_init = 0xf0; /* 9600 bps */
123
124 aprint_normal(" level %d", intr);
125 cons = 0;
126 if (*dipsw & 7) {
127 cons = 1;
128 aprint_normal(" (console)");
129 }
130 aprint_normal("\n");
131
132 hb_intr_establish(intr, INTEN0_KBDINT, IPL_TTY, kb_hb_intr, sc);
133
134 aa.console = cons;
135 aa.keymap = &kb_hb_keymapdata;
136 aa.accessops = &kb_hb_accessops;
137 aa.accesscookie = sc;
138 sc->sc_wskbddev = config_found(self, &aa, wskbddevprint, CFARGS_NONE);
139 }
140
141 int
kb_hb_intr(void * v)142 kb_hb_intr(void *v)
143 {
144 struct kb_hb_softc *sc = v;
145 struct kbreg *reg = sc->sc_reg;
146 volatile uint8_t *ien = (void *)INTEN0;
147 int code, type, release, val;
148 int rv = 0;
149
150 *ien &= ~RX_KBINTE;
151
152 while (reg->kb_stat & RX_KBRDY) {
153 code = reg->kb_data;
154 release = code & 0x80;
155 val = code & 0x7f;
156 type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
157 if (sc->sc_wskbddev)
158 wskbd_input(sc->sc_wskbddev, type, val);
159 rv = 1;
160 }
161
162 *ien |= RX_KBINTE;
163 return rv;
164 }
165
166 void
kb_hb_cnattach(void)167 kb_hb_cnattach(void)
168 {
169 volatile uint32_t *dipsw = (void *)DIP_SWITCH;
170
171 if (*dipsw & 7)
172 wskbd_cnattach(&kb_hb_consops, (void *)KEYB_DATA,
173 &kb_hb_keymapdata);
174 }
175
176 void
kb_hb_cngetc(void * v,u_int * type,int * data)177 kb_hb_cngetc(void *v, u_int *type, int *data)
178 {
179 struct kbreg *reg = v;
180 volatile uint8_t *ien = (void *)INTEN0;
181 int code, release, ointr;
182
183 ointr = *ien & RX_KBINTE;
184 *ien &= ~RX_KBINTE;
185
186 /* Wait for key data. */
187 while ((reg->kb_stat & RX_KBRDY) == 0);
188
189 code = reg->kb_data;
190 release = code & 0x80;
191 *data = code & 0x7f;
192 *type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
193
194 *ien |= ointr;
195 }
196
197 void
kb_hb_cnpollc(void * v,int on)198 kb_hb_cnpollc(void *v, int on)
199 {
200 }
201
202 int
kb_hb_enable(void * v,int on)203 kb_hb_enable(void *v, int on)
204 {
205
206 return 0;
207 }
208
209 void
kb_hb_setleds(void * v,int on)210 kb_hb_setleds(void *v, int on)
211 {
212 }
213
214 int
kb_hb_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)215 kb_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
216 {
217
218 switch (cmd) {
219
220 case WSKBDIO_GTYPE:
221 *(int *)data = 0; /* XXX */
222 return 0;
223 case WSKBDIO_SETLEDS:
224 return 0;
225 case WSKBDIO_GETLEDS:
226 *(int *)data = 0;
227 return 0;
228 }
229
230 return EPASSTHROUGH;
231 }
232