void FX3Stream::sendCallback(libusb_transfer *transfer) { FX3Stream* fx3 = (FX3Stream*)transfer->user_data; if( transfer->status != LIBUSB_TRANSFER_COMPLETED ) { if( FX3_DEBUG ) { printTransfer(transfer); } switch(transfer->status) { case LIBUSB_TRANSFER_TIMED_OUT: fx3->error = LIBUSB_ERROR_TIMEOUT; break; default: fx3->error = -1; break; } } if( transfer->length != transfer->actual_length ) { if( FX3_DEBUG && transfer->status == LIBUSB_TRANSFER_COMPLETED ) printTransfer(transfer); fx3->shortPackets += 1; } fx3->submittedBytes -= transfer->length; fx3->transferredBytes += transfer->actual_length; if( !fx3->error && fx3->transferredBytes+fx3->submittedBytes < fx3->totalBytes ) { int64_t remainingBytes = fx3->totalBytes-(fx3->transferredBytes+fx3->submittedBytes); if( remainingBytes > FX3_TRANSFER_SIZE ) { libusb_fill_bulk_transfer( transfer, fx3->devhandle, FX3_OUT_EP, &fx3->extBuf[fx3->transferredBytes+fx3->submittedBytes], FX3_TRANSFER_SIZE, FX3Stream::sendCallback, fx3, transfer->timeout ); fx3->submittedBytes += FX3_TRANSFER_SIZE; } else { libusb_fill_bulk_transfer( transfer, fx3->devhandle, FX3_OUT_EP, &fx3->extBuf[fx3->transferredBytes+fx3->submittedBytes], remainingBytes, FX3Stream::sendCallback, fx3, transfer->timeout ); fx3->submittedBytes += remainingBytes; } fx3->error = libusb_submit_transfer( transfer ); if( fx3->error ) { cerr << "Error submitting transfer: " << libusb_error_name(fx3->error) << endl; } } }
static void activate_run_state(struct fpi_ssm *ssm) { struct fp_img_dev *dev = ssm->priv; struct upektc_dev *upekdev = dev->priv; int r; switch (ssm->cur_state) { case WRITE_INIT: { struct libusb_transfer *transfer = libusb_alloc_transfer(0); if (!transfer) { fpi_ssm_mark_aborted(ssm, -ENOMEM); return; } libusb_fill_bulk_transfer(transfer, dev->udev, upekdev->ep_out, (unsigned char*)upekdev->setup_commands[upekdev->init_idx].cmd, UPEKTC_CMD_LEN, write_init_cb, ssm, BULK_TIMEOUT); r = libusb_submit_transfer(transfer); if (r < 0) { libusb_free_transfer(transfer); fpi_ssm_mark_aborted(ssm, -ENOMEM); } } break; case READ_DATA: { struct libusb_transfer *transfer = libusb_alloc_transfer(0); unsigned char *data; if (!transfer) { fpi_ssm_mark_aborted(ssm, -ENOMEM); break; } data = g_malloc(upekdev->setup_commands[upekdev->init_idx].response_len); libusb_fill_bulk_transfer(transfer, dev->udev, upekdev->ep_in, data, upekdev->setup_commands[upekdev->init_idx].response_len, read_init_data_cb, ssm, BULK_TIMEOUT); r = libusb_submit_transfer(transfer); if (r < 0) { g_free(data); libusb_free_transfer(transfer); fpi_ssm_mark_aborted(ssm, r); } } break; } }
int64_t FX3Stream::send(int64_t words, uint32_t *data, unsigned timeout) { if( error ) { cerr << "Blocked" << endl; return 0; } totalBytes = words*4; extBuf = (unsigned char*)data; transferredBytes = 0; submittedBytes = 0; shortPackets = 0; //Start transfer for( unsigned i = 0; i < transfers.size() && transferredBytes+submittedBytes < totalBytes; ++i ) { int64_t remainingBytes = totalBytes-(transferredBytes+submittedBytes); if( remainingBytes > FX3_TRANSFER_SIZE ) { libusb_fill_bulk_transfer( transfers[i], devhandle, FX3_OUT_EP, &extBuf[transferredBytes+submittedBytes], FX3_TRANSFER_SIZE, FX3Stream::sendCallback, this, timeout ); submittedBytes += FX3_TRANSFER_SIZE; } else if( remainingBytes > 0 ) { libusb_fill_bulk_transfer( transfers[i], devhandle, FX3_OUT_EP, &extBuf[transferredBytes+submittedBytes], remainingBytes, FX3Stream::sendCallback, this, timeout ); submittedBytes += remainingBytes; } error = libusb_submit_transfer( transfers[i] ); if( error ) { cerr << "Error submitting transfer: " << libusb_error_name(error) << endl; break; } } //Wait for transfer while( submittedBytes ) { int r = libusb_handle_events_completed(0,0); if( r ) { cerr << "Error handling events: " << libusb_error_name(r) << endl; error = r; break; } } if( shortPackets ) { cerr << "Error: Short packet(s) in outgoing transfers." << endl; shortPackets = 0; } if( error < 0 ) { cerr << "Error sending data: " << libusb_error_name(error) << endl; } if( error == LIBUSB_ERROR_TIMEOUT ) { error = 0; } return transferredBytes/4; }
static void capture_run_state(struct fpi_ssm *ssm) { struct fp_img_dev *dev = ssm->priv; struct upektc_dev *upekdev = dev->priv; int r; switch (ssm->cur_state) { case CAPTURE_WRITE_CMD: { struct libusb_transfer *transfer = libusb_alloc_transfer(0); if (!transfer) { fpi_ssm_mark_aborted(ssm, -ENOMEM); return; } libusb_fill_bulk_transfer(transfer, dev->udev, upekdev->ep_out, (unsigned char *)scan_cmd, UPEKTC_CMD_LEN, capture_cmd_cb, ssm, BULK_TIMEOUT); r = libusb_submit_transfer(transfer); if (r < 0) { libusb_free_transfer(transfer); fpi_ssm_mark_aborted(ssm, -ENOMEM); } } break; case CAPTURE_READ_DATA: { struct libusb_transfer *transfer = libusb_alloc_transfer(0); unsigned char *data; if (!transfer) { fpi_ssm_mark_aborted(ssm, -ENOMEM); break; } data = g_malloc(IMAGE_SIZE); libusb_fill_bulk_transfer(transfer, dev->udev, upekdev->ep_in, data, IMAGE_SIZE, capture_read_data_cb, ssm, BULK_TIMEOUT); r = libusb_submit_transfer(transfer); if (r < 0) { g_free(data); libusb_free_transfer(transfer); fpi_ssm_mark_aborted(ssm, r); } } break; }; }
static void dev_deactivate(struct fp_img_dev *dev) { struct vfs0050_dev *vfs_dev = dev->priv; int err = 0; int tmpoffset; int to_send; struct libusb_transfer *t; vfs_dev->is_active = 0; //EP2 IN t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP2_IN, vfs_dev->tmpbuf, 64, generic_async_cb, NULL, 100); libusb_submit_transfer(t); //EP1_IN t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_IN, vfs_dev->tmpbuf, 64, generic_async_cb, NULL, 100); libusb_submit_transfer(t); //EP1_OUT t = libusb_alloc_transfer(0); vfs_dev->tmpbuf[0] = 0x04; libusb_fill_bulk_transfer(t, dev->udev, EP1_OUT, vfs_dev->tmpbuf, 1, generic_async_cb, NULL, 100); libusb_submit_transfer(t); //EP1_IN t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_IN, vfs_dev->tmpbuf, 64, generic_async_cb, NULL, 100); libusb_submit_transfer(t); //EP1_OUT t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_OUT, vfs0050_deactivate1, 64, generic_async_cb, NULL, 100); libusb_submit_transfer(t); //EP1_OUT t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_OUT, &vfs0050_deactivate1[64], 61, generic_async_cb, NULL, 100); libusb_submit_transfer(t); tmpoffset = 0; do { to_send = sizeof(vfs0050_activate2) - tmpoffset; to_send = to_send >= 64 ? 64 : to_send; t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_OUT, vfs0050_activate2 + tmpoffset, to_send, generic_async_cb, NULL, 100); libusb_submit_transfer(t); tmpoffset += err; } while (err == 64); do { t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_IN, vfs_dev->tmpbuf, 64, generic_async_cb, NULL, 100); libusb_submit_transfer(t); } while(err == 64); t = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(t, dev->udev, EP1_IN, vfs_dev->tmpbuf, 64, generic_async_cb, NULL, 100); libusb_submit_transfer(t); //TODO: finish this, leaves device in inconsistent state. fpi_imgdev_deactivate_complete(dev); }
static libusb_transfer* alloc_lut (fusb_ephandle *self, int buffer_length, int endpoint, bool input_p, unsigned char *write_buffer, fusb_devhandle *dh) { struct libusb_transfer* lut = libusb_alloc_transfer(0); endpoint = (endpoint & 0x7f) | (input_p ? 0x80 : 0); if (input_p) write_buffer = new unsigned char [buffer_length]; // We need the base class libusb_device_handle libusb_device_handle *dev_handle = dh->get_usb_dev_handle(); // Load the libusb_transfer for bulk transfer libusb_fill_bulk_transfer (lut, // transfer dev_handle, // dev_handle endpoint, // endpoint write_buffer, // buffer buffer_length, // length generic_callback, // callback self, // user_data 0); // timeout return lut; }
void FX3Dev::startRead( DeviceDataHandlerIfce* handler ) { fprintf(stderr,"FX3Dev::startRead()\n"); size_tx_mb = 0.0; int res; for(uint32_t i = 0;i<buffers_count;i++) { transfers[i] = libusb_alloc_transfer(0); if ( transfers[i] == NULL ) { fprintf( stderr, "FX3Dev::startRead() __error__ libusb_alloc_transfer(0) returned NULL\n" ); abort(); } } for(uint32_t i = 0;i<buffers_count;i++) { libusb_fill_bulk_transfer(transfers[i], device_handle, (endpoint_from_dev_num | LIBUSB_ENDPOINT_IN), half_full_buffers[i], half_full_size8, FX3Dev::onDataReady, this, DEV_DOWNLOAD_TIMEOUT_MS); } data_handler = handler; event_loop_running = true; event_thread = std::thread(&FX3Dev::event_loop, this); for(uint32_t i = 0;i<buffers_count;i++) { res = libusb_submit_transfer(transfers[i]); if(res != 0) { fprintf(stderr,"FX3Dev::startRead() __error__ libusb_submit_transfer%d %d %s\n", i, res, libusb_error_name(res)); abort(); } } }
void FalconCommLibUSB::issueRead() { //If a read is already allocated, don't reallocate //We'll expect someone else to do this for us again later if(m_isReadAllocated) { return; } //Try to read over 64 and you'll fry libusb-1.0. Try to read under //64 and you'll fry OS X. So, read 64. out_transfer = libusb_alloc_transfer(0); if (!out_transfer) { m_errorCode = FALCON_COMM_DEVICE_ERROR; LOG_ERROR("Cannot allocate outbound transfer"); return; } libusb_fill_bulk_transfer(out_transfer, m_falconDevice, 0x81, output, 64, FalconCommLibUSB::cb_out, this, 1000); libusb_submit_transfer(out_transfer); m_isReadAllocated = true; }
A2300::BulkDataPort::TransferContext* A2300::BulkDataPort::CreateReadTransferContext( byte* bufFrame, size_t sizeFrame) { libusb_transfer *lut = libusb_alloc_transfer(0); TransferContext* pctxt = new TransferContext(); libusb_fill_bulk_transfer( lut, // transfer DeviceHandle(), // dev_handle epidIn(), // endpoint bufFrame, // buffer (int) sizeFrame, // length &A2300::BulkDataPort::LibusbAsyncReadCallback, // callback pctxt, // user_data 0 // timeout (ms) ); pctxt->pSrc = this; pctxt->lut = lut; pctxt->bufFrame = bufFrame; pctxt->nFrameSize = sizeFrame; //m_listReadContexts.push_back( pctxt); return pctxt; }
FCDevice::Transfer::Transfer(FCDevice *device, void *buffer, int length) : transfer(libusb_alloc_transfer(0)), device(device) { libusb_fill_bulk_transfer(transfer, device->mHandle, OUT_ENDPOINT, (uint8_t*) buffer, length, FCDevice::completeTransfer, this, 2000); }
bool FalconCommLibUSB::write(uint8_t* buffer, unsigned int size) { LOG_DEBUG("Writing " << size << " bytes"); if(!m_isCommOpen) { LOG_ERROR("Device not open"); m_errorCode = FALCON_COMM_DEVICE_NOT_VALID_ERROR; return false; } m_lastBytesWritten = size; in_transfer = libusb_alloc_transfer(0); if (!in_transfer) { m_errorCode = FALCON_COMM_DEVICE_ERROR; LOG_ERROR("Cannot allocate inbound transfer"); return false; } libusb_fill_bulk_transfer(in_transfer, m_falconDevice, 0x02, buffer, size, FalconCommLibUSB::cb_in, this, 0); libusb_submit_transfer(in_transfer); m_isWriteAllocated = true; m_hasBytesAvailable = false; issueRead(); return true; }
FCDevice::Transfer::Transfer(FCDevice *device, void *buffer, int length, PacketType type) : transfer(libusb_alloc_transfer(0)), type(type), finished(false) { libusb_fill_bulk_transfer(transfer, device->mHandle, OUT_ENDPOINT, (uint8_t*) buffer, length, FCDevice::completeTransfer, this, 2000); }
int slogic_prime_data(struct slogic_ctx *handle, unsigned int transfer_id){ char * buffer; struct libusb_transfer *newtransfer; buffer = malloc(handle->transfer_buffer_size); assert(buffer); newtransfer = libusb_alloc_transfer(0); newtransfer->flags |= (LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER ); if (newtransfer == NULL) { log_printf( ERR, "libusb_alloc_transfer failed\n"); handle->recording_state = UNKNOWN; return 1; } handle->transfers[transfer_id].logic_context = handle; handle->transfers[transfer_id].transfer = newtransfer; handle->transfers[transfer_id].transfer_id = transfer_id; handle->transfers[transfer_id].state = 0; //FIXME return value? libusb_fill_bulk_transfer(newtransfer, handle->device_handle, SALEAE_STREAMING_DATA_IN_ENDPOINT, (unsigned char *)buffer, handle->transfer_buffer_size,slogic_read_samples_callback,&handle->transfers[transfer_id], handle->transfer_timeout); return 0; }
int cmd_rx_data(struct libusb_device_handle* devh) { empty_usb_buf =(u8 *) malloc(BUFFER_SIZE); rx_xfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(rx_xfer, devh, DATA_IN, empty_usb_buf, BUFFER_SIZE, cb_xfer, NULL, TIMEOUT); cmd_rx_syms(devh, num_blocks); r = libusb_submit_transfer(rx_xfer); if (r < 0) { fprintf(stderr, "rx_xfer submission: %d\n", r); return -1; } while (1) { while (!usb_really_full) { handle_events_wrapper(); } /* process each received block */ for (i = 0; i < xfer_blocks; i++) { rx = (usb_pkt_rx *)(full_usb_buf + PKT_LEN * i); if(rx->pkt_type != KEEP_ALIVE) (*cb)(cb_args, rx, bank); bank = (bank + 1) % NUM_BANKS; } usb_really_full = 0; } }
static int usb_send_acl_packet(uint8_t *packet, int size){ int r; if (libusb_state != LIB_USB_TRANSFERS_ALLOCATED) return -1; // log_info("usb_send_acl_packet enter, size %u", size); // prepare transfer int completed = 0; libusb_fill_bulk_transfer(acl_out_transfer, handle, acl_out_addr, packet, size, async_callback, &completed, 0); acl_out_transfer->type = LIBUSB_TRANSFER_TYPE_BULK; // update stata before submitting transfer usb_acl_out_active = 1; r = libusb_submit_transfer(acl_out_transfer); if (r < 0) { usb_acl_out_active = 0; log_error("Error submitting acl transfer, %d", r); return -1; } return 0; }
SR_PRIV int hantek_6xxx_get_channeldata(const struct sr_dev_inst *sdi, libusb_transfer_cb_fn cb, uint32_t data_amount) { struct sr_usb_dev_inst *usb; struct libusb_transfer *transfer; int ret; unsigned char *buf; sr_dbg("Request channel data."); usb = sdi->conn; if (!(buf = g_try_malloc(data_amount))) { sr_err("Failed to malloc USB endpoint buffer."); return SR_ERR_MALLOC; } transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, usb->devhdl, HANTEK_EP_IN, buf, data_amount, cb, (void *)sdi, 4000); if ((ret = libusb_submit_transfer(transfer)) < 0) { sr_err("Failed to submit transfer: %s.", libusb_error_name(ret)); /* TODO: Free them all. */ libusb_free_transfer(transfer); g_free(buf); return SR_ERR; } return SR_OK; }
/** * \brief Submit a bulk transfer to the device. * * This method allocates and fills the bulk transfer, and submits it to * the libusb_submit_transfer function. It then starts to handle events, * and only returns when the complete flag is set. * \param dev_handle the libusb device handle */ void BulkTransfer::submit(libusb_device_handle *dev_handle) throw(USBError) { // allocate the transfer structure transfer = libusb_alloc_transfer(0); // fill the transfer libusb_fill_bulk_transfer(transfer, dev_handle, endpoint->bEndpointAddress(), data, length, bulktransfer_callback, this, timeout); // submit the transfer debug(LOG_DEBUG, DEBUG_LOG, 0, "submitting bulk transfer, timeout = %d", timeout); int rc = libusb_submit_transfer(transfer); if (rc != LIBUSB_SUCCESS) { throw USBError(libusb_error_name(rc)); } // handle events until the complete flag is set libusb_context *ctx = getContext(); while (!complete) { libusb_handle_events(ctx); } // at this point, the transfer has somehow completed, but we // don't know yet what happened. const char *cause = usb_status_name(transfer->status); if (NULL != cause) { debug(LOG_ERR, DEBUG_LOG, 0, "transfer failed: %s", cause); throw USBError(cause); } else { debug(LOG_DEBUG, DEBUG_LOG, 0, "transfer complete, %d bytes", transfer->actual_length); } }
static void aesX660_read_response(struct fpi_ssm *ssm, size_t buf_len, libusb_transfer_cb_fn callback) { struct fp_img_dev *dev = ssm->priv; struct libusb_transfer *transfer = libusb_alloc_transfer(0); unsigned char *data; int r; if (!transfer) { fpi_ssm_mark_aborted(ssm, -ENOMEM); return; } data = g_malloc(buf_len); libusb_fill_bulk_transfer(transfer, dev->udev, EP_IN, data, buf_len, callback, ssm, BULK_TIMEOUT); r = libusb_submit_transfer(transfer); if (r < 0) { fp_dbg("Failed to submit rx transfer: %d\n", r); g_free(data); libusb_free_transfer(transfer); fpi_ssm_mark_aborted(ssm, r); } }
static void start_finger_detection(struct fp_img_dev *dev) { int r; struct upektc_dev *upekdev = dev->priv; struct libusb_transfer *transfer; fp_dbg(""); if (upekdev->deactivating) { complete_deactivation(dev); return; } transfer = libusb_alloc_transfer(0); if (!transfer) { fpi_imgdev_session_error(dev, -ENOMEM); return; } libusb_fill_bulk_transfer(transfer, dev->udev, upekdev->ep_out, (unsigned char *)scan_cmd, UPEKTC_CMD_LEN, finger_det_cmd_cb, dev, BULK_TIMEOUT); r = libusb_submit_transfer(transfer); if (r < 0) { libusb_free_transfer(transfer); fpi_imgdev_session_error(dev, r); } }
static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *buffer, int length, int *transferred, unsigned int timeout, unsigned char type) { struct libusb_transfer *transfer = libusb_alloc_transfer(0); int completed = 0; int r; if (!transfer) return LIBUSB_ERROR_NO_MEM; libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length, bulk_transfer_cb, &completed, timeout); transfer->type = type; r = libusb_submit_transfer(transfer); if (r < 0) { libusb_free_transfer(transfer); return r; } while (!completed) { r = libusb_handle_events(HANDLE_CTX(dev_handle)); if (r < 0) { libusb_cancel_transfer(transfer); while (!completed) if (libusb_handle_events(HANDLE_CTX(dev_handle)) < 0) break; libusb_free_transfer(transfer); return r; } } *transferred = transfer->actual_length; switch (transfer->status) { case LIBUSB_TRANSFER_COMPLETED: r = 0; break; case LIBUSB_TRANSFER_TIMED_OUT: r = LIBUSB_ERROR_TIMEOUT; break; case LIBUSB_TRANSFER_STALL: r = LIBUSB_ERROR_PIPE; break; case LIBUSB_TRANSFER_OVERFLOW: r = LIBUSB_ERROR_OVERFLOW; break; case LIBUSB_TRANSFER_NO_DEVICE: r = LIBUSB_ERROR_NO_DEVICE; break; default: usbi_warn(HANDLE_CTX(dev_handle), "unrecognised status code %d", transfer->status); r = LIBUSB_ERROR_OTHER; } libusb_free_transfer(transfer); return r; }
int et_read_enroll_data(struct fp_img_dev* dev,struct et_init* dinit,void* user_data) { int r; static struct init_data adata; struct etss801u_dev* edev=dev->priv; if(edev->deactivating) { complete_deactivation(dev); return 0; } adata.init=dinit; adata.ssm=user_data; struct libusb_transfer* transfer=libusb_alloc_transfer(0); if(!transfer) return -ENOMEM; switch(dinit->stage) { /*case 16: libusb_fill_bulk_transfer(transfer,dev->udev,EP_IN,et_img_buf[dinit->stage-16],6157,et_read_enroll_cb,&adata,BULK_TIMEOUT); break;*/ case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: libusb_fill_bulk_transfer(transfer,dev->udev,EP_IN,et_img_buf[dinit->stage-17],61453,et_read_enroll_cb,&adata,BULK_TIMEOUT); break; default: libusb_fill_bulk_transfer(transfer,dev->udev,EP_IN,(char*)&ret_buf,8192,et_read_enroll_cb,&adata,BULK_TIMEOUT); break; } r=libusb_submit_transfer(transfer); if(r<0) { r=-EIO; libusb_free_transfer(transfer); } return r; }
ssize_t send_recv(struct stlink_libusb* handle, int terminate, unsigned char* txbuf, size_t txsize, unsigned char* rxbuf, size_t rxsize) { /* note: txbuf and rxbuf can point to the same area */ int res = 0; libusb_fill_bulk_transfer(handle->req_trans, handle->usb_handle, handle->ep_req, txbuf, txsize, NULL, NULL, 0 ); if (submit_wait(handle, handle->req_trans)) return -1; /* send_only */ if (rxsize != 0) { /* read the response */ libusb_fill_bulk_transfer(handle->rep_trans, handle->usb_handle, handle->ep_rep, rxbuf, rxsize, NULL, NULL, 0); if (submit_wait(handle, handle->rep_trans)) return -1; res = handle->rep_trans->actual_length; } if ((handle->protocoll == 1) && terminate) { fprintf(stderr, "This is never used....\n"); exit(EXIT_FAILURE); /* Read the SG reply */ unsigned char sg_buf[13]; libusb_fill_bulk_transfer (handle->rep_trans, handle->usb_handle, handle->ep_rep, sg_buf, 13, NULL, NULL, 0); res = submit_wait(handle, handle->rep_trans); /* The STLink doesn't seem to evaluate the sequence number */ handle->sg_transfer_idx++; if (res ) return -1; } return handle->rep_trans->actual_length; }
/////////////////////////////////////////////////////////////////////////////// // readAsync() - read async data from accessory device /////////////////////////////////////////////////////////////////////////////// //argument // callback : callback function. // void function(struct libusb_transfer *transfer) //return // <0 : LIBUSB_ERROR_NO_DEVICE if the device has been disconnected and a // LIBUSB_ERROR code on other failure. // =0 : Succes // int AOA::readAsync(libusb_transfer_cb_fn callback, char* buffer, int bufferLen, void* userData, unsigned int timeout) { libusb_fill_bulk_transfer( inTransfer, handle, inEP, // EP_RESPONSE, (uint8_t*)buffer, bufferLen, callback, userData, timeout); inTransfer->type = LIBUSB_TRANSFER_TYPE_BULK; int result = libusb_submit_transfer(inTransfer); return result; }
int hw_start_acquisition(int device_index, gpointer session_device_id) { struct usb_device_instance *udi; struct datafeed_packet *packet; struct datafeed_header *header; struct libusb_transfer *transfer; const struct libusb_pollfd **lupfd; int size, i; unsigned char *buf; if( !(udi = get_usb_device_instance(usb_devices, device_index))) return SIGROK_NOK; packet = g_malloc(sizeof(struct datafeed_packet)); header = g_malloc(sizeof(struct datafeed_header)); if(!packet || !header) return SIGROK_NOK; /* start with 2K transfer, subsequently increased to 4K */ size = 2048; for(i = 0; i < NUM_SIMUL_TRANSFERS; i++) { buf = g_malloc(size); transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, udi->devhdl, 2 | LIBUSB_ENDPOINT_IN, buf, size, receive_transfer, session_device_id, 40); if(libusb_submit_transfer(transfer) != 0) { /* TODO: free them all */ libusb_free_transfer(transfer); g_free(buf); return SIGROK_NOK; } size = 4096; } lupfd = libusb_get_pollfds(usb_context); for(i = 0; lupfd[i]; i++) add_source_fd(lupfd[i]->fd, lupfd[i]->events, receive_data, NULL); free(lupfd); packet->type = DF_HEADER; packet->length = sizeof(struct datafeed_header); packet->payload = (unsigned char *) header; header->feed_version = 1; gettimeofday(&header->starttime, NULL); header->rate = cur_sample_rate; header->protocol_id = PROTO_RAW; header->num_probes = NUM_PROBES; session_bus(session_device_id, packet); g_free(header); g_free(packet); return SIGROK_OK; }
static int alloc_transfers(void) { req_transfer = libusb_alloc_transfer(0); if (!req_transfer) return -ENOMEM; libusb_fill_bulk_transfer(req_transfer, devh, EP_IN, read_buffer, sizeof(read_buffer), cb_req, NULL, 0); return 0; }
/** * \brief Submit a bulk transfer to the device. * * This method allocates and fills the bulk transfer, and submits it to * the libusb_submit_transfer function. It then starts to handle events, * and only returns when the complete flag is set. * \param dev_handle the libusb device handle */ void BulkTransfer::submit(libusb_device_handle *dev_handle) throw(USBError) { // allocate the transfer structure transfer = libusb_alloc_transfer(0); // fill the transfer libusb_fill_bulk_transfer(transfer, dev_handle, endpoint->bEndpointAddress(), data, length, bulktransfer_callback, this, timeout); // submit the transfer int rc = libusb_submit_transfer(transfer); if (rc != LIBUSB_SUCCESS) { throw USBError(libusb_error_name(rc)); } // handle events until the complete flag is set libusb_context *ctx = getContext(); while (!complete) { libusb_handle_events(ctx); } // at this point, the transfer has somehow completed, but we // don't know yet what happened. const char *cause = NULL; switch (transfer->status) { case LIBUSB_TRANSFER_ERROR: cause = "transfer error"; break; case LIBUSB_TRANSFER_TIMED_OUT: cause = "transfer timed out"; break; case LIBUSB_TRANSFER_CANCELLED: cause = "transfer cancelled"; break; case LIBUSB_TRANSFER_STALL: cause = "transfer stall"; break; case LIBUSB_TRANSFER_NO_DEVICE: cause = "transfer no device"; break; case LIBUSB_TRANSFER_OVERFLOW: cause = "transfer overflow"; break; case LIBUSB_TRANSFER_COMPLETED: break; } if (NULL != cause) { debug(LOG_ERR, DEBUG_LOG, 0, "transfer failed: %s", cause); throw USBError(cause); } else { debug(LOG_DEBUG, DEBUG_LOG, 0, "transfer complete, %d bytes", transfer->actual_length); } }
bool start_transfers(libusb_device_handle *handle, uint32_t curr_frame_size) { struct libusb_transfer *xfr0,*xfr1; uint8_t* buff, *buff1; uint8_t ep_addr; int bsize = 16384; frame_size = curr_frame_size; // bulk transfers xfr0 = libusb_alloc_transfer(0); xfr1 = libusb_alloc_transfer(0); buff = frame_buffer_end; buff1 = buff + bsize; memset(frame_buffer_end, 0, bsize*2); xfr[0] = xfr0; xfr[1] = xfr1; ep_addr = find_ep(libusb_get_device(handle)); //debug("found ep: %d\n", ep_addr); libusb_clear_halt(handle, ep_addr); libusb_fill_bulk_transfer(xfr0, handle, ep_addr, buff, bsize, cb_xfr, reinterpret_cast<void*>(this), 0); libusb_fill_bulk_transfer(xfr1, handle, ep_addr, buff1, bsize, cb_xfr, reinterpret_cast<void*>(this), 0); int res = libusb_submit_transfer(xfr0); res |= libusb_submit_transfer(xfr1); num_transfers = 2; frame_complete_ind = 0; frame_work_ind = 0; last_pts = 0; last_fid = 0; last_frame_time = 0; return res == 0; }
void usb_write(uint8_t *data, size_t datalen) { struct libusb_transfer *xfer; unsigned char *buf; writes_pending += 1; xfer = libusb_alloc_transfer(0); buf = (unsigned char *)malloc(datalen); memcpy(buf, data, datalen); libusb_fill_bulk_transfer(xfer, usb_dev, d->ep_out, buf, datalen, usb_write_done, NULL, 0); libusb_submit_transfer(xfer); }
static void usb_initiate_transfer() { unsigned char *buf; buf = (unsigned char *)malloc(256); // Tell libusb we want to know about bulk transfers struct libusb_transfer *xfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(xfer, usb_dev, d->ep_in, buf, 256, usb_xfer_done, NULL, 0); libusb_submit_transfer(xfer); reads_pending += 1; }
void setupTransfer(uint8_t* buffer, uint32_t size) { atransfer = libusb_alloc_transfer(0); // 0 iso libusb_fill_bulk_transfer(atransfer, MyLibusbDeviceHandle, EP_IN, buffer, size, kolbask, (void *) 0, 5000); resubmit = 1; }