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