udisk.c 12 KB

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