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 #define FW_HEADER_SIZE 7
60 #define FW_MAX_PART_SIZE (4 * 1024)
62 static int upload_firmware(struct sr_context *ctx, libusb_device *dev, const char *name)
64 struct libusb_device_handle *hdl = NULL;
65 unsigned char *firmware = NULL;
67 size_t fw_size, fw_offset = 0;
68 uint32_t part_address = 0;
69 uint16_t part_size = 0;
70 uint8_t part_final = 0;
72 firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
73 name, &fw_size, 256 * 1024);
77 sr_info("Uploading firmware '%s'.", name);
79 if (libusb_open(dev, &hdl) != 0)
82 while ((fw_offset + FW_HEADER_SIZE) <= fw_size) {
83 part_size = GUINT16_FROM_LE(*(uint16_t*)(firmware + fw_offset));
84 part_address = GUINT32_FROM_LE(*(uint32_t*)(firmware + fw_offset + 2));
85 part_final = *(uint8_t*)(firmware + fw_offset + 6);
86 if (part_size > FW_MAX_PART_SIZE) {
87 sr_err("Part too large (%d).", part_size);
90 fw_offset += FW_HEADER_SIZE;
91 if ((fw_offset + part_size) > fw_size) {
92 sr_err("Truncated firmware file.");
95 ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
96 LIBUSB_ENDPOINT_OUT, 0xa0,
97 part_address & 0xffff, part_address >> 16,
98 firmware + fw_offset, part_size,
101 sr_err("Unable to send firmware to device: %s.",
102 libusb_error_name(ret));
107 sr_spew("Uploaded %d bytes.", part_size);
109 sr_info("Started firmware at 0x%x.", part_address);
110 fw_offset += part_size;
113 if ((!part_final) || (part_size != 0)) {
114 sr_err("Missing final part.");
120 sr_info("Firmware upload done.");
131 static gboolean scan_firmware(libusb_device *dev)
133 struct libusb_device_descriptor des;
134 struct libusb_device_handle *hdl;
136 unsigned char strdesc[64];
141 libusb_get_device_descriptor(dev, &des);
143 if (libusb_open(dev, &hdl) != 0)
146 if (libusb_get_string_descriptor_ascii(hdl,
147 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
149 if (strcmp((const char *)strdesc, "Saleae"))
152 if (libusb_get_string_descriptor_ascii(hdl,
153 des.iProduct, strdesc, sizeof(strdesc)) < 0)
155 if (strcmp((const char *)strdesc, "Logic Pro"))
167 static GSList *scan(struct sr_dev_driver *di, GSList *options)
169 struct drv_context *drvc;
170 struct dev_context *devc;
171 struct sr_dev_inst *sdi;
172 GSList *devices, *conn_devices;
173 libusb_device **devlist;
174 struct libusb_device_descriptor des;
176 char connection_id[64];
177 gboolean fw_loaded = FALSE;
182 drvc->instances = NULL;
185 for (GSList *l = options; l; l = l->next) {
186 struct sr_config *src = l->data;
190 conn = g_variant_get_string(src->data, NULL);
195 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
196 for (unsigned int i = 0; devlist[i]; i++) {
197 libusb_get_device_descriptor(devlist[i], &des);
199 if (des.idVendor != 0x21a9 || des.idProduct != 0x1006)
202 if (!scan_firmware(devlist[i])) {
203 sr_info("Found a Logic Pro 16 device (no firmware loaded).");
204 if (upload_firmware(drvc->sr_ctx, devlist[i],
205 "saleae-logicpro16-fx3.fw") != SR_OK) {
206 sr_err("Firmware upload failed.");
214 /* Give the device some time to come back and scan again */
215 libusb_free_device_list(devlist, 1);
216 g_usleep(500 * 1000);
217 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
220 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
221 for (unsigned int i = 0; devlist[i]; i++) {
223 struct sr_usb_dev_inst *usb = NULL;
226 for (l = conn_devices; l; l = l->next) {
228 if (usb->bus == libusb_get_bus_number(devlist[i])
229 && usb->address == libusb_get_device_address(devlist[i]))
233 /* This device matched none of the ones that
234 * matched the conn specification. */
238 libusb_get_device_descriptor(devlist[i], &des);
240 if (des.idVendor != 0x21a9 || des.idProduct != 0x1006)
243 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
245 sdi = g_malloc0(sizeof(struct sr_dev_inst));
246 sdi->status = SR_ST_INITIALIZING;
247 sdi->vendor = g_strdup("Saleae");
248 sdi->model = g_strdup("Logic Pro 16");
249 sdi->connection_id = g_strdup(connection_id);
251 for (unsigned int j = 0; j < ARRAY_SIZE(channel_names); j++)
252 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
255 sr_dbg("Found a Logic Pro 16 device.");
256 sdi->status = SR_ST_INACTIVE;
257 sdi->inst_type = SR_INST_USB;
258 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
259 libusb_get_device_address(devlist[i]), NULL);
261 devc = g_malloc0(sizeof(struct dev_context));
263 devices = g_slist_append(devices, sdi);
266 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
267 libusb_free_device_list(devlist, 1);
269 return std_scan_complete(di, devices);
272 static int dev_open(struct sr_dev_inst *sdi)
274 struct sr_dev_driver *di = sdi->driver;
275 struct drv_context *drvc = di->context;
276 struct dev_context *devc = sdi->priv;
277 struct sr_usb_dev_inst *usb = sdi->conn;
280 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
283 if ((ret = libusb_claim_interface(usb->devhdl, 0))) {
284 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
288 /* Configure default samplerate. */
289 if (devc->dig_samplerate == 0)
290 devc->dig_samplerate = samplerates[3];
295 static int dev_close(struct sr_dev_inst *sdi)
297 sr_usb_close(sdi->conn);
302 static int config_get(uint32_t key, GVariant **data,
303 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
305 struct sr_usb_dev_inst *usb;
306 struct dev_context *devc;
312 if (!sdi || !sdi->conn)
315 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
317 case SR_CONF_SAMPLERATE:
321 *data = g_variant_new_uint64(devc->dig_samplerate);
330 static int config_set(uint32_t key, GVariant *data,
331 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
333 struct dev_context *devc;
340 case SR_CONF_SAMPLERATE:
341 devc->dig_samplerate = g_variant_get_uint64(data);
350 static int config_list(uint32_t key, GVariant **data,
351 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
360 case SR_CONF_SCAN_OPTIONS:
361 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
362 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
364 case SR_CONF_DEVICE_OPTIONS:
366 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
367 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
369 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
370 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
373 case SR_CONF_SAMPLERATE:
374 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
375 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
376 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
377 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
378 *data = g_variant_builder_end(&gvb);
387 static void dev_acquisition_abort(const struct sr_dev_inst *sdi)
389 struct dev_context *devc = sdi->priv;
392 for (i = 0; i < devc->num_transfers; i++) {
393 if (devc->transfers[i])
394 libusb_cancel_transfer(devc->transfers[i]);
398 static int dev_acquisition_handle(int fd, int revents, void *cb_data)
400 struct sr_dev_inst *sdi = cb_data;
401 struct drv_context *drvc = sdi->driver->context;
402 struct timeval tv = ALL_ZERO;
407 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
412 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
414 struct dev_context *devc = sdi->priv;
415 struct drv_context *drvc = sdi->driver->context;
416 struct libusb_transfer *transfer;
417 struct sr_usb_dev_inst *usb;
421 ret = saleae_logic_pro_init(sdi);
425 ret = saleae_logic_pro_prepare(sdi);
431 devc->conv_buffer = g_malloc(CONV_BUFFER_SIZE);
433 devc->num_transfers = BUF_COUNT;
434 devc->transfers = g_malloc0(sizeof(*devc->transfers) * BUF_COUNT);
435 for (i = 0; i < devc->num_transfers; i++) {
436 buf = g_malloc(BUF_SIZE);
437 transfer = libusb_alloc_transfer(0);
438 libusb_fill_bulk_transfer(transfer, usb->devhdl,
439 2 | LIBUSB_ENDPOINT_IN, buf, BUF_SIZE,
440 saleae_logic_pro_receive_data, (void *)sdi, BUF_TIMEOUT);
441 if ((ret = libusb_submit_transfer(transfer)) != 0) {
442 sr_err("Failed to submit transfer: %s.",
443 libusb_error_name(ret));
444 libusb_free_transfer(transfer);
446 dev_acquisition_abort(sdi);
449 devc->transfers[i] = transfer;
450 devc->submitted_transfers++;
453 usb_source_add(sdi->session, drvc->sr_ctx, BUF_TIMEOUT, dev_acquisition_handle, (void *)sdi);
455 std_session_send_df_header(sdi);
457 saleae_logic_pro_start(sdi);
464 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
466 struct dev_context *devc = sdi->priv;
467 struct drv_context *drvc = sdi->driver->context;
469 saleae_logic_pro_stop(sdi);
471 std_session_send_df_end(sdi);
473 usb_source_remove(sdi->session, drvc->sr_ctx);
475 g_free(devc->conv_buffer);
480 SR_PRIV struct sr_dev_driver saleae_logic_pro_driver_info = {
481 .name = "saleae-logic-pro",
482 .longname = "Saleae Logic Pro",
485 .cleanup = std_cleanup,
487 .dev_list = std_dev_list,
488 .dev_clear = std_dev_clear,
489 .config_get = config_get,
490 .config_set = config_set,
491 .config_list = config_list,
492 .dev_open = dev_open,
493 .dev_close = dev_close,
494 .dev_acquisition_start = dev_acquisition_start,
495 .dev_acquisition_stop = dev_acquisition_stop,
499 SR_REGISTER_DEV_DRIVER(saleae_logic_pro_driver_info);