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