USB Host Library for Sprint Dongles

Fork of USBHostWANDongleSprint_bleedingedge by Donatien Garnier

Legacy Warning

This is an mbed 2 libary. To learn more about mbed OS 5, visit the docs.

Committer:
donatien
Date:
Wed Sep 12 08:15:02 2012 +0000
Revision:
0:bfed5767d0a5
Initial Commit

Who changed what in which revision?

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