summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/usb/usba/usba_bos.c
blob: df8bd00680665a0102ccf2a524cb46ec0d5a62a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019 Joyent, Inc.
 */

/*
 * Routines to access, parse, and manage the USB Binary Object Store
 */

#define	USBA_FRAMEWORK
#include <sys/usb/usba/usba_impl.h>
#include <sys/strsun.h>
#include <sys/sysmacros.h>

static size_t
usba_bos_parse_bos_descr(const uchar_t *buf, size_t buflen,
    usb_bos_descr_t *bosp, size_t rlen)
{
	if (buf == NULL || bosp == NULL || buflen < USB_BOS_PACKED_SIZE ||
	    buf[1] != USB_DESCR_TYPE_BOS) {
		return (USB_PARSE_ERROR);
	}

	return (usb_parse_data("ccsc", buf, buflen, bosp, rlen));
}

static boolean_t
usba_bos_parse_usb2ext(const uchar_t *buf, size_t buflen, usb_bos_t *bosp)
{
	size_t len;

	if (buflen != USB_BOS_USB2EXT_PACKED_SIZE) {
		return (B_FALSE);
	}

	len = usb_parse_data("cccl", buf, buflen, &bosp->ubos_caps.ubos_usb2,
	    sizeof (usb_bos_usb2ext_t));
	return (len == sizeof (usb_bos_usb2ext_t));
}

static boolean_t
usba_bos_parse_superspeed(const uchar_t *buf, size_t buflen, usb_bos_t *bosp)
{
	size_t len;

	if (buflen != USB_BOS_SSUSB_PACKED_SIZE) {
		return (B_FALSE);
	}

	len = usb_parse_data("ccccsccs", buf, buflen,
	    &bosp->ubos_caps.ubos_ssusb, sizeof (usb_bos_ssusb_t));
	return (len == sizeof (usb_bos_ssusb_t));
}

static boolean_t
usba_bos_parse_container(const uchar_t *buf, size_t buflen, usb_bos_t *bosp)
{
	size_t len;

	if (buflen != USB_BOS_CONTAINER_PACKED_SIZE) {
		return (B_FALSE);
	}

	len = usb_parse_data("cccc16c", buf, buflen,
	    &bosp->ubos_caps.ubos_container, sizeof (usb_bos_container_t));
	return (len == sizeof (usb_bos_container_t));
}

static boolean_t
usba_bos_parse_precision_time(const uchar_t *buf, size_t buflen,
    usb_bos_t *bosp)
{
	size_t len;

	if (buflen != USB_BOS_PRECISION_TIME_PACKED_SIZE) {
		return (B_FALSE);
	}

	len = usb_parse_data("ccc", buf, buflen, &bosp->ubos_caps.ubos_time,
	    sizeof (usb_bos_precision_time_t));
	/*
	 * The actual size of this structure will usually be rounded up to four
	 * bytes by the compiler, therefore we need to compare against the
	 * packed size.
	 */
	return (len == USB_BOS_PRECISION_TIME_PACKED_SIZE);
}

/*
 * Validate that the BOS looks reasonable. This means the following:
 *
 * - We read the whole length of the descriptor
 * - The total number of capabilities doesn't exceed the expected value
 * - The length of each device capabilities fits within our expected range
 *
 * After we finish that up, go through and save all of the valid BOS
 * descriptors, unpacking the ones that we actually understand.
 */
static boolean_t
usba_bos_save(usba_device_t *ud, const mblk_t *mp, usb_bos_descr_t *bdesc)
{
	size_t len = MBLKL(mp);
	const uchar_t *buf = mp->b_rptr;
	uint_t ncaps, nalloc;
	usb_bos_t *bos;

	if (bdesc->bLength != USB_BOS_PACKED_SIZE ||
	    bdesc->bNumDeviceCaps == 0 || len < USB_BOS_PACKED_SIZE ||
	    len < bdesc->wTotalLength) {
		return (B_FALSE);
	}

	len = MIN(len, bdesc->wTotalLength);
	buf += USB_BOS_PACKED_SIZE;
	len -= USB_BOS_PACKED_SIZE;

	if (len < USB_DEV_CAP_PACKED_SIZE) {
		return (B_FALSE);
	}

	ncaps = 0;
	while (len > 0) {
		usb_dev_cap_descr_t dev;

		if (usb_parse_data("ccc", buf, len, &dev, sizeof (dev)) !=
		    USB_DEV_CAP_PACKED_SIZE) {
			return (B_FALSE);
		}

		if (dev.bDescriptorType != USB_DESCR_TYPE_DEV_CAPABILITY ||
		    dev.bLength > len) {
			return (B_FALSE);
		}

		ncaps++;
		len -= dev.bLength;
		buf += dev.bLength;
	}

	if (ncaps != bdesc->bNumDeviceCaps) {
		return (B_FALSE);
	}

	nalloc = ncaps;
	bos = kmem_zalloc(sizeof (usb_bos_t) * nalloc, KM_SLEEP);
	buf = mp->b_rptr + USB_BOS_PACKED_SIZE;
	len = MIN(MBLKL(mp), bdesc->wTotalLength) - USB_BOS_PACKED_SIZE;
	ncaps = 0;
	while (len > 0) {
		usb_dev_cap_descr_t dev;
		boolean_t valid;

		if (usb_parse_data("ccc", buf, len, &dev, sizeof (dev)) !=
		    USB_DEV_CAP_PACKED_SIZE) {
			goto fail;
		}

		bos[ncaps].ubos_length = dev.bLength;
		bos[ncaps].ubos_type = dev.bDevCapabilityType;

		valid = B_FALSE;
		switch (dev.bDevCapabilityType) {
		case USB_BOS_TYPE_USB2_EXT:
			valid = usba_bos_parse_usb2ext(buf, dev.bLength,
			    &bos[ncaps]);
			break;
		case USB_BOS_TYPE_SUPERSPEED:
			valid = usba_bos_parse_superspeed(buf, dev.bLength,
			    &bos[ncaps]);
			break;
		case USB_BOS_TYPE_CONTAINER:
			valid = usba_bos_parse_container(buf, dev.bLength,
			    &bos[ncaps]);
			break;
		case USB_BOS_TYPE_PRECISION_TIME:
			valid = usba_bos_parse_precision_time(buf, dev.bLength,
			    &bos[ncaps]);
			break;
		default:
			/*
			 * Override the type to one that we know isn't used to
			 * indicate that the caller can't rely on the type
			 * that's present here.
			 */
			bos[ncaps].ubos_type = USB_BOS_TYPE_INVALID;
			bcopy(buf, bos[ncaps].ubos_caps.ubos_raw, dev.bLength);
			valid = B_TRUE;
			break;
		}

		if (valid) {
			ncaps++;
		} else {
			bos[ncaps].ubos_length = 0;
			bos[ncaps].ubos_type = USB_BOS_TYPE_INVALID;
			bzero(bos[ncaps].ubos_caps.ubos_raw,
			    sizeof (bos[ncaps].ubos_caps.ubos_raw));
		}
		len -= dev.bLength;
		buf += dev.bLength;
	}

	ud->usb_bos_nalloc = nalloc;
	ud->usb_bos_nents = ncaps;
	ud->usb_bos = bos;

	return (B_TRUE);

fail:
	kmem_free(bos, sizeof (usb_bos_t) * nalloc);
	return (B_FALSE);
}

/*
 * Read the Binary Object Store (BOS) data from the device and attempt to parse
 * it. Do not fail to attach the device if we cannot get all of the information
 * at this time. While certain aspects of the BOS are required for Windows,
 * which suggests that we could actually rely on it, we haven't historically.
 */
void
usba_get_binary_object_store(dev_info_t *dip, usba_device_t *ud)
{
	int			rval;
	mblk_t			*mp = NULL;
	usb_cr_t		completion_reason;
	usb_cb_flags_t		cb_flags;
	usb_pipe_handle_t	ph;
	size_t			size;
	usb_bos_descr_t		bos;

	/*
	 * The BOS is only supported on USB 3.x devices. Therefore if the bcdUSB
	 * is greater than USB 2.0, we can check this. Note, USB 3.x devices
	 * that are linked on a USB device will report version 2.1 in the bcdUSB
	 * field.
	 */
	if (ud->usb_dev_descr->bcdUSB <= 0x200) {
		return;
	}

	ph = usba_get_dflt_pipe_handle(dip);

	/*
	 * First get just the BOS descriptor itself.
	 */
	rval = usb_pipe_sync_ctrl_xfer(dip, ph,
	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
	    USB_REQ_GET_DESCR,			/* bRequest */
	    (USB_DESCR_TYPE_BOS << 8),		/* wValue */
	    0,					/* wIndex */
	    USB_BOS_PACKED_SIZE,		/* wLength */
	    &mp, USB_ATTRS_SHORT_XFER_OK,
	    &completion_reason, &cb_flags, 0);

	if (rval != USB_SUCCESS) {
		return;
	}

	size = usba_bos_parse_bos_descr(mp->b_rptr, MBLKL(mp), &bos,
	    sizeof (bos));
	freemsg(mp);
	mp = NULL;
	if (size < USB_BOS_PACKED_SIZE) {
		return;
	}

	/*
	 * Check to see if there are any capabilities and if it's worth getting
	 * the whole BOS.
	 */
	if (bos.bLength != USB_BOS_PACKED_SIZE || bos.bNumDeviceCaps == 0) {
		return;
	}

	rval = usb_pipe_sync_ctrl_xfer(dip, ph,
	    USB_DEV_REQ_DEV_TO_HOST | USB_DEV_REQ_TYPE_STANDARD,
	    USB_REQ_GET_DESCR,			/* bRequest */
	    (USB_DESCR_TYPE_BOS << 8),		/* wValue */
	    0,					/* wIndex */
	    bos.wTotalLength,			/* wLength */
	    &mp, USB_ATTRS_SHORT_XFER_OK,
	    &completion_reason, &cb_flags, 0);

	if (rval != USB_SUCCESS) {
		return;
	}

	size = usba_bos_parse_bos_descr(mp->b_rptr, MBLKL(mp), &bos,
	    sizeof (bos));
	if (size < USB_BOS_PACKED_SIZE) {
		freemsg(mp);
		return;
	}

	if (!usba_bos_save(ud, mp, &bos)) {
		freemsg(mp);
		return;
	}

	ud->usb_bos_mp = mp;
}

static void
usba_add_superspeed_props(dev_info_t *dip, usb_bos_ssusb_t *ssusb)
{
	char *supported[4];
	uint_t nsup = 0;
	char *min;

	if (ssusb->wSpeedsSupported & USB_BOS_SSUSB_SPEED_LOW) {
		supported[nsup++] = "low-speed";
	}

	if (ssusb->wSpeedsSupported & USB_BOS_SSUSB_SPEED_FULL) {
		supported[nsup++] = "full-speed";
	}

	if (ssusb->wSpeedsSupported & USB_BOS_SSUSB_SPEED_HIGH) {
		supported[nsup++] = "high-speed";
	}

	if (ssusb->wSpeedsSupported & USB_BOS_SSUSB_SPEED_SUPER) {
		supported[nsup++] = "super-speed";
	}

	if (nsup != 0 && ndi_prop_update_string_array(DDI_DEV_T_NONE, dip,
	    "usb-supported-speeds", supported, nsup) != DDI_PROP_SUCCESS) {
		USB_DPRINTF_L2(DPRINT_MASK_USBA, NULL, "failed to add "
		    "usb-supported-speeds property");
	}

	switch (ssusb->bFunctionalitySupport) {
	case 0:
		min = "low-speed";
		break;
	case 1:
		min = "full-speed";
		break;
	case 2:
		min = "high-speed";
		break;
	case 3:
		min = "super-speed";
		break;
	default:
		min = NULL;
	}

	if (min != NULL && ndi_prop_update_string(DDI_DEV_T_NONE, dip,
	    "usb-minimum-speed", min) != DDI_PROP_SUCCESS) {
		USB_DPRINTF_L2(DPRINT_MASK_USBA, NULL, "failed to add "
		    "usb-minimum-speed property");
	}
}

static void
usba_add_container_props(dev_info_t *dip, usb_bos_container_t *cp)
{
	if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, "usb-container-id",
	    cp->ContainerId, sizeof (cp->ContainerId)) != DDI_PROP_SUCCESS) {
		USB_DPRINTF_L2(DPRINT_MASK_USBA, NULL, "failed to add "
		    "usb-container-id property");
	}
}

void
usba_add_binary_object_store_props(dev_info_t *dip, usba_device_t *ud)
{
	uint_t i;

	if (ud->usb_bos == NULL) {
		return;
	}

	for (i = 0; i < ud->usb_bos_nents; i++) {
		usb_bos_t *bos = &ud->usb_bos[i];

		switch (bos->ubos_type) {
		case USB_BOS_TYPE_SUPERSPEED:
			usba_add_superspeed_props(dip,
			    &bos->ubos_caps.ubos_ssusb);
			break;
		case USB_BOS_TYPE_CONTAINER:
			usba_add_container_props(dip,
			    &bos->ubos_caps.ubos_container);
			break;
		default:
			/*
			 * This is a capability that we're not going to add
			 * devinfo properties to describe.
			 */
			continue;
		}
	}
}

void
usba_free_binary_object_store(usba_device_t *ud)
{
	if (ud->usb_bos_mp != NULL) {
		freemsg(ud->usb_bos_mp);
		ud->usb_bos_mp = NULL;
	}

	if (ud->usb_bos != NULL) {
		kmem_free(ud->usb_bos, sizeof (usb_bos_t) * ud->usb_bos_nalloc);
		ud->usb_bos = NULL;
		ud->usb_bos_nalloc = ud->usb_bos_nents = 0;
	}
}