Forked to make modifications to bring the USBHID into USB compliance and add additional features.

Dependents:   HW4_AudioControl

Fork of USBDevice by mbed official

As of Revision 18 everything in the USBHID specification is now implemented, except Multi-reports.

Revision comments for changelist 18

USBHID.cpp

  • Added SET_PROTOCOL support
  • Added GET_PROTOCOL support
  • protocolSate is set to 1 by default to match USB HID specification. This variable should be checked to determine which format theinput report should have. 1 - Use the user specified report format. 0 - Use the BOOT protocol report format.

Revision comments for changelist 16

  • HID_REPORT transformed from structure to class. This was done for several reasons.
  1. When multiple reports are used the 64 byte size for every report becomes a problem.
  2. The length value should always remain the same for a report, Make the constructor set the vale at the same time it allocates memory for the DATA area.
  • By default the data will be an array of MAX_HID_REPORT_SIZE like the structure,
  • When given a length argument, the hid_report.length will be set, and hid_report.data will be an array of the size given.
  • Length zero causes data to be NULL
  • Mostly backwards compatible. The definition of a destructor caused a compiler error in USBMouse::update and USBMousekeyboard::update. This error was caused by instatiation of HID_REPORT in the middle of an IF logic statement. These files have been modified. The error complained that the logic skipped object initialization. The HID_REPORT has been moved to the class definition. Since both ABSOLUTE and RELATIVE modes used the HID_REPORT, this seems to make more sense. Previously the hid_report would be instatiated in <class>::mousesend and <class>::update.

Revision comments for changelist 14

USBdevice.cpp

  • Modified USB device state to change from Configure when disconnect is called.
  • Modified the call back function for when the suspend state changes. This should be used to turn off peripherals to conserve power.

Revision comments for changelist 13

USBdevice.cpp

  • ) Changed DEBUG messages to be more descriptive for string descriptor
  • ) Bug fix: Control Transfers did not actually transfer the data from Buffer to transfer->ptr

USBHIDTypes.h

  • ) Added ALL CLASS request to KEYWORD list
  • ) Added KEYWORDS for report type

USBHID.h

  • ) Added a new constructor to specify size of feature report
  • ) Added HID_REPORT inputReport and featureReport
  • ) Added data structures to support IDLE rate
  • ) Added data structures to support callback functions

USBHID.cpp

  • ) Changed constructor to initialize new feature data structures
  • ) Implemented Set_IDLE/GET_IDLE and the periodic resend of non-changed data
  • ) Implemented HID specification required control transfer GET_REPORT
  • ) Fixed issue where Intreput transfers and control transfers did not access the same data structures.
  • ) Implemented Feature reports
  • ) Implemented Callback Hooks for get_report/set_report actions.
  • ) Added callback hooks for interupt actions in the new functions.
  • ) interupt transfer can now write to outputReport
  • ) Modified SET_REPORT code to function for multiple types.
  • ) Refactored some code in preperation to add multi report support
Test NumberTest DescriptionTest ResultNotes
1Use USBmouse to verify backward compatibility of constructor and methodsPass
2Test SET_REPORT can set a feature reportPass
3Test GET_REPORT can retrieve a feature reportPass
4Test SET_IDLE sets up a reoccuring triggerPassIOCTL_SET_POLL_FREQUENCY_MSEC does not function for the windows HID driver. A Special test program is used to rearm the IDLE rate after windows sets it to zero
5Test SET_IDLE disables a triggerPassWindows automatically sends this command to a HID device when it is inserted.
6Enabled DEBUG in USBDevice.cpp and generated str descriptor requests.Pass
7Test SET_REPORT can set an output reportPass
8Test GET_REPORT can retrieve an output reportPass
9ReadFile, accesses the input_reportPass
10WriteFile accesses the output_report, via interupt transfer when ep1_out is used.Pass
11WriteFile accesses the output_report, via control transfer when ep1_out is NOT used.Not Tested
12Callback hooks trigger independently for each type of set_report/get_reportPass
13New constructor sets feature_report sizePass
14Control transfer SET_REPORT and writeFile access the same data structureBUGThe same data structure is accessed, but the data transfer size is different. The writeFile strips the leading byte which is the report ID, The Control transfer keeps the byte.
15Control transfer GET_REPORT and readFile access the same data structureBUGThe same dtat structure is accessed, but the data transfer size is different. The readFile strips the leading byte which is the report ID, The Control transfer keeps the byte.
16Test GET_IDLE retrieves the IDLE rateUnknownWindows HID driver does not implement IOCTL_HID_GET_POLL_FREQUENCY_MSEC
Committer:
jakowisp
Date:
Thu Aug 08 08:02:46 2013 +0000
Revision:
18:cb3afa532fcd
Parent:
14:00cd29199e0e
Default ProtocolState was incorrectly being set to 0:Boot when the HID specification states that the default should be 1:Report

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 2:34856a6adb5b 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 2:34856a6adb5b 2 *
samux 2:34856a6adb5b 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 2:34856a6adb5b 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 2:34856a6adb5b 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 2:34856a6adb5b 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 2:34856a6adb5b 7 * Software is furnished to do so, subject to the following conditions:
samux 2:34856a6adb5b 8 *
samux 2:34856a6adb5b 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 2:34856a6adb5b 10 * substantial portions of the Software.
samux 2:34856a6adb5b 11 *
samux 2:34856a6adb5b 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 2:34856a6adb5b 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 2:34856a6adb5b 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 2:34856a6adb5b 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 2:34856a6adb5b 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 2:34856a6adb5b 17 */
samux 2:34856a6adb5b 18
samux 2:34856a6adb5b 19 #ifndef USBDEVICE_H
samux 2:34856a6adb5b 20 #define USBDEVICE_H
samux 2:34856a6adb5b 21
samux 2:34856a6adb5b 22 #include "mbed.h"
samux 2:34856a6adb5b 23 #include "USBDevice_Types.h"
samux 2:34856a6adb5b 24 #include "USBHAL.h"
samux 2:34856a6adb5b 25
samux 2:34856a6adb5b 26 class USBDevice: public USBHAL
samux 2:34856a6adb5b 27 {
samux 2:34856a6adb5b 28 public:
samux 2:34856a6adb5b 29 USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
samux 2:34856a6adb5b 30
samux 2:34856a6adb5b 31 /*
samux 2:34856a6adb5b 32 * Check if the device is configured
samux 2:34856a6adb5b 33 *
samux 2:34856a6adb5b 34 * @returns true if configured, false otherwise
samux 2:34856a6adb5b 35 */
samux 2:34856a6adb5b 36 bool configured(void);
samux 2:34856a6adb5b 37
samux 2:34856a6adb5b 38 /*
samux 2:34856a6adb5b 39 * Connect a device
samux 2:34856a6adb5b 40 */
samux 2:34856a6adb5b 41 void connect(void);
samux 2:34856a6adb5b 42
samux 2:34856a6adb5b 43 /*
samux 2:34856a6adb5b 44 * Disconnect a device
samux 2:34856a6adb5b 45 */
samux 2:34856a6adb5b 46 void disconnect(void);
samux 2:34856a6adb5b 47
samux 2:34856a6adb5b 48 /*
samux 2:34856a6adb5b 49 * Add an endpoint
samux 2:34856a6adb5b 50 *
samux 2:34856a6adb5b 51 * @param endpoint endpoint which will be added
samux 2:34856a6adb5b 52 * @param maxPacket Maximum size of a packet which can be sent for this endpoint
samux 2:34856a6adb5b 53 * @returns true if successful, false otherwise
samux 2:34856a6adb5b 54 */
samux 2:34856a6adb5b 55 bool addEndpoint(uint8_t endpoint, uint32_t maxPacket);
samux 2:34856a6adb5b 56
samux 2:34856a6adb5b 57 /*
samux 2:34856a6adb5b 58 * Start a reading on a certain endpoint.
samux 2:34856a6adb5b 59 * You can access the result of the reading by USBDevice_read
samux 2:34856a6adb5b 60 *
samux 2:34856a6adb5b 61 * @param endpoint endpoint which will be read
samux 2:34856a6adb5b 62 * @param maxSize the maximum length that can be read
samux 2:34856a6adb5b 63 * @return true if successful
samux 2:34856a6adb5b 64 */
samux 2:34856a6adb5b 65 bool readStart(uint8_t endpoint, uint32_t maxSize);
samux 2:34856a6adb5b 66
samux 2:34856a6adb5b 67 /*
samux 2:34856a6adb5b 68 * Read a certain endpoint. Before calling this function, USBUSBDevice_readStart
samux 2:34856a6adb5b 69 * must be called.
samux 2:34856a6adb5b 70 *
samux 2:34856a6adb5b 71 * Warning: blocking
samux 2:34856a6adb5b 72 *
samux 2:34856a6adb5b 73 * @param endpoint endpoint which will be read
samux 2:34856a6adb5b 74 * @param buffer buffer will be filled with the data received
samux 2:34856a6adb5b 75 * @param size the number of bytes read will be stored in *size
samux 2:34856a6adb5b 76 * @param maxSize the maximum length that can be read
samux 2:34856a6adb5b 77 * @returns true if successful
samux 2:34856a6adb5b 78 */
samux 2:34856a6adb5b 79 bool readEP(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
samux 2:34856a6adb5b 80
samux 2:34856a6adb5b 81 /*
samux 2:34856a6adb5b 82 * Read a certain endpoint.
samux 2:34856a6adb5b 83 *
samux 2:34856a6adb5b 84 * Warning: non blocking
samux 2:34856a6adb5b 85 *
samux 2:34856a6adb5b 86 * @param endpoint endpoint which will be read
samux 2:34856a6adb5b 87 * @param buffer buffer will be filled with the data received (if data are available)
samux 2:34856a6adb5b 88 * @param size the number of bytes read will be stored in *size
samux 2:34856a6adb5b 89 * @param maxSize the maximum length that can be read
samux 2:34856a6adb5b 90 * @returns true if successful
samux 2:34856a6adb5b 91 */
samux 2:34856a6adb5b 92 bool readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, uint32_t maxSize);
samux 2:34856a6adb5b 93
samux 2:34856a6adb5b 94 /*
samux 2:34856a6adb5b 95 * Write a certain endpoint.
samux 2:34856a6adb5b 96 *
samux 2:34856a6adb5b 97 * Warning: blocking
samux 2:34856a6adb5b 98 *
samux 2:34856a6adb5b 99 * @param endpoint endpoint to write
samux 2:34856a6adb5b 100 * @param buffer data contained in buffer will be write
samux 2:34856a6adb5b 101 * @param size the number of bytes to write
samux 2:34856a6adb5b 102 * @param maxSize the maximum length that can be written on this endpoint
samux 2:34856a6adb5b 103 */
samux 2:34856a6adb5b 104 bool write(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
samux 2:34856a6adb5b 105
samux 2:34856a6adb5b 106
samux 2:34856a6adb5b 107 /*
samux 2:34856a6adb5b 108 * Write a certain endpoint.
samux 2:34856a6adb5b 109 *
samux 2:34856a6adb5b 110 * Warning: non blocking
samux 2:34856a6adb5b 111 *
samux 2:34856a6adb5b 112 * @param endpoint endpoint to write
samux 2:34856a6adb5b 113 * @param buffer data contained in buffer will be write
samux 2:34856a6adb5b 114 * @param size the number of bytes to write
samux 2:34856a6adb5b 115 * @param maxSize the maximum length that can be written on this endpoint
samux 2:34856a6adb5b 116 */
samux 2:34856a6adb5b 117 bool writeNB(uint8_t endpoint, uint8_t * buffer, uint32_t size, uint32_t maxSize);
samux 2:34856a6adb5b 118
samux 2:34856a6adb5b 119
samux 2:34856a6adb5b 120 /*
samux 2:34856a6adb5b 121 * Called by USBDevice layer on bus reset. Warning: Called in ISR context
samux 2:34856a6adb5b 122 *
samux 2:34856a6adb5b 123 * May be used to reset state
samux 2:34856a6adb5b 124 */
samux 2:34856a6adb5b 125 virtual void USBCallback_busReset(void) {};
samux 2:34856a6adb5b 126
samux 2:34856a6adb5b 127 /*
samux 2:34856a6adb5b 128 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
samux 2:34856a6adb5b 129 * This is used to handle extensions to standard requests
samux 2:34856a6adb5b 130 * and class specific requests
samux 2:34856a6adb5b 131 *
samux 2:34856a6adb5b 132 * @returns true if class handles this request
samux 2:34856a6adb5b 133 */
samux 2:34856a6adb5b 134 virtual bool USBCallback_request() { return false; };
samux 2:34856a6adb5b 135
samux 2:34856a6adb5b 136 /*
samux 2:34856a6adb5b 137 * Called by USBDevice on Endpoint0 request completion
samux 2:34856a6adb5b 138 * if the 'notify' flag has been set to true. Warning: Called in ISR context
samux 2:34856a6adb5b 139 *
samux 2:34856a6adb5b 140 * In this case it is used to indicate that a HID report has
samux 2:34856a6adb5b 141 * been received from the host on endpoint 0
samux 2:34856a6adb5b 142 *
samux 2:34856a6adb5b 143 * @param buf buffer received on endpoint 0
samux 2:34856a6adb5b 144 * @param length length of this buffer
samux 2:34856a6adb5b 145 */
samux 2:34856a6adb5b 146 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {};
samux 2:34856a6adb5b 147
samux 2:34856a6adb5b 148 /*
samux 2:34856a6adb5b 149 * Called by USBDevice layer. Set configuration of the device.
samux 2:34856a6adb5b 150 * For instance, you can add all endpoints that you need on this function.
samux 2:34856a6adb5b 151 *
samux 2:34856a6adb5b 152 * @param configuration Number of the configuration
samux 2:34856a6adb5b 153 */
samux 2:34856a6adb5b 154 virtual bool USBCallback_setConfiguration(uint8_t configuration) { return false; };
samux 2:34856a6adb5b 155
samux 2:34856a6adb5b 156 /*
samux 2:34856a6adb5b 157 * Called by USBDevice layer. Set interface/alternate of the device.
samux 2:34856a6adb5b 158 *
samux 2:34856a6adb5b 159 * @param interface Number of the interface to be configured
samux 2:34856a6adb5b 160 * @param alternate Number of the alternate to be configured
samux 2:34856a6adb5b 161 * @returns true if class handles this request
samux 2:34856a6adb5b 162 */
samux 2:34856a6adb5b 163 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
samux 2:34856a6adb5b 164
samux 2:34856a6adb5b 165 /*
samux 2:34856a6adb5b 166 * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
samux 2:34856a6adb5b 167 *
samux 2:34856a6adb5b 168 * @returns pointer to the device descriptor
samux 2:34856a6adb5b 169 */
samux 2:34856a6adb5b 170 virtual uint8_t * deviceDesc();
samux 2:34856a6adb5b 171
samux 2:34856a6adb5b 172 /*
samux 2:34856a6adb5b 173 * Get configuration descriptor
samux 2:34856a6adb5b 174 *
samux 2:34856a6adb5b 175 * @returns pointer to the configuration descriptor
samux 2:34856a6adb5b 176 */
samux 2:34856a6adb5b 177 virtual uint8_t * configurationDesc(){return NULL;};
samux 2:34856a6adb5b 178
samux 2:34856a6adb5b 179 /*
samux 2:34856a6adb5b 180 * Get string lang id descriptor
samux 2:34856a6adb5b 181 *
samux 2:34856a6adb5b 182 * @return pointer to the string lang id descriptor
samux 2:34856a6adb5b 183 */
samux 2:34856a6adb5b 184 virtual uint8_t * stringLangidDesc();
samux 2:34856a6adb5b 185
samux 2:34856a6adb5b 186 /*
samux 2:34856a6adb5b 187 * Get string manufacturer descriptor
samux 2:34856a6adb5b 188 *
samux 2:34856a6adb5b 189 * @returns pointer to the string manufacturer descriptor
samux 2:34856a6adb5b 190 */
samux 2:34856a6adb5b 191 virtual uint8_t * stringImanufacturerDesc();
samux 2:34856a6adb5b 192
samux 2:34856a6adb5b 193 /*
samux 2:34856a6adb5b 194 * Get string product descriptor
samux 2:34856a6adb5b 195 *
samux 2:34856a6adb5b 196 * @returns pointer to the string product descriptor
samux 2:34856a6adb5b 197 */
samux 2:34856a6adb5b 198 virtual uint8_t * stringIproductDesc();
samux 2:34856a6adb5b 199
samux 2:34856a6adb5b 200 /*
samux 2:34856a6adb5b 201 * Get string serial descriptor
samux 2:34856a6adb5b 202 *
samux 2:34856a6adb5b 203 * @returns pointer to the string serial descriptor
samux 2:34856a6adb5b 204 */
samux 2:34856a6adb5b 205 virtual uint8_t * stringIserialDesc();
samux 2:34856a6adb5b 206
samux 2:34856a6adb5b 207 /*
samux 2:34856a6adb5b 208 * Get string configuration descriptor
samux 2:34856a6adb5b 209 *
samux 2:34856a6adb5b 210 * @returns pointer to the string configuration descriptor
samux 2:34856a6adb5b 211 */
samux 2:34856a6adb5b 212 virtual uint8_t * stringIConfigurationDesc();
samux 2:34856a6adb5b 213
samux 2:34856a6adb5b 214 /*
samux 2:34856a6adb5b 215 * Get string interface descriptor
samux 2:34856a6adb5b 216 *
samux 2:34856a6adb5b 217 * @returns pointer to the string interface descriptor
samux 2:34856a6adb5b 218 */
samux 2:34856a6adb5b 219 virtual uint8_t * stringIinterfaceDesc();
samux 2:34856a6adb5b 220
samux 2:34856a6adb5b 221 /*
samux 2:34856a6adb5b 222 * Get the length of the report descriptor
samux 2:34856a6adb5b 223 *
samux 2:34856a6adb5b 224 * @returns length of the report descriptor
samux 2:34856a6adb5b 225 */
samux 2:34856a6adb5b 226 virtual uint16_t reportDescLength() { return 0; };
samux 2:34856a6adb5b 227
samux 2:34856a6adb5b 228
samux 2:34856a6adb5b 229
samux 2:34856a6adb5b 230 protected:
samux 2:34856a6adb5b 231 virtual void busReset(void);
samux 2:34856a6adb5b 232 virtual void EP0setupCallback(void);
samux 2:34856a6adb5b 233 virtual void EP0out(void);
samux 2:34856a6adb5b 234 virtual void EP0in(void);
samux 2:34856a6adb5b 235 virtual void connectStateChanged(unsigned int connected);
samux 2:34856a6adb5b 236 virtual void suspendStateChanged(unsigned int suspended);
samux 2:34856a6adb5b 237 uint8_t * findDescriptor(uint8_t descriptorType);
samux 2:34856a6adb5b 238 CONTROL_TRANSFER * getTransferPtr(void);
samux 2:34856a6adb5b 239
samux 2:34856a6adb5b 240 uint16_t VENDOR_ID;
samux 2:34856a6adb5b 241 uint16_t PRODUCT_ID;
samux 2:34856a6adb5b 242 uint16_t PRODUCT_RELEASE;
jakowisp 14:00cd29199e0e 243 USB_DEVICE device;
jakowisp 14:00cd29199e0e 244
samux 2:34856a6adb5b 245 private:
samux 2:34856a6adb5b 246 bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);
samux 2:34856a6adb5b 247 bool requestGetDescriptor(void);
samux 2:34856a6adb5b 248 bool controlOut(void);
samux 2:34856a6adb5b 249 bool controlIn(void);
samux 2:34856a6adb5b 250 bool requestSetAddress(void);
samux 2:34856a6adb5b 251 bool requestSetConfiguration(void);
samux 2:34856a6adb5b 252 bool requestSetFeature(void);
samux 2:34856a6adb5b 253 bool requestClearFeature(void);
samux 2:34856a6adb5b 254 bool requestGetStatus(void);
samux 2:34856a6adb5b 255 bool requestSetup(void);
samux 2:34856a6adb5b 256 bool controlSetup(void);
samux 2:34856a6adb5b 257 void decodeSetupPacket(uint8_t *data, SETUP_PACKET *packet);
samux 2:34856a6adb5b 258 bool requestGetConfiguration(void);
samux 2:34856a6adb5b 259 bool requestGetInterface(void);
samux 2:34856a6adb5b 260 bool requestSetInterface(void);
samux 2:34856a6adb5b 261
samux 2:34856a6adb5b 262 CONTROL_TRANSFER transfer;
jakowisp 14:00cd29199e0e 263
samux 2:34856a6adb5b 264
samux 2:34856a6adb5b 265 uint16_t currentInterface;
samux 2:34856a6adb5b 266 uint8_t currentAlternate;
samux 2:34856a6adb5b 267 };
samux 2:34856a6adb5b 268
samux 2:34856a6adb5b 269
samux 2:34856a6adb5b 270 #endif