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