]>
Commit | Line | Data |
---|---|---|
f302a082 JH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
cf94c816 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
f302a082 JH |
5 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
6b73d9a5 | 23 | #include <string.h> |
8b35f474 | 24 | #include <inttypes.h> |
187b3582 | 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 | 32 | static const struct fx2lafw_profile supported_fx2[] = { |
7ae2f9d5 UH |
33 | /* |
34 | * CWAV USBee AX | |
17b6c75a | 35 | * EE Electronics ESLA201A |
9f05304e | 36 | * ARMFLY AX-Pro |
17b6c75a | 37 | */ |
f8b07fc6 | 38 | { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, |
d1ddc7a9 JH |
39 | FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", |
40 | 0 }, | |
93a9f3da | 41 | |
7ae2f9d5 UH |
42 | /* |
43 | * CWAV USBee SX | |
4502e869 JH |
44 | */ |
45 | { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL, | |
d1ddc7a9 JH |
46 | FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", |
47 | 0 }, | |
4502e869 | 48 | |
7ae2f9d5 UH |
49 | /* |
50 | * Saleae Logic | |
93a9f3da JH |
51 | * EE Electronics ESLA100 |
52 | * Robomotic MiniLogic | |
1663e470 | 53 | * Robomotic BugLogic 3 |
93a9f3da JH |
54 | */ |
55 | { 0x0925, 0x3881, "Saleae", "Logic", NULL, | |
d1ddc7a9 JH |
56 | FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", |
57 | 0 }, | |
93a9f3da | 58 | |
f488762a | 59 | /* |
1663e470 UH |
60 | * Default Cypress FX2 without EEPROM, e.g.: |
61 | * Lcsoft Mini Board | |
62 | * Braintechnology USB Interface V2.x | |
f488762a JH |
63 | */ |
64 | { 0x04B4, 0x8613, "Cypress", "FX2", NULL, | |
d1ddc7a9 JH |
65 | FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", |
66 | DEV_CAPS_16BIT }, | |
f488762a | 67 | |
1663e470 UH |
68 | /* |
69 | * Braintechnology USB-LPS | |
70 | */ | |
71 | { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL, | |
d1ddc7a9 JH |
72 | FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw", |
73 | DEV_CAPS_16BIT }, | |
1663e470 | 74 | |
f8b07fc6 | 75 | { 0, 0, 0, 0, 0, 0, 0 } |
187b3582 JH |
76 | }; |
77 | ||
915f7cc8 | 78 | static const int hwcaps[] = { |
8b35f474 JH |
79 | SR_HWCAP_LOGIC_ANALYZER, |
80 | SR_HWCAP_SAMPLERATE, | |
81 | ||
82 | /* These are really implemented in the driver, not the hardware. */ | |
83 | SR_HWCAP_LIMIT_SAMPLES, | |
84 | SR_HWCAP_CONTINUOUS, | |
772a0e61 | 85 | 0, |
8b35f474 JH |
86 | }; |
87 | ||
da686568 UH |
88 | /* |
89 | * TODO: Different probe_names[] for each supported device. | |
90 | */ | |
91 | static const char *probe_names[] = { | |
7ae2f9d5 UH |
92 | "0", |
93 | "1", | |
94 | "2", | |
95 | "3", | |
96 | "4", | |
97 | "5", | |
98 | "6", | |
99 | "7", | |
d1ddc7a9 JH |
100 | "8", |
101 | "9", | |
102 | "10", | |
103 | "11", | |
104 | "12", | |
105 | "13", | |
106 | "14", | |
107 | "15", | |
772a0e61 | 108 | NULL, |
8b35f474 JH |
109 | }; |
110 | ||
a533743d | 111 | static const uint64_t supported_samplerates[] = { |
79dc6498 JH |
112 | SR_KHZ(20), |
113 | SR_KHZ(25), | |
897c1a2e JH |
114 | SR_KHZ(50), |
115 | SR_KHZ(100), | |
9304d576 JH |
116 | SR_KHZ(200), |
117 | SR_KHZ(250), | |
118 | SR_KHZ(500), | |
8b35f474 JH |
119 | SR_MHZ(1), |
120 | SR_MHZ(2), | |
121 | SR_MHZ(3), | |
122 | SR_MHZ(4), | |
123 | SR_MHZ(6), | |
124 | SR_MHZ(8), | |
125 | SR_MHZ(12), | |
126 | SR_MHZ(16), | |
772a0e61 | 127 | SR_MHZ(24), |
934cde02 | 128 | 0, |
8b35f474 JH |
129 | }; |
130 | ||
a533743d | 131 | static const struct sr_samplerates samplerates = { |
590b9f9a UH |
132 | 0, |
133 | 0, | |
134 | 0, | |
135 | supported_samplerates, | |
8b35f474 JH |
136 | }; |
137 | ||
cac0bbaa | 138 | static GSList *dev_insts = NULL; |
187b3582 JH |
139 | static libusb_context *usb_context = NULL; |
140 | ||
1b79df2f | 141 | static int hw_dev_config_set(int dev_index, int hwcap, const void *value); |
da686568 | 142 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data); |
610dbb70 | 143 | |
b1eeb67e JH |
144 | /** |
145 | * Check the USB configuration to determine if this is an fx2lafw device. | |
146 | * | |
f9a69557 UH |
147 | * @return TRUE if the device's configuration profile match fx2lafw |
148 | * configuration, FALSE otherwise. | |
b1eeb67e | 149 | */ |
f9a69557 | 150 | static gboolean check_conf_profile(libusb_device *dev) |
b1eeb67e JH |
151 | { |
152 | struct libusb_device_descriptor des; | |
6b73d9a5 BV |
153 | struct libusb_device_handle *hdl; |
154 | gboolean ret; | |
155 | unsigned char strdesc[64]; | |
b1eeb67e | 156 | |
6b73d9a5 BV |
157 | hdl = NULL; |
158 | ret = FALSE; | |
b1eeb67e | 159 | while (!ret) { |
da686568 | 160 | /* Assume the FW has not been loaded, unless proven wrong. */ |
b1eeb67e JH |
161 | if (libusb_get_device_descriptor(dev, &des) != 0) |
162 | break; | |
163 | ||
6b73d9a5 | 164 | if (libusb_open(dev, &hdl) != 0) |
b1eeb67e JH |
165 | break; |
166 | ||
6b73d9a5 BV |
167 | if (libusb_get_string_descriptor_ascii(hdl, |
168 | des.iManufacturer, strdesc, sizeof(strdesc)) < 0) | |
b1eeb67e | 169 | break; |
6b73d9a5 | 170 | if (strncmp((const char *)strdesc, "sigrok", 6)) |
b1eeb67e JH |
171 | break; |
172 | ||
6b73d9a5 BV |
173 | if (libusb_get_string_descriptor_ascii(hdl, |
174 | des.iProduct, strdesc, sizeof(strdesc)) < 0) | |
b1eeb67e | 175 | break; |
6b73d9a5 | 176 | if (strncmp((const char *)strdesc, "fx2lafw", 7)) |
b1eeb67e JH |
177 | break; |
178 | ||
b1eeb67e | 179 | /* If we made it here, it must be an fx2lafw. */ |
f9a69557 | 180 | ret = TRUE; |
b1eeb67e | 181 | } |
6b73d9a5 BV |
182 | if (hdl) |
183 | libusb_close(hdl); | |
b1eeb67e JH |
184 | |
185 | return ret; | |
186 | } | |
187 | ||
da686568 | 188 | static int fx2lafw_dev_open(int dev_index) |
43125c69 JH |
189 | { |
190 | libusb_device **devlist; | |
191 | struct libusb_device_descriptor des; | |
192 | struct sr_dev_inst *sdi; | |
772a0e61 | 193 | struct context *ctx; |
13bf7ecc | 194 | struct version_info vi; |
ebc34738 | 195 | int ret, skip, i; |
1e94408a | 196 | uint8_t revid; |
43125c69 JH |
197 | |
198 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
199 | return SR_ERR; | |
200 | ctx = sdi->priv; | |
201 | ||
202 | if (sdi->status == SR_ST_ACTIVE) | |
203 | /* already in use */ | |
204 | return SR_ERR; | |
205 | ||
206 | skip = 0; | |
6fbe5e60 JH |
207 | const int device_count = libusb_get_device_list(usb_context, &devlist); |
208 | if (device_count < 0) { | |
209 | sr_err("fx2lafw: Failed to retrieve device list (%d)", | |
210 | device_count); | |
211 | return SR_ERR; | |
212 | } | |
213 | ||
214 | for (i = 0; i < device_count; i++) { | |
ebc34738 | 215 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { |
7ae2f9d5 UH |
216 | sr_err("fx2lafw: Failed to get device descriptor: %d.", |
217 | ret); | |
43125c69 JH |
218 | continue; |
219 | } | |
220 | ||
74fcfb80 JH |
221 | if (des.idVendor != ctx->profile->vid |
222 | || des.idProduct != ctx->profile->pid) | |
43125c69 JH |
223 | continue; |
224 | ||
225 | if (sdi->status == SR_ST_INITIALIZING) { | |
226 | if (skip != dev_index) { | |
227 | /* Skip devices of this type that aren't the one we want. */ | |
228 | skip += 1; | |
229 | continue; | |
230 | } | |
231 | } else if (sdi->status == SR_ST_INACTIVE) { | |
232 | /* | |
233 | * This device is fully enumerated, so we need to find | |
234 | * this device by vendor, product, bus and address. | |
235 | */ | |
236 | if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus | |
237 | || libusb_get_device_address(devlist[i]) != ctx->usb->address) | |
238 | /* this is not the one */ | |
239 | continue; | |
240 | } | |
241 | ||
ebc34738 | 242 | if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) { |
43125c69 JH |
243 | if (ctx->usb->address == 0xff) |
244 | /* | |
245 | * first time we touch this device after firmware upload, | |
246 | * so we don't know the address yet. | |
247 | */ | |
248 | ctx->usb->address = libusb_get_device_address(devlist[i]); | |
43125c69 | 249 | } else { |
7ae2f9d5 | 250 | sr_err("fx2lafw: Failed to open device: %d.", ret); |
13bf7ecc JH |
251 | break; |
252 | } | |
253 | ||
44dfd483 UH |
254 | ret = command_get_fw_version(ctx->usb->devhdl, &vi); |
255 | if (ret != SR_OK) { | |
13bf7ecc | 256 | sr_err("fx2lafw: Failed to retrieve " |
44dfd483 | 257 | "firmware version information."); |
13bf7ecc | 258 | break; |
43125c69 JH |
259 | } |
260 | ||
1e94408a UH |
261 | ret = command_get_revid_version(ctx->usb->devhdl, &revid); |
262 | if (ret != SR_OK) { | |
263 | sr_err("fx2lafw: Failed to retrieve REVID."); | |
264 | break; | |
265 | } | |
266 | ||
0a8c0c32 UH |
267 | /* |
268 | * Changes in major version mean incompatible/API changes, so | |
269 | * bail out if we encounter an incompatible version. | |
270 | * Different minor versions are OK, they should be compatible. | |
271 | */ | |
272 | if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) { | |
273 | sr_err("fx2lafw: Expected firmware version %d.x, " | |
274 | "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR, | |
275 | vi.major, vi.minor); | |
13bf7ecc JH |
276 | break; |
277 | } | |
278 | ||
279 | sdi->status = SR_ST_ACTIVE; | |
280 | sr_info("fx2lafw: Opened device %d on %d.%d " | |
1e94408a | 281 | "interface %d, firmware %d.%d, REVID %d.", |
13bf7ecc | 282 | sdi->index, ctx->usb->bus, ctx->usb->address, |
1e94408a | 283 | USB_INTERFACE, vi.major, vi.minor, revid); |
13bf7ecc | 284 | |
43125c69 JH |
285 | break; |
286 | } | |
287 | libusb_free_device_list(devlist, 1); | |
288 | ||
289 | if (sdi->status != SR_ST_ACTIVE) | |
290 | return SR_ERR; | |
291 | ||
292 | return SR_OK; | |
293 | } | |
294 | ||
f1898235 JH |
295 | static void close_dev(struct sr_dev_inst *sdi) |
296 | { | |
772a0e61 | 297 | struct context *ctx; |
f1898235 JH |
298 | |
299 | ctx = sdi->priv; | |
300 | ||
301 | if (ctx->usb->devhdl == NULL) | |
302 | return; | |
303 | ||
7ae2f9d5 UH |
304 | sr_info("fx2lafw: Closing device %d on %d.%d interface %d.", |
305 | sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE); | |
f1898235 JH |
306 | libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE); |
307 | libusb_close(ctx->usb->devhdl); | |
308 | ctx->usb->devhdl = NULL; | |
309 | sdi->status = SR_ST_INACTIVE; | |
310 | } | |
311 | ||
6c6781b6 JH |
312 | static int configure_probes(struct context *ctx, GSList *probes) |
313 | { | |
314 | struct sr_probe *probe; | |
315 | GSList *l; | |
316 | int probe_bit, stage, i; | |
317 | char *tc; | |
318 | ||
319 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
320 | ctx->trigger_mask[i] = 0; | |
321 | ctx->trigger_value[i] = 0; | |
322 | } | |
323 | ||
324 | stage = -1; | |
325 | for (l = probes; l; l = l->next) { | |
326 | probe = (struct sr_probe *)l->data; | |
327 | if (probe->enabled == FALSE) | |
328 | continue; | |
d1ddc7a9 JH |
329 | |
330 | if (probe->index > 8) | |
0a88ec3d | 331 | ctx->sample_wide = TRUE; |
d1ddc7a9 | 332 | |
6c6781b6 JH |
333 | probe_bit = 1 << (probe->index - 1); |
334 | if (!(probe->trigger)) | |
335 | continue; | |
336 | ||
337 | stage = 0; | |
338 | for (tc = probe->trigger; *tc; tc++) { | |
339 | ctx->trigger_mask[stage] |= probe_bit; | |
340 | if (*tc == '1') | |
341 | ctx->trigger_value[stage] |= probe_bit; | |
342 | stage++; | |
343 | if (stage > NUM_TRIGGER_STAGES) | |
344 | return SR_ERR; | |
345 | } | |
346 | } | |
347 | ||
348 | if (stage == -1) | |
349 | /* | |
350 | * We didn't configure any triggers, make sure acquisition | |
351 | * doesn't wait for any. | |
352 | */ | |
353 | ctx->trigger_stage = TRIGGER_FIRED; | |
354 | else | |
355 | ctx->trigger_stage = 0; | |
356 | ||
357 | return SR_OK; | |
358 | } | |
359 | ||
da686568 | 360 | static struct context *fx2lafw_dev_new(void) |
187b3582 | 361 | { |
772a0e61 | 362 | struct context *ctx; |
187b3582 | 363 | |
772a0e61 | 364 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
7ae2f9d5 | 365 | sr_err("fx2lafw: %s: ctx malloc failed.", __func__); |
187b3582 JH |
366 | return NULL; |
367 | } | |
368 | ||
6c6781b6 JH |
369 | ctx->trigger_stage = TRIGGER_FIRED; |
370 | ||
772a0e61 | 371 | return ctx; |
187b3582 | 372 | } |
f302a082 JH |
373 | |
374 | /* | |
375 | * API callbacks | |
376 | */ | |
377 | ||
da686568 | 378 | static int hw_init(const char *devinfo) |
f302a082 | 379 | { |
187b3582 JH |
380 | struct sr_dev_inst *sdi; |
381 | struct libusb_device_descriptor des; | |
da686568 | 382 | const struct fx2lafw_profile *prof; |
772a0e61 | 383 | struct context *ctx; |
187b3582 | 384 | libusb_device **devlist; |
ebc34738 | 385 | int ret; |
187b3582 JH |
386 | int devcnt = 0; |
387 | int i, j; | |
388 | ||
389 | /* Avoid compiler warnings. */ | |
da686568 | 390 | (void)devinfo; |
187b3582 JH |
391 | |
392 | if (libusb_init(&usb_context) != 0) { | |
7ae2f9d5 | 393 | sr_warn("fx2lafw: Failed to initialize libusb."); |
187b3582 JH |
394 | return 0; |
395 | } | |
396 | ||
921634ec | 397 | /* Find all fx2lafw compatible devices and upload firmware to them. */ |
187b3582 JH |
398 | libusb_get_device_list(usb_context, &devlist); |
399 | for (i = 0; devlist[i]; i++) { | |
400 | ||
ebc34738 | 401 | if ((ret = libusb_get_device_descriptor( |
7ae2f9d5 UH |
402 | devlist[i], &des)) != 0) { |
403 | sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret); | |
187b3582 JH |
404 | continue; |
405 | } | |
406 | ||
da686568 | 407 | prof = NULL; |
187b3582 JH |
408 | for (j = 0; supported_fx2[j].vid; j++) { |
409 | if (des.idVendor == supported_fx2[j].vid && | |
410 | des.idProduct == supported_fx2[j].pid) { | |
da686568 | 411 | prof = &supported_fx2[j]; |
187b3582 JH |
412 | } |
413 | } | |
414 | ||
415 | /* Skip if the device was not found */ | |
da686568 | 416 | if (!prof) |
187b3582 JH |
417 | continue; |
418 | ||
419 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, | |
da686568 | 420 | prof->vendor, prof->model, prof->model_version); |
f4a9e5c0 | 421 | if (!sdi) |
187b3582 JH |
422 | return 0; |
423 | ||
da686568 UH |
424 | ctx = fx2lafw_dev_new(); |
425 | ctx->profile = prof; | |
90282c82 | 426 | sdi->priv = ctx; |
be4b99e8 | 427 | dev_insts = g_slist_append(dev_insts, sdi); |
187b3582 | 428 | |
b1eeb67e JH |
429 | if (check_conf_profile(devlist[i])) { |
430 | /* Already has the firmware, so fix the new address. */ | |
7ae2f9d5 | 431 | sr_dbg("fx2lafw: Found an fx2lafw device."); |
b1eeb67e JH |
432 | sdi->status = SR_ST_INACTIVE; |
433 | ctx->usb = sr_usb_dev_inst_new | |
434 | (libusb_get_bus_number(devlist[i]), | |
435 | libusb_get_device_address(devlist[i]), NULL); | |
436 | } else { | |
f8b07fc6 | 437 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, |
da686568 | 438 | prof->firmware) == SR_OK) |
b1eeb67e | 439 | /* Remember when the firmware on this device was updated */ |
e8bd58ff | 440 | ctx->fw_updated = g_get_monotonic_time(); |
b1eeb67e | 441 | else |
7ae2f9d5 UH |
442 | sr_err("fx2lafw: Firmware upload failed for " |
443 | "device %d.", devcnt); | |
b1eeb67e JH |
444 | ctx->usb = sr_usb_dev_inst_new |
445 | (libusb_get_bus_number(devlist[i]), 0xff, NULL); | |
446 | } | |
447 | ||
187b3582 JH |
448 | devcnt++; |
449 | } | |
450 | libusb_free_device_list(devlist, 1); | |
451 | ||
452 | return devcnt; | |
f302a082 JH |
453 | } |
454 | ||
772a0e61 | 455 | static int hw_dev_open(int dev_index) |
f302a082 | 456 | { |
43125c69 | 457 | struct sr_dev_inst *sdi; |
772a0e61 | 458 | struct context *ctx; |
e8bd58ff UH |
459 | int ret; |
460 | int64_t timediff_us, timediff_ms; | |
43125c69 | 461 | |
772a0e61 | 462 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
43125c69 JH |
463 | return SR_ERR; |
464 | ctx = sdi->priv; | |
465 | ||
466 | /* | |
f60fdf6e UH |
467 | * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS |
468 | * milliseconds for the FX2 to renumerate. | |
43125c69 | 469 | */ |
3b6c1930 | 470 | ret = SR_ERR; |
e8bd58ff | 471 | if (ctx->fw_updated > 0) { |
7ae2f9d5 | 472 | sr_info("fx2lafw: Waiting for device to reset."); |
43125c69 JH |
473 | /* takes at least 300ms for the FX2 to be gone from the USB bus */ |
474 | g_usleep(300 * 1000); | |
e8bd58ff | 475 | timediff_ms = 0; |
f60fdf6e | 476 | while (timediff_ms < MAX_RENUM_DELAY_MS) { |
da686568 | 477 | if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK) |
43125c69 JH |
478 | break; |
479 | g_usleep(100 * 1000); | |
e8bd58ff UH |
480 | |
481 | timediff_us = g_get_monotonic_time() - ctx->fw_updated; | |
3b6c1930 BV |
482 | timediff_ms = timediff_us / 1000; |
483 | sr_spew("fx2lafw: waited %" PRIi64 " ms", timediff_ms); | |
43125c69 | 484 | } |
e8bd58ff | 485 | sr_info("fx2lafw: Device came back after %d ms.", timediff_ms); |
43125c69 | 486 | } else { |
da686568 | 487 | ret = fx2lafw_dev_open(dev_index); |
43125c69 JH |
488 | } |
489 | ||
ebc34738 | 490 | if (ret != SR_OK) { |
7ae2f9d5 | 491 | sr_err("fx2lafw: Unable to open device."); |
43125c69 JH |
492 | return SR_ERR; |
493 | } | |
494 | ctx = sdi->priv; | |
495 | ||
ebc34738 UH |
496 | ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE); |
497 | if (ret != 0) { | |
0c156e06 JH |
498 | switch(ret) { |
499 | case LIBUSB_ERROR_BUSY: | |
500 | sr_err("fx2lafw: Unable to claim USB interface. Another " | |
501 | "program or driver has already claimed it."); | |
502 | break; | |
503 | ||
504 | case LIBUSB_ERROR_NO_DEVICE: | |
505 | sr_err("fx2lafw: Device has been disconnected."); | |
506 | break; | |
507 | ||
508 | default: | |
509 | sr_err("fx2lafw: Unable to claim interface: %d.", ret); | |
510 | break; | |
511 | } | |
512 | ||
43125c69 JH |
513 | return SR_ERR; |
514 | } | |
515 | ||
f92994fd JH |
516 | if (ctx->cur_samplerate == 0) { |
517 | /* Samplerate hasn't been set; default to the slowest one. */ | |
518 | if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE, | |
590b9f9a | 519 | &supported_samplerates[0]) == SR_ERR) |
f92994fd JH |
520 | return SR_ERR; |
521 | } | |
522 | ||
f302a082 JH |
523 | return SR_OK; |
524 | } | |
525 | ||
f1898235 | 526 | static int hw_dev_close(int dev_index) |
f302a082 | 527 | { |
f1898235 JH |
528 | struct sr_dev_inst *sdi; |
529 | ||
530 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
7ae2f9d5 | 531 | sr_err("fx2lafw: %s: sdi was NULL.", __func__); |
0abee507 | 532 | return SR_ERR_BUG; |
f1898235 JH |
533 | } |
534 | ||
535 | /* TODO */ | |
536 | close_dev(sdi); | |
537 | ||
f302a082 JH |
538 | return SR_OK; |
539 | } | |
540 | ||
541 | static int hw_cleanup(void) | |
542 | { | |
187b3582 JH |
543 | GSList *l; |
544 | struct sr_dev_inst *sdi; | |
772a0e61 | 545 | struct context *ctx; |
62bc70e4 | 546 | int ret = SR_OK; |
187b3582 | 547 | |
f4a9e5c0 | 548 | for (l = dev_insts; l; l = l->next) { |
62bc70e4 JH |
549 | if (!(sdi = l->data)) { |
550 | /* Log error, but continue cleaning up the rest. */ | |
7ae2f9d5 UH |
551 | sr_err("fx2lafw: %s: sdi was NULL, continuing.", |
552 | __func__); | |
62bc70e4 JH |
553 | ret = SR_ERR_BUG; |
554 | continue; | |
555 | } | |
556 | if (!(ctx = sdi->priv)) { | |
557 | /* Log error, but continue cleaning up the rest. */ | |
558 | sr_err("fx2lafw: %s: sdi->priv was NULL, continuing", | |
559 | __func__); | |
560 | ret = SR_ERR_BUG; | |
561 | continue; | |
562 | } | |
563 | close_dev(sdi); | |
187b3582 JH |
564 | sdi = l->data; |
565 | sr_dev_inst_free(sdi); | |
566 | } | |
567 | ||
62bc70e4 JH |
568 | g_slist_free(dev_insts); |
569 | dev_insts = NULL; | |
187b3582 | 570 | |
f4a9e5c0 | 571 | if (usb_context) |
187b3582 JH |
572 | libusb_exit(usb_context); |
573 | usb_context = NULL; | |
574 | ||
62bc70e4 | 575 | return ret; |
f302a082 JH |
576 | } |
577 | ||
b7f578be | 578 | static const void *hw_dev_info_get(int dev_index, int dev_info_id) |
f302a082 | 579 | { |
8b35f474 | 580 | struct sr_dev_inst *sdi; |
772a0e61 | 581 | struct context *ctx; |
8b35f474 | 582 | |
772a0e61 | 583 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
8b35f474 | 584 | return NULL; |
cdfdd711 | 585 | ctx = sdi->priv; |
8b35f474 | 586 | |
772a0e61 | 587 | switch (dev_info_id) { |
8b35f474 JH |
588 | case SR_DI_INST: |
589 | return sdi; | |
590 | case SR_DI_NUM_PROBES: | |
d1ddc7a9 JH |
591 | return GINT_TO_POINTER( |
592 | (ctx->profile->dev_caps & DEV_CAPS_16BIT) ? | |
593 | 16 : 8); | |
8b35f474 | 594 | case SR_DI_PROBE_NAMES: |
da686568 | 595 | return probe_names; |
8b35f474 | 596 | case SR_DI_SAMPLERATES: |
590b9f9a | 597 | return &samplerates; |
8b35f474 JH |
598 | case SR_DI_TRIGGER_TYPES: |
599 | return TRIGGER_TYPES; | |
e3186647 JH |
600 | case SR_DI_CUR_SAMPLERATE: |
601 | return &ctx->cur_samplerate; | |
8b35f474 JH |
602 | } |
603 | ||
f302a082 JH |
604 | return NULL; |
605 | } | |
606 | ||
772a0e61 | 607 | static int hw_dev_status_get(int dev_index) |
f302a082 | 608 | { |
aae2fed6 | 609 | const struct sr_dev_inst *const sdi = |
772a0e61 | 610 | sr_dev_inst_get(dev_insts, dev_index); |
aae2fed6 JH |
611 | |
612 | if (!sdi) | |
613 | return SR_ST_NOT_FOUND; | |
614 | ||
615 | return sdi->status; | |
f302a082 JH |
616 | } |
617 | ||
915f7cc8 | 618 | static const int *hw_hwcap_get_all(void) |
f302a082 | 619 | { |
da686568 | 620 | return hwcaps; |
f302a082 JH |
621 | } |
622 | ||
1b79df2f | 623 | static int hw_dev_config_set(int dev_index, int hwcap, const void *value) |
f302a082 | 624 | { |
7cb621d4 | 625 | struct sr_dev_inst *sdi; |
772a0e61 | 626 | struct context *ctx; |
7cb621d4 JH |
627 | int ret; |
628 | ||
629 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
630 | return SR_ERR; | |
631 | ctx = sdi->priv; | |
632 | ||
e3186647 | 633 | if (hwcap == SR_HWCAP_SAMPLERATE) { |
1b79df2f | 634 | ctx->cur_samplerate = *(const uint64_t *)value; |
e3186647 JH |
635 | ret = SR_OK; |
636 | } else if (hwcap == SR_HWCAP_PROBECONFIG) { | |
6c6781b6 | 637 | ret = configure_probes(ctx, (GSList *) value); |
e3186647 | 638 | } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) { |
1b79df2f | 639 | ctx->limit_samples = *(const uint64_t *)value; |
7cb621d4 JH |
640 | ret = SR_OK; |
641 | } else { | |
642 | ret = SR_ERR; | |
643 | } | |
644 | ||
645 | return ret; | |
f302a082 JH |
646 | } |
647 | ||
1f9813eb | 648 | static int receive_data(int fd, int revents, void *cb_data) |
610dbb70 JH |
649 | { |
650 | struct timeval tv; | |
651 | ||
652 | /* Avoid compiler warnings. */ | |
653 | (void)fd; | |
654 | (void)revents; | |
1f9813eb | 655 | (void)cb_data; |
610dbb70 JH |
656 | |
657 | tv.tv_sec = tv.tv_usec = 0; | |
658 | libusb_handle_events_timeout(usb_context, &tv); | |
659 | ||
660 | return TRUE; | |
661 | } | |
662 | ||
2e526f4a | 663 | static void abort_acquisition(struct context *ctx) |
cb61e9f7 JH |
664 | { |
665 | ctx->num_samples = -1; | |
666 | } | |
667 | ||
3e6292b2 | 668 | static void finish_acquisition(struct context *ctx) |
2e526f4a JH |
669 | { |
670 | struct sr_datafeed_packet packet; | |
cb61e9f7 | 671 | int i; |
2e526f4a | 672 | |
cb61e9f7 | 673 | /* Terminate session */ |
2e526f4a JH |
674 | packet.type = SR_DF_END; |
675 | sr_session_send(ctx->session_dev_id, &packet); | |
676 | ||
cb61e9f7 JH |
677 | /* Remove fds from polling */ |
678 | const struct libusb_pollfd **const lupfd = | |
679 | libusb_get_pollfds(usb_context); | |
680 | for (i = 0; lupfd[i]; i++) | |
681 | sr_source_remove(lupfd[i]->fd); | |
682 | free(lupfd); /* NOT g_free()! */ | |
2e526f4a JH |
683 | } |
684 | ||
610dbb70 JH |
685 | static void receive_transfer(struct libusb_transfer *transfer) |
686 | { | |
687 | /* TODO: These statics have to move to the ctx struct. */ | |
610dbb70 JH |
688 | static int empty_transfer_count = 0; |
689 | struct sr_datafeed_packet packet; | |
690 | struct sr_datafeed_logic logic; | |
2e526f4a | 691 | struct context *ctx = transfer->user_data; |
d1ddc7a9 JH |
692 | int trigger_offset, i; |
693 | uint8_t *new_buf; | |
610dbb70 | 694 | |
610dbb70 JH |
695 | /* |
696 | * If acquisition has already ended, just free any queued up | |
697 | * transfer that come in. | |
698 | */ | |
2e526f4a | 699 | if (ctx->num_samples == -1) { |
610dbb70 JH |
700 | if (transfer) |
701 | libusb_free_transfer(transfer); | |
cb61e9f7 JH |
702 | |
703 | ctx->submitted_transfers--; | |
704 | if (ctx->submitted_transfers == 0) | |
705 | finish_acquisition(ctx); | |
706 | ||
610dbb70 JH |
707 | return; |
708 | } | |
709 | ||
7ae2f9d5 | 710 | sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.", |
610dbb70 JH |
711 | transfer->status, transfer->actual_length); |
712 | ||
713 | /* Save incoming transfer before reusing the transfer struct. */ | |
d1ddc7a9 JH |
714 | uint8_t *const cur_buf = transfer->buffer; |
715 | const int sample_width = ctx->sample_wide ? 2 : 1; | |
716 | const int cur_sample_count = transfer->actual_length / sample_width; | |
610dbb70 JH |
717 | |
718 | /* Fire off a new request. */ | |
719 | if (!(new_buf = g_try_malloc(4096))) { | |
7ae2f9d5 | 720 | sr_err("fx2lafw: %s: new_buf malloc failed.", __func__); |
eabf2403 | 721 | libusb_free_transfer(transfer); |
610dbb70 JH |
722 | return; /* TODO: SR_ERR_MALLOC */ |
723 | } | |
724 | ||
725 | transfer->buffer = new_buf; | |
726 | transfer->length = 4096; | |
727 | if (libusb_submit_transfer(transfer) != 0) { | |
728 | /* TODO: Stop session? */ | |
729 | /* TODO: Better error message. */ | |
7ae2f9d5 | 730 | sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__); |
610dbb70 JH |
731 | } |
732 | ||
d1ddc7a9 | 733 | if (transfer->actual_length == 0) { |
610dbb70 JH |
734 | empty_transfer_count++; |
735 | if (empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
736 | /* | |
737 | * The FX2 gave up. End the acquisition, the frontend | |
738 | * will work out that the samplecount is short. | |
739 | */ | |
2e526f4a | 740 | abort_acquisition(ctx); |
610dbb70 | 741 | } |
eabf2403 | 742 | g_free(cur_buf); |
610dbb70 JH |
743 | return; |
744 | } else { | |
745 | empty_transfer_count = 0; | |
746 | } | |
747 | ||
6c6781b6 JH |
748 | trigger_offset = 0; |
749 | if (ctx->trigger_stage >= 0) { | |
d1ddc7a9 JH |
750 | for (i = 0; i < cur_sample_count; i++) { |
751 | ||
752 | const uint16_t cur_sample = ctx->sample_wide ? | |
753 | *((const uint16_t*)cur_buf + i) : | |
754 | *((const uint8_t*)cur_buf + i); | |
6c6781b6 | 755 | |
d1ddc7a9 JH |
756 | if ((cur_sample & ctx->trigger_mask[ctx->trigger_stage]) == |
757 | ctx->trigger_value[ctx->trigger_stage]) { | |
6c6781b6 | 758 | /* Match on this trigger stage. */ |
d1ddc7a9 | 759 | ctx->trigger_buffer[ctx->trigger_stage] = cur_sample; |
6c6781b6 JH |
760 | ctx->trigger_stage++; |
761 | ||
d1ddc7a9 JH |
762 | if (ctx->trigger_stage == NUM_TRIGGER_STAGES || |
763 | ctx->trigger_mask[ctx->trigger_stage] == 0) { | |
6c6781b6 JH |
764 | /* Match on all trigger stages, we're done. */ |
765 | trigger_offset = i + 1; | |
766 | ||
767 | /* | |
768 | * TODO: Send pre-trigger buffer to session bus. | |
769 | * Tell the frontend we hit the trigger here. | |
770 | */ | |
771 | packet.type = SR_DF_TRIGGER; | |
772 | packet.payload = NULL; | |
773 | sr_session_send(ctx->session_dev_id, &packet); | |
774 | ||
775 | /* | |
776 | * Send the samples that triggered it, since we're | |
777 | * skipping past them. | |
778 | */ | |
779 | packet.type = SR_DF_LOGIC; | |
780 | packet.payload = &logic; | |
fa114e4a LPC |
781 | logic.unitsize = sizeof(*ctx->trigger_buffer); |
782 | logic.length = ctx->trigger_stage * logic.unitsize; | |
6c6781b6 JH |
783 | logic.data = ctx->trigger_buffer; |
784 | sr_session_send(ctx->session_dev_id, &packet); | |
785 | ||
786 | ctx->trigger_stage = TRIGGER_FIRED; | |
787 | break; | |
788 | } | |
f3ab43a8 LPC |
789 | } else if (ctx->trigger_stage > 0) { |
790 | /* | |
791 | * We had a match before, but not in the next sample. However, we may | |
792 | * have a match on this stage in the next bit -- trigger on 0001 will | |
793 | * fail on seeing 00001, so we need to go back to stage 0 -- but at | |
794 | * the next sample from the one that matched originally, which the | |
795 | * counter increment at the end of the loop takes care of. | |
796 | */ | |
6c6781b6 JH |
797 | i -= ctx->trigger_stage; |
798 | if (i < -1) | |
799 | i = -1; /* Oops, went back past this buffer. */ | |
800 | /* Reset trigger stage. */ | |
801 | ctx->trigger_stage = 0; | |
802 | } | |
803 | } | |
804 | } | |
610dbb70 | 805 | |
6c6781b6 JH |
806 | if (ctx->trigger_stage == TRIGGER_FIRED) { |
807 | /* Send the incoming transfer to the session bus. */ | |
0a88ec3d | 808 | const int trigger_offset_bytes = trigger_offset * sample_width; |
6c6781b6 JH |
809 | packet.type = SR_DF_LOGIC; |
810 | packet.payload = &logic; | |
d1ddc7a9 JH |
811 | logic.length = transfer->actual_length - trigger_offset_bytes; |
812 | logic.unitsize = sample_width; | |
813 | logic.data = cur_buf + trigger_offset_bytes; | |
6c6781b6 | 814 | sr_session_send(ctx->session_dev_id, &packet); |
6c6781b6 | 815 | |
d1ddc7a9 | 816 | ctx->num_samples += cur_sample_count; |
6c6781b6 JH |
817 | if (ctx->limit_samples && |
818 | (unsigned int)ctx->num_samples > ctx->limit_samples) { | |
819 | abort_acquisition(ctx); | |
820 | } | |
821 | } else { | |
822 | /* | |
823 | * TODO: Buffer pre-trigger data in capture | |
824 | * ratio-sized buffer. | |
825 | */ | |
610dbb70 | 826 | } |
eabf2403 | 827 | g_free(cur_buf); |
610dbb70 JH |
828 | } |
829 | ||
3cd3a20b | 830 | static int hw_dev_acquisition_start(int dev_index, void *cb_data) |
f302a082 | 831 | { |
610dbb70 | 832 | struct sr_dev_inst *sdi; |
bd47acab LPC |
833 | struct sr_datafeed_packet packet; |
834 | struct sr_datafeed_header header; | |
f366e86c | 835 | struct sr_datafeed_meta_logic meta; |
772a0e61 | 836 | struct context *ctx; |
610dbb70 JH |
837 | struct libusb_transfer *transfer; |
838 | const struct libusb_pollfd **lupfd; | |
ebc34738 | 839 | int ret, size, i; |
610dbb70 JH |
840 | unsigned char *buf; |
841 | ||
842 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
843 | return SR_ERR; | |
844 | ctx = sdi->priv; | |
3cd3a20b | 845 | ctx->session_dev_id = cb_data; |
2e526f4a | 846 | ctx->num_samples = 0; |
610dbb70 | 847 | |
610dbb70 JH |
848 | /* Start with 2K transfer, subsequently increased to 4K. */ |
849 | size = 2048; | |
850 | for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) { | |
851 | if (!(buf = g_try_malloc(size))) { | |
7ae2f9d5 | 852 | sr_err("fx2lafw: %s: buf malloc failed.", __func__); |
610dbb70 JH |
853 | return SR_ERR_MALLOC; |
854 | } | |
855 | transfer = libusb_alloc_transfer(0); | |
856 | libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl, | |
857 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
858 | receive_transfer, ctx, 40); | |
859 | if (libusb_submit_transfer(transfer) != 0) { | |
860 | /* TODO: Free them all. */ | |
861 | libusb_free_transfer(transfer); | |
862 | g_free(buf); | |
863 | return SR_ERR; | |
864 | } | |
cb61e9f7 JH |
865 | |
866 | ctx->submitted_transfers++; | |
610dbb70 JH |
867 | size = 4096; |
868 | } | |
869 | ||
870 | lupfd = libusb_get_pollfds(usb_context); | |
871 | for (i = 0; lupfd[i]; i++) | |
872 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, | |
873 | 40, receive_data, NULL); | |
874 | free(lupfd); /* NOT g_free()! */ | |
875 | ||
bd47acab LPC |
876 | packet.type = SR_DF_HEADER; |
877 | packet.payload = &header; | |
878 | header.feed_version = 1; | |
879 | gettimeofday(&header.starttime, NULL); | |
880 | sr_session_send(cb_data, &packet); | |
f366e86c BV |
881 | |
882 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
bd47acab LPC |
883 | packet.type = SR_DF_META_LOGIC; |
884 | packet.payload = &meta; | |
f366e86c | 885 | meta.samplerate = ctx->cur_samplerate; |
d1ddc7a9 | 886 | meta.num_probes = ctx->sample_wide ? 16 : 8; |
bd47acab | 887 | sr_session_send(cb_data, &packet); |
610dbb70 | 888 | |
ebc34738 | 889 | if ((ret = command_start_acquisition (ctx->usb->devhdl, |
d1ddc7a9 | 890 | ctx->cur_samplerate, ctx->sample_wide)) != SR_OK) { |
ebc34738 | 891 | return ret; |
017375d1 JH |
892 | } |
893 | ||
f302a082 JH |
894 | return SR_OK; |
895 | } | |
896 | ||
3cd3a20b | 897 | /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ |
c8f2c9dd | 898 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data) |
f302a082 | 899 | { |
2e526f4a | 900 | struct sr_dev_inst *sdi; |
5da93902 | 901 | |
f4a9e5c0 | 902 | /* Avoid compiler warnings. */ |
2e526f4a | 903 | (void)cb_data; |
5da93902 | 904 | |
2e526f4a JH |
905 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) |
906 | return SR_ERR; | |
907 | ||
908 | abort_acquisition(sdi->priv); | |
5da93902 | 909 | |
f302a082 JH |
910 | return SR_OK; |
911 | } | |
912 | ||
c09f0b57 | 913 | SR_PRIV struct sr_dev_driver fx2lafw_driver_info = { |
f302a082 | 914 | .name = "fx2lafw", |
2e7cb004 | 915 | .longname = "fx2lafw (generic driver for FX2 based LAs)", |
f302a082 JH |
916 | .api_version = 1, |
917 | .init = hw_init, | |
918 | .cleanup = hw_cleanup, | |
919 | .dev_open = hw_dev_open, | |
920 | .dev_close = hw_dev_close, | |
921 | .dev_info_get = hw_dev_info_get, | |
922 | .dev_status_get = hw_dev_status_get, | |
923 | .hwcap_get_all = hw_hwcap_get_all, | |
924 | .dev_config_set = hw_dev_config_set, | |
925 | .dev_acquisition_start = hw_dev_acquisition_start, | |
926 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
927 | }; |