xref: /freebsd/sys/dev/kbd/kbdreg.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _DEV_KBD_KBDREG_H_
32 #define _DEV_KBD_KBDREG_H_
33 
34 #ifdef _KERNEL
35 
36 #include "opt_kbd.h"			/* KBD_DELAY* */
37 
38 /* forward declarations */
39 typedef struct keyboard keyboard_t;
40 struct keymap;
41 struct accentmap;
42 struct fkeytab;
43 struct cdevsw;
44 
45 /* call back funcion */
46 typedef int		kbd_callback_func_t(keyboard_t *kbd, int event,
47 					    void *arg);
48 
49 /* keyboard function table */
50 typedef int		kbd_probe_t(int unit, void *arg, int flags);
51 typedef int		kbd_init_t(int unit, keyboard_t **kbdp, void *arg,
52 				   int flags);
53 typedef int		kbd_term_t(keyboard_t *kbd);
54 typedef int		kbd_intr_t(keyboard_t *kbd, void *arg);
55 typedef int		kbd_test_if_t(keyboard_t *kbd);
56 typedef int		kbd_enable_t(keyboard_t *kbd);
57 typedef int		kbd_disable_t(keyboard_t *kbd);
58 typedef int		kbd_read_t(keyboard_t *kbd, int wait);
59 typedef int		kbd_check_t(keyboard_t *kbd);
60 typedef u_int		kbd_read_char_t(keyboard_t *kbd, int wait);
61 typedef int		kbd_check_char_t(keyboard_t *kbd);
62 typedef int		kbd_ioctl_t(keyboard_t *kbd, u_long cmd, caddr_t data);
63 typedef int		kbd_lock_t(keyboard_t *kbd, int lock);
64 typedef void		kbd_clear_state_t(keyboard_t *kbd);
65 typedef int		kbd_get_state_t(keyboard_t *kbd, void *buf, size_t len);
66 typedef int		kbd_set_state_t(keyboard_t *kbd, void *buf, size_t len);
67 typedef u_char		*kbd_get_fkeystr_t(keyboard_t *kbd, int fkey,
68 					   size_t *len);
69 typedef int		kbd_poll_mode_t(keyboard_t *kbd, int on);
70 typedef void		kbd_diag_t(keyboard_t *kbd, int level);
71 
72 /* event types */
73 #define KBDIO_KEYINPUT	0
74 #define KBDIO_UNLOADING	1
75 
76 typedef struct keyboard_callback {
77 	kbd_callback_func_t *kc_func;
78 	void		*kc_arg;
79 } keyboard_callback_t;
80 
81 typedef struct keyboard_switch {
82 	kbd_probe_t	*probe;
83 	kbd_init_t	*init;
84 	kbd_term_t	*term;
85 	kbd_intr_t	*intr;
86 	kbd_test_if_t	*test_if;
87 	kbd_enable_t	*enable;
88 	kbd_disable_t	*disable;
89 	kbd_read_t	*read;
90 	kbd_check_t	*check;
91 	kbd_read_char_t	*read_char;
92 	kbd_check_char_t *check_char;
93 	kbd_ioctl_t	*ioctl;
94 	kbd_lock_t	*lock;
95 	kbd_clear_state_t *clear_state;
96 	kbd_get_state_t	*get_state;
97 	kbd_set_state_t	*set_state;
98 	kbd_get_fkeystr_t *get_fkeystr;
99 	kbd_poll_mode_t *poll;
100 	kbd_diag_t	*diag;
101 } keyboard_switch_t;
102 
103 /*
104  * Keyboard driver definition.  Some of these be immutable after definition
105  * time, e.g. one shouldn't be able to rename a driver or use a different kbdsw
106  * entirely, but patching individual methods is acceptable.
107  */
108 typedef struct keyboard_driver {
109     SLIST_ENTRY(keyboard_driver) link;
110     const char * const		name;
111     keyboard_switch_t * const	kbdsw;
112     /* backdoor for the console driver */
113     int				(* const configure)(int);
114     int				flags;
115 } keyboard_driver_t;
116 
117 #define	KBDF_REGISTERED		0x0001
118 
119 /* keyboard */
120 struct keyboard {
121 	/* the following fields are managed by kbdio */
122 	int		kb_index;	/* kbdio index# */
123 	int		kb_minor;	/* minor number of the sub-device */
124 	int		kb_flags;	/* internal flags */
125 #define KB_VALID	(1 << 16)	/* this entry is valid */
126 #define KB_NO_DEVICE	(1 << 17)	/* device not present */
127 #define KB_PROBED	(1 << 18)	/* device probed */
128 #define KB_INITIALIZED	(1 << 19)	/* device initialized */
129 #define KB_REGISTERED	(1 << 20)	/* device registered to kbdio */
130 #define KB_BUSY		(1 << 21)	/* device used by a client */
131 #define KB_POLLED	(1 << 22)	/* device is polled */
132 	int		kb_active;	/* 0: inactive */
133 	void		*kb_token;	/* id of the current client */
134 	keyboard_callback_t kb_callback;/* callback function */
135 
136 	/*
137 	 * Device configuration flags:
138 	 * The upper 16 bits are common between various keyboard devices.
139 	 * The lower 16 bits are device-specific.
140 	 */
141 	int		kb_config;
142 #define KB_CONF_PROBE_ONLY (1 << 16)	/* probe only, don't initialize */
143 
144 	/* the following fields are set up by the driver */
145 	char		*kb_name;	/* driver name */
146 	int		kb_unit;	/* unit # */
147 	int		kb_type;	/* KB_84, KB_101, KB_OTHER,... */
148 	int		kb_io_base;	/* port# if any */
149 	int		kb_io_size;	/* # of occupied port */
150 	int		kb_led;		/* LED status */
151 	struct keymap	*kb_keymap;	/* key map */
152 	struct accentmap *kb_accentmap;	/* accent map */
153 	struct fkeytab	*kb_fkeytab;	/* function key strings */
154 	int		kb_fkeytab_size;/* # of function key strings */
155 	void		*kb_data;	/* the driver's private data */
156 	int		kb_delay1;
157 	int		kb_delay2;
158 #ifndef KBD_DELAY1
159 #define KBD_DELAY1	500
160 #endif
161 #ifndef KBD_DELAY2
162 #define KBD_DELAY2	100
163 #endif
164 	unsigned long	kb_count;	/* # of processed key strokes */
165 	u_char		kb_lastact[NUM_KEYS/2];
166 	struct cdev *kb_dev;
167 	const keyboard_driver_t	*kb_drv;
168 };
169 
170 #define KBD_IS_VALID(k)		((k)->kb_flags & KB_VALID)
171 #define KBD_VALID(k)		((k)->kb_flags |= KB_VALID)
172 #define KBD_INVALID(k)		((k)->kb_flags &= ~KB_VALID)
173 #define KBD_HAS_DEVICE(k)	(!((k)->kb_flags & KB_NO_DEVICE))
174 #define KBD_FOUND_DEVICE(k)	((k)->kb_flags &= ~KB_NO_DEVICE)
175 #define KBD_IS_PROBED(k)	((k)->kb_flags & KB_PROBED)
176 #define KBD_PROBE_DONE(k)	((k)->kb_flags |= KB_PROBED)
177 #define KBD_IS_INITIALIZED(k)	((k)->kb_flags & KB_INITIALIZED)
178 #define KBD_INIT_DONE(k)	((k)->kb_flags |= KB_INITIALIZED)
179 #define KBD_IS_CONFIGURED(k)	((k)->kb_flags & KB_REGISTERED)
180 #define KBD_CONFIG_DONE(k)	((k)->kb_flags |= KB_REGISTERED)
181 #define KBD_IS_BUSY(k)		((k)->kb_flags & KB_BUSY)
182 #define KBD_BUSY(k)		((k)->kb_flags |= KB_BUSY)
183 #define KBD_UNBUSY(k)		((k)->kb_flags &= ~KB_BUSY)
184 #define KBD_IS_POLLED(k)	((k)->kb_flags & KB_POLLED)
185 #define KBD_POLL(k)		((k)->kb_flags |= KB_POLLED)
186 #define KBD_UNPOLL(k)		((k)->kb_flags &= ~KB_POLLED)
187 #define KBD_IS_ACTIVE(k)	((k)->kb_active)
188 #define KBD_ACTIVATE(k)		(++(k)->kb_active)
189 #define KBD_DEACTIVATE(k)	(--(k)->kb_active)
190 #define KBD_LED_VAL(k)		((k)->kb_led)
191 
192 /*
193  * Keyboard disciplines: call actual handlers via kbdsw[].
194  */
195 static __inline int
196 kbdd_probe(keyboard_t *kbd, int unit, void *arg, int flags)
197 {
198 
199 	return ((*kbd->kb_drv->kbdsw->probe)(unit, arg, flags));
200 }
201 
202 static __inline int
203 kbdd_init(keyboard_t *kbd, int unit, keyboard_t **kbdpp, void *arg, int flags)
204 {
205 
206 	return ((*kbd->kb_drv->kbdsw->init)(unit, kbdpp, arg, flags));
207 }
208 
209 static __inline int
210 kbdd_term(keyboard_t *kbd)
211 {
212 
213 	return ((*kbd->kb_drv->kbdsw->term)(kbd));
214 }
215 
216 static __inline int
217 kbdd_intr(keyboard_t *kbd, void *arg)
218 {
219 
220 	return ((*kbd->kb_drv->kbdsw->intr)(kbd, arg));
221 }
222 
223 static __inline int
224 kbdd_test_if(keyboard_t *kbd)
225 {
226 
227 	return ((*kbd->kb_drv->kbdsw->test_if)(kbd));
228 }
229 
230 static __inline int
231 kbdd_enable(keyboard_t *kbd)
232 {
233 
234 	return ((*kbd->kb_drv->kbdsw->enable)(kbd));
235 }
236 
237 static __inline int
238 kbdd_disable(keyboard_t *kbd)
239 {
240 
241 	return ((*kbd->kb_drv->kbdsw->disable)(kbd));
242 }
243 
244 static __inline int
245 kbdd_read(keyboard_t *kbd, int wait)
246 {
247 
248 	return ((*kbd->kb_drv->kbdsw->read)(kbd, wait));
249 }
250 
251 static __inline int
252 kbdd_check(keyboard_t *kbd)
253 {
254 
255 	return ((*kbd->kb_drv->kbdsw->check)(kbd));
256 }
257 
258 static __inline u_int
259 kbdd_read_char(keyboard_t *kbd, int wait)
260 {
261 
262 	return ((*kbd->kb_drv->kbdsw->read_char)(kbd, wait));
263 }
264 
265 static __inline int
266 kbdd_check_char(keyboard_t *kbd)
267 {
268 
269 	return ((*kbd->kb_drv->kbdsw->check_char)(kbd));
270 }
271 
272 static __inline int
273 kbdd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data)
274 {
275 
276 	if (kbd == NULL)
277 		return (ENODEV);
278 	return ((*kbd->kb_drv->kbdsw->ioctl)(kbd, cmd, data));
279 }
280 
281 static __inline int
282 kbdd_lock(keyboard_t *kbd, int lock)
283 {
284 
285 	return ((*kbd->kb_drv->kbdsw->lock)(kbd, lock));
286 }
287 
288 static __inline void
289 kbdd_clear_state(keyboard_t *kbd)
290 {
291 
292 	(*kbd->kb_drv->kbdsw->clear_state)(kbd);
293 }
294 
295 static __inline int
296 kbdd_get_state(keyboard_t *kbd, void *buf, int len)
297 {
298 
299 	return ((*kbd->kb_drv->kbdsw->get_state)(kbd, buf, len));
300 }
301 
302 static __inline int
303 kbdd_set_state(keyboard_t *kbd, void *buf, int len)
304 {
305 
306 	return ((*kbd->kb_drv->kbdsw->set_state)(kbd, buf, len));
307 }
308 
309 static __inline u_char *
310 kbdd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
311 {
312 
313 	return ((*kbd->kb_drv->kbdsw->get_fkeystr)(kbd, fkey, len));
314 }
315 
316 static __inline int
317 kbdd_poll(keyboard_t *kbd, int on)
318 {
319 
320 	return ((*kbd->kb_drv->kbdsw->poll)(kbd, on));
321 }
322 
323 static __inline void
324 kbdd_diag(keyboard_t *kbd, int level)
325 {
326 
327 	(*kbd->kb_drv->kbdsw->diag)(kbd, level);
328 }
329 
330 #define KEYBOARD_DRIVER(name, sw, config)		\
331 	static struct keyboard_driver name##_kbd_driver = { \
332 		{ NULL }, #name, &sw, config		\
333 	};						\
334 	DATA_SET(kbddriver_set, name##_kbd_driver);
335 
336 /* functions for the keyboard driver */
337 int			kbd_add_driver(keyboard_driver_t *driver);
338 int			kbd_delete_driver(keyboard_driver_t *driver);
339 int			kbd_register(keyboard_t *kbd);
340 int			kbd_unregister(keyboard_t *kbd);
341 keyboard_switch_t	*kbd_get_switch(char *driver);
342 void			kbd_init_struct(keyboard_t *kbd, char *name, int type,
343 					int unit, int config, int port,
344 					int port_size);
345 void			kbd_set_maps(keyboard_t *kbd, struct keymap *keymap,
346 				     struct accentmap *accmap,
347 				     struct fkeytab *fkeymap, int fkeymap_size);
348 
349 /* functions for the keyboard client */
350 int			kbd_allocate(char *driver, int unit, void *id,
351 				     kbd_callback_func_t *func, void *arg);
352 int			kbd_release(keyboard_t *kbd, void *id);
353 int			kbd_change_callback(keyboard_t *kbd, void *id,
354 				     kbd_callback_func_t *func, void *arg);
355 int			kbd_find_keyboard(char *driver, int unit);
356 int			kbd_find_keyboard2(char *driver, int unit, int index);
357 keyboard_t 		*kbd_get_keyboard(int index);
358 
359 /* a back door for the console driver to tickle the keyboard driver XXX */
360 int			kbd_configure(int flags);
361 			/* see `kb_config' above for flag bit definitions */
362 
363 /* evdev2kbd mappings */
364 void			kbd_ev_event(keyboard_t *kbd, uint16_t type,
365 				    uint16_t code, int32_t value);
366 
367 #ifdef KBD_INSTALL_CDEV
368 
369 /* virtual keyboard cdev driver functions */
370 int			kbd_attach(keyboard_t *kbd);
371 int			kbd_detach(keyboard_t *kbd);
372 
373 #endif /* KBD_INSTALL_CDEV */
374 
375 /* generic low-level keyboard functions */
376 
377 /* shift key state */
378 #define SHIFTS1		(1 << 16)
379 #define SHIFTS2		(1 << 17)
380 #define SHIFTS		(SHIFTS1 | SHIFTS2)
381 #define CTLS1		(1 << 18)
382 #define CTLS2		(1 << 19)
383 #define CTLS		(CTLS1 | CTLS2)
384 #define ALTS1		(1 << 20)
385 #define ALTS2		(1 << 21)
386 #define ALTS		(ALTS1 | ALTS2)
387 #define AGRS1		(1 << 22)
388 #define AGRS2		(1 << 23)
389 #define AGRS		(AGRS1 | AGRS2)
390 #define METAS1		(1 << 24)
391 #define METAS2		(1 << 25)
392 #define METAS		(METAS1 | METAS2)
393 #define NLKDOWN		(1 << 26)
394 #define SLKDOWN		(1 << 27)
395 #define CLKDOWN		(1 << 28)
396 #define ALKDOWN		(1 << 29)
397 #define SHIFTAON	(1 << 30)
398 /* lock key state (defined in sys/kbio.h) */
399 /*
400 #define CLKED		LED_CAP
401 #define NLKED		LED_NUM
402 #define SLKED		LED_SCR
403 #define ALKED		(1 << 3)
404 #define LOCK_MASK	(CLKED | NLKED | SLKED | ALKED)
405 #define LED_CAP		(1 << 0)
406 #define LED_NUM		(1 << 1)
407 #define LED_SCR		(1 << 2)
408 #define LED_MASK	(LED_CAP | LED_NUM | LED_SCR)
409 */
410 
411 /* Initialization for the kbd layer, performed by cninit. */
412 void	kbdinit(void);
413 
414 int 	genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg);
415 int 	genkbd_keyaction(keyboard_t *kbd, int keycode, int up,
416 			 int *shiftstate, int *accents);
417 
418 #endif
419 #endif /* !_DEV_KBD_KBDREG_H_ */
420