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