xref: /freebsd/sys/dev/kbd/kbdreg.h (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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  * $Id: kbdreg.h,v 1.2 1999/01/19 11:31:14 yokota Exp $
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 
38 /* call back funcion */
39 typedef int		kbd_callback_func_t(keyboard_t *kbd, int event,
40 					    void *arg);
41 /* event types */
42 #define KBDIO_KEYINPUT	0
43 #define KBDIO_UNLOADING	1
44 
45 typedef struct keyboard_callback {
46 	kbd_callback_func_t *kc_func;
47 	void		*kc_arg;
48 } keyboard_callback_t;
49 
50 /* keyboard */
51 struct keyboard {
52 	/* the following fields are managed by kbdio */
53 	int		kb_index;	/* kbdio index# */
54 	int		kb_minor;	/* minor number of the sub-device */
55 	int		kb_flags;	/* internal flags */
56 #define KB_VALID	(1 << 16)	/* this entry is valid */
57 #define KB_NO_DEVICE	(1 << 17)	/* device not present */
58 #define KB_PROBED	(1 << 18)	/* device probed */
59 #define KB_INITIALIZED	(1 << 19)	/* device initialized */
60 #define KB_REGISTERED	(1 << 20)	/* device registered to kbdio */
61 #define KB_BUSY		(1 << 21)	/* device used by a client */
62 	int		kb_active;	/* 0: inactive */
63 	void		*kb_token;	/* id of the current client */
64 	keyboard_callback_t kb_callback;/* callback function */
65 
66 	/*
67 	 * Device configuration flags:
68 	 * The upper 16 bits are common between various keyboard devices.
69 	 * The lower 16 bits are device-specific.
70 	 */
71 	int		kb_config;
72 #define KB_CONF_PROBE_ONLY (1 << 16)	/* probe only, don't initialize */
73 
74 	/* the following fields are set up by the driver */
75 	char		*kb_name;	/* driver name */
76 	int		kb_unit;	/* unit # */
77 	int		kb_type;	/* KB_84, KB_101, KB_OTHER,... */
78 	int		kb_io_base;	/* port# if any */
79 	int		kb_io_size;	/* # of occupied port */
80 	int		kb_led;		/* LED status */
81 	struct keymap	*kb_keymap;	/* key map */
82 	struct accentmap *kb_accentmap;	/* accent map */
83 	struct fkeytab	*kb_fkeytab;	/* function key strings */
84 	int		kb_fkeytab_size;/* # of function key strings */
85 	void		*kb_data;	/* the driver's private data */
86 	int		kb_delay1;
87 	int		kb_delay2;
88 #define KB_DELAY1	500
89 #define KB_DELAY2	100
90 };
91 
92 #define KBD_IS_VALID(k)		((k)->kb_flags & KB_VALID)
93 #define KBD_VALID(k)		((k)->kb_flags |= KB_VALID)
94 #define KBD_INVALID(k)		((k)->kb_flags &= ~KB_VALID)
95 #define KBD_HAS_DEVICE(k)	(!((k)->kb_flags & KB_NO_DEVICE))
96 #define KBD_FOUND_DEVICE(k)	((k)->kb_flags &= ~KB_NO_DEVICE)
97 #define KBD_IS_PROBED(k)	((k)->kb_flags & KB_PROBED)
98 #define KBD_PROBE_DONE(k)	((k)->kb_flags |= KB_PROBED)
99 #define KBD_IS_INITIALIZED(k)	((k)->kb_flags & KB_INITIALIZED)
100 #define KBD_INIT_DONE(k)	((k)->kb_flags |= KB_INITIALIZED)
101 #define KBD_IS_CONFIGURED(k)	((k)->kb_flags & KB_REGISTERED)
102 #define KBD_CONFIG_DONE(k)	((k)->kb_flags |= KB_REGISTERED)
103 #define KBD_IS_BUSY(k)		((k)->kb_flags & KB_BUSY)
104 #define KBD_BUSY(k)		((k)->kb_flags |= KB_BUSY)
105 #define KBD_UNBUSY(k)		((k)->kb_flags &= ~KB_BUSY)
106 #define KBD_IS_ACTIVE(k)	((k)->kb_active)
107 #define KBD_ACTIVATE(k)		(++(k)->kb_active)
108 #define KBD_DEACTIVATE(k)	(--(k)->kb_active)
109 #define KBD_LED_VAL(k)		((k)->kb_led)
110 
111 /* keyboard function table */
112 typedef int		kbd_probe_t(int unit, void *arg, int flags);
113 typedef int		kbd_init_t(int unit, keyboard_t **kbdp, void *arg,
114 				   int flags);
115 typedef int		kbd_term_t(keyboard_t *kbd);
116 typedef int		kbd_intr_t(keyboard_t *kbd, void *arg);
117 typedef int		kbd_test_if_t(keyboard_t *kbd);
118 typedef int		kbd_enable_t(keyboard_t *kbd);
119 typedef int		kbd_disable_t(keyboard_t *kbd);
120 typedef int		kbd_read_t(keyboard_t *kbd, int wait);
121 typedef int		kbd_check_t(keyboard_t *kbd);
122 typedef u_int		kbd_read_char_t(keyboard_t *kbd, int wait);
123 typedef int		kbd_check_char_t(keyboard_t *kbd);
124 typedef int		kbd_ioctl_t(keyboard_t *kbd, u_long cmd, caddr_t data);
125 typedef int		kbd_lock_t(keyboard_t *kbd, int lock);
126 typedef void		kbd_clear_state_t(keyboard_t *kbd);
127 typedef int		kbd_get_state_t(keyboard_t *kbd, void *buf, size_t len);
128 typedef int		kbd_set_state_t(keyboard_t *kbd, void *buf, size_t len);
129 typedef u_char		*kbd_get_fkeystr_t(keyboard_t *kbd, int fkey,
130 					   size_t *len);
131 typedef int		kbd_poll_mode_t(keyboard_t *kbd, int on);
132 typedef void		kbd_diag_t(keyboard_t *kbd, int level);
133 
134 typedef struct keyboard_switch {
135 	kbd_probe_t	*probe;
136 	kbd_init_t	*init;
137 	kbd_term_t	*term;
138 	kbd_intr_t	*intr;
139 	kbd_test_if_t	*test_if;
140 	kbd_enable_t	*enable;
141 	kbd_disable_t	*disable;
142 	kbd_read_t	*read;
143 	kbd_check_t	*check;
144 	kbd_read_char_t	*read_char;
145 	kbd_check_char_t *check_char;
146 	kbd_ioctl_t	*ioctl;
147 	kbd_lock_t	*lock;
148 	kbd_clear_state_t *clear_state;
149 	kbd_get_state_t	*get_state;
150 	kbd_set_state_t	*set_state;
151 	kbd_get_fkeystr_t *get_fkeystr;
152 	kbd_poll_mode_t *poll;
153 	kbd_diag_t	*diag;
154 } keyboard_switch_t;
155 
156 /* keyboard driver */
157 typedef struct keyboard_driver {
158     char		*name;
159     keyboard_switch_t	*kbdsw;
160     int			(*configure)(int); /* backdoor for the console driver */
161 } keyboard_driver_t;
162 
163 #ifdef KERNEL
164 
165 #define KEYBOARD_DRIVER(name, sw, config)		\
166 	static struct keyboard_driver name##_kbd_driver = { \
167 		#name, &sw, config			\
168 	};						\
169 	DATA_SET(kbddriver_set, name##_kbd_driver);
170 
171 /* global variables */
172 extern keyboard_switch_t **kbdsw;
173 extern struct linker_set kbddriver_set;
174 
175 /* functions for the keyboard driver */
176 int			kbd_register(keyboard_t *kbd);
177 int			kbd_unregister(keyboard_t *kbd);
178 keyboard_switch_t	*kbd_get_switch(char *driver);
179 void			kbd_init_struct(keyboard_t *kbd, char *name, int type,
180 					int unit, int config, int port,
181 					int port_size);
182 void			kbd_set_maps(keyboard_t *kbd, struct keymap *keymap,
183 				     struct accentmap *accmap,
184 				     struct fkeytab *fkeymap, int fkeymap_size);
185 
186 /* functions for the keyboard client */
187 int			kbd_allocate(char *driver, int unit, void *id,
188 				     kbd_callback_func_t *func, void *arg);
189 int			kbd_release(keyboard_t *kbd, void *id);
190 int			kbd_change_callback(keyboard_t *kbd, void *id,
191 				     kbd_callback_func_t *func, void *arg);
192 int			kbd_find_keyboard(char *driver, int unit);
193 keyboard_t 		*kbd_get_keyboard(int index);
194 
195 /* a back door for the console driver to tickle the keyboard driver XXX */
196 int			kbd_configure(int flags);
197 			/* see `kb_config' above for flag bit definitions */
198 
199 #ifdef KBD_INSTALL_CDEV
200 
201 /* virtual keyboard cdev driver functions */
202 
203 int			kbd_attach(dev_t dev, keyboard_t *kbd,
204 				   struct cdevsw *sw);
205 int			kbd_detach(dev_t dev, keyboard_t *kbd,
206 				   struct cdevsw *sw);
207 
208 /* generic keyboard cdev driver functions */
209 
210 typedef struct genkbd_softc {
211 	int		gkb_flags;	/* flag/status bits */
212 #define KB_ASLEEP	(1 << 0)
213 	struct clist	gkb_q;		/* input queue */
214 	struct selinfo	gkb_rsel;
215 } genkbd_softc_t;
216 
217 int	genkbdopen(genkbd_softc_t *sc, keyboard_t *kbd, int flag, int mode,
218 		   struct proc *p);
219 int	genkbdclose(genkbd_softc_t *sc, keyboard_t *kbd, int flag, int mode,
220 		    struct proc *p);
221 int	genkbdread(genkbd_softc_t *sc, keyboard_t *kbd, struct uio *uio,
222 		   int flag);
223 int	genkbdwrite(genkbd_softc_t *sc, keyboard_t *kbd, struct uio *uio,
224 		    int flag);
225 int	genkbdioctl(genkbd_softc_t *sc, keyboard_t *kbd, u_long cmd,
226 		    caddr_t arg, int flag, struct proc *p);
227 int	genkbdpoll(genkbd_softc_t *sc, keyboard_t *kbd, int event,
228 		   struct proc *p);
229 
230 #endif /* KBD_INSTALL_CDEV */
231 
232 /* generic low-level keyboard functions */
233 
234 /* shift key state */
235 #define SHIFTS1		(1 << 16)
236 #define SHIFTS2		(1 << 17)
237 #define SHIFTS		(SHIFTS1 | SHIFTS2)
238 #define CTLS1		(1 << 18)
239 #define CTLS2		(1 << 19)
240 #define CTLS		(CTLS1 | CTLS2)
241 #define ALTS1		(1 << 20)
242 #define ALTS2		(1 << 21)
243 #define ALTS		(ALTS1 | ALTS2)
244 #define AGRS1		(1 << 22)
245 #define AGRS2		(1 << 23)
246 #define AGRS		(AGRS1 | AGRS2)
247 #define METAS1		(1 << 24)
248 #define METAS2		(1 << 25)
249 #define METAS		(METAS1 | METAS2)
250 #define NLKDOWN		(1 << 26)
251 #define SLKDOWN		(1 << 27)
252 #define CLKDOWN		(1 << 28)
253 #define ALKDOWN		(1 << 29)
254 /* lock key state (defined in machine/console.h) */
255 /*
256 #define CLKED		LED_CAP
257 #define NLKED		LED_NUM
258 #define SLKED		LED_SCR
259 #define ALKED		(1 << 3)
260 #define LOCK_MASK	(CLKED | NLKED | SLKED | ALKED)
261 #define LED_CAP		(1 << 0)
262 #define LED_NUM		(1 << 1)
263 #define LED_SCR		(1 << 2)
264 #define LED_MASK	(LED_CAP | LED_NUM | LED_SCR)
265 */
266 
267 kbd_get_fkeystr_t	genkbd_get_fkeystr;
268 kbd_diag_t		genkbd_diag;
269 
270 int 	genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg);
271 int 	genkbd_keyaction(keyboard_t *kbd, int keycode, int down,
272 			 int *shiftstate, int *accents);
273 
274 #endif /* KERNEL */
275 
276 #endif /* !_DEV_KBD_KBDREG_H_ */
277