NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hexley 0:281d6ff68967 1
hexley 0:281d6ff68967 2 /*
hexley 0:281d6ff68967 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
hexley 0:281d6ff68967 4
hexley 0:281d6ff68967 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hexley 0:281d6ff68967 6 of this software and associated documentation files (the "Software"), to deal
hexley 0:281d6ff68967 7 in the Software without restriction, including without limitation the rights
hexley 0:281d6ff68967 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hexley 0:281d6ff68967 9 copies of the Software, and to permit persons to whom the Software is
hexley 0:281d6ff68967 10 furnished to do so, subject to the following conditions:
hexley 0:281d6ff68967 11
hexley 0:281d6ff68967 12 The above copyright notice and this permission notice shall be included in
hexley 0:281d6ff68967 13 all copies or substantial portions of the Software.
hexley 0:281d6ff68967 14
hexley 0:281d6ff68967 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hexley 0:281d6ff68967 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hexley 0:281d6ff68967 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hexley 0:281d6ff68967 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hexley 0:281d6ff68967 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hexley 0:281d6ff68967 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hexley 0:281d6ff68967 21 THE SOFTWARE.
hexley 0:281d6ff68967 22 */
hexley 0:281d6ff68967 23
hexley 0:281d6ff68967 24 #include "UsbDevice.h"
hexley 0:281d6ff68967 25
hexley 0:281d6ff68967 26 #include "netCfg.h"
hexley 0:281d6ff68967 27 #if NET_USB
hexley 0:281d6ff68967 28
hexley 0:281d6ff68967 29 //#define __DEBUG
hexley 0:281d6ff68967 30 #include "dbg/dbg.h"
hexley 0:281d6ff68967 31
hexley 0:281d6ff68967 32 UsbDevice::UsbDevice( UsbHostMgr* pMgr, int hub, int port, int addr ) : m_pControlEp(NULL), /*m_controlEp( this, 0x00, false, USB_CONTROL, 8 ),*/
hexley 0:281d6ff68967 33 m_pMgr(pMgr), m_connected(false), m_enumerated(false), m_hub(hub), m_port(port), m_addr(addr), m_refs(0),
hexley 0:281d6ff68967 34 m_vid(0), m_pid(0)
hexley 0:281d6ff68967 35 {
hexley 0:281d6ff68967 36
hexley 0:281d6ff68967 37 }
hexley 0:281d6ff68967 38
hexley 0:281d6ff68967 39 UsbDevice::~UsbDevice()
hexley 0:281d6ff68967 40 {
hexley 0:281d6ff68967 41 if(m_pControlEp)
hexley 0:281d6ff68967 42 delete m_pControlEp;
hexley 0:281d6ff68967 43 }
hexley 0:281d6ff68967 44
hexley 0:281d6ff68967 45 UsbErr UsbDevice::enumerate()
hexley 0:281d6ff68967 46 {
hexley 0:281d6ff68967 47 // USB_INT32S rc;
hexley 0:281d6ff68967 48
hexley 0:281d6ff68967 49 UsbErr rc;
hexley 0:281d6ff68967 50
hexley 0:281d6ff68967 51 DBG("Starting enumeration (m_pMgr = %p)\n", m_pMgr);
hexley 0:281d6ff68967 52
hexley 0:281d6ff68967 53 #if 1
hexley 0:281d6ff68967 54 m_pMgr->resetPort(m_hub, m_port);
hexley 0:281d6ff68967 55 #else
hexley 0:281d6ff68967 56 wait_ms(100); /* USB 2.0 spec says atleast 50ms delay beore port reset */
hexley 0:281d6ff68967 57 LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRS; // Initiate port reset
hexley 0:281d6ff68967 58 while (LPC_USB->HcRhPortStatus1 & OR_RH_PORT_PRS)
hexley 0:281d6ff68967 59 __WFI(); // Wait for port reset to complete...
hexley 0:281d6ff68967 60 LPC_USB->HcRhPortStatus1 = OR_RH_PORT_PRSC; // ...and clear port reset signal
hexley 0:281d6ff68967 61 wait_ms(200); /* Wait for 100 MS after port reset */
hexley 0:281d6ff68967 62 #endif
hexley 0:281d6ff68967 63
hexley 0:281d6ff68967 64 DBG("Port reset\n");
hexley 0:281d6ff68967 65
hexley 0:281d6ff68967 66 wait_ms(200);
hexley 0:281d6ff68967 67
hexley 0:281d6ff68967 68 m_pControlEp = new UsbEndpoint( this, 0x00, false, USB_CONTROL, 8, 0 );
hexley 0:281d6ff68967 69
hexley 0:281d6ff68967 70 //EDCtrl->Control = 8 << 16;/* Put max pkt size = 8 */
hexley 0:281d6ff68967 71 /* Read first 8 bytes of device desc */
hexley 0:281d6ff68967 72 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_DEVICE << 8)|(0), 0, m_controlDataBuf, 8);
hexley 0:281d6ff68967 73 if (rc)
hexley 0:281d6ff68967 74 {
hexley 0:281d6ff68967 75 DBG("RC=%d",rc);
hexley 0:281d6ff68967 76 return (rc);
hexley 0:281d6ff68967 77 }
hexley 0:281d6ff68967 78
hexley 0:281d6ff68967 79 DBG("Got descriptor, max ep size is %d\n", m_controlDataBuf[7]);
hexley 0:281d6ff68967 80
hexley 0:281d6ff68967 81 m_pControlEp->updateSize(m_controlDataBuf[7]); /* Get max pkt size of endpoint 0 */
hexley 0:281d6ff68967 82 rc = controlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, SET_ADDRESS, m_addr, 0, NULL, 0); /* Set the device address to m_addr */
hexley 0:281d6ff68967 83 if (rc)
hexley 0:281d6ff68967 84 {
hexley 0:281d6ff68967 85 // PRINT_Err(rc);
hexley 0:281d6ff68967 86 return (rc);
hexley 0:281d6ff68967 87 }
hexley 0:281d6ff68967 88 wait_ms(2);
hexley 0:281d6ff68967 89 //EDCtrl->Control = (EDCtrl->Control) | 1; /* Modify control pipe with address 1 */
hexley 0:281d6ff68967 90
hexley 0:281d6ff68967 91 //Update address
hexley 0:281d6ff68967 92 m_pControlEp->updateAddr(m_addr);
hexley 0:281d6ff68967 93 DBG("Ep addr is now %d", m_addr);
hexley 0:281d6ff68967 94 /**/
hexley 0:281d6ff68967 95
hexley 0:281d6ff68967 96 //rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_DEVICE, 0, TDBuffer, 17); //Read full device descriptor
hexley 0:281d6ff68967 97 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_DEVICE << 8)|(0), 0, m_controlDataBuf, 17);
hexley 0:281d6ff68967 98 if (rc)
hexley 0:281d6ff68967 99 {
hexley 0:281d6ff68967 100 //PRINT_Err(rc);
hexley 0:281d6ff68967 101 return (rc);
hexley 0:281d6ff68967 102 }
hexley 0:281d6ff68967 103 /*
hexley 0:281d6ff68967 104 rc = SerialCheckVidPid();
hexley 0:281d6ff68967 105 if (rc != OK) {
hexley 0:281d6ff68967 106 PRINT_Err(rc);
hexley 0:281d6ff68967 107 return (rc);
hexley 0:281d6ff68967 108 }
hexley 0:281d6ff68967 109 */
hexley 0:281d6ff68967 110 /**/
hexley 0:281d6ff68967 111
hexley 0:281d6ff68967 112 m_vid = *((uint16_t*)&m_controlDataBuf[8]);
hexley 0:281d6ff68967 113 m_pid = *((uint16_t*)&m_controlDataBuf[10]);
hexley 0:281d6ff68967 114
hexley 0:281d6ff68967 115 DBG("VID: %02x, PID: %02x\n", m_vid, m_pid);
hexley 0:281d6ff68967 116 /* Get the configuration descriptor */
hexley 0:281d6ff68967 117 //rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, 9);
hexley 0:281d6ff68967 118 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_CONFIGURATION << 8)|(0), 0, m_controlDataBuf, 9);
hexley 0:281d6ff68967 119 if (rc)
hexley 0:281d6ff68967 120 {
hexley 0:281d6ff68967 121 //PRINT_Err(rc);
hexley 0:281d6ff68967 122 return (rc);
hexley 0:281d6ff68967 123 }
hexley 0:281d6ff68967 124 /* Get the first configuration data */
hexley 0:281d6ff68967 125 //rc = HOST_GET_DESCRIPTOR(USB_DESCRIPTOR_TYPE_CONFIGURATION, 0, TDBuffer, *((uint16_t*)&TDBuffer[2]));
hexley 0:281d6ff68967 126 rc = controlReceive(USB_DEVICE_TO_HOST | USB_RECIPIENT_DEVICE, GET_DESCRIPTOR, (USB_DESCRIPTOR_TYPE_CONFIGURATION << 8)|(0), 0, m_controlDataBuf, *((uint16_t*)&m_controlDataBuf[2]));
hexley 0:281d6ff68967 127 if (rc)
hexley 0:281d6ff68967 128 {
hexley 0:281d6ff68967 129 //PRINT_Err(rc);
hexley 0:281d6ff68967 130 return (rc);
hexley 0:281d6ff68967 131 }
hexley 0:281d6ff68967 132
hexley 0:281d6ff68967 133 DBG("Desc len is %d\n", *((uint16_t*)&m_controlDataBuf[2]));
hexley 0:281d6ff68967 134
hexley 0:281d6ff68967 135 DBG("Set configuration\n");
hexley 0:281d6ff68967 136
hexley 0:281d6ff68967 137 //rc = USBH_SET_CONFIGURATION(1);/* Select device configuration 1 */
hexley 0:281d6ff68967 138 rc = controlSend(USB_HOST_TO_DEVICE | USB_RECIPIENT_DEVICE, SET_CONFIGURATION, 1, 0, NULL, 0);
hexley 0:281d6ff68967 139 if (rc)
hexley 0:281d6ff68967 140 {
hexley 0:281d6ff68967 141 // PRINT_Err(rc);
hexley 0:281d6ff68967 142 return rc;
hexley 0:281d6ff68967 143 }
hexley 0:281d6ff68967 144 wait_ms(100);/* Some devices may require this delay */
hexley 0:281d6ff68967 145
hexley 0:281d6ff68967 146 m_enumerated = true;
hexley 0:281d6ff68967 147 return USBERR_OK;
hexley 0:281d6ff68967 148 }
hexley 0:281d6ff68967 149
hexley 0:281d6ff68967 150 bool UsbDevice::connected()
hexley 0:281d6ff68967 151 {
hexley 0:281d6ff68967 152 return m_connected;
hexley 0:281d6ff68967 153 }
hexley 0:281d6ff68967 154
hexley 0:281d6ff68967 155 bool UsbDevice::enumerated()
hexley 0:281d6ff68967 156 {
hexley 0:281d6ff68967 157 return m_enumerated;
hexley 0:281d6ff68967 158 }
hexley 0:281d6ff68967 159
hexley 0:281d6ff68967 160 int UsbDevice::getPid()
hexley 0:281d6ff68967 161 {
hexley 0:281d6ff68967 162 return m_pid;
hexley 0:281d6ff68967 163 }
hexley 0:281d6ff68967 164
hexley 0:281d6ff68967 165 int UsbDevice::getVid()
hexley 0:281d6ff68967 166 {
hexley 0:281d6ff68967 167 return m_vid;
hexley 0:281d6ff68967 168 }
hexley 0:281d6ff68967 169
hexley 0:281d6ff68967 170 UsbErr UsbDevice::getConfigurationDescriptor(int config, uint8_t** pBuf)
hexley 0:281d6ff68967 171 {
hexley 0:281d6ff68967 172 //For now olny one config
hexley 0:281d6ff68967 173 *pBuf = m_controlDataBuf;
hexley 0:281d6ff68967 174 return USBERR_OK;
hexley 0:281d6ff68967 175 }
hexley 0:281d6ff68967 176
hexley 0:281d6ff68967 177 UsbErr UsbDevice::getInterfaceDescriptor(int config, int item, uint8_t** pBuf)
hexley 0:281d6ff68967 178 {
hexley 0:281d6ff68967 179 byte* desc_ptr = m_controlDataBuf;
hexley 0:281d6ff68967 180
hexley 0:281d6ff68967 181 /* if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION)
hexley 0:281d6ff68967 182 {
hexley 0:281d6ff68967 183 return USBERR_BADCONFIG;
hexley 0:281d6ff68967 184 }*/
hexley 0:281d6ff68967 185
hexley 0:281d6ff68967 186 if(item>=m_controlDataBuf[4])//Interfaces count
hexley 0:281d6ff68967 187 return USBERR_NOTFOUND;
hexley 0:281d6ff68967 188
hexley 0:281d6ff68967 189 desc_ptr += desc_ptr[0];
hexley 0:281d6ff68967 190
hexley 0:281d6ff68967 191 *pBuf = NULL;
hexley 0:281d6ff68967 192
hexley 0:281d6ff68967 193 while (desc_ptr < m_controlDataBuf + *((uint16_t*)&m_controlDataBuf[2]))
hexley 0:281d6ff68967 194 {
hexley 0:281d6ff68967 195
hexley 0:281d6ff68967 196 switch (desc_ptr[1]) {
hexley 0:281d6ff68967 197 case USB_DESCRIPTOR_TYPE_INTERFACE:
hexley 0:281d6ff68967 198 if(desc_ptr[2] == item)
hexley 0:281d6ff68967 199 {
hexley 0:281d6ff68967 200 *pBuf = desc_ptr;
hexley 0:281d6ff68967 201 return USBERR_OK;
hexley 0:281d6ff68967 202 }
hexley 0:281d6ff68967 203 desc_ptr += desc_ptr[0]; // Move to next descriptor start
hexley 0:281d6ff68967 204 break;
hexley 0:281d6ff68967 205 }
hexley 0:281d6ff68967 206
hexley 0:281d6ff68967 207 }
hexley 0:281d6ff68967 208
hexley 0:281d6ff68967 209 if(*pBuf == NULL)
hexley 0:281d6ff68967 210 return USBERR_NOTFOUND;
hexley 0:281d6ff68967 211
hexley 0:281d6ff68967 212 return USBERR_OK;
hexley 0:281d6ff68967 213 }
hexley 0:281d6ff68967 214
hexley 0:281d6ff68967 215
hexley 0:281d6ff68967 216 UsbErr UsbDevice::setConfiguration(int config)
hexley 0:281d6ff68967 217 {
hexley 0:281d6ff68967 218 return USBERR_OK;
hexley 0:281d6ff68967 219 }
hexley 0:281d6ff68967 220
hexley 0:281d6ff68967 221 UsbErr UsbDevice::controlSend(byte requestType, byte request, word value, word index, const byte* buf, int len)
hexley 0:281d6ff68967 222 {
hexley 0:281d6ff68967 223 UsbErr rc;
hexley 0:281d6ff68967 224 fillControlBuf(requestType, request, value, index, len);
hexley 0:281d6ff68967 225 m_pControlEp->setNextToken(TD_SETUP);
hexley 0:281d6ff68967 226 rc = m_pControlEp->transfer(m_controlBuf, 8);
hexley 0:281d6ff68967 227 while(m_pControlEp->status() == USBERR_PROCESSING);
hexley 0:281d6ff68967 228 rc = (UsbErr) MIN(0, m_pControlEp->status());
hexley 0:281d6ff68967 229 if(rc)
hexley 0:281d6ff68967 230 return rc;
hexley 0:281d6ff68967 231 if(len)
hexley 0:281d6ff68967 232 {
hexley 0:281d6ff68967 233 m_pControlEp->setNextToken(TD_OUT);
hexley 0:281d6ff68967 234 rc = m_pControlEp->transfer((byte*)buf, len);
hexley 0:281d6ff68967 235 while(m_pControlEp->status() == USBERR_PROCESSING);
hexley 0:281d6ff68967 236 rc = (UsbErr) MIN(0, m_pControlEp->status());
hexley 0:281d6ff68967 237 if(rc)
hexley 0:281d6ff68967 238 return rc;
hexley 0:281d6ff68967 239 }
hexley 0:281d6ff68967 240 m_pControlEp->setNextToken(TD_IN);
hexley 0:281d6ff68967 241 rc = m_pControlEp->transfer(NULL, 0);
hexley 0:281d6ff68967 242 while(m_pControlEp->status() == USBERR_PROCESSING);
hexley 0:281d6ff68967 243 rc = (UsbErr) MIN(0, m_pControlEp->status());
hexley 0:281d6ff68967 244 if(rc)
hexley 0:281d6ff68967 245 return rc;
hexley 0:281d6ff68967 246 return USBERR_OK;
hexley 0:281d6ff68967 247 }
hexley 0:281d6ff68967 248
hexley 0:281d6ff68967 249 UsbErr UsbDevice::controlReceive(byte requestType, byte request, word value, word index, const byte* buf, int len)
hexley 0:281d6ff68967 250 {
hexley 0:281d6ff68967 251 UsbErr rc;
hexley 0:281d6ff68967 252 fillControlBuf(requestType, request, value, index, len);
hexley 0:281d6ff68967 253 m_pControlEp->setNextToken(TD_SETUP);
hexley 0:281d6ff68967 254 rc = m_pControlEp->transfer(m_controlBuf, 8);
hexley 0:281d6ff68967 255 while(m_pControlEp->status() == USBERR_PROCESSING);
hexley 0:281d6ff68967 256 rc = (UsbErr) MIN(0, m_pControlEp->status());
hexley 0:281d6ff68967 257 if(rc)
hexley 0:281d6ff68967 258 return rc;
hexley 0:281d6ff68967 259 if(len)
hexley 0:281d6ff68967 260 {
hexley 0:281d6ff68967 261 m_pControlEp->setNextToken(TD_IN);
hexley 0:281d6ff68967 262 rc = m_pControlEp->transfer( (byte*) buf, len);
hexley 0:281d6ff68967 263 while(m_pControlEp->status() == USBERR_PROCESSING);
hexley 0:281d6ff68967 264 rc = (UsbErr) MIN(0, m_pControlEp->status());
hexley 0:281d6ff68967 265 if(rc)
hexley 0:281d6ff68967 266 return rc;
hexley 0:281d6ff68967 267 }
hexley 0:281d6ff68967 268 m_pControlEp->setNextToken(TD_OUT);
hexley 0:281d6ff68967 269 rc = m_pControlEp->transfer(NULL, 0);
hexley 0:281d6ff68967 270 while(m_pControlEp->status() == USBERR_PROCESSING);
hexley 0:281d6ff68967 271 rc = (UsbErr) MIN(0, m_pControlEp->status());
hexley 0:281d6ff68967 272 if(rc)
hexley 0:281d6ff68967 273 return rc;
hexley 0:281d6ff68967 274 return USBERR_OK;
hexley 0:281d6ff68967 275 }
hexley 0:281d6ff68967 276
hexley 0:281d6ff68967 277 void UsbDevice::fillControlBuf(byte requestType, byte request, word value, word index, int len)
hexley 0:281d6ff68967 278 {
hexley 0:281d6ff68967 279 #ifdef __BIG_ENDIAN
hexley 0:281d6ff68967 280 #error "Must implement BE to LE conv here"
hexley 0:281d6ff68967 281 #endif
hexley 0:281d6ff68967 282 m_controlBuf[0] = requestType;
hexley 0:281d6ff68967 283 m_controlBuf[1] = request;
hexley 0:281d6ff68967 284 //We are in LE so it's fine
hexley 0:281d6ff68967 285 *((word*)&m_controlBuf[2]) = value;
hexley 0:281d6ff68967 286 *((word*)&m_controlBuf[4]) = index;
hexley 0:281d6ff68967 287 *((word*)&m_controlBuf[6]) = (word) len;
hexley 0:281d6ff68967 288 }
hexley 0:281d6ff68967 289
hexley 0:281d6ff68967 290
hexley 0:281d6ff68967 291 #endif