]> sigrok.org Git - libsigrok.git/blame - hardware/saleae-logic/saleae-logic.c
sr: s/err/ret/ for consistency.
[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);
3cd3a20b 103static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
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;
ebc34738 180 int ret, skip, i;
a1bb33af 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 192 for (i = 0; devlist[i]; i++) {
ebc34738
UH
193 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
194 sr_err("logic: failed to get device descriptor: %d", ret);
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
ebc34738 219 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
ea9cfed7 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 {
ebc34738 232 sr_err("logic: failed to open device: %d", ret);
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;
ebc34738 335 int ret, 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;
ebc34738
UH
350 ret = libusb_get_device_descriptor(devlist[i], &des);
351 if (ret != 0) {
7b48d6e1 352 sr_err("logic: failed to get device descriptor: %d",
ebc34738 353 ret);
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;
ebc34738 407 int timediff, ret;
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 */
ebc34738 417 ret = 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) {
ebc34738 424 if ((ret = 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 {
ebc34738 432 ret = sl_open_dev(dev_index);
a1bb33af
UH
433 }
434
ebc34738 435 if (ret != 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
ebc34738
UH
441 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
442 if (ret != 0) {
443 sr_err("logic: Unable to claim interface: %d", ret);
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 462 sr_err("logic: %s: sdi was NULL", __func__);
0abee507 463 return SR_ERR_BUG;
697785d1
UH
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
1f9813eb 660static int receive_data(int fd, int revents, void *cb_data)
a1bb33af
UH
661{
662 struct timeval tv;
663
17e1afcb 664 /* Avoid compiler warnings. */
cb93f8a9
UH
665 (void)fd;
666 (void)revents;
1f9813eb 667 (void)cb_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
266784d5
JH
675static void abort_acquisition(struct context *ctx)
676{
677 struct sr_datafeed_packet packet;
678
679 packet.type = SR_DF_END;
680 sr_session_send(ctx->session_dev_id, &packet);
681
682 ctx->num_samples = -1;
683
684 /* TODO: Need to cancel and free any queued up transfers. */
685}
686
a0ecd83b 687static void receive_transfer(struct libusb_transfer *transfer)
a1bb33af 688{
ea9cfed7 689 /* TODO: These statics have to move to the ctx struct. */
a1bb33af 690 static int empty_transfer_count = 0;
b9c735a2 691 struct sr_datafeed_packet packet;
9c939c51 692 struct sr_datafeed_logic logic;
266784d5 693 struct context *ctx = transfer->user_data;
a1bb33af
UH
694 int cur_buflen, trigger_offset, i;
695 unsigned char *cur_buf, *new_buf;
696
f6958dab
UH
697 /*
698 * If acquisition has already ended, just free any queued up
699 * transfer that come in.
700 */
266784d5 701 if (ctx->num_samples == -1) {
9c48090a
BV
702 if (transfer)
703 libusb_free_transfer(transfer);
f6958dab
UH
704 return;
705 }
a1bb33af 706
7b48d6e1 707 sr_info("logic: receive_transfer(): status %d received %d bytes",
b08024a8 708 transfer->status, transfer->actual_length);
f6958dab
UH
709
710 /* Save incoming transfer before reusing the transfer struct. */
711 cur_buf = transfer->buffer;
712 cur_buflen = transfer->actual_length;
ea9cfed7 713 ctx = transfer->user_data;
f6958dab
UH
714
715 /* Fire off a new request. */
b53738ba 716 if (!(new_buf = g_try_malloc(4096))) {
7b48d6e1 717 sr_err("logic: %s: new_buf malloc failed", __func__);
133a37bf 718 return; /* TODO: SR_ERR_MALLOC */
b53738ba
UH
719 }
720
f6958dab
UH
721 transfer->buffer = new_buf;
722 transfer->length = 4096;
723 if (libusb_submit_transfer(transfer) != 0) {
724 /* TODO: Stop session? */
7b48d6e1
UH
725 /* TODO: Better error message. */
726 sr_err("logic: %s: libusb_submit_transfer error", __func__);
f6958dab
UH
727 }
728
729 if (cur_buflen == 0) {
730 empty_transfer_count++;
731 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
732 /*
733 * The FX2 gave up. End the acquisition, the frontend
734 * will work out that the samplecount is short.
735 */
266784d5 736 abort_acquisition(ctx);
6f5f21f9 737 }
f6958dab
UH
738 return;
739 } else {
740 empty_transfer_count = 0;
741 }
a1bb33af 742
f6958dab 743 trigger_offset = 0;
ea9cfed7 744 if (ctx->trigger_stage >= 0) {
f6958dab 745 for (i = 0; i < cur_buflen; i++) {
b5698bd7 746
ea9cfed7 747 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
b5698bd7 748 /* Match on this trigger stage. */
ea9cfed7
UH
749 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
750 ctx->trigger_stage++;
a634574e 751
ea9cfed7 752 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
b5698bd7
BV
753 /* Match on all trigger stages, we're done. */
754 trigger_offset = i + 1;
755
756 /*
757 * TODO: Send pre-trigger buffer to session bus.
758 * Tell the frontend we hit the trigger here.
759 */
5a2326a7 760 packet.type = SR_DF_TRIGGER;
9c939c51 761 packet.payload = NULL;
3cd3a20b 762 sr_session_send(ctx->session_dev_id, &packet);
b5698bd7
BV
763
764 /*
765 * Send the samples that triggered it, since we're
766 * skipping past them.
767 */
5a2326a7 768 packet.type = SR_DF_LOGIC;
9c939c51 769 packet.payload = &logic;
ea9cfed7 770 logic.length = ctx->trigger_stage;
9c939c51 771 logic.unitsize = 1;
ea9cfed7 772 logic.data = ctx->trigger_buffer;
3cd3a20b 773 sr_session_send(ctx->session_dev_id, &packet);
b5698bd7 774
ea9cfed7 775 ctx->trigger_stage = TRIGGER_FIRED;
b5698bd7
BV
776 break;
777 }
778 return;
779 }
780
781 /*
782 * We had a match before, but not in the next sample. However, we may
783 * have a match on this stage in the next bit -- trigger on 0001 will
784 * fail on seeing 00001, so we need to go back to stage 0 -- but at
785 * the next sample from the one that matched originally, which the
786 * counter increment at the end of the loop takes care of.
787 */
ea9cfed7
UH
788 if (ctx->trigger_stage > 0) {
789 i -= ctx->trigger_stage;
b5698bd7
BV
790 if (i < -1)
791 i = -1; /* Oops, went back past this buffer. */
792 /* Reset trigger stage. */
ea9cfed7 793 ctx->trigger_stage = 0;
b5698bd7 794 }
a1bb33af 795 }
f6958dab 796 }
a1bb33af 797
ea9cfed7 798 if (ctx->trigger_stage == TRIGGER_FIRED) {
f6958dab 799 /* Send the incoming transfer to the session bus. */
5a2326a7 800 packet.type = SR_DF_LOGIC;
9c939c51
BV
801 packet.payload = &logic;
802 logic.length = cur_buflen - trigger_offset;
803 logic.unitsize = 1;
804 logic.data = cur_buf + trigger_offset;
3cd3a20b 805 sr_session_send(ctx->session_dev_id, &packet);
f6958dab
UH
806 g_free(cur_buf);
807
266784d5
JH
808 ctx->num_samples += cur_buflen;
809 if (ctx->limit_samples && (unsigned int)ctx->num_samples > ctx->limit_samples) {
810 abort_acquisition(ctx);
a1bb33af 811 }
f6958dab
UH
812 } else {
813 /*
814 * TODO: Buffer pre-trigger data in capture
815 * ratio-sized buffer.
816 */
a1bb33af 817 }
a1bb33af
UH
818}
819
3cd3a20b 820static int hw_dev_acquisition_start(int dev_index, void *cb_data)
a1bb33af 821{
d68e2d1a 822 struct sr_dev_inst *sdi;
b9c735a2
UH
823 struct sr_datafeed_packet *packet;
824 struct sr_datafeed_header *header;
ea9cfed7 825 struct context *ctx;
a1bb33af
UH
826 struct libusb_transfer *transfer;
827 const struct libusb_pollfd **lupfd;
828 int size, i;
829 unsigned char *buf;
830
bb7ef793 831 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
e46b8fb1 832 return SR_ERR;
ea9cfed7 833 ctx = sdi->priv;
3cd3a20b 834 ctx->session_dev_id = cb_data;
266784d5 835 ctx->num_samples = 0;
a1bb33af 836
b53738ba 837 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
7b48d6e1 838 sr_err("logic: %s: packet malloc failed", __func__);
b53738ba
UH
839 return SR_ERR_MALLOC;
840 }
9c939c51 841
b53738ba 842 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
7b48d6e1 843 sr_err("logic: %s: header malloc failed", __func__);
b53738ba
UH
844 return SR_ERR_MALLOC;
845 }
a1bb33af 846
6f5f21f9 847 /* Start with 2K transfer, subsequently increased to 4K. */
a1bb33af 848 size = 2048;
6f5f21f9 849 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
b53738ba 850 if (!(buf = g_try_malloc(size))) {
7b48d6e1 851 sr_err("logic: %s: buf malloc failed", __func__);
b53738ba
UH
852 return SR_ERR_MALLOC;
853 }
a1bb33af 854 transfer = libusb_alloc_transfer(0);
ea9cfed7 855 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
6f5f21f9 856 2 | LIBUSB_ENDPOINT_IN, buf, size,
ea9cfed7 857 receive_transfer, ctx, 40);
6f5f21f9
UH
858 if (libusb_submit_transfer(transfer) != 0) {
859 /* TODO: Free them all. */
a1bb33af
UH
860 libusb_free_transfer(transfer);
861 g_free(buf);
e46b8fb1 862 return SR_ERR;
a1bb33af
UH
863 }
864 size = 4096;
865 }
866
867 lupfd = libusb_get_pollfds(usb_context);
6f5f21f9 868 for (i = 0; lupfd[i]; i++)
6f1be0a2
UH
869 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
870 NULL);
133a37bf 871 free(lupfd); /* NOT g_free()! */
a1bb33af 872
5a2326a7 873 packet->type = SR_DF_HEADER;
9c939c51 874 packet->payload = header;
a1bb33af
UH
875 header->feed_version = 1;
876 gettimeofday(&header->starttime, NULL);
ea9cfed7
UH
877 header->samplerate = ctx->cur_samplerate;
878 header->num_logic_probes = ctx->profile->num_probes;
3cd3a20b 879 sr_session_send(ctx->session_dev_id, packet);
a1bb33af
UH
880 g_free(header);
881 g_free(packet);
882
e46b8fb1 883 return SR_OK;
a1bb33af
UH
884}
885
3cd3a20b
UH
886/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
887static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
a1bb33af 888{
266784d5 889 struct sr_dev_inst *sdi;
afc8e4de 890
266784d5
JH
891 /* unused parameter */
892 (void)cb_data;
a1bb33af 893
266784d5
JH
894 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
895 return SR_ERR;
a1bb33af 896
266784d5 897 abort_acquisition(sdi->priv);
3010f21c
UH
898
899 return SR_OK;
a1bb33af
UH
900}
901
c09f0b57 902SR_PRIV struct sr_dev_driver saleae_logic_driver_info = {
e519ba86
UH
903 .name = "saleae-logic",
904 .longname = "Saleae Logic",
905 .api_version = 1,
906 .init = hw_init,
907 .cleanup = hw_cleanup,
e7eb703f
UH
908 .dev_open = hw_dev_open,
909 .dev_close = hw_dev_close,
5097b0d0 910 .dev_info_get = hw_dev_info_get,
e7eb703f 911 .dev_status_get = hw_dev_status_get,
ffedd0bf 912 .hwcap_get_all = hw_hwcap_get_all,
a9a245b4 913 .dev_config_set = hw_dev_config_set,
69040b7c
UH
914 .dev_acquisition_start = hw_dev_acquisition_start,
915 .dev_acquisition_stop = hw_dev_acquisition_stop,
a1bb33af 916};