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