mstorage.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-10-01 Yi Qiu first version
  9. * 2012-11-25 Heyuanjie87 reduce the memory consumption
  10. * 2012-12-09 Heyuanjie87 change function and endpoint handler
  11. * 2013-07-25 Yi Qiu update for USB CV test
  12. */
  13. #include <rtthread.h>
  14. #include <rtservice.h>
  15. #include "drivers/usb_device.h"
  16. #include "mstorage.h"
  17. #ifdef RT_USING_DFS_MNTTABLE
  18. #include "dfs_fs.h"
  19. #endif
  20. #ifdef RT_USB_DEVICE_MSTORAGE
  21. enum STAT
  22. {
  23. STAT_CBW,
  24. STAT_CMD,
  25. STAT_CSW,
  26. STAT_RECEIVE,
  27. STAT_SEND,
  28. };
  29. typedef enum
  30. {
  31. FIXED,
  32. COUNT,
  33. BLOCK_COUNT,
  34. }CB_SIZE_TYPE;
  35. typedef enum
  36. {
  37. DIR_IN,
  38. DIR_OUT,
  39. DIR_NONE,
  40. }CB_DIR;
  41. typedef rt_size_t (*cbw_handler)(ufunction_t func, ustorage_cbw_t cbw);
  42. struct scsi_cmd
  43. {
  44. rt_uint16_t cmd;
  45. cbw_handler handler;
  46. rt_size_t cmd_len;
  47. CB_SIZE_TYPE type;
  48. rt_size_t data_size;
  49. CB_DIR dir;
  50. };
  51. struct mstorage
  52. {
  53. struct ustorage_csw csw_response;
  54. uep_t ep_in;
  55. uep_t ep_out;
  56. int status;
  57. rt_uint32_t cb_data_size;
  58. rt_device_t disk;
  59. rt_uint32_t block;
  60. rt_int32_t count;
  61. rt_int32_t size;
  62. struct scsi_cmd* processing;
  63. struct rt_device_blk_geometry geometry;
  64. };
  65. ALIGN(4)
  66. static struct udevice_descriptor dev_desc =
  67. {
  68. USB_DESC_LENGTH_DEVICE, //bLength;
  69. USB_DESC_TYPE_DEVICE, //type;
  70. USB_BCD_VERSION, //bcdUSB;
  71. 0x00, //bDeviceClass;
  72. 0x00, //bDeviceSubClass;
  73. 0x00, //bDeviceProtocol;
  74. 0x40, //bMaxPacketSize0;
  75. _VENDOR_ID, //idVendor;
  76. _PRODUCT_ID, //idProduct;
  77. USB_BCD_DEVICE, //bcdDevice;
  78. USB_STRING_MANU_INDEX, //iManufacturer;
  79. USB_STRING_PRODUCT_INDEX, //iProduct;
  80. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  81. USB_DYNAMIC, //bNumConfigurations;
  82. };
  83. //FS and HS needed
  84. ALIGN(4)
  85. static struct usb_qualifier_descriptor dev_qualifier =
  86. {
  87. sizeof(dev_qualifier), //bLength
  88. USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
  89. 0x0200, //bcdUSB
  90. 0x00, //bDeviceClass
  91. 0x00, //bDeviceSubClass
  92. 0x00, //bDeviceProtocol
  93. 64, //bMaxPacketSize0
  94. 0x01, //bNumConfigurations
  95. 0,
  96. };
  97. ALIGN(4)
  98. const static struct umass_descriptor _mass_desc =
  99. {
  100. #ifdef RT_USB_DEVICE_COMPOSITE
  101. /* Interface Association Descriptor */
  102. {
  103. USB_DESC_LENGTH_IAD,
  104. USB_DESC_TYPE_IAD,
  105. USB_DYNAMIC,
  106. 0x01,
  107. USB_CLASS_MASS_STORAGE,
  108. 0x06,
  109. 0x50,
  110. 0x00,
  111. },
  112. #endif
  113. {
  114. USB_DESC_LENGTH_INTERFACE, //bLength;
  115. USB_DESC_TYPE_INTERFACE, //type;
  116. USB_DYNAMIC, //bInterfaceNumber;
  117. 0x00, //bAlternateSetting;
  118. 0x02, //bNumEndpoints
  119. USB_CLASS_MASS_STORAGE, //bInterfaceClass;
  120. 0x06, //bInterfaceSubClass;
  121. 0x50, //bInterfaceProtocol;
  122. 0x00, //iInterface;
  123. },
  124. {
  125. USB_DESC_LENGTH_ENDPOINT, //bLength;
  126. USB_DESC_TYPE_ENDPOINT, //type;
  127. USB_DYNAMIC | USB_DIR_OUT, //bEndpointAddress;
  128. USB_EP_ATTR_BULK, //bmAttributes;
  129. USB_DYNAMIC, //wMaxPacketSize;
  130. 0x00, //bInterval;
  131. },
  132. {
  133. USB_DESC_LENGTH_ENDPOINT, //bLength;
  134. USB_DESC_TYPE_ENDPOINT, //type;
  135. USB_DYNAMIC | USB_DIR_IN, //bEndpointAddress;
  136. USB_EP_ATTR_BULK, //bmAttributes;
  137. USB_DYNAMIC, //wMaxPacketSize;
  138. 0x00, //bInterval;
  139. },
  140. };
  141. ALIGN(4)
  142. const static char* _ustring[] =
  143. {
  144. "Language",
  145. "RT-Thread Team.",
  146. "RTT Mass Storage",
  147. "320219198301",
  148. "Configuration",
  149. "Interface",
  150. };
  151. static rt_size_t _test_unit_ready(ufunction_t func, ustorage_cbw_t cbw);
  152. static rt_size_t _request_sense(ufunction_t func, ustorage_cbw_t cbw);
  153. static rt_size_t _inquiry_cmd(ufunction_t func, ustorage_cbw_t cbw);
  154. static rt_size_t _allow_removal(ufunction_t func, ustorage_cbw_t cbw);
  155. static rt_size_t _start_stop(ufunction_t func, ustorage_cbw_t cbw);
  156. static rt_size_t _mode_sense_6(ufunction_t func, ustorage_cbw_t cbw);
  157. static rt_size_t _read_capacities(ufunction_t func, ustorage_cbw_t cbw);
  158. static rt_size_t _read_capacity(ufunction_t func, ustorage_cbw_t cbw);
  159. static rt_size_t _read_10(ufunction_t func, ustorage_cbw_t cbw);
  160. static rt_size_t _write_10(ufunction_t func, ustorage_cbw_t cbw);
  161. static rt_size_t _verify_10(ufunction_t func, ustorage_cbw_t cbw);
  162. ALIGN(4)
  163. static struct scsi_cmd cmd_data[] =
  164. {
  165. {SCSI_TEST_UNIT_READY, _test_unit_ready, 6, FIXED, 0, DIR_NONE},
  166. {SCSI_REQUEST_SENSE, _request_sense, 6, COUNT, 0, DIR_IN},
  167. {SCSI_INQUIRY_CMD, _inquiry_cmd, 6, COUNT, 0, DIR_IN},
  168. {SCSI_ALLOW_REMOVAL, _allow_removal, 6, FIXED, 0, DIR_NONE},
  169. {SCSI_MODE_SENSE_6, _mode_sense_6, 6, COUNT, 0, DIR_IN},
  170. {SCSI_START_STOP, _start_stop, 6, FIXED, 0, DIR_NONE},
  171. {SCSI_READ_CAPACITIES, _read_capacities, 10, COUNT, 0, DIR_NONE},
  172. {SCSI_READ_CAPACITY, _read_capacity, 10, FIXED, 8, DIR_IN},
  173. {SCSI_READ_10, _read_10, 10, BLOCK_COUNT, 0, DIR_IN},
  174. {SCSI_WRITE_10, _write_10, 10, BLOCK_COUNT, 0, DIR_OUT},
  175. {SCSI_VERIFY_10, _verify_10, 10, FIXED, 0, DIR_NONE},
  176. };
  177. static void _send_status(ufunction_t func)
  178. {
  179. struct mstorage *data;
  180. RT_ASSERT(func != RT_NULL);
  181. RT_DEBUG_LOG(RT_DEBUG_USB, ("_send_status\n"));
  182. data = (struct mstorage*)func->user_data;
  183. data->ep_in->request.buffer = (rt_uint8_t*)&data->csw_response;
  184. data->ep_in->request.size = SIZEOF_CSW;
  185. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  186. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  187. data->status = STAT_CSW;
  188. }
  189. static rt_size_t _test_unit_ready(ufunction_t func, ustorage_cbw_t cbw)
  190. {
  191. struct mstorage *data;
  192. RT_ASSERT(func != RT_NULL);
  193. RT_ASSERT(func->device != RT_NULL);
  194. RT_DEBUG_LOG(RT_DEBUG_USB, ("_test_unit_ready\n"));
  195. data = (struct mstorage*)func->user_data;
  196. data->csw_response.status = 0;
  197. return 0;
  198. }
  199. static rt_size_t _allow_removal(ufunction_t func, ustorage_cbw_t cbw)
  200. {
  201. struct mstorage *data;
  202. RT_ASSERT(func != RT_NULL);
  203. RT_ASSERT(func->device != RT_NULL);
  204. RT_DEBUG_LOG(RT_DEBUG_USB, ("_allow_removal\n"));
  205. data = (struct mstorage*)func->user_data;
  206. data->csw_response.status = 0;
  207. return 0;
  208. }
  209. /**
  210. * This function will handle inquiry command request.
  211. *
  212. * @param func the usb function object.
  213. * @param cbw the command block wrapper.
  214. *
  215. * @return RT_EOK on successful.
  216. */
  217. static rt_size_t _inquiry_cmd(ufunction_t func, ustorage_cbw_t cbw)
  218. {
  219. struct mstorage *data;
  220. rt_uint8_t *buf;
  221. RT_ASSERT(func != RT_NULL);
  222. RT_ASSERT(func->device != RT_NULL);
  223. RT_ASSERT(cbw != RT_NULL);
  224. RT_DEBUG_LOG(RT_DEBUG_USB, ("_inquiry_cmd\n"));
  225. data = (struct mstorage*)func->user_data;
  226. buf = data->ep_in->buffer;
  227. *(rt_uint32_t*)&buf[0] = 0x0 | (0x80 << 8);
  228. *(rt_uint32_t*)&buf[4] = 31;
  229. rt_memset(&buf[8], 0x20, 28);
  230. rt_memcpy(&buf[8], "RTT", 3);
  231. rt_memcpy(&buf[16], "USB Disk", 8);
  232. data->cb_data_size = MIN(data->cb_data_size, SIZEOF_INQUIRY_CMD);
  233. data->ep_in->request.buffer = buf;
  234. data->ep_in->request.size = data->cb_data_size;
  235. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  236. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  237. data->status = STAT_CMD;
  238. return data->cb_data_size;
  239. }
  240. /**
  241. * This function will handle sense request.
  242. *
  243. * @param func the usb function object.
  244. * @param cbw the command block wrapper.
  245. *
  246. * @return RT_EOK on successful.
  247. */
  248. static rt_size_t _request_sense(ufunction_t func, ustorage_cbw_t cbw)
  249. {
  250. struct mstorage *data;
  251. struct request_sense_data *buf;
  252. RT_ASSERT(func != RT_NULL);
  253. RT_ASSERT(func->device != RT_NULL);
  254. RT_ASSERT(cbw != RT_NULL);
  255. RT_DEBUG_LOG(RT_DEBUG_USB, ("_request_sense\n"));
  256. data = (struct mstorage*)func->user_data;
  257. buf = (struct request_sense_data *)data->ep_in->buffer;
  258. buf->ErrorCode = 0x70;
  259. buf->Valid = 0;
  260. buf->SenseKey = 2;
  261. buf->Information[0] = 0;
  262. buf->Information[1] = 0;
  263. buf->Information[2] = 0;
  264. buf->Information[3] = 0;
  265. buf->AdditionalSenseLength = 0x0a;
  266. buf->AdditionalSenseCode = 0x3a;
  267. buf->AdditionalSenseCodeQualifier = 0;
  268. data->cb_data_size = MIN(data->cb_data_size, SIZEOF_REQUEST_SENSE);
  269. data->ep_in->request.buffer = (rt_uint8_t*)data->ep_in->buffer;
  270. data->ep_in->request.size = data->cb_data_size;
  271. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  272. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  273. data->status = STAT_CMD;
  274. return data->cb_data_size;
  275. }
  276. /**
  277. * This function will handle mode_sense_6 request.
  278. *
  279. * @param func the usb function object.
  280. * @param cbw the command block wrapper.
  281. *
  282. * @return RT_EOK on successful.
  283. */
  284. static rt_size_t _mode_sense_6(ufunction_t func, ustorage_cbw_t cbw)
  285. {
  286. struct mstorage *data;
  287. rt_uint8_t *buf;
  288. RT_ASSERT(func != RT_NULL);
  289. RT_ASSERT(func->device != RT_NULL);
  290. RT_ASSERT(cbw != RT_NULL);
  291. RT_DEBUG_LOG(RT_DEBUG_USB, ("_mode_sense_6\n"));
  292. data = (struct mstorage*)func->user_data;
  293. buf = data->ep_in->buffer;
  294. buf[0] = 3;
  295. buf[1] = 0;
  296. buf[2] = 0;
  297. buf[3] = 0;
  298. data->cb_data_size = MIN(data->cb_data_size, SIZEOF_MODE_SENSE_6);
  299. data->ep_in->request.buffer = buf;
  300. data->ep_in->request.size = data->cb_data_size;
  301. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  302. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  303. data->status = STAT_CMD;
  304. return data->cb_data_size;
  305. }
  306. /**
  307. * This function will handle read_capacities request.
  308. *
  309. * @param func the usb function object.
  310. * @param cbw the command block wrapper.
  311. *
  312. * @return RT_EOK on successful.
  313. */
  314. static rt_size_t _read_capacities(ufunction_t func, ustorage_cbw_t cbw)
  315. {
  316. struct mstorage *data;
  317. rt_uint8_t *buf;
  318. rt_uint32_t sector_count, sector_size;
  319. RT_ASSERT(func != RT_NULL);
  320. RT_ASSERT(func->device != RT_NULL);
  321. RT_ASSERT(cbw != RT_NULL);
  322. RT_DEBUG_LOG(RT_DEBUG_USB, ("_read_capacities\n"));
  323. data = (struct mstorage*)func->user_data;
  324. buf = data->ep_in->buffer;
  325. sector_count = data->geometry.sector_count;
  326. sector_size = data->geometry.bytes_per_sector;
  327. *(rt_uint32_t*)&buf[0] = 0x08000000;
  328. buf[4] = sector_count >> 24;
  329. buf[5] = 0xff & (sector_count >> 16);
  330. buf[6] = 0xff & (sector_count >> 8);
  331. buf[7] = 0xff & (sector_count);
  332. buf[8] = 0x02;
  333. buf[9] = 0xff & (sector_size >> 16);
  334. buf[10] = 0xff & (sector_size >> 8);
  335. buf[11] = 0xff & sector_size;
  336. data->cb_data_size = MIN(data->cb_data_size, SIZEOF_READ_CAPACITIES);
  337. data->ep_in->request.buffer = buf;
  338. data->ep_in->request.size = data->cb_data_size;
  339. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  340. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  341. data->status = STAT_CMD;
  342. return data->cb_data_size;
  343. }
  344. /**
  345. * This function will handle read_capacity request.
  346. *
  347. * @param func the usb function object.
  348. * @param cbw the command block wapper.
  349. *
  350. * @return RT_EOK on successful.
  351. */
  352. static rt_size_t _read_capacity(ufunction_t func, ustorage_cbw_t cbw)
  353. {
  354. struct mstorage *data;
  355. rt_uint8_t *buf;
  356. rt_uint32_t sector_count, sector_size;
  357. RT_ASSERT(func != RT_NULL);
  358. RT_ASSERT(func->device != RT_NULL);
  359. RT_ASSERT(cbw != RT_NULL);
  360. RT_DEBUG_LOG(RT_DEBUG_USB, ("_read_capacity\n"));
  361. data = (struct mstorage*)func->user_data;
  362. buf = data->ep_in->buffer;
  363. sector_count = data->geometry.sector_count - 1; /* Last Logical Block Address */
  364. sector_size = data->geometry.bytes_per_sector;
  365. buf[0] = sector_count >> 24;
  366. buf[1] = 0xff & (sector_count >> 16);
  367. buf[2] = 0xff & (sector_count >> 8);
  368. buf[3] = 0xff & (sector_count);
  369. buf[4] = 0x0;
  370. buf[5] = 0xff & (sector_size >> 16);
  371. buf[6] = 0xff & (sector_size >> 8);
  372. buf[7] = 0xff & sector_size;
  373. data->cb_data_size = MIN(data->cb_data_size, SIZEOF_READ_CAPACITY);
  374. data->ep_in->request.buffer = buf;
  375. data->ep_in->request.size = data->cb_data_size;
  376. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  377. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  378. data->status = STAT_CMD;
  379. return data->cb_data_size;
  380. }
  381. /**
  382. * This function will handle read_10 request.
  383. *
  384. * @param func the usb function object.
  385. * @param cbw the command block wrapper.
  386. *
  387. * @return RT_EOK on successful.
  388. */
  389. static rt_size_t _read_10(ufunction_t func, ustorage_cbw_t cbw)
  390. {
  391. struct mstorage *data;
  392. rt_size_t size;
  393. RT_ASSERT(func != RT_NULL);
  394. RT_ASSERT(func->device != RT_NULL);
  395. RT_ASSERT(cbw != RT_NULL);
  396. data = (struct mstorage*)func->user_data;
  397. data->block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
  398. cbw->cb[5]<<0;
  399. data->count = cbw->cb[7]<<8 | cbw->cb[8]<<0;
  400. RT_ASSERT(data->count < data->geometry.sector_count);
  401. data->csw_response.data_reside = data->cb_data_size;
  402. size = rt_device_read(data->disk, data->block, data->ep_in->buffer, 1);
  403. if(size == 0)
  404. {
  405. rt_kprintf("read data error\n");
  406. }
  407. data->ep_in->request.buffer = data->ep_in->buffer;
  408. data->ep_in->request.size = data->geometry.bytes_per_sector;
  409. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  410. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  411. data->status = STAT_SEND;
  412. return data->geometry.bytes_per_sector;
  413. }
  414. /**
  415. * This function will handle write_10 request.
  416. *
  417. * @param func the usb function object.
  418. * @param cbw the command block wrapper.
  419. *
  420. * @return RT_EOK on successful.
  421. */
  422. static rt_size_t _write_10(ufunction_t func, ustorage_cbw_t cbw)
  423. {
  424. struct mstorage *data;
  425. RT_ASSERT(func != RT_NULL);
  426. RT_ASSERT(func->device != RT_NULL);
  427. RT_ASSERT(cbw != RT_NULL);
  428. data = (struct mstorage*)func->user_data;
  429. data->block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
  430. cbw->cb[5]<<0;
  431. data->count = cbw->cb[7]<<8 | cbw->cb[8];
  432. data->csw_response.data_reside = cbw->xfer_len;
  433. data->size = data->count * data->geometry.bytes_per_sector;
  434. RT_DEBUG_LOG(RT_DEBUG_USB, ("_write_10 count 0x%x block 0x%x 0x%x\n",
  435. data->count, data->block, data->geometry.sector_count));
  436. data->csw_response.data_reside = data->cb_data_size;
  437. data->ep_out->request.buffer = data->ep_out->buffer;
  438. data->ep_out->request.size = data->geometry.bytes_per_sector;
  439. data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
  440. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  441. data->status = STAT_RECEIVE;
  442. return data->geometry.bytes_per_sector;
  443. }
  444. /**
  445. * This function will handle verify_10 request.
  446. *
  447. * @param func the usb function object.
  448. *
  449. * @return RT_EOK on successful.
  450. */
  451. static rt_size_t _verify_10(ufunction_t func, ustorage_cbw_t cbw)
  452. {
  453. struct mstorage *data;
  454. RT_ASSERT(func != RT_NULL);
  455. RT_ASSERT(func->device != RT_NULL);
  456. RT_DEBUG_LOG(RT_DEBUG_USB, ("_verify_10\n"));
  457. data = (struct mstorage*)func->user_data;
  458. data->csw_response.status = 0;
  459. return 0;
  460. }
  461. static rt_size_t _start_stop(ufunction_t func,
  462. ustorage_cbw_t cbw)
  463. {
  464. struct mstorage *data;
  465. RT_ASSERT(func != RT_NULL);
  466. RT_ASSERT(func->device != RT_NULL);
  467. RT_DEBUG_LOG(RT_DEBUG_USB, ("_start_stop\n"));
  468. data = (struct mstorage*)func->user_data;
  469. data->csw_response.status = 0;
  470. return 0;
  471. }
  472. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  473. {
  474. struct mstorage *data;
  475. RT_ASSERT(func != RT_NULL);
  476. RT_ASSERT(func->device != RT_NULL);
  477. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_in_handler\n"));
  478. data = (struct mstorage*)func->user_data;
  479. switch(data->status)
  480. {
  481. case STAT_CSW:
  482. if(data->ep_in->request.size != SIZEOF_CSW)
  483. {
  484. rt_kprintf("Size of csw command error\n");
  485. rt_usbd_ep_set_stall(func->device, data->ep_in);
  486. }
  487. else
  488. {
  489. RT_DEBUG_LOG(RT_DEBUG_USB, ("return to cbw status\n"));
  490. data->ep_out->request.buffer = data->ep_out->buffer;
  491. data->ep_out->request.size = SIZEOF_CBW;
  492. data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
  493. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  494. data->status = STAT_CBW;
  495. }
  496. break;
  497. case STAT_CMD:
  498. if(data->csw_response.data_reside == 0xFF)
  499. {
  500. data->csw_response.data_reside = 0;
  501. }
  502. else
  503. {
  504. data->csw_response.data_reside -= data->ep_in->request.size;
  505. if(data->csw_response.data_reside != 0)
  506. {
  507. RT_DEBUG_LOG(RT_DEBUG_USB, ("data_reside %d, request %d\n",
  508. data->csw_response.data_reside, data->ep_in->request.size));
  509. if(data->processing->dir == DIR_OUT)
  510. {
  511. rt_usbd_ep_set_stall(func->device, data->ep_out);
  512. }
  513. else
  514. {
  515. rt_usbd_ep_set_stall(func->device, data->ep_in);
  516. }
  517. data->csw_response.data_reside = 0;
  518. }
  519. }
  520. _send_status(func);
  521. break;
  522. case STAT_SEND:
  523. data->csw_response.data_reside -= data->ep_in->request.size;
  524. data->count--;
  525. data->block++;
  526. if(data->count > 0 && data->csw_response.data_reside > 0)
  527. {
  528. if(rt_device_read(data->disk, data->block, data->ep_in->buffer, 1) == 0)
  529. {
  530. rt_kprintf("disk read error\n");
  531. rt_usbd_ep_set_stall(func->device, data->ep_in);
  532. return -RT_ERROR;
  533. }
  534. data->ep_in->request.buffer = data->ep_in->buffer;
  535. data->ep_in->request.size = data->geometry.bytes_per_sector;
  536. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  537. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  538. }
  539. else
  540. {
  541. _send_status(func);
  542. }
  543. break;
  544. }
  545. return RT_EOK;
  546. }
  547. #ifdef MASS_CBW_DUMP
  548. static void cbw_dump(struct ustorage_cbw* cbw)
  549. {
  550. RT_ASSERT(cbw != RT_NULL);
  551. RT_DEBUG_LOG(RT_DEBUG_USB, ("signature 0x%x\n", cbw->signature));
  552. RT_DEBUG_LOG(RT_DEBUG_USB, ("tag 0x%x\n", cbw->tag));
  553. RT_DEBUG_LOG(RT_DEBUG_USB, ("xfer_len 0x%x\n", cbw->xfer_len));
  554. RT_DEBUG_LOG(RT_DEBUG_USB, ("dflags 0x%x\n", cbw->dflags));
  555. RT_DEBUG_LOG(RT_DEBUG_USB, ("lun 0x%x\n", cbw->lun));
  556. RT_DEBUG_LOG(RT_DEBUG_USB, ("cb_len 0x%x\n", cbw->cb_len));
  557. RT_DEBUG_LOG(RT_DEBUG_USB, ("cb[0] 0x%x\n", cbw->cb[0]));
  558. }
  559. #endif
  560. static struct scsi_cmd* _find_cbw_command(rt_uint16_t cmd)
  561. {
  562. int i;
  563. for(i=0; i<sizeof(cmd_data)/sizeof(struct scsi_cmd); i++)
  564. {
  565. if(cmd_data[i].cmd == cmd)
  566. return &cmd_data[i];
  567. }
  568. return RT_NULL;
  569. }
  570. static void _cb_len_calc(ufunction_t func, struct scsi_cmd* cmd,
  571. ustorage_cbw_t cbw)
  572. {
  573. struct mstorage *data;
  574. RT_ASSERT(func != RT_NULL);
  575. RT_ASSERT(cmd != RT_NULL);
  576. RT_ASSERT(cbw != RT_NULL);
  577. data = (struct mstorage*)func->user_data;
  578. if(cmd->cmd_len == 6)
  579. {
  580. switch(cmd->type)
  581. {
  582. case COUNT:
  583. data->cb_data_size = cbw->cb[4];
  584. break;
  585. case BLOCK_COUNT:
  586. data->cb_data_size = cbw->cb[4] * data->geometry.bytes_per_sector;
  587. break;
  588. case FIXED:
  589. data->cb_data_size = cmd->data_size;
  590. break;
  591. default:
  592. break;
  593. }
  594. }
  595. else if(cmd->cmd_len == 10)
  596. {
  597. switch(cmd->type)
  598. {
  599. case COUNT:
  600. data->cb_data_size = cbw->cb[7]<<8 | cbw->cb[8];
  601. break;
  602. case BLOCK_COUNT:
  603. data->cb_data_size = (cbw->cb[7]<<8 | cbw->cb[8]) *
  604. data->geometry.bytes_per_sector;
  605. break;
  606. case FIXED:
  607. data->cb_data_size = cmd->data_size;
  608. break;
  609. default:
  610. break;
  611. }
  612. }
  613. else
  614. {
  615. // rt_kprintf("cmd_len error %d\n", cmd->cmd_len);
  616. }
  617. }
  618. static rt_bool_t _cbw_verify(ufunction_t func, struct scsi_cmd* cmd,
  619. ustorage_cbw_t cbw)
  620. {
  621. struct mstorage *data;
  622. RT_ASSERT(cmd != RT_NULL);
  623. RT_ASSERT(cbw != RT_NULL);
  624. RT_ASSERT(func != RT_NULL);
  625. data = (struct mstorage*)func->user_data;
  626. if(cmd->cmd_len != cbw->cb_len)
  627. {
  628. // rt_kprintf("cb_len error\n");
  629. cmd->cmd_len = cbw->cb_len;
  630. }
  631. if(cbw->xfer_len > 0 && data->cb_data_size == 0)
  632. {
  633. rt_kprintf("xfer_len > 0 && data_size == 0\n");
  634. return RT_FALSE;
  635. }
  636. if(cbw->xfer_len == 0 && data->cb_data_size > 0)
  637. {
  638. rt_kprintf("xfer_len == 0 && data_size > 0");
  639. return RT_FALSE;
  640. }
  641. if(((cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_OUT)) ||
  642. (!(cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_IN)))
  643. {
  644. rt_kprintf("dir error\n");
  645. return RT_FALSE;
  646. }
  647. if(cbw->xfer_len > data->cb_data_size)
  648. {
  649. rt_kprintf("xfer_len > data_size\n");
  650. return RT_FALSE;
  651. }
  652. if(cbw->xfer_len < data->cb_data_size)
  653. {
  654. // rt_kprintf("xfer_len < data_size\n");
  655. data->cb_data_size = cbw->xfer_len;
  656. data->csw_response.status = 1;
  657. }
  658. return RT_TRUE;
  659. }
  660. static rt_size_t _cbw_handler(ufunction_t func, struct scsi_cmd* cmd,
  661. ustorage_cbw_t cbw)
  662. {
  663. struct mstorage *data;
  664. RT_ASSERT(func != RT_NULL);
  665. RT_ASSERT(cbw != RT_NULL);
  666. RT_ASSERT(cmd->handler != RT_NULL);
  667. data = (struct mstorage*)func->user_data;
  668. data->processing = cmd;
  669. return cmd->handler(func, cbw);
  670. }
  671. /**
  672. * This function will handle mass storage bulk out endpoint request.
  673. *
  674. * @param func the usb function object.
  675. * @param size request size.
  676. *
  677. * @return RT_EOK.
  678. */
  679. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  680. {
  681. struct mstorage *data;
  682. struct scsi_cmd* cmd;
  683. rt_size_t len;
  684. struct ustorage_cbw* cbw;
  685. RT_ASSERT(func != RT_NULL);
  686. RT_ASSERT(func->device != RT_NULL);
  687. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
  688. data = (struct mstorage*)func->user_data;
  689. cbw = (struct ustorage_cbw*)data->ep_out->buffer;
  690. if(data->status == STAT_CBW)
  691. {
  692. /* dump cbw information */
  693. if(cbw->signature != CBW_SIGNATURE || size != SIZEOF_CBW)
  694. {
  695. goto exit;
  696. }
  697. data->csw_response.signature = CSW_SIGNATURE;
  698. data->csw_response.tag = cbw->tag;
  699. data->csw_response.data_reside = cbw->xfer_len;
  700. data->csw_response.status = 0;
  701. RT_DEBUG_LOG(RT_DEBUG_USB, ("ep_out reside %d\n", data->csw_response.data_reside));
  702. cmd = _find_cbw_command(cbw->cb[0]);
  703. if(cmd == RT_NULL)
  704. {
  705. rt_kprintf("can't find cbw command\n");
  706. goto exit;
  707. }
  708. _cb_len_calc(func, cmd, cbw);
  709. if(!_cbw_verify(func, cmd, cbw))
  710. {
  711. goto exit;
  712. }
  713. len = _cbw_handler(func, cmd, cbw);
  714. if(len == 0)
  715. {
  716. _send_status(func);
  717. }
  718. return RT_EOK;
  719. }
  720. else if(data->status == STAT_RECEIVE)
  721. {
  722. RT_DEBUG_LOG(RT_DEBUG_USB, ("\nwrite size %d block 0x%x oount 0x%x\n",
  723. size, data->block, data->size));
  724. data->size -= size;
  725. data->csw_response.data_reside -= size;
  726. rt_device_write(data->disk, data->block, data->ep_out->buffer, 1);
  727. if(data->csw_response.data_reside != 0)
  728. {
  729. data->ep_out->request.buffer = data->ep_out->buffer;
  730. data->ep_out->request.size = data->geometry.bytes_per_sector;
  731. data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
  732. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  733. data->block ++;
  734. }
  735. else
  736. {
  737. _send_status(func);
  738. }
  739. return RT_EOK;
  740. }
  741. exit:
  742. if(data->csw_response.data_reside)
  743. {
  744. if(cbw->dflags & USB_DIR_IN)
  745. {
  746. rt_usbd_ep_set_stall(func->device, data->ep_in);
  747. }
  748. else
  749. {
  750. rt_usbd_ep_set_stall(func->device, data->ep_in);
  751. rt_usbd_ep_set_stall(func->device, data->ep_out);
  752. }
  753. }
  754. data->csw_response.status = 1;
  755. _send_status(func);
  756. return -RT_ERROR;
  757. }
  758. /**
  759. * This function will handle mass storage interface request.
  760. *
  761. * @param func the usb function object.
  762. * @param setup the setup request.
  763. *
  764. * @return RT_EOK on successful.
  765. */
  766. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  767. {
  768. rt_uint8_t lun = 0;
  769. RT_ASSERT(func != RT_NULL);
  770. RT_ASSERT(func->device != RT_NULL);
  771. RT_ASSERT(setup != RT_NULL);
  772. RT_DEBUG_LOG(RT_DEBUG_USB, ("mstorage_interface_handler\n"));
  773. switch(setup->bRequest)
  774. {
  775. case USBREQ_GET_MAX_LUN:
  776. RT_DEBUG_LOG(RT_DEBUG_USB, ("USBREQ_GET_MAX_LUN\n"));
  777. if(setup->wValue || setup->wLength != 1)
  778. {
  779. rt_usbd_ep0_set_stall(func->device);
  780. }
  781. else
  782. {
  783. rt_usbd_ep0_write(func->device, &lun, setup->wLength);
  784. }
  785. break;
  786. case USBREQ_MASS_STORAGE_RESET:
  787. RT_DEBUG_LOG(RT_DEBUG_USB, ("USBREQ_MASS_STORAGE_RESET\n"));
  788. if(setup->wValue || setup->wLength != 0)
  789. {
  790. rt_usbd_ep0_set_stall(func->device);
  791. }
  792. else
  793. {
  794. dcd_ep0_send_status(func->device->dcd);
  795. }
  796. break;
  797. default:
  798. rt_kprintf("unknown interface request\n");
  799. break;
  800. }
  801. return RT_EOK;
  802. }
  803. /**
  804. * This function will run mass storage function, it will be called on handle set configuration request.
  805. *
  806. * @param func the usb function object.
  807. *
  808. * @return RT_EOK on successful.
  809. */
  810. static rt_err_t _function_enable(ufunction_t func)
  811. {
  812. struct mstorage *data;
  813. RT_ASSERT(func != RT_NULL);
  814. RT_DEBUG_LOG(RT_DEBUG_USB, ("Mass storage function enabled\n"));
  815. data = (struct mstorage*)func->user_data;
  816. data->disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME);
  817. if(data->disk == RT_NULL)
  818. {
  819. rt_kprintf("no data->disk named %s\n", RT_USB_MSTORAGE_DISK_NAME);
  820. return -RT_ERROR;
  821. }
  822. #ifdef RT_USING_DFS_MNTTABLE
  823. dfs_unmount_device(data->disk);
  824. #endif
  825. if(rt_device_open(data->disk, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  826. {
  827. rt_kprintf("disk open error\n");
  828. return -RT_ERROR;
  829. }
  830. if(rt_device_control(data->disk, RT_DEVICE_CTRL_BLK_GETGEOME,
  831. (void*)&data->geometry) != RT_EOK)
  832. {
  833. rt_kprintf("get disk info error\n");
  834. return -RT_ERROR;
  835. }
  836. data->ep_in->buffer = (rt_uint8_t*)rt_malloc(data->geometry.bytes_per_sector);
  837. if(data->ep_in->buffer == RT_NULL)
  838. {
  839. rt_kprintf("no memory\n");
  840. return -RT_ENOMEM;
  841. }
  842. data->ep_out->buffer = (rt_uint8_t*)rt_malloc(data->geometry.bytes_per_sector);
  843. if(data->ep_out->buffer == RT_NULL)
  844. {
  845. rt_free(data->ep_in->buffer);
  846. rt_kprintf("no memory\n");
  847. return -RT_ENOMEM;
  848. }
  849. /* prepare to read CBW request */
  850. data->ep_out->request.buffer = data->ep_out->buffer;
  851. data->ep_out->request.size = SIZEOF_CBW;
  852. data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
  853. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  854. return RT_EOK;
  855. }
  856. /**
  857. * This function will stop mass storage function, it will be called on handle set configuration request.
  858. *
  859. * @param device the usb device object.
  860. *
  861. * @return RT_EOK on successful.
  862. */
  863. static rt_err_t _function_disable(ufunction_t func)
  864. {
  865. struct mstorage *data;
  866. RT_ASSERT(func != RT_NULL);
  867. RT_DEBUG_LOG(RT_DEBUG_USB, ("Mass storage function disabled\n"));
  868. data = (struct mstorage*)func->user_data;
  869. if(data->ep_in->buffer != RT_NULL)
  870. {
  871. rt_free(data->ep_in->buffer);
  872. data->ep_in->buffer = RT_NULL;
  873. }
  874. if(data->ep_out->buffer != RT_NULL)
  875. {
  876. rt_free(data->ep_out->buffer);
  877. data->ep_out->buffer = RT_NULL;
  878. }
  879. if(data->disk != RT_NULL)
  880. {
  881. rt_device_close(data->disk);
  882. #ifdef RT_USING_DFS_MNTTABLE
  883. dfs_mount_device(data->disk);
  884. #endif
  885. data->disk = RT_NULL;
  886. }
  887. data->status = STAT_CBW;
  888. return RT_EOK;
  889. }
  890. static struct ufunction_ops ops =
  891. {
  892. _function_enable,
  893. _function_disable,
  894. RT_NULL,
  895. };
  896. static rt_err_t _mstorage_descriptor_config(umass_desc_t desc, rt_uint8_t cintf_nr, rt_uint8_t device_is_hs)
  897. {
  898. #ifdef RT_USB_DEVICE_COMPOSITE
  899. desc->iad_desc.bFirstInterface = cintf_nr;
  900. #endif
  901. desc->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  902. desc->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  903. return RT_EOK;
  904. }
  905. /**
  906. * This function will create a mass storage function instance.
  907. *
  908. * @param device the usb device object.
  909. *
  910. * @return RT_EOK on successful.
  911. */
  912. ufunction_t rt_usbd_function_mstorage_create(udevice_t device)
  913. {
  914. uintf_t intf;
  915. struct mstorage *data;
  916. ufunction_t func;
  917. ualtsetting_t setting;
  918. umass_desc_t mass_desc;
  919. /* parameter check */
  920. RT_ASSERT(device != RT_NULL);
  921. /* set usb device string description */
  922. rt_usbd_device_set_string(device, _ustring);
  923. /* create a mass storage function */
  924. func = rt_usbd_function_new(device, &dev_desc, &ops);
  925. device->dev_qualifier = &dev_qualifier;
  926. /* allocate memory for mass storage function data */
  927. data = (struct mstorage*)rt_malloc(sizeof(struct mstorage));
  928. rt_memset(data, 0, sizeof(struct mstorage));
  929. func->user_data = (void*)data;
  930. /* create an interface object */
  931. intf = rt_usbd_interface_new(device, _interface_handler);
  932. /* create an alternate setting object */
  933. setting = rt_usbd_altsetting_new(sizeof(struct umass_descriptor));
  934. /* config desc in alternate setting */
  935. rt_usbd_altsetting_config_descriptor(setting, &_mass_desc, (rt_off_t)&((umass_desc_t)0)->intf_desc);
  936. /* configure the msc interface descriptor */
  937. _mstorage_descriptor_config(setting->desc, intf->intf_num, device->dcd->device_is_hs);
  938. /* create a bulk out and a bulk in endpoint */
  939. mass_desc = (umass_desc_t)setting->desc;
  940. data->ep_in = rt_usbd_endpoint_new(&mass_desc->ep_in_desc, _ep_in_handler);
  941. data->ep_out = rt_usbd_endpoint_new(&mass_desc->ep_out_desc, _ep_out_handler);
  942. /* add the bulk out and bulk in endpoint to the alternate setting */
  943. rt_usbd_altsetting_add_endpoint(setting, data->ep_out);
  944. rt_usbd_altsetting_add_endpoint(setting, data->ep_in);
  945. /* add the alternate setting to the interface, then set default setting */
  946. rt_usbd_interface_add_altsetting(intf, setting);
  947. rt_usbd_set_altsetting(intf, 0);
  948. /* add the interface to the mass storage function */
  949. rt_usbd_function_add_interface(func, intf);
  950. return func;
  951. }
  952. struct udclass msc_class =
  953. {
  954. .rt_usbd_function_create = rt_usbd_function_mstorage_create
  955. };
  956. int rt_usbd_msc_class_register(void)
  957. {
  958. rt_usbd_class_register(&msc_class);
  959. return 0;
  960. }
  961. INIT_PREV_EXPORT(rt_usbd_msc_class_register);
  962. #endif