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