ewskbd.c revision 1.4 1 /* $NetBSD: ewskbd.c,v 1.4 2007/03/04 05:59:47 christos Exp $ */
2
3 /*
4 * Copyright (c) 2005 Izumi Tsutsui
5 * Copyright (c) 2004 Steve Rumble
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * EWS4800 serial keyboard driver attached to zs channel 0 at 4800 bps.
33 * This layer is the parent of wskbd.
34 *
35 * This driver is taken from sgimips.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: ewskbd.c,v 1.4 2007/03/04 05:59:47 christos Exp $");
40
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/device.h>
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wskbdvar.h>
49 #include <dev/wscons/wsksymdef.h>
50 #include <dev/wscons/wsksymvar.h>
51
52 #include <dev/ic/z8530reg.h>
53 #include <machine/z8530var.h>
54
55 #include <ews4800mips/dev/ews4800keymap.h>
56
57 #define EWSKBD_BAUD 4800
58
59 #define EWSKBD_TXQ_LEN 16 /* power of 2 */
60 #define EWSKBD_TXQ_LEN_MASK (EWSKBD_TXQ_LEN - 1)
61 #define EWSKBD_NEXTTXQ(x) (((x) + 1) & EWSKBD_TXQ_LEN_MASK)
62
63 #define EWSKBD_RXQ_LEN 64 /* power of 2 */
64 #define EWSKBD_RXQ_LEN_MASK (EWSKBD_RXQ_LEN - 1)
65 #define EWSKBD_NEXTRXQ(x) (((x) + 1) & EWSKBD_RXQ_LEN_MASK)
66
67 #define EWSKBD_KEY_UP 0x80
68 #define EWSKBD_KEY_MASK 0x7f
69
70 #ifdef EWSKBD_DEBUG
71 int ewskbd_debug = 0;
72 #define DPRINTF(_x) if (ewskbd_debug) printf _x
73 #else
74 #define DPRINTF(_x)
75 #endif
76
77 struct ewskbd_softc {
78 struct device sc_dev;
79 struct ewskbd_devconfig *sc_dc;
80 };
81
82 struct ewskbd_devconfig {
83 /* transmit tail-chasing fifo */
84 uint8_t txq[EWSKBD_TXQ_LEN];
85 u_int txq_head;
86 u_int txq_tail;
87
88 /* receive tail-chasing fifo */
89 uint8_t rxq[EWSKBD_RXQ_LEN];
90 u_int rxq_head;
91 u_int rxq_tail;
92
93 /* state */
94 u_int state;
95 #define TX_READY 0x01
96
97 /* LED status */
98 uint8_t leds;
99 #define EWSKBD_SETLEDS 0x90
100 #define EWSKBD_CAPSLOCK 0x02
101 #define EWSKBD_KANA 0x04
102
103 /* wscons glue */
104 struct device *wskbddev;
105 int enabled;
106 };
107
108 static int ewskbd_zsc_match(struct device *, struct cfdata *, void *);
109 static void ewskbd_zsc_attach(struct device *, struct device *, void *);
110 static int ewskbd_zsc_init(struct zs_chanstate *);
111 static void ewskbd_zsc_rxint(struct zs_chanstate *);
112 static void ewskbd_zsc_stint(struct zs_chanstate *, int);
113 static void ewskbd_zsc_txint(struct zs_chanstate *);
114 static void ewskbd_zsc_softint(struct zs_chanstate *);
115 static void ewskbd_zsc_send(struct zs_chanstate *, uint8_t *, u_int);
116
117 static void ewskbd_wskbd_input(struct zs_chanstate *, u_char);
118 static int ewskbd_wskbd_enable(void *, int);
119 static void ewskbd_wskbd_set_leds(void *, int);
120 static int ewskbd_wskbd_get_leds(void *);
121 static int ewskbd_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
122
123 void ewskbd_zsc_cnattach(uint32_t, uint32_t, int);
124 static void ewskbd_zsc_wskbd_getc(void *, u_int *, int *);
125 static void ewskbd_wskbd_pollc(void *, int);
126 static void ewskbd_wskbd_bell(void *, u_int, u_int, u_int);
127
128 CFATTACH_DECL(ewskbd_zsc, sizeof(struct ewskbd_softc),
129 ewskbd_zsc_match, ewskbd_zsc_attach, NULL, NULL);
130
131 static struct zsops ewskbd_zsops = {
132 ewskbd_zsc_rxint,
133 ewskbd_zsc_stint,
134 ewskbd_zsc_txint,
135 ewskbd_zsc_softint
136 };
137
138 const struct wskbd_mapdata ews4800kbd_wskbd_keymapdata = {
139 ews4800kbd_keydesctab,
140 KB_JP
141 };
142
143 const struct wskbd_accessops ewskbd_wskbd_accessops = {
144 ewskbd_wskbd_enable,
145 ewskbd_wskbd_set_leds,
146 ewskbd_wskbd_ioctl
147 };
148
149 const struct wskbd_consops ewskbd_wskbd_consops = {
150 ewskbd_zsc_wskbd_getc,
151 ewskbd_wskbd_pollc,
152 ewskbd_wskbd_bell
153 };
154
155 static struct ewskbd_devconfig ewskbd_console_dc;
156 static struct zs_chanstate conschan;
157 static int ewskbd_is_console;
158
159 static int
160 ewskbd_zsc_match(struct device *parent, struct cfdata *cf, void *aux)
161 {
162 struct zsc_attach_args *zsc_args = aux;
163 struct zsc_softc *zsc = (void *)parent;
164
165 /* keyboard is on channel B */
166 if ((zsc->zsc_flags & 0x0001 /* kbms port */) != 0 &&
167 zsc_args->channel == 1)
168 /* prior to generic zstty(4) */
169 return 3;
170
171 return 0;
172 }
173
174 static void
175 ewskbd_zsc_attach(struct device *parent, struct device *self, void *aux)
176 {
177 struct ewskbd_softc *sc;
178 struct zs_chanstate *cs;
179 struct zsc_softc *zsc;
180 struct zsc_attach_args *zsc_args;
181 struct wskbddev_attach_args wskaa;
182 int channel;
183
184 zsc = (void *)parent;
185 sc = (void *)self;
186 zsc_args = aux;
187
188 /* Establish ourself with the MD z8530 driver */
189 channel = zsc_args->channel;
190 cs = zsc->zsc_cs[channel];
191
192 if (ewskbd_is_console) {
193 sc->sc_dc = &ewskbd_console_dc;
194 wskaa.console = 1;
195 sc->sc_dc->enabled = 1;
196 } else {
197 wskaa.console = 0;
198
199 sc->sc_dc = malloc(sizeof(struct ewskbd_devconfig), M_DEVBUF,
200 M_WAITOK | M_ZERO);
201 if (sc->sc_dc == NULL) {
202 printf(": can't allocate memory\n");
203 return;
204 }
205 sc->sc_dc->enabled = 0;
206 }
207 cs->cs_defspeed = EWSKBD_BAUD;
208 cs->cs_ops = &ewskbd_zsops;
209 cs->cs_private = sc;
210
211 sc->sc_dc->txq_head = 0;
212 sc->sc_dc->txq_tail = 0;
213 sc->sc_dc->rxq_head = 0;
214 sc->sc_dc->rxq_tail = 0;
215 sc->sc_dc->state = TX_READY;
216 sc->sc_dc->leds = 0;
217
218 ewskbd_zsc_init(cs);
219
220 /* set default LED */
221 ewskbd_wskbd_set_leds(cs, 0);
222
223 printf(": baud rate %d\n", EWSKBD_BAUD);
224
225 /* attach wskbd */
226 wskaa.keymap = &ews4800kbd_wskbd_keymapdata;
227 wskaa.accessops = &ewskbd_wskbd_accessops;
228 wskaa.accesscookie = cs;
229 sc->sc_dc->wskbddev = config_found(self, &wskaa, wskbddevprint);
230 }
231
232 static int
233 ewskbd_zsc_init(struct zs_chanstate *cs)
234 {
235 int s;
236
237 s = splzs();
238
239 zs_write_reg(cs, 9, ZSWR9_B_RESET);
240 DELAY(100);
241 zs_write_reg(cs, 9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
242
243 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
244 cs->cs_preg[2] = 0;
245 cs->cs_preg[3] = ZSWR3_RX_8 | ZSWR3_RX_ENABLE;
246 cs->cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_PARENB;
247 cs->cs_preg[5] = ZSWR5_TX_8 | ZSWR5_RTS | ZSWR5_TX_ENABLE;
248 cs->cs_preg[6] = 0;
249 cs->cs_preg[7] = 0;
250 cs->cs_preg[8] = 0;
251 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
252 cs->cs_preg[10] = 0;
253 cs->cs_preg[11] = ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
254 ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD;
255 /* reg[11] and reg[12] are set by zs_set_speed() with cs_brg_clk */
256 zs_set_speed(cs, EWSKBD_BAUD);
257 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
258 cs->cs_preg[15] = 0;
259
260 zs_loadchannelregs(cs);
261
262 splx(s);
263
264 return 0;
265 }
266
267 static void
268 ewskbd_zsc_rxint(struct zs_chanstate *cs)
269 {
270 struct ewskbd_softc *sc;
271 struct ewskbd_devconfig *dc;
272 uint8_t c, r;
273
274 sc = cs->cs_private;
275 dc = sc->sc_dc;
276
277 /* clear errors */
278 r = zs_read_reg(cs, 1);
279 if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
280 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
281
282 /* read byte and append to our queue */
283 c = zs_read_data(cs);
284
285 dc->rxq[dc->rxq_tail] = c;
286 dc->rxq_tail = EWSKBD_NEXTRXQ(dc->rxq_tail);
287
288 cs->cs_softreq = 1;
289 }
290
291 static void
292 ewskbd_zsc_stint(struct zs_chanstate *cs, int force)
293 {
294
295 zs_write_csr(cs, ZSWR0_RESET_STATUS);
296 cs->cs_softreq = 1;
297 }
298
299 static void
300 ewskbd_zsc_txint(struct zs_chanstate *cs)
301 {
302 struct ewskbd_softc *sc;
303
304 sc = cs->cs_private;
305 zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
306 sc->sc_dc->state |= TX_READY;
307 cs->cs_softreq = 1;
308 }
309
310 static void
311 ewskbd_zsc_softint(struct zs_chanstate *cs)
312 {
313 struct ewskbd_softc *sc;
314 struct ewskbd_devconfig *dc;
315
316 sc = cs->cs_private;
317 dc = sc->sc_dc;
318
319 /* handle pending transmissions */
320 if (dc->txq_head != dc->txq_tail && (dc->state & TX_READY)) {
321 int s;
322
323 dc->state &= ~TX_READY;
324
325 s = splzs();
326 zs_write_data(cs, dc->txq[dc->txq_head]);
327 splx(s);
328
329 dc->txq_head = EWSKBD_NEXTTXQ(dc->txq_head);
330 }
331
332 /* don't bother if nobody is listening */
333 if (!dc->enabled) {
334 dc->rxq_head = dc->rxq_tail;
335 return;
336 }
337
338 /* handle incoming keystrokes/config */
339 while (dc->rxq_head != dc->rxq_tail) {
340 uint8_t key = dc->rxq[dc->rxq_head];
341
342 /* toss wskbd a bone */
343 ewskbd_wskbd_input(cs, key);
344
345 dc->rxq_head = EWSKBD_NEXTRXQ(dc->rxq_head);
346 }
347 }
348
349 /* expects to be in splzs() */
350 static void
351 ewskbd_zsc_send(struct zs_chanstate *cs, uint8_t *c, u_int len)
352 {
353 struct ewskbd_softc *sc;
354 struct ewskbd_devconfig *dc;
355 int i;
356
357 sc = cs->cs_private;
358 dc = sc->sc_dc;
359
360 for (i = 0; i < len; i++) {
361 if (dc->state & TX_READY) {
362 zs_write_data(cs, c[i]);
363 dc->state &= ~TX_READY;
364 } else {
365 dc->txq[dc->txq_tail] = c[i];
366 dc->txq_tail = EWSKBD_NEXTTXQ(dc->txq_tail);
367 cs->cs_softreq = 1;
368 }
369 }
370 }
371
372 /******************************************************************************
373 * wskbd glue
374 ******************************************************************************/
375
376 static void
377 ewskbd_wskbd_input(struct zs_chanstate *cs, uint8_t key)
378 {
379 struct ewskbd_softc *sc;
380 u_int type;
381
382 sc = cs->cs_private;
383
384 if (key & EWSKBD_KEY_UP)
385 type = WSCONS_EVENT_KEY_UP;
386 else
387 type = WSCONS_EVENT_KEY_DOWN;
388
389 wskbd_input(sc->sc_dc->wskbddev, type, (key & EWSKBD_KEY_MASK));
390
391 DPRINTF(("ewskbd_wskbd_input: inputted key 0x%x\n", key));
392
393 #ifdef WSDISPLAY_COMPAT_RAWKBD
394 wskbd_rawinput(sc->sc_dc->wskbddev, &key, 1);
395 #endif
396 }
397
398 static int
399 ewskbd_wskbd_enable(void *cookie, int on)
400 {
401 struct zs_chanstate *cs;
402 struct ewskbd_softc *sc;
403
404 cs = cookie;
405 sc = cs->cs_private;
406
407 if (on) {
408 if (sc->sc_dc->enabled)
409 return EBUSY;
410 else
411 sc->sc_dc->enabled = 1;
412 } else
413 sc->sc_dc->enabled = 0;
414
415 DPRINTF(("ewskbd_wskbd_enable: %s\n", on ? "enabled" : "disabled"));
416
417 return 0;
418 }
419
420 static void
421 ewskbd_wskbd_set_leds(void *cookie, int leds)
422 {
423 struct zs_chanstate *cs;
424 struct ewskbd_softc *sc;
425 int s;
426 uint8_t cmd;
427
428 cs = cookie;
429 sc = cs->cs_private;
430 cmd = 0;
431
432 if (leds & WSKBD_LED_CAPS)
433 cmd |= EWSKBD_CAPSLOCK;
434
435 sc->sc_dc->leds = cmd;
436
437 cmd |= EWSKBD_SETLEDS;
438
439 s = splzs();
440 ewskbd_zsc_send(cs, &cmd, 1);
441 splx(s);
442 }
443
444 static int
445 ewskbd_wskbd_get_leds(void *cookie)
446 {
447 struct zs_chanstate *cs;
448 struct ewskbd_softc *sc;
449 int leds;
450
451 cs = cookie;
452 sc = cs->cs_private;
453 leds = 0;
454
455 if (sc->sc_dc->leds & EWSKBD_CAPSLOCK)
456 leds |= WSKBD_LED_CAPS;
457
458 return leds;
459 }
460
461 static int
462 ewskbd_wskbd_ioctl(void *cookie, u_long cmd, void *data, int flag,
463 struct lwp *l)
464 {
465
466 switch (cmd) {
467 case WSKBDIO_GTYPE:
468 *(int *)data = WSKBD_TYPE_EWS4800;
469 break;
470
471 #ifdef notyet
472 case WSKBDIO_BELL:
473 case WSKBDIO_COMPLEXBELL:
474 case WSKBDIO_SETBELL:
475 case WSKBDIO_GETBELL:
476 case WSKBDIO_SETDEFAULTBELL:
477 case WSKBDIO_GETDEFAULTBELL:
478 case WSKBDIO_SETKEYREPEAT:
479 case WSKBDIO_GETKEYREPEAT:
480 case WSKBDIO_SETDEFAULTKEYREPEAT:
481 case WSKBDIO_GETDEFAULTKEYREPEAT:
482 #endif
483
484 case WSKBDIO_SETLEDS:
485 ewskbd_wskbd_set_leds(cookie, *(int *)data);
486 break;
487
488 case WSKBDIO_GETLEDS:
489 *(int *)data = ewskbd_wskbd_get_leds(cookie);
490 break;
491
492 #ifdef notyet
493 case WSKBDIO_GETMAP:
494 case WSKBDIO_SETMAP:
495 case WSKBDIO_GETENCODING:
496 case WSKBDIO_SETENCODING:
497 case WSKBDIO_SETMODE:
498 case WSKBDIO_GETMODE:
499 case WSKBDIO_SETKEYCLICK:
500 case WSKBDIO_GETKEYCLICK:
501 #endif
502
503 default:
504 return EPASSTHROUGH;
505 }
506
507 return 0;
508 }
509
510 /*
511 * console routines
512 */
513 void
514 ewskbd_zsc_cnattach(uint32_t csr, uint32_t data, int pclk)
515 {
516 struct zs_chanstate *cs;
517
518 cs = &conschan;
519
520 cs->cs_reg_csr = (void *)csr;
521 cs->cs_reg_data = (void *)data;
522 cs->cs_brg_clk = pclk / 16;
523 cs->cs_defspeed = EWSKBD_BAUD;
524
525 ewskbd_zsc_init(cs);
526
527 zs_putc(cs, EWSKBD_SETLEDS);
528
529 wskbd_cnattach(&ewskbd_wskbd_consops, cs, &ews4800kbd_wskbd_keymapdata);
530 ewskbd_is_console = 1;
531 }
532
533 static void
534 ewskbd_zsc_wskbd_getc(void *cookie, u_int *type, int *data)
535 {
536 int key;
537
538 key = zs_getc(cookie);
539
540 if (key & EWSKBD_KEY_UP)
541 *type = WSCONS_EVENT_KEY_UP;
542 else
543 *type = WSCONS_EVENT_KEY_DOWN;
544
545 *data = key & EWSKBD_KEY_MASK;
546 }
547
548 static void
549 ewskbd_wskbd_pollc(void *cookie, int on)
550 {
551
552 static bool __polling = false;
553 static int s;
554
555 if (on && !__polling) {
556 /* disable interrupt driven I/O */
557 s = splhigh();
558 } else if (!on && __polling) {
559 /* enable interrupt driven I/O */
560 __polling = false;
561 splx(s);
562 }
563 }
564
565 static void
566 ewskbd_wskbd_bell(void *cookie, u_int pitch, u_int period, u_int volume)
567 {
568
569 /* nothing */
570 }
571