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