// Regular thread void CWII_IPC_HLE_Device_hid::checkUsbUpdates(CWII_IPC_HLE_Device_hid* hid) { timeval tv; tv.tv_sec = 0; tv.tv_usec = 500; while (hid->usb_thread_running) { static u16 timeToFill = 0; if (timeToFill == 0) { std::lock_guard<std::mutex> lk(hid->s_device_list_reply); if (hid->deviceCommandAddress != 0){ hid->FillOutDevices(Memory::Read_U32(hid->deviceCommandAddress + 0x18), Memory::Read_U32(hid->deviceCommandAddress + 0x1C)); Memory::Write_U32(8, hid->deviceCommandAddress); // IOS seems to write back the command that was responded to Memory::Write_U32(/*COMMAND_IOCTL*/ 6, hid->deviceCommandAddress + 8); // Return value Memory::Write_U32(0, hid->deviceCommandAddress + 4); WII_IPC_HLE_Interface::EnqueReplyCallback(hid->deviceCommandAddress); hid->deviceCommandAddress = 0; } } timeToFill+=8; libusb_handle_events_timeout(NULL, &tv); } return; }
static int receive_data(int fd, int revents, void *cb_data) { struct timeval tv; struct dev_context *devc; struct drv_context *drvc; const struct sr_dev_inst *sdi; struct sr_dev_driver *di; (void)fd; (void)revents; sdi = cb_data; di = sdi->driver; drvc = di->priv; devc = sdi->priv; tv.tv_sec = tv.tv_usec = 0; libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); if (devc->sent_samples == -2) { logic16_abort_acquisition(sdi); abort_acquisition(devc); } return TRUE; }
static int gp_libusb1_check_int (GPPort *port, char *bytes, int size, int timeout) { int ret; struct timeval tv; C_PARAMS (port && port->pl->dh && timeout >= 0); if (port->pl->nrofirqs) goto handleirq; tv.tv_sec = timeout/1000; tv.tv_usec = (timeout%1000)*1000; ret = LOG_ON_LIBUSB_E (libusb_handle_events_timeout(port->pl->ctx, &tv)); if (port->pl->nrofirqs) goto handleirq; if (ret < LIBUSB_SUCCESS) return translate_libusb_error(ret, GP_ERROR_IO_READ); return GP_ERROR_TIMEOUT; handleirq: if (size > port->pl->irqlens[0]) size = port->pl->irqlens[0]; memcpy(bytes, port->pl->irqs[0], size); memmove(port->pl->irqs,port->pl->irqs+1,sizeof(port->pl->irqs[0])*(port->pl->nrofirqs-1)); memmove(port->pl->irqlens,port->pl->irqlens+1,sizeof(port->pl->irqlens[0])*(port->pl->nrofirqs-1)); port->pl->nrofirqs--; return size; }
bool fusb_devhandle::_reap (bool ok_to_block_p) { int ret; struct timeval tv; // Save pending size int pnd_size = d_pending_rqsts.size(); if (ok_to_block_p) { tv.tv_sec = 2; tv.tv_usec = 0; } else { tv.tv_sec = 0; tv.tv_usec = 0; } if ((ret = libusb_handle_events_timeout(d_ctx, &tv)) < 0) { LOG(ERR) << "libusb_handle_events_timeout returned " << ret; return false; } // Check that a pending transfer was removed if (pnd_size > d_pending_rqsts.size()) return true; else { return false; } }
// Construct a command using the specific language of the USBMAC6000 // and send the message to the device. // I got the format for this message from the code in USBMAC6000.cpp from // the video project, as implemented by Ryan Schubert at UNC. bool vrpn_LUDL_USBMAC6000::send_usbmac_command(unsigned device, unsigned command, unsigned index, int value) { if (_device_handle == NULL) { return false; } // Let libusb handle any outstanding events struct timeval zerotime; zerotime.tv_sec = 0; zerotime.tv_usec = 0; libusb_handle_events_timeout(_context, &zerotime); char msg[1024]; sprintf(msg, "can %u %u %u %i\n", device, command, index, value); int len = strlen(msg); int sent_len = 0; msg[len-1] = 0xD; //printf("dbg: Starting bulk send command\n"); int ret = libusb_bulk_transfer(_device_handle, _endpoint | LIBUSB_ENDPOINT_OUT, static_cast<vrpn_uint8 *>(static_cast<void*>(msg)), len, &sent_len, 50); //printf("dbg: Finished bulk send command\n"); if ((ret != 0) || (sent_len != len)) { #ifdef libusb_strerror fprintf(stderr,"vrpn_LUDL_USBMAC6000::send_usbmac_command(): Could not send: %s\n", libusb_strerror(static_cast<libusb_error>(ret))); #else fprintf(stderr,"vrpn_LUDL_USBMAC6000::send_usbmac_command(): Could not send: code %d\n", ret); #endif return false; } return true; }
static int usb_process_ds(struct data_source *ds) { if (libusb_state != LIB_USB_TRANSFERS_ALLOCATED) return -1; // log_info("begin usb_process_ds"); // always handling an event as we're called when data is ready struct timeval tv; memset(&tv, 0, sizeof(struct timeval)); libusb_handle_events_timeout(NULL, &tv); // Handle any packet in the order that they were received while (handle_packet) { // log_info("handle packet %p, endpoint %x, status %x", handle_packet, handle_packet->endpoint, handle_packet->status); void * next = handle_packet->user_data; handle_completed_transfer(handle_packet); // handle case where libusb_close might be called by hci packet handler if (libusb_state != LIB_USB_TRANSFERS_ALLOCATED) return -1; // Move to next in the list of packets to handle if (next) { handle_packet = (struct libusb_transfer*)next; } else { handle_packet = NULL; } } // log_info("end usb_process_ds"); return 0; }
// Regular thread void CWII_IPC_HLE_Device_hid::checkUsbUpdates(CWII_IPC_HLE_Device_hid* hid) { timeval tv; tv.tv_sec = 0; tv.tv_usec = 500; while (hid->usb_thread_running) { static u16 timeToFill = 0; if (timeToFill == 0) { std::lock_guard<std::mutex> lk(hid->m_device_list_reply_mutex); if (hid->deviceCommandAddress != 0) { hid->FillOutDevices(Memory::Read_U32(hid->deviceCommandAddress + 0x18), Memory::Read_U32(hid->deviceCommandAddress + 0x1C)); // The original hardware overwrites the command type with the async reply type. Memory::Write_U32(IPC_REP_ASYNC, hid->deviceCommandAddress); // IOS also seems to write back the command that was responded to in the FD field. Memory::Write_U32(IPC_CMD_IOCTL, hid->deviceCommandAddress + 8); // Return value Memory::Write_U32(0, hid->deviceCommandAddress + 4); WII_IPC_HLE_Interface::EnqueueReply_Threadsafe(hid->deviceCommandAddress); hid->deviceCommandAddress = 0; } } timeToFill += 8; libusb_handle_events_timeout(nullptr, &tv); } return; }
int receive_data(GSource *source, gpointer data) { struct timeval tv; tv.tv_sec = tv.tv_usec = 0; libusb_handle_events_timeout(usb_context, &tv); return TRUE; }
int freespace_perform() { struct timeval tv = {0, 0}; int rc; scanDevices(); rc = libusb_handle_events_timeout(freespace_libusb_context, &tv); return libusb_to_freespace_error(rc); }
static void *read_thread(void *param) { hid_device *dev = param; unsigned char *buf; const size_t length = dev->input_ep_max_packet_size; /* Set up the transfer object. */ buf = malloc(length); dev->transfer = libusb_alloc_transfer(0); libusb_fill_interrupt_transfer(dev->transfer, dev->device_handle, dev->input_endpoint, buf, length, read_callback, dev, 5000/*timeout*/); /* Make the first submission. Further submissions are made from inside read_callback() */ libusb_submit_transfer(dev->transfer); // Notify the main thread that the read thread is up and running. pthread_barrier_wait(&dev->barrier); /* Handle all the events. */ while (!dev->shutdown_thread) { int res; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 100; //TODO: Fix this value. res = libusb_handle_events_timeout(NULL, &tv); if (res < 0) { /* There was an error. Break out of this loop. */ break; } } /* Cancel any transfer that may be pending. This call will fail if no transfers are pending, but that's OK. */ if (libusb_cancel_transfer(dev->transfer) == 0) { /* The transfer was cancelled, so wait for its completion. */ libusb_handle_events(NULL); } /* The dev->transfer->buffer and dev->transfer objects are cleaned up in hid_close(). They are not cleaned up here because this thread could end either due to a disconnect or due to a user call to hid_close(). In both cases the objects can be safely cleaned up after the call to pthread_join() (in hid_close()), but since hid_close() calls libusb_cancel_transfer(), on these objects, they can not be cleaned up here. */ return NULL; }
static int _close_async_interrupts(GPPort *port) { int i, haveone; struct timeval tv; C_PARAMS (port); if (port->pl->dh == NULL) return GP_OK; /* Catch up on pending ones */ tv.tv_sec = 0; tv.tv_usec = 1000; LOG_ON_LIBUSB_E (libusb_handle_events_timeout(port->pl->ctx, &tv)); /* Now cancel and free the async transfers */ for (i = 0; i < sizeof(port->pl->transfers)/sizeof(port->pl->transfers[0]); i++) { if (port->pl->transfers[i]) { GP_LOG_D("canceling transfer %d:%p (status %d)",i, port->pl->transfers[i], port->pl->transfers[i]->status); /* this happens if the transfer is completed for instance, but not reaped. we cannot cancel it. */ if (LOG_ON_LIBUSB_E(libusb_cancel_transfer(port->pl->transfers[i])) < 0) { libusb_free_transfer (port->pl->transfers[i]); port->pl->transfers[i] = NULL; } } } tv.tv_sec = 0; tv.tv_usec = 0; LOG_ON_LIBUSB_E (libusb_handle_events_timeout(port->pl->ctx, &tv)); /* Do just one round ... this should be sufficient and avoids endless loops. */ haveone = 0; for (i = 0; i < sizeof(port->pl->transfers)/sizeof(port->pl->transfers[0]); i++) { if (port->pl->transfers[i]) { GP_LOG_D("checking: transfer %d:%p status %d",i, port->pl->transfers[i], port->pl->transfers[i]->status); haveone = 1; } } if (haveone) LOG_ON_LIBUSB_E (libusb_handle_events(port->pl->ctx)); return GP_OK; }
static int receive_data(int fd, int revents, void *user_data) { struct timeval tv; /* Avoid compiler warnings. */ (void)fd; (void)revents; (void)user_data; tv.tv_sec = tv.tv_usec = 0; libusb_handle_events_timeout(usb_context, &tv); return TRUE; }
XN_THREAD_PROC xnUSBHandleEventsThread(XN_THREAD_PARAM pThreadParam) { // init timeout struct timeval timeout; timeout.tv_sec = XN_USB_HANDLE_EVENTS_TIMEOUT / 1000; timeout.tv_usec = XN_USB_HANDLE_EVENTS_TIMEOUT % 1000; while (g_InitData.bShouldThreadRun) { // let libusb process its asynchronous events libusb_handle_events_timeout(g_InitData.pContext, &timeout); } XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
static int receive_data(int fd, int revents, void *cb_data) { struct timeval tv; struct drv_context *drvc; (void)fd; (void)revents; drvc = (struct drv_context *)cb_data; tv.tv_sec = tv.tv_usec = 0; libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); return TRUE; }
static void usb_handle_events(void *opaque) { int rc; libusb_context *context = opaque; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; rc = libusb_handle_events_timeout(context, &tv); if (rc < 0) { log_error("Could not handle USB events: %s (%d)", usb_get_error_name(rc), rc); } }
int usb_handle_events(int unused) { #ifndef WIN32 return libusb_handle_events(ctx); #else if(ctx != NULL) { struct timeval tv = { 0 }; return libusb_handle_events_timeout(ctx, &tv); } else { return 0; } #endif }
int slogic_execute_recording(struct slogic_ctx *handle){ int transfer_id,retval,ret; struct timeval timeout; handle->recording_state = WARMING_UP; for (transfer_id = 0; transfer_id < handle->n_transfer_buffers; transfer_id++) { slogic_prime_data(handle, transfer_id); } handle->recording_state = RUNNING; for (transfer_id = 0; transfer_id < handle->n_transfer_buffers; transfer_id++) { if(!transfer_id){ slogic_set_capture_async(handle); } slogic_pump_data(handle, transfer_id); handle->transfer_count++; } while (handle->recording_state == RUNNING) { timeout.tv_sec = 1; if((ret = libusb_handle_events_timeout(handle->usb_context, &timeout))){ log_printf( ERR, "libusb_handle_events: %s\n", usbutil_error_to_string(ret)); break; } } //spindown! if (handle->recording_state == COMPLETED_SUCCESSFULLY) { log_printf(INFO, "Capture Success!\n"); }else{ log_printf( ERR, "Capture Fail! recording_state=%d\n", handle->recording_state); retval = 1; } return retval; }
void vrpn_LUDL_USBMAC6000::mainloop() { if (_device_handle == NULL) { return; } // Let libusb handle any outstanding events struct timeval zerotime; zerotime.tv_sec = 0; zerotime.tv_usec = 0; libusb_handle_events_timeout(_context, &zerotime); // If one of the axes is moving, check to see whether it has stopped. // If so, report its new position. // XXX Would like to change this to poll (or have the device send // continuously) the actual position, rather than relying on it having // gotten where we asked it to go). if (!_axis_moving || !_axis_destination) { return; } int i; for (i = 0; i < o_num_channel; i++) { if (_axis_moving[i]) { if (!ludl_axis_moving(i+1)) { vrpn_Analog::channel[i] = _axis_destination[i]; _axis_moving[i] = false; } } } // Ask for and record the positions of the two axes. // Remember that the axes are numbered starting from 1 on the // LUDL controller but they go in Analog channels 2 and 3. vrpn_int32 position; if (ludl_axis_position(1, &position)) { channel[2] = position; } if (ludl_axis_position(2, &position)) { channel[3] = position; } // Let all of the servers do their thing. server_mainloop(); vrpn_gettimeofday(&_timestamp, NULL); report_changes(); // Call the server_mainloop on our unique base class. server_mainloop(); }
static void *poll_thread_main(void *arg) { int r = 0; DEBUGP("poll thread running\n"); while (!do_exit) { struct timeval tv = { 1, 0 }; r = libusb_handle_events_timeout(NULL, &tv); if (r < 0) { pthread_cond_signal(&exit_cond); break; } } DEBUGP("poll thread shutting down\n"); return NULL; }
static void* transfer_threadproc(void* arg) { hackrf_device* device = (hackrf_device*)arg; int error; struct timeval timeout = { 0, 500000 }; while( (device->streaming) && (do_exit == false) ) { error = libusb_handle_events_timeout(g_libusb_context, &timeout); if( (error != 0) && (error != LIBUSB_ERROR_INTERRUPTED) ) { device->streaming = false; } } return NULL; }
static void *poll_thread_main(void *arg) { int r = 0; printf("poll thread running\n"); while (!do_exit) { struct timeval tv = { 1, 0 }; r = libusb_handle_events_timeout(NULL, &tv); if (r < 0) { request_exit(2); break; } } printf("poll thread shutting down\n"); pthread_exit(NULL); }
int submit_wait(struct stlink_libusb *slu, struct libusb_transfer * trans) { struct timeval start; struct timeval now; struct timeval diff; struct trans_ctx trans_ctx; enum libusb_error error; trans_ctx.flags = 0; /* brief intrusion inside the libusb interface */ trans->callback = on_trans_done; trans->user_data = &trans_ctx; if ((error = libusb_submit_transfer(trans))) { printf("libusb_submit_transfer(%d)\n", error); return -1; } gettimeofday(&start, NULL); while (trans_ctx.flags == 0) { struct timeval timeout; timeout.tv_sec = 3; timeout.tv_usec = 0; if (libusb_handle_events_timeout(slu->libusb_ctx, &timeout)) { printf("libusb_handle_events()\n"); return -1; } gettimeofday(&now, NULL); timersub(&now, &start, &diff); if (diff.tv_sec >= 3) { printf("libusb_handle_events() timeout\n"); return -1; } } if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) { printf("libusb_handle_events() | has_error\n"); return -1; } return 0; }
static int handle_event(int fd, int revents, void *cb_data) { const struct sr_dev_inst *sdi; struct sr_datafeed_packet packet; struct timeval tv; struct sr_dev_driver *di; struct dev_context *devc; struct drv_context *drvc; (void)fd; (void)revents; sdi = cb_data; di = sdi->driver; drvc = di->context; devc = sdi->priv; /* Always handle pending libusb events. */ tv.tv_sec = tv.tv_usec = 0; libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); if (devc->dev_state == STOPPING) { /* We've been told to wind up the acquisition. */ sr_dbg("Stopping acquisition."); hantek_6xxx_stop_data_collecting(sdi); /* * TODO: Doesn't really cancel pending transfers so they might * come in after SR_DF_END is sent. */ usb_source_remove(sdi->session, drvc->sr_ctx); packet.type = SR_DF_END; packet.payload = NULL; sr_session_send(sdi, &packet); devc->dev_state = IDLE; return TRUE; } return TRUE; }
static void usb_loop(struct aura_node *node, const struct aura_pollfds *fd) { struct aura_buffer *buf; struct usb_dev_info *inf = aura_get_transportdata(node); struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; libusb_handle_events_timeout(inf->ctx, &tv); if (inf->cbusy) return; if (inf->state == AUSB_DEVICE_RESTART) { slog(4, SLOG_DEBUG, "usb: transport offlined, starting to look for a device"); aura_set_status(node, AURA_STATUS_OFFLINE); libusb_close(inf->handle); inf->handle = NULL; inf->state = AUSB_DEVICE_SEARCHING; ncusb_watch_for_device(inf->ctx, &inf->dev_descr); return; } if (inf->state == AUSB_DEVICE_OPERATIONAL) { if (inf->pending) submit_event_readout(node); else if ( (buf = aura_dequeue_buffer(&node->outbound_buffers)) ) { submit_call_write(node, buf); } } } static struct aura_transport usb = { .name = "usb", .open = usb_open, .close = usb_close, .loop = usb_loop, .buffer_overhead = LIBUSB_CONTROL_SETUP_SIZE, /* Offset for usb SETUP structure */ .buffer_offset = LIBUSB_CONTROL_SETUP_SIZE }; AURA_TRANSPORT(usb);
int freespace_flush(FreespaceDeviceId id) { struct FreespaceDevice* device = findDeviceById(id); struct FreespaceReceiveTransfer* rt; struct timeval tv; int repeat; int maxRepeats = FREESPACE_RECEIVE_QUEUE_SIZE * 2; if (device == NULL || device->state_ != FREESPACE_OPENED) { return FREESPACE_ERROR_NOT_FOUND; } // As long as there's work, try again. do { // Poll libusb to give it a chance to unload as many // events as possible. tv.tv_sec = 0; tv.tv_usec = 0; libusb_handle_events_timeout(freespace_libusb_context, &tv); repeat = 0; // Clear out our queue. rt = &device->receiveQueue_[device->receiveQueueHead_]; while (rt->submitted_ == 0) { rt->submitted_ = 1; libusb_submit_transfer(rt->transfer_); device->receiveQueueHead_++; if (device->receiveQueueHead_ >= FREESPACE_RECEIVE_QUEUE_SIZE) { device->receiveQueueHead_ = 0; } rt = &device->receiveQueue_[device->receiveQueueHead_]; repeat = 1; } maxRepeats--; } while (repeat > 0 && maxRepeats > 0); return FREESPACE_SUCCESS; }
int Context::handleEvents(State & state, Context_wrapper * wrapper){ Stack * stack = state.stack; int result = 0; timeval tv; bool timeValSet = fillTimeval(state, tv, 1); int completed; if (stack->is<LUA_TBOOLEAN>(2)){ completed = (stack->to<bool>(2)) ? 1 : 0; } else{ completed = -1; } if (timeValSet){ if (completed >= 0){ result = libusb_handle_events_timeout_completed(wrapper->context, &tv, &completed); } else{ result = libusb_handle_events_timeout(wrapper->context, &tv); } } else{ if (completed >= 0){ result = libusb_handle_events_completed(wrapper->context, &completed); } else{ result = libusb_handle_events(wrapper->context); } } if (result == LIBUSB_SUCCESS){ stack->push<bool>(true); return 1; } else{ stack->push<bool>(false); stack->push<int>(result); return 2; } }
void usbio_transfer_shutdown(struct usbio_transfer *xfer) { if (xfer->pending) { int res = libusb_cancel_transfer(xfer->transfer); if (res != LIBUSB_ERROR_NO_DEVICE) { int tries = 100; while(!xfer->cancel_confirm && tries > 0 && xfer->pending) { struct timeval tv = { .tv_sec = 0, .tv_usec = 1000 }; libusb_handle_events_timeout(xfer->usbctx, &tv); tries--; } if (!tries) g_warning("Timed out waiting for transfer '%s:%d' to complete; status = %d", xfer->transfer_type, xfer->index, xfer->transfer->status); } }
static void *event_thread(void *param) { yContextSt *ctx = (yContextSt*)param; char errmsg[YOCTO_ERRMSG_LEN]; ctx->usb_thread_state = USB_THREAD_RUNNING; /* Non-blocking. See if the OS has any reports to give. */ HALLOG("Start event_thread run loop\n"); while (ctx->usb_thread_state != USB_THREAD_MUST_STOP) { int res; struct timeval tv; memset(&tv,0,sizeof(tv)); tv.tv_sec = 1; res = libusb_handle_events_timeout(ctx->libusb, &tv); if (res < 0) { yLinSetErr("libusb_handle_events_timeout", res,errmsg); break; } } HALLOG("event_thread run loop stoped\n"); ctx->usb_thread_state = USB_THREAD_STOPED; return NULL; }
bool vrpn_LUDL_USBMAC6000::check_for_data(void) { if (_device_handle == NULL) { return false; } // Let libusb handle any outstanding events struct timeval zerotime; zerotime.tv_sec = 0; zerotime.tv_usec = 0; libusb_handle_events_timeout(_context, &zerotime); // Try to read as many characters as are left in the buffer from // the device. Keep track of how many we get. int chars_to_read = _INBUFFER_SIZE - _incount; int chars_read = 0; int ret; //printf("dbg: Starting bulk receive\n"); ret = libusb_bulk_transfer(_device_handle, _endpoint | LIBUSB_ENDPOINT_IN, &_inbuffer[_incount], chars_to_read, &chars_read, 1); //printf("dbg: Finished bulk receive\n"); if ( (ret != LIBUSB_SUCCESS) && (ret != LIBUSB_ERROR_TIMEOUT) ) { #ifdef libusb_strerror fprintf(stderr, "vrpn_LUDL_USBMAC6000::check_for_data(): Could not read data: %s\n", libusb_strerror(static_cast<libusb_error>(ret))); #else fprintf(stderr, "vrpn_LUDL_USBMAC6000::check_for_data(): Could not read data: code %d\n", ret); #endif return false; } _incount += chars_read; return true; }
static void *event_loop(void *ptr) { int ret, refcnt; struct timeval tv = {1, 0}; TRACE("Entering USB event loop\n"); while(1) { /* First check if someone is still listening to libusb events */ pthread_mutex_lock(&refcnt_mutex); refcnt = event_loop_refcnt; pthread_mutex_unlock(&refcnt_mutex); if(refcnt <= 0) break; /* Handle pending events */ ret=libusb_handle_events_timeout(libusb_ctx, &tv); if(ret != 0) { printf("USB error. Exiting\n"); break; } } TRACE("Exiting USB event loop\n"); return 0; }