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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Copyright 2017 Joyent, Inc.
25  */
26 
27 #ifndef _SYS_USB_HIDVAR_H
28 #define	_SYS_USB_HIDVAR_H
29 
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 #include <sys/usb/usba/usbai_private.h>
36 #include <sys/usb/usba/usba_ugen.h>
37 
38 /*
39  * HID : This header file contains the internal structures
40  * and variable definitions used in hid driver.
41  */
42 
43 /*
44  * HID USB device state management :
45  *
46  *	ONLINE-----1--->SUSPENDED----2---->ONLINE
47  *	  |
48  *	  +-----3--->DISCONNECTED----4----->ONLINE
49  *	  |
50  *	  +-----7--->POWERED DOWN----8----->POWER CHANGE---9--->ONLINE
51  *						|
52  *						+---3--->DISCONNECTED
53  *
54  *	POWERED DOWN----1--->SUSPENDED------2----->POWERED DOWN
55  *	  |		      |     ^
56  *	  |		      5     |
57  *	  |		      |     6
58  *	  |		      v     |
59  *	  +---------3----->DISCONNECTED-------4----->POWERED DOWN
60  *
61  *	1 = CPR SUSPEND
62  *	2 = CPR RESUME (with original device)
63  *	3 = Device Unplug
64  *	4 = Original Device Plugged in
65  *	5 = CPR RESUME (with device disconnected or with a wrong device)
66  *	6 = CPR SUSPEND on a disconnected device
67  *	7 = Device idles for time T & transitions to low power state
68  *	8 = Remote wakeup by device OR Application kicking off IO to device
69  *          This results in a Transistion state till PM calls the power
70  *	    entry point to raise the power level of the device
71  *	9 = Device entry point called to raise power level of the device
72  *
73  */
74 
75 
76 /* Boot Interface Subclass for HID devices */
77 #define	BOOT_INTERFACE		0x01
78 
79 /* Boot protocol values for keyboard and mouse */
80 #define	KEYBOARD_PROTOCOL	0x01		/* legacy keyboard */
81 #define	MOUSE_PROTOCOL		0x02		/* legacy mouse */
82 #define	NONE_PROTOCOL		0
83 /*
84  * If the hid descriptor is not valid, the following values are
85  * used.
86  */
87 #define	USBKPSZ			8	/* keyboard packet size */
88 #define	USBMSSZ			3	/* mouse packet size */
89 #define	USB_KB_HID_DESCR_LENGTH 0x3f	/* keyboard Report descr length */
90 #define	USB_MS_HID_DESCR_LENGTH 0x32	/* mouse Report descr length */
91 
92 /*
93  * Flags for the default pipe.
94  */
95 #define	HID_DEFAULT_PIPE_BUSY	0x01
96 
97 /*
98  * Hid interrupt pipe states. Interrupt pipe
99  * can be in only one of these states :
100  *
101  *	open--1-->data_transferring--1-->open
102  *	 |
103  *	 |----2---->closed
104  *
105  *	1 = interrupt pipe callback
106  *	2 = hid_close
107  */
108 #define	HID_INTERRUPT_PIPE_CLOSED 0x00 /* Int. pipe is closed */
109 #define	HID_INTERRUPT_PIPE_OPEN	0x01 /* Int. pipe is opened */
110 
111 /* HID mctl processing return codes */
112 #define	HID_SUCCESS	0	/* mctl processed successfully */
113 #define	HID_INPROGRESS	1	/* mctl queued/deferred for execution */
114 #define	HID_ENQUEUE	2	/* mctl queued/deferred for execution */
115 #define	HID_FAILURE	-1	/* mctl processing failed */
116 
117 /* Data is being sent up */
118 #define	HID_INTERRUPT_PIPE_DATA_TRANSFERRING	0x03
119 
120 /* Attach/detach states */
121 #define	HID_LOCK_INIT		0x01	/* Initial attach state */
122 #define	HID_MINOR_NODES		0x02	/* Set after minor node is created */
123 
124 /* HID Protocol Requests */
125 #define	SET_IDLE		0x0a	/* bRequest value to set idle request */
126 #define	DURATION		(0<<8)	/* no. of repeat reports (HID 7.2.4) */
127 #define	SET_PROTOCOL		0x0b	/* bRequest value for boot protocol */
128 
129 /* Hid PM scheme */
130 typedef enum {
131 	HID_PM_ACTIVITY,	/* device is power managed by idleness */
132 	HID_PM_OPEN_CLOSE,	/* device is busy on open, idle on close */
133 	HID_PM_APPLICATION	/* device is power managed by application */
134 } hid_pm_scheme_t;
135 
136 typedef struct hid_power {
137 
138 	void			*hid_state;	/* points back to hid_state */
139 
140 	int			hid_pm_busy;	/* device busy accounting */
141 
142 	hid_pm_scheme_t		hid_pm_strategy;	/* device PM */
143 
144 	uint8_t			hid_wakeup_enabled;
145 
146 	/* this is the bit mask of the power states that device has */
147 	uint8_t			hid_pwr_states;
148 
149 	/* wakeup and power transistion capabilites of an interface */
150 	uint8_t			hid_pm_capabilities;
151 
152 	/* flag to indicate if driver is about to raise power level */
153 	boolean_t		hid_raise_power;
154 
155 	/* current power level the device is in */
156 	uint8_t			hid_current_power;
157 
158 	/* mblk indicating that the device has powered up */
159 	mblk_t			*hid_pm_pwrup;
160 } hid_power_t;
161 
162 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_power_t::hid_state))
163 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_power_t::hid_pm_strategy))
164 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_power_t::hid_wakeup_enabled))
165 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_power_t::hid_pwr_states))
166 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_power_t::hid_pm_capabilities))
167 
168 
169 typedef struct hid_state {
170 	dev_info_t		*hid_dip;	/* per-device info handle */
171 	kmutex_t		hid_mutex;	/* for general locking */
172 	int			hid_instance;	/* instance number */
173 
174 	/* Attach/detach flags */
175 	int			hid_attach_flags;
176 
177 	/* device state flag */
178 	int			hid_dev_state;
179 
180 	/* outstanding requests on the default pipe */
181 	int			hid_default_pipe_req;
182 
183 	hid_power_t		*hid_pm;	/* ptr to power struct */
184 
185 	usb_client_dev_data_t	*hid_dev_data;	/* ptr to usb reg struct */
186 
187 	usb_dev_descr_t		*hid_dev_descr;	/* device descriptor. */
188 
189 	/* hid driver is attached to this interface */
190 	int			hid_interfaceno;
191 
192 	usb_if_descr_t		hid_if_descr;		/* interface descr */
193 	usb_hid_descr_t		hid_hid_descr;		/* hid descriptor */
194 	usb_ep_xdescr_t		hid_ep_intr_xdescr;	/* ep extended desc */
195 	hidparser_handle_t	hid_report_descr;	/* report descr */
196 
197 	usb_pipe_handle_t	hid_default_pipe;	/* default pipe */
198 	usb_pipe_handle_t	hid_interrupt_pipe;	/* intr pipe handle */
199 
200 	int			hid_packet_size;	/* data packet size */
201 
202 	/* Pipe policy for the interrupt pipe is saved here */
203 	usb_pipe_policy_t	hid_intr_pipe_policy;
204 
205 	/*
206 	 * This field is only used if the device provides polled input
207 	 * This is state information for the usba layer.
208 	 */
209 	usb_console_info_t	hid_polled_console_info;
210 
211 	/*
212 	 * This is the buffer that the raw characters are stored in.
213 	 * for polled mode.
214 	 */
215 	uchar_t			*hid_polled_raw_buf;
216 
217 	/* handle for outputting messages */
218 	usb_log_handle_t	hid_log_handle;
219 
220 	queue_t			*hid_internal_rq;
221 	queue_t			*hid_external_rq;
222 	/* which one of the above 2 streams gets the input */
223 	queue_t			*hid_inuse_rq;
224 	int			hid_internal_flag;	/* see below */
225 	int			hid_external_flag;	/* see below */
226 
227 	usb_ugen_hdl_t		hid_ugen_hdl;		/* ugen support */
228 } hid_state_t;
229 
230 /* warlock directives, stable data */
231 _NOTE(MUTEX_PROTECTS_DATA(hid_state_t::hid_mutex, hid_state_t))
232 _NOTE(MUTEX_PROTECTS_DATA(hid_state_t::hid_mutex, hid_power_t))
233 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_dip))
234 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_pm))
235 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_dev_data))
236 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_instance))
237 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_interrupt_pipe))
238 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_ep_intr_descr))
239 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_default_pipe))
240 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_log_handle))
241 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_if_descr))
242 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_dev_data))
243 _NOTE(DATA_READABLE_WITHOUT_LOCK(hid_state_t::hid_dev_descr))
244 _NOTE(SCHEME_PROTECTS_DATA("stable data", usb_ep_descr))
245 
246 
247 /*
248  * The hid_polled_console_info field is a handle from usba.  The
249  * handle is used when the kernel is in the single thread mode
250  * so the field is tagged with this note.
251  */
252 _NOTE(SCHEME_PROTECTS_DATA("unique per call",
253 				hid_state_t::hid_polled_console_info))
254 
255 /*
256  * structure for argument for callback routine for async
257  * data transfer through default pipe.
258  */
259 typedef struct hid_default_pipe_argument {
260 	/* Pointer to the write queue from which the message comes from */
261 	queue_t		*hid_default_pipe_arg_queue;
262 
263 	/* Message to be sent up to the stream */
264 	struct iocblk	hid_default_pipe_arg_mctlmsg;
265 
266 	/* Pointer to the original mblk_t received from hid_wput() */
267 	mblk_t		*hid_default_pipe_arg_mblk;
268 
269 	/* Request that caused this callback to happen */
270 	uchar_t		hid_default_pipe_arg_bRequest;
271 
272 } hid_default_pipe_arg_t;
273 
274 /*
275  * An instance of this structure is created per command down to the
276  * device.  The control callback is not executed until the call is
277  * made into usba, so there is no danger of a callback happening when
278  * the fields of the structure are being set.
279  */
280 _NOTE(SCHEME_PROTECTS_DATA("unique per call", hid_default_pipe_arg_t))
281 
282 /*
283  * An instance of this structure is created per command down to the
284  * device.  The callback is not executed until the call is
285  * made into usba, so there is no danger of a callback happening when
286  * the fields of the structure are being set.
287  */
288 
289 /* Value for hid_[internal|external]_flag */
290 #define	HID_STREAMS_OPEN	0x00000001	/* Streams are open */
291 #define	HID_STREAMS_DISMANTLING	0x00000002	/* In hid_close() */
292 
293 #define	HID_STREAMS_FLAG(q, hidp) ((q) == (hidp)->hid_internal_rq ? \
294 	(hidp)->hid_internal_flag : (hidp)->hid_external_flag)
295 
296 #define	HID_IS_OPEN(hidp)	(((hidp)->hid_internal_flag == \
297 	HID_STREAMS_OPEN) || ((hidp)->hid_external_flag == HID_STREAMS_OPEN))
298 
299 #define	HID_BAD_DESCR		0x01		/* Bad hid report descriptor */
300 
301 #define	HID_MINOR_NAME_LEN	20	/* Max length of minor_name string */
302 
303 /* hid_close will wait 60 secons for callbacks to be over */
304 #define	HID_CLOSE_WAIT_TIMEOUT	10
305 
306 /* define a timeout for draining requests on the default control pipe */
307 #define	HID_DEFAULT_PIPE_DRAIN_TIMEOUT	5
308 
309 /* To support PM on SUN mice of later revisions */
310 #define	HID_SUN_MOUSE_VENDOR_ID	0x0430
311 #define	HID_SUN_MOUSE_PROD_ID	0x0100
312 #define	HID_SUN_MOUSE_BCDDEVICE	0x0105	/* and later revisions */
313 
314 
315 /*
316  * Debug message Masks
317  */
318 #define	PRINT_MASK_ATTA		0x00000001
319 #define	PRINT_MASK_OPEN		0x00000002
320 #define	PRINT_MASK_CLOSE	0x00000004
321 #define	PRINT_MASK_EVENTS	0x00000008
322 #define	PRINT_MASK_PM		0x00000010
323 #define	PRINT_MASK_ALL		0xFFFFFFFF
324 
325 /*
326  * Define states local to hid driver
327  */
328 #define	USB_DEV_HID_POWER_CHANGE 0x80
329 
330 /* define for retrying control requests */
331 #define	HID_RETRY	10
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 
337 #endif	/* _SYS_USB_HIDVAR_H */
338