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