1 /*
2  * Copyright (c) 2022, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbd_core.h"
7 #include "usbd_hid.h"
8 
hid_class_interface_request_handler(uint8_t busid,struct usb_setup_packet * setup,uint8_t ** data,uint32_t * len)9 static int hid_class_interface_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
10 {
11     USB_LOG_DBG("HID Class request: "
12                 "bRequest 0x%02x\r\n",
13                 setup->bRequest);
14 
15     uint8_t intf_num = LO_BYTE(setup->wIndex);
16 
17     switch (setup->bRequest) {
18         case HID_REQUEST_GET_REPORT:
19             /* report id ,report type */
20             usbd_hid_get_report(busid, intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), data, len);
21             break;
22         case HID_REQUEST_GET_IDLE:
23             (*data)[0] = usbd_hid_get_idle(busid, intf_num, LO_BYTE(setup->wValue));
24             *len = 1;
25             break;
26         case HID_REQUEST_GET_PROTOCOL:
27             (*data)[0] = usbd_hid_get_protocol(busid, intf_num);
28             *len = 1;
29             break;
30         case HID_REQUEST_SET_REPORT:
31             /* report id ,report type, report, report len */
32             usbd_hid_set_report(busid, intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue), *data, *len);
33             break;
34         case HID_REQUEST_SET_IDLE:
35             /* report id, duration */
36             usbd_hid_set_idle(busid, intf_num, LO_BYTE(setup->wValue), HI_BYTE(setup->wValue));
37             break;
38         case HID_REQUEST_SET_PROTOCOL:
39             /* protocol */
40             usbd_hid_set_protocol(busid, intf_num, LO_BYTE(setup->wValue));
41             break;
42 
43         default:
44             USB_LOG_WRN("Unhandled HID Class bRequest 0x%02x\r\n", setup->bRequest);
45             return -1;
46     }
47 
48     return 0;
49 }
50 
usbd_hid_init_intf(uint8_t busid,struct usbd_interface * intf,const uint8_t * desc,uint32_t desc_len)51 struct usbd_interface *usbd_hid_init_intf(uint8_t busid, struct usbd_interface *intf, const uint8_t *desc, uint32_t desc_len)
52 {
53     (void)busid;
54 
55     intf->class_interface_handler = hid_class_interface_request_handler;
56     intf->class_endpoint_handler = NULL;
57     intf->vendor_handler = NULL;
58     intf->notify_handler = NULL;
59 
60     intf->hid_report_descriptor = desc;
61     intf->hid_report_descriptor_len = desc_len;
62     return intf;
63 }
64 
65 /*
66  * Appendix G: HID Request Support Requirements
67  *
68  * The following table enumerates the requests that need to be supported by various types of HID class devices.
69  * Device type     GetReport   SetReport   GetIdle     SetIdle     GetProtocol SetProtocol
70  * ------------------------------------------------------------------------------------------
71  * Boot Mouse      Required    Optional    Optional    Optional    Required    Required
72  * Non-Boot Mouse  Required    Optional    Optional    Optional    Optional    Optional
73  * Boot Keyboard   Required    Optional    Required    Required    Required    Required
74  * Non-Boot Keybrd Required    Optional    Required    Required    Optional    Optional
75  * Other Device    Required    Optional    Optional    Optional    Optional    Optional
76  */
77 
usbd_hid_get_report(uint8_t busid,uint8_t intf,uint8_t report_id,uint8_t report_type,uint8_t ** data,uint32_t * len)78 __WEAK void usbd_hid_get_report(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t **data, uint32_t *len)
79 {
80     (void)busid;
81     (void)intf;
82     (void)report_id;
83     (void)report_type;
84     (*data[0]) = 0;
85     *len = 1;
86 }
87 
usbd_hid_get_idle(uint8_t busid,uint8_t intf,uint8_t report_id)88 __WEAK uint8_t usbd_hid_get_idle(uint8_t busid, uint8_t intf, uint8_t report_id)
89 {
90     (void)busid;
91     (void)intf;
92     (void)report_id;
93     return 0;
94 }
95 
usbd_hid_get_protocol(uint8_t busid,uint8_t intf)96 __WEAK uint8_t usbd_hid_get_protocol(uint8_t busid, uint8_t intf)
97 {
98     (void)busid;
99     (void)intf;
100     return 0;
101 }
102 
usbd_hid_set_report(uint8_t busid,uint8_t intf,uint8_t report_id,uint8_t report_type,uint8_t * report,uint32_t report_len)103 __WEAK void usbd_hid_set_report(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t *report, uint32_t report_len)
104 {
105     (void)busid;
106     (void)intf;
107     (void)report_id;
108     (void)report_type;
109     (void)report;
110     (void)report_len;
111 }
112 
usbd_hid_set_idle(uint8_t busid,uint8_t intf,uint8_t report_id,uint8_t duration)113 __WEAK void usbd_hid_set_idle(uint8_t busid, uint8_t intf, uint8_t report_id, uint8_t duration)
114 {
115     (void)busid;
116     (void)intf;
117     (void)report_id;
118     (void)duration;
119 }
120 
usbd_hid_set_protocol(uint8_t busid,uint8_t intf,uint8_t protocol)121 __WEAK void usbd_hid_set_protocol(uint8_t busid, uint8_t intf, uint8_t protocol)
122 {
123     (void)busid;
124     (void)intf;
125     (void)protocol;
126 }