1617b9080SKazutaka YOKOTA /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4617b9080SKazutaka YOKOTA * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5617b9080SKazutaka YOKOTA * All rights reserved.
6617b9080SKazutaka YOKOTA *
7617b9080SKazutaka YOKOTA * Redistribution and use in source and binary forms, with or without
8617b9080SKazutaka YOKOTA * modification, are permitted provided that the following conditions
9617b9080SKazutaka YOKOTA * are met:
10617b9080SKazutaka YOKOTA * 1. Redistributions of source code must retain the above copyright
11617b9080SKazutaka YOKOTA * notice, this list of conditions and the following disclaimer as
12617b9080SKazutaka YOKOTA * the first lines of this file unmodified.
13617b9080SKazutaka YOKOTA * 2. Redistributions in binary form must reproduce the above copyright
14617b9080SKazutaka YOKOTA * notice, this list of conditions and the following disclaimer in the
15617b9080SKazutaka YOKOTA * documentation and/or other materials provided with the distribution.
16617b9080SKazutaka YOKOTA *
17617b9080SKazutaka YOKOTA * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18617b9080SKazutaka YOKOTA * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19617b9080SKazutaka YOKOTA * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20617b9080SKazutaka YOKOTA * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21617b9080SKazutaka YOKOTA * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22617b9080SKazutaka YOKOTA * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23617b9080SKazutaka YOKOTA * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24617b9080SKazutaka YOKOTA * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25617b9080SKazutaka YOKOTA * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26617b9080SKazutaka YOKOTA * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27617b9080SKazutaka YOKOTA *
28617b9080SKazutaka YOKOTA */
29617b9080SKazutaka YOKOTA
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31617b9080SKazutaka YOKOTA #include "opt_kbd.h"
32e9deda23SKazutaka YOKOTA #include "opt_atkbd.h"
3338ae0c59SOleksandr Tymoshenko #include "opt_evdev.h"
34617b9080SKazutaka YOKOTA
35617b9080SKazutaka YOKOTA #include <sys/param.h>
36617b9080SKazutaka YOKOTA #include <sys/systm.h>
37617b9080SKazutaka YOKOTA #include <sys/kernel.h>
38d3c87b85SKazutaka YOKOTA #include <sys/bus.h>
39520b6353SMarius Strobl #include <sys/eventhandler.h>
40617b9080SKazutaka YOKOTA #include <sys/proc.h>
41104a9b7eSAlexander Kabaev #include <sys/limits.h>
42617b9080SKazutaka YOKOTA #include <sys/malloc.h>
439e007a88SAlexander Motin #include <sys/sysctl.h>
44617b9080SKazutaka YOKOTA
45db3e34cbSKazutaka YOKOTA #include <machine/bus.h>
46db3e34cbSKazutaka YOKOTA #include <machine/resource.h>
47db3e34cbSKazutaka YOKOTA
4876495257SKazutaka YOKOTA #include <sys/kbio.h>
49617b9080SKazutaka YOKOTA #include <dev/kbd/kbdreg.h>
50520b6353SMarius Strobl #include <dev/atkbdc/atkbdreg.h>
51520b6353SMarius Strobl #include <dev/atkbdc/atkbdcreg.h>
52f359876fSKazutaka YOKOTA
5338ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
5438ae0c59SOleksandr Tymoshenko #include <dev/evdev/evdev.h>
5538ae0c59SOleksandr Tymoshenko #include <dev/evdev/input.h>
5638ae0c59SOleksandr Tymoshenko #endif
5738ae0c59SOleksandr Tymoshenko
58fadf3fb9SJohn Baldwin typedef struct atkbd_state {
59fadf3fb9SJohn Baldwin KBDC kbdc; /* keyboard controller */
60fadf3fb9SJohn Baldwin int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
61fadf3fb9SJohn Baldwin int ks_flags; /* flags */
62fadf3fb9SJohn Baldwin #define COMPOSE (1 << 0)
63fadf3fb9SJohn Baldwin int ks_polling;
64fadf3fb9SJohn Baldwin int ks_state; /* shift/lock key state */
65fadf3fb9SJohn Baldwin int ks_accents; /* accent key index (> 0) */
66fadf3fb9SJohn Baldwin u_int ks_composed_char; /* composed char code (> 0) */
67fadf3fb9SJohn Baldwin u_char ks_prefix; /* AT scan code prefix */
68fadf3fb9SJohn Baldwin struct callout ks_timer;
6938ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
7038ae0c59SOleksandr Tymoshenko struct evdev_dev *ks_evdev;
7138ae0c59SOleksandr Tymoshenko int ks_evdev_state;
7238ae0c59SOleksandr Tymoshenko #endif
73fadf3fb9SJohn Baldwin } atkbd_state_t;
74fadf3fb9SJohn Baldwin
759e007a88SAlexander Motin static SYSCTL_NODE(_hw, OID_AUTO, atkbd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
769e007a88SAlexander Motin "AT keyboard");
779e007a88SAlexander Motin
78ce881170SAlexander Motin static int atkbdhz = 0;
799e007a88SAlexander Motin SYSCTL_INT(_hw_atkbd, OID_AUTO, hz, CTLFLAG_RWTUN, &atkbdhz, 0,
809e007a88SAlexander Motin "Polling frequency (in hz)");
819e007a88SAlexander Motin
82fadf3fb9SJohn Baldwin static void atkbd_timeout(void *arg);
836c176113SMichael Gmelin static int atkbd_reset(KBDC kbdc, int flags, int c);
846c176113SMichael Gmelin
856c176113SMichael Gmelin #define HAS_QUIRK(p, q) (((atkbdc_softc_t *)(p))->quirks & q)
866c176113SMichael Gmelin #define ALLOW_DISABLE_KBD(kbdc) !HAS_QUIRK(kbdc, KBDC_QUIRK_KEEP_ACTIVATED)
87617b9080SKazutaka YOKOTA
88b19f7eecSRoger Pau Monné #define DEFAULT_DELAY 0x1 /* 500ms */
89b19f7eecSRoger Pau Monné #define DEFAULT_RATE 0x10 /* 14Hz */
90b19f7eecSRoger Pau Monné
9138ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
9238ae0c59SOleksandr Tymoshenko #define PS2_KEYBOARD_VENDOR 1
9338ae0c59SOleksandr Tymoshenko #define PS2_KEYBOARD_PRODUCT 1
9438ae0c59SOleksandr Tymoshenko #endif
9538ae0c59SOleksandr Tymoshenko
96617b9080SKazutaka YOKOTA int
atkbd_probe_unit(device_t dev,int irq,int flags)970897438aSWarner Losh atkbd_probe_unit(device_t dev, int irq, int flags)
98617b9080SKazutaka YOKOTA {
99617b9080SKazutaka YOKOTA keyboard_switch_t *sw;
100617b9080SKazutaka YOKOTA int args[2];
101e9deda23SKazutaka YOKOTA int error;
102617b9080SKazutaka YOKOTA
103617b9080SKazutaka YOKOTA sw = kbd_get_switch(ATKBD_DRIVER_NAME);
104617b9080SKazutaka YOKOTA if (sw == NULL)
105617b9080SKazutaka YOKOTA return ENXIO;
106617b9080SKazutaka YOKOTA
1070897438aSWarner Losh args[0] = device_get_unit(device_get_parent(dev));
108617b9080SKazutaka YOKOTA args[1] = irq;
1090897438aSWarner Losh error = (*sw->probe)(device_get_unit(dev), args, flags);
110e9deda23SKazutaka YOKOTA if (error)
111e9deda23SKazutaka YOKOTA return error;
112e9deda23SKazutaka YOKOTA return 0;
113617b9080SKazutaka YOKOTA }
114617b9080SKazutaka YOKOTA
115617b9080SKazutaka YOKOTA int
atkbd_attach_unit(device_t dev,keyboard_t ** kbd,int irq,int flags)1160897438aSWarner Losh atkbd_attach_unit(device_t dev, keyboard_t **kbd, int irq, int flags)
117617b9080SKazutaka YOKOTA {
118617b9080SKazutaka YOKOTA keyboard_switch_t *sw;
119fadf3fb9SJohn Baldwin atkbd_state_t *state;
120e9deda23SKazutaka YOKOTA int args[2];
121617b9080SKazutaka YOKOTA int error;
1220897438aSWarner Losh int unit;
123617b9080SKazutaka YOKOTA
124617b9080SKazutaka YOKOTA sw = kbd_get_switch(ATKBD_DRIVER_NAME);
125617b9080SKazutaka YOKOTA if (sw == NULL)
126617b9080SKazutaka YOKOTA return ENXIO;
127617b9080SKazutaka YOKOTA
128617b9080SKazutaka YOKOTA /* reset, initialize and enable the device */
1290897438aSWarner Losh unit = device_get_unit(dev);
1300897438aSWarner Losh args[0] = device_get_unit(device_get_parent(dev));
131e9deda23SKazutaka YOKOTA args[1] = irq;
132800da3b2SKazutaka YOKOTA *kbd = NULL;
133e9deda23SKazutaka YOKOTA error = (*sw->probe)(unit, args, flags);
134617b9080SKazutaka YOKOTA if (error)
135e9deda23SKazutaka YOKOTA return error;
136800da3b2SKazutaka YOKOTA error = (*sw->init)(unit, kbd, args, flags);
137e9deda23SKazutaka YOKOTA if (error)
138e9deda23SKazutaka YOKOTA return error;
139800da3b2SKazutaka YOKOTA (*sw->enable)(*kbd);
140617b9080SKazutaka YOKOTA
141617b9080SKazutaka YOKOTA #ifdef KBD_INSTALL_CDEV
142617b9080SKazutaka YOKOTA /* attach a virtual keyboard cdev */
143800da3b2SKazutaka YOKOTA error = kbd_attach(*kbd);
144617b9080SKazutaka YOKOTA if (error)
145617b9080SKazutaka YOKOTA return error;
146617b9080SKazutaka YOKOTA #endif
147617b9080SKazutaka YOKOTA
148617b9080SKazutaka YOKOTA /*
149617b9080SKazutaka YOKOTA * This is a kludge to compensate for lost keyboard interrupts.
150617b9080SKazutaka YOKOTA * A similar code used to be in syscons. See below. XXX
151617b9080SKazutaka YOKOTA */
152fadf3fb9SJohn Baldwin state = (atkbd_state_t *)(*kbd)->kb_data;
153fadf3fb9SJohn Baldwin callout_init(&state->ks_timer, 0);
154800da3b2SKazutaka YOKOTA atkbd_timeout(*kbd);
155617b9080SKazutaka YOKOTA
156617b9080SKazutaka YOKOTA if (bootverbose)
157800da3b2SKazutaka YOKOTA (*sw->diag)(*kbd, bootverbose);
158520b6353SMarius Strobl
159617b9080SKazutaka YOKOTA return 0;
160617b9080SKazutaka YOKOTA }
161617b9080SKazutaka YOKOTA
162617b9080SKazutaka YOKOTA static void
atkbd_timeout(void * arg)163617b9080SKazutaka YOKOTA atkbd_timeout(void *arg)
164617b9080SKazutaka YOKOTA {
165fadf3fb9SJohn Baldwin atkbd_state_t *state;
166617b9080SKazutaka YOKOTA keyboard_t *kbd;
167617b9080SKazutaka YOKOTA int s;
168617b9080SKazutaka YOKOTA
169617b9080SKazutaka YOKOTA /*
1707c6f57a3SKazutaka YOKOTA * The original text of the following comments are extracted
1717c6f57a3SKazutaka YOKOTA * from syscons.c (1.287)
1727c6f57a3SKazutaka YOKOTA *
173617b9080SKazutaka YOKOTA * With release 2.1 of the Xaccel server, the keyboard is left
174617b9080SKazutaka YOKOTA * hanging pretty often. Apparently an interrupt from the
175617b9080SKazutaka YOKOTA * keyboard is lost, and I don't know why (yet).
1767c6f57a3SKazutaka YOKOTA * This ugly hack calls the low-level interrupt routine if input
1777c6f57a3SKazutaka YOKOTA * is ready for the keyboard and conveniently hides the problem. XXX
1787c6f57a3SKazutaka YOKOTA *
1797c6f57a3SKazutaka YOKOTA * Try removing anything stuck in the keyboard controller; whether
1807c6f57a3SKazutaka YOKOTA * it's a keyboard scan code or mouse data. The low-level
1817c6f57a3SKazutaka YOKOTA * interrupt routine doesn't read the mouse data directly,
1827c6f57a3SKazutaka YOKOTA * but the keyboard controller driver will, as a side effect.
183617b9080SKazutaka YOKOTA */
184617b9080SKazutaka YOKOTA /*
1857c6f57a3SKazutaka YOKOTA * And here is bde's original comment about this:
1867c6f57a3SKazutaka YOKOTA *
1877c6f57a3SKazutaka YOKOTA * This is necessary to handle edge triggered interrupts - if we
1887c6f57a3SKazutaka YOKOTA * returned when our IRQ is high due to unserviced input, then there
1897c6f57a3SKazutaka YOKOTA * would be no more keyboard IRQs until the keyboard is reset by
1907c6f57a3SKazutaka YOKOTA * external powers.
1917c6f57a3SKazutaka YOKOTA *
1927c6f57a3SKazutaka YOKOTA * The keyboard apparently unwedges the irq in most cases.
193617b9080SKazutaka YOKOTA */
194617b9080SKazutaka YOKOTA s = spltty();
195617b9080SKazutaka YOKOTA kbd = (keyboard_t *)arg;
196259699b2SWojciech A. Koszek if (kbdd_lock(kbd, TRUE)) {
197617b9080SKazutaka YOKOTA /*
198617b9080SKazutaka YOKOTA * We have seen the lock flag is not set. Let's reset
199617b9080SKazutaka YOKOTA * the flag early, otherwise the LED update routine fails
200617b9080SKazutaka YOKOTA * which may want the lock during the interrupt routine.
201617b9080SKazutaka YOKOTA */
202259699b2SWojciech A. Koszek kbdd_lock(kbd, FALSE);
203259699b2SWojciech A. Koszek if (kbdd_check_char(kbd))
204259699b2SWojciech A. Koszek kbdd_intr(kbd, NULL);
205617b9080SKazutaka YOKOTA }
206617b9080SKazutaka YOKOTA splx(s);
2079e007a88SAlexander Motin if (atkbdhz > 0) {
208fadf3fb9SJohn Baldwin state = (atkbd_state_t *)kbd->kb_data;
2099e007a88SAlexander Motin callout_reset_sbt(&state->ks_timer, SBT_1S / atkbdhz, 0,
2109e007a88SAlexander Motin atkbd_timeout, arg, C_PREL(1));
2119e007a88SAlexander Motin }
212617b9080SKazutaka YOKOTA }
213617b9080SKazutaka YOKOTA
214617b9080SKazutaka YOKOTA /* LOW-LEVEL */
215617b9080SKazutaka YOKOTA
216617b9080SKazutaka YOKOTA #define ATKBD_DEFAULT 0
217617b9080SKazutaka YOKOTA
218617b9080SKazutaka YOKOTA /* keyboard driver declaration */
219617b9080SKazutaka YOKOTA static int atkbd_configure(int flags);
220617b9080SKazutaka YOKOTA static kbd_probe_t atkbd_probe;
221617b9080SKazutaka YOKOTA static kbd_init_t atkbd_init;
222617b9080SKazutaka YOKOTA static kbd_term_t atkbd_term;
223617b9080SKazutaka YOKOTA static kbd_intr_t atkbd_intr;
224617b9080SKazutaka YOKOTA static kbd_test_if_t atkbd_test_if;
225617b9080SKazutaka YOKOTA static kbd_enable_t atkbd_enable;
226617b9080SKazutaka YOKOTA static kbd_disable_t atkbd_disable;
227617b9080SKazutaka YOKOTA static kbd_read_t atkbd_read;
228617b9080SKazutaka YOKOTA static kbd_check_t atkbd_check;
229617b9080SKazutaka YOKOTA static kbd_read_char_t atkbd_read_char;
230617b9080SKazutaka YOKOTA static kbd_check_char_t atkbd_check_char;
231617b9080SKazutaka YOKOTA static kbd_ioctl_t atkbd_ioctl;
232617b9080SKazutaka YOKOTA static kbd_lock_t atkbd_lock;
233617b9080SKazutaka YOKOTA static kbd_clear_state_t atkbd_clear_state;
234617b9080SKazutaka YOKOTA static kbd_get_state_t atkbd_get_state;
235617b9080SKazutaka YOKOTA static kbd_set_state_t atkbd_set_state;
236e9deda23SKazutaka YOKOTA static kbd_poll_mode_t atkbd_poll;
237617b9080SKazutaka YOKOTA
23851da11a2SMark Murray static keyboard_switch_t atkbdsw = {
2391c5c067aSKyle Evans .probe = atkbd_probe,
2401c5c067aSKyle Evans .init = atkbd_init,
2411c5c067aSKyle Evans .term = atkbd_term,
2421c5c067aSKyle Evans .intr = atkbd_intr,
2431c5c067aSKyle Evans .test_if = atkbd_test_if,
2441c5c067aSKyle Evans .enable = atkbd_enable,
2451c5c067aSKyle Evans .disable = atkbd_disable,
2461c5c067aSKyle Evans .read = atkbd_read,
2471c5c067aSKyle Evans .check = atkbd_check,
2481c5c067aSKyle Evans .read_char = atkbd_read_char,
2491c5c067aSKyle Evans .check_char = atkbd_check_char,
2501c5c067aSKyle Evans .ioctl = atkbd_ioctl,
2511c5c067aSKyle Evans .lock = atkbd_lock,
2521c5c067aSKyle Evans .clear_state = atkbd_clear_state,
2531c5c067aSKyle Evans .get_state = atkbd_get_state,
2541c5c067aSKyle Evans .set_state = atkbd_set_state,
2551c5c067aSKyle Evans .poll = atkbd_poll,
256617b9080SKazutaka YOKOTA };
257617b9080SKazutaka YOKOTA
258617b9080SKazutaka YOKOTA KEYBOARD_DRIVER(atkbd, atkbdsw, atkbd_configure);
259617b9080SKazutaka YOKOTA
260617b9080SKazutaka YOKOTA /* local functions */
261b19f7eecSRoger Pau Monné static int set_typematic(keyboard_t *kbd);
262617b9080SKazutaka YOKOTA static int setup_kbd_port(KBDC kbdc, int port, int intr);
263617b9080SKazutaka YOKOTA static int get_kbd_echo(KBDC kbdc);
264617b9080SKazutaka YOKOTA static int probe_keyboard(KBDC kbdc, int flags);
265617b9080SKazutaka YOKOTA static int init_keyboard(KBDC kbdc, int *type, int flags);
266617b9080SKazutaka YOKOTA static int write_kbd(KBDC kbdc, int command, int data);
267617b9080SKazutaka YOKOTA static int get_kbd_id(KBDC kbdc);
268e9deda23SKazutaka YOKOTA static int typematic(int delay, int rate);
26957995893SKazutaka YOKOTA static int typematic_delay(int delay);
27057995893SKazutaka YOKOTA static int typematic_rate(int rate);
271617b9080SKazutaka YOKOTA
27238ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
27348f2b006SVladimir Kondratyev static evdev_event_t atkbd_ev_event;
27448f2b006SVladimir Kondratyev
27538ae0c59SOleksandr Tymoshenko static const struct evdev_methods atkbd_evdev_methods = {
27648f2b006SVladimir Kondratyev .ev_event = atkbd_ev_event,
27738ae0c59SOleksandr Tymoshenko };
27838ae0c59SOleksandr Tymoshenko #endif
27938ae0c59SOleksandr Tymoshenko
280617b9080SKazutaka YOKOTA /* local variables */
281617b9080SKazutaka YOKOTA
282617b9080SKazutaka YOKOTA /* the initial key map, accent map and fkey strings */
283e9deda23SKazutaka YOKOTA #ifdef ATKBD_DFLT_KEYMAP
284e9deda23SKazutaka YOKOTA #define KBD_DFLT_KEYMAP
285e9deda23SKazutaka YOKOTA #include "atkbdmap.h"
286e9deda23SKazutaka YOKOTA #endif
287f359876fSKazutaka YOKOTA #include <dev/kbd/kbdtables.h>
288617b9080SKazutaka YOKOTA
289617b9080SKazutaka YOKOTA /* structures for the default keyboard */
290617b9080SKazutaka YOKOTA static keyboard_t default_kbd;
291617b9080SKazutaka YOKOTA static atkbd_state_t default_kbd_state;
292617b9080SKazutaka YOKOTA static keymap_t default_keymap;
293617b9080SKazutaka YOKOTA static accentmap_t default_accentmap;
294617b9080SKazutaka YOKOTA static fkeytab_t default_fkeytab[NUM_FKEYS];
295617b9080SKazutaka YOKOTA
296617b9080SKazutaka YOKOTA /*
297617b9080SKazutaka YOKOTA * The back door to the keyboard driver!
298617b9080SKazutaka YOKOTA * This function is called by the console driver, via the kbdio module,
299617b9080SKazutaka YOKOTA * to tickle keyboard drivers when the low-level console is being initialized.
300617b9080SKazutaka YOKOTA * Almost nothing in the kernel has been initialied yet. Try to probe
301617b9080SKazutaka YOKOTA * keyboards if possible.
3022bb113d0SJeroen Ruigrok van der Werven * NOTE: because of the way the low-level console is initialized, this routine
303617b9080SKazutaka YOKOTA * may be called more than once!!
304617b9080SKazutaka YOKOTA */
305617b9080SKazutaka YOKOTA static int
atkbd_configure(int flags)306617b9080SKazutaka YOKOTA atkbd_configure(int flags)
307617b9080SKazutaka YOKOTA {
308617b9080SKazutaka YOKOTA keyboard_t *kbd;
309617b9080SKazutaka YOKOTA int arg[2];
310e9deda23SKazutaka YOKOTA int i;
311617b9080SKazutaka YOKOTA
3126621aa5cSMaxim Sobolev /*
3136621aa5cSMaxim Sobolev * Probe the keyboard controller, if not present or if the driver
3146621aa5cSMaxim Sobolev * is disabled, unregister the keyboard if any.
3156621aa5cSMaxim Sobolev */
3166621aa5cSMaxim Sobolev if (atkbdc_configure() != 0 ||
3176621aa5cSMaxim Sobolev resource_disabled("atkbd", ATKBD_DEFAULT)) {
318e9deda23SKazutaka YOKOTA i = kbd_find_keyboard(ATKBD_DRIVER_NAME, ATKBD_DEFAULT);
319e9deda23SKazutaka YOKOTA if (i >= 0) {
320e9deda23SKazutaka YOKOTA kbd = kbd_get_keyboard(i);
321e9deda23SKazutaka YOKOTA kbd_unregister(kbd);
322e9deda23SKazutaka YOKOTA kbd->kb_flags &= ~KB_REGISTERED;
323e9deda23SKazutaka YOKOTA }
32429e59d7dSKazutaka YOKOTA return 0;
325e9deda23SKazutaka YOKOTA }
32657600c23SKazutaka YOKOTA
32757600c23SKazutaka YOKOTA /* XXX: a kludge to obtain the device configuration flags */
32857600c23SKazutaka YOKOTA if (resource_int_value("atkbd", ATKBD_DEFAULT, "flags", &i) == 0)
32957600c23SKazutaka YOKOTA flags |= i;
330617b9080SKazutaka YOKOTA
331617b9080SKazutaka YOKOTA /* probe the default keyboard */
332617b9080SKazutaka YOKOTA arg[0] = -1;
333617b9080SKazutaka YOKOTA arg[1] = -1;
334e9deda23SKazutaka YOKOTA kbd = NULL;
335e9deda23SKazutaka YOKOTA if (atkbd_probe(ATKBD_DEFAULT, arg, flags))
336e9deda23SKazutaka YOKOTA return 0;
337e9deda23SKazutaka YOKOTA if (atkbd_init(ATKBD_DEFAULT, &kbd, arg, flags))
338617b9080SKazutaka YOKOTA return 0;
339617b9080SKazutaka YOKOTA
340e9deda23SKazutaka YOKOTA /* return the number of found keyboards */
341e9deda23SKazutaka YOKOTA return 1;
342617b9080SKazutaka YOKOTA }
343617b9080SKazutaka YOKOTA
344617b9080SKazutaka YOKOTA /* low-level functions */
345617b9080SKazutaka YOKOTA
346e9deda23SKazutaka YOKOTA /* detect a keyboard */
347617b9080SKazutaka YOKOTA static int
atkbd_probe(int unit,void * arg,int flags)348e9deda23SKazutaka YOKOTA atkbd_probe(int unit, void *arg, int flags)
349e9deda23SKazutaka YOKOTA {
350e9deda23SKazutaka YOKOTA KBDC kbdc;
351db3e34cbSKazutaka YOKOTA int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
352e9deda23SKazutaka YOKOTA
353e9deda23SKazutaka YOKOTA /* XXX */
354e9deda23SKazutaka YOKOTA if (unit == ATKBD_DEFAULT) {
355e9deda23SKazutaka YOKOTA if (KBD_IS_PROBED(&default_kbd))
356e9deda23SKazutaka YOKOTA return 0;
357e9deda23SKazutaka YOKOTA }
358e9deda23SKazutaka YOKOTA
359db3e34cbSKazutaka YOKOTA kbdc = atkbdc_open(data[0]);
360e9deda23SKazutaka YOKOTA if (kbdc == NULL)
361e9deda23SKazutaka YOKOTA return ENXIO;
362e9deda23SKazutaka YOKOTA if (probe_keyboard(kbdc, flags)) {
363e9deda23SKazutaka YOKOTA if (flags & KB_CONF_FAIL_IF_NO_KBD)
364e9deda23SKazutaka YOKOTA return ENXIO;
365e9deda23SKazutaka YOKOTA }
366e9deda23SKazutaka YOKOTA return 0;
367e9deda23SKazutaka YOKOTA }
368e9deda23SKazutaka YOKOTA
369e9deda23SKazutaka YOKOTA /* reset and initialize the device */
370e9deda23SKazutaka YOKOTA static int
atkbd_init(int unit,keyboard_t ** kbdp,void * arg,int flags)371e9deda23SKazutaka YOKOTA atkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
372617b9080SKazutaka YOKOTA {
373617b9080SKazutaka YOKOTA keyboard_t *kbd;
374617b9080SKazutaka YOKOTA atkbd_state_t *state;
375617b9080SKazutaka YOKOTA keymap_t *keymap;
376617b9080SKazutaka YOKOTA accentmap_t *accmap;
377617b9080SKazutaka YOKOTA fkeytab_t *fkeymap;
378617b9080SKazutaka YOKOTA int fkeymap_size;
37956f5e27cSKazutaka YOKOTA int delay[2];
380db3e34cbSKazutaka YOKOTA int *data = (int *)arg; /* data[0]: controller, data[1]: irq */
381ff690962SSam Leffler int error, needfree;
38238ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
38338ae0c59SOleksandr Tymoshenko struct evdev_dev *evdev;
38438ae0c59SOleksandr Tymoshenko char phys_loc[8];
38538ae0c59SOleksandr Tymoshenko #endif
386617b9080SKazutaka YOKOTA
387617b9080SKazutaka YOKOTA /* XXX */
388617b9080SKazutaka YOKOTA if (unit == ATKBD_DEFAULT) {
389617b9080SKazutaka YOKOTA *kbdp = kbd = &default_kbd;
390e9deda23SKazutaka YOKOTA if (KBD_IS_INITIALIZED(kbd) && KBD_IS_CONFIGURED(kbd))
391617b9080SKazutaka YOKOTA return 0;
392617b9080SKazutaka YOKOTA state = &default_kbd_state;
393617b9080SKazutaka YOKOTA keymap = &default_keymap;
394617b9080SKazutaka YOKOTA accmap = &default_accentmap;
395617b9080SKazutaka YOKOTA fkeymap = default_fkeytab;
39673a1170aSPedro F. Giffuni fkeymap_size = nitems(default_fkeytab);
397ff690962SSam Leffler needfree = 0;
398617b9080SKazutaka YOKOTA } else if (*kbdp == NULL) {
3997cc0979fSDavid Malone *kbdp = kbd = malloc(sizeof(*kbd), M_DEVBUF, M_NOWAIT | M_ZERO);
4007cc0979fSDavid Malone state = malloc(sizeof(*state), M_DEVBUF, M_NOWAIT | M_ZERO);
401ff690962SSam Leffler /* NB: these will always be initialized 'cuz !KBD_IS_PROBED */
402617b9080SKazutaka YOKOTA keymap = malloc(sizeof(key_map), M_DEVBUF, M_NOWAIT);
403617b9080SKazutaka YOKOTA accmap = malloc(sizeof(accent_map), M_DEVBUF, M_NOWAIT);
404617b9080SKazutaka YOKOTA fkeymap = malloc(sizeof(fkey_tab), M_DEVBUF, M_NOWAIT);
405617b9080SKazutaka YOKOTA fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]);
406ff690962SSam Leffler needfree = 1;
4077cc0979fSDavid Malone if ((kbd == NULL) || (state == NULL) || (keymap == NULL)
4087cc0979fSDavid Malone || (accmap == NULL) || (fkeymap == NULL)) {
409ff690962SSam Leffler error = ENOMEM;
410ff690962SSam Leffler goto bad;
411617b9080SKazutaka YOKOTA }
412e9deda23SKazutaka YOKOTA } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) {
413617b9080SKazutaka YOKOTA return 0;
414617b9080SKazutaka YOKOTA } else {
415617b9080SKazutaka YOKOTA kbd = *kbdp;
416617b9080SKazutaka YOKOTA state = (atkbd_state_t *)kbd->kb_data;
417617b9080SKazutaka YOKOTA bzero(state, sizeof(*state));
418617b9080SKazutaka YOKOTA keymap = kbd->kb_keymap;
419617b9080SKazutaka YOKOTA accmap = kbd->kb_accentmap;
420617b9080SKazutaka YOKOTA fkeymap = kbd->kb_fkeytab;
421617b9080SKazutaka YOKOTA fkeymap_size = kbd->kb_fkeytab_size;
422ff690962SSam Leffler needfree = 0;
423617b9080SKazutaka YOKOTA }
424617b9080SKazutaka YOKOTA
425e9deda23SKazutaka YOKOTA if (!KBD_IS_PROBED(kbd)) {
426db3e34cbSKazutaka YOKOTA state->kbdc = atkbdc_open(data[0]);
427ff690962SSam Leffler if (state->kbdc == NULL) {
428ff690962SSam Leffler error = ENXIO;
429ff690962SSam Leffler goto bad;
430ff690962SSam Leffler }
431e9deda23SKazutaka YOKOTA kbd_init_struct(kbd, ATKBD_DRIVER_NAME, KB_OTHER, unit, flags,
432db3e34cbSKazutaka YOKOTA 0, 0);
433617b9080SKazutaka YOKOTA bcopy(&key_map, keymap, sizeof(key_map));
434617b9080SKazutaka YOKOTA bcopy(&accent_map, accmap, sizeof(accent_map));
435617b9080SKazutaka YOKOTA bcopy(fkey_tab, fkeymap,
436617b9080SKazutaka YOKOTA imin(fkeymap_size * sizeof(fkeymap[0]), sizeof(fkey_tab)));
437617b9080SKazutaka YOKOTA kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size);
438617b9080SKazutaka YOKOTA kbd->kb_data = (void *)state;
439617b9080SKazutaka YOKOTA
440e9deda23SKazutaka YOKOTA if (probe_keyboard(state->kbdc, flags)) { /* shouldn't happen */
441ff690962SSam Leffler if (flags & KB_CONF_FAIL_IF_NO_KBD) {
442ff690962SSam Leffler error = ENXIO;
443ff690962SSam Leffler goto bad;
444ff690962SSam Leffler }
445617b9080SKazutaka YOKOTA } else {
446617b9080SKazutaka YOKOTA KBD_FOUND_DEVICE(kbd);
447617b9080SKazutaka YOKOTA }
448617b9080SKazutaka YOKOTA atkbd_clear_state(kbd);
449617b9080SKazutaka YOKOTA state->ks_mode = K_XLATE;
450617b9080SKazutaka YOKOTA /*
451617b9080SKazutaka YOKOTA * FIXME: set the initial value for lock keys in ks_state
452617b9080SKazutaka YOKOTA * according to the BIOS data?
453617b9080SKazutaka YOKOTA */
454617b9080SKazutaka YOKOTA KBD_PROBE_DONE(kbd);
455617b9080SKazutaka YOKOTA }
456e9deda23SKazutaka YOKOTA if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
4576c6028ffSKazutaka YOKOTA kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY;
458617b9080SKazutaka YOKOTA if (KBD_HAS_DEVICE(kbd)
459e9deda23SKazutaka YOKOTA && init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
4608ce4e518SHidetoshi Shimokawa && (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD)) {
4618ce4e518SHidetoshi Shimokawa kbd_unregister(kbd);
462ff690962SSam Leffler error = ENXIO;
463ff690962SSam Leffler goto bad;
4648ce4e518SHidetoshi Shimokawa }
465e9deda23SKazutaka YOKOTA atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
466b19f7eecSRoger Pau Monné set_typematic(kbd);
46756f5e27cSKazutaka YOKOTA delay[0] = kbd->kb_delay1;
46856f5e27cSKazutaka YOKOTA delay[1] = kbd->kb_delay2;
46956f5e27cSKazutaka YOKOTA atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
47038ae0c59SOleksandr Tymoshenko
47138ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
47238ae0c59SOleksandr Tymoshenko /* register as evdev provider on first init */
47338ae0c59SOleksandr Tymoshenko if (state->ks_evdev == NULL) {
47438ae0c59SOleksandr Tymoshenko snprintf(phys_loc, sizeof(phys_loc), "atkbd%d", unit);
47538ae0c59SOleksandr Tymoshenko evdev = evdev_alloc();
47638ae0c59SOleksandr Tymoshenko evdev_set_name(evdev, "AT keyboard");
47738ae0c59SOleksandr Tymoshenko evdev_set_phys(evdev, phys_loc);
47838ae0c59SOleksandr Tymoshenko evdev_set_id(evdev, BUS_I8042, PS2_KEYBOARD_VENDOR,
47938ae0c59SOleksandr Tymoshenko PS2_KEYBOARD_PRODUCT, 0);
48038ae0c59SOleksandr Tymoshenko evdev_set_methods(evdev, kbd, &atkbd_evdev_methods);
48138ae0c59SOleksandr Tymoshenko evdev_support_event(evdev, EV_SYN);
48238ae0c59SOleksandr Tymoshenko evdev_support_event(evdev, EV_KEY);
48338ae0c59SOleksandr Tymoshenko evdev_support_event(evdev, EV_LED);
48438ae0c59SOleksandr Tymoshenko evdev_support_event(evdev, EV_REP);
48538ae0c59SOleksandr Tymoshenko evdev_support_all_known_keys(evdev);
48638ae0c59SOleksandr Tymoshenko evdev_support_led(evdev, LED_NUML);
48738ae0c59SOleksandr Tymoshenko evdev_support_led(evdev, LED_CAPSL);
48838ae0c59SOleksandr Tymoshenko evdev_support_led(evdev, LED_SCROLLL);
48938ae0c59SOleksandr Tymoshenko
490f86e7267SVladimir Kondratyev if (evdev_register_mtx(evdev, &Giant))
49138ae0c59SOleksandr Tymoshenko evdev_free(evdev);
49238ae0c59SOleksandr Tymoshenko else
49338ae0c59SOleksandr Tymoshenko state->ks_evdev = evdev;
49438ae0c59SOleksandr Tymoshenko state->ks_evdev_state = 0;
49538ae0c59SOleksandr Tymoshenko }
49638ae0c59SOleksandr Tymoshenko #endif
49738ae0c59SOleksandr Tymoshenko
498617b9080SKazutaka YOKOTA KBD_INIT_DONE(kbd);
499617b9080SKazutaka YOKOTA }
500617b9080SKazutaka YOKOTA if (!KBD_IS_CONFIGURED(kbd)) {
501ff690962SSam Leffler if (kbd_register(kbd) < 0) {
502ff690962SSam Leffler error = ENXIO;
503ff690962SSam Leffler goto bad;
504ff690962SSam Leffler }
505617b9080SKazutaka YOKOTA KBD_CONFIG_DONE(kbd);
506617b9080SKazutaka YOKOTA }
507617b9080SKazutaka YOKOTA
508617b9080SKazutaka YOKOTA return 0;
509ff690962SSam Leffler bad:
510ff690962SSam Leffler if (needfree) {
511ff690962SSam Leffler if (state != NULL)
512ff690962SSam Leffler free(state, M_DEVBUF);
513ff690962SSam Leffler if (keymap != NULL)
514ff690962SSam Leffler free(keymap, M_DEVBUF);
515ff690962SSam Leffler if (accmap != NULL)
516ff690962SSam Leffler free(accmap, M_DEVBUF);
517ff690962SSam Leffler if (fkeymap != NULL)
518ff690962SSam Leffler free(fkeymap, M_DEVBUF);
519ff690962SSam Leffler if (kbd != NULL) {
520ff690962SSam Leffler free(kbd, M_DEVBUF);
521ff690962SSam Leffler *kbdp = NULL; /* insure ref doesn't leak to caller */
522ff690962SSam Leffler }
523ff690962SSam Leffler }
524ff690962SSam Leffler return error;
525617b9080SKazutaka YOKOTA }
526617b9080SKazutaka YOKOTA
527617b9080SKazutaka YOKOTA /* finish using this keyboard */
528617b9080SKazutaka YOKOTA static int
atkbd_term(keyboard_t * kbd)529617b9080SKazutaka YOKOTA atkbd_term(keyboard_t *kbd)
530617b9080SKazutaka YOKOTA {
531fadf3fb9SJohn Baldwin atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
532fadf3fb9SJohn Baldwin
533617b9080SKazutaka YOKOTA kbd_unregister(kbd);
534fadf3fb9SJohn Baldwin callout_drain(&state->ks_timer);
535617b9080SKazutaka YOKOTA return 0;
536617b9080SKazutaka YOKOTA }
537617b9080SKazutaka YOKOTA
538617b9080SKazutaka YOKOTA /* keyboard interrupt routine */
539617b9080SKazutaka YOKOTA static int
atkbd_intr(keyboard_t * kbd,void * arg)540e9deda23SKazutaka YOKOTA atkbd_intr(keyboard_t *kbd, void *arg)
541617b9080SKazutaka YOKOTA {
542018cd707SMaksim Yevmenkin atkbd_state_t *state = (atkbd_state_t *)kbd->kb_data;
54356f5e27cSKazutaka YOKOTA int delay[2];
544617b9080SKazutaka YOKOTA int c;
545617b9080SKazutaka YOKOTA
546ac3912b0SRuslan Ermilov if (!KBD_HAS_DEVICE(kbd)) {
547ac3912b0SRuslan Ermilov /*
548ac3912b0SRuslan Ermilov * The keyboard was not detected before;
549ac3912b0SRuslan Ermilov * it must have been reconnected!
550ac3912b0SRuslan Ermilov */
551f0143dacSWarner Losh init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config);
552ac3912b0SRuslan Ermilov KBD_FOUND_DEVICE(kbd);
553ac3912b0SRuslan Ermilov atkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
554b19f7eecSRoger Pau Monné set_typematic(kbd);
555ac3912b0SRuslan Ermilov delay[0] = kbd->kb_delay1;
556ac3912b0SRuslan Ermilov delay[1] = kbd->kb_delay2;
557ac3912b0SRuslan Ermilov atkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay);
558ac3912b0SRuslan Ermilov }
559ac3912b0SRuslan Ermilov
560018cd707SMaksim Yevmenkin if (state->ks_polling)
561018cd707SMaksim Yevmenkin return 0;
562018cd707SMaksim Yevmenkin
563617b9080SKazutaka YOKOTA if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) {
564617b9080SKazutaka YOKOTA /* let the callback function to process the input */
565617b9080SKazutaka YOKOTA (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT,
566617b9080SKazutaka YOKOTA kbd->kb_callback.kc_arg);
567617b9080SKazutaka YOKOTA } else {
568617b9080SKazutaka YOKOTA /* read and discard the input; no one is waiting for input */
569617b9080SKazutaka YOKOTA do {
570617b9080SKazutaka YOKOTA c = atkbd_read_char(kbd, FALSE);
571617b9080SKazutaka YOKOTA } while (c != NOKEY);
572617b9080SKazutaka YOKOTA }
573617b9080SKazutaka YOKOTA return 0;
574617b9080SKazutaka YOKOTA }
575617b9080SKazutaka YOKOTA
576617b9080SKazutaka YOKOTA /* test the interface to the device */
577617b9080SKazutaka YOKOTA static int
atkbd_test_if(keyboard_t * kbd)578617b9080SKazutaka YOKOTA atkbd_test_if(keyboard_t *kbd)
579617b9080SKazutaka YOKOTA {
580617b9080SKazutaka YOKOTA int error;
581617b9080SKazutaka YOKOTA int s;
582617b9080SKazutaka YOKOTA
583617b9080SKazutaka YOKOTA error = 0;
584617b9080SKazutaka YOKOTA empty_both_buffers(((atkbd_state_t *)kbd->kb_data)->kbdc, 10);
585617b9080SKazutaka YOKOTA s = spltty();
586617b9080SKazutaka YOKOTA if (!test_controller(((atkbd_state_t *)kbd->kb_data)->kbdc))
587617b9080SKazutaka YOKOTA error = EIO;
588617b9080SKazutaka YOKOTA else if (test_kbd_port(((atkbd_state_t *)kbd->kb_data)->kbdc) != 0)
589617b9080SKazutaka YOKOTA error = EIO;
590617b9080SKazutaka YOKOTA splx(s);
591617b9080SKazutaka YOKOTA
592617b9080SKazutaka YOKOTA return error;
593617b9080SKazutaka YOKOTA }
594617b9080SKazutaka YOKOTA
595617b9080SKazutaka YOKOTA /*
596617b9080SKazutaka YOKOTA * Enable the access to the device; until this function is called,
597617b9080SKazutaka YOKOTA * the client cannot read from the keyboard.
598617b9080SKazutaka YOKOTA */
599617b9080SKazutaka YOKOTA static int
atkbd_enable(keyboard_t * kbd)600617b9080SKazutaka YOKOTA atkbd_enable(keyboard_t *kbd)
601617b9080SKazutaka YOKOTA {
602617b9080SKazutaka YOKOTA int s;
603617b9080SKazutaka YOKOTA
604617b9080SKazutaka YOKOTA s = spltty();
605617b9080SKazutaka YOKOTA KBD_ACTIVATE(kbd);
606617b9080SKazutaka YOKOTA splx(s);
607617b9080SKazutaka YOKOTA return 0;
608617b9080SKazutaka YOKOTA }
609617b9080SKazutaka YOKOTA
610617b9080SKazutaka YOKOTA /* disallow the access to the device */
611617b9080SKazutaka YOKOTA static int
atkbd_disable(keyboard_t * kbd)612617b9080SKazutaka YOKOTA atkbd_disable(keyboard_t *kbd)
613617b9080SKazutaka YOKOTA {
614617b9080SKazutaka YOKOTA int s;
615617b9080SKazutaka YOKOTA
616617b9080SKazutaka YOKOTA s = spltty();
617617b9080SKazutaka YOKOTA KBD_DEACTIVATE(kbd);
618617b9080SKazutaka YOKOTA splx(s);
619617b9080SKazutaka YOKOTA return 0;
620617b9080SKazutaka YOKOTA }
621617b9080SKazutaka YOKOTA
622617b9080SKazutaka YOKOTA /* read one byte from the keyboard if it's allowed */
623617b9080SKazutaka YOKOTA static int
atkbd_read(keyboard_t * kbd,int wait)624617b9080SKazutaka YOKOTA atkbd_read(keyboard_t *kbd, int wait)
625617b9080SKazutaka YOKOTA {
626617b9080SKazutaka YOKOTA int c;
627617b9080SKazutaka YOKOTA
628617b9080SKazutaka YOKOTA if (wait)
629617b9080SKazutaka YOKOTA c = read_kbd_data(((atkbd_state_t *)kbd->kb_data)->kbdc);
630617b9080SKazutaka YOKOTA else
631617b9080SKazutaka YOKOTA c = read_kbd_data_no_wait(((atkbd_state_t *)kbd->kb_data)->kbdc);
63282d654e8SKazutaka YOKOTA if (c != -1)
63382d654e8SKazutaka YOKOTA ++kbd->kb_count;
634617b9080SKazutaka YOKOTA return (KBD_IS_ACTIVE(kbd) ? c : -1);
635617b9080SKazutaka YOKOTA }
636617b9080SKazutaka YOKOTA
637617b9080SKazutaka YOKOTA /* check if data is waiting */
638617b9080SKazutaka YOKOTA static int
atkbd_check(keyboard_t * kbd)639617b9080SKazutaka YOKOTA atkbd_check(keyboard_t *kbd)
640617b9080SKazutaka YOKOTA {
641617b9080SKazutaka YOKOTA if (!KBD_IS_ACTIVE(kbd))
642617b9080SKazutaka YOKOTA return FALSE;
643617b9080SKazutaka YOKOTA return kbdc_data_ready(((atkbd_state_t *)kbd->kb_data)->kbdc);
644617b9080SKazutaka YOKOTA }
645617b9080SKazutaka YOKOTA
646617b9080SKazutaka YOKOTA /* read char from the keyboard */
647617b9080SKazutaka YOKOTA static u_int
atkbd_read_char(keyboard_t * kbd,int wait)648617b9080SKazutaka YOKOTA atkbd_read_char(keyboard_t *kbd, int wait)
649617b9080SKazutaka YOKOTA {
650617b9080SKazutaka YOKOTA atkbd_state_t *state;
651617b9080SKazutaka YOKOTA u_int action;
652617b9080SKazutaka YOKOTA int scancode;
653617b9080SKazutaka YOKOTA int keycode;
654617b9080SKazutaka YOKOTA
655617b9080SKazutaka YOKOTA state = (atkbd_state_t *)kbd->kb_data;
656617b9080SKazutaka YOKOTA next_code:
657617b9080SKazutaka YOKOTA /* do we have a composed char to return? */
658617b9080SKazutaka YOKOTA if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) {
659617b9080SKazutaka YOKOTA action = state->ks_composed_char;
660617b9080SKazutaka YOKOTA state->ks_composed_char = 0;
661617b9080SKazutaka YOKOTA if (action > UCHAR_MAX)
662617b9080SKazutaka YOKOTA return ERRKEY;
663617b9080SKazutaka YOKOTA return action;
664617b9080SKazutaka YOKOTA }
665617b9080SKazutaka YOKOTA
666617b9080SKazutaka YOKOTA /* see if there is something in the keyboard port */
667617b9080SKazutaka YOKOTA if (wait) {
668617b9080SKazutaka YOKOTA do {
669617b9080SKazutaka YOKOTA scancode = read_kbd_data(state->kbdc);
670617b9080SKazutaka YOKOTA } while (scancode == -1);
671617b9080SKazutaka YOKOTA } else {
672617b9080SKazutaka YOKOTA scancode = read_kbd_data_no_wait(state->kbdc);
673617b9080SKazutaka YOKOTA if (scancode == -1)
674617b9080SKazutaka YOKOTA return NOKEY;
675617b9080SKazutaka YOKOTA }
67682d654e8SKazutaka YOKOTA ++kbd->kb_count;
67782d654e8SKazutaka YOKOTA
67882d654e8SKazutaka YOKOTA #if KBDIO_DEBUG >= 10
67982d654e8SKazutaka YOKOTA printf("atkbd_read_char(): scancode:0x%x\n", scancode);
68082d654e8SKazutaka YOKOTA #endif
681617b9080SKazutaka YOKOTA
68238ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
68338ae0c59SOleksandr Tymoshenko /* push evdev event */
68438ae0c59SOleksandr Tymoshenko if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && state->ks_evdev != NULL) {
6856a26c99fSVladimir Kondratyev /* "hancha" and "han/yong" korean keys handling */
6866a26c99fSVladimir Kondratyev if (state->ks_evdev_state == 0 &&
6876a26c99fSVladimir Kondratyev (scancode == 0xF1 || scancode == 0xF2)) {
6886a26c99fSVladimir Kondratyev keycode = evdev_scancode2key(&state->ks_evdev_state,
6896a26c99fSVladimir Kondratyev scancode & 0x7F);
6906a26c99fSVladimir Kondratyev evdev_push_event(state->ks_evdev, EV_KEY,
6916a26c99fSVladimir Kondratyev (uint16_t)keycode, 1);
6926a26c99fSVladimir Kondratyev evdev_sync(state->ks_evdev);
6936a26c99fSVladimir Kondratyev }
6946a26c99fSVladimir Kondratyev
69538ae0c59SOleksandr Tymoshenko keycode = evdev_scancode2key(&state->ks_evdev_state,
69638ae0c59SOleksandr Tymoshenko scancode);
69738ae0c59SOleksandr Tymoshenko
69838ae0c59SOleksandr Tymoshenko if (keycode != KEY_RESERVED) {
69938ae0c59SOleksandr Tymoshenko evdev_push_event(state->ks_evdev, EV_KEY,
70038ae0c59SOleksandr Tymoshenko (uint16_t)keycode, scancode & 0x80 ? 0 : 1);
70138ae0c59SOleksandr Tymoshenko evdev_sync(state->ks_evdev);
70238ae0c59SOleksandr Tymoshenko }
70338ae0c59SOleksandr Tymoshenko }
70418308893SVladimir Kondratyev
70518308893SVladimir Kondratyev if (state->ks_evdev != NULL && evdev_is_grabbed(state->ks_evdev))
70618308893SVladimir Kondratyev return (NOKEY);
70738ae0c59SOleksandr Tymoshenko #endif
70838ae0c59SOleksandr Tymoshenko
709617b9080SKazutaka YOKOTA /* return the byte as is for the K_RAW mode */
710617b9080SKazutaka YOKOTA if (state->ks_mode == K_RAW)
711617b9080SKazutaka YOKOTA return scancode;
712617b9080SKazutaka YOKOTA
713617b9080SKazutaka YOKOTA /* translate the scan code into a keycode */
714617b9080SKazutaka YOKOTA keycode = scancode & 0x7F;
715617b9080SKazutaka YOKOTA switch (state->ks_prefix) {
716617b9080SKazutaka YOKOTA case 0x00: /* normal scancode */
717617b9080SKazutaka YOKOTA switch(scancode) {
718617b9080SKazutaka YOKOTA case 0xB8: /* left alt (compose key) released */
719617b9080SKazutaka YOKOTA if (state->ks_flags & COMPOSE) {
720617b9080SKazutaka YOKOTA state->ks_flags &= ~COMPOSE;
721617b9080SKazutaka YOKOTA if (state->ks_composed_char > UCHAR_MAX)
722617b9080SKazutaka YOKOTA state->ks_composed_char = 0;
723617b9080SKazutaka YOKOTA }
724617b9080SKazutaka YOKOTA break;
725617b9080SKazutaka YOKOTA case 0x38: /* left alt (compose key) pressed */
726617b9080SKazutaka YOKOTA if (!(state->ks_flags & COMPOSE)) {
727617b9080SKazutaka YOKOTA state->ks_flags |= COMPOSE;
728617b9080SKazutaka YOKOTA state->ks_composed_char = 0;
729617b9080SKazutaka YOKOTA }
730617b9080SKazutaka YOKOTA break;
731617b9080SKazutaka YOKOTA case 0xE0:
732617b9080SKazutaka YOKOTA case 0xE1:
733617b9080SKazutaka YOKOTA state->ks_prefix = scancode;
734617b9080SKazutaka YOKOTA goto next_code;
735617b9080SKazutaka YOKOTA }
736617b9080SKazutaka YOKOTA break;
737617b9080SKazutaka YOKOTA case 0xE0: /* 0xE0 prefix */
738617b9080SKazutaka YOKOTA state->ks_prefix = 0;
739617b9080SKazutaka YOKOTA switch (keycode) {
740617b9080SKazutaka YOKOTA case 0x1C: /* right enter key */
741617b9080SKazutaka YOKOTA keycode = 0x59;
742617b9080SKazutaka YOKOTA break;
743617b9080SKazutaka YOKOTA case 0x1D: /* right ctrl key */
744617b9080SKazutaka YOKOTA keycode = 0x5A;
745617b9080SKazutaka YOKOTA break;
746617b9080SKazutaka YOKOTA case 0x35: /* keypad divide key */
747617b9080SKazutaka YOKOTA keycode = 0x5B;
748617b9080SKazutaka YOKOTA break;
749617b9080SKazutaka YOKOTA case 0x37: /* print scrn key */
750617b9080SKazutaka YOKOTA keycode = 0x5C;
751617b9080SKazutaka YOKOTA break;
752617b9080SKazutaka YOKOTA case 0x38: /* right alt key (alt gr) */
753617b9080SKazutaka YOKOTA keycode = 0x5D;
754617b9080SKazutaka YOKOTA break;
755ffba82a9SKazutaka YOKOTA case 0x46: /* ctrl-pause/break on AT 101 (see below) */
756ffba82a9SKazutaka YOKOTA keycode = 0x68;
757ffba82a9SKazutaka YOKOTA break;
758617b9080SKazutaka YOKOTA case 0x47: /* grey home key */
759617b9080SKazutaka YOKOTA keycode = 0x5E;
760617b9080SKazutaka YOKOTA break;
761617b9080SKazutaka YOKOTA case 0x48: /* grey up arrow key */
762617b9080SKazutaka YOKOTA keycode = 0x5F;
763617b9080SKazutaka YOKOTA break;
764617b9080SKazutaka YOKOTA case 0x49: /* grey page up key */
765617b9080SKazutaka YOKOTA keycode = 0x60;
766617b9080SKazutaka YOKOTA break;
767617b9080SKazutaka YOKOTA case 0x4B: /* grey left arrow key */
768617b9080SKazutaka YOKOTA keycode = 0x61;
769617b9080SKazutaka YOKOTA break;
770617b9080SKazutaka YOKOTA case 0x4D: /* grey right arrow key */
771617b9080SKazutaka YOKOTA keycode = 0x62;
772617b9080SKazutaka YOKOTA break;
773617b9080SKazutaka YOKOTA case 0x4F: /* grey end key */
774617b9080SKazutaka YOKOTA keycode = 0x63;
775617b9080SKazutaka YOKOTA break;
776617b9080SKazutaka YOKOTA case 0x50: /* grey down arrow key */
777617b9080SKazutaka YOKOTA keycode = 0x64;
778617b9080SKazutaka YOKOTA break;
779617b9080SKazutaka YOKOTA case 0x51: /* grey page down key */
780617b9080SKazutaka YOKOTA keycode = 0x65;
781617b9080SKazutaka YOKOTA break;
782617b9080SKazutaka YOKOTA case 0x52: /* grey insert key */
783617b9080SKazutaka YOKOTA keycode = 0x66;
784617b9080SKazutaka YOKOTA break;
785617b9080SKazutaka YOKOTA case 0x53: /* grey delete key */
786617b9080SKazutaka YOKOTA keycode = 0x67;
787617b9080SKazutaka YOKOTA break;
788617b9080SKazutaka YOKOTA /* the following 3 are only used on the MS "Natural" keyboard */
789617b9080SKazutaka YOKOTA case 0x5b: /* left Window key */
790617b9080SKazutaka YOKOTA keycode = 0x69;
791617b9080SKazutaka YOKOTA break;
792617b9080SKazutaka YOKOTA case 0x5c: /* right Window key */
793617b9080SKazutaka YOKOTA keycode = 0x6a;
794617b9080SKazutaka YOKOTA break;
795617b9080SKazutaka YOKOTA case 0x5d: /* menu key */
796617b9080SKazutaka YOKOTA keycode = 0x6b;
797617b9080SKazutaka YOKOTA break;
7987ea199a7SMax Khon case 0x5e: /* power key */
7997ea199a7SMax Khon keycode = 0x6d;
8007ea199a7SMax Khon break;
8017ea199a7SMax Khon case 0x5f: /* sleep key */
8027ea199a7SMax Khon keycode = 0x6e;
8037ea199a7SMax Khon break;
8047ea199a7SMax Khon case 0x63: /* wake key */
8057ea199a7SMax Khon keycode = 0x6f;
8067ea199a7SMax Khon break;
807617b9080SKazutaka YOKOTA default: /* ignore everything else */
808617b9080SKazutaka YOKOTA goto next_code;
809617b9080SKazutaka YOKOTA }
810617b9080SKazutaka YOKOTA break;
811617b9080SKazutaka YOKOTA case 0xE1: /* 0xE1 prefix */
812ffba82a9SKazutaka YOKOTA /*
813ffba82a9SKazutaka YOKOTA * The pause/break key on the 101 keyboard produces:
814ffba82a9SKazutaka YOKOTA * E1-1D-45 E1-9D-C5
815ffba82a9SKazutaka YOKOTA * Ctrl-pause/break produces:
816ffba82a9SKazutaka YOKOTA * E0-46 E0-C6 (See above.)
817ffba82a9SKazutaka YOKOTA */
818617b9080SKazutaka YOKOTA state->ks_prefix = 0;
819617b9080SKazutaka YOKOTA if (keycode == 0x1D)
820617b9080SKazutaka YOKOTA state->ks_prefix = 0x1D;
821617b9080SKazutaka YOKOTA goto next_code;
822617b9080SKazutaka YOKOTA /* NOT REACHED */
823617b9080SKazutaka YOKOTA case 0x1D: /* pause / break */
824617b9080SKazutaka YOKOTA state->ks_prefix = 0;
825617b9080SKazutaka YOKOTA if (keycode != 0x45)
826617b9080SKazutaka YOKOTA goto next_code;
827617b9080SKazutaka YOKOTA keycode = 0x68;
828617b9080SKazutaka YOKOTA break;
829617b9080SKazutaka YOKOTA }
830617b9080SKazutaka YOKOTA
831ffba82a9SKazutaka YOKOTA if (kbd->kb_type == KB_84) {
832ffba82a9SKazutaka YOKOTA switch (keycode) {
833ffba82a9SKazutaka YOKOTA case 0x37: /* *(numpad)/print screen */
834ffba82a9SKazutaka YOKOTA if (state->ks_flags & SHIFTS)
835ffba82a9SKazutaka YOKOTA keycode = 0x5c; /* print screen */
836ffba82a9SKazutaka YOKOTA break;
837ffba82a9SKazutaka YOKOTA case 0x45: /* num lock/pause */
838ffba82a9SKazutaka YOKOTA if (state->ks_flags & CTLS)
839ffba82a9SKazutaka YOKOTA keycode = 0x68; /* pause */
840ffba82a9SKazutaka YOKOTA break;
841ffba82a9SKazutaka YOKOTA case 0x46: /* scroll lock/break */
842ffba82a9SKazutaka YOKOTA if (state->ks_flags & CTLS)
843ffba82a9SKazutaka YOKOTA keycode = 0x6c; /* break */
844ffba82a9SKazutaka YOKOTA break;
845ffba82a9SKazutaka YOKOTA }
846ffba82a9SKazutaka YOKOTA } else if (kbd->kb_type == KB_101) {
847ffba82a9SKazutaka YOKOTA switch (keycode) {
848ffba82a9SKazutaka YOKOTA case 0x5c: /* print screen */
849ffba82a9SKazutaka YOKOTA if (state->ks_flags & ALTS)
850ffba82a9SKazutaka YOKOTA keycode = 0x54; /* sysrq */
851ffba82a9SKazutaka YOKOTA break;
852ffba82a9SKazutaka YOKOTA case 0x68: /* pause/break */
853ffba82a9SKazutaka YOKOTA if (state->ks_flags & CTLS)
854ffba82a9SKazutaka YOKOTA keycode = 0x6c; /* break */
855ffba82a9SKazutaka YOKOTA break;
856ffba82a9SKazutaka YOKOTA }
857ffba82a9SKazutaka YOKOTA }
858ffba82a9SKazutaka YOKOTA
859617b9080SKazutaka YOKOTA /* return the key code in the K_CODE mode */
860617b9080SKazutaka YOKOTA if (state->ks_mode == K_CODE)
861617b9080SKazutaka YOKOTA return (keycode | (scancode & 0x80));
862617b9080SKazutaka YOKOTA
863617b9080SKazutaka YOKOTA /* compose a character code */
864617b9080SKazutaka YOKOTA if (state->ks_flags & COMPOSE) {
86583fc7c6cSKazutaka YOKOTA switch (keycode | (scancode & 0x80)) {
866617b9080SKazutaka YOKOTA /* key pressed, process it */
867617b9080SKazutaka YOKOTA case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */
868617b9080SKazutaka YOKOTA state->ks_composed_char *= 10;
869194da42dSKazutaka YOKOTA state->ks_composed_char += keycode - 0x40;
870617b9080SKazutaka YOKOTA if (state->ks_composed_char > UCHAR_MAX)
871617b9080SKazutaka YOKOTA return ERRKEY;
872617b9080SKazutaka YOKOTA goto next_code;
873617b9080SKazutaka YOKOTA case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */
874617b9080SKazutaka YOKOTA state->ks_composed_char *= 10;
875194da42dSKazutaka YOKOTA state->ks_composed_char += keycode - 0x47;
876617b9080SKazutaka YOKOTA if (state->ks_composed_char > UCHAR_MAX)
877617b9080SKazutaka YOKOTA return ERRKEY;
878617b9080SKazutaka YOKOTA goto next_code;
879617b9080SKazutaka YOKOTA case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */
880617b9080SKazutaka YOKOTA state->ks_composed_char *= 10;
881194da42dSKazutaka YOKOTA state->ks_composed_char += keycode - 0x4E;
882617b9080SKazutaka YOKOTA if (state->ks_composed_char > UCHAR_MAX)
883617b9080SKazutaka YOKOTA return ERRKEY;
884617b9080SKazutaka YOKOTA goto next_code;
885617b9080SKazutaka YOKOTA case 0x52: /* keypad 0 */
886617b9080SKazutaka YOKOTA state->ks_composed_char *= 10;
887617b9080SKazutaka YOKOTA if (state->ks_composed_char > UCHAR_MAX)
888617b9080SKazutaka YOKOTA return ERRKEY;
889617b9080SKazutaka YOKOTA goto next_code;
890617b9080SKazutaka YOKOTA
891617b9080SKazutaka YOKOTA /* key released, no interest here */
892617b9080SKazutaka YOKOTA case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */
893617b9080SKazutaka YOKOTA case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */
894617b9080SKazutaka YOKOTA case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */
895617b9080SKazutaka YOKOTA case 0xD2: /* keypad 0 */
896617b9080SKazutaka YOKOTA goto next_code;
897617b9080SKazutaka YOKOTA
898617b9080SKazutaka YOKOTA case 0x38: /* left alt key */
899617b9080SKazutaka YOKOTA break;
900617b9080SKazutaka YOKOTA
901617b9080SKazutaka YOKOTA default:
902617b9080SKazutaka YOKOTA if (state->ks_composed_char > 0) {
903617b9080SKazutaka YOKOTA state->ks_flags &= ~COMPOSE;
904617b9080SKazutaka YOKOTA state->ks_composed_char = 0;
905617b9080SKazutaka YOKOTA return ERRKEY;
906617b9080SKazutaka YOKOTA }
907617b9080SKazutaka YOKOTA break;
908617b9080SKazutaka YOKOTA }
909617b9080SKazutaka YOKOTA }
910617b9080SKazutaka YOKOTA
911617b9080SKazutaka YOKOTA /* keycode to key action */
912617b9080SKazutaka YOKOTA action = genkbd_keyaction(kbd, keycode, scancode & 0x80,
913617b9080SKazutaka YOKOTA &state->ks_state, &state->ks_accents);
914617b9080SKazutaka YOKOTA if (action == NOKEY)
915617b9080SKazutaka YOKOTA goto next_code;
916617b9080SKazutaka YOKOTA else
917617b9080SKazutaka YOKOTA return action;
918617b9080SKazutaka YOKOTA }
919617b9080SKazutaka YOKOTA
920617b9080SKazutaka YOKOTA /* check if char is waiting */
921617b9080SKazutaka YOKOTA static int
atkbd_check_char(keyboard_t * kbd)922617b9080SKazutaka YOKOTA atkbd_check_char(keyboard_t *kbd)
923617b9080SKazutaka YOKOTA {
924617b9080SKazutaka YOKOTA atkbd_state_t *state;
925617b9080SKazutaka YOKOTA
926617b9080SKazutaka YOKOTA if (!KBD_IS_ACTIVE(kbd))
927617b9080SKazutaka YOKOTA return FALSE;
928617b9080SKazutaka YOKOTA state = (atkbd_state_t *)kbd->kb_data;
929617b9080SKazutaka YOKOTA if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
930617b9080SKazutaka YOKOTA return TRUE;
931617b9080SKazutaka YOKOTA return kbdc_data_ready(state->kbdc);
932617b9080SKazutaka YOKOTA }
933617b9080SKazutaka YOKOTA
934617b9080SKazutaka YOKOTA /* some useful control functions */
935617b9080SKazutaka YOKOTA static int
atkbd_ioctl(keyboard_t * kbd,u_long cmd,caddr_t arg)936617b9080SKazutaka YOKOTA atkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
937617b9080SKazutaka YOKOTA {
938520b6353SMarius Strobl /* translate LED_XXX bits into the device specific bits */
939617b9080SKazutaka YOKOTA static u_char ledmap[8] = {
940617b9080SKazutaka YOKOTA 0, 4, 2, 6, 1, 5, 3, 7,
941617b9080SKazutaka YOKOTA };
942617b9080SKazutaka YOKOTA atkbd_state_t *state = kbd->kb_data;
943617b9080SKazutaka YOKOTA int error;
944617b9080SKazutaka YOKOTA int s;
945617b9080SKazutaka YOKOTA int i;
9469fddcc66SRuslan Ermilov #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
9479fddcc66SRuslan Ermilov defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
9489fddcc66SRuslan Ermilov int ival;
9499fddcc66SRuslan Ermilov #endif
950617b9080SKazutaka YOKOTA
951617b9080SKazutaka YOKOTA s = spltty();
952617b9080SKazutaka YOKOTA switch (cmd) {
953617b9080SKazutaka YOKOTA case KDGKBMODE: /* get keyboard mode */
954617b9080SKazutaka YOKOTA *(int *)arg = state->ks_mode;
955617b9080SKazutaka YOKOTA break;
9569fddcc66SRuslan Ermilov #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
9579fddcc66SRuslan Ermilov defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
9589fddcc66SRuslan Ermilov case _IO('K', 7):
9599fddcc66SRuslan Ermilov ival = IOCPARM_IVAL(arg);
9609fddcc66SRuslan Ermilov arg = (caddr_t)&ival;
9619fddcc66SRuslan Ermilov /* FALLTHROUGH */
9629fddcc66SRuslan Ermilov #endif
963617b9080SKazutaka YOKOTA case KDSKBMODE: /* set keyboard mode */
964617b9080SKazutaka YOKOTA switch (*(int *)arg) {
965617b9080SKazutaka YOKOTA case K_XLATE:
966617b9080SKazutaka YOKOTA if (state->ks_mode != K_XLATE) {
967617b9080SKazutaka YOKOTA /* make lock key state and LED state match */
968617b9080SKazutaka YOKOTA state->ks_state &= ~LOCK_MASK;
969617b9080SKazutaka YOKOTA state->ks_state |= KBD_LED_VAL(kbd);
970617b9080SKazutaka YOKOTA }
971617b9080SKazutaka YOKOTA /* FALLTHROUGH */
972617b9080SKazutaka YOKOTA case K_RAW:
973617b9080SKazutaka YOKOTA case K_CODE:
974617b9080SKazutaka YOKOTA if (state->ks_mode != *(int *)arg) {
975617b9080SKazutaka YOKOTA atkbd_clear_state(kbd);
976617b9080SKazutaka YOKOTA state->ks_mode = *(int *)arg;
977617b9080SKazutaka YOKOTA }
978617b9080SKazutaka YOKOTA break;
979617b9080SKazutaka YOKOTA default:
980617b9080SKazutaka YOKOTA splx(s);
981617b9080SKazutaka YOKOTA return EINVAL;
982617b9080SKazutaka YOKOTA }
983617b9080SKazutaka YOKOTA break;
984617b9080SKazutaka YOKOTA
985617b9080SKazutaka YOKOTA case KDGETLED: /* get keyboard LED */
986617b9080SKazutaka YOKOTA *(int *)arg = KBD_LED_VAL(kbd);
987617b9080SKazutaka YOKOTA break;
9889fddcc66SRuslan Ermilov #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
9899fddcc66SRuslan Ermilov defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
9909fddcc66SRuslan Ermilov case _IO('K', 66):
9919fddcc66SRuslan Ermilov ival = IOCPARM_IVAL(arg);
9929fddcc66SRuslan Ermilov arg = (caddr_t)&ival;
9939fddcc66SRuslan Ermilov /* FALLTHROUGH */
9949fddcc66SRuslan Ermilov #endif
995617b9080SKazutaka YOKOTA case KDSETLED: /* set keyboard LED */
996617b9080SKazutaka YOKOTA /* NOTE: lock key state in ks_state won't be changed */
997617b9080SKazutaka YOKOTA if (*(int *)arg & ~LOCK_MASK) {
998617b9080SKazutaka YOKOTA splx(s);
999617b9080SKazutaka YOKOTA return EINVAL;
1000617b9080SKazutaka YOKOTA }
1001617b9080SKazutaka YOKOTA i = *(int *)arg;
1002617b9080SKazutaka YOKOTA /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
10032073104fSAndrey A. Chernov if (state->ks_mode == K_XLATE &&
10042073104fSAndrey A. Chernov kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
1005617b9080SKazutaka YOKOTA if (i & ALKED)
1006617b9080SKazutaka YOKOTA i |= CLKED;
1007617b9080SKazutaka YOKOTA else
1008617b9080SKazutaka YOKOTA i &= ~CLKED;
1009617b9080SKazutaka YOKOTA }
1010617b9080SKazutaka YOKOTA if (KBD_HAS_DEVICE(kbd)) {
1011617b9080SKazutaka YOKOTA error = write_kbd(state->kbdc, KBDC_SET_LEDS,
1012617b9080SKazutaka YOKOTA ledmap[i & LED_MASK]);
1013617b9080SKazutaka YOKOTA if (error) {
1014617b9080SKazutaka YOKOTA splx(s);
1015617b9080SKazutaka YOKOTA return error;
1016617b9080SKazutaka YOKOTA }
1017617b9080SKazutaka YOKOTA }
101838ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
101938ae0c59SOleksandr Tymoshenko /* push LED states to evdev */
102038ae0c59SOleksandr Tymoshenko if (state->ks_evdev != NULL &&
102138ae0c59SOleksandr Tymoshenko evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
102238ae0c59SOleksandr Tymoshenko evdev_push_leds(state->ks_evdev, *(int *)arg);
102338ae0c59SOleksandr Tymoshenko #endif
1024617b9080SKazutaka YOKOTA KBD_LED_VAL(kbd) = *(int *)arg;
1025617b9080SKazutaka YOKOTA break;
1026617b9080SKazutaka YOKOTA
1027617b9080SKazutaka YOKOTA case KDGKBSTATE: /* get lock key state */
1028617b9080SKazutaka YOKOTA *(int *)arg = state->ks_state & LOCK_MASK;
1029617b9080SKazutaka YOKOTA break;
10309fddcc66SRuslan Ermilov #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
10319fddcc66SRuslan Ermilov defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
10329fddcc66SRuslan Ermilov case _IO('K', 20):
10339fddcc66SRuslan Ermilov ival = IOCPARM_IVAL(arg);
10349fddcc66SRuslan Ermilov arg = (caddr_t)&ival;
10359fddcc66SRuslan Ermilov /* FALLTHROUGH */
10369fddcc66SRuslan Ermilov #endif
1037617b9080SKazutaka YOKOTA case KDSKBSTATE: /* set lock key state */
1038617b9080SKazutaka YOKOTA if (*(int *)arg & ~LOCK_MASK) {
1039617b9080SKazutaka YOKOTA splx(s);
1040617b9080SKazutaka YOKOTA return EINVAL;
1041617b9080SKazutaka YOKOTA }
1042617b9080SKazutaka YOKOTA state->ks_state &= ~LOCK_MASK;
1043617b9080SKazutaka YOKOTA state->ks_state |= *(int *)arg;
1044617b9080SKazutaka YOKOTA splx(s);
1045617b9080SKazutaka YOKOTA /* set LEDs and quit */
1046617b9080SKazutaka YOKOTA return atkbd_ioctl(kbd, KDSETLED, arg);
1047617b9080SKazutaka YOKOTA
1048e9deda23SKazutaka YOKOTA case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
1049617b9080SKazutaka YOKOTA splx(s);
1050617b9080SKazutaka YOKOTA if (!KBD_HAS_DEVICE(kbd))
1051617b9080SKazutaka YOKOTA return 0;
1052e9deda23SKazutaka YOKOTA i = typematic(((int *)arg)[0], ((int *)arg)[1]);
105357995893SKazutaka YOKOTA error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, i);
105457995893SKazutaka YOKOTA if (error == 0) {
105557995893SKazutaka YOKOTA kbd->kb_delay1 = typematic_delay(i);
105657995893SKazutaka YOKOTA kbd->kb_delay2 = typematic_rate(i);
105738ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
105838ae0c59SOleksandr Tymoshenko if (state->ks_evdev != NULL &&
105938ae0c59SOleksandr Tymoshenko evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
106038ae0c59SOleksandr Tymoshenko evdev_push_repeats(state->ks_evdev, kbd);
106138ae0c59SOleksandr Tymoshenko #endif
106257995893SKazutaka YOKOTA }
106357995893SKazutaka YOKOTA return error;
1064e9deda23SKazutaka YOKOTA
10659fddcc66SRuslan Ermilov #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
10669fddcc66SRuslan Ermilov defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
10679fddcc66SRuslan Ermilov case _IO('K', 67):
10689fddcc66SRuslan Ermilov ival = IOCPARM_IVAL(arg);
10699fddcc66SRuslan Ermilov arg = (caddr_t)&ival;
10709fddcc66SRuslan Ermilov /* FALLTHROUGH */
10719fddcc66SRuslan Ermilov #endif
1072e9deda23SKazutaka YOKOTA case KDSETRAD: /* set keyboard repeat rate (old interface) */
1073e9deda23SKazutaka YOKOTA splx(s);
1074e9deda23SKazutaka YOKOTA if (!KBD_HAS_DEVICE(kbd))
1075e9deda23SKazutaka YOKOTA return 0;
107657995893SKazutaka YOKOTA error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, *(int *)arg);
107757995893SKazutaka YOKOTA if (error == 0) {
107857995893SKazutaka YOKOTA kbd->kb_delay1 = typematic_delay(*(int *)arg);
107957995893SKazutaka YOKOTA kbd->kb_delay2 = typematic_rate(*(int *)arg);
108038ae0c59SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
108138ae0c59SOleksandr Tymoshenko if (state->ks_evdev != NULL &&
108238ae0c59SOleksandr Tymoshenko evdev_rcpt_mask & EVDEV_RCPT_HW_KBD)
108338ae0c59SOleksandr Tymoshenko evdev_push_repeats(state->ks_evdev, kbd);
108438ae0c59SOleksandr Tymoshenko #endif
108557995893SKazutaka YOKOTA }
108657995893SKazutaka YOKOTA return error;
1087617b9080SKazutaka YOKOTA
1088617b9080SKazutaka YOKOTA case PIO_KEYMAP: /* set keyboard translation table */
1089617b9080SKazutaka YOKOTA case PIO_KEYMAPENT: /* set keyboard translation table entry */
1090617b9080SKazutaka YOKOTA case PIO_DEADKEYMAP: /* set accent key translation table */
109150fce3b4SEd Maste #ifdef COMPAT_FREEBSD13
1092f2005895SStefan Eßer case OPIO_KEYMAP: /* set keyboard translation table (compat) */
10934972fb92SStefan Eßer case OPIO_DEADKEYMAP: /* set accent key translation table (compat) */
1094f2005895SStefan Eßer #endif /* COMPAT_FREEBSD13 */
1095617b9080SKazutaka YOKOTA state->ks_accents = 0;
1096617b9080SKazutaka YOKOTA /* FALLTHROUGH */
1097617b9080SKazutaka YOKOTA default:
1098617b9080SKazutaka YOKOTA splx(s);
1099617b9080SKazutaka YOKOTA return genkbd_commonioctl(kbd, cmd, arg);
1100617b9080SKazutaka YOKOTA }
1101617b9080SKazutaka YOKOTA
1102617b9080SKazutaka YOKOTA splx(s);
1103617b9080SKazutaka YOKOTA return 0;
1104617b9080SKazutaka YOKOTA }
1105617b9080SKazutaka YOKOTA
1106617b9080SKazutaka YOKOTA /* lock the access to the keyboard */
1107617b9080SKazutaka YOKOTA static int
atkbd_lock(keyboard_t * kbd,int lock)1108617b9080SKazutaka YOKOTA atkbd_lock(keyboard_t *kbd, int lock)
1109617b9080SKazutaka YOKOTA {
1110617b9080SKazutaka YOKOTA return kbdc_lock(((atkbd_state_t *)kbd->kb_data)->kbdc, lock);
1111617b9080SKazutaka YOKOTA }
1112617b9080SKazutaka YOKOTA
1113617b9080SKazutaka YOKOTA /* clear the internal state of the keyboard */
1114617b9080SKazutaka YOKOTA static void
atkbd_clear_state(keyboard_t * kbd)1115617b9080SKazutaka YOKOTA atkbd_clear_state(keyboard_t *kbd)
1116617b9080SKazutaka YOKOTA {
1117617b9080SKazutaka YOKOTA atkbd_state_t *state;
1118617b9080SKazutaka YOKOTA
1119617b9080SKazutaka YOKOTA state = (atkbd_state_t *)kbd->kb_data;
1120617b9080SKazutaka YOKOTA state->ks_flags = 0;
1121e9deda23SKazutaka YOKOTA state->ks_polling = 0;
1122617b9080SKazutaka YOKOTA state->ks_state &= LOCK_MASK; /* preserve locking key state */
1123617b9080SKazutaka YOKOTA state->ks_accents = 0;
1124617b9080SKazutaka YOKOTA state->ks_composed_char = 0;
1125617b9080SKazutaka YOKOTA #if 0
1126617b9080SKazutaka YOKOTA state->ks_prefix = 0; /* XXX */
1127617b9080SKazutaka YOKOTA #endif
1128617b9080SKazutaka YOKOTA }
1129617b9080SKazutaka YOKOTA
1130617b9080SKazutaka YOKOTA /* save the internal state */
1131617b9080SKazutaka YOKOTA static int
atkbd_get_state(keyboard_t * kbd,void * buf,size_t len)1132617b9080SKazutaka YOKOTA atkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
1133617b9080SKazutaka YOKOTA {
1134617b9080SKazutaka YOKOTA if (len == 0)
1135617b9080SKazutaka YOKOTA return sizeof(atkbd_state_t);
1136617b9080SKazutaka YOKOTA if (len < sizeof(atkbd_state_t))
1137617b9080SKazutaka YOKOTA return -1;
1138617b9080SKazutaka YOKOTA bcopy(kbd->kb_data, buf, sizeof(atkbd_state_t));
1139617b9080SKazutaka YOKOTA return 0;
1140617b9080SKazutaka YOKOTA }
1141617b9080SKazutaka YOKOTA
1142617b9080SKazutaka YOKOTA /* set the internal state */
1143617b9080SKazutaka YOKOTA static int
atkbd_set_state(keyboard_t * kbd,void * buf,size_t len)1144617b9080SKazutaka YOKOTA atkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
1145617b9080SKazutaka YOKOTA {
1146617b9080SKazutaka YOKOTA if (len < sizeof(atkbd_state_t))
1147617b9080SKazutaka YOKOTA return ENOMEM;
1148617b9080SKazutaka YOKOTA if (((atkbd_state_t *)kbd->kb_data)->kbdc
1149617b9080SKazutaka YOKOTA != ((atkbd_state_t *)buf)->kbdc)
1150617b9080SKazutaka YOKOTA return ENOMEM;
1151617b9080SKazutaka YOKOTA bcopy(buf, kbd->kb_data, sizeof(atkbd_state_t));
1152617b9080SKazutaka YOKOTA return 0;
1153617b9080SKazutaka YOKOTA }
1154617b9080SKazutaka YOKOTA
1155e9deda23SKazutaka YOKOTA static int
atkbd_poll(keyboard_t * kbd,int on)1156e9deda23SKazutaka YOKOTA atkbd_poll(keyboard_t *kbd, int on)
1157e9deda23SKazutaka YOKOTA {
1158e9deda23SKazutaka YOKOTA atkbd_state_t *state;
1159e9deda23SKazutaka YOKOTA int s;
1160e9deda23SKazutaka YOKOTA
1161e9deda23SKazutaka YOKOTA state = (atkbd_state_t *)kbd->kb_data;
1162e9deda23SKazutaka YOKOTA s = spltty();
1163e9deda23SKazutaka YOKOTA if (on)
1164e9deda23SKazutaka YOKOTA ++state->ks_polling;
1165e9deda23SKazutaka YOKOTA else
1166e9deda23SKazutaka YOKOTA --state->ks_polling;
1167e9deda23SKazutaka YOKOTA splx(s);
1168e9deda23SKazutaka YOKOTA return 0;
1169e9deda23SKazutaka YOKOTA }
1170e9deda23SKazutaka YOKOTA
11716c176113SMichael Gmelin static int
atkbd_reset(KBDC kbdc,int flags,int c)11726c176113SMichael Gmelin atkbd_reset(KBDC kbdc, int flags, int c)
11736c176113SMichael Gmelin {
11746c176113SMichael Gmelin /* reset keyboard hardware */
11756c176113SMichael Gmelin if (!(flags & KB_CONF_NO_RESET) && !reset_kbd(kbdc)) {
11766c176113SMichael Gmelin /*
11776c176113SMichael Gmelin * KEYBOARD ERROR
11786c176113SMichael Gmelin * Keyboard reset may fail either because the keyboard
11796c176113SMichael Gmelin * doen't exist, or because the keyboard doesn't pass
11806c176113SMichael Gmelin * the self-test, or the keyboard controller on the
11816c176113SMichael Gmelin * motherboard and the keyboard somehow fail to shake hands.
11826c176113SMichael Gmelin * It is just possible, particularly in the last case,
11836c176113SMichael Gmelin * that the keyboard controller may be left in a hung state.
11846c176113SMichael Gmelin * test_controller() and test_kbd_port() appear to bring
11856c176113SMichael Gmelin * the keyboard controller back (I don't know why and how,
11866c176113SMichael Gmelin * though.)
11876c176113SMichael Gmelin */
11886c176113SMichael Gmelin empty_both_buffers(kbdc, 10);
11896c176113SMichael Gmelin test_controller(kbdc);
11906c176113SMichael Gmelin test_kbd_port(kbdc);
11916c176113SMichael Gmelin /*
11926c176113SMichael Gmelin * We could disable the keyboard port and interrupt... but,
11936c176113SMichael Gmelin * the keyboard may still exist (see above).
11946c176113SMichael Gmelin */
11956c176113SMichael Gmelin set_controller_command_byte(kbdc,
11966c176113SMichael Gmelin ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
11976c176113SMichael Gmelin if (bootverbose)
11986c176113SMichael Gmelin printf("atkbd: failed to reset the keyboard.\n");
11996c176113SMichael Gmelin return (EIO);
12006c176113SMichael Gmelin }
12016c176113SMichael Gmelin return (0);
12026c176113SMichael Gmelin }
12036c176113SMichael Gmelin
120448f2b006SVladimir Kondratyev #ifdef EVDEV_SUPPORT
120548f2b006SVladimir Kondratyev static void
atkbd_ev_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)120648f2b006SVladimir Kondratyev atkbd_ev_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
120748f2b006SVladimir Kondratyev int32_t value)
120848f2b006SVladimir Kondratyev {
120948f2b006SVladimir Kondratyev keyboard_t *kbd = evdev_get_softc(evdev);
121048f2b006SVladimir Kondratyev
121148f2b006SVladimir Kondratyev if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD &&
121248f2b006SVladimir Kondratyev (type == EV_LED || type == EV_REP)) {
121348f2b006SVladimir Kondratyev mtx_lock(&Giant);
121448f2b006SVladimir Kondratyev kbd_ev_event(kbd, type, code, value);
121548f2b006SVladimir Kondratyev mtx_unlock(&Giant);
121648f2b006SVladimir Kondratyev }
121748f2b006SVladimir Kondratyev }
121848f2b006SVladimir Kondratyev #endif
121948f2b006SVladimir Kondratyev
1220617b9080SKazutaka YOKOTA /* local functions */
1221617b9080SKazutaka YOKOTA
1222617b9080SKazutaka YOKOTA static int
set_typematic(keyboard_t * kbd)1223b19f7eecSRoger Pau Monné set_typematic(keyboard_t *kbd)
122456f5e27cSKazutaka YOKOTA {
1225b19f7eecSRoger Pau Monné int val, error;
1226b19f7eecSRoger Pau Monné atkbd_state_t *state = kbd->kb_data;
122756f5e27cSKazutaka YOKOTA
1228b19f7eecSRoger Pau Monné val = typematic(DEFAULT_DELAY, DEFAULT_RATE);
1229b19f7eecSRoger Pau Monné error = write_kbd(state->kbdc, KBDC_SET_TYPEMATIC, val);
1230b19f7eecSRoger Pau Monné if (error == 0) {
1231b19f7eecSRoger Pau Monné kbd->kb_delay1 = typematic_delay(val);
1232b19f7eecSRoger Pau Monné kbd->kb_delay2 = typematic_rate(val);
1233b19f7eecSRoger Pau Monné }
12343219f535SJung-uk Kim
1235b19f7eecSRoger Pau Monné return (error);
123656f5e27cSKazutaka YOKOTA }
123756f5e27cSKazutaka YOKOTA
123856f5e27cSKazutaka YOKOTA static int
setup_kbd_port(KBDC kbdc,int port,int intr)1239617b9080SKazutaka YOKOTA setup_kbd_port(KBDC kbdc, int port, int intr)
1240617b9080SKazutaka YOKOTA {
1241617b9080SKazutaka YOKOTA if (!set_controller_command_byte(kbdc,
1242617b9080SKazutaka YOKOTA KBD_KBD_CONTROL_BITS,
1243617b9080SKazutaka YOKOTA ((port) ? KBD_ENABLE_KBD_PORT : KBD_DISABLE_KBD_PORT)
1244617b9080SKazutaka YOKOTA | ((intr) ? KBD_ENABLE_KBD_INT : KBD_DISABLE_KBD_INT)))
1245617b9080SKazutaka YOKOTA return 1;
1246617b9080SKazutaka YOKOTA return 0;
1247617b9080SKazutaka YOKOTA }
1248617b9080SKazutaka YOKOTA
1249617b9080SKazutaka YOKOTA static int
get_kbd_echo(KBDC kbdc)1250617b9080SKazutaka YOKOTA get_kbd_echo(KBDC kbdc)
1251617b9080SKazutaka YOKOTA {
1252617b9080SKazutaka YOKOTA /* enable the keyboard port, but disable the keyboard intr. */
1253617b9080SKazutaka YOKOTA if (setup_kbd_port(kbdc, TRUE, FALSE))
1254617b9080SKazutaka YOKOTA /* CONTROLLER ERROR: there is very little we can do... */
1255617b9080SKazutaka YOKOTA return ENXIO;
1256617b9080SKazutaka YOKOTA
1257617b9080SKazutaka YOKOTA /* see if something is present */
1258617b9080SKazutaka YOKOTA write_kbd_command(kbdc, KBDC_ECHO);
1259617b9080SKazutaka YOKOTA if (read_kbd_data(kbdc) != KBD_ECHO) {
1260617b9080SKazutaka YOKOTA empty_both_buffers(kbdc, 10);
1261617b9080SKazutaka YOKOTA test_controller(kbdc);
1262617b9080SKazutaka YOKOTA test_kbd_port(kbdc);
1263617b9080SKazutaka YOKOTA return ENXIO;
1264617b9080SKazutaka YOKOTA }
1265617b9080SKazutaka YOKOTA
1266617b9080SKazutaka YOKOTA /* enable the keyboard port and intr. */
1267617b9080SKazutaka YOKOTA if (setup_kbd_port(kbdc, TRUE, TRUE)) {
1268617b9080SKazutaka YOKOTA /*
1269617b9080SKazutaka YOKOTA * CONTROLLER ERROR
1270617b9080SKazutaka YOKOTA * This is serious; the keyboard intr is left disabled!
1271617b9080SKazutaka YOKOTA */
1272617b9080SKazutaka YOKOTA return ENXIO;
1273617b9080SKazutaka YOKOTA }
1274617b9080SKazutaka YOKOTA
1275617b9080SKazutaka YOKOTA return 0;
1276617b9080SKazutaka YOKOTA }
1277617b9080SKazutaka YOKOTA
1278617b9080SKazutaka YOKOTA static int
probe_keyboard(KBDC kbdc,int flags)1279617b9080SKazutaka YOKOTA probe_keyboard(KBDC kbdc, int flags)
1280617b9080SKazutaka YOKOTA {
1281617b9080SKazutaka YOKOTA /*
1282617b9080SKazutaka YOKOTA * Don't try to print anything in this function. The low-level
1283617b9080SKazutaka YOKOTA * console may not have been initialized yet...
1284617b9080SKazutaka YOKOTA */
1285617b9080SKazutaka YOKOTA int err;
1286617b9080SKazutaka YOKOTA int c;
1287617b9080SKazutaka YOKOTA int m;
1288617b9080SKazutaka YOKOTA
1289617b9080SKazutaka YOKOTA if (!kbdc_lock(kbdc, TRUE)) {
1290617b9080SKazutaka YOKOTA /* driver error? */
1291617b9080SKazutaka YOKOTA return ENXIO;
1292617b9080SKazutaka YOKOTA }
1293617b9080SKazutaka YOKOTA
1294aec1fa1cSKazutaka YOKOTA /* temporarily block data transmission from the keyboard */
1295aec1fa1cSKazutaka YOKOTA write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1296aec1fa1cSKazutaka YOKOTA
1297617b9080SKazutaka YOKOTA /* flush any noise in the buffer */
1298aec1fa1cSKazutaka YOKOTA empty_both_buffers(kbdc, 100);
1299617b9080SKazutaka YOKOTA
1300617b9080SKazutaka YOKOTA /* save the current keyboard controller command byte */
1301617b9080SKazutaka YOKOTA m = kbdc_get_device_mask(kbdc) & ~KBD_KBD_CONTROL_BITS;
1302617b9080SKazutaka YOKOTA c = get_controller_command_byte(kbdc);
1303617b9080SKazutaka YOKOTA if (c == -1) {
1304617b9080SKazutaka YOKOTA /* CONTROLLER ERROR */
1305617b9080SKazutaka YOKOTA kbdc_set_device_mask(kbdc, m);
1306617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1307617b9080SKazutaka YOKOTA return ENXIO;
1308617b9080SKazutaka YOKOTA }
1309617b9080SKazutaka YOKOTA
1310617b9080SKazutaka YOKOTA /*
1311617b9080SKazutaka YOKOTA * The keyboard may have been screwed up by the boot block.
1312617b9080SKazutaka YOKOTA * We may just be able to recover from error by testing the controller
1313617b9080SKazutaka YOKOTA * and the keyboard port. The controller command byte needs to be
1314617b9080SKazutaka YOKOTA * saved before this recovery operation, as some controllers seem
1315617b9080SKazutaka YOKOTA * to set the command byte to particular values.
1316617b9080SKazutaka YOKOTA */
1317617b9080SKazutaka YOKOTA test_controller(kbdc);
1318f5517dc8SJohn Baldwin if (!(flags & KB_CONF_NO_PROBE_TEST))
1319617b9080SKazutaka YOKOTA test_kbd_port(kbdc);
1320617b9080SKazutaka YOKOTA
1321617b9080SKazutaka YOKOTA err = get_kbd_echo(kbdc);
1322a68d00e3SKazutaka YOKOTA
1323a68d00e3SKazutaka YOKOTA /*
1324a68d00e3SKazutaka YOKOTA * Even if the keyboard doesn't seem to be present (err != 0),
1325a68d00e3SKazutaka YOKOTA * we shall enable the keyboard port and interrupt so that
1326a68d00e3SKazutaka YOKOTA * the driver will be operable when the keyboard is attached
1327a68d00e3SKazutaka YOKOTA * to the system later. It is NOT recommended to hot-plug
1328a68d00e3SKazutaka YOKOTA * the AT keyboard, but many people do so...
1329a68d00e3SKazutaka YOKOTA */
1330a68d00e3SKazutaka YOKOTA kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1331a68d00e3SKazutaka YOKOTA setup_kbd_port(kbdc, TRUE, TRUE);
1332a68d00e3SKazutaka YOKOTA #if 0
1333617b9080SKazutaka YOKOTA if (err == 0) {
1334617b9080SKazutaka YOKOTA kbdc_set_device_mask(kbdc, m | KBD_KBD_CONTROL_BITS);
1335617b9080SKazutaka YOKOTA } else {
1336617b9080SKazutaka YOKOTA /* try to restore the command byte as before */
13376c176113SMichael Gmelin set_controller_command_byte(kbdc,
13386c176113SMichael Gmelin ALLOW_DISABLE_KBD(kbdc) ? 0xff : KBD_KBD_CONTROL_BITS, c);
1339617b9080SKazutaka YOKOTA kbdc_set_device_mask(kbdc, m);
1340617b9080SKazutaka YOKOTA }
1341a68d00e3SKazutaka YOKOTA #endif
1342617b9080SKazutaka YOKOTA
1343617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
13446c176113SMichael Gmelin return (HAS_QUIRK(kbdc, KBDC_QUIRK_IGNORE_PROBE_RESULT) ? 0 : err);
1345617b9080SKazutaka YOKOTA }
1346617b9080SKazutaka YOKOTA
1347617b9080SKazutaka YOKOTA static int
init_keyboard(KBDC kbdc,int * type,int flags)1348617b9080SKazutaka YOKOTA init_keyboard(KBDC kbdc, int *type, int flags)
1349617b9080SKazutaka YOKOTA {
1350617b9080SKazutaka YOKOTA int codeset;
1351617b9080SKazutaka YOKOTA int id;
1352617b9080SKazutaka YOKOTA int c;
1353617b9080SKazutaka YOKOTA
1354617b9080SKazutaka YOKOTA if (!kbdc_lock(kbdc, TRUE)) {
1355617b9080SKazutaka YOKOTA /* driver error? */
1356617b9080SKazutaka YOKOTA return EIO;
1357617b9080SKazutaka YOKOTA }
1358617b9080SKazutaka YOKOTA
1359aec1fa1cSKazutaka YOKOTA /* temporarily block data transmission from the keyboard */
1360aec1fa1cSKazutaka YOKOTA write_controller_command(kbdc, KBDC_DISABLE_KBD_PORT);
1361aec1fa1cSKazutaka YOKOTA
1362617b9080SKazutaka YOKOTA /* save the current controller command byte */
136382d654e8SKazutaka YOKOTA empty_both_buffers(kbdc, 200);
1364617b9080SKazutaka YOKOTA c = get_controller_command_byte(kbdc);
1365617b9080SKazutaka YOKOTA if (c == -1) {
1366617b9080SKazutaka YOKOTA /* CONTROLLER ERROR */
1367617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1368617b9080SKazutaka YOKOTA printf("atkbd: unable to get the current command byte value.\n");
1369617b9080SKazutaka YOKOTA return EIO;
1370617b9080SKazutaka YOKOTA }
1371617b9080SKazutaka YOKOTA if (bootverbose)
1372617b9080SKazutaka YOKOTA printf("atkbd: the current kbd controller command byte %04x\n",
1373617b9080SKazutaka YOKOTA c);
1374617b9080SKazutaka YOKOTA #if 0
1375617b9080SKazutaka YOKOTA /* override the keyboard lock switch */
1376617b9080SKazutaka YOKOTA c |= KBD_OVERRIDE_KBD_LOCK;
1377617b9080SKazutaka YOKOTA #endif
1378617b9080SKazutaka YOKOTA
1379617b9080SKazutaka YOKOTA /* enable the keyboard port, but disable the keyboard intr. */
1380617b9080SKazutaka YOKOTA if (setup_kbd_port(kbdc, TRUE, FALSE)) {
1381617b9080SKazutaka YOKOTA /* CONTROLLER ERROR: there is very little we can do... */
1382617b9080SKazutaka YOKOTA printf("atkbd: unable to set the command byte.\n");
1383617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1384617b9080SKazutaka YOKOTA return EIO;
1385617b9080SKazutaka YOKOTA }
1386617b9080SKazutaka YOKOTA
13876c176113SMichael Gmelin if (HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
13886c176113SMichael Gmelin atkbd_reset(kbdc, flags, c)) {
13896c176113SMichael Gmelin kbdc_lock(kbdc, FALSE);
13906c176113SMichael Gmelin return EIO;
13916c176113SMichael Gmelin }
13926c176113SMichael Gmelin
1393617b9080SKazutaka YOKOTA /*
1394617b9080SKazutaka YOKOTA * Check if we have an XT keyboard before we attempt to reset it.
1395617b9080SKazutaka YOKOTA * The procedure assumes that the keyboard and the controller have
1396617b9080SKazutaka YOKOTA * been set up properly by BIOS and have not been messed up
1397617b9080SKazutaka YOKOTA * during the boot process.
1398617b9080SKazutaka YOKOTA */
1399617b9080SKazutaka YOKOTA codeset = -1;
1400617b9080SKazutaka YOKOTA if (flags & KB_CONF_ALT_SCANCODESET)
1401617b9080SKazutaka YOKOTA /* the user says there is a XT keyboard */
1402617b9080SKazutaka YOKOTA codeset = 1;
1403617b9080SKazutaka YOKOTA #ifdef KBD_DETECT_XT_KEYBOARD
1404617b9080SKazutaka YOKOTA else if ((c & KBD_TRANSLATION) == 0) {
1405617b9080SKazutaka YOKOTA /* SET_SCANCODE_SET is not always supported; ignore error */
1406617b9080SKazutaka YOKOTA if (send_kbd_command_and_data(kbdc, KBDC_SET_SCANCODE_SET, 0)
1407617b9080SKazutaka YOKOTA == KBD_ACK)
1408617b9080SKazutaka YOKOTA codeset = read_kbd_data(kbdc);
1409617b9080SKazutaka YOKOTA }
1410617b9080SKazutaka YOKOTA if (bootverbose)
1411617b9080SKazutaka YOKOTA printf("atkbd: scancode set %d\n", codeset);
1412617b9080SKazutaka YOKOTA #endif /* KBD_DETECT_XT_KEYBOARD */
1413617b9080SKazutaka YOKOTA
1414617b9080SKazutaka YOKOTA *type = KB_OTHER;
1415617b9080SKazutaka YOKOTA id = get_kbd_id(kbdc);
1416617b9080SKazutaka YOKOTA switch(id) {
1417934d11e3SKazutaka YOKOTA case 0x41ab: /* 101/102/... Enhanced */
1418934d11e3SKazutaka YOKOTA case 0x83ab: /* ditto */
1419934d11e3SKazutaka YOKOTA case 0x54ab: /* SpaceSaver */
1420934d11e3SKazutaka YOKOTA case 0x84ab: /* ditto */
1421934d11e3SKazutaka YOKOTA #if 0
1422934d11e3SKazutaka YOKOTA case 0x90ab: /* 'G' */
1423934d11e3SKazutaka YOKOTA case 0x91ab: /* 'P' */
1424934d11e3SKazutaka YOKOTA case 0x92ab: /* 'A' */
1425934d11e3SKazutaka YOKOTA #endif
1426617b9080SKazutaka YOKOTA *type = KB_101;
1427617b9080SKazutaka YOKOTA break;
1428617b9080SKazutaka YOKOTA case -1: /* AT 84 keyboard doesn't return ID */
1429617b9080SKazutaka YOKOTA *type = KB_84;
1430617b9080SKazutaka YOKOTA break;
1431617b9080SKazutaka YOKOTA default:
1432617b9080SKazutaka YOKOTA break;
1433617b9080SKazutaka YOKOTA }
1434617b9080SKazutaka YOKOTA if (bootverbose)
1435617b9080SKazutaka YOKOTA printf("atkbd: keyboard ID 0x%x (%d)\n", id, *type);
1436617b9080SKazutaka YOKOTA
14376c176113SMichael Gmelin if (!HAS_QUIRK(kbdc, KBDC_QUIRK_RESET_AFTER_PROBE) &&
14386c176113SMichael Gmelin atkbd_reset(kbdc, flags, c)) {
1439617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1440617b9080SKazutaka YOKOTA return EIO;
1441617b9080SKazutaka YOKOTA }
1442617b9080SKazutaka YOKOTA
1443617b9080SKazutaka YOKOTA /*
14442ce7d7a0SPoul-Henning Kamp * Allow us to set the XT_KEYBD flag so that keyboards
1445617b9080SKazutaka YOKOTA * such as those on the IBM ThinkPad laptop computers can be used
1446617b9080SKazutaka YOKOTA * with the standard console driver.
1447617b9080SKazutaka YOKOTA */
1448617b9080SKazutaka YOKOTA if (codeset == 1) {
1449617b9080SKazutaka YOKOTA if (send_kbd_command_and_data(kbdc,
1450617b9080SKazutaka YOKOTA KBDC_SET_SCANCODE_SET, codeset) == KBD_ACK) {
1451617b9080SKazutaka YOKOTA /* XT kbd doesn't need scan code translation */
1452617b9080SKazutaka YOKOTA c &= ~KBD_TRANSLATION;
1453617b9080SKazutaka YOKOTA } else {
1454617b9080SKazutaka YOKOTA /*
1455617b9080SKazutaka YOKOTA * KEYBOARD ERROR
1456617b9080SKazutaka YOKOTA * The XT kbd isn't usable unless the proper scan
1457617b9080SKazutaka YOKOTA * code set is selected.
1458617b9080SKazutaka YOKOTA */
14596c176113SMichael Gmelin set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
14606c176113SMichael Gmelin ? 0xff : KBD_KBD_CONTROL_BITS, c);
1461617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1462617b9080SKazutaka YOKOTA printf("atkbd: unable to set the XT keyboard mode.\n");
1463617b9080SKazutaka YOKOTA return EIO;
1464617b9080SKazutaka YOKOTA }
1465617b9080SKazutaka YOKOTA }
1466617b9080SKazutaka YOKOTA
14676c176113SMichael Gmelin /*
14686c176113SMichael Gmelin * Some keyboards require a SETLEDS command to be sent after
14696c176113SMichael Gmelin * the reset command before they will send keystrokes to us
14706c176113SMichael Gmelin */
14716c176113SMichael Gmelin if (HAS_QUIRK(kbdc, KBDC_QUIRK_SETLEDS_ON_INIT) &&
14726c176113SMichael Gmelin send_kbd_command_and_data(kbdc, KBDC_SET_LEDS, 0) != KBD_ACK) {
14736c176113SMichael Gmelin printf("atkbd: setleds failed\n");
14746c176113SMichael Gmelin }
14756c176113SMichael Gmelin if (!ALLOW_DISABLE_KBD(kbdc))
14766c176113SMichael Gmelin send_kbd_command(kbdc, KBDC_ENABLE_KBD);
14776c176113SMichael Gmelin
1478617b9080SKazutaka YOKOTA /* enable the keyboard port and intr. */
1479617b9080SKazutaka YOKOTA if (!set_controller_command_byte(kbdc,
1480617b9080SKazutaka YOKOTA KBD_KBD_CONTROL_BITS | KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK,
1481617b9080SKazutaka YOKOTA (c & (KBD_TRANSLATION | KBD_OVERRIDE_KBD_LOCK))
1482617b9080SKazutaka YOKOTA | KBD_ENABLE_KBD_PORT | KBD_ENABLE_KBD_INT)) {
1483617b9080SKazutaka YOKOTA /*
1484617b9080SKazutaka YOKOTA * CONTROLLER ERROR
1485617b9080SKazutaka YOKOTA * This is serious; we are left with the disabled
1486617b9080SKazutaka YOKOTA * keyboard intr.
1487617b9080SKazutaka YOKOTA */
14886c176113SMichael Gmelin set_controller_command_byte(kbdc, ALLOW_DISABLE_KBD(kbdc)
14896c176113SMichael Gmelin ? 0xff : (KBD_KBD_CONTROL_BITS | KBD_TRANSLATION |
14906c176113SMichael Gmelin KBD_OVERRIDE_KBD_LOCK), c);
1491617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1492617b9080SKazutaka YOKOTA printf("atkbd: unable to enable the keyboard port and intr.\n");
1493617b9080SKazutaka YOKOTA return EIO;
1494617b9080SKazutaka YOKOTA }
1495617b9080SKazutaka YOKOTA
1496617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1497617b9080SKazutaka YOKOTA return 0;
1498617b9080SKazutaka YOKOTA }
1499617b9080SKazutaka YOKOTA
1500617b9080SKazutaka YOKOTA static int
write_kbd(KBDC kbdc,int command,int data)1501617b9080SKazutaka YOKOTA write_kbd(KBDC kbdc, int command, int data)
1502617b9080SKazutaka YOKOTA {
1503617b9080SKazutaka YOKOTA int s;
1504617b9080SKazutaka YOKOTA
1505617b9080SKazutaka YOKOTA /* prevent the timeout routine from polling the keyboard */
1506617b9080SKazutaka YOKOTA if (!kbdc_lock(kbdc, TRUE))
1507617b9080SKazutaka YOKOTA return EBUSY;
1508617b9080SKazutaka YOKOTA
1509617b9080SKazutaka YOKOTA /* disable the keyboard and mouse interrupt */
1510617b9080SKazutaka YOKOTA s = spltty();
1511617b9080SKazutaka YOKOTA #if 0
1512617b9080SKazutaka YOKOTA c = get_controller_command_byte(kbdc);
1513617b9080SKazutaka YOKOTA if ((c == -1)
1514617b9080SKazutaka YOKOTA || !set_controller_command_byte(kbdc,
1515617b9080SKazutaka YOKOTA kbdc_get_device_mask(kbdc),
1516617b9080SKazutaka YOKOTA KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1517617b9080SKazutaka YOKOTA | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1518617b9080SKazutaka YOKOTA /* CONTROLLER ERROR */
1519617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1520617b9080SKazutaka YOKOTA splx(s);
1521617b9080SKazutaka YOKOTA return EIO;
1522617b9080SKazutaka YOKOTA }
1523617b9080SKazutaka YOKOTA /*
1524617b9080SKazutaka YOKOTA * Now that the keyboard controller is told not to generate
1525617b9080SKazutaka YOKOTA * the keyboard and mouse interrupts, call `splx()' to allow
1526617b9080SKazutaka YOKOTA * the other tty interrupts. The clock interrupt may also occur,
1527617b9080SKazutaka YOKOTA * but the timeout routine (`scrn_timer()') will be blocked
1528617b9080SKazutaka YOKOTA * by the lock flag set via `kbdc_lock()'
1529617b9080SKazutaka YOKOTA */
1530617b9080SKazutaka YOKOTA splx(s);
1531617b9080SKazutaka YOKOTA #endif
1532617b9080SKazutaka YOKOTA if (send_kbd_command_and_data(kbdc, command, data) != KBD_ACK)
1533617b9080SKazutaka YOKOTA send_kbd_command(kbdc, KBDC_ENABLE_KBD);
1534617b9080SKazutaka YOKOTA #if 0
1535617b9080SKazutaka YOKOTA /* restore the interrupts */
1536f0143dacSWarner Losh if (!set_controller_command_byte(kbdc, kbdc_get_device_mask(kbdc),
1537617b9080SKazutaka YOKOTA c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1538617b9080SKazutaka YOKOTA /* CONTROLLER ERROR */
1539617b9080SKazutaka YOKOTA }
1540617b9080SKazutaka YOKOTA #else
1541617b9080SKazutaka YOKOTA splx(s);
1542617b9080SKazutaka YOKOTA #endif
1543617b9080SKazutaka YOKOTA kbdc_lock(kbdc, FALSE);
1544617b9080SKazutaka YOKOTA
1545617b9080SKazutaka YOKOTA return 0;
1546617b9080SKazutaka YOKOTA }
1547617b9080SKazutaka YOKOTA
1548617b9080SKazutaka YOKOTA static int
get_kbd_id(KBDC kbdc)1549617b9080SKazutaka YOKOTA get_kbd_id(KBDC kbdc)
1550617b9080SKazutaka YOKOTA {
1551617b9080SKazutaka YOKOTA int id1, id2;
1552617b9080SKazutaka YOKOTA
1553617b9080SKazutaka YOKOTA empty_both_buffers(kbdc, 10);
1554617b9080SKazutaka YOKOTA id1 = id2 = -1;
1555617b9080SKazutaka YOKOTA if (send_kbd_command(kbdc, KBDC_SEND_DEV_ID) != KBD_ACK)
1556617b9080SKazutaka YOKOTA return -1;
1557617b9080SKazutaka YOKOTA
1558617b9080SKazutaka YOKOTA DELAY(10000); /* 10 msec delay */
1559617b9080SKazutaka YOKOTA id1 = read_kbd_data(kbdc);
1560617b9080SKazutaka YOKOTA if (id1 != -1)
1561617b9080SKazutaka YOKOTA id2 = read_kbd_data(kbdc);
1562617b9080SKazutaka YOKOTA
1563617b9080SKazutaka YOKOTA if ((id1 == -1) || (id2 == -1)) {
1564617b9080SKazutaka YOKOTA empty_both_buffers(kbdc, 10);
1565617b9080SKazutaka YOKOTA test_controller(kbdc);
1566617b9080SKazutaka YOKOTA test_kbd_port(kbdc);
1567617b9080SKazutaka YOKOTA return -1;
1568617b9080SKazutaka YOKOTA }
1569617b9080SKazutaka YOKOTA return ((id2 << 8) | id1);
1570617b9080SKazutaka YOKOTA }
1571617b9080SKazutaka YOKOTA
157257995893SKazutaka YOKOTA static int
typematic_delay(int i)157357995893SKazutaka YOKOTA typematic_delay(int i)
157457995893SKazutaka YOKOTA {
1575*971bac5aSMichael return (kbdelays[(i >> 5) & 3]);
157657995893SKazutaka YOKOTA }
157757995893SKazutaka YOKOTA
157857995893SKazutaka YOKOTA static int
typematic_rate(int i)157957995893SKazutaka YOKOTA typematic_rate(int i)
158057995893SKazutaka YOKOTA {
1581*971bac5aSMichael return (kbrates[i & 0x1f]);
158257995893SKazutaka YOKOTA }
158357995893SKazutaka YOKOTA
158457995893SKazutaka YOKOTA static int
typematic(int delay,int rate)158557995893SKazutaka YOKOTA typematic(int delay, int rate)
158657995893SKazutaka YOKOTA {
1587e9deda23SKazutaka YOKOTA int value;
1588e9deda23SKazutaka YOKOTA int i;
1589e9deda23SKazutaka YOKOTA
1590*971bac5aSMichael for (i = nitems(kbdelays) - 1; i > 0; --i) {
1591*971bac5aSMichael if (delay >= kbdelays[i])
1592e9deda23SKazutaka YOKOTA break;
1593e9deda23SKazutaka YOKOTA }
1594e9deda23SKazutaka YOKOTA value = i << 5;
1595*971bac5aSMichael for (i = nitems(kbrates) - 1; i > 0; --i) {
1596*971bac5aSMichael if (rate >= kbrates[i])
1597e9deda23SKazutaka YOKOTA break;
1598e9deda23SKazutaka YOKOTA }
1599e9deda23SKazutaka YOKOTA value |= i;
1600e9deda23SKazutaka YOKOTA return value;
1601e9deda23SKazutaka YOKOTA }
1602