hid.c 11 KB

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