LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size, const char *name, const char *mode) { LoadS ls; DBGMSG2("luaL_loadbufferx","start"); ls.s = buff; ls.size = size; return lua_load(L, getS, &ls, name, mode); }
// Junk the jig challenge. uchar usbFunctionWrite(uchar *data, uchar len) { static int bytes_out = 0; bytes_out += 8; if (bytes_out >= 64) { expire = 50; state = p5_challenged; DBGMSG2("Finished challenge."); return 0; } else { return USB_NO_MSG; } }
TCgReturnCode CgxDriverIsReadCanceled(TCgxDriverState *pState) { DBG_FUNC_NAME("CgxDriverIsReadCanceled") TCgReturnCode rc = ECgOk; // Check if snap was unconditionally canceled if (pState->transfer.cancel.request && (pState->transfer.bytes.received > pState->transfer.cancel.onByte)) { CGCoreReset(CGCORE_ENABLE_CORE); CgCpuDmaStop(CG_DRIVER_DMA_CHANNEL_READ); return ECgCanceled; } // Check if we have reached the cancel point if (pState->transfer.cancel.request) { U32 curDmaCount = 0; U32 requestedDmaCount = 0; rc = CgCpuDmaRequestedCount(CG_DRIVER_DMA_CHANNEL_READ, &requestedDmaCount); // how many bursts requested to receive DBGMSG1("requestedDmaCount = %d", requestedDmaCount); rc = CgCpuDmaCurCount(CG_DRIVER_DMA_CHANNEL_READ, &curDmaCount); // how many bursts left to receive DBGMSG1("curDmaCount = %d", curDmaCount); DBGMSG2("%d >= %d ? ==> canceled", pState->transfer.bytes.received + (requestedDmaCount - curDmaCount) , pState->transfer.cancel.onByte); if (pState->transfer.bytes.received + (requestedDmaCount - curDmaCount) >= pState->transfer.cancel.onByte) { CGCoreReset(CGCORE_ENABLE_CORE); CgCpuDmaStop(CG_DRIVER_DMA_CHANNEL_READ); return ECgCanceled; } } return ECgOk; }
TCgReturnCode CgxDriverPrepareRecieve( TCgxDriverState *pState, unsigned char *apBuf, U32 aBufPhys, U32 aBufSize, unsigned long aLength, unsigned long aBlockLength, TCgByteOrder aByteOrder) { DBG_FUNC_NAME("CgxDriverPrepareRecieve") TCgReturnCode rc = ECgOk; U32 blockIndex = 0; U32 chunksInBlock = 0; U32 chunkSize = 0; U32 length = aLength + CgxDriverSnapLengthExtraBytes; // Potential patch U32 numOfBlocksInBuffer = 0; memset(gChunksList,0,sizeof(TCgCpuDmaTask)*MAX_DMA_TRANSFER_TASKS); DBGMSG1("buffer v-address = 0x%x", apBuf); DBGMSG1("start address value = 0x%x", *((U32*)apBuf) ); DBGMSG1("dest address value = 0x%x", *((U32*)apBuf + 128) ); pState->transfer.blockSize = ((aBlockLength == 0) || (length < aBlockLength)) ? length : aBlockLength; /*bxd modify for not limted to 512k Bytes each block*/ chunkSize = pState->transfer.blockSize; //chunkSize = MIN(MAX_DMA_CHUNK_SIZE, pState->transfer.blockSize); chunksInBlock = pState->transfer.blockSize / chunkSize; DBGMSG3("Requested %d bytes, Block=(%d bytes,%d chunks)", length, pState->transfer.blockSize, chunksInBlock); DBGMSG1("Maximum DMA chunk size is set to %d bytes", chunkSize); pState->flags.overrun = FALSE; pState->transfer.cancel.request = FALSE; pState->transfer.cancel.onByte = 0; pState->transfer.cancel.onChunk = 0; pState->transfer.done = 0; pState->transfer.originalBuffer = apBuf; pState->transfer.bufferPhys = pState->buffer.physAddr; pState->transfer.byteOrder = aByteOrder; pState->transfer.bytes.required = length; pState->transfer.bytes.received = 0; pState->transfer.blocks.required = (length + pState->transfer.blockSize - 1) / pState->transfer.blockSize; #ifdef CGCORE_ACCESS_VIA_SPI block_num = pState->transfer.blocks.required; #endif DBGMSG1("%d blocks required", pState->transfer.blocks.required); pState->transfer.lastBlockSize = length - (pState->transfer.blocks.required - 1) * pState->transfer.blockSize; DBGMSG1("last block size: %d bytes", pState->transfer.lastBlockSize); pState->transfer.blocks.received = 0; pState->transfer.chunks.required = 0; pState->transfer.chunks.received = 0; pState->transfer.chunks.active = 0; // split snap to blocks ////////////////////////////////////////////////////////////////////////// if (pState->transfer.blocks.required * chunksInBlock > MAX_DMA_TRANSFER_TASKS) { DBGMSG2("ERROR! too many chunks! %d > %d", pState->transfer.blocks.required * chunksInBlock, MAX_DMA_TRANSFER_TASKS); return ECgErrMemory; } numOfBlocksInBuffer = aBufSize / pState->transfer.blockSize; DBGMSG2("aBufSize = %d, blockSize = %d", aBufSize, pState->transfer.blockSize ); for(blockIndex = 0; blockIndex < pState->transfer.blocks.required; blockIndex++) { // Init the snap's blocks' offsets pState->transfer.bufferOffset[blockIndex] = pState->transfer.blockSize * (blockIndex % numOfBlocksInBuffer); // roll-over to start of buffer, // if we reached end of buffer. //DBGMSG2("buffer[%d] = 0x%08X", blockIndex, pState->transfer.bufferPhys + pState->transfer.bufferOffset[blockIndex]) } // split blocks to chunks ////////////////////////////////////////////////////////////////////////// for(blockIndex = 0; blockIndex < pState->transfer.blocks.required; blockIndex++) { U32 chunkIndex = 0; U32 more, dest; more = (blockIndex == (pState->transfer.blocks.required-1)) ? pState->transfer.lastBlockSize : pState->transfer.blockSize; dest = pState->transfer.bufferPhys + pState->transfer.bufferOffset[blockIndex]; // DBGMSG4("Block %02d/%02d: % 6d bytes @ 0x%08X", blockIndex + 1, pState->transfer.blocks.required, for(chunkIndex = 0; chunkIndex < chunksInBlock-1; chunkIndex++) { U32 size = MIN(more, chunkSize); if ((more - size) < chunkSize) break; gChunksList[pState->transfer.chunks.required].address = dest; gChunksList[pState->transfer.chunks.required].length = size; more -= size; // DBGMSG5("chunk %02d/%02d in block: length %d bytes @ 0x%08X (%d more)", chunkIndex + 1, chunksInBlock, dest += size; pState->transfer.chunks.required++; } // last chunk gChunksList[pState->transfer.chunks.required].address = dest; gChunksList[pState->transfer.chunks.required].length = more; DBGMSG3("last chunk out of %02d in block: % 6d bytes @ 0x%08X", chunksInBlock, more, dest); pState->transfer.chunks.required++; } DBGMSG1("%d chunks required", pState->transfer.chunks.required); if (length > pState->transfer.blockSize * pState->transfer.blocks.required) { // there are leftovers U32 more = length - pState->transfer.blockSize * pState->transfer.blocks.required; U32 dest = pState->transfer.bufferPhys + pState->transfer.bufferOffset[pState->transfer.blocks.required]; DBGMSG4("Extra Block % 2d/% 2d: % 6d bytes @ 0x%08X", pState->transfer.blocks.required, pState->transfer.blocks.required, more, dest); for (; (more > 0) && (pState->transfer.chunks.required < MAX_DMA_TRANSFER_TASKS); pState->transfer.chunks.required++) { U32 size = MIN(more, chunkSize); gChunksList[pState->transfer.chunks.required].address = dest; gChunksList[pState->transfer.chunks.required].length = size; more -= size; dest += size; DBGMSG4("chunk % 4d: % 4d bytes @ 0x%08X (%d bytes left)", pState->transfer.chunks.required + 1, size, dest, more); } DBGMSG1("%d chunks required", pState->transfer.chunks.required); } // setup 1st DMA chunk #ifndef CGCORE_ACCESS_VIA_SPI rc = CgCpuDmaSetupFromGps(gChunksList, pState->transfer.chunks.required, CG_DRIVER_DMA_CHANNEL_READ, CGCORE_BASE_ADDR + 0x00000070); rc = CgCpuDmaStart(CG_DRIVER_DMA_CHANNEL_READ); #else // rc =CgxCpuSpiReadData(); #endif return rc; }
usbMsgLen_t usbFunctionSetup(uchar data[8]) { usbRequest_t *rq = (usbRequest_t *) data; uchar msg[] = {usbDeviceAddr, usbNewDeviceAddr}; if (port_cur == 6 && rq->bRequest == 0xAA) { /* holy crap, it worked! */ state = done; return 0; } if (port_cur == 5 && rq->bRequest == USBRQ_SET_INTERFACE) { /* can ignore this */ return 0; } if (port_cur == 0 && rq->bmRequestType == 0xA0 && rq->bRequest == 0x06 && rq->wValue.bytes[1] == 0x29) { usbMsgPtr = (void *) HUB_Hub_Descriptor_ram; return sizeof(HUB_Hub_Descriptor_ram); } if (port_cur == 0 && rq->bmRequestType == 0xA0 && rq->bRequest == 0x00 && // GET HUB STATUS rq->wValue.word == 0x00 && rq->wIndex.word == 0x00 && rq->wLength.word == 0x04) { DBGMSG2("Get hub status."); outBuffer_Write_Word(0x0000); outBuffer_Write_Word(0x0000); return sendOutBuffer(); } if (port_cur == 0 && rq->bmRequestType == 0xA3 && rq->bRequest == 0x00 && // GET PORT STATUS rq->wValue.word == 0x00 && rq->wLength.word == 0x04) { uint8_t p = rq->wIndex.word; if (p < 1 || p > 6) return 0; DBGX2("Get port status: ", &p, 1); outBuffer_Write_Word(port_status[p - 1]); outBuffer_Write_Word(port_change[p - 1]); return sendOutBuffer(); } if (port_cur == 0 && rq->bmRequestType == 0x23 && rq->bRequest == 0x03 && // SET_FEATURE rq->wLength.word == 0x00) { uint8_t p = rq->wIndex.word; if (p < 1 || p > 6) return 0; switch(rq->wValue.word) { case 0x0008: // PORT_POWER DBGX2("Set port power: ", &p, 1); if (p == 6 && state == init) { /* after the 6th port is powered, wait a bit and continue */ state = hub_ready; expire = 15; } break; case 0x0004: // PORT_RESET DBGX2("Set port reset: ", &p, 1); hub_int_response |= (1 << p); port_change[p - 1] |= C_PORT_RESET; break; } return 0; } if (port_cur == 0 && rq->bmRequestType == 0x23 && rq->bRequest == 0x01 && // CLEAR_FEATURE rq->wLength.word == 0x00) { uint8_t p = rq->wIndex.word; if (p < 1 || p > 6) return 0; switch(rq->wValue.word) { case 0x0010: // C_PORT_CONNECTION DBGX2("Clear C_PORT_CONN: ", &p, 1); port_change[p - 1] &= ~C_PORT_CONN; last_port_conn_clear = p; break; case 0x0014: // C_PORT_RESET DBGX2("Clear C_PORT_RESET: ", &p, 1); port_change[p - 1] &= ~C_PORT_RESET; last_port_reset_clear = p; break; } return 0; } DBGX2("Unknown setup request:", rq, sizeof(usbRequest_t)); panic(RED, GREEN); return 0; }
usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) { const uint8_t DescriptorType = rq->wValue.bytes[1]; const uint8_t DescriptorNumber = rq->wValue.bytes[0]; const uint16_t wLength = rq->wLength.word; usbMsgLen_t Size = 0; switch (DescriptorType) { case USBDESCR_DEVICE: switch (port_cur) { case 0: usbMsgPtr = (void *) HUB_Device_Descriptor; Size = sizeof(HUB_Device_Descriptor); break; case 1: usbMsgPtr = (void *) port1_device_descriptor; Size = sizeof(port1_device_descriptor); break; case 2: usbMsgPtr = (void *) port2_device_descriptor; Size = sizeof(port2_device_descriptor); break; case 3: usbMsgPtr = (void *) port3_device_descriptor; Size = sizeof(port3_device_descriptor); break; case 4: usbMsgPtr = (void *) port4_device_descriptor; Size = sizeof(port4_device_descriptor); break; case 5: usbMsgPtr = (void *) port5_device_descriptor; Size = sizeof(port5_device_descriptor); break; case 6: usbMsgPtr = (void *) port6_device_descriptor; Size = sizeof(port6_device_descriptor); break; } break; case USBDESCR_CONFIG: switch (port_cur) { case 0: usbMsgPtr = (void *) HUB_Config_Descriptor; Size = sizeof(HUB_Config_Descriptor); break; case 1: // 4 configurations are the same. // For the initial 8-byte request, we give a different // length response than in the full request. if (DescriptorNumber < 4) { if (wLength == 8) { usbMsgPtr = (void *) port1_short_config_descriptor; Size = sizeof(port1_short_config_descriptor); } else { usbMsgPtr = (void *) port1_config_descriptor; Size = sizeof(port1_config_descriptor); } if (DescriptorNumber == 3 && wLength > 8) { state = p1_ready; expire = 20; } } break; case 2: // only 1 config usbMsgPtr = (void *) port2_config_descriptor; Size = sizeof(port2_config_descriptor); state = p2_ready; expire = 15; break; case 3: // 2 configurations are the same usbMsgPtr = (void *) port3_config_descriptor; Size = sizeof(port3_config_descriptor); if (DescriptorNumber == 1 && wLength > 8) { state = p3_ready; expire = 20; } break; case 4: // 3 configurations if (DescriptorNumber == 0) { usbMsgPtr = (void *) port4_config_descriptor_1; Size = sizeof(port4_config_descriptor_1); } else if (DescriptorNumber == 1) { if (wLength == 8) { usbMsgPtr = (void *) port4_short_config_descriptor_2; Size = sizeof(port4_short_config_descriptor_2); } else { usbMsgPtr = (void *) port4_config_descriptor_2; Size = sizeof(port4_config_descriptor_2); } } else if (DescriptorNumber == 2) { usbMsgPtr = (void *) port4_config_descriptor_3; Size = sizeof(port4_config_descriptor_3); if (wLength > 8) { state = p4_ready; expire = 20; // longer seems to help this one? } } break; case 5: // 1 config usbMsgPtr = (void *) port5_config_descriptor; Size = sizeof(port5_config_descriptor); break; case 6: // 1 config usbMsgPtr = (void *) port6_config_descriptor; Size = sizeof(port6_config_descriptor); break; } break; } DBGMSG2("getDescriptorType and number"); DBG2(0x01, &DescriptorType, 1); DBG2(0x01, &DescriptorNumber, 1); return Size; }