USBHostMSD.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* mbed USBHost Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "USBHostMSD.h"
  17. #if USBHOST_MSD
  18. #include "dbg.h"
  19. #define CBW_SIGNATURE 0x43425355
  20. #define CSW_SIGNATURE 0x53425355
  21. #define DEVICE_TO_HOST 0x80
  22. #define HOST_TO_DEVICE 0x00
  23. #define GET_MAX_LUN (0xFE)
  24. #define BO_MASS_STORAGE_RESET (0xFF)
  25. USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
  26. {
  27. host = USBHost::getHostInst();
  28. init();
  29. }
  30. void USBHostMSD::init() {
  31. dev_connected = false;
  32. dev = NULL;
  33. bulk_in = NULL;
  34. bulk_out = NULL;
  35. dev_connected = false;
  36. blockSize = 0;
  37. blockCount = 0;
  38. msd_intf = -1;
  39. msd_device_found = false;
  40. disk_init = false;
  41. dev_connected = false;
  42. nb_ep = 0;
  43. }
  44. bool USBHostMSD::connected()
  45. {
  46. return dev_connected;
  47. }
  48. bool USBHostMSD::connect()
  49. {
  50. if (dev_connected) {
  51. return true;
  52. }
  53. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
  54. if ((dev = host->getDevice(i)) != NULL) {
  55. USB_DBG("Trying to connect MSD device\r\n");
  56. if(host->enumerate(dev, this))
  57. break;
  58. if (msd_device_found) {
  59. bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
  60. bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
  61. if (!bulk_in || !bulk_out)
  62. continue;
  63. USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf);
  64. dev->setName("MSD", msd_intf);
  65. host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
  66. dev_connected = true;
  67. return true;
  68. }
  69. } //if()
  70. } //for()
  71. init();
  72. return false;
  73. }
  74. /*virtual*/ void USBHostMSD::setVidPid(uint16_t vid, uint16_t pid)
  75. {
  76. // we don't check VID/PID for MSD driver
  77. }
  78. /*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
  79. {
  80. if ((msd_intf == -1) &&
  81. (intf_class == MSD_CLASS) &&
  82. (intf_subclass == 0x06) &&
  83. (intf_protocol == 0x50)) {
  84. msd_intf = intf_nb;
  85. return true;
  86. }
  87. return false;
  88. }
  89. /*virtual*/ bool USBHostMSD::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
  90. {
  91. if (intf_nb == msd_intf) {
  92. if (type == BULK_ENDPOINT) {
  93. nb_ep++;
  94. if (nb_ep == 2)
  95. msd_device_found = true;
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. int USBHostMSD::testUnitReady() {
  102. USB_DBG("Test unit ready");
  103. return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
  104. }
  105. int USBHostMSD::readCapacity() {
  106. USB_DBG("Read capacity");
  107. uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
  108. uint8_t result[8];
  109. int status = SCSITransfer(cmd, 10, DEVICE_TO_HOST, result, 8);
  110. if (status == 0) {
  111. blockCount = (result[0] << 24) | (result[1] << 16) | (result[2] << 8) | result[3];
  112. blockSize = (result[4] << 24) | (result[5] << 16) | (result[6] << 8) | result[7];
  113. USB_INFO("MSD [dev: %p] - blockCount: %lld, blockSize: %d, Capacity: %lld\r\n", dev, blockCount, blockSize, blockCount*blockSize);
  114. }
  115. return status;
  116. }
  117. int USBHostMSD::SCSIRequestSense() {
  118. USB_DBG("Request sense");
  119. uint8_t cmd[6] = {0x03,0,0,0,18,0};
  120. uint8_t result[18];
  121. int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 18);
  122. return status;
  123. }
  124. int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
  125. USB_DBG("Inquiry");
  126. uint8_t evpd = (page_code == 0) ? 0 : 1;
  127. uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0};
  128. uint8_t result[36];
  129. int status = SCSITransfer(cmd, 6, DEVICE_TO_HOST, result, 36);
  130. if (status == 0) {
  131. char vid_pid[17];
  132. memcpy(vid_pid, &result[8], 8);
  133. vid_pid[8] = 0;
  134. USB_INFO("MSD [dev: %p] - Vendor ID: %s", dev, vid_pid);
  135. memcpy(vid_pid, &result[16], 16);
  136. vid_pid[16] = 0;
  137. USB_INFO("MSD [dev: %p] - Product ID: %s", dev, vid_pid);
  138. memcpy(vid_pid, &result[32], 4);
  139. vid_pid[4] = 0;
  140. USB_INFO("MSD [dev: %p] - Product rev: %s", dev, vid_pid);
  141. }
  142. return status;
  143. }
  144. int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
  145. // if ep stalled: send clear feature
  146. if (res == USB_TYPE_STALL_ERROR) {
  147. res = host->controlWrite( dev,
  148. USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
  149. CLEAR_FEATURE,
  150. 0, ep->getAddress(), NULL, 0);
  151. // set state to IDLE if clear feature successful
  152. if (res == USB_TYPE_OK) {
  153. ep->setState(USB_TYPE_IDLE);
  154. }
  155. }
  156. if (res != USB_TYPE_OK)
  157. return -1;
  158. return 0;
  159. }
  160. int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
  161. int res = 0;
  162. cbw.Signature = CBW_SIGNATURE;
  163. cbw.Tag = 0;
  164. cbw.DataLength = transfer_len;
  165. cbw.Flags = flags;
  166. cbw.LUN = 0;
  167. cbw.CBLength = cmd_len;
  168. memset(cbw.CB,0,sizeof(cbw.CB));
  169. if (cmd) {
  170. memcpy(cbw.CB,cmd,cmd_len);
  171. }
  172. // send the cbw
  173. USB_DBG("Send CBW");
  174. res = host->bulkWrite(dev, bulk_out,(uint8_t *)&cbw, 31);
  175. if (checkResult(res, bulk_out))
  176. return -1;
  177. // data stage if needed
  178. if (data) {
  179. USB_DBG("data stage");
  180. if (flags == HOST_TO_DEVICE) {
  181. res = host->bulkWrite(dev, bulk_out, data, transfer_len);
  182. if (checkResult(res, bulk_out))
  183. return -1;
  184. } else if (flags == DEVICE_TO_HOST) {
  185. res = host->bulkRead(dev, bulk_in, data, transfer_len);
  186. if (checkResult(res, bulk_in))
  187. return -1;
  188. }
  189. }
  190. // status stage
  191. csw.Signature = 0;
  192. USB_DBG("Read CSW");
  193. res = host->bulkRead(dev, bulk_in,(uint8_t *)&csw, 13);
  194. if (checkResult(res, bulk_in))
  195. return -1;
  196. if (csw.Signature != CSW_SIGNATURE) {
  197. return -1;
  198. }
  199. USB_DBG("recv csw: status: %d", csw.Status);
  200. // ModeSense?
  201. if ((csw.Status == 1) && (cmd[0] != 0x03)) {
  202. USB_DBG("request mode sense");
  203. return SCSIRequestSense();
  204. }
  205. // perform reset recovery
  206. if ((csw.Status == 2) && (cmd[0] != 0x03)) {
  207. // send Bulk-Only Mass Storage Reset request
  208. res = host->controlWrite( dev,
  209. USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
  210. BO_MASS_STORAGE_RESET,
  211. 0, msd_intf, NULL, 0);
  212. // unstall both endpoints
  213. res = host->controlWrite( dev,
  214. USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
  215. CLEAR_FEATURE,
  216. 0, bulk_in->getAddress(), NULL, 0);
  217. res = host->controlWrite( dev,
  218. USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD,
  219. CLEAR_FEATURE,
  220. 0, bulk_out->getAddress(), NULL, 0);
  221. }
  222. return csw.Status;
  223. }
  224. int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
  225. uint8_t cmd[10];
  226. memset(cmd,0,10);
  227. cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
  228. cmd[2] = (block >> 24) & 0xff;
  229. cmd[3] = (block >> 16) & 0xff;
  230. cmd[4] = (block >> 8) & 0xff;
  231. cmd[5] = block & 0xff;
  232. cmd[7] = (nbBlock >> 8) & 0xff;
  233. cmd[8] = nbBlock & 0xff;
  234. return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
  235. }
  236. int USBHostMSD::getMaxLun() {
  237. uint8_t buf[1], res;
  238. res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
  239. 0xfe, 0, msd_intf, buf, 1);
  240. USB_DBG("max lun: %d", buf[0]);
  241. return res;
  242. }
  243. int USBHostMSD::disk_initialize() {
  244. USB_DBG("FILESYSTEM: init");
  245. uint16_t i, timeout = 10;
  246. getMaxLun();
  247. for (i = 0; i < timeout; i++) {
  248. Thread::wait(100);
  249. if (!testUnitReady())
  250. break;
  251. }
  252. if (i == timeout) {
  253. disk_init = false;
  254. return -1;
  255. }
  256. inquiry(0, 0);
  257. disk_init = 1;
  258. return readCapacity();
  259. }
  260. int USBHostMSD::disk_write(const uint8_t* buffer, uint64_t block_number, uint8_t count) {
  261. USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count);
  262. if (!disk_init) {
  263. disk_initialize();
  264. }
  265. if (!disk_init)
  266. return -1;
  267. for (uint64_t b = block_number; b < block_number + count; b++) {
  268. if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE))
  269. return -1;
  270. buffer += 512;
  271. }
  272. return 0;
  273. }
  274. int USBHostMSD::disk_read(uint8_t* buffer, uint64_t block_number, uint8_t count) {
  275. USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count);
  276. if (!disk_init) {
  277. disk_initialize();
  278. }
  279. if (!disk_init)
  280. return -1;
  281. for (uint64_t b = block_number; b < block_number + count; b++) {
  282. if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST))
  283. return -1;
  284. buffer += 512;
  285. }
  286. return 0;
  287. }
  288. uint64_t USBHostMSD::disk_sectors() {
  289. USB_DBG("FILESYSTEM: sectors");
  290. if (!disk_init) {
  291. disk_initialize();
  292. }
  293. if (!disk_init)
  294. return 0;
  295. return blockCount;
  296. }
  297. #endif