zs_kbd.c revision 1.3 1 /* $NetBSD: zs_kbd.c,v 1.3 2004/07/20 04:55:21 rumble Exp $ */
2
3 /*
4 * Copyright (c) 2004 Steve Rumble
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * IP12/IP20 serial keyboard driver attached to zs channel 0 at 600bps.
32 * This layer is the parent of wskbd.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: zs_kbd.c,v 1.3 2004/07/20 04:55:21 rumble Exp $");
37
38 #include <sys/param.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wskbdvar.h>
46 #include <dev/wscons/wsksymdef.h>
47 #include <dev/wscons/wsksymvar.h>
48
49 #include <dev/ic/z8530reg.h>
50 #include <machine/machtype.h>
51 #include <machine/z8530var.h>
52
53 #define ZSKBD_BAUD 600
54 #define ZSKBD_TXQ_LEN 16 /* power of 2 */
55 #define ZSKBD_RXQ_LEN 64 /* power of 2 */
56
57 #define ZSKBD_DIP_SYNC 0x6E
58 #define ZSKBD_KEY_UP 0x80
59
60 #ifdef ZSKBD_DEBUG
61 int zskbd_debug = 0;
62
63 #define DPRINTF(_x) if (zskbd_debug) printf _x
64 #else
65 #define DPRINTF(_x)
66 #endif
67
68 struct zskbd_softc {
69 struct device sc_dev;
70
71 struct zskbd_devconfig *sc_dc;
72 };
73
74 struct zskbd_devconfig {
75 /* transmit tail-chasing fifo */
76 u_char txq[ZSKBD_TXQ_LEN];
77 u_int txq_head;
78 u_int txq_tail;
79
80 /* receive tail-chasing fifo */
81 u_char rxq[ZSKBD_RXQ_LEN];
82 u_int rxq_head;
83 u_int rxq_tail;
84
85 /* state */
86 #define TX_READY 0x1
87 #define RX_DIP 0x2
88 u_int state;
89
90 /* keyboard configuration */
91 #define ZSKBD_CTRL_A 0x0
92 #define ZSKBD_CTRL_A_SBEEP 0x2 /* 200 ms */
93 #define ZSKBD_CTRL_A_LBEEP 0x4 /* 1000 ms */
94 #define ZSKBD_CTRL_A_NOCLICK 0x8 /* turn off keyboard click */
95 #define ZSKBD_CTRL_A_RCB 0x10 /* request config byte */
96 #define ZSKBD_CTRL_A_NUMLK 0x20 /* num lock led */
97 #define ZSKBD_CTRL_A_CAPSLK 0x40 /* caps lock led */
98 #define ZSKBD_CTRL_A_AUTOREP 0x80 /* auto-repeat after 650 ms, 28x/sec */
99
100 #define ZSKBD_CTRL_B 0x1
101 #define ZSKBD_CTRL_B_CMPL_DS1_2 0x2 /* complement of ds1+ds2 (num+capslk) */
102 #define ZSKBD_CTRL_B_SCRLK 0x4 /* scroll lock light */
103 #define ZSKBD_CTRL_B_L1 0x8 /* user-configurable lights */
104 #define ZSKBD_CTRL_B_L2 0x10
105 #define ZSKBD_CTRL_B_L3 0x20
106 #define ZSKBD_CTRL_B_L4 0x40
107 u_char kbd_conf[2];
108
109 /* dip switch settings */
110 u_char dip;
111
112 /* wscons glue */
113 struct device *wskbddev;
114 int enabled;
115 };
116
117 static int zskbd_match(struct device *, struct cfdata *, void *);
118 static void zskbd_attach(struct device *, struct device *, void *);
119 static void zskbd_rxint(struct zs_chanstate *);
120 static void zskbd_stint(struct zs_chanstate *, int);
121 static void zskbd_txint(struct zs_chanstate *);
122 static void zskbd_softint(struct zs_chanstate *);
123 static void zskbd_send(struct zs_chanstate *, u_char *, u_int);
124 static void zskbd_ctrl(struct zs_chanstate *, u_char, u_char,
125 u_char, u_char);
126
127 static void zskbd_wskbd_input(struct zs_chanstate *, u_char);
128 static int zskbd_wskbd_enable(void *, int);
129 static void zskbd_wskbd_set_leds(void *, int);
130 static int zskbd_wskbd_get_leds(void *);
131 static void zskbd_wskbd_set_keyclick(void *, int);
132 static int zskbd_wskbd_get_keyclick(void *);
133 static int zskbd_wskbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
134
135 void zskbd_cnattach(int, int);
136 static void zskbd_wskbd_getc(void *, u_int *, int *);
137 static void zskbd_wskbd_pollc(void *, int);
138 static void zskbd_wskbd_bell(void *, u_int, u_int, u_int);
139
140 extern struct zschan *zs_get_chan_addr(int, int);
141 extern int zs_getc(void *);
142 extern void zs_putc(void *, int);
143
144 CFATTACH_DECL(zskbd, sizeof(struct zskbd_softc),
145 zskbd_match, zskbd_attach, NULL, NULL);
146
147 static struct zsops zskbd_zsops = {
148 zskbd_rxint,
149 zskbd_stint,
150 zskbd_txint,
151 zskbd_softint
152 };
153
154 extern const struct wscons_keydesc wssgi_keydesctab[];
155 const struct wskbd_mapdata sgikbd_wskbd_keymapdata = {
156 wssgi_keydesctab, KB_US
157 };
158
159 const struct wskbd_accessops zskbd_wskbd_accessops = {
160 zskbd_wskbd_enable,
161 zskbd_wskbd_set_leds,
162 zskbd_wskbd_ioctl
163 };
164
165 const struct wskbd_consops zskbd_wskbd_consops = {
166 zskbd_wskbd_getc,
167 zskbd_wskbd_pollc,
168 zskbd_wskbd_bell
169 };
170
171 static struct zskbd_devconfig zskbd_console_dc;
172 static int zskbd_is_console = 0;
173
174 static int
175 zskbd_match(struct device *parent, struct cfdata *cf, void *aux)
176 {
177 if (mach_type == MACH_SGI_IP12 || mach_type == MACH_SGI_IP20) {
178 struct zsc_attach_args *args = aux;
179
180 if (args->channel == 0)
181 return (1);
182 }
183
184 return (0);
185 }
186
187 static void
188 zskbd_attach(struct device *parent, struct device *self, void *aux)
189 {
190 int s, channel;
191 struct zsc_softc *zsc = (struct zsc_softc *)parent;
192 struct zskbd_softc *sc = (struct zskbd_softc *)self;
193 struct zsc_attach_args *args = aux;
194 struct zs_chanstate *cs;
195 struct wskbddev_attach_args wskaa;
196
197 /* Establish ourself with the MD z8530 driver */
198 channel = args->channel;
199 cs = zsc->zsc_cs[channel];
200 cs->cs_ops = &zskbd_zsops;
201 cs->cs_private = sc;
202
203 if (zskbd_is_console) {
204 sc->sc_dc = &zskbd_console_dc;
205 wskaa.console = 1;
206 sc->sc_dc->enabled = 1;
207 } else {
208 wskaa.console = 0;
209
210 sc->sc_dc = malloc(sizeof(struct zskbd_devconfig), M_DEVBUF,
211 M_WAITOK);
212 if (sc->sc_dc == NULL)
213 panic("zskbd out of memory");
214
215 sc->sc_dc->enabled = 0;
216 }
217
218 sc->sc_dc->txq_head = 0;
219 sc->sc_dc->txq_tail = 0;
220 sc->sc_dc->rxq_head = 0;
221 sc->sc_dc->rxq_tail = 0;
222 sc->sc_dc->state = TX_READY;
223 sc->sc_dc->dip = 0;
224 sc->sc_dc->kbd_conf[ZSKBD_CTRL_A] = 0;
225 sc->sc_dc->kbd_conf[ZSKBD_CTRL_B] = 0;
226
227 printf(": baud rate %d\n", ZSKBD_BAUD);
228
229 s = splzs();
230 zs_write_reg(cs, 9, (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET);
231 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
232 cs->cs_preg[4] = (cs->cs_preg[4] & ZSWR4_CLK_MASK) |
233 (ZSWR4_ONESB | ZSWR4_PARENB); /* 1 stop, odd parity */
234 zs_set_speed(cs, ZSKBD_BAUD);
235 zs_loadchannelregs(cs);
236
237 /* request DIP switch settings just in case */
238 zskbd_ctrl(cs, ZSKBD_CTRL_A_RCB, 0, 0, 0);
239
240 splx(s);
241
242 /* attach wskbd */
243 wskaa.keymap = &sgikbd_wskbd_keymapdata;
244 wskaa.accessops = &zskbd_wskbd_accessops;
245 wskaa.accesscookie = cs;
246 sc->sc_dc->wskbddev = config_found(self, &wskaa, wskbddevprint);
247 }
248
249 static void
250 zskbd_rxint(struct zs_chanstate *cs)
251 {
252 u_char c, r;
253 struct zskbd_softc *sc = (struct zskbd_softc *)cs->cs_private;
254 struct zskbd_devconfig *dc = sc->sc_dc;
255
256 /* clear errors */
257 r = zs_read_reg(cs, 1);
258 if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
259 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
260
261 /* read byte and append to our queue */
262 c = zs_read_data(cs);
263
264 dc->rxq[dc->rxq_tail] = c;
265 dc->rxq_tail = (dc->rxq_tail + 1) & ~ZSKBD_RXQ_LEN;
266
267 cs->cs_softreq = 1;
268 }
269
270 static void
271 zskbd_stint(struct zs_chanstate *cs, int force)
272 {
273 zs_write_csr(cs, ZSWR0_RESET_STATUS);
274 cs->cs_softreq = 1;
275 }
276
277 static void
278 zskbd_txint(struct zs_chanstate *cs)
279 {
280 struct zskbd_softc *sc = (struct zskbd_softc *)cs->cs_private;
281
282 zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
283 sc->sc_dc->state |= TX_READY;
284 cs->cs_softreq = 1;
285 }
286
287 static void
288 zskbd_softint(struct zs_chanstate *cs)
289 {
290 struct zskbd_softc *sc = (struct zskbd_softc *)cs->cs_private;
291 struct zskbd_devconfig *dc = sc->sc_dc;
292
293 /* handle pending transmissions */
294 if (dc->txq_head != dc->txq_tail && (dc->state & TX_READY)) {
295 int s;
296
297 dc->state &= ~TX_READY;
298
299 s = splzs();
300 zs_write_data(cs, dc->txq[dc->txq_head]);
301 splx(s);
302
303 dc->txq_head = (dc->txq_head + 1) & ~ZSKBD_TXQ_LEN;
304 }
305
306 /* don't bother if nobody is listening */
307 if (!dc->enabled) {
308 dc->rxq_head = dc->rxq_tail;
309 return;
310 }
311
312 /* handle incoming keystrokes/config */
313 while (dc->rxq_head != dc->rxq_tail) {
314 u_char key = dc->rxq[dc->rxq_head];
315
316 if (dc->state & RX_DIP) {
317 dc->dip = key;
318 dc->state &= ~RX_DIP;
319 } else if (key == ZSKBD_DIP_SYNC) {
320 dc->state |= RX_DIP;
321 } else {
322 /* toss wskbd a bone */
323 zskbd_wskbd_input(cs, key);
324 }
325
326 dc->rxq_head = (dc->rxq_head + 1) & ~ZSKBD_RXQ_LEN;
327 }
328 }
329
330 /* expects to be in splzs() */
331 static void
332 zskbd_send(struct zs_chanstate *cs, u_char *c, u_int len)
333 {
334 u_int i;
335 struct zskbd_softc *sc = (struct zskbd_softc *)cs->cs_private;
336 struct zskbd_devconfig *dc = sc->sc_dc;
337
338 for (i = 0; i < len; i++) {
339 if (dc->state & TX_READY) {
340 zs_write_data(cs, c[i]);
341 dc->state &= ~TX_READY;
342 } else {
343 dc->txq[dc->txq_tail] = c[i];
344 dc->txq_tail = (dc->txq_tail + 1) & ~ZSKBD_TXQ_LEN;
345 cs->cs_softreq = 1;
346 }
347 }
348 }
349
350 /* expects to be in splzs() */
351 static void
352 zskbd_ctrl(struct zs_chanstate *cs, u_char a_on, u_char a_off,
353 u_char b_on, u_char b_off)
354 {
355 struct zskbd_softc *sc = (struct zskbd_softc *)cs->cs_private;
356 struct zskbd_devconfig *dc = sc->sc_dc;
357
358 dc->kbd_conf[ZSKBD_CTRL_A] |= a_on;
359 dc->kbd_conf[ZSKBD_CTRL_A] &= ~(a_off | ZSKBD_CTRL_B);
360 dc->kbd_conf[ZSKBD_CTRL_B] &= ~b_off;
361 dc->kbd_conf[ZSKBD_CTRL_B] |= (b_on | ZSKBD_CTRL_B);
362
363 zskbd_send(cs, dc->kbd_conf, 2);
364
365 /* make sure we don't resend these each time */
366 dc->kbd_conf[ZSKBD_CTRL_A] &= ~(ZSKBD_CTRL_A_RCB | ZSKBD_CTRL_A_SBEEP |
367 ZSKBD_CTRL_A_LBEEP);
368 }
369
370 /******************************************************************************
371 * wskbd glue
372 ******************************************************************************/
373
374 static void
375 zskbd_wskbd_input(struct zs_chanstate *cs, u_char key)
376 {
377 u_int type;
378 struct zskbd_softc *sc = (struct zskbd_softc *)cs->cs_private;
379
380 if (key & ZSKBD_KEY_UP)
381 type = WSCONS_EVENT_KEY_UP;
382 else
383 type = WSCONS_EVENT_KEY_DOWN;
384
385 wskbd_input(sc->sc_dc->wskbddev, type, (key & ~ZSKBD_KEY_UP));
386
387 DPRINTF(("zskbd_wskbd_input: inputted key 0x%x\n", key));
388
389 #ifdef WSDISPLAY_COMPAT_RAWKBD
390 wskbd_rawinput(sc->sc_dc->wskbddev, &key, 1);
391 #endif
392 }
393
394 static int
395 zskbd_wskbd_enable(void *cookie, int on)
396 {
397 struct zskbd_softc *sc = (struct zskbd_softc *)
398 ((struct zs_chanstate *)cookie)->cs_private;
399 if (on) {
400 if (sc->sc_dc->enabled)
401 return (EBUSY);
402 else
403 sc->sc_dc->enabled = 1;
404 } else
405 sc->sc_dc->enabled = 0;
406
407 DPRINTF(("zskbd_wskbd_enable: %s\n", on ? "enabled" : "disabled"));
408
409 return (0);
410 }
411
412 static void
413 zskbd_wskbd_set_leds(void *cookie, int leds)
414 {
415 int s;
416 u_char a_on = 0, a_off = 0, b_on = 0, b_off = 0;
417
418 if (leds & WSKBD_LED_CAPS)
419 a_on |= ZSKBD_CTRL_A_CAPSLK;
420 else
421 a_off |= ZSKBD_CTRL_A_CAPSLK;
422
423 if (leds & WSKBD_LED_NUM)
424 a_on |= ZSKBD_CTRL_A_NUMLK;
425 else
426 a_off |= ZSKBD_CTRL_A_NUMLK;
427
428 if (leds & WSKBD_LED_SCROLL)
429 b_on |= ZSKBD_CTRL_B_SCRLK;
430 else
431 b_off |= ZSKBD_CTRL_B_SCRLK;
432
433 s = splzs();
434 zskbd_ctrl((struct zs_chanstate *)cookie, a_on, a_off, b_on, b_off);
435 splx(s);
436 }
437
438 static int
439 zskbd_wskbd_get_leds(void *cookie)
440 {
441 int leds = 0;
442 struct zskbd_softc *sc = (struct zskbd_softc *)
443 ((struct zs_chanstate *)cookie)->cs_private;
444
445 if (sc->sc_dc->kbd_conf[ZSKBD_CTRL_A] & ZSKBD_CTRL_A_NUMLK)
446 leds |= WSKBD_LED_NUM;
447
448 if (sc->sc_dc->kbd_conf[ZSKBD_CTRL_A] & ZSKBD_CTRL_A_CAPSLK)
449 leds |= WSKBD_LED_CAPS;
450
451 if (sc->sc_dc->kbd_conf[ZSKBD_CTRL_B] & ZSKBD_CTRL_B_SCRLK)
452 leds |= WSKBD_LED_SCROLL;
453
454 return (leds);
455 }
456
457 static void
458 zskbd_wskbd_set_keyclick(void *cookie, int on)
459 {
460 int s;
461 struct zs_chanstate *cs = (struct zs_chanstate *)cookie;
462
463 if (on) {
464 if (!zskbd_wskbd_get_keyclick(cookie)) {
465 s = splzs();
466 zskbd_ctrl(cs, 0, ZSKBD_CTRL_A_NOCLICK, 0, 0);
467 splx(s);
468 }
469 } else {
470 if (zskbd_wskbd_get_keyclick(cookie)) {
471 s = splzs();
472 zskbd_ctrl(cs, ZSKBD_CTRL_A_NOCLICK, 0, 0, 0);
473 splx(s);
474 }
475 }
476 }
477
478 static int
479 zskbd_wskbd_get_keyclick(void *cookie)
480 {
481 struct zskbd_softc *sc = (struct zskbd_softc *)
482 ((struct zs_chanstate *)cookie)->cs_private;
483
484 if (sc->sc_dc->kbd_conf[ZSKBD_CTRL_A] & ZSKBD_CTRL_A_NOCLICK)
485 return (0);
486 else
487 return (1);
488 }
489
490 static int
491 zskbd_wskbd_ioctl(void *cookie, u_long cmd,
492 caddr_t data, int flag, struct proc *p)
493 {
494 switch (cmd) {
495 case WSKBDIO_GTYPE:
496 *(int *)data = WSKBD_TYPE_SGI;
497 break;
498
499 #ifdef notyet
500 case WSKBDIO_BELL:
501 case WSKBDIO_COMPLEXBELL:
502 case WSKBDIO_SETBELL:
503 case WSKBDIO_GETBELL:
504 case WSKBDIO_SETDEFAULTBELL:
505 case WSKBDIO_GETDEFAULTBELL:
506 case WSKBDIO_SETKEYREPEAT:
507 case WSKBDIO_GETKEYREPEAT:
508 case WSKBDIO_SETDEFAULTKEYREPEAT:
509 case WSKBDIO_GETDEFAULTKEYREPEAT:
510 #endif
511
512 case WSKBDIO_SETLEDS:
513 zskbd_wskbd_set_leds(cookie, *(int *)data);
514 break;
515
516 case WSKBDIO_GETLEDS:
517 *(int *)data = zskbd_wskbd_get_leds(cookie);
518 break;
519
520 #ifdef notyet
521 case WSKBDIO_GETMAP:
522 case WSKBDIO_SETMAP:
523 case WSKBDIO_GETENCODING:
524 case WSKBDIO_SETENCODING:
525 case WSKBDIO_SETMODE:
526 case WSKBDIO_GETMODE:
527 #endif
528
529 case WSKBDIO_SETKEYCLICK:
530 zskbd_wskbd_set_keyclick(cookie, *(int *)data);
531 break;
532
533 case WSKBDIO_GETKEYCLICK:
534 *(int *)data = zskbd_wskbd_get_keyclick(cookie);
535 break;
536
537 default:
538 return (EPASSTHROUGH);
539 }
540
541 return (0);
542 }
543
544 /*
545 * console routines
546 */
547 void
548 zskbd_cnattach(int zsunit, int zschan)
549 {
550 wskbd_cnattach(&zskbd_wskbd_consops, zs_get_chan_addr(zsunit, zschan),
551 &sgikbd_wskbd_keymapdata);
552 zskbd_is_console = 1;
553 }
554
555 static void
556 zskbd_wskbd_getc(void *cookie, u_int *type, int *data)
557 {
558 int key = zs_getc(cookie);
559
560 if (key & ZSKBD_KEY_UP)
561 *type = WSCONS_EVENT_KEY_UP;
562 else
563 *type = WSCONS_EVENT_KEY_DOWN;
564
565 *data = key & ~ZSKBD_KEY_UP;
566 }
567
568 static void
569 zskbd_wskbd_pollc(void *cookie, int on)
570 {
571 }
572
573 static void
574 zskbd_wskbd_bell(void *cookie, u_int pitch, u_int period, u_int volume)
575 {
576 /*
577 * Since we don't have any state, this'll nuke our lights,
578 * key click, and other bits in ZSKBD_CTRL_A.
579 */
580 if (period >= 1000)
581 zs_putc(cookie, ZSKBD_CTRL_A_LBEEP);
582 else
583 zs_putc(cookie, ZSKBD_CTRL_A_SBEEP);
584 }
585