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