1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_USB_USBKBM_H
27 #define	_SYS_USB_USBKBM_H
28 
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/time.h>
35 #include <sys/vuid_event.h>
36 #include <sys/stream.h>
37 #include <sys/kbd.h>
38 
39 
40 /*
41  * USB keyboard LED masks (used to set LED's on USB keyboards)
42  */
43 #define	USB_LED_NUM_LOCK	0x1
44 #define	USB_LED_CAPS_LOCK	0x2
45 #define	USB_LED_SCROLL_LOCK	0x4
46 #define	USB_LED_COMPOSE		0x8
47 #define	USB_LED_KANA		0x10	/* Valid only on Japanese layout */
48 
49 /* Modifier key masks */
50 #define	USB_LCTLBIT   0x01
51 #define	USB_LSHIFTBIT 0x02
52 #define	USB_LALTBIT   0x04
53 #define	USB_LMETABIT  0x08
54 #define	USB_RCTLBIT   0x10
55 #define	USB_RSHIFTBIT 0x20
56 #define	USB_RALTBIT   0x40
57 #define	USB_RMETABIT  0x80
58 
59 #define	USB_LSHIFTKEY	225
60 #define	USB_LCTLCKEY	224
61 #define	USB_LALTKEY	226
62 #define	USB_LMETAKEY	227
63 #define	USB_RCTLCKEY	228
64 #define	USB_RSHIFTKEY	229
65 #define	USB_RMETAKEY	231
66 #define	USB_RALTKEY	230
67 
68 /*
69  * The keyboard would report ErrorRollOver in all array fields when
70  * the number of non-modifier keys pressed exceeds the Report Count.
71  */
72 #define	USB_ERRORROLLOVER 1
73 
74 
75 /*
76  * This defines the format of translation tables.
77  *
78  * A translation table is USB_KEYMAP_SIZE "entries", each of which is 2
79  * bytes (unsigned shorts).  The top 8 bits of each entry are decoded by
80  * a case statement in getkey.c.  If the entry is less than 0x100, it
81  * is sent out as an EUC character (possibly with bucky bits
82  * OR-ed in).  "Special" entries are 0x100 or greater, and
83  * invoke more complicated actions.
84  */
85 
86 /*
87  * HID-spec-defined report size (in bytes) for each USB HID boot-protocol
88  * mode report.
89  */
90 
91 #define	USB_KBD_BOOT_PROTOCOL_PACKET_SIZE	8
92 
93 /* definitions for various state machines */
94 #define	USBKBM_OPEN	0x00000001 /* keyboard is open for business */
95 #define	USBKBM_QWAIT	0x00000002 /* keyboard is waiting for a response */
96 
97 /*
98  * Polled key state
99  */
100 typedef struct poll_keystate {
101 	int		poll_key;		/* scancode */
102 	enum keystate   poll_state;		/* pressed or released */
103 } poll_keystate_t;
104 
105 #define	USB_POLLED_BUFFER_SIZE	20	/* # of characters in poll buffer */
106 
107 #define	USBKBM_MAXPKTSIZE	10	/* Maximum size of a packet */
108 
109 typedef struct usbkbm_report_format {
110 	uint8_t	keyid;	/* report id of keyboard input */
111 	uint_t	kpos;	/* keycode offset in the keyboard data */
112 	uint_t	klen;	/* length of keycodes */
113 	uint_t	tlen;	/* length of the input report (inc. report id) */
114 } usbkbm_report_format_t;
115 
116 /* state structure for usbkbm */
117 typedef struct  usbkbm_state {
118 	struct kbtrans		*usbkbm_kbtrans;
119 	queue_t			*usbkbm_readq;		/* read queue */
120 	queue_t			*usbkbm_writeq;		/* write queue */
121 	int			usbkbm_flags;
122 
123 	/* Report format of keyboard data */
124 	usbkbm_report_format_t	usbkbm_report_format;
125 
126 	/* Pointer to the parser handle */
127 	hidparser_handle_t	usbkbm_report_descr;
128 	uint16_t		usbkbm_layout;		/* keyboard layout */
129 	/*
130 	 * Setting this indicates that the second IOCTL
131 	 * after KBD_CMD_SETLED follows
132 	 */
133 	int			usbkbm_setled_second_byte;
134 	/* Keyboard packets sent last */
135 	uchar_t			usbkbm_lastusbpacket[USBKBM_MAXPKTSIZE];
136 
137 	/* Currently processed key events of the current keyboard packet */
138 	uchar_t			usbkbm_pendingusbpacket[USBKBM_MAXPKTSIZE];
139 
140 	hid_polled_input_callback_t
141 				usbkbm_hid_callback;	/* poll information */
142 
143 	mblk_t			*usbkbm_pending_link; /* mp waiting response */
144 
145 	/* "ioctl" awaiting buffer */
146 	mblk_t			*usbkbm_streams_iocpending;
147 
148 	/* id from qbufcall on allocb failure */
149 	bufcall_id_t		usbkbm_streams_bufcallid;
150 
151 	/* Polled input information */
152 	struct cons_polledio	usbkbm_polled_info;
153 
154 	int			usbkbm_vkbd_type;
155 
156 	/* keyboard device info from hid */
157 	hid_vid_pid_t		usbkbm_vid_pid;
158 
159 	/* These entries are for polled input */
160 	uint_t		usbkbm_polled_buffer_num_characters;
161 	poll_keystate_t	usbkbm_polled_scancode_buffer[USB_POLLED_BUFFER_SIZE];
162 	poll_keystate_t	*usbkbm_polled_buffer_head;
163 	poll_keystate_t	*usbkbm_polled_buffer_tail;
164 
165 	/* Boot protocol or report protocol */
166 	uint8_t	protocol;
167 } usbkbm_state_t;
168 
169 #define	USB_PRESSED	0x00	/* key was pressed */
170 #define	USB_RELEASED	0x01	/* key was released */
171 
172 /* Sun Japanese type6 and type7 keyboards layout numbers, vid and pid */
173 #define	SUN_JAPANESE_TYPE6		271
174 #define	SUN_JAPANESE_TYPE7		15
175 #define	HID_SUN_JAPANESE_TYPE6_KBD_VID	0x0430
176 #define	HID_SUN_JAPANESE_TYPE6_KBD_PID	0x0005
177 
178 
179 /* Number of entries in the keytable */
180 #define	KEYMAP_SIZE_USB		255
181 
182 /* Size in bytes of the keytable */
183 #define	USB_KEYTABLE_SIZE	(KEYMAP_SIZE_USB * sizeof (keymap_entry_t))
184 
185 /* structure to save global state */
186 typedef struct usbkbm_save_state {
187 	/* LED state */
188 	uchar_t		usbkbm_save_led;
189 	uchar_t		usbkbm_layout;
190 
191 	/* Keymap information */
192 	struct keyboard usbkbm_save_keyindex;
193 
194 } usbkbm_save_state_t;
195 
196 /*
197  * Masks for debug printing
198  */
199 #define	PRINT_MASK_ATTA		0x00000001
200 #define	PRINT_MASK_OPEN 	0x00000002
201 #define	PRINT_MASK_CLOSE	0x00000004
202 #define	PRINT_MASK_PACKET	0x00000008
203 #define	PRINT_MASK_ALL		0xFFFFFFFF
204 
205 #define	INDEXTO_PC	1	/* To PC table */
206 #define	INDEXTO_USB	0	/* To USB table */
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 
212 #endif	/* _SYS_USB_USBKBM_H */
213