File:
[DragonFly] /
src /
sys /
dev /
misc /
kbd /
kbd.c
Revision
1.10:
download - view:
text,
annotated -
select for diffs
Wed May 19 22:52:42 2004 UTC (9 years ago) by
dillon
Branches:
MAIN
CVS tags:
HEAD,
DragonFly_1_0_REL,
DragonFly_1_0_RC1,
DragonFly_1_0A_REL
Device layer rollup commit.
* cdevsw_add() is now required. cdevsw_add() and cdevsw_remove() may specify
a mask/match indicating the range of supported minor numbers. Multiple
cdevsw_add()'s using the same major number, but distinctly different
ranges, may be issued. All devices that failed to call cdevsw_add() before
now do.
* cdevsw_remove() now automatically marks all devices within its supported
range as being destroyed.
* vnode->v_rdev is no longer resolved when the vnode is created. Instead,
only v_udev (a newly added field) is resolved. v_rdev is resolved when
the vnode is opened and cleared on the last close.
* A great deal of code was making rather dubious assumptions with regards
to the validity of devices associated with vnodes, primarily due to
the persistence of a device structure due to being indexed by (major, minor)
instead of by (cdevsw, major, minor). In particular, if you run a program
which connects to a USB device and then you pull the USB device and plug
it back in, the vnode subsystem will continue to believe that the device
is open when, in fact, it isn't (because it was destroyed and recreated).
In particular, note that all the VFS mount procedures now check devices
via v_udev instead of v_rdev prior to calling VOP_OPEN(), since v_rdev
is NULL prior to the first open.
* The disk layer's device interaction has been rewritten. The disk layer
(i.e. the slice and disklabel management layer) no longer overloads
its data onto the device structure representing the underlying physical
disk. Instead, the disk layer uses the new cdevsw_add() functionality
to register its own cdevsw using the underlying device's major number,
and simply does NOT register the underlying device's cdevsw. No
confusion is created because the device hash is now based on
(cdevsw,major,minor) rather then (major,minor).
NOTE: This also means that underlying raw disk devices may use the entire
device minor number instead of having to reserve the bits used by the disk
layer, and also means that can we (theoretically) stack a fully
disklabel-supported 'disk' on top of any block device.
* The new reference counting scheme prevents this by associating a device
with a cdevsw and disconnecting the device from its cdevsw when the cdevsw
is removed. Additionally, all udev2dev() lookups run through the cdevsw
mask/match and only successfully find devices still associated with an
active cdevsw.
* Major work on MFS: MFS no longer shortcuts vnode and device creation. It
now creates a real vnode and a real device and implements real open and
close VOPs. Additionally, due to the disk layer changes, MFS is no longer
limited to 255 mounts. The new limit is 16 million. Since MFS creates a
real device node, mount_mfs will now create a real /dev/mfs<PID> device
that can be read from userland (e.g. so you can dump an MFS filesystem).
* BUF AND DEVICE STRATEGY changes. The struct buf contains a b_dev field.
In order to properly handle stacked devices we now require that the b_dev
field be initialized before the device strategy routine is called. This
required some additional work in various VFS implementations. To enforce
this requirement, biodone() now sets b_dev to NODEV. The new disk layer
will adjust b_dev before forwarding a request to the actual physical
device.
* A bug in the ISO CD boot sequence which resulted in a panic has been fixed.
Testing by: lots of people, but David Rhodus found the most aggregious bugs.
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 $
27: * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.10 2004/05/19 22:52:42 dillon Exp $
28: */
29:
30: #include "opt_kbd.h"
31:
32: #include <sys/param.h>
33: #include <sys/systm.h>
34: #include <sys/kernel.h>
35: #include <sys/malloc.h>
36: #include <sys/conf.h>
37: #include <sys/proc.h>
38: #include <sys/tty.h>
39: #include <sys/poll.h>
40: #include <sys/vnode.h>
41: #include <sys/uio.h>
42:
43: #include <machine/console.h>
44:
45: #include "kbdreg.h"
46:
47: #define KBD_INDEX(dev) minor(dev)
48:
49: typedef struct genkbd_softc {
50: int gkb_flags; /* flag/status bits */
51: #define KB_ASLEEP (1 << 0)
52: struct clist gkb_q; /* input queue */
53: struct selinfo gkb_rsel;
54: } genkbd_softc_t;
55:
56: static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
57: SLIST_HEAD_INITIALIZER(keyboard_drivers);
58:
59: SET_DECLARE(kbddriver_set, const keyboard_driver_t);
60:
61: /* local arrays */
62:
63: /*
64: * We need at least one entry each in order to initialize a keyboard
65: * for the kernel console. The arrays will be increased dynamically
66: * when necessary.
67: */
68:
69: static int keyboards = 1;
70: static keyboard_t *kbd_ini;
71: static keyboard_t **keyboard = &kbd_ini;
72: static keyboard_switch_t *kbdsw_ini;
73: keyboard_switch_t **kbdsw = &kbdsw_ini;
74:
75: #define ARRAY_DELTA 4
76:
77: static int
78: kbd_realloc_array(void)
79: {
80: keyboard_t **new_kbd;
81: keyboard_switch_t **new_kbdsw;
82: int newsize;
83: int s;
84:
85: s = spltty();
86: newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
87: new_kbd = malloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
88: M_WAITOK | M_ZERO);
89: new_kbdsw = malloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
90: M_WAITOK | M_ZERO);
91: bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
92: bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
93: if (keyboards > 1) {
94: free(keyboard, M_DEVBUF);
95: free(kbdsw, M_DEVBUF);
96: }
97: keyboard = new_kbd;
98: kbdsw = new_kbdsw;
99: keyboards = newsize;
100: splx(s);
101:
102: if (bootverbose)
103: printf("kbd: new array size %d\n", keyboards);
104:
105: return 0;
106: }
107:
108: /*
109: * Low-level keyboard driver functions
110: * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
111: * driver, call these functions to initialize the keyboard_t structure
112: * and register it to the virtual keyboard driver `kbd'.
113: */
114:
115: /* initialize the keyboard_t structure */
116: void
117: kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
118: int port, int port_size)
119: {
120: kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
121: kbd->kb_name = name;
122: kbd->kb_type = type;
123: kbd->kb_unit = unit;
124: kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
125: kbd->kb_led = 0; /* unknown */
126: kbd->kb_io_base = port;
127: kbd->kb_io_size = port_size;
128: kbd->kb_data = NULL;
129: kbd->kb_keymap = NULL;
130: kbd->kb_accentmap = NULL;
131: kbd->kb_fkeytab = NULL;
132: kbd->kb_fkeytab_size = 0;
133: kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
134: kbd->kb_delay2 = KB_DELAY2;
135: kbd->kb_count = 0L;
136: bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
137: }
138:
139: void
140: kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
141: fkeytab_t *fkeymap, int fkeymap_size)
142: {
143: kbd->kb_keymap = keymap;
144: kbd->kb_accentmap = accmap;
145: kbd->kb_fkeytab = fkeymap;
146: kbd->kb_fkeytab_size = fkeymap_size;
147: }
148:
149: /* declare a new keyboard driver */
150: int
151: kbd_add_driver(keyboard_driver_t *driver)
152: {
153: if (SLIST_NEXT(driver, link))
154: return EINVAL;
155: SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
156: return 0;
157: }
158:
159: int
160: kbd_delete_driver(keyboard_driver_t *driver)
161: {
162: SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
163: SLIST_NEXT(driver, link) = NULL;
164: return 0;
165: }
166:
167: /* register a keyboard and associate it with a function table */
168: int
169: kbd_register(keyboard_t *kbd)
170: {
171: const keyboard_driver_t **list;
172: const keyboard_driver_t *p;
173: int index;
174:
175: for (index = 0; index < keyboards; ++index) {
176: if (keyboard[index] == NULL)
177: break;
178: }
179: if (index >= keyboards) {
180: if (kbd_realloc_array())
181: return -1;
182: }
183:
184: kbd->kb_index = index;
185: KBD_UNBUSY(kbd);
186: KBD_VALID(kbd);
187: kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
188: kbd->kb_token = NULL;
189: kbd->kb_callback.kc_func = NULL;
190: kbd->kb_callback.kc_arg = NULL;
191:
192: SLIST_FOREACH(p, &keyboard_drivers, link) {
193: if (strcmp(p->name, kbd->kb_name) == 0) {
194: keyboard[index] = kbd;
195: kbdsw[index] = p->kbdsw;
196: return index;
197: }
198: }
199: SET_FOREACH(list, kbddriver_set) {
200: p = *list;
201: if (strcmp(p->name, kbd->kb_name) == 0) {
202: keyboard[index] = kbd;
203: kbdsw[index] = p->kbdsw;
204: return index;
205: }
206: }
207:
208: return -1;
209: }
210:
211: int
212: kbd_unregister(keyboard_t *kbd)
213: {
214: int error;
215: int s;
216:
217: if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
218: return ENOENT;
219: if (keyboard[kbd->kb_index] != kbd)
220: return ENOENT;
221:
222: s = spltty();
223: if (KBD_IS_BUSY(kbd)) {
224: error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
225: kbd->kb_callback.kc_arg);
226: if (error) {
227: splx(s);
228: return error;
229: }
230: if (KBD_IS_BUSY(kbd)) {
231: splx(s);
232: return EBUSY;
233: }
234: }
235: KBD_INVALID(kbd);
236: keyboard[kbd->kb_index] = NULL;
237: kbdsw[kbd->kb_index] = NULL;
238:
239: splx(s);
240: return 0;
241: }
242:
243: /* find a funciton table by the driver name */
244: keyboard_switch_t
245: *kbd_get_switch(char *driver)
246: {
247: const keyboard_driver_t **list;
248: const keyboard_driver_t *p;
249:
250: SLIST_FOREACH(p, &keyboard_drivers, link) {
251: if (strcmp(p->name, driver) == 0)
252: return p->kbdsw;
253: }
254: SET_FOREACH(list, kbddriver_set) {
255: p = *list;
256: if (strcmp(p->name, driver) == 0)
257: return p->kbdsw;
258: }
259:
260: return NULL;
261: }
262:
263: /*
264: * Keyboard client functions
265: * Keyboard clients, such as the console driver `syscons' and the keyboard
266: * cdev driver, use these functions to claim and release a keyboard for
267: * exclusive use.
268: */
269:
270: /* find the keyboard specified by a driver name and a unit number */
271: int
272: kbd_find_keyboard(char *driver, int unit)
273: {
274: int i;
275:
276: for (i = 0; i < keyboards; ++i) {
277: if (keyboard[i] == NULL)
278: continue;
279: if (!KBD_IS_VALID(keyboard[i]))
280: continue;
281: if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
282: continue;
283: if ((unit != -1) && (keyboard[i]->kb_unit != unit))
284: continue;
285: return i;
286: }
287: return -1;
288: }
289:
290: /* allocate a keyboard */
291: int
292: kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
293: void *arg)
294: {
295: int index;
296: int s;
297:
298: if (func == NULL)
299: return -1;
300:
301: s = spltty();
302: index = kbd_find_keyboard(driver, unit);
303: if (index >= 0) {
304: if (KBD_IS_BUSY(keyboard[index])) {
305: splx(s);
306: return -1;
307: }
308: keyboard[index]->kb_token = id;
309: KBD_BUSY(keyboard[index]);
310: keyboard[index]->kb_callback.kc_func = func;
311: keyboard[index]->kb_callback.kc_arg = arg;
312: (*kbdsw[index]->clear_state)(keyboard[index]);
313: }
314: splx(s);
315: return index;
316: }
317:
318: int
319: kbd_release(keyboard_t *kbd, void *id)
320: {
321: int error;
322: int s;
323:
324: s = spltty();
325: if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
326: error = EINVAL;
327: } else if (kbd->kb_token != id) {
328: error = EPERM;
329: } else {
330: kbd->kb_token = NULL;
331: KBD_UNBUSY(kbd);
332: kbd->kb_callback.kc_func = NULL;
333: kbd->kb_callback.kc_arg = NULL;
334: (*kbdsw[kbd->kb_index]->clear_state)(kbd);
335: error = 0;
336: }
337: splx(s);
338: return error;
339: }
340:
341: int
342: kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
343: void *arg)
344: {
345: int error;
346: int s;
347:
348: s = spltty();
349: if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
350: error = EINVAL;
351: } else if (kbd->kb_token != id) {
352: error = EPERM;
353: } else if (func == NULL) {
354: error = EINVAL;
355: } else {
356: kbd->kb_callback.kc_func = func;
357: kbd->kb_callback.kc_arg = arg;
358: error = 0;
359: }
360: splx(s);
361: return error;
362: }
363:
364: /* get a keyboard structure */
365: keyboard_t
366: *kbd_get_keyboard(int index)
367: {
368: if ((index < 0) || (index >= keyboards))
369: return NULL;
370: if (keyboard[index] == NULL)
371: return NULL;
372: if (!KBD_IS_VALID(keyboard[index]))
373: return NULL;
374: return keyboard[index];
375: }
376:
377: /*
378: * The back door for the console driver; configure keyboards
379: * This function is for the kernel console to initialize keyboards
380: * at very early stage.
381: */
382:
383: int
384: kbd_configure(int flags)
385: {
386: const keyboard_driver_t **list;
387: const keyboard_driver_t *p;
388:
389: SLIST_FOREACH(p, &keyboard_drivers, link) {
390: if (p->configure != NULL)
391: (*p->configure)(flags);
392: }
393: SET_FOREACH(list, kbddriver_set) {
394: p = *list;
395: if (p->configure != NULL)
396: (*p->configure)(flags);
397: }
398:
399: return 0;
400: }
401:
402: #ifdef KBD_INSTALL_CDEV
403:
404: /*
405: * Virtual keyboard cdev driver functions
406: * The virtual keyboard driver dispatches driver functions to
407: * appropriate subdrivers.
408: */
409:
410: #define KBD_UNIT(dev) minor(dev)
411:
412: static d_open_t genkbdopen;
413: static d_close_t genkbdclose;
414: static d_read_t genkbdread;
415: static d_write_t genkbdwrite;
416: static d_ioctl_t genkbdioctl;
417: static d_poll_t genkbdpoll;
418:
419: #define CDEV_MAJOR 112
420:
421: static struct cdevsw kbd_cdevsw = {
422: /* name */ "kbd",
423: /* maj */ CDEV_MAJOR,
424: /* flags */ 0,
425: /* port */ NULL,
426: /* clone */ NULL,
427:
428: /* open */ genkbdopen,
429: /* close */ genkbdclose,
430: /* read */ genkbdread,
431: /* write */ genkbdwrite,
432: /* ioctl */ genkbdioctl,
433: /* poll */ genkbdpoll,
434: /* mmap */ nommap,
435: /* strategy */ nostrategy,
436: /* dump */ nodump,
437: /* psize */ nopsize
438: };
439:
440: int
441: kbd_attach(keyboard_t *kbd)
442: {
443: dev_t dev;
444:
445: if (kbd->kb_index >= keyboards)
446: return EINVAL;
447: if (keyboard[kbd->kb_index] != kbd)
448: return EINVAL;
449:
450: cdevsw_add(&kbd_cdevsw, -1, kbd->kb_index);
451: dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
452: "kbd%r", kbd->kb_index);
453: if (dev->si_drv1 == NULL)
454: dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
455: M_WAITOK);
456: bzero(dev->si_drv1, sizeof(genkbd_softc_t));
457:
458: printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
459: return 0;
460: }
461:
462: int
463: kbd_detach(keyboard_t *kbd)
464: {
465: dev_t dev;
466:
467: if (kbd->kb_index >= keyboards)
468: return EINVAL;
469: if (keyboard[kbd->kb_index] != kbd)
470: return EINVAL;
471:
472: /*
473: * Deal with refs properly. The KBD driver really ought to have
474: * recorded the dev_t separately.
475: */
476: if ((dev = make_adhoc_dev(&kbd_cdevsw, kbd->kb_index)) != NODEV) {
477: if (dev->si_drv1) {
478: free(dev->si_drv1, M_DEVBUF);
479: dev->si_drv1 = NULL;
480: }
481: }
482: cdevsw_remove(&kbd_cdevsw, -1, kbd->kb_index);
483: return 0;
484: }
485:
486: /*
487: * Generic keyboard cdev driver functions
488: * Keyboard subdrivers may call these functions to implement common
489: * driver functions.
490: */
491:
492: #define KB_QSIZE 512
493: #define KB_BUFSIZE 64
494:
495: static kbd_callback_func_t genkbd_event;
496:
497: static int
498: genkbdopen(dev_t dev, int mode, int flag, d_thread_t *td)
499: {
500: keyboard_t *kbd;
501: genkbd_softc_t *sc;
502: int s;
503: int i;
504:
505: s = spltty();
506: sc = dev->si_drv1;
507: kbd = kbd_get_keyboard(KBD_INDEX(dev));
508: if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
509: splx(s);
510: return ENXIO;
511: }
512: i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
513: genkbd_event, (void *)sc);
514: if (i < 0) {
515: splx(s);
516: return EBUSY;
517: }
518: /* assert(i == kbd->kb_index) */
519: /* assert(kbd == kbd_get_keyboard(i)) */
520:
521: /*
522: * NOTE: even when we have successfully claimed a keyboard,
523: * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
524: */
525:
526: #if 0
527: bzero(&sc->gkb_q, sizeof(sc->gkb_q));
528: #endif
529: clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
530: sc->gkb_rsel.si_flags = 0;
531: sc->gkb_rsel.si_pid = 0;
532: splx(s);
533:
534: return 0;
535: }
536:
537: static int
538: genkbdclose(dev_t dev, int mode, int flag, d_thread_t *td)
539: {
540: keyboard_t *kbd;
541: genkbd_softc_t *sc;
542: int s;
543:
544: /*
545: * NOTE: the device may have already become invalid.
546: * kbd == NULL || !KBD_IS_VALID(kbd)
547: */
548: s = spltty();
549: sc = dev->si_drv1;
550: kbd = kbd_get_keyboard(KBD_INDEX(dev));
551: if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
552: /* XXX: we shall be forgiving and don't report error... */
553: } else {
554: kbd_release(kbd, (void *)sc);
555: #if 0
556: clist_free_cblocks(&sc->gkb_q);
557: #endif
558: }
559: splx(s);
560: return 0;
561: }
562:
563: static int
564: genkbdread(dev_t dev, struct uio *uio, int flag)
565: {
566: keyboard_t *kbd;
567: genkbd_softc_t *sc;
568: u_char buffer[KB_BUFSIZE];
569: int len;
570: int error;
571: int s;
572:
573: /* wait for input */
574: s = spltty();
575: sc = dev->si_drv1;
576: kbd = kbd_get_keyboard(KBD_INDEX(dev));
577: if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
578: splx(s);
579: return ENXIO;
580: }
581: while (sc->gkb_q.c_cc == 0) {
582: if (flag & IO_NDELAY) {
583: splx(s);
584: return EWOULDBLOCK;
585: }
586: sc->gkb_flags |= KB_ASLEEP;
587: error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
588: kbd = kbd_get_keyboard(KBD_INDEX(dev));
589: if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
590: splx(s);
591: return ENXIO; /* our keyboard has gone... */
592: }
593: if (error) {
594: sc->gkb_flags &= ~KB_ASLEEP;
595: splx(s);
596: return error;
597: }
598: }
599: splx(s);
600:
601: /* copy as much input as possible */
602: error = 0;
603: while (uio->uio_resid > 0) {
604: len = imin(uio->uio_resid, sizeof(buffer));
605: len = q_to_b(&sc->gkb_q, buffer, len);
606: if (len <= 0)
607: break;
608: error = uiomove(buffer, len, uio);
609: if (error)
610: break;
611: }
612:
613: return error;
614: }
615:
616: static int
617: genkbdwrite(dev_t dev, struct uio *uio, int flag)
618: {
619: keyboard_t *kbd;
620:
621: kbd = kbd_get_keyboard(KBD_INDEX(dev));
622: if ((kbd == NULL) || !KBD_IS_VALID(kbd))
623: return ENXIO;
624: return ENODEV;
625: }
626:
627: static int
628: genkbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
629: {
630: keyboard_t *kbd;
631: int error;
632:
633: kbd = kbd_get_keyboard(KBD_INDEX(dev));
634: if ((kbd == NULL) || !KBD_IS_VALID(kbd))
635: return ENXIO;
636: error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
637: if (error == ENOIOCTL)
638: error = ENODEV;
639: return error;
640: }
641:
642: static int
643: genkbdpoll(dev_t dev, int events, d_thread_t *td)
644: {
645: keyboard_t *kbd;
646: genkbd_softc_t *sc;
647: int revents;
648: int s;
649:
650: revents = 0;
651: s = spltty();
652: sc = dev->si_drv1;
653: kbd = kbd_get_keyboard(KBD_INDEX(dev));
654: if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
655: revents = POLLHUP; /* the keyboard has gone */
656: } else if (events & (POLLIN | POLLRDNORM)) {
657: if (sc->gkb_q.c_cc > 0)
658: revents = events & (POLLIN | POLLRDNORM);
659: else
660: selrecord(td, &sc->gkb_rsel);
661: }
662: splx(s);
663: return revents;
664: }
665:
666: static int
667: genkbd_event(keyboard_t *kbd, int event, void *arg)
668: {
669: genkbd_softc_t *sc;
670: size_t len;
671: u_char *cp;
672: int mode;
673: int c;
674:
675: /* assert(KBD_IS_VALID(kbd)) */
676: sc = (genkbd_softc_t *)arg;
677:
678: switch (event) {
679: case KBDIO_KEYINPUT:
680: break;
681: case KBDIO_UNLOADING:
682: /* the keyboard is going... */
683: kbd_release(kbd, (void *)sc);
684: if (sc->gkb_flags & KB_ASLEEP) {
685: sc->gkb_flags &= ~KB_ASLEEP;
686: wakeup((caddr_t)sc);
687: }
688: selwakeup(&sc->gkb_rsel);
689: return 0;
690: default:
691: return EINVAL;
692: }
693:
694: /* obtain the current key input mode */
695: if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
696: mode = K_XLATE;
697:
698: /* read all pending input */
699: while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
700: c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
701: if (c == NOKEY)
702: continue;
703: if (c == ERRKEY) /* XXX: ring bell? */
704: continue;
705: if (!KBD_IS_BUSY(kbd))
706: /* the device is not open, discard the input */
707: continue;
708:
709: /* store the byte as is for K_RAW and K_CODE modes */
710: if (mode != K_XLATE) {
711: putc(KEYCHAR(c), &sc->gkb_q);
712: continue;
713: }
714:
715: /* K_XLATE */
716: if (c & RELKEY) /* key release is ignored */
717: continue;
718:
719: /* process special keys; most of them are just ignored... */
720: if (c & SPCLKEY) {
721: switch (KEYCHAR(c)) {
722: default:
723: /* ignore them... */
724: continue;
725: case BTAB: /* a backtab: ESC [ Z */
726: putc(0x1b, &sc->gkb_q);
727: putc('[', &sc->gkb_q);
728: putc('Z', &sc->gkb_q);
729: continue;
730: }
731: }
732:
733: /* normal chars, normal chars with the META, function keys */
734: switch (KEYFLAGS(c)) {
735: case 0: /* a normal char */
736: putc(KEYCHAR(c), &sc->gkb_q);
737: break;
738: case MKEY: /* the META flag: prepend ESC */
739: putc(0x1b, &sc->gkb_q);
740: putc(KEYCHAR(c), &sc->gkb_q);
741: break;
742: case FKEY | SPCLKEY: /* a function key, return string */
743: cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
744: KEYCHAR(c), &len);
745: if (cp != NULL) {
746: while (len-- > 0)
747: putc(*cp++, &sc->gkb_q);
748: }
749: break;
750: }
751: }
752:
753: /* wake up sleeping/polling processes */
754: if (sc->gkb_q.c_cc > 0) {
755: if (sc->gkb_flags & KB_ASLEEP) {
756: sc->gkb_flags &= ~KB_ASLEEP;
757: wakeup((caddr_t)sc);
758: }
759: selwakeup(&sc->gkb_rsel);
760: }
761:
762: return 0;
763: }
764:
765: #endif /* KBD_INSTALL_CDEV */
766:
767: /*
768: * Generic low-level keyboard functions
769: * The low-level functions in the keyboard subdriver may use these
770: * functions.
771: */
772:
773: int
774: genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
775: {
776: keyarg_t *keyp;
777: fkeyarg_t *fkeyp;
778: int s;
779: int i;
780:
781: s = spltty();
782: switch (cmd) {
783:
784: case KDGKBINFO: /* get keyboard information */
785: ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
786: i = imin(strlen(kbd->kb_name) + 1,
787: sizeof(((keyboard_info_t *)arg)->kb_name));
788: bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
789: ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
790: ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
791: ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
792: ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
793: break;
794:
795: case KDGKBTYPE: /* get keyboard type */
796: *(int *)arg = kbd->kb_type;
797: break;
798:
799: case KDGETREPEAT: /* get keyboard repeat rate */
800: ((int *)arg)[0] = kbd->kb_delay1;
801: ((int *)arg)[1] = kbd->kb_delay2;
802: break;
803:
804: case GIO_KEYMAP: /* get keyboard translation table */
805: bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
806: break;
807: case PIO_KEYMAP: /* set keyboard translation table */
808: #ifndef KBD_DISABLE_KEYMAP_LOAD
809: bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
810: bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
811: break;
812: #else
813: splx(s);
814: return ENODEV;
815: #endif
816:
817: case GIO_KEYMAPENT: /* get keyboard translation table entry */
818: keyp = (keyarg_t *)arg;
819: if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
820: /sizeof(kbd->kb_keymap->key[0])) {
821: splx(s);
822: return EINVAL;
823: }
824: bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
825: sizeof(keyp->key));
826: break;
827: case PIO_KEYMAPENT: /* set keyboard translation table entry */
828: #ifndef KBD_DISABLE_KEYMAP_LOAD
829: keyp = (keyarg_t *)arg;
830: if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
831: /sizeof(kbd->kb_keymap->key[0])) {
832: splx(s);
833: return EINVAL;
834: }
835: bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
836: sizeof(keyp->key));
837: break;
838: #else
839: splx(s);
840: return ENODEV;
841: #endif
842:
843: case GIO_DEADKEYMAP: /* get accent key translation table */
844: bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
845: break;
846: case PIO_DEADKEYMAP: /* set accent key translation table */
847: #ifndef KBD_DISABLE_KEYMAP_LOAD
848: bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
849: break;
850: #else
851: splx(s);
852: return ENODEV;
853: #endif
854:
855: case GETFKEY: /* get functionkey string */
856: fkeyp = (fkeyarg_t *)arg;
857: if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
858: splx(s);
859: return EINVAL;
860: }
861: bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
862: kbd->kb_fkeytab[fkeyp->keynum].len);
863: fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
864: break;
865: case SETFKEY: /* set functionkey string */
866: #ifndef KBD_DISABLE_KEYMAP_LOAD
867: fkeyp = (fkeyarg_t *)arg;
868: if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
869: splx(s);
870: return EINVAL;
871: }
872: kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
873: bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
874: kbd->kb_fkeytab[fkeyp->keynum].len);
875: break;
876: #else
877: splx(s);
878: return ENODEV;
879: #endif
880:
881: default:
882: splx(s);
883: return ENOIOCTL;
884: }
885:
886: splx(s);
887: return 0;
888: }
889:
890: /* get a pointer to the string associated with the given function key */
891: u_char
892: *genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
893: {
894: if (kbd == NULL)
895: return NULL;
896: fkey -= F_FN;
897: if (fkey > kbd->kb_fkeytab_size)
898: return NULL;
899: *len = kbd->kb_fkeytab[fkey].len;
900: return kbd->kb_fkeytab[fkey].str;
901: }
902:
903: /* diagnostic dump */
904: static char
905: *get_kbd_type_name(int type)
906: {
907: static struct {
908: int type;
909: char *name;
910: } name_table[] = {
911: { KB_84, "AT 84" },
912: { KB_101, "AT 101/102" },
913: { KB_OTHER, "generic" },
914: };
915: int i;
916:
917: for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
918: if (type == name_table[i].type)
919: return name_table[i].name;
920: }
921: return "unknown";
922: }
923:
924: void
925: genkbd_diag(keyboard_t *kbd, int level)
926: {
927: if (level > 0) {
928: printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
929: kbd->kb_index, kbd->kb_name, kbd->kb_unit,
930: get_kbd_type_name(kbd->kb_type), kbd->kb_type,
931: kbd->kb_config, kbd->kb_flags);
932: if (kbd->kb_io_base > 0)
933: printf(", port:0x%x-0x%x", kbd->kb_io_base,
934: kbd->kb_io_base + kbd->kb_io_size - 1);
935: printf("\n");
936: }
937: }
938:
939: #define set_lockkey_state(k, s, l) \
940: if (!((s) & l ## DOWN)) { \
941: int i; \
942: (s) |= l ## DOWN; \
943: (s) ^= l ## ED; \
944: i = (s) & LOCK_MASK; \
945: (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
946: }
947:
948: static u_int
949: save_accent_key(keyboard_t *kbd, u_int key, int *accents)
950: {
951: int i;
952:
953: /* make an index into the accent map */
954: i = key - F_ACC + 1;
955: if ((i > kbd->kb_accentmap->n_accs)
956: || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
957: /* the index is out of range or pointing to an empty entry */
958: *accents = 0;
959: return ERRKEY;
960: }
961:
962: /*
963: * If the same accent key has been hit twice, produce the accent char
964: * itself.
965: */
966: if (i == *accents) {
967: key = kbd->kb_accentmap->acc[i - 1].accchar;
968: *accents = 0;
969: return key;
970: }
971:
972: /* remember the index and wait for the next key */
973: *accents = i;
974: return NOKEY;
975: }
976:
977: static u_int
978: make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
979: {
980: struct acc_t *acc;
981: int i;
982:
983: acc = &kbd->kb_accentmap->acc[*accents - 1];
984: *accents = 0;
985:
986: /*
987: * If the accent key is followed by the space key,
988: * produce the accent char itself.
989: */
990: if (ch == ' ')
991: return acc->accchar;
992:
993: /* scan the accent map */
994: for (i = 0; i < NUM_ACCENTCHARS; ++i) {
995: if (acc->map[i][0] == 0) /* end of table */
996: break;
997: if (acc->map[i][0] == ch)
998: return acc->map[i][1];
999: }
1000: /* this char cannot be accented... */
1001: return ERRKEY;
1002: }
1003:
1004: int
1005: genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1006: int *accents)
1007: {
1008: struct keyent_t *key;
1009: int state = *shiftstate;
1010: int action;
1011: int f;
1012: int i;
1013:
1014: i = keycode;
1015: f = state & (AGRS | ALKED);
1016: if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1017: i += ALTGR_OFFSET;
1018: key = &kbd->kb_keymap->key[i];
1019: i = ((state & SHIFTS) ? 1 : 0)
1020: | ((state & CTLS) ? 2 : 0)
1021: | ((state & ALTS) ? 4 : 0);
1022: if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1023: || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1024: i ^= 1;
1025:
1026: if (up) { /* break: key released */
1027: action = kbd->kb_lastact[keycode];
1028: kbd->kb_lastact[keycode] = NOP;
1029: switch (action) {
1030: case LSHA:
1031: if (state & SHIFTAON) {
1032: set_lockkey_state(kbd, state, ALK);
1033: state &= ~ALKDOWN;
1034: }
1035: action = LSH;
1036: /* FALL THROUGH */
1037: case LSH:
1038: state &= ~SHIFTS1;
1039: break;
1040: case RSHA:
1041: if (state & SHIFTAON) {
1042: set_lockkey_state(kbd, state, ALK);
1043: state &= ~ALKDOWN;
1044: }
1045: action = RSH;
1046: /* FALL THROUGH */
1047: case RSH:
1048: state &= ~SHIFTS2;
1049: break;
1050: case LCTRA:
1051: if (state & SHIFTAON) {
1052: set_lockkey_state(kbd, state, ALK);
1053: state &= ~ALKDOWN;
1054: }
1055: action = LCTR;
1056: /* FALL THROUGH */
1057: case LCTR:
1058: state &= ~CTLS1;
1059: break;
1060: case RCTRA:
1061: if (state & SHIFTAON) {
1062: set_lockkey_state(kbd, state, ALK);
1063: state &= ~ALKDOWN;
1064: }
1065: action = RCTR;
1066: /* FALL THROUGH */
1067: case RCTR:
1068: state &= ~CTLS2;
1069: break;
1070: case LALTA:
1071: if (state & SHIFTAON) {
1072: set_lockkey_state(kbd, state, ALK);
1073: state &= ~ALKDOWN;
1074: }
1075: action = LALT;
1076: /* FALL THROUGH */
1077: case LALT:
1078: state &= ~ALTS1;
1079: break;
1080: case RALTA:
1081: if (state & SHIFTAON) {
1082: set_lockkey_state(kbd, state, ALK);
1083: state &= ~ALKDOWN;
1084: }
1085: action = RALT;
1086: /* FALL THROUGH */
1087: case RALT:
1088: state &= ~ALTS2;
1089: break;
1090: case ASH:
1091: state &= ~AGRS1;
1092: break;
1093: case META:
1094: state &= ~METAS1;
1095: break;
1096: case NLK:
1097: state &= ~NLKDOWN;
1098: break;
1099: case CLK:
1100: #ifndef PC98
1101: state &= ~CLKDOWN;
1102: #else
1103: state &= ~CLKED;
1104: i = state & LOCK_MASK;
1105: (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1106: (caddr_t)&i);
1107: #endif
1108: break;
1109: case SLK:
1110: state &= ~SLKDOWN;
1111: break;
1112: case ALK:
1113: state &= ~ALKDOWN;
1114: break;
1115: case NOP:
1116: /* release events of regular keys are not reported */
1117: *shiftstate &= ~SHIFTAON;
1118: return NOKEY;
1119: }
1120: *shiftstate = state & ~SHIFTAON;
1121: return (SPCLKEY | RELKEY | action);
1122: } else { /* make: key pressed */
1123: action = key->map[i];
1124: state &= ~SHIFTAON;
1125: if (key->spcl & (0x80 >> i)) {
1126: /* special keys */
1127: if (kbd->kb_lastact[keycode] == NOP)
1128: kbd->kb_lastact[keycode] = action;
1129: if (kbd->kb_lastact[keycode] != action)
1130: action = NOP;
1131: switch (action) {
1132: /* LOCKING KEYS */
1133: case NLK:
1134: set_lockkey_state(kbd, state, NLK);
1135: break;
1136: case CLK:
1137: #ifndef PC98
1138: set_lockkey_state(kbd, state, CLK);
1139: #else
1140: state |= CLKED;
1141: i = state & LOCK_MASK;
1142: (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1143: (caddr_t)&i);
1144: #endif
1145: break;
1146: case SLK:
1147: set_lockkey_state(kbd, state, SLK);
1148: break;
1149: case ALK:
1150: set_lockkey_state(kbd, state, ALK);
1151: break;
1152: /* NON-LOCKING KEYS */
1153: case SPSC: case RBT: case SUSP: case STBY:
1154: case DBG: case NEXT: case PREV: case PNC:
1155: case HALT: case PDWN:
1156: *accents = 0;
1157: break;
1158: case BTAB:
1159: *accents = 0;
1160: action |= BKEY;
1161: break;
1162: case LSHA:
1163: state |= SHIFTAON;
1164: action = LSH;
1165: /* FALL THROUGH */
1166: case LSH:
1167: state |= SHIFTS1;
1168: break;
1169: case RSHA:
1170: state |= SHIFTAON;
1171: action = RSH;
1172: /* FALL THROUGH */
1173: case RSH:
1174: state |= SHIFTS2;
1175: break;
1176: case LCTRA:
1177: state |= SHIFTAON;
1178: action = LCTR;
1179: /* FALL THROUGH */
1180: case LCTR:
1181: state |= CTLS1;
1182: break;
1183: case RCTRA:
1184: state |= SHIFTAON;
1185: action = RCTR;
1186: /* FALL THROUGH */
1187: case RCTR:
1188: state |= CTLS2;
1189: break;
1190: case LALTA:
1191: state |= SHIFTAON;
1192: action = LALT;
1193: /* FALL THROUGH */
1194: case LALT:
1195: state |= ALTS1;
1196: break;
1197: case RALTA:
1198: state |= SHIFTAON;
1199: action = RALT;
1200: /* FALL THROUGH */
1201: case RALT:
1202: state |= ALTS2;
1203: break;
1204: case ASH:
1205: state |= AGRS1;
1206: break;
1207: case META:
1208: state |= METAS1;
1209: break;
1210: case NOP:
1211: *shiftstate = state;
1212: return NOKEY;
1213: default:
1214: /* is this an accent (dead) key? */
1215: *shiftstate = state;
1216: if (action >= F_ACC && action <= L_ACC) {
1217: action = save_accent_key(kbd, action,
1218: accents);
1219: switch (action) {
1220: case NOKEY:
1221: case ERRKEY:
1222: return action;
1223: default:
1224: if (state & METAS)
1225: return (action | MKEY);
1226: else
1227: return action;
1228: }
1229: /* NOT REACHED */
1230: }
1231: /* other special keys */
1232: if (*accents > 0) {
1233: *accents = 0;
1234: return ERRKEY;
1235: }
1236: if (action >= F_FN && action <= L_FN)
1237: action |= FKEY;
1238: /* XXX: return fkey string for the FKEY? */
1239: return (SPCLKEY | action);
1240: }
1241: *shiftstate = state;
1242: return (SPCLKEY | action);
1243: } else {
1244: /* regular keys */
1245: kbd->kb_lastact[keycode] = NOP;
1246: *shiftstate = state;
1247: if (*accents > 0) {
1248: /* make an accented char */
1249: action = make_accent_char(kbd, action, accents);
1250: if (action == ERRKEY)
1251: return action;
1252: }
1253: if (state & METAS)
1254: action |= MKEY;
1255: return action;
1256: }
1257: }
1258: /* NOT REACHED */
1259: }