xref: /titanic_44/usr/src/uts/common/sys/usb/usba/whcdi.h (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
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_WHCDI_H
27 #define	_SYS_USB_WHCDI_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/usb/usba/usba_types.h>
34 #include <sys/usb/usba/wusba.h>
35 #include <sys/usb/usba/wusba_io.h>
36 #include <sys/usb/usba/wa.h>	/* for wusb_secrt_data_t */
37 
38 
39 /*
40  * This file contains data structures and functions that might be
41  * shared by HWA and WHCI drivers.
42  */
43 
44 typedef struct wusb_hc_cc_list {
45 	wusb_cc_t		cc;
46 	struct wusb_hc_cc_list	*next;
47 } wusb_hc_cc_list_t;
48 
49 struct wusb_hc_data;
50 
51 typedef struct wusb_dev_info {
52 	struct wusb_hc_data	*wdev_hc; /* the HC this device attaches */
53 	uint8_t			wdev_cdid[16];
54 	uint16_t		wdev_addr;
55 	uint16_t		wdev_state;
56 	uint8_t			wdev_is_newconn;
57 	uint8_t			wdev_beacon_attr;
58 	usb_pipe_handle_t	wdev_ph;	/* used before authenticated */
59 	wusb_secrt_data_t	wdev_secrt_data;
60 	usb_uwb_cap_descr_t	*wdev_uwb_descr;
61 	wusb_cc_t		*wdev_cc;
62 	uint8_t			wdev_ptk[16];
63 	uint8_t			wdev_tkid[3];
64 	timeout_id_t		wdev_trust_timer; /* TrustTimeout timer */
65 	uint8_t			wdev_active;
66 } wusb_dev_info_t;
67 
68 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_dev_info::wdev_addr))
69 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_dev_info::wdev_uwb_descr))
70 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_dev_info::wdev_hc))
71 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_dev_info::wdev_secrt_data))
72 
73 /*
74  * According to WUSB 1.0 spec, WUSB hosts can support up to 127 devices.
75  * To comply with USB bus convention that bus address 1 is assigned
76  * to the host controller device, the addresses assigned to WUSB devices
77  * would start from 2. So the max device number is reduced to 126.
78  */
79 #define	WUSB_MAX_PORTS		126
80 
81 #define	WUSB_CHILD_ZAP		0x1
82 
83 typedef struct wusb_hc_data {
84 	dev_info_t		*hc_dip;
85 	void			*hc_private_data;
86 	uint8_t			hc_chid[16];
87 	uint8_t			hc_cluster_id;
88 	uint8_t			hc_num_mmcies;
89 	kmutex_t		hc_mutex;
90 	wusb_ie_header_t	**hc_mmcie_list;
91 
92 	boolean_t		hc_newcon_enabled;
93 
94 	/* save the often used IEs so as not to allocate them each time */
95 	wusb_ie_keepalive_t		hc_alive_ie;
96 
97 	/* children info structures */
98 	uint8_t			hc_num_ports;
99 	wusb_dev_info_t		**hc_dev_infos;
100 	dev_info_t		**hc_children_dips;
101 	size_t			hc_cd_list_length;
102 	usba_device_t		**hc_usba_devices;
103 
104 	/* for bus unconfig */
105 	uint8_t			hc_children_state[WUSB_MAX_PORTS + 1];
106 
107 	/* child connection functions */
108 	void	(*disconnect_dev)(dev_info_t *, usb_port_t);
109 	void	(*reconnect_dev)(dev_info_t *, usb_port_t);
110 	int	(*create_child)(dev_info_t *, usb_port_t);
111 	int	(*destroy_child)(dev_info_t *, usb_port_t);
112 
113 	/*
114 	 * some necessary host functions:
115 	 * Both HWA and HCI must implement these entries to support basic
116 	 * host controller operations.
117 	 */
118 	int	(*set_encrypt)(dev_info_t *, usb_port_t, uint8_t);
119 	int	(*set_ptk)(dev_info_t *, usb_key_descr_t *, size_t, usb_port_t);
120 	int	(*set_gtk)(dev_info_t *, usb_key_descr_t *, size_t);
121 	int	(*set_device_info)(dev_info_t *, wusb_dev_info_t *, usb_port_t);
122 	int	(*set_cluster_id) (dev_info_t *, uint8_t id);
123 	int	(*set_stream_idx) (dev_info_t *, uint8_t idx);
124 	int	(*set_wusb_mas)	(dev_info_t *, uint8_t *data);
125 	int	(*add_mmc_ie)	(dev_info_t *, uint8_t interval, uint8_t rcnt,
126 				uint8_t iehdl, uint16_t len, uint8_t *data);
127 	int	(*rem_mmc_ie)	(dev_info_t *, uint8_t iehdl);
128 	int	(*stop_ch)	(dev_info_t *, uint32_t time);
129 	int	(*set_num_dnts)	(dev_info_t *, uint8_t interval, uint8_t nslot);
130 	int	(*get_time)	(dev_info_t *, uint8_t timetype,
131 				uint16_t timelen, uint32_t *time);
132 
133 	/* host addr in MAC layer */
134 	uint16_t		hc_addr;
135 
136 	/* beaconing channel */
137 	uint8_t			hc_channel;
138 
139 	/* reserved MASes. bitmaps */
140 	uint8_t			hc_mas[WUSB_SET_WUSB_MAS_LEN];
141 
142 	/* connection context list for the host */
143 	wusb_hc_cc_list_t	*hc_cc_list;
144 
145 	/* group temporal key */
146 	usb_key_descr_t		hc_gtk;
147 	uint8_t			hc_gtk_padding[15];
148 } wusb_hc_data_t;
149 
150 _NOTE(MUTEX_PROTECTS_DATA(wusb_hc_data_t::hc_mutex, wusb_dev_info_t))
151 _NOTE(MUTEX_PROTECTS_DATA(wusb_hc_data_t::hc_mutex, wusb_hc_data_t))
152 
153 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::hc_num_ports))
154 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::hc_num_mmcies))
155 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::hc_dip))
156 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::hc_gtk))
157 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::add_mmc_ie))
158 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::rem_mmc_ie))
159 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_cluster_id))
160 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_encrypt))
161 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_gtk))
162 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_ptk))
163 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_num_dnts))
164 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_stream_idx))
165 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::set_wusb_mas))
166 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::stop_ch))
167 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::create_child))
168 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::destroy_child))
169 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::disconnect_dev))
170 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::reconnect_dev))
171 _NOTE(DATA_READABLE_WITHOUT_LOCK(wusb_hc_data_t::get_time))
172 
173 _NOTE(SCHEME_PROTECTS_DATA("local use only",
174 				wusb_ie_host_disconnect::bLength))
175 _NOTE(SCHEME_PROTECTS_DATA("local use only",
176 				wusb_ie_host_disconnect::bIEIdentifier))
177 _NOTE(SCHEME_PROTECTS_DATA("local use only",
178 				wusb_ccm_nonce::sfn))
179 /*
180  * WUSB 1.0 4.3.8.5 says the range of cluster id is in 0x80-0xfe,
181  * we limit the maximum WUSB host controller numbers to 31 now,
182  * and take the upper portion of this range as the broadcast
183  * cluster id
184  */
185 #define	WUSB_CLUSTER_ID_COUNT		31
186 #define	WUSB_MIN_CLUSTER_ID		0xe0
187 
188 #define	WUSB_TRUST_TIMEOUT	4 /* WUSB 4.15.1 TrustTimeout = 4s */
189 #define	WUSB_TRUST_TIMEOUT_US	WUSB_TRUST_TIMEOUT * MICROSEC
190 
191 #define	WUSB_PERIODIC_ENDPOINT(endpoint) (((endpoint->bmAttributes & \
192 	USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR) ||\
193 	((endpoint->bmAttributes &\
194 	USB_EP_ATTR_MASK) == USB_EP_ATTR_ISOCH))
195 
196 #define	WUSB_ISOC_ENDPOINT(endpoint) (((endpoint->bmAttributes &\
197 	USB_EP_ATTR_MASK) == USB_EP_ATTR_ISOCH))
198 
199 #define	WUSB_INTR_ENDPOINT(endpoint) (((endpoint->bmAttributes &\
200 	USB_EP_ATTR_MASK) == USB_EP_ATTR_INTR))
201 
202 /* helper functions */
203 uint8_t	wusb_hc_get_cluster_id();
204 void	wusb_hc_free_cluster_id(uint8_t id);
205 int	wusb_hc_get_iehdl(wusb_hc_data_t *hc_data, wusb_ie_header_t *hdr,
206 	uint8_t *iehdl);
207 void	wusb_hc_free_iehdl(wusb_hc_data_t *hc_data, uint8_t iehdl);
208 
209 uint_t	wusb_hc_is_dev_connected(wusb_hc_data_t *hc_data, uint8_t *cdid,
210 	usb_port_t *port);
211 uint_t	wusb_hc_is_addr_valid(wusb_hc_data_t *hc_data, uint8_t addr,
212 	usb_port_t *port);
213 usb_port_t	wusb_hc_get_free_port(wusb_hc_data_t *hc_data);
214 
215 /* device notification support */
216 int	wusb_hc_ack_conn(wusb_hc_data_t *hc_data, usb_port_t port);
217 int	wusb_hc_ack_disconn(wusb_hc_data_t *hc_data, uint8_t addr);
218 void	wusb_hc_rm_ack(wusb_hc_data_t *hc_data);
219 int	wusb_hc_send_keepalive_ie(wusb_hc_data_t *hc_data, uint8_t addr);
220 int	wusb_hc_auth_dev(wusb_hc_data_t *hc_data, usb_port_t port,
221 	usb_pipe_handle_t ph, uint8_t ifc, wusb_secrt_data_t *secrt_data);
222 int	wusb_hc_handle_port_connect(wusb_hc_data_t *hc_data, usb_port_t port,
223 	usb_pipe_handle_t ph, uint8_t ifc, wusb_secrt_data_t *secrt_data);
224 void	wusb_hc_handle_dn_connect(wusb_hc_data_t *hc_data,
225 	usb_pipe_handle_t ph, uint8_t ifc, uint8_t *data, size_t len,
226 	wusb_secrt_data_t *secrt_data);
227 void	wusb_hc_handle_dn_disconnect(wusb_hc_data_t *hc_data, uint8_t addr,
228 	uint8_t *data, size_t len);
229 
230 /* wusb common device function */
231 int	wusb_create_child_devi(dev_info_t *dip, char *node_name,
232 	usba_hcdi_ops_t *usba_hcdi_ops, dev_info_t *usb_root_hub_dip,
233 	usb_port_status_t port_status, usba_device_t *usba_device,
234 	dev_info_t **child_dip);
235 int	wusb_get_dev_security_descr(usb_pipe_handle_t ph,
236 	wusb_secrt_data_t *secrt_data);
237 int	wusb_get_bos_cloud(dev_info_t *child_dip, usba_device_t *child_ud);
238 int	wusb_get_rc_dev_by_hc(dev_info_t *dip, dev_t *dev);
239 
240 int16_t	wusb_get_ccm_encryption_value(wusb_secrt_data_t *secrt_data);
241 
242 /* device dynamical configuration functions */
243 void	wusb_hc_disconnect_dev(wusb_hc_data_t *hc_data, usb_port_t port);
244 void	wusb_hc_reconnect_dev(wusb_hc_data_t *hc_data, usb_port_t port);
245 int	wusb_hc_create_child(wusb_hc_data_t *hc_data, usb_port_t port);
246 int	wusb_hc_destroy_child(wusb_hc_data_t *hc_data, usb_port_t port);
247 
248 /* WUSB HC common requests */
249 int	wusb_hc_set_cluster_id(wusb_hc_data_t *hc_data, uint8_t cluster_id);
250 
251 int	wusb_hc_set_stream_idx(wusb_hc_data_t *hc_data, uint8_t stream_idx);
252 
253 int	wusb_hc_set_wusb_mas(wusb_hc_data_t *hc_data, uint8_t *data);
254 
255 int	wusb_hc_add_mmc_ie(wusb_hc_data_t *hc_data, uint8_t interval,
256 	uint8_t rcnt, uint8_t iehdl, uint16_t len, uint8_t *data);
257 
258 int	wusb_hc_remove_mmc_ie(wusb_hc_data_t *hc_data, uint8_t iehdl);
259 void	wusb_hc_rem_ie(wusb_hc_data_t *hc_data, wusb_ie_header_t *ieh);
260 
261 int	wusb_hc_stop_ch(wusb_hc_data_t *hc_data, uint32_t timeoff);
262 
263 int	wusb_hc_set_num_dnts(wusb_hc_data_t *hc_data, uint8_t interval,
264 	uint8_t nslots);
265 
266 int	wusb_hc_get_time(wusb_hc_data_t *hc_data, uint8_t time_type,
267 	uint16_t len, uint32_t *time);
268 
269 int	wusb_hc_add_host_info(wusb_hc_data_t *hc_data, uint8_t stream_idx);
270 
271 void	wusb_hc_rem_host_info(wusb_hc_data_t *hc_data);
272 
273 int	wusb_hc_send_host_disconnect(wusb_hc_data_t *hc_data);
274 
275 int	wusb_hc_set_device_info(wusb_hc_data_t *hc_data, usb_port_t port);
276 
277 /* WUSB HC connection context list operations */
278 void	wusb_hc_add_cc(wusb_hc_cc_list_t **cc_list, wusb_hc_cc_list_t *new_cc);
279 void	wusb_hc_rem_cc(wusb_hc_cc_list_t **cc_list, wusb_cc_t *old_cc);
280 void	wusb_hc_free_cc_list(wusb_hc_cc_list_t *cc_list);
281 wusb_cc_t *wusb_hc_cc_matched(wusb_hc_cc_list_t *cc_list, uint8_t *cdid);
282 
283 /* security functions */
284 int	wusb_dev_set_encrypt(usb_pipe_handle_t ph, uint8_t value);
285 int	wusb_enable_dev_encrypt(wusb_hc_data_t *hc, wusb_dev_info_t *dev_info);
286 int	wusb_dev_set_key(usb_pipe_handle_t ph, uint8_t key_index,
287 	usb_key_descr_t *key, size_t klen);
288 int	wusb_hc_set_encrypt(wusb_hc_data_t *hc_data, usb_port_t port,
289 	uint8_t type);
290 int	wusb_hc_set_ptk(wusb_hc_data_t *hc_data, uint8_t *key_data,
291 	usb_port_t port);
292 int	wusb_hc_set_gtk(wusb_hc_data_t *hc_data, uint8_t *key_data,
293 	uint8_t *tkid);
294 
295 /* crypto functions */
296 int	PRF(const uchar_t *key, size_t klen, wusb_ccm_nonce_t *nonce,
297 	const uchar_t *adata, size_t alen,
298 	const uchar_t *bdata, size_t blen,
299 	uchar_t *out, size_t bitlen);
300 
301 #define	PRF_64(key, klen, nonce, adata, alen, bdata, blen, out)	\
302 	PRF(key, klen, nonce, adata, alen, bdata, blen, out, 64)
303 
304 #define	PRF_128(key, klen, nonce, adata, alen, bdata, blen, out)	\
305 	PRF(key, klen, nonce, adata, alen, bdata, blen, out, 128)
306 
307 #define	PRF_256(key, klen, nonce, adata, alen, bdata, blen, out)	\
308 	PRF(key, klen, nonce, adata, alen, bdata, blen, out, 256)
309 
310 int	wusb_gen_random_nonce(wusb_hc_data_t *hc_data,
311 	wusb_dev_info_t *dev_info, uchar_t *rbuf);
312 
313 int	wusb_4way_handshake(wusb_hc_data_t *hc_data, usb_port_t port,
314 	usb_pipe_handle_t ph, uint8_t ifc);
315 
316 #ifdef __cplusplus
317 }
318 #endif
319 
320 #endif	/* _SYS_USB_WHCDI_H */
321