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