hid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "hid.h"
  13. #ifdef RT_USBH_HID
  14. static struct uclass_driver hid_driver;
  15. static rt_list_t _protocal_list;
  16. /**
  17. * This function will do USB_REQ_SET_IDLE request to set idle period to the usb hid device
  18. *
  19. * @param intf the interface instance.
  20. * @duration the idle period of requesting data.
  21. * @report_id the report id
  22. *
  23. * @return the error code, RT_EOK on successfully.
  24. */
  25. rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id)
  26. {
  27. struct urequest setup;
  28. struct uinstance* device;
  29. int timeout = USB_TIMEOUT_BASIC;
  30. /* parameter check */
  31. RT_ASSERT(intf != RT_NULL);
  32. RT_ASSERT(intf->device != RT_NULL);
  33. device = intf->device;
  34. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  35. USB_REQ_TYPE_INTERFACE;
  36. setup.request = USB_REQ_SET_IDLE;
  37. setup.index = 0;
  38. setup.length = 0;
  39. setup.value = (duration << 8 )| report_id;
  40. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
  41. timeout) == 0) return RT_EOK;
  42. else return -RT_FALSE;
  43. }
  44. /**
  45. * This function will do USB_REQ_GET_REPORT request to get report from the usb hid device
  46. *
  47. * @param intf the interface instance.
  48. * @buffer the data buffer to save usb report descriptor.
  49. * @param nbytes the size of buffer
  50. *
  51. * @return the error code, RT_EOK on successfully.
  52. */
  53. rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type,
  54. rt_uint8_t id, rt_uint8_t *buffer, rt_size_t size)
  55. {
  56. struct urequest setup;
  57. struct uinstance* device;
  58. int timeout = USB_TIMEOUT_BASIC;
  59. /* parameter check */
  60. RT_ASSERT(intf != RT_NULL);
  61. RT_ASSERT(intf->device != RT_NULL);
  62. device = intf->device;
  63. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
  64. USB_REQ_TYPE_INTERFACE;
  65. setup.request = USB_REQ_GET_REPORT;
  66. setup.index = intf->intf_desc->bInterfaceNumber;
  67. setup.length = size;
  68. setup.value = (type << 8 ) + id;
  69. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size,
  70. timeout) == size) return RT_EOK;
  71. else return -RT_FALSE;
  72. }
  73. /**
  74. * This function will do USB_REQ_SET_REPORT request to set report to the usb hid device
  75. *
  76. * @param intf the interface instance.
  77. * @buffer the data buffer to save usb report descriptor.
  78. * @param nbytes the size of buffer
  79. *
  80. * @return the error code, RT_EOK on successfully.
  81. */
  82. rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_t size)
  83. {
  84. struct urequest setup;
  85. struct uinstance* device;
  86. int timeout = USB_TIMEOUT_BASIC;
  87. /* parameter check */
  88. RT_ASSERT(intf != RT_NULL);
  89. RT_ASSERT(intf->device != RT_NULL);
  90. device = intf->device;
  91. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  92. USB_REQ_TYPE_INTERFACE;
  93. setup.request = USB_REQ_SET_REPORT;
  94. setup.index = intf->intf_desc->bInterfaceNumber;
  95. setup.length = size;
  96. setup.value = 0x02 << 8;
  97. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size,
  98. timeout) == size) return RT_EOK;
  99. else return -RT_FALSE;
  100. }
  101. /**
  102. * This function will do USB_REQ_SET_PROTOCOL request to set protocal to the usb hid device.
  103. *
  104. * @param intf the interface instance.
  105. * @param protocol the protocol id.
  106. *
  107. * @return the error code, RT_EOK on successfully.
  108. */
  109. rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol)
  110. {
  111. struct urequest setup;
  112. struct uinstance* device;
  113. int timeout = USB_TIMEOUT_BASIC;
  114. /* parameter check */
  115. RT_ASSERT(intf != RT_NULL);
  116. RT_ASSERT(intf->device != RT_NULL);
  117. device = intf->device;
  118. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  119. USB_REQ_TYPE_INTERFACE;
  120. setup.request = USB_REQ_SET_PROTOCOL;
  121. setup.index = 0;
  122. setup.length = 0;
  123. setup.value = protocol;
  124. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, RT_NULL, 0,
  125. timeout) == 0) return RT_EOK;
  126. else return -RT_FALSE;
  127. }
  128. /**
  129. * This function will do USB_REQ_GET_DESCRIPTOR request for the device instance
  130. * to set feature of the hub port.
  131. *
  132. * @param intf the interface instance.
  133. * @buffer the data buffer to save usb report descriptor.
  134. * @param nbytes the size of buffer
  135. *
  136. * @return the error code, RT_EOK on successfully.
  137. */
  138. rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf,
  139. rt_uint8_t *buffer, rt_size_t size)
  140. {
  141. struct urequest setup;
  142. struct uinstance* device;
  143. int timeout = USB_TIMEOUT_BASIC;
  144. /* parameter check */
  145. RT_ASSERT(intf != RT_NULL);
  146. RT_ASSERT(intf->device != RT_NULL);
  147. device = intf->device;
  148. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD|
  149. USB_REQ_TYPE_INTERFACE;
  150. setup.request = USB_REQ_GET_DESCRIPTOR;
  151. setup.index = 0;
  152. setup.length = size;
  153. setup.value = USB_DESC_TYPE_REPORT << 8;
  154. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, size,
  155. timeout) == size) return RT_EOK;
  156. else return -RT_FALSE;
  157. }
  158. /**
  159. * This function will register specified hid protocal to protocal list
  160. *
  161. * @param protocal the specified protocal.
  162. *
  163. * @return the error code, RT_EOK on successfully.
  164. */
  165. rt_err_t rt_usbh_hid_protocal_register(uprotocal_t protocal)
  166. {
  167. RT_ASSERT(protocal != RT_NULL);
  168. if (protocal == RT_NULL) return -RT_ERROR;
  169. /* insert class driver into driver list */
  170. rt_list_insert_after(&_protocal_list, &(protocal->list));
  171. return RT_EOK;
  172. }
  173. /**
  174. * This function is the callback function of hid's int endpoint, it is invoked when data comes.
  175. *
  176. * @param context the context of the callback function.
  177. *
  178. * @return none.
  179. */
  180. static void rt_usbh_hid_callback(void* context)
  181. {
  182. upipe_t pipe;
  183. struct uhid* hid;
  184. int timeout = USB_TIMEOUT_LONG;
  185. /* parameter check */
  186. RT_ASSERT(context != RT_NULL);
  187. pipe = (upipe_t)context;
  188. hid = (struct uhid*)pipe->intf->user_data;
  189. /* invoke protocal callback function */
  190. hid->protocal->callback((void*)hid);
  191. /* parameter check */
  192. RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
  193. rt_usb_hcd_int_xfer(pipe->intf->device->hcd, pipe, hid->buffer,
  194. pipe->ep.wMaxPacketSize, timeout);
  195. }
  196. /**
  197. * This function will find specified hid protocal from protocal list
  198. *
  199. * @param pro_id the protocal id.
  200. *
  201. * @return the found protocal or RT_NULL if there is no this protocal.
  202. */
  203. static uprotocal_t rt_usbh_hid_protocal_find(int pro_id)
  204. {
  205. struct rt_list_node *node;
  206. /* try to find protocal object */
  207. for (node = _protocal_list.next; node != &_protocal_list; node = node->next)
  208. {
  209. uprotocal_t protocal =
  210. (uprotocal_t)rt_list_entry(node, struct uprotocal, list);
  211. if (protocal->pro_id == pro_id) return protocal;
  212. }
  213. /* not found */
  214. return RT_NULL;
  215. }
  216. /**
  217. * This function will run hid class driver when usb device is detected and identified
  218. * as a hid class device, it will continue the enumulate process.
  219. *
  220. * @param arg the argument.
  221. *
  222. * @return the error code, RT_EOK on successfully.
  223. */
  224. static rt_err_t rt_usbh_hid_enable(void* arg)
  225. {
  226. int i = 0, pro_id;
  227. uprotocal_t protocal;
  228. struct uhid* hid;
  229. struct uintf* intf = (struct uintf*)arg;
  230. int timeout = USB_TIMEOUT_BASIC;
  231. upipe_t pipe;
  232. /* parameter check */
  233. if(intf == RT_NULL)
  234. {
  235. rt_kprintf("the interface is not available\n");
  236. return -RT_EIO;
  237. }
  238. pro_id = intf->intf_desc->bInterfaceProtocol;
  239. RT_DEBUG_LOG(RT_DEBUG_USB,
  240. ("HID device enable, protocal id %d\n", pro_id));
  241. protocal = rt_usbh_hid_protocal_find(pro_id);
  242. if(protocal == RT_NULL)
  243. {
  244. rt_kprintf("can't find hid protocal %d\n", pro_id);
  245. intf->user_data = RT_NULL;
  246. return -RT_ERROR;
  247. }
  248. hid = rt_malloc(sizeof(struct uhid));
  249. RT_ASSERT(hid != RT_NULL);
  250. /* initilize the data structure */
  251. rt_memset(hid, 0, sizeof(struct uhid));
  252. intf->user_data = (void*)hid;
  253. hid->protocal = protocal;
  254. for(i=0; i<intf->intf_desc->bNumEndpoints; i++)
  255. {
  256. rt_err_t ret;
  257. uep_desc_t ep_desc;
  258. /* get endpoint descriptor */
  259. rt_usbh_get_endpoint_descriptor(intf->intf_desc, i, &ep_desc);
  260. if(ep_desc == RT_NULL)
  261. {
  262. rt_kprintf("rt_usbh_get_endpoint_descriptor error\n");
  263. return -RT_ERROR;
  264. }
  265. if(USB_EP_ATTR(ep_desc->bmAttributes) != USB_EP_ATTR_INT)
  266. continue;
  267. if(!(ep_desc->bEndpointAddress & USB_DIR_IN)) continue;
  268. ret = rt_usb_hcd_alloc_pipe(intf->device->hcd, &hid->pipe_in,
  269. intf, ep_desc, rt_usbh_hid_callback);
  270. if(ret != RT_EOK) return ret;
  271. }
  272. /* initialize hid protocal */
  273. hid->protocal->init((void*)intf);
  274. pipe = hid->pipe_in;
  275. /* parameter check */
  276. RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
  277. rt_usb_hcd_int_xfer(pipe->intf->device->hcd, hid->pipe_in,
  278. hid->buffer, hid->pipe_in->ep.wMaxPacketSize, timeout);
  279. return RT_EOK;
  280. }
  281. /**
  282. * This function will be invoked when usb device plug out is detected and it would clean
  283. * and release all hub class related resources.
  284. *
  285. * @param arg the argument.
  286. *
  287. * @return the error code, RT_EOK on successfully.
  288. */
  289. static rt_err_t rt_usbh_hid_disable(void* arg)
  290. {
  291. struct uhid* hid;
  292. struct uintf* intf = (struct uintf*)arg;
  293. RT_ASSERT(intf != RT_NULL);
  294. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hid_disable\n"));
  295. hid = (struct uhid*)intf->user_data;
  296. if(hid != RT_NULL)
  297. {
  298. if(hid->pipe_in != RT_NULL)
  299. {
  300. /* free the HID in pipe */
  301. rt_usb_hcd_free_pipe(intf->device->hcd, hid->pipe_in);
  302. }
  303. /* free the hid instance */
  304. rt_free(hid);
  305. }
  306. /* free the instance */
  307. rt_free(intf);
  308. return RT_EOK;
  309. }
  310. /**
  311. * This function will register hid class driver to the usb class driver manager.
  312. * and it should be invoked in the usb system initialization.
  313. *
  314. * @return the error code, RT_EOK on successfully.
  315. */
  316. ucd_t rt_usbh_class_driver_hid(void)
  317. {
  318. rt_list_init(&_protocal_list);
  319. hid_driver.class_code = USB_CLASS_HID;
  320. hid_driver.enable = rt_usbh_hid_enable;
  321. hid_driver.disable = rt_usbh_hid_disable;
  322. return &hid_driver;
  323. }
  324. #endif