|
| 1 | +/* |
| 2 | + Modified by Matthew Heironimus to support HID Report Descriptors to be in |
| 3 | + standard RAM in addition to program memory (PROGMEM). |
| 4 | +
|
| 5 | + Copyright (c) 2015, Arduino LLC |
| 6 | + Original code (pre-library): Copyright (c) 2011, Peter Barrett |
| 7 | +
|
| 8 | + Permission to use, copy, modify, and/or distribute this software for |
| 9 | + any purpose with or without fee is hereby granted, provided that the |
| 10 | + above copyright notice and this permission notice appear in all copies. |
| 11 | +
|
| 12 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 13 | + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 14 | + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR |
| 15 | + BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES |
| 16 | + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 17 | + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 18 | + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 19 | + SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "DynamicHID.h" |
| 23 | + |
| 24 | +#if defined(USBCON) |
| 25 | + |
| 26 | +#ifdef _VARIANT_ARDUINO_DUE_X_ |
| 27 | +#define USB_SendControl USBD_SendControl |
| 28 | +#define USB_Send USBD_Send |
| 29 | +#endif |
| 30 | + |
| 31 | +DynamicHID_& DynamicHID() |
| 32 | +{ |
| 33 | + static DynamicHID_ obj; |
| 34 | + return obj; |
| 35 | +} |
| 36 | + |
| 37 | +int DynamicHID_::getInterface(uint8_t* interfaceCount) |
| 38 | +{ |
| 39 | + *interfaceCount += 1; // uses 1 |
| 40 | + DYNAMIC_HIDDescriptor hidInterface = { |
| 41 | + D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, DYNAMIC_HID_SUBCLASS_NONE, DYNAMIC_HID_PROTOCOL_NONE), |
| 42 | + D_HIDREPORT(descriptorSize), |
| 43 | + D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01) |
| 44 | + }; |
| 45 | + return USB_SendControl(0, &hidInterface, sizeof(hidInterface)); |
| 46 | +} |
| 47 | + |
| 48 | +int DynamicHID_::getDescriptor(USBSetup& setup) |
| 49 | +{ |
| 50 | + // Check if this is a HID Class Descriptor request |
| 51 | + if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; } |
| 52 | + if (setup.wValueH != DYNAMIC_HID_REPORT_DESCRIPTOR_TYPE) { return 0; } |
| 53 | + |
| 54 | + // In a HID Class Descriptor wIndex cointains the interface number |
| 55 | + if (setup.wIndex != pluggedInterface) { return 0; } |
| 56 | + |
| 57 | + int total = 0; |
| 58 | + DynamicHIDSubDescriptor* node; |
| 59 | + for (node = rootNode; node; node = node->next) { |
| 60 | + int res = USB_SendControl((node->inProgMem ? TRANSFER_PGM : 0), node->data, node->length); |
| 61 | + if (res == -1) |
| 62 | + return -1; |
| 63 | + total += res; |
| 64 | + } |
| 65 | + |
| 66 | + // Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol |
| 67 | + // due to the USB specs, but Windows and Linux just assumes its in report mode. |
| 68 | + protocol = DYNAMIC_HID_REPORT_PROTOCOL; |
| 69 | + |
| 70 | + return total; |
| 71 | +} |
| 72 | + |
| 73 | +uint8_t DynamicHID_::getShortName(char *name) |
| 74 | +{ |
| 75 | + name[0] = 'H'; |
| 76 | + name[1] = 'I'; |
| 77 | + name[2] = 'D'; |
| 78 | + name[3] = 'A' + (descriptorSize & 0x0F); |
| 79 | + name[4] = 'A' + ((descriptorSize >> 4) & 0x0F); |
| 80 | + return 5; |
| 81 | +} |
| 82 | + |
| 83 | +void DynamicHID_::AppendDescriptor(DynamicHIDSubDescriptor *node) |
| 84 | +{ |
| 85 | + if (!rootNode) { |
| 86 | + rootNode = node; |
| 87 | + } else { |
| 88 | + DynamicHIDSubDescriptor *current = rootNode; |
| 89 | + while (current->next) { |
| 90 | + current = current->next; |
| 91 | + } |
| 92 | + current->next = node; |
| 93 | + } |
| 94 | + descriptorSize += node->length; |
| 95 | +} |
| 96 | + |
| 97 | +int DynamicHID_::SendReport(uint8_t id, const void* data, int len) |
| 98 | +{ |
| 99 | + uint8_t p[len + 1]; |
| 100 | + p[0] = id; |
| 101 | + memcpy(&p[1], data, len); |
| 102 | + return USB_Send(pluggedEndpoint | TRANSFER_RELEASE, p, len + 1); |
| 103 | +} |
| 104 | + |
| 105 | +bool DynamicHID_::setup(USBSetup& setup) |
| 106 | +{ |
| 107 | + if (pluggedInterface != setup.wIndex) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + uint8_t request = setup.bRequest; |
| 112 | + uint8_t requestType = setup.bmRequestType; |
| 113 | + |
| 114 | + if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE) |
| 115 | + { |
| 116 | + if (request == DYNAMIC_HID_GET_REPORT) { |
| 117 | + // TODO: DYNAMIC_HID_GetReport(); |
| 118 | + return true; |
| 119 | + } |
| 120 | + if (request == DYNAMIC_HID_GET_PROTOCOL) { |
| 121 | + // TODO: Send8(protocol); |
| 122 | + return true; |
| 123 | + } |
| 124 | + if (request == DYNAMIC_HID_GET_IDLE) { |
| 125 | + // TODO: Send8(idle); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE) |
| 130 | + { |
| 131 | + if (request == DYNAMIC_HID_SET_PROTOCOL) { |
| 132 | + // The USB Host tells us if we are in boot or report mode. |
| 133 | + // This only works with a real boot compatible device. |
| 134 | + protocol = setup.wValueL; |
| 135 | + return true; |
| 136 | + } |
| 137 | + if (request == DYNAMIC_HID_SET_IDLE) { |
| 138 | + idle = setup.wValueL; |
| 139 | + return true; |
| 140 | + } |
| 141 | + if (request == DYNAMIC_HID_SET_REPORT) |
| 142 | + { |
| 143 | + //uint8_t reportID = setup.wValueL; |
| 144 | + //uint16_t length = setup.wLength; |
| 145 | + //uint8_t data[length]; |
| 146 | + // Make sure to not read more data than USB_EP_SIZE. |
| 147 | + // You can read multiple times through a loop. |
| 148 | + // The first byte (may!) contain the reportID on a multreport. |
| 149 | + //USB_RecvControl(data, length); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return false; |
| 154 | +} |
| 155 | + |
| 156 | +DynamicHID_::DynamicHID_(void) : PluggableUSBModule(1, 1, epType), |
| 157 | + rootNode(NULL), descriptorSize(0), |
| 158 | + protocol(DYNAMIC_HID_REPORT_PROTOCOL), idle(1) |
| 159 | +{ |
| 160 | + epType[0] = EP_TYPE_INTERRUPT_IN; |
| 161 | + PluggableUSB().plug(this); |
| 162 | +} |
| 163 | + |
| 164 | +int DynamicHID_::begin(void) |
| 165 | +{ |
| 166 | + return 0; |
| 167 | +} |
| 168 | + |
| 169 | +#endif /* if defined(USBCON) */ |
0 commit comments