]> sigrok.org Git - libsigrok.git/blame - hardware/fx2lafw/fx2lafw.c
demo: don't use deprecated hwcap_get_all() driver API call
[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;
1b79df2f 146static int hw_dev_config_set(int dev_index, int hwcap, const void *value);
da686568 147static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
610dbb70 148
b1eeb67e
JH
149/**
150 * Check the USB configuration to determine if this is an fx2lafw device.
151 *
f9a69557
UH
152 * @return TRUE if the device's configuration profile match fx2lafw
153 * configuration, FALSE otherwise.
b1eeb67e 154 */
f9a69557 155static gboolean check_conf_profile(libusb_device *dev)
b1eeb67e
JH
156{
157 struct libusb_device_descriptor des;
6b73d9a5
BV
158 struct libusb_device_handle *hdl;
159 gboolean ret;
160 unsigned char strdesc[64];
b1eeb67e 161
6b73d9a5
BV
162 hdl = NULL;
163 ret = FALSE;
b1eeb67e 164 while (!ret) {
da686568 165 /* Assume the FW has not been loaded, unless proven wrong. */
b1eeb67e
JH
166 if (libusb_get_device_descriptor(dev, &des) != 0)
167 break;
168
6b73d9a5 169 if (libusb_open(dev, &hdl) != 0)
b1eeb67e
JH
170 break;
171
6b73d9a5
BV
172 if (libusb_get_string_descriptor_ascii(hdl,
173 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
b1eeb67e 174 break;
6b73d9a5 175 if (strncmp((const char *)strdesc, "sigrok", 6))
b1eeb67e
JH
176 break;
177
6b73d9a5
BV
178 if (libusb_get_string_descriptor_ascii(hdl,
179 des.iProduct, strdesc, sizeof(strdesc)) < 0)
b1eeb67e 180 break;
6b73d9a5 181 if (strncmp((const char *)strdesc, "fx2lafw", 7))
b1eeb67e
JH
182 break;
183
b1eeb67e 184 /* If we made it here, it must be an fx2lafw. */
f9a69557 185 ret = TRUE;
b1eeb67e 186 }
6b73d9a5
BV
187 if (hdl)
188 libusb_close(hdl);
b1eeb67e
JH
189
190 return ret;
191}
192
da686568 193static int fx2lafw_dev_open(int dev_index)
43125c69
JH
194{
195 libusb_device **devlist;
196 struct libusb_device_descriptor des;
197 struct sr_dev_inst *sdi;
772a0e61 198 struct context *ctx;
13bf7ecc 199 struct version_info vi;
ebc34738 200 int ret, skip, i;
1e94408a 201 uint8_t revid;
43125c69 202
a8cc8e44 203 if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
43125c69
JH
204 return SR_ERR;
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) {
231 if (skip != dev_index) {
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
f1898235
JH
300static void close_dev(struct sr_dev_inst *sdi)
301{
772a0e61 302 struct context *ctx;
f1898235
JH
303
304 ctx = sdi->priv;
305
306 if (ctx->usb->devhdl == NULL)
307 return;
308
7ae2f9d5
UH
309 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
310 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
f1898235
JH
311 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
312 libusb_close(ctx->usb->devhdl);
313 ctx->usb->devhdl = NULL;
314 sdi->status = SR_ST_INACTIVE;
315}
316
6c6781b6
JH
317static int configure_probes(struct context *ctx, GSList *probes)
318{
319 struct sr_probe *probe;
320 GSList *l;
321 int probe_bit, stage, i;
322 char *tc;
323
324 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
325 ctx->trigger_mask[i] = 0;
326 ctx->trigger_value[i] = 0;
327 }
328
329 stage = -1;
330 for (l = probes; l; l = l->next) {
331 probe = (struct sr_probe *)l->data;
332 if (probe->enabled == FALSE)
333 continue;
d1ddc7a9
JH
334
335 if (probe->index > 8)
0a88ec3d 336 ctx->sample_wide = TRUE;
d1ddc7a9 337
6c6781b6
JH
338 probe_bit = 1 << (probe->index - 1);
339 if (!(probe->trigger))
340 continue;
341
342 stage = 0;
343 for (tc = probe->trigger; *tc; tc++) {
344 ctx->trigger_mask[stage] |= probe_bit;
345 if (*tc == '1')
346 ctx->trigger_value[stage] |= probe_bit;
347 stage++;
348 if (stage > NUM_TRIGGER_STAGES)
349 return SR_ERR;
350 }
351 }
352
353 if (stage == -1)
354 /*
355 * We didn't configure any triggers, make sure acquisition
356 * doesn't wait for any.
357 */
358 ctx->trigger_stage = TRIGGER_FIRED;
359 else
360 ctx->trigger_stage = 0;
361
362 return SR_OK;
363}
364
da686568 365static struct context *fx2lafw_dev_new(void)
187b3582 366{
772a0e61 367 struct context *ctx;
187b3582 368
772a0e61 369 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
7ae2f9d5 370 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
187b3582
JH
371 return NULL;
372 }
373
6c6781b6
JH
374 ctx->trigger_stage = TRIGGER_FIRED;
375
772a0e61 376 return ctx;
187b3582 377}
f302a082 378
f69b47f0
BV
379static int clear_instances(void)
380{
381 GSList *l;
382 struct sr_dev_inst *sdi;
383 struct context *ctx;
384 int ret;
385
386 ret = SR_OK;
387 for (l = fdi->instances; l; l = l->next) {
388 if (!(sdi = l->data)) {
389 /* Log error, but continue cleaning up the rest. */
390 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
391 __func__);
392 ret = SR_ERR_BUG;
393 continue;
394 }
395 if (!(ctx = sdi->priv)) {
396 /* Log error, but continue cleaning up the rest. */
397 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
398 __func__);
399 ret = SR_ERR_BUG;
400 continue;
401 }
402 close_dev(sdi);
403 sdi = l->data;
404 sr_dev_inst_free(sdi);
405 }
406
407 g_slist_free(fdi->instances);
408 fdi->instances = NULL;
409
410 return ret;
411}
412
413
f302a082
JH
414/*
415 * API callbacks
416 */
417
40dda2c3 418static int hw_init(void)
61136ea6
BV
419{
420
421 if (libusb_init(&usb_context) != 0) {
422 sr_warn("fx2lafw: Failed to initialize libusb.");
423 return SR_ERR;
424 }
425
426 return SR_OK;
427}
428
3a7a22cb 429static GSList *hw_scan(GSList *options)
f302a082 430{
3a7a22cb 431 GSList *devices;
187b3582 432 struct libusb_device_descriptor des;
3a7a22cb 433 struct sr_dev_inst *sdi;
da686568 434 const struct fx2lafw_profile *prof;
772a0e61 435 struct context *ctx;
3a7a22cb 436 struct sr_probe *probe;
187b3582 437 libusb_device **devlist;
3a7a22cb
BV
438 int devcnt, num_logic_probes, ret, i, j;
439
440 /* Avoid compiler warnings. */
441 (void)options;
187b3582 442
f69b47f0
BV
443 /* This scan always invalidates any previous scans. */
444 clear_instances();
445
921634ec 446 /* Find all fx2lafw compatible devices and upload firmware to them. */
3a7a22cb 447 devices = NULL;
187b3582
JH
448 libusb_get_device_list(usb_context, &devlist);
449 for (i = 0; devlist[i]; i++) {
450
ebc34738 451 if ((ret = libusb_get_device_descriptor(
7ae2f9d5
UH
452 devlist[i], &des)) != 0) {
453 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
187b3582
JH
454 continue;
455 }
456
da686568 457 prof = NULL;
187b3582
JH
458 for (j = 0; supported_fx2[j].vid; j++) {
459 if (des.idVendor == supported_fx2[j].vid &&
460 des.idProduct == supported_fx2[j].pid) {
da686568 461 prof = &supported_fx2[j];
187b3582
JH
462 }
463 }
464
465 /* Skip if the device was not found */
da686568 466 if (!prof)
187b3582
JH
467 continue;
468
a8cc8e44 469 devcnt = g_slist_length(fdi->instances);
187b3582 470 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
da686568 471 prof->vendor, prof->model, prof->model_version);
f4a9e5c0 472 if (!sdi)
187b3582 473 return 0;
c5e82ca5 474 sdi->driver = fdi;
187b3582 475
06717a8a
BV
476 /* Fill in probelist according to this device's profile. */
477 num_logic_probes = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
c5e82ca5 478 for (j = 0; j < num_logic_probes; j++) {
06717a8a
BV
479 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
480 probe_names[j])))
481 return 0;
482 sdi->probes = g_slist_append(sdi->probes, probe);
483 }
484
da686568
UH
485 ctx = fx2lafw_dev_new();
486 ctx->profile = prof;
90282c82 487 sdi->priv = ctx;
a8cc8e44
BV
488 fdi->instances = g_slist_append(fdi->instances, sdi);
489 devices = g_slist_append(devices, sdi);
187b3582 490
b1eeb67e
JH
491 if (check_conf_profile(devlist[i])) {
492 /* Already has the firmware, so fix the new address. */
7ae2f9d5 493 sr_dbg("fx2lafw: Found an fx2lafw device.");
b1eeb67e
JH
494 sdi->status = SR_ST_INACTIVE;
495 ctx->usb = sr_usb_dev_inst_new
496 (libusb_get_bus_number(devlist[i]),
497 libusb_get_device_address(devlist[i]), NULL);
498 } else {
f8b07fc6 499 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
da686568 500 prof->firmware) == SR_OK)
b1eeb67e 501 /* Remember when the firmware on this device was updated */
e8bd58ff 502 ctx->fw_updated = g_get_monotonic_time();
b1eeb67e 503 else
7ae2f9d5
UH
504 sr_err("fx2lafw: Firmware upload failed for "
505 "device %d.", devcnt);
b1eeb67e
JH
506 ctx->usb = sr_usb_dev_inst_new
507 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
508 }
187b3582
JH
509 }
510 libusb_free_device_list(devlist, 1);
511
3a7a22cb 512 return devices;
f302a082
JH
513}
514
772a0e61 515static int hw_dev_open(int dev_index)
f302a082 516{
43125c69 517 struct sr_dev_inst *sdi;
772a0e61 518 struct context *ctx;
e8bd58ff
UH
519 int ret;
520 int64_t timediff_us, timediff_ms;
43125c69 521
a8cc8e44 522 if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
43125c69
JH
523 return SR_ERR;
524 ctx = sdi->priv;
525
526 /*
f60fdf6e
UH
527 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
528 * milliseconds for the FX2 to renumerate.
43125c69 529 */
3b6c1930 530 ret = SR_ERR;
e8bd58ff 531 if (ctx->fw_updated > 0) {
7ae2f9d5 532 sr_info("fx2lafw: Waiting for device to reset.");
43125c69
JH
533 /* takes at least 300ms for the FX2 to be gone from the USB bus */
534 g_usleep(300 * 1000);
e8bd58ff 535 timediff_ms = 0;
f60fdf6e 536 while (timediff_ms < MAX_RENUM_DELAY_MS) {
da686568 537 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
43125c69
JH
538 break;
539 g_usleep(100 * 1000);
e8bd58ff
UH
540
541 timediff_us = g_get_monotonic_time() - ctx->fw_updated;
3b6c1930
BV
542 timediff_ms = timediff_us / 1000;
543 sr_spew("fx2lafw: waited %" PRIi64 " ms", timediff_ms);
43125c69 544 }
e8bd58ff 545 sr_info("fx2lafw: Device came back after %d ms.", timediff_ms);
43125c69 546 } else {
da686568 547 ret = fx2lafw_dev_open(dev_index);
43125c69
JH
548 }
549
ebc34738 550 if (ret != SR_OK) {
7ae2f9d5 551 sr_err("fx2lafw: Unable to open device.");
43125c69
JH
552 return SR_ERR;
553 }
554 ctx = sdi->priv;
555
ebc34738
UH
556 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
557 if (ret != 0) {
0c156e06
JH
558 switch(ret) {
559 case LIBUSB_ERROR_BUSY:
560 sr_err("fx2lafw: Unable to claim USB interface. Another "
561 "program or driver has already claimed it.");
562 break;
563
564 case LIBUSB_ERROR_NO_DEVICE:
565 sr_err("fx2lafw: Device has been disconnected.");
566 break;
567
568 default:
569 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
570 break;
571 }
572
43125c69
JH
573 return SR_ERR;
574 }
575
f92994fd
JH
576 if (ctx->cur_samplerate == 0) {
577 /* Samplerate hasn't been set; default to the slowest one. */
578 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
590b9f9a 579 &supported_samplerates[0]) == SR_ERR)
f92994fd
JH
580 return SR_ERR;
581 }
582
f302a082
JH
583 return SR_OK;
584}
585
f1898235 586static int hw_dev_close(int dev_index)
f302a082 587{
f1898235
JH
588 struct sr_dev_inst *sdi;
589
a8cc8e44 590 if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index))) {
7ae2f9d5 591 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
0abee507 592 return SR_ERR_BUG;
f1898235
JH
593 }
594
595 /* TODO */
596 close_dev(sdi);
597
f302a082
JH
598 return SR_OK;
599}
600
601static int hw_cleanup(void)
602{
f69b47f0 603 int ret;
187b3582 604
f69b47f0 605 ret = clear_instances();
187b3582 606
f4a9e5c0 607 if (usb_context)
187b3582
JH
608 libusb_exit(usb_context);
609 usb_context = NULL;
610
62bc70e4 611 return ret;
f302a082
JH
612}
613
6e9339aa
BV
614static int hw_info_get(int dev_info_id, const void **data,
615 const struct sr_dev_inst *sdi)
f302a082 616{
772a0e61 617 struct context *ctx;
8b35f474 618
772a0e61 619 switch (dev_info_id) {
8b35f474 620 case SR_DI_INST:
a27999e6
BV
621 *data = sdi;
622 break;
623 case SR_DI_HWOPTS:
624 *data = NULL;
625 break;
626 case SR_DI_HWCAPS:
627 *data = hwcaps;
628 break;
8b35f474 629 case SR_DI_NUM_PROBES:
6e9339aa
BV
630 if (sdi) {
631 ctx = sdi->priv;
632 *data = GINT_TO_POINTER(
633 (ctx->profile->dev_caps & DEV_CAPS_16BIT) ?
634 16 : 8);
635 } else
636 return SR_ERR;
637 break;
8b35f474 638 case SR_DI_PROBE_NAMES:
6e9339aa
BV
639 *data = probe_names;
640 break;
8b35f474 641 case SR_DI_SAMPLERATES:
6e9339aa
BV
642 *data = &samplerates;
643 break;
8b35f474 644 case SR_DI_TRIGGER_TYPES:
6e9339aa
BV
645 *data = TRIGGER_TYPES;
646 break;
e3186647 647 case SR_DI_CUR_SAMPLERATE:
6e9339aa
BV
648 if (sdi) {
649 ctx = sdi->priv;
650 *data = &ctx->cur_samplerate;
651 } else
652 return SR_ERR;
653 break;
654 default:
655 return SR_ERR_ARG;
8b35f474
JH
656 }
657
6e9339aa 658 return SR_OK;
f302a082
JH
659}
660
772a0e61 661static int hw_dev_status_get(int dev_index)
f302a082 662{
aae2fed6 663 const struct sr_dev_inst *const sdi =
a8cc8e44 664 sr_dev_inst_get(fdi->instances, dev_index);
aae2fed6
JH
665
666 if (!sdi)
667 return SR_ST_NOT_FOUND;
668
669 return sdi->status;
f302a082
JH
670}
671
1b79df2f 672static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
f302a082 673{
7cb621d4 674 struct sr_dev_inst *sdi;
772a0e61 675 struct context *ctx;
7cb621d4
JH
676 int ret;
677
a8cc8e44 678 if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
7cb621d4
JH
679 return SR_ERR;
680 ctx = sdi->priv;
681
e3186647 682 if (hwcap == SR_HWCAP_SAMPLERATE) {
1b79df2f 683 ctx->cur_samplerate = *(const uint64_t *)value;
e3186647
JH
684 ret = SR_OK;
685 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
6c6781b6 686 ret = configure_probes(ctx, (GSList *) value);
e3186647 687 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
1b79df2f 688 ctx->limit_samples = *(const uint64_t *)value;
7cb621d4
JH
689 ret = SR_OK;
690 } else {
691 ret = SR_ERR;
692 }
693
694 return ret;
f302a082
JH
695}
696
1f9813eb 697static int receive_data(int fd, int revents, void *cb_data)
610dbb70
JH
698{
699 struct timeval tv;
700
701 /* Avoid compiler warnings. */
702 (void)fd;
703 (void)revents;
1f9813eb 704 (void)cb_data;
610dbb70
JH
705
706 tv.tv_sec = tv.tv_usec = 0;
707 libusb_handle_events_timeout(usb_context, &tv);
708
709 return TRUE;
710}
711
2e526f4a 712static void abort_acquisition(struct context *ctx)
cb61e9f7 713{
e7d087bf 714 int i;
0caa1ef0 715
cb61e9f7 716 ctx->num_samples = -1;
0caa1ef0 717
e7d087bf 718 for (i = ctx->num_transfers - 1; i >= 0; i--) {
0caa1ef0
LPC
719 if (ctx->transfers[i])
720 libusb_cancel_transfer(ctx->transfers[i]);
721 }
cb61e9f7
JH
722}
723
3e6292b2 724static void finish_acquisition(struct context *ctx)
2e526f4a
JH
725{
726 struct sr_datafeed_packet packet;
cb61e9f7 727 int i;
2e526f4a 728
cb61e9f7 729 /* Terminate session */
2e526f4a
JH
730 packet.type = SR_DF_END;
731 sr_session_send(ctx->session_dev_id, &packet);
732
cb61e9f7
JH
733 /* Remove fds from polling */
734 const struct libusb_pollfd **const lupfd =
735 libusb_get_pollfds(usb_context);
736 for (i = 0; lupfd[i]; i++)
737 sr_source_remove(lupfd[i]->fd);
738 free(lupfd); /* NOT g_free()! */
0caa1ef0
LPC
739
740 ctx->num_transfers = 0;
741 g_free(ctx->transfers);
2e526f4a
JH
742}
743
f855de75
LPC
744static void free_transfer(struct libusb_transfer *transfer)
745{
746 struct context *ctx = transfer->user_data;
0caa1ef0 747 unsigned int i;
f855de75
LPC
748
749 g_free(transfer->buffer);
750 transfer->buffer = NULL;
751 libusb_free_transfer(transfer);
752
0caa1ef0
LPC
753 for (i = 0; i < ctx->num_transfers; i++) {
754 if (ctx->transfers[i] == transfer) {
755 ctx->transfers[i] = NULL;
756 break;
757 }
758 }
759
f855de75
LPC
760 ctx->submitted_transfers--;
761 if (ctx->submitted_transfers == 0)
762 finish_acquisition(ctx);
763
764}
765
17dff8a1
LPC
766static void resubmit_transfer(struct libusb_transfer *transfer)
767{
17dff8a1
LPC
768 if (libusb_submit_transfer(transfer) != 0) {
769 free_transfer(transfer);
770 /* TODO: Stop session? */
771 /* TODO: Better error message. */
772 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
773 }
774}
775
610dbb70
JH
776static void receive_transfer(struct libusb_transfer *transfer)
777{
7ce737a7 778 gboolean packet_has_error = FALSE;
610dbb70
JH
779 struct sr_datafeed_packet packet;
780 struct sr_datafeed_logic logic;
2e526f4a 781 struct context *ctx = transfer->user_data;
d1ddc7a9 782 int trigger_offset, i;
610dbb70 783
610dbb70
JH
784 /*
785 * If acquisition has already ended, just free any queued up
786 * transfer that come in.
787 */
2e526f4a 788 if (ctx->num_samples == -1) {
f855de75 789 free_transfer(transfer);
610dbb70
JH
790 return;
791 }
792
7ae2f9d5 793 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
610dbb70
JH
794 transfer->status, transfer->actual_length);
795
796 /* Save incoming transfer before reusing the transfer struct. */
d1ddc7a9
JH
797 uint8_t *const cur_buf = transfer->buffer;
798 const int sample_width = ctx->sample_wide ? 2 : 1;
799 const int cur_sample_count = transfer->actual_length / sample_width;
610dbb70 800
7ce737a7
LPC
801 switch (transfer->status) {
802 case LIBUSB_TRANSFER_NO_DEVICE:
803 abort_acquisition(ctx);
804 free_transfer(transfer);
805 return;
806 case LIBUSB_TRANSFER_COMPLETED:
807 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
808 break;
809 default:
810 packet_has_error = TRUE;
811 break;
812 }
813
814 if (transfer->actual_length == 0 || packet_has_error) {
2769eed9
LPC
815 ctx->empty_transfer_count++;
816 if (ctx->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
610dbb70
JH
817 /*
818 * The FX2 gave up. End the acquisition, the frontend
819 * will work out that the samplecount is short.
820 */
2e526f4a 821 abort_acquisition(ctx);
17dff8a1
LPC
822 free_transfer(transfer);
823 } else {
824 resubmit_transfer(transfer);
610dbb70
JH
825 }
826 return;
827 } else {
2769eed9 828 ctx->empty_transfer_count = 0;
610dbb70
JH
829 }
830
6c6781b6
JH
831 trigger_offset = 0;
832 if (ctx->trigger_stage >= 0) {
d1ddc7a9
JH
833 for (i = 0; i < cur_sample_count; i++) {
834
835 const uint16_t cur_sample = ctx->sample_wide ?
836 *((const uint16_t*)cur_buf + i) :
837 *((const uint8_t*)cur_buf + i);
6c6781b6 838
d1ddc7a9
JH
839 if ((cur_sample & ctx->trigger_mask[ctx->trigger_stage]) ==
840 ctx->trigger_value[ctx->trigger_stage]) {
6c6781b6 841 /* Match on this trigger stage. */
d1ddc7a9 842 ctx->trigger_buffer[ctx->trigger_stage] = cur_sample;
6c6781b6
JH
843 ctx->trigger_stage++;
844
d1ddc7a9
JH
845 if (ctx->trigger_stage == NUM_TRIGGER_STAGES ||
846 ctx->trigger_mask[ctx->trigger_stage] == 0) {
6c6781b6
JH
847 /* Match on all trigger stages, we're done. */
848 trigger_offset = i + 1;
849
850 /*
851 * TODO: Send pre-trigger buffer to session bus.
852 * Tell the frontend we hit the trigger here.
853 */
854 packet.type = SR_DF_TRIGGER;
855 packet.payload = NULL;
856 sr_session_send(ctx->session_dev_id, &packet);
857
858 /*
859 * Send the samples that triggered it, since we're
860 * skipping past them.
861 */
862 packet.type = SR_DF_LOGIC;
863 packet.payload = &logic;
fa114e4a
LPC
864 logic.unitsize = sizeof(*ctx->trigger_buffer);
865 logic.length = ctx->trigger_stage * logic.unitsize;
6c6781b6
JH
866 logic.data = ctx->trigger_buffer;
867 sr_session_send(ctx->session_dev_id, &packet);
868
869 ctx->trigger_stage = TRIGGER_FIRED;
870 break;
871 }
f3ab43a8
LPC
872 } else if (ctx->trigger_stage > 0) {
873 /*
874 * We had a match before, but not in the next sample. However, we may
875 * have a match on this stage in the next bit -- trigger on 0001 will
876 * fail on seeing 00001, so we need to go back to stage 0 -- but at
877 * the next sample from the one that matched originally, which the
878 * counter increment at the end of the loop takes care of.
879 */
6c6781b6
JH
880 i -= ctx->trigger_stage;
881 if (i < -1)
882 i = -1; /* Oops, went back past this buffer. */
883 /* Reset trigger stage. */
884 ctx->trigger_stage = 0;
885 }
886 }
887 }
610dbb70 888
6c6781b6
JH
889 if (ctx->trigger_stage == TRIGGER_FIRED) {
890 /* Send the incoming transfer to the session bus. */
0a88ec3d 891 const int trigger_offset_bytes = trigger_offset * sample_width;
6c6781b6
JH
892 packet.type = SR_DF_LOGIC;
893 packet.payload = &logic;
d1ddc7a9
JH
894 logic.length = transfer->actual_length - trigger_offset_bytes;
895 logic.unitsize = sample_width;
896 logic.data = cur_buf + trigger_offset_bytes;
6c6781b6 897 sr_session_send(ctx->session_dev_id, &packet);
6c6781b6 898
d1ddc7a9 899 ctx->num_samples += cur_sample_count;
6c6781b6
JH
900 if (ctx->limit_samples &&
901 (unsigned int)ctx->num_samples > ctx->limit_samples) {
902 abort_acquisition(ctx);
0caa1ef0
LPC
903 free_transfer(transfer);
904 return;
6c6781b6
JH
905 }
906 } else {
907 /*
908 * TODO: Buffer pre-trigger data in capture
909 * ratio-sized buffer.
910 */
610dbb70 911 }
17dff8a1
LPC
912
913 resubmit_transfer(transfer);
610dbb70
JH
914}
915
5af666a9
LPC
916static unsigned int to_bytes_per_ms(unsigned int samplerate)
917{
918 return samplerate / 1000;
919}
920
921static size_t get_buffer_size(struct context *ctx)
922{
923 size_t s;
924
925 /* The buffer should be large enough to hold 10ms of data and a multiple
926 * of 512. */
927 s = 10 * to_bytes_per_ms(ctx->cur_samplerate);
928 return (s + 511) & ~511;
929}
930
931static unsigned int get_number_of_transfers(struct context *ctx)
932{
933 unsigned int n;
934
935 /* Total buffer size should be able to hold about 500ms of data */
936 n = 500 * to_bytes_per_ms(ctx->cur_samplerate) / get_buffer_size(ctx);
937
938 if (n > NUM_SIMUL_TRANSFERS)
939 return NUM_SIMUL_TRANSFERS;
940
941 return n;
942}
943
944static unsigned int get_timeout(struct context *ctx)
945{
946 size_t total_size;
947 unsigned int timeout;
948
949 total_size = get_buffer_size(ctx) * get_number_of_transfers(ctx);
950 timeout = total_size / to_bytes_per_ms(ctx->cur_samplerate);
951 return timeout + timeout / 4; /* Leave a headroom of 25% percent */
952}
953
3cd3a20b 954static int hw_dev_acquisition_start(int dev_index, void *cb_data)
f302a082 955{
610dbb70 956 struct sr_dev_inst *sdi;
bd47acab
LPC
957 struct sr_datafeed_packet packet;
958 struct sr_datafeed_header header;
f366e86c 959 struct sr_datafeed_meta_logic meta;
772a0e61 960 struct context *ctx;
610dbb70
JH
961 struct libusb_transfer *transfer;
962 const struct libusb_pollfd **lupfd;
5af666a9
LPC
963 unsigned int i;
964 int ret;
610dbb70
JH
965 unsigned char *buf;
966
a8cc8e44 967 if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
610dbb70
JH
968 return SR_ERR;
969 ctx = sdi->priv;
c03f0450
LPC
970
971 if (ctx->submitted_transfers != 0)
972 return SR_ERR;
973
3cd3a20b 974 ctx->session_dev_id = cb_data;
2e526f4a 975 ctx->num_samples = 0;
2769eed9 976 ctx->empty_transfer_count = 0;
610dbb70 977
5af666a9
LPC
978 const unsigned int timeout = get_timeout(ctx);
979 const unsigned int num_transfers = get_number_of_transfers(ctx);
980 const size_t size = get_buffer_size(ctx);
981
0caa1ef0
LPC
982 ctx->transfers = g_try_malloc0(sizeof(*ctx->transfers) * num_transfers);
983 if (!ctx->transfers)
984 return SR_ERR;
985
986 ctx->num_transfers = num_transfers;
987
5af666a9 988 for (i = 0; i < num_transfers; i++) {
610dbb70 989 if (!(buf = g_try_malloc(size))) {
7ae2f9d5 990 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
610dbb70
JH
991 return SR_ERR_MALLOC;
992 }
993 transfer = libusb_alloc_transfer(0);
994 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
995 2 | LIBUSB_ENDPOINT_IN, buf, size,
5af666a9 996 receive_transfer, ctx, timeout);
610dbb70 997 if (libusb_submit_transfer(transfer) != 0) {
610dbb70
JH
998 libusb_free_transfer(transfer);
999 g_free(buf);
25f5d66a 1000 abort_acquisition(ctx);
610dbb70
JH
1001 return SR_ERR;
1002 }
0caa1ef0 1003 ctx->transfers[i] = transfer;
cb61e9f7 1004 ctx->submitted_transfers++;
610dbb70
JH
1005 }
1006
1007 lupfd = libusb_get_pollfds(usb_context);
1008 for (i = 0; lupfd[i]; i++)
1009 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
5af666a9 1010 timeout, receive_data, NULL);
610dbb70
JH
1011 free(lupfd); /* NOT g_free()! */
1012
bd47acab
LPC
1013 packet.type = SR_DF_HEADER;
1014 packet.payload = &header;
1015 header.feed_version = 1;
1016 gettimeofday(&header.starttime, NULL);
1017 sr_session_send(cb_data, &packet);
f366e86c
BV
1018
1019 /* Send metadata about the SR_DF_LOGIC packets to come. */
bd47acab
LPC
1020 packet.type = SR_DF_META_LOGIC;
1021 packet.payload = &meta;
f366e86c 1022 meta.samplerate = ctx->cur_samplerate;
d1ddc7a9 1023 meta.num_probes = ctx->sample_wide ? 16 : 8;
bd47acab 1024 sr_session_send(cb_data, &packet);
610dbb70 1025
ebc34738 1026 if ((ret = command_start_acquisition (ctx->usb->devhdl,
d1ddc7a9 1027 ctx->cur_samplerate, ctx->sample_wide)) != SR_OK) {
25f5d66a 1028 abort_acquisition(ctx);
ebc34738 1029 return ret;
017375d1
JH
1030 }
1031
f302a082
JH
1032 return SR_OK;
1033}
1034
3cd3a20b 1035/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
c8f2c9dd 1036static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
f302a082 1037{
2e526f4a 1038 struct sr_dev_inst *sdi;
5da93902 1039
f4a9e5c0 1040 /* Avoid compiler warnings. */
2e526f4a 1041 (void)cb_data;
5da93902 1042
a8cc8e44 1043 if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
2e526f4a
JH
1044 return SR_ERR;
1045
1046 abort_acquisition(sdi->priv);
5da93902 1047
f302a082
JH
1048 return SR_OK;
1049}
1050
c09f0b57 1051SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
f302a082 1052 .name = "fx2lafw",
2e7cb004 1053 .longname = "fx2lafw (generic driver for FX2 based LAs)",
f302a082
JH
1054 .api_version = 1,
1055 .init = hw_init,
1056 .cleanup = hw_cleanup,
61136ea6 1057 .scan = hw_scan,
f302a082
JH
1058 .dev_open = hw_dev_open,
1059 .dev_close = hw_dev_close,
6e9339aa 1060 .info_get = hw_info_get,
f302a082 1061 .dev_status_get = hw_dev_status_get,
f302a082
JH
1062 .dev_config_set = hw_dev_config_set,
1063 .dev_acquisition_start = hw_dev_acquisition_start,
1064 .dev_acquisition_stop = hw_dev_acquisition_stop,
a8cc8e44 1065 .instances = NULL,
f302a082 1066};