-
Notifications
You must be signed in to change notification settings - Fork 262
/
usb_subr.c
1919 lines (1690 loc) · 52.1 KB
/
usb_subr.c
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
/* $NetBSD: usb_subr.c,v 1.278 2023/04/11 08:50:07 riastradh Exp $ */
/* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */
/*
* Copyright (c) 1998, 2004 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/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.278 2023/04/11 08:50:07 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
#include "opt_usb.h"
#include "opt_usbverbose.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/device.h>
#include <sys/select.h>
#include <sys/proc.h>
#include <sys/bus.h>
#include <sys/module.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>
#include <dev/usb/usb_verbose.h>
#include <dev/usb/usbhist.h>
#include "locators.h"
#define DPRINTF(FMT,A,B,C,D) USBHIST_LOG(usbdebug,FMT,A,B,C,D)
#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
Static void usbd_devinfo(struct usbd_device *, int, char *, size_t);
Static int usbd_getnewaddr(struct usbd_bus *);
Static int usbd_print(void *, const char *);
Static int usbd_ifprint(void *, const char *);
Static void usbd_free_iface_data(struct usbd_device *, int);
uint32_t usb_cookie_no = 0;
Static 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",
};
DEV_VERBOSE_DEFINE(usb);
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;
}
}
static void
usbd_trim_spaces(char *p)
{
char *q, *e;
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 */
}
static void
usbd_get_device_string(struct usbd_device *ud, uByte index, char **buf)
{
char *b;
usbd_status err;
b = kmem_alloc(USB_MAX_ENCODED_STRING_LEN, KM_SLEEP);
err = usbd_get_string0(ud, index, b, true);
if (err != USBD_NORMAL_COMPLETION) {
kmem_free(b, USB_MAX_ENCODED_STRING_LEN);
b = NULL;
} else {
usbd_trim_spaces(b);
}
*buf = b;
}
void
usbd_get_device_strings(struct usbd_device *ud)
{
usb_device_descriptor_t *udd = &ud->ud_ddesc;
usbd_get_device_string(ud, udd->iManufacturer, &ud->ud_vendor);
usbd_get_device_string(ud, udd->iProduct, &ud->ud_product);
usbd_get_device_string(ud, udd->iSerialNumber, &ud->ud_serial);
}
void
usbd_devinfo_vp(struct usbd_device *dev, char *v, size_t vl, char *p,
size_t pl, int usedev, int useencoded)
{
usb_device_descriptor_t *udd = &dev->ud_ddesc;
if (dev == NULL)
return;
v[0] = p[0] = '\0';
if (usedev) {
if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) ==
USBD_NORMAL_COMPLETION)
usbd_trim_spaces(v);
if (usbd_get_string0(dev, udd->iProduct, p, useencoded) ==
USBD_NORMAL_COMPLETION)
usbd_trim_spaces(p);
} else {
if (dev->ud_vendor) {
strlcpy(v, dev->ud_vendor, vl);
}
if (dev->ud_product) {
strlcpy(p, dev->ud_product, pl);
}
}
if (v[0] == '\0')
usb_findvendor(v, vl, UGETW(udd->idVendor));
if (p[0] == '\0')
usb_findproduct(p, pl, UGETW(udd->idVendor),
UGETW(udd->idProduct));
}
int
usbd_printBCD(char *cp, size_t l, int bcd)
{
return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff);
}
Static void
usbd_devinfo(struct usbd_device *dev, int showclass, char *cp, size_t l)
{
usb_device_descriptor_t *udd = &dev->ud_ddesc;
char *vendor, *product;
int bcdDevice, bcdUSB;
char *ep;
vendor = kmem_alloc(USB_MAX_ENCODED_STRING_LEN * 2, KM_SLEEP);
product = &vendor[USB_MAX_ENCODED_STRING_LEN];
ep = cp + l;
usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN,
product, USB_MAX_ENCODED_STRING_LEN, 0, 1);
cp += snprintf(cp, ep - cp, "%s (0x%04x) %s (0x%04x)", vendor,
UGETW(udd->idVendor), product, UGETW(udd->idProduct));
if (showclass)
cp += snprintf(cp, ep - cp, ", class %d/%d",
udd->bDeviceClass, udd->bDeviceSubClass);
bcdUSB = UGETW(udd->bcdUSB);
bcdDevice = UGETW(udd->bcdDevice);
cp += snprintf(cp, ep - cp, ", rev ");
cp += usbd_printBCD(cp, ep - cp, bcdUSB);
*cp++ = '/';
cp += usbd_printBCD(cp, ep - cp, bcdDevice);
cp += snprintf(cp, ep - cp, ", addr %d", dev->ud_addr);
*cp = 0;
kmem_free(vendor, USB_MAX_ENCODED_STRING_LEN * 2);
}
char *
usbd_devinfo_alloc(struct usbd_device *dev, int showclass)
{
char *devinfop;
devinfop = kmem_alloc(DEVINFOSIZE, KM_SLEEP);
usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE);
return devinfop;
}
void
usbd_devinfo_free(char *devinfop)
{
kmem_free(devinfop, DEVINFOSIZE);
}
/* Delay for a certain number of ms */
void
usb_delay_ms_locked(struct usbd_bus *bus, u_int ms, kmutex_t *lock)
{
/* Wait at least two clock ticks so we know the time has passed. */
if (bus->ub_usepolling || cold)
delay((ms+1) * 1000);
else
kpause("usbdly", false, (ms*hz+999)/1000 + 1, lock);
}
void
usb_delay_ms(struct usbd_bus *bus, u_int ms)
{
usb_delay_ms_locked(bus, ms, NULL);
}
/* Delay given a device handle. */
void
usbd_delay_ms_locked(struct usbd_device *dev, u_int ms, kmutex_t *lock)
{
usb_delay_ms_locked(dev->ud_bus, ms, lock);
}
/* Delay given a device handle. */
void
usbd_delay_ms(struct usbd_device *dev, u_int ms)
{
usb_delay_ms_locked(dev->ud_bus, ms, NULL);
}
usbd_status
usbd_reset_port(struct usbd_device *dev, int port, usb_port_status_t *ps)
{
USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "port %jd", port, 0, 0, 0);
usb_device_request_t req;
usbd_status err;
int n;
req.bmRequestType = UT_WRITE_CLASS_OTHER;
req.bRequest = UR_SET_FEATURE;
USETW(req.wValue, UHF_PORT_RESET);
USETW(req.wIndex, port);
USETW(req.wLength, 0);
err = usbd_do_request(dev, &req, 0);
DPRINTFN(1, "port %jd reset done, error=%jd", port, err, 0, 0);
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("get status failed %jd", err, 0, 0, 0);
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;
err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
#ifdef USB_DEBUG
if (err)
DPRINTF("clear port feature failed %jd", err, 0, 0, 0);
#endif
/* Wait for the device to recover from reset. */
usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY);
return err;
}
usb_interface_descriptor_t *
usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "iface/alt idx %jd/%jd",
ifaceidx, altidx, 0, 0);
char *p = (char *)cd;
char *end = p + UGETW(cd->wTotalLength);
usb_descriptor_t *desc;
usb_interface_descriptor_t *idesc;
int curidx, lastidx, curaidx = 0;
for (curidx = lastidx = -1; end - p >= sizeof(*desc);) {
desc = (usb_descriptor_t *)p;
DPRINTFN(4, "idx=%jd(%jd) altidx=%jd(%jd)", ifaceidx, curidx,
altidx, curaidx);
DPRINTFN(4, "len=%jd type=%jd", desc->bLength,
desc->bDescriptorType, 0, 0);
if (desc->bLength < USB_DESCRIPTOR_SIZE)
break;
if (desc->bLength > end - p)
break;
p += desc->bLength;
if (desc->bDescriptorType != UDESC_INTERFACE)
continue;
if (desc->bLength < USB_INTERFACE_DESCRIPTOR_SIZE)
break;
idesc = (usb_interface_descriptor_t *)desc;
if (idesc->bInterfaceNumber != lastidx) {
lastidx = idesc->bInterfaceNumber;
curidx++;
curaidx = 0;
} else {
curaidx++;
}
if (ifaceidx == curidx && altidx == curaidx)
return idesc;
}
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 *idesc;
usb_endpoint_descriptor_t *edesc;
usb_descriptor_t *desc;
int curidx;
idesc = usbd_find_idesc(cd, ifaceidx, altidx);
if (idesc == NULL)
return NULL;
if (endptidx >= idesc->bNumEndpoints) /* quick exit */
return NULL;
curidx = -1;
for (p = (char *)idesc + idesc->bLength; end - p >= sizeof(*edesc);) {
desc = (usb_descriptor_t *)p;
if (desc->bLength < USB_DESCRIPTOR_SIZE)
break;
if (desc->bLength > end - p)
break;
p += desc->bLength;
if (desc->bDescriptorType == UDESC_INTERFACE)
break;
if (desc->bDescriptorType != UDESC_ENDPOINT)
continue;
if (desc->bLength < USB_ENDPOINT_DESCRIPTOR_SIZE)
break;
edesc = (usb_endpoint_descriptor_t *)desc;
curidx++;
if (curidx == endptidx)
return edesc;
}
return NULL;
}
static void
usbd_iface_init(struct usbd_device *dev, int ifaceidx)
{
struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx];
memset(ifc, 0, sizeof(*ifc));
ifc->ui_dev = dev;
ifc->ui_idesc = NULL;
ifc->ui_index = 0;
ifc->ui_altindex = 0;
ifc->ui_endpoints = NULL;
ifc->ui_busy = 0;
}
static void
usbd_iface_fini(struct usbd_device *dev, int ifaceidx)
{
struct usbd_interface *ifc __diagused = &dev->ud_ifaces[ifaceidx];
KASSERT(ifc->ui_dev == dev);
KASSERT(ifc->ui_idesc == NULL);
KASSERT(ifc->ui_index == 0);
KASSERT(ifc->ui_altindex == 0);
KASSERT(ifc->ui_endpoints == NULL);
KASSERTMSG(ifc->ui_busy == 0, "%"PRId64, ifc->ui_busy);
}
/*
* usbd_iface_lock/locked/unlock, usbd_iface_piperef/pipeunref
*
* We lock the interface while we are setting it, and we acquire a
* reference to the interface for each pipe opened on it.
*
* Setting the interface while pipes are open is forbidden, and
* opening pipes while the interface is being set is forbidden.
*/
bool
usbd_iface_locked(struct usbd_interface *iface)
{
bool locked;
mutex_enter(iface->ui_dev->ud_bus->ub_lock);
locked = (iface->ui_busy == -1);
mutex_exit(iface->ui_dev->ud_bus->ub_lock);
return locked;
}
static void
usbd_iface_exlock(struct usbd_interface *iface)
{
mutex_enter(iface->ui_dev->ud_bus->ub_lock);
KASSERTMSG(iface->ui_busy == 0, "interface is not idle,"
" busy=%"PRId64, iface->ui_busy);
iface->ui_busy = -1;
mutex_exit(iface->ui_dev->ud_bus->ub_lock);
}
usbd_status
usbd_iface_lock(struct usbd_interface *iface)
{
usbd_status err;
mutex_enter(iface->ui_dev->ud_bus->ub_lock);
KASSERTMSG(iface->ui_busy != -1, "interface is locked");
KASSERTMSG(iface->ui_busy >= 0, "%"PRId64, iface->ui_busy);
if (iface->ui_busy) {
err = USBD_IN_USE;
} else {
iface->ui_busy = -1;
err = 0;
}
mutex_exit(iface->ui_dev->ud_bus->ub_lock);
return err;
}
void
usbd_iface_unlock(struct usbd_interface *iface)
{
mutex_enter(iface->ui_dev->ud_bus->ub_lock);
KASSERTMSG(iface->ui_busy == -1, "interface is not locked,"
" busy=%"PRId64, iface->ui_busy);
iface->ui_busy = 0;
mutex_exit(iface->ui_dev->ud_bus->ub_lock);
}
usbd_status
usbd_iface_piperef(struct usbd_interface *iface)
{
usbd_status err;
mutex_enter(iface->ui_dev->ud_bus->ub_lock);
KASSERTMSG(iface->ui_busy >= -1, "%"PRId64, iface->ui_busy);
if (iface->ui_busy == -1) {
err = USBD_IN_USE;
} else {
iface->ui_busy++;
err = 0;
}
mutex_exit(iface->ui_dev->ud_bus->ub_lock);
return err;
}
void
usbd_iface_pipeunref(struct usbd_interface *iface)
{
mutex_enter(iface->ui_dev->ud_bus->ub_lock);
KASSERTMSG(iface->ui_busy != -1, "interface is locked");
KASSERTMSG(iface->ui_busy != 0, "interface not in use");
KASSERTMSG(iface->ui_busy >= 1, "%"PRId64, iface->ui_busy);
iface->ui_busy--;
mutex_exit(iface->ui_dev->ud_bus->ub_lock);
}
usbd_status
usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "ifaceidx=%jd altidx=%jd",
ifaceidx, altidx, 0, 0);
struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx];
usb_descriptor_t *desc;
usb_interface_descriptor_t *idesc;
usb_endpoint_descriptor_t *ed;
struct usbd_endpoint *endpoints;
char *p, *end;
int endpt, nendpt;
KASSERT(ifc->ui_dev == dev);
KASSERT(usbd_iface_locked(ifc));
idesc = usbd_find_idesc(dev->ud_cdesc, ifaceidx, altidx);
if (idesc == NULL)
return USBD_INVAL;
nendpt = idesc->bNumEndpoints;
DPRINTFN(4, "found idesc nendpt=%jd", nendpt, 0, 0, 0);
if (nendpt != 0) {
endpoints = kmem_alloc(nendpt * sizeof(struct usbd_endpoint),
KM_SLEEP);
} else
endpoints = NULL;
p = (char *)idesc + idesc->bLength;
end = (char *)dev->ud_cdesc + UGETW(dev->ud_cdesc->wTotalLength);
KASSERTMSG((char *)dev->ud_cdesc <= (char *)idesc, "cdesc=%p idesc=%p",
dev->ud_cdesc, idesc);
KASSERTMSG((char *)idesc < end, "idesc=%p end=%p", idesc, end);
for (endpt = 0; endpt < nendpt; endpt++) {
DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0);
for (; end - p >= sizeof(*desc); p += desc->bLength) {
desc = (usb_descriptor_t *)p;
DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd",
(uintptr_t)p, (uintptr_t)end, desc->bLength,
desc->bDescriptorType);
if (desc->bLength < sizeof(*desc)) {
printf("%s: bad descriptor: too short\n",
__func__);
goto bad;
} else if (desc->bLength > end - p) {
printf("%s: bad descriptor: too long\n",
__func__);
goto bad;
} else if (desc->bDescriptorType == UDESC_INTERFACE) {
printf("%s: bad descriptor: iface desc\n",
__func__);
goto bad;
}
if (desc->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE &&
desc->bDescriptorType == UDESC_ENDPOINT) {
ed = (usb_endpoint_descriptor_t *)p;
goto found;
}
}
printf("%s: no desc found\n", __func__);
goto bad;
found:
endpoints[endpt].ue_edesc = ed;
if (dev->ud_speed == USB_SPEED_HIGH) {
u_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);
#ifdef DIAGNOSTIC
printf("usbd_fill_iface_data: bad max "
"packet size\n");
#endif
}
break;
default:
break;
}
}
endpoints[endpt].ue_refcnt = 0;
endpoints[endpt].ue_toggle = 0;
KASSERTMSG(end - p >= ed->bLength, "p=%p end=%p length=%u",
p, end, ed->bLength);
p += ed->bLength;
}
#undef ed
/* Success! Free the old endpoints and commit the changes. */
if (ifc->ui_endpoints) {
kmem_free(ifc->ui_endpoints, (sizeof(ifc->ui_endpoints[0]) *
ifc->ui_idesc->bNumEndpoints));
}
ifc->ui_idesc = idesc;
ifc->ui_index = ifaceidx;
ifc->ui_altindex = altidx;
ifc->ui_endpoints = endpoints;
return USBD_NORMAL_COMPLETION;
bad:
if (endpoints)
kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
return USBD_INVAL;
}
Static void
usbd_free_iface_data(struct usbd_device *dev, int ifcno)
{
struct usbd_interface *ifc = &dev->ud_ifaces[ifcno];
KASSERT(ifc->ui_dev == dev);
KASSERT(ifc->ui_idesc != NULL);
KASSERT(usbd_iface_locked(ifc));
if (ifc->ui_endpoints) {
int nendpt = ifc->ui_idesc->bNumEndpoints;
size_t sz = nendpt * sizeof(struct usbd_endpoint);
kmem_free(ifc->ui_endpoints, sz);
ifc->ui_endpoints = NULL;
}
ifc->ui_altindex = 0;
ifc->ui_index = 0;
ifc->ui_idesc = NULL;
}
usbd_status
usbd_set_config_no(struct usbd_device *dev, int no, int msg)
{
USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "%jd", no, 0, 0, 0);
usb_config_descriptor_t cd;
usbd_status err;
int index;
if (no == USB_UNCONFIG_NO)
return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg);
/* Figure out what config index to use. */
for (index = 0; index < dev->ud_ddesc.bNumConfigurations; index++) {
err = usbd_get_config_desc(dev, index, &cd);
if (err)
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)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "dev=%#jx index=%jd",
(uintptr_t)dev, index, 0, 0);
usb_config_descriptor_t cd, *cdp;
usb_bos_descriptor_t *bdp = NULL;
usbd_status err;
int i, ifcidx, nifc, len, selfpowered, power;
if (index >= dev->ud_ddesc.bNumConfigurations &&
index != USB_UNCONFIG_INDEX) {
/* panic? */
printf("usbd_set_config_index: illegal index\n");
return USBD_INVAL;
}
/* XXX check that all interfaces are idle */
if (dev->ud_config != USB_UNCONFIG_NO) {
DPRINTF("free old config", 0, 0, 0, 0);
/* Free all configuration data structures. */
nifc = dev->ud_cdesc->bNumInterface;
for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
usbd_iface_exlock(&dev->ud_ifaces[ifcidx]);
usbd_free_iface_data(dev, ifcidx);
usbd_iface_unlock(&dev->ud_ifaces[ifcidx]);
usbd_iface_fini(dev, ifcidx);
}
kmem_free(dev->ud_ifaces, nifc * sizeof(struct usbd_interface));
kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength));
if (dev->ud_bdesc != NULL)
kmem_free(dev->ud_bdesc,
UGETW(dev->ud_bdesc->wTotalLength));
dev->ud_ifaces = NULL;
dev->ud_cdesc = NULL;
dev->ud_bdesc = NULL;
dev->ud_config = USB_UNCONFIG_NO;
}
if (index == USB_UNCONFIG_INDEX) {
/* We are unconfiguring the device, so leave unallocated. */
DPRINTF("set config 0", 0, 0, 0, 0);
err = usbd_set_config(dev, USB_UNCONFIG_NO);
if (err) {
DPRINTF("setting config=0 failed, err = %jd", err,
0, 0, 0);
}
return err;
}
/* Get the short descriptor. */
err = usbd_get_config_desc(dev, index, &cd);
if (err) {
DPRINTF("get_config_desc=%jd", err, 0, 0, 0);
return err;
}
len = UGETW(cd.wTotalLength);
if (len < USB_CONFIG_DESCRIPTOR_SIZE) {
DPRINTF("empty short descriptor", 0, 0, 0, 0);
return USBD_INVAL;
}
cdp = kmem_alloc(len, KM_SLEEP);
/* Get the full descriptor. Try a few times for slow devices. */
for (i = 0; i < 3; i++) {
err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp);
if (!err)
break;
usbd_delay_ms(dev, 200);
}
if (err) {
DPRINTF("get_desc=%jd", err, 0, 0, 0);
goto bad;
}
if (cdp->bDescriptorType != UDESC_CONFIG) {
DPRINTF("bad desc %jd", cdp->bDescriptorType, 0, 0, 0);
err = USBD_INVAL;
goto bad;
}
if (UGETW(cdp->wTotalLength) != UGETW(cd.wTotalLength)) {
DPRINTF("bad len %jd", UGETW(cdp->wTotalLength), 0, 0, 0);
err = USBD_INVAL;
goto bad;
}
if (USB_IS_SS(dev->ud_speed)) {
usb_bos_descriptor_t bd;
/* get short bos desc */
err = usbd_get_bos_desc(dev, index, &bd);
if (!err) {
int blen = UGETW(bd.wTotalLength);
if (blen < USB_BOS_DESCRIPTOR_SIZE) {
DPRINTF("empty bos descriptor", 0, 0, 0, 0);
err = USBD_INVAL;
goto bad;
}
bdp = kmem_alloc(blen, KM_SLEEP);
/* Get the full desc */
for (i = 0; i < 3; i++) {
err = usbd_get_desc(dev, UDESC_BOS, index, blen,
bdp);
if (!err)
break;
usbd_delay_ms(dev, 200);
}
if (err || bdp->bDescriptorType != UDESC_BOS ||
UGETW(bdp->wTotalLength) != UGETW(bd.wTotalLength)) {
DPRINTF("error %jd or bad desc %jd", err,
bdp->bDescriptorType, 0, 0);
kmem_free(bdp, blen);
bdp = NULL;
}
}
}
dev->ud_bdesc = bdp;
/*
* Figure out if the device is self or bus powered.
*/
#if 0 /* XXX various devices don't report the power state correctly */
selfpowered = 0;
err = usbd_get_device_status(dev, &ds);
if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED))
selfpowered = 1;
#endif
/*
* Use the power state in the configuration we are going
* to set. This doesn't necessarily reflect the actual
* power state of the device; the driver can control this
* by choosing the appropriate configuration.
*/
selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED);
DPRINTF("addr %jd cno=%jd attr=0x%02jx, selfpowered=%jd",
dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes,
selfpowered);
DPRINTF("max power=%jd", cdp->bMaxPower * 2, 0, 0, 0);
/* Check if we have enough power. */
#if 0 /* this is a no-op, see above */
if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) {
if (msg)
printf("%s: device addr %d (config %d): "
"can't set self powered configuration\n",
device_xname(dev->ud_bus->bdev), dev->ud_addr,
cdp->bConfigurationValue);
err = USBD_NO_POWER;
goto bad;
}
#endif
#ifdef USB_DEBUG
if (dev->ud_powersrc == NULL) {
DPRINTF("No power source?", 0, 0, 0, 0);
err = USBD_IOERROR;
goto bad;
}
#endif
power = cdp->bMaxPower * 2;
if (power > dev->ud_powersrc->up_power) {
DPRINTF("power exceeded %jd %jd", power,
dev->ud_powersrc->up_power, 0, 0);
/* XXX print nicer message. */
if (msg)
printf("%s: device addr %d (config %d) exceeds power "
"budget, %d mA > %d mA\n",
device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr,
cdp->bConfigurationValue,
power, dev->ud_powersrc->up_power);
err = USBD_NO_POWER;
goto bad;
}
dev->ud_power = power;
dev->ud_selfpowered = selfpowered;
/* Set the actual configuration value. */
DPRINTF("set config %jd", cdp->bConfigurationValue, 0, 0, 0);
err = usbd_set_config(dev, cdp->bConfigurationValue);
if (err) {
DPRINTF("setting config=%jd failed, error=%jd",
cdp->bConfigurationValue, err, 0, 0);
goto bad;
}
KASSERTMSG(dev->ud_ifaces == NULL, "ud_ifaces=%p", dev->ud_ifaces);
/* Allocate and fill interface data. */
nifc = cdp->bNumInterface;
if (nifc == 0) {
DPRINTF("no interfaces", 0, 0, 0, 0);
err = USBD_INVAL;
goto bad;
}
dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface),
KM_SLEEP);
DPRINTFN(5, "dev=%#jx cdesc=%#jx", (uintptr_t)dev, (uintptr_t)cdp,
0, 0);
dev->ud_cdesc = cdp;
dev->ud_config = cdp->bConfigurationValue;
for (ifcidx = 0; ifcidx < nifc; ifcidx++) {
usbd_iface_init(dev, ifcidx);
usbd_iface_exlock(&dev->ud_ifaces[ifcidx]);
err = usbd_fill_iface_data(dev, ifcidx, 0);
usbd_iface_unlock(&dev->ud_ifaces[ifcidx]);
if (err) {
while (--ifcidx >= 0) {
usbd_iface_exlock(&dev->ud_ifaces[ifcidx]);
usbd_free_iface_data(dev, ifcidx);
usbd_iface_unlock(&dev->ud_ifaces[ifcidx]);
usbd_iface_fini(dev, ifcidx);
}
kmem_free(dev->ud_ifaces,
nifc * sizeof(struct usbd_interface));
dev->ud_ifaces = NULL;
goto bad;
}
}
return USBD_NORMAL_COMPLETION;
bad:
/* XXX Use usbd_set_config() to reset the config? */
/* XXX Should we forbid USB_UNCONFIG_NO from bConfigurationValue? */
dev->ud_config = USB_UNCONFIG_NO;
KASSERT(dev->ud_ifaces == NULL);
kmem_free(cdp, len);
dev->ud_cdesc = NULL;
if (bdp != NULL) {
kmem_free(bdp, UGETW(bdp->wTotalLength));
dev->ud_bdesc = NULL;
}
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)
{
return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0);
}
usbd_status
usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface,
struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "dev=%#jx addr=%jd iface=%#jx ep=%#jx",
(uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep);
struct usbd_pipe *p = NULL;
bool ep_acquired = false;
usbd_status err;
/* Block exclusive use of the endpoint by later pipes. */
err = usbd_endpoint_acquire(dev, ep, flags & USBD_EXCLUSIVE_USE);
if (err)
goto out;
ep_acquired = true;
p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP);
DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0);
p->up_dev = dev;
p->up_iface = iface;
p->up_endpoint = ep;
p->up_intrxfer = NULL;
p->up_running = 0;
p->up_aborting = 0;
p->up_serialise = true;
p->up_repeat = 0;
p->up_interval = ival;
p->up_flags = flags;
SIMPLEQ_INIT(&p->up_queue);
p->up_callingxfer = NULL;
cv_init(&p->up_callingcv, "usbpipecb");
p->up_abortlwp = NULL;
err = dev->ud_bus->ub_methods->ubm_open(p);
if (err) {
DPRINTF("endpoint=%#jx failed, error=%jd",
(uintptr_t)ep->ue_edesc->bEndpointAddress, err, 0, 0);
goto out;
}
KASSERT(p->up_methods->upm_start || p->up_serialise == false);
usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p,
USB_TASKQ_MPSAFE);
DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0);
*pipe = p;
p = NULL; /* handed off to caller */
ep_acquired = false; /* handed off to pipe */
err = USBD_NORMAL_COMPLETION;
out: if (p) {
KASSERT(p->up_abortlwp == NULL);
KASSERT(p->up_callingxfer == NULL);
cv_destroy(&p->up_callingcv);
kmem_free(p, dev->ud_bus->ub_pipesize);
}
if (ep_acquired)
usbd_endpoint_release(dev, ep);
return err;
}
usbd_status
usbd_endpoint_acquire(struct usbd_device *dev, struct usbd_endpoint *ep,
int flags)
{
usbd_status err;
mutex_enter(dev->ud_bus->ub_lock);
if (ep->ue_refcnt == INT_MAX) {
err = USBD_IN_USE; /* XXX rule out or switch to 64-bit */
} else if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt) {