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