]>
Commit | Line | Data |
---|---|---|
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> | |
22 | #include <inttypes.h> | |
23 | #include <glib.h> | |
24 | #include <libusb.h> | |
25 | #include "config.h" | |
26 | #include "sigrok.h" | |
27 | #include "sigrok-internal.h" | |
28 | #include "fx2lafw.h" | |
29 | #include "command.h" | |
30 | ||
31 | static const struct fx2lafw_profile supported_fx2[] = { | |
32 | /* | |
33 | * CWAV USBee AX | |
34 | * EE Electronics ESLA201A | |
35 | */ | |
36 | { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL, | |
37 | FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 }, | |
38 | ||
39 | /* | |
40 | * CWAV USBee SX | |
41 | */ | |
42 | { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL, | |
43 | FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 }, | |
44 | ||
45 | /* | |
46 | * Saleae Logic | |
47 | * EE Electronics ESLA100 | |
48 | * Robomotic MiniLogic | |
49 | */ | |
50 | { 0x0925, 0x3881, "Saleae", "Logic", NULL, | |
51 | FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 }, | |
52 | ||
53 | { 0, 0, 0, 0, 0, 0, 0 } | |
54 | }; | |
55 | ||
56 | static int hwcaps[] = { | |
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, | |
63 | 0, | |
64 | }; | |
65 | ||
66 | /* | |
67 | * TODO: Different probe_names[] for each supported device. | |
68 | */ | |
69 | static const char *probe_names[] = { | |
70 | "0", | |
71 | "1", | |
72 | "2", | |
73 | "3", | |
74 | "4", | |
75 | "5", | |
76 | "6", | |
77 | "7", | |
78 | NULL, | |
79 | }; | |
80 | ||
81 | static uint64_t supported_samplerates[] = { | |
82 | SR_MHZ(1), | |
83 | SR_MHZ(2), | |
84 | SR_MHZ(3), | |
85 | SR_MHZ(4), | |
86 | SR_MHZ(6), | |
87 | SR_MHZ(8), | |
88 | SR_MHZ(12), | |
89 | SR_MHZ(16), | |
90 | SR_MHZ(24), | |
91 | }; | |
92 | ||
93 | static struct sr_samplerates samplerates = { | |
94 | 0, | |
95 | 0, | |
96 | 0, | |
97 | supported_samplerates, | |
98 | }; | |
99 | ||
100 | static GSList *dev_insts = NULL; | |
101 | static libusb_context *usb_context = NULL; | |
102 | ||
103 | static int hw_dev_config_set(int dev_index, int hwcap, void *value); | |
104 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data); | |
105 | ||
106 | /** | |
107 | * Check the USB configuration to determine if this is an fx2lafw device. | |
108 | * | |
109 | * @return TRUE if the device's configuration profile match fx2lafw | |
110 | * configuration, FALSE otherwise. | |
111 | */ | |
112 | static gboolean check_conf_profile(libusb_device *dev) | |
113 | { | |
114 | struct libusb_device_descriptor des; | |
115 | struct libusb_config_descriptor *conf_dsc = NULL; | |
116 | const struct libusb_interface_descriptor *intf_dsc; | |
117 | gboolean ret = FALSE; | |
118 | ||
119 | while (!ret) { | |
120 | /* Assume the FW has not been loaded, unless proven wrong. */ | |
121 | ret = FALSE; | |
122 | ||
123 | if (libusb_get_device_descriptor(dev, &des) != 0) | |
124 | break; | |
125 | ||
126 | /* Need exactly 1 configuration. */ | |
127 | if (des.bNumConfigurations != 1) | |
128 | break; | |
129 | ||
130 | if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0) | |
131 | break; | |
132 | ||
133 | /* Need exactly 1 interface. */ | |
134 | if (conf_dsc->bNumInterfaces != 1) | |
135 | break; | |
136 | ||
137 | /* Need just one alternate setting. */ | |
138 | if (conf_dsc->interface[0].num_altsetting != 1) | |
139 | break; | |
140 | ||
141 | /* Need exactly 2 endpoints. */ | |
142 | intf_dsc = &(conf_dsc->interface[0].altsetting[0]); | |
143 | if (intf_dsc->bNumEndpoints != 2) | |
144 | break; | |
145 | ||
146 | /* The first endpoint should be 2 (inbound). */ | |
147 | if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) != | |
148 | (2 | LIBUSB_ENDPOINT_IN)) | |
149 | break; | |
150 | ||
151 | /* If we made it here, it must be an fx2lafw. */ | |
152 | ret = TRUE; | |
153 | } | |
154 | ||
155 | if (conf_dsc) | |
156 | libusb_free_config_descriptor(conf_dsc); | |
157 | ||
158 | return ret; | |
159 | } | |
160 | ||
161 | static int fx2lafw_dev_open(int dev_index) | |
162 | { | |
163 | libusb_device **devlist; | |
164 | struct libusb_device_descriptor des; | |
165 | struct sr_dev_inst *sdi; | |
166 | struct context *ctx; | |
167 | int ret, skip, i; | |
168 | ||
169 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
170 | return SR_ERR; | |
171 | ctx = sdi->priv; | |
172 | ||
173 | if (sdi->status == SR_ST_ACTIVE) | |
174 | /* already in use */ | |
175 | return SR_ERR; | |
176 | ||
177 | skip = 0; | |
178 | libusb_get_device_list(usb_context, &devlist); | |
179 | for (i = 0; devlist[i]; i++) { | |
180 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
181 | sr_err("fx2lafw: Failed to get device descriptor: %d.", | |
182 | ret); | |
183 | continue; | |
184 | } | |
185 | ||
186 | if (des.idVendor != ctx->profile->vid | |
187 | || des.idProduct != ctx->profile->pid) | |
188 | continue; | |
189 | ||
190 | if (sdi->status == SR_ST_INITIALIZING) { | |
191 | if (skip != dev_index) { | |
192 | /* Skip devices of this type that aren't the one we want. */ | |
193 | skip += 1; | |
194 | continue; | |
195 | } | |
196 | } else if (sdi->status == SR_ST_INACTIVE) { | |
197 | /* | |
198 | * This device is fully enumerated, so we need to find | |
199 | * this device by vendor, product, bus and address. | |
200 | */ | |
201 | if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus | |
202 | || libusb_get_device_address(devlist[i]) != ctx->usb->address) | |
203 | /* this is not the one */ | |
204 | continue; | |
205 | } | |
206 | ||
207 | if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) { | |
208 | if (ctx->usb->address == 0xff) | |
209 | /* | |
210 | * first time we touch this device after firmware upload, | |
211 | * so we don't know the address yet. | |
212 | */ | |
213 | ctx->usb->address = libusb_get_device_address(devlist[i]); | |
214 | ||
215 | sdi->status = SR_ST_ACTIVE; | |
216 | sr_info("fx2lafw: Opened device %d on %d.%d " | |
217 | "interface %d.", sdi->index, ctx->usb->bus, | |
218 | ctx->usb->address, USB_INTERFACE); | |
219 | } else { | |
220 | sr_err("fx2lafw: Failed to open device: %d.", ret); | |
221 | } | |
222 | ||
223 | /* if we made it here, we handled the device one way or another */ | |
224 | break; | |
225 | } | |
226 | libusb_free_device_list(devlist, 1); | |
227 | ||
228 | if (sdi->status != SR_ST_ACTIVE) | |
229 | return SR_ERR; | |
230 | ||
231 | return SR_OK; | |
232 | } | |
233 | ||
234 | static void close_dev(struct sr_dev_inst *sdi) | |
235 | { | |
236 | struct context *ctx; | |
237 | ||
238 | ctx = sdi->priv; | |
239 | ||
240 | if (ctx->usb->devhdl == NULL) | |
241 | return; | |
242 | ||
243 | sr_info("fx2lafw: Closing device %d on %d.%d interface %d.", | |
244 | sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE); | |
245 | libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE); | |
246 | libusb_close(ctx->usb->devhdl); | |
247 | ctx->usb->devhdl = NULL; | |
248 | sdi->status = SR_ST_INACTIVE; | |
249 | } | |
250 | ||
251 | static int configure_probes(struct context *ctx, GSList *probes) | |
252 | { | |
253 | struct sr_probe *probe; | |
254 | GSList *l; | |
255 | int probe_bit, stage, i; | |
256 | char *tc; | |
257 | ||
258 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
259 | ctx->trigger_mask[i] = 0; | |
260 | ctx->trigger_value[i] = 0; | |
261 | } | |
262 | ||
263 | stage = -1; | |
264 | for (l = probes; l; l = l->next) { | |
265 | probe = (struct sr_probe *)l->data; | |
266 | if (probe->enabled == FALSE) | |
267 | continue; | |
268 | probe_bit = 1 << (probe->index - 1); | |
269 | if (!(probe->trigger)) | |
270 | continue; | |
271 | ||
272 | stage = 0; | |
273 | for (tc = probe->trigger; *tc; tc++) { | |
274 | ctx->trigger_mask[stage] |= probe_bit; | |
275 | if (*tc == '1') | |
276 | ctx->trigger_value[stage] |= probe_bit; | |
277 | stage++; | |
278 | if (stage > NUM_TRIGGER_STAGES) | |
279 | return SR_ERR; | |
280 | } | |
281 | } | |
282 | ||
283 | if (stage == -1) | |
284 | /* | |
285 | * We didn't configure any triggers, make sure acquisition | |
286 | * doesn't wait for any. | |
287 | */ | |
288 | ctx->trigger_stage = TRIGGER_FIRED; | |
289 | else | |
290 | ctx->trigger_stage = 0; | |
291 | ||
292 | return SR_OK; | |
293 | } | |
294 | ||
295 | static struct context *fx2lafw_dev_new(void) | |
296 | { | |
297 | struct context *ctx; | |
298 | ||
299 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { | |
300 | sr_err("fx2lafw: %s: ctx malloc failed.", __func__); | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | ctx->trigger_stage = TRIGGER_FIRED; | |
305 | ||
306 | return ctx; | |
307 | } | |
308 | ||
309 | /* | |
310 | * API callbacks | |
311 | */ | |
312 | ||
313 | static int hw_init(const char *devinfo) | |
314 | { | |
315 | struct sr_dev_inst *sdi; | |
316 | struct libusb_device_descriptor des; | |
317 | const struct fx2lafw_profile *prof; | |
318 | struct context *ctx; | |
319 | libusb_device **devlist; | |
320 | int ret; | |
321 | int devcnt = 0; | |
322 | int i, j; | |
323 | ||
324 | /* Avoid compiler warnings. */ | |
325 | (void)devinfo; | |
326 | ||
327 | if (libusb_init(&usb_context) != 0) { | |
328 | sr_warn("fx2lafw: Failed to initialize libusb."); | |
329 | return 0; | |
330 | } | |
331 | ||
332 | /* Find all fx2lafw compatible devices and upload firware to them. */ | |
333 | libusb_get_device_list(usb_context, &devlist); | |
334 | for (i = 0; devlist[i]; i++) { | |
335 | ||
336 | if ((ret = libusb_get_device_descriptor( | |
337 | devlist[i], &des)) != 0) { | |
338 | sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret); | |
339 | continue; | |
340 | } | |
341 | ||
342 | prof = NULL; | |
343 | for (j = 0; supported_fx2[j].vid; j++) { | |
344 | if (des.idVendor == supported_fx2[j].vid && | |
345 | des.idProduct == supported_fx2[j].pid) { | |
346 | prof = &supported_fx2[j]; | |
347 | } | |
348 | } | |
349 | ||
350 | /* Skip if the device was not found */ | |
351 | if (!prof) | |
352 | continue; | |
353 | ||
354 | sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING, | |
355 | prof->vendor, prof->model, prof->model_version); | |
356 | if (!sdi) | |
357 | return 0; | |
358 | ||
359 | ctx = fx2lafw_dev_new(); | |
360 | ctx->profile = prof; | |
361 | sdi->priv = ctx; | |
362 | dev_insts = g_slist_append(dev_insts, sdi); | |
363 | ||
364 | if (check_conf_profile(devlist[i])) { | |
365 | /* Already has the firmware, so fix the new address. */ | |
366 | sr_dbg("fx2lafw: Found an fx2lafw device."); | |
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 { | |
372 | if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, | |
373 | prof->firmware) == SR_OK) | |
374 | /* Remember when the firmware on this device was updated */ | |
375 | g_get_current_time(&ctx->fw_updated); | |
376 | else | |
377 | sr_err("fx2lafw: Firmware upload failed for " | |
378 | "device %d.", devcnt); | |
379 | ctx->usb = sr_usb_dev_inst_new | |
380 | (libusb_get_bus_number(devlist[i]), 0xff, NULL); | |
381 | } | |
382 | ||
383 | devcnt++; | |
384 | } | |
385 | libusb_free_device_list(devlist, 1); | |
386 | ||
387 | return devcnt; | |
388 | } | |
389 | ||
390 | static int hw_dev_open(int dev_index) | |
391 | { | |
392 | GTimeVal cur_time; | |
393 | struct sr_dev_inst *sdi; | |
394 | struct context *ctx; | |
395 | int timediff, ret; | |
396 | ||
397 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
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 | */ | |
405 | ret = 0; | |
406 | if (GTV_TO_MSEC(ctx->fw_updated) > 0) { | |
407 | sr_info("fx2lafw: Waiting for device to reset."); | |
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) { | |
412 | if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK) | |
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 | } | |
418 | sr_info("fx2lafw: Device came back after %d ms.", timediff); | |
419 | } else { | |
420 | ret = fx2lafw_dev_open(dev_index); | |
421 | } | |
422 | ||
423 | if (ret != SR_OK) { | |
424 | sr_err("fx2lafw: Unable to open device."); | |
425 | return SR_ERR; | |
426 | } | |
427 | ctx = sdi->priv; | |
428 | ||
429 | ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE); | |
430 | if (ret != 0) { | |
431 | sr_err("fx2lafw: Unable to claim interface: %d.", ret); | |
432 | return SR_ERR; | |
433 | } | |
434 | ||
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, | |
438 | &supported_samplerates[0]) == SR_ERR) | |
439 | return SR_ERR; | |
440 | } | |
441 | ||
442 | return SR_OK; | |
443 | } | |
444 | ||
445 | static int hw_dev_close(int dev_index) | |
446 | { | |
447 | struct sr_dev_inst *sdi; | |
448 | ||
449 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
450 | sr_err("fx2lafw: %s: sdi was NULL.", __func__); | |
451 | return SR_ERR_BUG; | |
452 | } | |
453 | ||
454 | /* TODO */ | |
455 | close_dev(sdi); | |
456 | ||
457 | return SR_OK; | |
458 | } | |
459 | ||
460 | static int hw_cleanup(void) | |
461 | { | |
462 | GSList *l; | |
463 | struct sr_dev_inst *sdi; | |
464 | struct context *ctx; | |
465 | int ret = SR_OK; | |
466 | ||
467 | for (l = dev_insts; l; l = l->next) { | |
468 | if (!(sdi = l->data)) { | |
469 | /* Log error, but continue cleaning up the rest. */ | |
470 | sr_err("fx2lafw: %s: sdi was NULL, continuing.", | |
471 | __func__); | |
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); | |
483 | sdi = l->data; | |
484 | sr_dev_inst_free(sdi); | |
485 | } | |
486 | ||
487 | g_slist_free(dev_insts); | |
488 | dev_insts = NULL; | |
489 | ||
490 | if (usb_context) | |
491 | libusb_exit(usb_context); | |
492 | usb_context = NULL; | |
493 | ||
494 | return ret; | |
495 | } | |
496 | ||
497 | static void *hw_dev_info_get(int dev_index, int dev_info_id) | |
498 | { | |
499 | struct sr_dev_inst *sdi; | |
500 | struct context *ctx; | |
501 | ||
502 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
503 | return NULL; | |
504 | ctx = sdi->priv; | |
505 | ||
506 | switch (dev_info_id) { | |
507 | case SR_DI_INST: | |
508 | return sdi; | |
509 | case SR_DI_NUM_PROBES: | |
510 | return GINT_TO_POINTER(ctx->profile->num_probes); | |
511 | case SR_DI_PROBE_NAMES: | |
512 | return probe_names; | |
513 | case SR_DI_SAMPLERATES: | |
514 | return &samplerates; | |
515 | case SR_DI_TRIGGER_TYPES: | |
516 | return TRIGGER_TYPES; | |
517 | case SR_DI_CUR_SAMPLERATE: | |
518 | return &ctx->cur_samplerate; | |
519 | } | |
520 | ||
521 | return NULL; | |
522 | } | |
523 | ||
524 | static int hw_dev_status_get(int dev_index) | |
525 | { | |
526 | const struct sr_dev_inst *const sdi = | |
527 | sr_dev_inst_get(dev_insts, dev_index); | |
528 | ||
529 | if (!sdi) | |
530 | return SR_ST_NOT_FOUND; | |
531 | ||
532 | return sdi->status; | |
533 | } | |
534 | ||
535 | static int *hw_hwcap_get_all(void) | |
536 | { | |
537 | return hwcaps; | |
538 | } | |
539 | ||
540 | static int hw_dev_config_set(int dev_index, int hwcap, void *value) | |
541 | { | |
542 | struct sr_dev_inst *sdi; | |
543 | struct context *ctx; | |
544 | int ret; | |
545 | ||
546 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
547 | return SR_ERR; | |
548 | ctx = sdi->priv; | |
549 | ||
550 | if (hwcap == SR_HWCAP_SAMPLERATE) { | |
551 | ctx->cur_samplerate = *(uint64_t *)value; | |
552 | ret = SR_OK; | |
553 | } else if (hwcap == SR_HWCAP_PROBECONFIG) { | |
554 | ret = configure_probes(ctx, (GSList *) value); | |
555 | } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) { | |
556 | ctx->limit_samples = *(uint64_t *)value; | |
557 | ret = SR_OK; | |
558 | } else { | |
559 | ret = SR_ERR; | |
560 | } | |
561 | ||
562 | return ret; | |
563 | } | |
564 | ||
565 | static int receive_data(int fd, int revents, void *cb_data) | |
566 | { | |
567 | struct timeval tv; | |
568 | ||
569 | /* Avoid compiler warnings. */ | |
570 | (void)fd; | |
571 | (void)revents; | |
572 | (void)cb_data; | |
573 | ||
574 | tv.tv_sec = tv.tv_usec = 0; | |
575 | libusb_handle_events_timeout(usb_context, &tv); | |
576 | ||
577 | return TRUE; | |
578 | } | |
579 | ||
580 | static void abort_acquisition(struct context *ctx) | |
581 | { | |
582 | ctx->num_samples = -1; | |
583 | } | |
584 | ||
585 | static void finish_acquisition(struct context *ctx) | |
586 | { | |
587 | struct sr_datafeed_packet packet; | |
588 | int i; | |
589 | ||
590 | /* Terminate session */ | |
591 | packet.type = SR_DF_END; | |
592 | sr_session_send(ctx->session_dev_id, &packet); | |
593 | ||
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()! */ | |
600 | } | |
601 | ||
602 | static void receive_transfer(struct libusb_transfer *transfer) | |
603 | { | |
604 | /* TODO: These statics have to move to the ctx struct. */ | |
605 | static int empty_transfer_count = 0; | |
606 | struct sr_datafeed_packet packet; | |
607 | struct sr_datafeed_logic logic; | |
608 | struct context *ctx = transfer->user_data; | |
609 | int cur_buflen, trigger_offset, i; | |
610 | unsigned char *cur_buf, *new_buf; | |
611 | ||
612 | /* | |
613 | * If acquisition has already ended, just free any queued up | |
614 | * transfer that come in. | |
615 | */ | |
616 | if (ctx->num_samples == -1) { | |
617 | if (transfer) | |
618 | libusb_free_transfer(transfer); | |
619 | ||
620 | ctx->submitted_transfers--; | |
621 | if (ctx->submitted_transfers == 0) | |
622 | finish_acquisition(ctx); | |
623 | ||
624 | return; | |
625 | } | |
626 | ||
627 | sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.", | |
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; | |
633 | ||
634 | /* Fire off a new request. */ | |
635 | if (!(new_buf = g_try_malloc(4096))) { | |
636 | sr_err("fx2lafw: %s: new_buf malloc failed.", __func__); | |
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. */ | |
645 | sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__); | |
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 | */ | |
655 | abort_acquisition(ctx); | |
656 | } | |
657 | return; | |
658 | } else { | |
659 | empty_transfer_count = 0; | |
660 | } | |
661 | ||
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 | } | |
716 | ||
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 | */ | |
737 | } | |
738 | } | |
739 | ||
740 | static int hw_dev_acquisition_start(int dev_index, void *cb_data) | |
741 | { | |
742 | struct sr_dev_inst *sdi; | |
743 | struct sr_datafeed_packet *packet; | |
744 | struct sr_datafeed_header *header; | |
745 | struct context *ctx; | |
746 | struct libusb_transfer *transfer; | |
747 | const struct libusb_pollfd **lupfd; | |
748 | int ret, size, i; | |
749 | unsigned char *buf; | |
750 | ||
751 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
752 | return SR_ERR; | |
753 | ctx = sdi->priv; | |
754 | ctx->session_dev_id = cb_data; | |
755 | ctx->num_samples = 0; | |
756 | ||
757 | if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { | |
758 | sr_err("fx2lafw: %s: packet malloc failed.", __func__); | |
759 | return SR_ERR_MALLOC; | |
760 | } | |
761 | ||
762 | if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { | |
763 | sr_err("fx2lafw: %s: header malloc failed.", __func__); | |
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))) { | |
771 | sr_err("fx2lafw: %s: buf malloc failed.", __func__); | |
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 | } | |
784 | ||
785 | ctx->submitted_transfers++; | |
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); | |
799 | header->samplerate = ctx->cur_samplerate; | |
800 | header->num_logic_probes = ctx->profile->num_probes; | |
801 | sr_session_send(cb_data, packet); | |
802 | g_free(header); | |
803 | g_free(packet); | |
804 | ||
805 | if ((ret = command_start_acquisition (ctx->usb->devhdl, | |
806 | ctx->cur_samplerate)) != SR_OK) { | |
807 | return ret; | |
808 | } | |
809 | ||
810 | return SR_OK; | |
811 | } | |
812 | ||
813 | /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ | |
814 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data) | |
815 | { | |
816 | struct sr_dev_inst *sdi; | |
817 | ||
818 | /* Avoid compiler warnings. */ | |
819 | (void)cb_data; | |
820 | ||
821 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) | |
822 | return SR_ERR; | |
823 | ||
824 | abort_acquisition(sdi->priv); | |
825 | ||
826 | return SR_OK; | |
827 | } | |
828 | ||
829 | SR_PRIV struct sr_dev_driver fx2lafw_driver_info = { | |
830 | .name = "fx2lafw", | |
831 | .longname = "fx2lafw (generic driver for FX2 based LAs)", | |
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 | }; |