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