mstorage.c 32 KB

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