/************************************************************************ * Function Name : hc_parse_trans * * This function checks the status of the transmitted or received packet * and copy the data from the SL811HS register into a buffer. * * 1) Check the status of the packet * 2) If successful, and IN packet then copy the data from the SL811HS register * into a buffer * * Input: hci = data structure for the host controller * actbytes = pointer to actual number of bytes * data = data buffer * cc = packet status * length = the urb transmit length * pid = packet ID * urb_state = the current stage of USB transaction * * Return: 0 ***********************************************************************/ static inline int hc_parse_trans (hci_t * hci, int *actbytes, __u8 * data, int *cc, int *toggle, int length, int pid, int urb_state) { __u8 addr; __u8 len; DBGFUNC ("enter hc_parse_trans\n"); /* get packet status; convert ack rcvd to ack-not-rcvd */ *cc = (int) SL811Read (hci, SL11H_PKTSTATREG); if (*cc & (SL11H_STATMASK_ERROR | SL11H_STATMASK_TMOUT | SL11H_STATMASK_OVF | SL11H_STATMASK_NAK | SL11H_STATMASK_STALL)) { if (*cc & SL11H_STATMASK_OVF) DBGERR ("parse trans: error recv ack, cc = 0x%x, TX_BASE_Len = " "0x%x, TX_count=0x%x\n", *cc, SL811Read (hci, SL11H_BUFLNTHREG), SL811Read (hci, SL11H_XFERCNTREG)); } else { DBGVERBOSE ("parse trans: recv ack, cc = 0x%x, len = 0x%x, \n", *cc, length); /* Successful data */ if ((pid == PID_IN) && (urb_state != US_CTRL_SETUP)) { /* Find the base address */ addr = SL811Read (hci, SL11H_BUFADDRREG); /* Find the Transmit Length */ len = SL811Read (hci, SL11H_BUFLNTHREG); /* The actual data length = xmit length reg - xfer count reg */ *actbytes = len - SL811Read (hci, SL11H_XFERCNTREG); if ((data != NULL) && (*actbytes > 0)) { SL811BufRead (hci, addr, data, *actbytes); } else if ((data == NULL) && (*actbytes <= 0)) { DBGERR ("hc_parse_trans: data = NULL or actbyte = 0x%x\n", *actbytes); return 0; } } else if (pid == PID_OUT) { *actbytes = length; } else { // printk ("ERR:parse_trans, pid != IN or OUT, pid = 0x%x\n", pid); } *toggle = !*toggle; } return 0; }
//***************************************************************************************** // usbXfer: // successful transfer = return TRUE // fail transfer = return FALSE //***************************************************************************************** u_int8 usbXfer(void) { u_int8 xferLen, data0, data1,cmd; u_int8 intr,result,remainder,dataX,bufLen,addr,timeout; //------------------------------------------------ // Default setting for usb trasnfer //------------------------------------------------ dataX=timeout=0; //result = SL811Read(EP0Status); data0 = EP0_Buf; // DATA0 buffer address data1 = data0 + ( u_int8)usbstack.wPayload; // DATA1 buffer address bXXGFlags.DATA_STOP=FALSE; bXXGFlags.TIMEOUT_ERR=FALSE; //------------------------------------------------ // Define data transfer payload //------------------------------------------------ if (usbstack.wLen >= usbstack.wPayload) // select proper data payload xferLen = usbstack.wPayload; // limit to wPayload size else // else take < payload len xferLen = usbstack.wLen; // // For IN token if (usbstack.pid==PID_IN) // for current IN tokens { // cmd = sDATA0_RD; // FS/FS on Hub, sync to sof } // For OUT token else if(usbstack.pid==PID_OUT) // for OUT tokens { if(xferLen) // only when there are { //intr=usbstack.setup.wLength; //usbstack.setup.wLength=WordSwap(usbstack.setup.wLength); SL811BufWrite(data0,usbstack.buffer,xferLen); // data to transfer on USB //usbstack.setup.wLength=intr; } cmd = sDATA0_WR; // FS/FS on Hub, sync to sof // implement data toggle bXXGFlags.bData1 = uDev.bData1[usbstack.endpoint]; uDev.bData1[usbstack.endpoint] = (uDev.bData1[usbstack.endpoint] ? 0 : 1); // DataToggle if(bXXGFlags.bData1) cmd |= 0x40; // Set Data1 bit in command } // For SETUP/OUT token else // for current SETUP/OUT tokens { if(xferLen) // only when there are { intr=usbstack.setup.wLength; usbstack.setup.wValue=usbstack.setup.wValue; usbstack.setup.wIndex=usbstack.setup.wIndex; usbstack.setup.wLength=usbstack.setup.wLength; SL811BufWrite(data0,( u_int8 *)&usbstack.setup,xferLen); // data to transfer on USB usbstack.setup.wLength=intr; } cmd = sDATA0_WR; // FS/FS on Hub, sync to sof } //------------------------------------------------ // For EP0's IN/OUT token data, start with DATA1 // Control Endpoint0's status stage. // For data endpoint, IN/OUT data, start ???? //------------------------------------------------ if (usbstack.endpoint == 0 && usbstack.pid != PID_SETUP) // for Ep0's IN/OUT token cmd |= 0x40; // always set DATA1 //------------------------------------------------ // Arming of USB data transfer for the first pkt //------------------------------------------------ SL811Write(EP0Status,((usbstack.endpoint&0x0F)|usbstack.pid)); // PID + EP address SL811Write(EP0Counter,usbstack.usbaddr); // USB address SL811Write(EP0Address,data0); // buffer address, start with "data0" SL811Write(EP0XferLen,xferLen); // data transfer length SL811Write(IntStatus,INT_CLEAR); // clear interrupt status SL811Write(EP0Control,cmd); // Enable ARM and USB transfer start here //------------------------------------------------ // Main loop for completing a wLen data trasnfer //------------------------------------------------ while(TRUE) { //---------------Wait for done interrupt------------------ while(TRUE) // always ensure requested device is { // inserted at all time, then you will //intr=SL811Read(cSOFcnt); //intr=SL811Read(IntEna); intr = SL811Read(IntStatus); // wait for interrupt to be done, and if((intr & USB_DETECT) || (intr & INSERT_REMOVE)) // proceed to parse result from slave { // device. bXXGFlags.DATA_STOP = TRUE; // if device is removed, set DATA_STOP return FALSE; // flag true, so that main loop will } // know this condition and exit gracefully if(intr & USB_A_DONE) break; // interrupt done !!! } SL811Write(IntStatus,INT_CLEAR); // clear interrupt status result = SL811Read(EP0Status); // read EP0status register remainder = SL811Read(EP0Counter); // remainder value in last pkt xfer //-------------------------ACK---------------------------- if (result & EP0_ACK) // Transmission ACK { // SETUP TOKEN if(usbstack.pid == PID_SETUP) // do nothing for SETUP/OUT token break; // exit while(1) immediately // OUT TOKEN else if(usbstack.pid == PID_OUT) break; // IN TOKEN else if(usbstack.pid == PID_IN) { // for IN token only usbstack.wLen -= (WORD)xferLen; // update remainding wLen value cmd ^= 0x40; // toggle DATA0/DATA1 dataX++; // point to next dataX //------------------------------------------------ // If host requested for more data than the slave // have, and if the slave's data len is a multiple // of its endpoint payload size/last xferLen. Do // not overwrite data in previous buffer. //------------------------------------------------ if(remainder==xferLen) // empty data detected bufLen = 0; // do not overwriten previous data else // reset bufLen to zero bufLen = xferLen; // update previous buffer length //------------------------------------------------ // Arm for next data transfer when requested data // length have not reach zero, i.e. wLen!=0, and // last xferlen of data was completed, i.e. // remainder is equal to zero, not a short pkt //------------------------------------------------ if(!remainder && usbstack.wLen) // remainder==0 when last xferLen { // was all completed or wLen!=0 addr = (dataX & 1) ? data1:data0; // select next address for data xferLen = (u_int8)(usbstack.wLen>=usbstack.wPayload) ? usbstack.wPayload:usbstack.wLen; // get data length required //if (FULL_SPEED) // sync with SOF transfer cmd |= 0x20; // always sync SOF when FS, regardless SL811Write(EP0XferLen, xferLen); // select next xfer length SL811Write(EP0Address, addr); // data buffer addr SL811Write(IntStatus,INT_CLEAR); // is a LS is on Hub. SL811Write(EP0Control,cmd); // Enable USB transfer and re-arm } //------------------------------------------------ // Copy last IN token data pkt from prev transfer // Check if there was data available during the // last data transfer //------------------------------------------------ if(bufLen) { SL811BufRead(((dataX&1)?data0:data1), usbstack.buffer, bufLen); usbstack.buffer += bufLen; } //------------------------------------------------ // Terminate on short packets, i.e. remainder!=0 // a short packet or empty data packet OR when // requested data len have completed, i.e.wLen=0 // For a LOWSPEED device, the 1st device descp, // wPayload is default to 64-u_int8, LS device will // only send back a max of 8-u_int8 device descp, // and host detect this as a short packet, and // terminate with OUT status stage //------------------------------------------------ if(remainder || !usbstack.wLen) break; }// PID IN } //-------------------------NAK---------------------------- if (result & EP0_NAK) // NAK Detected { if(usbstack.endpoint==0) // on ep0 during enumeration of LS device { // happen when slave is not fast enough, SL811Write(IntStatus,INT_CLEAR); // clear interrupt status, need to SL811Write(EP0Control,cmd); // re-arm and request for last cmd, IN token result = 0; // respond to NAK status only } else // normal data endpoint, exit now !!! , non-zero ep break; // main loop control the interval polling } //-----------------------TIMEOUT-------------------------- if (result & EP0_TIMEOUT) // TIMEOUT Detected { if(usbstack.endpoint==0) // happens when hub enumeration { if(++timeout >= TIMEOUT_RETRY) { timeout--; break; // exit on the timeout detected } SL811Write(IntStatus,INT_CLEAR); // clear interrupt status, need to SL811Write(EP0Control,cmd); // re-arm and request for last cmd again } else { // all other data endpoint, data transfer bXXGFlags.TIMEOUT_ERR = TRUE; // failed, set flag to terminate transfer break; // happens when data transfer on a device } // through the hub } //-----------------------STALL---------------------------- if (result & EP0_STALL) // STALL detected return TRUE; // for unsupported request. //----------------------OVEFLOW--------------------------- if (result & EP0_OVERFLOW) // OVERFLOW detected //result=result; break; //-----------------------ERROR---------------------------- if (result & EP0_ERROR) // ERROR detected //result=result; break; } // end of While(1) if (result & EP0_ACK) // on ACK transmission return TRUE; // return OK return FALSE; // fail transmission }