static int tmpf_write_use_mmap(chunk_s *chunk, info_s *info_ptr, off_t offset) { SALDL_ASSERT(chunk); SALDL_ASSERT(info_ptr); #ifdef HAVE_MMAP file_s *tmp_f = chunk->storage; SALDL_ASSERT(tmp_f); SALDL_ASSERT(tmp_f->file); int chunk_fd = fileno(tmp_f->file); SALDL_ASSERT(chunk_fd > 0); SALDL_ASSERT(chunk->size); void *tmp_buf = mmap(0, chunk->size, PROT_READ, MAP_SHARED, chunk_fd, 0); if (tmp_buf == MAP_FAILED) { warn_msg(FN, "mmap()ing chunk file %"SAL_ZU" failed, %s.", chunk->idx, strerror(errno)); warn_msg(FN, "Falling back to fread()/fwrite() ."); return -2; } saldl_fwrite_fflush(tmp_buf, 1, chunk->size, info_ptr->file, info_ptr->part_filename, offset); if (munmap(tmp_buf, chunk->size)) { warn_msg(FN, "munmap()ing chunk file %"SAL_ZU" failed.", chunk->idx); } return 0; #else (void) offset; return -1; #endif }
BamMerge::BamMerge(const vector<string>& bam_fnames, vector<int64_t> file_offsets) : _bam_fnames(bam_fnames), _lines(less_bam(true)), _last_id(0) { if (bam_fnames.size() <= 0) return; for (size_t i = 0; i < _bam_fnames.size(); ++i) { const char* fname = _bam_fnames[i].c_str(); samfile_t* fp = samopen(fname, "rb", 0); if (fp==0) { warn_msg(ERR_BAM_OPEN, fname); exit(1); } if (bam_fnames.size() == file_offsets.size() && file_offsets[i] > 0) bgzf_seek(fp->x.bam, file_offsets[i], SEEK_SET); bam1_t* b = bam_init1(); if (samread(fp, b) > 0) { _src_files.push_back(fp); CBamLine brec(_lines.size(), b, fp->header); _lines.push(brec); } else { bam_destroy1(b); } } if (_lines.size() == 0) { warn_msg("Warning: no input BAM records found.\n"); exit(1); } }
static void _handle_ack(struct cxl_afu_h *afu) { uint8_t data[sizeof(uint64_t)]; if (!afu) fatal_msg("NULL afu passed to libcxl.c:_handle_ack"); DPRINTF("MMIO ACK\n"); if ((afu->mmio.type == PSLSE_MMIO_READ64)| (afu->mmio.type == PSLSE_MMIO_EBREAD)) { if (get_bytes_silent(afu->fd, sizeof(uint64_t), data, 1000, 0) < 0) { warn_msg("Socket failure getting MMIO Ack"); _all_idle(afu); afu->mmio.data = 0xFEEDB00FFEEDB00FL; } else { memcpy(&(afu->mmio.data), data, sizeof(uint64_t)); afu->mmio.data = ntohll(afu->mmio.data); } } if (afu->mmio.type == PSLSE_MMIO_READ32) { if (get_bytes_silent(afu->fd, sizeof(uint32_t), data, 1000, 0) < 0) { warn_msg("Socket failure getting MMIO Read 32 data"); afu->mmio.data = 0xFEEDB00FL; _all_idle(afu); } else { memcpy(&(afu->mmio.data), data, sizeof(uint32_t)); debug_msg("KEM:0x%08x", afu->mmio.data); afu->mmio.data = ntohl(afu->mmio.data); debug_msg("KEM:0x%08x", afu->mmio.data); } } afu->mmio.state = LIBCXL_REQ_IDLE; }
bool BuildsMetaMakefileGenerator::init() { if(init_flag) return false; init_flag = true; const ProStringList &builds = project->values("BUILDS"); bool use_single_build = builds.isEmpty(); if(builds.count() > 1 && Option::output.fileName() == "-") { use_single_build = true; warn_msg(WarnLogic, "Cannot direct to stdout when using multiple BUILDS."); } else if(0 && !use_single_build && project->first("TEMPLATE") == "subdirs") { use_single_build = true; warn_msg(WarnLogic, "Cannot specify multiple builds with TEMPLATE subdirs."); } if(!use_single_build) { for(int i = 0; i < builds.count(); i++) { ProString build = builds[i]; MakefileGenerator *makefile = processBuild(build); if(!makefile) return false; if(!makefile->supportsMetaBuild()) { warn_msg(WarnLogic, "QMAKESPEC does not support multiple BUILDS."); clearBuilds(); use_single_build = true; break; } else { Build *b = new Build; b->name = name; if(builds.count() != 1) b->build = build.toQString(); b->makefile = makefile; makefiles += b; } } } if(use_single_build) { Build *build = new Build; build->name = name; build->makefile = createMakefileGenerator(project, false); if (build->makefile){ makefiles += build; }else { delete build; return false; } } return true; }
void QMakeSourceFileInfo::addSourceFile(const QString &f, uchar seek, QMakeSourceFileInfo::SourceFileType type) { if(!files) files = new SourceFiles; QMakeLocalFileName fn(f); SourceFile *file = files->lookupFile(fn); if(!file) { file = new SourceFile; file->file = fn; files->addFile(file); } else { if(file->type != type && file->type != TYPE_UNKNOWN && type != TYPE_UNKNOWN) warn_msg(WarnLogic, "%s is marked as %d, then %d!", f.toLatin1().constData(), file->type, type); } if(type != TYPE_UNKNOWN) file->type = type; if(seek & SEEK_MOCS && !file->moc_checked) findMocs(file); if(seek & SEEK_DEPS && !file->dep_checked) findDeps(file); }
bool QMakeMetaInfo::readLib(QString lib) { clear(); QString meta_file = findLib(lib); if(cache_vars.contains(meta_file)) { vars = cache_vars[meta_file]; return true; } bool ret = false; if(!meta_file.isNull()) { if(meta_file.endsWith(Option::pkgcfg_ext)) { if((ret=readPkgCfgFile(meta_file))) meta_type = "pkgcfg"; } else if(meta_file.endsWith(Option::libtool_ext)) { if((ret=readLibtoolFile(meta_file))) meta_type = "libtool"; } else if(meta_file.endsWith(Option::prl_ext)) { QMakeProject proj; if(!proj.read(Option::fixPathToLocalOS(meta_file), QMakeProject::ReadProFile)) return false; meta_type = "qmake"; vars = proj.variables(); ret = true; } else { warn_msg(WarnLogic, "QMakeMetaInfo: unknown file format for %s", meta_file.toLatin1().constData()); } } if(ret) cache_vars.insert(meta_file, vars); return ret; }
static void _mmio_read(struct cxl_afu_h *afu) { uint8_t *buffer; uint32_t addr; int size, offset; if (!afu) fatal_msg("NULL afu passed to libcxl.c:_mmio_read"); size = 1 + sizeof(addr); buffer = (uint8_t *) malloc(size); buffer[0] = afu->mmio.type; offset = 1; addr = htonl(afu->mmio.addr); memcpy((char *)&(buffer[offset]), (char *)&addr, sizeof(addr)); if (put_bytes_silent(afu->fd, size, buffer) != size) { warn_msg("_mmio_read: put_bytes_silent failed"); free(buffer); close_socket(&(afu->fd)); afu->opened = 0; afu->attached = 0; afu->mmio.state = LIBCXL_REQ_IDLE; afu->mmio.data = 0xFEEDB00FFEEDB00FL; return; } free(buffer); afu->mmio.state = LIBCXL_REQ_PENDING; }
void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer ) { // Make sure buffer isn't full if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) ) { warn_msg("I2C_BufferPush failed, buffer full: "); printHex( byte ); print( NL ); return; } // Check for wrap-around case if ( buffer->tail + 1 >= buffer->size ) { buffer->tail = 0; } // Normal case else { buffer->tail++; } // Add byte to buffer buffer->buffer[ buffer->tail ] = byte; }
// Attach to AFU static void _attach(struct psl *psl, struct client *client) { uint64_t wed; uint8_t ack; uint8_t buffer[MAX_LINE_CHARS]; size_t size; // FIXME: This only works for dedicate mode // Get wed value from application ack = PSLSE_DETACH; size = sizeof(uint64_t); if (get_bytes_silent(client->fd, size, buffer, psl->timeout, &(client->abort)) < 0) { warn_msg("Failed to get WED value from client"); client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); goto attach_done; } memcpy((char *)&wed, (char *)buffer, sizeof(uint64_t)); wed = ntohll(wed); // Send start to AFU if (add_job(psl->job, PSL_JOB_START, wed) != NULL) { psl->idle_cycles = PSL_IDLE_CYCLES; ack = PSLSE_ATTACH; } attach_done: if (put_bytes(client->fd, 1, &ack, psl->dbg_fp, psl->dbg_id, client->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); } }
// Check address alignment static int _aligned(addr, size) { // Check valid size if ((size == 0) || (size & (size - 1))) { warn_msg("AFU issued command with invalid size %d", size); return 0; } // Check aligned address if (addr & (size - 1)) { warn_msg("AFU issued command with unaligned address %016" PRIx64, addr); return 0; } return 1; }
void doimageresources(psd_file_t f){ long len = get4B(f); VERBOSE("\nImage resources (%ld bytes):\n", len); while(len > 0) len -= doirb(f); if(len != 0) warn_msg("image resources overran expected size by %d bytes\n", -len); }
// Update Time State trigger // Only valid with Sleep/Resume and Inactive/Active triggers // States: // * 0x00 - Off // * 0x01 - Activate // * 0x02 - On // * 0x03 - Deactivate void Macro_timeState( uint8_t type, uint16_t cur_time, uint8_t state ) { // Make sure this is a valid trigger type switch ( type ) { case TriggerType_Sleep1: case TriggerType_Resume1: case TriggerType_Inactive1: case TriggerType_Active1: break; // Ignore if not the correct type default: warn_msg("Invalid time state trigger update: "); printHex( type ); print(NL); return; } // cur_time is controlled by the caller // When this function called the trigger is active if ( cur_time > 0xFF ) { warn_msg("Only 255 time instances are accepted for a time state trigger: "); printInt16( cur_time ); print(NL); return; } uint8_t index = cur_time; // Only add to macro trigger list if one of three states switch ( state ) { case ScheduleType_A: // Activate case ScheduleType_On: // On case ScheduleType_D: // Deactivate macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index; macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state; macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type; macroTriggerEventBufferSize++; break; } }
static int signal_thread(void *arg) { int sig; pthread_sigmask(SIG_BLOCK, &sigset, NULL); while (1) { sigwait(&sigset, &sig); if (sig == SIGINT) { warn_msg("Ctrl-C received\n"); } else { warn_msg("Unknown signal. Still exiting\n"); } quitflag = 1; break; } return 0; }
/*! * @brief IO system shut down. * * When user wants to stop the codec system, this * function call is needed, to release the interrupt * signal, free the working buffer/code buffer/parameter * buffer, unmap the register into user space, and * close the codec device. * * @param none * * @return * @li 0 System shutting down success. * @li -1 System shutting down failure. */ int IOSystemShutdown(void) { /* Exit directly if already shutdown */ if (vpu_fd == -1) return 0; /* Make sure real shutdown is done when no instance needs to access vpu in the same process */ if (vpu_active_num > 1) { vpu_active_num--; return 0; } else if (!vpu_active_num) { warn_msg(" No instance is actived\n"); return 0; } if (!semaphore_wait(vpu_semap, API_MUTEX)) { err_msg("Unable to get mutex\n"); return -1; } /* * Do not call IOFreePhyMem/IOFreePhyPicParaMem/IOFreePhyUserDataMem * to free memory, let kernel do. */ #ifdef BUILD_FOR_ANDROID if (bit_work_addr.virt_uaddr != 0) { if (munmap((void *)bit_work_addr.virt_uaddr, bit_work_addr.size) != 0) err_msg("munmap failed\n"); } bit_work_addr.virt_uaddr = 0; #else IOFreeVirtMem(&bit_work_addr); #endif if (munmap((void *)vpu_reg_base, BIT_REG_MARGIN) != 0) err_msg("munmap failed\n"); vpu_active_num--; semaphore_post(vpu_semap, API_MUTEX); vpu_semaphore_close(vpu_shared_mem); if (vpu_fd >= 0) { close(vpu_fd); vpu_fd = -1; } return 0; }
// Update layer state // States: // * 0x00 - Off // * 0x01 - Activate // * 0x02 - On // * 0x03 - Deactivate void Macro_layerState( uint16_t layerIndex, uint8_t state ) { // Lookup done based on size of layerIndex uint8_t index = 0; TriggerType type = TriggerType_Layer1; // Only add to macro trigger list if one of three states // Mask around Shift/Latch/Lock state switch ( state & ScheduleType_D ) { case ScheduleType_A: // Activate case ScheduleType_On: // On case ScheduleType_D: // Deactivate // Check if layer is out of range if ( layerIndex > LayerNum_KLL ) { warn_msg("LayerIndex is out of range/not defined: "); printInt16( layerIndex ); print( NL ); return; } // Determine which type if ( layerIndex < 256 ) { index = layerIndex; } else if ( layerIndex < 512 ) { index = layerIndex - 256; type = TriggerType_Layer2; } else if ( layerIndex < 768 ) { index = layerIndex - 512; type = TriggerType_Layer3; } else if ( layerIndex < 1024 ) { index = layerIndex - 768; type = TriggerType_Layer4; } macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index; macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state; macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type; macroTriggerEventBufferSize++; break; } }
// Update animation state // States: // * 0x00 - Off // * 0x06 - Done // * 0x07 - Repeat void Macro_animationState( uint16_t animationIndex, uint8_t state ) { // Lookup done based on size of layerIndex uint8_t index = 0; TriggerType type = TriggerType_Animation1; // Only add to macro trigger list if one of three states switch ( state ) { case ScheduleType_Done: // Activate case ScheduleType_Repeat: // On // Check if animation index is out of range if ( animationIndex > AnimationNum_KLL ) { warn_msg("AnimationIndex is out of range/not defined: "); printInt16( animationIndex ); print( NL ); return; } // Determine which type if ( animationIndex < 256 ) { index = animationIndex; } else if ( animationIndex < 512 ) { index = animationIndex - 256; type = TriggerType_Animation2; } else if ( animationIndex < 768 ) { index = animationIndex - 512; type = TriggerType_Animation3; } else if ( animationIndex < 1024 ) { index = animationIndex - 768; type = TriggerType_Animation4; } macroTriggerEventBuffer[ macroTriggerEventBufferSize ].index = index; macroTriggerEventBuffer[ macroTriggerEventBufferSize ].state = state; macroTriggerEventBuffer[ macroTriggerEventBufferSize ].type = type; macroTriggerEventBufferSize++; break; } }
void Connect_addBytes( uint8_t *buffer, uint8_t count, uint8_t uart ) { // Too big to fit into buffer if ( count > UART_Buffer_Size ) { erro_msg("Too big of a command to fit into the buffer..."); return; } // Invalid UART if ( uart >= UART_Num_Interfaces ) { erro_print("Invalid UART to send from..."); return; } // Delay UART copy until there's some space left while ( uart_tx_buf[ uart ].items + count > UART_Buffer_Size ) { warn_msg("Too much data to send on UART"); printInt8( uart ); print( ", waiting..." NL ); delay_ms( 1 ); // FIXME Buffer will not drain here.... } // Append data to ring buffer for ( uint8_t c = 0; c < count; c++ ) { if ( Connect_debug ) { printHex( buffer[ c ] ); print(" +"); printInt8( uart ); print( NL ); } uart_tx_buf[ uart ].buffer[ uart_tx_buf[ uart ].tail++ ] = buffer[ c ]; uart_tx_buf[ uart ].items++; if ( uart_tx_buf[ uart ].tail >= UART_Buffer_Size ) uart_tx_buf[ uart ].tail = 0; if ( uart_tx_buf[ uart ].head == uart_tx_buf[ uart ].tail ) uart_tx_buf[ uart ].head++; if ( uart_tx_buf[ uart ].head >= UART_Buffer_Size ) uart_tx_buf[ uart ].head = 0; } }
static int _handle_afu_error(struct cxl_afu_h *afu) { uint64_t error; uint16_t size; uint8_t data[sizeof(error)]; int i; if (!afu) fatal_msg("NULL afu passed to libcxl.c:_handle_afu_error"); DPRINTF("AFU ERROR\n"); if (get_bytes_silent(afu->fd, sizeof(error), data, 1000, 0) < 0) { warn_msg("Socket failure getting AFU ERROR"); _all_idle(afu); return -1; } memcpy(&error, data, sizeof(error)); error = ntohll(error); // Only track a single AFU error at a time pthread_mutex_lock(&(afu->event_lock)); i = 0; while (afu->events[i] != NULL) { if (afu->events[i]->header.type == CXL_EVENT_AFU_ERROR) { pthread_mutex_unlock(&(afu->event_lock)); return 0; } ++i; } assert(i < EVENT_QUEUE_MAX); size = sizeof(struct cxl_event_header) + sizeof(struct cxl_event_afu_error); afu->events[i] = (struct cxl_event *)calloc(1, size); afu->events[i]->header.type = CXL_EVENT_AFU_ERROR; afu->events[i]->header.size = size; afu->events[i]->header.process_element = afu->context; afu->events[i]->afu_error.error = error; do { i = write(afu->pipe[1], &(afu->events[i]->header.type), 1); } while ((i == 0) || (errno == EINTR)); pthread_mutex_unlock(&(afu->event_lock)); return i; }
/* * Registers with rpcbind the program number with all versions, from low to * high, with the netid, all specified in 'rpc'. If registration fails, * returns -1, else 0. */ int register_rpc_service(const char *fmri, const rpc_info_t *rpc) { struct netconfig *nconf; int ver; debug_msg("Entering register_rpc_service: instance: %s", fmri); if ((nconf = getnetconfigent(rpc->netid)) == NULL) { /* * Check whether getnetconfigent() failed as a result of * having no IPv6 interfaces configured for a v6 netid, or * as a result of a 'real' error, and output an appropriate * message with an appropriate severity. */ if (is_v6_netid(rpc->netid) && !can_use_af(AF_INET6)) { warn_msg(gettext( "Couldn't register netid %s for RPC instance %s " "because no IPv6 interfaces are plumbed"), rpc->netid, fmri); } else { error_msg(gettext( "Failed to lookup netid '%s' for instance %s: %s"), rpc->netid, fmri, nc_sperror()); } return (-1); } for (ver = rpc->lowver; ver <= rpc->highver; ver++) { if (!rpcb_set(rpc->prognum, ver, nconf, &(rpc->netbuf))) { error_msg(gettext("Failed to register version %d " "of RPC service instance %s, netid %s"), ver, fmri, rpc->netid); for (ver--; ver >= rpc->lowver; ver--) (void) rpcb_unset(rpc->prognum, ver, nconf); freenetconfigent(nconf); return (-1); } } freenetconfigent(nconf); return (0); }
static int _handle_interrupt(struct cxl_afu_h *afu) { uint16_t size, irq; uint8_t data[sizeof(irq)]; int i; if (!afu) fatal_msg("NULL afu passed to libcxl.c:_handle_interrupt"); DPRINTF("AFU INTERRUPT\n"); if (get_bytes_silent(afu->fd, sizeof(irq), data, 1000, 0) < 0) { warn_msg("Socket failure getting IRQ"); _all_idle(afu); return -1; } memcpy(&irq, data, sizeof(irq)); irq = ntohs(irq); // Only track a single interrupt at a time pthread_mutex_lock(&(afu->event_lock)); i = 0; while (afu->events[i] != NULL) { if (afu->events[i]->header.type == CXL_EVENT_AFU_INTERRUPT) { pthread_mutex_unlock(&(afu->event_lock)); return 0; } ++i; } assert(i < EVENT_QUEUE_MAX); size = sizeof(struct cxl_event_header) + sizeof(struct cxl_event_afu_interrupt); afu->events[i] = (struct cxl_event *)calloc(1, size); afu->events[i]->header.type = CXL_EVENT_AFU_INTERRUPT; afu->events[i]->header.size = size; afu->events[i]->header.process_element = afu->context; afu->events[i]->irq.irq = irq; do { i = write(afu->pipe[1], &(afu->events[i]->header.type), 1); } while ((i == 0) || (errno == EINTR)); pthread_mutex_unlock(&(afu->event_lock)); return i; }
int tsthread_create(tsthread_ptr* const tptr, const struct usb_endpoint_st* const pusbep) { struct tsthread_param* ps; {//# const unsigned param_size = ROUNDUP(sizeof(struct tsthread_param), 0xF); const unsigned buffer_size = ROUNDUP(TS_BufSize ,0xF); const unsigned unitSize = ROUNDUP(pusbep->xfer_size ,0x1FF); const unsigned unitNum = TS_BufSize / unitSize; const unsigned actlen_size = sizeof(int) * unitNum; unsigned totalSize = param_size + actlen_size + buffer_size; void* ptr = uHeapAlloc( totalSize ); if(! ptr) { warn_msg(errno,"failed to allocate TS buffer"); return -1; } void* const buffer_ptr = ptr; ptr += buffer_size; ps = ptr; ps->buffer = buffer_ptr; ptr += param_size; ps->actual_length = ptr; //ptr += actlen_size; ps->buff_num = unitNum; ps->actual_length[0] = -1; //# the first block is pending if(pusbep->endpoint & 0x100) { //# Isochronous ps->buff_unitSize = pusbep->xfer_size; }else{ ps->buff_unitSize = unitSize; } } ps->pUSB = pusbep; ps->flags = 0; ps->buff_pop = 0; tsthread_start(ps); if(pthread_create((pthread_t *)&ps->thread_ptr, NULL, (void *)tsthread, ps) != 0) { warn_info(errno,"pthread_create failed"); uHeapFree(ps->buffer); return -1; } *tptr = ps; return 0; }
// Format and add interrupt to command list static void _add_interrupt(struct cmd *cmd, uint32_t handle, uint32_t tag, uint32_t command, uint32_t abort, uint16_t irq) { uint32_t resp = PSL_RESPONSE_DONE; enum cmd_type type = CMD_INTERRUPT; if (!irq || (irq > cmd->client[handle]->max_irqs)) { warn_msg("AFU issued interrupt with illegal source id"); resp = PSL_RESPONSE_FAILED; type = CMD_OTHER; goto int_done; } // Only track first interrupt until software reads event if (!cmd->irq) cmd->irq = irq; int_done: _add_cmd(cmd, handle, tag, command, abort, type, (uint64_t) irq, 0, MEM_IDLE, resp, 0); }
bool GBuildMakefileGenerator::openOutput(QFile &file, const QString &build) const { debug_msg(1, "file is %s", file.fileName().toLatin1().constData()); QFileInfo fi(file); if (fi.filePath().isEmpty()) file.setFileName(qmake_getpwd() + QDir::separator() + file.fileName()); if (!file.fileName().endsWith(projectSuffix())) { QString outputName(file.fileName()); outputName += QDir::separator(); outputName += fileInfo(project->projectFile()).baseName(); outputName += projectSuffix(); warn_msg(WarnParser, outputName.toAscii()); file.setFileName(outputName); } debug_msg(1, "file is %s", file.fileName().toLatin1().constData()); bool ret = MakefileGenerator::openOutput(file, QString()); return ret; }
/* * create_listen_sock */ int create_listen_sock (int port, int *sock_p) { int sock; struct sockaddr_in addr; int reuse; sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { err_msg(0, "Failed to create socket: %s", strerror(errno)); return 0; } reuse = 1; if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { warn_msg("Failed to set reuse addr option: %s", strerror(errno)); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { err_msg("Failed to bind to port %d: %s", port, strerror(errno)); close(sock); return 0; } if (listen(sock, 5)) { err_msg("Failed to listen on socket: %s", strerror(errno)); close(sock); return 0; } *sock_p = sock; return 1; }
void saldl_pthread_join_accept_einval(pthread_t thread, void **retval) { int ret; while (1) { ret = pthread_join(thread, retval); switch (ret) { case 0: return; case EINVAL: /* If download completed, and saldl was interrupted, it's * possible that exit_routine() would be called while joining * a thread. Re-joining that thread would fail with EINVAL. * So we consider this non-fatal. And only issue a warning. */ warn_msg(FN, "EINVAL returned, pthread already joined!"); return; default: fatal(FN, "Failed: %s.", strerror(ret)); } } }
// Initialize and start PSL thread // // The return value is encode int a 16-bit value divided into 4 for each // possible adapter. Then the 4 bits in each adapter represent the 4 possible // AFUs on an adapter. For example: afu0.0 is 0x8000 and afu3.0 is 0x0008. uint16_t psl_init(struct psl **head, struct parms *parms, char *id, char *host, int port, pthread_mutex_t * lock, FILE * dbg_fp) { struct psl *psl; struct job_event *reset; uint16_t location; location = 0x8000; if ((psl = (struct psl *)calloc(1, sizeof(struct psl))) == NULL) { perror("malloc"); error_msg("Unable to allocation memory for psl"); goto init_fail; } psl->timeout = parms->timeout; if ((strlen(id) != 6) || strncmp(id, "afu", 3) || (id[4] != '.')) { warn_msg("Invalid afu name: %s", id); goto init_fail; } if ((id[3] < '0') || (id[3] > '3')) { warn_msg("Invalid afu major: %c", id[3]); goto init_fail; } if ((id[5] < '0') || (id[5] > '3')) { warn_msg("Invalid afu minor: %c", id[5]); goto init_fail; } psl->dbg_fp = dbg_fp; psl->major = id[3] - '0'; psl->minor = id[5] - '0'; psl->dbg_id = psl->major << 4; psl->dbg_id |= psl->minor; location >>= (4 * psl->major); location >>= psl->minor; if ((psl->name = (char *)malloc(strlen(id) + 1)) == NULL) { perror("malloc"); error_msg("Unable to allocation memory for psl->name"); goto init_fail; } strcpy(psl->name, id); if ((psl->host = (char *)malloc(strlen(host) + 1)) == NULL) { perror("malloc"); error_msg("Unable to allocation memory for psl->host"); goto init_fail; } strcpy(psl->host, host); psl->port = port; psl->client = NULL; psl->idle_cycles = PSL_IDLE_CYCLES; psl->lock = lock; // Connect to AFU psl->afu_event = (struct AFU_EVENT *)malloc(sizeof(struct AFU_EVENT)); if (psl->afu_event == NULL) { perror("malloc"); goto init_fail; } info_msg("Attempting to connect AFU: %s @ %s:%d", psl->name, psl->host, psl->port); if (psl_init_afu_event(psl->afu_event, psl->host, psl->port) != PSL_SUCCESS) { warn_msg("Unable to connect AFU: %s @ %s:%d", psl->name, psl->host, psl->port); goto init_fail; } // DEBUG debug_afu_connect(psl->dbg_fp, psl->dbg_id); // Initialize job handler if ((psl->job = job_init(psl->afu_event, &(psl->state), psl->name, psl->dbg_fp, psl->dbg_id)) == NULL) { perror("job_init"); goto init_fail; } // Initialize mmio handler if ((psl->mmio = mmio_init(psl->afu_event, psl->timeout, psl->name, psl->dbg_fp, psl->dbg_id)) == NULL) { perror("mmio_init"); goto init_fail; } // Initialize cmd handler if ((psl->cmd = cmd_init(psl->afu_event, parms, psl->mmio, &(psl->state), psl->name, psl->dbg_fp, psl->dbg_id)) == NULL) { perror("cmd_init"); goto init_fail; } // Set credits for AFU if (psl_aux1_change(psl->afu_event, psl->cmd->credits) != PSL_SUCCESS) { warn_msg("Unable to set credits"); goto init_fail; } // Start psl loop thread if (pthread_create(&(psl->thread), NULL, _psl_loop, psl)) { perror("pthread_create"); goto init_fail; } // Add psl to list while ((*head != NULL) && ((*head)->major < psl->major)) { head = &((*head)->_next); } while ((*head != NULL) && ((*head)->major == psl->major) && ((*head)->minor < psl->minor)) { head = &((*head)->_next); } psl->_next = *head; if (psl->_next != NULL) psl->_next->_prev = psl; *head = psl; // Send reset to AFU reset = add_job(psl->job, PSL_JOB_RESET, 0L); while (psl->job->job == reset) { /*infinite loop */ lock_delay(psl->lock); } // Read AFU descriptor psl->state = PSLSE_DESC; read_descriptor(psl->mmio, psl->lock); // Finish PSL configuration psl->state = PSLSE_IDLE; if (dedicated_mode_support(psl->mmio)) { // AFU supports Dedicated Mode psl->max_clients = 1; } if (directed_mode_support(psl->mmio)) { // AFU supports Directed Mode psl->max_clients = psl->mmio->desc.num_of_processes; } if (psl->max_clients == 0) { error_msg("AFU programming model is invalid"); goto init_fail; } psl->client = (struct client **)calloc(psl->max_clients, sizeof(struct client *)); psl->cmd->client = psl->client; psl->cmd->max_clients = psl->max_clients; return location; init_fail: if (psl) { if (psl->afu_event) { psl_close_afu_event(psl->afu_event); free(psl->afu_event); } if (psl->host) free(psl->host); if (psl->name) free(psl->name); free(psl); } pthread_mutex_unlock(lock); return 0; }
// PSL thread loop static void *_psl_loop(void *ptr) { struct psl *psl = (struct psl *)ptr; struct cmd_event *event, *temp; int events, i, stopped, reset; uint8_t ack = PSLSE_DETACH; stopped = 1; pthread_mutex_lock(psl->lock); while (psl->state != PSLSE_DONE) { // idle_cycles continues to generate clock cycles for some // time after the AFU has gone idle. Eventually clocks will // not be presented to an idle AFU to keep simulation // waveforms from getting huge with no activity cycles. if (psl->state != PSLSE_IDLE) { psl->idle_cycles = PSL_IDLE_CYCLES; if (stopped) info_msg("Clocking %s", psl->name); fflush(stdout); stopped = 0; } if (psl->idle_cycles) { // Clock AFU psl_signal_afu_model(psl->afu_event); // Check for events from AFU events = psl_get_afu_events(psl->afu_event); // Error on socket if (events < 0) { warn_msg("Lost connection with AFU"); break; } // Handle events from AFU if (events > 0) _handle_afu(psl); // Drive events to AFU send_job(psl->job); send_mmio(psl->mmio); if (psl->mmio->list == NULL) psl->idle_cycles--; } else { if (!stopped) info_msg("Stopping clocks to %s", psl->name); stopped = 1; lock_delay(psl->lock); } // Skip client section if AFU descriptor hasn't been read yet if (psl->client == NULL) { lock_delay(psl->lock); continue; } // Check for event from application reset = 0; for (i = 0; i < psl->max_clients; i++) { if (psl->client[i] == NULL) continue; if ((psl->client[i]->state == CLIENT_NONE) && (psl->client[i]->idle_cycles == 0)) { put_bytes(psl->client[i]->fd, 1, &ack, psl->dbg_fp, psl->dbg_id, psl->client[i]->context); _free(psl, psl->client[i]); psl->client[i] = NULL; reset = 1; continue; } if (psl->state == PSLSE_RESET) continue; _handle_client(psl, psl->client[i]); if (psl->client[i]->idle_cycles) { psl->client[i]->idle_cycles--; } if (client_cmd(psl->cmd, psl->client[i])) { psl->client[i]->idle_cycles = PSL_IDLE_CYCLES; } } // Send reset to AFU if (reset == 1) { psl->cmd->buffer_read = NULL; event = psl->cmd->list; while (event != NULL) { if (reset) { warn_msg ("Client dropped context before AFU completed"); reset = 0; } warn_msg("Dumping command tag=0x%02x", event->tag); if (event->data) { free(event->data); } if (event->parity) { free(event->parity); } temp = event; event = event->_next; free(temp); } psl->cmd->list = NULL; info_msg("Sending reset to AFU"); add_job(psl->job, PSL_JOB_RESET, 0L); } lock_delay(psl->lock); } // Disconnect clients for (i = 0; i < psl->max_clients; i++) { if ((psl->client != NULL) && (psl->client[i] != NULL)) { // FIXME: Send warning to clients first? info_msg("Disconnecting %s context %d", psl->name, psl->client[i]->context); close_socket(&(psl->client[i]->fd)); } } // DEBUG debug_afu_drop(psl->dbg_fp, psl->dbg_id); // Disconnect from simulator, free memory and shut down thread info_msg("Disconnecting %s @ %s:%d", psl->name, psl->host, psl->port); if (psl->client) free(psl->client); if (psl->_prev) psl->_prev->_next = psl->_next; if (psl->_next) psl->_next->_prev = psl->_prev; if (psl->cmd) { free(psl->cmd); } if (psl->job) { free(psl->job); } if (psl->mmio) { free(psl->mmio); } if (psl->host) free(psl->host); if (psl->afu_event) { psl_close_afu_event(psl->afu_event); free(psl->afu_event); } if (psl->name) free(psl->name); if (*(psl->head) == psl) *(psl->head) = psl->_next; pthread_mutex_unlock(psl->lock); free(psl); pthread_exit(NULL); }
uint8_t Connect_receive_ScanCode( uint8_t byte, uint16_t *pending_bytes, uint8_t uart_num ) { // Check the directionality if ( uart_num == UART_Master ) { erro_print("Invalid ScanCode direction..."); } // Master node, trigger scan codes if ( Connect_master ) switch ( (*pending_bytes)-- ) { // Byte count always starts at 0xFFFF case 0xFFFF: // Device Id Connect_receive_ScanCodeDeviceId = byte; break; case 0xFFFE: // Number of TriggerGuides in bytes (byte * 3) *pending_bytes = byte * sizeof( TriggerGuide ); Connect_receive_ScanCodeBufferPos = 0; break; default: // Set the specific TriggerGuide entry ((uint8_t*)&Connect_receive_ScanCodeBuffer)[ Connect_receive_ScanCodeBufferPos++ ] = byte; // Reset the BufferPos if higher than sizeof TriggerGuide // And send the TriggerGuide to the Macro Module if ( Connect_receive_ScanCodeBufferPos >= sizeof( TriggerGuide ) ) { Connect_receive_ScanCodeBufferPos = 0; // Adjust ScanCode offset if ( Connect_receive_ScanCodeDeviceId > 0 ) { // Check if this node is too large if ( Connect_receive_ScanCodeDeviceId >= InterconnectNodeMax ) { warn_msg("Not enough interconnect layout nodes configured: "); printHex( Connect_receive_ScanCodeDeviceId ); print( NL ); break; } // This variable is in generatedKeymaps.h extern uint8_t InterconnectOffsetList[]; Connect_receive_ScanCodeBuffer.scanCode = Connect_receive_ScanCodeBuffer.scanCode + InterconnectOffsetList[ Connect_receive_ScanCodeDeviceId - 1 ]; } // ScanCode receive debug if ( Connect_debug ) { dbug_msg(""); printHex( Connect_receive_ScanCodeBuffer.type ); print(" "); printHex( Connect_receive_ScanCodeBuffer.state ); print(" "); printHex( Connect_receive_ScanCodeBuffer.scanCode ); print( NL ); } // Send ScanCode to macro module Macro_interconnectAdd( &Connect_receive_ScanCodeBuffer ); } break; } // Propagate ScanCode packet // XXX It would be safer to buffer the scancodes first, before transmitting the packet -Jacob // The current method is the more efficient/aggressive, but could cause issues if there were errors during transmission else switch ( (*pending_bytes)-- ) { // Byte count always starts at 0xFFFF case 0xFFFF: // Device Id { Connect_receive_ScanCodeDeviceId = byte; // Lock the master Tx buffer uart_lockTx( UART_Master ); // Send header + Id byte uint8_t header[] = { 0x16, 0x01, ScanCode, byte }; Connect_addBytes( header, sizeof( header ), UART_Master ); break; } case 0xFFFE: // Number of TriggerGuides in bytes *pending_bytes = byte * sizeof( TriggerGuide ); Connect_receive_ScanCodeBufferPos = 0; // Pass through byte Connect_addBytes( &byte, 1, UART_Master ); break; default: // Pass through byte Connect_addBytes( &byte, 1, UART_Master ); // Unlock Tx Buffer after sending last byte if ( *pending_bytes == 0 ) uart_unlockTx( UART_Master ); break; } // Check whether the scan codes have finished sending return *pending_bytes == 0 ? 1 : 0; }
// Adds a single USB Code to the USB Output buffer // Argument #1: USB Code void Output_usbCodeSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args ) { #if enableKeyboard_define == 1 // Display capability name if ( stateType == 0xFF && state == 0xFF ) { print("Output_usbCodeSend(usbCode)"); return; } // Depending on which mode the keyboard is in the USB needs Press/Hold/Release events uint8_t keyPress = 0; // Default to key release // Only send press and release events if ( stateType == 0x00 && state == 0x02 ) // Hold state return; // If press, send bit (NKRO) or byte (6KRO) if ( stateType == 0x00 && state == 0x01 ) // Press state keyPress = 1; // Get the keycode from arguments uint8_t key = args[0]; // Depending on which mode the keyboard is in, USBKeys_Keys array is used differently // Boot mode - Maximum of 6 byte codes // NKRO mode - Each bit of the 26 byte corresponds to a key // Bits 0 - 45 (bytes 0 - 5) correspond to USB Codes 4 - 49 (Main) // Bits 48 - 161 (bytes 6 - 20) correspond to USB Codes 51 - 164 (Secondary) // Bits 168 - 213 (bytes 21 - 26) correspond to USB Codes 176 - 221 (Tertiary) // Bits 214 - 216 unused uint8_t bytePosition = 0; uint8_t byteShift = 0; switch ( USBKeys_Protocol ) { case 0: // Boot Mode // Set the modifier bit if this key is a modifier if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier) { if ( keyPress ) { USBKeys_primary.modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0 } else // Release { USBKeys_primary.modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0 } USBKeys_primary.changed |= USBKeyChangeState_Modifiers; } // Normal USB Code else { // Determine if key was set uint8_t keyFound = 0; uint8_t old_sent = USBKeys_Sent; for ( uint8_t curkey = 0, newkey = 0; curkey < old_sent; curkey++, newkey++ ) { // On press, key already present, don't re-add if ( keyPress && USBKeys_primary.keys[newkey] == key ) { keyFound = 1; break; } // On release, remove if found if ( !keyPress && USBKeys_primary.keys[newkey] == key ) { // Shift next key onto this one // (Doesn't matter if it overflows, buffer is large enough, and size is used) USBKeys_primary.keys[newkey--] = USBKeys_primary.keys[++curkey]; USBKeys_Sent--; keyFound = 1; USBKeys_primary.changed = USBKeyChangeState_MainKeys; break; } } // USB Key limit reached if ( USBKeys_Sent >= USB_BOOT_MAX_KEYS ) { warn_print("USB Key limit reached"); break; } // Add key if not already found in the buffer if ( keyPress && !keyFound ) { USBKeys_primary.keys[USBKeys_Sent++] = key; USBKeys_primary.changed = USBKeyChangeState_MainKeys; } } break; case 1: // NKRO Mode // Set the modifier bit if this key is a modifier if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier) { if ( keyPress ) { USBKeys_primary.modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0 } else // Release { USBKeys_primary.modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0 } USBKeys_primary.changed |= USBKeyChangeState_Modifiers; break; } // First 6 bytes else if ( key >= 4 && key <= 49 ) { // Lookup (otherwise division or multiple checks are needed to do alignment) // Starting at 0th position, each byte has 8 bits, starting at 4th bit uint8_t keyPos = key + (0 * 8 - 4); // Starting position in array, Ignoring 4 keys switch ( keyPos ) { byteLookup( 0 ); byteLookup( 1 ); byteLookup( 2 ); byteLookup( 3 ); byteLookup( 4 ); byteLookup( 5 ); } USBKeys_primary.changed |= USBKeyChangeState_MainKeys; } // Next 14 bytes else if ( key >= 51 && key <= 155 ) { // Lookup (otherwise division or multiple checks are needed to do alignment) // Starting at 6th byte position, each byte has 8 bits, starting at 51st bit uint8_t keyPos = key + (6 * 8 - 51); // Starting position in array switch ( keyPos ) { byteLookup( 6 ); byteLookup( 7 ); byteLookup( 8 ); byteLookup( 9 ); byteLookup( 10 ); byteLookup( 11 ); byteLookup( 12 ); byteLookup( 13 ); byteLookup( 14 ); byteLookup( 15 ); byteLookup( 16 ); byteLookup( 17 ); byteLookup( 18 ); byteLookup( 19 ); } USBKeys_primary.changed |= USBKeyChangeState_SecondaryKeys; } // Next byte else if ( key >= 157 && key <= 164 ) { // Lookup (otherwise division or multiple checks are needed to do alignment) uint8_t keyPos = key + (20 * 8 - 157); // Starting position in array, Ignoring 6 keys switch ( keyPos ) { byteLookup( 20 ); } USBKeys_primary.changed |= USBKeyChangeState_TertiaryKeys; } // Last 6 bytes else if ( key >= 176 && key <= 221 ) { // Lookup (otherwise division or multiple checks are needed to do alignment) uint8_t keyPos = key + (21 * 8 - 176); // Starting position in array switch ( keyPos ) { byteLookup( 21 ); byteLookup( 22 ); byteLookup( 23 ); byteLookup( 24 ); byteLookup( 25 ); byteLookup( 26 ); } USBKeys_primary.changed |= USBKeyChangeState_QuartiaryKeys; } // Received 0x00 // This is a special USB Code that internally indicates a "break" // It is used to send "nothing" in order to break up sequences of USB Codes else if ( key == 0x00 ) { USBKeys_primary.changed |= USBKeyChangeState_MainKeys; // Also flush out buffers just in case Output_flushBuffers(); break; } // Invalid key else { warn_msg("USB Code not within 4-49 (0x4-0x31), 51-155 (0x33-0x9B), 157-164 (0x9D-0xA4), 176-221 (0xB0-0xDD) or 224-231 (0xE0-0xE7) NKRO Mode: "); printHex( key ); print( NL ); break; } // Set/Unset if ( keyPress ) { USBKeys_primary.keys[bytePosition] |= (1 << byteShift); USBKeys_Sent--; } else // Release { USBKeys_primary.keys[bytePosition] &= ~(1 << byteShift); USBKeys_Sent++; } break; } #endif }
int parse_args(int argc, char *argv[], int i) { int status = 0, opt, val; input_arg[i].cmd.chromaInterleave = 1; if (cpu_is_mx6x()) input_arg[i].cmd.bs_mode = 1; do { opt = getopt(argc, argv, options); switch (opt) { case 'i': strncpy(input_arg[i].cmd.input, optarg, MAX_PATH); input_arg[i].cmd.src_scheme = PATH_FILE; break; case 'o': if (input_arg[i].cmd.dst_scheme == PATH_NET) { warn_msg("-o ignored because of -n\n"); break; } strncpy(input_arg[i].cmd.output, optarg, MAX_PATH); input_arg[i].cmd.dst_scheme = PATH_FILE; break; case 'x': val = atoi(optarg); if ((input_arg[i].mode == ENCODE) || (input_arg[i].mode == LOOPBACK)) input_arg[i].cmd.video_node_capture = val; else { if (val == 1) { input_arg[i].cmd.dst_scheme = PATH_IPU; info_msg("Display through IPU LIB\n"); if (cpu_is_mx6x()) warn_msg("IPU lib is OBSOLETE, please try other renderer\n"); #ifdef BUILD_FOR_ANDROID } else if (val == 2) { input_arg[i].cmd.dst_scheme = PATH_G2D; info_msg("Display through G2D\n"); #endif } else { input_arg[i].cmd.dst_scheme = PATH_V4L2; info_msg("Display through V4L2\n"); input_arg[i].cmd.video_node = val; } if (cpu_is_mx27() && (input_arg[i].cmd.dst_scheme == PATH_IPU)) { input_arg[i].cmd.dst_scheme = PATH_V4L2; warn_msg("ipu lib disp only support in ipuv3\n"); } } break; case 'n': if (input_arg[i].mode == ENCODE) { /* contains the ip address */ strncpy(input_arg[i].cmd.output, optarg, 64); input_arg[i].cmd.dst_scheme = PATH_NET; } else { warn_msg("-n option used only for encode\n"); } break; case 'p': input_arg[i].cmd.port = atoi(optarg); break; case 'r': input_arg[i].cmd.rot_angle = atoi(optarg); if (input_arg[i].cmd.rot_angle) input_arg[i].cmd.rot_en = 1; break; case 'u': input_arg[i].cmd.ext_rot_en = atoi(optarg); /* ipu/gpu rotation will override vpu rotation */ if (input_arg[i].cmd.ext_rot_en) input_arg[i].cmd.rot_en = 0; break; case 'f': input_arg[i].cmd.format = atoi(optarg); break; case 'c': input_arg[i].cmd.count = atoi(optarg); break; case 'v': input_arg[i].cmd.vdi_motion = optarg[0]; break; case 'w': input_arg[i].cmd.width = atoi(optarg); break; case 'h': input_arg[i].cmd.height = atoi(optarg); break; case 'j': input_arg[i].cmd.loff = atoi(optarg); break; case 'k': input_arg[i].cmd.toff = atoi(optarg); break; case 'g': input_arg[i].cmd.gop = atoi(optarg); break; case 's': if (cpu_is_mx6x()) input_arg[i].cmd.bs_mode = atoi(optarg); else input_arg[i].cmd.prescan = atoi(optarg); break; case 'b': input_arg[i].cmd.bitrate = atoi(optarg); break; case 'd': input_arg[i].cmd.deblock_en = atoi(optarg); break; case 'e': input_arg[i].cmd.dering_en = atoi(optarg); break; case 'm': input_arg[i].cmd.mirror = atoi(optarg); if (input_arg[i].cmd.mirror) input_arg[i].cmd.rot_en = 1; break; case 't': input_arg[i].cmd.chromaInterleave = atoi(optarg); break; case 'l': input_arg[i].cmd.mp4_h264Class = atoi(optarg); break; case 'a': input_arg[i].cmd.fps = atoi(optarg); break; case 'y': input_arg[i].cmd.mapType = atoi(optarg); break; case 'q': input_arg[i].cmd.quantParam = atoi(optarg); break; case -1: break; default: status = -1; break; } } while ((opt != -1) && (status == 0)); optind = 1; return status; }