• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(struct usb_setup_packet * setup,uint8_t ** data,uint32_t * len)9 static int hid_class_interface_request_handler(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(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(intf_num, LO_BYTE(setup->wValue));
24             *len = 1;
25             break;
26         case HID_REQUEST_GET_PROTOCOL:
27             (*data)[0] = usbd_hid_get_protocol(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(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(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(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(struct usbd_interface * intf,const uint8_t * desc,uint32_t desc_len)51 struct usbd_interface *usbd_hid_init_intf(struct usbd_interface *intf, const uint8_t *desc, uint32_t desc_len)
52 {
53     intf->class_interface_handler = hid_class_interface_request_handler;
54     intf->class_endpoint_handler = NULL;
55     intf->vendor_handler = NULL;
56     intf->notify_handler = NULL;
57 
58     intf->hid_report_descriptor = desc;
59     intf->hid_report_descriptor_len = desc_len;
60     return intf;
61 }
62 
usbd_hid_get_report(uint8_t intf,uint8_t report_id,uint8_t report_type,uint8_t ** data,uint32_t * len)63 __WEAK void usbd_hid_get_report(uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t **data, uint32_t *len)
64 {
65     (*data[0]) = 0;
66     *len = 1;
67 }
68 
usbd_hid_get_idle(uint8_t intf,uint8_t report_id)69 __WEAK uint8_t usbd_hid_get_idle(uint8_t intf, uint8_t report_id)
70 {
71     return 0;
72 }
73 
usbd_hid_get_protocol(uint8_t intf)74 __WEAK uint8_t usbd_hid_get_protocol(uint8_t intf)
75 {
76     return 0;
77 }
78 
usbd_hid_set_report(uint8_t intf,uint8_t report_id,uint8_t report_type,uint8_t * report,uint32_t report_len)79 __WEAK void usbd_hid_set_report(uint8_t intf, uint8_t report_id, uint8_t report_type, uint8_t *report, uint32_t report_len)
80 {
81 }
82 
usbd_hid_set_idle(uint8_t intf,uint8_t report_id,uint8_t duration)83 __WEAK void usbd_hid_set_idle(uint8_t intf, uint8_t report_id, uint8_t duration)
84 {
85 }
86 
usbd_hid_set_protocol(uint8_t intf,uint8_t protocol)87 __WEAK void usbd_hid_set_protocol(uint8_t intf, uint8_t protocol)
88 {
89 }