static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct sr_dev_inst *sdi; struct drv_context *drvc; struct sr_usb_dev_inst *usb; struct libusb_device_descriptor des; libusb_device **devlist; GSList *devices; char connection_id[64]; size_t i; int r; (void)options; drvc = di->context; devices = NULL; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { libusb_get_device_descriptor(devlist[i], &des); if (des.idVendor != LOGICSTUDIO16_VID) continue; usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); usb = NULL; switch (des.idProduct) { case LOGICSTUDIO16_PID_HAVE_FIRMWARE: usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); sdi = create_device(usb, SR_ST_INACTIVE, 0); break; case LOGICSTUDIO16_PID_LACK_FIRMWARE: r = ezusb_upload_firmware(drvc->sr_ctx, devlist[i], USB_CONFIGURATION, FX2_FIRMWARE); if (r != SR_OK) { /* * An error message has already been logged by * ezusb_upload_firmware(). */ continue; } /* * Put unknown as the address so that we know we still * need to get the proper address after the device * renumerates. */ usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), UNKNOWN_ADDRESS, NULL); sdi = create_device(usb, SR_ST_INITIALIZING, g_get_monotonic_time()); break; default: break; } /* Cannot handle this device? */ if (!usb) continue; sdi->connection_id = g_strdup(connection_id); devices = g_slist_append(devices, sdi); } libusb_free_device_list(devlist, 1); return std_scan_complete(di, devices); }
static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct sr_dev_inst *sdi; struct drv_context *drvc; struct dev_context *devc; const struct zp_model *prof; struct libusb_device_descriptor des; struct libusb_device_handle *hdl; libusb_device **devlist; GSList *devices; int ret, i, j; char serial_num[64], connection_id[64]; (void)options; drvc = di->context; devices = NULL; /* Find all ZEROPLUS analyzers and add them to device list. */ libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */ for (i = 0; devlist[i]; i++) { libusb_get_device_descriptor(devlist[i], &des); if ((ret = libusb_open(devlist[i], &hdl)) < 0) continue; if (des.iSerialNumber == 0) { serial_num[0] = '\0'; } else if ((ret = libusb_get_string_descriptor_ascii(hdl, des.iSerialNumber, (unsigned char *) serial_num, sizeof(serial_num))) < 0) { sr_warn("Failed to get serial number string descriptor: %s.", libusb_error_name(ret)); continue; } libusb_close(hdl); usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); prof = NULL; for (j = 0; j < zeroplus_models[j].vid; j++) { if (des.idVendor == zeroplus_models[j].vid && des.idProduct == zeroplus_models[j].pid) { prof = &zeroplus_models[j]; } } /* Skip if the device was not found. */ if (!prof) continue; sr_info("Found ZEROPLUS %s.", prof->model_name); /* Register the device with libsigrok. */ sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INACTIVE; sdi->vendor = g_strdup(VENDOR_NAME); sdi->model = g_strdup(prof->model_name); sdi->driver = di; sdi->serial_num = g_strdup(serial_num); sdi->connection_id = g_strdup(connection_id); /* Allocate memory for our private driver context. */ devc = g_malloc0(sizeof(struct dev_context)); sdi->priv = devc; devc->prof = prof; devc->num_channels = prof->channels; #ifdef ZP_EXPERIMENTAL devc->max_sample_depth = 128 * 1024; devc->max_samplerate = 200; #else devc->max_sample_depth = prof->sample_depth * 1024; devc->max_samplerate = prof->max_sampling_freq; #endif devc->max_samplerate *= SR_MHZ(1); devc->memory_size = MEMORY_SIZE_8K; // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES); /* Fill in channellist according to this device's profile. */ for (j = 0; j < devc->num_channels; j++) sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]); devices = g_slist_append(devices, sdi); drvc->instances = g_slist_append(drvc->instances, sdi); sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new( libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); } libusb_free_device_list(devlist, 1); return devices; }
static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; struct dev_context *devc; struct drv_context *drvc; struct sr_usb_dev_inst *usb; libusb_device **devlist, *dev; int device_count, ret, i; char connection_id[64]; drvc = di->context; usb = sdi->conn; if (!(devc = sdi->priv)) { sr_err("%s: sdi->priv was NULL", __func__); return SR_ERR_ARG; } device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); if (device_count < 0) { sr_err("Failed to retrieve device list."); return SR_ERR; } dev = NULL; for (i = 0; i < device_count; i++) { usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); if (!strcmp(sdi->connection_id, connection_id)) { dev = devlist[i]; break; } } if (!dev) { sr_err("Device on %d.%d (logical) / %s (physical) disappeared!", usb->bus, usb->address, sdi->connection_id); return SR_ERR; } if (!(ret = libusb_open(dev, &(usb->devhdl)))) { sdi->status = SR_ST_ACTIVE; sr_info("Opened device on %d.%d (logical) / %s (physical) interface %d.", usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); } else { sr_err("Failed to open device: %s.", libusb_error_name(ret)); return SR_ERR; } ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION); if (ret < 0) { sr_err("Unable to set USB configuration %d: %s.", USB_CONFIGURATION, libusb_error_name(ret)); return SR_ERR; } ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); if (ret != 0) { sr_err("Unable to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } /* Set default configuration after power on. */ if (analyzer_read_status(usb->devhdl) == 0) analyzer_configure(usb->devhdl); analyzer_reset(usb->devhdl); analyzer_initialize(usb->devhdl); //analyzer_set_memory_size(MEMORY_SIZE_512K); // analyzer_set_freq(g_freq, g_freq_scale); analyzer_set_trigger_count(1); // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger) // * get_memory_size(g_memory_size)) / 100) >> 2); #if 0 if (g_double_mode == 1) analyzer_set_compression(COMPRESSION_DOUBLE); else if (g_compression == 1) analyzer_set_compression(COMPRESSION_ENABLE); else #endif analyzer_set_compression(COMPRESSION_NONE); if (devc->cur_samplerate == 0) { /* Samplerate hasn't been set. Default to 1MHz. */ analyzer_set_freq(1, FREQ_SCALE_MHZ); devc->cur_samplerate = SR_MHZ(1); } if (devc->cur_threshold == 0) set_voltage_threshold(devc, 1.5); return SR_OK; }
SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di) { libusb_device **devlist; struct sr_usb_dev_inst *usb; struct libusb_device_descriptor des; struct dev_context *devc; struct drv_context *drvc; struct version_info vi; int ret = SR_ERR, i, device_count; uint8_t revid; char connection_id[64]; drvc = di->context; devc = sdi->priv; usb = sdi->conn; device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); if (device_count < 0) { sr_err("Failed to get device list: %s.", libusb_error_name(device_count)); return SR_ERR; } for (i = 0; i < device_count; i++) { libusb_get_device_descriptor(devlist[i], &des); if (des.idVendor != devc->profile->vid || des.idProduct != devc->profile->pid) continue; if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) { /* * Check device by its physical USB bus/port address. */ usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); if (strcmp(sdi->connection_id, connection_id)) /* This is not the one. */ continue; } if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { if (usb->address == 0xff) /* * First time we touch this device after FW * upload, so we don't know the address yet. */ usb->address = libusb_get_device_address(devlist[i]); } else { sr_err("Failed to open device: %s.", libusb_error_name(ret)); ret = SR_ERR; break; } if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { 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)); ret = SR_ERR; break; } } } ret = command_get_fw_version(usb->devhdl, &vi); if (ret != SR_OK) { sr_err("Failed to get firmware version."); break; } ret = command_get_revid_version(sdi, &revid); if (ret != SR_OK) { sr_err("Failed to get REVID."); break; } /* * Changes in major version mean incompatible/API changes, so * bail out if we encounter an incompatible version. * Different minor versions are OK, they should be compatible. */ if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) { sr_err("Expected firmware version %d.x, " "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR, vi.major, vi.minor); break; } sr_info("Opened device on %d.%d (logical) / %s (physical), " "interface %d, firmware %d.%d.", usb->bus, usb->address, connection_id, USB_INTERFACE, vi.major, vi.minor); sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.", revid, (revid != 1) ? " (FX2)" : "A (FX2LP)"); ret = SR_OK; break; } libusb_free_device_list(devlist, 1); return ret; }
static GSList *scan(struct sr_dev_driver *di, 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; const struct dso_profile *prof; GSList *l, *devices, *conn_devices; struct libusb_device_descriptor des; libusb_device **devlist; int ret, i, j; const char *conn; char connection_id[64]; drvc = di->context; devices = 0; conn = NULL; for (l = options; l; l = l->next) { src = l->data; if (src->key == SR_CONF_CONN) { conn = g_variant_get_string(src->data, NULL); break; } } if (conn) conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); else conn_devices = NULL; /* Find all Hantek DSO devices and upload firmware to all of them. */ libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { if (conn) { usb = NULL; for (l = conn_devices; l; l = l->next) { usb = l->data; if (usb->bus == libusb_get_bus_number(devlist[i]) && usb->address == libusb_get_device_address(devlist[i])) break; } if (!l) /* This device matched none of the ones that * matched the conn specification. */ continue; } if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { sr_err("Failed to get device descriptor: %s.", libusb_error_name(ret)); continue; } usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); prof = NULL; for (j = 0; dev_profiles[j].orig_vid; j++) { if (des.idVendor == dev_profiles[j].orig_vid && des.idProduct == dev_profiles[j].orig_pid) { /* Device matches the pre-firmware profile. */ prof = &dev_profiles[j]; sr_dbg("Found a %s %s.", prof->vendor, prof->model); sdi = dso_dev_new(prof); sdi->connection_id = g_strdup(connection_id); devices = g_slist_append(devices, sdi); devc = sdi->priv; if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, prof->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"); /* Dummy USB address of 0xff will get overwritten later. */ sdi->conn = sr_usb_dev_inst_new( libusb_get_bus_number(devlist[i]), 0xff, NULL); break; } else if (des.idVendor == dev_profiles[j].fw_vid && des.idProduct == dev_profiles[j].fw_pid) { /* Device matches the post-firmware profile. */ prof = &dev_profiles[j]; sr_dbg("Found a %s %s.", prof->vendor, prof->model); sdi = dso_dev_new(prof); sdi->connection_id = g_strdup(connection_id); sdi->status = SR_ST_INACTIVE; devices = g_slist_append(devices, sdi); sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new( libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); break; } } if (!prof) /* not a supported VID/PID */ continue; } libusb_free_device_list(devlist, 1); return devices; }
static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct drv_context *drvc; struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_usb_dev_inst *usb; struct sr_channel *ch; struct sr_channel_group *cg; struct sr_config *src; const struct fx2lafw_profile *prof; GSList *l, *devices, *conn_devices; gboolean has_firmware; struct libusb_device_descriptor des; libusb_device **devlist; struct libusb_device_handle *hdl; int ret, i, j; int num_logic_channels = 0, num_analog_channels = 0; const char *conn; char manufacturer[64], product[64], serial_num[64], connection_id[64]; char channel_name[16]; drvc = di->context; conn = NULL; for (l = options; l; l = l->next) { src = l->data; switch (src->key) { case SR_CONF_CONN: conn = g_variant_get_string(src->data, NULL); break; } } if (conn) conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); else conn_devices = NULL; /* Find all fx2lafw compatible devices and upload firmware to them. */ devices = NULL; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { if (conn) { usb = NULL; for (l = conn_devices; l; l = l->next) { usb = l->data; if (usb->bus == libusb_get_bus_number(devlist[i]) && usb->address == libusb_get_device_address(devlist[i])) break; } if (!l) /* This device matched none of the ones that * matched the conn specification. */ continue; } libusb_get_device_descriptor( devlist[i], &des); if (!is_plausible(&des)) continue; if ((ret = libusb_open(devlist[i], &hdl)) < 0) { sr_warn("Failed to open potential device with " "VID:PID %04x:%04x: %s.", des.idVendor, des.idProduct, libusb_error_name(ret)); continue; } if (des.iManufacturer == 0) { manufacturer[0] = '\0'; } else if ((ret = libusb_get_string_descriptor_ascii(hdl, des.iManufacturer, (unsigned char *) manufacturer, sizeof(manufacturer))) < 0) { sr_warn("Failed to get manufacturer string descriptor: %s.", libusb_error_name(ret)); continue; } if (des.iProduct == 0) { product[0] = '\0'; } else if ((ret = libusb_get_string_descriptor_ascii(hdl, des.iProduct, (unsigned char *) product, sizeof(product))) < 0) { sr_warn("Failed to get product string descriptor: %s.", libusb_error_name(ret)); continue; } if (des.iSerialNumber == 0) { serial_num[0] = '\0'; } else if ((ret = libusb_get_string_descriptor_ascii(hdl, des.iSerialNumber, (unsigned char *) serial_num, sizeof(serial_num))) < 0) { sr_warn("Failed to get serial number string descriptor: %s.", libusb_error_name(ret)); continue; } libusb_close(hdl); if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) continue; prof = NULL; for (j = 0; supported_fx2[j].vid; j++) { if (des.idVendor == supported_fx2[j].vid && des.idProduct == supported_fx2[j].pid && (!supported_fx2[j].usb_manufacturer || !strcmp(manufacturer, supported_fx2[j].usb_manufacturer)) && (!supported_fx2[j].usb_product || !strcmp(product, supported_fx2[j].usb_product))) { prof = &supported_fx2[j]; break; } } if (!prof) continue; sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INITIALIZING; sdi->vendor = g_strdup(prof->vendor); sdi->model = g_strdup(prof->model); sdi->version = g_strdup(prof->model_version); sdi->serial_num = g_strdup(serial_num); sdi->connection_id = g_strdup(connection_id); /* Fill in channellist according to this device's profile. */ num_logic_channels = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8; num_analog_channels = prof->dev_caps & DEV_CAPS_AX_ANALOG ? 1 : 0; /* Logic channels, all in one channel group. */ cg = g_malloc0(sizeof(struct sr_channel_group)); cg->name = g_strdup("Logic"); for (j = 0; j < num_logic_channels; j++) { sprintf(channel_name, "D%d", j); ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_name); cg->channels = g_slist_append(cg->channels, ch); } sdi->channel_groups = g_slist_append(NULL, cg); for (j = 0; j < num_analog_channels; j++) { snprintf(channel_name, 16, "A%d", j); ch = sr_channel_new(sdi, j + num_logic_channels, SR_CHANNEL_ANALOG, TRUE, channel_name); /* Every analog channel gets its own channel group. */ cg = g_malloc0(sizeof(struct sr_channel_group)); cg->name = g_strdup(channel_name); cg->channels = g_slist_append(NULL, ch); sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } devc = fx2lafw_dev_new(); devc->profile = prof; sdi->priv = devc; devices = g_slist_append(devices, sdi); devc->samplerates = samplerates; devc->num_samplerates = ARRAY_SIZE(samplerates); has_firmware = usb_match_manuf_prod(devlist[i], "sigrok", "fx2lafw"); if (has_firmware) { /* Already has the firmware, so fix the new address. */ sr_dbg("Found an fx2lafw device."); sdi->status = SR_ST_INACTIVE; sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); } else { if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i], USB_CONFIGURATION, prof->firmware) == SR_OK) { /* Store when this device's FW was updated. */ devc->fw_updated = g_get_monotonic_time(); } else { sr_err("Firmware upload failed for " "device %d.%d (logical), name %s.", libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), prof->firmware); } sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), 0xff, NULL); } } libusb_free_device_list(devlist, 1); g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); return std_scan_complete(di, devices); }
/** * Queries a device instances' connection identifier. * * @param sdi Device instance to use. Must not be NULL. * * @return A copy of the connection id string or NULL. The caller is responsible * for g_free()ing the string when it is no longer needed. */ SR_API const char *sr_dev_inst_connid_get(const struct sr_dev_inst *sdi) { #ifdef HAVE_LIBUSB_1_0 struct drv_context *drvc; int r, cnt, i, a, b; char connection_id[64]; struct sr_usb_dev_inst *usb; struct libusb_device **devlist; struct libusb_device_descriptor des; #endif if (!sdi) return NULL; #ifdef HAVE_LIBSERIALPORT struct sr_serial_dev_inst *serial; if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_SERIAL)) { /* connection_id isn't populated, let's do that here. */ serial = sdi->conn; ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(serial->port); } #endif #ifdef HAVE_LIBUSB_1_0 if ((!sdi->connection_id) && (sdi->inst_type == SR_INST_USB)) { /* connection_id isn't populated, let's do that here. */ drvc = sdi->driver->priv; usb = sdi->conn; if ((cnt = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist)) < 0) { sr_err("Failed to retrieve device list: %s.", libusb_error_name(cnt)); return NULL; } for (i = 0; i < cnt; i++) { if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { sr_err("Failed to get device descriptor: %s.", libusb_error_name(r)); continue; } /* Find the USB device by the logical address we know. */ b = libusb_get_bus_number(devlist[i]); a = libusb_get_device_address(devlist[i]); if (b != usb->bus || a != usb->address) continue; usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); ((struct sr_dev_inst *)sdi)->connection_id = g_strdup(connection_id); break; } libusb_free_device_list(devlist, 1); } #endif return sdi->connection_id; }
static int logic16_dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di; libusb_device **devlist; struct sr_usb_dev_inst *usb; struct libusb_device_descriptor des; struct drv_context *drvc; int ret, i, device_count; char connection_id[64]; di = sdi->driver; drvc = di->priv; usb = sdi->conn; if (sdi->status == SR_ST_ACTIVE) /* Device is already in use. */ return SR_ERR; device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); if (device_count < 0) { sr_err("Failed to get device list: %s.", libusb_error_name(device_count)); return SR_ERR; } for (i = 0; i < device_count; i++) { if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { sr_err("Failed to get device descriptor: %s.", libusb_error_name(ret)); continue; } if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) continue; if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) { /* * Check device by its physical USB bus/port address. */ usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); if (strcmp(sdi->connection_id, connection_id)) /* This is not the one. */ continue; } if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { if (usb->address == 0xff) /* * First time we touch this device after FW * upload, so we don't know the address yet. */ usb->address = libusb_get_device_address(devlist[i]); } else { sr_err("Failed to open device: %s.", libusb_error_name(ret)); break; } ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); if (ret == LIBUSB_ERROR_BUSY) { sr_err("Unable to claim USB interface. Another " "program or driver has already claimed it."); break; } else if (ret == LIBUSB_ERROR_NO_DEVICE) { sr_err("Device has been disconnected."); break; } else if (ret != 0) { sr_err("Unable to claim interface: %s.", libusb_error_name(ret)); break; } if ((ret = logic16_init_device(sdi)) != SR_OK) { sr_err("Failed to init device."); break; } sdi->status = SR_ST_ACTIVE; sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.", usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); break; } libusb_free_device_list(devlist, 1); if (sdi->status != SR_ST_ACTIVE) { if (usb->devhdl) { libusb_release_interface(usb->devhdl, USB_INTERFACE); libusb_close(usb->devhdl); usb->devhdl = NULL; } return SR_ERR; } return SR_OK; }
static GSList *scan(struct sr_dev_driver *di, 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; GSList *l, *devices, *conn_devices; struct libusb_device_descriptor des; libusb_device **devlist; int ret; unsigned int i, j; const char *conn; char connection_id[64]; drvc = di->priv; conn = NULL; for (l = options; l; l = l->next) { src = l->data; switch (src->key) { case SR_CONF_CONN: conn = g_variant_get_string(src->data, NULL); break; } } if (conn) conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); else conn_devices = NULL; /* Find all Logic16 devices and upload firmware to them. */ devices = NULL; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { if (conn) { usb = NULL; for (l = conn_devices; l; l = l->next) { usb = l->data; if (usb->bus == libusb_get_bus_number(devlist[i]) && usb->address == libusb_get_device_address(devlist[i])) break; } if (!l) /* This device matched none of the ones that * matched the conn specification. */ continue; } if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { sr_warn("Failed to get device descriptor: %s.", libusb_error_name(ret)); continue; } usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID) continue; sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INITIALIZING; sdi->vendor = g_strdup("Saleae"); sdi->model = g_strdup("Logic16"); sdi->driver = di; sdi->connection_id = g_strdup(connection_id); for (j = 0; j < ARRAY_SIZE(channel_names); j++) sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]); devc = g_malloc0(sizeof(struct dev_context)); devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V; sdi->priv = devc; drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); if (check_conf_profile(devlist[i])) { /* Already has the firmware, so fix the new address. */ sr_dbg("Found a Logic16 device."); sdi->status = SR_ST_INACTIVE; sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new( libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); } else { if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FX2_FIRMWARE) == SR_OK) /* Store when this device's FW was updated. */ devc->fw_updated = g_get_monotonic_time(); else sr_err("Firmware upload failed."); sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new( libusb_get_bus_number(devlist[i]), 0xff, NULL); } } libusb_free_device_list(devlist, 1); g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); return devices; }
static int open_device(struct sr_dev_inst *sdi) { struct drv_context *drvc; struct sr_usb_dev_inst *usb; struct libusb_device_descriptor des; libusb_device **devlist; char connection_id[64]; bool is_opened; size_t i; int r; if (sdi->status == SR_ST_ACTIVE) return SR_ERR; drvc = sdi->driver->context; usb = sdi->conn; is_opened = FALSE; libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { libusb_get_device_descriptor(devlist[i], &des); if (des.idVendor != LOGICSTUDIO16_VID || des.idProduct != LOGICSTUDIO16_PID_HAVE_FIRMWARE) continue; usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); /* * Check if this device is the same one that we associated * with this sdi in scan() and bail if it isn't. */ if (strcmp(sdi->connection_id, connection_id)) continue; r = libusb_open(devlist[i], &usb->devhdl); if (r) { sr_err("Failed to open device: %s.", libusb_error_name(r)); break; } /* Fix up address after firmware upload. */ if (usb->address == UNKNOWN_ADDRESS) usb->address = libusb_get_device_address(devlist[i]); is_opened = TRUE; break; } libusb_free_device_list(devlist, 1); if (!is_opened) return SR_ERR; if ((r = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) { sr_err("Failed to claim interface: %s.", libusb_error_name(r)); return SR_ERR; } sdi->status = SR_ST_ACTIVE; return SR_OK; }