hid.c 11 KB

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