blob: 1ef55dcd1c776705099d337060fc48cd3205775b [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Simon Glass19e81512015-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 Denkd79de1d2013-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 Glass19e81512015-03-25 12:22:02 -060016#include <dm.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060017#include <memalign.h>
wdenk8d5d28a2005-04-02 22:37:54 +000018#include <asm/byteorder.h>
Tom Rini73424522011-12-15 08:40:51 -070019#include <asm/unaligned.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010020#include <part.h>
wdenke887afc2002-08-27 09:44:07 +000021#include <usb.h>
wdenke887afc2002-08-27 09:44:07 +000022
wdenkacd05362005-02-24 23:23:29 +000023#ifdef CONFIG_USB_STORAGE
Wolfgang Denkdc770c72008-07-14 15:19:07 +020024static int usb_stor_curr_dev = -1; /* current device */
wdenkacd05362005-02-24 23:23:29 +000025#endif
Simon Glass5b026f92015-07-06 16:47:50 -060026#if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
Simon Glassdb19dc52015-07-06 16:47:48 -060027static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
Simon Glass1e9961d2011-02-16 11:14:33 -080028#endif
wdenke887afc2002-08-27 09:44:07 +000029
wdenk4ea537d2003-12-07 18:32:37 +000030/* some display routines (info command) */
Kim Phillipsdc00a682012-10-29 13:34:31 +000031static char *usb_get_class_desc(unsigned char dclass)
wdenke887afc2002-08-27 09:44:07 +000032{
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +010033 switch (dclass) {
34 case USB_CLASS_PER_INTERFACE:
35 return "See Interface";
36 case USB_CLASS_AUDIO:
37 return "Audio";
38 case USB_CLASS_COMM:
39 return "Communication";
40 case USB_CLASS_HID:
41 return "Human Interface";
42 case USB_CLASS_PRINTER:
43 return "Printer";
44 case USB_CLASS_MASS_STORAGE:
45 return "Mass Storage";
46 case USB_CLASS_HUB:
47 return "Hub";
48 case USB_CLASS_DATA:
49 return "CDC Data";
50 case USB_CLASS_VENDOR_SPEC:
51 return "Vendor specific";
52 default:
53 return "";
wdenke887afc2002-08-27 09:44:07 +000054 }
55}
56
Kim Phillipsdc00a682012-10-29 13:34:31 +000057static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
58 unsigned char proto)
wdenke887afc2002-08-27 09:44:07 +000059{
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +010060 switch (dclass) {
61 case USB_CLASS_PER_INTERFACE:
62 printf("See Interface");
63 break;
64 case USB_CLASS_HID:
65 printf("Human Interface, Subclass: ");
66 switch (subclass) {
67 case USB_SUB_HID_NONE:
68 printf("None");
wdenke887afc2002-08-27 09:44:07 +000069 break;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +010070 case USB_SUB_HID_BOOT:
71 printf("Boot ");
72 switch (proto) {
73 case USB_PROT_HID_NONE:
74 printf("None");
75 break;
76 case USB_PROT_HID_KEYBOARD:
77 printf("Keyboard");
78 break;
79 case USB_PROT_HID_MOUSE:
80 printf("Mouse");
81 break;
82 default:
83 printf("reserved");
84 break;
wdenke887afc2002-08-27 09:44:07 +000085 }
86 break;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +010087 default:
88 printf("reserved");
89 break;
90 }
91 break;
92 case USB_CLASS_MASS_STORAGE:
93 printf("Mass Storage, ");
94 switch (subclass) {
95 case US_SC_RBC:
96 printf("RBC ");
97 break;
98 case US_SC_8020:
99 printf("SFF-8020i (ATAPI)");
100 break;
101 case US_SC_QIC:
102 printf("QIC-157 (Tape)");
103 break;
104 case US_SC_UFI:
105 printf("UFI");
106 break;
107 case US_SC_8070:
108 printf("SFF-8070");
109 break;
110 case US_SC_SCSI:
111 printf("Transp. SCSI");
wdenke887afc2002-08-27 09:44:07 +0000112 break;
113 default:
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100114 printf("reserved");
115 break;
116 }
117 printf(", ");
118 switch (proto) {
119 case US_PR_CB:
120 printf("Command/Bulk");
121 break;
122 case US_PR_CBI:
123 printf("Command/Bulk/Int");
124 break;
125 case US_PR_BULK:
126 printf("Bulk only");
127 break;
128 default:
129 printf("reserved");
130 break;
131 }
132 break;
133 default:
134 printf("%s", usb_get_class_desc(dclass));
135 break;
wdenke887afc2002-08-27 09:44:07 +0000136 }
137}
138
Kim Phillipsdc00a682012-10-29 13:34:31 +0000139static void usb_display_string(struct usb_device *dev, int index)
wdenke887afc2002-08-27 09:44:07 +0000140{
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530141 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
142
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100143 if (index != 0) {
144 if (usb_string(dev, index, &buffer[0], 256) > 0)
145 printf("String: \"%s\"", buffer);
wdenke887afc2002-08-27 09:44:07 +0000146 }
147}
148
Kim Phillipsdc00a682012-10-29 13:34:31 +0000149static void usb_display_desc(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000150{
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100151 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
152 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
Tom Rix83b9e1d2009-10-31 12:37:38 -0500153 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100154 (dev->descriptor.bcdUSB>>8) & 0xff,
155 dev->descriptor.bcdUSB & 0xff);
156
157 if (strlen(dev->mf) || strlen(dev->prod) ||
158 strlen(dev->serial))
159 printf(" - %s %s %s\n", dev->mf, dev->prod,
160 dev->serial);
wdenke887afc2002-08-27 09:44:07 +0000161 if (dev->descriptor.bDeviceClass) {
162 printf(" - Class: ");
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100163 usb_display_class_sub(dev->descriptor.bDeviceClass,
164 dev->descriptor.bDeviceSubClass,
165 dev->descriptor.bDeviceProtocol);
wdenke887afc2002-08-27 09:44:07 +0000166 printf("\n");
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100167 } else {
168 printf(" - Class: (from Interface) %s\n",
169 usb_get_class_desc(
Tom Rix83b9e1d2009-10-31 12:37:38 -0500170 dev->config.if_desc[0].desc.bInterfaceClass));
wdenke887afc2002-08-27 09:44:07 +0000171 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100172 printf(" - PacketSize: %d Configurations: %d\n",
173 dev->descriptor.bMaxPacketSize0,
174 dev->descriptor.bNumConfigurations);
175 printf(" - Vendor: 0x%04x Product 0x%04x Version %d.%d\n",
176 dev->descriptor.idVendor, dev->descriptor.idProduct,
177 (dev->descriptor.bcdDevice>>8) & 0xff,
178 dev->descriptor.bcdDevice & 0xff);
wdenke887afc2002-08-27 09:44:07 +0000179 }
180
181}
182
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000183static void usb_display_conf_desc(struct usb_config_descriptor *config,
Kim Phillipsdc00a682012-10-29 13:34:31 +0000184 struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000185{
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100186 printf(" Configuration: %d\n", config->bConfigurationValue);
187 printf(" - Interfaces: %d %s%s%dmA\n", config->bNumInterfaces,
188 (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
189 (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
Tom Rix83b9e1d2009-10-31 12:37:38 -0500190 config->bMaxPower*2);
wdenke887afc2002-08-27 09:44:07 +0000191 if (config->iConfiguration) {
192 printf(" - ");
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100193 usb_display_string(dev, config->iConfiguration);
wdenke887afc2002-08-27 09:44:07 +0000194 printf("\n");
195 }
196}
197
Kim Phillipsdc00a682012-10-29 13:34:31 +0000198static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
199 struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000200{
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100201 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
202 printf(" - Alternate Setting %d, Endpoints: %d\n",
203 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
wdenke887afc2002-08-27 09:44:07 +0000204 printf(" - Class ");
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100205 usb_display_class_sub(ifdesc->bInterfaceClass,
206 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
wdenke887afc2002-08-27 09:44:07 +0000207 printf("\n");
208 if (ifdesc->iInterface) {
209 printf(" - ");
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100210 usb_display_string(dev, ifdesc->iInterface);
wdenke887afc2002-08-27 09:44:07 +0000211 printf("\n");
212 }
213}
214
Kim Phillipsdc00a682012-10-29 13:34:31 +0000215static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
wdenke887afc2002-08-27 09:44:07 +0000216{
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100217 printf(" - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
218 (epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
219 switch ((epdesc->bmAttributes & 0x03)) {
220 case 0:
221 printf("Control");
222 break;
223 case 1:
224 printf("Isochronous");
225 break;
226 case 2:
227 printf("Bulk");
228 break;
229 case 3:
230 printf("Interrupt");
231 break;
wdenke887afc2002-08-27 09:44:07 +0000232 }
Tom Rini73424522011-12-15 08:40:51 -0700233 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100234 if ((epdesc->bmAttributes & 0x03) == 0x3)
235 printf(" Interval %dms", epdesc->bInterval);
wdenke887afc2002-08-27 09:44:07 +0000236 printf("\n");
237}
238
239/* main routine to diasplay the configs, interfaces and endpoints */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000240static void usb_display_config(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000241{
Tom Rix83b9e1d2009-10-31 12:37:38 -0500242 struct usb_config *config;
243 struct usb_interface *ifdesc;
wdenke887afc2002-08-27 09:44:07 +0000244 struct usb_endpoint_descriptor *epdesc;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100245 int i, ii;
wdenke887afc2002-08-27 09:44:07 +0000246
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100247 config = &dev->config;
Tom Rix83b9e1d2009-10-31 12:37:38 -0500248 usb_display_conf_desc(&config->desc, dev);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100249 for (i = 0; i < config->no_of_if; i++) {
250 ifdesc = &config->if_desc[i];
Tom Rix83b9e1d2009-10-31 12:37:38 -0500251 usb_display_if_desc(&ifdesc->desc, dev);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100252 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
253 epdesc = &ifdesc->ep_desc[ii];
wdenke887afc2002-08-27 09:44:07 +0000254 usb_display_ep_desc(epdesc);
255 }
256 }
257 printf("\n");
258}
259
Simon Glass19e81512015-03-25 12:22:02 -0600260/*
261 * With driver model this isn't right since we can have multiple controllers
262 * and the device numbering starts at 1 on each bus.
263 * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
264 */
Julius Wernerd4046702013-02-28 18:08:40 +0000265static struct usb_device *usb_find_device(int devnum)
266{
Simon Glass19e81512015-03-25 12:22:02 -0600267#ifdef CONFIG_DM_USB
268 struct usb_device *udev;
269 struct udevice *hub;
270 struct uclass *uc;
271 int ret;
272
273 /* Device addresses start at 1 */
274 devnum++;
275 ret = uclass_get(UCLASS_USB_HUB, &uc);
276 if (ret)
277 return NULL;
278
279 uclass_foreach_dev(hub, uc) {
280 struct udevice *dev;
281
282 if (!device_active(hub))
283 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600284 udev = dev_get_parent_priv(hub);
Simon Glass19e81512015-03-25 12:22:02 -0600285 if (udev->devnum == devnum)
286 return udev;
287
288 for (device_find_first_child(hub, &dev);
289 dev;
290 device_find_next_child(&dev)) {
291 if (!device_active(hub))
292 continue;
293
Simon Glassde44acf2015-09-28 23:32:01 -0600294 udev = dev_get_parent_priv(dev);
Simon Glass19e81512015-03-25 12:22:02 -0600295 if (udev->devnum == devnum)
296 return udev;
297 }
298 }
299#else
Simon Glass729e2572015-03-25 12:22:00 -0600300 struct usb_device *udev;
Julius Wernerd4046702013-02-28 18:08:40 +0000301 int d;
302
303 for (d = 0; d < USB_MAX_DEVICE; d++) {
Simon Glass729e2572015-03-25 12:22:00 -0600304 udev = usb_get_dev_index(d);
305 if (udev == NULL)
Julius Wernerd4046702013-02-28 18:08:40 +0000306 return NULL;
Simon Glass729e2572015-03-25 12:22:00 -0600307 if (udev->devnum == devnum)
308 return udev;
Julius Wernerd4046702013-02-28 18:08:40 +0000309 }
Simon Glass19e81512015-03-25 12:22:02 -0600310#endif
Julius Wernerd4046702013-02-28 18:08:40 +0000311
312 return NULL;
313}
314
Stefan Roese6a6d9d12009-01-22 10:11:21 +0100315static inline char *portspeed(int speed)
316{
Vivek Gautamdf3f2212013-04-12 16:34:38 +0530317 char *speed_str;
318
319 switch (speed) {
320 case USB_SPEED_SUPER:
321 speed_str = "5 Gb/s";
322 break;
323 case USB_SPEED_HIGH:
324 speed_str = "480 Mb/s";
325 break;
326 case USB_SPEED_LOW:
327 speed_str = "1.5 Mb/s";
328 break;
329 default:
330 speed_str = "12 Mb/s";
331 break;
332 }
333
334 return speed_str;
Stefan Roese6a6d9d12009-01-22 10:11:21 +0100335}
336
wdenke887afc2002-08-27 09:44:07 +0000337/* shows the device tree recursively */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000338static void usb_show_tree_graph(struct usb_device *dev, char *pre)
wdenke887afc2002-08-27 09:44:07 +0000339{
Simon Glass19e81512015-03-25 12:22:02 -0600340 int index;
Wolfgang Denk6c7c5582011-10-05 23:01:59 +0200341 int has_child, last_child;
wdenke887afc2002-08-27 09:44:07 +0000342
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100343 index = strlen(pre);
344 printf(" %s", pre);
Simon Glass19e81512015-03-25 12:22:02 -0600345#ifdef CONFIG_DM_USB
346 has_child = device_has_active_children(dev->dev);
347#else
wdenke887afc2002-08-27 09:44:07 +0000348 /* check if the device has connected children */
Simon Glass19e81512015-03-25 12:22:02 -0600349 int i;
350
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100351 has_child = 0;
352 for (i = 0; i < dev->maxchild; i++) {
353 if (dev->children[i] != NULL)
354 has_child = 1;
wdenke887afc2002-08-27 09:44:07 +0000355 }
Simon Glass19e81512015-03-25 12:22:02 -0600356#endif
wdenke887afc2002-08-27 09:44:07 +0000357 /* check if we are the last one */
Simon Glass19e81512015-03-25 12:22:02 -0600358#ifdef CONFIG_DM_USB
Hans de Goedef97c8e22015-06-17 21:33:50 +0200359 /* Not the root of the usb tree? */
360 if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
361 last_child = device_is_last_sibling(dev->dev);
Simon Glass19e81512015-03-25 12:22:02 -0600362#else
Hans de Goedef97c8e22015-06-17 21:33:50 +0200363 if (dev->parent != NULL) { /* not root? */
364 last_child = 1;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100365 for (i = 0; i < dev->parent->maxchild; i++) {
wdenke887afc2002-08-27 09:44:07 +0000366 /* search for children */
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100367 if (dev->parent->children[i] == dev) {
368 /* found our pointer, see if we have a
369 * little sister
370 */
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100371 while (i++ < dev->parent->maxchild) {
372 if (dev->parent->children[i] != NULL) {
wdenke887afc2002-08-27 09:44:07 +0000373 /* found a sister */
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100374 last_child = 0;
wdenke887afc2002-08-27 09:44:07 +0000375 break;
376 } /* if */
377 } /* while */
378 } /* device found */
379 } /* for all children of the parent */
Simon Glass19e81512015-03-25 12:22:02 -0600380#endif
wdenke887afc2002-08-27 09:44:07 +0000381 printf("\b+-");
382 /* correct last child */
Simon Glass729e2572015-03-25 12:22:00 -0600383 if (last_child && index)
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100384 pre[index-1] = ' ';
wdenke887afc2002-08-27 09:44:07 +0000385 } /* if not root hub */
386 else
387 printf(" ");
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100388 printf("%d ", dev->devnum);
389 pre[index++] = ' ';
390 pre[index++] = has_child ? '|' : ' ';
391 pre[index] = 0;
392 printf(" %s (%s, %dmA)\n", usb_get_class_desc(
Tom Rix83b9e1d2009-10-31 12:37:38 -0500393 dev->config.if_desc[0].desc.bInterfaceClass),
Stefan Roese6a6d9d12009-01-22 10:11:21 +0100394 portspeed(dev->speed),
Tom Rix83b9e1d2009-10-31 12:37:38 -0500395 dev->config.desc.bMaxPower * 2);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100396 if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
397 printf(" %s %s %s %s\n", pre, dev->mf, dev->prod, dev->serial);
398 printf(" %s\n", pre);
Simon Glass19e81512015-03-25 12:22:02 -0600399#ifdef CONFIG_DM_USB
400 struct udevice *child;
401
402 for (device_find_first_child(dev->dev, &child);
403 child;
404 device_find_next_child(&child)) {
405 struct usb_device *udev;
406
407 if (!device_active(child))
408 continue;
409
Simon Glassde44acf2015-09-28 23:32:01 -0600410 udev = dev_get_parent_priv(child);
Simon Glass19e81512015-03-25 12:22:02 -0600411
412 /* Ignore emulators, we only want real devices */
413 if (device_get_uclass_id(child) != UCLASS_USB_EMUL) {
414 usb_show_tree_graph(udev, pre);
415 pre[index] = 0;
416 }
417 }
418#else
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100419 if (dev->maxchild > 0) {
420 for (i = 0; i < dev->maxchild; i++) {
421 if (dev->children[i] != NULL) {
422 usb_show_tree_graph(dev->children[i], pre);
423 pre[index] = 0;
wdenke887afc2002-08-27 09:44:07 +0000424 }
425 }
426 }
Simon Glass19e81512015-03-25 12:22:02 -0600427#endif
wdenke887afc2002-08-27 09:44:07 +0000428}
429
430/* main routine for the tree command */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000431static void usb_show_tree(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000432{
433 char preamble[32];
434
Simon Glass729e2572015-03-25 12:22:00 -0600435 memset(preamble, '\0', sizeof(preamble));
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100436 usb_show_tree_graph(dev, &preamble[0]);
wdenke887afc2002-08-27 09:44:07 +0000437}
438
Julius Wernerd4046702013-02-28 18:08:40 +0000439static int usb_test(struct usb_device *dev, int port, char* arg)
440{
441 int mode;
442
443 if (port > dev->maxchild) {
444 printf("Device is no hub or does not have %d ports.\n", port);
445 return 1;
446 }
447
448 switch (arg[0]) {
449 case 'J':
450 case 'j':
451 printf("Setting Test_J mode");
452 mode = USB_TEST_MODE_J;
453 break;
454 case 'K':
455 case 'k':
456 printf("Setting Test_K mode");
457 mode = USB_TEST_MODE_K;
458 break;
459 case 'S':
460 case 's':
461 printf("Setting Test_SE0_NAK mode");
462 mode = USB_TEST_MODE_SE0_NAK;
463 break;
464 case 'P':
465 case 'p':
466 printf("Setting Test_Packet mode");
467 mode = USB_TEST_MODE_PACKET;
468 break;
469 case 'F':
470 case 'f':
471 printf("Setting Test_Force_Enable mode");
472 mode = USB_TEST_MODE_FORCE_ENABLE;
473 break;
474 default:
475 printf("Unrecognized test mode: %s\nAvailable modes: "
476 "J, K, S[E0_NAK], P[acket], F[orce_Enable]\n", arg);
477 return 1;
478 }
479
480 if (port)
481 printf(" on downstream facing port %d...\n", port);
482 else
483 printf(" on upstream facing port...\n");
484
485 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
486 port ? USB_RT_PORT : USB_RECIP_DEVICE,
487 port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
488 (mode << 8) | port,
489 NULL, 0, USB_CNTL_TIMEOUT) == -1) {
490 printf("Error during SET_FEATURE.\n");
491 return 1;
492 } else {
493 printf("Test mode successfully set. Use 'usb start' "
494 "to return to normal operation.\n");
495 return 0;
496 }
497}
498
wdenke887afc2002-08-27 09:44:07 +0000499
wdenke887afc2002-08-27 09:44:07 +0000500/******************************************************************************
501 * usb boot command intepreter. Derived from diskboot
502 */
503#ifdef CONFIG_USB_STORAGE
Kim Phillipsdc00a682012-10-29 13:34:31 +0000504static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000505{
Rob Herringa20cbf92012-09-21 04:02:30 +0000506 return common_diskboot(cmdtp, "usb", argc, argv);
wdenke887afc2002-08-27 09:44:07 +0000507}
508#endif /* CONFIG_USB_STORAGE */
509
Hans de Goede3e51df02014-09-20 16:54:38 +0200510static int do_usb_stop_keyboard(int force)
Hans de Goedec7c11742014-09-20 16:54:35 +0200511{
512#ifdef CONFIG_USB_KEYBOARD
Hans de Goede3e51df02014-09-20 16:54:38 +0200513 if (usb_kbd_deregister(force) != 0) {
Hans de Goedec7c11742014-09-20 16:54:35 +0200514 printf("USB not stopped: usbkbd still using USB\n");
515 return 1;
516 }
517#endif
518 return 0;
519}
wdenke887afc2002-08-27 09:44:07 +0000520
Hans de Goede2af82562015-01-06 14:27:41 +0100521static void do_usb_start(void)
522{
523 bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
524
525 if (usb_init() < 0)
526 return;
527
Simon Glass19e81512015-03-25 12:22:02 -0600528 /* Driver model will probe the devices as they are found */
529#ifndef CONFIG_DM_USB
Hans de Goede2af82562015-01-06 14:27:41 +0100530#ifdef CONFIG_USB_STORAGE
531 /* try to recognize storage devices immediately */
532 usb_stor_curr_dev = usb_stor_scan(1);
533#endif
Marcel Ziswilerb1b83332015-08-05 16:58:17 +0200534#endif
Hans de Goede2af82562015-01-06 14:27:41 +0100535#ifdef CONFIG_USB_HOST_ETHER
Simon Glassdb19dc52015-07-06 16:47:48 -0600536# ifdef CONFIG_DM_ETH
Marcel Ziswilerb1b83332015-08-05 16:58:17 +0200537# ifndef CONFIG_DM_USB
538# error "You must use CONFIG_DM_USB if you want to use CONFIG_USB_HOST_ETHER with CONFIG_DM_ETH"
539# endif
540# else
Hans de Goede2af82562015-01-06 14:27:41 +0100541 /* try to recognize ethernet devices immediately */
542 usb_ether_curr_dev = usb_host_eth_scan(1);
Marcel Ziswilerb1b83332015-08-05 16:58:17 +0200543# endif
Simon Glassdb19dc52015-07-06 16:47:48 -0600544#endif
Hans de Goede2af82562015-01-06 14:27:41 +0100545#ifdef CONFIG_USB_KEYBOARD
546 drv_usb_kbd_init();
547#endif
548}
Simon Glass19e81512015-03-25 12:22:02 -0600549
550#ifdef CONFIG_DM_USB
551static void show_info(struct udevice *dev)
552{
553 struct udevice *child;
554 struct usb_device *udev;
555
Simon Glassde44acf2015-09-28 23:32:01 -0600556 udev = dev_get_parent_priv(dev);
Simon Glass19e81512015-03-25 12:22:02 -0600557 usb_display_desc(udev);
558 usb_display_config(udev);
559 for (device_find_first_child(dev, &child);
560 child;
561 device_find_next_child(&child)) {
562 if (device_active(child))
563 show_info(child);
564 }
565}
566
567static int usb_device_info(void)
568{
569 struct udevice *bus;
570
571 for (uclass_first_device(UCLASS_USB, &bus);
572 bus;
573 uclass_next_device(&bus)) {
574 struct udevice *hub;
575
576 device_find_first_child(bus, &hub);
577 if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
578 device_active(hub)) {
579 show_info(hub);
580 }
581 }
582
583 return 0;
584}
585#endif
Hans de Goede2af82562015-01-06 14:27:41 +0100586
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100587/******************************************************************************
wdenke887afc2002-08-27 09:44:07 +0000588 * usb command intepreter
589 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000590static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000591{
Simon Glass729e2572015-03-25 12:22:00 -0600592 struct usb_device *udev = NULL;
wdenke887afc2002-08-27 09:44:07 +0000593 int i;
Bartlomiej Siekae39589f2006-08-03 23:20:13 +0200594 extern char usb_started;
wdenkacd05362005-02-24 23:23:29 +0000595#ifdef CONFIG_USB_STORAGE
wdenke887afc2002-08-27 09:44:07 +0000596 block_dev_desc_t *stor_dev;
wdenkacd05362005-02-24 23:23:29 +0000597#endif
wdenke887afc2002-08-27 09:44:07 +0000598
Wolfgang Denk3b683112010-07-17 01:06:04 +0200599 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000600 return CMD_RET_USAGE;
Serge Ziryukin84e20a22010-04-25 21:32:36 +0300601
Hans de Goede2af82562015-01-06 14:27:41 +0100602 if (strncmp(argv[1], "start", 5) == 0) {
603 if (usb_started)
604 return 0; /* Already started */
605 printf("starting USB...\n");
606 do_usb_start();
607 return 0;
608 }
609
610 if (strncmp(argv[1], "reset", 5) == 0) {
611 printf("resetting USB...\n");
Hans de Goede3e51df02014-09-20 16:54:38 +0200612 if (do_usb_stop_keyboard(1) != 0)
Hans de Goedec7c11742014-09-20 16:54:35 +0200613 return 1;
wdenke887afc2002-08-27 09:44:07 +0000614 usb_stop();
Hans de Goede2af82562015-01-06 14:27:41 +0100615 do_usb_start();
wdenke887afc2002-08-27 09:44:07 +0000616 return 0;
617 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100618 if (strncmp(argv[1], "stop", 4) == 0) {
Hans de Goedec7c11742014-09-20 16:54:35 +0200619 if (argc != 2)
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100620 console_assign(stdin, "serial");
Hans de Goede3e51df02014-09-20 16:54:38 +0200621 if (do_usb_stop_keyboard(0) != 0)
Hans de Goedec7c11742014-09-20 16:54:35 +0200622 return 1;
wdenke887afc2002-08-27 09:44:07 +0000623 printf("stopping USB..\n");
624 usb_stop();
625 return 0;
626 }
Bartlomiej Siekae39589f2006-08-03 23:20:13 +0200627 if (!usb_started) {
628 printf("USB is stopped. Please issue 'usb start' first.\n");
629 return 1;
630 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100631 if (strncmp(argv[1], "tree", 4) == 0) {
Lucas Stache6d33452012-09-26 00:14:36 +0200632 puts("USB device tree:\n");
Simon Glass19e81512015-03-25 12:22:02 -0600633#ifdef CONFIG_DM_USB
634 struct udevice *bus;
635
636 for (uclass_first_device(UCLASS_USB, &bus);
637 bus;
638 uclass_next_device(&bus)) {
639 struct usb_device *udev;
Hans de Goede9c203592015-06-17 21:33:53 +0200640 struct udevice *dev;
Simon Glass19e81512015-03-25 12:22:02 -0600641
Hans de Goede9c203592015-06-17 21:33:53 +0200642 device_find_first_child(bus, &dev);
643 if (dev && device_active(dev)) {
Simon Glassde44acf2015-09-28 23:32:01 -0600644 udev = dev_get_parent_priv(dev);
Simon Glass19e81512015-03-25 12:22:02 -0600645 usb_show_tree(udev);
646 }
647 }
648#else
Lucas Stache6d33452012-09-26 00:14:36 +0200649 for (i = 0; i < USB_MAX_DEVICE; i++) {
Simon Glass729e2572015-03-25 12:22:00 -0600650 udev = usb_get_dev_index(i);
651 if (udev == NULL)
Lucas Stache6d33452012-09-26 00:14:36 +0200652 break;
Simon Glass729e2572015-03-25 12:22:00 -0600653 if (udev->parent == NULL)
654 usb_show_tree(udev);
Lucas Stache6d33452012-09-26 00:14:36 +0200655 }
Simon Glass19e81512015-03-25 12:22:02 -0600656#endif
wdenke887afc2002-08-27 09:44:07 +0000657 return 0;
658 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100659 if (strncmp(argv[1], "inf", 3) == 0) {
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100660 if (argc == 2) {
Simon Glass19e81512015-03-25 12:22:02 -0600661#ifdef CONFIG_DM_USB
662 usb_device_info();
663#else
664 int d;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100665 for (d = 0; d < USB_MAX_DEVICE; d++) {
Simon Glass729e2572015-03-25 12:22:00 -0600666 udev = usb_get_dev_index(d);
667 if (udev == NULL)
wdenke887afc2002-08-27 09:44:07 +0000668 break;
Simon Glass729e2572015-03-25 12:22:00 -0600669 usb_display_desc(udev);
670 usb_display_config(udev);
wdenke887afc2002-08-27 09:44:07 +0000671 }
Simon Glass19e81512015-03-25 12:22:02 -0600672#endif
wdenke887afc2002-08-27 09:44:07 +0000673 return 0;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100674 } else {
Simon Glass19e81512015-03-25 12:22:02 -0600675 /*
676 * With driver model this isn't right since we can
677 * have multiple controllers and the device numbering
678 * starts at 1 on each bus.
679 */
Julius Wernerd4046702013-02-28 18:08:40 +0000680 i = simple_strtoul(argv[2], NULL, 10);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100681 printf("config for device %d\n", i);
Simon Glass729e2572015-03-25 12:22:00 -0600682 udev = usb_find_device(i);
683 if (udev == NULL) {
Loïc Minier5d0569a2011-02-03 22:04:26 +0100684 printf("*** No device available ***\n");
wdenke887afc2002-08-27 09:44:07 +0000685 return 0;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100686 } else {
Simon Glass729e2572015-03-25 12:22:00 -0600687 usb_display_desc(udev);
688 usb_display_config(udev);
wdenke887afc2002-08-27 09:44:07 +0000689 }
690 }
691 return 0;
692 }
Julius Wernerd4046702013-02-28 18:08:40 +0000693 if (strncmp(argv[1], "test", 4) == 0) {
694 if (argc < 5)
695 return CMD_RET_USAGE;
696 i = simple_strtoul(argv[2], NULL, 10);
Simon Glass729e2572015-03-25 12:22:00 -0600697 udev = usb_find_device(i);
698 if (udev == NULL) {
Julius Wernerd4046702013-02-28 18:08:40 +0000699 printf("Device %d does not exist.\n", i);
700 return 1;
701 }
702 i = simple_strtoul(argv[3], NULL, 10);
Simon Glass729e2572015-03-25 12:22:00 -0600703 return usb_test(udev, i, argv[4]);
Julius Wernerd4046702013-02-28 18:08:40 +0000704 }
wdenke887afc2002-08-27 09:44:07 +0000705#ifdef CONFIG_USB_STORAGE
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100706 if (strncmp(argv[1], "stor", 4) == 0)
Aras Vaichas7ede1862008-03-25 12:09:07 +1100707 return usb_stor_info();
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200708
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100709 if (strncmp(argv[1], "part", 4) == 0) {
Christian Eggers1eef5c32008-06-27 19:46:51 +0200710 int devno, ok = 0;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100711 if (argc == 2) {
Kim B. Heino300ea512010-03-12 15:46:56 +0200712 for (devno = 0; ; ++devno) {
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100713 stor_dev = usb_stor_get_dev(devno);
Kim B. Heino300ea512010-03-12 15:46:56 +0200714 if (stor_dev == NULL)
715 break;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100716 if (stor_dev->type != DEV_TYPE_UNKNOWN) {
Christian Eggers1eef5c32008-06-27 19:46:51 +0200717 ok++;
718 if (devno)
719 printf("\n");
Wolfgang Denk2f4d3652010-03-25 14:07:23 +0100720 debug("print_part of %x\n", devno);
Christian Eggers1eef5c32008-06-27 19:46:51 +0200721 print_part(stor_dev);
722 }
723 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100724 } else {
725 devno = simple_strtoul(argv[2], NULL, 16);
726 stor_dev = usb_stor_get_dev(devno);
Kim B. Heino300ea512010-03-12 15:46:56 +0200727 if (stor_dev != NULL &&
728 stor_dev->type != DEV_TYPE_UNKNOWN) {
wdenke887afc2002-08-27 09:44:07 +0000729 ok++;
Wolfgang Denk2f4d3652010-03-25 14:07:23 +0100730 debug("print_part of %x\n", devno);
wdenke887afc2002-08-27 09:44:07 +0000731 print_part(stor_dev);
732 }
733 }
734 if (!ok) {
735 printf("\nno USB devices available\n");
736 return 1;
737 }
738 return 0;
739 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100740 if (strcmp(argv[1], "read") == 0) {
741 if (usb_stor_curr_dev < 0) {
wdenke887afc2002-08-27 09:44:07 +0000742 printf("no current device selected\n");
743 return 1;
744 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100745 if (argc == 5) {
wdenke887afc2002-08-27 09:44:07 +0000746 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
747 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
748 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
749 unsigned long n;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100750 printf("\nUSB read: device %d block # %ld, count %ld"
751 " ... ", usb_stor_curr_dev, blk, cnt);
752 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
753 n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
754 (ulong *)addr);
755 printf("%ld blocks read: %s\n", n,
756 (n == cnt) ? "OK" : "ERROR");
757 if (n == cnt)
wdenke887afc2002-08-27 09:44:07 +0000758 return 0;
759 return 1;
760 }
761 }
Mahavir Jaind43a0b82009-11-03 12:22:10 +0530762 if (strcmp(argv[1], "write") == 0) {
763 if (usb_stor_curr_dev < 0) {
764 printf("no current device selected\n");
765 return 1;
766 }
767 if (argc == 5) {
768 unsigned long addr = simple_strtoul(argv[2], NULL, 16);
769 unsigned long blk = simple_strtoul(argv[3], NULL, 16);
770 unsigned long cnt = simple_strtoul(argv[4], NULL, 16);
771 unsigned long n;
772 printf("\nUSB write: device %d block # %ld, count %ld"
773 " ... ", usb_stor_curr_dev, blk, cnt);
774 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
775 n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
776 (ulong *)addr);
777 printf("%ld blocks write: %s\n", n,
778 (n == cnt) ? "OK" : "ERROR");
779 if (n == cnt)
780 return 0;
781 return 1;
782 }
783 }
Wolfgang Denkc7a4f7d2005-07-21 11:57:57 +0200784 if (strncmp(argv[1], "dev", 3) == 0) {
785 if (argc == 3) {
wdenke887afc2002-08-27 09:44:07 +0000786 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100787 printf("\nUSB device %d: ", dev);
Kim B. Heino300ea512010-03-12 15:46:56 +0200788 stor_dev = usb_stor_get_dev(dev);
789 if (stor_dev == NULL) {
wdenke887afc2002-08-27 09:44:07 +0000790 printf("unknown device\n");
791 return 1;
792 }
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100793 printf("\n Device %d: ", dev);
wdenke887afc2002-08-27 09:44:07 +0000794 dev_print(stor_dev);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100795 if (stor_dev->type == DEV_TYPE_UNKNOWN)
wdenke887afc2002-08-27 09:44:07 +0000796 return 1;
wdenke887afc2002-08-27 09:44:07 +0000797 usb_stor_curr_dev = dev;
798 printf("... is now current device\n");
799 return 0;
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100800 } else {
801 printf("\nUSB device %d: ", usb_stor_curr_dev);
802 stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
wdenke887afc2002-08-27 09:44:07 +0000803 dev_print(stor_dev);
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100804 if (stor_dev->type == DEV_TYPE_UNKNOWN)
wdenke887afc2002-08-27 09:44:07 +0000805 return 1;
wdenke887afc2002-08-27 09:44:07 +0000806 return 0;
807 }
808 return 0;
809 }
810#endif /* CONFIG_USB_STORAGE */
Simon Glassa06dfc72011-12-10 08:44:01 +0000811 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000812}
813
wdenkf287a242003-07-01 21:06:45 +0000814U_BOOT_CMD(
815 usb, 5, 1, do_usb,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600816 "USB sub-system",
Veli-Pekka Peltoladdb90d32011-11-02 16:59:56 +0200817 "start - start (scan) USB controller\n"
818 "usb reset - reset (rescan) USB controller\n"
Veli-Pekka Peltola1f47a4d2011-11-02 16:59:55 +0200819 "usb stop [f] - stop USB [f]=force stop\n"
820 "usb tree - show USB device tree\n"
wdenk20c98a62004-04-23 20:32:05 +0000821 "usb info [dev] - show available USB devices\n"
Julius Wernerd4046702013-02-28 18:08:40 +0000822 "usb test [dev] [port] [mode] - set USB 2.0 test mode\n"
823 " (specify port 0 to indicate the device's upstream port)\n"
824 " Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]\n"
825#ifdef CONFIG_USB_STORAGE
Veli-Pekka Peltola1f47a4d2011-11-02 16:59:55 +0200826 "usb storage - show details of USB storage devices\n"
wdenka2b932d2005-06-27 13:30:03 +0000827 "usb dev [dev] - show or set current USB storage device\n"
Michael Trimarchi5f2e7fc2008-11-26 17:41:34 +0100828 "usb part [dev] - print partition table of one or all USB storage"
Julius Wernerd4046702013-02-28 18:08:40 +0000829 " devices\n"
wdenk20c98a62004-04-23 20:32:05 +0000830 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
Sergei Poselenov95b57aa2010-08-09 16:01:42 +0400831 " to memory address `addr'\n"
Mahavir Jaind43a0b82009-11-03 12:22:10 +0530832 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
833 " from memory address `addr'"
Julius Wernerd4046702013-02-28 18:08:40 +0000834#endif /* CONFIG_USB_STORAGE */
wdenk57b2d802003-06-27 21:31:46 +0000835);
836
837
Julius Wernerd4046702013-02-28 18:08:40 +0000838#ifdef CONFIG_USB_STORAGE
wdenkf287a242003-07-01 21:06:45 +0000839U_BOOT_CMD(
840 usbboot, 3, 1, do_usbboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600841 "boot from USB device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200842 "loadAddr dev:part"
wdenk57b2d802003-06-27 21:31:46 +0000843);
Julius Wernerd4046702013-02-28 18:08:40 +0000844#endif /* CONFIG_USB_STORAGE */