Blame view

common/cmd_usb.c 19.9 KB
e887afc9d   wdenk   Initial revision
1
2
3
4
  /*
   * (C) Copyright 2001
   * Denis Peter, MPL AG Switzerland
   *
6a1b206dc   Simon Glass   dm: usb: Convert ...
5
6
7
   * Adapted for U-Boot driver model
   * (C) Copyright 2015 Google, Inc
   *
e887afc9d   wdenk   Initial revision
8
9
10
   * Most of this source has been derived from the Linux USB
   * project.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
11
   * SPDX-License-Identifier:	GPL-2.0+
e887afc9d   wdenk   Initial revision
12
13
14
15
   */
  
  #include <common.h>
  #include <command.h>
24b852a7a   Simon Glass   Move console defi...
16
  #include <console.h>
6a1b206dc   Simon Glass   dm: usb: Convert ...
17
  #include <dm.h>
cf92e05c0   Simon Glass   Move ALLOC_CACHE_...
18
  #include <memalign.h>
414eec35e   wdenk   Fix problems with...
19
  #include <asm/byteorder.h>
b2fb47f18   Tom Rini   USB: Use (get|put...
20
  #include <asm/unaligned.h>
735dd97b1   Grant Likely   [PATCH 1_4] Merge...
21
  #include <part.h>
e887afc9d   wdenk   Initial revision
22
  #include <usb.h>
e887afc9d   wdenk   Initial revision
23

1968e615d   wdenk   Cleanup USB and p...
24
  #ifdef CONFIG_USB_STORAGE
d0ff51ba5   Wolfgang Denk   Code cleanup: fix...
25
  static int usb_stor_curr_dev = -1; /* current device */
1968e615d   wdenk   Cleanup USB and p...
26
  #endif
c8c2797c3   Simon Glass   dm: usb: eth: Sup...
27
  #if defined(CONFIG_USB_HOST_ETHER) && !defined(CONFIG_DM_ETH)
69559093f   Simon Glass   dm: usb: Avoid us...
28
  static int __maybe_unused usb_ether_curr_dev = -1; /* current ethernet device */
89d48367e   Simon Glass   Add USB host ethe...
29
  #endif
e887afc9d   wdenk   Initial revision
30

a2663ea4f   wdenk   * Patches by Davi...
31
  /* some display routines (info command) */
088f1b199   Kim Phillips   common/cmd_*.c: s...
32
  static char *usb_get_class_desc(unsigned char dclass)
e887afc9d   wdenk   Initial revision
33
  {
de39f8c19   Michael Trimarchi   USB style patch, ...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  	switch (dclass) {
  	case USB_CLASS_PER_INTERFACE:
  		return "See Interface";
  	case USB_CLASS_AUDIO:
  		return "Audio";
  	case USB_CLASS_COMM:
  		return "Communication";
  	case USB_CLASS_HID:
  		return "Human Interface";
  	case USB_CLASS_PRINTER:
  		return "Printer";
  	case USB_CLASS_MASS_STORAGE:
  		return "Mass Storage";
  	case USB_CLASS_HUB:
  		return "Hub";
  	case USB_CLASS_DATA:
  		return "CDC Data";
  	case USB_CLASS_VENDOR_SPEC:
  		return "Vendor specific";
  	default:
  		return "";
e887afc9d   wdenk   Initial revision
55
56
  	}
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
57
58
  static void usb_display_class_sub(unsigned char dclass, unsigned char subclass,
  				  unsigned char proto)
e887afc9d   wdenk   Initial revision
59
  {
de39f8c19   Michael Trimarchi   USB style patch, ...
60
61
62
63
64
65
66
67
68
  	switch (dclass) {
  	case USB_CLASS_PER_INTERFACE:
  		printf("See Interface");
  		break;
  	case USB_CLASS_HID:
  		printf("Human Interface, Subclass: ");
  		switch (subclass) {
  		case USB_SUB_HID_NONE:
  			printf("None");
e887afc9d   wdenk   Initial revision
69
  			break;
de39f8c19   Michael Trimarchi   USB style patch, ...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  		case USB_SUB_HID_BOOT:
  			printf("Boot ");
  			switch (proto) {
  			case USB_PROT_HID_NONE:
  				printf("None");
  				break;
  			case USB_PROT_HID_KEYBOARD:
  				printf("Keyboard");
  				break;
  			case USB_PROT_HID_MOUSE:
  				printf("Mouse");
  				break;
  			default:
  				printf("reserved");
  				break;
e887afc9d   wdenk   Initial revision
85
86
  			}
  			break;
de39f8c19   Michael Trimarchi   USB style patch, ...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  		default:
  			printf("reserved");
  			break;
  		}
  		break;
  	case USB_CLASS_MASS_STORAGE:
  		printf("Mass Storage, ");
  		switch (subclass) {
  		case US_SC_RBC:
  			printf("RBC ");
  			break;
  		case US_SC_8020:
  			printf("SFF-8020i (ATAPI)");
  			break;
  		case US_SC_QIC:
  			printf("QIC-157 (Tape)");
  			break;
  		case US_SC_UFI:
  			printf("UFI");
  			break;
  		case US_SC_8070:
  			printf("SFF-8070");
  			break;
  		case US_SC_SCSI:
  			printf("Transp. SCSI");
  			break;
  		default:
  			printf("reserved");
  			break;
  		}
  		printf(", ");
  		switch (proto) {
  		case US_PR_CB:
  			printf("Command/Bulk");
  			break;
  		case US_PR_CBI:
  			printf("Command/Bulk/Int");
  			break;
  		case US_PR_BULK:
  			printf("Bulk only");
e887afc9d   wdenk   Initial revision
127
128
  			break;
  		default:
de39f8c19   Michael Trimarchi   USB style patch, ...
129
130
131
132
133
134
135
  			printf("reserved");
  			break;
  		}
  		break;
  	default:
  		printf("%s", usb_get_class_desc(dclass));
  		break;
e887afc9d   wdenk   Initial revision
136
137
  	}
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
138
  static void usb_display_string(struct usb_device *dev, int index)
e887afc9d   wdenk   Initial revision
139
  {
f57661394   Puneet Saxena   USB: Align buffer...
140
  	ALLOC_CACHE_ALIGN_BUFFER(char, buffer, 256);
de39f8c19   Michael Trimarchi   USB style patch, ...
141
142
143
  	if (index != 0) {
  		if (usb_string(dev, index, &buffer[0], 256) > 0)
  			printf("String: \"%s\"", buffer);
e887afc9d   wdenk   Initial revision
144
145
  	}
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
146
  static void usb_display_desc(struct usb_device *dev)
e887afc9d   wdenk   Initial revision
147
  {
de39f8c19   Michael Trimarchi   USB style patch, ...
148
149
150
  	if (dev->descriptor.bDescriptorType == USB_DT_DEVICE) {
  		printf("%d: %s,  USB Revision %x.%x
  ", dev->devnum,
8f8bd565f   Tom Rix   USB Consolidate d...
151
  		usb_get_class_desc(dev->config.if_desc[0].desc.bInterfaceClass),
de39f8c19   Michael Trimarchi   USB style patch, ...
152
153
154
155
156
157
158
159
  				   (dev->descriptor.bcdUSB>>8) & 0xff,
  				   dev->descriptor.bcdUSB & 0xff);
  
  		if (strlen(dev->mf) || strlen(dev->prod) ||
  		    strlen(dev->serial))
  			printf(" - %s %s %s
  ", dev->mf, dev->prod,
  				dev->serial);
e887afc9d   wdenk   Initial revision
160
161
  		if (dev->descriptor.bDeviceClass) {
  			printf(" - Class: ");
de39f8c19   Michael Trimarchi   USB style patch, ...
162
163
164
  			usb_display_class_sub(dev->descriptor.bDeviceClass,
  					      dev->descriptor.bDeviceSubClass,
  					      dev->descriptor.bDeviceProtocol);
e887afc9d   wdenk   Initial revision
165
166
  			printf("
  ");
de39f8c19   Michael Trimarchi   USB style patch, ...
167
168
169
170
  		} else {
  			printf(" - Class: (from Interface) %s
  ",
  			       usb_get_class_desc(
8f8bd565f   Tom Rix   USB Consolidate d...
171
  				dev->config.if_desc[0].desc.bInterfaceClass));
e887afc9d   wdenk   Initial revision
172
  		}
de39f8c19   Michael Trimarchi   USB style patch, ...
173
174
175
176
177
178
179
180
181
  		printf(" - PacketSize: %d  Configurations: %d
  ",
  			dev->descriptor.bMaxPacketSize0,
  			dev->descriptor.bNumConfigurations);
  		printf(" - Vendor: 0x%04x  Product 0x%04x Version %d.%d
  ",
  			dev->descriptor.idVendor, dev->descriptor.idProduct,
  			(dev->descriptor.bcdDevice>>8) & 0xff,
  			dev->descriptor.bcdDevice & 0xff);
e887afc9d   wdenk   Initial revision
182
183
184
  	}
  
  }
c60795f41   Ilya Yanok   usb: use linux/us...
185
  static void usb_display_conf_desc(struct usb_config_descriptor *config,
088f1b199   Kim Phillips   common/cmd_*.c: s...
186
  				  struct usb_device *dev)
e887afc9d   wdenk   Initial revision
187
  {
de39f8c19   Michael Trimarchi   USB style patch, ...
188
189
190
191
192
193
  	printf("   Configuration: %d
  ", config->bConfigurationValue);
  	printf("   - Interfaces: %d %s%s%dmA
  ", config->bNumInterfaces,
  	       (config->bmAttributes & 0x40) ? "Self Powered " : "Bus Powered ",
  	       (config->bmAttributes & 0x20) ? "Remote Wakeup " : "",
8f8bd565f   Tom Rix   USB Consolidate d...
194
  		config->bMaxPower*2);
e887afc9d   wdenk   Initial revision
195
196
  	if (config->iConfiguration) {
  		printf("   - ");
de39f8c19   Michael Trimarchi   USB style patch, ...
197
  		usb_display_string(dev, config->iConfiguration);
e887afc9d   wdenk   Initial revision
198
199
200
201
  		printf("
  ");
  	}
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
202
203
  static void usb_display_if_desc(struct usb_interface_descriptor *ifdesc,
  				struct usb_device *dev)
e887afc9d   wdenk   Initial revision
204
  {
de39f8c19   Michael Trimarchi   USB style patch, ...
205
206
207
208
209
  	printf("     Interface: %d
  ", ifdesc->bInterfaceNumber);
  	printf("     - Alternate Setting %d, Endpoints: %d
  ",
  		ifdesc->bAlternateSetting, ifdesc->bNumEndpoints);
e887afc9d   wdenk   Initial revision
210
  	printf("     - Class ");
de39f8c19   Michael Trimarchi   USB style patch, ...
211
212
  	usb_display_class_sub(ifdesc->bInterfaceClass,
  		ifdesc->bInterfaceSubClass, ifdesc->bInterfaceProtocol);
e887afc9d   wdenk   Initial revision
213
214
215
216
  	printf("
  ");
  	if (ifdesc->iInterface) {
  		printf("     - ");
de39f8c19   Michael Trimarchi   USB style patch, ...
217
  		usb_display_string(dev, ifdesc->iInterface);
e887afc9d   wdenk   Initial revision
218
219
220
221
  		printf("
  ");
  	}
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
222
  static void usb_display_ep_desc(struct usb_endpoint_descriptor *epdesc)
e887afc9d   wdenk   Initial revision
223
  {
de39f8c19   Michael Trimarchi   USB style patch, ...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  	printf("     - Endpoint %d %s ", epdesc->bEndpointAddress & 0xf,
  		(epdesc->bEndpointAddress & 0x80) ? "In" : "Out");
  	switch ((epdesc->bmAttributes & 0x03)) {
  	case 0:
  		printf("Control");
  		break;
  	case 1:
  		printf("Isochronous");
  		break;
  	case 2:
  		printf("Bulk");
  		break;
  	case 3:
  		printf("Interrupt");
  		break;
e887afc9d   wdenk   Initial revision
239
  	}
b2fb47f18   Tom Rini   USB: Use (get|put...
240
  	printf(" MaxPacket %d", get_unaligned(&epdesc->wMaxPacketSize));
de39f8c19   Michael Trimarchi   USB style patch, ...
241
242
  	if ((epdesc->bmAttributes & 0x03) == 0x3)
  		printf(" Interval %dms", epdesc->bInterval);
e887afc9d   wdenk   Initial revision
243
244
245
246
247
  	printf("
  ");
  }
  
  /* main routine to diasplay the configs, interfaces and endpoints */
088f1b199   Kim Phillips   common/cmd_*.c: s...
248
  static void usb_display_config(struct usb_device *dev)
e887afc9d   wdenk   Initial revision
249
  {
8f8bd565f   Tom Rix   USB Consolidate d...
250
251
  	struct usb_config *config;
  	struct usb_interface *ifdesc;
e887afc9d   wdenk   Initial revision
252
  	struct usb_endpoint_descriptor *epdesc;
de39f8c19   Michael Trimarchi   USB style patch, ...
253
254
255
  	int i, ii;
  
  	config = &dev->config;
8f8bd565f   Tom Rix   USB Consolidate d...
256
  	usb_display_conf_desc(&config->desc, dev);
de39f8c19   Michael Trimarchi   USB style patch, ...
257
258
  	for (i = 0; i < config->no_of_if; i++) {
  		ifdesc = &config->if_desc[i];
8f8bd565f   Tom Rix   USB Consolidate d...
259
  		usb_display_if_desc(&ifdesc->desc, dev);
de39f8c19   Michael Trimarchi   USB style patch, ...
260
261
  		for (ii = 0; ii < ifdesc->no_of_ep; ii++) {
  			epdesc = &ifdesc->ep_desc[ii];
e887afc9d   wdenk   Initial revision
262
263
264
265
266
267
  			usb_display_ep_desc(epdesc);
  		}
  	}
  	printf("
  ");
  }
6a1b206dc   Simon Glass   dm: usb: Convert ...
268
269
270
271
272
  /*
   * With driver model this isn't right since we can have multiple controllers
   * and the device numbering starts at 1 on each bus.
   * TODO(sjg@chromium.org): Add a way to specify the controller/bus.
   */
7d9aa8fd8   Julius Werner   usb: Add new comm...
273
274
  static struct usb_device *usb_find_device(int devnum)
  {
6a1b206dc   Simon Glass   dm: usb: Convert ...
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  #ifdef CONFIG_DM_USB
  	struct usb_device *udev;
  	struct udevice *hub;
  	struct uclass *uc;
  	int ret;
  
  	/* Device addresses start at 1 */
  	devnum++;
  	ret = uclass_get(UCLASS_USB_HUB, &uc);
  	if (ret)
  		return NULL;
  
  	uclass_foreach_dev(hub, uc) {
  		struct udevice *dev;
  
  		if (!device_active(hub))
  			continue;
bcbe3d157   Simon Glass   dm: Rename dev_ge...
292
  		udev = dev_get_parent_priv(hub);
6a1b206dc   Simon Glass   dm: usb: Convert ...
293
294
295
296
297
298
299
300
  		if (udev->devnum == devnum)
  			return udev;
  
  		for (device_find_first_child(hub, &dev);
  		     dev;
  		     device_find_next_child(&dev)) {
  			if (!device_active(hub))
  				continue;
bcbe3d157   Simon Glass   dm: Rename dev_ge...
301
  			udev = dev_get_parent_priv(dev);
6a1b206dc   Simon Glass   dm: usb: Convert ...
302
303
304
305
306
  			if (udev->devnum == devnum)
  				return udev;
  		}
  	}
  #else
cad4291cd   Simon Glass   dm: usb: Adjust u...
307
  	struct usb_device *udev;
7d9aa8fd8   Julius Werner   usb: Add new comm...
308
309
310
  	int d;
  
  	for (d = 0; d < USB_MAX_DEVICE; d++) {
cad4291cd   Simon Glass   dm: usb: Adjust u...
311
312
  		udev = usb_get_dev_index(d);
  		if (udev == NULL)
7d9aa8fd8   Julius Werner   usb: Add new comm...
313
  			return NULL;
cad4291cd   Simon Glass   dm: usb: Adjust u...
314
315
  		if (udev->devnum == devnum)
  			return udev;
7d9aa8fd8   Julius Werner   usb: Add new comm...
316
  	}
6a1b206dc   Simon Glass   dm: usb: Convert ...
317
  #endif
7d9aa8fd8   Julius Werner   usb: Add new comm...
318
319
320
  
  	return NULL;
  }
f1c1f5402   Stefan Roese   USB: Add high-spe...
321
322
  static inline char *portspeed(int speed)
  {
6497c6670   Vivek Gautam   USB: SS: Add supp...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  	char *speed_str;
  
  	switch (speed) {
  	case USB_SPEED_SUPER:
  		speed_str = "5 Gb/s";
  		break;
  	case USB_SPEED_HIGH:
  		speed_str = "480 Mb/s";
  		break;
  	case USB_SPEED_LOW:
  		speed_str = "1.5 Mb/s";
  		break;
  	default:
  		speed_str = "12 Mb/s";
  		break;
  	}
  
  	return speed_str;
f1c1f5402   Stefan Roese   USB: Add high-spe...
341
  }
e887afc9d   wdenk   Initial revision
342
  /* shows the device tree recursively */
088f1b199   Kim Phillips   common/cmd_*.c: s...
343
  static void usb_show_tree_graph(struct usb_device *dev, char *pre)
e887afc9d   wdenk   Initial revision
344
  {
6a1b206dc   Simon Glass   dm: usb: Convert ...
345
  	int index;
10f4dd784   Wolfgang Denk   common/cmd_usb.c:...
346
  	int has_child, last_child;
e887afc9d   wdenk   Initial revision
347

de39f8c19   Michael Trimarchi   USB style patch, ...
348
349
  	index = strlen(pre);
  	printf(" %s", pre);
6a1b206dc   Simon Glass   dm: usb: Convert ...
350
351
352
  #ifdef CONFIG_DM_USB
  	has_child = device_has_active_children(dev->dev);
  #else
e887afc9d   wdenk   Initial revision
353
  	/* check if the device has connected children */
6a1b206dc   Simon Glass   dm: usb: Convert ...
354
  	int i;
de39f8c19   Michael Trimarchi   USB style patch, ...
355
356
357
358
  	has_child = 0;
  	for (i = 0; i < dev->maxchild; i++) {
  		if (dev->children[i] != NULL)
  			has_child = 1;
e887afc9d   wdenk   Initial revision
359
  	}
6a1b206dc   Simon Glass   dm: usb: Convert ...
360
  #endif
e887afc9d   wdenk   Initial revision
361
  	/* check if we are the last one */
6a1b206dc   Simon Glass   dm: usb: Convert ...
362
  #ifdef CONFIG_DM_USB
c27b32905   Hans de Goede   dm: usb: Fix "usb...
363
364
365
  	/* Not the root of the usb tree? */
  	if (device_get_uclass_id(dev->dev->parent) != UCLASS_USB) {
  		last_child = device_is_last_sibling(dev->dev);
6a1b206dc   Simon Glass   dm: usb: Convert ...
366
  #else
c27b32905   Hans de Goede   dm: usb: Fix "usb...
367
368
  	if (dev->parent != NULL) { /* not root? */
  		last_child = 1;
de39f8c19   Michael Trimarchi   USB style patch, ...
369
  		for (i = 0; i < dev->parent->maxchild; i++) {
e887afc9d   wdenk   Initial revision
370
  			/* search for children */
de39f8c19   Michael Trimarchi   USB style patch, ...
371
372
373
374
  			if (dev->parent->children[i] == dev) {
  				/* found our pointer, see if we have a
  				 * little sister
  				 */
de39f8c19   Michael Trimarchi   USB style patch, ...
375
376
  				while (i++ < dev->parent->maxchild) {
  					if (dev->parent->children[i] != NULL) {
e887afc9d   wdenk   Initial revision
377
  						/* found a sister */
de39f8c19   Michael Trimarchi   USB style patch, ...
378
  						last_child = 0;
e887afc9d   wdenk   Initial revision
379
380
381
382
383
  						break;
  					} /* if */
  				} /* while */
  			} /* device found */
  		} /* for all children of the parent */
6a1b206dc   Simon Glass   dm: usb: Convert ...
384
  #endif
e887afc9d   wdenk   Initial revision
385
386
  		printf("\b+-");
  		/* correct last child */
cad4291cd   Simon Glass   dm: usb: Adjust u...
387
  		if (last_child && index)
de39f8c19   Michael Trimarchi   USB style patch, ...
388
  			pre[index-1] = ' ';
e887afc9d   wdenk   Initial revision
389
390
391
  	} /* if not root hub */
  	else
  		printf(" ");
de39f8c19   Michael Trimarchi   USB style patch, ...
392
393
394
395
396
397
  	printf("%d ", dev->devnum);
  	pre[index++] = ' ';
  	pre[index++] = has_child ? '|' : ' ';
  	pre[index] = 0;
  	printf(" %s (%s, %dmA)
  ", usb_get_class_desc(
8f8bd565f   Tom Rix   USB Consolidate d...
398
  					dev->config.if_desc[0].desc.bInterfaceClass),
f1c1f5402   Stefan Roese   USB: Add high-spe...
399
  					portspeed(dev->speed),
8f8bd565f   Tom Rix   USB Consolidate d...
400
  					dev->config.desc.bMaxPower * 2);
de39f8c19   Michael Trimarchi   USB style patch, ...
401
402
403
404
405
  	if (strlen(dev->mf) || strlen(dev->prod) || strlen(dev->serial))
  		printf(" %s  %s %s %s
  ", pre, dev->mf, dev->prod, dev->serial);
  	printf(" %s
  ", pre);
6a1b206dc   Simon Glass   dm: usb: Convert ...
406
407
408
409
410
411
412
413
414
415
  #ifdef CONFIG_DM_USB
  	struct udevice *child;
  
  	for (device_find_first_child(dev->dev, &child);
  	     child;
  	     device_find_next_child(&child)) {
  		struct usb_device *udev;
  
  		if (!device_active(child))
  			continue;
bcbe3d157   Simon Glass   dm: Rename dev_ge...
416
  		udev = dev_get_parent_priv(child);
6a1b206dc   Simon Glass   dm: usb: Convert ...
417
418
419
420
421
422
423
424
  
  		/* Ignore emulators, we only want real devices */
  		if (device_get_uclass_id(child) != UCLASS_USB_EMUL) {
  			usb_show_tree_graph(udev, pre);
  			pre[index] = 0;
  		}
  	}
  #else
de39f8c19   Michael Trimarchi   USB style patch, ...
425
426
427
428
429
  	if (dev->maxchild > 0) {
  		for (i = 0; i < dev->maxchild; i++) {
  			if (dev->children[i] != NULL) {
  				usb_show_tree_graph(dev->children[i], pre);
  				pre[index] = 0;
e887afc9d   wdenk   Initial revision
430
431
432
  			}
  		}
  	}
6a1b206dc   Simon Glass   dm: usb: Convert ...
433
  #endif
e887afc9d   wdenk   Initial revision
434
435
436
  }
  
  /* main routine for the tree command */
45bfa47e1   Simon Glass   usb: Refactor USB...
437
  static void usb_show_subtree(struct usb_device *dev)
e887afc9d   wdenk   Initial revision
438
439
  {
  	char preamble[32];
cad4291cd   Simon Glass   dm: usb: Adjust u...
440
  	memset(preamble, '\0', sizeof(preamble));
de39f8c19   Michael Trimarchi   USB style patch, ...
441
  	usb_show_tree_graph(dev, &preamble[0]);
e887afc9d   wdenk   Initial revision
442
  }
45bfa47e1   Simon Glass   usb: Refactor USB...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
  void usb_show_tree(void)
  {
  #ifdef CONFIG_DM_USB
  	struct udevice *bus;
  
  	for (uclass_first_device(UCLASS_USB, &bus);
  		bus;
  		uclass_next_device(&bus)) {
  		struct usb_device *udev;
  		struct udevice *dev;
  
  		device_find_first_child(bus, &dev);
  		if (dev && device_active(dev)) {
  			udev = dev_get_parent_priv(dev);
  			usb_show_subtree(udev);
  		}
  	}
  #else
  	struct usb_device *udev;
  	int i;
  
  	for (i = 0; i < USB_MAX_DEVICE; i++) {
  		udev = usb_get_dev_index(i);
  		if (udev == NULL)
  			break;
  		if (udev->parent == NULL)
  			usb_show_subtree(udev);
  	}
  #endif
  }
7d9aa8fd8   Julius Werner   usb: Add new comm...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  static int usb_test(struct usb_device *dev, int port, char* arg)
  {
  	int mode;
  
  	if (port > dev->maxchild) {
  		printf("Device is no hub or does not have %d ports.
  ", port);
  		return 1;
  	}
  
  	switch (arg[0]) {
  	case 'J':
  	case 'j':
  		printf("Setting Test_J mode");
  		mode = USB_TEST_MODE_J;
  		break;
  	case 'K':
  	case 'k':
  		printf("Setting Test_K mode");
  		mode = USB_TEST_MODE_K;
  		break;
  	case 'S':
  	case 's':
  		printf("Setting Test_SE0_NAK mode");
  		mode = USB_TEST_MODE_SE0_NAK;
  		break;
  	case 'P':
  	case 'p':
  		printf("Setting Test_Packet mode");
  		mode = USB_TEST_MODE_PACKET;
  		break;
  	case 'F':
  	case 'f':
  		printf("Setting Test_Force_Enable mode");
  		mode = USB_TEST_MODE_FORCE_ENABLE;
  		break;
  	default:
  		printf("Unrecognized test mode: %s
  Available modes: "
  		       "J, K, S[E0_NAK], P[acket], F[orce_Enable]
  ", arg);
  		return 1;
  	}
  
  	if (port)
  		printf(" on downstream facing port %d...
  ", port);
  	else
  		printf(" on upstream facing port...
  ");
  
  	if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_FEATURE,
  			    port ? USB_RT_PORT : USB_RECIP_DEVICE,
  			    port ? USB_PORT_FEAT_TEST : USB_FEAT_TEST,
  			    (mode << 8) | port,
  			    NULL, 0, USB_CNTL_TIMEOUT) == -1) {
  		printf("Error during SET_FEATURE.
  ");
  		return 1;
  	} else {
  		printf("Test mode successfully set. Use 'usb start' "
  		       "to return to normal operation.
  ");
  		return 0;
  	}
  }
e887afc9d   wdenk   Initial revision
539

e887afc9d   wdenk   Initial revision
540
541
542
543
  /******************************************************************************
   * usb boot command intepreter. Derived from diskboot
   */
  #ifdef CONFIG_USB_STORAGE
088f1b199   Kim Phillips   common/cmd_*.c: s...
544
  static int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
e887afc9d   wdenk   Initial revision
545
  {
7405a1331   Rob Herring   combine block dev...
546
  	return common_diskboot(cmdtp, "usb", argc, argv);
e887afc9d   wdenk   Initial revision
547
548
  }
  #endif /* CONFIG_USB_STORAGE */
8a8a2257e   Hans de Goede   usb: kbd: Allow "...
549
  static int do_usb_stop_keyboard(int force)
6e78c74f6   Hans de Goede   usb: kbd: On a "u...
550
551
  {
  #ifdef CONFIG_USB_KEYBOARD
8a8a2257e   Hans de Goede   usb: kbd: Allow "...
552
  	if (usb_kbd_deregister(force) != 0) {
6e78c74f6   Hans de Goede   usb: kbd: On a "u...
553
554
555
556
557
558
559
  		printf("USB not stopped: usbkbd still using USB
  ");
  		return 1;
  	}
  #endif
  	return 0;
  }
e887afc9d   wdenk   Initial revision
560

b50722640   Hans de Goede   USB: make "usb st...
561
562
563
564
565
566
  static void do_usb_start(void)
  {
  	bootstage_mark_name(BOOTSTAGE_ID_USB_START, "usb_start");
  
  	if (usb_init() < 0)
  		return;
6a1b206dc   Simon Glass   dm: usb: Convert ...
567
568
  	/* Driver model will probe the devices as they are found */
  #ifndef CONFIG_DM_USB
34ab37eef   Simon Glass   dm: usb: Add supp...
569
  # ifdef CONFIG_USB_STORAGE
b50722640   Hans de Goede   USB: make "usb st...
570
571
  	/* try to recognize storage devices immediately */
  	usb_stor_curr_dev = usb_stor_scan(1);
34ab37eef   Simon Glass   dm: usb: Add supp...
572
573
574
575
576
  # endif
  # ifdef CONFIG_USB_KEYBOARD
  	drv_usb_kbd_init();
  # endif
  #endif /* !CONFIG_DM_USB */
b50722640   Hans de Goede   USB: make "usb st...
577
  #ifdef CONFIG_USB_HOST_ETHER
69559093f   Simon Glass   dm: usb: Avoid us...
578
  # ifdef CONFIG_DM_ETH
389f1856b   Marcel Ziswiler   dm: usb: fix USB ...
579
580
581
582
  #  ifndef CONFIG_DM_USB
  #   error "You must use CONFIG_DM_USB if you want to use CONFIG_USB_HOST_ETHER with CONFIG_DM_ETH"
  #  endif
  # else
b50722640   Hans de Goede   USB: make "usb st...
583
584
  	/* try to recognize ethernet devices immediately */
  	usb_ether_curr_dev = usb_host_eth_scan(1);
389f1856b   Marcel Ziswiler   dm: usb: fix USB ...
585
  # endif
69559093f   Simon Glass   dm: usb: Avoid us...
586
  #endif
b50722640   Hans de Goede   USB: make "usb st...
587
  }
6a1b206dc   Simon Glass   dm: usb: Convert ...
588
589
590
591
592
  #ifdef CONFIG_DM_USB
  static void show_info(struct udevice *dev)
  {
  	struct udevice *child;
  	struct usb_device *udev;
bcbe3d157   Simon Glass   dm: Rename dev_ge...
593
  	udev = dev_get_parent_priv(dev);
6a1b206dc   Simon Glass   dm: usb: Convert ...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  	usb_display_desc(udev);
  	usb_display_config(udev);
  	for (device_find_first_child(dev, &child);
  	     child;
  	     device_find_next_child(&child)) {
  		if (device_active(child))
  			show_info(child);
  	}
  }
  
  static int usb_device_info(void)
  {
  	struct udevice *bus;
  
  	for (uclass_first_device(UCLASS_USB, &bus);
  	     bus;
  	     uclass_next_device(&bus)) {
  		struct udevice *hub;
  
  		device_find_first_child(bus, &hub);
  		if (device_get_uclass_id(hub) == UCLASS_USB_HUB &&
  		    device_active(hub)) {
  			show_info(hub);
  		}
  	}
  
  	return 0;
  }
  #endif
de39f8c19   Michael Trimarchi   USB style patch, ...
623
  /******************************************************************************
e887afc9d   wdenk   Initial revision
624
625
   * usb command intepreter
   */
088f1b199   Kim Phillips   common/cmd_*.c: s...
626
  static int do_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
e887afc9d   wdenk   Initial revision
627
  {
cad4291cd   Simon Glass   dm: usb: Adjust u...
628
  	struct usb_device *udev = NULL;
e887afc9d   wdenk   Initial revision
629
  	int i;
e51aae382   Bartlomiej Sieka   Prevent USB comma...
630
  	extern char usb_started;
1968e615d   wdenk   Cleanup USB and p...
631
  #ifdef CONFIG_USB_STORAGE
e887afc9d   wdenk   Initial revision
632
  	block_dev_desc_t *stor_dev;
1968e615d   wdenk   Cleanup USB and p...
633
  #endif
e887afc9d   wdenk   Initial revision
634

47e26b1bf   Wolfgang Denk   cmd_usage(): simp...
635
  	if (argc < 2)
4c12eeb8b   Simon Glass   Convert cmd_usage...
636
  		return CMD_RET_USAGE;
65d342541   Serge Ziryukin   cmd_usb.c: show c...
637

b50722640   Hans de Goede   USB: make "usb st...
638
639
640
641
642
643
644
645
646
647
648
649
  	if (strncmp(argv[1], "start", 5) == 0) {
  		if (usb_started)
  			return 0; /* Already started */
  		printf("starting USB...
  ");
  		do_usb_start();
  		return 0;
  	}
  
  	if (strncmp(argv[1], "reset", 5) == 0) {
  		printf("resetting USB...
  ");
8a8a2257e   Hans de Goede   usb: kbd: Allow "...
650
  		if (do_usb_stop_keyboard(1) != 0)
6e78c74f6   Hans de Goede   usb: kbd: On a "u...
651
  			return 1;
e887afc9d   wdenk   Initial revision
652
  		usb_stop();
b50722640   Hans de Goede   USB: make "usb st...
653
  		do_usb_start();
e887afc9d   wdenk   Initial revision
654
655
  		return 0;
  	}
de39f8c19   Michael Trimarchi   USB style patch, ...
656
  	if (strncmp(argv[1], "stop", 4) == 0) {
6e78c74f6   Hans de Goede   usb: kbd: On a "u...
657
  		if (argc != 2)
de39f8c19   Michael Trimarchi   USB style patch, ...
658
  			console_assign(stdin, "serial");
8a8a2257e   Hans de Goede   usb: kbd: Allow "...
659
  		if (do_usb_stop_keyboard(0) != 0)
6e78c74f6   Hans de Goede   usb: kbd: On a "u...
660
  			return 1;
e887afc9d   wdenk   Initial revision
661
662
663
664
665
  		printf("stopping USB..
  ");
  		usb_stop();
  		return 0;
  	}
e51aae382   Bartlomiej Sieka   Prevent USB comma...
666
667
668
669
670
  	if (!usb_started) {
  		printf("USB is stopped. Please issue 'usb start' first.
  ");
  		return 1;
  	}
de39f8c19   Michael Trimarchi   USB style patch, ...
671
  	if (strncmp(argv[1], "tree", 4) == 0) {
93c2582fe   Lucas Stach   usb: add support ...
672
673
  		puts("USB device tree:
  ");
45bfa47e1   Simon Glass   usb: Refactor USB...
674
  		usb_show_tree();
e887afc9d   wdenk   Initial revision
675
676
  		return 0;
  	}
de39f8c19   Michael Trimarchi   USB style patch, ...
677
  	if (strncmp(argv[1], "inf", 3) == 0) {
de39f8c19   Michael Trimarchi   USB style patch, ...
678
  		if (argc == 2) {
6a1b206dc   Simon Glass   dm: usb: Convert ...
679
680
681
682
  #ifdef CONFIG_DM_USB
  			usb_device_info();
  #else
  			int d;
de39f8c19   Michael Trimarchi   USB style patch, ...
683
  			for (d = 0; d < USB_MAX_DEVICE; d++) {
cad4291cd   Simon Glass   dm: usb: Adjust u...
684
685
  				udev = usb_get_dev_index(d);
  				if (udev == NULL)
e887afc9d   wdenk   Initial revision
686
  					break;
cad4291cd   Simon Glass   dm: usb: Adjust u...
687
688
  				usb_display_desc(udev);
  				usb_display_config(udev);
e887afc9d   wdenk   Initial revision
689
  			}
6a1b206dc   Simon Glass   dm: usb: Convert ...
690
  #endif
e887afc9d   wdenk   Initial revision
691
  			return 0;
de39f8c19   Michael Trimarchi   USB style patch, ...
692
  		} else {
6a1b206dc   Simon Glass   dm: usb: Convert ...
693
694
695
696
697
  			/*
  			 * With driver model this isn't right since we can
  			 * have multiple controllers and the device numbering
  			 * starts at 1 on each bus.
  			 */
7d9aa8fd8   Julius Werner   usb: Add new comm...
698
  			i = simple_strtoul(argv[2], NULL, 10);
de39f8c19   Michael Trimarchi   USB style patch, ...
699
700
  			printf("config for device %d
  ", i);
cad4291cd   Simon Glass   dm: usb: Adjust u...
701
702
  			udev = usb_find_device(i);
  			if (udev == NULL) {
6052cbab4   Loïc Minier   Fix misc spelling...
703
704
  				printf("*** No device available ***
  ");
e887afc9d   wdenk   Initial revision
705
  				return 0;
de39f8c19   Michael Trimarchi   USB style patch, ...
706
  			} else {
cad4291cd   Simon Glass   dm: usb: Adjust u...
707
708
  				usb_display_desc(udev);
  				usb_display_config(udev);
e887afc9d   wdenk   Initial revision
709
710
711
712
  			}
  		}
  		return 0;
  	}
7d9aa8fd8   Julius Werner   usb: Add new comm...
713
714
715
716
  	if (strncmp(argv[1], "test", 4) == 0) {
  		if (argc < 5)
  			return CMD_RET_USAGE;
  		i = simple_strtoul(argv[2], NULL, 10);
cad4291cd   Simon Glass   dm: usb: Adjust u...
717
718
  		udev = usb_find_device(i);
  		if (udev == NULL) {
7d9aa8fd8   Julius Werner   usb: Add new comm...
719
720
721
722
723
  			printf("Device %d does not exist.
  ", i);
  			return 1;
  		}
  		i = simple_strtoul(argv[3], NULL, 10);
cad4291cd   Simon Glass   dm: usb: Adjust u...
724
  		return usb_test(udev, i, argv[4]);
7d9aa8fd8   Julius Werner   usb: Add new comm...
725
  	}
e887afc9d   wdenk   Initial revision
726
  #ifdef CONFIG_USB_STORAGE
de39f8c19   Michael Trimarchi   USB style patch, ...
727
  	if (strncmp(argv[1], "stor", 4) == 0)
f6b44e0e4   Aras Vaichas   USB Storage, add ...
728
  		return usb_stor_info();
9c998aa83   Wolfgang Denk   Fix low-level OHC...
729

de39f8c19   Michael Trimarchi   USB style patch, ...
730
  	if (strncmp(argv[1], "part", 4) == 0) {
d4b5f3fa0   Christian Eggers   Fix "usb part" co...
731
  		int devno, ok = 0;
de39f8c19   Michael Trimarchi   USB style patch, ...
732
  		if (argc == 2) {
aaad108b8   Kim B. Heino   USB storage count
733
  			for (devno = 0; ; ++devno) {
de39f8c19   Michael Trimarchi   USB style patch, ...
734
  				stor_dev = usb_stor_get_dev(devno);
aaad108b8   Kim B. Heino   USB storage count
735
736
  				if (stor_dev == NULL)
  					break;
de39f8c19   Michael Trimarchi   USB style patch, ...
737
  				if (stor_dev->type != DEV_TYPE_UNKNOWN) {
d4b5f3fa0   Christian Eggers   Fix "usb part" co...
738
739
740
741
  					ok++;
  					if (devno)
  						printf("
  ");
060f28532   Wolfgang Denk   cmd_usb.c: print ...
742
743
  					debug("print_part of %x
  ", devno);
d4b5f3fa0   Christian Eggers   Fix "usb part" co...
744
745
746
  					print_part(stor_dev);
  				}
  			}
de39f8c19   Michael Trimarchi   USB style patch, ...
747
748
749
  		} else {
  			devno = simple_strtoul(argv[2], NULL, 16);
  			stor_dev = usb_stor_get_dev(devno);
aaad108b8   Kim B. Heino   USB storage count
750
751
  			if (stor_dev != NULL &&
  			    stor_dev->type != DEV_TYPE_UNKNOWN) {
e887afc9d   wdenk   Initial revision
752
  				ok++;
060f28532   Wolfgang Denk   cmd_usb.c: print ...
753
754
  				debug("print_part of %x
  ", devno);
e887afc9d   wdenk   Initial revision
755
756
757
758
759
760
761
762
763
764
765
  				print_part(stor_dev);
  			}
  		}
  		if (!ok) {
  			printf("
  no USB devices available
  ");
  			return 1;
  		}
  		return 0;
  	}
de39f8c19   Michael Trimarchi   USB style patch, ...
766
767
  	if (strcmp(argv[1], "read") == 0) {
  		if (usb_stor_curr_dev < 0) {
e887afc9d   wdenk   Initial revision
768
769
770
771
  			printf("no current device selected
  ");
  			return 1;
  		}
de39f8c19   Michael Trimarchi   USB style patch, ...
772
  		if (argc == 5) {
e887afc9d   wdenk   Initial revision
773
774
775
776
  			unsigned long addr = simple_strtoul(argv[2], NULL, 16);
  			unsigned long blk  = simple_strtoul(argv[3], NULL, 16);
  			unsigned long cnt  = simple_strtoul(argv[4], NULL, 16);
  			unsigned long n;
de39f8c19   Michael Trimarchi   USB style patch, ...
777
778
779
780
781
782
783
784
785
786
  			printf("
  USB read: device %d block # %ld, count %ld"
  				" ... ", usb_stor_curr_dev, blk, cnt);
  			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  			n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
  						 (ulong *)addr);
  			printf("%ld blocks read: %s
  ", n,
  				(n == cnt) ? "OK" : "ERROR");
  			if (n == cnt)
e887afc9d   wdenk   Initial revision
787
788
789
790
  				return 0;
  			return 1;
  		}
  	}
127e10842   Mahavir Jain   usb: write comman...
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
  	if (strcmp(argv[1], "write") == 0) {
  		if (usb_stor_curr_dev < 0) {
  			printf("no current device selected
  ");
  			return 1;
  		}
  		if (argc == 5) {
  			unsigned long addr = simple_strtoul(argv[2], NULL, 16);
  			unsigned long blk  = simple_strtoul(argv[3], NULL, 16);
  			unsigned long cnt  = simple_strtoul(argv[4], NULL, 16);
  			unsigned long n;
  			printf("
  USB write: device %d block # %ld, count %ld"
  				" ... ", usb_stor_curr_dev, blk, cnt);
  			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
  			n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
  						(ulong *)addr);
  			printf("%ld blocks write: %s
  ", n,
  				(n == cnt) ? "OK" : "ERROR");
  			if (n == cnt)
  				return 0;
  			return 1;
  		}
  	}
9c998aa83   Wolfgang Denk   Fix low-level OHC...
816
817
  	if (strncmp(argv[1], "dev", 3) == 0) {
  		if (argc == 3) {
e887afc9d   wdenk   Initial revision
818
  			int dev = (int)simple_strtoul(argv[2], NULL, 10);
de39f8c19   Michael Trimarchi   USB style patch, ...
819
820
  			printf("
  USB device %d: ", dev);
aaad108b8   Kim B. Heino   USB storage count
821
822
  			stor_dev = usb_stor_get_dev(dev);
  			if (stor_dev == NULL) {
e887afc9d   wdenk   Initial revision
823
824
825
826
  				printf("unknown device
  ");
  				return 1;
  			}
de39f8c19   Michael Trimarchi   USB style patch, ...
827
828
  			printf("
      Device %d: ", dev);
e887afc9d   wdenk   Initial revision
829
  			dev_print(stor_dev);
de39f8c19   Michael Trimarchi   USB style patch, ...
830
  			if (stor_dev->type == DEV_TYPE_UNKNOWN)
e887afc9d   wdenk   Initial revision
831
  				return 1;
e887afc9d   wdenk   Initial revision
832
833
834
835
  			usb_stor_curr_dev = dev;
  			printf("... is now current device
  ");
  			return 0;
de39f8c19   Michael Trimarchi   USB style patch, ...
836
837
838
839
  		} else {
  			printf("
  USB device %d: ", usb_stor_curr_dev);
  			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
e887afc9d   wdenk   Initial revision
840
  			dev_print(stor_dev);
de39f8c19   Michael Trimarchi   USB style patch, ...
841
  			if (stor_dev->type == DEV_TYPE_UNKNOWN)
e887afc9d   wdenk   Initial revision
842
  				return 1;
e887afc9d   wdenk   Initial revision
843
844
845
846
847
  			return 0;
  		}
  		return 0;
  	}
  #endif /* CONFIG_USB_STORAGE */
4c12eeb8b   Simon Glass   Convert cmd_usage...
848
  	return CMD_RET_USAGE;
e887afc9d   wdenk   Initial revision
849
  }
0d4983930   wdenk   Patch by Kenneth ...
850
851
  U_BOOT_CMD(
  	usb,	5,	1,	do_usb,
2fb2604d5   Peter Tyser   Command usage cle...
852
  	"USB sub-system",
1af9f9633   Veli-Pekka Peltola   usb: add help for...
853
854
855
856
  	"start - start (scan) USB controller
  "
  	"usb reset - reset (rescan) USB controller
  "
b3813a221   Veli-Pekka Peltola   cosmetic: remove ...
857
858
859
860
  	"usb stop [f] - stop USB [f]=force stop
  "
  	"usb tree - show USB device tree
  "
5cf91d6bd   wdenk   * Modify KUP4X bo...
861
862
  	"usb info [dev] - show available USB devices
  "
7d9aa8fd8   Julius Werner   usb: Add new comm...
863
864
865
866
867
868
869
  	"usb test [dev] [port] [mode] - set USB 2.0 test mode
  "
  	"    (specify port 0 to indicate the device's upstream port)
  "
  	"    Available modes: J, K, S[E0_NAK], P[acket], F[orce_Enable]
  "
  #ifdef CONFIG_USB_STORAGE
b3813a221   Veli-Pekka Peltola   cosmetic: remove ...
870
871
  	"usb storage - show details of USB storage devices
  "
342717f72   wdenk   * Fix baudrate ca...
872
873
  	"usb dev [dev] - show or set current USB storage device
  "
de39f8c19   Michael Trimarchi   USB style patch, ...
874
  	"usb part [dev] - print partition table of one or all USB storage"
7d9aa8fd8   Julius Werner   usb: Add new comm...
875
876
  	"    devices
  "
5cf91d6bd   wdenk   * Modify KUP4X bo...
877
878
  	"usb read addr blk# cnt - read `cnt' blocks starting at block `blk#'
  "
842404ea0   Sergei Poselenov   Fixed clobbered o...
879
880
  	"    to memory address `addr'
  "
127e10842   Mahavir Jain   usb: write comman...
881
882
883
  	"usb write addr blk# cnt - write `cnt' blocks starting at block `blk#'
  "
  	"    from memory address `addr'"
7d9aa8fd8   Julius Werner   usb: Add new comm...
884
  #endif /* CONFIG_USB_STORAGE */
8bde7f776   wdenk   * Code cleanup:
885
  );
7d9aa8fd8   Julius Werner   usb: Add new comm...
886
  #ifdef CONFIG_USB_STORAGE
0d4983930   wdenk   Patch by Kenneth ...
887
888
  U_BOOT_CMD(
  	usbboot,	3,	1,	do_usbboot,
2fb2604d5   Peter Tyser   Command usage cle...
889
  	"boot from USB device",
a89c33db9   Wolfgang Denk   General help mess...
890
  	"loadAddr dev:part"
8bde7f776   wdenk   * Code cleanup:
891
  );
7d9aa8fd8   Julius Werner   usb: Add new comm...
892
  #endif /* CONFIG_USB_STORAGE */