usb_mass_storage.c 5.3 KB

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