USB Host WAN Dongle library

Fork of USBHostWANDongle_bleedingedge by Donatien Garnier

Committer:
donatien
Date:
Thu May 24 16:39:35 2012 +0000
Revision:
0:ae46a0638b2c
Child:
6:075e36a3463e
Initial Commit

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 0:ae46a0638b2c 24 #include "Endpoint.h"
donatien 0:ae46a0638b2c 25
donatien 0:ae46a0638b2c 26 #define MAX_DEVICE_NB 1
donatien 0:ae46a0638b2c 27
donatien 0:ae46a0638b2c 28 // singleton class
donatien 0:ae46a0638b2c 29 class USBHost : public USBHALHost {
donatien 0:ae46a0638b2c 30 public:
donatien 0:ae46a0638b2c 31 /*
donatien 0:ae46a0638b2c 32 * Static method to create or retrieve the single USBHost instance
donatien 0:ae46a0638b2c 33 */
donatien 0:ae46a0638b2c 34 static USBHost * getHostInst();
donatien 0:ae46a0638b2c 35
donatien 0:ae46a0638b2c 36 USB_TYPE getDeviceDescriptor(USBDeviceConnected * dev, uint8_t * buf) ;
donatien 0:ae46a0638b2c 37 USB_TYPE getConfigurationDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint8_t * len_conf_descr = NULL) ;
donatien 0:ae46a0638b2c 38 USB_TYPE setConfiguration(USBDeviceConnected * dev, uint8_t conf) ;
donatien 0:ae46a0638b2c 39 USB_TYPE getStringDescriptor(USBDeviceConnected * dev, uint8_t index, uint8_t * buf) ;
donatien 0:ae46a0638b2c 40 USB_TYPE getReportDescriptor(USBDeviceConnected * dev, uint8_t * buf, uint8_t len) ;
donatien 0:ae46a0638b2c 41
donatien 0:ae46a0638b2c 42 /*
donatien 0:ae46a0638b2c 43 * Control read: setup stage, data stage and status stage
donatien 0:ae46a0638b2c 44 *
donatien 0:ae46a0638b2c 45 * @param dev the control read will be done for this device
donatien 0:ae46a0638b2c 46 * @param requestType request type
donatien 0:ae46a0638b2c 47 * @param request request
donatien 0:ae46a0638b2c 48 * @param value value
donatien 0:ae46a0638b2c 49 * @param index index
donatien 0:ae46a0638b2c 50 * @param buf pointer on a buffer where will be store the data received
donatien 0:ae46a0638b2c 51 * @param len length of the transfer
donatien 0:ae46a0638b2c 52 *
donatien 0:ae46a0638b2c 53 * @returns status of the control read
donatien 0:ae46a0638b2c 54 */
donatien 0:ae46a0638b2c 55 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 56
donatien 0:ae46a0638b2c 57 /*
donatien 0:ae46a0638b2c 58 * Control write: setup stage, data stage and status stage
donatien 0:ae46a0638b2c 59 *
donatien 0:ae46a0638b2c 60 * @param dev the control write will be done for this device
donatien 0:ae46a0638b2c 61 * @param requestType request type
donatien 0:ae46a0638b2c 62 * @param request request
donatien 0:ae46a0638b2c 63 * @param value value
donatien 0:ae46a0638b2c 64 * @param index index
donatien 0:ae46a0638b2c 65 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 66 * @param len length of the transfer
donatien 0:ae46a0638b2c 67 *
donatien 0:ae46a0638b2c 68 * @returns status of the control write
donatien 0:ae46a0638b2c 69 */
donatien 0:ae46a0638b2c 70 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 71
donatien 0:ae46a0638b2c 72
donatien 0:ae46a0638b2c 73 /*
donatien 0:ae46a0638b2c 74 * Bulk read
donatien 0:ae46a0638b2c 75 *
donatien 0:ae46a0638b2c 76 * @param dev the bulk transfer will be done for this device
donatien 0:ae46a0638b2c 77 * @param ep endpoint which will be used to read a packet
donatien 0:ae46a0638b2c 78 * @param buf pointer on a buffer where will be store the data received
donatien 0:ae46a0638b2c 79 * @param len length of the transfer
donatien 0:ae46a0638b2c 80 * @param blocking if true, the read is blocking (wait for completion)
donatien 0:ae46a0638b2c 81 *
donatien 0:ae46a0638b2c 82 * @returns status of the bulk read
donatien 0:ae46a0638b2c 83 */
donatien 0:ae46a0638b2c 84 USB_TYPE bulkRead(USBDeviceConnected * dev, Endpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 85
donatien 0:ae46a0638b2c 86 /*
donatien 0:ae46a0638b2c 87 * Bulk write
donatien 0:ae46a0638b2c 88 *
donatien 0:ae46a0638b2c 89 * @param dev the bulk transfer will be done for this device
donatien 0:ae46a0638b2c 90 * @param ep endpoint which will be used to write a packet
donatien 0:ae46a0638b2c 91 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 92 * @param len length of the transfer
donatien 0:ae46a0638b2c 93 * @param blocking if true, the write is blocking (wait for completion)
donatien 0:ae46a0638b2c 94 *
donatien 0:ae46a0638b2c 95 * @returns status of the bulk write
donatien 0:ae46a0638b2c 96 */
donatien 0:ae46a0638b2c 97 USB_TYPE bulkWrite(USBDeviceConnected * dev, Endpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 98
donatien 0:ae46a0638b2c 99 /*
donatien 0:ae46a0638b2c 100 * Interrupt read
donatien 0:ae46a0638b2c 101 *
donatien 0:ae46a0638b2c 102 * @param dev the bulk transfer will be done for this device
donatien 0:ae46a0638b2c 103 * @param ep endpoint which will be used to write a packet
donatien 0:ae46a0638b2c 104 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 105 * @param len length of the transfer
donatien 0:ae46a0638b2c 106 * @param blocking if true, the read is blocking (wait for completion)
donatien 0:ae46a0638b2c 107 *
donatien 0:ae46a0638b2c 108 * @returns status of the interrupt read
donatien 0:ae46a0638b2c 109 */
donatien 0:ae46a0638b2c 110 USB_TYPE interruptRead(USBDeviceConnected * dev, Endpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 111
donatien 0:ae46a0638b2c 112 /*
donatien 0:ae46a0638b2c 113 * Interrupt write
donatien 0:ae46a0638b2c 114 *
donatien 0:ae46a0638b2c 115 * @param dev the bulk transfer will be done for this device
donatien 0:ae46a0638b2c 116 * @param ep endpoint which will be used to write a packet
donatien 0:ae46a0638b2c 117 * @param buf pointer on a buffer which will be written
donatien 0:ae46a0638b2c 118 * @param len length of the transfer
donatien 0:ae46a0638b2c 119 * @param blocking if true, the write is blocking (wait for completion)
donatien 0:ae46a0638b2c 120 *
donatien 0:ae46a0638b2c 121 * @returns status of the interrupt write
donatien 0:ae46a0638b2c 122 */
donatien 0:ae46a0638b2c 123 USB_TYPE interruptWrite(USBDeviceConnected * dev, Endpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) ;
donatien 0:ae46a0638b2c 124
donatien 0:ae46a0638b2c 125 /*
donatien 0:ae46a0638b2c 126 * Enumerate a device. This method is responsible for:
donatien 0:ae46a0638b2c 127 * - set the address of the device
donatien 0:ae46a0638b2c 128 * - fill a USBDeviceConnected object:
donatien 0:ae46a0638b2c 129 * - add interfaces, endpoints, ...
donatien 0:ae46a0638b2c 130 * - set a configuration
donatien 0:ae46a0638b2c 131 *
donatien 0:ae46a0638b2c 132 * @param dev device which will be enumerated
donatien 0:ae46a0638b2c 133 *
donatien 0:ae46a0638b2c 134 * @returns status of the enumeration
donatien 0:ae46a0638b2c 135 */
donatien 0:ae46a0638b2c 136 USB_TYPE enumerate(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 137
donatien 0:ae46a0638b2c 138 /*
donatien 0:ae46a0638b2c 139 * Get a device
donatien 0:ae46a0638b2c 140 *
donatien 0:ae46a0638b2c 141 * @param index index of the device which will be returned
donatien 0:ae46a0638b2c 142 *
donatien 0:ae46a0638b2c 143 * @returns pointer on the "index" device
donatien 0:ae46a0638b2c 144 */
donatien 0:ae46a0638b2c 145 USBDeviceConnected * getDevice(uint8_t index) ;
donatien 0:ae46a0638b2c 146
donatien 0:ae46a0638b2c 147 /*
donatien 0:ae46a0638b2c 148 * reset port and hub of a specific device
donatien 0:ae46a0638b2c 149 *
donatien 0:ae46a0638b2c 150 * @param pointer on the device hich will be reseted
donatien 0:ae46a0638b2c 151 */
donatien 0:ae46a0638b2c 152 void resetDevice(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 153
donatien 0:ae46a0638b2c 154 /*
donatien 0:ae46a0638b2c 155 * If there is a HID device connected, the host stores the length of the report descriptor.
donatien 0:ae46a0638b2c 156 * This avoid to the driver to re-ask the configuration descriptor to request the report descriptor
donatien 0:ae46a0638b2c 157 *
donatien 0:ae46a0638b2c 158 * @returns length of the report descriptor
donatien 0:ae46a0638b2c 159 */
donatien 0:ae46a0638b2c 160 uint16_t getLengthReportDescr() {
donatien 0:ae46a0638b2c 161 return lenReportDescr;
donatien 0:ae46a0638b2c 162 };
donatien 0:ae46a0638b2c 163
donatien 0:ae46a0638b2c 164 /**
donatien 0:ae46a0638b2c 165 * register a driver into the host associated with a callback function called when the device is disconnected
donatien 0:ae46a0638b2c 166 *
donatien 0:ae46a0638b2c 167 * @param dev device
donatien 0:ae46a0638b2c 168 * @param tptr pointer to the object to call the member function on
donatien 0:ae46a0638b2c 169 * @param mptr pointer to the member function to be called
donatien 0:ae46a0638b2c 170 */
donatien 0:ae46a0638b2c 171 template<typename T>
donatien 0:ae46a0638b2c 172 void registerDriver(USBDeviceConnected * dev, uint8_t intf, T* tptr, void (T::*mptr)(void)) {
donatien 0:ae46a0638b2c 173 int index = findDevice(dev);
donatien 0:ae46a0638b2c 174 if ((index != -1) && (mptr != NULL) && (tptr != NULL)) {
donatien 0:ae46a0638b2c 175 dev->onDisconnect(intf, tptr, mptr);
donatien 0:ae46a0638b2c 176 }
donatien 0:ae46a0638b2c 177 }
donatien 0:ae46a0638b2c 178
donatien 0:ae46a0638b2c 179 /**
donatien 0:ae46a0638b2c 180 * register a driver into the host associated with a callback function called when the device is disconnected
donatien 0:ae46a0638b2c 181 *
donatien 0:ae46a0638b2c 182 * @param dev device
donatien 0:ae46a0638b2c 183 * @param fn callback called when the specified device has been disconnected
donatien 0:ae46a0638b2c 184 */
donatien 0:ae46a0638b2c 185 void registerDriver(USBDeviceConnected * dev, uint8_t intf, void (*fn)(void)) {
donatien 0:ae46a0638b2c 186 int index = findDevice(dev);
donatien 0:ae46a0638b2c 187 if ((index != -1) && (fn != NULL)) {
donatien 0:ae46a0638b2c 188 dev->onDisconnect(intf, fn);
donatien 0:ae46a0638b2c 189 }
donatien 0:ae46a0638b2c 190 }
donatien 0:ae46a0638b2c 191
donatien 0:ae46a0638b2c 192
donatien 0:ae46a0638b2c 193 protected:
donatien 0:ae46a0638b2c 194
donatien 0:ae46a0638b2c 195 /*
donatien 0:ae46a0638b2c 196 * Virtual method called when a device has been connected
donatien 0:ae46a0638b2c 197 *
donatien 0:ae46a0638b2c 198 * @param hub hub number of the device
donatien 0:ae46a0638b2c 199 * @param port port number of the device
donatien 0:ae46a0638b2c 200 * @param lowSpeed 1 if low speed, 0 otherwise
donatien 0:ae46a0638b2c 201 */
donatien 0:ae46a0638b2c 202 virtual void deviceConnected(int hub, int port, bool lowSpeed) ;
donatien 0:ae46a0638b2c 203
donatien 0:ae46a0638b2c 204 /*
donatien 0:ae46a0638b2c 205 * Virtuel method called when a device has been disconnected
donatien 0:ae46a0638b2c 206 *
donatien 0:ae46a0638b2c 207 * @param hub hub number of the device
donatien 0:ae46a0638b2c 208 * @param port port number of the device
donatien 0:ae46a0638b2c 209 * @param addr list of the TDs which have been completed to dequeue freed TDs
donatien 0:ae46a0638b2c 210 */
donatien 0:ae46a0638b2c 211 virtual void deviceDisconnected(int hub, int port, volatile uint32_t addr) ;
donatien 0:ae46a0638b2c 212
donatien 0:ae46a0638b2c 213 /*
donatien 0:ae46a0638b2c 214 * Virtual method called when a transfer has been completed
donatien 0:ae46a0638b2c 215 *
donatien 0:ae46a0638b2c 216 * @param addr list of the TDs which have been completed
donatien 0:ae46a0638b2c 217 */
donatien 0:ae46a0638b2c 218 virtual void transferCompleted(volatile uint32_t addr) ;
donatien 0:ae46a0638b2c 219
donatien 0:ae46a0638b2c 220
donatien 0:ae46a0638b2c 221 private:
donatien 0:ae46a0638b2c 222 // singleton class -> constructor is private
donatien 0:ae46a0638b2c 223 USBHost();
donatien 0:ae46a0638b2c 224
donatien 0:ae46a0638b2c 225 static USBHost * instHost;
donatien 0:ae46a0638b2c 226
donatien 0:ae46a0638b2c 227 uint8_t nb_devices;
donatien 0:ae46a0638b2c 228 uint16_t lenReportDescr;
donatien 0:ae46a0638b2c 229
donatien 0:ae46a0638b2c 230 void fillControlBuf(uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, int len) ;
donatien 0:ae46a0638b2c 231 void parseConfDescr(USBDeviceConnected * dev, uint8_t * conf_descr, uint32_t len) ;
donatien 0:ae46a0638b2c 232 void freeDevice(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 233 int findDevice(USBDeviceConnected * dev) ;
donatien 0:ae46a0638b2c 234
donatien 0:ae46a0638b2c 235
donatien 0:ae46a0638b2c 236 // endpoints
donatien 0:ae46a0638b2c 237 void unqueueEndpoint(Endpoint * ep) ;
donatien 0:ae46a0638b2c 238 Endpoint endpoints[MAX_ENDPOINT];
donatien 0:ae46a0638b2c 239 Endpoint* volatile control;
donatien 0:ae46a0638b2c 240
donatien 0:ae46a0638b2c 241 Endpoint* volatile headControlEndpoint;
donatien 0:ae46a0638b2c 242 Endpoint* volatile headBulkEndpoint;
donatien 0:ae46a0638b2c 243 Endpoint* volatile headInterruptEndpoint;
donatien 0:ae46a0638b2c 244
donatien 0:ae46a0638b2c 245 Endpoint* volatile tailControlEndpoint;
donatien 0:ae46a0638b2c 246 Endpoint* volatile tailBulkEndpoint;
donatien 0:ae46a0638b2c 247 Endpoint* volatile tailInterruptEndpoint;
donatien 0:ae46a0638b2c 248
donatien 0:ae46a0638b2c 249 bool controlEndpointAllocated;
donatien 0:ae46a0638b2c 250
donatien 0:ae46a0638b2c 251
donatien 0:ae46a0638b2c 252 // devices connected
donatien 0:ae46a0638b2c 253 USBDeviceConnected devices[MAX_DEVICE_NB];
donatien 0:ae46a0638b2c 254 volatile bool deviceInUse[MAX_DEVICE_NB];
donatien 0:ae46a0638b2c 255 volatile bool deviceReset[MAX_DEVICE_NB];
donatien 0:ae46a0638b2c 256
donatien 0:ae46a0638b2c 257 /*
donatien 0:ae46a0638b2c 258 * Add a transfer on the TD linked list associated to an ED
donatien 0:ae46a0638b2c 259 *
donatien 0:ae46a0638b2c 260 * @param ed the transfer is associated to this ed
donatien 0:ae46a0638b2c 261 * @param buf pointer on a buffer where will be read/write data to send or receive
donatien 0:ae46a0638b2c 262 * @param len transfer length
donatien 0:ae46a0638b2c 263 *
donatien 0:ae46a0638b2c 264 * @return status of the transfer
donatien 0:ae46a0638b2c 265 */
donatien 0:ae46a0638b2c 266 USB_TYPE addTransfer(Endpoint * ed, uint8_t * buf, uint32_t len) ;
donatien 0:ae46a0638b2c 267
donatien 0:ae46a0638b2c 268 /*
donatien 0:ae46a0638b2c 269 * Link the endpoint to the linked list and attach an endpoint this endpoint to a device
donatien 0:ae46a0638b2c 270 *
donatien 0:ae46a0638b2c 271 * @param dev pointer on a USBDeviceConnected object
donatien 0:ae46a0638b2c 272 * @param ep pointer on the Endpoint which will be added
donatien 0:ae46a0638b2c 273 *
donatien 0:ae46a0638b2c 274 * return true if successful
donatien 0:ae46a0638b2c 275 */
donatien 0:ae46a0638b2c 276 bool addEndpoint(USBDeviceConnected * dev, uint8_t intf_nb, Endpoint * ep) ;
donatien 0:ae46a0638b2c 277
donatien 0:ae46a0638b2c 278 /*
donatien 0:ae46a0638b2c 279 * Create an endpoint descriptor. Warning: the endpoint is not linked.
donatien 0:ae46a0638b2c 280 *
donatien 0:ae46a0638b2c 281 * @param type endpoint type (CONTROL_ENDPOINT, BULK_ENDPOINT, INTERRUPT_ENDPOINT)
donatien 0:ae46a0638b2c 282 * @param dir endpoint direction (no meaning for CONTROL_ENDPOINT)
donatien 0:ae46a0638b2c 283 * @param size endpoint max packet size
donatien 0:ae46a0638b2c 284 * @param addr endpoint address
donatien 0:ae46a0638b2c 285 *
donatien 0:ae46a0638b2c 286 * @returns pointer on the Endpoint created
donatien 0:ae46a0638b2c 287 */
donatien 0:ae46a0638b2c 288 Endpoint * newEndpoint(ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t addr) ;
donatien 0:ae46a0638b2c 289
donatien 0:ae46a0638b2c 290
donatien 0:ae46a0638b2c 291 // to store a setup packet
donatien 0:ae46a0638b2c 292 uint8_t setupPacket[8];
donatien 0:ae46a0638b2c 293
donatien 0:ae46a0638b2c 294
donatien 0:ae46a0638b2c 295 /////////////////////////
donatien 0:ae46a0638b2c 296 /// FOR DEBUG
donatien 0:ae46a0638b2c 297 /////////////////////////
donatien 0:ae46a0638b2c 298 void printBulk();
donatien 0:ae46a0638b2c 299 void printInt();
donatien 0:ae46a0638b2c 300
donatien 0:ae46a0638b2c 301 };
donatien 0:ae46a0638b2c 302
donatien 0:ae46a0638b2c 303 #endif