+ return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
+}
+
+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 hantek_6xxx_profile *prof;
+ GSList *l, *devices, *conn_devices;
+ struct libusb_device_descriptor des;
+ libusb_device **devlist;
+ int 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 60xx 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;
+ }
+
+ libusb_get_device_descriptor(devlist[i], &des);
+
+ if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+ continue;
+
+ 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 = hantek_6xxx_dev_new(prof);
+ sdi->connection_id = g_strdup(connection_id);
+ devices = g_slist_append(devices, sdi);
+ devc = sdi->priv;
+ if (ezusb_upload_firmware(drvc->sr_ctx, 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, name %s.", prof->firmware);
+ }
+ /* 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
+ && des.bcdDevice == dev_profiles[j].fw_prod_ver) {
+ /* Device matches the post-firmware profile. */
+ prof = &dev_profiles[j];
+ sr_dbg("Found a %s %s.", prof->vendor, prof->model);
+ sdi = hantek_6xxx_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 std_scan_complete(di, devices);