static void *hw_get_device_info(int device_index, int device_info_id) { struct sr_device_instance *sdi; struct fx2_device *fx2; void *info = NULL; if (!(sdi = sr_get_device_instance(device_instances, device_index))) return NULL; fx2 = sdi->priv; switch (device_info_id) { case SR_DI_INSTANCE: info = sdi; break; case SR_DI_NUM_PROBES: info = GINT_TO_POINTER(fx2->profile->num_probes); break; case SR_DI_SAMPLERATES: info = &samplerates; break; case SR_DI_TRIGGER_TYPES: info = TRIGGER_TYPES; break; case SR_DI_CUR_SAMPLERATE: info = &fx2->cur_samplerate; break; } return info; }
static int hw_set_configuration(int device_index, int capability, void *value) { struct sr_device_instance *sdi; struct fx2_device *fx2; int ret; uint64_t *tmp_u64; if (!(sdi = sr_get_device_instance(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; if (capability == SR_HWCAP_SAMPLERATE) { tmp_u64 = value; ret = set_configuration_samplerate(sdi, *tmp_u64); } else if (capability == SR_HWCAP_PROBECONFIG) { ret = configure_probes(fx2, (GSList *) value); } else if (capability == SR_HWCAP_LIMIT_SAMPLES) { tmp_u64 = value; fx2->limit_samples = *tmp_u64; ret = SR_OK; } else { ret = SR_ERR; } return ret; }
static void *hw_get_device_info(int device_index, int device_info_id) { struct sr_device_instance *sdi; void *info = NULL; if (!(sdi = sr_get_device_instance(device_instances, device_index))) { sr_err("demo: %s: sdi was NULL", __func__); return NULL; } switch (device_info_id) { case SR_DI_INSTANCE: info = sdi; break; case SR_DI_NUM_PROBES: info = GINT_TO_POINTER(NUM_PROBES); break; case SR_DI_SAMPLERATES: info = &samplerates; break; case SR_DI_CUR_SAMPLERATE: info = &cur_samplerate; break; case SR_DI_PATTERNMODES: info = &pattern_strings; break; } return info; }
static int hw_get_status(int device_index) { struct sr_device_instance *sdi; sdi = sr_get_device_instance(device_instances, device_index); if (sdi) return sdi->status; else return SR_ST_NOT_FOUND; }
static int hw_opendev(int device_index) { GTimeVal cur_time; struct sr_device_instance *sdi; struct fx2_device *fx2; int timediff, err; if (!(sdi = sr_get_device_instance(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; /* * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms * for the FX2 to renumerate */ err = 0; if (GTV_TO_MSEC(fx2->fw_updated) > 0) { sr_info("saleae: waiting for device to reset"); /* takes at least 300ms for the FX2 to be gone from the USB bus */ g_usleep(300*1000); timediff = 0; while (timediff < MAX_RENUM_DELAY) { if ((err = sl_open_device(device_index)) == SR_OK) break; g_usleep(100*1000); g_get_current_time(&cur_time); timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fx2->fw_updated); } sr_info("saleae: device came back after %d ms", timediff); } else { err = sl_open_device(device_index); } if (err != SR_OK) { sr_warn("unable to open device"); return SR_ERR; } fx2 = sdi->priv; err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE); if (err != 0) { sr_warn("Unable to claim interface: %d", err); return SR_ERR; } if (fx2->cur_samplerate == 0) { /* Samplerate hasn't been set; default to the slowest one. */ if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE, &supported_samplerates[0]) == SR_ERR) return SR_ERR; } return SR_OK; }
static struct session_vdevice *get_vdevice_by_index(int device_index) { struct sr_device_instance *sdi; struct session_vdevice *vdevice; if (!(sdi = sr_get_device_instance(device_instances, device_index))) return NULL; vdevice = sdi->priv; return vdevice; }
static int hw_closedev(int device_index) { struct sr_device_instance *sdi; if (!(sdi = sr_get_device_instance(device_instances, device_index))) { sr_err("logic: %s: sdi was NULL", __func__); return SR_ERR; /* TODO: SR_ERR_ARG? */ } /* TODO */ close_device(sdi); return SR_OK; }
static int hw_start_acquisition(int device_index, gpointer session_data) { struct sr_device_instance *sdi; struct sr_datafeed_packet *packet; struct sr_datafeed_header *header; struct fx2_device *fx2; struct libusb_transfer *transfer; const struct libusb_pollfd **lupfd; int size, i; unsigned char *buf; if (!(sdi = sr_get_device_instance(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; fx2->session_data = session_data; if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { sr_err("saleae: %s: packet malloc failed", __func__); return SR_ERR_MALLOC; } if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { sr_err("saleae: %s: header malloc failed", __func__); return SR_ERR_MALLOC; } /* Start with 2K transfer, subsequently increased to 4K. */ size = 2048; for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) { if (!(buf = g_try_malloc(size))) { sr_err("saleae: %s: buf malloc failed", __func__); return SR_ERR_MALLOC; } transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl, 2 | LIBUSB_ENDPOINT_IN, buf, size, receive_transfer, fx2, 40); if (libusb_submit_transfer(transfer) != 0) { /* TODO: Free them all. */ libusb_free_transfer(transfer); g_free(buf); return SR_ERR; } size = 4096; } lupfd = libusb_get_pollfds(usb_context); for (i = 0; lupfd[i]; i++) sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data, NULL); free(lupfd); packet->type = SR_DF_HEADER; packet->payload = header; header->feed_version = 1; gettimeofday(&header->starttime, NULL); header->samplerate = fx2->cur_samplerate; header->num_logic_probes = fx2->profile->num_probes; header->num_analog_probes = 0; sr_session_bus(session_data, packet); g_free(header); g_free(packet); return SR_OK; }
static int sl_open_device(int device_index) { libusb_device **devlist; struct libusb_device_descriptor des; struct sr_device_instance *sdi; struct fx2_device *fx2; int err, skip, i; if (!(sdi = sr_get_device_instance(device_instances, device_index))) return SR_ERR; fx2 = sdi->priv; if (sdi->status == SR_ST_ACTIVE) /* already in use */ return SR_ERR; skip = 0; libusb_get_device_list(usb_context, &devlist); for (i = 0; devlist[i]; i++) { if ((err = libusb_get_device_descriptor(devlist[i], &des))) { sr_warn("failed to get device descriptor: %d", err); continue; } if (des.idVendor != fx2->profile->fw_vid || des.idProduct != fx2->profile->fw_pid) continue; if (sdi->status == SR_ST_INITIALIZING) { if (skip != device_index) { /* Skip devices of this type that aren't the one we want. */ skip += 1; continue; } } else if (sdi->status == SR_ST_INACTIVE) { /* * This device is fully enumerated, so we need to find this * device by vendor, product, bus and address. */ if (libusb_get_bus_number(devlist[i]) != sdi->usb->bus || libusb_get_device_address(devlist[i]) != sdi->usb->address) /* this is not the one */ continue; } if (!(err = libusb_open(devlist[i], &sdi->usb->devhdl))) { if (sdi->usb->address == 0xff) /* * first time we touch this device after firmware upload, * so we don't know the address yet. */ sdi->usb->address = libusb_get_device_address(devlist[i]); sdi->status = SR_ST_ACTIVE; sr_info("saleae: opened device %d on %d.%d interface %d", sdi->index, sdi->usb->bus, sdi->usb->address, USB_INTERFACE); } else { sr_warn("failed to open device: %d", err); } /* if we made it here, we handled the device one way or another */ break; } libusb_free_device_list(devlist, 1); if (sdi->status != SR_ST_ACTIVE) return SR_ERR; return SR_OK; }