1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 USBHID::USBHID(uint8_t output_report_length
, uint8_t input_report_length
, uint16_t vendor_id
, uint16_t product_id
, uint16_t product_release
, bool connect
): USBDevice(vendor_id
, product_id
, product_release
)
26 output_length
= output_report_length
;
27 input_length
= input_report_length
;
34 bool USBHID::send(HID_REPORT
*report
)
36 return write(EPINT_IN
, report
->data
, report
->length
, MAX_HID_REPORT_SIZE
);
39 bool USBHID::sendNB(HID_REPORT
*report
)
41 return writeNB(EPINT_IN
, report
->data
, report
->length
, MAX_HID_REPORT_SIZE
);
45 bool USBHID::read(HID_REPORT
*report
)
47 uint32_t bytesRead
= 0;
49 result
= USBDevice::readEP(EPINT_OUT
, report
->data
, &bytesRead
, MAX_HID_REPORT_SIZE
);
50 if(!readStart(EPINT_OUT
, MAX_HID_REPORT_SIZE
))
52 report
->length
= bytesRead
;
57 bool USBHID::readNB(HID_REPORT
*report
)
59 uint32_t bytesRead
= 0;
61 result
= USBDevice::readEP_NB(EPINT_OUT
, report
->data
, &bytesRead
, MAX_HID_REPORT_SIZE
);
62 // if readEP_NB did not succeed, does not issue a readStart
65 report
->length
= bytesRead
;
66 if(!readStart(EPINT_OUT
, MAX_HID_REPORT_SIZE
))
72 uint16_t USBHID::reportDescLength() {
80 // Route callbacks from lower layers to class(es)
84 // Called in ISR context
85 // Called by USBDevice on Endpoint0 request
86 // This is used to handle extensions to standard requests
87 // and class specific requests
88 // Return true if class handles this request
89 bool USBHID::USBCallback_request() {
91 CONTROL_TRANSFER
* transfer
= getTransferPtr();
92 uint8_t *hidDescriptor
;
94 // Process additional standard requests
96 if ((transfer
->setup
.bmRequestType
.Type
== STANDARD_TYPE
))
98 switch (transfer
->setup
.bRequest
)
101 switch (DESCRIPTOR_TYPE(transfer
->setup
.wValue
))
103 case REPORT_DESCRIPTOR
:
104 if ((reportDesc() != NULL
) \
105 && (reportDescLength() != 0))
107 transfer
->remaining
= reportDescLength();
108 transfer
->ptr
= reportDesc();
109 transfer
->direction
= DEVICE_TO_HOST
;
114 // Find the HID descriptor, after the configuration descriptor
115 hidDescriptor
= findDescriptor(HID_DESCRIPTOR
);
116 if (hidDescriptor
!= NULL
)
118 transfer
->remaining
= HID_DESCRIPTOR_LENGTH
;
119 transfer
->ptr
= hidDescriptor
;
120 transfer
->direction
= DEVICE_TO_HOST
;
134 // Process class-specific requests
136 if (transfer
->setup
.bmRequestType
.Type
== CLASS_TYPE
)
138 switch (transfer
->setup
.bRequest
)
141 // First byte will be used for report ID
142 outputReport
.data
[0] = transfer
->setup
.wValue
& 0xff;
143 outputReport
.length
= transfer
->setup
.wLength
+ 1;
145 transfer
->remaining
= sizeof(outputReport
.data
) - 1;
146 transfer
->ptr
= &outputReport
.data
[1];
147 transfer
->direction
= HOST_TO_DEVICE
;
148 transfer
->notify
= true;
159 #define DEFAULT_CONFIGURATION (1)
162 // Called in ISR context
163 // Set configuration. Return false if the
164 // configuration is not supported
165 bool USBHID::USBCallback_setConfiguration(uint8_t configuration
) {
166 if (configuration
!= DEFAULT_CONFIGURATION
) {
170 // Configure endpoints > 0
171 addEndpoint(EPINT_IN
, MAX_PACKET_SIZE_EPINT
);
172 addEndpoint(EPINT_OUT
, MAX_PACKET_SIZE_EPINT
);
174 // We activate the endpoint to be able to recceive data
175 readStart(EPINT_OUT
, MAX_PACKET_SIZE_EPINT
);
180 uint8_t * USBHID::stringIinterfaceDesc() {
181 static uint8_t stringIinterfaceDescriptor
[] = {
183 STRING_DESCRIPTOR
, //bDescriptorType 0x03
184 'H',0,'I',0,'D',0, //bString iInterface - HID
186 return stringIinterfaceDescriptor
;
189 uint8_t * USBHID::stringIproductDesc() {
190 static uint8_t stringIproductDescriptor
[] = {
192 STRING_DESCRIPTOR
, //bDescriptorType 0x03
193 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
195 return stringIproductDescriptor
;
200 uint8_t * USBHID::reportDesc() {
201 static uint8_t reportDescriptor
[] = {
202 0x06, LSB(0xFFAB), MSB(0xFFAB),
203 0x0A, LSB(0x0200), MSB(0x0200),
204 0xA1, 0x01, // Collection 0x01
205 0x75, 0x08, // report size = 8 bits
206 0x15, 0x00, // logical minimum = 0
207 0x26, 0xFF, 0x00, // logical maximum = 255
208 0x95, input_length
, // report count
210 0x81, 0x02, // Input (array)
211 0x95, output_length
,// report count
213 0x91, 0x02, // Output (array)
214 0xC0 // end collection
217 reportLength
= sizeof(reportDescriptor
);
218 return reportDescriptor
;
221 #define DEFAULT_CONFIGURATION (1)
222 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
223 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
224 + (1 * HID_DESCRIPTOR_LENGTH) \
225 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
227 uint8_t * USBHID::configurationDesc() {
228 static uint8_t configurationDescriptor
[] = {
229 CONFIGURATION_DESCRIPTOR_LENGTH
,// bLength
230 CONFIGURATION_DESCRIPTOR
, // bDescriptorType
231 LSB(TOTAL_DESCRIPTOR_LENGTH
), // wTotalLength (LSB)
232 MSB(TOTAL_DESCRIPTOR_LENGTH
), // wTotalLength (MSB)
233 0x01, // bNumInterfaces
234 DEFAULT_CONFIGURATION
, // bConfigurationValue
235 0x00, // iConfiguration
236 C_RESERVED
| C_SELF_POWERED
, // bmAttributes
237 C_POWER(0), // bMaxPower
239 INTERFACE_DESCRIPTOR_LENGTH
, // bLength
240 INTERFACE_DESCRIPTOR
, // bDescriptorType
241 0x00, // bInterfaceNumber
242 0x00, // bAlternateSetting
243 0x02, // bNumEndpoints
244 HID_CLASS
, // bInterfaceClass
245 HID_SUBCLASS_NONE
, // bInterfaceSubClass
246 HID_PROTOCOL_NONE
, // bInterfaceProtocol
249 HID_DESCRIPTOR_LENGTH
, // bLength
250 HID_DESCRIPTOR
, // bDescriptorType
251 LSB(HID_VERSION_1_11
), // bcdHID (LSB)
252 MSB(HID_VERSION_1_11
), // bcdHID (MSB)
253 0x00, // bCountryCode
254 0x01, // bNumDescriptors
255 REPORT_DESCRIPTOR
, // bDescriptorType
256 (uint8_t)(LSB(this->reportDescLength())), // wDescriptorLength (LSB)
257 (uint8_t)(MSB(this->reportDescLength())), // wDescriptorLength (MSB)
259 ENDPOINT_DESCRIPTOR_LENGTH
, // bLength
260 ENDPOINT_DESCRIPTOR
, // bDescriptorType
261 PHY_TO_DESC(EPINT_IN
), // bEndpointAddress
262 E_INTERRUPT
, // bmAttributes
263 LSB(MAX_PACKET_SIZE_EPINT
), // wMaxPacketSize (LSB)
264 MSB(MAX_PACKET_SIZE_EPINT
), // wMaxPacketSize (MSB)
265 1, // bInterval (milliseconds)
267 ENDPOINT_DESCRIPTOR_LENGTH
, // bLength
268 ENDPOINT_DESCRIPTOR
, // bDescriptorType
269 PHY_TO_DESC(EPINT_OUT
), // bEndpointAddress
270 E_INTERRUPT
, // bmAttributes
271 LSB(MAX_PACKET_SIZE_EPINT
), // wMaxPacketSize (LSB)
272 MSB(MAX_PACKET_SIZE_EPINT
), // wMaxPacketSize (MSB)
273 1, // bInterval (milliseconds)
275 return configurationDescriptor
;