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