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