xref: /netbsd-src/sys/arch/newsmips/dev/kb_hb.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: kb_hb.c,v 1.11 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_hb.c,v 1.11 2007/03/04 06:00:26 christos 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 	u_char kb_data;
47 	u_char kb_stat;
48 	u_char kb_reset;
49 	u_char kb_init;
50 };
51 
52 struct kb_hb_softc {
53 	struct device sc_dev;
54 	volatile struct kbreg *sc_reg;
55 	struct device *sc_wskbddev;
56 };
57 
58 int kb_hb_match(struct device *, struct cfdata *, void *);
59 void kb_hb_attach(struct device *, struct device *, 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(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
92 kb_hb_match(struct device *parent, struct cfdata *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
103 kb_hb_attach(struct device *parent, struct device *self, void *aux)
104 {
105 	struct kb_hb_softc *sc = (void *)self;
106 	struct hb_attach_args *ha = aux;
107 	volatile struct kbreg *reg;
108 	volatile int *dipsw = (void *)DIP_SWITCH;
109 	struct wskbddev_attach_args aa;
110 	int intr, cons;
111 
112 	reg = (struct kbreg *)ha->ha_addr;
113 	intr = ha->ha_level;
114 
115 	if (intr == -1)
116 		intr = 2;
117 
118 	sc->sc_reg = reg;
119 	reg->kb_reset = 0x01;
120 	reg->kb_init = 0xf0;	/* 9600 bps */
121 
122 	printf(" level %d", intr);
123 	cons = 0;
124 	if (*dipsw & 7) {
125 		cons = 1;
126 		printf(" (console)");
127 	}
128 	printf("\n");
129 
130 	hb_intr_establish(intr, INTEN0_KBDINT, IPL_TTY, kb_hb_intr, sc);
131 
132 	aa.console = cons;
133 	aa.keymap = &kb_hb_keymapdata;
134 	aa.accessops = &kb_hb_accessops;
135 	aa.accesscookie = sc;
136 	sc->sc_wskbddev = config_found(self, &aa, wskbddevprint);
137 }
138 
139 int
140 kb_hb_intr(void *v)
141 {
142 	struct kb_hb_softc *sc = v;
143 	volatile struct kbreg *reg = sc->sc_reg;
144 	volatile u_char *ien = (void *)INTEN0;
145 	int code, type, release, val;
146 	int rv = 0;
147 
148 	*ien &= ~RX_KBINTE;
149 
150 	while (reg->kb_stat & RX_KBRDY) {
151 		code = reg->kb_data;
152 		release = code & 0x80;
153 		val = code & 0x7f;
154 		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
155 		if (sc->sc_wskbddev)
156 			wskbd_input(sc->sc_wskbddev, type, val);
157 		rv = 1;
158 	}
159 
160 	*ien |= RX_KBINTE;
161 	return rv;
162 }
163 
164 void
165 kb_hb_cnattach(void)
166 {
167 	volatile int *dipsw = (void *)DIP_SWITCH;
168 
169 	if (*dipsw & 7)
170 		wskbd_cnattach(&kb_hb_consops, (void *)KEYB_DATA,
171 		    &kb_hb_keymapdata);
172 }
173 
174 void
175 kb_hb_cngetc(void *v, u_int *type, int *data)
176 {
177 	volatile struct kbreg *reg = v;
178 	volatile u_char *ien = (void *)INTEN0;
179 	int code, release, ointr;
180 
181 	ointr = *ien & RX_KBINTE;
182 	*ien &= ~RX_KBINTE;
183 
184 	/* Wait for key data. */
185 	while ((reg->kb_stat & RX_KBRDY) == 0);
186 
187 	code = reg->kb_data;
188 	release = code & 0x80;
189 	*data = code & 0x7f;
190 	*type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
191 
192 	*ien |= ointr;
193 }
194 
195 void
196 kb_hb_cnpollc(void *v, int on)
197 {
198 }
199 
200 int
201 kb_hb_enable(void *v, int on)
202 {
203 
204 	return 0;
205 }
206 
207 void
208 kb_hb_setleds(void *v, int on)
209 {
210 }
211 
212 int
213 kb_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
214 {
215 
216 	switch (cmd) {
217 
218 	case WSKBDIO_GTYPE:
219 		*(int *)data = 0;		/* XXX */
220 		return 0;
221 	case WSKBDIO_SETLEDS:
222 		return 0;
223 	case WSKBDIO_GETLEDS:
224 		*(int *)data = 0;
225 		return 0;
226 	}
227 
228 	return EPASSTHROUGH;
229 }
230