usbh_msc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_msc.h"
  8. #include "usb_scsi.h"
  9. #undef USB_DBG_TAG
  10. #define USB_DBG_TAG "usbh_msc"
  11. #include "usb_log.h"
  12. #define DEV_FORMAT "/dev/sd%c"
  13. #ifndef CONFIG_USBHOST_MSC_READY_CHECK_TIMES
  14. #define CONFIG_USBHOST_MSC_READY_CHECK_TIMES 10
  15. #endif
  16. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_msc_cbw_csw[CONFIG_USBHOST_MAX_MSC_CLASS][USB_ALIGN_UP(64, CONFIG_USB_ALIGN_SIZE)];
  17. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_msc_buf[CONFIG_USBHOST_MAX_MSC_CLASS][USB_ALIGN_UP(64, CONFIG_USB_ALIGN_SIZE)];
  18. static struct usbh_msc g_msc_class[CONFIG_USBHOST_MAX_MSC_CLASS];
  19. static uint32_t g_devinuse = 0;
  20. static struct usbh_msc_modeswitch_config *g_msc_modeswitch_config = NULL;
  21. static struct usbh_msc *usbh_msc_class_alloc(void)
  22. {
  23. uint8_t devno;
  24. for (devno = 0; devno < CONFIG_USBHOST_MAX_MSC_CLASS; devno++) {
  25. if ((g_devinuse & (1U << devno)) == 0) {
  26. g_devinuse |= (1U << devno);
  27. memset(&g_msc_class[devno], 0, sizeof(struct usbh_msc));
  28. g_msc_class[devno].sdchar = 'a' + devno;
  29. return &g_msc_class[devno];
  30. }
  31. }
  32. return NULL;
  33. }
  34. static void usbh_msc_class_free(struct usbh_msc *msc_class)
  35. {
  36. uint8_t devno = msc_class->sdchar - 'a';
  37. if (devno < 32) {
  38. g_devinuse &= ~(1U << devno);
  39. }
  40. memset(msc_class, 0, sizeof(struct usbh_msc));
  41. }
  42. static int usbh_msc_get_maxlun(struct usbh_msc *msc_class, uint8_t *buffer)
  43. {
  44. struct usb_setup_packet *setup;
  45. if (!msc_class || !msc_class->hport) {
  46. return -USB_ERR_INVAL;
  47. }
  48. setup = msc_class->hport->setup;
  49. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
  50. setup->bRequest = MSC_REQUEST_GET_MAX_LUN;
  51. setup->wValue = 0;
  52. setup->wIndex = msc_class->intf;
  53. setup->wLength = 1;
  54. return usbh_control_transfer(msc_class->hport, setup, buffer);
  55. }
  56. static void usbh_msc_cbw_dump(struct CBW *cbw)
  57. {
  58. int i;
  59. USB_LOG_DBG("CBW:\r\n");
  60. USB_LOG_DBG(" signature: 0x%08x\r\n", (unsigned int)cbw->dSignature);
  61. USB_LOG_DBG(" tag: 0x%08x\r\n", (unsigned int)cbw->dTag);
  62. USB_LOG_DBG(" datlen: 0x%08x\r\n", (unsigned int)cbw->dDataLength);
  63. USB_LOG_DBG(" flags: 0x%02x\r\n", cbw->bmFlags);
  64. USB_LOG_DBG(" lun: 0x%02x\r\n", cbw->bLUN);
  65. USB_LOG_DBG(" cblen: 0x%02x\r\n", cbw->bCBLength);
  66. USB_LOG_DBG("CB:\r\n");
  67. for (i = 0; i < cbw->bCBLength; i += 8) {
  68. USB_LOG_DBG(" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n",
  69. cbw->CB[i], cbw->CB[i + 1], cbw->CB[i + 2],
  70. cbw->CB[i + 3], cbw->CB[i + 4], cbw->CB[i + 5],
  71. cbw->CB[i + 6], cbw->CB[i + 7]);
  72. }
  73. }
  74. static void usbh_msc_csw_dump(struct CSW *csw)
  75. {
  76. (void)csw;
  77. USB_LOG_DBG("CSW:\r\n");
  78. USB_LOG_DBG(" signature: 0x%08x\r\n", (unsigned int)csw->dSignature);
  79. USB_LOG_DBG(" tag: 0x%08x\r\n", (unsigned int)csw->dTag);
  80. USB_LOG_DBG(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue);
  81. USB_LOG_DBG(" status: 0x%02x\r\n", csw->bStatus);
  82. }
  83. static inline int usbh_msc_bulk_in_transfer(struct usbh_msc *msc_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  84. {
  85. int ret;
  86. struct usbh_urb *urb = &msc_class->bulkin_urb;
  87. usbh_bulk_urb_fill(urb, msc_class->hport, msc_class->bulkin, buffer, buflen, timeout, NULL, NULL);
  88. ret = usbh_submit_urb(urb);
  89. if (ret == 0) {
  90. ret = urb->actual_length;
  91. }
  92. return ret;
  93. }
  94. static inline int usbh_msc_bulk_out_transfer(struct usbh_msc *msc_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
  95. {
  96. int ret;
  97. struct usbh_urb *urb = &msc_class->bulkout_urb;
  98. usbh_bulk_urb_fill(urb, msc_class->hport, msc_class->bulkout, buffer, buflen, timeout, NULL, NULL);
  99. ret = usbh_submit_urb(urb);
  100. if (ret == 0) {
  101. ret = urb->actual_length;
  102. }
  103. return ret;
  104. }
  105. static int usbh_bulk_cbw_csw_xfer(struct usbh_msc *msc_class, struct CBW *cbw, struct CSW *csw, uint8_t *buffer, uint32_t timeout)
  106. {
  107. int nbytes;
  108. usbh_msc_cbw_dump(cbw);
  109. /* Send the CBW */
  110. nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, timeout);
  111. if (nbytes < 0) {
  112. USB_LOG_ERR("cbw transfer error: %d\r\n", nbytes);
  113. goto __err_exit;
  114. }
  115. if (cbw->dDataLength != 0) {
  116. if (cbw->CB[0] == SCSI_CMD_WRITE10) {
  117. nbytes = usbh_msc_bulk_out_transfer(msc_class, buffer, cbw->dDataLength, timeout);
  118. } else if (cbw->CB[0] == SCSI_CMD_READCAPACITY10) {
  119. nbytes = usbh_msc_bulk_in_transfer(msc_class, buffer, cbw->dDataLength, timeout);
  120. if (nbytes >= 0) {
  121. /* Save the capacity information */
  122. msc_class->blocknum = GET_BE32(&buffer[0]) + 1;
  123. msc_class->blocksize = GET_BE32(&buffer[4]);
  124. }
  125. } else {
  126. nbytes = usbh_msc_bulk_in_transfer(msc_class, buffer, cbw->dDataLength, timeout);
  127. }
  128. if (nbytes < 0) {
  129. USB_LOG_ERR("msc data transfer error: %d\r\n", nbytes);
  130. goto __err_exit;
  131. }
  132. }
  133. /* Receive the CSW */
  134. memset(csw, 0, USB_SIZEOF_MSC_CSW);
  135. nbytes = usbh_msc_bulk_in_transfer(msc_class, (uint8_t *)csw, USB_SIZEOF_MSC_CSW, timeout);
  136. if (nbytes < 0) {
  137. USB_LOG_ERR("csw transfer error: %d\r\n", nbytes);
  138. goto __err_exit;
  139. }
  140. usbh_msc_csw_dump(csw);
  141. /* check csw status */
  142. if (csw->dSignature != MSC_CSW_Signature) {
  143. USB_LOG_ERR("csw signature error\r\n");
  144. return -USB_ERR_INVAL;
  145. }
  146. if (csw->bStatus != 0) {
  147. USB_LOG_ERR("csw bStatus %d\r\n", csw->bStatus);
  148. return -USB_ERR_INVAL;
  149. }
  150. __err_exit:
  151. return nbytes < 0 ? (int)nbytes : 0;
  152. }
  153. static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class)
  154. {
  155. struct CBW *cbw;
  156. /* Construct the CBW */
  157. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  158. memset(cbw, 0, USB_SIZEOF_MSC_CBW);
  159. cbw->dSignature = MSC_CBW_Signature;
  160. cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF;
  161. cbw->CB[0] = SCSI_CMD_TESTUNITREADY;
  162. return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], NULL, CONFIG_USBHOST_MSC_TIMEOUT);
  163. }
  164. static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class)
  165. {
  166. struct CBW *cbw;
  167. /* Construct the CBW */
  168. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  169. memset(cbw, 0, USB_SIZEOF_MSC_CBW);
  170. cbw->dSignature = MSC_CBW_Signature;
  171. cbw->bmFlags = 0x80;
  172. cbw->dDataLength = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
  173. cbw->bCBLength = SCSICMD_REQUESTSENSE_SIZEOF;
  174. cbw->CB[0] = SCSI_CMD_REQUESTSENSE;
  175. cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
  176. return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], g_msc_buf[msc_class->sdchar - 'a'], CONFIG_USBHOST_MSC_TIMEOUT);
  177. }
  178. static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class)
  179. {
  180. struct CBW *cbw;
  181. /* Construct the CBW */
  182. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  183. memset(cbw, 0, USB_SIZEOF_MSC_CBW);
  184. cbw->dSignature = MSC_CBW_Signature;
  185. cbw->dDataLength = SCSIRESP_INQUIRY_SIZEOF;
  186. cbw->bmFlags = 0x80;
  187. cbw->bCBLength = SCSICMD_INQUIRY_SIZEOF;
  188. cbw->CB[0] = SCSI_CMD_INQUIRY;
  189. cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF;
  190. return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], g_msc_buf[msc_class->sdchar - 'a'], CONFIG_USBHOST_MSC_TIMEOUT);
  191. }
  192. static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class)
  193. {
  194. struct CBW *cbw;
  195. /* Construct the CBW */
  196. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  197. memset(cbw, 0, USB_SIZEOF_MSC_CBW);
  198. cbw->dSignature = MSC_CBW_Signature;
  199. cbw->dDataLength = SCSIRESP_READCAPACITY10_SIZEOF;
  200. cbw->bmFlags = 0x80;
  201. cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF;
  202. cbw->CB[0] = SCSI_CMD_READCAPACITY10;
  203. return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], g_msc_buf[msc_class->sdchar - 'a'], CONFIG_USBHOST_MSC_TIMEOUT);
  204. }
  205. static inline void usbh_msc_modeswitch(struct usbh_msc *msc_class, const uint8_t *message)
  206. {
  207. struct CBW *cbw;
  208. /* Construct the CBW */
  209. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  210. memcpy(g_msc_cbw_csw[msc_class->sdchar - 'a'], message, 31);
  211. usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], NULL, CONFIG_USBHOST_MSC_TIMEOUT);
  212. }
  213. static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
  214. {
  215. struct usb_endpoint_descriptor *ep_desc;
  216. struct usbh_msc_modeswitch_config *config;
  217. int ret;
  218. struct usbh_msc *msc_class = usbh_msc_class_alloc();
  219. if (msc_class == NULL) {
  220. USB_LOG_ERR("Fail to alloc msc_class\r\n");
  221. return -USB_ERR_NOMEM;
  222. }
  223. msc_class->hport = hport;
  224. msc_class->intf = intf;
  225. hport->config.intf[intf].priv = msc_class;
  226. ret = usbh_msc_get_maxlun(msc_class, g_msc_buf[msc_class->sdchar - 'a']);
  227. if (ret < 0) {
  228. if (ret == -USB_ERR_STALL) {
  229. USB_LOG_WRN("Device does not support multiple LUNs\r\n");
  230. g_msc_buf[msc_class->sdchar - 'a'][0] = 0;
  231. } else {
  232. return ret;
  233. }
  234. }
  235. USB_LOG_INFO("Get max LUN:%u\r\n", g_msc_buf[msc_class->sdchar - 'a'][0] + 1);
  236. for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
  237. ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
  238. if (ep_desc->bEndpointAddress & 0x80) {
  239. USBH_EP_INIT(msc_class->bulkin, ep_desc);
  240. } else {
  241. USBH_EP_INIT(msc_class->bulkout, ep_desc);
  242. }
  243. }
  244. if (g_msc_modeswitch_config) {
  245. uint8_t num = 0;
  246. while (1) {
  247. config = &g_msc_modeswitch_config[num];
  248. if (config && config->name) {
  249. if ((hport->device_desc.idVendor == config->vid) &&
  250. (hport->device_desc.idProduct == config->pid)) {
  251. USB_LOG_INFO("%s usb_modeswitch enable\r\n", config->name);
  252. usbh_msc_modeswitch(msc_class, config->message_content);
  253. return 0;
  254. }
  255. num++;
  256. } else {
  257. break;
  258. }
  259. }
  260. }
  261. snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
  262. USB_LOG_INFO("Register MSC Class:%s\r\n", hport->config.intf[intf].devname);
  263. usbh_msc_run(msc_class);
  264. return ret;
  265. }
  266. static int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf)
  267. {
  268. int ret = 0;
  269. struct usbh_msc *msc_class = (struct usbh_msc *)hport->config.intf[intf].priv;
  270. if (msc_class) {
  271. if (msc_class->bulkin) {
  272. usbh_kill_urb(&msc_class->bulkin_urb);
  273. }
  274. if (msc_class->bulkout) {
  275. usbh_kill_urb(&msc_class->bulkout_urb);
  276. }
  277. if (hport->config.intf[intf].devname[0] != '\0') {
  278. USB_LOG_INFO("Unregister MSC Class:%s\r\n", hport->config.intf[intf].devname);
  279. usbh_msc_stop(msc_class);
  280. }
  281. usbh_msc_class_free(msc_class);
  282. }
  283. return ret;
  284. }
  285. int usbh_msc_scsi_init(struct usbh_msc *msc_class)
  286. {
  287. int ret;
  288. uint16_t cnt;
  289. cnt = 0;
  290. while (usbh_msc_scsi_testunitready(msc_class) < 0) {
  291. USB_LOG_WRN("Device not ready, try again...\r\n");
  292. ret = usbh_msc_scsi_requestsense(msc_class);
  293. if (ret < 0) {
  294. USB_LOG_ERR("Fail to scsi_testunitready\r\n");
  295. }
  296. cnt++;
  297. if (cnt > CONFIG_USBHOST_MSC_READY_CHECK_TIMES) {
  298. return -USB_ERR_NODEV;
  299. }
  300. }
  301. ret = usbh_msc_scsi_inquiry(msc_class);
  302. if (ret < 0) {
  303. USB_LOG_ERR("Fail to scsi_inquiry\r\n");
  304. return ret;
  305. }
  306. ret = usbh_msc_scsi_readcapacity10(msc_class);
  307. if (ret < 0) {
  308. USB_LOG_ERR("Fail to scsi_readcapacity10\r\n");
  309. return ret;
  310. }
  311. if (msc_class->blocksize > 0) {
  312. USB_LOG_INFO("Capacity info:\r\n");
  313. USB_LOG_INFO("Block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
  314. } else {
  315. USB_LOG_ERR("Invalid block size\r\n");
  316. return -USB_ERR_RANGE;
  317. }
  318. return 0;
  319. }
  320. int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors)
  321. {
  322. struct CBW *cbw;
  323. /* Construct the CBW */
  324. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  325. memset(cbw, 0, USB_SIZEOF_MSC_CBW);
  326. cbw->dSignature = MSC_CBW_Signature;
  327. cbw->dDataLength = (msc_class->blocksize * nsectors);
  328. cbw->bCBLength = SCSICMD_WRITE10_SIZEOF;
  329. cbw->CB[0] = SCSI_CMD_WRITE10;
  330. SET_BE32(&cbw->CB[2], start_sector);
  331. SET_BE16(&cbw->CB[7], nsectors);
  332. return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], (uint8_t *)buffer, CONFIG_USBHOST_MSC_TIMEOUT);
  333. }
  334. int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors)
  335. {
  336. struct CBW *cbw;
  337. /* Construct the CBW */
  338. cbw = (struct CBW *)g_msc_cbw_csw[msc_class->sdchar - 'a'];
  339. memset(cbw, 0, USB_SIZEOF_MSC_CBW);
  340. cbw->dSignature = MSC_CBW_Signature;
  341. cbw->dDataLength = (msc_class->blocksize * nsectors);
  342. cbw->bmFlags = 0x80;
  343. cbw->bCBLength = SCSICMD_READ10_SIZEOF;
  344. cbw->CB[0] = SCSI_CMD_READ10;
  345. SET_BE32(&cbw->CB[2], start_sector);
  346. SET_BE16(&cbw->CB[7], nsectors);
  347. return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_cbw_csw[msc_class->sdchar - 'a'], (uint8_t *)buffer, CONFIG_USBHOST_MSC_TIMEOUT);
  348. }
  349. void usbh_msc_modeswitch_enable(struct usbh_msc_modeswitch_config *config)
  350. {
  351. if (config) {
  352. g_msc_modeswitch_config = config;
  353. } else {
  354. g_msc_modeswitch_config = NULL;
  355. }
  356. }
  357. __WEAK void usbh_msc_run(struct usbh_msc *msc_class)
  358. {
  359. (void)msc_class;
  360. }
  361. __WEAK void usbh_msc_stop(struct usbh_msc *msc_class)
  362. {
  363. (void)msc_class;
  364. }
  365. const struct usbh_class_driver msc_class_driver = {
  366. .driver_name = "msc",
  367. .connect = usbh_msc_connect,
  368. .disconnect = usbh_msc_disconnect
  369. };
  370. CLASS_INFO_DEFINE const struct usbh_class_info msc_class_info = {
  371. .match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
  372. .bInterfaceClass = USB_DEVICE_CLASS_MASS_STORAGE,
  373. .bInterfaceSubClass = MSC_SUBCLASS_SCSI,
  374. .bInterfaceProtocol = MSC_PROTOCOL_BULK_ONLY,
  375. .id_table = NULL,
  376. .class_driver = &msc_class_driver
  377. };