fork from va009039/USBLocalFileSystem
Dependents: 11u35_usbLocalFilesystem
Fork of USBLocalFileSystem by
USBMSD2/USB_HID.cpp@11:c396747794c6, 2016-03-04 (annotated)
- Committer:
- k4zuki
- Date:
- Fri Mar 04 10:27:29 2016 +0000
- Revision:
- 11:c396747794c6
- Parent:
- 0:39eb4d5b97df
remove SWSPI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:39eb4d5b97df | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
va009039 | 0:39eb4d5b97df | 2 | * |
va009039 | 0:39eb4d5b97df | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
va009039 | 0:39eb4d5b97df | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
va009039 | 0:39eb4d5b97df | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
va009039 | 0:39eb4d5b97df | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
va009039 | 0:39eb4d5b97df | 7 | * Software is furnished to do so, subject to the following conditions: |
va009039 | 0:39eb4d5b97df | 8 | * |
va009039 | 0:39eb4d5b97df | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
va009039 | 0:39eb4d5b97df | 10 | * substantial portions of the Software. |
va009039 | 0:39eb4d5b97df | 11 | * |
va009039 | 0:39eb4d5b97df | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
va009039 | 0:39eb4d5b97df | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
va009039 | 0:39eb4d5b97df | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
va009039 | 0:39eb4d5b97df | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 | 0:39eb4d5b97df | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
va009039 | 0:39eb4d5b97df | 17 | */ |
va009039 | 0:39eb4d5b97df | 18 | |
va009039 | 0:39eb4d5b97df | 19 | #include "USB_HID.h" |
va009039 | 0:39eb4d5b97df | 20 | |
va009039 | 0:39eb4d5b97df | 21 | #if (DEBUG2 > 3) |
va009039 | 0:39eb4d5b97df | 22 | #define HID_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 0:39eb4d5b97df | 23 | #else |
va009039 | 0:39eb4d5b97df | 24 | #define HID_DBG(...) while(0) |
va009039 | 0:39eb4d5b97df | 25 | #endif |
va009039 | 0:39eb4d5b97df | 26 | |
va009039 | 0:39eb4d5b97df | 27 | USB_HID::USB_HID(USBDevice* device, uint8_t output_report_length, uint8_t input_report_length) : _device(device) |
va009039 | 0:39eb4d5b97df | 28 | { |
va009039 | 0:39eb4d5b97df | 29 | HID_DBG("device=%p", device); |
va009039 | 0:39eb4d5b97df | 30 | |
va009039 | 0:39eb4d5b97df | 31 | output_length = output_report_length; |
va009039 | 0:39eb4d5b97df | 32 | input_length = input_report_length; |
va009039 | 0:39eb4d5b97df | 33 | } |
va009039 | 0:39eb4d5b97df | 34 | |
va009039 | 0:39eb4d5b97df | 35 | bool USB_HID::send(HID_REPORT *report) |
va009039 | 0:39eb4d5b97df | 36 | { |
va009039 | 0:39eb4d5b97df | 37 | return _device->write(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 38 | } |
va009039 | 0:39eb4d5b97df | 39 | |
va009039 | 0:39eb4d5b97df | 40 | bool USB_HID::sendNB(HID_REPORT *report) |
va009039 | 0:39eb4d5b97df | 41 | { |
va009039 | 0:39eb4d5b97df | 42 | return _device->writeNB(HID_EPINT_IN, report->data, report->length, MAX_HID_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 43 | } |
va009039 | 0:39eb4d5b97df | 44 | |
va009039 | 0:39eb4d5b97df | 45 | bool USB_HID::read(HID_REPORT *report) |
va009039 | 0:39eb4d5b97df | 46 | { |
va009039 | 0:39eb4d5b97df | 47 | uint32_t bytesRead = 0; |
va009039 | 0:39eb4d5b97df | 48 | bool result; |
va009039 | 0:39eb4d5b97df | 49 | result = _device->readEP(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 50 | if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE)) |
va009039 | 0:39eb4d5b97df | 51 | return false; |
va009039 | 0:39eb4d5b97df | 52 | report->length = bytesRead; |
va009039 | 0:39eb4d5b97df | 53 | return result; |
va009039 | 0:39eb4d5b97df | 54 | } |
va009039 | 0:39eb4d5b97df | 55 | |
va009039 | 0:39eb4d5b97df | 56 | bool USB_HID::readNB(HID_REPORT *report) |
va009039 | 0:39eb4d5b97df | 57 | { |
va009039 | 0:39eb4d5b97df | 58 | uint32_t bytesRead = 0; |
va009039 | 0:39eb4d5b97df | 59 | bool result; |
va009039 | 0:39eb4d5b97df | 60 | result = _device->readEP_NB(HID_EPINT_OUT, report->data, &bytesRead, MAX_HID_REPORT_SIZE); |
va009039 | 0:39eb4d5b97df | 61 | report->length = bytesRead; |
va009039 | 0:39eb4d5b97df | 62 | if(!_device->readStart(HID_EPINT_OUT, MAX_HID_REPORT_SIZE)) |
va009039 | 0:39eb4d5b97df | 63 | return false; |
va009039 | 0:39eb4d5b97df | 64 | return result; |
va009039 | 0:39eb4d5b97df | 65 | } |
va009039 | 0:39eb4d5b97df | 66 | |
va009039 | 0:39eb4d5b97df | 67 | /* virtual */ uint8_t * USB_HID::reportDesc() { |
va009039 | 0:39eb4d5b97df | 68 | static uint8_t reportDescriptor[] = { |
va009039 | 0:39eb4d5b97df | 69 | 0x06, 0x00, 0xff, |
va009039 | 0:39eb4d5b97df | 70 | 0x09, 0x01, // usage |
va009039 | 0:39eb4d5b97df | 71 | 0xA1, 0x01, // Collection 0x01 |
va009039 | 0:39eb4d5b97df | 72 | 0x15, 0x00, // logical minimum = 0 |
va009039 | 0:39eb4d5b97df | 73 | 0x26, 0xFF, 0x00, // logical maximum = 255 |
va009039 | 0:39eb4d5b97df | 74 | 0x75, 0x08, // report size = 8 bits |
va009039 | 0:39eb4d5b97df | 75 | 0x95, 0x40, // report count |
va009039 | 0:39eb4d5b97df | 76 | 0x09, 0x01, // usage |
va009039 | 0:39eb4d5b97df | 77 | 0x81, 0x02, // Input (array) |
va009039 | 0:39eb4d5b97df | 78 | 0x95, 0x40, // report count |
va009039 | 0:39eb4d5b97df | 79 | 0x09, 0x01, // usage |
va009039 | 0:39eb4d5b97df | 80 | 0x91, 0x02, // Output (array) |
va009039 | 0:39eb4d5b97df | 81 | 0x95, 0x01, // report count |
va009039 | 0:39eb4d5b97df | 82 | 0x09, 0x01, // usage |
va009039 | 0:39eb4d5b97df | 83 | 0xb1, 0x02, |
va009039 | 0:39eb4d5b97df | 84 | 0xC0 // end collection |
va009039 | 0:39eb4d5b97df | 85 | }; |
va009039 | 0:39eb4d5b97df | 86 | reportLength = sizeof(reportDescriptor); |
va009039 | 0:39eb4d5b97df | 87 | return reportDescriptor; |
va009039 | 0:39eb4d5b97df | 88 | } |
va009039 | 0:39eb4d5b97df | 89 | |
va009039 | 0:39eb4d5b97df | 90 | /* virtual */ uint16_t USB_HID::reportDescLength() { |
va009039 | 0:39eb4d5b97df | 91 | reportDesc(); |
va009039 | 0:39eb4d5b97df | 92 | return reportLength; |
va009039 | 0:39eb4d5b97df | 93 | } |
va009039 | 0:39eb4d5b97df | 94 | |
va009039 | 0:39eb4d5b97df | 95 | bool USB_HID::Request_callback(CONTROL_TRANSFER* transfer, uint8_t* hidDescriptor) |
va009039 | 0:39eb4d5b97df | 96 | { |
va009039 | 0:39eb4d5b97df | 97 | // Process additional standard requests |
va009039 | 0:39eb4d5b97df | 98 | if (transfer->setup.bmRequestType.Type == STANDARD_TYPE) { |
va009039 | 0:39eb4d5b97df | 99 | switch (transfer->setup.bRequest) { |
va009039 | 0:39eb4d5b97df | 100 | case GET_DESCRIPTOR: |
va009039 | 0:39eb4d5b97df | 101 | switch (DESCRIPTOR_TYPE(transfer->setup.wValue)) { |
va009039 | 0:39eb4d5b97df | 102 | case REPORT_DESCRIPTOR: |
va009039 | 0:39eb4d5b97df | 103 | if ((reportDesc() != NULL) && (reportDescLength() != 0)) { |
va009039 | 0:39eb4d5b97df | 104 | transfer->remaining = reportDescLength(); |
va009039 | 0:39eb4d5b97df | 105 | transfer->ptr = reportDesc(); |
va009039 | 0:39eb4d5b97df | 106 | transfer->direction = DEVICE_TO_HOST; |
va009039 | 0:39eb4d5b97df | 107 | return true; |
va009039 | 0:39eb4d5b97df | 108 | } |
va009039 | 0:39eb4d5b97df | 109 | break; |
va009039 | 0:39eb4d5b97df | 110 | case HID_DESCRIPTOR: |
va009039 | 0:39eb4d5b97df | 111 | if (hidDescriptor != NULL) { |
va009039 | 0:39eb4d5b97df | 112 | transfer->remaining = HID_DESCRIPTOR_LENGTH; |
va009039 | 0:39eb4d5b97df | 113 | transfer->ptr = hidDescriptor; |
va009039 | 0:39eb4d5b97df | 114 | transfer->direction = DEVICE_TO_HOST; |
va009039 | 0:39eb4d5b97df | 115 | return true; |
va009039 | 0:39eb4d5b97df | 116 | } |
va009039 | 0:39eb4d5b97df | 117 | break; |
va009039 | 0:39eb4d5b97df | 118 | default: |
va009039 | 0:39eb4d5b97df | 119 | break; |
va009039 | 0:39eb4d5b97df | 120 | } |
va009039 | 0:39eb4d5b97df | 121 | break; |
va009039 | 0:39eb4d5b97df | 122 | default: |
va009039 | 0:39eb4d5b97df | 123 | break; |
va009039 | 0:39eb4d5b97df | 124 | } |
va009039 | 0:39eb4d5b97df | 125 | } |
va009039 | 0:39eb4d5b97df | 126 | |
va009039 | 0:39eb4d5b97df | 127 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
va009039 | 0:39eb4d5b97df | 128 | switch (transfer->setup.bRequest) { |
va009039 | 0:39eb4d5b97df | 129 | case SET_REPORT: |
va009039 | 0:39eb4d5b97df | 130 | // First byte will be used for report ID |
va009039 | 0:39eb4d5b97df | 131 | outputReport.data[0] = transfer->setup.wValue & 0xff; |
va009039 | 0:39eb4d5b97df | 132 | outputReport.length = transfer->setup.wLength + 1; |
va009039 | 0:39eb4d5b97df | 133 | |
va009039 | 0:39eb4d5b97df | 134 | transfer->remaining = sizeof(outputReport.data) - 1; |
va009039 | 0:39eb4d5b97df | 135 | transfer->ptr = &outputReport.data[1]; |
va009039 | 0:39eb4d5b97df | 136 | transfer->direction = HOST_TO_DEVICE; |
va009039 | 0:39eb4d5b97df | 137 | transfer->notify = true; |
va009039 | 0:39eb4d5b97df | 138 | return true; |
va009039 | 0:39eb4d5b97df | 139 | } |
va009039 | 0:39eb4d5b97df | 140 | } |
va009039 | 0:39eb4d5b97df | 141 | return false; |
va009039 | 0:39eb4d5b97df | 142 | } |