USB Host WAN Dongle library

Fork of USBHostWANDongle_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Tue Jul 31 10:37:16 2012 +0000
Revision:
9:c9e9817c398c
Parent:
8:0d1ec493842c
Child:
10:08bce4cd973a
Renamed Endpoint->USBEndpoint because it conflicted with the Socket API! Made some weird symbols mixups happen that made everything explode when the first USB endpoint was allocated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:ae46a0638b2c 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
donatien 0:ae46a0638b2c 2 *
donatien 0:ae46a0638b2c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
donatien 0:ae46a0638b2c 4 * and associated documentation files (the "Software"), to deal in the Software without
donatien 0:ae46a0638b2c 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
donatien 0:ae46a0638b2c 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
donatien 0:ae46a0638b2c 7 * Software is furnished to do so, subject to the following conditions:
donatien 0:ae46a0638b2c 8 *
donatien 0:ae46a0638b2c 9 * The above copyright notice and this permission notice shall be included in all copies or
donatien 0:ae46a0638b2c 10 * substantial portions of the Software.
donatien 0:ae46a0638b2c 11 *
donatien 0:ae46a0638b2c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
donatien 0:ae46a0638b2c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
donatien 0:ae46a0638b2c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
donatien 0:ae46a0638b2c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 0:ae46a0638b2c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
donatien 0:ae46a0638b2c 17 */
donatien 0:ae46a0638b2c 18
donatien 0:ae46a0638b2c 19 #ifndef USBHOST_H
donatien 0:ae46a0638b2c 20 #define USBHOST_H
donatien 0:ae46a0638b2c 21
donatien 0:ae46a0638b2c 22 #include "USBHALHost.h"
donatien 0:ae46a0638b2c 23 #include "USBDeviceConnected.h"
donatien 9:c9e9817c398c 24 #include "USBEndpoint.h"
donatien 6:075e36a3463e 25 #include "IUSBEnumerator.h"
donatien 0:ae46a0638b2c 26
donatien 0:ae46a0638b2c 27 #define MAX_DEVICE_NB 1
donatien 0:ae46a0638b2c 28
donatien 0:ae46a0638b2c 29 // singleton class
donatien 0:ae46a0638b2c 30 class USBHost : public USBHALHost {
donatien 0:ae46a0638b2c 31 public:
donatien 0:ae46a0638b2c 32 /*
donatien 0:ae46a0638b2c 33 * Static method to create or retrieve the single USBHost instance
donatien 0:ae46a0638b2c 34 */
donatien 0:ae46a0638b2c 35 static USBHost * getHostInst();
donatien 0:ae46a0638b2c 36
donatien 0:ae46a0638b2c 37 USB_TYPE getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf) ;
donatien 8:0d1ec493842c 38 USB_TYPE getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint16_t * len_conf_descr = NULL) ;
donatien 0:ae46a0638b2c 39 USB_TYPE setConfiguration(USBDeviceConnected * dev, uint8_t conf) ;
donatien 0:ae46a0638b2c 40 USB_TYPE getStringDescriptor(USBDeviceConnected * dev, uint8_t index, uint8_t * buf) ;
donatien 0:ae46a0638b2c 41 USB_TYPE getReportDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint8_t len) ;
donatien 0:ae46a0638b2c 42
donatien 0:ae46a0638b2c 43 /*
donatien 0:ae46a0638b2c 44 * Control read: setup stage, data stage and status stage
donatien 0:ae46a0638b2c 45 *
donatien 0:ae46a0638b2c 46 * @param dev the control read will be done for this device
donatien 0:ae46a0638b2c 47 * @param requestType request type
donatien 0:ae46a0638b2c 48 * @param request request
donatien 0:ae46a0638b2c 49 * @param value value
donatien 0:ae46a0638b2c 50 * @param index index
donatien 0:ae46a0638b2c 51 * @param buf pointer on a buffer where will be store the data received
donatien 0:ae46a0638b2c 52 * @param len length of the transfer
donatien 0:ae46a0638b2c 53 *
donatien 0:ae46a0638b2c 54 * @returns status of the control read
donatien 0:ae46a0638b2c 55 */
donatien 0:ae46a0638b2c 56 USB_TYPE controlRead(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) ;
donatien 0:ae46a0638b2c 57
donatien 0:ae46a0638b2c 58 /*
donatien 0:ae46a0638b2c 59 * Control write: setup stage, data stage and status stage
donatien 0:ae46a0638b2c 60 *
donatien 0:ae46a0638b2c 61 * @param dev the control write will be done for this device
donatien 0:ae46a0638b2c 62 * @param requestType request type
donatien 0:ae46a0638b2c 63 * @param request request
donatien 0:ae46a0638b2c 64 * @param value value
donatien 0:ae46a0638b2c 65 * @param index index
donatien 0:ae46a0638b2c 66 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 67 * @param len length of the transfer
donatien 0:ae46a0638b2c 68 *
donatien 0:ae46a0638b2c 69 * @returns status of the control write
donatien 0:ae46a0638b2c 70 */
donatien 0:ae46a0638b2c 71 USB_TYPE controlWrite(USBDeviceConnected * dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) ;
donatien 0:ae46a0638b2c 72
donatien 0:ae46a0638b2c 73
donatien 0:ae46a0638b2c 74 /*
donatien 0:ae46a0638b2c 75 * Bulk read
donatien 0:ae46a0638b2c 76 *
donatien 0:ae46a0638b2c 77 * @param dev the bulk transfer will be done for this device
donatien 9:c9e9817c398c 78 * @param ep USBEndpoint which will be used to read a packet
donatien 0:ae46a0638b2c 79 * @param buf pointer on a buffer where will be store the data received
donatien 0:ae46a0638b2c 80 * @param len length of the transfer
donatien 0:ae46a0638b2c 81 * @param blocking if true, the read is blocking (wait for completion)
donatien 0:ae46a0638b2c 82 *
donatien 0:ae46a0638b2c 83 * @returns status of the bulk read
donatien 0:ae46a0638b2c 84 */
donatien 9:c9e9817c398c 85 USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 86
donatien 0:ae46a0638b2c 87 /*
donatien 0:ae46a0638b2c 88 * Bulk write
donatien 0:ae46a0638b2c 89 *
donatien 0:ae46a0638b2c 90 * @param dev the bulk transfer will be done for this device
donatien 9:c9e9817c398c 91 * @param ep USBEndpoint which will be used to write a packet
donatien 0:ae46a0638b2c 92 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 93 * @param len length of the transfer
donatien 0:ae46a0638b2c 94 * @param blocking if true, the write is blocking (wait for completion)
donatien 0:ae46a0638b2c 95 *
donatien 0:ae46a0638b2c 96 * @returns status of the bulk write
donatien 0:ae46a0638b2c 97 */
donatien 9:c9e9817c398c 98 USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 99
donatien 0:ae46a0638b2c 100 /*
donatien 0:ae46a0638b2c 101 * Interrupt read
donatien 0:ae46a0638b2c 102 *
donatien 0:ae46a0638b2c 103 * @param dev the bulk transfer will be done for this device
donatien 9:c9e9817c398c 104 * @param ep USBEndpoint which will be used to write a packet
donatien 0:ae46a0638b2c 105 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 106 * @param len length of the transfer
donatien 0:ae46a0638b2c 107 * @param blocking if true, the read is blocking (wait for completion)
donatien 0:ae46a0638b2c 108 *
donatien 0:ae46a0638b2c 109 * @returns status of the interrupt read
donatien 0:ae46a0638b2c 110 */
donatien 9:c9e9817c398c 111 USB_TYPE interruptRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 112
donatien 0:ae46a0638b2c 113 /*
donatien 0:ae46a0638b2c 114 * Interrupt write
donatien 0:ae46a0638b2c 115 *
donatien 0:ae46a0638b2c 116 * @param dev the bulk transfer will be done for this device
donatien 9:c9e9817c398c 117 * @param ep USBEndpoint which will be used to write a packet
donatien 0:ae46a0638b2c 118 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 119 * @param len length of the transfer
donatien 0:ae46a0638b2c 120 * @param blocking if true, the write is blocking (wait for completion)
donatien 0:ae46a0638b2c 121 *
donatien 0:ae46a0638b2c 122 * @returns status of the interrupt write
donatien 0:ae46a0638b2c 123 */
donatien 9:c9e9817c398c 124 USB_TYPE interruptWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 125
donatien 0:ae46a0638b2c 126 /*
donatien 0:ae46a0638b2c 127 * Enumerate a device. This method is responsible for:
donatien 0:ae46a0638b2c 128 * - set the address of the device
donatien 0:ae46a0638b2c 129 * - fill a USBDeviceConnected object:
donatien 0:ae46a0638b2c 130 * - add interfaces, endpoints, ...
donatien 0:ae46a0638b2c 131 * - set a configuration
donatien 0:ae46a0638b2c 132 *
donatien 0:ae46a0638b2c 133 * @param dev device which will be enumerated
donatien 0:ae46a0638b2c 134 *
donatien 0:ae46a0638b2c 135 * @returns status of the enumeration
donatien 0:ae46a0638b2c 136 */
donatien 6:075e36a3463e 137 USB_TYPE enumerate(USBDeviceConnected * dev, IUSBEnumerator* pEnumerator) ;
donatien 0:ae46a0638b2c 138
donatien 0:ae46a0638b2c 139 /*
donatien 0:ae46a0638b2c 140 * Get a device
donatien 0:ae46a0638b2c 141 *
donatien 0:ae46a0638b2c 142 * @param index index of the device which will be returned
donatien 0:ae46a0638b2c 143 *
donatien 0:ae46a0638b2c 144 * @returns pointer on the "index" device
donatien 0:ae46a0638b2c 145 */
donatien 0:ae46a0638b2c 146 USBDeviceConnected * getDevice(uint8_t index) ;
donatien 0:ae46a0638b2c 147
donatien 0:ae46a0638b2c 148 /*
donatien 0:ae46a0638b2c 149 * reset port and hub of a specific device
donatien 0:ae46a0638b2c 150 *
donatien 0:ae46a0638b2c 151 * @param pointer on the device hich will be reseted
donatien 0:ae46a0638b2c 152 */
donatien 0:ae46a0638b2c 153 void resetDevice(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 154
donatien 0:ae46a0638b2c 155 /*
donatien 0:ae46a0638b2c 156 * If there is a HID device connected, the host stores the length of the report descriptor.
donatien 0:ae46a0638b2c 157 * This avoid to the driver to re-ask the configuration descriptor to request the report descriptor
donatien 0:ae46a0638b2c 158 *
donatien 0:ae46a0638b2c 159 * @returns length of the report descriptor
donatien 0:ae46a0638b2c 160 */
donatien 0:ae46a0638b2c 161 uint16_t getLengthReportDescr() {
donatien 0:ae46a0638b2c 162 return lenReportDescr;
donatien 0:ae46a0638b2c 163 };
donatien 0:ae46a0638b2c 164
donatien 0:ae46a0638b2c 165 /**
donatien 0:ae46a0638b2c 166 * register a driver into the host associated with a callback function called when the device is disconnected
donatien 0:ae46a0638b2c 167 *
donatien 0:ae46a0638b2c 168 * @param dev device
donatien 0:ae46a0638b2c 169 * @param tptr pointer to the object to call the member function on
donatien 0:ae46a0638b2c 170 * @param mptr pointer to the member function to be called
donatien 0:ae46a0638b2c 171 */
donatien 0:ae46a0638b2c 172 template<typename T>
donatien 0:ae46a0638b2c 173 void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
donatien 0:ae46a0638b2c 174 int index = findDevice(dev);
donatien 0:ae46a0638b2c 175 if ((index != -1) && (mptr != NULL) && (tptr != NULL)) {
donatien 0:ae46a0638b2c 176 dev->onDisconnect(intf, tptr, mptr);
donatien 0:ae46a0638b2c 177 }
donatien 0:ae46a0638b2c 178 }
donatien 0:ae46a0638b2c 179
donatien 0:ae46a0638b2c 180 /**
donatien 0:ae46a0638b2c 181 * register a driver into the host associated with a callback function called when the device is disconnected
donatien 0:ae46a0638b2c 182 *
donatien 0:ae46a0638b2c 183 * @param dev device
donatien 0:ae46a0638b2c 184 * @param fn callback called when the specified device has been disconnected
donatien 0:ae46a0638b2c 185 */
donatien 0:ae46a0638b2c 186 void registerDriver(USBDeviceConnected * dev, uint8_t intf, void (*fn)(void)) {
donatien 0:ae46a0638b2c 187 int index = findDevice(dev);
donatien 0:ae46a0638b2c 188 if ((index != -1) && (fn != NULL)) {
donatien 0:ae46a0638b2c 189 dev->onDisconnect(intf, fn);
donatien 0:ae46a0638b2c 190 }
donatien 0:ae46a0638b2c 191 }
donatien 0:ae46a0638b2c 192
donatien 0:ae46a0638b2c 193
donatien 0:ae46a0638b2c 194 protected:
donatien 0:ae46a0638b2c 195
donatien 0:ae46a0638b2c 196 /*
donatien 0:ae46a0638b2c 197 * Virtual method called when a device has been connected
donatien 0:ae46a0638b2c 198 *
donatien 0:ae46a0638b2c 199 * @param hub hub number of the device
donatien 0:ae46a0638b2c 200 * @param port port number of the device
donatien 0:ae46a0638b2c 201 * @param lowSpeed 1 if low speed, 0 otherwise
donatien 0:ae46a0638b2c 202 */
donatien 0:ae46a0638b2c 203 virtual void deviceConnected(int hub, int port, bool lowSpeed) ;
donatien 0:ae46a0638b2c 204
donatien 0:ae46a0638b2c 205 /*
donatien 0:ae46a0638b2c 206 * Virtuel method called when a device has been disconnected
donatien 0:ae46a0638b2c 207 *
donatien 0:ae46a0638b2c 208 * @param hub hub number of the device
donatien 0:ae46a0638b2c 209 * @param port port number of the device
donatien 0:ae46a0638b2c 210 * @param addr list of the TDs which have been completed to dequeue freed TDs
donatien 0:ae46a0638b2c 211 */
donatien 0:ae46a0638b2c 212 virtual void deviceDisconnected(int hub, int port, volatile uint32_t addr) ;
donatien 0:ae46a0638b2c 213
donatien 0:ae46a0638b2c 214 /*
donatien 0:ae46a0638b2c 215 * Virtual method called when a transfer has been completed
donatien 0:ae46a0638b2c 216 *
donatien 0:ae46a0638b2c 217 * @param addr list of the TDs which have been completed
donatien 0:ae46a0638b2c 218 */
donatien 0:ae46a0638b2c 219 virtual void transferCompleted(volatile uint32_t addr) ;
donatien 0:ae46a0638b2c 220
donatien 0:ae46a0638b2c 221
donatien 0:ae46a0638b2c 222 private:
donatien 0:ae46a0638b2c 223 // singleton class -> constructor is private
donatien 0:ae46a0638b2c 224 USBHost();
donatien 0:ae46a0638b2c 225
donatien 0:ae46a0638b2c 226 static USBHost * instHost;
donatien 0:ae46a0638b2c 227
donatien 0:ae46a0638b2c 228 uint8_t nb_devices;
donatien 0:ae46a0638b2c 229 uint16_t lenReportDescr;
donatien 0:ae46a0638b2c 230
donatien 0:ae46a0638b2c 231 void fillControlBuf(uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, int len) ;
donatien 6:075e36a3463e 232 void parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len, IUSBEnumerator* pEnumerator) ;
donatien 0:ae46a0638b2c 233 void freeDevice(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 234 int findDevice(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 235
donatien 0:ae46a0638b2c 236
donatien 0:ae46a0638b2c 237 // endpoints
donatien 9:c9e9817c398c 238 void unqueueEndpoint(USBEndpoint * ep) ;
donatien 9:c9e9817c398c 239 USBEndpoint endpoints[MAX_ENDPOINT];
donatien 9:c9e9817c398c 240 USBEndpoint* volatile control;
donatien 0:ae46a0638b2c 241
donatien 9:c9e9817c398c 242 USBEndpoint* volatile headControlEndpoint;
donatien 9:c9e9817c398c 243 USBEndpoint* volatile headBulkEndpoint;
donatien 9:c9e9817c398c 244 USBEndpoint* volatile headInterruptEndpoint;
donatien 0:ae46a0638b2c 245
donatien 9:c9e9817c398c 246 USBEndpoint* volatile tailControlEndpoint;
donatien 9:c9e9817c398c 247 USBEndpoint* volatile tailBulkEndpoint;
donatien 9:c9e9817c398c 248 USBEndpoint* volatile tailInterruptEndpoint;
donatien 0:ae46a0638b2c 249
donatien 0:ae46a0638b2c 250 bool controlEndpointAllocated;
donatien 0:ae46a0638b2c 251
donatien 0:ae46a0638b2c 252
donatien 0:ae46a0638b2c 253 // devices connected
donatien 0:ae46a0638b2c 254 USBDeviceConnected devices[MAX_DEVICE_NB];
donatien 0:ae46a0638b2c 255 volatile bool deviceInUse[MAX_DEVICE_NB];
donatien 0:ae46a0638b2c 256 volatile bool deviceReset[MAX_DEVICE_NB];
donatien 0:ae46a0638b2c 257
donatien 0:ae46a0638b2c 258 /*
donatien 0:ae46a0638b2c 259 * Add a transfer on the TD linked list associated to an ED
donatien 0:ae46a0638b2c 260 *
donatien 0:ae46a0638b2c 261 * @param ed the transfer is associated to this ed
donatien 0:ae46a0638b2c 262 * @param buf pointer on a buffer where will be read/write data to send or receive
donatien 0:ae46a0638b2c 263 * @param len transfer length
donatien 0:ae46a0638b2c 264 *
donatien 0:ae46a0638b2c 265 * @return status of the transfer
donatien 0:ae46a0638b2c 266 */
donatien 9:c9e9817c398c 267 USB_TYPE addTransfer(USBEndpoint * ed, uint8_t * buf, uint32_t len) ;
donatien 0:ae46a0638b2c 268
donatien 0:ae46a0638b2c 269 /*
donatien 9:c9e9817c398c 270 * Link the USBEndpoint to the linked list and attach an USBEndpoint this USBEndpoint to a device
donatien 0:ae46a0638b2c 271 *
donatien 0:ae46a0638b2c 272 * @param dev pointer on a USBDeviceConnected object
donatien 9:c9e9817c398c 273 * @param ep pointer on the USBEndpoint which will be added
donatien 0:ae46a0638b2c 274 *
donatien 0:ae46a0638b2c 275 * return true if successful
donatien 0:ae46a0638b2c 276 */
donatien 9:c9e9817c398c 277 bool addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, USBEndpoint * ep) ;
donatien 0:ae46a0638b2c 278
donatien 0:ae46a0638b2c 279 /*
donatien 9:c9e9817c398c 280 * Create an USBEndpoint descriptor. Warning: the USBEndpoint is not linked.
donatien 0:ae46a0638b2c 281 *
donatien 9:c9e9817c398c 282 * @param type USBEndpoint type (CONTROL_ENDPOINT, BULK_ENDPOINT, INTERRUPT_ENDPOINT)
donatien 9:c9e9817c398c 283 * @param dir USBEndpoint direction (no meaning for CONTROL_ENDPOINT)
donatien 9:c9e9817c398c 284 * @param size USBEndpoint max packet size
donatien 9:c9e9817c398c 285 * @param addr USBEndpoint address
donatien 0:ae46a0638b2c 286 *
donatien 9:c9e9817c398c 287 * @returns pointer on the USBEndpoint created
donatien 0:ae46a0638b2c 288 */
donatien 9:c9e9817c398c 289 USBEndpoint * newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr) ;
donatien 0:ae46a0638b2c 290
donatien 0:ae46a0638b2c 291
donatien 0:ae46a0638b2c 292 // to store a setup packet
donatien 0:ae46a0638b2c 293 uint8_t setupPacket[8];
donatien 0:ae46a0638b2c 294
donatien 0:ae46a0638b2c 295
donatien 0:ae46a0638b2c 296 /////////////////////////
donatien 0:ae46a0638b2c 297 /// FOR DEBUG
donatien 0:ae46a0638b2c 298 /////////////////////////
donatien 0:ae46a0638b2c 299 void printBulk();
donatien 0:ae46a0638b2c 300 void printInt();
donatien 0:ae46a0638b2c 301
donatien 0:ae46a0638b2c 302 };
donatien 0:ae46a0638b2c 303
donatien 0:ae46a0638b2c 304 #endif