adk.c 12 KB

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