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