mstorage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * File : mstorage.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-10-01 Yi Qiu first version
  13. * 2012-11-25 Heyuanjie87 reduce the memory consumption
  14. * 2012-12-09 Heyuanjie87 change class and endpoint handler
  15. */
  16. #include <rtthread.h>
  17. #include <rtservice.h>
  18. #include <rtdevice.h>
  19. #include "mstorage.h"
  20. #ifdef RT_USB_DEVICE_MSTORAGE
  21. #define STATUS_CBW 0x00
  22. #define STATUS_CSW 0x01
  23. #define STATUS_RECEIVE 0x02
  24. #define STATUS_SEND 0x03
  25. static int status = STATUS_CBW;
  26. ALIGN(RT_ALIGN_SIZE)
  27. static struct ustorage_csw csw;
  28. static rt_device_t disk;
  29. static rt_uint32_t _block;
  30. static rt_uint32_t _count, _size;
  31. static struct rt_device_blk_geometry geometry;
  32. static struct udevice_descriptor dev_desc =
  33. {
  34. USB_DESC_LENGTH_DEVICE, //bLength;
  35. USB_DESC_TYPE_DEVICE, //type;
  36. USB_BCD_VERSION, //bcdUSB;
  37. USB_CLASS_MASS_STORAGE, //bDeviceClass;
  38. 0x00, //bDeviceSubClass;
  39. 0x00, //bDeviceProtocol;
  40. 0x40, //bMaxPacketSize0;
  41. _VENDOR_ID, //idVendor;
  42. _PRODUCT_ID, //idProduct;
  43. USB_BCD_DEVICE, //bcdDevice;
  44. USB_STRING_MANU_INDEX, //iManufacturer;
  45. USB_STRING_PRODUCT_INDEX, //iProduct;
  46. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  47. USB_DYNAMIC, //bNumConfigurations;
  48. };
  49. const static struct umass_descriptor _mass_desc =
  50. {
  51. USB_DESC_LENGTH_INTERFACE, //bLength;
  52. USB_DESC_TYPE_INTERFACE, //type;
  53. USB_DYNAMIC, //bInterfaceNumber;
  54. 0x00, //bAlternateSetting;
  55. 0x02, //bNumEndpoints
  56. USB_CLASS_MASS_STORAGE, //bInterfaceClass;
  57. 0x06, //bInterfaceSubClass;
  58. 0x50, //bInterfaceProtocol;
  59. 0x00, //iInterface;
  60. USB_DESC_LENGTH_ENDPOINT, //bLength;
  61. USB_DESC_TYPE_ENDPOINT, //type;
  62. USB_DYNAMIC | USB_DIR_OUT, //bEndpointAddress;
  63. USB_EP_ATTR_BULK, //bmAttributes;
  64. 0x40, //wMaxPacketSize;
  65. 0x00, //bInterval;
  66. USB_DESC_LENGTH_ENDPOINT, //bLength;
  67. USB_DESC_TYPE_ENDPOINT, //type;
  68. USB_DYNAMIC | USB_DIR_IN, //bEndpointAddress;
  69. USB_EP_ATTR_BULK, //bmAttributes;
  70. 0x40, //wMaxPacketSize;
  71. 0x00, //bInterval;
  72. };
  73. const static char* _ustring[] =
  74. {
  75. "Language",
  76. "RT-Thread Team.",
  77. "RTT Mass Storage",
  78. "1.1.0",
  79. "Configuration",
  80. "Interface",
  81. };
  82. /**
  83. * This function will allocate an usb device instance from system.
  84. *
  85. * @param parent the hub instance to which the new allocated device attached.
  86. * @param port the hub port.
  87. *
  88. * @return the allocate instance on successful, or RT_NULL on failure.
  89. */
  90. static rt_err_t _inquiry_cmd(udevice_t device, uep_t ep_in)
  91. {
  92. rt_uint8_t data[36];
  93. *(rt_uint32_t*)&data[0] = 0x0 | (0x80 << 8);
  94. *(rt_uint32_t*)&data[4] = 31;
  95. rt_memset(&data[8], 0x20, 28);
  96. rt_memcpy(&data[8], "RTT", 3);
  97. rt_memcpy(&data[16], "USB Disk", 8);
  98. dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 36);
  99. return RT_EOK;
  100. }
  101. /**
  102. * This function will handle sense request.
  103. *
  104. * @param device the usb device object.
  105. *
  106. * @return RT_EOK on successful.
  107. */
  108. static rt_err_t _request_sense(udevice_t device, uep_t ep_in)
  109. {
  110. struct request_sense_data data;
  111. data.ErrorCode = 0x70;
  112. data.Valid = 0;
  113. data.SenseKey = 5;
  114. data.Information[0] = 0;
  115. data.Information[1] = 0;
  116. data.Information[2] = 0;
  117. data.Information[3] = 0;
  118. data.AdditionalSenseLength = 0x0b;
  119. data.AdditionalSenseCode = 0x20;
  120. data.AdditionalSenseCodeQualifier =0;
  121. dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, sizeof(struct request_sense_data));
  122. return RT_EOK;
  123. }
  124. /**
  125. * This function will handle mode_sense_6 request.
  126. *
  127. * @param device the usb device object.
  128. *
  129. * @return RT_EOK on successful.
  130. */
  131. static rt_err_t _mode_sense_6(udevice_t device, uep_t ep_in)
  132. {
  133. rt_uint8_t data[4];
  134. data[0]=3;
  135. data[1]=0;
  136. data[2]=0;
  137. data[3]=0;
  138. dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 4);
  139. return RT_EOK;
  140. }
  141. /**
  142. * This function will handle read_capacities request.
  143. *
  144. * @param device the usb device object.
  145. *
  146. * @return RT_EOK on successful.
  147. */
  148. static rt_err_t _read_capacities(udevice_t device, uep_t ep_in)
  149. {
  150. rt_uint8_t data[12];
  151. rt_uint32_t sector_count, sector_size;
  152. RT_ASSERT(device != RT_NULL);
  153. sector_count = geometry.sector_count;
  154. sector_size = geometry.bytes_per_sector;
  155. *(rt_uint32_t*)&data[0] = 0x08000000;
  156. data[4] = sector_count >> 24;
  157. data[5] = 0xff & (sector_count >> 16);
  158. data[6] = 0xff & (sector_count >> 8);
  159. data[7] = 0xff & (sector_count);
  160. data[8] = 0x02;
  161. data[9] = 0xff & (sector_size >> 16);
  162. data[10] = 0xff & (sector_size >> 8);
  163. data[11] = 0xff & sector_size;
  164. dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 12);
  165. return RT_EOK;
  166. }
  167. /**
  168. * This function will handle read_capacity request.
  169. *
  170. * @param device the usb device object.
  171. *
  172. * @return RT_EOK on successful.
  173. */
  174. static rt_err_t _read_capacity(udevice_t device, uep_t ep_in)
  175. {
  176. rt_uint8_t data[8];
  177. rt_uint32_t sector_count, sector_size;
  178. RT_ASSERT(device != RT_NULL);
  179. sector_count = geometry.sector_count;
  180. sector_size = geometry.bytes_per_sector;
  181. data[0] = sector_count >> 24;
  182. data[1] = 0xff & (sector_count >> 16);
  183. data[2] = 0xff & (sector_count >> 8);
  184. data[3] = 0xff & (sector_count);
  185. data[4] = 0x0;
  186. data[5] = 0xff & (sector_size >> 16);
  187. data[6] = 0xff & (sector_size >> 8);
  188. data[7] = 0xff & sector_size;
  189. dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 8);
  190. return RT_EOK;
  191. }
  192. /**
  193. * This function will handle read_10 request.
  194. *
  195. * @param device the usb device object.
  196. * @param cbw the command block wrapper.
  197. *
  198. * @return RT_EOK on successful.
  199. */
  200. static rt_err_t _read_10(udevice_t device, ustorage_cbw_t cbw, uep_t ep_in)
  201. {
  202. RT_ASSERT(device != RT_NULL);
  203. RT_ASSERT(cbw != RT_NULL);
  204. _block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
  205. cbw->cb[5]<<0 ;
  206. _count = cbw->cb[7]<<8 | cbw->cb[8]<<0 ;
  207. RT_ASSERT(_count < geometry.sector_count);
  208. rt_device_read(disk, _block, ep_in->buffer, 1);
  209. dcd_ep_write(device->dcd, ep_in, ep_in->buffer, geometry.bytes_per_sector);
  210. _count --;
  211. if (_count)
  212. {
  213. _block ++;
  214. status = STATUS_SEND;
  215. }
  216. else
  217. {
  218. status = STATUS_CSW;
  219. }
  220. return RT_EOK;
  221. }
  222. /**
  223. * This function will handle write_10 request.
  224. *
  225. * @param device the usb device object.
  226. * @param cbw the command block wrapper.
  227. *
  228. * @return RT_EOK on successful.
  229. */
  230. static rt_err_t _write_10(udevice_t device, ustorage_cbw_t cbw, uep_t ep_out)
  231. {
  232. RT_ASSERT(device != RT_NULL);
  233. RT_ASSERT(cbw != RT_NULL);
  234. _block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
  235. cbw->cb[5]<<0 ;
  236. _count = cbw->cb[7]<<8 | cbw->cb[8]<<0;
  237. csw.data_reside = cbw->xfer_len;
  238. _size = _count * geometry.bytes_per_sector;
  239. RT_DEBUG_LOG(RT_DEBUG_USB, ("_write_10 count 0x%x 0x%x\n",
  240. _count, geometry.sector_count));
  241. dcd_ep_read(device->dcd, ep_out, ep_out->buffer, geometry.bytes_per_sector);
  242. return RT_EOK;
  243. }
  244. /**
  245. * This function will handle verify_10 request.
  246. *
  247. * @param device the usb device object.
  248. *
  249. * @return RT_EOK on successful.
  250. */
  251. static rt_err_t _verify_10(udevice_t device)
  252. {
  253. return RT_EOK;
  254. }
  255. /**
  256. * This function will handle mass storage bulk in endpoint request.
  257. *
  258. * @param device the usb device object.
  259. * @param size request size.
  260. *
  261. * @return RT_EOK.
  262. */
  263. static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
  264. {
  265. mass_eps_t eps;
  266. RT_ASSERT(device != RT_NULL);
  267. eps = cls->eps;
  268. if(status == STATUS_CSW)
  269. {
  270. dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
  271. status = STATUS_CBW;
  272. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
  273. }
  274. if(status == STATUS_SEND)
  275. {
  276. rt_device_read(disk, _block, eps->ep_in->buffer, 1);
  277. dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer,
  278. geometry.bytes_per_sector);
  279. _count --;
  280. if (_count)
  281. {
  282. _block ++;
  283. status = STATUS_SEND;
  284. }
  285. else
  286. {
  287. status = STATUS_CSW;
  288. }
  289. }
  290. return RT_EOK;
  291. }
  292. #ifdef MASS_CBW_DUMP
  293. static void cbw_dump(struct ustorage_cbw* cbw)
  294. {
  295. RT_ASSERT(cbw != RT_NULL);
  296. RT_DEBUG_LOG(RT_DEBUG_USB, ("signature 0x%x\n", cbw->signature));
  297. RT_DEBUG_LOG(RT_DEBUG_USB, ("tag 0x%x\n", cbw->tag));
  298. RT_DEBUG_LOG(RT_DEBUG_USB, ("xfer_len 0x%x\n", cbw->xfer_len));
  299. RT_DEBUG_LOG(RT_DEBUG_USB, ("dflags 0x%x\n", cbw->dflags));
  300. RT_DEBUG_LOG(RT_DEBUG_USB, ("lun 0x%x\n", cbw->lun));
  301. RT_DEBUG_LOG(RT_DEBUG_USB, ("cb_len 0x%x\n", cbw->cb_len));
  302. RT_DEBUG_LOG(RT_DEBUG_USB, ("cb[0] 0x%x\n", cbw->cb[0]));
  303. }
  304. #endif
  305. /**
  306. * This function will handle mass storage bulk out endpoint request.
  307. *
  308. * @param device the usb device object.
  309. * @param size request size.
  310. *
  311. * @return RT_EOK.
  312. */
  313. static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
  314. {
  315. mass_eps_t eps;
  316. RT_ASSERT(device != RT_NULL);
  317. eps = (mass_eps_t)cls->eps;
  318. if(status == STATUS_CBW)
  319. {
  320. struct ustorage_cbw* cbw;
  321. /* dump cbw information */
  322. cbw = (struct ustorage_cbw*)eps->ep_out->buffer;
  323. if(cbw->signature == CBW_SIGNATURE)
  324. {
  325. csw.signature = CSW_SIGNATURE;
  326. csw.tag = cbw->tag;
  327. csw.data_reside = 0;
  328. csw.status = 0;
  329. }
  330. else
  331. return -RT_ERROR;
  332. switch(cbw->cb[0])
  333. {
  334. case SCSI_TEST_UNIT_READY:
  335. dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
  336. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
  337. break;
  338. case SCSI_REQUEST_SENSE:
  339. _request_sense(device, eps->ep_in);
  340. status = STATUS_CSW;
  341. break;
  342. case SCSI_INQUIRY_CMD:
  343. _inquiry_cmd(device, eps->ep_in);
  344. status = STATUS_CSW;
  345. break;
  346. case SCSI_MODE_SENSE_6:
  347. _mode_sense_6(device, eps->ep_in);
  348. status = STATUS_CSW;
  349. break;
  350. case SCSI_ALLOW_MEDIUM_REMOVAL:
  351. dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
  352. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
  353. break;
  354. case SCSI_READ_CAPACITIES:
  355. _read_capacities(device, eps->ep_in);
  356. status = STATUS_CSW;
  357. break;
  358. case SCSI_READ_CAPACITY:
  359. _read_capacity(device, eps->ep_in);
  360. status = STATUS_CSW;
  361. break;
  362. case SCSI_READ_10:
  363. _read_10(device, cbw, eps->ep_in);
  364. break;
  365. case SCSI_WRITE_10:
  366. _write_10(device, cbw, eps->ep_out);
  367. status = STATUS_RECEIVE;
  368. break;
  369. case SCSI_VERIFY_10:
  370. _verify_10(device);
  371. break;
  372. }
  373. }
  374. else if(status == STATUS_RECEIVE)
  375. {
  376. RT_DEBUG_LOG(RT_DEBUG_USB, ("write size 0x%x block 0x%x oount 0x%x\n",
  377. size, _block, _size));
  378. _size -= size;
  379. csw.data_reside -= size;
  380. rt_device_write(disk, _block, eps->ep_in->buffer, 1);
  381. _block ++;
  382. if(_size == 0)
  383. {
  384. dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
  385. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
  386. status = STATUS_CBW;
  387. }
  388. else
  389. {
  390. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
  391. geometry.bytes_per_sector);
  392. }
  393. }
  394. else
  395. {
  396. rt_kprintf("none cbw status\n");
  397. }
  398. return RT_EOK;
  399. }
  400. /**
  401. * This function will handle mass storage interface request.
  402. *
  403. * @param device the usb device object.
  404. * @param setup the setup request.
  405. *
  406. * @return RT_EOK on successful.
  407. */
  408. static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
  409. {
  410. rt_uint8_t lun = 0;
  411. RT_ASSERT(device != RT_NULL);
  412. RT_ASSERT(setup != RT_NULL);
  413. RT_DEBUG_LOG(RT_DEBUG_USB, ("_interface_handler\n"));
  414. switch(setup->request)
  415. {
  416. case USBREQ_GET_MAX_LUN:
  417. dcd_ep_write(device->dcd, 0, &lun, 1);
  418. break;
  419. case USBREQ_MASS_STORAGE_RESET:
  420. break;
  421. default:
  422. rt_kprintf("unknown interface request\n");
  423. break;
  424. }
  425. return RT_EOK;
  426. }
  427. /**
  428. * This function will run mass storage class, it will be called on handle set configuration request.
  429. *
  430. * @param device the usb device object.
  431. *
  432. * @return RT_EOK on successful.
  433. */
  434. static rt_err_t _class_run(udevice_t device, uclass_t cls)
  435. {
  436. mass_eps_t eps;
  437. rt_uint8_t *buffer;
  438. RT_ASSERT(device != RT_NULL);
  439. RT_DEBUG_LOG(RT_DEBUG_USB, ("mass storage run\n"));
  440. eps = (mass_eps_t)cls->eps;
  441. disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME);
  442. if(disk == RT_NULL)
  443. {
  444. rt_kprintf("no disk named %s\n", RT_USB_MSTORAGE_DISK_NAME);
  445. return -RT_ERROR;
  446. }
  447. if(rt_device_control(disk, RT_DEVICE_CTRL_BLK_GETGEOME, (void*)&geometry) != RT_EOK)
  448. return -RT_ERROR;
  449. buffer = (rt_uint8_t*)rt_malloc(geometry.bytes_per_sector);
  450. if(buffer == RT_NULL)
  451. return -RT_ENOMEM;
  452. eps->ep_out->buffer = buffer;
  453. eps->ep_in->buffer = buffer;
  454. dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
  455. return RT_EOK;
  456. }
  457. /**
  458. * This function will stop mass storage class, it will be called on handle set configuration request.
  459. *
  460. * @param device the usb device object.
  461. *
  462. * @return RT_EOK on successful.
  463. */
  464. static rt_err_t _class_stop(udevice_t device, uclass_t cls)
  465. {
  466. mass_eps_t eps;
  467. RT_ASSERT(device != RT_NULL);
  468. RT_DEBUG_LOG(RT_DEBUG_USB, ("mass storage stop\n"));
  469. eps = (mass_eps_t)cls->eps;
  470. rt_free(eps->ep_in->buffer);
  471. eps->ep_out->buffer = RT_NULL;
  472. eps->ep_in->buffer = RT_NULL;
  473. return RT_EOK;
  474. }
  475. static struct uclass_ops ops =
  476. {
  477. _class_run,
  478. _class_stop,
  479. RT_NULL,
  480. };
  481. /**
  482. * This function will create a mass storage class instance.
  483. *
  484. * @param device the usb device object.
  485. *
  486. * @return RT_EOK on successful.
  487. */
  488. uclass_t rt_usbd_class_mstorage_create(udevice_t device)
  489. {
  490. uintf_t intf;
  491. mass_eps_t eps;
  492. uclass_t mstorage;
  493. ualtsetting_t setting;
  494. umass_desc_t mass_desc;
  495. /* parameter check */
  496. RT_ASSERT(device != RT_NULL);
  497. /* set usb device string description */
  498. rt_usbd_device_set_string(device, _ustring);
  499. /* create a mass storage class */
  500. mstorage = rt_usbd_class_create(device, &dev_desc, &ops);
  501. /* create a mass storage endpoints collection */
  502. eps = (mass_eps_t)rt_malloc(sizeof(struct mass_eps));
  503. mstorage->eps = (void*)eps;
  504. /* create an interface */
  505. intf = rt_usbd_interface_create(device, _interface_handler);
  506. /* create an alternate setting */
  507. setting = rt_usbd_altsetting_create(sizeof(struct umass_descriptor));
  508. /* config desc in alternate setting */
  509. rt_usbd_altsetting_config_descriptor(setting, &_mass_desc, 0);
  510. /* create a bulk out and a bulk in endpoint */
  511. mass_desc = (umass_desc_t)setting->desc;
  512. eps->ep_in = rt_usbd_endpoint_create(&mass_desc->ep_in_desc, _ep_in_handler);
  513. eps->ep_out = rt_usbd_endpoint_create(&mass_desc->ep_out_desc, _ep_out_handler);
  514. /* add the bulk out and bulk in endpoint to the alternate setting */
  515. rt_usbd_altsetting_add_endpoint(setting, eps->ep_out);
  516. rt_usbd_altsetting_add_endpoint(setting, eps->ep_in);
  517. /* add the alternate setting to the interface, then set default setting */
  518. rt_usbd_interface_add_altsetting(intf, setting);
  519. rt_usbd_set_altsetting(intf, 0);
  520. /* add the interface to the mass storage class */
  521. rt_usbd_class_add_interface(mstorage, intf);
  522. return mstorage;
  523. }
  524. #endif