static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; struct drv_context *drvc = di->context; struct sr_usb_dev_inst *usb; int ret; usb = sdi->conn; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; if ((ret = libusb_set_configuration(usb->devhdl, 1))) { sr_err("Failed to set configuration: %s.", libusb_error_name(ret)); return SR_ERR; } if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } sdi->status = SR_ST_ACTIVE; return ret; }
static int dev_open(struct sr_dev_inst *sdi) { struct drv_context *drvc; struct sr_usb_dev_inst *usb; int ret; drvc = di->priv; if (!drvc) { sr_err("Driver was not initialized."); return SR_ERR; } usb = sdi->conn; ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); if (ret != SR_OK) return ret; ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); if (ret < 0) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } sdi->status = SR_ST_INITIALIZING; ret = lwla_init_device(sdi); if (ret == SR_OK) sdi->status = SR_ST_ACTIVE; return ret; }
static int hw_dev_open(struct sr_dev_inst *sdi) { struct drv_context *drvc; struct dev_context *devc; drvc = di->priv; devc = sdi->priv; return sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb); }
static int dev_open(struct sr_dev_inst *sdi, int dmm) { struct drv_context *drvc; struct sr_usb_dev_inst *usb; int ret; drvc = udmms[dmm].di->priv; usb = sdi->conn; if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) sdi->status = SR_ST_ACTIVE; return ret; }
static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di; struct drv_context *drvc; struct sr_usb_dev_inst *usb; int ret; di = sdi->driver; drvc = di->context; usb = sdi->conn; if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) sdi->status = SR_ST_ACTIVE; return ret; }
static int scan_kecheng(struct sr_dev_driver *di, struct sr_usb_dev_inst *usb, char **model) { struct drv_context *drvc; int len, ret; unsigned char cmd, buf[32]; drvc = di->context; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; cmd = CMD_IDENTIFY; ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5); if (ret != 0) { libusb_close(usb->devhdl); sr_dbg("Failed to send Identify command: %s", libusb_error_name(ret)); return SR_ERR; } ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 32, &len, 10); if (ret != 0) { libusb_close(usb->devhdl); sr_dbg("Failed to receive response: %s", libusb_error_name(ret)); return SR_ERR; } libusb_close(usb->devhdl); usb->devhdl = NULL; if (len < 2 || buf[0] != (CMD_IDENTIFY | 0x80) || buf[1] > 30) { sr_dbg("Invalid response to Identify command"); return SR_ERR; } buf[buf[1] + 2] = '\x0'; *model = g_strndup((const gchar *)buf + 2, 30); return SR_OK; }
static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; struct drv_context *drvc = di->context; struct sr_usb_dev_inst *usb; int ret; usb = sdi->conn; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; /* * The libusb 1.0.9 Darwin backend is broken: it can report a kernel * driver being active, but detaching it always returns an error. */ #if !defined(__APPLE__) if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) { sr_err("failed to detach kernel driver: %s", libusb_error_name(ret)); return SR_ERR; } } #endif if ((ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION))) { sr_err("Failed to set configuration: %s.", libusb_error_name(ret)); return SR_ERR; } if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } sdi->status = SR_ST_ACTIVE; return ret; }
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 dev_open(struct sr_dev_inst *sdi) { struct drv_context *drvc; struct dev_context *devc; struct sr_usb_dev_inst *usb; uint8_t buffer[PACKET_LENGTH]; int ret; if (!(drvc = di->priv)) { sr_err("Driver was not initialized."); return SR_ERR; } usb = sdi->conn; devc = sdi->priv; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; /* * Determine if a kernel driver is active on this interface and, if so, * detach it. */ if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE); if (ret < 0) { sr_err("Failed to detach kernel driver: %s.", libusb_error_name(ret)); return SR_ERR; } } if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } libusb_fill_control_transfer(devc->xfer_in, usb->devhdl, devc->xfer_buf_in, sl2_receive_transfer_in, sdi, USB_TIMEOUT); libusb_fill_control_transfer(devc->xfer_out, usb->devhdl, devc->xfer_buf_out, sl2_receive_transfer_out, sdi, USB_TIMEOUT); memset(buffer, 0, sizeof(buffer)); buffer[0] = CMD_RESET; if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) { sr_err("Device reset failed: %s.", libusb_error_name(ret)); return SR_ERR; } /* * Set the device to idle state. If the device is not in idle state it * possibly will reset itself after a few seconds without being used * and thereby close the connection. */ buffer[0] = CMD_IDLE; if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) { sr_err("Failed to set device in idle state: %s.", libusb_error_name(ret)); return SR_ERR; } sdi->status = SR_ST_ACTIVE; return SR_OK; }
static GSList *hw_scan(GSList *options) { struct drv_context *drvc; struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_usb_dev_inst *usb; struct sr_config *src; struct sr_probe *probe; libusb_device *dev; GSList *usb_devices, *devices, *l; int i; const char *conn; (void)options; drvc = di->priv; /* USB scan is always authoritative. */ clear_instances(); conn = NULL; for (l = options; l; l = l->next) { src = l->data; switch (src->key) { case SR_CONF_CONN: conn = src->value; break; } } if (!conn) conn = OSCI_VIDPID; devices = NULL; if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { for (l = usb_devices; l; l = l->next) { usb = l->data; if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, OSCI_VENDOR, OSCI_MODEL, OSCI_VERSION))) return NULL; sdi->driver = di; for (i = 0; probe_names[i]; i++) { if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, probe_names[i]))) return NULL; sdi->probes = g_slist_append(sdi->probes, probe); } if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) return NULL; sdi->priv = devc; devc->usb = usb; if (strcmp(conn, OSCI_VIDPID)) { if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) break; dev = libusb_get_device(usb->devhdl); if (ezusb_upload_firmware(dev, 0, OSCI_FIRMWARE) == SR_OK) /* Remember when the firmware on this device was updated */ devc->fw_updated = g_get_monotonic_time(); else sr_err("Firmware upload failed for device " "at bus %d address %d.", usb->bus, usb->address); } drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); } g_slist_free(usb_devices); } else g_slist_free_full(usb_devices, g_free); return devices; }
SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb, struct device_info *dev_info) { struct drv_context *drvc; uint8_t buffer[PACKET_LENGTH]; int ret; drvc = di->priv; if (!dev_info) return SR_ERR_ARG; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK) return SR_ERR; /* * Determine if a kernel driver is active on this interface and, if so, * detach it. */ if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) { ret = libusb_detach_kernel_driver(usb.devhdl, USB_INTERFACE); if (ret < 0) { sr_err("Failed to detach kernel driver: %s.", libusb_error_name(ret)); libusb_close(usb.devhdl); return SR_ERR; } } ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE); if (ret) { sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); libusb_close(usb.devhdl); return SR_ERR; } memset(buffer, 0, sizeof(buffer)); /* * Reset the device to ensure it is in a proper state to request the * device information. */ buffer[0] = CMD_RESET; if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { sr_err("Resetting of device failed: %s.", libusb_error_name(ret)); libusb_release_interface(usb.devhdl, USB_INTERFACE); libusb_close(usb.devhdl); return SR_ERR; } buffer[0] = CMD_INFO; if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { sr_err("Requesting of device information failed: %s.", libusb_error_name(ret)); libusb_release_interface(usb.devhdl, USB_INTERFACE); libusb_close(usb.devhdl); return SR_ERR; } if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) { sr_err("Receiving of device information failed: %s.", libusb_error_name(ret)); libusb_release_interface(usb.devhdl, USB_INTERFACE); libusb_close(usb.devhdl); return SR_ERR; } memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t)); dev_info->serial = GUINT32_FROM_LE(dev_info->serial); dev_info->fw_ver_major = buffer[5]; dev_info->fw_ver_minor = buffer[6]; buffer[0] = CMD_RESET; if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { sr_err("Device reset failed: %s.", libusb_error_name(ret)); libusb_release_interface(usb.devhdl, USB_INTERFACE); libusb_close(usb.devhdl); return SR_ERR; } /* * Set the device to idle state. If the device is not in idle state it * possibly will reset itself after a few seconds without being used * and thereby close the connection. */ buffer[0] = CMD_IDLE; if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { sr_err("Failed to set device in idle state: %s.", libusb_error_name(ret)); libusb_release_interface(usb.devhdl, USB_INTERFACE); libusb_close(usb.devhdl); return SR_ERR; } ret = libusb_release_interface(usb.devhdl, USB_INTERFACE); if (ret < 0) { sr_err("Failed to release interface: %s.", libusb_error_name(ret)); libusb_close(usb.devhdl); return SR_ERR; } libusb_close(usb.devhdl); return SR_OK; }