I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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