mstorage.c 31 KB

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