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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 #ifndef _SYS_USBA_UGEND_H
26 #define	_SYS_USBA_UGEND_H
27 
28 
29 /*
30  * UGEN - USB Generic Driver Support
31  * This file contains the UGEN specific data structure definitions
32  * and UGEN specific macros.
33  */
34 #include <sys/usb/usba/usbai_private.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /* ugen handle passed to client drivers as an opaque token */
41 typedef struct {
42 	dev_info_t	*hdl_dip;
43 	uint_t		hdl_flags;
44 	dev_t		hdl_minor_node_ugen_bits_mask;
45 	uint_t		hdl_minor_node_ugen_bits_shift;
46 	uint_t		hdl_minor_node_ugen_bits_limit;
47 
48 	dev_t		hdl_minor_node_instance_mask;
49 	uint_t		hdl_minor_node_instance_shift;
50 	uint_t		hdl_minor_node_instance_limit;
51 
52 	struct ugen_state *hdl_ugenp;
53 	char		*hdl_log_name;
54 	uint_t		hdl_log_name_length;
55 } usb_ugen_hdl_impl_t;
56 
57 _NOTE(SCHEME_PROTECTS_DATA("stable data", usb_ugen_hdl_impl_t))
58 
59 /* devt lookup support */
60 typedef struct ugen_devt_list_entry {
61 	struct ugen_devt_list_entry	*list_next;
62 	struct ugen_devt_list_entry	*list_prev;
63 	dev_t				list_dev;
64 	struct ugen_state		*list_state;
65 } ugen_devt_list_entry_t;
66 
67 typedef struct ugen_devt_cache_entry  {
68 	dev_t				cache_dev;
69 	struct ugen_state		*cache_state;
70 	uint_t				cache_hit;
71 } ugen_devt_cache_entry_t;
72 
73 #define	UGEN_DEVT_CACHE_SIZE 10
74 
75 /* minor node definition */
76 #ifdef _LP64
77 #define	UGEN_MINOR_NODE_SIZE		32
78 #else
79 #define	UGEN_MINOR_NODE_SIZE		18
80 #endif
81 
82 #define	UGEN_MINOR_INSTANCE_MASK(ugenp) \
83 		(ugenp)->ug_hdl->hdl_minor_node_instance_mask
84 #define	UGEN_MINOR_INSTANCE_LIMIT(ugenp) \
85 		(ugenp)->ug_hdl->hdl_minor_node_instance_limit
86 #define	UGEN_MINOR_INSTANCE_SHIFT(ugenp) \
87 		(ugenp)->ug_hdl->hdl_minor_node_instance_shift
88 
89 #define	UGEN_MINOR_IDX_SHIFT(ugenp) \
90 		(ugenp)->ug_hdl->hdl_minor_node_ugen_bits_shift
91 #define	UGEN_MINOR_IDX_LIMIT(ugenp) \
92 		(ugenp)->ug_hdl->hdl_minor_node_ugen_bits_limit
93 
94 #define	UGEN_MINOR_GET_IDX(ugenp, dev) \
95 		((getminor(dev) >> UGEN_MINOR_IDX_SHIFT(ugenp)) & \
96 		(ugenp)->ug_hdl->hdl_minor_node_ugen_bits_mask)
97 
98 #define	UGEN_MINOR_INSTANCE(ugenp, dev) \
99 	(getminor(dev) & UGEN_MINOR_INSTANCE_MASK(ugenp))
100 
101 
102 #define	UGEN_N_ENDPOINTS		32
103 
104 /* UGEN specific macros */
105 #define	UGEN_SETUP_PKT_SIZE		8	/* Ctrl xfer Setup token sz */
106 
107 /*
108  * minor node is contructed as follows for ugen driver (other client
109  * drivers that export a ugen interface may have a different layout):
110  *
111  * 17			 9			0
112  * +---------------------+----------------------+
113  * | minor index	 |	instance	|
114  * +---------------------+----------------------+
115  *
116  * Note that only 512 endpoint minor nodes can be supported (each
117  * endpoint requires a status endpoint as well so we can only support
118  * 256 endpoints)
119  *
120  * the real minor node is:
121  *
122  * 47	  40	  32	  24	 16	  8	  0
123  * +-------+-------+-------+------+-------+-------+
124  * | cfgval| cfgidx| iface | alt  |epidx  | type  |
125  * +-------+-------+-------+------+-------+-------+
126  *
127  * We get from the minor code to minor number thru ugen_minor_node_table
128  */
129 typedef uint64_t ugen_minor_t;
130 
131 #define	UGEN_MINOR_DEV_STAT_NODE	0x00
132 #define	UGEN_MINOR_EP_XFER_NODE		0x01
133 #define	UGEN_MINOR_EP_STAT_NODE		0x02
134 #define	UGEN_OWNS_DEVICE		0x04
135 
136 #define	UGEN_MINOR_EPIDX_SHIFT		8
137 #define	UGEN_MINOR_ALT_SHIFT		16
138 #define	UGEN_MINOR_IF_SHIFT		24
139 #define	UGEN_MINOR_CFGIDX_SHIFT		32
140 #define	UGEN_MINOR_CFGVAL_SHIFT		40
141 
142 #define	UGEN_MINOR_TYPE(ugenp, dev) \
143 	(ugen_devt2minor((ugenp), (dev)) & 0x3)
144 #define	UGEN_MINOR_EPIDX(ugenp, dev) \
145 	((ugen_devt2minor((ugenp), (dev)) >> UGEN_MINOR_EPIDX_SHIFT) & 0xFF)
146 
147 #define	UGEN_MINOR_ALT(ugenp, dev) \
148 	((ugen_devt2minor((ugenp), (dev)) >> UGEN_MINOR_ALT_SHIFT) & 0xFF)
149 
150 #define	UGEN_MINOR_IF(ugenp, dev) \
151 	((ugen_devt2minor((ugenp), (dev)) >> UGEN_MINOR_IF_SHIFT) & 0xFF)
152 
153 #define	UGEN_MINOR_CFGIDX(ugenp, dev) \
154 	((ugen_devt2minor((ugenp), (dev)) >> UGEN_MINOR_CFGIDX_SHIFT) & 0xFF)
155 
156 #define	UGEN_MINOR_CFGVAL(ugenp, dev) \
157 	((ugen_devt2minor((ugenp), (dev)) >> UGEN_MINOR_CFGVAL_SHIFT) & 0xFF)
158 
159 
160 /*
161  * According to usb2.0 spec (table 9-13), for all ep, bits 10..0 specify the
162  * max pkt size; for high speed ISOC/INTR ep, bits 12..11 specify the number of
163  * additional transaction opportunities per microframe.
164  */
165 #define	UGEN_PKT_SIZE(pktsize)	(pktsize & 0x07ff) * (1 + ((pktsize >> 11) & 3))
166 
167 
168 /*
169  * Structure for holding isoc data packets information
170  */
171 typedef struct ugen_isoc_pkt_info {
172 	ushort_t	isoc_pkts_count;
173 	uint_t		isoc_pkts_length;
174 	ugen_isoc_pkt_descr_t    *isoc_pkt_descr; /* array of pkt descr */
175 } ugen_isoc_pkt_info_t;
176 
177 /*
178  * Endpoint structure
179  * Holds all the information needed to manage the endpoint
180  */
181 typedef struct ugen_ep {
182 	uint_t		ep_state;	/* Endpoint state, see below */
183 	usb_ep_descr_t	ep_descr;	/* Endpoint descriptor */
184 	usb_ep_xdescr_t	ep_xdescr;	/* Extended endpoint descriptor */
185 	uchar_t		ep_cfgidx;	/* cfg index */
186 	uchar_t		ep_if;		/* Interface # */
187 	uchar_t		ep_alt;		/* alternate # */
188 	uchar_t		ep_done;	/* cmd is done */
189 	boolean_t	ep_one_xfer;	/* use one xfer on intr IN eps */
190 	uint_t		ep_lcmd_status;	/* last cmd status */
191 	int		ep_xfer_oflag;	/* open flag */
192 	int		ep_stat_oflag;	/* open flag */
193 	size_t		ep_buf_limit;	/* one second of data */
194 	usb_pipe_handle_t ep_ph;	/* Endpoint pipe handle */
195 	usb_pipe_policy_t ep_pipe_policy;
196 	kmutex_t	ep_mutex;	/* Mutex protecting ugen_ep */
197 	kcondvar_t	ep_wait_cv;	/* block for completion */
198 	usb_serialization_t ep_ser_cookie;	/* one xfer at the time */
199 	mblk_t		*ep_data;	/* IN data (ctrl & intr) */
200 	struct buf	*ep_bp;		/* save current buf ptr */
201 	struct pollhead	ep_pollhead;	/* for polling	*/
202 	ugen_isoc_pkt_info_t ep_isoc_info;	/* for isoc eps */
203 	int		ep_isoc_in_inited;	/* isoc IN init flag */
204 } ugen_ep_t;
205 
206 _NOTE(MUTEX_PROTECTS_DATA(ugen_ep::ep_mutex, ugen_ep))
207 
208 /* endpoints descriptor access */
209 #define	UGEN_XFER_TYPE(epp) ((epp)->ep_descr.bmAttributes & USB_EP_ATTR_MASK)
210 #define	UGEN_XFER_DIR(epp) ((epp)->ep_descr.bEndpointAddress & USB_EP_DIR_IN)
211 #define	UGEN_XFER_ADDR(epp) ((epp)->ep_descr.bEndpointAddress)
212 
213 #define	UGEN_INTR_BUF_LIMIT	4096
214 
215 /* endpoint xfer/stat states */
216 #define	UGEN_EP_STATE_NONE		0x00
217 #define	UGEN_EP_STATE_ACTIVE		0x01
218 #define	UGEN_EP_STATE_XFER_OPEN		0x02
219 #define	UGEN_EP_STATE_STAT_OPEN		0x04
220 #define	UGEN_EP_STATE_XS_OPEN		(UGEN_EP_STATE_XFER_OPEN | \
221 					    UGEN_EP_STATE_STAT_OPEN)
222 #define	UGEN_EP_STATE_INTR_IN_POLLING_ON		0x10
223 #define	UGEN_EP_STATE_INTR_IN_POLLING_IS_STOPPED	0x20
224 #define	UGEN_EP_STATE_INTR_IN_POLL_PENDING		0x40
225 
226 #define	UGEN_EP_STATE_ISOC_IN_POLLING_ON	0x100
227 #define	UGEN_EP_STATE_ISOC_IN_POLLING_IS_STOPPED	0x200
228 #define	UGEN_EP_STATE_ISOC_IN_POLL_PENDING	0x400
229 
230 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_ep::ep_ph))
231 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_ep::ep_descr))
232 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_ep::ep_ser_cookie))
233 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_ep::ep_if))
234 
235 _NOTE(SCHEME_PROTECTS_DATA("USBA", usb_ctrl_req))
236 _NOTE(SCHEME_PROTECTS_DATA("USBA", usb_bulk_req))
237 _NOTE(SCHEME_PROTECTS_DATA("USBA", usb_intr_req))
238 
239 typedef struct ugen_dev_stat {
240 	int			dev_oflag;	/* open flag */
241 	uint_t			dev_stat;	/* internal status */
242 	uint_t			dev_state;	/* exported state */
243 	kcondvar_t		dev_wait_cv;	/* block for change */
244 	struct pollhead		dev_pollhead;	/* for polling */
245 } ugen_dev_stat_t;
246 
247 /* dev_stat */
248 #define	UGEN_DEV_STATUS_INACTIVE	0x0
249 #define	UGEN_DEV_STATUS_ACTIVE		0x1
250 #define	UGEN_DEV_STATUS_POLL_PENDING	0x2
251 #define	UGEN_DEV_STATUS_CHANGED		0x4
252 
253 /* Power Management support */
254 typedef struct ugen_power  {
255 	uint_t			pwr_states;
256 	int			pwr_busy;		/* busy accounting */
257 	uint8_t			pwr_wakeup_enabled;
258 	uint8_t			pwr_current;
259 } ugen_power_t;
260 
261 /* UGEN state structure */
262 typedef struct ugen_state {
263 	usb_ugen_hdl_impl_t	*ug_hdl;		/* pointer to handle */
264 	dev_info_t		*ug_dip;		/* Dev info */
265 	uint_t			ug_instance;		/* Instance number */
266 	uint_t			ug_dev_state;
267 	uint_t			ug_dev_stat_state;
268 	uint_t			ug_open_count;
269 	uint_t			ug_pending_cmds;
270 	uint_t			ug_initial_cfgidx;
271 
272 	/* locks */
273 	kmutex_t		ug_mutex;		/* Instance mutex */
274 	usb_serialization_t	ug_ser_cookie;		/* access */
275 
276 	/* USB debugging system support */
277 	usb_log_handle_t	ug_log_hdl;
278 
279 	/* registration data */
280 	usb_client_dev_data_t	*ug_dev_data;
281 
282 	/* Endpoint management list */
283 	ugen_ep_t		ug_ep[UGEN_N_ENDPOINTS];
284 
285 	/* encoding minor numbers as we only have 8 bits in the minor # */
286 	ugen_minor_t		*ug_minor_node_table;
287 	int			ug_minor_node_table_index;
288 	size_t			ug_minor_node_table_size;
289 
290 	/* device status management */
291 	ugen_dev_stat_t		ug_ds;
292 
293 	/* PM Support */
294 	ugen_power_t		*ug_pm;
295 
296 	/* Maximum transfer size for bulk endpoints */
297 	size_t			ug_max_bulk_xfer_sz;
298 
299 	/* Used to deallocate allocated resources */
300 	ushort_t		ug_cleanup_flags;
301 } ugen_state_t;
302 
303 _NOTE(SCHEME_PROTECTS_DATA("unshared", buf))
304 
305 _NOTE(MUTEX_PROTECTS_DATA(ugen_state::ug_mutex, ugen_state))
306 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_log_hdl))
307 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_hdl))
308 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_ser_cookie))
309 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_dip))
310 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_pm))
311 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_instance))
312 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_minor_node_table))
313 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_minor_node_table_index))
314 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_max_bulk_xfer_sz))
315 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_dev_data))
316 _NOTE(DATA_READABLE_WITHOUT_LOCK(ugen_state::ug_cleanup_flags))
317 
318 
319 /* ugen_cleanup_flags */
320 #define	UGEN_INIT_LOCKS			0x01
321 
322 /* additional USB device states */
323 #define	USB_UGEN_DEV_UNAVAILABLE_RESUME		0x90
324 #define	USB_UGEN_DEV_UNAVAILABLE_RECONNECT	0x91
325 
326 /* Debugging information */
327 #define	UGEN_PRINT_ATTA		0x1
328 #define	UGEN_PRINT_CBOPS	0x2
329 #define	UGEN_PRINT_CPR		0x4
330 #define	UGEN_PRINT_POLL		0x8
331 #define	UGEN_PRINT_XFER		0x10
332 #define	UGEN_PRINT_HOTPLUG	0x20
333 #define	UGEN_PRINT_STAT		0x40
334 #define	UGEN_PRINT_PM		0x80
335 #define	UGEN_PRINT_ALL		0xFFFFFFFF
336 
337 #ifdef	__cplusplus
338 }
339 #endif
340 
341 #endif	/* _SYS_USBA_UGEND_H */
342