static void usb_cdc_ecm_rx(uint8_t ep, uint8_t stat) { int recv_len; // if no buffers available or we're already dropping the frame, // drop this one too if (recv_ring.size == 0 || recv_ring_drop) { LOG_DEBUG("Got usb packet - dropping"); recv_len = usbRead(ep, NULL, MAX_USB_PACKET_SIZE); recv_ring_drop = 1; } else { LOG_DEBUG("Got usb packet - recieving"); // recieve data for the current frame. Here we assume that // frames being recieved do not exceed length of supplied buffer // (otherwise we will enter into invalid state...) //TODO: This is security vurnelability for potentially sensitive product... volatile usb_buffer_t* buffer = &recv_ring.buffers[recv_ring.begin]; int len = MIN(buffer->length - buffer->current, MAX_USB_PACKET_SIZE); recv_len = usbRead(ep, buffer->data + buffer->current, len); buffer->current += recv_len; } // if short packet recieved - full frame is received if (recv_len < MAX_USB_PACKET_SIZE) { if (!recv_ring_drop) { usbring_free_buffer(&recv_ring); } recv_ring_drop = 0; } }
void usb_cdc_ecm_rx(uint8_t ep, uint8_t stat) { int len; int ip_length; int i; uint16_t eth_hdr_type; //printf("cdc ecm rx ep-state=%x state=%d transferred=%d",stat,rndisRxState,rndisRxTransferCount); switch (rndisRxState) { case RNDIS_RX_IDLE: rndisRxTransferCount = 0; rndisRxPacketSize = 0; /* it may increase later */ /* hopefully, it follows the header immediately... */ if (receive_packet == 0) { if (RING_IS_FULL(receive_ring_head, receive_ring_tail, RECEIVE_RING_SIZE)) { rndisRxState = RNDIS_RX_DROP; DBG("usb_eth_rx: receive ring is full, head=%d, tail=%d",receive_ring_head, receive_ring_tail); len = usbRead(ep, NULL, 64); } else { rndisRxState = RNDIS_RX_RECV; receive_packet = &receive_ring[receive_ring_tail]; packet_init(receive_packet); len = usbRead(ep, receive_packet->data , 64); rndisRxTransferCount += len; } } break; case RNDIS_RX_DROP: len = usbRead(ep, NULL, 64); //ETH_DEV_DBG_INFO("rndis rx read %d bytes more in drop state",len); if (len >= 0) rndisRxTransferCount += len; break; case RNDIS_RX_RECV: len = usbRead(ep, (receive_packet->data) + rndisRxTransferCount, 64 /* maybe we need to limit this */); if (len >= 0) rndisRxTransferCount += len; break; } if (len < 64) { /* a short packet terminates the Ethernet frame */ rndisRxPackets++; DBG("cdc ecm rx done with a packet (%d bytes)",rndisRxTransferCount); //for (i=0; i<rndisRxPacketSize; i++) DBG("%d %02x",i,(receive_packet->data)[i]); //if (rndisRxPacketSize==54) while(1); if (rndisRxState == RNDIS_RX_RECV) { receive_packet->size = rndisRxTransferCount; receive_packet = 0; RING_INC(receive_ring_tail, RECEIVE_RING_SIZE); } rndisRxState = RNDIS_RX_IDLE; } }
static void usbBuildTxBlock(void) { uchar wantLen, len, txLen, token; wantLen = usbMsgLen; if(wantLen > 8) wantLen = 8; usbMsgLen -= wantLen; token = USBPID_DATA1; if(usbMsgFlags & USB_FLG_TX_PACKET) token = USBPID_DATA0; usbMsgFlags++; len = usbRead(usbTxBuf + 1, wantLen); if(len <= 8){ /* valid data packet */ usbCrc16Append(usbTxBuf + 1, len); txLen = len + 4; /* length including sync byte */ if(len < 8) /* a partial package identifies end of message */ usbMsgLen = 0xff; }else{ token = USBPID_STALL; txLen = 2; /* length including sync byte */ usbMsgLen = 0xff; } usbTxBuf[0] = token; usbTxLen = txLen; #if DEBUG_LEVEL > 1 DBG2(0x20, usbTxBuf, txLen-1); #else DBG1(0x20, usbTxBuf + 1, 2); #endif }
void driver::catch_data() { int_least16_t temp_1 = 0, temp_2 = 0, temp_3 = 0; uint8_t * buffer = new uint8_t[CUSTOM_RQ_DATA_LEN]; int_least16_t counter = 0; while(counter < MEASURE_CYCLES) { usbRead(CUSTOM_RQ_DATA, 0, buffer, CUSTOM_RQ_DATA_LEN); if(buffer[6] != 0 or buffer[7] != 0) { temp_1 += buffer[0]; temp_1 += buffer[1] << 8; temp_2 += buffer[2]; temp_2 += buffer[3] << 8; temp_3 += buffer[4]; temp_3 += buffer[5] << 8; counter += buffer[6]; counter += buffer[7] << 8; } } if(counter != 0) { m_x = - temp_2/counter; m_y = temp_1/counter; m_z = temp_3/counter; } }
/** Read the USB device using #usbRead. */ void mouUSBRead(DevicePtr pDev, MOTIONPROC motion, ENQUEUEPROC enqueue, CHECKPROC checkspecial, BLOCK block) { usbRead(pDev, motion, enqueue, BTN_MISC, block); }
/** Read the USB device using #usbRead. */ void othUSBRead(DevicePtr pDev, MOTIONPROC motion, ENQUEUEPROC enqueue, CHECKPROC checkspecial, BLOCK block) { usbRead(pDev, motion, enqueue, 0xffff, block); }
static void vcomBulkOut(uint8_t EP, uint8_t EPStatus) { int i, n; if (fifoFree(&rxfifo) < MAX_PACKET_SIZE) return; /* may not fit, we drop the data */ n = usbRead(EP, _vcom_buffer, sizeof(_vcom_buffer)); for (i = 0; i < n; i++) fifoPut(&rxfifo, _vcom_buffer[i]); }
int PacketUsbCdc::getMoreBytes( ) { if( slipRxPacket.size( ) < 1 ) // if there's nothing left over from last time { int available = numberOfAvailableBytes( ); if( available < 0 ) return -1; if( available > 0 ) { slipRxPacket.resize( available ); int read = usbRead( slipRxPacket.data( ), slipRxPacket.size( ) ); if( read < 0 ) return -1; } } return PacketInterface::OK; }
int32_t usbReadByte(uint8_t *data) { return usbRead(data, 1); }
void PacketUsbCdc::run() { char justGot; OscUsbPacket* currentPacket = NULL; char* packetP = NULL; bool packetStarted = false; open( ); while( 1 ) { if( !usbIsOpen() ) open(); UsbStatus readResult = usbRead( &justGot, 1 ); //we're only ever going to read 1 character at a time //messageInterface->message( 1, "usb> Just read...usb open? %d, handle? %d\n", usbIsOpen(), deviceHandle ); if( readResult == GOT_CHAR ) //we got a character { switch( justGot ) { case END: if( packetStarted && currentPacket->length ) // it was the END byte { // Need to protect the packetList structure from multithreaded diddling { QMutexLocker locker( &packetListMutex ); *packetP = '\0'; packetList.append( currentPacket ); packetCount++; } packetReadyInterface->packetWaiting( ); packetStarted = false; } else // it was the START byte { currentPacket = new OscUsbPacket( ); packetP = currentPacket->packetBuf; packetStarted = true; } break; // if it's the same code as an ESC character, get another character, // then figure out what to store in the packet based on that. case ESC: readResult = usbRead( &justGot, 1 ); if( readResult == GOT_CHAR ) { switch( justGot ) { case ESC_END: justGot = END; break; case ESC_ESC: justGot = ESC; break; } } else break; // otherwise, just stick it in the packet default: if( packetP == NULL ) break; *packetP = justGot; packetP++; currentPacket->length++; } } else { if( usbIsOpen() ) this->sleepMs( 10 ); else this->sleepMs( 1000 ); } } // should never get here... close( ); }
void usb_eth_rx(uint8_t ep, uint8_t stat) { int len; int ip_length; uint16_t eth_hdr_type; if (receive_packet == 0) { if (RING_IS_FULL(receive_ring_head, receive_ring_tail, RECEIVE_RING_SIZE)) { ETH_DEV_DBG_ERROR("usb_eth_rx: receive ring is full, head=%d, tail=%d", receive_ring_head, receive_ring_tail); return; } receive_packet = &receive_ring[receive_ring_tail]; packet_init(receive_packet); } if (receive_packet->transferred < PACKET_SIZE) { len = usbRead(ep, &receive_packet->data[0] + receive_packet->transferred, PACKET_SIZE - receive_packet->transferred); } else { /* discard the bytes */ len = usbRead(ep, 0, 0); } receive_packet->transferred += len; ETH_DEV_DBG_INFO("usb_eth_rx: read %d bytes, head=%d, tail=%d", len, receive_ring_head, receive_ring_tail); if (receive_packet->transferred < UIP_LLH_LEN) { /* ethernet header was not received yet got some garbage which we need to ignore */ ETH_DEV_DBG_ERROR("got part of packet with %d bytes", receive_packet->transferred); receive_packet->transferred = 0; return; } if (receive_packet->size == 0) { eth_hdr_type = (receive_packet->data[0xc] << 8) | receive_packet->data[0xd]; if (eth_hdr_type == UIP_ETHTYPE_IP) { ip_length = receive_packet->data[0x10]*0xff + receive_packet->data[0x11]; receive_packet->size = ip_length + UIP_LLH_LEN; } else if (eth_hdr_type == UIP_ETHTYPE_ARP) { receive_packet->size = receive_packet->transferred; } else { ETH_DEV_DBG_ERROR("Unknown ethernet frame 0x%x", eth_hdr_type); receive_packet->size = receive_packet->transferred; } } if (receive_packet->transferred >= receive_packet->size) { if (receive_packet->transferred >= receive_packet->size) { if (receive_packet->transferred >= PACKET_SIZE) { ETH_DEV_DBG_ERROR("Discarding packet of size %d", receive_packet->transferred); packet_init(receive_packet); receive_packet = 0; } else { ETH_DEV_DBG_INFO("Received packet of size %d", receive_packet->transferred); receive_packet = 0; RING_INC(receive_ring_tail, RECEIVE_RING_SIZE); } } } }
void usb_rndis_rx(uint8_t ep, uint8_t stat) { int len; int ip_length; int i; uint16_t eth_hdr_type; if (!(stat & EP_STATUS_DATA)) { ETH_DEV_DBG_ERROR("rndis rx, no data..."); return; } //DBG("rndis rx ep-state=%x state=%d packetsize=%d transferred=%d",stat,rndisRxState,rndisRxPacketSize,rndisRxTransferCount); switch (rndisRxState) { case RNDIS_RX_IDLE: //if (rndisRxState == RNDIS_RX_IDLE) { len = usbRead(ep, rndisPacketHeader, 64); if (len==1 && rndisPacketHeader[0] == 0) { DBG("single byte USB packet (this is normal for rndis"); return; } if (len < 44) { ETH_DEV_DBG_ERROR("Whoops, USB packet length shorter than RNDIS message header (only %d bytes)",len); return; } uint32_t type = uint32FromLittleEndian(rndisPacketHeader); uint32_t rndislen = uint32FromLittleEndian(rndisPacketHeader+4); uint32_t dataoff = uint32FromLittleEndian(rndisPacketHeader+8); uint32_t datalen = uint32FromLittleEndian(rndisPacketHeader+12); uint32_t ooboff = uint32FromLittleEndian(rndisPacketHeader+16); uint32_t ooblen = uint32FromLittleEndian(rndisPacketHeader+20); uint32_t numoob = uint32FromLittleEndian(rndisPacketHeader+24); uint32_t perpacket_infooff = uint32FromLittleEndian(rndisPacketHeader+28); uint32_t perpacket_infolen = uint32FromLittleEndian(rndisPacketHeader+32); //DBG("rndis rx type=%x len=%d dataoff=%d datalen=%d ooblen=%d numoob=%d perpacket_infooff=%d perpacket_infolen=%d", // type,len,dataoff,datalen,ooblen,numoob,perpacket_infooff,perpacket_infolen); if (type != 1) while(1); rndisRxTransferCount = 0; rndisRxPacketSize = datalen; /* hopefully, it follows the header immediately... */ if (receive_packet == 0) { if (RING_IS_FULL(receive_ring_head, receive_ring_tail, RECEIVE_RING_SIZE)) { DBG("usb_eth_rx: receive ring is full, head=%d, tail=%d",receive_ring_head, receive_ring_tail); rndisRxState = RNDIS_RX_DROP; for (i=0; i<(len-44); i++) { rndisRxTransferCount++; } } else { rndisRxState = RNDIS_RX_RECV; receive_packet = &receive_ring[receive_ring_tail]; packet_init(receive_packet); for (i=0; i<(len-44); i++) { (receive_packet->data)[ rndisRxTransferCount ] = rndisPacketHeader[44+i]; rndisRxTransferCount++; } } } break; case RNDIS_RX_DROP: //if (rndisRxState == RNDIS_RX_DROP) { len = usbRead(ep, NULL, rndisRxPacketSize - rndisRxTransferCount); //ETH_DEV_DBG_INFO("rndis rx read %d bytes more in drop state",len); if (len >= 0) rndisRxTransferCount += len; break; // } case RNDIS_RX_RECV: //if (rndisRxState == RNDIS_RX_RECV) { //DBG("rndis rx recv read from endpoint"); len = usbRead(ep, (receive_packet->data) + rndisRxTransferCount, rndisRxPacketSize - rndisRxTransferCount); //DBG("rndis rx read %d bytes more in recv state",len); if (len >= 0) rndisRxTransferCount += len; break; } if (rndisRxTransferCount == rndisRxPacketSize) { rndisRxPackets++; DBG("rndis rx done with a packet (%d bytes)",rndisRxPacketSize); //for (i=0; i<rndisRxPacketSize; i++) DBG("%d %02x",i,(receive_packet->data)[i]); //if (rndisRxPacketSize==54) while(1); if (rndisRxState == RNDIS_RX_RECV) { receive_packet->size = rndisRxPacketSize; receive_packet = 0; RING_INC(receive_ring_tail, RECEIVE_RING_SIZE); xxx = 1; } rndisRxState = RNDIS_RX_IDLE; } }