adk.c 11 KB

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