2 * This file is part of the libsigrok project.
4 * Copyright (C) 2017 Jan Luebbe <jluebbe@lasnet.de>
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/>.
26 #define BUF_SIZE (16 * 1024)
27 #define BUF_TIMEOUT (1000 * 1000)
29 SR_PRIV struct sr_dev_driver saleae_logic_pro_driver_info;
31 static const uint32_t drvopts[] = {
32 SR_CONF_LOGIC_ANALYZER,
35 static const uint32_t scanopts[] = {
39 static const uint32_t devopts[] = {
41 SR_CONF_CONN | SR_CONF_GET,
42 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 static const char *channel_names[] = {
46 "0", "1", "2", "3", "4", "5", "6", "7",
47 "8", "9", "10", "11", "12", "13", "14", "15",
50 static const uint64_t samplerates[] = {
59 static gboolean scan_firmware(libusb_device *dev)
61 struct libusb_device_descriptor des;
62 struct libusb_device_handle *hdl;
64 unsigned char strdesc[64];
69 libusb_get_device_descriptor(dev, &des);
71 if (libusb_open(dev, &hdl) != 0)
74 if (libusb_get_string_descriptor_ascii(hdl,
75 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
77 if (strcmp((const char *)strdesc, "Saleae"))
80 if (libusb_get_string_descriptor_ascii(hdl,
81 des.iProduct, strdesc, sizeof(strdesc)) < 0)
83 if (strcmp((const char *)strdesc, "Logic Pro"))
95 static GSList *scan(struct sr_dev_driver *di, GSList *options)
97 struct drv_context *drvc;
98 struct dev_context *devc;
99 struct sr_dev_inst *sdi;
100 GSList *devices, *conn_devices;
101 libusb_device **devlist;
102 struct libusb_device_descriptor des;
104 char connection_id[64];
108 drvc->instances = NULL;
111 for (GSList *l = options; l; l = l->next) {
112 struct sr_config *src = l->data;
116 conn = g_variant_get_string(src->data, NULL);
122 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
126 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
127 for (unsigned int i = 0; devlist[i]; i++) {
129 struct sr_usb_dev_inst *usb = NULL;
132 for (l = conn_devices; l; l = l->next) {
134 if (usb->bus == libusb_get_bus_number(devlist[i])
135 && usb->address == libusb_get_device_address(devlist[i]))
139 /* This device matched none of the ones that
140 * matched the conn specification. */
144 libusb_get_device_descriptor(devlist[i], &des);
146 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
148 if (des.idVendor != 0x21a9 || des.idProduct != 0x1006)
151 if (!scan_firmware(devlist[i])) {
152 sr_err("Found a Logic Pro device, but firmware is not loaded (use Saleae application).");
156 sdi = g_malloc0(sizeof(struct sr_dev_inst));
157 sdi->status = SR_ST_INITIALIZING;
158 sdi->vendor = g_strdup("Saleae");
159 sdi->model = g_strdup("Logic Pro 16");
160 sdi->connection_id = g_strdup(connection_id);
162 for (unsigned int j = 0; j < ARRAY_SIZE(channel_names); j++)
163 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
166 sr_dbg("Found a Logic Pro 16 device.");
167 sdi->status = SR_ST_INACTIVE;
168 sdi->inst_type = SR_INST_USB;
169 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
170 libusb_get_device_address(devlist[i]), NULL);
172 devc = g_malloc0(sizeof(struct dev_context));
174 devices = g_slist_append(devices, sdi);
177 libusb_free_device_list(devlist, 1);
178 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
180 return std_scan_complete(di, devices);
183 static int dev_open(struct sr_dev_inst *sdi)
185 struct sr_dev_driver *di = sdi->driver;
186 struct drv_context *drvc = di->context;
187 struct dev_context *devc = sdi->priv;
188 struct sr_usb_dev_inst *usb = sdi->conn;
191 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
194 if ((ret = libusb_claim_interface(usb->devhdl, 0))) {
195 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
199 /* Configure default samplerate. */
200 if (devc->dig_samplerate == 0)
201 devc->dig_samplerate = samplerates[3];
206 static int dev_close(struct sr_dev_inst *sdi)
208 sr_usb_close(sdi->conn);
213 static int config_get(uint32_t key, GVariant **data,
214 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
216 struct sr_usb_dev_inst *usb;
217 struct dev_context *devc;
223 if (!sdi || !sdi->conn)
226 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
228 case SR_CONF_SAMPLERATE:
232 *data = g_variant_new_uint64(devc->dig_samplerate);
241 static int config_set(uint32_t key, GVariant *data,
242 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
244 struct dev_context *devc;
251 case SR_CONF_SAMPLERATE:
252 devc->dig_samplerate = g_variant_get_uint64(data);
261 static int config_list(uint32_t key, GVariant **data,
262 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
271 case SR_CONF_SCAN_OPTIONS:
272 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
273 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
275 case SR_CONF_DEVICE_OPTIONS:
277 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
278 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
280 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
281 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
284 case SR_CONF_SAMPLERATE:
285 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
286 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
287 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
288 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
289 *data = g_variant_builder_end(&gvb);
298 static void dev_acquisition_abort(const struct sr_dev_inst *sdi)
300 struct dev_context *devc = sdi->priv;
303 for (i = 0; i < devc->num_transfers; i++) {
304 if (devc->transfers[i])
305 libusb_cancel_transfer(devc->transfers[i]);
309 static int dev_acquisition_handle(int fd, int revents, void *cb_data)
311 struct sr_dev_inst *sdi = cb_data;
312 struct drv_context *drvc = sdi->driver->context;
313 struct timeval tv = ALL_ZERO;
318 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
323 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
325 struct dev_context *devc = sdi->priv;
326 struct drv_context *drvc = sdi->driver->context;
327 struct libusb_transfer *transfer;
328 struct sr_usb_dev_inst *usb;
332 ret = saleae_logic_pro_init(sdi);
336 ret = saleae_logic_pro_prepare(sdi);
342 devc->conv_buffer = g_malloc(CONV_BUFFER_SIZE);
344 devc->num_transfers = BUF_COUNT;
345 devc->transfers = g_malloc0(sizeof(*devc->transfers) * BUF_COUNT);
346 for (i = 0; i < devc->num_transfers; i++) {
347 buf = g_malloc(BUF_SIZE);
348 transfer = libusb_alloc_transfer(0);
349 libusb_fill_bulk_transfer(transfer, usb->devhdl,
350 2 | LIBUSB_ENDPOINT_IN, buf, BUF_SIZE,
351 saleae_logic_pro_receive_data, (void *)sdi, BUF_TIMEOUT);
352 if ((ret = libusb_submit_transfer(transfer)) != 0) {
353 sr_err("Failed to submit transfer: %s.",
354 libusb_error_name(ret));
355 libusb_free_transfer(transfer);
357 dev_acquisition_abort(sdi);
360 devc->transfers[i] = transfer;
361 devc->submitted_transfers++;
364 usb_source_add(sdi->session, drvc->sr_ctx, BUF_TIMEOUT, dev_acquisition_handle, (void *)sdi);
366 std_session_send_df_header(sdi);
368 saleae_logic_pro_start(sdi);
375 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
377 struct dev_context *devc = sdi->priv;
378 struct drv_context *drvc = sdi->driver->context;
380 saleae_logic_pro_stop(sdi);
382 std_session_send_df_end(sdi);
384 usb_source_remove(sdi->session, drvc->sr_ctx);
386 g_free(devc->conv_buffer);
391 SR_PRIV struct sr_dev_driver saleae_logic_pro_driver_info = {
392 .name = "saleae-logic-pro",
393 .longname = "Saleae Logic Pro",
396 .cleanup = std_cleanup,
398 .dev_list = std_dev_list,
399 .dev_clear = std_dev_clear,
400 .config_get = config_get,
401 .config_set = config_set,
402 .config_list = config_list,
403 .dev_open = dev_open,
404 .dev_close = dev_close,
405 .dev_acquisition_start = dev_acquisition_start,
406 .dev_acquisition_stop = dev_acquisition_stop,
410 SR_REGISTER_DEV_DRIVER(saleae_logic_pro_driver_info);