udisk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-12-12 Yi Qiu first version
  9. */
  10. #include <rtthread.h>
  11. #include <dfs_fs.h>
  12. #include <drivers/usb_host.h>
  13. #include "mass.h"
  14. #ifdef RT_USBH_MSTORAGE
  15. #define UDISK_MAX_COUNT 8
  16. static rt_uint8_t _udisk_idset = 0;
  17. static int udisk_get_id(void)
  18. {
  19. int i;
  20. for(i=0; i< UDISK_MAX_COUNT; i++)
  21. {
  22. if((_udisk_idset & (1 << i)) != 0) continue;
  23. else break;
  24. }
  25. /* it should not happen */
  26. if(i == UDISK_MAX_COUNT) RT_ASSERT(0);
  27. _udisk_idset |= (1 << i);
  28. return i;
  29. }
  30. static void udisk_free_id(int id)
  31. {
  32. RT_ASSERT(id < UDISK_MAX_COUNT)
  33. _udisk_idset &= ~(1 << id);
  34. }
  35. /**
  36. * This function will initialize the udisk device
  37. *
  38. * @param dev the pointer of device driver structure
  39. *
  40. * @return RT_EOK
  41. */
  42. static rt_err_t rt_udisk_init(rt_device_t dev)
  43. {
  44. return RT_EOK;
  45. }
  46. /**
  47. * This function will read some data from a device.
  48. *
  49. * @param dev the pointer of device driver structure
  50. * @param pos the position of reading
  51. * @param buffer the data buffer to save read data
  52. * @param size the size of buffer
  53. *
  54. * @return the actually read size on successful, otherwise negative returned.
  55. */
  56. static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer,
  57. rt_size_t size)
  58. {
  59. rt_err_t ret;
  60. struct uhintf* intf;
  61. struct ustor_data* data;
  62. int timeout = USB_TIMEOUT_LONG;
  63. /* check parameter */
  64. RT_ASSERT(dev != RT_NULL);
  65. RT_ASSERT(buffer != RT_NULL);
  66. if(size > 4096) timeout *= 2;
  67. data = (struct ustor_data*)dev->user_data;
  68. intf = data->intf;
  69. ret = rt_usbh_storage_read10(intf, (rt_uint8_t*)buffer, pos, size, timeout);
  70. if (ret != RT_EOK)
  71. {
  72. rt_kprintf("usb mass_storage read failed\n");
  73. return 0;
  74. }
  75. return size;
  76. }
  77. /**
  78. * This function will write some data to a device.
  79. *
  80. * @param dev the pointer of device driver structure
  81. * @param pos the position of written
  82. * @param buffer the data buffer to be written to device
  83. * @param size the size of buffer
  84. *
  85. * @return the actually written size on successful, otherwise negative returned.
  86. */
  87. static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buffer,
  88. rt_size_t size)
  89. {
  90. rt_err_t ret;
  91. struct uhintf* intf;
  92. struct ustor_data* data;
  93. int timeout = USB_TIMEOUT_LONG;
  94. /* check parameter */
  95. RT_ASSERT(dev != RT_NULL);
  96. RT_ASSERT(buffer != RT_NULL);
  97. if(size * SECTOR_SIZE > 4096) timeout *= 2;
  98. data = (struct ustor_data*)dev->user_data;
  99. intf = data->intf;
  100. ret = rt_usbh_storage_write10(intf, (rt_uint8_t*)buffer, pos, size, timeout);
  101. if (ret != RT_EOK)
  102. {
  103. rt_kprintf("usb mass_storage write %d sector failed\n", size);
  104. return 0;
  105. }
  106. return size;
  107. }
  108. /**
  109. * This function will execute SCSI_INQUIRY_CMD command to get inquiry data.
  110. *
  111. * @param intf the interface instance.
  112. * @param buffer the data buffer to save inquiry data
  113. *
  114. * @return the error code, RT_EOK on successfully.
  115. */
  116. static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args)
  117. {
  118. ustor_t stor;
  119. struct ustor_data* data;
  120. /* check parameter */
  121. RT_ASSERT(dev != RT_NULL);
  122. data = (struct ustor_data*)dev->user_data;
  123. stor = (ustor_t)data->intf->user_data;
  124. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  125. {
  126. struct rt_device_blk_geometry *geometry;
  127. geometry = (struct rt_device_blk_geometry *)args;
  128. if (geometry == RT_NULL) return -RT_ERROR;
  129. geometry->bytes_per_sector = SECTOR_SIZE;
  130. geometry->block_size = stor->capicity[1];
  131. geometry->sector_count = stor->capicity[0];
  132. }
  133. return RT_EOK;
  134. }
  135. #ifdef RT_USING_DEVICE_OPS
  136. const static struct rt_device_ops udisk_device_ops =
  137. {
  138. rt_udisk_init,
  139. RT_NULL,
  140. RT_NULL,
  141. rt_udisk_read,
  142. rt_udisk_write,
  143. rt_udisk_control
  144. };
  145. #endif
  146. /**
  147. * This function will run udisk driver when usb disk is detected.
  148. *
  149. * @param intf the usb interface instance.
  150. *
  151. * @return the error code, RT_EOK on successfully.
  152. */
  153. rt_err_t rt_udisk_run(struct uhintf* intf)
  154. {
  155. int i = 0;
  156. rt_err_t ret;
  157. char dname[4];
  158. char sname[8];
  159. rt_uint8_t max_lun, *sector, sense[18], inquiry[36];
  160. struct dfs_partition part[MAX_PARTITION_COUNT];
  161. ustor_t stor;
  162. /* check parameter */
  163. RT_ASSERT(intf != RT_NULL);
  164. /* set interface */
  165. // ret = rt_usbh_set_interface(intf->device, intf->intf_desc->bInterfaceNumber);
  166. // if(ret != RT_EOK)
  167. // rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
  168. /* reset mass storage class device */
  169. ret = rt_usbh_storage_reset(intf);
  170. if(ret != RT_EOK) return ret;
  171. stor = (ustor_t)intf->user_data;
  172. /* get max logic unit number */
  173. ret = rt_usbh_storage_get_max_lun(intf, &max_lun);
  174. if(ret != RT_EOK)
  175. rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
  176. /* reset pipe in endpoint */
  177. if(stor->pipe_in->status == UPIPE_STATUS_STALL)
  178. {
  179. ret = rt_usbh_clear_feature(intf->device,
  180. stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
  181. if(ret != RT_EOK) return ret;
  182. }
  183. /* reset pipe out endpoint */
  184. if(stor->pipe_out->status == UPIPE_STATUS_STALL)
  185. {
  186. ret = rt_usbh_clear_feature(intf->device,
  187. stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
  188. if(ret != RT_EOK) return ret;
  189. }
  190. while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK)
  191. {
  192. if(ret == -RT_EIO) return ret;
  193. rt_thread_delay(5);
  194. if(i++ < 10) continue;
  195. rt_kprintf("rt_usbh_storage_inquiry error\n");
  196. return -RT_ERROR;
  197. }
  198. i = 0;
  199. /* wait device ready */
  200. while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK)
  201. {
  202. if(ret == -RT_EIO) return ret;
  203. ret = rt_usbh_storage_request_sense(intf, sense);
  204. if(ret == -RT_EIO) return ret;
  205. rt_thread_delay(10);
  206. if(i++ < 10) continue;
  207. rt_kprintf("rt_usbh_storage_test_unit_ready error\n");
  208. return -RT_ERROR;
  209. }
  210. i = 0;
  211. rt_memset(stor->capicity, 0, sizeof(stor->capicity));
  212. /* get storage capacity */
  213. while((ret = rt_usbh_storage_get_capacity(intf,
  214. (rt_uint8_t*)stor->capicity)) != RT_EOK)
  215. {
  216. if(ret == -RT_EIO) return ret;
  217. rt_thread_delay(50);
  218. if(i++ < 10) continue;
  219. stor->capicity[0] = 2880;
  220. stor->capicity[1] = 0x200;
  221. rt_kprintf("rt_usbh_storage_get_capacity error\n");
  222. break;
  223. }
  224. stor->capicity[0] = uswap_32(stor->capicity[0]);
  225. stor->capicity[1] = uswap_32(stor->capicity[1]);
  226. stor->capicity[0] += 1;
  227. RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n",
  228. stor->capicity[0], stor->capicity[1]));
  229. /* get the first sector to read partition table */
  230. sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE);
  231. if (sector == RT_NULL)
  232. {
  233. rt_kprintf("allocate partition sector buffer failed\n");
  234. return -RT_ERROR;
  235. }
  236. rt_memset(sector, 0, SECTOR_SIZE);
  237. RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n"));
  238. /* get the partition table */
  239. ret = rt_usbh_storage_read10(intf, sector, 0, 1, USB_TIMEOUT_LONG);
  240. if(ret != RT_EOK)
  241. {
  242. rt_kprintf("read parition table error\n");
  243. rt_free(sector);
  244. return -RT_ERROR;
  245. }
  246. RT_DEBUG_LOG(RT_DEBUG_USB, ("finished reading partition\n"));
  247. for(i=0; i<MAX_PARTITION_COUNT; i++)
  248. {
  249. /* get the first partition */
  250. ret = dfs_filesystem_get_partition(&part[i], sector, i);
  251. if (ret == RT_EOK)
  252. {
  253. struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
  254. rt_memset(data, 0, sizeof(struct ustor_data));
  255. data->intf = intf;
  256. data->udisk_id = udisk_get_id();
  257. rt_snprintf(dname, 6, "ud%d-%d", data->udisk_id, i);
  258. rt_snprintf(sname, 8, "sem_ud%d", i);
  259. data->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
  260. /* register sdcard device */
  261. stor->dev[i].type = RT_Device_Class_Block;
  262. #ifdef RT_USING_DEVICE_OPS
  263. stor->dev[i].ops = &udisk_device_ops;
  264. #else
  265. stor->dev[i].init = rt_udisk_init;
  266. stor->dev[i].read = rt_udisk_read;
  267. stor->dev[i].write = rt_udisk_write;
  268. stor->dev[i].control = rt_udisk_control;
  269. #endif
  270. stor->dev[i].user_data = (void*)data;
  271. rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR |
  272. RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  273. stor->dev_cnt++;
  274. if (dfs_mount(stor->dev[i].parent.name, UDISK_MOUNTPOINT, "elm",
  275. 0, 0) == 0)
  276. {
  277. RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount successfully\n", i));
  278. }
  279. else
  280. {
  281. RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount failed\n", i));
  282. }
  283. }
  284. else
  285. {
  286. if(i == 0)
  287. {
  288. struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
  289. rt_memset(data, 0, sizeof(struct ustor_data));
  290. data->udisk_id = udisk_get_id();
  291. /* there is no partition table */
  292. data->part.offset = 0;
  293. data->part.size = 0;
  294. data->intf = intf;
  295. data->part.lock = rt_sem_create("sem_ud", 1, RT_IPC_FLAG_FIFO);
  296. rt_snprintf(dname, 7, "udisk%d", data->udisk_id);
  297. /* register sdcard device */
  298. stor->dev[0].type = RT_Device_Class_Block;
  299. #ifdef RT_USING_DEVICE_OPS
  300. stor->dev[i].ops = &udisk_device_ops;
  301. #else
  302. stor->dev[0].init = rt_udisk_init;
  303. stor->dev[0].read = rt_udisk_read;
  304. stor->dev[0].write = rt_udisk_write;
  305. stor->dev[0].control = rt_udisk_control;
  306. #endif
  307. stor->dev[0].user_data = (void*)data;
  308. rt_device_register(&stor->dev[0], dname,
  309. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE
  310. | RT_DEVICE_FLAG_STANDALONE);
  311. stor->dev_cnt++;
  312. if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT,
  313. "elm", 0, 0) == 0)
  314. {
  315. rt_kprintf("Mount FAT on Udisk successful.\n");
  316. }
  317. else
  318. {
  319. rt_kprintf("Mount FAT on Udisk failed.\n");
  320. }
  321. }
  322. break;
  323. }
  324. }
  325. rt_free(sector);
  326. return RT_EOK;
  327. }
  328. /**
  329. * This function will be invoked when usb disk plug out is detected and it would clean
  330. * and release all udisk related resources.
  331. *
  332. * @param intf the usb interface instance.
  333. *
  334. * @return the error code, RT_EOK on successfully.
  335. */
  336. rt_err_t rt_udisk_stop(struct uhintf* intf)
  337. {
  338. int i;
  339. ustor_t stor;
  340. struct ustor_data* data;
  341. /* check parameter */
  342. RT_ASSERT(intf != RT_NULL);
  343. RT_ASSERT(intf->device != RT_NULL);
  344. stor = (ustor_t)intf->user_data;
  345. RT_ASSERT(stor != RT_NULL);
  346. for(i=0; i<stor->dev_cnt; i++)
  347. {
  348. rt_device_t dev = &stor->dev[i];
  349. data = (struct ustor_data*)dev->user_data;
  350. /* unmount filesystem */
  351. dfs_unmount(UDISK_MOUNTPOINT);
  352. /* delete semaphore */
  353. rt_sem_delete(data->part.lock);
  354. udisk_free_id(data->udisk_id);
  355. rt_free(data);
  356. /* unregister device */
  357. rt_device_unregister(&stor->dev[i]);
  358. }
  359. return RT_EOK;
  360. }
  361. #endif