mstorage.c 17 KB

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