forked from TMaxElectronics/MidiStick_Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_device_hid.c
241 lines (208 loc) · 9.47 KB
/
usb_device_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright 2015 Microchip Technology Inc. (www.microchip.com)
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.
To request to license the code under the MLA license (www.microchip.com/mla_license),
please contact [email protected]
*******************************************************************************/
//DOM-IGNORE-END
/*******************************************************************************
USB Device Human Interface Device (HID) Layer
Company:
Microchip Technology Inc.
File Name:
usb_device_hid.c
Summary:
USB Device Human Interface Device (HID) Layer interface API.
Description:
USB Device Human Interface Device (HID) Layer interface API.
*******************************************************************************/
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include "usb lib/usb_config.h"
#include "usb lib/usb.h"
#include "usb lib/usb_device_hid.h"
// *****************************************************************************
// *****************************************************************************
// Section: File Scope or Global Constants
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
// Section: File Scope Data Types
// *****************************************************************************
// *****************************************************************************
typedef struct __attribute__((packed))
{
unsigned :8;
unsigned :8;
uint8_t reportId;
uint8_t duration;
} USB_SETUP_SET_IDLE_RATE;
typedef struct __attribute__((packed))
{
unsigned :8;
unsigned :8;
uint8_t protocol;
} USB_SETUP_SET_PROTOCOL;
// *****************************************************************************
// *****************************************************************************
// Section: Variables
// *****************************************************************************
// *****************************************************************************
static uint8_t idle_rate;
static uint8_t active_protocol; // [0] Boot Protocol [1] Report Protocol
extern const struct{uint8_t report[HID_RPT01_SIZE];}hid_rpt01;
// *****************************************************************************
// *****************************************************************************
// Section: Prototypes
// *****************************************************************************
// *****************************************************************************
#if defined USER_GET_REPORT_HANDLER
void USER_GET_REPORT_HANDLER(void);
#endif
#if defined USER_SET_REPORT_HANDLER
extern void USER_SET_REPORT_HANDLER(void);
#endif
// *****************************************************************************
// *****************************************************************************
// Section: Macros or Functions
// *****************************************************************************
// *****************************************************************************
//To implement a set idle rate callback function in the application,
//Make sure "#define USB_DEVICE_HID_IDLE_RATE_CALLBACK(reportID, newIdleRate) USBHIDCBSetIdleRateHandler(reportID, newIdleRate)"
//is placed in your usb_config.h file, and then in your application .c file,
//add the void USBHIDCBSetIdleRateHandler(reportID, newIdleRate) function
//implementation that saves the new idle rate and report ID info, so that it
//gets used later when sending subsequent HID input report packets to the host.
#ifndef USB_DEVICE_HID_IDLE_RATE_CALLBACK
#define USB_DEVICE_HID_IDLE_RATE_CALLBACK(reportId, idleRate)
#else
extern void USB_DEVICE_HID_IDLE_RATE_CALLBACK(uint8_t reportId, uint8_t idleRate);
#endif
/********************************************************************
Function:
void USBCheckHIDRequest(void)
Summary:
This routine handles HID specific request that happen on EP0.
This function should be called from the USBCBCheckOtherReq() call back
function whenever implementing a HID device.
Description:
This routine handles HID specific request that happen on EP0. These
include, but are not limited to, requests for the HID report
descriptors. This function should be called from the
USBCBCheckOtherReq() call back function whenever using an HID device.
Typical Usage:
<code>
void USBCBCheckOtherReq(void)
{
//Since the stack didn't handle the request I need to check
// my class drivers to see if it is for them
USBCheckHIDRequest();
}
</code>
PreCondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
*******************************************************************/
void USBCheckHIDRequest(void)
{
if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return;
if(SetupPkt.bIntfID != HID_INTF_ID) return;
/*
* There are two standard requests that hid.c may support.
* 1. GET_DSC(DSC_HID,DSC_RPT,DSC_PHY);
* 2. SET_DSC(DSC_HID,DSC_RPT,DSC_PHY);
*/
if(SetupPkt.bRequest == USB_REQUEST_GET_DESCRIPTOR)
{
switch(SetupPkt.bDescriptorType)
{
case DSC_HID: //HID Descriptor
if(USBActiveConfiguration == 1)
{
USBEP0SendROMPtr(
(const uint8_t*)&configDescriptor1 + 72, //72 is a magic number. It is the offset from start of the configuration descriptor to the start of the HID descriptor.
sizeof(USB_HID_DSC)+3,
USB_EP0_INCLUDE_ZERO);
}
break;
case DSC_RPT: //Report Descriptor
//if(USBActiveConfiguration == 1)
{
USBEP0SendROMPtr(
(const uint8_t*)&hid_rpt01,
HID_RPT01_SIZE, //See usbcfg.h
USB_EP0_INCLUDE_ZERO);
}
break;
case DSC_PHY: //Physical Descriptor
//Note: The below placeholder code is commented out. HID Physical Descriptors are optional and are not used
//in many types of HID applications. If an application does not have a physical descriptor,
//then the device should return STALL in response to this request (stack will do this automatically
//if no-one claims ownership of the control transfer).
//If an application does implement a physical descriptor, then make sure to declare
//hid_phy01 (rom structure containing the descriptor data), and hid_phy01 (the size of the descriptors in uint8_ts),
//and then uncomment the below code.
//if(USBActiveConfiguration == 1)
//{
// USBEP0SendROMPtr((const uint8_t*)&hid_phy01, sizeof(hid_phy01), USB_EP0_INCLUDE_ZERO);
//}
break;
}//end switch(SetupPkt.bDescriptorType)
}//end if(SetupPkt.bRequest == GET_DSC)
if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD)
{
return;
}
switch(SetupPkt.bRequest)
{
case GET_REPORT:
#if defined USER_GET_REPORT_HANDLER
USER_GET_REPORT_HANDLER();
#endif
break;
case SET_REPORT:
#if defined USER_SET_REPORT_HANDLER
USER_SET_REPORT_HANDLER();
#endif
break;
case GET_IDLE:
USBEP0SendRAMPtr(
(uint8_t*)&idle_rate,
1,
USB_EP0_INCLUDE_ZERO);
break;
case SET_IDLE:
USBEP0Transmit(USB_EP0_NO_DATA);
idle_rate = SetupPkt.W_Value.byte.HB;
USB_DEVICE_HID_IDLE_RATE_CALLBACK(SetupPkt.W_Value.byte.LB, idle_rate);
break;
case GET_PROTOCOL:
USBEP0SendRAMPtr(
(uint8_t*)&active_protocol,
1,
USB_EP0_NO_OPTIONS);
break;
case SET_PROTOCOL:
USBEP0Transmit(USB_EP0_NO_DATA);
active_protocol = SetupPkt.W_Value.byte.LB;
break;
}//end switch(SetupPkt.bRequest)
}//end USBCheckHIDRequest