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