]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
e887afc9 WD |
2 | /* |
3 | * (C) Copyright 2001 | |
4 | * Denis Peter, MPL AG Switzerland | |
5 | * | |
6a1b206d SG |
6 | * Adapted for U-Boot driver model |
7 | * (C) Copyright 2015 Google, Inc | |
8 | * | |
e887afc9 WD |
9 | * Most of this source has been derived from the Linux USB |
10 | * project. | |
e887afc9 WD |
11 | */ |
12 | ||
13 | #include <common.h> | |
e6f6f9e6 | 14 | #include <blk.h> |
52f24238 | 15 | #include <bootstage.h> |
e887afc9 | 16 | #include <command.h> |
24b852a7 | 17 | #include <console.h> |
6a1b206d | 18 | #include <dm.h> |
192eab93 | 19 | #include <dm/uclass-internal.h> |
cf92e05c | 20 | #include <memalign.h> |
414eec35 | 21 | #include <asm/byteorder.h> |
b2fb47f1 | 22 | #include <asm/unaligned.h> |
735dd97b | 23 | #include <part.h> |
e887afc9 | 24 | #include <usb.h> |
e887afc9 | 25 | |
1968e615 | 26 | #ifdef CONFIG_USB_STORAGE |
d0ff51ba | 27 | static int usb_stor_curr_dev = -1; /* current device */ |
1968e615 | 28 | #endif |
c8c2797c | 29 | #if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH) |
69559093 | 30 | static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */ |
89d48367 | 31 | #endif |
e887afc9 | 32 | |
a2663ea4 | 33 | /* some display routines (info command) */ |
088f1b19 | 34 | static char *usb_get_class_desc(unsigned char dclass) |
e887afc9 | 35 | { |
de39f8c1 MT |
36 | switch (dclass) { |
37 | case USB_CLASS_PER_INTERFACE: | |
38 | return "See Interface"; | |
39 | case USB_CLASS_AUDIO: | |
40 | return "Audio"; | |
41 | case USB_CLASS_COMM: | |
42 | return "Communication"; | |
43 | case USB_CLASS_HID: | |
44 | return "Human Interface"; | |
45 | case USB_CLASS_PRINTER: | |
46 | return "Printer"; | |
47 | case USB_CLASS_MASS_STORAGE: | |
48 | return "Mass Storage"; | |
49 | case USB_CLASS_HUB: | |
50 | return "Hub"; | |
51 | case USB_CLASS_DATA: | |
52 | return "CDC Data"; | |
53 | case USB_CLASS_VENDOR_SPEC: | |
54 | return "Vendor specific"; | |
55 | default: | |
56 | return ""; | |
e887afc9 WD |
57 | } |
58 | } | |
59 | ||
088f1b19 KP |
60 | static void usb_display_class_sub(unsigned char dclass, unsigned char subclass, |
61 | unsigned char proto) | |
e887afc9 | 62 | { |
de39f8c1 MT |
63 | switch (dclass) { |
64 | case USB_CLASS_PER_INTERFACE: | |
65 | printf("See Interface"); | |
66 | break; | |
67 | case USB_CLASS_HID: | |
68 | printf("Human Interface, Subclass: "); | |
69 | switch (subclass) { | |
70 | case USB_SUB_HID_NONE: | |
71 | printf("None"); | |
e887afc9 | 72 | break; |
de39f8c1 MT |
73 | case USB_SUB_HID_BOOT: |
74 | printf("Boot "); | |
75 | switch (proto) { | |
76 | case USB_PROT_HID_NONE: | |
77 | printf("None"); | |
78 | break; | |
79 | case USB_PROT_HID_KEYBOARD: | |
80 | printf("Keyboard"); | |
81 | break; | |
82 | case USB_PROT_HID_MOUSE: | |
83 | printf("Mouse"); | |
84 | break; | |
85 | default: | |
86 | printf("reserved"); | |
87 | break; | |
e887afc9 WD |
88 | } |
89 | break; | |
de39f8c1 MT |
90 | default: |
91 | printf("reserved"); | |
92 | break; | |
93 | } | |
94 | break; | |
95 | case USB_CLASS_MASS_STORAGE: | |
96 | printf("Mass Storage, "); | |
97 | switch (subclass) { | |
98 | case US_SC_RBC: | |
99 | printf("RBC "); | |
100 | break; | |
101 | case US_SC_8020: | |
102 | printf("SFF-8020i (ATAPI)"); | |
103 | break; | |
104 | case US_SC_QIC: | |
105 | printf("QIC-157 (Tape)"); | |
106 | break; | |
107 | case US_SC_UFI: | |
108 | printf("UFI"); | |
109 | break; | |
110 | case US_SC_8070: | |
111 | printf("SFF-8070"); | |
112 | break; | |
113 | case US_SC_SCSI: | |
114 | printf("Transp. SCSI"); | |
115 | break; | |
116 | default: | |
117 | printf("reserved"); | |
118 | break; | |
119 | } | |
120 | printf(", "); | |
121 | switch (proto) { | |
122 | case US_PR_CB: | |
123 | printf("Command/Bulk"); | |
124 | break; | |
125 | case US_PR_CBI: | |
126 | printf("Command/Bulk/Int"); | |
127 | break; | |
128 | case US_PR_BULK: | |
129 | printf("Bulk only"); | |
e887afc9 WD |
130 | break; |
131 | default: | |
de39f8c1 MT |
132 | printf("reserved"); |
133 | break; | |
134 | } | |
135 | break; | |
136 | default: | |
137 | printf("%s", usb_get_class_desc(dclass)); | |
138 | break; | |
e887afc9 WD |
139 | } |
140 | } | |
141 | ||
088f1b19 | 142 | static void usb_display_string(struct usb_device *dev, int index) |
e887afc9 | 143 | { |
f5766139 PS |
144 | ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256); |
145 | ||
de39f8c1 MT |
146 | if (index != 0) { |
147 | if (usb_string(dev, index, &buffer[0], 256) > 0) | |
148 | printf("String: \"%s\"", buffer); | |
e887afc9 WD |
149 | } |
150 | } | |
151 | ||
088f1b19 | 152 | static void usb_display_desc(struct usb_device *dev) |
e887afc9 | 153 | { |
78abf14f BM |
154 | uint packet_size = dev->descriptor.bMaxPacketSize0; |
155 | ||
de39f8c1 MT |
156 | if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) { |
157 | printf("%d: %s, USB Revision %x.%x\n", dev->devnum, | |
8f8bd565 | 158 | usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass), |
de39f8c1 MT |
159 | (dev->descriptor.bcdUSB>>8) & 0xff, |
160 | dev->descriptor.bcdUSB & 0xff); | |
161 | ||
162 | if (strlen(dev->mf) || strlen(dev->prod) || | |
163 | strlen(dev->serial)) | |
164 | printf(" - %s %s %s\n", dev->mf, dev->prod, | |
165 | dev->serial); | |
e887afc9 WD |
166 | if (dev->descriptor.bDeviceClass) { |
167 | printf(" - Class: "); | |
de39f8c1 MT |
168 | usb_display_class_sub(dev->descriptor.bDeviceClass, |
169 | dev->descriptor.bDeviceSubClass, | |
170 | dev->descriptor.bDeviceProtocol); | |
e887afc9 | 171 | printf("\n"); |
de39f8c1 MT |
172 | } else { |
173 | printf(" - Class: (from Interface) %s\n", | |
174 | usb_get_class_desc( | |
8f8bd565 | 175 | dev->config.if_desc[0].desc.bInterfaceClass)); |
e887afc9 | 176 | } |
78abf14f BM |
177 | if (dev->descriptor.bcdUSB >= cpu_to_le16(0x0300)) |
178 | packet_size = 1 << packet_size; | |
de39f8c1 | 179 | printf(" - PacketSize: %d Configurations: %d\n", |
78abf14f | 180 | packet_size, dev->descriptor.bNumConfigurations); |
de39f8c1 MT |
181 | printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n", |
182 | dev->descriptor.idVendor, dev->descriptor.idProduct, | |
183 | (dev->descriptor.bcdDevice>>8) & 0xff, | |
184 | dev->descriptor.bcdDevice & 0xff); | |
e887afc9 WD |
185 | } |
186 | ||
187 | } | |
188 | ||
c60795f4 | 189 | static void usb_display_conf_desc(struct usb_config_descriptor *config, |
088f1b19 | 190 | struct usb_device *dev) |
e887afc9 | 191 | { |
de39f8c1 MT |
192 | printf(" Configuration: %d\n", config->bConfigurationValue); |
193 | printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces, | |
194 | (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ", | |
195 | (config->bmAttributes & 0x20) ? "Remote Wakeup " : "", | |
8f8bd565 | 196 | config->bMaxPower*2); |
e887afc9 WD |
197 | if (config->iConfiguration) { |
198 | printf(" - "); | |
de39f8c1 | 199 | usb_display_string(dev, config->iConfiguration); |
e887afc9 WD |
200 | printf("\n"); |
201 | } | |
202 | } | |
203 | ||
088f1b19 KP |
204 | static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc, |
205 | struct usb_device *dev) | |
e887afc9 | 206 | { |
de39f8c1 MT |
207 | printf(" Interface: %d\n", ifdesc->bInterfaceNumber); |
208 | printf(" - Alternate Setting %d, Endpoints: %d\n", | |
209 | ifdesc->bAlternateSetting, ifdesc->bNumEndpoints); | |
e887afc9 | 210 | printf(" - Class "); |
de39f8c1 MT |
211 | usb_display_class_sub(ifdesc->bInterfaceClass, |
212 | ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol); | |
e887afc9 WD |
213 | printf("\n"); |
214 | if (ifdesc->iInterface) { | |
215 | printf(" - "); | |
de39f8c1 | 216 | usb_display_string(dev, ifdesc->iInterface); |
e887afc9 WD |
217 | printf("\n"); |
218 | } | |
219 | } | |
220 | ||
088f1b19 | 221 | static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc) |
e887afc9 | 222 | { |
de39f8c1 MT |
223 | printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf, |
224 | (epdesc->bEndpointAddress & 0x80) ? "In" : "Out"); | |
225 | switch ((epdesc->bmAttributes & 0x03)) { | |
226 | case 0: | |
227 | printf("Control"); | |
228 | break; | |
229 | case 1: | |
230 | printf("Isochronous"); | |
231 | break; | |
232 | case 2: | |
233 | printf("Bulk"); | |
234 | break; | |
235 | case 3: | |
236 | printf("Interrupt"); | |
237 | break; | |
e887afc9 | 238 | } |
b2fb47f1 | 239 | printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize)); |
de39f8c1 MT |
240 | if ((epdesc->bmAttributes & 0x03) == 0x3) |
241 | printf(" Interval %dms", epdesc->bInterval); | |
e887afc9 WD |
242 | printf("\n"); |
243 | } | |
244 | ||
245 | /* main routine to diasplay the configs, interfaces and endpoints */ | |
088f1b19 | 246 | static void usb_display_config(struct usb_device *dev) |
e887afc9 | 247 | { |
8f8bd565 TR |
248 | struct usb_config *config; |
249 | struct usb_interface *ifdesc; | |
e887afc9 | 250 | struct usb_endpoint_descriptor *epdesc; |
de39f8c1 MT |
251 | int i, ii; |
252 | ||
253 | config = &dev->config; | |
8f8bd565 | 254 | usb_display_conf_desc(&config->desc, dev); |
de39f8c1 MT |
255 | for (i = 0; i < config->no_of_if; i++) { |
256 | ifdesc = &config->if_desc[i]; | |
8f8bd565 | 257 | usb_display_if_desc(&ifdesc->desc, dev); |
de39f8c1 MT |
258 | for (ii = 0; ii < ifdesc->no_of_ep; ii++) { |
259 | epdesc = &ifdesc->ep_desc[ii]; | |
e887afc9 WD |
260 | usb_display_ep_desc(epdesc); |
261 | } | |
262 | } | |
263 | printf("\n"); | |
264 | } | |
265 | ||
6a1b206d SG |
266 | /* |
267 | * With driver model this isn't right since we can have multiple controllers | |
268 | * and the device numbering starts at 1 on each bus. | |
269 | * TODO([email protected]): Add a way to specify the controller/bus. | |
270 | */ | |
7d9aa8fd JW |
271 | static struct usb_device *usb_find_device(int devnum) |
272 | { | |
6a1b206d SG |
273 | #ifdef CONFIG_DM_USB |
274 | struct usb_device *udev; | |
275 | struct udevice *hub; | |
276 | struct uclass *uc; | |
277 | int ret; | |
278 | ||
279 | /* Device addresses start at 1 */ | |
280 | devnum++; | |
281 | ret = uclass_get(UCLASS_USB_HUB, &uc); | |
282 | if (ret) | |
283 | return NULL; | |
284 | ||
285 | uclass_foreach_dev(hub, uc) { | |
286 | struct udevice *dev; | |
287 | ||
288 | if (!device_active(hub)) | |
289 | continue; | |
bcbe3d15 | 290 | udev = dev_get_parent_priv(hub); |
6a1b206d SG |
291 | if (udev->devnum == devnum) |
292 | return udev; | |
293 | ||
294 | for (device_find_first_child(hub, &dev); | |
295 | dev; | |
296 | device_find_next_child(&dev)) { | |
297 | if (!device_active(hub)) | |
298 | continue; | |
299 | ||
bcbe3d15 | 300 | udev = dev_get_parent_priv(dev); |
6a1b206d SG |
301 | if (udev->devnum == devnum) |
302 | return udev; | |
303 | } | |
304 | } | |
305 | #else | |
cad4291c | 306 | struct usb_device *udev; |
7d9aa8fd JW |
307 | int d; |
308 | ||
309 | for (d = 0; d < USB_MAX_DEVICE; d++) { | |
cad4291c SG |
310 | udev = usb_get_dev_index(d); |
311 | if (udev == NULL) | |
7d9aa8fd | 312 | return NULL; |
cad4291c SG |
313 | if (udev->devnum == devnum) |
314 | return udev; | |
7d9aa8fd | 315 | } |
6a1b206d | 316 | #endif |
7d9aa8fd JW |
317 | |
318 | return NULL; | |
319 | } | |
320 | ||
51f60a89 | 321 | static inline const char *portspeed(int speed) |
f1c1f540 | 322 | { |
6497c667 VG |
323 | switch (speed) { |
324 | case USB_SPEED_SUPER: | |
51f60a89 | 325 | return "5 Gb/s"; |
6497c667 | 326 | case USB_SPEED_HIGH: |
51f60a89 | 327 | return "480 Mb/s"; |
6497c667 | 328 | case USB_SPEED_LOW: |
51f60a89 | 329 | return "1.5 Mb/s"; |
6497c667 | 330 | default: |
51f60a89 | 331 | return "12 Mb/s"; |
6497c667 | 332 | } |
f1c1f540 SR |
333 | } |
334 | ||
e887afc9 | 335 | /* shows the device tree recursively */ |
088f1b19 | 336 | static void usb_show_tree_graph(struct usb_device *dev, char *pre) |
e887afc9 | 337 | { |
6a1b206d | 338 | int index; |
10f4dd78 | 339 | int has_child, last_child; |
e887afc9 | 340 | |
de39f8c1 MT |
341 | index = strlen(pre); |
342 | printf(" %s", pre); | |
6a1b206d SG |
343 | #ifdef CONFIG_DM_USB |
344 | has_child = device_has_active_children(dev->dev); | |
abd7cedb SG |
345 | if (device_get_uclass_id(dev->dev) == UCLASS_MASS_STORAGE) { |
346 | struct udevice *child; | |
347 | ||
348 | for (device_find_first_child(dev->dev, &child); | |
349 | child; | |
350 | device_find_next_child(&child)) { | |
351 | if (device_get_uclass_id(child) == UCLASS_BLK) | |
352 | has_child = 0; | |
353 | } | |
354 | } | |
6a1b206d | 355 | #else |
e887afc9 | 356 | /* check if the device has connected children */ |
6a1b206d SG |
357 | int i; |
358 | ||
de39f8c1 MT |
359 | has_child = 0; |
360 | for (i = 0; i < dev->maxchild; i++) { | |
361 | if (dev->children[i] != NULL) | |
362 | has_child = 1; | |
e887afc9 | 363 | } |
6a1b206d | 364 | #endif |
e887afc9 | 365 | /* check if we are the last one */ |
6a1b206d | 366 | #ifdef CONFIG_DM_USB |
c27b3290 HG |
367 | /* Not the root of the usb tree? */ |
368 | if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) { | |
369 | last_child = device_is_last_sibling(dev->dev); | |
6a1b206d | 370 | #else |
c27b3290 HG |
371 | if (dev->parent != NULL) { /* not root? */ |
372 | last_child = 1; | |
de39f8c1 | 373 | for (i = 0; i < dev->parent->maxchild; i++) { |
e887afc9 | 374 | /* search for children */ |
de39f8c1 MT |
375 | if (dev->parent->children[i] == dev) { |
376 | /* found our pointer, see if we have a | |
377 | * little sister | |
378 | */ | |
de39f8c1 MT |
379 | while (i++ < dev->parent->maxchild) { |
380 | if (dev->parent->children[i] != NULL) { | |
e887afc9 | 381 | /* found a sister */ |
de39f8c1 | 382 | last_child = 0; |
e887afc9 WD |
383 | break; |
384 | } /* if */ | |
385 | } /* while */ | |
386 | } /* device found */ | |
387 | } /* for all children of the parent */ | |
6a1b206d | 388 | #endif |
e887afc9 WD |
389 | printf("\b+-"); |
390 | /* correct last child */ | |
cad4291c | 391 | if (last_child && index) |
de39f8c1 | 392 | pre[index-1] = ' '; |
e887afc9 WD |
393 | } /* if not root hub */ |
394 | else | |
395 | printf(" "); | |
de39f8c1 MT |
396 | printf("%d ", dev->devnum); |
397 | pre[index++] = ' '; | |
398 | pre[index++] = has_child ? '|' : ' '; | |
399 | pre[index] = 0; | |
400 | printf(" %s (%s, %dmA)\n", usb_get_class_desc( | |
8f8bd565 | 401 | dev->config.if_desc[0].desc.bInterfaceClass), |
f1c1f540 | 402 | portspeed(dev->speed), |
8f8bd565 | 403 | dev->config.desc.bMaxPower * 2); |
de39f8c1 MT |
404 | if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial)) |
405 | printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial); | |
406 | printf(" %s\n", pre); | |
6a1b206d SG |
407 | #ifdef CONFIG_DM_USB |
408 | struct udevice *child; | |
409 | ||
410 | for (device_find_first_child(dev->dev, &child); | |
411 | child; | |
412 | device_find_next_child(&child)) { | |
413 | struct usb_device *udev; | |
414 | ||
415 | if (!device_active(child)) | |
416 | continue; | |
417 | ||
bcbe3d15 | 418 | udev = dev_get_parent_priv(child); |
6a1b206d | 419 | |
abd7cedb SG |
420 | /* |
421 | * Ignore emulators and block child devices, we only want | |
422 | * real devices | |
423 | */ | |
7a875a8e XDF |
424 | if (udev && |
425 | (device_get_uclass_id(child) != UCLASS_BOOTDEV) && | |
426 | (device_get_uclass_id(child) != UCLASS_USB_EMUL) && | |
abd7cedb | 427 | (device_get_uclass_id(child) != UCLASS_BLK)) { |
6a1b206d SG |
428 | usb_show_tree_graph(udev, pre); |
429 | pre[index] = 0; | |
430 | } | |
431 | } | |
432 | #else | |
de39f8c1 MT |
433 | if (dev->maxchild > 0) { |
434 | for (i = 0; i < dev->maxchild; i++) { | |
435 | if (dev->children[i] != NULL) { | |
436 | usb_show_tree_graph(dev->children[i], pre); | |
437 | pre[index] = 0; | |
e887afc9 WD |
438 | } |
439 | } | |
440 | } | |
6a1b206d | 441 | #endif |
e887afc9 WD |
442 | } |
443 | ||
444 | /* main routine for the tree command */ | |
45bfa47e | 445 | static void usb_show_subtree(struct usb_device *dev) |
e887afc9 WD |
446 | { |
447 | char preamble[32]; | |
448 | ||
cad4291c | 449 | memset(preamble, '\0', sizeof(preamble)); |
de39f8c1 | 450 | usb_show_tree_graph(dev, &preamble[0]); |
e887afc9 WD |
451 | } |
452 | ||
45bfa47e | 453 | #ifdef CONFIG_DM_USB |
2138fd6d HG |
454 | typedef void (*usb_dev_func_t)(struct usb_device *udev); |
455 | ||
456 | static void usb_for_each_root_dev(usb_dev_func_t func) | |
457 | { | |
45bfa47e SG |
458 | struct udevice *bus; |
459 | ||
192eab93 | 460 | for (uclass_find_first_device(UCLASS_USB, &bus); |
45bfa47e | 461 | bus; |
192eab93 | 462 | uclass_find_next_device(&bus)) { |
45bfa47e SG |
463 | struct usb_device *udev; |
464 | struct udevice *dev; | |
465 | ||
192eab93 HG |
466 | if (!device_active(bus)) |
467 | continue; | |
468 | ||
45bfa47e SG |
469 | device_find_first_child(bus, &dev); |
470 | if (dev && device_active(dev)) { | |
471 | udev = dev_get_parent_priv(dev); | |
2138fd6d | 472 | func(udev); |
45bfa47e SG |
473 | } |
474 | } | |
2138fd6d HG |
475 | } |
476 | #endif | |
477 | ||
478 | void usb_show_tree(void) | |
479 | { | |
480 | #ifdef CONFIG_DM_USB | |
481 | usb_for_each_root_dev(usb_show_subtree); | |
45bfa47e SG |
482 | #else |
483 | struct usb_device *udev; | |
484 | int i; | |
485 | ||
486 | for (i = 0; i < USB_MAX_DEVICE; i++) { | |
487 | udev = usb_get_dev_index(i); | |
488 | if (udev == NULL) | |
489 | break; | |
490 | if (udev->parent == NULL) | |
491 | usb_show_subtree(udev); | |
492 | } | |
493 | #endif | |
494 | } | |
495 | ||
7d9aa8fd JW |
496 | static int usb_test(struct usb_device *dev, int port, char* arg) |
497 | { | |
498 | int mode; | |
499 | ||
500 | if (port > dev->maxchild) { | |
501 | printf("Device is no hub or does not have %d ports.\n", port); | |
502 | return 1; | |
503 | } | |
504 | ||
505 | switch (arg[0]) { | |
506 | case 'J': | |
507 | case 'j': | |
508 | printf("Setting Test_J mode"); | |
509 | mode = USB_TEST_MODE_J; | |
510 | break; | |
511 | case 'K': | |
512 | case 'k': | |
513 | printf("Setting Test_K mode"); | |
514 | mode = USB_TEST_MODE_K; | |
515 | break; | |
516 | case 'S': | |
517 | case 's': | |
518 | printf("Setting Test_SE0_NAK mode"); | |
519 | mode = USB_TEST_MODE_SE0_NAK; | |
520 | break; | |
521 | case 'P': | |
522 | case 'p': | |
523 | printf("Setting Test_Packet mode"); | |
524 | mode = USB_TEST_MODE_PACKET; | |
525 | break; | |
526 | case 'F': | |
527 | case 'f': | |
528 | printf("Setting Test_Force_Enable mode"); | |
529 | mode = USB_TEST_MODE_FORCE_ENABLE; | |
530 | break; | |
531 | default: | |
532 | printf("Unrecognized test mode: %s\nAvailable modes: " | |
533 | "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg); | |
534 | return 1; | |
535 | } | |
536 | ||
537 | if (port) | |
538 | printf(" on downstream facing port %d...\n", port); | |
539 | else | |
540 | printf(" on upstream facing port...\n"); | |
541 | ||
542 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE, | |
543 | port ? USB_RT_PORT : USB_RECIP_DEVICE, | |
544 | port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST, | |
545 | (mode << 8) | port, | |
546 | NULL, 0, USB_CNTL_TIMEOUT) == -1) { | |
547 | printf("Error during SET_FEATURE.\n"); | |
548 | return 1; | |
549 | } else { | |
550 | printf("Test mode successfully set. Use 'usb start' " | |
551 | "to return to normal operation.\n"); | |
552 | return 0; | |
553 | } | |
554 | } | |
555 | ||
e887afc9 | 556 | |
e887afc9 WD |
557 | /****************************************************************************** |
558 | * usb boot command intepreter. Derived from diskboot | |
559 | */ | |
560 | #ifdef CONFIG_USB_STORAGE | |
09140113 SG |
561 | static int do_usbboot(struct cmd_tbl *cmdtp, int flag, int argc, |
562 | char *const argv[]) | |
e887afc9 | 563 | { |
7405a133 | 564 | return common_diskboot(cmdtp, "usb", argc, argv); |
e887afc9 WD |
565 | } |
566 | #endif /* CONFIG_USB_STORAGE */ | |
567 | ||
8a8a2257 | 568 | static int do_usb_stop_keyboard(int force) |
6e78c74f | 569 | { |
9a80e714 | 570 | #if !defined CONFIG_DM_USB && defined CONFIG_USB_KEYBOARD |
8a8a2257 | 571 | if (usb_kbd_deregister(force) != 0) { |
6e78c74f HG |
572 | printf("USB not stopped: usbkbd still using USB\n"); |
573 | return 1; | |
574 | } | |
575 | #endif | |
576 | return 0; | |
577 | } | |
e887afc9 | 578 | |
b5072264 HG |
579 | static void do_usb_start(void) |
580 | { | |
581 | bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start"); | |
582 | ||
583 | if (usb_init() < 0) | |
584 | return; | |
585 | ||
6a1b206d | 586 | /* Driver model will probe the devices as they are found */ |
34ab37ee | 587 | # ifdef CONFIG_USB_STORAGE |
b5072264 HG |
588 | /* try to recognize storage devices immediately */ |
589 | usb_stor_curr_dev = usb_stor_scan(1); | |
34ab37ee | 590 | # endif |
b984700c | 591 | #ifndef CONFIG_DM_USB |
34ab37ee SG |
592 | # ifdef CONFIG_USB_KEYBOARD |
593 | drv_usb_kbd_init(); | |
594 | # endif | |
595 | #endif /* !CONFIG_DM_USB */ | |
b5072264 HG |
596 | } |
597 | ||
6a1b206d | 598 | #ifdef CONFIG_DM_USB |
e6e188f5 | 599 | static void usb_show_info(struct usb_device *udev) |
6a1b206d SG |
600 | { |
601 | struct udevice *child; | |
6a1b206d | 602 | |
6a1b206d SG |
603 | usb_display_desc(udev); |
604 | usb_display_config(udev); | |
e6e188f5 | 605 | for (device_find_first_child(udev->dev, &child); |
6a1b206d SG |
606 | child; |
607 | device_find_next_child(&child)) { | |
abd7cedb | 608 | if (device_active(child) && |
7a875a8e | 609 | (device_get_uclass_id(child) != UCLASS_BOOTDEV) && |
abd7cedb SG |
610 | (device_get_uclass_id(child) != UCLASS_USB_EMUL) && |
611 | (device_get_uclass_id(child) != UCLASS_BLK)) { | |
e6e188f5 | 612 | udev = dev_get_parent_priv(child); |
7a875a8e XDF |
613 | if (udev) |
614 | usb_show_info(udev); | |
6a1b206d SG |
615 | } |
616 | } | |
6a1b206d SG |
617 | } |
618 | #endif | |
619 | ||
de39f8c1 | 620 | /****************************************************************************** |
e887afc9 WD |
621 | * usb command intepreter |
622 | */ | |
09140113 | 623 | static int do_usb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
e887afc9 | 624 | { |
cad4291c | 625 | struct usb_device *udev = NULL; |
e887afc9 | 626 | int i; |
e887afc9 | 627 | |
47e26b1b | 628 | if (argc < 2) |
4c12eeb8 | 629 | return CMD_RET_USAGE; |
65d34254 | 630 | |
b5072264 HG |
631 | if (strncmp(argv[1], "start", 5) == 0) { |
632 | if (usb_started) | |
633 | return 0; /* Already started */ | |
634 | printf("starting USB...\n"); | |
635 | do_usb_start(); | |
636 | return 0; | |
637 | } | |
638 | ||
639 | if (strncmp(argv[1], "reset", 5) == 0) { | |
640 | printf("resetting USB...\n"); | |
8a8a2257 | 641 | if (do_usb_stop_keyboard(1) != 0) |
6e78c74f | 642 | return 1; |
e887afc9 | 643 | usb_stop(); |
b5072264 | 644 | do_usb_start(); |
e887afc9 WD |
645 | return 0; |
646 | } | |
de39f8c1 | 647 | if (strncmp(argv[1], "stop", 4) == 0) { |
6e78c74f | 648 | if (argc != 2) |
de39f8c1 | 649 | console_assign(stdin, "serial"); |
8a8a2257 | 650 | if (do_usb_stop_keyboard(0) != 0) |
6e78c74f | 651 | return 1; |
e887afc9 WD |
652 | printf("stopping USB..\n"); |
653 | usb_stop(); | |
654 | return 0; | |
655 | } | |
e51aae38 BS |
656 | if (!usb_started) { |
657 | printf("USB is stopped. Please issue 'usb start' first.\n"); | |
658 | return 1; | |
659 | } | |
de39f8c1 | 660 | if (strncmp(argv[1], "tree", 4) == 0) { |
93c2582f | 661 | puts("USB device tree:\n"); |
45bfa47e | 662 | usb_show_tree(); |
e887afc9 WD |
663 | return 0; |
664 | } | |
de39f8c1 | 665 | if (strncmp(argv[1], "inf", 3) == 0) { |
de39f8c1 | 666 | if (argc == 2) { |
6a1b206d | 667 | #ifdef CONFIG_DM_USB |
e6e188f5 | 668 | usb_for_each_root_dev(usb_show_info); |
6a1b206d SG |
669 | #else |
670 | int d; | |
de39f8c1 | 671 | for (d = 0; d < USB_MAX_DEVICE; d++) { |
cad4291c SG |
672 | udev = usb_get_dev_index(d); |
673 | if (udev == NULL) | |
e887afc9 | 674 | break; |
cad4291c SG |
675 | usb_display_desc(udev); |
676 | usb_display_config(udev); | |
e887afc9 | 677 | } |
6a1b206d | 678 | #endif |
e887afc9 | 679 | return 0; |
de39f8c1 | 680 | } else { |
6a1b206d SG |
681 | /* |
682 | * With driver model this isn't right since we can | |
683 | * have multiple controllers and the device numbering | |
684 | * starts at 1 on each bus. | |
685 | */ | |
0b1284eb | 686 | i = dectoul(argv[2], NULL); |
de39f8c1 | 687 | printf("config for device %d\n", i); |
cad4291c SG |
688 | udev = usb_find_device(i); |
689 | if (udev == NULL) { | |
6052cbab | 690 | printf("*** No device available ***\n"); |
e887afc9 | 691 | return 0; |
de39f8c1 | 692 | } else { |
cad4291c SG |
693 | usb_display_desc(udev); |
694 | usb_display_config(udev); | |
e887afc9 WD |
695 | } |
696 | } | |
697 | return 0; | |
698 | } | |
7d9aa8fd JW |
699 | if (strncmp(argv[1], "test", 4) == 0) { |
700 | if (argc < 5) | |
701 | return CMD_RET_USAGE; | |
0b1284eb | 702 | i = dectoul(argv[2], NULL); |
cad4291c SG |
703 | udev = usb_find_device(i); |
704 | if (udev == NULL) { | |
7d9aa8fd JW |
705 | printf("Device %d does not exist.\n", i); |
706 | return 1; | |
707 | } | |
0b1284eb | 708 | i = dectoul(argv[3], NULL); |
cad4291c | 709 | return usb_test(udev, i, argv[4]); |
7d9aa8fd | 710 | } |
e887afc9 | 711 | #ifdef CONFIG_USB_STORAGE |
de39f8c1 | 712 | if (strncmp(argv[1], "stor", 4) == 0) |
f6b44e0e | 713 | return usb_stor_info(); |
9c998aa8 | 714 | |
e33a5c6b | 715 | return blk_common_cmd(argc, argv, UCLASS_USB, &usb_stor_curr_dev); |
c16ad675 | 716 | #else |
4c12eeb8 | 717 | return CMD_RET_USAGE; |
c16ad675 | 718 | #endif /* CONFIG_USB_STORAGE */ |
e887afc9 WD |
719 | } |
720 | ||
0d498393 WD |
721 | U_BOOT_CMD( |
722 | usb, 5, 1, do_usb, | |
2fb2604d | 723 | "USB sub-system", |
1af9f963 VPP |
724 | "start - start (scan) USB controller\n" |
725 | "usb reset - reset (rescan) USB controller\n" | |
b3813a22 VPP |
726 | "usb stop [f] - stop USB [f]=force stop\n" |
727 | "usb tree - show USB device tree\n" | |
5cf91d6b | 728 | "usb info [dev] - show available USB devices\n" |
7d9aa8fd JW |
729 | "usb test [dev] [port] [mode] - set USB 2.0 test mode\n" |
730 | " (specify port 0 to indicate the device's upstream port)\n" | |
731 | " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n" | |
732 | #ifdef CONFIG_USB_STORAGE | |
b3813a22 | 733 | "usb storage - show details of USB storage devices\n" |
342717f7 | 734 | "usb dev [dev] - show or set current USB storage device\n" |
de39f8c1 | 735 | "usb part [dev] - print partition table of one or all USB storage" |
7d9aa8fd | 736 | " devices\n" |
5cf91d6b | 737 | "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
842404ea | 738 | " to memory address `addr'\n" |
127e1084 MJ |
739 | "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n" |
740 | " from memory address `addr'" | |
7d9aa8fd | 741 | #endif /* CONFIG_USB_STORAGE */ |
8bde7f77 WD |
742 | ); |
743 | ||
744 | ||
7d9aa8fd | 745 | #ifdef CONFIG_USB_STORAGE |
0d498393 WD |
746 | U_BOOT_CMD( |
747 | usbboot, 3, 1, do_usbboot, | |
2fb2604d | 748 | "boot from USB device", |
a89c33db | 749 | "loadAddr dev:part" |
8bde7f77 | 750 | ); |
7d9aa8fd | 751 | #endif /* CONFIG_USB_STORAGE */ |