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