hid.c 11 KB

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