xref: /freebsd/sys/dev/kbd/kbdreg.h (revision 3078531de10dcae44b253a35125c949ff4235284)
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 #define KB_DELAY1	500
155 #define KB_DELAY2	100
156 	unsigned long	kb_count;	/* # of processed key strokes */
157 	u_char		kb_lastact[NUM_KEYS/2];
158 	struct cdev *kb_dev;
159 	const keyboard_driver_t	*kb_drv;
160 };
161 
162 #define KBD_IS_VALID(k)		((k)->kb_flags & KB_VALID)
163 #define KBD_VALID(k)		((k)->kb_flags |= KB_VALID)
164 #define KBD_INVALID(k)		((k)->kb_flags &= ~KB_VALID)
165 #define KBD_HAS_DEVICE(k)	(!((k)->kb_flags & KB_NO_DEVICE))
166 #define KBD_FOUND_DEVICE(k)	((k)->kb_flags &= ~KB_NO_DEVICE)
167 #define KBD_IS_PROBED(k)	((k)->kb_flags & KB_PROBED)
168 #define KBD_PROBE_DONE(k)	((k)->kb_flags |= KB_PROBED)
169 #define KBD_IS_INITIALIZED(k)	((k)->kb_flags & KB_INITIALIZED)
170 #define KBD_INIT_DONE(k)	((k)->kb_flags |= KB_INITIALIZED)
171 #define KBD_IS_CONFIGURED(k)	((k)->kb_flags & KB_REGISTERED)
172 #define KBD_CONFIG_DONE(k)	((k)->kb_flags |= KB_REGISTERED)
173 #define KBD_IS_BUSY(k)		((k)->kb_flags & KB_BUSY)
174 #define KBD_BUSY(k)		((k)->kb_flags |= KB_BUSY)
175 #define KBD_UNBUSY(k)		((k)->kb_flags &= ~KB_BUSY)
176 #define KBD_IS_POLLED(k)	((k)->kb_flags & KB_POLLED)
177 #define KBD_POLL(k)		((k)->kb_flags |= KB_POLLED)
178 #define KBD_UNPOLL(k)		((k)->kb_flags &= ~KB_POLLED)
179 #define KBD_IS_ACTIVE(k)	((k)->kb_active)
180 #define KBD_ACTIVATE(k)		(++(k)->kb_active)
181 #define KBD_DEACTIVATE(k)	(--(k)->kb_active)
182 #define KBD_LED_VAL(k)		((k)->kb_led)
183 
184 /*
185  * Keyboard disciplines: call actual handlers via kbdsw[].
186  */
187 static __inline int
188 kbdd_probe(keyboard_t *kbd, int unit, void *arg, int flags)
189 {
190 
191 	return ((*kbd->kb_drv->kbdsw->probe)(unit, arg, flags));
192 }
193 
194 static __inline int
195 kbdd_init(keyboard_t *kbd, int unit, keyboard_t **kbdpp, void *arg, int flags)
196 {
197 
198 	return ((*kbd->kb_drv->kbdsw->init)(unit, kbdpp, arg, flags));
199 }
200 
201 static __inline int
202 kbdd_term(keyboard_t *kbd)
203 {
204 
205 	return ((*kbd->kb_drv->kbdsw->term)(kbd));
206 }
207 
208 static __inline int
209 kbdd_intr(keyboard_t *kbd, void *arg)
210 {
211 
212 	return ((*kbd->kb_drv->kbdsw->intr)(kbd, arg));
213 }
214 
215 static __inline int
216 kbdd_test_if(keyboard_t *kbd)
217 {
218 
219 	return ((*kbd->kb_drv->kbdsw->test_if)(kbd));
220 }
221 
222 static __inline int
223 kbdd_enable(keyboard_t *kbd)
224 {
225 
226 	return ((*kbd->kb_drv->kbdsw->enable)(kbd));
227 }
228 
229 static __inline int
230 kbdd_disable(keyboard_t *kbd)
231 {
232 
233 	return ((*kbd->kb_drv->kbdsw->disable)(kbd));
234 }
235 
236 static __inline int
237 kbdd_read(keyboard_t *kbd, int wait)
238 {
239 
240 	return ((*kbd->kb_drv->kbdsw->read)(kbd, wait));
241 }
242 
243 static __inline int
244 kbdd_check(keyboard_t *kbd)
245 {
246 
247 	return ((*kbd->kb_drv->kbdsw->check)(kbd));
248 }
249 
250 static __inline u_int
251 kbdd_read_char(keyboard_t *kbd, int wait)
252 {
253 
254 	return ((*kbd->kb_drv->kbdsw->read_char)(kbd, wait));
255 }
256 
257 static __inline int
258 kbdd_check_char(keyboard_t *kbd)
259 {
260 
261 	return ((*kbd->kb_drv->kbdsw->check_char)(kbd));
262 }
263 
264 static __inline int
265 kbdd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data)
266 {
267 
268 	if (kbd == NULL)
269 		return (ENODEV);
270 	return ((*kbd->kb_drv->kbdsw->ioctl)(kbd, cmd, data));
271 }
272 
273 static __inline int
274 kbdd_lock(keyboard_t *kbd, int lock)
275 {
276 
277 	return ((*kbd->kb_drv->kbdsw->lock)(kbd, lock));
278 }
279 
280 static __inline void
281 kbdd_clear_state(keyboard_t *kbd)
282 {
283 
284 	(*kbd->kb_drv->kbdsw->clear_state)(kbd);
285 }
286 
287 static __inline int
288 kbdd_get_state(keyboard_t *kbd, void *buf, int len)
289 {
290 
291 	return ((*kbd->kb_drv->kbdsw->get_state)(kbd, buf, len));
292 }
293 
294 static __inline int
295 kbdd_set_state(keyboard_t *kbd, void *buf, int len)
296 {
297 
298 	return ((*kbd->kb_drv->kbdsw->set_state)(kbd, buf, len));
299 }
300 
301 static __inline u_char *
302 kbdd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
303 {
304 
305 	return ((*kbd->kb_drv->kbdsw->get_fkeystr)(kbd, fkey, len));
306 }
307 
308 static __inline int
309 kbdd_poll(keyboard_t *kbd, int on)
310 {
311 
312 	return ((*kbd->kb_drv->kbdsw->poll)(kbd, on));
313 }
314 
315 static __inline void
316 kbdd_diag(keyboard_t *kbd, int level)
317 {
318 
319 	(*kbd->kb_drv->kbdsw->diag)(kbd, level);
320 }
321 
322 #define KEYBOARD_DRIVER(name, sw, config)		\
323 	static struct keyboard_driver name##_kbd_driver = { \
324 		{ NULL }, #name, &sw, config		\
325 	};						\
326 	DATA_SET(kbddriver_set, name##_kbd_driver);
327 
328 /* functions for the keyboard driver */
329 int			kbd_add_driver(keyboard_driver_t *driver);
330 int			kbd_delete_driver(keyboard_driver_t *driver);
331 int			kbd_register(keyboard_t *kbd);
332 int			kbd_unregister(keyboard_t *kbd);
333 keyboard_switch_t	*kbd_get_switch(char *driver);
334 void			kbd_init_struct(keyboard_t *kbd, char *name, int type,
335 					int unit, int config, int port,
336 					int port_size);
337 void			kbd_set_maps(keyboard_t *kbd, struct keymap *keymap,
338 				     struct accentmap *accmap,
339 				     struct fkeytab *fkeymap, int fkeymap_size);
340 
341 /* functions for the keyboard client */
342 int			kbd_allocate(char *driver, int unit, void *id,
343 				     kbd_callback_func_t *func, void *arg);
344 int			kbd_release(keyboard_t *kbd, void *id);
345 int			kbd_change_callback(keyboard_t *kbd, void *id,
346 				     kbd_callback_func_t *func, void *arg);
347 int			kbd_find_keyboard(char *driver, int unit);
348 int			kbd_find_keyboard2(char *driver, int unit, int index);
349 keyboard_t 		*kbd_get_keyboard(int index);
350 
351 /* a back door for the console driver to tickle the keyboard driver XXX */
352 int			kbd_configure(int flags);
353 			/* see `kb_config' above for flag bit definitions */
354 
355 /* evdev2kbd mappings */
356 void			kbd_ev_event(keyboard_t *kbd, uint16_t type,
357 				    uint16_t code, int32_t value);
358 
359 #ifdef KBD_INSTALL_CDEV
360 
361 /* virtual keyboard cdev driver functions */
362 int			kbd_attach(keyboard_t *kbd);
363 int			kbd_detach(keyboard_t *kbd);
364 
365 #endif /* KBD_INSTALL_CDEV */
366 
367 /* generic low-level keyboard functions */
368 
369 /* shift key state */
370 #define SHIFTS1		(1 << 16)
371 #define SHIFTS2		(1 << 17)
372 #define SHIFTS		(SHIFTS1 | SHIFTS2)
373 #define CTLS1		(1 << 18)
374 #define CTLS2		(1 << 19)
375 #define CTLS		(CTLS1 | CTLS2)
376 #define ALTS1		(1 << 20)
377 #define ALTS2		(1 << 21)
378 #define ALTS		(ALTS1 | ALTS2)
379 #define AGRS1		(1 << 22)
380 #define AGRS2		(1 << 23)
381 #define AGRS		(AGRS1 | AGRS2)
382 #define METAS1		(1 << 24)
383 #define METAS2		(1 << 25)
384 #define METAS		(METAS1 | METAS2)
385 #define NLKDOWN		(1 << 26)
386 #define SLKDOWN		(1 << 27)
387 #define CLKDOWN		(1 << 28)
388 #define ALKDOWN		(1 << 29)
389 #define SHIFTAON	(1 << 30)
390 /* lock key state (defined in sys/kbio.h) */
391 /*
392 #define CLKED		LED_CAP
393 #define NLKED		LED_NUM
394 #define SLKED		LED_SCR
395 #define ALKED		(1 << 3)
396 #define LOCK_MASK	(CLKED | NLKED | SLKED | ALKED)
397 #define LED_CAP		(1 << 0)
398 #define LED_NUM		(1 << 1)
399 #define LED_SCR		(1 << 2)
400 #define LED_MASK	(LED_CAP | LED_NUM | LED_SCR)
401 */
402 
403 /* Initialization for the kbd layer, performed by cninit. */
404 void	kbdinit(void);
405 
406 int 	genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg);
407 int 	genkbd_keyaction(keyboard_t *kbd, int keycode, int up,
408 			 int *shiftstate, int *accents);
409 
410 #endif /* !_DEV_KBD_KBDREG_H_ */
411