2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 #define LOGIC16_VID 0x21a9
29 #define LOGIC16_PID 0x1001
32 #define USB_INTERFACE 0
33 #define USB_CONFIGURATION 1
34 #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw"
36 #define MAX_RENUM_DELAY_MS 3000
39 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
40 static struct sr_dev_driver *di = &saleae_logic16_driver_info;
42 static const char *probe_names[NUM_PROBES + 1] = {
43 "0", "1", "2", "3", "4", "5", "6", "7", "8",
44 "9", "10", "11", "12", "13", "14", "15",
48 static int init(struct sr_context *sr_ctx)
50 return std_init(sr_ctx, di, LOG_PREFIX);
53 static gboolean check_conf_profile(libusb_device *dev)
55 struct libusb_device_descriptor des;
56 struct libusb_device_handle *hdl;
58 unsigned char strdesc[64];
63 /* Assume the FW has not been loaded, unless proven wrong. */
64 if (libusb_get_device_descriptor(dev, &des) != 0)
67 if (libusb_open(dev, &hdl) != 0)
70 if (libusb_get_string_descriptor_ascii(hdl,
71 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
73 if (strcmp((const char *)strdesc, "Saleae LLC"))
76 if (libusb_get_string_descriptor_ascii(hdl,
77 des.iProduct, strdesc, sizeof(strdesc)) < 0)
79 if (strcmp((const char *)strdesc, "Logic S/16"))
82 /* If we made it here, it must be a configured Logic16. */
91 static GSList *scan(GSList *options)
93 struct drv_context *drvc;
94 struct dev_context *devc;
95 struct sr_dev_inst *sdi;
96 struct sr_usb_dev_inst *usb;
97 struct sr_probe *probe;
98 struct sr_config *src;
99 GSList *l, *devices, *conn_devices;
100 struct libusb_device_descriptor des;
101 libusb_device **devlist;
102 int devcnt, ret, i, j;
108 for (l = options; l; l = l->next) {
112 conn = g_variant_get_string(src->data, NULL);
117 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
121 /* Find all Logic16 devices and upload firmware to them. */
123 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
124 for (i = 0; devlist[i]; i++) {
127 for (l = conn_devices; l; l = l->next) {
129 if (usb->bus == libusb_get_bus_number(devlist[i])
130 && usb->address == libusb_get_device_address(devlist[i]))
134 /* This device matched none of the ones that
135 * matched the conn specification. */
139 if ((ret = libusb_get_device_descriptor( devlist[i], &des)) != 0) {
140 sr_warn("Failed to get device descriptor: %s.",
141 libusb_error_name(ret));
145 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
148 devcnt = g_slist_length(drvc->instances);
149 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
150 "Saleae", "Logic16", NULL);
155 for (j = 0; probe_names[j]; j++) {
156 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
159 sdi->probes = g_slist_append(sdi->probes, probe);
162 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
165 drvc->instances = g_slist_append(drvc->instances, sdi);
166 devices = g_slist_append(devices, sdi);
168 if (check_conf_profile(devlist[i])) {
169 /* Already has the firmware, so fix the new address. */
170 sr_dbg("Found a Logic16 device.");
171 sdi->status = SR_ST_INACTIVE;
172 sdi->inst_type = SR_INST_USB;
173 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
174 libusb_get_device_address(devlist[i]), NULL);
176 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
177 FX2_FIRMWARE) == SR_OK)
178 /* Store when this device's FW was updated. */
179 devc->fw_updated = g_get_monotonic_time();
181 sr_err("Firmware upload failed for "
182 "device %d.", devcnt);
183 sdi->inst_type = SR_INST_USB;
184 sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
188 libusb_free_device_list(devlist, 1);
189 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
194 static GSList *dev_list(void)
196 struct drv_context *drvc;
200 return drvc->instances;
203 static int dev_clear(void)
205 return std_dev_clear(di, NULL);
208 static int logic16_dev_open(struct sr_dev_inst *sdi)
210 libusb_device **devlist;
211 struct sr_usb_dev_inst *usb;
212 struct libusb_device_descriptor des;
213 struct dev_context *devc;
214 struct drv_context *drvc;
215 int ret, skip, i, device_count;
221 if (sdi->status == SR_ST_ACTIVE)
222 /* Device is already in use. */
226 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
227 if (device_count < 0) {
228 sr_err("Failed to get device list: %s.",
229 libusb_error_name(device_count));
233 for (i = 0; i < device_count; i++) {
234 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
235 sr_err("Failed to get device descriptor: %s.",
236 libusb_error_name(ret));
240 if (des.idVendor != LOGIC16_VID
241 || des.idProduct != LOGIC16_PID)
244 if (sdi->status == SR_ST_INITIALIZING) {
245 if (skip != sdi->index) {
246 /* Skip devices of this type that aren't the one we want. */
250 } else if (sdi->status == SR_ST_INACTIVE) {
252 * This device is fully enumerated, so we need to find
253 * this device by vendor, product, bus and address.
255 if (libusb_get_bus_number(devlist[i]) != usb->bus
256 || libusb_get_device_address(devlist[i]) != usb->address)
257 /* This is not the one. */
261 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
262 if (usb->address == 0xff)
264 * First time we touch this device after FW
265 * upload, so we don't know the address yet.
267 usb->address = libusb_get_device_address(devlist[i]);
269 sr_err("Failed to open device: %s.",
270 libusb_error_name(ret));
274 sdi->status = SR_ST_ACTIVE;
275 sr_info("Opened device %d on %d.%d, "
277 sdi->index, usb->bus, usb->address,
282 libusb_free_device_list(devlist, 1);
284 if (sdi->status != SR_ST_ACTIVE)
290 static int dev_open(struct sr_dev_inst *sdi)
292 struct sr_usb_dev_inst *usb;
293 struct dev_context *devc;
295 int64_t timediff_us, timediff_ms;
301 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
302 * milliseconds for the FX2 to renumerate.
305 if (devc->fw_updated > 0) {
306 sr_info("Waiting for device to reset.");
307 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
308 g_usleep(300 * 1000);
310 while (timediff_ms < MAX_RENUM_DELAY_MS) {
311 if ((ret = logic16_dev_open(sdi)) == SR_OK)
313 g_usleep(100 * 1000);
315 timediff_us = g_get_monotonic_time() - devc->fw_updated;
316 timediff_ms = timediff_us / 1000;
317 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
320 sr_err("Device failed to renumerate.");
323 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
325 sr_info("Firmware upload was not needed.");
326 ret = logic16_dev_open(sdi);
330 sr_err("Unable to open device.");
334 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
337 case LIBUSB_ERROR_BUSY:
338 sr_err("Unable to claim USB interface. Another "
339 "program or driver has already claimed it.");
341 case LIBUSB_ERROR_NO_DEVICE:
342 sr_err("Device has been disconnected.");
345 sr_err("Unable to claim interface: %s.",
346 libusb_error_name(ret));
353 if (devc->cur_samplerate == 0) {
354 /* Samplerate hasn't been set; default to the slowest one. */
355 devc->cur_samplerate = 500000;
361 static int dev_close(struct sr_dev_inst *sdi)
363 struct sr_usb_dev_inst *usb;
366 if (usb->devhdl == NULL)
369 sr_info("Closing device %d on %d.%d interface %d.",
370 sdi->index, usb->bus, usb->address, USB_INTERFACE);
371 libusb_release_interface(usb->devhdl, USB_INTERFACE);
372 libusb_close(usb->devhdl);
374 sdi->status = SR_ST_INACTIVE;
379 static int cleanup(void)
382 struct drv_context *drvc;
384 if (!(drvc = di->priv))
385 /* Can get called on an unused driver, doesn't matter. */
395 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
412 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
418 if (sdi->status != SR_ST_ACTIVE)
419 return SR_ERR_DEV_CLOSED;
431 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
448 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
454 if (sdi->status != SR_ST_ACTIVE)
455 return SR_ERR_DEV_CLOSED;
457 /* TODO: configure hardware, reset acquisition state, set up
458 * callbacks and send header packet. */
463 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
467 if (sdi->status != SR_ST_ACTIVE)
468 return SR_ERR_DEV_CLOSED;
470 /* TODO: stop acquisition. */
475 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
476 .name = "saleae-logic16",
477 .longname = "Saleae Logic16",
482 .dev_list = dev_list,
483 .dev_clear = dev_clear,
484 .config_get = config_get,
485 .config_set = config_set,
486 .config_list = config_list,
487 .dev_open = dev_open,
488 .dev_close = dev_close,
489 .dev_acquisition_start = dev_acquisition_start,
490 .dev_acquisition_stop = dev_acquisition_stop,