AMD64 - Refactor uio_resid and size_t assumptions.
[dragonfly.git] / sys / dev / misc / kbd / kbd.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $
028066b1 27 * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.23 2007/05/08 02:31:39 dillon Exp $
a38e57d5
MD
28 */
29/*
30 * Generic keyboard driver.
31 *
32 * Interrupt note: keyboards use clist functions and since usb keyboard
33 * interrupts are not protected by spltty(), we must use a critical section
34 * to protect against corruption.
984263bc
MD
35 */
36
37#include "opt_kbd.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/conf.h>
44#include <sys/proc.h>
45#include <sys/tty.h>
46#include <sys/poll.h>
47#include <sys/vnode.h>
48#include <sys/uio.h>
a38e57d5
MD
49#include <sys/thread.h>
50#include <sys/thread2.h>
984263bc
MD
51
52#include <machine/console.h>
53
1f2de5d4 54#include "kbdreg.h"
984263bc
MD
55
56#define KBD_INDEX(dev) minor(dev)
57
8a0a8f43 58struct genkbd_softc {
984263bc
MD
59 int gkb_flags; /* flag/status bits */
60#define KB_ASLEEP (1 << 0)
61 struct clist gkb_q; /* input queue */
62 struct selinfo gkb_rsel;
8a0a8f43
MD
63};
64
65typedef struct genkbd_softc *genkbd_softc_t;
984263bc
MD
66
67static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
68 SLIST_HEAD_INITIALIZER(keyboard_drivers);
69
dc62b251
MD
70SET_DECLARE(kbddriver_set, const keyboard_driver_t);
71
984263bc
MD
72/* local arrays */
73
74/*
75 * We need at least one entry each in order to initialize a keyboard
76 * for the kernel console. The arrays will be increased dynamically
77 * when necessary.
78 */
79
80static int keyboards = 1;
81static keyboard_t *kbd_ini;
82static keyboard_t **keyboard = &kbd_ini;
83static keyboard_switch_t *kbdsw_ini;
84 keyboard_switch_t **kbdsw = &kbdsw_ini;
85
86#define ARRAY_DELTA 4
87
88static int
89kbd_realloc_array(void)
90{
91 keyboard_t **new_kbd;
92 keyboard_switch_t **new_kbdsw;
93 int newsize;
984263bc 94
984263bc 95 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
efda3bd0 96 new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
bf22d4c1 97 M_WAITOK | M_ZERO);
efda3bd0 98 new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
bf22d4c1 99 M_WAITOK | M_ZERO);
984263bc
MD
100 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
101 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
a38e57d5 102 crit_enter();
984263bc 103 if (keyboards > 1) {
efda3bd0
MD
104 kfree(keyboard, M_DEVBUF);
105 kfree(kbdsw, M_DEVBUF);
984263bc
MD
106 }
107 keyboard = new_kbd;
108 kbdsw = new_kbdsw;
109 keyboards = newsize;
a38e57d5 110 crit_exit();
984263bc
MD
111
112 if (bootverbose)
e3869ec7 113 kprintf("kbd: new array size %d\n", keyboards);
984263bc
MD
114
115 return 0;
116}
117
118/*
e30db56f
MD
119 * Low-level keyboard driver functions.
120 *
984263bc
MD
121 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
122 * driver, call these functions to initialize the keyboard_t structure
123 * and register it to the virtual keyboard driver `kbd'.
e30db56f
MD
124 *
125 * The reinit call is made when a driver has partially detached a keyboard
126 * but does not unregistered it, then wishes to reinitialize it later on.
127 * This is how the USB keyboard driver handles the 'default' keyboard,
128 * because unregistering the keyboard associated with the console will
129 * destroy its console association forever.
984263bc 130 */
984263bc 131void
e30db56f 132kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
984263bc 133{
e30db56f 134 kbd->kb_flags |= KB_NO_DEVICE; /* device has not been found */
984263bc
MD
135 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
136 kbd->kb_led = 0; /* unknown */
984263bc
MD
137 kbd->kb_data = NULL;
138 kbd->kb_keymap = NULL;
139 kbd->kb_accentmap = NULL;
140 kbd->kb_fkeytab = NULL;
141 kbd->kb_fkeytab_size = 0;
142 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
143 kbd->kb_delay2 = KB_DELAY2;
e30db56f
MD
144 kbd->kb_count = 0;
145 kbd->kb_pref = pref;
984263bc
MD
146 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
147}
148
e30db56f
MD
149/* initialize the keyboard_t structure */
150void
151kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
152 int pref, int port, int port_size)
153{
154 kbd->kb_flags = 0;
155 kbd->kb_name = name;
156 kbd->kb_type = type;
157 kbd->kb_unit = unit;
158 kbd->kb_io_base = port;
159 kbd->kb_io_size = port_size;
160 kbd_reinit_struct(kbd, config, pref);
161}
162
984263bc
MD
163void
164kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
165 fkeytab_t *fkeymap, int fkeymap_size)
166{
167 kbd->kb_keymap = keymap;
168 kbd->kb_accentmap = accmap;
169 kbd->kb_fkeytab = fkeymap;
170 kbd->kb_fkeytab_size = fkeymap_size;
171}
172
173/* declare a new keyboard driver */
174int
175kbd_add_driver(keyboard_driver_t *driver)
176{
177 if (SLIST_NEXT(driver, link))
178 return EINVAL;
179 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
180 return 0;
181}
182
183int
184kbd_delete_driver(keyboard_driver_t *driver)
185{
186 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
187 SLIST_NEXT(driver, link) = NULL;
188 return 0;
189}
190
191/* register a keyboard and associate it with a function table */
192int
193kbd_register(keyboard_t *kbd)
194{
195 const keyboard_driver_t **list;
196 const keyboard_driver_t *p;
197 int index;
198
199 for (index = 0; index < keyboards; ++index) {
200 if (keyboard[index] == NULL)
201 break;
202 }
203 if (index >= keyboards) {
204 if (kbd_realloc_array())
205 return -1;
206 }
207
208 kbd->kb_index = index;
209 KBD_UNBUSY(kbd);
210 KBD_VALID(kbd);
211 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
212 kbd->kb_token = NULL;
213 kbd->kb_callback.kc_func = NULL;
214 kbd->kb_callback.kc_arg = NULL;
c5e92a81 215 callout_init(&kbd->kb_atkbd_timeout_ch);
984263bc
MD
216
217 SLIST_FOREACH(p, &keyboard_drivers, link) {
218 if (strcmp(p->name, kbd->kb_name) == 0) {
219 keyboard[index] = kbd;
220 kbdsw[index] = p->kbdsw;
221 return index;
222 }
223 }
dc62b251
MD
224 SET_FOREACH(list, kbddriver_set) {
225 p = *list;
984263bc
MD
226 if (strcmp(p->name, kbd->kb_name) == 0) {
227 keyboard[index] = kbd;
228 kbdsw[index] = p->kbdsw;
229 return index;
230 }
231 }
232
233 return -1;
234}
235
236int
237kbd_unregister(keyboard_t *kbd)
238{
239 int error;
984263bc
MD
240
241 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
242 return ENOENT;
243 if (keyboard[kbd->kb_index] != kbd)
244 return ENOENT;
245
a38e57d5 246 crit_enter();
c5e92a81 247 callout_stop(&kbd->kb_atkbd_timeout_ch);
984263bc
MD
248 if (KBD_IS_BUSY(kbd)) {
249 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
250 kbd->kb_callback.kc_arg);
251 if (error) {
a38e57d5 252 crit_exit();
984263bc
MD
253 return error;
254 }
255 if (KBD_IS_BUSY(kbd)) {
a38e57d5 256 crit_exit();
984263bc
MD
257 return EBUSY;
258 }
259 }
260 KBD_INVALID(kbd);
261 keyboard[kbd->kb_index] = NULL;
262 kbdsw[kbd->kb_index] = NULL;
263
a38e57d5 264 crit_exit();
984263bc
MD
265 return 0;
266}
267
268/* find a funciton table by the driver name */
3d8ba483
SW
269keyboard_switch_t *
270kbd_get_switch(char *driver)
984263bc
MD
271{
272 const keyboard_driver_t **list;
273 const keyboard_driver_t *p;
274
275 SLIST_FOREACH(p, &keyboard_drivers, link) {
276 if (strcmp(p->name, driver) == 0)
277 return p->kbdsw;
278 }
dc62b251
MD
279 SET_FOREACH(list, kbddriver_set) {
280 p = *list;
984263bc
MD
281 if (strcmp(p->name, driver) == 0)
282 return p->kbdsw;
283 }
284
285 return NULL;
286}
287
288/*
289 * Keyboard client functions
290 * Keyboard clients, such as the console driver `syscons' and the keyboard
291 * cdev driver, use these functions to claim and release a keyboard for
292 * exclusive use.
293 */
294
295/* find the keyboard specified by a driver name and a unit number */
296int
297kbd_find_keyboard(char *driver, int unit)
298{
299 int i;
e30db56f
MD
300 int pref;
301 int pref_index;
302
303 pref = 0;
304 pref_index = -1;
984263bc
MD
305
306 for (i = 0; i < keyboards; ++i) {
307 if (keyboard[i] == NULL)
308 continue;
309 if (!KBD_IS_VALID(keyboard[i]))
310 continue;
311 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
312 continue;
313 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
314 continue;
e30db56f
MD
315 if (pref <= keyboard[i]->kb_pref) {
316 pref = keyboard[i]->kb_pref;
317 pref_index = i;
318 }
984263bc 319 }
e30db56f 320 return (pref_index);
984263bc
MD
321}
322
323/* allocate a keyboard */
324int
325kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
326 void *arg)
327{
328 int index;
984263bc
MD
329
330 if (func == NULL)
331 return -1;
332
a38e57d5 333 crit_enter();
984263bc
MD
334 index = kbd_find_keyboard(driver, unit);
335 if (index >= 0) {
336 if (KBD_IS_BUSY(keyboard[index])) {
a38e57d5 337 crit_exit();
984263bc
MD
338 return -1;
339 }
340 keyboard[index]->kb_token = id;
341 KBD_BUSY(keyboard[index]);
342 keyboard[index]->kb_callback.kc_func = func;
343 keyboard[index]->kb_callback.kc_arg = arg;
344 (*kbdsw[index]->clear_state)(keyboard[index]);
345 }
a38e57d5 346 crit_exit();
984263bc
MD
347 return index;
348}
349
350int
351kbd_release(keyboard_t *kbd, void *id)
352{
353 int error;
984263bc 354
a38e57d5 355 crit_enter();
984263bc
MD
356 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
357 error = EINVAL;
358 } else if (kbd->kb_token != id) {
359 error = EPERM;
360 } else {
361 kbd->kb_token = NULL;
362 KBD_UNBUSY(kbd);
363 kbd->kb_callback.kc_func = NULL;
364 kbd->kb_callback.kc_arg = NULL;
365 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
366 error = 0;
367 }
a38e57d5 368 crit_exit();
984263bc
MD
369 return error;
370}
371
372int
373kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
374 void *arg)
375{
376 int error;
984263bc 377
a38e57d5 378 crit_enter();
984263bc
MD
379 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
380 error = EINVAL;
381 } else if (kbd->kb_token != id) {
382 error = EPERM;
383 } else if (func == NULL) {
384 error = EINVAL;
385 } else {
386 kbd->kb_callback.kc_func = func;
387 kbd->kb_callback.kc_arg = arg;
388 error = 0;
389 }
a38e57d5 390 crit_exit();
984263bc
MD
391 return error;
392}
393
394/* get a keyboard structure */
3d8ba483
SW
395keyboard_t *
396kbd_get_keyboard(int index)
984263bc
MD
397{
398 if ((index < 0) || (index >= keyboards))
399 return NULL;
400 if (keyboard[index] == NULL)
401 return NULL;
402 if (!KBD_IS_VALID(keyboard[index]))
403 return NULL;
404 return keyboard[index];
405}
406
407/*
408 * The back door for the console driver; configure keyboards
409 * This function is for the kernel console to initialize keyboards
410 * at very early stage.
411 */
412
413int
414kbd_configure(int flags)
415{
416 const keyboard_driver_t **list;
417 const keyboard_driver_t *p;
418
419 SLIST_FOREACH(p, &keyboard_drivers, link) {
420 if (p->configure != NULL)
421 (*p->configure)(flags);
422 }
dc62b251
MD
423 SET_FOREACH(list, kbddriver_set) {
424 p = *list;
984263bc
MD
425 if (p->configure != NULL)
426 (*p->configure)(flags);
427 }
428
429 return 0;
430}
431
432#ifdef KBD_INSTALL_CDEV
433
434/*
435 * Virtual keyboard cdev driver functions
436 * The virtual keyboard driver dispatches driver functions to
437 * appropriate subdrivers.
438 */
439
440#define KBD_UNIT(dev) minor(dev)
441
442static d_open_t genkbdopen;
443static d_close_t genkbdclose;
444static d_read_t genkbdread;
445static d_write_t genkbdwrite;
446static d_ioctl_t genkbdioctl;
447static d_poll_t genkbdpoll;
448
449#define CDEV_MAJOR 112
450
fef8985e
MD
451static struct dev_ops kbd_ops = {
452 { "kbd", CDEV_MAJOR, 0 },
453 .d_open = genkbdopen,
454 .d_close = genkbdclose,
455 .d_read = genkbdread,
456 .d_write = genkbdwrite,
457 .d_ioctl = genkbdioctl,
458 .d_poll = genkbdpoll,
984263bc
MD
459};
460
8a0a8f43
MD
461/*
462 * Attach a keyboard.
463 *
464 * NOTE: The usb driver does not detach the default keyboard if it is
465 * unplugged, but calls kbd_attach() when it is plugged back in.
466 */
984263bc
MD
467int
468kbd_attach(keyboard_t *kbd)
469{
b13267a5 470 cdev_t dev;
984263bc
MD
471
472 if (kbd->kb_index >= keyboards)
473 return EINVAL;
474 if (keyboard[kbd->kb_index] != kbd)
475 return EINVAL;
476
8a0a8f43
MD
477 if (kbd->kb_dev == NULL) {
478 kbd->kb_dev = make_dev(&kbd_ops, kbd->kb_index,
479 UID_ROOT, GID_WHEEL, 0600,
480 "kbd%r", kbd->kb_index);
481 }
482 dev = kbd->kb_dev;
483 if (dev->si_drv1 == NULL) {
484 dev->si_drv1 = kmalloc(sizeof(struct genkbd_softc), M_DEVBUF,
485 M_WAITOK);
486 }
487 bzero(dev->si_drv1, sizeof(struct genkbd_softc));
984263bc 488
e3869ec7 489 kprintf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
984263bc
MD
490 return 0;
491}
492
493int
494kbd_detach(keyboard_t *kbd)
495{
b13267a5 496 cdev_t dev;
984263bc
MD
497
498 if (kbd->kb_index >= keyboards)
499 return EINVAL;
500 if (keyboard[kbd->kb_index] != kbd)
501 return EINVAL;
502
8a0a8f43 503 if ((dev = kbd->kb_dev) != NULL) {
e4c9c0c8 504 if (dev->si_drv1) {
efda3bd0 505 kfree(dev->si_drv1, M_DEVBUF);
e4c9c0c8
MD
506 dev->si_drv1 = NULL;
507 }
8a0a8f43 508 kbd->kb_dev = NULL;
e4c9c0c8 509 }
cd29885a 510 dev_ops_remove_minor(&kbd_ops, kbd->kb_index);
984263bc
MD
511 return 0;
512}
513
514/*
515 * Generic keyboard cdev driver functions
516 * Keyboard subdrivers may call these functions to implement common
517 * driver functions.
518 */
519
520#define KB_QSIZE 512
521#define KB_BUFSIZE 64
522
523static kbd_callback_func_t genkbd_event;
524
525static int
fef8985e 526genkbdopen(struct dev_open_args *ap)
984263bc 527{
b13267a5 528 cdev_t dev = ap->a_head.a_dev;
984263bc 529 keyboard_t *kbd;
8a0a8f43 530 genkbd_softc_t sc;
984263bc
MD
531 int i;
532
a38e57d5 533 crit_enter();
984263bc
MD
534 sc = dev->si_drv1;
535 kbd = kbd_get_keyboard(KBD_INDEX(dev));
536 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
a38e57d5 537 crit_exit();
984263bc
MD
538 return ENXIO;
539 }
540 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
541 genkbd_event, (void *)sc);
542 if (i < 0) {
a38e57d5 543 crit_exit();
984263bc
MD
544 return EBUSY;
545 }
546 /* assert(i == kbd->kb_index) */
547 /* assert(kbd == kbd_get_keyboard(i)) */
548
549 /*
550 * NOTE: even when we have successfully claimed a keyboard,
551 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
552 */
553
554#if 0
555 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
556#endif
557 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
558 sc->gkb_rsel.si_flags = 0;
559 sc->gkb_rsel.si_pid = 0;
a38e57d5 560 crit_exit();
984263bc
MD
561
562 return 0;
563}
564
565static int
fef8985e 566genkbdclose(struct dev_close_args *ap)
984263bc 567{
b13267a5 568 cdev_t dev = ap->a_head.a_dev;
984263bc 569 keyboard_t *kbd;
8a0a8f43 570 genkbd_softc_t sc;
984263bc
MD
571
572 /*
573 * NOTE: the device may have already become invalid.
574 * kbd == NULL || !KBD_IS_VALID(kbd)
575 */
a38e57d5 576 crit_enter();
984263bc
MD
577 sc = dev->si_drv1;
578 kbd = kbd_get_keyboard(KBD_INDEX(dev));
579 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
580 /* XXX: we shall be forgiving and don't report error... */
581 } else {
582 kbd_release(kbd, (void *)sc);
583#if 0
584 clist_free_cblocks(&sc->gkb_q);
585#endif
586 }
a38e57d5 587 crit_exit();
984263bc
MD
588 return 0;
589}
590
591static int
fef8985e 592genkbdread(struct dev_read_args *ap)
984263bc 593{
b13267a5 594 cdev_t dev = ap->a_head.a_dev;
fef8985e 595 struct uio *uio = ap->a_uio;
984263bc 596 keyboard_t *kbd;
8a0a8f43 597 genkbd_softc_t sc;
984263bc
MD
598 u_char buffer[KB_BUFSIZE];
599 int len;
600 int error;
984263bc
MD
601
602 /* wait for input */
a38e57d5 603 crit_enter();
984263bc
MD
604 sc = dev->si_drv1;
605 kbd = kbd_get_keyboard(KBD_INDEX(dev));
606 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
a38e57d5 607 crit_exit();
984263bc
MD
608 return ENXIO;
609 }
610 while (sc->gkb_q.c_cc == 0) {
fef8985e 611 if (ap->a_ioflag & IO_NDELAY) {
a38e57d5 612 crit_exit();
984263bc
MD
613 return EWOULDBLOCK;
614 }
615 sc->gkb_flags |= KB_ASLEEP;
377d4740 616 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
984263bc
MD
617 kbd = kbd_get_keyboard(KBD_INDEX(dev));
618 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
a38e57d5 619 crit_exit();
984263bc
MD
620 return ENXIO; /* our keyboard has gone... */
621 }
622 if (error) {
623 sc->gkb_flags &= ~KB_ASLEEP;
a38e57d5 624 crit_exit();
984263bc
MD
625 return error;
626 }
627 }
a38e57d5 628 crit_exit();
984263bc
MD
629
630 /* copy as much input as possible */
631 error = 0;
632 while (uio->uio_resid > 0) {
e54488bb 633 len = (int)szmin(uio->uio_resid, sizeof(buffer));
984263bc 634 len = q_to_b(&sc->gkb_q, buffer, len);
e54488bb 635 if (len == 0)
984263bc 636 break;
e54488bb 637 error = uiomove(buffer, (size_t)len, uio);
984263bc
MD
638 if (error)
639 break;
640 }
641
642 return error;
643}
644
645static int
fef8985e 646genkbdwrite(struct dev_write_args *ap)
984263bc 647{
b13267a5 648 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
649 keyboard_t *kbd;
650
651 kbd = kbd_get_keyboard(KBD_INDEX(dev));
652 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
653 return ENXIO;
654 return ENODEV;
655}
656
657static int
fef8985e 658genkbdioctl(struct dev_ioctl_args *ap)
984263bc 659{
b13267a5 660 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
661 keyboard_t *kbd;
662 int error;
663
664 kbd = kbd_get_keyboard(KBD_INDEX(dev));
665 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
666 return ENXIO;
fef8985e 667 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, ap->a_cmd, ap->a_data);
984263bc
MD
668 if (error == ENOIOCTL)
669 error = ENODEV;
670 return error;
671}
672
673static int
fef8985e 674genkbdpoll(struct dev_poll_args *ap)
984263bc 675{
b13267a5 676 cdev_t dev = ap->a_head.a_dev;
984263bc 677 keyboard_t *kbd;
8a0a8f43 678 genkbd_softc_t sc;
984263bc 679 int revents;
984263bc
MD
680
681 revents = 0;
a38e57d5 682 crit_enter();
984263bc
MD
683 sc = dev->si_drv1;
684 kbd = kbd_get_keyboard(KBD_INDEX(dev));
685 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
686 revents = POLLHUP; /* the keyboard has gone */
fef8985e 687 } else if (ap->a_events & (POLLIN | POLLRDNORM)) {
984263bc 688 if (sc->gkb_q.c_cc > 0)
fef8985e 689 revents = ap->a_events & (POLLIN | POLLRDNORM);
984263bc 690 else
fef8985e 691 selrecord(curthread, &sc->gkb_rsel);
984263bc 692 }
a38e57d5 693 crit_exit();
fef8985e
MD
694 ap->a_events = revents;
695 return (0);
984263bc
MD
696}
697
698static int
699genkbd_event(keyboard_t *kbd, int event, void *arg)
700{
8a0a8f43 701 genkbd_softc_t sc;
984263bc
MD
702 size_t len;
703 u_char *cp;
704 int mode;
705 int c;
706
707 /* assert(KBD_IS_VALID(kbd)) */
8a0a8f43 708 sc = (genkbd_softc_t)arg;
984263bc
MD
709
710 switch (event) {
711 case KBDIO_KEYINPUT:
712 break;
713 case KBDIO_UNLOADING:
714 /* the keyboard is going... */
715 kbd_release(kbd, (void *)sc);
716 if (sc->gkb_flags & KB_ASLEEP) {
717 sc->gkb_flags &= ~KB_ASLEEP;
718 wakeup((caddr_t)sc);
719 }
720 selwakeup(&sc->gkb_rsel);
721 return 0;
722 default:
723 return EINVAL;
724 }
725
726 /* obtain the current key input mode */
727 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
728 mode = K_XLATE;
729
730 /* read all pending input */
731 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
732 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
733 if (c == NOKEY)
734 continue;
735 if (c == ERRKEY) /* XXX: ring bell? */
736 continue;
737 if (!KBD_IS_BUSY(kbd))
738 /* the device is not open, discard the input */
739 continue;
740
741 /* store the byte as is for K_RAW and K_CODE modes */
742 if (mode != K_XLATE) {
0ced1954 743 clist_putc(KEYCHAR(c), &sc->gkb_q);
984263bc
MD
744 continue;
745 }
746
747 /* K_XLATE */
748 if (c & RELKEY) /* key release is ignored */
749 continue;
750
751 /* process special keys; most of them are just ignored... */
752 if (c & SPCLKEY) {
753 switch (KEYCHAR(c)) {
754 default:
755 /* ignore them... */
756 continue;
757 case BTAB: /* a backtab: ESC [ Z */
0ced1954
MD
758 clist_putc(0x1b, &sc->gkb_q);
759 clist_putc('[', &sc->gkb_q);
760 clist_putc('Z', &sc->gkb_q);
984263bc
MD
761 continue;
762 }
763 }
764
765 /* normal chars, normal chars with the META, function keys */
766 switch (KEYFLAGS(c)) {
767 case 0: /* a normal char */
0ced1954 768 clist_putc(KEYCHAR(c), &sc->gkb_q);
984263bc
MD
769 break;
770 case MKEY: /* the META flag: prepend ESC */
0ced1954
MD
771 clist_putc(0x1b, &sc->gkb_q);
772 clist_putc(KEYCHAR(c), &sc->gkb_q);
984263bc
MD
773 break;
774 case FKEY | SPCLKEY: /* a function key, return string */
775 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
776 KEYCHAR(c), &len);
777 if (cp != NULL) {
778 while (len-- > 0)
0ced1954 779 clist_putc(*cp++, &sc->gkb_q);
984263bc
MD
780 }
781 break;
782 }
783 }
784
785 /* wake up sleeping/polling processes */
786 if (sc->gkb_q.c_cc > 0) {
787 if (sc->gkb_flags & KB_ASLEEP) {
788 sc->gkb_flags &= ~KB_ASLEEP;
789 wakeup((caddr_t)sc);
790 }
791 selwakeup(&sc->gkb_rsel);
792 }
793
794 return 0;
795}
796
797#endif /* KBD_INSTALL_CDEV */
798
799/*
800 * Generic low-level keyboard functions
801 * The low-level functions in the keyboard subdriver may use these
802 * functions.
803 */
804
805int
806genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
807{
808 keyarg_t *keyp;
809 fkeyarg_t *fkeyp;
984263bc
MD
810 int i;
811
a38e57d5 812 crit_enter();
984263bc
MD
813 switch (cmd) {
814
815 case KDGKBINFO: /* get keyboard information */
816 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
817 i = imin(strlen(kbd->kb_name) + 1,
818 sizeof(((keyboard_info_t *)arg)->kb_name));
819 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
820 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
821 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
822 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
823 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
824 break;
825
826 case KDGKBTYPE: /* get keyboard type */
827 *(int *)arg = kbd->kb_type;
828 break;
829
830 case KDGETREPEAT: /* get keyboard repeat rate */
831 ((int *)arg)[0] = kbd->kb_delay1;
832 ((int *)arg)[1] = kbd->kb_delay2;
833 break;
834
835 case GIO_KEYMAP: /* get keyboard translation table */
836 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
837 break;
838 case PIO_KEYMAP: /* set keyboard translation table */
839#ifndef KBD_DISABLE_KEYMAP_LOAD
840 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
841 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
842 break;
843#else
a38e57d5 844 crit_exit();
984263bc
MD
845 return ENODEV;
846#endif
847
848 case GIO_KEYMAPENT: /* get keyboard translation table entry */
849 keyp = (keyarg_t *)arg;
850 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
851 /sizeof(kbd->kb_keymap->key[0])) {
a38e57d5 852 crit_exit();
984263bc
MD
853 return EINVAL;
854 }
855 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
856 sizeof(keyp->key));
857 break;
858 case PIO_KEYMAPENT: /* set keyboard translation table entry */
859#ifndef KBD_DISABLE_KEYMAP_LOAD
860 keyp = (keyarg_t *)arg;
861 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
862 /sizeof(kbd->kb_keymap->key[0])) {
a38e57d5 863 crit_exit();
984263bc
MD
864 return EINVAL;
865 }
866 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
867 sizeof(keyp->key));
868 break;
869#else
a38e57d5 870 crit_exit();
984263bc
MD
871 return ENODEV;
872#endif
873
874 case GIO_DEADKEYMAP: /* get accent key translation table */
875 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
876 break;
877 case PIO_DEADKEYMAP: /* set accent key translation table */
878#ifndef KBD_DISABLE_KEYMAP_LOAD
879 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
880 break;
881#else
a38e57d5 882 crit_exit();
984263bc
MD
883 return ENODEV;
884#endif
885
886 case GETFKEY: /* get functionkey string */
887 fkeyp = (fkeyarg_t *)arg;
888 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
a38e57d5 889 crit_exit();
984263bc
MD
890 return EINVAL;
891 }
892 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
893 kbd->kb_fkeytab[fkeyp->keynum].len);
894 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
895 break;
896 case SETFKEY: /* set functionkey string */
897#ifndef KBD_DISABLE_KEYMAP_LOAD
898 fkeyp = (fkeyarg_t *)arg;
899 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
a38e57d5 900 crit_exit();
984263bc
MD
901 return EINVAL;
902 }
903 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
904 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
905 kbd->kb_fkeytab[fkeyp->keynum].len);
906 break;
907#else
a38e57d5 908 crit_exit();
984263bc
MD
909 return ENODEV;
910#endif
911
912 default:
a38e57d5 913 crit_exit();
984263bc
MD
914 return ENOIOCTL;
915 }
916
a38e57d5 917 crit_exit();
984263bc
MD
918 return 0;
919}
920
921/* get a pointer to the string associated with the given function key */
3d8ba483
SW
922u_char *
923genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
984263bc
MD
924{
925 if (kbd == NULL)
926 return NULL;
927 fkey -= F_FN;
928 if (fkey > kbd->kb_fkeytab_size)
929 return NULL;
930 *len = kbd->kb_fkeytab[fkey].len;
931 return kbd->kb_fkeytab[fkey].str;
932}
933
934/* diagnostic dump */
3d8ba483
SW
935static char *
936get_kbd_type_name(int type)
984263bc
MD
937{
938 static struct {
939 int type;
940 char *name;
941 } name_table[] = {
942 { KB_84, "AT 84" },
943 { KB_101, "AT 101/102" },
944 { KB_OTHER, "generic" },
945 };
946 int i;
947
948 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
949 if (type == name_table[i].type)
950 return name_table[i].name;
951 }
952 return "unknown";
953}
954
955void
956genkbd_diag(keyboard_t *kbd, int level)
957{
958 if (level > 0) {
e3869ec7 959 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
984263bc
MD
960 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
961 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
962 kbd->kb_config, kbd->kb_flags);
963 if (kbd->kb_io_base > 0)
e3869ec7 964 kprintf(", port:0x%x-0x%x", kbd->kb_io_base,
984263bc 965 kbd->kb_io_base + kbd->kb_io_size - 1);
e3869ec7 966 kprintf("\n");
984263bc
MD
967 }
968}
969
970#define set_lockkey_state(k, s, l) \
971 if (!((s) & l ## DOWN)) { \
972 int i; \
973 (s) |= l ## DOWN; \
974 (s) ^= l ## ED; \
975 i = (s) & LOCK_MASK; \
976 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
977 }
978
979static u_int
980save_accent_key(keyboard_t *kbd, u_int key, int *accents)
981{
982 int i;
983
984 /* make an index into the accent map */
985 i = key - F_ACC + 1;
986 if ((i > kbd->kb_accentmap->n_accs)
987 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
988 /* the index is out of range or pointing to an empty entry */
989 *accents = 0;
990 return ERRKEY;
991 }
992
993 /*
994 * If the same accent key has been hit twice, produce the accent char
995 * itself.
996 */
997 if (i == *accents) {
998 key = kbd->kb_accentmap->acc[i - 1].accchar;
999 *accents = 0;
1000 return key;
1001 }
1002
1003 /* remember the index and wait for the next key */
1004 *accents = i;
1005 return NOKEY;
1006}
1007
1008static u_int
1009make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1010{
1011 struct acc_t *acc;
1012 int i;
1013
1014 acc = &kbd->kb_accentmap->acc[*accents - 1];
1015 *accents = 0;
1016
1017 /*
1018 * If the accent key is followed by the space key,
1019 * produce the accent char itself.
1020 */
1021 if (ch == ' ')
1022 return acc->accchar;
1023
1024 /* scan the accent map */
1025 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1026 if (acc->map[i][0] == 0) /* end of table */
1027 break;
1028 if (acc->map[i][0] == ch)
1029 return acc->map[i][1];
1030 }
1031 /* this char cannot be accented... */
1032 return ERRKEY;
1033}
1034
1035int
1036genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1037 int *accents)
1038{
1039 struct keyent_t *key;
1040 int state = *shiftstate;
1041 int action;
1042 int f;
1043 int i;
1044
1045 i = keycode;
1046 f = state & (AGRS | ALKED);
1047 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1048 i += ALTGR_OFFSET;
1049 key = &kbd->kb_keymap->key[i];
1050 i = ((state & SHIFTS) ? 1 : 0)
1051 | ((state & CTLS) ? 2 : 0)
1052 | ((state & ALTS) ? 4 : 0);
1053 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1054 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1055 i ^= 1;
1056
1057 if (up) { /* break: key released */
1058 action = kbd->kb_lastact[keycode];
1059 kbd->kb_lastact[keycode] = NOP;
1060 switch (action) {
1061 case LSHA:
1062 if (state & SHIFTAON) {
1063 set_lockkey_state(kbd, state, ALK);
1064 state &= ~ALKDOWN;
1065 }
1066 action = LSH;
1067 /* FALL THROUGH */
1068 case LSH:
1069 state &= ~SHIFTS1;
1070 break;
1071 case RSHA:
1072 if (state & SHIFTAON) {
1073 set_lockkey_state(kbd, state, ALK);
1074 state &= ~ALKDOWN;
1075 }
1076 action = RSH;
1077 /* FALL THROUGH */
1078 case RSH:
1079 state &= ~SHIFTS2;
1080 break;
1081 case LCTRA:
1082 if (state & SHIFTAON) {
1083 set_lockkey_state(kbd, state, ALK);
1084 state &= ~ALKDOWN;
1085 }
1086 action = LCTR;
1087 /* FALL THROUGH */
1088 case LCTR:
1089 state &= ~CTLS1;
1090 break;
1091 case RCTRA:
1092 if (state & SHIFTAON) {
1093 set_lockkey_state(kbd, state, ALK);
1094 state &= ~ALKDOWN;
1095 }
1096 action = RCTR;
1097 /* FALL THROUGH */
1098 case RCTR:
1099 state &= ~CTLS2;
1100 break;
1101 case LALTA:
1102 if (state & SHIFTAON) {
1103 set_lockkey_state(kbd, state, ALK);
1104 state &= ~ALKDOWN;
1105 }
1106 action = LALT;
1107 /* FALL THROUGH */
1108 case LALT:
1109 state &= ~ALTS1;
1110 break;
1111 case RALTA:
1112 if (state & SHIFTAON) {
1113 set_lockkey_state(kbd, state, ALK);
1114 state &= ~ALKDOWN;
1115 }
1116 action = RALT;
1117 /* FALL THROUGH */
1118 case RALT:
1119 state &= ~ALTS2;
1120 break;
1121 case ASH:
1122 state &= ~AGRS1;
1123 break;
1124 case META:
1125 state &= ~METAS1;
1126 break;
1127 case NLK:
1128 state &= ~NLKDOWN;
1129 break;
1130 case CLK:
984263bc 1131 state &= ~CLKDOWN;
984263bc
MD
1132 break;
1133 case SLK:
1134 state &= ~SLKDOWN;
1135 break;
1136 case ALK:
1137 state &= ~ALKDOWN;
1138 break;
1139 case NOP:
1140 /* release events of regular keys are not reported */
1141 *shiftstate &= ~SHIFTAON;
1142 return NOKEY;
1143 }
1144 *shiftstate = state & ~SHIFTAON;
1145 return (SPCLKEY | RELKEY | action);
1146 } else { /* make: key pressed */
1147 action = key->map[i];
1148 state &= ~SHIFTAON;
1149 if (key->spcl & (0x80 >> i)) {
1150 /* special keys */
1151 if (kbd->kb_lastact[keycode] == NOP)
1152 kbd->kb_lastact[keycode] = action;
1153 if (kbd->kb_lastact[keycode] != action)
1154 action = NOP;
1155 switch (action) {
1156 /* LOCKING KEYS */
1157 case NLK:
1158 set_lockkey_state(kbd, state, NLK);
1159 break;
1160 case CLK:
984263bc 1161 set_lockkey_state(kbd, state, CLK);
984263bc
MD
1162 break;
1163 case SLK:
1164 set_lockkey_state(kbd, state, SLK);
1165 break;
1166 case ALK:
1167 set_lockkey_state(kbd, state, ALK);
1168 break;
1169 /* NON-LOCKING KEYS */
1170 case SPSC: case RBT: case SUSP: case STBY:
1171 case DBG: case NEXT: case PREV: case PNC:
1172 case HALT: case PDWN:
1173 *accents = 0;
1174 break;
1175 case BTAB:
1176 *accents = 0;
1177 action |= BKEY;
1178 break;
1179 case LSHA:
1180 state |= SHIFTAON;
1181 action = LSH;
1182 /* FALL THROUGH */
1183 case LSH:
1184 state |= SHIFTS1;
1185 break;
1186 case RSHA:
1187 state |= SHIFTAON;
1188 action = RSH;
1189 /* FALL THROUGH */
1190 case RSH:
1191 state |= SHIFTS2;
1192 break;
1193 case LCTRA:
1194 state |= SHIFTAON;
1195 action = LCTR;
1196 /* FALL THROUGH */
1197 case LCTR:
1198 state |= CTLS1;
1199 break;
1200 case RCTRA:
1201 state |= SHIFTAON;
1202 action = RCTR;
1203 /* FALL THROUGH */
1204 case RCTR:
1205 state |= CTLS2;
1206 break;
1207 case LALTA:
1208 state |= SHIFTAON;
1209 action = LALT;
1210 /* FALL THROUGH */
1211 case LALT:
1212 state |= ALTS1;
1213 break;
1214 case RALTA:
1215 state |= SHIFTAON;
1216 action = RALT;
1217 /* FALL THROUGH */
1218 case RALT:
1219 state |= ALTS2;
1220 break;
1221 case ASH:
1222 state |= AGRS1;
1223 break;
1224 case META:
1225 state |= METAS1;
1226 break;
1227 case NOP:
1228 *shiftstate = state;
1229 return NOKEY;
1230 default:
1231 /* is this an accent (dead) key? */
1232 *shiftstate = state;
1233 if (action >= F_ACC && action <= L_ACC) {
1234 action = save_accent_key(kbd, action,
1235 accents);
1236 switch (action) {
1237 case NOKEY:
1238 case ERRKEY:
1239 return action;
1240 default:
1241 if (state & METAS)
1242 return (action | MKEY);
1243 else
1244 return action;
1245 }
1246 /* NOT REACHED */
1247 }
1248 /* other special keys */
1249 if (*accents > 0) {
1250 *accents = 0;
1251 return ERRKEY;
1252 }
1253 if (action >= F_FN && action <= L_FN)
1254 action |= FKEY;
1255 /* XXX: return fkey string for the FKEY? */
1256 return (SPCLKEY | action);
1257 }
1258 *shiftstate = state;
1259 return (SPCLKEY | action);
1260 } else {
1261 /* regular keys */
1262 kbd->kb_lastact[keycode] = NOP;
1263 *shiftstate = state;
1264 if (*accents > 0) {
1265 /* make an accented char */
1266 action = make_accent_char(kbd, action, accents);
1267 if (action == ERRKEY)
1268 return action;
1269 }
1270 if (state & METAS)
1271 action |= MKEY;
1272 return action;
1273 }
1274 }
1275 /* NOT REACHED */
1276}