blob: 23253f22231c7e65e8bc4369a0d013f9ad0d900f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenke887afc2002-08-27 09:44:07 +00002/*
3 * (C) Copyright 2001
4 * Denis Peter, MPL AG Switzerland
5 *
Simon Glass6a1b2062015-03-25 12:22:02 -06006 * Adapted for U-Boot driver model
7 * (C) Copyright 2015 Google, Inc
8 *
wdenke887afc2002-08-27 09:44:07 +00009 * Most of this source has been derived from the Linux USB
10 * project.
wdenke887afc2002-08-27 09:44:07 +000011 */
12
13#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060014#include <blk.h>
Simon Glass52f24232020-05-10 11:40:00 -060015#include <bootstage.h>
wdenke887afc2002-08-27 09:44:07 +000016#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070017#include <console.h>
Simon Glass6a1b2062015-03-25 12:22:02 -060018#include <dm.h>
Hans de Goede192eab92016-04-03 00:04:39 +020019#include <dm/uclass-internal.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
wdenk414eec32005-04-02 22:37:54 +000021#include <asm/byteorder.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070022#include <asm/unaligned.h>
Grant Likely735dd972007-02-20 09:04:34 +010023#include <part.h>
wdenke887afc2002-08-27 09:44:07 +000024#include <usb.h>
wdenke887afc2002-08-27 09:44:07 +000025
wdenk1968e612005-02-24 23:23:29 +000026#ifdef CONFIG_USB_STORAGE
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +020027static int usb_stor_curr_dev = -1; /* current device */
wdenk1968e612005-02-24 23:23:29 +000028#endif
Simon Glassc8c27972015-07-06 16:47:50 -060029#if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
Simon Glass69559092015-07-06 16:47:48 -060030static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
Simon Glass89d48362011-02-16 11:14:33 -080031#endif
wdenke887afc2002-08-27 09:44:07 +000032
wdenka2663ea2003-12-07 18:32:37 +000033/* some display routines (info command) */
Kim Phillips088f1b12012-10-29 13:34:31 +000034static char *usb_get_class_desc(unsigned char dclass)
wdenke887afc2002-08-27 09:44:07 +000035{
Michael Trimarchide39f8c2008-11-26 17:41:34 +010036 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 "";
wdenke887afc2002-08-27 09:44:07 +000057 }
58}
59
Kim Phillips088f1b12012-10-29 13:34:31 +000060static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
61 unsigned char proto)
wdenke887afc2002-08-27 09:44:07 +000062{
Michael Trimarchide39f8c2008-11-26 17:41:34 +010063 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");
wdenke887afc2002-08-27 09:44:07 +000072 break;
Michael Trimarchide39f8c2008-11-26 17:41:34 +010073 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;
wdenke887afc2002-08-27 09:44:07 +000088 }
89 break;
90 default:
Michael Trimarchide39f8c2008-11-26 17:41:34 +010091 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");
130 break;
131 default:
132 printf("reserved");
133 break;
134 }
135 break;
136 default:
137 printf("%s", usb_get_class_desc(dclass));
138 break;
wdenke887afc2002-08-27 09:44:07 +0000139 }
140}
141
Kim Phillips088f1b12012-10-29 13:34:31 +0000142static void usb_display_string(struct usb_device *dev, int index)
wdenke887afc2002-08-27 09:44:07 +0000143{
Puneet Saxenaf5766132012-04-03 14:56:06 +0530144 ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
145
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100146 if (index != 0) {
147 if (usb_string(dev, index, &buffer[0], 256) > 0)
148 printf("String: \"%s\"", buffer);
wdenke887afc2002-08-27 09:44:07 +0000149 }
150}
151
Kim Phillips088f1b12012-10-29 13:34:31 +0000152static void usb_display_desc(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000153{
Bin Meng78abf142017-07-19 21:50:07 +0800154 uint packet_size = dev->descriptor.bMaxPacketSize0;
155
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100156 if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
157 printf("%d: %s, USB Revision %x.%x\n", dev->devnum,
Tom Rix8f8bd562009-10-31 12:37:38 -0500158 usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100159 (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);
wdenke887afc2002-08-27 09:44:07 +0000166 if (dev->descriptor.bDeviceClass) {
167 printf(" - Class: ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100168 usb_display_class_sub(dev->descriptor.bDeviceClass,
169 dev->descriptor.bDeviceSubClass,
170 dev->descriptor.bDeviceProtocol);
wdenke887afc2002-08-27 09:44:07 +0000171 printf("\n");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100172 } else {
173 printf(" - Class: (from Interface) %s\n",
174 usb_get_class_desc(
Tom Rix8f8bd562009-10-31 12:37:38 -0500175 dev->config.if_desc[0].desc.bInterfaceClass));
wdenke887afc2002-08-27 09:44:07 +0000176 }
Bin Meng78abf142017-07-19 21:50:07 +0800177 if (dev->descriptor.bcdUSB >= cpu_to_le16(0x0300))
178 packet_size = 1 << packet_size;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100179 printf(" - PacketSize: %d Configurations: %d\n",
Bin Meng78abf142017-07-19 21:50:07 +0800180 packet_size, dev->descriptor.bNumConfigurations);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100181 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);
wdenke887afc2002-08-27 09:44:07 +0000185 }
186
187}
188
Ilya Yanokc60795f2012-11-06 13:48:20 +0000189static void usb_display_conf_desc(struct usb_config_descriptor *config,
Kim Phillips088f1b12012-10-29 13:34:31 +0000190 struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000191{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100192 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 " : "",
Tom Rix8f8bd562009-10-31 12:37:38 -0500196 config->bMaxPower*2);
wdenke887afc2002-08-27 09:44:07 +0000197 if (config->iConfiguration) {
198 printf(" - ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100199 usb_display_string(dev, config->iConfiguration);
wdenke887afc2002-08-27 09:44:07 +0000200 printf("\n");
201 }
202}
203
Kim Phillips088f1b12012-10-29 13:34:31 +0000204static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
205 struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000206{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100207 printf(" Interface: %d\n", ifdesc->bInterfaceNumber);
208 printf(" - Alternate Setting %d, Endpoints: %d\n",
209 ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
wdenke887afc2002-08-27 09:44:07 +0000210 printf(" - Class ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100211 usb_display_class_sub(ifdesc->bInterfaceClass,
212 ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
wdenke887afc2002-08-27 09:44:07 +0000213 printf("\n");
214 if (ifdesc->iInterface) {
215 printf(" - ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100216 usb_display_string(dev, ifdesc->iInterface);
wdenke887afc2002-08-27 09:44:07 +0000217 printf("\n");
218 }
219}
220
Kim Phillips088f1b12012-10-29 13:34:31 +0000221static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
wdenke887afc2002-08-27 09:44:07 +0000222{
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100223 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;
wdenke887afc2002-08-27 09:44:07 +0000238 }
Tom Rinib2fb47f2011-12-15 08:40:51 -0700239 printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100240 if ((epdesc->bmAttributes & 0x03) == 0x3)
241 printf(" Interval %dms", epdesc->bInterval);
wdenke887afc2002-08-27 09:44:07 +0000242 printf("\n");
243}
244
245/* main routine to diasplay the configs, interfaces and endpoints */
Kim Phillips088f1b12012-10-29 13:34:31 +0000246static void usb_display_config(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000247{
Tom Rix8f8bd562009-10-31 12:37:38 -0500248 struct usb_config *config;
249 struct usb_interface *ifdesc;
wdenke887afc2002-08-27 09:44:07 +0000250 struct usb_endpoint_descriptor *epdesc;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100251 int i, ii;
wdenke887afc2002-08-27 09:44:07 +0000252
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100253 config = &dev->config;
Tom Rix8f8bd562009-10-31 12:37:38 -0500254 usb_display_conf_desc(&config->desc, dev);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100255 for (i = 0; i < config->no_of_if; i++) {
256 ifdesc = &config->if_desc[i];
Tom Rix8f8bd562009-10-31 12:37:38 -0500257 usb_display_if_desc(&ifdesc->desc, dev);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100258 for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
259 epdesc = &ifdesc->ep_desc[ii];
wdenke887afc2002-08-27 09:44:07 +0000260 usb_display_ep_desc(epdesc);
261 }
262 }
263 printf("\n");
264}
265
Simon Glass6a1b2062015-03-25 12:22:02 -0600266/*
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(sjg@chromium.org): Add a way to specify the controller/bus.
270 */
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000271static struct usb_device *usb_find_device(int devnum)
272{
Simon Glass6a1b2062015-03-25 12:22:02 -0600273#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;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600290 udev = dev_get_parent_priv(hub);
Simon Glass6a1b2062015-03-25 12:22:02 -0600291 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
Simon Glassbcbe3d12015-09-28 23:32:01 -0600300 udev = dev_get_parent_priv(dev);
Simon Glass6a1b2062015-03-25 12:22:02 -0600301 if (udev->devnum == devnum)
302 return udev;
303 }
304 }
305#else
Simon Glasscad42912015-03-25 12:22:00 -0600306 struct usb_device *udev;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000307 int d;
308
309 for (d = 0; d < USB_MAX_DEVICE; d++) {
Simon Glasscad42912015-03-25 12:22:00 -0600310 udev = usb_get_dev_index(d);
311 if (udev == NULL)
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000312 return NULL;
Simon Glasscad42912015-03-25 12:22:00 -0600313 if (udev->devnum == devnum)
314 return udev;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000315 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600316#endif
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000317
318 return NULL;
319}
320
Ismael Luceno Cortes51f60a82019-04-01 16:09:13 +0000321static inline const char *portspeed(int speed)
Stefan Roesef1c1f542009-01-22 10:11:21 +0100322{
Vivek Gautam6497c662013-04-12 16:34:38 +0530323 switch (speed) {
324 case USB_SPEED_SUPER:
Ismael Luceno Cortes51f60a82019-04-01 16:09:13 +0000325 return "5 Gb/s";
Vivek Gautam6497c662013-04-12 16:34:38 +0530326 case USB_SPEED_HIGH:
Ismael Luceno Cortes51f60a82019-04-01 16:09:13 +0000327 return "480 Mb/s";
Vivek Gautam6497c662013-04-12 16:34:38 +0530328 case USB_SPEED_LOW:
Ismael Luceno Cortes51f60a82019-04-01 16:09:13 +0000329 return "1.5 Mb/s";
Vivek Gautam6497c662013-04-12 16:34:38 +0530330 default:
Ismael Luceno Cortes51f60a82019-04-01 16:09:13 +0000331 return "12 Mb/s";
Vivek Gautam6497c662013-04-12 16:34:38 +0530332 }
Stefan Roesef1c1f542009-01-22 10:11:21 +0100333}
334
wdenke887afc2002-08-27 09:44:07 +0000335/* shows the device tree recursively */
Kim Phillips088f1b12012-10-29 13:34:31 +0000336static void usb_show_tree_graph(struct usb_device *dev, char *pre)
wdenke887afc2002-08-27 09:44:07 +0000337{
Simon Glass6a1b2062015-03-25 12:22:02 -0600338 int index;
Wolfgang Denk10f4dd72011-10-05 23:01:59 +0200339 int has_child, last_child;
wdenke887afc2002-08-27 09:44:07 +0000340
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100341 index = strlen(pre);
342 printf(" %s", pre);
Simon Glass6a1b2062015-03-25 12:22:02 -0600343#ifdef CONFIG_DM_USB
344 has_child = device_has_active_children(dev->dev);
Suneel Garapatiabd7ced2017-10-23 17:28:40 -0700345 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 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600355#else
wdenke887afc2002-08-27 09:44:07 +0000356 /* check if the device has connected children */
Simon Glass6a1b2062015-03-25 12:22:02 -0600357 int i;
358
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100359 has_child = 0;
360 for (i = 0; i < dev->maxchild; i++) {
361 if (dev->children[i] != NULL)
362 has_child = 1;
wdenke887afc2002-08-27 09:44:07 +0000363 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600364#endif
wdenke887afc2002-08-27 09:44:07 +0000365 /* check if we are the last one */
Simon Glass6a1b2062015-03-25 12:22:02 -0600366#ifdef CONFIG_DM_USB
Hans de Goedec27b3292015-06-17 21:33:50 +0200367 /* 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);
Simon Glass6a1b2062015-03-25 12:22:02 -0600370#else
Hans de Goedec27b3292015-06-17 21:33:50 +0200371 if (dev->parent != NULL) { /* not root? */
372 last_child = 1;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100373 for (i = 0; i < dev->parent->maxchild; i++) {
wdenke887afc2002-08-27 09:44:07 +0000374 /* search for children */
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100375 if (dev->parent->children[i] == dev) {
376 /* found our pointer, see if we have a
377 * little sister
378 */
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100379 while (i++ < dev->parent->maxchild) {
380 if (dev->parent->children[i] != NULL) {
wdenke887afc2002-08-27 09:44:07 +0000381 /* found a sister */
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100382 last_child = 0;
wdenke887afc2002-08-27 09:44:07 +0000383 break;
384 } /* if */
385 } /* while */
386 } /* device found */
387 } /* for all children of the parent */
Simon Glass6a1b2062015-03-25 12:22:02 -0600388#endif
wdenke887afc2002-08-27 09:44:07 +0000389 printf("\b+-");
390 /* correct last child */
Simon Glasscad42912015-03-25 12:22:00 -0600391 if (last_child && index)
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100392 pre[index-1] = ' ';
wdenke887afc2002-08-27 09:44:07 +0000393 } /* if not root hub */
394 else
395 printf(" ");
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100396 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(
Tom Rix8f8bd562009-10-31 12:37:38 -0500401 dev->config.if_desc[0].desc.bInterfaceClass),
Stefan Roesef1c1f542009-01-22 10:11:21 +0100402 portspeed(dev->speed),
Tom Rix8f8bd562009-10-31 12:37:38 -0500403 dev->config.desc.bMaxPower * 2);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100404 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);
Simon Glass6a1b2062015-03-25 12:22:02 -0600407#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
Simon Glassbcbe3d12015-09-28 23:32:01 -0600418 udev = dev_get_parent_priv(child);
Simon Glass6a1b2062015-03-25 12:22:02 -0600419
Suneel Garapatiabd7ced2017-10-23 17:28:40 -0700420 /*
421 * Ignore emulators and block child devices, we only want
422 * real devices
423 */
Xavier Drudis Ferran7a875a82023-06-21 09:13:07 +0200424 if (udev &&
425 (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
426 (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
Suneel Garapatiabd7ced2017-10-23 17:28:40 -0700427 (device_get_uclass_id(child) != UCLASS_BLK)) {
Simon Glass6a1b2062015-03-25 12:22:02 -0600428 usb_show_tree_graph(udev, pre);
429 pre[index] = 0;
430 }
431 }
432#else
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100433 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;
wdenke887afc2002-08-27 09:44:07 +0000438 }
439 }
440 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600441#endif
wdenke887afc2002-08-27 09:44:07 +0000442}
443
444/* main routine for the tree command */
Simon Glass45bfa472015-11-08 23:47:51 -0700445static void usb_show_subtree(struct usb_device *dev)
wdenke887afc2002-08-27 09:44:07 +0000446{
447 char preamble[32];
448
Simon Glasscad42912015-03-25 12:22:00 -0600449 memset(preamble, '\0', sizeof(preamble));
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100450 usb_show_tree_graph(dev, &preamble[0]);
wdenke887afc2002-08-27 09:44:07 +0000451}
452
Simon Glass45bfa472015-11-08 23:47:51 -0700453#ifdef CONFIG_DM_USB
Hans de Goede2138fd62016-07-03 20:22:04 +0200454typedef void (*usb_dev_func_t)(struct usb_device *udev);
455
456static void usb_for_each_root_dev(usb_dev_func_t func)
457{
Simon Glass45bfa472015-11-08 23:47:51 -0700458 struct udevice *bus;
459
Hans de Goede192eab92016-04-03 00:04:39 +0200460 for (uclass_find_first_device(UCLASS_USB, &bus);
Simon Glass45bfa472015-11-08 23:47:51 -0700461 bus;
Hans de Goede192eab92016-04-03 00:04:39 +0200462 uclass_find_next_device(&bus)) {
Simon Glass45bfa472015-11-08 23:47:51 -0700463 struct usb_device *udev;
464 struct udevice *dev;
465
Hans de Goede192eab92016-04-03 00:04:39 +0200466 if (!device_active(bus))
467 continue;
468
Simon Glass45bfa472015-11-08 23:47:51 -0700469 device_find_first_child(bus, &dev);
470 if (dev && device_active(dev)) {
471 udev = dev_get_parent_priv(dev);
Hans de Goede2138fd62016-07-03 20:22:04 +0200472 func(udev);
Simon Glass45bfa472015-11-08 23:47:51 -0700473 }
474 }
Hans de Goede2138fd62016-07-03 20:22:04 +0200475}
476#endif
477
478void usb_show_tree(void)
479{
480#ifdef CONFIG_DM_USB
481 usb_for_each_root_dev(usb_show_subtree);
Simon Glass45bfa472015-11-08 23:47:51 -0700482#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
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000496static 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
wdenke887afc2002-08-27 09:44:07 +0000556
wdenke887afc2002-08-27 09:44:07 +0000557/******************************************************************************
558 * usb boot command intepreter. Derived from diskboot
559 */
560#ifdef CONFIG_USB_STORAGE
Simon Glass09140112020-05-10 11:40:03 -0600561static int do_usbboot(struct cmd_tbl *cmdtp, int flag, int argc,
562 char *const argv[])
wdenke887afc2002-08-27 09:44:07 +0000563{
Rob Herring7405a132012-09-21 04:02:30 +0000564 return common_diskboot(cmdtp, "usb", argc, argv);
wdenke887afc2002-08-27 09:44:07 +0000565}
566#endif /* CONFIG_USB_STORAGE */
567
Hans de Goede8a8a2252014-09-20 16:54:38 +0200568static int do_usb_stop_keyboard(int force)
Hans de Goede6e78c742014-09-20 16:54:35 +0200569{
Hans de Goede9a80e712016-04-03 09:18:53 +0200570#if !defined CONFIG_DM_USB && defined CONFIG_USB_KEYBOARD
Hans de Goede8a8a2252014-09-20 16:54:38 +0200571 if (usb_kbd_deregister(force) != 0) {
Hans de Goede6e78c742014-09-20 16:54:35 +0200572 printf("USB not stopped: usbkbd still using USB\n");
573 return 1;
574 }
575#endif
576 return 0;
577}
wdenke887afc2002-08-27 09:44:07 +0000578
Hans de Goedeb5072262015-01-06 14:27:41 +0100579static 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
Simon Glass6a1b2062015-03-25 12:22:02 -0600586 /* Driver model will probe the devices as they are found */
Simon Glass34ab37e2015-09-08 11:15:11 -0600587# ifdef CONFIG_USB_STORAGE
Hans de Goedeb5072262015-01-06 14:27:41 +0100588 /* try to recognize storage devices immediately */
589 usb_stor_curr_dev = usb_stor_scan(1);
Simon Glass34ab37e2015-09-08 11:15:11 -0600590# endif
Michal Simekb9847002016-12-21 09:35:08 +0100591#ifndef CONFIG_DM_USB
Simon Glass34ab37e2015-09-08 11:15:11 -0600592# ifdef CONFIG_USB_KEYBOARD
593 drv_usb_kbd_init();
594# endif
595#endif /* !CONFIG_DM_USB */
Hans de Goedeb5072262015-01-06 14:27:41 +0100596}
597
Simon Glass6a1b2062015-03-25 12:22:02 -0600598#ifdef CONFIG_DM_USB
Hans de Goedee6e188f2016-07-03 20:22:05 +0200599static void usb_show_info(struct usb_device *udev)
Simon Glass6a1b2062015-03-25 12:22:02 -0600600{
601 struct udevice *child;
Simon Glass6a1b2062015-03-25 12:22:02 -0600602
Simon Glass6a1b2062015-03-25 12:22:02 -0600603 usb_display_desc(udev);
604 usb_display_config(udev);
Hans de Goedee6e188f2016-07-03 20:22:05 +0200605 for (device_find_first_child(udev->dev, &child);
Simon Glass6a1b2062015-03-25 12:22:02 -0600606 child;
607 device_find_next_child(&child)) {
Suneel Garapatiabd7ced2017-10-23 17:28:40 -0700608 if (device_active(child) &&
Xavier Drudis Ferran7a875a82023-06-21 09:13:07 +0200609 (device_get_uclass_id(child) != UCLASS_BOOTDEV) &&
Suneel Garapatiabd7ced2017-10-23 17:28:40 -0700610 (device_get_uclass_id(child) != UCLASS_USB_EMUL) &&
611 (device_get_uclass_id(child) != UCLASS_BLK)) {
Hans de Goedee6e188f2016-07-03 20:22:05 +0200612 udev = dev_get_parent_priv(child);
Xavier Drudis Ferran7a875a82023-06-21 09:13:07 +0200613 if (udev)
614 usb_show_info(udev);
Simon Glass6a1b2062015-03-25 12:22:02 -0600615 }
616 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600617}
618#endif
619
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100620/******************************************************************************
wdenke887afc2002-08-27 09:44:07 +0000621 * usb command intepreter
622 */
Simon Glass09140112020-05-10 11:40:03 -0600623static int do_usb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +0000624{
Simon Glasscad42912015-03-25 12:22:00 -0600625 struct usb_device *udev = NULL;
wdenke887afc2002-08-27 09:44:07 +0000626 int i;
wdenke887afc2002-08-27 09:44:07 +0000627
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200628 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000629 return CMD_RET_USAGE;
Serge Ziryukin65d34252010-04-25 21:32:36 +0300630
Hans de Goedeb5072262015-01-06 14:27:41 +0100631 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");
Hans de Goede8a8a2252014-09-20 16:54:38 +0200641 if (do_usb_stop_keyboard(1) != 0)
Hans de Goede6e78c742014-09-20 16:54:35 +0200642 return 1;
wdenke887afc2002-08-27 09:44:07 +0000643 usb_stop();
Hans de Goedeb5072262015-01-06 14:27:41 +0100644 do_usb_start();
wdenke887afc2002-08-27 09:44:07 +0000645 return 0;
646 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100647 if (strncmp(argv[1], "stop", 4) == 0) {
Hans de Goede6e78c742014-09-20 16:54:35 +0200648 if (argc != 2)
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100649 console_assign(stdin, "serial");
Hans de Goede8a8a2252014-09-20 16:54:38 +0200650 if (do_usb_stop_keyboard(0) != 0)
Hans de Goede6e78c742014-09-20 16:54:35 +0200651 return 1;
wdenke887afc2002-08-27 09:44:07 +0000652 printf("stopping USB..\n");
653 usb_stop();
654 return 0;
655 }
Bartlomiej Siekae51aae32006-08-03 23:20:13 +0200656 if (!usb_started) {
657 printf("USB is stopped. Please issue 'usb start' first.\n");
658 return 1;
659 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100660 if (strncmp(argv[1], "tree", 4) == 0) {
Lucas Stach93c25822012-09-26 00:14:36 +0200661 puts("USB device tree:\n");
Simon Glass45bfa472015-11-08 23:47:51 -0700662 usb_show_tree();
wdenke887afc2002-08-27 09:44:07 +0000663 return 0;
664 }
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100665 if (strncmp(argv[1], "inf", 3) == 0) {
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100666 if (argc == 2) {
Simon Glass6a1b2062015-03-25 12:22:02 -0600667#ifdef CONFIG_DM_USB
Hans de Goedee6e188f2016-07-03 20:22:05 +0200668 usb_for_each_root_dev(usb_show_info);
Simon Glass6a1b2062015-03-25 12:22:02 -0600669#else
670 int d;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100671 for (d = 0; d < USB_MAX_DEVICE; d++) {
Simon Glasscad42912015-03-25 12:22:00 -0600672 udev = usb_get_dev_index(d);
673 if (udev == NULL)
wdenke887afc2002-08-27 09:44:07 +0000674 break;
Simon Glasscad42912015-03-25 12:22:00 -0600675 usb_display_desc(udev);
676 usb_display_config(udev);
wdenke887afc2002-08-27 09:44:07 +0000677 }
Simon Glass6a1b2062015-03-25 12:22:02 -0600678#endif
wdenke887afc2002-08-27 09:44:07 +0000679 return 0;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100680 } else {
Simon Glass6a1b2062015-03-25 12:22:02 -0600681 /*
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 */
Simon Glass0b1284e2021-07-24 09:03:30 -0600686 i = dectoul(argv[2], NULL);
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100687 printf("config for device %d\n", i);
Simon Glasscad42912015-03-25 12:22:00 -0600688 udev = usb_find_device(i);
689 if (udev == NULL) {
Loïc Minier6052cba2011-02-03 22:04:26 +0100690 printf("*** No device available ***\n");
wdenke887afc2002-08-27 09:44:07 +0000691 return 0;
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100692 } else {
Simon Glasscad42912015-03-25 12:22:00 -0600693 usb_display_desc(udev);
694 usb_display_config(udev);
wdenke887afc2002-08-27 09:44:07 +0000695 }
696 }
697 return 0;
698 }
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000699 if (strncmp(argv[1], "test", 4) == 0) {
700 if (argc < 5)
701 return CMD_RET_USAGE;
Simon Glass0b1284e2021-07-24 09:03:30 -0600702 i = dectoul(argv[2], NULL);
Simon Glasscad42912015-03-25 12:22:00 -0600703 udev = usb_find_device(i);
704 if (udev == NULL) {
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000705 printf("Device %d does not exist.\n", i);
706 return 1;
707 }
Simon Glass0b1284e2021-07-24 09:03:30 -0600708 i = dectoul(argv[3], NULL);
Simon Glasscad42912015-03-25 12:22:00 -0600709 return usb_test(udev, i, argv[4]);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000710 }
wdenke887afc2002-08-27 09:44:07 +0000711#ifdef CONFIG_USB_STORAGE
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100712 if (strncmp(argv[1], "stor", 4) == 0)
Aras Vaichasf6b44e02008-03-25 12:09:07 +1100713 return usb_stor_info();
Wolfgang Denk9c998aa2005-07-21 11:57:57 +0200714
Simon Glasse33a5c62022-08-11 19:34:59 -0600715 return blk_common_cmd(argc, argv, UCLASS_USB, &usb_stor_curr_dev);
Simon Glassc16ad672017-07-29 11:34:58 -0600716#else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000717 return CMD_RET_USAGE;
Simon Glassc16ad672017-07-29 11:34:58 -0600718#endif /* CONFIG_USB_STORAGE */
wdenke887afc2002-08-27 09:44:07 +0000719}
720
wdenk0d498392003-07-01 21:06:45 +0000721U_BOOT_CMD(
722 usb, 5, 1, do_usb,
Peter Tyser2fb26042009-01-27 18:03:12 -0600723 "USB sub-system",
Veli-Pekka Peltola1af9f962011-11-02 16:59:56 +0200724 "start - start (scan) USB controller\n"
725 "usb reset - reset (rescan) USB controller\n"
Veli-Pekka Peltolab3813a22011-11-02 16:59:55 +0200726 "usb stop [f] - stop USB [f]=force stop\n"
727 "usb tree - show USB device tree\n"
wdenk5cf91d62004-04-23 20:32:05 +0000728 "usb info [dev] - show available USB devices\n"
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000729 "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
Veli-Pekka Peltolab3813a22011-11-02 16:59:55 +0200733 "usb storage - show details of USB storage devices\n"
wdenk342717f2005-06-27 13:30:03 +0000734 "usb dev [dev] - show or set current USB storage device\n"
Michael Trimarchide39f8c2008-11-26 17:41:34 +0100735 "usb part [dev] - print partition table of one or all USB storage"
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000736 " devices\n"
wdenk5cf91d62004-04-23 20:32:05 +0000737 "usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
Sergei Poselenov842404e2010-08-09 16:01:42 +0400738 " to memory address `addr'\n"
Mahavir Jain127e1082009-11-03 12:22:10 +0530739 "usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'\n"
740 " from memory address `addr'"
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000741#endif /* CONFIG_USB_STORAGE */
wdenk8bde7f72003-06-27 21:31:46 +0000742);
743
744
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000745#ifdef CONFIG_USB_STORAGE
wdenk0d498392003-07-01 21:06:45 +0000746U_BOOT_CMD(
747 usbboot, 3, 1, do_usbboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600748 "boot from USB device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200749 "loadAddr dev:part"
wdenk8bde7f72003-06-27 21:31:46 +0000750);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000751#endif /* CONFIG_USB_STORAGE */