usbh_msc.c 13 KB

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