int usb_78k0_write(struct UART78K0 *uart, uint8_t *data, int length, int *transferred, int timeout) { uint8_t endpoint = ENDPOINT_OUT; int ret; int try = 0; do { ret = libusb_bulk_transfer(uart->handle, endpoint, data, length, transferred, timeout); if (ret == LIBUSB_ERROR_PIPE) { libusb_clear_halt(uart->handle, endpoint); } try++; } while ((ret == LIBUSB_ERROR_PIPE) && (try < RETRY_MAX)); return ret; } int usb_78k0_read(struct UART78K0 *uart, uint8_t *buf, int length, int *transferred, int timeout_ms) { #ifndef UART_ASYNC_READ uint8_t endpoint = ENDPOINT_IN; int ret; int try = 0; do { ret = libusb_bulk_transfer(uart->handle, endpoint, buf, length, transferred, timeout_ms); if (ret == LIBUSB_ERROR_PIPE) { libusb_clear_halt(uart->handle, endpoint); } try++; } while ((ret == LIBUSB_ERROR_PIPE) && (try < RETRY_MAX)); return ret; #else struct timeval start; gettimeofday(&start, NULL); struct timespec timeout; timeout.tv_sec = start.tv_sec + timeout_ms / 1000; timeout.tv_nsec = start.tv_usec * 1000 + (timeout_ms % 1000) * 1000000; int ret = 0; *transferred = 0; while (*transferred < length && ret == 0) { pthread_mutex_lock(&uart->read_mutex); while (uart->read_buffer_size == 0) { ret = pthread_cond_timedwait(&uart->read_cond, &uart->read_mutex, &timeout); if (ret == ETIMEDOUT) { break; } } int size = (uart->read_buffer_size > length - *transferred) ? (length - *transferred) : uart->read_buffer_size; memcpy(buf + *transferred, uart->read_buffer, size); *transferred += size; uart->read_buffer_size -= size; if (uart->read_buffer_size > 0) { memmove(uart->read_buffer, uart->read_buffer + size, uart->read_buffer_size); } pthread_mutex_unlock(&uart->read_mutex); } return ret; #endif }
static int gp_port_usb_clear_halt_lib(GPPort *port, int ep) { int ret=0; if (!port || !port->pl->dh) return GP_ERROR_BAD_PARAMETERS; switch (ep) { case GP_PORT_USB_ENDPOINT_IN : ret=libusb_clear_halt(port->pl->dh, port->settings.usb.inep); break; case GP_PORT_USB_ENDPOINT_OUT : ret=libusb_clear_halt(port->pl->dh, port->settings.usb.outep); break; case GP_PORT_USB_ENDPOINT_INT : ret=libusb_clear_halt(port->pl->dh, port->settings.usb.intep); break; default: gp_port_set_error (port, "gp_port_usb_clear_halt: " "bad EndPoint argument"); return GP_ERROR_BAD_PARAMETERS; } return (ret ? GP_ERROR_IO_USB_CLEAR_HALT : GP_OK); }
void cleanup_USB500( libusb_device_handle *udev ) { if (udev) { libusb_clear_halt(udev, LIBUSB_ENDPOINT_IN|2); libusb_clear_halt(udev, LIBUSB_ENDPOINT_OUT|2); libusb_release_interface(udev, 0); libusb_close(udev); } }
//********************************************************** //! //! Open connection to device enumerated by bdm_usb_find_devices() //! //! @param device_no Device number to open //! //! @return \n //! == ICP_RC_OK (0) => Success\n //! == ICP_RC_USB_ERROR => USB failure //! ICP_ErrorType bdm_usb_open( unsigned int device_no ) { // print("bdm_usb_open( %d )\n", device_no); if (!initialised) { print("bdm_usb_open() - Not initialised! \n"); return ICP_RC_USB_ERROR; } if (device_no >= deviceCount) { print("bdm_usb_open() - Illegal device #\n"); return ICP_RC_ILLEGAL_PARAMS; } if (usbDeviceHandle != NULL) { print("bdm_usb_open() - Closing previous device\n"); bdm_usb_close(); } int rc = libusb_open(bdmDevices[device_no], &usbDeviceHandle); if (rc != LIBUSB_SUCCESS) { print("bdm_usb_open() - libusb_open() failed, rc = %s\n", libusb_strerror((libusb_error)rc)); usbDeviceHandle = NULL; return ICP_RC_USB_ERROR; } int configuration = 0; rc = libusb_get_configuration(usbDeviceHandle, &configuration); if (rc != LIBUSB_SUCCESS) { print("bdm_usb_open() - libusb_get_configuration() failed, rc = %s\n", libusb_strerror((libusb_error)rc)); } if (configuration != 1) { rc = libusb_set_configuration(usbDeviceHandle, 1); if (rc != LIBUSB_SUCCESS) { print("bdm_usb_open() - libusb_set_configuration(1) failed, rc = %s\n", libusb_strerror((libusb_error)rc)); // Release the device libusb_close(usbDeviceHandle); usbDeviceHandle = NULL; return ICP_RC_USB_ERROR; } } rc = libusb_claim_interface(usbDeviceHandle, 0); if (rc != LIBUSB_SUCCESS) { print("bdm_usb_open() - libusb_claim_interface(0) failed, rc = %s\n", libusb_strerror((libusb_error)rc)); // Release the device libusb_set_configuration(usbDeviceHandle, 0); libusb_close(usbDeviceHandle); usbDeviceHandle = NULL; return ICP_RC_USB_ERROR; } rc = libusb_clear_halt(usbDeviceHandle, 0x01); if (rc != LIBUSB_SUCCESS) { print("bdm_usb_open() - libusb_clear_halt(...,0x01) failed, rc = %s\n", libusb_strerror((libusb_error)rc)); } rc = libusb_clear_halt(usbDeviceHandle, 0x82); if (rc != LIBUSB_SUCCESS) { print("bdm_usb_open() - libusb_clear_halt(...,0x82) failed, rc = %s\n", libusb_strerror((libusb_error)rc)); } return (ICP_RC_OK); }
static int clearHalt(deviceInfo *info, unsigned int ep) { usbDevice *handle = handleFromInfoPtr(info); switch (ep) { case EP_IN: return libusb_clear_halt(handle->device, handle->epIn->bEndpointAddress); case EP_OUT: return libusb_clear_halt(handle->device, handle->epOut->bEndpointAddress); } return -1; }
static void async_callback(struct libusb_transfer *transfer) { if (libusb_state != LIB_USB_TRANSFERS_ALLOCATED) return; int r; // log_info("begin async_callback endpoint %x, status %x, actual length %u", transfer->endpoint, transfer->status, transfer->actual_length ); if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { queue_transfer(transfer); } else if (transfer->status == LIBUSB_TRANSFER_STALL){ log_info("-> Transfer stalled, trying again"); r = libusb_clear_halt(handle, transfer->endpoint); if (r) { log_error("Error rclearing halt %d", r); } r = libusb_submit_transfer(transfer); if (r) { log_error("Error re-submitting transfer %d", r); } } else { log_info("async_callback. not data -> resubmit transfer, endpoint %x, status %x, length %u", transfer->endpoint, transfer->status, transfer->actual_length); // No usable data, just resubmit packet r = libusb_submit_transfer(transfer); if (r) { log_error("Error re-submitting transfer %d", r); } } // log_info("end async_callback"); }
static int readcard(libusb_device_handle *handle) { unsigned char buff[1280]; int transferred = 0, rv; rv = libusb_clear_halt(handle, 0x82); if (rv != 0) { usbx_strerror(rv); return -1; } rv = libusb_bulk_transfer (handle, 0x82, buff, sizeof(buff), &transferred, 10 * 1000); if (rv != 0) { usbx_strerror(rv); return -1; } printf("read %d bytes\n", transferred); for (rv = 0; rv < transferred; rv++) { printf(isprint(buff[rv]) ? "%c" : "\\x%02X", buff[rv]); } printf("\n"); return transferred; }
int irecv_bulk_transfer(irecv_client_t client, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout) { int ret; #ifndef WIN32 ret = libusb_bulk_transfer(client->handle, endpoint, data, length, transferred, timeout); if (ret < 0) { libusb_clear_halt(client->handle, endpoint); } #else if (endpoint == 0x4) { ret = DeviceIoControl(client->handle, 0x220195, data, length, data, length, (PDWORD) transferred, NULL); } else { ret = 0; } ret = (ret == 0 ? -1 : 0); #endif return ret; }
static int gp_libusb1_clear_halt_lib(GPPort *port, int ep) { unsigned char internal_ep; C_PARAMS (port && port->pl->dh); switch (ep) { case GP_PORT_USB_ENDPOINT_IN : internal_ep = port->settings.usb.inep; break; case GP_PORT_USB_ENDPOINT_OUT : internal_ep = port->settings.usb.outep; break; case GP_PORT_USB_ENDPOINT_INT : internal_ep = port->settings.usb.intep; break; default: gp_port_set_error (port, "bad EndPoint argument 0x%x", ep); return GP_ERROR_BAD_PARAMETERS; } C_LIBUSB (libusb_clear_halt(port->pl->dh, internal_ep), GP_ERROR_IO_USB_CLEAR_HALT); return GP_OK; }
void usb_ClearEndpnt(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout){ int xferd; libusb_clear_halt(dev_handle,endpoint); libusb_bulk_transfer(dev_handle, endpoint, data, length, &xferd, timeout); cout<<"Entered Clean Endpoint\n"<<endl; libusb_bulk_transfer(dev_handle, endpoint, data, length, &xferd, timeout); }
QByteArray CameraUSB::read(const unsigned sizePacket, const int timeOut, bool *state) { if(!_devHandle) { if(state) *state = false; return QByteArray(); } unsigned char buffer[sizePacket]; int transferredByte = 0; int ret = libusb_bulk_transfer(_devHandle, _epIn, buffer, sizePacket, &transferredByte, timeOut); if(ret != 0) { if(ret==LIBUSB_ERROR_TIMEOUT) { /*qDebug()<<"Read timeout"<<endl;*/ } else if(ret==LIBUSB_ERROR_NO_DEVICE) { // device disconnected qDebug()<<"Camera disconnected"; if(state) *state = false; return QByteArray(); } else { qDebug()<<"Cannot read packet: "<<QString(libusb_strerror((libusb_error)ret))<<transferredByte; if(libusb_reset_device(_devHandle)!= 0) qDebug()<<"Cannot reset device"<<endl; if(libusb_clear_halt(_devHandle, _epOut)!= 0) qDebug()<<"Cannot clear EPOUT"<<endl; if(libusb_clear_halt(_devHandle, _epIn)!= 0) qDebug()<<"Cannot clear EPIN"<<endl; if(state) *state = false; return QByteArray(); } } if(state) *state = true; return QByteArray((const char *)buffer, transferredByte); }
void cleanup_USB1608FS_Plus(libusb_device_handle *udev) { if (udev) { libusb_clear_halt(udev, LIBUSB_ENDPOINT_IN|1); libusb_release_interface(udev, 0); libusb_close(udev); } }
static int send_mass_storage_command(libusb_device_handle *handle, uint8_t endpoint, uint8_t lun, uint8_t *cdb, uint8_t direction, int data_length, uint32_t *ret_tag) { static uint32_t tag = 1; uint8_t cdb_len; int i, r, size; struct command_block_wrapper cbw; if (cdb == NULL) { return -1; } if (endpoint & LIBUSB_ENDPOINT_IN) { perr("send_mass_storage_command: cannot send command on IN endpoint\n"); return -1; } cdb_len = cdb_length[cdb[0]]; if ((cdb_len == 0) || (cdb_len > sizeof(cbw.CBWCB))) { perr("send_mass_storage_command: don't know how to handle this command (%02X, length %d)\n", cdb[0], cdb_len); return -1; } memset(&cbw, 0, sizeof(cbw)); cbw.dCBWSignature[0] = 'U'; cbw.dCBWSignature[1] = 'S'; cbw.dCBWSignature[2] = 'B'; cbw.dCBWSignature[3] = 'C'; *ret_tag = tag; cbw.dCBWTag = tag++; cbw.dCBWDataTransferLength = data_length; cbw.bmCBWFlags = direction; cbw.bCBWLUN = lun; // Subclass is 1 or 6 => cdb_len cbw.bCBWCBLength = cdb_len; memcpy(cbw.CBWCB, cdb, cdb_len); i = 0; do { // The transfer length must always be exactly 31 bytes. r = libusb_bulk_transfer(handle, endpoint, (unsigned char*)&cbw, 31, &size, 1000); if (r == LIBUSB_ERROR_PIPE) { libusb_clear_halt(handle, endpoint); } i++; } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX)); if (r != LIBUSB_SUCCESS) { perr(" send_mass_storage_command: %s\n", libusb_strerror((enum libusb_error)r)); return -1; } printf(" sent %d CDB bytes\n", cdb_len); return 0; }
/** * PicoLCD 20x2 Initialization Function * * The 20x2 doesn't need any initialization. This function just returns 0. * @param lcdDevice Pointer to the LcdDevice structure * @return returns 0 */ int lcd_20x2_init(MyLcdDevice *lcdDevice) { libusb_clear_halt(lcdDevice->lcdHandle, LIBUSB_ENDPOINT_IN + 1); /* Initialize your decode stuff here*/ IR20x4 *ir20x4; lcdDevice->irStruct=(void*)malloc(sizeof(IR20x4)); ir20x4=(IR20x4*)lcdDevice->irStruct; ir20x4->bitCount=0; ir20x4->rawBitCount=0; ir20x4->rc5Raw=(unsigned char *)malloc(128); /*assuming each element in irData is a LONG_PULSE (1 1) or LONG_SPACE(0 0) */ memset(ir20x4->rc5Raw,0,128); return 0; }
int usbClearEndpoint (UsbDevice *device, unsigned char endpointAddress) { UsbDeviceExtension *devx = device->extension; if (usbGetHandle(devx)) { int result = libusb_clear_halt(devx->handle, endpointAddress); if (result == LIBUSB_SUCCESS) return 1; usbSetErrno(result, "libusb_clear_halt"); } return 0; }
enum libusb_error uhd_iface_clear_halt(uhd_iface *iface) { enum libusb_error err; assert(uhd_iface_valid(iface)); err = libusb_clear_halt(iface->dev->handle, iface->int_in_ep_addr); if (err != LIBUSB_SUCCESS) return err; return LIBUSB_SUCCESS; }
void irecv_usb_clear_halt(libusb_device_handle *usb_handle, unsigned char endpoint) { libusbip_error_t error = LIBUSBIP_E_SUCCESS; if (libirecovery_usage_context == IRECV_CTX_LOCAL) { libusb_clear_halt(usb_handle, endpoint); return; } error = libusbip_clear_halt(&libirecovery_connection_info, &libirecovery_device_handle, endpoint); if (error < 0) { debug("libusbip_clear_halt failed\n"); } }
static int get_usb_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t *tag) { unsigned char csw[13]; memset(csw, 0, sizeof(csw)); int transferred; int ret; int try = 0; do { ret = libusb_bulk_transfer(handle, endpoint, (unsigned char *)&csw, sizeof(csw), &transferred, SG_TIMEOUT_MSEC); if (ret == LIBUSB_ERROR_PIPE) { libusb_clear_halt(handle, endpoint); } try++; } while ((ret == LIBUSB_ERROR_PIPE) && (try < 3)); if (ret != LIBUSB_SUCCESS) { fprintf(stderr, "%s: receiving failed: %d\n", __func__, ret); return -1; } if (transferred != sizeof(csw)) { fprintf(stderr, "%s: received unexpected amount: %d\n", __func__, transferred); return -1; } uint32_t rsig = read_uint32(csw, 0); uint32_t rtag = read_uint32(csw, 4); uint32_t residue = read_uint32(csw, 8); #define USB_CSW_SIGNATURE 0x53425355 // 'U' 'S' 'B' 'S' (reversed) if (rsig != USB_CSW_SIGNATURE) { WLOG("status signature was invalid: %#x\n", rsig); return -1; } DLOG("residue was= %#x\n", residue); *tag = rtag; uint8_t rstatus = csw[12]; DLOG("rstatus = %x\n", rstatus); return rstatus; } static int dump_CDB_command(uint8_t *cdb, uint8_t cdb_len) { char dbugblah[100]; char *dbugp = dbugblah; dbugp += sprintf(dbugp, "Sending CDB ["); for (uint8_t i = 0; i < cdb_len; i++) { dbugp += sprintf(dbugp, " %#02x", (unsigned int) cdb[i]); } sprintf(dbugp, "]\n"); DLOG(dbugblah); return 0; }
static int scpi_usbtmc_libusb_close(void *priv) { int ret; struct scpi_usbtmc_libusb *uscpi = priv; struct sr_usb_dev_inst *usb = uscpi->usb; if (!usb->devhdl) return SR_ERR; if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) sr_err("Failed to clear halt/stall condition for EP %d: %s.", uscpi->bulk_in_ep, libusb_error_name(ret)); if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) sr_err("Failed to clear halt/stall condition for EP %d: %s.", uscpi->bulk_out_ep, libusb_error_name(ret)); if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0) sr_err("Failed to clear halt/stall condition for EP %d: %s.", uscpi->interrupt_ep, libusb_error_name(ret)); if ((ret = libusb_release_interface(usb->devhdl, uscpi->interface)) < 0) sr_err("Failed to release interface: %s.", libusb_error_name(ret)); if (uscpi->detached_kernel_driver) { if ((ret = libusb_attach_kernel_driver(usb->devhdl, uscpi->interface)) < 0) sr_err("Failed to re-attach kernel driver: %s.", libusb_error_name(ret)); uscpi->detached_kernel_driver = 0; } libusb_close(usb->devhdl); usb->devhdl = NULL; return SR_OK; }
static int get_mass_storage_status(libusb_device_handle *handle, uint8_t endpoint, uint32_t expected_tag) { int i, r, size; struct command_status_wrapper csw; // The device is allowed to STALL this transfer. If it does, you have to // clear the stall and try again. i = 0; do { r = libusb_bulk_transfer(handle, endpoint, (unsigned char*)&csw, 13, &size, 1000); if (r == LIBUSB_ERROR_PIPE) { libusb_clear_halt(handle, endpoint); } i++; } while ((r == LIBUSB_ERROR_PIPE) && (i<RETRY_MAX)); if (r != LIBUSB_SUCCESS) { perr(" get_mass_storage_status: %s\n", libusb_strerror((enum libusb_error)r)); return -1; } if (size != 13) { perr(" get_mass_storage_status: received %d bytes (expected 13)\n", size); return -1; } if (csw.dCSWTag != expected_tag) { perr(" get_mass_storage_status: mismatched tags (expected %08X, received %08X)\n", expected_tag, csw.dCSWTag); return -1; } // For this test, we ignore the dCSWSignature check for validity... printf(" Mass Storage Status: %02X (%s)\n", csw.bCSWStatus, csw.bCSWStatus?"FAILED":"Success"); if (csw.dCSWTag != expected_tag) return -1; if (csw.bCSWStatus) { // REQUEST SENSE is appropriate only if bCSWStatus is 1, meaning that the // command failed somehow. Larger values (2 in particular) mean that // the command couldn't be understood. if (csw.bCSWStatus == 1) return -2; // request Get Sense else return -1; } // In theory we also should check dCSWDataResidue. But lots of devices // set it wrongly. return 0; }
int USBLink::receive(uint8_t *data, uint32_t len, uint16_t timeoutMs) { int res, transferred; if (timeoutMs==0) // 0 equals infinity timeoutMs = 50; if ((res=libusb_bulk_transfer(m_handle, 0x82, (unsigned char *)data, len, &transferred, timeoutMs))<0) { #ifdef __MACOS__ libusb_clear_halt(m_handle, 0x82); #endif qDebug("libusb_bulk_read %d", res); return res; } return transferred; }
int USBLink::receive(uint8_t *data, uint32_t len, uint16_t timeoutMs) { int res, transferred; if (timeoutMs==0) // 0 equals infinity timeoutMs = 50; // Note: if this call is taking more time than than expected, check to see if we're connected as USB 2.0. Bad USB cables can // cause us to revert to a 1.0 connection. if ((res=libusb_bulk_transfer(m_handle, 0x82, (unsigned char *)data, len, &transferred, timeoutMs))<0) { log("pixydebug: libusb_bulk_transfer() = %d\n", res); #ifdef __MACOS__ libusb_clear_halt(m_handle, 0x82); #endif return res; } return transferred; }
int USBLink::send(const uint8_t *data, uint32_t len, uint16_t timeoutMs) { int res, transferred; if (timeoutMs==0) // 0 equals infinity timeoutMs = 10; if ((res=libusb_bulk_transfer(m_handle, 0x02, (unsigned char *)data, len, &transferred, timeoutMs))<0) { #ifdef __MACOS__ libusb_clear_halt(m_handle, 0x02); #endif log("pixydebug: USBLink::send() returned %d\n", res); return res; } log("pixydebug: USBLink::send() returned %d\n", transferred); return transferred; }
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; }
bool IdacDriverUsb24Base::claim(bool bUnhalt) { CHECK_PRECOND_RETVAL(handle() != NULL, false); int res; libusb_device* dev = libusb_get_device(handle()); CHECK_ASSERT_RETVAL(dev != NULL, false); libusb_config_descriptor* config = NULL; res = libusb_get_config_descriptor(dev, 0, &config); CHECK_USBRESULT_RETVAL(res, false); // Select configuration for the first descriptor res = libusb_set_configuration(handle(), config->bConfigurationValue); CHECK_USBRESULT_RETVAL(res, false); CHECK_ASSERT_RETVAL(config->bNumInterfaces > 0, false); const libusb_interface* interface = &config->interface[0]; CHECK_ASSERT_RETVAL(interface->num_altsetting > 0, false); const libusb_interface_descriptor* altsetting = &interface->altsetting[0]; const int idInterface = altsetting->bInterfaceNumber; res = libusb_claim_interface(handle(), idInterface); CHECK_USBRESULT_RETVAL(res, false); res = libusb_set_interface_alt_setting(handle(), idInterface, altsetting->bAlternateSetting); CHECK_USBRESULT_RETVAL(res, false); if (bUnhalt) { for (int i = 0; i < altsetting->bNumEndpoints; i++) { const int idEndpoint = altsetting->endpoint[i].bEndpointAddress; res = libusb_clear_halt(handle(), idEndpoint); CHECK_USBRESULT_RETVAL(res, false); } } return true; }
void irpc_send_usb_clear_halt(struct irpc_connection_info *ci) { tpl_node *tn = NULL; irpc_retval_t retval = IRPC_SUCCESS; irpc_device_handle handle; char endpoint; int sock = ci->client_sock; // Read irpc_device_handle, and endpoint to server. tn = tpl_map(IRPC_CLEAR_HALT_FMT, &handle, &endpoint); tpl_load(tn, TPL_FD, sock); tpl_unpack(tn, 0); tpl_free(tn); if (libusb_clear_halt(irpc_handle, endpoint) != 0) retval = IRPC_FAILURE; // Send libusb_clear_halt packet. tn = tpl_map(IRPC_INT_FMT, &retval); tpl_pack(tn, 0); tpl_dump(tn, TPL_FD, sock); tpl_free(tn); }
static int scpi_usbtmc_libusb_open(void *priv) { struct scpi_usbtmc_libusb *uscpi = priv; struct sr_usb_dev_inst *usb = uscpi->usb; struct libusb_device *dev; struct libusb_device_descriptor des; struct libusb_config_descriptor *confdes; const struct libusb_interface_descriptor *intfdes; const struct libusb_endpoint_descriptor *ep; int confidx, intfidx, epidx, config = 0; uint8_t capabilities[24]; int ret, found = 0; if (usb->devhdl) return SR_OK; if (sr_usb_open(uscpi->ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; dev = libusb_get_device(usb->devhdl); if ((ret = libusb_get_device_descriptor(dev, &des))) { sr_err("Failed to get device descriptor: %s.", libusb_error_name(ret)); return SR_ERR; } for (confidx = 0; confidx < des.bNumConfigurations; confidx++) { if (libusb_get_config_descriptor(dev, confidx, &confdes) != 0) { sr_err("Failed to get configuration descriptor: %s.", libusb_error_name(ret)); continue; } for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) { intfdes = confdes->interface[intfidx].altsetting; if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION || intfdes->bInterfaceSubClass != SUBCLASS_USBTMC || intfdes->bInterfaceProtocol != USBTMC_USB488) continue; uscpi->interface = intfdes->bInterfaceNumber; sr_dbg("Interface %d", uscpi->interface); config = confdes->bConfigurationValue; sr_dbg("Configuration %d", config); for (epidx = 0; epidx < intfdes->bNumEndpoints; epidx++) { ep = &intfdes->endpoint[epidx]; if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK && !(ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK))) { uscpi->bulk_out_ep = ep->bEndpointAddress; sr_dbg("Bulk OUT EP %d", uscpi->bulk_out_ep); } if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK && ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) { uscpi->bulk_in_ep = ep->bEndpointAddress; sr_dbg("Bulk IN EP %d", uscpi->bulk_in_ep); } if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT && ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) { uscpi->interrupt_ep = ep->bEndpointAddress; sr_dbg("Interrupt EP %d", uscpi->interrupt_ep); } } found = 1; } libusb_free_config_descriptor(confdes); if (found) break; } if (!found) { sr_err("Failed to find USBTMC interface."); return SR_ERR; } if (libusb_kernel_driver_active(usb->devhdl, uscpi->interface) == 1) { if ((ret = libusb_detach_kernel_driver(usb->devhdl, uscpi->interface)) < 0) { sr_err("Failed to detach kernel driver: %s.", libusb_error_name(ret)); return SR_ERR; } uscpi->detached_kernel_driver = 1; } if ((ret = libusb_set_configuration(usb->devhdl, config))) { sr_err("Failed to set configuration: %s.", libusb_error_name(ret)); return SR_ERR; } if ((ret = libusb_claim_interface(usb->devhdl, uscpi->interface))) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) { sr_err("Failed to clear halt/stall condition for EP %d: %s.", uscpi->bulk_in_ep, libusb_error_name(ret)); return SR_ERR; } if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) { sr_err("Failed to clear halt/stall condition for EP %d: %s.", uscpi->bulk_out_ep, libusb_error_name(ret)); return SR_ERR; } if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0) { sr_err("Failed to clear halt/stall condition for EP %d: %s.", uscpi->interrupt_ep, libusb_error_name(ret)); return SR_ERR; } /* Get capabilities. */ ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, GET_CAPABILITIES, 0, uscpi->interface, capabilities, sizeof(capabilities), TRANSFER_TIMEOUT); if (ret == sizeof(capabilities)) { uscpi->usbtmc_int_cap = capabilities[ 4]; uscpi->usbtmc_dev_cap = capabilities[ 5]; uscpi->usb488_dev_cap = capabilities[15]; } sr_dbg("Device capabilities: %s%s%s%s%s, %s, %s", uscpi->usb488_dev_cap & USB488_DEV_CAP_SCPI ? "SCPI, " : "", uscpi->usbtmc_dev_cap & USBTMC_DEV_CAP_TERMCHAR ? "TermChar, ": "", uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "L3, " : uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "" : "L4, ", uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "T5, " : uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "" : "T6, ", uscpi->usb488_dev_cap & USB488_DEV_CAP_SR1 ? "SR1" : "SR0", uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1 ? "RL1" : "RL0", uscpi->usb488_dev_cap & USB488_DEV_CAP_DT1 ? "DT1" : "DT0"); return SR_OK; }
static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in) { int r, size, descriptor_size; uint8_t hid_report_descriptor[256]; uint8_t *report_buffer; FILE *fd; printf("\nReading HID Report Descriptors:\n"); descriptor_size = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_STANDARD|LIBUSB_RECIPIENT_INTERFACE, LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_REPORT<<8, 0, hid_report_descriptor, sizeof(hid_report_descriptor), 1000); if (descriptor_size < 0) { printf(" Failed\n"); return -1; } display_buffer_hex(hid_report_descriptor, descriptor_size); if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) { if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != descriptor_size) { printf(" Error writing descriptor to file\n"); } fclose(fd); } size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_FEATURE); if (size <= 0) { printf("\nSkipping Feature Report readout (None detected)\n"); } else { report_buffer = (uint8_t*) calloc(size, 1); if (report_buffer == NULL) { return -1; } printf("\nReading Feature Report (length %d)...\n", size); r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE, HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 0, report_buffer, (uint16_t)size, 5000); if (r >= 0) { display_buffer_hex(report_buffer, size); } else { switch(r) { case LIBUSB_ERROR_NOT_FOUND: printf(" No Feature Report available for this device\n"); break; case LIBUSB_ERROR_PIPE: printf(" Detected stall - resetting pipe...\n"); libusb_clear_halt(handle, 0); break; default: printf(" Error: %s\n", libusb_strerror((enum libusb_error)r)); break; } } free(report_buffer); } size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_INPUT); if (size <= 0) { printf("\nSkipping Input Report readout (None detected)\n"); } else { report_buffer = (uint8_t*) calloc(size, 1); if (report_buffer == NULL) { return -1; } printf("\nReading Input Report (length %d)...\n", size); r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE, HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, report_buffer, (uint16_t)size, 5000); if (r >= 0) { display_buffer_hex(report_buffer, size); } else { switch(r) { case LIBUSB_ERROR_TIMEOUT: printf(" Timeout! Please make sure you act on the device within the 5 seconds allocated...\n"); break; case LIBUSB_ERROR_PIPE: printf(" Detected stall - resetting pipe...\n"); libusb_clear_halt(handle, 0); break; default: printf(" Error: %s\n", libusb_strerror((enum libusb_error)r)); break; } } // Attempt a bulk read from endpoint 0 (this should just return a raw input report) printf("\nTesting interrupt read using endpoint %02X...\n", endpoint_in); r = libusb_interrupt_transfer(handle, endpoint_in, report_buffer, size, &size, 5000); if (r >= 0) { display_buffer_hex(report_buffer, size); } else { printf(" %s\n", libusb_strerror((enum libusb_error)r)); } free(report_buffer); } return 0; }
int cyusb_clear_halt(cyusb_handle *h, unsigned char endpoint) { return ( libusb_clear_halt(h, endpoint) ); }
void readCSW(libusb_device_handle *handler) { int r1; unsigned char buf1[1024]; int transferred=0; int i; int state = 1; while (state != 0) { switch(state) { case 1: r1 = libusb_bulk_transfer(handler, 0x82, buf1, 1024, &transferred, 3000); if (r1==0) { printf ("CSW:"); for (i=0; i<transferred; i++) { printf (" %02x",buf1[i]); } printf ("\n"); } state = 2; break; case 2: if (r1 == 0) { state = 3; } else { state = 5; } break; case 3: if (transferred == 13 && buf1[0] == '\x55' && buf1[1]=='\x53' && buf1[2]=='\x42' && buf1[3]=='\x53') { state = 4; } else { state = 8; } break; case 4: if (transferred == 13 && buf1[12] == '\x02') { state = 8; } else { state = 0; } break; case 5: r1 = libusb_clear_halt(handler, 0x82); if (r1 != 0) { error_at_line(0,0,__FILE__,__LINE__,"Error: cannot clear halt for endpoints 0x82"); } state = 6; break; case 6: r1 = libusb_bulk_transfer(handler, 0x82, buf1, 1024, &transferred, 3000); if (r1==0) { printf ("CSW:"); for (i=0; i<transferred; i++) { printf (" %02x",buf1[i]); } printf ("\n"); } state = 7; break; case 7: if (r1 == 0) { state = 3; } else { state = 8; } break; case 8: r1 = libusb_reset_device(handler); if (r1 != 0) { error_at_line(0,0,__FILE__,__LINE__,"Error: cannot reset device"); } state = 0; break; } } return; }