udisk.c 12 KB

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