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