]> sigrok.org Git - libsigrok.git/blame - hardware/saleae-logic/saleae-logic.c
sr: Fix/document probe names.
[libsigrok.git] / hardware / saleae-logic / saleae-logic.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <sys/time.h>
23#include <inttypes.h>
24#include <glib.h>
25#include <libusb.h>
1a081ca6 26#include "config.h"
b7f09cf8
UH
27#include "sigrok.h"
28#include "sigrok-internal.h"
6d754b6d 29#include "saleae-logic.h"
a1bb33af 30
6d754b6d 31static struct fx2_profile supported_fx2[] = {
e10d6e32
BV
32 /* Saleae Logic */
33 { 0x0925, 0x3881, 0x0925, 0x3881, "Saleae", "Logic", NULL, 8 },
34 /* default Cypress FX2 without EEPROM */
35 { 0x04b4, 0x8613, 0x0925, 0x3881, "Cypress", "FX2", NULL, 16 },
6d754b6d 36 { 0, 0, 0, 0, 0, 0, 0, 0 }
e10d6e32
BV
37};
38
ffedd0bf 39static int hwcaps[] = {
5a2326a7
UH
40 SR_HWCAP_LOGIC_ANALYZER,
41 SR_HWCAP_SAMPLERATE,
a1bb33af 42
6f5f21f9 43 /* These are really implemented in the driver, not the hardware. */
5a2326a7
UH
44 SR_HWCAP_LIMIT_SAMPLES,
45 SR_HWCAP_CONTINUOUS,
6f5f21f9 46 0,
a1bb33af
UH
47};
48
d261dbbf
UH
49/*
50 * Probes are numbered 1-8.
51 *
52 * TODO: FX2 eval boards with the standard Cypress VID/PID can have 16 pins
53 * or probes in theory, which is not supported by the Saleae Logic firmware.
54 */
c37d2b1b 55static const char *probe_names[] = {
464d12c7
KS
56 "0",
57 "1",
58 "2",
59 "3",
60 "4",
61 "5",
62 "6",
63 "7",
64 "8",
65 "9",
66 "10",
67 "11",
68 "12",
69 "13",
70 "14",
71 "15",
72 NULL,
73};
74
a1bb33af 75static uint64_t supported_samplerates[] = {
59df0c77
UH
76 SR_KHZ(200),
77 SR_KHZ(250),
78 SR_KHZ(500),
79 SR_MHZ(1),
80 SR_MHZ(2),
81 SR_MHZ(4),
82 SR_MHZ(8),
83 SR_MHZ(12),
84 SR_MHZ(16),
85 SR_MHZ(24),
6f5f21f9 86 0,
a1bb33af
UH
87};
88
60679b18 89static struct sr_samplerates samplerates = {
59df0c77
UH
90 SR_KHZ(200),
91 SR_MHZ(24),
c9140419 92 SR_HZ(0),
6f5f21f9 93 supported_samplerates,
a1bb33af
UH
94};
95
e7eb703f 96/* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
d68e2d1a 97static GSList *dev_insts = NULL;
6d754b6d 98static libusb_context *usb_context = NULL;
a1bb33af 99
003f9beb
UH
100static int new_saleae_logic_firmware = 0;
101
a9a245b4 102static int hw_dev_config_set(int dev_index, int hwcap, void *value);
69040b7c 103static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id);
a1bb33af 104
28fc6de0
UH
105/**
106 * Check the USB configuration to determine if this is a Saleae Logic.
107 *
108 * @return 1 if the device's configuration profile match the Logic firmware's
109 * configuration, 0 otherwise.
a1bb33af 110 */
e5d1717e 111static int check_conf_profile(libusb_device *dev)
a1bb33af
UH
112{
113 struct libusb_device_descriptor des;
6f5f21f9 114 struct libusb_config_descriptor *conf_dsc = NULL;
a1bb33af 115 const struct libusb_interface_descriptor *intf_dsc;
6f5f21f9 116 int ret = -1;
a1bb33af 117
6f5f21f9
UH
118 while (ret == -1) {
119 /* Assume it's not a Saleae Logic unless proven wrong. */
a1bb33af
UH
120 ret = 0;
121
6f5f21f9 122 if (libusb_get_device_descriptor(dev, &des) != 0)
a1bb33af
UH
123 break;
124
6f5f21f9
UH
125 if (des.bNumConfigurations != 1)
126 /* Need exactly 1 configuration. */
a1bb33af
UH
127 break;
128
6f5f21f9 129 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
a1bb33af
UH
130 break;
131
6f5f21f9
UH
132 if (conf_dsc->bNumInterfaces != 1)
133 /* Need exactly 1 interface. */
a1bb33af
UH
134 break;
135
6f5f21f9
UH
136 if (conf_dsc->interface[0].num_altsetting != 1)
137 /* Need just one alternate setting. */
a1bb33af
UH
138 break;
139
140 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
003f9beb
UH
141 if (intf_dsc->bNumEndpoints == 4) {
142 /* The new Saleae Logic firmware has 4 endpoints. */
143 new_saleae_logic_firmware = 1;
144 } else if (intf_dsc->bNumEndpoints == 2) {
145 /* The old Saleae Logic firmware has 2 endpoints. */
146 new_saleae_logic_firmware = 0;
147 } else {
148 /* Other number of endpoints -> not a Saleae Logic. */
a1bb33af 149 break;
003f9beb 150 }
a1bb33af 151
6f5f21f9
UH
152 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
153 (1 | LIBUSB_ENDPOINT_OUT))
d38cd6c1 154 /* The first endpoint should be 1 (outbound). */
a1bb33af
UH
155 break;
156
6f5f21f9
UH
157 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
158 (2 | LIBUSB_ENDPOINT_IN))
d38cd6c1 159 /* The second endpoint should be 2 (inbound). */
a1bb33af
UH
160 break;
161
003f9beb
UH
162 /* TODO: The new firmware has 4 endpoints... */
163
6f5f21f9 164 /* If we made it here, it must be a Saleae Logic. */
a1bb33af
UH
165 ret = 1;
166 }
6f5f21f9
UH
167
168 if (conf_dsc)
a1bb33af
UH
169 libusb_free_config_descriptor(conf_dsc);
170
171 return ret;
172}
173
bb7ef793 174static int sl_open_dev(int dev_index)
a1bb33af 175{
a1bb33af
UH
176 libusb_device **devlist;
177 struct libusb_device_descriptor des;
d68e2d1a 178 struct sr_dev_inst *sdi;
ea9cfed7 179 struct context *ctx;
a1bb33af
UH
180 int err, skip, i;
181
bb7ef793 182 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
6d754b6d 183 return SR_ERR;
ea9cfed7 184 ctx = sdi->priv;
a1bb33af 185
e10d6e32
BV
186 if (sdi->status == SR_ST_ACTIVE)
187 /* already in use */
6d754b6d 188 return SR_ERR;
e10d6e32
BV
189
190 skip = 0;
a1bb33af 191 libusb_get_device_list(usb_context, &devlist);
e10d6e32
BV
192 for (i = 0; devlist[i]; i++) {
193 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
7b48d6e1 194 sr_err("logic: failed to get device descriptor: %d", err);
e10d6e32 195 continue;
a1bb33af 196 }
e10d6e32 197
ea9cfed7
UH
198 if (des.idVendor != ctx->profile->fw_vid
199 || des.idProduct != ctx->profile->fw_pid)
e10d6e32
BV
200 continue;
201
202 if (sdi->status == SR_ST_INITIALIZING) {
bb7ef793 203 if (skip != dev_index) {
e10d6e32
BV
204 /* Skip devices of this type that aren't the one we want. */
205 skip += 1;
206 continue;
207 }
208 } else if (sdi->status == SR_ST_INACTIVE) {
209 /*
7b48d6e1
UH
210 * This device is fully enumerated, so we need to find
211 * this device by vendor, product, bus and address.
e10d6e32 212 */
ea9cfed7
UH
213 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
214 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
e10d6e32
BV
215 /* this is not the one */
216 continue;
217 }
218
ea9cfed7
UH
219 if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
220 if (ctx->usb->address == 0xff)
e10d6e32
BV
221 /*
222 * first time we touch this device after firmware upload,
223 * so we don't know the address yet.
224 */
ea9cfed7 225 ctx->usb->address = libusb_get_device_address(devlist[i]);
e10d6e32
BV
226
227 sdi->status = SR_ST_ACTIVE;
7b48d6e1 228 sr_info("logic: opened device %d on %d.%d interface %d",
ea9cfed7
UH
229 sdi->index, ctx->usb->bus,
230 ctx->usb->address, USB_INTERFACE);
e10d6e32 231 } else {
7b48d6e1 232 sr_err("logic: failed to open device: %d", err);
a1bb33af 233 }
6d754b6d
BV
234
235 /* if we made it here, we handled the device one way or another */
236 break;
a1bb33af
UH
237 }
238 libusb_free_device_list(devlist, 1);
239
6d754b6d
BV
240 if (sdi->status != SR_ST_ACTIVE)
241 return SR_ERR;
a1bb33af 242
6d754b6d 243 return SR_OK;
a1bb33af
UH
244}
245
bb7ef793 246static void close_dev(struct sr_dev_inst *sdi)
a1bb33af 247{
ea9cfed7 248 struct context *ctx;
69890f73 249
ea9cfed7 250 ctx = sdi->priv;
69890f73 251
ea9cfed7 252 if (ctx->usb->devhdl == NULL)
f6958dab
UH
253 return;
254
7b48d6e1 255 sr_info("logic: closing device %d on %d.%d interface %d", sdi->index,
ea9cfed7
UH
256 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
257 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
258 libusb_close(ctx->usb->devhdl);
259 ctx->usb->devhdl = NULL;
5a2326a7 260 sdi->status = SR_ST_INACTIVE;
a1bb33af
UH
261}
262
ea9cfed7 263static int configure_probes(struct context *ctx, GSList *probes)
a1bb33af 264{
1afe8989 265 struct sr_probe *probe;
a1bb33af
UH
266 GSList *l;
267 int probe_bit, stage, i;
268 char *tc;
269
ea9cfed7 270 ctx->probe_mask = 0;
6f5f21f9 271 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
ea9cfed7
UH
272 ctx->trigger_mask[i] = 0;
273 ctx->trigger_value[i] = 0;
a1bb33af
UH
274 }
275
276 stage = -1;
6f5f21f9 277 for (l = probes; l; l = l->next) {
1afe8989 278 probe = (struct sr_probe *)l->data;
6f5f21f9 279 if (probe->enabled == FALSE)
a1bb33af
UH
280 continue;
281 probe_bit = 1 << (probe->index - 1);
ea9cfed7 282 ctx->probe_mask |= probe_bit;
989938f6
UH
283 if (!(probe->trigger))
284 continue;
285
286 stage = 0;
287 for (tc = probe->trigger; *tc; tc++) {
ea9cfed7 288 ctx->trigger_mask[stage] |= probe_bit;
989938f6 289 if (*tc == '1')
ea9cfed7 290 ctx->trigger_value[stage] |= probe_bit;
989938f6
UH
291 stage++;
292 if (stage > NUM_TRIGGER_STAGES)
e46b8fb1 293 return SR_ERR;
a1bb33af
UH
294 }
295 }
296
6f5f21f9
UH
297 if (stage == -1)
298 /*
299 * We didn't configure any triggers, make sure acquisition
300 * doesn't wait for any.
301 */
ea9cfed7 302 ctx->trigger_stage = TRIGGER_FIRED;
a1bb33af 303 else
ea9cfed7 304 ctx->trigger_stage = 0;
a1bb33af 305
e46b8fb1 306 return SR_OK;
a1bb33af
UH
307}
308
ea9cfed7 309static struct context *fx2_dev_new(void)
6d754b6d 310{
ea9cfed7 311 struct context *ctx;
6d754b6d 312
ea9cfed7
UH
313 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
314 sr_err("logic: %s: ctx malloc failed", __func__);
6d754b6d
BV
315 return NULL;
316 }
ea9cfed7
UH
317 ctx->trigger_stage = TRIGGER_FIRED;
318 ctx->usb = NULL;
6d754b6d 319
ea9cfed7 320 return ctx;
6d754b6d
BV
321}
322
323
a1bb33af
UH
324/*
325 * API callbacks
326 */
327
bb7ef793 328static int hw_init(const char *devinfo)
a1bb33af 329{
d68e2d1a 330 struct sr_dev_inst *sdi;
a1bb33af 331 struct libusb_device_descriptor des;
6d754b6d 332 struct fx2_profile *fx2_prof;
ea9cfed7 333 struct context *ctx;
a1bb33af 334 libusb_device **devlist;
e10d6e32 335 int err, devcnt, i, j;
a1bb33af 336
17e1afcb 337 /* Avoid compiler warnings. */
bb7ef793 338 (void)devinfo;
afc8e4de 339
6f5f21f9 340 if (libusb_init(&usb_context) != 0) {
7b48d6e1 341 sr_err("logic: Failed to initialize USB.");
a1bb33af
UH
342 return 0;
343 }
a1bb33af 344
6f5f21f9 345 /* Find all Saleae Logic devices and upload firmware to all of them. */
a1bb33af
UH
346 devcnt = 0;
347 libusb_get_device_list(usb_context, &devlist);
6f5f21f9 348 for (i = 0; devlist[i]; i++) {
6d754b6d 349 fx2_prof = NULL;
a1bb33af 350 err = libusb_get_device_descriptor(devlist[i], &des);
6f5f21f9 351 if (err != 0) {
7b48d6e1
UH
352 sr_err("logic: failed to get device descriptor: %d",
353 err);
a1bb33af
UH
354 continue;
355 }
356
e10d6e32
BV
357 for (j = 0; supported_fx2[j].orig_vid; j++) {
358 if (des.idVendor == supported_fx2[j].orig_vid
359 && des.idProduct == supported_fx2[j].orig_pid) {
6d754b6d 360 fx2_prof = &supported_fx2[j];
e10d6e32
BV
361 break;
362 }
363 }
6d754b6d 364 if (!fx2_prof)
e10d6e32
BV
365 /* not a supported VID/PID */
366 continue;
a1bb33af 367
d3683c42 368 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
6d754b6d 369 fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version);
f6958dab
UH
370 if (!sdi)
371 return 0;
ea9cfed7
UH
372 ctx = fx2_dev_new();
373 ctx->profile = fx2_prof;
374 sdi->priv = ctx;
d68e2d1a 375 dev_insts = g_slist_append(dev_insts, sdi);
6f5f21f9 376
6d754b6d 377 if (check_conf_profile(devlist[i])) {
f6958dab 378 /* Already has the firmware, so fix the new address. */
7b48d6e1 379 sr_dbg("logic: Found a Saleae Logic with %s firmware.",
003f9beb 380 new_saleae_logic_firmware ? "new" : "old");
e10d6e32 381 sdi->status = SR_ST_INACTIVE;
ea9cfed7 382 ctx->usb = sr_usb_dev_inst_new
f6958dab 383 (libusb_get_bus_number(devlist[i]),
fed16f06 384 libusb_get_device_address(devlist[i]), NULL);
6d754b6d
BV
385 } else {
386 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
387 /* Remember when the firmware on this device was updated */
ea9cfed7 388 g_get_current_time(&ctx->fw_updated);
6d754b6d 389 else
7b48d6e1
UH
390 sr_err("logic: firmware upload failed for "
391 "device %d", devcnt);
ea9cfed7 392 ctx->usb = sr_usb_dev_inst_new
6d754b6d 393 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
a1bb33af 394 }
f6958dab 395 devcnt++;
a1bb33af
UH
396 }
397 libusb_free_device_list(devlist, 1);
398
399 return devcnt;
400}
401
e7eb703f 402static int hw_dev_open(int dev_index)
a1bb33af
UH
403{
404 GTimeVal cur_time;
d68e2d1a 405 struct sr_dev_inst *sdi;
ea9cfed7 406 struct context *ctx;
a1bb33af 407 int timediff, err;
e10d6e32 408
bb7ef793 409 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
6d754b6d 410 return SR_ERR;
ea9cfed7 411 ctx = sdi->priv;
6d754b6d 412
e10d6e32
BV
413 /*
414 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
415 * for the FX2 to renumerate
416 */
6d754b6d 417 err = 0;
ea9cfed7 418 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
7b48d6e1 419 sr_info("logic: waiting for device to reset");
e10d6e32 420 /* takes at least 300ms for the FX2 to be gone from the USB bus */
7b48d6e1 421 g_usleep(300 * 1000);
e10d6e32
BV
422 timediff = 0;
423 while (timediff < MAX_RENUM_DELAY) {
bb7ef793 424 if ((err = sl_open_dev(dev_index)) == SR_OK)
e10d6e32 425 break;
7b48d6e1 426 g_usleep(100 * 1000);
e10d6e32 427 g_get_current_time(&cur_time);
ea9cfed7 428 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
a1bb33af 429 }
7b48d6e1 430 sr_info("logic: device came back after %d ms", timediff);
e10d6e32 431 } else {
bb7ef793 432 err = sl_open_dev(dev_index);
a1bb33af
UH
433 }
434
6d754b6d 435 if (err != SR_OK) {
7b48d6e1 436 sr_err("logic: unable to open device");
e46b8fb1 437 return SR_ERR;
a1bb33af 438 }
ea9cfed7 439 ctx = sdi->priv;
a1bb33af 440
ea9cfed7 441 err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
6f5f21f9 442 if (err != 0) {
7b48d6e1 443 sr_err("logic: Unable to claim interface: %d", err);
e46b8fb1 444 return SR_ERR;
a1bb33af
UH
445 }
446
ea9cfed7 447 if (ctx->cur_samplerate == 0) {
6f5f21f9 448 /* Samplerate hasn't been set; default to the slowest one. */
a9a245b4 449 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
e46b8fb1
UH
450 &supported_samplerates[0]) == SR_ERR)
451 return SR_ERR;
a1bb33af
UH
452 }
453
e46b8fb1 454 return SR_OK;
a1bb33af
UH
455}
456
e7eb703f 457static int hw_dev_close(int dev_index)
a1bb33af 458{
d68e2d1a 459 struct sr_dev_inst *sdi;
a1bb33af 460
bb7ef793 461 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
697785d1
UH
462 sr_err("logic: %s: sdi was NULL", __func__);
463 return SR_ERR; /* TODO: SR_ERR_ARG? */
464 }
465
466 /* TODO */
bb7ef793 467 close_dev(sdi);
697785d1
UH
468
469 return SR_OK;
a1bb33af
UH
470}
471
57ab7d9f 472static int hw_cleanup(void)
a1bb33af
UH
473{
474 GSList *l;
d68e2d1a 475 struct sr_dev_inst *sdi;
ea9cfed7 476 struct context *ctx;
57ab7d9f 477 int ret = SR_OK;
a1bb33af 478
69890f73 479 /* Properly close and free all devices. */
d68e2d1a 480 for (l = dev_insts; l; l = l->next) {
57ab7d9f
UH
481 if (!(sdi = l->data)) {
482 /* Log error, but continue cleaning up the rest. */
7b48d6e1 483 sr_err("logic: %s: sdi was NULL, continuing", __func__);
57ab7d9f
UH
484 ret = SR_ERR_BUG;
485 continue;
486 }
ea9cfed7 487 if (!(ctx = sdi->priv)) {
57ab7d9f 488 /* Log error, but continue cleaning up the rest. */
7b48d6e1 489 sr_err("logic: %s: sdi->priv was NULL, continuing",
57ab7d9f
UH
490 __func__);
491 ret = SR_ERR_BUG;
492 continue;
493 }
bb7ef793 494 close_dev(sdi);
ea9cfed7 495 sr_usb_dev_inst_free(ctx->usb);
d3683c42 496 sr_dev_inst_free(sdi);
69890f73 497 }
a1bb33af 498
d68e2d1a
UH
499 g_slist_free(dev_insts);
500 dev_insts = NULL;
a1bb33af 501
6f5f21f9 502 if (usb_context)
a1bb33af
UH
503 libusb_exit(usb_context);
504 usb_context = NULL;
57ab7d9f
UH
505
506 return ret;
a1bb33af
UH
507}
508
5097b0d0 509static void *hw_dev_info_get(int dev_index, int dev_info_id)
a1bb33af 510{
d68e2d1a 511 struct sr_dev_inst *sdi;
ea9cfed7 512 struct context *ctx;
6f5f21f9 513 void *info = NULL;
a1bb33af 514
bb7ef793 515 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
a1bb33af 516 return NULL;
ea9cfed7 517 ctx = sdi->priv;
a1bb33af 518
bb7ef793 519 switch (dev_info_id) {
1d9a8a5f 520 case SR_DI_INST:
a1bb33af
UH
521 info = sdi;
522 break;
5a2326a7 523 case SR_DI_NUM_PROBES:
ea9cfed7 524 info = GINT_TO_POINTER(ctx->profile->num_probes);
a1bb33af 525 break;
464d12c7
KS
526 case SR_DI_PROBE_NAMES:
527 info = probe_names;
528 break;
5a2326a7 529 case SR_DI_SAMPLERATES:
a1bb33af
UH
530 info = &samplerates;
531 break;
5a2326a7 532 case SR_DI_TRIGGER_TYPES:
a1bb33af
UH
533 info = TRIGGER_TYPES;
534 break;
5a2326a7 535 case SR_DI_CUR_SAMPLERATE:
ea9cfed7 536 info = &ctx->cur_samplerate;
a1bb33af
UH
537 break;
538 }
539
540 return info;
541}
542
e7eb703f 543static int hw_dev_status_get(int dev_index)
a1bb33af 544{
d68e2d1a 545 struct sr_dev_inst *sdi;
a1bb33af 546
bb7ef793 547 sdi = sr_dev_inst_get(dev_insts, dev_index);
6f5f21f9 548 if (sdi)
a1bb33af
UH
549 return sdi->status;
550 else
5a2326a7 551 return SR_ST_NOT_FOUND;
a1bb33af
UH
552}
553
ffedd0bf 554static int *hw_hwcap_get_all(void)
a1bb33af 555{
ffedd0bf 556 return hwcaps;
a1bb33af
UH
557}
558
003f9beb
UH
559static uint8_t new_firmware_divider_value(uint64_t samplerate)
560{
561 switch (samplerate) {
562 case SR_MHZ(24):
563 return 0xe0;
564 break;
565 case SR_MHZ(16):
566 return 0xd5;
567 break;
568 case SR_MHZ(12):
569 return 0xe2;
570 break;
571 case SR_MHZ(8):
572 return 0xd4;
573 break;
574 case SR_MHZ(4):
575 return 0xda;
576 break;
577 case SR_MHZ(2):
578 return 0xe6;
579 break;
580 case SR_MHZ(1):
581 return 0x8e;
582 break;
583 case SR_KHZ(500):
584 return 0xfe;
585 break;
586 case SR_KHZ(250):
587 return 0x9e;
588 break;
589 case SR_KHZ(200):
590 return 0x4e;
591 break;
592 }
593
594 /* Shouldn't happen. */
7b48d6e1 595 sr_err("logic: %s: Invalid samplerate %" PRIu64 "",
003f9beb
UH
596 __func__, samplerate);
597 return 0;
598}
599
a9a245b4 600static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
a1bb33af 601{
ea9cfed7 602 struct context *ctx;
a1bb33af
UH
603 uint8_t divider;
604 int ret, result, i;
605 unsigned char buf[2];
606
ea9cfed7 607 ctx = sdi->priv;
6f5f21f9
UH
608 for (i = 0; supported_samplerates[i]; i++) {
609 if (supported_samplerates[i] == samplerate)
a1bb33af
UH
610 break;
611 }
6f5f21f9 612 if (supported_samplerates[i] == 0)
e46b8fb1 613 return SR_ERR_SAMPLERATE;
a1bb33af 614
003f9beb
UH
615 if (new_saleae_logic_firmware)
616 divider = new_firmware_divider_value(samplerate);
617 else
618 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
a1bb33af 619
7b48d6e1 620 sr_info("logic: setting samplerate to %" PRIu64 " Hz (divider %d)",
b08024a8 621 samplerate, divider);
003f9beb
UH
622
623 buf[0] = (new_saleae_logic_firmware) ? 0xd5 : 0x01;
a1bb33af 624 buf[1] = divider;
ea9cfed7 625 ret = libusb_bulk_transfer(ctx->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
6f5f21f9
UH
626 buf, 2, &result, 500);
627 if (ret != 0) {
7b48d6e1 628 sr_err("logic: failed to set samplerate: %d", ret);
e46b8fb1 629 return SR_ERR;
a1bb33af 630 }
ea9cfed7 631 ctx->cur_samplerate = samplerate;
a1bb33af 632
e46b8fb1 633 return SR_OK;
a1bb33af
UH
634}
635
a9a245b4 636static int hw_dev_config_set(int dev_index, int hwcap, void *value)
a1bb33af 637{
d68e2d1a 638 struct sr_dev_inst *sdi;
ea9cfed7 639 struct context *ctx;
a1bb33af 640 int ret;
a1bb33af 641
bb7ef793 642 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
e46b8fb1 643 return SR_ERR;
ea9cfed7 644 ctx = sdi->priv;
a1bb33af 645
ffedd0bf 646 if (hwcap == SR_HWCAP_SAMPLERATE) {
ee61b340 647 ret = set_samplerate(sdi, *(uint64_t *)value);
ffedd0bf 648 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
ea9cfed7 649 ret = configure_probes(ctx, (GSList *) value);
ffedd0bf 650 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
ea9cfed7 651 ctx->limit_samples = *(uint64_t *)value;
e46b8fb1 652 ret = SR_OK;
6f5f21f9 653 } else {
e46b8fb1 654 ret = SR_ERR;
6f5f21f9 655 }
a1bb33af
UH
656
657 return ret;
658}
659
a1bb33af
UH
660static int receive_data(int fd, int revents, void *user_data)
661{
662 struct timeval tv;
663
17e1afcb 664 /* Avoid compiler warnings. */
cb93f8a9
UH
665 (void)fd;
666 (void)revents;
667 (void)user_data;
afc8e4de 668
a1bb33af
UH
669 tv.tv_sec = tv.tv_usec = 0;
670 libusb_handle_events_timeout(usb_context, &tv);
671
672 return TRUE;
673}
674
a0ecd83b 675static void receive_transfer(struct libusb_transfer *transfer)
a1bb33af 676{
ea9cfed7 677 /* TODO: These statics have to move to the ctx struct. */
a1bb33af
UH
678 static int num_samples = 0;
679 static int empty_transfer_count = 0;
b9c735a2 680 struct sr_datafeed_packet packet;
9c939c51 681 struct sr_datafeed_logic logic;
ea9cfed7 682 struct context *ctx;
a1bb33af
UH
683 int cur_buflen, trigger_offset, i;
684 unsigned char *cur_buf, *new_buf;
685
69040b7c 686 /* hw_dev_acquisition_stop() is telling us to stop. */
f6958dab 687 if (transfer == NULL)
a1bb33af 688 num_samples = -1;
a1bb33af 689
f6958dab
UH
690 /*
691 * If acquisition has already ended, just free any queued up
692 * transfer that come in.
693 */
6f5f21f9 694 if (num_samples == -1) {
9c48090a
BV
695 if (transfer)
696 libusb_free_transfer(transfer);
f6958dab
UH
697 return;
698 }
a1bb33af 699
7b48d6e1 700 sr_info("logic: receive_transfer(): status %d received %d bytes",
b08024a8 701 transfer->status, transfer->actual_length);
f6958dab
UH
702
703 /* Save incoming transfer before reusing the transfer struct. */
704 cur_buf = transfer->buffer;
705 cur_buflen = transfer->actual_length;
ea9cfed7 706 ctx = transfer->user_data;
f6958dab
UH
707
708 /* Fire off a new request. */
b53738ba 709 if (!(new_buf = g_try_malloc(4096))) {
7b48d6e1 710 sr_err("logic: %s: new_buf malloc failed", __func__);
133a37bf 711 return; /* TODO: SR_ERR_MALLOC */
b53738ba
UH
712 }
713
f6958dab
UH
714 transfer->buffer = new_buf;
715 transfer->length = 4096;
716 if (libusb_submit_transfer(transfer) != 0) {
717 /* TODO: Stop session? */
7b48d6e1
UH
718 /* TODO: Better error message. */
719 sr_err("logic: %s: libusb_submit_transfer error", __func__);
f6958dab
UH
720 }
721
722 if (cur_buflen == 0) {
723 empty_transfer_count++;
724 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
725 /*
726 * The FX2 gave up. End the acquisition, the frontend
727 * will work out that the samplecount is short.
728 */
ea9cfed7 729 hw_dev_acquisition_stop(-1, ctx->session_data);
6f5f21f9 730 }
f6958dab
UH
731 return;
732 } else {
733 empty_transfer_count = 0;
734 }
a1bb33af 735
f6958dab 736 trigger_offset = 0;
ea9cfed7 737 if (ctx->trigger_stage >= 0) {
f6958dab 738 for (i = 0; i < cur_buflen; i++) {
b5698bd7 739
ea9cfed7 740 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
b5698bd7 741 /* Match on this trigger stage. */
ea9cfed7
UH
742 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
743 ctx->trigger_stage++;
a634574e 744
ea9cfed7 745 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
b5698bd7
BV
746 /* Match on all trigger stages, we're done. */
747 trigger_offset = i + 1;
748
749 /*
750 * TODO: Send pre-trigger buffer to session bus.
751 * Tell the frontend we hit the trigger here.
752 */
5a2326a7 753 packet.type = SR_DF_TRIGGER;
9c939c51 754 packet.payload = NULL;
ea9cfed7 755 sr_session_bus(ctx->session_data, &packet);
b5698bd7
BV
756
757 /*
758 * Send the samples that triggered it, since we're
759 * skipping past them.
760 */
5a2326a7 761 packet.type = SR_DF_LOGIC;
9c939c51 762 packet.payload = &logic;
ea9cfed7 763 logic.length = ctx->trigger_stage;
9c939c51 764 logic.unitsize = 1;
ea9cfed7
UH
765 logic.data = ctx->trigger_buffer;
766 sr_session_bus(ctx->session_data, &packet);
b5698bd7 767
ea9cfed7 768 ctx->trigger_stage = TRIGGER_FIRED;
b5698bd7
BV
769 break;
770 }
771 return;
772 }
773
774 /*
775 * We had a match before, but not in the next sample. However, we may
776 * have a match on this stage in the next bit -- trigger on 0001 will
777 * fail on seeing 00001, so we need to go back to stage 0 -- but at
778 * the next sample from the one that matched originally, which the
779 * counter increment at the end of the loop takes care of.
780 */
ea9cfed7
UH
781 if (ctx->trigger_stage > 0) {
782 i -= ctx->trigger_stage;
b5698bd7
BV
783 if (i < -1)
784 i = -1; /* Oops, went back past this buffer. */
785 /* Reset trigger stage. */
ea9cfed7 786 ctx->trigger_stage = 0;
b5698bd7 787 }
a1bb33af 788 }
f6958dab 789 }
a1bb33af 790
ea9cfed7 791 if (ctx->trigger_stage == TRIGGER_FIRED) {
f6958dab 792 /* Send the incoming transfer to the session bus. */
5a2326a7 793 packet.type = SR_DF_LOGIC;
9c939c51
BV
794 packet.payload = &logic;
795 logic.length = cur_buflen - trigger_offset;
796 logic.unitsize = 1;
797 logic.data = cur_buf + trigger_offset;
ea9cfed7 798 sr_session_bus(ctx->session_data, &packet);
f6958dab
UH
799 g_free(cur_buf);
800
801 num_samples += cur_buflen;
ea9cfed7
UH
802 if (ctx->limit_samples && (unsigned int) num_samples > ctx->limit_samples) {
803 hw_dev_acquisition_stop(-1, ctx->session_data);
a1bb33af 804 }
f6958dab
UH
805 } else {
806 /*
807 * TODO: Buffer pre-trigger data in capture
808 * ratio-sized buffer.
809 */
a1bb33af 810 }
a1bb33af
UH
811}
812
69040b7c 813static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
a1bb33af 814{
d68e2d1a 815 struct sr_dev_inst *sdi;
b9c735a2
UH
816 struct sr_datafeed_packet *packet;
817 struct sr_datafeed_header *header;
ea9cfed7 818 struct context *ctx;
a1bb33af
UH
819 struct libusb_transfer *transfer;
820 const struct libusb_pollfd **lupfd;
821 int size, i;
822 unsigned char *buf;
823
bb7ef793 824 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
e46b8fb1 825 return SR_ERR;
ea9cfed7
UH
826 ctx = sdi->priv;
827 ctx->session_data = session_data;
a1bb33af 828
b53738ba 829 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
7b48d6e1 830 sr_err("logic: %s: packet malloc failed", __func__);
b53738ba
UH
831 return SR_ERR_MALLOC;
832 }
9c939c51 833
b53738ba 834 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
7b48d6e1 835 sr_err("logic: %s: header malloc failed", __func__);
b53738ba
UH
836 return SR_ERR_MALLOC;
837 }
a1bb33af 838
6f5f21f9 839 /* Start with 2K transfer, subsequently increased to 4K. */
a1bb33af 840 size = 2048;
6f5f21f9 841 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
b53738ba 842 if (!(buf = g_try_malloc(size))) {
7b48d6e1 843 sr_err("logic: %s: buf malloc failed", __func__);
b53738ba
UH
844 return SR_ERR_MALLOC;
845 }
a1bb33af 846 transfer = libusb_alloc_transfer(0);
ea9cfed7 847 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
6f5f21f9 848 2 | LIBUSB_ENDPOINT_IN, buf, size,
ea9cfed7 849 receive_transfer, ctx, 40);
6f5f21f9
UH
850 if (libusb_submit_transfer(transfer) != 0) {
851 /* TODO: Free them all. */
a1bb33af
UH
852 libusb_free_transfer(transfer);
853 g_free(buf);
e46b8fb1 854 return SR_ERR;
a1bb33af
UH
855 }
856 size = 4096;
857 }
858
859 lupfd = libusb_get_pollfds(usb_context);
6f5f21f9 860 for (i = 0; lupfd[i]; i++)
6f1be0a2
UH
861 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
862 NULL);
133a37bf 863 free(lupfd); /* NOT g_free()! */
a1bb33af 864
5a2326a7 865 packet->type = SR_DF_HEADER;
9c939c51 866 packet->payload = header;
a1bb33af
UH
867 header->feed_version = 1;
868 gettimeofday(&header->starttime, NULL);
ea9cfed7
UH
869 header->samplerate = ctx->cur_samplerate;
870 header->num_logic_probes = ctx->profile->num_probes;
6d754b6d 871 sr_session_bus(session_data, packet);
a1bb33af
UH
872 g_free(header);
873 g_free(packet);
874
e46b8fb1 875 return SR_OK;
a1bb33af
UH
876}
877
bb7ef793 878/* This stops acquisition on ALL devices, ignoring dev_index. */
69040b7c 879static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
a1bb33af 880{
b9c735a2 881 struct sr_datafeed_packet packet;
a1bb33af 882
17e1afcb 883 /* Avoid compiler warnings. */
bb7ef793 884 (void)dev_index;
afc8e4de 885
5a2326a7 886 packet.type = SR_DF_END;
6d754b6d 887 sr_session_bus(session_data, &packet);
a1bb33af
UH
888
889 receive_transfer(NULL);
890
6f5f21f9 891 /* TODO: Need to cancel and free any queued up transfers. */
3010f21c
UH
892
893 return SR_OK;
a1bb33af
UH
894}
895
bb7ef793 896SR_PRIV struct sr_dev_plugin saleae_logic_plugin_info = {
e519ba86
UH
897 .name = "saleae-logic",
898 .longname = "Saleae Logic",
899 .api_version = 1,
900 .init = hw_init,
901 .cleanup = hw_cleanup,
e7eb703f
UH
902 .dev_open = hw_dev_open,
903 .dev_close = hw_dev_close,
5097b0d0 904 .dev_info_get = hw_dev_info_get,
e7eb703f 905 .dev_status_get = hw_dev_status_get,
ffedd0bf 906 .hwcap_get_all = hw_hwcap_get_all,
a9a245b4 907 .dev_config_set = hw_dev_config_set,
69040b7c
UH
908 .dev_acquisition_start = hw_dev_acquisition_start,
909 .dev_acquisition_stop = hw_dev_acquisition_stop,
a1bb33af 910};