xref: /illumos-gate/usr/src/uts/common/sys/kbd.h (revision 2570281cf351044b6936651ce26dbe1f801dcbd8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_KBD_H
27 #define	_SYS_KBD_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 /*
34  * Following #defines are related to the older keyboards which
35  * are no longer supported by kb module.  The #defines ane left
36  * for older programs to still compile.
37  */
38 #define	KB_KLUNK	0x00		/* Micro Switch 103SD32-2 */
39 #define	KB_VT100	0x01		/* Keytronics VT100 compatible */
40 #define	KB_SUN2		0x02		/* Sun-2 custom keyboard */
41 #define	KB_VT220	0x81		/* Emulation VT220 */
42 #define	KB_VT220I	0x82		/* International VT220 Emulation */
43 
44 #define	NOTPRESENT	0xFF		/* Keyboard is not plugged in */
45 #define	KBD_CMD_LED1	0x04		/* Turn on LED 1 for Sun-2 */
46 #define	KBD_CMD_NOLED1	0x05		/* Turn off LED 1 for Sun-2 */
47 #define	KBD_CMD_LED2	0x06		/* Turn on LED 2 for Sun-2 */
48 #define	KBD_CMD_NOLED2	0x07		/* Turn off LED 2 for Sun-2 */
49 
50 #define	CTLSMASK	0x0100		/* Set if ^S was last keyed of ^S, ^Q */
51 					/* determines which NOSCROLL sends. */
52 
53 #define	NOSCROLL	0x303	/* This key alternately sends ^S or ^Q */
54 #define	CTRLS		0x304	/* This sends ^S and lets NOSCROLL know */
55 #define	CTRLQ		0x305	/* This sends ^Q and lets NOSCROLL know */
56 
57 
58 /*
59  * Following are the only keyboard types supported by kb module.
60  * (Type 5, Hobo, US101A are also supported but they report
61  * themselves as Type 4 keyboard with a different layout id.)
62  */
63 #define	KB_SUN3		3		/* Type 3 Sun keyboard */
64 #define	KB_SUN4		4		/* Type 4 Sun keyboard */
65 #define	KB_USB		6		/* USB keyboard */
66 #define	KB_PC		101		/* Type 101 AT keyboard */
67 
68 #define	KB_ASCII	0x0F		/* Ascii terminal masquerading as kbd */
69 
70 /*
71  * This structure is used to enumerate the supported keyboard types.
72  * The array of these is terminated by an entry with a NULL table entry.
73  * The first entry is used if none match.
74  */
75 struct keyboards {
76 	int		id;	/* Keyboard type, per KIOCTYPE */
77 	struct keyboard	*table;	/* Keyboard table to use. */
78 };
79 
80 /*
81  * Various special characters that might show up on the port
82  */
83 #define	IDLEKEY		0x7F		/* Keyboard is idle; no keys down */
84 #define	ERRORKEY	0x7E		/* Keyboard detected an error */
85 #define	RESETKEY	0xFF		/* Keyboard was just reset */
86 #define	LAYOUTKEY	0xFE		/* Keyboard layout byte follows */
87 
88 #define	PRESSED		0x00		/* 0x80 bit off: key was pressed */
89 #define	RELEASED	0x80		/* 0x80 bit on : key was released */
90 
91 /*
92  * Commands to the Sun-3 keyboard.
93  */
94 #define	KBD_CMD_RESET		0x01	/* Reset keyboard as if power-up */
95 #define	KBD_CMD_BELL		0x02	/* Turn on the bell */
96 #define	KBD_CMD_NOBELL		0x03	/* Turn off the bell */
97 #define	KBD_CMD_CLICK		0x0A	/* Turn on the click annunciator */
98 #define	KBD_CMD_NOCLICK		0x0B	/* Turn off the click annunciator */
99 
100 /*
101  * Commands to the Type 4 keyboard, in addition to those above.
102  */
103 #define	KBD_CMD_AUTOTEST	0x0C	/* Initiate test sequence */
104 #define	KBD_CMD_SETLED		0x0E	/* Set keyboard LED's */
105 #define	KBD_CMD_GETLAYOUT	0x0F	/* Request that keyboard indicate */
106 					/* layout */
107 /*
108  * Type 4 keyboard LED masks (used to set LED's)
109  */
110 #define	LED_NUM_LOCK	0x1
111 #define	LED_COMPOSE	0x2
112 #define	LED_SCROLL_LOCK 0x4
113 #define	LED_CAPS_LOCK	0x8
114 #define	LED_KANA	0x10		/* Valid only on Japanese layout */
115 
116 /*
117  * Software related definitions
118  */
119 /*
120  * These are the states that the keyboard scanner can be in.
121  *
122  * It starts out in NORMAL state.
123  */
124 #define	NORMAL		0		/* The usual (ho, hum) */
125 #define	ABORT1		1		/* Got KEYABORT1 */
126 #define	COMPOSE1	2		/* Got COMPOSE */
127 #define	COMPOSE2	3		/* Got COMPOSE plus first key */
128 #define	FLTACCENT	4		/* Got floating accent key */
129 #define	NEWABORT1	5		/* Got NEW KEYABORT1 */
130 
131 /*
132  * Size of ASCII set as used in compose handling.
133  */
134 #define	ASCII_SET_SIZE	128
135 
136 /*
137  * These are how you can have your input translated.
138  * TR_EVENT means that each keystroke is sent as a firm event.
139  * TR_UNTRANS_EVENT also sends a firm event for each up / down transition,
140  * but the value is untranslated: the event id is the key station; the
141  * value indicates whether the transition was up or down; the value of the
142  * shift-mask is undefined.
143  */
144 #define	TR_NONE		  0
145 #define	TR_ASCII	  1
146 #define	TR_EVENT	  2
147 #define	TR_UNTRANS_EVENT  3
148 
149 /*
150  * These bits can appear in the result of TR_NONE & TR_UNTRANS_EVENT getkey()s.
151  */
152 #define	BUILDKEY(key, state)	(key | state)
153 #define	STATEOF(key)	((key) & RELEASED)	/* 0 = key down, !=0 = key up */
154 #define	KEYOF(key)	((key) & ~RELEASED)	/* The key number that moved */
155 #define	NOKEY		(-1)		/* The argument was 0, and no key was */
156 					/* depressed.  They were all elated. */
157 
158 /*
159  * "Bucky" bits.  These are bits for mode keys.	 The META bit is ORed into the
160  * result of TR_ASCII getkey()s, and can be ORed into the result of TR_EVENT
161  * getkey()s for backwards compatibility.
162  * (NOKEY can also appear if no keypress was queued up.)
163  */
164 #define	METABIT		0		/* Meta key depressed with key */
165 #define	METAMASK	0x000080
166 #define	SYSTEMBIT	1		/* Upper left key was down w/key */
167 #define	SYSTEMMASK	0x000100
168 /* other "bucky" bits can be defined at will.  See "BUCKYBITS" below. */
169 
170 /*
171  * This defines the bit positions used within "shiftmask" to
172  * indicate the "pressed" (1) or "released" (0) state of shift keys.
173  * Both the bit numbers, and the aggregate masks, are defined.
174  *
175  * The "UPMASK" is a minor kludge.  Since whether the key is going
176  * up or down determines the translation table (just as the shift
177  * keys' positions do), we OR it with "shiftmask" to get "tempmask",
178  * which is the mask which is actually used to determine the
179  * translation table to use.  Don't reassign 0x0080 for anything
180  * else, or we'll have to shift and such to squeeze in UPMASK,
181  * since it comes in from the hardware as 0x80.
182  */
183 #define	CAPSLOCK	0		/* Caps Lock key */
184 #define	CAPSMASK	0x0001
185 #define	SHIFTLOCK	1		/* Shift Lock key */
186 #define	LEFTSHIFT	2		/* Left-hand shift key */
187 #define	RIGHTSHIFT	3		/* Right-hand shift key */
188 #define	SHIFTMASK	0x000E
189 #define	LEFTCTRL	4		/* Left-hand (or only) control key */
190 #define	RIGHTCTRL	5		/* Right-hand control key */
191 #define	CTRLMASK	0x0030
192 /*	META		6		Meta keys */
193 /*	META_SHIFT_MASK 0x0040		reserved */
194 /*	TOP		7		do not use! */
195 /*	TOPMASK		0x0080		UPMASK in keyboard driver */
196 /*	CMD		8		reserved */
197 /*	CMDMASK		0x0100		reserved */
198 #define	ALTGRAPH	9		/* Alt Graph key */
199 #define	ALTGRAPHMASK	0x0200
200 #define	ALT		10		/* Left (or only) Alt key */
201 #define	LEFTALT		10		/* Left Alt key */
202 #define	ALTMASK		0x1400
203 #define	NUMLOCK		11		/* Num Lock key */
204 #define	NUMLOCKMASK	0x0800
205 #define	RIGHTALT	12		/* Right Alt key */
206 #define	UPMASK		0x0080
207 
208 /*
209  * This defines the format of translation tables.
210  *
211  * A translation table is KEYMAP_SIZE "entries", each of which is 2 bytes
212  * (unsigned shorts).  The top 8 bits of each entry are decoded by
213  * a case statement in getkey.c.  If the entry is less than 0x100, it
214  * is sent out as an EUC character (possibly with bucky bits
215  * OR-ed in).  "Special" entries are 0x100 or greater, and
216  * invoke more complicated actions.
217  *
218  * The KEYMAP_SIZE is dependent upon the keyboard type.  For example, the
219  * Sun Type 4/5 keyboards have a KEYMAP_SIZE of 128 where a USB keyboard
220  * has a KEYMAP_SIZE of 255.  Instead of defining a KEYMAP_SIZE per
221  * keyboard type, a keyboard specific module/driver may supply the value
222  * at run time by defining the KEYMAP_SIZE_VARIABLE and filling in the
223  * keyboard struct appropriately.
224  */
225 
226 typedef unsigned int keymap_entry_t;
227 #ifdef KEYMAP_SIZE_VARIABLE
228 
229 
230 #else
231 #define	KEYMAP_SIZE	128
232 
233 struct keymap {
234 	keymap_entry_t	keymap[KEYMAP_SIZE];	/* maps keycodes to actions */
235 };
236 
237 #endif
238 
239 /*
240  * This structure is used for "exception" cases for key sequences that
241  * we want to map, that should not be handled by keymap entries (For
242  * example: using Control-Shift-F1 on PC's for the compose key).
243  */
244 struct exception_map {
245 	/*
246 	 * these are the modifier keys that we "care" about
247 	 */
248 	unsigned int exc_care;
249 
250 	/*
251 	 * This is the mask of modifier keys that we want to match
252 	 */
253 	unsigned int exc_mask;
254 
255 	/*
256 	 * This is the key that we want to match.
257 	 */
258 	int exc_key;
259 
260 	/*
261 	 * This is our translated version of the matching sequence.
262 	 */
263 	keymap_entry_t exc_entry;
264 };
265 
266 /*
267  * A keyboard is defined by its keymaps and what state it resets at idle.
268  *
269  * The masks k_idleshifts and k_idlebuckys are AND-ed with the current
270  * state of  shiftmask	and  buckybits	when a "keyboard idle" code
271  * is received.	 This ensures that where we "think" the shift & bucky
272  * keys are, more accurately reflects where they really are, since the
273  * keyboard knows better than us.  However, some keyboards don't know
274  * about shift states that should be remembered across idles.  Such
275  * shifts are described by k_toggleshifts.  k_toggleshifts are used to
276  * identify such shifts.  A toggle shift state is maintained separately
277  * from the general shift state.  The toggle shift state is OR-ed
278  * with the state general shift state when an idle is received.
279  * k_toggleshifts should not appear in the k_up table.
280  */
281 struct keyboard {
282 #ifdef KEYMAP_SIZE_VARIABLE
283 	int		k_keymap_size;	/* Number of entries in keymaps */
284 	keymap_entry_t	*k_normal;	/* Unshifted */
285 	keymap_entry_t	*k_shifted;	/* Shifted */
286 	keymap_entry_t	*k_caps;	/* Caps locked */
287 	keymap_entry_t	*k_altgraph;	/* Alt Graph down */
288 	keymap_entry_t	*k_numlock;	/* Num Lock down */
289 	keymap_entry_t	*k_control;	/* Controlled */
290 	keymap_entry_t	*k_up;		/* Key went up */
291 #else
292 	struct keymap	*k_normal;	/* Unshifted */
293 	struct keymap	*k_shifted;	/* Shifted */
294 	struct keymap	*k_caps;	/* Caps locked */
295 	struct keymap	*k_altgraph;	/* Alt Graph down */
296 	struct keymap	*k_numlock;	/* Num Lock down */
297 	struct keymap	*k_control;	/* Controlled */
298 	struct keymap	*k_up;		/* Key went up */
299 #endif
300 	int		k_idleshifts;	/* Shifts that keep across idle */
301 	int		k_idlebuckys;	/* Bucky bits that keep across idle */
302 	unsigned char	k_abort1;	/* 1st key of abort sequence */
303 	unsigned char	k_abort1a;	/* alternate 1st key */
304 	unsigned char	k_abort2;	/* 2nd key of abort sequence */
305 	int		k_toggleshifts; /* Shifts that toggle on down from */
306 					/* kbd and keep across idle */
307 	struct exception_map *k_except;	/* Oddball cases */
308 	unsigned char	k_newabort1;	/* 1st key of new abort sequence */
309 	unsigned char	k_newabort1a;	/* alternate 1st key */
310 	unsigned char	k_newabort2;	/* 2nd key of new abort sequence */
311 };
312 
313 /*
314  * Define the compose sequence structure.  First and second
315  * ASCII chars of 0 indicate the end of the table.
316  */
317 struct compose_sequence_t {
318 	unsigned char	first;	/* first ASCII char after COMPOSE key */
319 	unsigned char	second; /* second ASCII char after COMPOSE key */
320 	keymap_entry_t	utf8;	/* equivalent UTF-8 code */
321 };
322 
323 /*
324  * Define the floating accent sequence structure.
325  */
326 struct fltaccent_sequence_t {
327 	keymap_entry_t	fa_entry;	/* floating accent keymap entry */
328 	unsigned char	ascii;		/* ASCII char after FA-type key */
329 	keymap_entry_t	utf8;		/* equivalent UTF-8 code */
330 };
331 
332 /*
333  * The top byte is used to store the flags, leaving 24 bits for char.
334  */
335 #define	KEYCHAR(c)	((c) & 0x00ffffff)
336 #define	KEYFLAGS(c)	((c) & ~0x00ffffff)
337 
338 /*
339  * The "special" entries' top 4 bits are defined below.	 Generally they are
340  * used with a 4-bit parameter (such as a bit number) in the low 4 bits.
341  * The bytes whose top 4 bits are 0x0 thru 0x7 happen to be ascii
342  * characters.	They are not special cased, but just normal cased.
343  */
344 
345 #define	SPECIAL(h, l)	(((h) << 24) | (l))
346 
347 #define	SHIFTKEYS SPECIAL(0x1, 0)
348 				/* thru 0x10F.	This key helps to determine */
349 				/* the translation table used.	The bit */
350 				/* position of its bit in "shiftmask" */
351 				/* is added to the entry, eg */
352 				/* SHIFTKEYS+LEFTCTRL.	When this entry is */
353 				/* invoked, the bit in "shiftmask" is */
354 				/* toggled.  Depending which tables you put */
355 				/* it in, this works well for hold-down */
356 				/* keys or press-on, press-off keys.  */
357 #define	BUCKYBITS SPECIAL(0x2, 0)
358 				/* thru 0x20F.	This key determines the state */
359 				/* of one of the "bucky" bits above the */
360 				/* returned ASCII character.  This is */
361 				/* basically a way to pass mode-key-up/down */
362 				/* information back to the caller with each */
363 				/* "real" key depressed.  The concept, and */
364 				/* name "bucky" (derivation unknown) comes */
365 				/* from the MIT/SAIL "TV" system...they had */
366 				/* TOP, META, CTRL, and a few other bucky */
367 				/* bits.  The bit position of its bit in */
368 				/* "buckybits", minus 7, is added to the */
369 				/* entry; eg bit 0x00000400 is BUCKYBITS+3. */
370 				/* The "-7" prevents us from messing up the */
371 				/* ASCII char, and gives us 16 useful bucky */
372 				/* bits.  When this entry is invoked, */
373 				/* the designated bit in "buckybits" is */
374 				/* toggled.  Depending which tables you put */
375 				/* it in, this works well for hold-down */
376 				/* keys or press-on, press-off keys.  */
377 #define	FUNNY SPECIAL(0x3, 0)	/* thru 0x30F.	This key does one of 16 funny */
378 				/* things based on the low 4 bits: */
379 #define	NOP SPECIAL(0x3, 0x0)	/* This key does nothing. */
380 #define	OOPS SPECIAL(0x3, 0x1) /* This key exists but is undefined. */
381 #define	HOLE SPECIAL(0x3, 0x2) /* This key does not exist on the keyboard. */
382 				/* Its position code should never be */
383 				/* generated.  This indicates a software/ */
384 				/* hardware mismatch, or bugs. */
385 #define	RESET SPECIAL(0x3, 0x6) /* Kbd was just reset */
386 #define	ERROR SPECIAL(0x3, 0x7) /* Kbd just detected an internal error */
387 #define	IDLE SPECIAL(0x3, 0x8) /* Kbd is idle (no keys down) */
388 #define	COMPOSE SPECIAL(0x3, 0x9) /* This key is the Compose key. */
389 #define	NONL SPECIAL(0x3, 0xA) /* This key not affected by Num Lock */
390 /* Combinations 0x30B to 0x30F are reserved for non-parameterized functions */
391 
392 #define	FA_CLASS SPECIAL(0x4, 0)
393 				/* thru 0x40F.	These are for "floating */
394 				/* accent" characters.	The low-order 4 bits */
395 				/* select one of those characters. */
396 /* Definitions for the individual floating accents: */
397 #define	FA_UMLAUT SPECIAL(0x4, 0x0)	/* umlaut accent */
398 #define	FA_CFLEX SPECIAL(0x4, 0x1)	/* circumflex accent */
399 #define	FA_TILDE SPECIAL(0x4, 0x2)	/* tilde accent */
400 #define	FA_CEDILLA SPECIAL(0x4, 0x3)	/* cedilla accent */
401 #define	FA_ACUTE SPECIAL(0x4, 0x4)	/* acute accent */
402 #define	FA_GRAVE SPECIAL(0x4, 0x5)	/* grave accent */
403 #define	FA_MACRON SPECIAL(0x4, 0x6)	/* macron accent */
404 #define	FA_BREVE SPECIAL(0x4, 0x7)	/* breve accent */
405 #define	FA_DOT SPECIAL(0x4, 0x8)	/* dot accent */
406 #define	FA_SLASH SPECIAL(0x4, 0x9)	/* slash accent */
407 #define	FA_RING SPECIAL(0x4, 0xa)	/* ring accent */
408 #define	FA_APOSTROPHE SPECIAL(0x4, 0xb)	/* apostrophe accent */
409 #define	FA_DACUTE SPECIAL(0x4, 0xc)	/* double acute accent */
410 #define	FA_OGONEK SPECIAL(0x4, 0xd)	/* ogonek accent */
411 #define	FA_CARON SPECIAL(0x4, 0xe)	/* caron accent */
412 
413 #define	STRING SPECIAL(0x5, 0)	/* thru 0x50F.	The low-order 4 bits index */
414 				/* a table select a string to be returned, */
415 				/* char by char.  Each entry the table is */
416 				/* null terminated. */
417 #define	KTAB_STRLEN	10	/* Maximum string length (including null) */
418 /* Definitions for the individual string numbers: */
419 #define	HOMEARROW	0x00
420 #define	UPARROW		0x01
421 #define	DOWNARROW	0x02
422 #define	LEFTARROW	0x03
423 #define	RIGHTARROW	0x04
424 /* string numbers 5 thru F are available to users making custom entries */
425 
426 /*
427  * In the following function key groupings, the low-order 4 bits indicate
428  * the function key number within the group, and the next 4 bits indicate
429  * the group.
430  */
431 #define	FUNCKEYS	SPECIAL(0x6, 0)
432 #define	LEFTFUNC SPECIAL(0x6, 0x0)	/* thru 0x60F.	The "left" group. */
433 #define	RIGHTFUNC SPECIAL(0x6, 0x10)	/* thru 0x61F.	The "right" group. */
434 #define	TOPFUNC SPECIAL(0x6, 0x20)	/* thru 0x62F.	The "top" group. */
435 #define	BOTTOMFUNC SPECIAL(0x6, 0x30)	/* thru 0x63F.	The "bottom" group. */
436 #define	LF(n)		(LEFTFUNC+(n)-1)
437 #define	RF(n)		(RIGHTFUNC+(n)-1)
438 #define	TF(n)		(TOPFUNC+(n)-1)
439 #define	BF(n)		(BOTTOMFUNC+(n)-1)
440 
441 /*
442  * The actual keyboard positions may not be on the left/right/top/bottom
443  * of the physical keyboard (although they usually are).
444  * What is important is that we have reserved 64 keys for function keys.
445  *
446  * Normally, striking a function key will cause the following escape sequence
447  * to be sent through the character stream:
448  *	ESC[0..9z
449  * where ESC is a single escape character and 0..9 indicate some number of
450  * digits needed to encode the function key as a decimal number.
451  */
452 #define	PADKEYS		SPECIAL(0x7, 0)
453 #define	PADEQUAL SPECIAL(0x7, 0x00)		/* keypad = */
454 #define	PADSLASH SPECIAL(0x7, 0x01)		/* keypad / */
455 #define	PADSTAR SPECIAL(0x7, 0x02)		/* keypad * */
456 #define	PADMINUS SPECIAL(0x7, 0x03)		/* keypad - */
457 #define	PADSEP SPECIAL(0x7, 0x04)		/* keypad,  */
458 #define	PAD7 SPECIAL(0x7, 0x05)		/* keypad 7 */
459 #define	PAD8 SPECIAL(0x7, 0x06)		/* keypad 8 */
460 #define	PAD9 SPECIAL(0x7, 0x07)		/* keypad 9 */
461 #define	PADPLUS SPECIAL(0x7, 0x08)		/* keypad + */
462 #define	PAD4 SPECIAL(0x7, 0x09)		/* keypad 4 */
463 #define	PAD5 SPECIAL(0x7, 0x0A)		/* keypad 5 */
464 #define	PAD6 SPECIAL(0x7, 0x0B)		/* keypad 6 */
465 #define	PAD1 SPECIAL(0x7, 0x0C)		/* keypad 1 */
466 #define	PAD2 SPECIAL(0x7, 0x0D)		/* keypad 2 */
467 #define	PAD3 SPECIAL(0x7, 0x0E)		/* keypad 3 */
468 #define	PAD0 SPECIAL(0x7, 0x0F)		/* keypad 0 */
469 #define	PADDOT SPECIAL(0x7, 0x10)		/* keypad . */
470 #define	PADENTER SPECIAL(0x7, 0x11)		/* keypad Enter */
471 
472 #ifdef	__cplusplus
473 }
474 #endif
475 
476 #endif	/* _SYS_KBD_H */
477