adk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * File : adk.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-12-12 Yi Qiu first version
  23. */
  24. #include <rtthread.h>
  25. #include <drivers/usb_host.h>
  26. #include "adk.h"
  27. #ifdef RT_USBH_ADK
  28. static struct uclass_driver adk_driver;
  29. static const char* _adk_manufacturer = RT_NULL;
  30. static const char* _adk_model = RT_NULL;
  31. static const char* _adk_description = RT_NULL;
  32. static const char* _adk_version = RT_NULL;
  33. static const char* _adk_uri = RT_NULL;
  34. static const char* _adk_serial = RT_NULL;
  35. rt_err_t rt_usbh_adk_set_string(const char* manufacturer, const char* model,
  36. const char* description, const char* _version, const char* uri,
  37. const char* serial)
  38. {
  39. _adk_manufacturer = manufacturer;
  40. _adk_model = model;
  41. _adk_description = description;
  42. _adk_version = _version;
  43. _adk_uri = uri;
  44. _adk_serial = serial;
  45. return RT_EOK;
  46. }
  47. #ifdef RT_USING_MODULE
  48. #include <rtm.h>
  49. RTM_EXPORT(rt_usbh_adk_set_string);
  50. #endif
  51. /**
  52. * This function will do USB_REQ_GET_PROTOCOL request to set idle period to the usb adk device
  53. *
  54. * @param intf the interface instance.
  55. * @duration the idle period of requesting data.
  56. * @report_id the report id
  57. *
  58. * @return the error code, RT_EOK on successfully.
  59. */
  60. static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protocol)
  61. {
  62. struct urequest setup;
  63. uinst_t device;
  64. int timeout = 100;
  65. /* parameter check */
  66. RT_ASSERT(intf != RT_NULL);
  67. RT_ASSERT(intf->device != RT_NULL);
  68. device = intf->device;
  69. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_VENDOR |
  70. USB_REQ_TYPE_DEVICE;
  71. setup.request = USB_REQ_GET_PROTOCOL;
  72. setup.index = 0;
  73. setup.length = 2;
  74. setup.value = 0;
  75. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)protocol, 2,
  76. timeout) == 0) return RT_EOK;
  77. else return -RT_FALSE;
  78. }
  79. /**
  80. * This function will do USB_REQ_SEND_STRING request to set idle period to the usb adk device
  81. *
  82. * @param intf the interface instance.
  83. * @duration the idle period of requesting data.
  84. * @report_id the report id
  85. *
  86. * @return the error code, RT_EOK on successfully.
  87. */
  88. static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index,
  89. const char* str)
  90. {
  91. struct urequest setup;
  92. uinst_t device;
  93. int timeout = 100;
  94. /* parameter check */
  95. RT_ASSERT(intf != RT_NULL);
  96. RT_ASSERT(intf->device != RT_NULL);
  97. device = intf->device;
  98. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
  99. USB_REQ_TYPE_DEVICE;
  100. setup.request = USB_REQ_SEND_STRING;
  101. setup.index = index;
  102. setup.length = rt_strlen(str) + 1;
  103. setup.value = 0;
  104. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, (void*)str,
  105. rt_strlen(str) + 1, timeout) == 0) return RT_EOK;
  106. else return -RT_FALSE;
  107. }
  108. /**
  109. * This function will do USB_REQ_START request to set idle period to the usb adk device
  110. *
  111. * @param intf the interface instance.
  112. * @duration the idle period of requesting data.
  113. * @report_id the report id
  114. *
  115. * @return the error code, RT_EOK on successfully.
  116. */
  117. static rt_err_t rt_usbh_adk_start(struct uintf* intf)
  118. {
  119. struct urequest setup;
  120. uinst_t device;
  121. int timeout = 100;
  122. /* parameter check */
  123. RT_ASSERT(intf != RT_NULL);
  124. RT_ASSERT(intf->device != RT_NULL);
  125. device = intf->device;
  126. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_VENDOR |
  127. USB_REQ_TYPE_DEVICE;
  128. setup.request = USB_REQ_START;
  129. setup.index = 0;
  130. setup.length = 0;
  131. setup.value = 0;
  132. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
  133. timeout) == 0) return RT_EOK;
  134. else return -RT_FALSE;
  135. }
  136. /**
  137. * This function will read data from usb adk device
  138. *
  139. * @param intf the interface instance.
  140. *
  141. * @return the error code, RT_EOK on successfully.
  142. */
  143. static rt_size_t rt_usbh_adk_read(rt_device_t device, rt_off_t pos, void* buffer,
  144. rt_size_t size)
  145. {
  146. uadk_t adk;
  147. rt_size_t length;
  148. struct uintf* intf;
  149. /* check parameter */
  150. RT_ASSERT(device != RT_NULL);
  151. RT_ASSERT(buffer != RT_NULL);
  152. intf = (struct uintf*)device->user_data;
  153. adk = (uadk_t)intf->user_data;
  154. length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_in,
  155. buffer, size, 300);
  156. return length;
  157. }
  158. /**
  159. * This function will write data to usb adk device
  160. *
  161. * @param intf the interface instance.
  162. *
  163. * @return the error code, RT_EOK on successfully.
  164. */
  165. static rt_size_t rt_usbh_adk_write (rt_device_t device, rt_off_t pos, const void* buffer,
  166. rt_size_t size)
  167. {
  168. uadk_t adk;
  169. rt_size_t length;
  170. struct uintf* intf;
  171. RT_ASSERT(buffer != RT_NULL);
  172. intf = (struct uintf*)device->user_data;
  173. adk = (uadk_t)intf->user_data;
  174. length = rt_usb_hcd_bulk_xfer(intf->device->hcd, adk->pipe_out,
  175. (void*)buffer, size, 300);
  176. return length;
  177. }
  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. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_run\n"));
  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. RT_DEBUG_LOG(RT_DEBUG_USB, ("found android accessory device\n"));
  208. }
  209. else
  210. {
  211. RT_DEBUG_LOG(RT_DEBUG_USB, ("switch device\n"));
  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. RT_DEBUG_LOG(RT_DEBUG_USB, ("manufacturer %s\n", _adk_manufacturer));
  235. RT_DEBUG_LOG(RT_DEBUG_USB, ("model %s\n", _adk_model));
  236. RT_DEBUG_LOG(RT_DEBUG_USB, ("description %s\n", _adk_description));
  237. RT_DEBUG_LOG(RT_DEBUG_USB, ("version %s\n", _adk_version));
  238. RT_DEBUG_LOG(RT_DEBUG_USB, ("uri %s\n", _adk_uri));
  239. RT_DEBUG_LOG(RT_DEBUG_USB, ("serial %s\n", _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. 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. adk->device.user_data = (void*)intf;
  299. rt_device_register(&adk->device, "adkdev", RT_DEVICE_FLAG_RDWR);
  300. return RT_EOK;
  301. }
  302. /**
  303. * This function will be invoked when usb device plug out is detected and it would clean
  304. * and release all hub class related resources.
  305. *
  306. * @param arg the argument.
  307. *
  308. * @return the error code, RT_EOK on successfully.
  309. */
  310. static rt_err_t rt_usbh_adk_disable(void* arg)
  311. {
  312. uadk_t adk;
  313. struct uintf* intf = (struct uintf*)arg;
  314. RT_ASSERT(intf != RT_NULL);
  315. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_adk_stop\n"));
  316. adk = (uadk_t)intf->user_data;
  317. if(adk == RT_NULL)
  318. {
  319. rt_free(intf);
  320. return RT_EOK;
  321. }
  322. if(adk->pipe_in != RT_NULL)
  323. rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_in);
  324. if(adk->pipe_out != RT_NULL)
  325. rt_usb_hcd_free_pipe(intf->device->hcd, adk->pipe_out);
  326. /* unregister adk device */
  327. rt_device_unregister(&adk->device);
  328. /* free adk instance */
  329. if(adk != RT_NULL)
  330. {
  331. rt_free(adk);
  332. }
  333. /* free interface instance */
  334. rt_free(intf);
  335. return RT_EOK;
  336. }
  337. /**
  338. * This function will register adk class driver to the usb class driver manager.
  339. * and it should be invoked in the usb system initialization.
  340. *
  341. * @return the error code, RT_EOK on successfully.
  342. */
  343. ucd_t rt_usbh_class_driver_adk(void)
  344. {
  345. adk_driver.class_code = USB_CLASS_ADK;
  346. adk_driver.enable = rt_usbh_adk_enable;
  347. adk_driver.disable = rt_usbh_adk_disable;
  348. return &adk_driver;
  349. }
  350. #endif