summaryrefslogtreecommitdiff
path: root/sys/dev/usb/usb_subr.c
blob: e371603f6a965fa6a297796e64668444b815928d (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
/*	$OpenBSD: usb_subr.c,v 1.145 2018/11/20 11:51:23 mpi Exp $ */
/*	$NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $	*/
/*	$FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $	*/

/*
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) at
 * Carlstedt Research & Technology.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/selinfo.h>
#include <sys/rwlock.h>

#include <machine/bus.h>

#include <dev/usb/usb.h>

#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usb_quirks.h>

#ifdef USB_DEBUG
#define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
#define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
extern int usbdebug;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

usbd_status	usbd_set_config(struct usbd_device *, int);
void		usbd_devinfo(struct usbd_device *, int, char *, size_t);
char		*usbd_get_device_string(struct usbd_device *, uByte);
char		*usbd_get_string(struct usbd_device *, int, char *, size_t);
int		usbd_getnewaddr(struct usbd_bus *);
int		usbd_print(void *, const char *);
void		usbd_free_iface_data(struct usbd_device *, int);
int		usbd_cache_devinfo(struct usbd_device *);
usbd_status	usbd_probe_and_attach(struct device *,
		    struct usbd_device *, int, int);

int		usbd_printBCD(char *cp, size_t len, int bcd);
void		usb_free_device(struct usbd_device *);
int		usbd_parse_idesc(struct usbd_device *, struct usbd_interface *);

#ifdef USBVERBOSE
#include <dev/usb/usbdevs_data.h>
#endif /* USBVERBOSE */

const char * const usbd_error_strs[] = {
	"NORMAL_COMPLETION",
	"IN_PROGRESS",
	"PENDING_REQUESTS",
	"NOT_STARTED",
	"INVAL",
	"NOMEM",
	"CANCELLED",
	"BAD_ADDRESS",
	"IN_USE",
	"NO_ADDR",
	"SET_ADDR_FAILED",
	"NO_POWER",
	"TOO_DEEP",
	"IOERROR",
	"NOT_CONFIGURED",
	"TIMEOUT",
	"SHORT_XFER",
	"STALLED",
	"INTERRUPTED",
	"XXX",
};

const char *
usbd_errstr(usbd_status err)
{
	static char buffer[5];

	if (err < USBD_ERROR_MAX)
		return (usbd_error_strs[err]);
	else {
		snprintf(buffer, sizeof(buffer), "%d", err);
		return (buffer);
	}
}

usbd_status
usbd_get_string_desc(struct usbd_device *dev, int sindex, int langid,
    usb_string_descriptor_t *sdesc, int *sizep)
{
	usb_device_request_t req;
	usbd_status err;
	int actlen;

	req.bmRequestType = UT_READ_DEVICE;
	req.bRequest = UR_GET_DESCRIPTOR;
	USETW2(req.wValue, UDESC_STRING, sindex);
	USETW(req.wIndex, langid);
	USETW(req.wLength, 2);	/* size and descriptor type first */
	err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
	    &actlen, USBD_DEFAULT_TIMEOUT);
	if (err)
		return (err);

	if (actlen < 2)
		return (USBD_SHORT_XFER);

	USETW(req.wLength, sdesc->bLength);	/* the whole string */
	err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK,
	    &actlen, USBD_DEFAULT_TIMEOUT);
	if (err)
		return (err);

	if (actlen != sdesc->bLength) {
		DPRINTFN(-1, ("%s: expected %d, got %d\n", __func__,
		    sdesc->bLength, actlen));
	}

	*sizep = actlen;
	return (USBD_NORMAL_COMPLETION);
}

char *
usbd_get_string(struct usbd_device *dev, int si, char *buf, size_t buflen)
{
	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
	usb_string_descriptor_t us;
	char *s;
	int i, n;
	u_int16_t c;
	usbd_status err;
	int size;

	if (si == 0)
		return (0);
	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
		return (0);
	if (dev->langid == USBD_NOLANG) {
		/* Set up default language */
		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
		    &size);
		if (err || size < 4)
			dev->langid = 0; /* Well, just pick English then */
		else {
			/* Pick the first language as the default. */
			dev->langid = UGETW(us.bString[0]);
		}
	}
	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
	if (err)
		return (0);
	s = buf;
	n = size / 2 - 1;
	for (i = 0; i < n && i < buflen ; i++) {
		c = UGETW(us.bString[i]);
		/* Convert from Unicode, handle buggy strings. */
		if ((c & 0xff00) == 0)
			*s++ = c;
		else if ((c & 0x00ff) == 0 && swap)
			*s++ = c >> 8;
		else
			*s++ = '?';
	}
	if (buflen > 0)
		*s++ = 0;
	return (buf);
}

static void
usbd_trim_spaces(char *p)
{
	char *q, *e;

	if (p == NULL)
		return;
	q = e = p;
	while (*q == ' ')	/* skip leading spaces */
		q++;
	while ((*p = *q++))	/* copy string */
		if (*p++ != ' ') /* remember last non-space */
			e = p;
	*e = 0;			/* kill trailing spaces */
}

char *
usbd_get_device_string(struct usbd_device *dev, uByte index)
{
	char *buf;

	buf = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT);
	if (buf == NULL)
		return (NULL);

	if (usbd_get_string(dev, index, buf, USB_MAX_STRING_LEN) != NULL) {
		usbd_trim_spaces(buf);
	} else {
		free(buf, M_USB, USB_MAX_STRING_LEN);
		buf = NULL;
	}

	return (buf);
}

int
usbd_cache_devinfo(struct usbd_device *dev)
{
	usb_device_descriptor_t *udd = &dev->ddesc;

	dev->serial = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT);
	if (dev->serial == NULL)
		return (ENOMEM);

	if (usbd_get_string(dev, udd->iSerialNumber, dev->serial, USB_MAX_STRING_LEN) != NULL) {
		usbd_trim_spaces(dev->serial);
	} else {
		free(dev->serial, M_USB, USB_MAX_STRING_LEN);
		dev->serial = NULL;
	}

	dev->vendor = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT);
	if (dev->vendor == NULL)
		return (ENOMEM);

	if (usbd_get_string(dev, udd->iManufacturer, dev->vendor, USB_MAX_STRING_LEN) != NULL) {
		usbd_trim_spaces(dev->vendor);
	} else {
#ifdef USBVERBOSE
		const struct usb_known_vendor *ukv;

		for (ukv = usb_known_vendors; ukv->vendorname != NULL; ukv++) {
			if (ukv->vendor == UGETW(udd->idVendor)) {
				strlcpy(dev->vendor, ukv->vendorname,
				    USB_MAX_STRING_LEN);
				break;
			}
		}
		if (ukv->vendorname == NULL)
#endif
			snprintf(dev->vendor, USB_MAX_STRING_LEN, "vendor 0x%04x",
			    UGETW(udd->idVendor));
	}

	dev->product = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT);
	if (dev->product == NULL)
		return (ENOMEM);

	if (usbd_get_string(dev, udd->iProduct, dev->product, USB_MAX_STRING_LEN) != NULL) {
		usbd_trim_spaces(dev->product);
	} else {
#ifdef USBVERBOSE
		const struct usb_known_product *ukp;

		for (ukp = usb_known_products; ukp->productname != NULL; ukp++) {
			if (ukp->vendor == UGETW(udd->idVendor) &&
			    (ukp->product == UGETW(udd->idProduct))) {
				strlcpy(dev->product, ukp->productname,
				    USB_MAX_STRING_LEN);
				break;
			}
		}
		if (ukp->productname == NULL)
#endif
			snprintf(dev->product, USB_MAX_STRING_LEN, "product 0x%04x",
			    UGETW(udd->idProduct));
	}

	return (0);
}

int
usbd_printBCD(char *cp, size_t len, int bcd)
{
	int l;

	l = snprintf(cp, len, "%x.%02x", bcd >> 8, bcd & 0xff);
	if (l == -1 || len == 0)
		return (0);
	if (l >= len)
		return len - 1;
	return (l);
}

void
usbd_devinfo(struct usbd_device *dev, int showclass, char *base, size_t len)
{
	usb_device_descriptor_t *udd = &dev->ddesc;
	char *cp = base;
	int bcdDevice, bcdUSB;

	snprintf(cp, len, "\"%s %s\"", dev->vendor, dev->product);
	cp += strlen(cp);
	if (showclass) {
		snprintf(cp, base + len - cp, ", class %d/%d",
		    udd->bDeviceClass, udd->bDeviceSubClass);
		cp += strlen(cp);
	}
	bcdUSB = UGETW(udd->bcdUSB);
	bcdDevice = UGETW(udd->bcdDevice);
	snprintf(cp, base + len - cp, " rev ");
	cp += strlen(cp);
	usbd_printBCD(cp, base + len - cp, bcdUSB);
	cp += strlen(cp);
	snprintf(cp, base + len - cp, "/");
	cp += strlen(cp);
	usbd_printBCD(cp, base + len - cp, bcdDevice);
	cp += strlen(cp);
	snprintf(cp, base + len - cp, " addr %d", dev->address);
}

/* Delay for a certain number of ms */
void
usb_delay_ms(struct usbd_bus *bus, u_int ms)
{
	static int usb_delay_wchan;

	/* Wait at least two clock ticks so we know the time has passed. */
	if (bus->use_polling || cold)
		delay((ms+1) * 1000);
	else
		tsleep(&usb_delay_wchan, PRIBIO, "usbdly",
		    (ms*hz+999)/1000 + 1);
}

/* Delay given a device handle. */
void
usbd_delay_ms(struct usbd_device *dev, u_int ms)
{
	if (usbd_is_dying(dev))
		return;

	usb_delay_ms(dev->bus, ms);
}

usbd_status
usbd_port_disown_to_1_1(struct usbd_device *dev, int port)
{
	usb_port_status_t ps;
	usbd_status err;
	int n;

	err = usbd_set_port_feature(dev, port, UHF_PORT_DISOWN_TO_1_1);
	DPRINTF(("%s: port %d disown request done, error=%s\n", __func__,
	    port, usbd_errstr(err)));
	if (err)
		return (err);
	n = 10;
	do {
		/* Wait for device to recover from reset. */
		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
		err = usbd_get_port_status(dev, port, &ps);
		if (err) {
			DPRINTF(("%s: get status failed %d\n", __func__, err));
			return (err);
		}
		/* If the device disappeared, just give up. */
		if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
			return (USBD_NORMAL_COMPLETION);
	} while ((UGETW(ps.wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);
	if (n == 0)
		return (USBD_TIMEOUT);

	return (err);
}

int
usbd_reset_port(struct usbd_device *dev, int port)
{
	usb_port_status_t ps;
	int n;

	if (usbd_set_port_feature(dev, port, UHF_PORT_RESET))
		return (EIO);
	DPRINTF(("%s: port %d reset done\n", __func__, port));
	n = 10;
	do {
		/* Wait for device to recover from reset. */
		usbd_delay_ms(dev, USB_PORT_RESET_DELAY);
		if (usbd_get_port_status(dev, port, &ps)) {
			DPRINTF(("%s: get status failed\n", __func__));
			return (EIO);
		}
		/* If the device disappeared, just give up. */
		if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS))
			return (0);
	} while ((UGETW(ps.wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0);

	/* Clear port reset even if a timeout occured. */
	if (usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET)) {
		DPRINTF(("%s: clear port feature failed\n", __func__));
		return (EIO);
	}

	if (n == 0)
		return (ETIMEDOUT);

	/* Wait for the device to recover from reset. */
	usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
	return (0);
}

usb_interface_descriptor_t *
usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
{
	char *p = (char *)cd;
	char *end = p + UGETW(cd->wTotalLength);
	usb_interface_descriptor_t *d;
	int curidx, lastidx, curaidx = 0;

	for (curidx = lastidx = -1; p < end; ) {
		d = (usb_interface_descriptor_t *)p;
		DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d "
			    "type=%d\n",
			    ifaceidx, curidx, altidx, curaidx,
			    d->bLength, d->bDescriptorType));
		if (d->bLength == 0) /* bad descriptor */
			break;
		p += d->bLength;
		if (p <= end && d->bDescriptorType == UDESC_INTERFACE) {
			if (d->bInterfaceNumber != lastidx) {
				lastidx = d->bInterfaceNumber;
				curidx++;
				curaidx = 0;
			} else
				curaidx++;
			if (ifaceidx == curidx && altidx == curaidx)
				return (d);
		}
	}
	return (NULL);
}

usb_endpoint_descriptor_t *
usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
		int endptidx)
{
	char *p = (char *)cd;
	char *end = p + UGETW(cd->wTotalLength);
	usb_interface_descriptor_t *d;
	usb_endpoint_descriptor_t *e;
	int curidx;

	d = usbd_find_idesc(cd, ifaceidx, altidx);
	if (d == NULL)
		return (NULL);
	if (endptidx >= d->bNumEndpoints) /* quick exit */
		return (NULL);

	curidx = -1;
	for (p = (char *)d + d->bLength; p < end; ) {
		e = (usb_endpoint_descriptor_t *)p;
		if (e->bLength == 0) /* bad descriptor */
			break;
		p += e->bLength;
		if (p <= end && e->bDescriptorType == UDESC_INTERFACE)
			return (NULL);
		if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) {
			curidx++;
			if (curidx == endptidx)
				return (e);
		}
	}
	return (NULL);
}

usbd_status
usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx)
{
	struct usbd_interface *ifc = &dev->ifaces[ifaceidx];
	usb_interface_descriptor_t *idesc;
	int nendpt;

	DPRINTFN(4,("%s: ifaceidx=%d altidx=%d\n", __func__, ifaceidx, altidx));

	idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx);
	if (idesc == NULL)
		return (USBD_INVAL);

	nendpt = idesc->bNumEndpoints;
	DPRINTFN(4,("%s: found idesc nendpt=%d\n", __func__, nendpt));

	ifc->device = dev;
	ifc->idesc = idesc;
	ifc->index = ifaceidx;
	ifc->altindex = altidx;
	ifc->endpoints = NULL;
	ifc->priv = NULL;
	LIST_INIT(&ifc->pipes);
	ifc->nendpt = nendpt;

	if (nendpt != 0) {
		ifc->endpoints = mallocarray(nendpt, sizeof(*ifc->endpoints),
		    M_USB, M_NOWAIT | M_ZERO);
		if (ifc->endpoints == NULL)
			return (USBD_NOMEM);
	}

	if (usbd_parse_idesc(dev, ifc)) {
		free(ifc->endpoints, M_USB, nendpt * sizeof(*ifc->endpoints));
		ifc->endpoints = NULL;
		return (USBD_INVAL);
	}

	return (USBD_NORMAL_COMPLETION);
}

int
usbd_parse_idesc(struct usbd_device *dev, struct usbd_interface *ifc)
{
#define ed ((usb_endpoint_descriptor_t *)p)
	char *p, *end;
	int i;

	p = (char *)ifc->idesc + ifc->idesc->bLength;
	end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength);

	for (i = 0; i < ifc->idesc->bNumEndpoints; i++) {
		for (; p < end; p += ed->bLength) {
			if (p + ed->bLength <= end && ed->bLength != 0 &&
			    ed->bDescriptorType == UDESC_ENDPOINT)
				break;

			if (ed->bLength == 0 ||
			    ed->bDescriptorType == UDESC_INTERFACE)
			    	return (-1);
		}

		if (p >= end)
			return (-1);

		if (dev->speed == USB_SPEED_HIGH) {
			unsigned int mps;

			/* Control and bulk endpoints have max packet limits. */
			switch (UE_GET_XFERTYPE(ed->bmAttributes)) {
			case UE_CONTROL:
				mps = USB_2_MAX_CTRL_PACKET;
				goto check;
			case UE_BULK:
				mps = USB_2_MAX_BULK_PACKET;
			check:
				if (UGETW(ed->wMaxPacketSize) != mps) {
					USETW(ed->wMaxPacketSize, mps);
					DPRINTF(("%s: bad max packet size\n",
					    __func__));
				}
				break;
			default:
				break;
			}
		}

		ifc->endpoints[i].edesc = ed;
		ifc->endpoints[i].refcnt = 0;
		ifc->endpoints[i].savedtoggle = 0;
		p += ed->bLength;
	}

	return (0);
#undef ed
}

void
usbd_free_iface_data(struct usbd_device *dev, int ifcno)
{
	struct usbd_interface *ifc = &dev->ifaces[ifcno];

	free(ifc->endpoints, M_USB, ifc->nendpt * sizeof(*ifc->endpoints));
	ifc->endpoints = NULL;
}

usbd_status
usbd_set_config(struct usbd_device *dev, int conf)
{
	usb_device_request_t req;

	req.bmRequestType = UT_WRITE_DEVICE;
	req.bRequest = UR_SET_CONFIG;
	USETW(req.wValue, conf);
	USETW(req.wIndex, 0);
	USETW(req.wLength, 0);
	return (usbd_do_request(dev, &req, 0));
}

usbd_status
usbd_set_config_no(struct usbd_device *dev, int no, int msg)
{
	int index;
	usb_config_descriptor_t cd;
	usbd_status err;

	DPRINTFN(5,("%s: %d\n", __func__, no));
	/* Figure out what config index to use. */
	for (index = 0; index < dev->ddesc.bNumConfigurations; index++) {
		err = usbd_get_desc(dev, UDESC_CONFIG, index,
		    USB_CONFIG_DESCRIPTOR_SIZE, &cd);
		if (err || cd.bDescriptorType != UDESC_CONFIG)
			return (err);
		if (cd.bConfigurationValue == no)
			return (usbd_set_config_index(dev, index, msg));
	}
	return (USBD_INVAL);
}

usbd_status
usbd_set_config_index(struct usbd_device *dev, int index, int msg)
{
	usb_status_t ds;
	usb_config_descriptor_t cd, *cdp;
	usbd_status err;
	int i, ifcidx, nifc, cdplen, selfpowered, power;

	DPRINTFN(5,("%s: dev=%p index=%d\n", __func__, dev, index));

	/* XXX check that all interfaces are idle */
	if (dev->config != USB_UNCONFIG_NO) {
		DPRINTF(("%s: free old config\n", __func__));
		/* Free all configuration data structures. */
		nifc = dev->cdesc->bNumInterface;
		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
			usbd_free_iface_data(dev, ifcidx);
		free(dev->ifaces, M_USB, nifc * sizeof(*dev->ifaces));
		free(dev->cdesc, M_USB, UGETW(dev->cdesc->wTotalLength));
		dev->ifaces = NULL;
		dev->cdesc = NULL;
		dev->config = USB_UNCONFIG_NO;
	}

	if (index == USB_UNCONFIG_INDEX) {
		/* We are unconfiguring the device, so leave unallocated. */
		DPRINTF(("%s: set config 0\n", __func__));
		err = usbd_set_config(dev, USB_UNCONFIG_NO);
		if (err)
			DPRINTF(("%s: setting config=0 failed, error=%s\n",
			    __func__, usbd_errstr(err)));
		return (err);
	}

	/* Get the short descriptor. */
	err = usbd_get_desc(dev, UDESC_CONFIG, index,
	    USB_CONFIG_DESCRIPTOR_SIZE, &cd);
	if (err)
		return (err);
	if (cd.bDescriptorType != UDESC_CONFIG)
		return (USBD_INVAL);
	cdplen = UGETW(cd.wTotalLength);
	cdp = malloc(cdplen, M_USB, M_NOWAIT);
	if (cdp == NULL)
		return (USBD_NOMEM);
	/* Get the full descriptor. */
	for (i = 0; i < 3; i++) {
		err = usbd_get_desc(dev, UDESC_CONFIG, index, cdplen, cdp);
		if (!err)
			break;
		usbd_delay_ms(dev, 200);
	}
	if (err)
		goto bad;

	if (cdp->bDescriptorType != UDESC_CONFIG) {
		DPRINTFN(-1,("%s: bad desc %d\n", __func__,
		    cdp->bDescriptorType));
		err = USBD_INVAL;
		goto bad;
	}

	/* Figure out if the device is self or bus powered. */
	selfpowered = 0;
	if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) &&
	    (cdp->bmAttributes & UC_SELF_POWERED)) {
		/* May be self powered. */
		if (cdp->bmAttributes & UC_BUS_POWERED) {
			/* Must ask device. */
			if (dev->quirks->uq_flags & UQ_POWER_CLAIM) {
				/*
				 * Hub claims to be self powered, but isn't.
				 * It seems that the power status can be
				 * determined by the hub characteristics.
				 */
				usb_hub_descriptor_t hd;
				usb_device_request_t req;
				req.bmRequestType = UT_READ_CLASS_DEVICE;
				req.bRequest = UR_GET_DESCRIPTOR;
				USETW(req.wValue, 0);
				USETW(req.wIndex, 0);
				USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
				err = usbd_do_request(dev, &req, &hd);
				if (!err &&
				    (UGETW(hd.wHubCharacteristics) &
				     UHD_PWR_INDIVIDUAL))
					selfpowered = 1;
				DPRINTF(("%s: charac=0x%04x, error=%s\n",
				    __func__, UGETW(hd.wHubCharacteristics),
				    usbd_errstr(err)));
			} else {
				err = usbd_get_device_status(dev, &ds);
				if (!err &&
				    (UGETW(ds.wStatus) & UDS_SELF_POWERED))
					selfpowered = 1;
				DPRINTF(("%s: status=0x%04x, error=%s\n",
				    __func__, UGETW(ds.wStatus),
				    usbd_errstr(err)));
			}
		} else
			selfpowered = 1;
	}
	DPRINTF(("%s: (addr %d) cno=%d attr=0x%02x, selfpowered=%d, power=%d\n",
	    __func__, dev->address, cdp->bConfigurationValue, cdp->bmAttributes,
	    selfpowered, cdp->bMaxPower * 2));

	/* Check if we have enough power. */
#ifdef USB_DEBUG
	if (dev->powersrc == NULL) {
		DPRINTF(("%s: No power source?\n", __func__));
		err = USBD_IOERROR;
		goto bad;
	}
#endif
	power = cdp->bMaxPower * 2;
	if (power > dev->powersrc->power) {
		DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power));
		/* XXX print nicer message. */
		if (msg)
			printf("%s: device addr %d (config %d) exceeds power "
			    "budget, %d mA > %d mA\n",
			    dev->bus->bdev.dv_xname, dev->address,
			    cdp->bConfigurationValue,
			    power, dev->powersrc->power);
		err = USBD_NO_POWER;
		goto bad;
	}
	dev->power = power;
	dev->self_powered = selfpowered;

	/* Set the actual configuration value. */
	DPRINTF(("%s: set config %d\n", __func__, cdp->bConfigurationValue));
	err = usbd_set_config(dev, cdp->bConfigurationValue);
	if (err) {
		DPRINTF(("%s: setting config=%d failed, error=%s\n", __func__,
		    cdp->bConfigurationValue, usbd_errstr(err)));
		goto bad;
	}

	/* Allocate and fill interface data. */
	nifc = cdp->bNumInterface;
	dev->ifaces = mallocarray(nifc, sizeof(*dev->ifaces), M_USB,
	    M_NOWAIT | M_ZERO);
	if (dev->ifaces == NULL) {
		err = USBD_NOMEM;
		goto bad;
	}
	DPRINTFN(5,("%s: dev=%p cdesc=%p\n", __func__, dev, cdp));
	dev->cdesc = cdp;
	dev->config = cdp->bConfigurationValue;
	for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
		err = usbd_fill_iface_data(dev, ifcidx, 0);
		if (err)
			return (err);
	}

	return (USBD_NORMAL_COMPLETION);

 bad:
	free(cdp, M_USB, cdplen);
	return (err);
}

/* XXX add function for alternate settings */

usbd_status
usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface,
    struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe)
{
	struct usbd_pipe *p;
	usbd_status err;

	DPRINTF(("%s: dev=%p iface=%p ep=%p pipe=%p\n", __func__,
		    dev, iface, ep, pipe));
	p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT|M_ZERO);
	if (p == NULL)
		return (USBD_NOMEM);
	p->pipe_size = dev->bus->pipe_size;
	p->device = dev;
	p->iface = iface;
	p->endpoint = ep;
	ep->refcnt++;
	p->interval = ival;
	SIMPLEQ_INIT(&p->queue);
	err = dev->bus->methods->open_pipe(p);
	if (err) {
		DPRINTF(("%s: endpoint=0x%x failed, error=%s\n", __func__,
			 ep->edesc->bEndpointAddress, usbd_errstr(err)));
		free(p, M_USB, dev->bus->pipe_size);
		return (err);
	}
	*pipe = p;
	return (USBD_NORMAL_COMPLETION);
}

int
usbd_set_address(struct usbd_device *dev, int addr)
{
	usb_device_request_t req;

	req.bmRequestType = UT_WRITE_DEVICE;
	req.bRequest = UR_SET_ADDRESS;
	USETW(req.wValue, addr);
	USETW(req.wIndex, 0);
	USETW(req.wLength, 0);
	if (usbd_do_request(dev, &req, 0))
		return (1);

	/* Allow device time to set new address */
	usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);

	return (0);
}

int
usbd_getnewaddr(struct usbd_bus *bus)
{
	int addr;

	for (addr = 1; addr < USB_MAX_DEVICES; addr++)
		if (bus->devices[addr] == NULL)
			return (addr);
	return (-1);
}

usbd_status
usbd_probe_and_attach(struct device *parent, struct usbd_device *dev, int port,
    int addr)
{
	struct usb_attach_arg uaa;
	usb_device_descriptor_t *dd = &dev->ddesc;
	int i, confi, nifaces, len;
	usbd_status err;
	struct device *dv;
	struct usbd_interface **ifaces;
	extern struct rwlock usbpalock;

	rw_enter_write(&usbpalock);

	uaa.device = dev;
	uaa.iface = NULL;
	uaa.ifaces = NULL;
	uaa.nifaces = 0;
	uaa.usegeneric = 0;
	uaa.port = port;
	uaa.configno = UHUB_UNK_CONFIGURATION;
	uaa.ifaceno = UHUB_UNK_INTERFACE;
	uaa.vendor = UGETW(dd->idVendor);
	uaa.product = UGETW(dd->idProduct);
	uaa.release = UGETW(dd->bcdDevice);

	/* First try with device specific drivers. */
	DPRINTF(("usbd_probe_and_attach trying device specific drivers\n"));
	dv = config_found(parent, &uaa, usbd_print);
	if (dv) {
		dev->subdevs = mallocarray(2, sizeof dv, M_USB, M_NOWAIT);
		if (dev->subdevs == NULL) {
			err = USBD_NOMEM;
			goto fail;
		}
		dev->subdevs[dev->ndevs++] = dv;
		dev->subdevs[dev->ndevs] = 0;
		err = USBD_NORMAL_COMPLETION;
		goto fail;
	}

	DPRINTF(("%s: no device specific driver found\n", __func__));

	DPRINTF(("%s: looping over %d configurations\n", __func__,
		 dd->bNumConfigurations));
	/* Next try with interface drivers. */
	for (confi = 0; confi < dd->bNumConfigurations; confi++) {
		DPRINTFN(1,("%s: trying config idx=%d\n", __func__,
			    confi));
		err = usbd_set_config_index(dev, confi, 1);
		if (err) {
#ifdef USB_DEBUG
			DPRINTF(("%s: port %d, set config at addr %d failed, "
				 "error=%s\n", parent->dv_xname, port,
				 addr, usbd_errstr(err)));
#else
			printf("%s: port %d, set config %d at addr %d failed\n",
			    parent->dv_xname, port, confi, addr);
#endif

 			goto fail;
		}
		nifaces = dev->cdesc->bNumInterface;
		uaa.configno = dev->cdesc->bConfigurationValue;
		ifaces = mallocarray(nifaces, sizeof(*ifaces), M_USB, M_NOWAIT);
		if (ifaces == NULL) {
			err = USBD_NOMEM;
			goto fail;
		}
		for (i = 0; i < nifaces; i++)
			ifaces[i] = &dev->ifaces[i];
		uaa.ifaces = ifaces;
		uaa.nifaces = nifaces;

		/* add 1 for possible ugen and 1 for NULL terminator */
		dev->subdevs = mallocarray(nifaces + 2, sizeof(dv), M_USB,
		    M_NOWAIT | M_ZERO);
		if (dev->subdevs == NULL) {
			free(ifaces, M_USB, nifaces * sizeof(*ifaces));
			err = USBD_NOMEM;
			goto fail;
		}
		len = (nifaces + 2) * sizeof(dv);

		for (i = 0; i < nifaces; i++) {
			if (usbd_iface_claimed(dev, i))
				continue;
			uaa.iface = ifaces[i];
			uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber;
			dv = config_found(parent, &uaa, usbd_print);
			if (dv != NULL) {
				dev->subdevs[dev->ndevs++] = dv;
				usbd_claim_iface(dev, i);
			}
		}
		free(ifaces, M_USB, nifaces * sizeof(*ifaces));

		if (dev->ndevs > 0) {
			for (i = 0; i < nifaces; i++) {
				if (!usbd_iface_claimed(dev, i))
					break;
			}
			if (i < nifaces)
				goto generic;
			 else
				goto fail;
		}

		free(dev->subdevs, M_USB, (nifaces + 2) * sizeof(dv));
		dev->subdevs = NULL;
	}
	/* No interfaces were attached in any of the configurations. */

	if (dd->bNumConfigurations > 1) /* don't change if only 1 config */
		usbd_set_config_index(dev, 0, 0);

	DPRINTF(("%s: no interface drivers found\n", __func__));

generic:
	/* Finally try the generic driver. */
	uaa.iface = NULL;
	uaa.usegeneric = 1;
	uaa.configno = dev->ndevs == 0 ? UHUB_UNK_CONFIGURATION :
	    dev->cdesc->bConfigurationValue;
	uaa.ifaceno = UHUB_UNK_INTERFACE;
	dv = config_found(parent, &uaa, usbd_print);
	if (dv != NULL) {
		if (dev->ndevs == 0) {
			dev->subdevs = mallocarray(2, sizeof dv, M_USB, M_NOWAIT);
			if (dev->subdevs == NULL) {
				err = USBD_NOMEM;
				goto fail;
			}
		}
		dev->subdevs[dev->ndevs++] = dv;
		dev->subdevs[dev->ndevs] = 0;
		err = USBD_NORMAL_COMPLETION;
		goto fail;
	}

	/*
	 * The generic attach failed, but leave the device as it is.
	 * We just did not find any drivers, that's all.  The device is
	 * fully operational and not harming anyone.
	 */
	DPRINTF(("%s: generic attach failed\n", __func__));
 	err = USBD_NORMAL_COMPLETION;
fail:
	rw_exit_write(&usbpalock);
	return (err);
}


/*
 * Called when a new device has been put in the powered state,
 * but not yet in the addressed state.
 * Get initial descriptor, set the address, get full descriptor,
 * and attach a driver.
 */
usbd_status
usbd_new_device(struct device *parent, struct usbd_bus *bus, int depth,
		int speed, int port, struct usbd_port *up)
{
	struct usbd_device *dev, *adev, *hub;
	usb_device_descriptor_t *dd;
	usbd_status err;
	uint32_t mps, mps0;
	int addr, i, p;

	DPRINTF(("%s: bus=%p port=%d depth=%d speed=%d\n", __func__,
		 bus, port, depth, speed));

	/*
	 * Fixed size for ep0 max packet, FULL device variable size is
	 * handled below.
	 */
	switch (speed) {
	case USB_SPEED_LOW:
		mps0 = 8;
		break;
	case USB_SPEED_HIGH:
	case USB_SPEED_FULL:
		mps0 = 64;
		break;
	case USB_SPEED_SUPER:
		mps0 = 512;
		break;
	default:
		return (USBD_INVAL);
	}

	addr = usbd_getnewaddr(bus);
	if (addr < 0) {
		printf("%s: No free USB addresses, new device ignored.\n",
		    bus->bdev.dv_xname);
		return (USBD_NO_ADDR);
	}

	dev = malloc(sizeof *dev, M_USB, M_NOWAIT | M_ZERO);
	if (dev == NULL)
		return (USBD_NOMEM);

	dev->bus = bus;

	/* Set up default endpoint handle. */
	dev->def_ep.edesc = &dev->def_ep_desc;

	/* Set up default endpoint descriptor. */
	dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE;
	dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT;
	dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
	dev->def_ep_desc.bmAttributes = UE_CONTROL;
	dev->def_ep_desc.bInterval = 0;
	USETW(dev->def_ep_desc.wMaxPacketSize, mps0);

	dev->quirks = &usbd_no_quirk;
	dev->address = USB_START_ADDR;
	dev->ddesc.bMaxPacketSize = 0;
	dev->depth = depth;
	dev->powersrc = up;
	dev->myhub = up->parent;
	dev->speed = speed;
	dev->langid = USBD_NOLANG;

	up->device = dev;

	/* Locate port on upstream high speed hub */
	for (adev = dev, hub = up->parent;
	    hub != NULL && hub->speed != USB_SPEED_HIGH;
	    adev = hub, hub = hub->myhub)
		;
	if (hub) {
		for (p = 0; p < hub->hub->nports; p++) {
			if (hub->hub->ports[p].device == adev) {
				dev->myhsport = &hub->hub->ports[p];
				goto found;
			}
		}
		panic("usbd_new_device: cannot find HS port");
	found:
		DPRINTFN(1,("%s: high speed port %d\n", __func__, p));
	} else {
		dev->myhsport = NULL;
	}

	/* Establish the default pipe. */
	err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL,
	    &dev->default_pipe);
	if (err) {
		usb_free_device(dev);
		up->device = NULL;
		return (err);
	}

	dd = &dev->ddesc;

	/* Try to get device descriptor */
	/* 
	 * some device will need small size query at first (XXX: out of spec)
	 * we will get full size descriptor later, just determin the maximum
	 * packet size of the control pipe at this moment.
	 */
	for (i = 0; i < 3; i++) {
		/* Get the first 8 bytes of the device descriptor. */
		/* 8 byte is magic size, some device only return 8 byte for 1st
		 * query (XXX: out of spec) */
		err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd);
		if (!err)
			break;
		usbd_delay_ms(dev, 100+50*i);
	}

	/* some device need actual size request for the query. try again */
	if (err) {
		USETW(dev->def_ep_desc.wMaxPacketSize,
			USB_DEVICE_DESCRIPTOR_SIZE);
		usbd_reset_port(up->parent, port);
		for (i = 0; i < 3; i++) {
			err = usbd_get_desc(dev, UDESC_DEVICE, 0, 
				USB_DEVICE_DESCRIPTOR_SIZE, dd);
			if (!err)
				break;
			usbd_delay_ms(dev, 100+50*i);
		}
	}

	/* XXX some devices need more time to wake up */
	if (err) {
		USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET);
		usbd_reset_port(up->parent, port);
		usbd_delay_ms(dev, 500);
		err = usbd_get_desc(dev, UDESC_DEVICE, 0, 
			USB_MAX_IPACKET, dd);
	}

	if (err) {
		usb_free_device(dev);
		up->device = NULL;
		return (err);
	}

	DPRINTF(("%s: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, "
		 "protocol=%d, maxpacket=%d, len=%d, speed=%d\n", __func__,
		 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass,
		 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength,
		 dev->speed));

	if ((dd->bDescriptorType != UDESC_DEVICE) ||
	    (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE)) {
		usb_free_device(dev);
		up->device = NULL;
		return (USBD_INVAL);
	}

	mps = dd->bMaxPacketSize;
	if (speed == USB_SPEED_SUPER) {
		if (mps == 0xff)
			mps = 9;
		/* xHCI Section 4.8.2.1 */
		mps = (1 << mps);
	}

	if (mps != mps0) {
		if ((speed == USB_SPEED_LOW) ||
		    (mps != 8 && mps != 16 && mps != 32 && mps != 64)) {
			usb_free_device(dev);
			up->device = NULL;
			return (USBD_INVAL);
		}
		USETW(dev->def_ep_desc.wMaxPacketSize, mps);
	}


	/* Set the address if the HC didn't do it already. */
	if (bus->methods->dev_setaddr != NULL &&
	    bus->methods->dev_setaddr(dev, addr)) {
		usb_free_device(dev);
		up->device = NULL;
		return (USBD_SET_ADDR_FAILED);
 	}

	/* Wait for device to settle before reloading the descriptor. */
	usbd_delay_ms(dev, 10);

	/*
	 * If this device is attached to an xHCI controller, this
	 * address does not correspond to the hardware one.
	 */
	dev->address = addr;
	bus->devices[addr] = dev;

	err = usbd_reload_device_desc(dev);
	if (err) {
		usb_free_device(dev);
		up->device = NULL;
		return (err);
	}

	/* send disown request to handover 2.0 to 1.1. */
	if (dev->quirks->uq_flags & UQ_EHCI_NEEDTO_DISOWN) {
		/* only effective when the target device is on ehci */
		if (dev->bus->usbrev == USBREV_2_0) {
			DPRINTF(("%s: disown request issues to dev:%p on usb2.0 bus\n",
				__func__, dev));
			usbd_port_disown_to_1_1(dev->myhub, port);
			/* reset_port required to finish disown request */
			usbd_reset_port(dev->myhub, port);
  			return (USBD_NORMAL_COMPLETION);
		}
	}

	/* Assume 100mA bus powered for now. Changed when configured. */
	dev->power = USB_MIN_POWER;
	dev->self_powered = 0;

	DPRINTF(("%s: new dev (addr %d), dev=%p, parent=%p\n", __func__,
		 addr, dev, parent));

	/* Get device info and cache it */
	err = usbd_cache_devinfo(dev);
	if (err) {
		usb_free_device(dev);
		up->device = NULL;
		return (err);
  	}

	err = usbd_probe_and_attach(parent, dev, port, addr);
	if (err) {
		usb_free_device(dev);
		up->device = NULL;
		return (err);
  	}

  	return (USBD_NORMAL_COMPLETION);
}

usbd_status
usbd_reload_device_desc(struct usbd_device *dev)
{
	usbd_status err;

	/* Get the full device descriptor. */
	err = usbd_get_desc(dev, UDESC_DEVICE, 0,
		USB_DEVICE_DESCRIPTOR_SIZE, &dev->ddesc);
	if (err)
		return (err);

	/* Figure out what's wrong with this device. */
	dev->quirks = usbd_find_quirk(&dev->ddesc);

	return (USBD_NORMAL_COMPLETION);
}

int
usbd_print(void *aux, const char *pnp)
{
	struct usb_attach_arg *uaa = aux;
	char *devinfop;

	devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK);
	usbd_devinfo(uaa->device, 0, devinfop, DEVINFOSIZE);

	DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device));
	if (pnp) {
		if (!uaa->usegeneric) {
			free(devinfop, M_TEMP, DEVINFOSIZE);
			return (QUIET);
		}
		printf("%s at %s", devinfop, pnp);
	}
	if (uaa->port != 0)
		printf(" port %d", uaa->port);
	if (uaa->configno != UHUB_UNK_CONFIGURATION)
		printf(" configuration %d", uaa->configno);
	if (uaa->ifaceno != UHUB_UNK_INTERFACE)
		printf(" interface %d", uaa->ifaceno);

	if (!pnp)
		printf(" %s\n", devinfop);
	free(devinfop, M_TEMP, DEVINFOSIZE);
	return (UNCONF);
}

void
usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di)
{
	struct usbd_port *p;
	int i;

	di->udi_bus = dev->bus->usbctl->dv_unit;
	di->udi_addr = dev->address;
	strlcpy(di->udi_vendor, dev->vendor, sizeof(di->udi_vendor));
	strlcpy(di->udi_product, dev->product, sizeof(di->udi_product));
	usbd_printBCD(di->udi_release, sizeof di->udi_release,
	    UGETW(dev->ddesc.bcdDevice));
	di->udi_vendorNo = UGETW(dev->ddesc.idVendor);
	di->udi_productNo = UGETW(dev->ddesc.idProduct);
	di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice);
	di->udi_class = dev->ddesc.bDeviceClass;
	di->udi_subclass = dev->ddesc.bDeviceSubClass;
	di->udi_protocol = dev->ddesc.bDeviceProtocol;
	di->udi_config = dev->config;
	di->udi_power = dev->self_powered ? 0 : dev->power;
	di->udi_speed = dev->speed;
	di->udi_port = dev->powersrc ? dev->powersrc->portno : 0;

	if (dev->subdevs != NULL) {
		for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) {
			strncpy(di->udi_devnames[i],
			    dev->subdevs[i]->dv_xname, USB_MAX_DEVNAMELEN);
			di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0';
		}
	} else
		i = 0;

	for (/*i is set */; i < USB_MAX_DEVNAMES; i++)
		di->udi_devnames[i][0] = 0; /* empty */

	if (dev->hub) {
		for (i = 0;
		    i < nitems(di->udi_ports) && i < dev->hub->nports; i++) {
			p = &dev->hub->ports[i];
			di->udi_ports[i] = UGETW(p->status.wPortChange) << 16 |
			    UGETW(p->status.wPortStatus);
		}
		di->udi_nports = dev->hub->nports;
	} else
		di->udi_nports = 0;

	bzero(di->udi_serial, sizeof(di->udi_serial));
	if (dev->serial != NULL)
		strlcpy(di->udi_serial, dev->serial,
		    sizeof(di->udi_serial));
}

/* Retrieve a complete descriptor for a certain device and index. */
usb_config_descriptor_t *
usbd_get_cdesc(struct usbd_device *dev, int index, u_int *lenp)
{
	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
	u_int len;
	usbd_status err;

	if (index == USB_CURRENT_CONFIG_INDEX) {
		tdesc = usbd_get_config_descriptor(dev);
		if (tdesc == NULL)
			return (NULL);
		len = UGETW(tdesc->wTotalLength);
		if (lenp)
			*lenp = len;
		cdesc = malloc(len, M_TEMP, M_WAITOK);
		memcpy(cdesc, tdesc, len);
		DPRINTFN(5,("%s: current, len=%u\n", __func__, len));
	} else {
		err = usbd_get_desc(dev, UDESC_CONFIG, index,
		    USB_CONFIG_DESCRIPTOR_SIZE, &cdescr);
		if (err || cdescr.bDescriptorType != UDESC_CONFIG)
			return (0);
		len = UGETW(cdescr.wTotalLength);
		DPRINTFN(5,("%s: index=%d, len=%u\n", __func__, index, len));
		if (lenp)
			*lenp = len;
		cdesc = malloc(len, M_TEMP, M_WAITOK);
		err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdesc);
		if (err) {
			free(cdesc, M_TEMP, len);
			return (0);
		}
	}
	return (cdesc);
}

void
usb_free_device(struct usbd_device *dev)
{
	int ifcidx, nifc;

	DPRINTF(("%s: %p\n", __func__, dev));

	if (dev->default_pipe != NULL) {
		usbd_abort_pipe(dev->default_pipe);
		usbd_close_pipe(dev->default_pipe);
	}
	if (dev->ifaces != NULL) {
		nifc = dev->cdesc->bNumInterface;
		for (ifcidx = 0; ifcidx < nifc; ifcidx++)
			usbd_free_iface_data(dev, ifcidx);
		free(dev->ifaces, M_USB, nifc * sizeof(*dev->ifaces));
	}
	if (dev->cdesc != NULL)
		free(dev->cdesc, M_USB, UGETW(dev->cdesc->wTotalLength));
	if (dev->subdevs != NULL)
		free(dev->subdevs, M_USB, 0);
	dev->bus->devices[dev->address] = NULL;

	if (dev->vendor != NULL)
		free(dev->vendor, M_USB, USB_MAX_STRING_LEN);
	if (dev->product != NULL)
		free(dev->product, M_USB, USB_MAX_STRING_LEN);
	if (dev->serial != NULL)
		free(dev->serial, M_USB, USB_MAX_STRING_LEN);

	free(dev, M_USB, sizeof *dev);
}

/*
 * Should only be called by the USB thread doing bus exploration to
 * avoid connect/disconnect races.
 */
int
usbd_detach(struct usbd_device *dev, struct device *parent)
{
	int i, rv = 0;

	usbd_deactivate(dev);

	if (dev->ndevs > 0) {
		for (i = 0; dev->subdevs[i] != NULL; i++)
			rv |= config_detach(dev->subdevs[i], DETACH_FORCE);
	}

	if (rv == 0)
		usb_free_device(dev);

	return (rv);
}