adk.c 11 KB

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