udisk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 uintf* 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 uintf* 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, rt_uint8_t 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. /**
  140. * This function will run udisk driver when usb disk is detected.
  141. *
  142. * @param intf the usb interface instance.
  143. *
  144. * @return the error code, RT_EOK on successfully.
  145. */
  146. rt_err_t rt_udisk_run(struct uintf* intf)
  147. {
  148. int i = 0;
  149. rt_err_t ret;
  150. char dname[4];
  151. char sname[8];
  152. rt_uint8_t max_lun, *sector, sense[18], inquiry[36];
  153. struct dfs_partition part[MAX_PARTITION_COUNT];
  154. ustor_t stor;
  155. /* check parameter */
  156. RT_ASSERT(intf != RT_NULL);
  157. /* set interface */
  158. // ret = rt_usbh_set_interface(intf->device, intf->intf_desc->bInterfaceNumber);
  159. // if(ret != RT_EOK)
  160. // rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
  161. /* reset mass storage class device */
  162. ret = rt_usbh_storage_reset(intf);
  163. if(ret != RT_EOK) return ret;
  164. stor = (ustor_t)intf->user_data;
  165. /* get max logic unit number */
  166. ret = rt_usbh_storage_get_max_lun(intf, &max_lun);
  167. if(ret != RT_EOK)
  168. rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
  169. /* reset pipe in endpoint */
  170. ret = rt_usbh_clear_feature(intf->device,
  171. stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
  172. if(ret != RT_EOK) return ret;
  173. /* reset pipe out endpoint */
  174. ret = rt_usbh_clear_feature(intf->device,
  175. stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
  176. if(ret != RT_EOK) return ret;
  177. while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK)
  178. {
  179. if(ret == -RT_EIO) return ret;
  180. rt_thread_delay(5);
  181. if(i++ < 10) continue;
  182. rt_kprintf("rt_usbh_storage_inquiry error\n");
  183. return -RT_ERROR;
  184. }
  185. i = 0;
  186. /* wait device ready */
  187. while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK)
  188. {
  189. if(ret == -RT_EIO) return ret;
  190. ret = rt_usbh_storage_request_sense(intf, sense);
  191. if(ret == -RT_EIO) return ret;
  192. rt_thread_delay(10);
  193. if(i++ < 10) continue;
  194. rt_kprintf("rt_usbh_storage_test_unit_ready error\n");
  195. return -RT_ERROR;
  196. }
  197. i = 0;
  198. rt_memset(stor->capicity, 0, sizeof(stor->capicity));
  199. /* get storage capacity */
  200. while((ret = rt_usbh_storage_get_capacity(intf,
  201. (rt_uint8_t*)stor->capicity)) != RT_EOK)
  202. {
  203. if(ret == -RT_EIO) return ret;
  204. rt_thread_delay(50);
  205. if(i++ < 10) continue;
  206. stor->capicity[0] = 2880;
  207. stor->capicity[1] = 0x200;
  208. rt_kprintf("rt_usbh_storage_get_capacity error\n");
  209. break;
  210. }
  211. stor->capicity[0] = uswap_32(stor->capicity[0]);
  212. stor->capicity[1] = uswap_32(stor->capicity[1]);
  213. stor->capicity[0] += 1;
  214. RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n",
  215. stor->capicity[0], stor->capicity[1]));
  216. /* get the first sector to read partition table */
  217. sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE);
  218. if (sector == RT_NULL)
  219. {
  220. rt_kprintf("allocate partition sector buffer failed\n");
  221. return -RT_ERROR;
  222. }
  223. rt_memset(sector, 0, SECTOR_SIZE);
  224. RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n"));
  225. /* get the partition table */
  226. ret = rt_usbh_storage_read10(intf, sector, 0, 1, 500);
  227. if(ret != RT_EOK)
  228. {
  229. rt_kprintf("read parition table error\n");
  230. rt_free(sector);
  231. return -RT_ERROR;
  232. }
  233. RT_DEBUG_LOG(RT_DEBUG_USB, ("finished reading partition\n"));
  234. for(i=0; i<MAX_PARTITION_COUNT; i++)
  235. {
  236. /* get the first partition */
  237. ret = dfs_filesystem_get_partition(&part[i], sector, i);
  238. if (ret == RT_EOK)
  239. {
  240. struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
  241. rt_memset(data, 0, sizeof(struct ustor_data));
  242. data->intf = intf;
  243. data->udisk_id = udisk_get_id();
  244. data->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
  245. rt_snprintf(dname, 6, "ud%d-%d", data->udisk_id, i);
  246. rt_snprintf(sname, 8, "sem_ud%d", i);
  247. /* register sdcard device */
  248. stor->dev[i].type = RT_Device_Class_Block;
  249. stor->dev[i].init = rt_udisk_init;
  250. stor->dev[i].read = rt_udisk_read;
  251. stor->dev[i].write = rt_udisk_write;
  252. stor->dev[i].control = rt_udisk_control;
  253. stor->dev[i].user_data = (void*)data;
  254. rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR |
  255. RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  256. stor->dev_cnt++;
  257. if (dfs_mount(stor->dev[i].parent.name, UDISK_MOUNTPOINT, "elm",
  258. 0, 0) == 0)
  259. {
  260. RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount successfully\n", i));
  261. }
  262. else
  263. {
  264. RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount failed\n", i));
  265. }
  266. }
  267. else
  268. {
  269. if(i == 0)
  270. {
  271. struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
  272. rt_memset(data, 0, sizeof(struct ustor_data));
  273. data->udisk_id = udisk_get_id();
  274. /* there is no partition table */
  275. data->part.offset = 0;
  276. data->part.size = 0;
  277. data->intf = intf;
  278. data->part.lock = rt_sem_create("sem_ud", 1, RT_IPC_FLAG_FIFO);
  279. rt_snprintf(dname, 7, "udisk%d", data->udisk_id);
  280. /* register sdcard device */
  281. stor->dev[0].type = RT_Device_Class_Block;
  282. stor->dev[0].init = rt_udisk_init;
  283. stor->dev[0].read = rt_udisk_read;
  284. stor->dev[0].write = rt_udisk_write;
  285. stor->dev[0].control = rt_udisk_control;
  286. stor->dev[0].user_data = (void*)data;
  287. rt_device_register(&stor->dev[0], dname,
  288. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE
  289. | RT_DEVICE_FLAG_STANDALONE);
  290. stor->dev_cnt++;
  291. if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT,
  292. "elm", 0, 0) == 0)
  293. {
  294. rt_kprintf("Mount FAT on Udisk successful.\n");
  295. }
  296. else
  297. {
  298. rt_kprintf("Mount FAT on Udisk failed.\n");
  299. }
  300. }
  301. break;
  302. }
  303. }
  304. rt_free(sector);
  305. return RT_EOK;
  306. }
  307. /**
  308. * This function will be invoked when usb disk plug out is detected and it would clean
  309. * and release all udisk related resources.
  310. *
  311. * @param intf the usb interface instance.
  312. *
  313. * @return the error code, RT_EOK on successfully.
  314. */
  315. rt_err_t rt_udisk_stop(struct uintf* intf)
  316. {
  317. int i;
  318. ustor_t stor;
  319. struct ustor_data* data;
  320. /* check parameter */
  321. RT_ASSERT(intf != RT_NULL);
  322. RT_ASSERT(intf->device != RT_NULL);
  323. stor = (ustor_t)intf->user_data;
  324. RT_ASSERT(stor != RT_NULL);
  325. for(i=0; i<stor->dev_cnt; i++)
  326. {
  327. rt_device_t dev = &stor->dev[i];
  328. data = (struct ustor_data*)dev->user_data;
  329. /* unmount filesystem */
  330. dfs_unmount(UDISK_MOUNTPOINT);
  331. /* delete semaphore */
  332. rt_sem_delete(data->part.lock);
  333. udisk_free_id(data->udisk_id);
  334. rt_free(data);
  335. /* unregister device */
  336. rt_device_unregister(&stor->dev[i]);
  337. }
  338. return RT_EOK;
  339. }
  340. #endif