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