mstorage.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. USB_CLASS_MASS_STORAGE, //bDeviceClass;
  72. 0x06, //bDeviceSubClass;
  73. 0x50, //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. USB_CLASS_MASS_STORAGE, //bDeviceClass
  91. 0x06, //bDeviceSubClass
  92. 0x50, //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_kprintf("warning:in stall path but not stall\n");
  516. /* FIXME: Disable the operation or the disk cannot work. */
  517. //rt_usbd_ep_set_stall(func->device, data->ep_in);
  518. }
  519. data->csw_response.data_reside = 0;
  520. }
  521. }
  522. _send_status(func);
  523. break;
  524. case STAT_SEND:
  525. data->csw_response.data_reside -= data->ep_in->request.size;
  526. data->count--;
  527. data->block++;
  528. if(data->count > 0 && data->csw_response.data_reside > 0)
  529. {
  530. if(rt_device_read(data->disk, data->block, data->ep_in->buffer, 1) == 0)
  531. {
  532. rt_kprintf("disk read error\n");
  533. rt_usbd_ep_set_stall(func->device, data->ep_in);
  534. return -RT_ERROR;
  535. }
  536. data->ep_in->request.buffer = data->ep_in->buffer;
  537. data->ep_in->request.size = data->geometry.bytes_per_sector;
  538. data->ep_in->request.req_type = UIO_REQUEST_WRITE;
  539. rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
  540. }
  541. else
  542. {
  543. _send_status(func);
  544. }
  545. break;
  546. }
  547. return RT_EOK;
  548. }
  549. #ifdef MASS_CBW_DUMP
  550. static void cbw_dump(struct ustorage_cbw* cbw)
  551. {
  552. RT_ASSERT(cbw != RT_NULL);
  553. RT_DEBUG_LOG(RT_DEBUG_USB, ("signature 0x%x\n", cbw->signature));
  554. RT_DEBUG_LOG(RT_DEBUG_USB, ("tag 0x%x\n", cbw->tag));
  555. RT_DEBUG_LOG(RT_DEBUG_USB, ("xfer_len 0x%x\n", cbw->xfer_len));
  556. RT_DEBUG_LOG(RT_DEBUG_USB, ("dflags 0x%x\n", cbw->dflags));
  557. RT_DEBUG_LOG(RT_DEBUG_USB, ("lun 0x%x\n", cbw->lun));
  558. RT_DEBUG_LOG(RT_DEBUG_USB, ("cb_len 0x%x\n", cbw->cb_len));
  559. RT_DEBUG_LOG(RT_DEBUG_USB, ("cb[0] 0x%x\n", cbw->cb[0]));
  560. }
  561. #endif
  562. static struct scsi_cmd* _find_cbw_command(rt_uint16_t cmd)
  563. {
  564. int i;
  565. for(i=0; i<sizeof(cmd_data)/sizeof(struct scsi_cmd); i++)
  566. {
  567. if(cmd_data[i].cmd == cmd)
  568. return &cmd_data[i];
  569. }
  570. return RT_NULL;
  571. }
  572. static void _cb_len_calc(ufunction_t func, struct scsi_cmd* cmd,
  573. ustorage_cbw_t cbw)
  574. {
  575. struct mstorage *data;
  576. RT_ASSERT(func != RT_NULL);
  577. RT_ASSERT(cmd != RT_NULL);
  578. RT_ASSERT(cbw != RT_NULL);
  579. data = (struct mstorage*)func->user_data;
  580. if(cmd->cmd_len == 6)
  581. {
  582. switch(cmd->type)
  583. {
  584. case COUNT:
  585. data->cb_data_size = cbw->cb[4];
  586. break;
  587. case BLOCK_COUNT:
  588. data->cb_data_size = cbw->cb[4] * data->geometry.bytes_per_sector;
  589. break;
  590. case FIXED:
  591. data->cb_data_size = cmd->data_size;
  592. break;
  593. default:
  594. break;
  595. }
  596. }
  597. else if(cmd->cmd_len == 10)
  598. {
  599. switch(cmd->type)
  600. {
  601. case COUNT:
  602. data->cb_data_size = cbw->cb[7]<<8 | cbw->cb[8];
  603. break;
  604. case BLOCK_COUNT:
  605. data->cb_data_size = (cbw->cb[7]<<8 | cbw->cb[8]) *
  606. data->geometry.bytes_per_sector;
  607. break;
  608. case FIXED:
  609. data->cb_data_size = cmd->data_size;
  610. break;
  611. default:
  612. break;
  613. }
  614. }
  615. //workaround: for stability in full-speed mode
  616. else if(cmd->cmd_len == 12)
  617. {
  618. switch(cmd->type)
  619. {
  620. case COUNT:
  621. data->cb_data_size = cbw->cb[4];
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. else
  628. {
  629. rt_kprintf("cmd_len error %d\n", cmd->cmd_len);
  630. }
  631. }
  632. static rt_bool_t _cbw_verify(ufunction_t func, struct scsi_cmd* cmd,
  633. ustorage_cbw_t cbw)
  634. {
  635. struct mstorage *data;
  636. RT_ASSERT(cmd != RT_NULL);
  637. RT_ASSERT(cbw != RT_NULL);
  638. RT_ASSERT(func != RT_NULL);
  639. data = (struct mstorage*)func->user_data;
  640. if(cmd->cmd_len != cbw->cb_len)
  641. {
  642. rt_kprintf("cb_len error\n");
  643. cmd->cmd_len = cbw->cb_len;
  644. }
  645. if(cbw->xfer_len > 0 && data->cb_data_size == 0)
  646. {
  647. rt_kprintf("xfer_len > 0 && data_size == 0\n");
  648. return RT_FALSE;
  649. }
  650. if(cbw->xfer_len == 0 && data->cb_data_size > 0)
  651. {
  652. rt_kprintf("xfer_len == 0 && data_size > 0");
  653. return RT_FALSE;
  654. }
  655. if(((cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_OUT)) ||
  656. (!(cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_IN)))
  657. {
  658. rt_kprintf("dir error\n");
  659. return RT_FALSE;
  660. }
  661. if(cbw->xfer_len > data->cb_data_size)
  662. {
  663. rt_kprintf("xfer_len > data_size\n");
  664. return RT_FALSE;
  665. }
  666. if(cbw->xfer_len < data->cb_data_size)
  667. {
  668. rt_kprintf("xfer_len < data_size\n");
  669. data->cb_data_size = cbw->xfer_len;
  670. data->csw_response.status = 1;
  671. }
  672. return RT_TRUE;
  673. }
  674. static rt_size_t _cbw_handler(ufunction_t func, struct scsi_cmd* cmd,
  675. ustorage_cbw_t cbw)
  676. {
  677. struct mstorage *data;
  678. RT_ASSERT(func != RT_NULL);
  679. RT_ASSERT(cbw != RT_NULL);
  680. RT_ASSERT(cmd->handler != RT_NULL);
  681. data = (struct mstorage*)func->user_data;
  682. data->processing = cmd;
  683. return cmd->handler(func, cbw);
  684. }
  685. /**
  686. * This function will handle mass storage bulk out endpoint request.
  687. *
  688. * @param func the usb function object.
  689. * @param size request size.
  690. *
  691. * @return RT_EOK.
  692. */
  693. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  694. {
  695. struct mstorage *data;
  696. struct scsi_cmd* cmd;
  697. rt_size_t len;
  698. struct ustorage_cbw* cbw;
  699. RT_ASSERT(func != RT_NULL);
  700. RT_ASSERT(func->device != RT_NULL);
  701. RT_DEBUG_LOG(RT_DEBUG_USB, ("_ep_out_handler %d\n", size));
  702. data = (struct mstorage*)func->user_data;
  703. cbw = (struct ustorage_cbw*)data->ep_out->buffer;
  704. if(data->status == STAT_CBW)
  705. {
  706. /* dump cbw information */
  707. if(cbw->signature != CBW_SIGNATURE || size != SIZEOF_CBW)
  708. {
  709. goto exit;
  710. }
  711. data->csw_response.signature = CSW_SIGNATURE;
  712. data->csw_response.tag = cbw->tag;
  713. data->csw_response.data_reside = cbw->xfer_len;
  714. data->csw_response.status = 0;
  715. RT_DEBUG_LOG(RT_DEBUG_USB, ("ep_out reside %d\n", data->csw_response.data_reside));
  716. cmd = _find_cbw_command(cbw->cb[0]);
  717. if(cmd == RT_NULL)
  718. {
  719. rt_kprintf("can't find cbw command\n");
  720. goto exit;
  721. }
  722. _cb_len_calc(func, cmd, cbw);
  723. if(!_cbw_verify(func, cmd, cbw))
  724. {
  725. goto exit;
  726. }
  727. len = _cbw_handler(func, cmd, cbw);
  728. if(len == 0)
  729. {
  730. _send_status(func);
  731. }
  732. return RT_EOK;
  733. }
  734. else if(data->status == STAT_RECEIVE)
  735. {
  736. RT_DEBUG_LOG(RT_DEBUG_USB, ("\nwrite size %d block 0x%x oount 0x%x\n",
  737. size, data->block, data->size));
  738. data->size -= size;
  739. data->csw_response.data_reside -= size;
  740. rt_device_write(data->disk, data->block, data->ep_out->buffer, 1);
  741. if(data->csw_response.data_reside != 0)
  742. {
  743. data->ep_out->request.buffer = data->ep_out->buffer;
  744. data->ep_out->request.size = data->geometry.bytes_per_sector;
  745. data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
  746. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  747. data->block ++;
  748. }
  749. else
  750. {
  751. _send_status(func);
  752. }
  753. return RT_EOK;
  754. }
  755. exit:
  756. if(data->csw_response.data_reside)
  757. {
  758. if(cbw->dflags & USB_DIR_IN)
  759. {
  760. rt_usbd_ep_set_stall(func->device, data->ep_in);
  761. }
  762. else
  763. {
  764. rt_usbd_ep_set_stall(func->device, data->ep_in);
  765. rt_usbd_ep_set_stall(func->device, data->ep_out);
  766. }
  767. }
  768. data->csw_response.status = 1;
  769. _send_status(func);
  770. return -RT_ERROR;
  771. }
  772. /**
  773. * This function will handle mass storage interface request.
  774. *
  775. * @param func the usb function object.
  776. * @param setup the setup request.
  777. *
  778. * @return RT_EOK on successful.
  779. */
  780. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  781. {
  782. rt_uint8_t lun = 0;
  783. RT_ASSERT(func != RT_NULL);
  784. RT_ASSERT(func->device != RT_NULL);
  785. RT_ASSERT(setup != RT_NULL);
  786. RT_DEBUG_LOG(RT_DEBUG_USB, ("mstorage_interface_handler\n"));
  787. switch(setup->bRequest)
  788. {
  789. case USBREQ_GET_MAX_LUN:
  790. RT_DEBUG_LOG(RT_DEBUG_USB, ("USBREQ_GET_MAX_LUN\n"));
  791. if(setup->wValue || setup->wLength != 1)
  792. {
  793. rt_usbd_ep0_set_stall(func->device);
  794. }
  795. else
  796. {
  797. rt_usbd_ep0_write(func->device, &lun, setup->wLength);
  798. }
  799. break;
  800. case USBREQ_MASS_STORAGE_RESET:
  801. RT_DEBUG_LOG(RT_DEBUG_USB, ("USBREQ_MASS_STORAGE_RESET\n"));
  802. if(setup->wValue || setup->wLength != 0)
  803. {
  804. rt_usbd_ep0_set_stall(func->device);
  805. }
  806. else
  807. {
  808. dcd_ep0_send_status(func->device->dcd);
  809. }
  810. break;
  811. default:
  812. rt_kprintf("unknown interface request\n");
  813. break;
  814. }
  815. return RT_EOK;
  816. }
  817. /**
  818. * This function will run mass storage function, it will be called on handle set configuration request.
  819. *
  820. * @param func the usb function object.
  821. *
  822. * @return RT_EOK on successful.
  823. */
  824. static rt_err_t _function_enable(ufunction_t func)
  825. {
  826. struct mstorage *data;
  827. RT_ASSERT(func != RT_NULL);
  828. RT_DEBUG_LOG(RT_DEBUG_USB, ("Mass storage function enabled\n"));
  829. data = (struct mstorage*)func->user_data;
  830. data->disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME);
  831. if(data->disk == RT_NULL)
  832. {
  833. rt_kprintf("no data->disk named %s\n", RT_USB_MSTORAGE_DISK_NAME);
  834. return -RT_ERROR;
  835. }
  836. #ifdef RT_USING_DFS_MNTTABLE
  837. dfs_unmount_device(data->disk);
  838. #endif
  839. if(rt_device_open(data->disk, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  840. {
  841. rt_kprintf("disk open error\n");
  842. return -RT_ERROR;
  843. }
  844. if(rt_device_control(data->disk, RT_DEVICE_CTRL_BLK_GETGEOME,
  845. (void*)&data->geometry) != RT_EOK)
  846. {
  847. rt_kprintf("get disk info error\n");
  848. return -RT_ERROR;
  849. }
  850. data->ep_in->buffer = (rt_uint8_t*)rt_malloc(data->geometry.bytes_per_sector);
  851. if(data->ep_in->buffer == RT_NULL)
  852. {
  853. rt_kprintf("no memory\n");
  854. return -RT_ENOMEM;
  855. }
  856. data->ep_out->buffer = (rt_uint8_t*)rt_malloc(data->geometry.bytes_per_sector);
  857. if(data->ep_out->buffer == RT_NULL)
  858. {
  859. rt_free(data->ep_in->buffer);
  860. rt_kprintf("no memory\n");
  861. return -RT_ENOMEM;
  862. }
  863. /* prepare to read CBW request */
  864. data->ep_out->request.buffer = data->ep_out->buffer;
  865. data->ep_out->request.size = SIZEOF_CBW;
  866. data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
  867. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  868. return RT_EOK;
  869. }
  870. /**
  871. * This function will stop mass storage function, it will be called on handle set configuration request.
  872. *
  873. * @param device the usb device object.
  874. *
  875. * @return RT_EOK on successful.
  876. */
  877. static rt_err_t _function_disable(ufunction_t func)
  878. {
  879. struct mstorage *data;
  880. RT_ASSERT(func != RT_NULL);
  881. RT_DEBUG_LOG(RT_DEBUG_USB, ("Mass storage function disabled\n"));
  882. data = (struct mstorage*)func->user_data;
  883. if(data->ep_in->buffer != RT_NULL)
  884. {
  885. rt_free(data->ep_in->buffer);
  886. data->ep_in->buffer = RT_NULL;
  887. }
  888. if(data->ep_out->buffer != RT_NULL)
  889. {
  890. rt_free(data->ep_out->buffer);
  891. data->ep_out->buffer = RT_NULL;
  892. }
  893. if(data->disk != RT_NULL)
  894. {
  895. rt_device_close(data->disk);
  896. #ifdef RT_USING_DFS_MNTTABLE
  897. dfs_mount_device(data->disk);
  898. #endif
  899. data->disk = RT_NULL;
  900. }
  901. data->status = STAT_CBW;
  902. return RT_EOK;
  903. }
  904. static struct ufunction_ops ops =
  905. {
  906. _function_enable,
  907. _function_disable,
  908. RT_NULL,
  909. };
  910. static rt_err_t _mstorage_descriptor_config(umass_desc_t desc, rt_uint8_t cintf_nr, rt_uint8_t device_is_hs)
  911. {
  912. #ifdef RT_USB_DEVICE_COMPOSITE
  913. desc->iad_desc.bFirstInterface = cintf_nr;
  914. #endif
  915. desc->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  916. desc->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
  917. return RT_EOK;
  918. }
  919. /**
  920. * This function will create a mass storage function instance.
  921. *
  922. * @param device the usb device object.
  923. *
  924. * @return RT_EOK on successful.
  925. */
  926. ufunction_t rt_usbd_function_mstorage_create(udevice_t device)
  927. {
  928. uintf_t intf;
  929. struct mstorage *data;
  930. ufunction_t func;
  931. ualtsetting_t setting;
  932. umass_desc_t mass_desc;
  933. /* parameter check */
  934. RT_ASSERT(device != RT_NULL);
  935. /* set usb device string description */
  936. rt_usbd_device_set_string(device, _ustring);
  937. /* create a mass storage function */
  938. func = rt_usbd_function_new(device, &dev_desc, &ops);
  939. device->dev_qualifier = &dev_qualifier;
  940. /* allocate memory for mass storage function data */
  941. data = (struct mstorage*)rt_malloc(sizeof(struct mstorage));
  942. rt_memset(data, 0, sizeof(struct mstorage));
  943. func->user_data = (void*)data;
  944. /* create an interface object */
  945. intf = rt_usbd_interface_new(device, _interface_handler);
  946. /* create an alternate setting object */
  947. setting = rt_usbd_altsetting_new(sizeof(struct umass_descriptor));
  948. /* config desc in alternate setting */
  949. rt_usbd_altsetting_config_descriptor(setting, &_mass_desc, (rt_off_t)&((umass_desc_t)0)->intf_desc);
  950. /* configure the msc interface descriptor */
  951. _mstorage_descriptor_config(setting->desc, intf->intf_num, device->dcd->device_is_hs);
  952. /* create a bulk out and a bulk in endpoint */
  953. mass_desc = (umass_desc_t)setting->desc;
  954. data->ep_in = rt_usbd_endpoint_new(&mass_desc->ep_in_desc, _ep_in_handler);
  955. data->ep_out = rt_usbd_endpoint_new(&mass_desc->ep_out_desc, _ep_out_handler);
  956. /* add the bulk out and bulk in endpoint to the alternate setting */
  957. rt_usbd_altsetting_add_endpoint(setting, data->ep_out);
  958. rt_usbd_altsetting_add_endpoint(setting, data->ep_in);
  959. /* add the alternate setting to the interface, then set default setting */
  960. rt_usbd_interface_add_altsetting(intf, setting);
  961. rt_usbd_set_altsetting(intf, 0);
  962. /* add the interface to the mass storage function */
  963. rt_usbd_function_add_interface(func, intf);
  964. return func;
  965. }
  966. struct udclass msc_class =
  967. {
  968. .rt_usbd_function_create = rt_usbd_function_mstorage_create
  969. };
  970. int rt_usbd_msc_class_register(void)
  971. {
  972. rt_usbd_class_register(&msc_class);
  973. return 0;
  974. }
  975. INIT_PREV_EXPORT(rt_usbd_msc_class_register);
  976. #endif