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