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