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