usb_mass_storage.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 Samsung Electronics
  4. * Lukasz Majewski <l.majewski@samsung.com>
  5. *
  6. * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
  7. */
  8. #include <common.h>
  9. #include <blk.h>
  10. #include <command.h>
  11. #include <console.h>
  12. #include <errno.h>
  13. #include <g_dnl.h>
  14. #include <malloc.h>
  15. #include <part.h>
  16. #include <usb.h>
  17. #include <usb_mass_storage.h>
  18. #include <watchdog.h>
  19. static int ums_read_sector(struct ums *ums_dev,
  20. ulong start, lbaint_t blkcnt, void *buf)
  21. {
  22. struct blk_desc *block_dev = &ums_dev->block_dev;
  23. lbaint_t blkstart = start + ums_dev->start_sector;
  24. return blk_dread(block_dev, blkstart, blkcnt, buf);
  25. }
  26. static int ums_write_sector(struct ums *ums_dev,
  27. ulong start, lbaint_t blkcnt, const void *buf)
  28. {
  29. struct blk_desc *block_dev = &ums_dev->block_dev;
  30. lbaint_t blkstart = start + ums_dev->start_sector;
  31. return blk_dwrite(block_dev, blkstart, blkcnt, buf);
  32. }
  33. static struct ums *ums;
  34. static int ums_count;
  35. static void ums_fini(void)
  36. {
  37. int i;
  38. for (i = 0; i < ums_count; i++)
  39. free((void *)ums[i].name);
  40. free(ums);
  41. ums = NULL;
  42. ums_count = 0;
  43. }
  44. #define UMS_NAME_LEN 16
  45. static int ums_init(const char *devtype, const char *devnums_part_str)
  46. {
  47. char *s, *t, *devnum_part_str, *name;
  48. struct blk_desc *block_dev;
  49. struct disk_partition info;
  50. int partnum;
  51. int ret = -1;
  52. struct ums *ums_new;
  53. s = strdup(devnums_part_str);
  54. if (!s)
  55. return -1;
  56. t = s;
  57. ums_count = 0;
  58. for (;;) {
  59. devnum_part_str = strsep(&t, ",");
  60. if (!devnum_part_str)
  61. break;
  62. partnum = blk_get_device_part_str(devtype, devnum_part_str,
  63. &block_dev, &info, 1);
  64. if (partnum < 0)
  65. goto cleanup;
  66. /* Check if the argument is in legacy format. If yes,
  67. * expose all partitions by setting the partnum = 0
  68. * e.g. ums 0 mmc 0
  69. */
  70. if (!strchr(devnum_part_str, ':'))
  71. partnum = 0;
  72. /* f_mass_storage.c assumes SECTOR_SIZE sectors */
  73. if (block_dev->blksz != SECTOR_SIZE)
  74. goto cleanup;
  75. ums_new = realloc(ums, (ums_count + 1) * sizeof(*ums));
  76. if (!ums_new)
  77. goto cleanup;
  78. ums = ums_new;
  79. /* if partnum = 0, expose all partitions */
  80. if (partnum == 0) {
  81. ums[ums_count].start_sector = 0;
  82. ums[ums_count].num_sectors = block_dev->lba;
  83. } else {
  84. ums[ums_count].start_sector = info.start;
  85. ums[ums_count].num_sectors = info.size;
  86. }
  87. ums[ums_count].read_sector = ums_read_sector;
  88. ums[ums_count].write_sector = ums_write_sector;
  89. name = malloc(UMS_NAME_LEN);
  90. if (!name)
  91. goto cleanup;
  92. snprintf(name, UMS_NAME_LEN, "UMS disk %d", ums_count);
  93. ums[ums_count].name = name;
  94. ums[ums_count].block_dev = *block_dev;
  95. printf("UMS: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n",
  96. ums_count, ums[ums_count].block_dev.devnum,
  97. ums[ums_count].block_dev.hwpart,
  98. ums[ums_count].start_sector,
  99. ums[ums_count].num_sectors);
  100. ums_count++;
  101. }
  102. if (ums_count)
  103. ret = 0;
  104. cleanup:
  105. free(s);
  106. if (ret < 0)
  107. ums_fini();
  108. return ret;
  109. }
  110. static int do_usb_mass_storage(cmd_tbl_t *cmdtp, int flag,
  111. int argc, char * const argv[])
  112. {
  113. const char *usb_controller;
  114. const char *devtype;
  115. const char *devnum;
  116. unsigned int controller_index;
  117. int rc;
  118. int cable_ready_timeout __maybe_unused;
  119. if (argc < 3)
  120. return CMD_RET_USAGE;
  121. usb_controller = argv[1];
  122. if (argc >= 4) {
  123. devtype = argv[2];
  124. devnum = argv[3];
  125. } else {
  126. devtype = "mmc";
  127. devnum = argv[2];
  128. }
  129. rc = ums_init(devtype, devnum);
  130. if (rc < 0)
  131. return CMD_RET_FAILURE;
  132. controller_index = (unsigned int)(simple_strtoul(
  133. usb_controller, NULL, 0));
  134. if (usb_gadget_initialize(controller_index)) {
  135. pr_err("Couldn't init USB controller.\n");
  136. rc = CMD_RET_FAILURE;
  137. goto cleanup_ums_init;
  138. }
  139. rc = fsg_init(ums, ums_count);
  140. if (rc) {
  141. pr_err("fsg_init failed\n");
  142. rc = CMD_RET_FAILURE;
  143. goto cleanup_board;
  144. }
  145. rc = g_dnl_register("usb_dnl_ums");
  146. if (rc) {
  147. pr_err("g_dnl_register failed\n");
  148. rc = CMD_RET_FAILURE;
  149. goto cleanup_board;
  150. }
  151. /* Timeout unit: seconds */
  152. cable_ready_timeout = UMS_CABLE_READY_TIMEOUT;
  153. if (!g_dnl_board_usb_cable_connected()) {
  154. /*
  155. * Won't execute if we don't know whether the cable is
  156. * connected.
  157. */
  158. puts("Please connect USB cable.\n");
  159. while (!g_dnl_board_usb_cable_connected()) {
  160. if (ctrlc()) {
  161. puts("\rCTRL+C - Operation aborted.\n");
  162. rc = CMD_RET_SUCCESS;
  163. goto cleanup_register;
  164. }
  165. if (!cable_ready_timeout) {
  166. puts("\rUSB cable not detected.\n" \
  167. "Command exit.\n");
  168. rc = CMD_RET_SUCCESS;
  169. goto cleanup_register;
  170. }
  171. printf("\rAuto exit in: %.2d s.", cable_ready_timeout);
  172. mdelay(1000);
  173. cable_ready_timeout--;
  174. }
  175. puts("\r\n");
  176. }
  177. while (1) {
  178. usb_gadget_handle_interrupts(controller_index);
  179. rc = fsg_main_thread(NULL);
  180. if (rc) {
  181. /* Check I/O error */
  182. if (rc == -EIO)
  183. printf("\rCheck USB cable connection\n");
  184. /* Check CTRL+C */
  185. if (rc == -EPIPE)
  186. printf("\rCTRL+C - Operation aborted\n");
  187. rc = CMD_RET_SUCCESS;
  188. goto cleanup_register;
  189. }
  190. WATCHDOG_RESET();
  191. }
  192. cleanup_register:
  193. g_dnl_unregister();
  194. cleanup_board:
  195. usb_gadget_release(controller_index);
  196. cleanup_ums_init:
  197. ums_fini();
  198. return rc;
  199. }
  200. U_BOOT_CMD(ums, 4, 1, do_usb_mass_storage,
  201. "Use the UMS [USB Mass Storage]",
  202. "<USB_controller> [<devtype>] <dev[:part]> e.g. ums 0 mmc 0\n"
  203. " devtype defaults to mmc"
  204. );