hid.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * File : hid.c
  3. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017Äê3ÔÂ13ÈÕ Urey the first version
  8. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtservice.h>
  12. #include <rtdevice.h>
  13. #include "drivers/usb_common.h"
  14. #include "drivers/usb_device.h"
  15. #include "hid.h"
  16. #ifdef RT_USB_DEVICE_HID
  17. struct hid_s
  18. {
  19. uep_t ep_in;
  20. uep_t ep_out;
  21. int status;
  22. rt_uint16_t protocol;
  23. rt_uint8_t report_buf[MAX_REPORT_SIZE];
  24. };
  25. /* CustomHID_ConfigDescriptor */
  26. const rt_uint8_t _report_desc[] __attribute__ ((packed))=
  27. {
  28. 0x06, 0xFF, 0x00, /* USAGE_PAGE (Vendor Page: 0xFF00) */
  29. 0x09, 0x01, /* USAGE (Demo Kit) */
  30. 0xa1, 0x01, /* COLLECTION (Application) */
  31. /* 6 */
  32. /* Led 1 */
  33. 0x85, 0x01, /* REPORT_ID (1) */
  34. 0x09, 0x01, /* USAGE (LED 1) */
  35. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  36. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  37. 0x75, 0x08, /* REPORT_SIZE (8) */
  38. 0x95, 0x01, /* REPORT_COUNT (1) */
  39. 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  40. 0x85, 0x01, /* REPORT_ID (1) */
  41. 0x09, 0x01, /* USAGE (LED 1) */
  42. 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
  43. /* 26 */
  44. /* Led 2 */
  45. 0x85, 0x02, /* REPORT_ID 2 */
  46. 0x09, 0x02, /* USAGE (LED 2) */
  47. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  48. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  49. 0x75, 0x08, /* REPORT_SIZE (8) */
  50. 0x95, 0x01, /* REPORT_COUNT (1) */
  51. 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  52. 0x85, 0x02, /* REPORT_ID (2) */
  53. 0x09, 0x02, /* USAGE (LED 2) */
  54. 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
  55. /* 46 */
  56. /* Led 3 */
  57. 0x85, 0x03, /* REPORT_ID (3) */
  58. 0x09, 0x03, /* USAGE (LED 3) */
  59. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  60. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  61. 0x75, 0x08, /* REPORT_SIZE (8) */
  62. 0x95, 0x01, /* REPORT_COUNT (1) */
  63. 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  64. 0x85, 0x03, /* REPORT_ID (3) */
  65. 0x09, 0x03, /* USAGE (LED 3) */
  66. 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
  67. /* 66 */
  68. /* Led 4 */
  69. 0x85, 0x04, /* REPORT_ID 4) */
  70. 0x09, 0x04, /* USAGE (LED 4) */
  71. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  72. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  73. 0x75, 0x08, /* REPORT_SIZE (8) */
  74. 0x95, 0x01, /* REPORT_COUNT (1) */
  75. 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  76. 0x85, 0x04, /* REPORT_ID (4) */
  77. 0x09, 0x04, /* USAGE (LED 4) */
  78. 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
  79. /* 86 */
  80. /* key Push Button */
  81. 0x85, 0x05, /* REPORT_ID (5) */
  82. 0x09, 0x05, /* USAGE (Push Button) */
  83. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  84. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  85. 0x75, 0x01, /* REPORT_SIZE (1) */
  86. 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
  87. 0x09, 0x05, /* USAGE (Push Button) */
  88. 0x75, 0x01, /* REPORT_SIZE (1) */
  89. 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  90. 0x75, 0x07, /* REPORT_SIZE (7) */
  91. 0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
  92. 0x85, 0x05, /* REPORT_ID (2) */
  93. 0x75, 0x07, /* REPORT_SIZE (7) */
  94. 0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
  95. /* 114 */
  96. /* Tamper Push Button */
  97. 0x85, 0x06, /* REPORT_ID (6) */
  98. 0x09, 0x06, /* USAGE (Tamper Push Button) */
  99. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  100. 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
  101. 0x75, 0x01, /* REPORT_SIZE (1) */
  102. 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
  103. 0x09, 0x06, /* USAGE (Tamper Push Button) */
  104. 0x75, 0x01, /* REPORT_SIZE (1) */
  105. 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  106. 0x75, 0x07, /* REPORT_SIZE (7) */
  107. 0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
  108. 0x85, 0x06, /* REPORT_ID (6) */
  109. 0x75, 0x07, /* REPORT_SIZE (7) */
  110. 0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
  111. /* 142 */
  112. /* ADC IN */
  113. 0x85, 0x07, /* REPORT_ID (7) */
  114. 0x09, 0x07, /* USAGE (ADC IN) */
  115. 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
  116. 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
  117. 0x75, 0x08, /* REPORT_SIZE (8) */
  118. 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
  119. 0x85, 0x07, /* REPORT_ID (7) */
  120. 0x09, 0x07, /* USAGE (ADC in) */
  121. 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
  122. /* 161 */
  123. 0xc0 /* END_COLLECTION */
  124. }; /* CustomHID_ReportDescriptor */
  125. static struct udevice_descriptor _dev_desc =
  126. {
  127. USB_DESC_LENGTH_DEVICE, //bLength;
  128. USB_DESC_TYPE_DEVICE, //type;
  129. USB_BCD_VERSION, //bcdUSB;
  130. USB_CLASS_HID, //bDeviceClass;
  131. 0x00, //bDeviceSubClass;
  132. 0x00, //bDeviceProtocol;
  133. 64, //bMaxPacketSize0;
  134. _VENDOR_ID, //idVendor;
  135. _PRODUCT_ID, //idProduct;
  136. USB_BCD_DEVICE, //bcdDevice;
  137. USB_STRING_MANU_INDEX, //iManufacturer;
  138. USB_STRING_PRODUCT_INDEX, //iProduct;
  139. USB_STRING_SERIAL_INDEX, //iSerialNumber;
  140. USB_DYNAMIC, //bNumConfigurations;
  141. };
  142. static struct usb_qualifier_descriptor _dev_qualifier =
  143. {
  144. sizeof(_dev_qualifier),
  145. USB_DESC_TYPE_DEVICEQUALIFIER,
  146. 0x0200,
  147. USB_CLASS_HID,
  148. 0x00,
  149. 64,
  150. 0x01,
  151. 0,
  152. };
  153. /* hid interface descriptor */
  154. const static struct uhid_comm_descriptor _hid_comm_desc =
  155. {
  156. #ifdef RT_USB_DEVICE_COMPOSITE
  157. /* Interface Association Descriptor */
  158. USB_DESC_LENGTH_IAD,
  159. USB_DESC_TYPE_IAD,
  160. USB_DYNAMIC,
  161. 0x02,
  162. 0x03, /* bInterfaceClass: HID */
  163. USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  164. USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  165. 0x00,
  166. #endif
  167. /* Interface Descriptor */
  168. USB_DESC_LENGTH_INTERFACE,
  169. USB_DESC_TYPE_INTERFACE,
  170. USB_DYNAMIC, /* bInterfaceNumber: Number of Interface */
  171. 0x00, /* bAlternateSetting: Alternate setting */
  172. 0x02, /* bNumEndpoints */
  173. 0x03, /* bInterfaceClass: HID */
  174. USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
  175. USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
  176. 0, /* iInterface: Index of string descriptor */
  177. /* HID Descriptor */
  178. HID_DESCRIPTOR_SIZE, /* bLength: HID Descriptor size */
  179. HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
  180. 0x0110, /* bcdHID: HID Class Spec release number */
  181. 0x00, /* bCountryCode: Hardware target country */
  182. 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
  183. 0x22, /* bDescriptorType */
  184. sizeof(_report_desc), /* wItemLength: Total length of Report descriptor */
  185. /* Endpoint Descriptor IN */
  186. USB_DESC_LENGTH_ENDPOINT,
  187. USB_DESC_TYPE_ENDPOINT,
  188. USB_DYNAMIC | USB_DIR_IN,
  189. USB_EP_ATTR_INT,
  190. 0x08,
  191. 0x20,
  192. /* Endpoint Descriptor OUT */
  193. USB_DESC_LENGTH_ENDPOINT,
  194. USB_DESC_TYPE_ENDPOINT,
  195. USB_DYNAMIC | USB_DIR_OUT,
  196. USB_EP_ATTR_INT,
  197. 0x08,
  198. 0x20,
  199. };
  200. const static char* _ustring[] =
  201. {
  202. "Language",
  203. "RT-Thread Team.",
  204. "RTT HID-Device",
  205. "32021919830108",
  206. "Configuration",
  207. "Interface",
  208. };
  209. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  210. static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
  211. {
  212. unsigned char *buf = (unsigned char*)ptr;
  213. int i, j;
  214. for (i=0; i<buflen; i+=16)
  215. {
  216. rt_kprintf("%06x: ", i);
  217. for (j=0; j<16; j++)
  218. if (i+j < buflen)
  219. rt_kprintf("%02x ", buf[i+j]);
  220. else
  221. rt_kprintf(" ");
  222. rt_kprintf(" ");
  223. for (j=0; j<16; j++)
  224. if (i+j < buflen)
  225. rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.');
  226. rt_kprintf("\n");
  227. }
  228. }
  229. static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
  230. {
  231. struct hid_s *data;
  232. RT_ASSERT(func != RT_NULL);
  233. RT_ASSERT(func->device != RT_NULL);
  234. rt_kprintf("%s size = %d\n",__func__,size);
  235. data = (struct hid_s *) func->user_data;
  236. if(size != 0)
  237. {
  238. dump_hex(data->ep_out->buffer,size);
  239. }
  240. data->ep_out->request.buffer = data->ep_out->buffer;
  241. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  242. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  243. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  244. return RT_EOK;
  245. }
  246. static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
  247. {
  248. struct hid_s *data;
  249. RT_ASSERT(func != RT_NULL);
  250. RT_ASSERT(func->device != RT_NULL);
  251. data = (struct hid_s *) func->user_data;
  252. return RT_EOK;
  253. }
  254. static rt_err_t _hid_set_report_callback(udevice_t device, rt_size_t size)
  255. {
  256. RT_DEBUG_LOG(RT_DEBUG_USB, ("_hid_set_report_callback\n"));
  257. if(size != 0)
  258. {
  259. rt_kprintf("HID set report callback:\n");
  260. dump_hex(device->dcd->ep0.request.buffer - size,size);
  261. }
  262. dcd_ep0_send_status(device->dcd);
  263. return RT_EOK;
  264. }
  265. /**
  266. * This function will handle hid interface bRequest.
  267. *
  268. * @param device the usb device object.
  269. * @param setup the setup bRequest.
  270. *
  271. * @return RT_EOK on successful.
  272. */
  273. static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
  274. {
  275. RT_ASSERT(func != RT_NULL);
  276. RT_ASSERT(func->device != RT_NULL);
  277. RT_ASSERT(setup != RT_NULL);
  278. struct hid_s *data = (struct hid_s *) func->user_data;
  279. if(setup->wIndex != 0)
  280. return -RT_EIO;
  281. switch (setup->bRequest)
  282. {
  283. case USB_REQ_GET_DESCRIPTOR:
  284. if((setup->wValue >> 8) == USB_DESC_TYPE_REPORT)
  285. {
  286. rt_kprintf("HID get report descriptor\n");
  287. rt_kprintf("sizeof _report_desc = %d bytes\n",sizeof(_report_desc));
  288. rt_usbd_ep0_write(func->device, (void *)(&_report_desc[0]), sizeof(_report_desc));
  289. }
  290. else if((setup->wValue >> 8) == USB_DESC_TYPE_HID)
  291. {
  292. rt_kprintf("HID get hid descriptor\n");
  293. rt_usbd_ep0_write(func->device, (void *)(&_hid_comm_desc.hid_desc), sizeof(struct uhid_descriptor));
  294. }
  295. break;
  296. case USB_HID_REQ_GET_REPORT:
  297. rt_kprintf("HID get report \n");
  298. rt_kprintf("wLength = %d\n",setup->wLength);
  299. if(setup->wLength == 0)
  300. {
  301. rt_usbd_ep0_set_stall(func->device);
  302. break;
  303. }
  304. if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE))
  305. setup->wLength = MAX_REPORT_SIZE;
  306. rt_usbd_ep0_write(func->device, data->report_buf,setup->wLength);
  307. break;
  308. case USB_HID_REQ_GET_IDLE:
  309. rt_kprintf("HID get idle \n");
  310. dcd_ep0_send_status(func->device->dcd);
  311. break;
  312. case USB_HID_REQ_GET_PROTOCOL:
  313. rt_kprintf("HID get protocol \n");
  314. rt_usbd_ep0_write(func->device, &data->protocol,2);
  315. break;
  316. case USB_HID_REQ_SET_REPORT:
  317. rt_kprintf("HID set report \n");
  318. rt_kprintf("set report size = %d\n",setup->wLength);
  319. if((setup->wLength == 0) || (setup->wLength > MAX_REPORT_SIZE))
  320. rt_usbd_ep0_set_stall(func->device);
  321. rt_usbd_ep0_read(func->device, data->report_buf, setup->wLength, _hid_set_report_callback);
  322. break;
  323. case USB_HID_REQ_SET_IDLE:
  324. {
  325. int duration = (setup->wValue >> 8);
  326. int report_id = (setup->wValue & 0xFF);
  327. rt_kprintf("HID set idle \n");
  328. rt_kprintf("duration = %d,report_id = %d\n",duration,report_id);
  329. dcd_ep0_send_status(func->device->dcd);
  330. }
  331. break;
  332. case USB_HID_REQ_SET_PROTOCOL:
  333. rt_kprintf("HID set protocol \n");
  334. data->protocol = setup->wValue;
  335. dcd_ep0_send_status(func->device->dcd);
  336. break;
  337. }
  338. return RT_EOK;
  339. }
  340. /**
  341. * This function will run cdc function, it will be called on handle set configuration bRequest.
  342. *
  343. * @param func the usb function object.
  344. *
  345. * @return RT_EOK on successful.
  346. */
  347. static rt_err_t _function_enable(ufunction_t func)
  348. {
  349. struct hid_s *data;
  350. RT_ASSERT(func != RT_NULL);
  351. RT_ASSERT(func->device != RT_NULL);
  352. data = (struct hid_s *) func->user_data;
  353. RT_DEBUG_LOG(RT_DEBUG_USB, ("hid function enable\n"));
  354. //
  355. // _vcom_reset_state(func);
  356. //
  357. data->ep_out->buffer = rt_malloc(HID_RX_BUFSIZE);
  358. data->ep_out->request.buffer = data->ep_out->buffer;
  359. data->ep_out->request.size = EP_MAXPACKET(data->ep_out);
  360. data->ep_out->request.req_type = UIO_REQUEST_READ_BEST;
  361. rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
  362. return RT_EOK;
  363. }
  364. /**
  365. * This function will stop cdc function, it will be called on handle set configuration bRequest.
  366. *
  367. * @param func the usb function object.
  368. *
  369. * @return RT_EOK on successful.
  370. */
  371. static rt_err_t _function_disable(ufunction_t func)
  372. {
  373. struct hid_s *data;
  374. RT_ASSERT(func != RT_NULL);
  375. RT_ASSERT(func->device != RT_NULL);
  376. data = (struct hid_s *) func->user_data;
  377. RT_DEBUG_LOG(RT_DEBUG_USB, ("hid function disable\n"));
  378. if(data->ep_out->buffer != RT_NULL)
  379. {
  380. rt_free(data->ep_out->buffer);
  381. data->ep_out->buffer = RT_NULL;
  382. }
  383. return RT_EOK;
  384. }
  385. static struct ufunction_ops ops =
  386. {
  387. _function_enable,
  388. _function_disable,
  389. RT_NULL,
  390. };
  391. /**
  392. * This function will configure hid descriptor.
  393. *
  394. * @param comm the communication interface number.
  395. * @param data the data interface number.
  396. *
  397. * @return RT_EOK on successful.
  398. */
  399. static rt_err_t _hid_descriptor_config(uhid_comm_desc_t hid, rt_uint8_t cintf_nr)
  400. {
  401. #ifdef RT_USB_DEVICE_COMPOSITE
  402. hid->iad_desc.bFirstInterface = cintf_nr;
  403. #endif
  404. return RT_EOK;
  405. }
  406. static void rt_usb_hid_init(struct ufunction *func)
  407. {
  408. }
  409. /**
  410. * This function will create a hid function instance.
  411. *
  412. * @param device the usb device object.
  413. *
  414. * @return RT_EOK on successful.
  415. */
  416. ufunction_t rt_usbd_function_hid_create(udevice_t device)
  417. {
  418. ufunction_t func;
  419. struct hid_s *data;
  420. uintf_t hid_intf;
  421. ualtsetting_t hid_setting;
  422. uhid_comm_desc_t hid_desc;
  423. /* parameter check */
  424. RT_ASSERT(device != RT_NULL);
  425. /* set usb device string description */
  426. rt_usbd_device_set_string(device, _ustring);
  427. /* create a cdc function */
  428. func = rt_usbd_function_new(device, &_dev_desc, &ops);
  429. rt_usbd_device_set_qualifier(device, &_dev_qualifier);
  430. /* allocate memory for cdc vcom data */
  431. data = (struct hid_s*)rt_malloc(sizeof(struct hid_s));
  432. rt_memset(data, 0, sizeof(struct hid_s));
  433. func->user_data = (void*)data;
  434. /* initilize hid */
  435. rt_usb_hid_init(func);
  436. /* create an interface object */
  437. hid_intf = rt_usbd_interface_new(device, _interface_handler);
  438. /* create an alternate setting object */
  439. hid_setting = rt_usbd_altsetting_new(sizeof(struct uhid_comm_descriptor));
  440. /* config desc in alternate setting */
  441. rt_usbd_altsetting_config_descriptor(hid_setting, &_hid_comm_desc, (rt_off_t)&((uhid_comm_desc_t)0)->intf_desc);
  442. /* configure the hid interface descriptor */
  443. _hid_descriptor_config(hid_setting->desc, hid_intf->intf_num);
  444. /* create endpoint */
  445. hid_desc = (uhid_comm_desc_t)hid_setting->desc;
  446. data->ep_out = rt_usbd_endpoint_new(&hid_desc->ep_out_desc, _ep_out_handler);
  447. data->ep_in = rt_usbd_endpoint_new(&hid_desc->ep_in_desc, _ep_in_handler);
  448. /* add the int out and int in endpoint to the alternate setting */
  449. rt_usbd_altsetting_add_endpoint(hid_setting, data->ep_out);
  450. rt_usbd_altsetting_add_endpoint(hid_setting, data->ep_in);
  451. /* add the alternate setting to the interface, then set default setting */
  452. rt_usbd_interface_add_altsetting(hid_intf, hid_setting);
  453. rt_usbd_set_altsetting(hid_intf, 0);
  454. /* add the interface to the mass storage function */
  455. rt_usbd_function_add_interface(func, hid_intf);
  456. return func;
  457. }
  458. #endif /* RT_USB_DEVICE_HID */