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