|
| 1 | +/* |
| 2 | +Copyright (c) 2014-2015 NicoHood |
| 3 | +See the readme for credit to other people. |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in |
| 13 | +all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +THE SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +#include "RawHID.h" |
| 25 | + |
| 26 | +static const uint8_t _hidReportDescriptorRawHID[] PROGMEM = { |
| 27 | + /* RAW HID */ |
| 28 | + 0x06, lowByte(RAWHID_USAGE_PAGE), highByte(RAWHID_USAGE_PAGE), /* 30 */ |
| 29 | + 0x0A, lowByte(RAWHID_USAGE), highByte(RAWHID_USAGE), |
| 30 | + |
| 31 | + 0xA1, 0x01, /* Collection 0x01 */ |
| 32 | + // RawHID is not multireport compatible. |
| 33 | + // On Linux it might work with some modifications, |
| 34 | + // however you are not happy to use it like that. |
| 35 | + //0x85, HID_REPORTID_RAWHID, /* REPORT_ID */ |
| 36 | + 0x75, 0x08, /* report size = 8 bits */ |
| 37 | + 0x15, 0x00, /* logical minimum = 0 */ |
| 38 | + 0x26, 0xFF, 0x00, /* logical maximum = 255 */ |
| 39 | + |
| 40 | + 0x95, RAWHID_TX_SIZE, /* report count TX */ |
| 41 | + 0x09, 0x01, /* usage */ |
| 42 | + 0x81, 0x02, /* Input (array) */ |
| 43 | + |
| 44 | + 0x95, RAWHID_RX_SIZE, /* report count RX */ |
| 45 | + 0x09, 0x02, /* usage */ |
| 46 | + 0x91, 0x02, /* Output (array) */ |
| 47 | + 0xC0 /* end collection */ |
| 48 | +}; |
| 49 | + |
| 50 | +RawHID_::RawHID_(void) : PUSBListNode(1, 1, epType), protocol(1), idle(1), dataLength(0) |
| 51 | +{ |
| 52 | + epType[0] = EP_TYPE_INTERRUPT_IN; |
| 53 | + PluggableUSB().plug(this); |
| 54 | +} |
| 55 | + |
| 56 | +int RawHID_::getInterface(uint8_t* interfaceCount) |
| 57 | +{ |
| 58 | + // TODO add a 2nd OUT endpoint to get more speed??? |
| 59 | + // Maybe as optional device |
| 60 | + *interfaceCount += 1; // uses 1 |
| 61 | + HIDDescriptor hidInterface = { |
| 62 | + D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE), |
| 63 | + D_HIDREPORT(sizeof(_hidReportDescriptorRawHID)), |
| 64 | + D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) |
| 65 | + }; |
| 66 | + return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); |
| 67 | +} |
| 68 | + |
| 69 | +int RawHID_::getDescriptor(USBSetup& setup) |
| 70 | +{ |
| 71 | + // Check if this is a HID Class Descriptor request |
| 72 | + if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; } |
| 73 | + if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; } |
| 74 | + |
| 75 | + // In a HID Class Descriptor wIndex cointains the interface number |
| 76 | + if (setup.wIndex != pluggedInterface) { return 0; } |
| 77 | + |
| 78 | + return USB_SendControl(TRANSFER_PGM, _hidReportDescriptorRawHID, sizeof(_hidReportDescriptorRawHID)); |
| 79 | +} |
| 80 | + |
| 81 | +bool RawHID_::setup(USBSetup& setup) |
| 82 | +{ |
| 83 | + if (pluggedInterface != setup.wIndex) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + uint8_t request = setup.bRequest; |
| 88 | + uint8_t requestType = setup.bmRequestType; |
| 89 | + |
| 90 | + if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) |
| 91 | + { |
| 92 | + if (request == HID_GET_REPORT) { |
| 93 | + // TODO: HID_GetReport(); |
| 94 | + return true; |
| 95 | + } |
| 96 | + if (request == HID_GET_PROTOCOL) { |
| 97 | + // TODO: Send8(protocol); |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE) |
| 103 | + { |
| 104 | + if (request == HID_SET_PROTOCOL) { |
| 105 | + protocol = setup.wValueL; |
| 106 | + return true; |
| 107 | + } |
| 108 | + if (request == HID_SET_IDLE) { |
| 109 | + idle = setup.wValueL; |
| 110 | + return true; |
| 111 | + } |
| 112 | + if (request == HID_SET_REPORT) |
| 113 | + { |
| 114 | + // TODO |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | +} |
| 120 | + |
| 121 | +void RawHID_::SendReport(void* data, int length){ |
| 122 | + USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, length); |
| 123 | +} |
| 124 | + |
| 125 | +RawHID_ RawHID; |
0 commit comments