udisk.c 12 KB

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