2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Tilman Sauerbeck <tilman@code-monkey.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #define LOGICSTUDIO16_VID 0x05ff
28 #define LOGICSTUDIO16_PID_LACK_FIRMWARE 0xa001
29 #define LOGICSTUDIO16_PID_HAVE_FIRMWARE 0xa002
31 #define USB_INTERFACE 0
32 #define USB_CONFIGURATION 0
33 #define FX2_FIRMWARE "lecroy-logicstudio16-fx2lp.fw"
35 #define UNKNOWN_ADDRESS 0xff
36 #define MAX_RENUM_DELAY_MS 3000
38 static const uint32_t devopts[] = {
39 SR_CONF_LOGIC_ANALYZER,
40 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
45 static const int32_t trigger_matches[] = {
53 static const uint64_t samplerates[] = {
74 SR_PRIV struct sr_dev_driver lecroy_logicstudio_driver_info;
76 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
78 return std_init(sr_ctx, di, LOG_PREFIX);
81 static struct sr_dev_inst *create_device(struct sr_dev_driver *di,
82 struct sr_usb_dev_inst *usb, enum sr_dev_inst_status status,
85 struct sr_dev_inst *sdi;
86 struct dev_context *devc;
90 sdi = g_malloc0(sizeof(struct sr_dev_inst));
92 sdi->vendor = g_strdup("LeCroy");
93 sdi->model = g_strdup("LogicStudio16");
95 sdi->inst_type = SR_INST_USB;
98 for (i = 0; i < 16; i++) {
99 snprintf(channel_name, sizeof(channel_name), "D%i", i);
100 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
103 devc = g_malloc0(sizeof(struct dev_context));
107 devc->fw_updated = fw_updated;
108 devc->capture_ratio = 50;
110 lls_set_samplerate(sdi, SR_MHZ(500));
115 static GSList *scan(struct sr_dev_driver *di, GSList *options)
117 struct sr_dev_inst *sdi;
118 struct drv_context *drvc;
119 struct sr_usb_dev_inst *usb;
120 struct libusb_device_descriptor des;
121 libusb_device **devlist;
123 char connection_id[64];
130 drvc->instances = NULL;
134 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
136 for (i = 0; devlist[i]; i++) {
137 libusb_get_device_descriptor(devlist[i], &des);
139 if (des.idVendor != LOGICSTUDIO16_VID)
142 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
146 switch (des.idProduct) {
147 case LOGICSTUDIO16_PID_HAVE_FIRMWARE:
148 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
149 libusb_get_device_address(devlist[i]), NULL);
151 sdi = create_device(di, usb, SR_ST_INACTIVE, 0);
153 case LOGICSTUDIO16_PID_LACK_FIRMWARE:
154 r = ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
155 USB_CONFIGURATION, FX2_FIRMWARE);
158 * An error message has already been logged by
159 * ezusb_upload_firmware().
165 * Put unknown as the address so that we know we still
166 * need to get the proper address after the device
169 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
170 UNKNOWN_ADDRESS, NULL);
172 sdi = create_device(di, usb, SR_ST_INITIALIZING,
173 g_get_monotonic_time());
179 /* Cannot handle this device? */
183 sdi->connection_id = g_strdup(connection_id);
185 drvc->instances = g_slist_append(drvc->instances, sdi);
186 devices = g_slist_append(devices, sdi);
189 libusb_free_device_list(devlist, 1);
194 static int open_device(struct sr_dev_inst *sdi)
196 struct drv_context *drvc;
197 struct sr_usb_dev_inst *usb;
198 struct libusb_device_descriptor des;
199 libusb_device **devlist;
200 char connection_id[64];
205 if (sdi->status == SR_ST_ACTIVE)
208 drvc = sdi->driver->context;
213 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
215 for (i = 0; devlist[i]; i++) {
216 libusb_get_device_descriptor(devlist[i], &des);
218 if (des.idVendor != LOGICSTUDIO16_VID ||
219 des.idProduct != LOGICSTUDIO16_PID_HAVE_FIRMWARE)
222 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
225 * Check if this device is the same one that we associated
226 * with this sdi in scan() and bail if it isn't.
228 if (strcmp(sdi->connection_id, connection_id))
231 r = libusb_open(devlist[i], &usb->devhdl);
234 sr_err("Failed to open device: %s.",
235 libusb_error_name(r));
239 /* Fix up address after firmware upload. */
240 if (usb->address == UNKNOWN_ADDRESS)
241 usb->address = libusb_get_device_address(devlist[i]);
248 libusb_free_device_list(devlist, 1);
253 if ((r = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
254 sr_err("Failed to claim interface: %s.",
255 libusb_error_name(r));
259 sdi->status = SR_ST_ACTIVE;
264 static int dev_open(struct sr_dev_inst *sdi)
266 struct drv_context *drvc;
267 struct dev_context *devc;
268 int64_t timediff_us, timediff_ms;
271 drvc = sdi->driver->context;
275 sr_err("Driver was not initialized.");
280 * If we didn't need to upload FX2 firmware in scan(), open the device
281 * right away. Otherwise, wait up to MAX_RENUM_DELAY_MS ms for the
284 if (!devc->fw_updated) {
285 ret = open_device(sdi);
287 sr_info("Waiting for device to reset.");
289 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
290 g_usleep(300 * 1000);
293 while (timediff_ms < MAX_RENUM_DELAY_MS) {
294 ret = open_device(sdi);
299 g_usleep(100 * 1000);
301 timediff_us = g_get_monotonic_time() - devc->fw_updated;
302 timediff_ms = timediff_us / 1000;
304 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
308 sr_err("Device failed to renumerate.");
312 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
316 sr_err("Unable to open device.");
321 * Only allocate the sample buffer now since it's rather large.
322 * Don't want to allocate it before we know we are going to use it.
324 devc->fetched_samples = g_malloc(SAMPLE_BUF_SIZE);
326 devc->conv8to16 = g_malloc(CONV_8TO16_BUF_SIZE);
328 devc->intr_xfer = libusb_alloc_transfer(0);
329 devc->bulk_xfer = libusb_alloc_transfer(0);
334 static int dev_close(struct sr_dev_inst *sdi)
336 struct sr_usb_dev_inst *usb;
337 struct dev_context *devc;
342 g_free(devc->fetched_samples);
343 devc->fetched_samples = NULL;
345 g_free(devc->conv8to16);
346 devc->conv8to16 = NULL;
348 if (devc->intr_xfer) {
349 devc->intr_xfer->buffer = NULL; /* Points into devc. */
350 libusb_free_transfer(devc->intr_xfer);
351 devc->intr_xfer = NULL;
354 if (devc->bulk_xfer) {
355 devc->bulk_xfer->buffer = NULL; /* Points into devc. */
356 libusb_free_transfer(devc->bulk_xfer);
357 devc->bulk_xfer = NULL;
363 libusb_release_interface(usb->devhdl, 0);
365 libusb_close(usb->devhdl);
368 sdi->status = SR_ST_INACTIVE;
373 static int config_get(uint32_t key, GVariant **data,
374 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
376 struct dev_context *devc;
386 case SR_CONF_SAMPLERATE:
387 *data = g_variant_new_uint64(lls_get_samplerate(sdi));
389 case SR_CONF_CAPTURE_RATIO:
390 *data = g_variant_new_uint64(devc->capture_ratio);
399 static int config_set(uint32_t key, GVariant *data,
400 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
402 struct dev_context *devc;
411 if (sdi->status != SR_ST_ACTIVE)
412 return SR_ERR_DEV_CLOSED;
415 case SR_CONF_SAMPLERATE:
416 return lls_set_samplerate(sdi, g_variant_get_uint64(data));
417 case SR_CONF_CAPTURE_RATIO:
418 devc->capture_ratio = g_variant_get_uint64(data);
419 if (devc->capture_ratio > 100)
429 static int config_list(uint32_t key, GVariant **data,
430 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
439 case SR_CONF_DEVICE_OPTIONS:
440 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
441 devopts, ARRAY_SIZE(devopts),
444 case SR_CONF_SAMPLERATE:
445 g_variant_builder_init(&vb, G_VARIANT_TYPE("a{sv}"));
446 var = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
447 samplerates, ARRAY_SIZE(samplerates),
449 g_variant_builder_add(&vb, "{sv}", "samplerates", var);
450 *data = g_variant_builder_end(&vb);
452 case SR_CONF_TRIGGER_MATCH:
453 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
454 trigger_matches, ARRAY_SIZE(trigger_matches),
464 static int config_commit(const struct sr_dev_inst *sdi)
466 return lls_setup_acquisition(sdi);
469 static int receive_usb_data(int fd, int revents, void *cb_data)
471 struct drv_context *drvc;
477 drvc = (struct drv_context *)cb_data;
482 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
488 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
490 struct drv_context *drvc;
493 if (sdi->status != SR_ST_ACTIVE)
494 return SR_ERR_DEV_CLOSED;
496 drvc = sdi->driver->context;
498 if ((ret = lls_start_acquisition(sdi)) < 0)
501 std_session_send_df_header(sdi, LOG_PREFIX);
503 return usb_source_add(sdi->session, drvc->sr_ctx, 100,
504 receive_usb_data, drvc);
507 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
509 if (sdi->status != SR_ST_ACTIVE)
510 return SR_ERR_DEV_CLOSED;
512 return lls_stop_acquisition(sdi);
515 SR_PRIV struct sr_dev_driver lecroy_logicstudio_driver_info = {
516 .name = "lecroy-logicstudio",
517 .longname = "LeCroy LogicStudio",
520 .cleanup = std_cleanup,
522 .dev_list = std_dev_list,
523 .config_get = config_get,
524 .config_set = config_set,
525 .config_list = config_list,
526 .config_commit = config_commit,
527 .dev_open = dev_open,
528 .dev_close = dev_close,
529 .dev_acquisition_start = dev_acquisition_start,
530 .dev_acquisition_stop = dev_acquisition_stop,