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 #define NUM_CHANNELS 16
40 static const uint32_t drvopts[] = {
41 SR_CONF_LOGIC_ANALYZER,
44 static const uint32_t devopts[] = {
45 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
47 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
50 static const int32_t trigger_matches[] = {
58 static const uint64_t samplerates[] = {
79 static struct sr_dev_inst *create_device(struct sr_usb_dev_inst *usb,
80 enum sr_dev_inst_status status, int64_t fw_updated)
82 struct sr_dev_inst *sdi;
83 struct dev_context *devc;
87 sdi = g_malloc0(sizeof(struct sr_dev_inst));
89 sdi->vendor = g_strdup("LeCroy");
90 sdi->model = g_strdup("LogicStudio16");
91 sdi->inst_type = SR_INST_USB;
94 for (i = 0; i < NUM_CHANNELS; i++) {
95 snprintf(channel_name, sizeof(channel_name), "D%i", i);
96 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
99 devc = g_malloc0(sizeof(struct dev_context));
103 devc->fw_updated = fw_updated;
104 devc->capture_ratio = 50;
106 lls_set_samplerate(sdi, SR_MHZ(500));
111 static GSList *scan(struct sr_dev_driver *di, GSList *options)
113 struct sr_dev_inst *sdi;
114 struct drv_context *drvc;
115 struct sr_usb_dev_inst *usb;
116 struct libusb_device_descriptor des;
117 libusb_device **devlist;
119 char connection_id[64];
129 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
131 for (i = 0; devlist[i]; i++) {
132 libusb_get_device_descriptor(devlist[i], &des);
134 if (des.idVendor != LOGICSTUDIO16_VID)
137 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
142 switch (des.idProduct) {
143 case LOGICSTUDIO16_PID_HAVE_FIRMWARE:
144 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
145 libusb_get_device_address(devlist[i]), NULL);
147 sdi = create_device(usb, SR_ST_INACTIVE, 0);
149 case LOGICSTUDIO16_PID_LACK_FIRMWARE:
150 r = ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
151 USB_CONFIGURATION, FX2_FIRMWARE);
154 * An error message has already been logged by
155 * ezusb_upload_firmware().
161 * Put unknown as the address so that we know we still
162 * need to get the proper address after the device
165 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
166 UNKNOWN_ADDRESS, NULL);
168 sdi = create_device(usb, SR_ST_INITIALIZING,
169 g_get_monotonic_time());
175 /* Cannot handle this device? */
179 sdi->connection_id = g_strdup(connection_id);
181 devices = g_slist_append(devices, sdi);
184 libusb_free_device_list(devlist, 1);
186 return std_scan_complete(di, devices);
189 static int open_device(struct sr_dev_inst *sdi)
191 struct drv_context *drvc;
192 struct sr_usb_dev_inst *usb;
193 struct libusb_device_descriptor des;
194 libusb_device **devlist;
195 char connection_id[64];
200 drvc = sdi->driver->context;
205 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
207 for (i = 0; devlist[i]; i++) {
208 libusb_get_device_descriptor(devlist[i], &des);
210 if (des.idVendor != LOGICSTUDIO16_VID ||
211 des.idProduct != LOGICSTUDIO16_PID_HAVE_FIRMWARE)
214 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
218 * Check if this device is the same one that we associated
219 * with this sdi in scan() and bail if it isn't.
221 if (strcmp(sdi->connection_id, connection_id))
224 r = libusb_open(devlist[i], &usb->devhdl);
227 sr_err("Failed to open device: %s.",
228 libusb_error_name(r));
232 /* Fix up address after firmware upload. */
233 if (usb->address == UNKNOWN_ADDRESS)
234 usb->address = libusb_get_device_address(devlist[i]);
241 libusb_free_device_list(devlist, 1);
246 if ((r = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
247 sr_err("Failed to claim interface: %s.",
248 libusb_error_name(r));
252 sdi->status = SR_ST_ACTIVE;
257 static int dev_open(struct sr_dev_inst *sdi)
259 struct dev_context *devc;
260 int64_t timediff_us, timediff_ms;
266 * If we didn't need to upload FX2 firmware in scan(), open the device
267 * right away. Otherwise, wait up to MAX_RENUM_DELAY_MS ms for the
270 if (!devc->fw_updated) {
271 ret = open_device(sdi);
273 sr_info("Waiting for device to reset.");
275 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
276 g_usleep(300 * 1000);
279 while (timediff_ms < MAX_RENUM_DELAY_MS) {
280 ret = open_device(sdi);
285 g_usleep(100 * 1000);
287 timediff_us = g_get_monotonic_time() - devc->fw_updated;
288 timediff_ms = timediff_us / 1000;
290 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
294 sr_err("Device failed to renumerate.");
298 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
302 sr_err("Unable to open device.");
307 * Only allocate the sample buffer now since it's rather large.
308 * Don't want to allocate it before we know we are going to use it.
310 devc->fetched_samples = g_malloc(SAMPLE_BUF_SIZE);
312 devc->conv8to16 = g_malloc(CONV_8TO16_BUF_SIZE);
314 devc->intr_xfer = libusb_alloc_transfer(0);
315 devc->bulk_xfer = libusb_alloc_transfer(0);
320 static int dev_close(struct sr_dev_inst *sdi)
322 struct sr_usb_dev_inst *usb;
323 struct dev_context *devc;
328 g_free(devc->fetched_samples);
329 devc->fetched_samples = NULL;
331 g_free(devc->conv8to16);
332 devc->conv8to16 = NULL;
334 if (devc->intr_xfer) {
335 devc->intr_xfer->buffer = NULL; /* Points into devc. */
336 libusb_free_transfer(devc->intr_xfer);
337 devc->intr_xfer = NULL;
340 if (devc->bulk_xfer) {
341 devc->bulk_xfer->buffer = NULL; /* Points into devc. */
342 libusb_free_transfer(devc->bulk_xfer);
343 devc->bulk_xfer = NULL;
349 libusb_release_interface(usb->devhdl, 0);
351 libusb_close(usb->devhdl);
357 static int config_get(uint32_t key, GVariant **data,
358 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
360 struct dev_context *devc;
370 case SR_CONF_SAMPLERATE:
371 *data = g_variant_new_uint64(lls_get_samplerate(sdi));
373 case SR_CONF_CAPTURE_RATIO:
374 *data = g_variant_new_uint64(devc->capture_ratio);
383 static int config_set(uint32_t key, GVariant *data,
384 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
386 struct dev_context *devc;
396 case SR_CONF_SAMPLERATE:
397 return lls_set_samplerate(sdi, g_variant_get_uint64(data));
398 case SR_CONF_CAPTURE_RATIO:
399 devc->capture_ratio = g_variant_get_uint64(data);
408 static int config_list(uint32_t key, GVariant **data,
409 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
412 case SR_CONF_DEVICE_OPTIONS:
413 return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts);
414 case SR_CONF_SAMPLERATE:
415 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
417 case SR_CONF_TRIGGER_MATCH:
418 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
427 static int config_commit(const struct sr_dev_inst *sdi)
429 return lls_setup_acquisition(sdi);
432 static int receive_usb_data(int fd, int revents, void *cb_data)
434 struct drv_context *drvc;
440 drvc = (struct drv_context *)cb_data;
445 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
451 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
453 struct drv_context *drvc;
456 drvc = sdi->driver->context;
458 if ((ret = lls_start_acquisition(sdi)) < 0)
461 std_session_send_df_header(sdi);
463 return usb_source_add(sdi->session, drvc->sr_ctx, 100,
464 receive_usb_data, drvc);
467 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
469 return lls_stop_acquisition(sdi);
472 static struct sr_dev_driver lecroy_logicstudio_driver_info = {
473 .name = "lecroy-logicstudio",
474 .longname = "LeCroy LogicStudio",
477 .cleanup = std_cleanup,
479 .dev_list = std_dev_list,
480 .dev_clear = std_dev_clear,
481 .config_get = config_get,
482 .config_set = config_set,
483 .config_list = config_list,
484 .config_commit = config_commit,
485 .dev_open = dev_open,
486 .dev_close = dev_close,
487 .dev_acquisition_start = dev_acquisition_start,
488 .dev_acquisition_stop = dev_acquisition_stop,
491 SR_REGISTER_DEV_DRIVER(lecroy_logicstudio_driver_info);