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