blob: 0ade7759f08e68b6cdffb5f9466016df09cc84d6 [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Simon Glass6a1b2062015-03-25 12:22:02 -06005 * Adapted for U-Boot driver model
6 * (C) Copyright 2015 Google, Inc
7 *
wdenke887afc2002-08-27 09:44:07 +00008 * Most of this source has been derived from the Linux USB
9 * project.
10 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
wdenke887afc2002-08-27 09:44:07 +000012 */
13
14#include <common.h>
15#include <command.h>
Simon Glass6a1b2062015-03-25 12:22:02 -060016#include <dm.h>
wdenk414eec32005-04-02 22:37:54 +000017#include <asm/byteorder.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070018#include <asm/unaligned.h>
Grant Likely735dd972007-02-20 09:04:34 +010019#include <part.h>
wdenke887afc2002-08-27 09:44:07 +000020#include <usb.h>
wdenke887afc2002-08-27 09:44:07 +000021
wdenk1968e612005-02-24 23:23:29 +000022#ifdef CONFIG_USB_STORAGE
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +020023static int usb_stor_curr_dev = -1; /* current device */
wdenk1968e612005-02-24 23:23:29 +000024#endif
Simon Glassc8c27972015-07-06 16:47:50 -060025#if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
Simon Glass69559092015-07-06 16:47:48 -060026static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
Simon Glass89d48362011-02-16 11:14:33 -080027#endif
wdenke887afc2002-08-27 09:44:07 +000028
wdenka2663ea2003-12-07 18:32:37 +000029/* some display routines (info command) */
Kim Phillips088f1b12012-10-29 13:34:31 +000030static char *usb_get_class_desc(unsigned char dclass)
wdenke887afc2002-08-27 09:44:07 +000031{
Michael Trimarchide39f8c2008-11-26 17:41:34 +010032 switch (dclass) {
33 case USB_CLASS_PER_INTERFACE:
34 return "See Interface";
35 case USB_CLASS_AUDIO:
36 return "Audio";
37 case USB_CLASS_COMM:
38 return "Communication";
39 case USB_CLASS_HID:
40 return "Human Interface";
41 case USB_CLASS_PRINTER:
42 return "Printer";
43 case USB_CLASS_MASS_STORAGE:
44 return "Mass Storage";
45 case USB_CLASS_HUB:
46 return "Hub";
47 case USB_CLASS_DATA:
48 return "CDC Data";
49 case USB_CLASS_VENDOR_SPEC:
50 return "Vendor specific";
51 default:
52 return "";
wdenke887afc2002-08-27 09:44:07 +000053 }
54}
55
Kim Phillips088f1b12012-10-29 13:34:31 +000056static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
57 unsigned char proto)
wdenke887afc2002-08-27 09:44:07 +000058{
Michael Trimarchide39f8c2008-11-26 17:41:34 +010059 switch (dclass) {
60 case USB_CLASS_PER_INTERFACE:
61 printf("See Interface");
62 break;
63 case USB_CLASS_HID:
64 printf("Human Interface, Subclass: ");
65 switch (subclass) {
66 case USB_SUB_HID_NONE:
67 printf("None");
wdenke887afc2002-08-27 09:44:07 +000068 break;
Michael Trimarchide39f8c2008-11-26 17:41:34 +010069 case USB_SUB_HID_BOOT:
70 printf("Boot ");
71 switch (proto) {
72 case USB_PROT_HID_NONE:
73 printf("None");
74 break;
75 case USB_PROT_HID_KEYBOARD:
76 printf("Keyboard");
77 break;
78 case USB_PROT_HID_MOUSE:
79 printf("Mouse");
80 break;
81 default:
82 printf("reserved");
83 break;
wdenke887afc2002-08-27 09:44:07 +000084 }
85 break;
86 default:
Michael Trimarchide39f8c2008-11-26 17:41:34 +010087 printf("reserved");
88 break;
89 }
90 break;
91 case USB_CLASS_MASS_STORAGE:
92 printf("Mass Storage, ");
93 switch (subclass) {
94 case US_SC_RBC:
95 printf("RBC ");
96 break;
97 case US_SC_8020:
98 printf("SFF-8020i (ATAPI)");
99 break;
100 case US_SC_QIC:
101 printf("QIC-157 (Tape)");
102 break;
103 case US_SC_UFI:
104 printf("UFI");
105 break;
106 case US_SC_8070:
107 printf("SFF-8070");
108 break;
109 case US_SC_SCSI:
110 printf("Transp. SCSI");
111 break;
112 default:
113 printf("reserved");
114 break;
115 }
116 printf(", ");
117 switch (proto) {
118 case US_PR_CB:
119 printf("Command/Bulk");
120 break;
121 case US_PR_CBI:
122 printf("Command/Bulk/Int");
123 break;
124 case US_PR_BULK:
125 printf("Bulk only");
126 break;
127 default:
128 printf("reserved");
129 break;
130 }
131 break;
132 default:
133 printf("%s", usb_get_class_desc(dclass));
134 break;
wdenke887afc2002-08-27 09:44:07 +0000135 }
136}
137
Kim Phillips088f1b12012-10-29 13:34:31 +0000138static void usb_display_string(struct usb_device *dev, int index)
wdenke887afc2002-08-27 09:44:07 +0000139{
Puneet Saxenaf5766132012-04-03 14:56:06 +0530140 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
141
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100142 if (index != 0) {
143 if (usb_string(dev, index, &buffer[0], 256) > 0)
144 printf("String: \"%s\"", buffer);
wdenke887afc2002-08-27 09:44:07 +0000145 }
146}
147
Kim Phillips088f1b12012-10-29 13:34:31 +0000148static void usb_display_desc(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000149{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100150 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
151 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
Tom Rix8f8bd562009-10-31 12:37:38 -0500152 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100153 (dev->descriptor.bcdUSB>>8) & 0xff,
154 dev->descriptor.bcdUSB & 0xff);
155
156 if (strlen(dev->mf) || strlen(dev->prod) ||
157 strlen(dev->serial))
158 printf(" - %s %s %s\n", dev->mf, dev->prod,
159 dev->serial);
wdenke887afc2002-08-27 09:44:07 +0000160 if (dev->descriptor.bDeviceClass) {
161 printf(" - Class: ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100162 usb_display_class_sub(dev->descriptor.bDeviceClass,
163 dev->descriptor.bDeviceSubClass,
164 dev->descriptor.bDeviceProtocol);
wdenke887afc2002-08-27 09:44:07 +0000165 printf("\n");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100166 } else {
167 printf(" - Class: (from Interface) %s\n",
168 usb_get_class_desc(
Tom Rix8f8bd562009-10-31 12:37:38 -0500169 dev->config.if_desc[0].desc.bInterfaceClass));
wdenke887afc2002-08-27 09:44:07 +0000170 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100171 printf(" - PacketSize: %d Configurations: %d\n",
172 dev->descriptor.bMaxPacketSize0,
173 dev->descriptor.bNumConfigurations);
174 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
175 dev->descriptor.idVendor, dev->descriptor.idProduct,
176 (dev->descriptor.bcdDevice>>8) & 0xff,
177 dev->descriptor.bcdDevice & 0xff);
wdenke887afc2002-08-27 09:44:07 +0000178 }
179
180}
181
Ilya Yanokc60795f2012-11-06 13:48:20 +0000182static void usb_display_conf_desc(struct usb_config_descriptor *config,
Kim Phillips088f1b12012-10-29 13:34:31 +0000183 struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000184{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100185 printf(" Configuration: %d\n", config->bConfigurationValue);
186 printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
187 (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
188 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
Tom Rix8f8bd562009-10-31 12:37:38 -0500189 config->bMaxPower*2);
wdenke887afc2002-08-27 09:44:07 +0000190 if (config->iConfiguration) {
191 printf(" - ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100192 usb_display_string(dev, config->iConfiguration);
wdenke887afc2002-08-27 09:44:07 +0000193 printf("\n");
194 }
195}
196
Kim Phillips088f1b12012-10-29 13:34:31 +0000197static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
198 struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000199{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100200 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
201 printf(" - Alternate Setting %d, Endpoints: %d\n",
202 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
wdenke887afc2002-08-27 09:44:07 +0000203 printf(" - Class ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100204 usb_display_class_sub(ifdesc->bInterfaceClass,
205 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
wdenke887afc2002-08-27 09:44:07 +0000206 printf("\n");
207 if (ifdesc->iInterface) {
208 printf(" - ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100209 usb_display_string(dev, ifdesc->iInterface);
wdenke887afc2002-08-27 09:44:07 +0000210 printf("\n");
211 }
212}
213
Kim Phillips088f1b12012-10-29 13:34:31 +0000214static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
wdenke887afc2002-08-27 09:44:07 +0000215{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100216 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
217 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
218 switch ((epdesc->bmAttributes & 0x03)) {
219 case 0:
220 printf("Control");
221 break;
222 case 1:
223 printf("Isochronous");
224 break;
225 case 2:
226 printf("Bulk");
227 break;
228 case 3:
229 printf("Interrupt");
230 break;
wdenke887afc2002-08-27 09:44:07 +0000231 }
Tom Rinib2fb47f2011-12-15 08:40:51 -0700232 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100233 if ((epdesc->bmAttributes & 0x03) == 0x3)
234 printf(" Interval %dms", epdesc->bInterval);
wdenke887afc2002-08-27 09:44:07 +0000235 printf("\n");
236}
237
238/* main routine to diasplay the configs, interfaces and endpoints */
Kim Phillips088f1b12012-10-29 13:34:31 +0000239static void usb_display_config(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000240{
Tom Rix8f8bd562009-10-31 12:37:38 -0500241 struct usb_config *config;
242 struct usb_interface *ifdesc;
wdenke887afc2002-08-27 09:44:07 +0000243 struct usb_endpoint_descriptor *epdesc;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100244 int i, ii;
wdenke887afc2002-08-27 09:44:07 +0000245
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100246 config = &dev->config;
Tom Rix8f8bd562009-10-31 12:37:38 -0500247 usb_display_conf_desc(&config->desc, dev);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100248 for (i = 0; i < config->no_of_if; i++) {
249 ifdesc = &config->if_desc[i];
Tom Rix8f8bd562009-10-31 12:37:38 -0500250 usb_display_if_desc(&ifdesc->desc, dev);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100251 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
252 epdesc = &ifdesc->ep_desc[ii];
wdenke887afc2002-08-27 09:44:07 +0000253 usb_display_ep_desc(epdesc);
254 }
255 }
256 printf("\n");
257}
258
Simon Glass6a1b2062015-03-25 12:22:02 -0600259/*
260 * With driver model this isn't right since we can have multiple controllers
261 * and the device numbering starts at 1 on each bus.
262 * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
263 */
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000264static struct usb_device *usb_find_device(int devnum)
265{
Simon Glass6a1b2062015-03-25 12:22:02 -0600266#ifdef CONFIG_DM_USB
267 struct usb_device *udev;
268 struct udevice *hub;
269 struct uclass *uc;
270 int ret;
271
272 /* Device addresses start at 1 */
273 devnum++;
274 ret = uclass_get(UCLASS_USB_HUB, &uc);
275 if (ret)
276 return NULL;
277
278 uclass_foreach_dev(hub, uc) {
279 struct udevice *dev;
280
281 if (!device_active(hub))
282 continue;
283 udev = dev_get_parentdata(hub);
284 if (udev->devnum == devnum)
285 return udev;
286
287 for (device_find_first_child(hub, &dev);
288 dev;
289 device_find_next_child(&dev)) {
290 if (!device_active(hub))
291 continue;
292
293 udev = dev_get_parentdata(dev);
294 if (udev->devnum == devnum)
295 return udev;
296 }
297 }
298#else
Simon Glasscad42912015-03-25 12:22:00 -0600299 struct usb_device *udev;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000300 int d;
301
302 for (d = 0; d < USB_MAX_DEVICE; d++) {
Simon Glasscad42912015-03-25 12:22:00 -0600303 udev = usb_get_dev_index(d);
304 if (udev == NULL)
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000305 return NULL;
Simon Glasscad42912015-03-25 12:22:00 -0600306 if (udev->devnum == devnum)
307 return udev;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000308 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600309#endif
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000310
311 return NULL;
312}
313
Stefan Roesef1c1f542009-01-22 10:11:21 +0100314static inline char *portspeed(int speed)
315{
Vivek Gautam6497c662013-04-12 16:34:38 +0530316 char *speed_str;
317
318 switch (speed) {
319 case USB_SPEED_SUPER:
320 speed_str = "5 Gb/s";
321 break;
322 case USB_SPEED_HIGH:
323 speed_str = "480 Mb/s";
324 break;
325 case USB_SPEED_LOW:
326 speed_str = "1.5 Mb/s";
327 break;
328 default:
329 speed_str = "12 Mb/s";
330 break;
331 }
332
333 return speed_str;
Stefan Roesef1c1f542009-01-22 10:11:21 +0100334}
335
wdenke887afc2002-08-27 09:44:07 +0000336/* shows the device tree recursively */
Kim Phillips088f1b12012-10-29 13:34:31 +0000337static void usb_show_tree_graph(struct usb_device *dev, char *pre)
wdenke887afc2002-08-27 09:44:07 +0000338{
Simon Glass6a1b2062015-03-25 12:22:02 -0600339 int index;
Wolfgang Denk10f4dd72011-10-05 23:01:59 +0200340 int has_child, last_child;
wdenke887afc2002-08-27 09:44:07 +0000341
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100342 index = strlen(pre);
343 printf(" %s", pre);
Simon Glass6a1b2062015-03-25 12:22:02 -0600344#ifdef CONFIG_DM_USB
345 has_child = device_has_active_children(dev->dev);
346#else
wdenke887afc2002-08-27 09:44:07 +0000347 /* check if the device has connected children */
Simon Glass6a1b2062015-03-25 12:22:02 -0600348 int i;
349
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100350 has_child = 0;
351 for (i = 0; i < dev->maxchild; i++) {
352 if (dev->children[i] != NULL)
353 has_child = 1;
wdenke887afc2002-08-27 09:44:07 +0000354 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600355#endif
wdenke887afc2002-08-27 09:44:07 +0000356 /* check if we are the last one */
Simon Glass6a1b2062015-03-25 12:22:02 -0600357#ifdef CONFIG_DM_USB
Hans de Goedec27b3292015-06-17 21:33:50 +0200358 /* Not the root of the usb tree? */
359 if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
360 last_child = device_is_last_sibling(dev->dev);
Simon Glass6a1b2062015-03-25 12:22:02 -0600361#else
Hans de Goedec27b3292015-06-17 21:33:50 +0200362 if (dev->parent != NULL) { /* not root? */
363 last_child = 1;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100364 for (i = 0; i < dev->parent->maxchild; i++) {
wdenke887afc2002-08-27 09:44:07 +0000365 /* search for children */
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100366 if (dev->parent->children[i] == dev) {
367 /* found our pointer, see if we have a
368 * little sister
369 */
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100370 while (i++ < dev->parent->maxchild) {
371 if (dev->parent->children[i] != NULL) {
wdenke887afc2002-08-27 09:44:07 +0000372 /* found a sister */
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100373 last_child = 0;
wdenke887afc2002-08-27 09:44:07 +0000374 break;
375 } /* if */
376 } /* while */
377 } /* device found */
378 } /* for all children of the parent */
Simon Glass6a1b2062015-03-25 12:22:02 -0600379#endif
wdenke887afc2002-08-27 09:44:07 +0000380 printf("\b+-");
381 /* correct last child */
Simon Glasscad42912015-03-25 12:22:00 -0600382 if (last_child && index)
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100383 pre[index-1] = ' ';
wdenke887afc2002-08-27 09:44:07 +0000384 } /* if not root hub */
385 else
386 printf(" ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100387 printf("%d ", dev->devnum);
388 pre[index++] = ' ';
389 pre[index++] = has_child ? '|' : ' ';
390 pre[index] = 0;
391 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
Tom Rix8f8bd562009-10-31 12:37:38 -0500392 dev->config.if_desc[0].desc.bInterfaceClass),
Stefan Roesef1c1f542009-01-22 10:11:21 +0100393 portspeed(dev->speed),
Tom Rix8f8bd562009-10-31 12:37:38 -0500394 dev->config.desc.bMaxPower * 2);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100395 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
396 printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
397 printf(" %s\n", pre);
Simon Glass6a1b2062015-03-25 12:22:02 -0600398#ifdef CONFIG_DM_USB
399 struct udevice *child;
400
401 for (device_find_first_child(dev->dev, &child);
402 child;
403 device_find_next_child(&child)) {
404 struct usb_device *udev;
405
406 if (!device_active(child))
407 continue;
408
409 udev = dev_get_parentdata(child);
410
411 /* Ignore emulators, we only want real devices */
412 if (device_get_uclass_id(child) != UCLASS_USB_EMUL) {
413 usb_show_tree_graph(udev, pre);
414 pre[index] = 0;
415 }
416 }
417#else
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100418 if (dev->maxchild > 0) {
419 for (i = 0; i < dev->maxchild; i++) {
420 if (dev->children[i] != NULL) {
421 usb_show_tree_graph(dev->children[i], pre);
422 pre[index] = 0;
wdenke887afc2002-08-27 09:44:07 +0000423 }
424 }
425 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600426#endif
wdenke887afc2002-08-27 09:44:07 +0000427}
428
429/* main routine for the tree command */
Kim Phillips088f1b12012-10-29 13:34:31 +0000430static void usb_show_tree(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000431{
432 char preamble[32];
433
Simon Glasscad42912015-03-25 12:22:00 -0600434 memset(preamble, '\0', sizeof(preamble));
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100435 usb_show_tree_graph(dev, &preamble[0]);
wdenke887afc2002-08-27 09:44:07 +0000436}
437
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000438static int usb_test(struct usb_device *dev, int port, char* arg)
439{
440 int mode;
441
442 if (port > dev->maxchild) {
443 printf("Device is no hub or does not have %d ports.\n", port);
444 return 1;
445 }
446
447 switch (arg[0]) {
448 case 'J':
449 case 'j':
450 printf("Setting Test_J mode");
451 mode = USB_TEST_MODE_J;
452 break;
453 case 'K':
454 case 'k':
455 printf("Setting Test_K mode");
456 mode = USB_TEST_MODE_K;
457 break;
458 case 'S':
459 case 's':
460 printf("Setting Test_SE0_NAK mode");
461 mode = USB_TEST_MODE_SE0_NAK;
462 break;
463 case 'P':
464 case 'p':
465 printf("Setting Test_Packet mode");
466 mode = USB_TEST_MODE_PACKET;
467 break;
468 case 'F':
469 case 'f':
470 printf("Setting Test_Force_Enable mode");
471 mode = USB_TEST_MODE_FORCE_ENABLE;
472 break;
473 default:
474 printf("Unrecognized test mode: %s\nAvailable modes: "
475 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
476 return 1;
477 }
478
479 if (port)
480 printf(" on downstream facing port %d...\n", port);
481 else
482 printf(" on upstream facing port...\n");
483
484 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
485 port ? USB_RT_PORT : USB_RECIP_DEVICE,
486 port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
487 (mode << 8) | port,
488 NULL, 0, USB_CNTL_TIMEOUT) == -1) {
489 printf("Error during SET_FEATURE.\n");
490 return 1;
491 } else {
492 printf("Test mode successfully set. Use 'usb start' "
493 "to return to normal operation.\n");
494 return 0;
495 }
496}
497
wdenke887afc2002-08-27 09:44:07 +0000498
wdenke887afc2002-08-27 09:44:07 +0000499/******************************************************************************
500 * usb boot command intepreter. Derived from diskboot
501 */
502#ifdef CONFIG_USB_STORAGE
Kim Phillips088f1b12012-10-29 13:34:31 +0000503static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000504{
Rob Herring7405a132012-09-21 04:02:30 +0000505 return common_diskboot(cmdtp, "usb", argc, argv);
wdenke887afc2002-08-27 09:44:07 +0000506}
507#endif /* CONFIG_USB_STORAGE */
508
Hans de Goede8a8a2252014-09-20 16:54:38 +0200509static int do_usb_stop_keyboard(int force)
Hans de Goede6e78c742014-09-20 16:54:35 +0200510{
511#ifdef CONFIG_USB_KEYBOARD
Hans de Goede8a8a2252014-09-20 16:54:38 +0200512 if (usb_kbd_deregister(force) != 0) {
Hans de Goede6e78c742014-09-20 16:54:35 +0200513 printf("USB not stopped: usbkbd still using USB\n");
514 return 1;
515 }
516#endif
517 return 0;
518}
wdenke887afc2002-08-27 09:44:07 +0000519
Hans de Goedeb5072262015-01-06 14:27:41 +0100520static void do_usb_start(void)
521{
522 bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
523
524 if (usb_init() < 0)
525 return;
526
Simon Glass6a1b2062015-03-25 12:22:02 -0600527 /* Driver model will probe the devices as they are found */
528#ifndef CONFIG_DM_USB
Hans de Goedeb5072262015-01-06 14:27:41 +0100529#ifdef CONFIG_USB_STORAGE
530 /* try to recognize storage devices immediately */
531 usb_stor_curr_dev = usb_stor_scan(1);
532#endif
533#ifdef CONFIG_USB_HOST_ETHER
Simon Glass69559092015-07-06 16:47:48 -0600534# ifdef CONFIG_DM_ETH
535# error "You must use CONFIG_DM_USB if you want to use CONFIG_USB_HOST_ETHER with CONFIG_DM_ETH"
536# endif
Hans de Goedeb5072262015-01-06 14:27:41 +0100537 /* try to recognize ethernet devices immediately */
538 usb_ether_curr_dev = usb_host_eth_scan(1);
539#endif
Simon Glass69559092015-07-06 16:47:48 -0600540#endif
Hans de Goedeb5072262015-01-06 14:27:41 +0100541#ifdef CONFIG_USB_KEYBOARD
542 drv_usb_kbd_init();
543#endif
544}
545
Simon Glass6a1b2062015-03-25 12:22:02 -0600546#ifdef CONFIG_DM_USB
547static void show_info(struct udevice *dev)
548{
549 struct udevice *child;
550 struct usb_device *udev;
551
552 udev = dev_get_parentdata(dev);
553 usb_display_desc(udev);
554 usb_display_config(udev);
555 for (device_find_first_child(dev, &child);
556 child;
557 device_find_next_child(&child)) {
558 if (device_active(child))
559 show_info(child);
560 }
561}
562
563static int usb_device_info(void)
564{
565 struct udevice *bus;
566
567 for (uclass_first_device(UCLASS_USB, &bus);
568 bus;
569 uclass_next_device(&bus)) {
570 struct udevice *hub;
571
572 device_find_first_child(bus, &hub);
573 if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
574 device_active(hub)) {
575 show_info(hub);
576 }
577 }
578
579 return 0;
580}
581#endif
582
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100583/******************************************************************************
wdenke887afc2002-08-27 09:44:07 +0000584 * usb command intepreter
585 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000586static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000587{
Simon Glasscad42912015-03-25 12:22:00 -0600588 struct usb_device *udev = NULL;
wdenke887afc2002-08-27 09:44:07 +0000589 int i;
Bartlomiej Siekae51aae32006-08-03 23:20:13 +0200590 extern char usb_started;
wdenk1968e612005-02-24 23:23:29 +0000591#ifdef CONFIG_USB_STORAGE
wdenke887afc2002-08-27 09:44:07 +0000592 block_dev_desc_t *stor_dev;
wdenk1968e612005-02-24 23:23:29 +0000593#endif
wdenke887afc2002-08-27 09:44:07 +0000594
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200595 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000596 return CMD_RET_USAGE;
Serge Ziryukin65d34252010-04-25 21:32:36 +0300597
Hans de Goedeb5072262015-01-06 14:27:41 +0100598 if (strncmp(argv[1], "start", 5) == 0) {
599 if (usb_started)
600 return 0; /* Already started */
601 printf("starting USB...\n");
602 do_usb_start();
603 return 0;
604 }
605
606 if (strncmp(argv[1], "reset", 5) == 0) {
607 printf("resetting USB...\n");
Hans de Goede8a8a2252014-09-20 16:54:38 +0200608 if (do_usb_stop_keyboard(1) != 0)
Hans de Goede6e78c742014-09-20 16:54:35 +0200609 return 1;
wdenke887afc2002-08-27 09:44:07 +0000610 usb_stop();
Hans de Goedeb5072262015-01-06 14:27:41 +0100611 do_usb_start();
wdenke887afc2002-08-27 09:44:07 +0000612 return 0;
613 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100614 if (strncmp(argv[1], "stop", 4) == 0) {
Hans de Goede6e78c742014-09-20 16:54:35 +0200615 if (argc != 2)
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100616 console_assign(stdin, "serial");
Hans de Goede8a8a2252014-09-20 16:54:38 +0200617 if (do_usb_stop_keyboard(0) != 0)
Hans de Goede6e78c742014-09-20 16:54:35 +0200618 return 1;
wdenke887afc2002-08-27 09:44:07 +0000619 printf("stopping USB..\n");
620 usb_stop();
621 return 0;
622 }
Bartlomiej Siekae51aae32006-08-03 23:20:13 +0200623 if (!usb_started) {
624 printf("USB is stopped. Please issue 'usb start' first.\n");
625 return 1;
626 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100627 if (strncmp(argv[1], "tree", 4) == 0) {
Lucas Stach93c25822012-09-26 00:14:36 +0200628 puts("USB device tree:\n");
Simon Glass6a1b2062015-03-25 12:22:02 -0600629#ifdef CONFIG_DM_USB
630 struct udevice *bus;
631
632 for (uclass_first_device(UCLASS_USB, &bus);
633 bus;
634 uclass_next_device(&bus)) {
635 struct usb_device *udev;
Hans de Goedefd1bd212015-06-17 21:33:53 +0200636 struct udevice *dev;
Simon Glass6a1b2062015-03-25 12:22:02 -0600637
Hans de Goedefd1bd212015-06-17 21:33:53 +0200638 device_find_first_child(bus, &dev);
639 if (dev && device_active(dev)) {
640 udev = dev_get_parentdata(dev);
Simon Glass6a1b2062015-03-25 12:22:02 -0600641 usb_show_tree(udev);
642 }
643 }
644#else
Lucas Stach93c25822012-09-26 00:14:36 +0200645 for (i = 0; i < USB_MAX_DEVICE; i++) {
Simon Glasscad42912015-03-25 12:22:00 -0600646 udev = usb_get_dev_index(i);
647 if (udev == NULL)
Lucas Stach93c25822012-09-26 00:14:36 +0200648 break;
Simon Glasscad42912015-03-25 12:22:00 -0600649 if (udev->parent == NULL)
650 usb_show_tree(udev);
Lucas Stach93c25822012-09-26 00:14:36 +0200651 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600652#endif
wdenke887afc2002-08-27 09:44:07 +0000653 return 0;
654 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100655 if (strncmp(argv[1], "inf", 3) == 0) {
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100656 if (argc == 2) {
Simon Glass6a1b2062015-03-25 12:22:02 -0600657#ifdef CONFIG_DM_USB
658 usb_device_info();
659#else
660 int d;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100661 for (d = 0; d < USB_MAX_DEVICE; d++) {
Simon Glasscad42912015-03-25 12:22:00 -0600662 udev = usb_get_dev_index(d);
663 if (udev == NULL)
wdenke887afc2002-08-27 09:44:07 +0000664 break;
Simon Glasscad42912015-03-25 12:22:00 -0600665 usb_display_desc(udev);
666 usb_display_config(udev);
wdenke887afc2002-08-27 09:44:07 +0000667 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600668#endif
wdenke887afc2002-08-27 09:44:07 +0000669 return 0;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100670 } else {
Simon Glass6a1b2062015-03-25 12:22:02 -0600671 /*
672 * With driver model this isn't right since we can
673 * have multiple controllers and the device numbering
674 * starts at 1 on each bus.
675 */
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000676 i = simple_strtoul(argv[2], NULL, 10);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100677 printf("config for device %d\n", i);
Simon Glasscad42912015-03-25 12:22:00 -0600678 udev = usb_find_device(i);
679 if (udev == NULL) {
Loïc Minier6052cba2011-02-03 22:04:26 +0100680 printf("*** No device available ***\n");
wdenke887afc2002-08-27 09:44:07 +0000681 return 0;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100682 } else {
Simon Glasscad42912015-03-25 12:22:00 -0600683 usb_display_desc(udev);
684 usb_display_config(udev);
wdenke887afc2002-08-27 09:44:07 +0000685 }
686 }
687 return 0;
688 }
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000689 if (strncmp(argv[1], "test", 4) == 0) {
690 if (argc < 5)
691 return CMD_RET_USAGE;
692 i = simple_strtoul(argv[2], NULL, 10);
Simon Glasscad42912015-03-25 12:22:00 -0600693 udev = usb_find_device(i);
694 if (udev == NULL) {
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000695 printf("Device %d does not exist.\n", i);
696 return 1;
697 }
698 i = simple_strtoul(argv[3], NULL, 10);
Simon Glasscad42912015-03-25 12:22:00 -0600699 return usb_test(udev, i, argv[4]);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000700 }
wdenke887afc2002-08-27 09:44:07 +0000701#ifdef CONFIG_USB_STORAGE
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100702 if (strncmp(argv[1], "stor", 4) == 0)
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100703 return usb_stor_info();
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200704
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100705 if (strncmp(argv[1], "part", 4) == 0) {
Christian Eggersd4b5f3f2008-06-27 19:46:51 +0200706 int devno, ok = 0;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100707 if (argc == 2) {
Kim B. Heinoaaad1082010-03-12 15:46:56 +0200708 for (devno = 0; ; ++devno) {
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100709 stor_dev = usb_stor_get_dev(devno);
Kim B. Heinoaaad1082010-03-12 15:46:56 +0200710 if (stor_dev == NULL)
711 break;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100712 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
Christian Eggersd4b5f3f2008-06-27 19:46:51 +0200713 ok++;
714 if (devno)
715 printf("\n");
Wolfgang Denk060f2852010-03-25 14:07:23 +0100716 debug("print_part of %x\n", devno);
Christian Eggersd4b5f3f2008-06-27 19:46:51 +0200717 print_part(stor_dev);
718 }
719 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100720 } else {
721 devno = simple_strtoul(argv[2], NULL, 16);
722 stor_dev = usb_stor_get_dev(devno);
Kim B. Heinoaaad1082010-03-12 15:46:56 +0200723 if (stor_dev != NULL &&
724 stor_dev->type != DEV_TYPE_UNKNOWN) {
wdenke887afc2002-08-27 09:44:07 +0000725 ok++;
Wolfgang Denk060f2852010-03-25 14:07:23 +0100726 debug("print_part of %x\n", devno);
wdenke887afc2002-08-27 09:44:07 +0000727 print_part(stor_dev);
728 }
729 }
730 if (!ok) {
731 printf("\nno USB devices available\n");
732 return 1;
733 }
734 return 0;
735 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100736 if (strcmp(argv[1], "read") == 0) {
737 if (usb_stor_curr_dev < 0) {
wdenke887afc2002-08-27 09:44:07 +0000738 printf("no current device selected\n");
739 return 1;
740 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100741 if (argc == 5) {
wdenke887afc2002-08-27 09:44:07 +0000742 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
743 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
744 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
745 unsigned long n;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100746 printf("\nUSB read: device %d block # %ld, count %ld"
747 " ... ", usb_stor_curr_dev, blk, cnt);
748 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
749 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
750 (ulong *)addr);
751 printf("%ld blocks read: %s\n", n,
752 (n == cnt) ? "OK" : "ERROR");
753 if (n == cnt)
wdenke887afc2002-08-27 09:44:07 +0000754 return 0;
755 return 1;
756 }
757 }
Mahavir Jain127e1082009-11-03 12:22:10 +0530758 if (strcmp(argv[1], "write") == 0) {
759 if (usb_stor_curr_dev < 0) {
760 printf("no current device selected\n");
761 return 1;
762 }
763 if (argc == 5) {
764 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
765 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
766 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
767 unsigned long n;
768 printf("\nUSB write: device %d block # %ld, count %ld"
769 " ... ", usb_stor_curr_dev, blk, cnt);
770 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
771 n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
772 (ulong *)addr);
773 printf("%ld blocks write: %s\n", n,
774 (n == cnt) ? "OK" : "ERROR");
775 if (n == cnt)
776 return 0;
777 return 1;
778 }
779 }
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200780 if (strncmp(argv[1], "dev", 3) == 0) {
781 if (argc == 3) {
wdenke887afc2002-08-27 09:44:07 +0000782 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100783 printf("\nUSB device %d: ", dev);
Kim B. Heinoaaad1082010-03-12 15:46:56 +0200784 stor_dev = usb_stor_get_dev(dev);
785 if (stor_dev == NULL) {
wdenke887afc2002-08-27 09:44:07 +0000786 printf("unknown device\n");
787 return 1;
788 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100789 printf("\n Device %d: ", dev);
wdenke887afc2002-08-27 09:44:07 +0000790 dev_print(stor_dev);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100791 if (stor_dev->type == DEV_TYPE_UNKNOWN)
wdenke887afc2002-08-27 09:44:07 +0000792 return 1;
wdenke887afc2002-08-27 09:44:07 +0000793 usb_stor_curr_dev = dev;
794 printf("... is now current device\n");
795 return 0;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100796 } else {
797 printf("\nUSB device %d: ", usb_stor_curr_dev);
798 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
wdenke887afc2002-08-27 09:44:07 +0000799 dev_print(stor_dev);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100800 if (stor_dev->type == DEV_TYPE_UNKNOWN)
wdenke887afc2002-08-27 09:44:07 +0000801 return 1;
wdenke887afc2002-08-27 09:44:07 +0000802 return 0;
803 }
804 return 0;
805 }
806#endif /* CONFIG_USB_STORAGE */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000807 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000808}
809
wdenk0d498392003-07-01 21:06:45 +0000810U_BOOT_CMD(
811 usb, 5, 1, do_usb,
Peter Tyser2fb26042009-01-27 18:03:12 -0600812 "USB sub-system",
Veli-Pekka Peltola1af9f962011-11-02 16:59:56 +0200813 "start - start (scan) USB controller\n"
814 "usb reset - reset (rescan) USB controller\n"
Veli-Pekka Peltolab3813a22011-11-02 16:59:55 +0200815 "usb stop [f] - stop USB [f]=force stop\n"
816 "usb tree - show USB device tree\n"
wdenk5cf91d62004-04-23 20:32:05 +0000817 "usb info [dev] - show available USB devices\n"
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000818 "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
819 " (specify port 0 to indicate the device's upstream port)\n"
820 " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
821#ifdef CONFIG_USB_STORAGE
Veli-Pekka Peltolab3813a22011-11-02 16:59:55 +0200822 "usb storage - show details of USB storage devices\n"
wdenk342717f2005-06-27 13:30:03 +0000823 "usb dev [dev] - show or set current USB storage device\n"
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100824 "usb part [dev] - print partition table of one or all USB storage"
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000825 " devices\n"
wdenk5cf91d62004-04-23 20:32:05 +0000826 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
Sergei Poselenov842404e2010-08-09 16:01:42 +0400827 " to memory address `addr'\n"
Mahavir Jain127e1082009-11-03 12:22:10 +0530828 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
829 " from memory address `addr'"
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000830#endif /* CONFIG_USB_STORAGE */
wdenk8bde7f72003-06-27 21:31:46 +0000831);
832
833
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000834#ifdef CONFIG_USB_STORAGE
wdenk0d498392003-07-01 21:06:45 +0000835U_BOOT_CMD(
836 usbboot, 3, 1, do_usbboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600837 "boot from USB device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200838 "loadAddr dev:part"
wdenk8bde7f72003-06-27 21:31:46 +0000839);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000840#endif /* CONFIG_USB_STORAGE */