xref: /netbsd-src/sys/arch/newsmips/dev/kb_hb.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: kb_hb.c,v 1.2 2002/03/17 19:40:46 atatat 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/param.h>
30 #include <sys/device.h>
31 #include <sys/systm.h>
32 
33 #include <dev/wscons/wsconsio.h>
34 #include <dev/wscons/wskbdvar.h>
35 #include <dev/wscons/wsksymdef.h>
36 #include <dev/wscons/wsksymvar.h>
37 
38 #include <machine/autoconf.h>
39 #include <machine/adrsmap.h>
40 
41 struct kbreg {
42 	u_char kb_data;
43 	u_char kb_stat;
44 	u_char kb_reset;
45 	u_char kb_init;
46 };
47 
48 struct kb_hb_softc {
49 	struct device sc_dev;
50 	volatile struct kbreg *sc_reg;
51 	struct device *sc_wskbddev;
52 };
53 
54 int kb_hb_match(struct device *, struct cfdata *, void *);
55 void kb_hb_attach(struct device *, struct device *, void *);
56 int kb_hb_intr(void *);
57 
58 void kb_hb_cnattach(void);
59 void kb_hb_cngetc(void *, u_int *, int *);
60 void kb_hb_cnpollc(void *, int);
61 
62 int kb_hb_enable(void *, int);
63 void kb_hb_setleds(void *, int);
64 int kb_hb_ioctl(void *, u_long, caddr_t, int, struct proc *);
65 
66 extern struct wscons_keydesc newskb_keydesctab[];
67 
68 struct cfattach kb_hb_ca = {
69 	sizeof(struct kb_hb_softc), kb_hb_match, kb_hb_attach
70 };
71 
72 struct wskbd_accessops kb_hb_accessops = {
73 	kb_hb_enable,
74 	kb_hb_setleds,
75 	kb_hb_ioctl
76 };
77 
78 struct wskbd_consops kb_hb_consops = {
79 	kb_hb_cngetc,
80 	kb_hb_cnpollc
81 };
82 
83 struct wskbd_mapdata kb_hb_keymapdata = {
84 	newskb_keydesctab,
85 	KB_JP
86 };
87 
88 int
89 kb_hb_match(parent, cf, aux)
90 	struct device *parent;
91 	struct cfdata *cf;
92 	void *aux;
93 {
94 	struct confargs *ca = aux;
95 
96 	if (strcmp(ca->ca_name, "kb") == 0)
97 		return 1;
98 
99 	return 0;
100 }
101 
102 void
103 kb_hb_attach(parent, self, aux)
104 	struct device *parent, *self;
105 	void *aux;
106 {
107 	struct kb_hb_softc *sc = (void *)self;
108 	volatile struct kbreg *reg = (void *)self->dv_cfdata->cf_addr;
109 	int intr = self->dv_cfdata->cf_level;
110 	volatile int *dipsw = (void *)DIP_SWITCH;
111 	struct wskbddev_attach_args aa;
112 	int cons = 0;
113 
114 	if (intr == -1)
115 		intr = 2;
116 
117 	sc->sc_reg = reg;
118 	reg->kb_reset = 0x01;
119 	reg->kb_init = 0xf0;	/* 9600 bps */
120 
121 	printf(" level %d", intr);
122 	if (*dipsw & 7) {
123 		cons = 1;
124 		printf(" (console)");
125 	}
126 	printf("\n");
127 
128 	hb_intr_establish(intr, IPL_TTY, kb_hb_intr, sc);
129 
130 	aa.console = cons;
131 	aa.keymap = &kb_hb_keymapdata;
132 	aa.accessops = &kb_hb_accessops;
133 	aa.accesscookie = sc;
134 	sc->sc_wskbddev = config_found(self, &aa, wskbddevprint);
135 }
136 
137 int
138 kb_hb_intr(v)
139 	void *v;
140 {
141 	struct kb_hb_softc *sc = v;
142 	volatile struct kbreg *reg = sc->sc_reg;
143 	volatile u_char *ien = (void *)INTEN0;
144 	int code, type, release, val;
145 	int rv = 0;
146 
147 	*ien &= ~RX_KBINTE;
148 
149 	while (reg->kb_stat & RX_KBRDY) {
150 		code = reg->kb_data;
151 		release = code & 0x80;
152 		val = code & 0x7f;
153 		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
154 		if (sc->sc_wskbddev)
155 			wskbd_input(sc->sc_wskbddev, type, val);
156 		rv = 1;
157 	}
158 
159 	*ien |= RX_KBINTE;
160 	return rv;
161 }
162 
163 void
164 kb_hb_cnattach()
165 {
166 	volatile int *dipsw = (void *)DIP_SWITCH;
167 	volatile struct kbreg *reg = (void *)KEYB_DATA;
168 
169 	if (*dipsw & 7)
170 		wskbd_cnattach(&kb_hb_consops, (void *)reg, &kb_hb_keymapdata);
171 }
172 
173 void
174 kb_hb_cngetc(v, type, data)
175 	void *v;
176 	u_int *type;
177 	int *data;
178 {
179 	volatile struct kbreg *reg = v;
180 	volatile u_char *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
198 kb_hb_cnpollc(v, on)
199 	void *v;
200 	int on;
201 {
202 }
203 
204 int
205 kb_hb_enable(v, on)
206 	void *v;
207 	int on;
208 {
209 	return 0;
210 }
211 
212 void
213 kb_hb_setleds(v, on)
214 	void *v;
215 	int on;
216 {
217 }
218 
219 int
220 kb_hb_ioctl(v, cmd, data, flag, p)
221 	void *v;
222 	u_long cmd;
223 	caddr_t data;
224 	int flag;
225 	struct proc *p;
226 {
227 	switch (cmd) {
228 
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