-
Notifications
You must be signed in to change notification settings - Fork 1
/
usb_hid.c
110 lines (94 loc) · 3.26 KB
/
usb_hid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "config.h"
#include "ch.h"
#include "hal.h"
#include "usb_hid.h"
#include "usbcfg.h"
hid_data_t hid_in_data, hid_out_data;
void hid_receive(USBDriver *usbp)
{
chSysLockFromISR();
usbStartReceiveI(usbp, HID_OUT_EP_ADDRESS, hid_out_data.stream, sizeof(hid_out_data.stream));
chSysUnlockFromISR();
}
void hid_transmit(USBDriver *usbp)
{
if (usbp->state != USB_ACTIVE) return;
chSysLockFromISR();
usbStartTransmitI(usbp, HID_IN_EP_ADDRESS, hid_in_data.stream, sizeof(hid_in_data.stream));
chSysUnlockFromISR();
}
bool hidRequestsHook(USBDriver *usbp)
{
const USBDescriptor *dp;
if ((usbp->setup[0] & (USB_RTYPE_TYPE_MASK | USB_RTYPE_RECIPIENT_MASK)) ==
(USB_RTYPE_TYPE_STD | USB_RTYPE_RECIPIENT_INTERFACE)) {
switch (usbp->setup[1]) {
case USB_REQ_GET_DESCRIPTOR:
dp = usbp->config->get_descriptor_cb(
usbp, usbp->setup[3], usbp->setup[2],
usbFetchWord(&usbp->setup[4])
);
if (dp == NULL) return FALSE;
usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
return TRUE;
default:
return FALSE;
}
}
if (((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE) &&
(usbp->setup[1] == USB_REQ_SET_INTERFACE)) {
usbSetupTransfer(usbp, NULL, 0, NULL);
return true;
}
if ((usbp->setup[0] & (USB_RTYPE_TYPE_MASK | USB_RTYPE_RECIPIENT_MASK)) ==
(USB_RTYPE_TYPE_CLASS | USB_RTYPE_RECIPIENT_INTERFACE)) {
switch (usbp->setup[1]) {
case HID_GET_REPORT_REQUEST:
usbSetupTransfer(usbp, hid_in_data.stream, sizeof(hid_in_data.stream), NULL);
return TRUE;
case HID_GET_IDLE_REQUEST:
usbSetupTransfer(usbp, NULL, 0, NULL);
return TRUE;
case HID_GET_PROTOCOL_REQUEST:
return TRUE;
case HID_SET_REPORT_REQUEST:
usbSetupTransfer(usbp, NULL, 0, NULL);
return TRUE;
case HID_SET_IDLE_REQUEST:
usbSetupTransfer(usbp, NULL, 0, NULL);
return TRUE;
case HID_SET_PROTOCOL_REQUEST:
return TRUE;
default:
return FALSE;
}
}
if ((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) {
return sduRequestsHook(usbp);
}
return FALSE;
}
// Callback for the IN endpoint (INTERRUPT), device -> host
void hidDataTransmitted(USBDriver *usbp, usbep_t ep)
{
(void)usbp;
(void)ep;
}
// Callback for the OUT endpoint (INTERRUPT), host -> device
void hidDataReceived(USBDriver *usbp, usbep_t ep)
{
(void)usbp;
(void)ep;
}