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