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