History log of /openbsd-current/sys/dev/wscons/wskbd.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.119 25-Mar-2024 mvs

Add 'ws_' prefix to 'wseventvar' structure members. No functional
changes.

ok miod


Revision tags: OPENBSD_7_5_BASE
# 1.118 18-Feb-2024 anton

Prevent ioctl(WSKBDIO_GETENCODINGS) NULL deference when sysctl
machdep.forceukbd is enabled without any USB keyboard being attached.

Found the hard way by sthen@; ok miod@


# 1.117 23-Nov-2023 deraadt

now always needs sys/task.h


# 1.116 22-Nov-2023 tobhe

Add support for keyboard backlight hotkeys in wskbd and hook up macppc apple
keyboards.

From jon (at) elytron (dot) openbsd (dot) amsterdam with some changes by me
ok gkoehler@


Revision tags: OPENBSD_7_4_BASE
# 1.115 09-Jul-2023 tobhe

Add suspend key support in wskbd and make it work on Apple ARM laptops.

feedback and ok miod@


Revision tags: OPENBSD_7_3_BASE
# 1.114 10-Nov-2022 matthieu

Move the code handling brightness keys into a task, since with
inteldrm(4) it requires a process context. ok kettenis@


Revision tags: OPENBSD_7_2_BASE
# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.118 18-Feb-2024 anton

Prevent ioctl(WSKBDIO_GETENCODINGS) NULL deference when sysctl
machdep.forceukbd is enabled without any USB keyboard being attached.

Found the hard way by sthen@; ok miod@


# 1.117 23-Nov-2023 deraadt

now always needs sys/task.h


# 1.116 22-Nov-2023 tobhe

Add support for keyboard backlight hotkeys in wskbd and hook up macppc apple
keyboards.

From jon (at) elytron (dot) openbsd (dot) amsterdam with some changes by me
ok gkoehler@


Revision tags: OPENBSD_7_4_BASE
# 1.115 09-Jul-2023 tobhe

Add suspend key support in wskbd and make it work on Apple ARM laptops.

feedback and ok miod@


Revision tags: OPENBSD_7_3_BASE
# 1.114 10-Nov-2022 matthieu

Move the code handling brightness keys into a task, since with
inteldrm(4) it requires a process context. ok kettenis@


Revision tags: OPENBSD_7_2_BASE
# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.117 23-Nov-2023 deraadt

now always needs sys/task.h


# 1.116 22-Nov-2023 tobhe

Add support for keyboard backlight hotkeys in wskbd and hook up macppc apple
keyboards.

From jon (at) elytron (dot) openbsd (dot) amsterdam with some changes by me
ok gkoehler@


Revision tags: OPENBSD_7_4_BASE
# 1.115 09-Jul-2023 tobhe

Add suspend key support in wskbd and make it work on Apple ARM laptops.

feedback and ok miod@


Revision tags: OPENBSD_7_3_BASE
# 1.114 10-Nov-2022 matthieu

Move the code handling brightness keys into a task, since with
inteldrm(4) it requires a process context. ok kettenis@


Revision tags: OPENBSD_7_2_BASE
# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.117 23-Nov-2023 deraadt

now always needs sys/task.h


# 1.116 22-Nov-2023 tobhe

Add support for keyboard backlight hotkeys in wskbd and hook up macppc apple
keyboards.

From jon (at) elytron (dot) openbsd (dot) amsterdam with some changes by me
ok gkoehler@


Revision tags: OPENBSD_7_4_BASE
# 1.115 09-Jul-2023 tobhe

Add suspend key support in wskbd and make it work on Apple ARM laptops.

feedback and ok miod@


Revision tags: OPENBSD_7_3_BASE
# 1.114 10-Nov-2022 matthieu

Move the code handling brightness keys into a task, since with
inteldrm(4) it requires a process context. ok kettenis@


Revision tags: OPENBSD_7_2_BASE
# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.115 09-Jul-2023 tobhe

Add suspend key support in wskbd and make it work on Apple ARM laptops.

feedback and ok miod@


Revision tags: OPENBSD_7_3_BASE
# 1.114 10-Nov-2022 matthieu

Move the code handling brightness keys into a task, since with
inteldrm(4) it requires a process context. ok kettenis@


Revision tags: OPENBSD_7_2_BASE
# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.114 10-Nov-2022 matthieu

Move the code handling brightness keys into a task, since with
inteldrm(4) it requires a process context. ok kettenis@


Revision tags: OPENBSD_7_2_BASE
# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.113 02-Jul-2022 visa

Remove unused device poll functions.

Also remove unneeded includes of <sys/poll.h> and <sys/select.h>.

Some addenda from jsg@.

OK miod@ mpi@


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.112 06-Apr-2022 naddy

constify struct cfattach


Revision tags: OPENBSD_7_1_BASE
# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.111 16-Feb-2022 anton

Currently, wskbd_set_mixervolume() only adjusts the volume of the first
attached audio device, i.e. audio0. This approach does not work well
while using additional audio devices equipped with physical volume keys
since those would only affect the volume of audio0.

Instead, correlate audio and ucc devices attached over USB in order to
adjust the volume of the correct audio device. This is done by passing a
cookie from the common point of attachment which is later used to
correlate the audio and wskbd device.

The same approach could be adopted for audio and wskbd devices attaching
on a different bus. Keep in mind that it's of importance to make use of
and increment the same global cookie identifier to avoid collisions.

Makes the volume keys on my Logitech G435 Headset do the right thing.

ok ratchov@


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.110 30-Dec-2021 anton

Prevent a double free by assigning the new keymap and corresponding size
after the allocation and initialization is done. Otherwise, a race is
possible if malloc ends up sleeping.

ok sashan@

Reported-by: syzbot+7f8224e9f1a3487caf25@syzkaller.appspotmail.com


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.109 22-Oct-2021 anton

One could end up with the wrong encoding in xenocara while having a ucc
keyboard attached and /etc/kbdtype being present. The advertised
encoding of a wsmux is a bit fragile as the last attached device will
dictate it. If this happens to be a ucc keyboard, KB_US will always be
the advertised encoding as its encoding is immutable and /etc/kbdtype is
ignored.

Instead, do not advertise the encoding for ucc devices when the parent
mux queries its attached devices. However, asking the device directly
(i.e. bypassing the mux) still returns the encoding as wsconsctl(8)
would otherwise report an error.

Thanks to landry@ for the report and testing.


Revision tags: OPENBSD_7_0_BASE
# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.108 20-Sep-2021 anton

Changing the encoding of a ucc keyboard doesn't make sense as only one
encoding is supported. Instead, silently ignore such requests. Gets rid
of the following warning emitted by kbd(8) while booting with a ucc
keyboard attached and /etc/kbdtype being present:

kbd: unsupported encoding uk on /dev/wskbd2

I ended up repurposing KB_MACHDEP as is became unused back in 2008. Note
that running a kernel with this commit applied requires kbd and
wsconsctl to be recompiled in order to show correct encodings.

Problem reported by landry@ and ok deraadt@


Revision tags: OPENBSD_6_9_BASE
# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.107 02-Nov-2020 tobhe

Enable brightness keys on powerbooks where the keyboard attaches as ukbd(4).

ok kn@


Revision tags: OPENBSD_6_8_BASE
# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.106 29-Jul-2020 anton

Fix yet another panic in which wsevent_fini() ends up being called with
NULL. This one is a race caused by clearing the me_evp member before
calling routines that could end up sleeping.

While here, make wsmux_mux_close() look more like the other mux close
routines for increased symmetry.

ok mpi@

Reported-by: syzbot+fb9ad34ba42994683850@syzkaller.appspotmail.com


Revision tags: OPENBSD_6_7_BASE
# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.105 26-Apr-2020 bru

Remove unused assignments.

CID 1453143

ok kettenis@


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.104 19-Apr-2020 kettenis

Move logic to change brightness level in reasonable steps from acpivout(4)
into wsdisplay(4). This code is now exposed through
wsdisplay_brightness_{step,zero,cycle} functions that can be called by
any driver that handles brightnes "hotkeys". These functions take
a wsdisplay(4) device pointer as their first argument, which should be
provided if a clear association between events and a particular display
exist. This is used in wskbd(4). Otherwise NULL can be passed and
the code will direct the request at the first wsdisplay(4) that
implements brightness adjustment.

Tested by many. Fixes brightness keys on x395 and other thinkpads with
AMD graphics.

ok patrick@


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.103 24-Mar-2020 anton

Remove a chatty debug printf (not enabled by default) in wskbdopen() and
wsmouseopen(); bringing them closer to wsmuxopen(). No functional
change.


# 1.102 24-Mar-2020 anton

Ensure that me_evp is still NULL before assignment during open of wscons
devices. This condition is checked early on during open but since the
same routine could end up sleeping before assigning me_evp, a race
against adding the same wscons device to a wsmux could be lost. This in
turn can cause a NULL deference during close.

ok mpi@

Reported-by: syzbot+34c3041bfd96c888c8bd@syzkaller.appspotmail.com


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.101 22-Mar-2020 anton

zap trailing whitespace


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.100 08-Jan-2020 visa

Unify handling of ioctls FIOSETOWN/SIOCSPGRP/TIOCSPGRP and
FIOGETOWN/SIOCGPGRP/TIOCGPGRP. Do this by determining the meaning of
the ID parameter inside the sigio code. Also add cases for FIOSETOWN
and FIOGETOWN where there have been TIOCSPGRP and TIOCGPGRP before.
These changes allow removing the ID translation from sys_fcntl() and
sys_ioctl().

Idea from NetBSD

OK mpi@, claudio@


Revision tags: OPENBSD_6_6_BASE
# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.99 08-Aug-2019 cheloha

wskbd, wsmouse(4): tsleep(9) -> tsleep_nsec(9); ok mpi@ kn@


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.98 22-May-2019 anton

Cleanup debug macros in wscons. Favor __func__ over spelling out the actual
function name in order to reduce grep noise. Also, some of them where referring
to the wrong function.


# 1.97 22-May-2019 anton

A wscons device may only be opened in read/write mode once. However,
after checking for exclusive access, malloc() can sleep in
wsevent_init() opening up for a potential race where more than one
thread may be able open the device. Prevent this by checking if the race
was won after calling malloc().

While here, switch to mallocarray as proposed by both cheloha@ and mpi@

ok mpi@


Revision tags: OPENBSD_6_5_BASE
# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.96 19-Feb-2019 anton

favor C99 initializers for improved grepability; ok mpi@


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.95 01-Feb-2019 anton

In wskbdclose(), use the same logic as in wskbdopen() to determine if
the device was opened in write-only mode. Relying on me_evar being NULL
does not work if the wskbd device was opened first followed by opening a
wsmux device. Closing the wskbd device first at this stage would cause
the wscons_event queue inherited from the wsmux device to be freed. This
in turn could cause a panic if an ioctl(WSMUXIO_INJECTEVENT) command is
issued to the wsmux device.

ok deraadt@ visa@

Reported-by: syzbot+ed88256423ae8d882b8b@syzkaller.appspotmail.com


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.94 20-Nov-2018 anton

Add support for TIOCGPGRP ioctl commands to wscons.

ok visa@


# 1.93 20-Nov-2018 anton

No need for wscons to handle FIOSETOWN ioctl requests since sys_ioctl()
translates such requests into TIOCSPGRP.

ok visa@


# 1.92 19-Nov-2018 anton

Utilize sigio with wscons. The old behavior of always making the process group
of the process who opens the device the default recipient of sigio is removed as
a side-effect of this change. Issuing ioctl(FIOSETOWN) is therefore mandatory in
order to receive sigio, which is more consistent with other subsystems
supporting sigio.

This change is all made possible by the new sigio(9) API and prevents wscons
from keeping a pointer to the recipient process. Usage of such pointer could
cause a panic since wscons is not inform on process deallocation, leaving a
dangling pointer behind.

ok mpi@ visa@


Revision tags: OPENBSD_6_4_BASE
# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.91 18-Apr-2018 mpi

Try to get the layout from the default mux when attaching a console
keyboard late.

This makes keyboard re-attaching due to the machdep.forceukbd sysctl(2)
respect /etc/kbdtype.

Issue reported by Artturi Alm, jmc@ and landry@.

ok kettenis@


Revision tags: OPENBSD_6_3_BASE
# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.90 19-Feb-2018 mpi

Remove almost unused `flags' argument of suser().

The account flag `ASU' will no longer be set but that makes suser()
mpsafe since it no longer mess with a per-process field.

No objection from millert@, ok tedu@, bluhm@


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.89 22-Jan-2018 fcambus

More sizes for free() in wscons(4).

OK visa@


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster


# 1.88 17-Jan-2018 fcambus

Add sizes for free() in wscons(4).

OK deraadt@, visa@, mpi@


Revision tags: OPENBSD_6_2_BASE
# 1.87 12-May-2017 mpi

Introduce a new keyboard console hook to enter ddb(4) and make ukbd(4)
use it.

Instead of defering every input of a USB console keyboard to a timeout
via a queue of one element, only differ entering ddb(4) once a matching
control sequenece has been typed.

This prevent loosing inputs when a USB console keyboard is "too fast".

Fix a problem reported by matthieu@, Adam McDougall and Hrvoje Popovski.

ok stsp@, dlg@


# 1.86 30-Apr-2017 mpi

Rename Debugger() into db_enter().

Using a name with the 'db_' prefix makes it invisible from the dynamic
profiler.

ok deraadt@, kettenis@, visa@


Revision tags: OPENBSD_6_1_BASE
# 1.85 11-Mar-2017 mpi

Introduce a new knob to force the first USB keyboard as console input.

By setting "machdep.forceukbd=1" you can now use your USB keyboard in
ddb(4) even if your BIOS emulates a pckbd(4).

ok tom@, kettenis@, deraadt@


# 1.84 30-Sep-2016 kettenis

Add a WSKBDIO_GETENCODINGS ioctl that returns the supported keyboard
encodings.

ok deraadt@, jca@


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.83 12-Dec-2015 jung

add new ioctls to control keyboard backlights

tested by Bryan Vyhmeister, krw, tb, and myself

ok jcs krw tb


# 1.82 10-Sep-2015 mpi

kqueue(2) support for wsmouse(4), wskbd(4) and wsmux(4).

Needed for libinput port.

ok guenther@, miod@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.81 13-Dec-2014 doug

yet more mallocarray() changes.

ok tedu@ deraadt@


Revision tags: OPENBSD_5_6_BASE
# 1.80 12-Jul-2014 tedu

add a size argument to free. will be used soon, but for now default to 0.
after discussions with beck deraadt kettenis.


# 1.79 11-Jul-2014 guenther

It's init as a process that's special, not init's original thread.
Remember initprocess instead of initproc.

ok matthew@ blambert@


# 1.78 15-May-2014 mpi

Use a more appropriate malloc(9) type than M_TEMP for the keymap memory
since it is only freed when your keyboard is detached.


# 1.77 05-May-2014 mpi

Plug a memory leak, free the keymap's memory on detach.

ok miod@


Revision tags: OPENBSD_5_5_BASE
# 1.76 26-Jan-2014 miod

Attempt to make user changes of keyboard layout a bit more `sticky' on wsmux
kernels:
- keyboard drivers will now tell wskbd if the keyboard layout they ask
for is a default value, or a value they are 100% sure of (either
because your kernel has a XXXKBD_LAYOUT option, or because the
driver can tell the keyboard layout, e.g. by the country code on USB
keyboards which provide it, such as Sun's)
- when attaching a keyboard with a non-default layout, the layout will
become the default layout of the mux for new keyboard attachments if
the mux doesn't have a layout set already.
- when changing the keyboard layout of a particular keyboard with an
ioctl (i.e. using kbd(8) or wsconsctl(8)), the layout will become the
default layout of the mux for new keyboard attachments.

ok mpi@


# 1.75 04-Nov-2013 mpi

Convert wskbd_set_mixervolume() to use a task internally instead of
being called in a workq.

ok kettenis@


# 1.74 18-Oct-2013 miod

UTF-8 support for wsdisplay emulation modes. Both vt100 and sun emulation
will now recognize xterm-compatible escape sequences ESC % G and ESC % @ to
enter and leave UTF-8 mode, respectively.

Not enabled on SMALL_KERNEL.

Most of this written during c2k7, completed during b2k13.

Note that the value of several international keysyms change, to switch from
ISO Latin-[257] values to the real Unicode values. Be sure to update your
includes and rebuild and install wsconsctl.


# 1.73 18-Oct-2013 miod

Introduce a dedicated private header file to control the optional features
of wscons (which usually get disabled for installation kernels, to save
space), instead of duplicating parts of it to too many places.

No functional change.


Revision tags: OPENBSD_5_3_BASE OPENBSD_5_4_BASE
# 1.72 06-Jan-2013 ratchov

don't propagate XF86XK_Audio{Lower,Raise}Volume keys to userspace,
otherwise volume may be adjusted twice: once by X apps and once in
the kernel.

feedback from many, ok shadchin@


# 1.71 17-Oct-2012 jsg

#if NWSDISPLAY > 0 not #if WSDISPLAY > 0
ok miod@


Revision tags: OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.70 09-Nov-2011 shadchin

If directly open /dev/wskbdX, then wskbdX be removed from mux wskbd,
but if close wskbdX, wskbdX not returned to mux. Fix it.

ok miod@


Revision tags: OPENBSD_5_0_BASE
# 1.69 24-Jun-2011 naddy

machdep.kbdreset enables a shutdown by Ctrl-Alt-Del on amd64 and
i386. Stop abusing it on other archs for controling a shutdown by
pressing the soft power button:

* Add a MI sysctl hw.allowpowerdown; if set to 1 (the default) it
allows a power button shutdown.
* Make acpi(4)/acpibtn(4) honor hw.allowpowerdown.
* Switch the various power button intercepts on landisk, sgi, sparc64
and zaurus over to hw.allowpowerdown.
* Garbage collect the machdep.kbdreset sysctl on all archs other than
amd64 and i386.

ok miod@


# 1.68 23-Jun-2011 deraadt

a bit more ansi; ok tedu


# 1.67 06-Jun-2011 deraadt

Microphone mute button support from acpithinkpad (or elsewhere).
The audio side is a bit lacking; it does not yet mute all types of
devices.
from Alexander Polakov
ok jakemsr


# 1.66 14-Apr-2011 shadchin

Reducing number of call update_leds().
Updating state LED only when necessary.

ok krw@


# 1.65 13-Apr-2011 shadchin

wskbd_holdscreen() is not needed and can be removed

ok miod@


Revision tags: OPENBSD_4_9_BASE
# 1.64 20-Nov-2010 miod

Change wsdisplay_kbdinput() to get an aray of keysym_t and a count, instead of
a single keysym_t at a time - this means tty sanity checks will only happen
once.

Introduce wsdisplay_rawkbdinput() for raw mode input, since raw input is
a byte array. As a bonus this allows us to skip the `is this a KS_GROUP_Ascii
value' test in that case.


# 1.63 20-Nov-2010 miod

Replace all mentions of sc_base.me_dispdv with sc_displaydv. The latter is a
short #define for the former, and seeing both forms in this file is confusing.


Revision tags: OPENBSD_4_8_BASE
# 1.62 26-Jul-2010 guenther

Correct the links between threads, processes, pgrps, and sessions,
so that the process-level stuff is to/from struct process and not
struct proc. This fixes a bunch of problem cases in rthreads.
Based on earlier work by blambert and myself, but mostly written
at c2k10.

Tested by many: deraadt, sthen, krw, ray, and in snapshots


Revision tags: OPENBSD_4_7_BASE
# 1.61 13-Oct-2009 pirofti

Get rid of devact enum, substitute it with an int and coresponding defines.

This is needed for the addition of further suspend/resume actions.

Okay deraadt@, marco@.


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.60 21-Jan-2009 grange

Replace timeout_add(msecs * hz / 1000) with timeout_add_msec(msecs).
No functional changes.

ok krw@ miod@


# 1.59 21-Dec-2008 dlg

on i386 and amd64, let ctrl-alt-delete let you enter ddb if ddb.console=1
and machdep.kbdreset=2.

"i like it" deraadt@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.58 11-Sep-2007 gilles

KNF

prompted and "much better" by marco@, ok pyr@


# 1.57 10-Sep-2007 gilles

more MALLOC/FREE -> malloc/free and M_ZERO changes

ok pyr@


Revision tags: OPENBSD_4_2_BASE
# 1.56 02-Jun-2007 tedu

little fixes to get hotkeys working again. from alexey vatchenko


# 1.55 31-May-2007 tedu

switch wskbd hotkeys over to workq. this eliminates quite a bit of code,
so remove the option too. if you have audio and wskbd, you have hotkeys.
ok dlg


# 1.54 25-Mar-2007 tedu

POLLERR is the correct return code for poll errors
ok art deraadt miod


Revision tags: OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.53 14-Aug-2006 miod

Really do what I intended on SMALL_KERNEL configurations.


# 1.52 14-Aug-2006 miod

Add an attribute to the keyboard devices which can produce the keys the
wskbd hotkey thread knows about, and make this code conditional on this
attribute. No functional change, simply smaller kernels on a few platforms.


# 1.51 06-Aug-2006 miod

Do not try and mate wskbd0 and wsdisplay0 if there is now wsdisplay in the
kernel; spotted by kettenis.


# 1.50 05-Aug-2006 miod

On platforms which do not use wsmux (because they can't have more than one
keyboard at once), force wskbd0 to connect to wsdisplay0 even if they are
not console devices.


# 1.49 01-Aug-2006 miod

Avoid a few NULL dereferences, and boot -c works on vax with glass console.


# 1.48 31-Jul-2006 miod

Kill autorepeat on ``all keys up'' event.


# 1.47 29-May-2006 miod

Add support for the audio volume keys found on many laptops' builtin
keyboard. These specific keys are posted to a kernel thread which will
issue mixer commands if an audio device exists.

Written by Alexey Vatchenko <avv , mail zp ua> with tweaks by deraadt@ and I.


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE
# 1.46 14-Aug-2005 miod

Honor (the lack of) FWRITE in the various ioctl routines; with input from
fgs@ and mickey@; ok deraadt@.


# 1.45 08-Jul-2005 marc

Fix mismatched brace and compile when NWSMUX and NWSDISPLAY not defined.
OK uwe@


# 1.44 02-Jun-2005 miod

Do not compile scrollback support if option SMALL_KERNEL.
ok deraadt@


# 1.43 18-May-2005 miod

Do not compile the screen burner code if SMALL_KERNEL, helps floppies.
ok deraadt@


# 1.42 15-May-2005 miod

Partial sync to the NetBSD wscons code, bringing a better wsmux behaviour
and bugfixes (the kqueue code, /dev/wsmuxctl and screen border color changes
have not been picked), keeping local changes.

Tested by many on alpha/cats/hp300/i386/macppc/sparc/sparc64/zaurus if not more.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.41 24-Jun-2004 tholo

This moves access to wall and uptime variables in MI code,
encapsulating all such access into wall-defined functions
that makes sure locking is done as needed.

It also cleans up some uses of wall time vs. uptime some
places, but there is sure to be more of these needed as
well, particularily in MD code. Also, many current calls
to microtime() should probably be changed to getmicrotime(),
or to the {,get}microuptime() versions.

ok art@ deraadt@ aaron@ matthieu@ beck@ sturm@ millert@ others
"Oh, that is not your problem!" from miod@


Revision tags: SMP_SYNC_A SMP_SYNC_B
# 1.40 05-Apr-2004 miod

Most of the non-US keyboard layouts define an ``AltGr'' key to access up
to two extra symbols per key; usually, this key also serves as the compose
key, if used with shift.

Because of this, accessing the fourth symbol of a key would work with a
sequence altgr-shift-key, but not with a sequence shift-altgr-key, since
it would trigger compose behaviour.

In order to be less confusing, let the AltGr behaviour win over the Compose
behaviour if AltGr would produce a different symbol. Since most of those
AltGr symbols are never part of compose rules, this is transparent.

Problem found by xsa@; ok deraadt@ otto@ xsa@


Revision tags: OPENBSD_3_5_BASE
# 1.39 14-Mar-2004 miod

Caps lock should only affect the letter keys, rather than enforcing a shift
state. Fixes PR#2555


# 1.38 27-Feb-2004 deraadt

kbd_reset can work on amd64 too


# 1.37 23-Sep-2003 millert

Replace select backends with poll backends. selscan() and pollscan()
now call the poll backend. With this change we implement greater
poll(2) functionality instead of emulating it via the select backend.
Adapted from NetBSD and including some changes from FreeBSD.
Tested by many, deraadt@ OK


Revision tags: OPENBSD_3_4_BASE
# 1.36 15-Aug-2003 tedu

change arguments to suser. suser now takes the process, and a flags
argument. old cred only calls user suser_ucred. this will allow future
work to more flexibly implement the idea of a root process. looks like
something i saw in freebsd, but a little different.
use of suser_ucred vs suser in file system code should be looked at again,
for the moment semantics remain unchanged.
review and input from art@ testing and further review miod@


# 1.35 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_3_BASE UBC_SYNC_A
# 1.34 23-Dec-2002 miod

Repair minor indentation problem.


Revision tags: OPENBSD_3_2_BASE UBC_SYNC_B
# 1.33 20-May-2002 miod

Do not blindly call wsdisplay routines if no wsdisplay device is active
and the user presses a magic key.
ok mickey@


# 1.32 30-Apr-2002 mickey

cdev_decl is done in conf.h, do not dupe here


Revision tags: OPENBSD_3_1_BASE
# 1.31 05-Apr-2002 jason

protect another use of wskbd_repeat with #if NWSDISPLAY > 0


# 1.30 30-Mar-2002 mickey

fix two evil bugs:
panic on absent wsdiplay on scrollback;
missing breaks.


# 1.29 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: UBC_BASE
# 1.28 30-Oct-2001 mickey

branches: 1.28.2;
do not allow setting autorepeat delays causing zero timeouts later.
pointed out for the case of del*=0 in pr@2156 by
Juan Vera <juan@coredump.com.ar>.


# 1.27 25-Oct-2001 drahn

If the console keyboard goes away, reinit wskbd_console_device.
Will allow future console keyboard attaches to occur.


Revision tags: OPENBSD_3_0_BASE
# 1.26 30-Sep-2001 mickey

add mode_lock for mode_shift as the caps_lock is to shift; from Igor Grabin <violent@death.kiev.ua>


# 1.25 26-Sep-2001 jcs

Connect USB keyboards to the display after attaching, no longer requiring
'wsconscfg -k' to be run.

Thanks to James Matthews for the extra keyboard.

aaron@ ok


# 1.24 16-Sep-2001 millert

Add some missing lengths checks when passing data from userland to
kernel. From based on NetBSD patches.


# 1.23 11-Jun-2001 mickey

good when it compiles


# 1.22 11-Jun-2001 mickey

follow x11 behaviour wrt caps^shift, even i like it this way, but deraadtified


# 1.21 09-Jun-2001 aaron

Set sc_dying in wskbd_detach, otherwise it's pointless.


# 1.20 09-Jun-2001 aaron

Remove sc_ready from the softc, we can just test sc_events.io instead.


# 1.19 09-Jun-2001 aaron

Allow wskbd_cngetc() to return any key pressed, not just ones in the ASCII
group; fixes PR/1787. This mimics the behavior of pccons and pcvt; mickey@ ok


# 1.18 08-Jun-2001 mickey

and add a note on result value importance


# 1.17 08-Jun-2001 mickey

invert caps w/ shifts, also cleanup the whole routine; pointed out by pvalchev@; aaron@ ok


# 1.16 08-May-2001 mickey

branches: 1.16.2;
inplement screen burner in screenblank(1) style (no program though).
disabled by default, also supports vsync blanking, disabled by default.
aaron@ looked at it a while back and i fixed problems he had indicated.


Revision tags: OPENBSD_2_9_BASE
# 1.15 18-Apr-2001 aaron

branches: 1.15.2;
Oops -- adjust the comment as well.


# 1.14 18-Apr-2001 aaron

Set the default bell pitch to 400, like X.


# 1.13 09-Apr-2001 aaron

Honor the db_console sysctl variable, like PCVT did.


# 1.12 30-Mar-2001 aaron

Add a new wsmuxop, dissetdisplay, which wsmux uses to disocver whether a mux
device is connected to a display or not. Use it in wsmux_detach_sc(). This
fixes a problem I've been struggling with whereby the machine panics if I
detach a USB keyboard before disconnecting it from wsdisplay.


# 1.11 01-Mar-2001 aaron

Wrap KS_Cmd_KbdReset in #ifdef __i386__ for now; politely pointed to this by
drahn@, snarked at by mickey@ ;)


# 1.10 01-Mar-2001 aaron

Add CTRL-ALT-DEL machine reset support, like PCVT had, enabled by the
machdep.kbdreset sysctl as configured in /etc/sysctl.conf.


# 1.9 11-Feb-2001 aaron

Allow up to 12 virtual terminals (CTRL-ALT-F1 through CTRL-ALT-F12). Only
6 are enabled by default. After these changes, to enable more you must
change the "option WSDISPLAY_DEFAULTSCREENS" in the kernel config file and
edit /etc/ttys to switch the new ones from 'off' to 'on'.


# 1.8 10-Feb-2001 mickey

sync w/ netbsd; aaron@ ok


# 1.7 08-Feb-2001 aaron

Cause keypresses to reset the screen in case we are in scrollback (previously
the screen was only restored if a new character was actually displayed on the
screen); jcs@rt.fm. This brings us closer to the behavior of PCVT. Also, while
I'm here, add some #ifdef so wskbd does not depend on wsdisplay (pointed out
to me by fgsch@).


# 1.6 30-Jan-2001 aaron

- Add some checks for (sc != NULL). Missed by me previously. Fixes boot -c.
- Remove static.


# 1.5 30-Jan-2001 aaron

WSMOUSEDEVCF_MUX_DEFAULT -> WSKBDDEVCF_MUX_DEFAULT, although this didn't
matter much since they are the same value, but confusing nonetheless.


# 1.4 24-Nov-2000 aaron

Better key repeat logic. When we're in repeat mode, with the exception of
modifier keys (SHIFT, CTRL, ALT, etc.), and a key "up" event is received,
only delete the key repeat timeout if the key that generated the "up"
event is the key currently being repeated. When a key "down" event is
received, the opposite -- only delete the key repeat if the key that generated
the "down" event is _not_ the key currently being repeated.

Playing here is a bit dangerous since we are constraining the conditions in
which a timeout(9) is deleted (we don't want to mistakenly get stuck in a
non-legitamite key repeat).

Using timeout(9) for this is kind of gross. I guess it is done here to avoid
key repeat code in all of the actual hardware keyboard drivers.

There is still a bug here. If you have a key held down and detach your
keyboard (by either unplugging it or doing a KVM switch), the timeout is not
deleted and the key continues to repeat until it gets the keyboard back and
receives an interrupt. Perhaps we should not be ignoring typematic keys after
all?


# 1.3 15-Nov-2000 aaron

Support console scrollback in wscons through a new "scrollback" accessop that
hooks into the lower-level display driver. The updated vga(4) driver I'm about
to commit has support for this. This is a fairly unobtrusive way to implement
scrollback while maintaining the abstraction of the wscons system.


Revision tags: OPENBSD_2_8_BASE
# 1.2 01-Aug-2000 mickey

branches: 1.2.2;
update wscons, nothing serious, mostly api fixes for us; from netbsd through aaron@


# 1.1 16-May-2000 mickey

import newer wscons source from netbsd.
art@ said it will make his life easier in alpha
rhands@ said it is not used in powerpc for now
bjc@ said that it would make vax port easier
mickey@ said other platforms can use it, and it's faster