adk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * File : adk.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 <drivers/usb_host.h>
  16. #include "adk.h"
  17. #ifdef RT_USB_CLASS_ADK
  18. static struct uclass_driver adk_driver;
  19. rt_err_t rt_usb_adk_set_string(const char* manufacturer, const char* model,
  20. const char* description, const char* version, const char* uri,
  21. const char* serial)
  22. {
  23. adk_driver.manufacturer = manufacturer;
  24. adk_driver.model = model;
  25. adk_driver.description = description;
  26. adk_driver.version = version;
  27. adk_driver.uri = uri;
  28. adk_driver.serial = serial;
  29. return RT_EOK;
  30. }
  31. #ifdef RT_USING_MODULE
  32. #include <rtm.h>
  33. RTM_EXPORT(rt_usb_adk_set_string);
  34. #endif
  35. /**
  36. * This function will do USB_REQ_GET_PROTOCOL request to set idle period to the usb adk device
  37. *
  38. * @param ifinst the interface instance.
  39. * @duration the idle period of requesting data.
  40. * @report_id the report id
  41. *
  42. * @return the error code, RT_EOK on successfully.
  43. */
  44. static rt_err_t rt_usb_adk_get_protocol(uifinst_t ifinst, rt_uint16_t *protocol)
  45. {
  46. struct ureqest setup;
  47. uinst_t uinst;
  48. int timeout = 100;
  49. /* parameter check */
  50. RT_ASSERT(ifinst != RT_NULL);
  51. RT_ASSERT(ifinst->uinst != RT_NULL);
  52. uinst = ifinst->uinst;
  53. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR |
  54. USB_REQ_TYPE_DEVICE;
  55. setup.request = USB_REQ_GET_PROTOCOL;
  56. setup.index = 0;
  57. setup.length = 2;
  58. setup.value = 0;
  59. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, (void*)protocol, 2,
  60. timeout) == 0) return RT_EOK;
  61. else return -RT_FALSE;
  62. }
  63. /**
  64. * This function will do USB_REQ_SEND_STRING request to set idle period to the usb adk device
  65. *
  66. * @param ifinst the interface instance.
  67. * @duration the idle period of requesting data.
  68. * @report_id the report id
  69. *
  70. * @return the error code, RT_EOK on successfully.
  71. */
  72. static rt_err_t rt_usb_adk_send_string(uifinst_t ifinst, rt_uint16_t index,
  73. const char* str)
  74. {
  75. struct ureqest setup;
  76. uinst_t uinst;
  77. int timeout = 100;
  78. /* parameter check */
  79. RT_ASSERT(ifinst != RT_NULL);
  80. RT_ASSERT(ifinst->uinst != RT_NULL);
  81. uinst = ifinst->uinst;
  82. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
  83. USB_REQ_TYPE_DEVICE;
  84. setup.request = USB_REQ_SEND_STRING;
  85. setup.index = index;
  86. setup.length = rt_strlen(str) + 1;
  87. setup.value = 0;
  88. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, (void*)str,
  89. rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
  90. else return -RT_FALSE;
  91. }
  92. /**
  93. * This function will do USB_REQ_START request to set idle period to the usb adk device
  94. *
  95. * @param ifinst the interface instance.
  96. * @duration the idle period of requesting data.
  97. * @report_id the report id
  98. *
  99. * @return the error code, RT_EOK on successfully.
  100. */
  101. static rt_err_t rt_usb_adk_start(uifinst_t ifinst)
  102. {
  103. struct ureqest setup;
  104. uinst_t uinst;
  105. int timeout = 100;
  106. /* parameter check */
  107. RT_ASSERT(ifinst != RT_NULL);
  108. RT_ASSERT(ifinst->uinst != RT_NULL);
  109. uinst = ifinst->uinst;
  110. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
  111. USB_REQ_TYPE_DEVICE;
  112. setup.request = USB_REQ_START;
  113. setup.index = 0;
  114. setup.length = 0;
  115. setup.value = 0;
  116. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  117. timeout) == 0) return RT_EOK;
  118. else return -RT_FALSE;
  119. }
  120. /**
  121. * This function will read data from usb adk device
  122. *
  123. * @param ifinst the interface instance.
  124. *
  125. * @return the error code, RT_EOK on successfully.
  126. */
  127. static rt_size_t rt_usb_adk_read(rt_device_t device, rt_off_t pos, void* buffer,
  128. rt_size_t size)
  129. {
  130. uadkinst_t adkinst;
  131. rt_size_t length;
  132. uifinst_t ifinst;
  133. /* check parameter */
  134. RT_ASSERT(device != RT_NULL);
  135. RT_ASSERT(buffer != RT_NULL);
  136. ifinst = (uifinst_t)device->user_data;
  137. adkinst = (uadkinst_t)ifinst->user_data;
  138. length = rt_usb_hcd_bulk_xfer(ifinst->uinst->hcd, adkinst->pipe_in,
  139. buffer, size, 300);
  140. return length;
  141. }
  142. /**
  143. * This function will write data to usb adk device
  144. *
  145. * @param ifinst the interface instance.
  146. *
  147. * @return the error code, RT_EOK on successfully.
  148. */
  149. static rt_size_t rt_usb_adk_write (rt_device_t device, rt_off_t pos, const void* buffer,
  150. rt_size_t size)
  151. {
  152. uadkinst_t adkinst;
  153. rt_size_t length;
  154. uifinst_t ifinst;
  155. RT_ASSERT(buffer != RT_NULL);
  156. ifinst = (uifinst_t)device->user_data;
  157. adkinst = (uadkinst_t)ifinst->user_data;
  158. length = rt_usb_hcd_bulk_xfer(ifinst->uinst->hcd, adkinst->pipe_out,
  159. (void*)buffer, size, 300);
  160. return length;
  161. }
  162. /**
  163. * This function will run adk class driver when usb device is detected and identified
  164. * as a adk class device, it will continue the enumulate process.
  165. *
  166. * @param arg the argument.
  167. *
  168. * @return the error code, RT_EOK on successfully.
  169. */
  170. static rt_err_t rt_usb_adk_run(void* arg)
  171. {
  172. int i = 0;
  173. uadkinst_t adkinst;
  174. uifinst_t ifinst = (uifinst_t)arg;
  175. udev_desc_t dev_desc;
  176. rt_uint16_t protocol;
  177. rt_err_t ret;
  178. /* parameter check */
  179. if(ifinst == RT_NULL)
  180. {
  181. rt_kprintf("the interface is not available\n");
  182. return -RT_EIO;
  183. }
  184. RT_DEBUG_LOG(RT_DEBUG_USB,("rt_usb_adk_run\n"));
  185. dev_desc = &ifinst->uinst->dev_desc;
  186. if(dev_desc->idVendor == USB_ACCESSORY_VENDOR_ID &&
  187. (dev_desc->idProduct == USB_ACCESSORY_PRODUCT_ID ||
  188. dev_desc->idProduct == USB_ACCESSORY_ADB_PRODUCT_ID))
  189. {
  190. if(ifinst->intf_desc->bInterfaceSubClass != 0xFF) return -RT_ERROR;
  191. RT_DEBUG_LOG(RT_DEBUG_USB,("found android accessory device\n"));
  192. }
  193. else
  194. {
  195. RT_DEBUG_LOG(RT_DEBUG_USB,("switch device\n"));
  196. if((ret = rt_usb_adk_get_protocol(ifinst, &protocol)) != RT_EOK)
  197. {
  198. rt_kprintf("rt_usb_adk_get_protocol failed\n");
  199. return ret;
  200. }
  201. if(protocol != 1)
  202. {
  203. rt_kprintf("read protocol failed\n");
  204. return -RT_ERROR;
  205. }
  206. rt_usb_adk_send_string(ifinst,
  207. ACCESSORY_STRING_MANUFACTURER, adk_driver.manufacturer);
  208. rt_usb_adk_send_string(ifinst,
  209. ACCESSORY_STRING_MODEL, adk_driver.model);
  210. rt_usb_adk_send_string(ifinst,
  211. ACCESSORY_STRING_DESCRIPTION, adk_driver.description);
  212. rt_usb_adk_send_string(ifinst,
  213. ACCESSORY_STRING_VERSION, adk_driver.version);
  214. rt_usb_adk_send_string(ifinst,
  215. ACCESSORY_STRING_URI, adk_driver.uri);
  216. rt_usb_adk_send_string(ifinst,
  217. ACCESSORY_STRING_SERIAL, adk_driver.serial);
  218. RT_DEBUG_LOG(RT_DEBUG_USB,("manufacturer %s\n", adk_driver.manufacturer));
  219. RT_DEBUG_LOG(RT_DEBUG_USB,("model %s\n", adk_driver.model));
  220. RT_DEBUG_LOG(RT_DEBUG_USB,("description %s\n", adk_driver.description));
  221. RT_DEBUG_LOG(RT_DEBUG_USB,("version %s\n", adk_driver.version));
  222. RT_DEBUG_LOG(RT_DEBUG_USB,("uri %s\n", adk_driver.uri));
  223. RT_DEBUG_LOG(RT_DEBUG_USB,("serial %s\n", adk_driver.serial));
  224. if((ret = rt_usb_adk_start(ifinst)) != RT_EOK)
  225. {
  226. rt_kprintf("rt_usb_adk_start failed\n");
  227. return ret;
  228. }
  229. return RT_EOK;
  230. }
  231. adkinst = rt_malloc(sizeof(struct uadkinst));
  232. RT_ASSERT(adkinst != RT_NULL);
  233. /* initilize the data structure */
  234. rt_memset(adkinst, 0, sizeof(struct uadkinst));
  235. ifinst->user_data = (void*)adkinst;
  236. for(i=0; i<ifinst->intf_desc->bNumEndpoints; i++)
  237. {
  238. uep_desc_t ep_desc;
  239. /* get endpoint descriptor from interface descriptor */
  240. rt_usb_get_endpoint_descriptor(ifinst->intf_desc, i, &ep_desc);
  241. if(ep_desc == RT_NULL)
  242. {
  243. rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
  244. return -RT_ERROR;
  245. }
  246. /* the endpoint type of adk class should be BULK */
  247. if((ep_desc->bmAttributes & USB_EP_ATTR_TYPE_MASK) != USB_EP_ATTR_BULK)
  248. continue;
  249. /* allocate pipes according to the endpoint type */
  250. if(ep_desc->bEndpointAddress & USB_DIR_IN)
  251. {
  252. /* allocate an in pipe for the adk instance */
  253. ret = rt_usb_hcd_alloc_pipe(ifinst->uinst->hcd, &adkinst->pipe_in,
  254. ifinst, ep_desc, RT_NULL);
  255. if(ret != RT_EOK) return ret;
  256. }
  257. else
  258. {
  259. /* allocate an output pipe for the adk instance */
  260. ret = rt_usb_hcd_alloc_pipe(ifinst->uinst->hcd, &adkinst->pipe_out,
  261. ifinst, ep_desc, RT_NULL);
  262. if(ret != RT_EOK) return ret;
  263. }
  264. }
  265. /* check pipes infomation */
  266. if(adkinst->pipe_in == RT_NULL || adkinst->pipe_out == RT_NULL)
  267. {
  268. rt_kprintf("pipe error, unsupported device\n");
  269. return -RT_ERROR;
  270. }
  271. /* set configuration */
  272. ret = rt_usb_set_configure(ifinst->uinst, 1);
  273. if(ret != RT_EOK) return ret;
  274. /* register adk device */
  275. adkinst->device.type = RT_Device_Class_Char;
  276. adkinst->device.init = RT_NULL;
  277. adkinst->device.open = RT_NULL;
  278. adkinst->device.close = RT_NULL;
  279. adkinst->device.read = rt_usb_adk_read;
  280. adkinst->device.write = rt_usb_adk_write;
  281. adkinst->device.control = RT_NULL;
  282. adkinst->device.user_data = (void*)ifinst;
  283. rt_device_register(&adkinst->device, "adkdev", RT_DEVICE_FLAG_RDWR);
  284. return RT_EOK;
  285. }
  286. /**
  287. * This function will be invoked when usb device plug out is detected and it would clean
  288. * and release all hub class related resources.
  289. *
  290. * @param arg the argument.
  291. *
  292. * @return the error code, RT_EOK on successfully.
  293. */
  294. static rt_err_t rt_usb_adk_stop(void* arg)
  295. {
  296. uadkinst_t adkinst;
  297. uifinst_t ifinst = (uifinst_t)arg;
  298. RT_ASSERT(ifinst != RT_NULL);
  299. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_adk_stop\n"));
  300. adkinst = (uadkinst_t)ifinst->user_data;
  301. if(adkinst == RT_NULL)
  302. {
  303. rt_free(ifinst);
  304. return RT_EOK;
  305. }
  306. if(adkinst->pipe_in != RT_NULL)
  307. rt_usb_hcd_free_pipe(ifinst->uinst->hcd, adkinst->pipe_in);
  308. if(adkinst->pipe_out != RT_NULL)
  309. rt_usb_hcd_free_pipe(ifinst->uinst->hcd, adkinst->pipe_out);
  310. /* unregister adk device */
  311. rt_device_unregister(&adkinst->device);
  312. /* free adk instance */
  313. if(adkinst != RT_NULL) rt_free(adkinst);
  314. /* free interface instance */
  315. rt_free(ifinst);
  316. return RT_EOK;
  317. }
  318. /**
  319. * This function will register adk class driver to the usb class driver manager.
  320. * and it should be invoked in the usb system initialization.
  321. *
  322. * @return the error code, RT_EOK on successfully.
  323. */
  324. ucd_t rt_usb_class_driver_adk(void)
  325. {
  326. adk_driver.class_code = USB_CLASS_ADK;
  327. adk_driver.run = rt_usb_adk_run;
  328. adk_driver.stop = rt_usb_adk_stop;
  329. return &adk_driver;
  330. }
  331. #endif