]> sigrok.org Git - libsigrok.git/blame - hardware/fx2lafw/fx2lafw.c
sr/drivers: fix off-by-one if frontend-initiated probe configuration
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
CommitLineData
f302a082
JH
1/*
2 * This file is part of the sigrok project.
3 *
cf94c816 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
f302a082
JH
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
6b73d9a5 23#include <string.h>
8b35f474 24#include <inttypes.h>
187b3582 25#include <libusb.h>
f302a082 26#include "config.h"
45c59c8b
BV
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
f302a082 29#include "fx2lafw.h"
f6582cd7 30#include "command.h"
f302a082 31
4679d14d 32static const struct fx2lafw_profile supported_fx2[] = {
7ae2f9d5
UH
33 /*
34 * CWAV USBee AX
17b6c75a 35 * EE Electronics ESLA201A
9f05304e 36 * ARMFLY AX-Pro
17b6c75a 37 */
f8b07fc6 38 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
d1ddc7a9
JH
39 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw",
40 0 },
0e8d0e24
IF
41 /*
42 * CWAV USBee DX
43 * XZL-Studio DX
44 */
45 { 0x08a9, 0x0015, "CWAV", "USBee DX", NULL,
46 FIRMWARE_DIR "/fx2lafw-cwav-usbeedx.fw",
47 DEV_CAPS_16BIT },
93a9f3da 48
7ae2f9d5
UH
49 /*
50 * CWAV USBee SX
4502e869
JH
51 */
52 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
d1ddc7a9
JH
53 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw",
54 0 },
4502e869 55
7ae2f9d5
UH
56 /*
57 * Saleae Logic
93a9f3da
JH
58 * EE Electronics ESLA100
59 * Robomotic MiniLogic
1663e470 60 * Robomotic BugLogic 3
93a9f3da
JH
61 */
62 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
d1ddc7a9
JH
63 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw",
64 0 },
93a9f3da 65
f488762a 66 /*
1663e470
UH
67 * Default Cypress FX2 without EEPROM, e.g.:
68 * Lcsoft Mini Board
69 * Braintechnology USB Interface V2.x
f488762a
JH
70 */
71 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
d1ddc7a9
JH
72 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw",
73 DEV_CAPS_16BIT },
f488762a 74
1663e470
UH
75 /*
76 * Braintechnology USB-LPS
77 */
78 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
d1ddc7a9
JH
79 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw",
80 DEV_CAPS_16BIT },
1663e470 81
f8b07fc6 82 { 0, 0, 0, 0, 0, 0, 0 }
187b3582
JH
83};
84
915f7cc8 85static const int hwcaps[] = {
8b35f474
JH
86 SR_HWCAP_LOGIC_ANALYZER,
87 SR_HWCAP_SAMPLERATE,
88
89 /* These are really implemented in the driver, not the hardware. */
90 SR_HWCAP_LIMIT_SAMPLES,
91 SR_HWCAP_CONTINUOUS,
772a0e61 92 0,
8b35f474
JH
93};
94
da686568 95static const char *probe_names[] = {
7ae2f9d5
UH
96 "0",
97 "1",
98 "2",
99 "3",
100 "4",
101 "5",
102 "6",
103 "7",
d1ddc7a9
JH
104 "8",
105 "9",
106 "10",
107 "11",
108 "12",
109 "13",
110 "14",
111 "15",
772a0e61 112 NULL,
8b35f474
JH
113};
114
a533743d 115static const uint64_t supported_samplerates[] = {
79dc6498
JH
116 SR_KHZ(20),
117 SR_KHZ(25),
897c1a2e
JH
118 SR_KHZ(50),
119 SR_KHZ(100),
9304d576
JH
120 SR_KHZ(200),
121 SR_KHZ(250),
122 SR_KHZ(500),
8b35f474
JH
123 SR_MHZ(1),
124 SR_MHZ(2),
125 SR_MHZ(3),
126 SR_MHZ(4),
127 SR_MHZ(6),
128 SR_MHZ(8),
129 SR_MHZ(12),
130 SR_MHZ(16),
772a0e61 131 SR_MHZ(24),
934cde02 132 0,
8b35f474
JH
133};
134
a533743d 135static const struct sr_samplerates samplerates = {
590b9f9a
UH
136 0,
137 0,
138 0,
139 supported_samplerates,
8b35f474
JH
140};
141
187b3582
JH
142static libusb_context *usb_context = NULL;
143
a8cc8e44
BV
144SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
145static struct sr_dev_driver *fdi = &fx2lafw_driver_info;
25a0f108 146static int hw_dev_close(struct sr_dev_inst *sdi);
6f4b1868
BV
147static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
148 const void *value);
3ffb6964
BV
149static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
150 void *cb_data);
610dbb70 151
b1eeb67e
JH
152/**
153 * Check the USB configuration to determine if this is an fx2lafw device.
154 *
f9a69557
UH
155 * @return TRUE if the device's configuration profile match fx2lafw
156 * configuration, FALSE otherwise.
b1eeb67e 157 */
f9a69557 158static gboolean check_conf_profile(libusb_device *dev)
b1eeb67e
JH
159{
160 struct libusb_device_descriptor des;
6b73d9a5
BV
161 struct libusb_device_handle *hdl;
162 gboolean ret;
163 unsigned char strdesc[64];
b1eeb67e 164
6b73d9a5
BV
165 hdl = NULL;
166 ret = FALSE;
b1eeb67e 167 while (!ret) {
da686568 168 /* Assume the FW has not been loaded, unless proven wrong. */
b1eeb67e
JH
169 if (libusb_get_device_descriptor(dev, &des) != 0)
170 break;
171
6b73d9a5 172 if (libusb_open(dev, &hdl) != 0)
b1eeb67e
JH
173 break;
174
6b73d9a5
BV
175 if (libusb_get_string_descriptor_ascii(hdl,
176 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
b1eeb67e 177 break;
6b73d9a5 178 if (strncmp((const char *)strdesc, "sigrok", 6))
b1eeb67e
JH
179 break;
180
6b73d9a5
BV
181 if (libusb_get_string_descriptor_ascii(hdl,
182 des.iProduct, strdesc, sizeof(strdesc)) < 0)
b1eeb67e 183 break;
6b73d9a5 184 if (strncmp((const char *)strdesc, "fx2lafw", 7))
b1eeb67e
JH
185 break;
186
b1eeb67e 187 /* If we made it here, it must be an fx2lafw. */
f9a69557 188 ret = TRUE;
b1eeb67e 189 }
6b73d9a5
BV
190 if (hdl)
191 libusb_close(hdl);
b1eeb67e
JH
192
193 return ret;
194}
195
25a0f108 196static int fx2lafw_dev_open(struct sr_dev_inst *sdi)
43125c69
JH
197{
198 libusb_device **devlist;
199 struct libusb_device_descriptor des;
772a0e61 200 struct context *ctx;
13bf7ecc 201 struct version_info vi;
ebc34738 202 int ret, skip, i;
1e94408a 203 uint8_t revid;
43125c69 204
43125c69
JH
205 ctx = sdi->priv;
206
207 if (sdi->status == SR_ST_ACTIVE)
208 /* already in use */
209 return SR_ERR;
210
211 skip = 0;
6fbe5e60
JH
212 const int device_count = libusb_get_device_list(usb_context, &devlist);
213 if (device_count < 0) {
214 sr_err("fx2lafw: Failed to retrieve device list (%d)",
215 device_count);
216 return SR_ERR;
217 }
218
219 for (i = 0; i < device_count; i++) {
ebc34738 220 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
7ae2f9d5
UH
221 sr_err("fx2lafw: Failed to get device descriptor: %d.",
222 ret);
43125c69
JH
223 continue;
224 }
225
74fcfb80
JH
226 if (des.idVendor != ctx->profile->vid
227 || des.idProduct != ctx->profile->pid)
43125c69
JH
228 continue;
229
230 if (sdi->status == SR_ST_INITIALIZING) {
25a0f108 231 if (skip != sdi->index) {
43125c69
JH
232 /* Skip devices of this type that aren't the one we want. */
233 skip += 1;
234 continue;
235 }
236 } else if (sdi->status == SR_ST_INACTIVE) {
237 /*
238 * This device is fully enumerated, so we need to find
239 * this device by vendor, product, bus and address.
240 */
241 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
242 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
243 /* this is not the one */
244 continue;
245 }
246
ebc34738 247 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
43125c69
JH
248 if (ctx->usb->address == 0xff)
249 /*
250 * first time we touch this device after firmware upload,
251 * so we don't know the address yet.
252 */
253 ctx->usb->address = libusb_get_device_address(devlist[i]);
43125c69 254 } else {
7ae2f9d5 255 sr_err("fx2lafw: Failed to open device: %d.", ret);
13bf7ecc
JH
256 break;
257 }
258
44dfd483
UH
259 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
260 if (ret != SR_OK) {
13bf7ecc 261 sr_err("fx2lafw: Failed to retrieve "
44dfd483 262 "firmware version information.");
13bf7ecc 263 break;
43125c69
JH
264 }
265
1e94408a
UH
266 ret = command_get_revid_version(ctx->usb->devhdl, &revid);
267 if (ret != SR_OK) {
268 sr_err("fx2lafw: Failed to retrieve REVID.");
269 break;
270 }
271
0a8c0c32
UH
272 /*
273 * Changes in major version mean incompatible/API changes, so
274 * bail out if we encounter an incompatible version.
275 * Different minor versions are OK, they should be compatible.
276 */
277 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
278 sr_err("fx2lafw: Expected firmware version %d.x, "
279 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
280 vi.major, vi.minor);
13bf7ecc
JH
281 break;
282 }
283
284 sdi->status = SR_ST_ACTIVE;
285 sr_info("fx2lafw: Opened device %d on %d.%d "
1e94408a 286 "interface %d, firmware %d.%d, REVID %d.",
13bf7ecc 287 sdi->index, ctx->usb->bus, ctx->usb->address,
1e94408a 288 USB_INTERFACE, vi.major, vi.minor, revid);
13bf7ecc 289
43125c69
JH
290 break;
291 }
292 libusb_free_device_list(devlist, 1);
293
294 if (sdi->status != SR_ST_ACTIVE)
295 return SR_ERR;
296
297 return SR_OK;
298}
299
6c6781b6
JH
300static int configure_probes(struct context *ctx, GSList *probes)
301{
302 struct sr_probe *probe;
303 GSList *l;
304 int probe_bit, stage, i;
305 char *tc;
306
307 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
308 ctx->trigger_mask[i] = 0;
309 ctx->trigger_value[i] = 0;
310 }
311
312 stage = -1;
313 for (l = probes; l; l = l->next) {
314 probe = (struct sr_probe *)l->data;
315 if (probe->enabled == FALSE)
316 continue;
d1ddc7a9 317
b35c8293 318 if (probe->index > 7)
0a88ec3d 319 ctx->sample_wide = TRUE;
d1ddc7a9 320
b35c8293 321 probe_bit = 1 << (probe->index);
6c6781b6
JH
322 if (!(probe->trigger))
323 continue;
324
325 stage = 0;
326 for (tc = probe->trigger; *tc; tc++) {
327 ctx->trigger_mask[stage] |= probe_bit;
328 if (*tc == '1')
329 ctx->trigger_value[stage] |= probe_bit;
330 stage++;
331 if (stage > NUM_TRIGGER_STAGES)
332 return SR_ERR;
333 }
334 }
335
336 if (stage == -1)
337 /*
338 * We didn't configure any triggers, make sure acquisition
339 * doesn't wait for any.
340 */
341 ctx->trigger_stage = TRIGGER_FIRED;
342 else
343 ctx->trigger_stage = 0;
344
345 return SR_OK;
346}
347
da686568 348static struct context *fx2lafw_dev_new(void)
187b3582 349{
772a0e61 350 struct context *ctx;
187b3582 351
772a0e61 352 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
7ae2f9d5 353 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
187b3582
JH
354 return NULL;
355 }
356
6c6781b6
JH
357 ctx->trigger_stage = TRIGGER_FIRED;
358
772a0e61 359 return ctx;
187b3582 360}
f302a082 361
f69b47f0
BV
362static int clear_instances(void)
363{
364 GSList *l;
365 struct sr_dev_inst *sdi;
366 struct context *ctx;
367 int ret;
368
369 ret = SR_OK;
370 for (l = fdi->instances; l; l = l->next) {
371 if (!(sdi = l->data)) {
372 /* Log error, but continue cleaning up the rest. */
373 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
374 __func__);
375 ret = SR_ERR_BUG;
376 continue;
377 }
378 if (!(ctx = sdi->priv)) {
379 /* Log error, but continue cleaning up the rest. */
380 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
381 __func__);
382 ret = SR_ERR_BUG;
383 continue;
384 }
25a0f108 385 hw_dev_close(sdi);
f69b47f0
BV
386 sdi = l->data;
387 sr_dev_inst_free(sdi);
388 }
389
390 g_slist_free(fdi->instances);
391 fdi->instances = NULL;
392
393 return ret;
394}
395
396
f302a082
JH
397/*
398 * API callbacks
399 */
400
40dda2c3 401static int hw_init(void)
61136ea6
BV
402{
403
404 if (libusb_init(&usb_context) != 0) {
405 sr_warn("fx2lafw: Failed to initialize libusb.");
406 return SR_ERR;
407 }
408
409 return SR_OK;
410}
411
3a7a22cb 412static GSList *hw_scan(GSList *options)
f302a082 413{
3a7a22cb 414 GSList *devices;
187b3582 415 struct libusb_device_descriptor des;
3a7a22cb 416 struct sr_dev_inst *sdi;
da686568 417 const struct fx2lafw_profile *prof;
772a0e61 418 struct context *ctx;
3a7a22cb 419 struct sr_probe *probe;
187b3582 420 libusb_device **devlist;
3a7a22cb
BV
421 int devcnt, num_logic_probes, ret, i, j;
422
423 /* Avoid compiler warnings. */
424 (void)options;
187b3582 425
f69b47f0
BV
426 /* This scan always invalidates any previous scans. */
427 clear_instances();
428
921634ec 429 /* Find all fx2lafw compatible devices and upload firmware to them. */
3a7a22cb 430 devices = NULL;
187b3582
JH
431 libusb_get_device_list(usb_context, &devlist);
432 for (i = 0; devlist[i]; i++) {
433
ebc34738 434 if ((ret = libusb_get_device_descriptor(
7ae2f9d5
UH
435 devlist[i], &des)) != 0) {
436 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
187b3582
JH
437 continue;
438 }
439
da686568 440 prof = NULL;
187b3582
JH
441 for (j = 0; supported_fx2[j].vid; j++) {
442 if (des.idVendor == supported_fx2[j].vid &&
443 des.idProduct == supported_fx2[j].pid) {
da686568 444 prof = &supported_fx2[j];
187b3582
JH
445 }
446 }
447
448 /* Skip if the device was not found */
da686568 449 if (!prof)
187b3582
JH
450 continue;
451
a8cc8e44 452 devcnt = g_slist_length(fdi->instances);
187b3582 453 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
da686568 454 prof->vendor, prof->model, prof->model_version);
f4a9e5c0 455 if (!sdi)
25a0f108 456 return NULL;
c5e82ca5 457 sdi->driver = fdi;
187b3582 458
06717a8a
BV
459 /* Fill in probelist according to this device's profile. */
460 num_logic_probes = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
c5e82ca5 461 for (j = 0; j < num_logic_probes; j++) {
06717a8a
BV
462 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
463 probe_names[j])))
25a0f108 464 return NULL;
06717a8a
BV
465 sdi->probes = g_slist_append(sdi->probes, probe);
466 }
467
da686568
UH
468 ctx = fx2lafw_dev_new();
469 ctx->profile = prof;
90282c82 470 sdi->priv = ctx;
a8cc8e44
BV
471 fdi->instances = g_slist_append(fdi->instances, sdi);
472 devices = g_slist_append(devices, sdi);
187b3582 473
b1eeb67e
JH
474 if (check_conf_profile(devlist[i])) {
475 /* Already has the firmware, so fix the new address. */
7ae2f9d5 476 sr_dbg("fx2lafw: Found an fx2lafw device.");
b1eeb67e
JH
477 sdi->status = SR_ST_INACTIVE;
478 ctx->usb = sr_usb_dev_inst_new
479 (libusb_get_bus_number(devlist[i]),
480 libusb_get_device_address(devlist[i]), NULL);
481 } else {
f8b07fc6 482 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
da686568 483 prof->firmware) == SR_OK)
b1eeb67e 484 /* Remember when the firmware on this device was updated */
e8bd58ff 485 ctx->fw_updated = g_get_monotonic_time();
b1eeb67e 486 else
7ae2f9d5
UH
487 sr_err("fx2lafw: Firmware upload failed for "
488 "device %d.", devcnt);
b1eeb67e
JH
489 ctx->usb = sr_usb_dev_inst_new
490 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
491 }
187b3582
JH
492 }
493 libusb_free_device_list(devlist, 1);
494
3a7a22cb 495 return devices;
f302a082
JH
496}
497
25a0f108 498static int hw_dev_open(struct sr_dev_inst *sdi)
f302a082 499{
772a0e61 500 struct context *ctx;
e8bd58ff
UH
501 int ret;
502 int64_t timediff_us, timediff_ms;
43125c69 503
43125c69
JH
504 ctx = sdi->priv;
505
506 /*
f60fdf6e
UH
507 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
508 * milliseconds for the FX2 to renumerate.
43125c69 509 */
3b6c1930 510 ret = SR_ERR;
e8bd58ff 511 if (ctx->fw_updated > 0) {
7ae2f9d5 512 sr_info("fx2lafw: Waiting for device to reset.");
43125c69
JH
513 /* takes at least 300ms for the FX2 to be gone from the USB bus */
514 g_usleep(300 * 1000);
e8bd58ff 515 timediff_ms = 0;
f60fdf6e 516 while (timediff_ms < MAX_RENUM_DELAY_MS) {
25a0f108 517 if ((ret = fx2lafw_dev_open(sdi)) == SR_OK)
43125c69
JH
518 break;
519 g_usleep(100 * 1000);
e8bd58ff
UH
520
521 timediff_us = g_get_monotonic_time() - ctx->fw_updated;
3b6c1930
BV
522 timediff_ms = timediff_us / 1000;
523 sr_spew("fx2lafw: waited %" PRIi64 " ms", timediff_ms);
43125c69 524 }
e8bd58ff 525 sr_info("fx2lafw: Device came back after %d ms.", timediff_ms);
43125c69 526 } else {
25a0f108 527 ret = fx2lafw_dev_open(sdi);
43125c69
JH
528 }
529
ebc34738 530 if (ret != SR_OK) {
7ae2f9d5 531 sr_err("fx2lafw: Unable to open device.");
43125c69
JH
532 return SR_ERR;
533 }
534 ctx = sdi->priv;
535
ebc34738
UH
536 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
537 if (ret != 0) {
0c156e06
JH
538 switch(ret) {
539 case LIBUSB_ERROR_BUSY:
540 sr_err("fx2lafw: Unable to claim USB interface. Another "
541 "program or driver has already claimed it.");
542 break;
543
544 case LIBUSB_ERROR_NO_DEVICE:
545 sr_err("fx2lafw: Device has been disconnected.");
546 break;
547
548 default:
549 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
550 break;
551 }
552
43125c69
JH
553 return SR_ERR;
554 }
555
f92994fd
JH
556 if (ctx->cur_samplerate == 0) {
557 /* Samplerate hasn't been set; default to the slowest one. */
6f4b1868 558 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
590b9f9a 559 &supported_samplerates[0]) == SR_ERR)
f92994fd
JH
560 return SR_ERR;
561 }
562
f302a082
JH
563 return SR_OK;
564}
565
25a0f108 566static int hw_dev_close(struct sr_dev_inst *sdi)
f302a082 567{
25a0f108 568 struct context *ctx;
f1898235 569
25a0f108
BV
570 ctx = sdi->priv;
571 if (ctx->usb->devhdl == NULL)
572 return SR_ERR;
f1898235 573
25a0f108
BV
574 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
575 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
576 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
577 libusb_close(ctx->usb->devhdl);
578 ctx->usb->devhdl = NULL;
579 sdi->status = SR_ST_INACTIVE;
f1898235 580
f302a082
JH
581 return SR_OK;
582}
583
584static int hw_cleanup(void)
585{
f69b47f0 586 int ret;
187b3582 587
f69b47f0 588 ret = clear_instances();
187b3582 589
f4a9e5c0 590 if (usb_context)
187b3582
JH
591 libusb_exit(usb_context);
592 usb_context = NULL;
593
62bc70e4 594 return ret;
f302a082
JH
595}
596
cbd798f4 597static int hw_info_get(int info_id, const void **data,
6e9339aa 598 const struct sr_dev_inst *sdi)
f302a082 599{
772a0e61 600 struct context *ctx;
8b35f474 601
cbd798f4 602 switch (info_id) {
8b35f474 603 case SR_DI_INST:
a27999e6
BV
604 *data = sdi;
605 break;
a27999e6
BV
606 case SR_DI_HWCAPS:
607 *data = hwcaps;
608 break;
8b35f474 609 case SR_DI_NUM_PROBES:
6e9339aa
BV
610 if (sdi) {
611 ctx = sdi->priv;
612 *data = GINT_TO_POINTER(
613 (ctx->profile->dev_caps & DEV_CAPS_16BIT) ?
614 16 : 8);
615 } else
616 return SR_ERR;
617 break;
8b35f474 618 case SR_DI_PROBE_NAMES:
6e9339aa
BV
619 *data = probe_names;
620 break;
8b35f474 621 case SR_DI_SAMPLERATES:
6e9339aa
BV
622 *data = &samplerates;
623 break;
8b35f474 624 case SR_DI_TRIGGER_TYPES:
6e9339aa
BV
625 *data = TRIGGER_TYPES;
626 break;
e3186647 627 case SR_DI_CUR_SAMPLERATE:
6e9339aa
BV
628 if (sdi) {
629 ctx = sdi->priv;
630 *data = &ctx->cur_samplerate;
631 } else
632 return SR_ERR;
633 break;
634 default:
635 return SR_ERR_ARG;
8b35f474
JH
636 }
637
6e9339aa 638 return SR_OK;
f302a082
JH
639}
640
6f4b1868
BV
641static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
642 const void *value)
f302a082 643{
772a0e61 644 struct context *ctx;
7cb621d4
JH
645 int ret;
646
7cb621d4
JH
647 ctx = sdi->priv;
648
e3186647 649 if (hwcap == SR_HWCAP_SAMPLERATE) {
1b79df2f 650 ctx->cur_samplerate = *(const uint64_t *)value;
e3186647
JH
651 ret = SR_OK;
652 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
6c6781b6 653 ret = configure_probes(ctx, (GSList *) value);
e3186647 654 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
1b79df2f 655 ctx->limit_samples = *(const uint64_t *)value;
7cb621d4
JH
656 ret = SR_OK;
657 } else {
658 ret = SR_ERR;
659 }
660
661 return ret;
f302a082
JH
662}
663
1f9813eb 664static int receive_data(int fd, int revents, void *cb_data)
610dbb70
JH
665{
666 struct timeval tv;
667
668 /* Avoid compiler warnings. */
669 (void)fd;
670 (void)revents;
1f9813eb 671 (void)cb_data;
610dbb70
JH
672
673 tv.tv_sec = tv.tv_usec = 0;
674 libusb_handle_events_timeout(usb_context, &tv);
675
676 return TRUE;
677}
678
2e526f4a 679static void abort_acquisition(struct context *ctx)
cb61e9f7 680{
e7d087bf 681 int i;
0caa1ef0 682
cb61e9f7 683 ctx->num_samples = -1;
0caa1ef0 684
e7d087bf 685 for (i = ctx->num_transfers - 1; i >= 0; i--) {
0caa1ef0
LPC
686 if (ctx->transfers[i])
687 libusb_cancel_transfer(ctx->transfers[i]);
688 }
cb61e9f7
JH
689}
690
3e6292b2 691static void finish_acquisition(struct context *ctx)
2e526f4a
JH
692{
693 struct sr_datafeed_packet packet;
cb61e9f7 694 int i;
2e526f4a 695
cb61e9f7 696 /* Terminate session */
2e526f4a
JH
697 packet.type = SR_DF_END;
698 sr_session_send(ctx->session_dev_id, &packet);
699
cb61e9f7
JH
700 /* Remove fds from polling */
701 const struct libusb_pollfd **const lupfd =
702 libusb_get_pollfds(usb_context);
703 for (i = 0; lupfd[i]; i++)
704 sr_source_remove(lupfd[i]->fd);
705 free(lupfd); /* NOT g_free()! */
0caa1ef0
LPC
706
707 ctx->num_transfers = 0;
708 g_free(ctx->transfers);
2e526f4a
JH
709}
710
f855de75
LPC
711static void free_transfer(struct libusb_transfer *transfer)
712{
713 struct context *ctx = transfer->user_data;
0caa1ef0 714 unsigned int i;
f855de75
LPC
715
716 g_free(transfer->buffer);
717 transfer->buffer = NULL;
718 libusb_free_transfer(transfer);
719
0caa1ef0
LPC
720 for (i = 0; i < ctx->num_transfers; i++) {
721 if (ctx->transfers[i] == transfer) {
722 ctx->transfers[i] = NULL;
723 break;
724 }
725 }
726
f855de75
LPC
727 ctx->submitted_transfers--;
728 if (ctx->submitted_transfers == 0)
729 finish_acquisition(ctx);
730
731}
732
17dff8a1
LPC
733static void resubmit_transfer(struct libusb_transfer *transfer)
734{
17dff8a1
LPC
735 if (libusb_submit_transfer(transfer) != 0) {
736 free_transfer(transfer);
737 /* TODO: Stop session? */
738 /* TODO: Better error message. */
739 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
740 }
741}
742
610dbb70
JH
743static void receive_transfer(struct libusb_transfer *transfer)
744{
7ce737a7 745 gboolean packet_has_error = FALSE;
610dbb70
JH
746 struct sr_datafeed_packet packet;
747 struct sr_datafeed_logic logic;
2e526f4a 748 struct context *ctx = transfer->user_data;
d1ddc7a9 749 int trigger_offset, i;
610dbb70 750
610dbb70
JH
751 /*
752 * If acquisition has already ended, just free any queued up
753 * transfer that come in.
754 */
2e526f4a 755 if (ctx->num_samples == -1) {
f855de75 756 free_transfer(transfer);
610dbb70
JH
757 return;
758 }
759
7ae2f9d5 760 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
610dbb70
JH
761 transfer->status, transfer->actual_length);
762
763 /* Save incoming transfer before reusing the transfer struct. */
d1ddc7a9
JH
764 uint8_t *const cur_buf = transfer->buffer;
765 const int sample_width = ctx->sample_wide ? 2 : 1;
766 const int cur_sample_count = transfer->actual_length / sample_width;
610dbb70 767
7ce737a7
LPC
768 switch (transfer->status) {
769 case LIBUSB_TRANSFER_NO_DEVICE:
770 abort_acquisition(ctx);
771 free_transfer(transfer);
772 return;
773 case LIBUSB_TRANSFER_COMPLETED:
774 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
775 break;
776 default:
777 packet_has_error = TRUE;
778 break;
779 }
780
781 if (transfer->actual_length == 0 || packet_has_error) {
2769eed9
LPC
782 ctx->empty_transfer_count++;
783 if (ctx->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
610dbb70
JH
784 /*
785 * The FX2 gave up. End the acquisition, the frontend
786 * will work out that the samplecount is short.
787 */
2e526f4a 788 abort_acquisition(ctx);
17dff8a1
LPC
789 free_transfer(transfer);
790 } else {
791 resubmit_transfer(transfer);
610dbb70
JH
792 }
793 return;
794 } else {
2769eed9 795 ctx->empty_transfer_count = 0;
610dbb70
JH
796 }
797
6c6781b6
JH
798 trigger_offset = 0;
799 if (ctx->trigger_stage >= 0) {
d1ddc7a9
JH
800 for (i = 0; i < cur_sample_count; i++) {
801
802 const uint16_t cur_sample = ctx->sample_wide ?
803 *((const uint16_t*)cur_buf + i) :
804 *((const uint8_t*)cur_buf + i);
6c6781b6 805
d1ddc7a9
JH
806 if ((cur_sample & ctx->trigger_mask[ctx->trigger_stage]) ==
807 ctx->trigger_value[ctx->trigger_stage]) {
6c6781b6 808 /* Match on this trigger stage. */
d1ddc7a9 809 ctx->trigger_buffer[ctx->trigger_stage] = cur_sample;
6c6781b6
JH
810 ctx->trigger_stage++;
811
d1ddc7a9
JH
812 if (ctx->trigger_stage == NUM_TRIGGER_STAGES ||
813 ctx->trigger_mask[ctx->trigger_stage] == 0) {
6c6781b6
JH
814 /* Match on all trigger stages, we're done. */
815 trigger_offset = i + 1;
816
817 /*
818 * TODO: Send pre-trigger buffer to session bus.
819 * Tell the frontend we hit the trigger here.
820 */
821 packet.type = SR_DF_TRIGGER;
822 packet.payload = NULL;
823 sr_session_send(ctx->session_dev_id, &packet);
824
825 /*
826 * Send the samples that triggered it, since we're
827 * skipping past them.
828 */
829 packet.type = SR_DF_LOGIC;
830 packet.payload = &logic;
fa114e4a
LPC
831 logic.unitsize = sizeof(*ctx->trigger_buffer);
832 logic.length = ctx->trigger_stage * logic.unitsize;
6c6781b6
JH
833 logic.data = ctx->trigger_buffer;
834 sr_session_send(ctx->session_dev_id, &packet);
835
836 ctx->trigger_stage = TRIGGER_FIRED;
837 break;
838 }
f3ab43a8
LPC
839 } else if (ctx->trigger_stage > 0) {
840 /*
841 * We had a match before, but not in the next sample. However, we may
842 * have a match on this stage in the next bit -- trigger on 0001 will
843 * fail on seeing 00001, so we need to go back to stage 0 -- but at
844 * the next sample from the one that matched originally, which the
845 * counter increment at the end of the loop takes care of.
846 */
6c6781b6
JH
847 i -= ctx->trigger_stage;
848 if (i < -1)
849 i = -1; /* Oops, went back past this buffer. */
850 /* Reset trigger stage. */
851 ctx->trigger_stage = 0;
852 }
853 }
854 }
610dbb70 855
6c6781b6
JH
856 if (ctx->trigger_stage == TRIGGER_FIRED) {
857 /* Send the incoming transfer to the session bus. */
0a88ec3d 858 const int trigger_offset_bytes = trigger_offset * sample_width;
6c6781b6
JH
859 packet.type = SR_DF_LOGIC;
860 packet.payload = &logic;
d1ddc7a9
JH
861 logic.length = transfer->actual_length - trigger_offset_bytes;
862 logic.unitsize = sample_width;
863 logic.data = cur_buf + trigger_offset_bytes;
6c6781b6 864 sr_session_send(ctx->session_dev_id, &packet);
6c6781b6 865
d1ddc7a9 866 ctx->num_samples += cur_sample_count;
6c6781b6
JH
867 if (ctx->limit_samples &&
868 (unsigned int)ctx->num_samples > ctx->limit_samples) {
869 abort_acquisition(ctx);
0caa1ef0
LPC
870 free_transfer(transfer);
871 return;
6c6781b6
JH
872 }
873 } else {
874 /*
875 * TODO: Buffer pre-trigger data in capture
876 * ratio-sized buffer.
877 */
610dbb70 878 }
17dff8a1
LPC
879
880 resubmit_transfer(transfer);
610dbb70
JH
881}
882
5af666a9
LPC
883static unsigned int to_bytes_per_ms(unsigned int samplerate)
884{
885 return samplerate / 1000;
886}
887
888static size_t get_buffer_size(struct context *ctx)
889{
890 size_t s;
891
892 /* The buffer should be large enough to hold 10ms of data and a multiple
893 * of 512. */
894 s = 10 * to_bytes_per_ms(ctx->cur_samplerate);
895 return (s + 511) & ~511;
896}
897
898static unsigned int get_number_of_transfers(struct context *ctx)
899{
900 unsigned int n;
901
902 /* Total buffer size should be able to hold about 500ms of data */
903 n = 500 * to_bytes_per_ms(ctx->cur_samplerate) / get_buffer_size(ctx);
904
905 if (n > NUM_SIMUL_TRANSFERS)
906 return NUM_SIMUL_TRANSFERS;
907
908 return n;
909}
910
911static unsigned int get_timeout(struct context *ctx)
912{
913 size_t total_size;
914 unsigned int timeout;
915
916 total_size = get_buffer_size(ctx) * get_number_of_transfers(ctx);
917 timeout = total_size / to_bytes_per_ms(ctx->cur_samplerate);
918 return timeout + timeout / 4; /* Leave a headroom of 25% percent */
919}
920
3ffb6964
BV
921static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
922 void *cb_data)
f302a082 923{
bd47acab
LPC
924 struct sr_datafeed_packet packet;
925 struct sr_datafeed_header header;
f366e86c 926 struct sr_datafeed_meta_logic meta;
772a0e61 927 struct context *ctx;
610dbb70
JH
928 struct libusb_transfer *transfer;
929 const struct libusb_pollfd **lupfd;
5af666a9
LPC
930 unsigned int i;
931 int ret;
610dbb70
JH
932 unsigned char *buf;
933
610dbb70 934 ctx = sdi->priv;
c03f0450
LPC
935 if (ctx->submitted_transfers != 0)
936 return SR_ERR;
937
3cd3a20b 938 ctx->session_dev_id = cb_data;
2e526f4a 939 ctx->num_samples = 0;
2769eed9 940 ctx->empty_transfer_count = 0;
610dbb70 941
5af666a9
LPC
942 const unsigned int timeout = get_timeout(ctx);
943 const unsigned int num_transfers = get_number_of_transfers(ctx);
944 const size_t size = get_buffer_size(ctx);
945
0caa1ef0
LPC
946 ctx->transfers = g_try_malloc0(sizeof(*ctx->transfers) * num_transfers);
947 if (!ctx->transfers)
948 return SR_ERR;
949
950 ctx->num_transfers = num_transfers;
951
5af666a9 952 for (i = 0; i < num_transfers; i++) {
610dbb70 953 if (!(buf = g_try_malloc(size))) {
7ae2f9d5 954 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
610dbb70
JH
955 return SR_ERR_MALLOC;
956 }
957 transfer = libusb_alloc_transfer(0);
958 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
959 2 | LIBUSB_ENDPOINT_IN, buf, size,
5af666a9 960 receive_transfer, ctx, timeout);
610dbb70 961 if (libusb_submit_transfer(transfer) != 0) {
610dbb70
JH
962 libusb_free_transfer(transfer);
963 g_free(buf);
25f5d66a 964 abort_acquisition(ctx);
610dbb70
JH
965 return SR_ERR;
966 }
0caa1ef0 967 ctx->transfers[i] = transfer;
cb61e9f7 968 ctx->submitted_transfers++;
610dbb70
JH
969 }
970
971 lupfd = libusb_get_pollfds(usb_context);
972 for (i = 0; lupfd[i]; i++)
973 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
5af666a9 974 timeout, receive_data, NULL);
610dbb70
JH
975 free(lupfd); /* NOT g_free()! */
976
bd47acab
LPC
977 packet.type = SR_DF_HEADER;
978 packet.payload = &header;
979 header.feed_version = 1;
980 gettimeofday(&header.starttime, NULL);
981 sr_session_send(cb_data, &packet);
f366e86c
BV
982
983 /* Send metadata about the SR_DF_LOGIC packets to come. */
bd47acab
LPC
984 packet.type = SR_DF_META_LOGIC;
985 packet.payload = &meta;
f366e86c 986 meta.samplerate = ctx->cur_samplerate;
d1ddc7a9 987 meta.num_probes = ctx->sample_wide ? 16 : 8;
bd47acab 988 sr_session_send(cb_data, &packet);
610dbb70 989
ebc34738 990 if ((ret = command_start_acquisition (ctx->usb->devhdl,
d1ddc7a9 991 ctx->cur_samplerate, ctx->sample_wide)) != SR_OK) {
25f5d66a 992 abort_acquisition(ctx);
ebc34738 993 return ret;
017375d1
JH
994 }
995
f302a082
JH
996 return SR_OK;
997}
998
3cd3a20b 999/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
3ffb6964
BV
1000static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
1001 void *cb_data)
f302a082 1002{
5da93902 1003
f4a9e5c0 1004 /* Avoid compiler warnings. */
2e526f4a 1005 (void)cb_data;
5da93902 1006
2e526f4a 1007 abort_acquisition(sdi->priv);
5da93902 1008
f302a082
JH
1009 return SR_OK;
1010}
1011
c09f0b57 1012SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
f302a082 1013 .name = "fx2lafw",
2e7cb004 1014 .longname = "fx2lafw (generic driver for FX2 based LAs)",
f302a082
JH
1015 .api_version = 1,
1016 .init = hw_init,
1017 .cleanup = hw_cleanup,
61136ea6 1018 .scan = hw_scan,
f302a082
JH
1019 .dev_open = hw_dev_open,
1020 .dev_close = hw_dev_close,
6e9339aa 1021 .info_get = hw_info_get,
f302a082
JH
1022 .dev_config_set = hw_dev_config_set,
1023 .dev_acquisition_start = hw_dev_acquisition_start,
1024 .dev_acquisition_stop = hw_dev_acquisition_stop,
a8cc8e44 1025 .instances = NULL,
f302a082 1026};