]> sigrok.org Git - libsigrok.git/blame - hardware/saleae-logic/saleae-logic.c
make output modules a bit more crashproof
[libsigrok.git] / hardware / saleae-logic / saleae-logic.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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>
408e7199 26#include <sigrok.h>
a1bb33af 27#include "config.h"
a1bb33af 28
74b9b438 29#define USB_VENDOR 0x0925
a1bb33af 30#define USB_PRODUCT 0x3881
74b9b438
BV
31#define USB_VENDOR_NAME "Saleae"
32#define USB_MODEL_NAME "Logic"
33#define USB_MODEL_VERSION ""
a1bb33af 34
74b9b438
BV
35#define USB_INTERFACE 0
36#define USB_CONFIGURATION 1
37#define NUM_PROBES 8
38#define NUM_TRIGGER_STAGES 4
39#define TRIGGER_TYPES "01"
40#define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw"
a1bb33af
UH
41
42/* delay in ms */
74b9b438
BV
43#define FIRMWARE_RENUM_DELAY 2000
44#define NUM_SIMUL_TRANSFERS 10
45#define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2)
a1bb33af 46
fed16f06 47/* Software trigger implementation: positive values indicate trigger stage. */
74b9b438 48#define TRIGGER_FIRED -1
a1bb33af 49
6f5f21f9 50/* There is only one model Saleae Logic, and this is what it supports: */
a1bb33af
UH
51static int capabilities[] = {
52 HWCAP_LOGIC_ANALYZER,
53 HWCAP_SAMPLERATE,
54
6f5f21f9 55 /* These are really implemented in the driver, not the hardware. */
a1bb33af 56 HWCAP_LIMIT_SAMPLES,
74b9b438 57 HWCAP_CONTINUOUS,
6f5f21f9 58 0,
a1bb33af
UH
59};
60
6f5f21f9 61/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
a1bb33af
UH
62static GSList *device_instances = NULL;
63
6f5f21f9
UH
64/*
65 * Since we can't keep track of a Saleae Logic device after upgrading the
a1bb33af
UH
66 * firmware -- it re-enumerates into a different device address after the
67 * upgrade -- this is like a global lock. No device will open until a proper
68 * delay after the last device was upgraded.
69 */
afc8e4de 70GTimeVal firmware_updated = { 0, 0 };
a1bb33af
UH
71
72static libusb_context *usb_context = NULL;
73
74static uint64_t supported_samplerates[] = {
75 KHZ(200),
76 KHZ(250),
77 KHZ(500),
78 MHZ(1),
79 MHZ(2),
80 MHZ(4),
81 MHZ(8),
82 MHZ(12),
83 MHZ(16),
84 MHZ(24),
6f5f21f9 85 0,
a1bb33af
UH
86};
87
88static struct samplerates samplerates = {
89 KHZ(200),
90 MHZ(24),
91 0,
6f5f21f9 92 supported_samplerates,
a1bb33af
UH
93};
94
6f5f21f9 95/* TODO: All of these should go in a device-specific struct. */
4c100f32 96static uint64_t cur_samplerate = 0;
a1bb33af 97static uint64_t limit_samples = 0;
6f5f21f9
UH
98static uint8_t probe_mask = 0;
99static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
100static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
101static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
fed16f06 102
a1bb33af
UH
103int trigger_stage = TRIGGER_FIRED;
104
a1bb33af 105static int hw_set_configuration(int device_index, int capability, void *value);
9c48090a 106static void hw_stop_acquisition(int device_index, gpointer session_device_id);
a1bb33af 107
28fc6de0
UH
108/**
109 * Check the USB configuration to determine if this is a Saleae Logic.
110 *
111 * @return 1 if the device's configuration profile match the Logic firmware's
112 * configuration, 0 otherwise.
a1bb33af
UH
113 */
114int check_conf_profile(libusb_device *dev)
115{
116 struct libusb_device_descriptor des;
6f5f21f9 117 struct libusb_config_descriptor *conf_dsc = NULL;
a1bb33af 118 const struct libusb_interface_descriptor *intf_dsc;
6f5f21f9 119 int ret = -1;
a1bb33af 120
6f5f21f9
UH
121 while (ret == -1) {
122 /* Assume it's not a Saleae Logic unless proven wrong. */
a1bb33af
UH
123 ret = 0;
124
6f5f21f9 125 if (libusb_get_device_descriptor(dev, &des) != 0)
a1bb33af
UH
126 break;
127
6f5f21f9
UH
128 if (des.bNumConfigurations != 1)
129 /* Need exactly 1 configuration. */
a1bb33af
UH
130 break;
131
6f5f21f9 132 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
a1bb33af
UH
133 break;
134
6f5f21f9
UH
135 if (conf_dsc->bNumInterfaces != 1)
136 /* Need exactly 1 interface. */
a1bb33af
UH
137 break;
138
6f5f21f9
UH
139 if (conf_dsc->interface[0].num_altsetting != 1)
140 /* Need just one alternate setting. */
a1bb33af
UH
141 break;
142
143 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
6f5f21f9
UH
144 if (intf_dsc->bNumEndpoints != 2)
145 /* Need 2 endpoints. */
a1bb33af
UH
146 break;
147
6f5f21f9
UH
148 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
149 (1 | LIBUSB_ENDPOINT_OUT))
150 /* First endpoint should be 1 (outbound). */
a1bb33af
UH
151 break;
152
6f5f21f9
UH
153 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
154 (2 | LIBUSB_ENDPOINT_IN))
155 /* First endpoint should be 2 (inbound). */
a1bb33af
UH
156 break;
157
6f5f21f9 158 /* If we made it here, it must be a Saleae Logic. */
a1bb33af
UH
159 ret = 1;
160 }
6f5f21f9
UH
161
162 if (conf_dsc)
a1bb33af
UH
163 libusb_free_config_descriptor(conf_dsc);
164
165 return ret;
166}
167
a1bb33af
UH
168struct sigrok_device_instance *sl_open_device(int device_index)
169{
170 struct sigrok_device_instance *sdi;
171 libusb_device **devlist;
172 struct libusb_device_descriptor des;
173 int err, skip, i;
174
6f5f21f9 175 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
a1bb33af
UH
176 return NULL;
177
178 libusb_get_device_list(usb_context, &devlist);
6f5f21f9
UH
179 if (sdi->status == ST_INITIALIZING) {
180 /*
181 * This device was renumerating last time we touched it.
182 * opendev() guarantees we've waited long enough for it to
183 * have booted properly, so now we need to find it on
a1bb33af
UH
184 * the bus and record its new address.
185 */
186 skip = 0;
6f5f21f9 187 for (i = 0; devlist[i]; i++) {
90429916
UH
188 /* TODO: Error handling. */
189 err = opendev2(device_index, &sdi, devlist[i], &des,
5e59f476
UH
190 &skip, USB_VENDOR, USB_PRODUCT,
191 USB_INTERFACE);
a1bb33af 192 }
6f5f21f9
UH
193 } else if (sdi->status == ST_INACTIVE) {
194 /*
195 * This device is fully enumerated, so we need to find this
196 * device by vendor, product, bus and address.
197 */
a1bb33af 198 libusb_get_device_list(usb_context, &devlist);
6f5f21f9 199 for (i = 0; devlist[i]; i++) {
90429916 200 /* TODO: Error handling. */
5e59f476
UH
201 err = opendev3(&sdi, devlist[i], &des, USB_VENDOR,
202 USB_PRODUCT, USB_INTERFACE);
a1bb33af 203 }
6f5f21f9
UH
204 } else {
205 /* Status must be ST_ACTIVE, i.e. already in use... */
a1bb33af
UH
206 sdi = NULL;
207 }
208 libusb_free_device_list(devlist, 1);
209
6f5f21f9 210 if (sdi && sdi->status != ST_ACTIVE)
a1bb33af
UH
211 sdi = NULL;
212
213 return sdi;
214}
215
a1bb33af
UH
216int upload_firmware(libusb_device *dev)
217{
edf60d05 218 int ret;
a1bb33af 219
08cfe6a2
UH
220 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
221 if (ret != 0)
a1bb33af
UH
222 return 1;
223
6f5f21f9 224 /* Remember when the last firmware update was done. */
a1bb33af
UH
225 g_get_current_time(&firmware_updated);
226
227 return 0;
228}
229
a1bb33af
UH
230static void close_device(struct sigrok_device_instance *sdi)
231{
f6958dab
UH
232 if (sdi->usb->devhdl == NULL)
233 return;
234
e6ac9ac8 235 g_message("saleae: closing device %d on %d.%d interface %d", sdi->index,
f6958dab
UH
236 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
237 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
238 libusb_close(sdi->usb->devhdl);
239 sdi->usb->devhdl = NULL;
240 sdi->status = ST_INACTIVE;
a1bb33af
UH
241}
242
fed16f06 243static int configure_probes(GSList *probes)
a1bb33af
UH
244{
245 struct probe *probe;
246 GSList *l;
247 int probe_bit, stage, i;
248 char *tc;
249
250 probe_mask = 0;
6f5f21f9 251 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
a1bb33af
UH
252 trigger_mask[i] = 0;
253 trigger_value[i] = 0;
254 }
255
256 stage = -1;
6f5f21f9
UH
257 for (l = probes; l; l = l->next) {
258 probe = (struct probe *)l->data;
259 if (probe->enabled == FALSE)
a1bb33af
UH
260 continue;
261 probe_bit = 1 << (probe->index - 1);
262 probe_mask |= probe_bit;
989938f6
UH
263 if (!(probe->trigger))
264 continue;
265
266 stage = 0;
267 for (tc = probe->trigger; *tc; tc++) {
268 trigger_mask[stage] |= probe_bit;
269 if (*tc == '1')
270 trigger_value[stage] |= probe_bit;
271 stage++;
272 if (stage > NUM_TRIGGER_STAGES)
273 return SIGROK_ERR;
a1bb33af
UH
274 }
275 }
276
6f5f21f9
UH
277 if (stage == -1)
278 /*
279 * We didn't configure any triggers, make sure acquisition
280 * doesn't wait for any.
281 */
a1bb33af
UH
282 trigger_stage = TRIGGER_FIRED;
283 else
284 trigger_stage = 0;
285
286 return SIGROK_OK;
287}
288
a1bb33af
UH
289/*
290 * API callbacks
291 */
292
293static int hw_init(char *deviceinfo)
294{
295 struct sigrok_device_instance *sdi;
296 struct libusb_device_descriptor des;
297 libusb_device **devlist;
298 int err, devcnt, i;
299
afc8e4de
UH
300 /* QUICK HACK */
301 deviceinfo = deviceinfo;
302
6f5f21f9 303 if (libusb_init(&usb_context) != 0) {
a1bb33af
UH
304 g_warning("Failed to initialize USB.");
305 return 0;
306 }
a1bb33af 307
6f5f21f9 308 /* Find all Saleae Logic devices and upload firmware to all of them. */
a1bb33af
UH
309 devcnt = 0;
310 libusb_get_device_list(usb_context, &devlist);
6f5f21f9 311 for (i = 0; devlist[i]; i++) {
a1bb33af 312 err = libusb_get_device_descriptor(devlist[i], &des);
6f5f21f9 313 if (err != 0) {
a1bb33af
UH
314 g_warning("failed to get device descriptor: %d", err);
315 continue;
316 }
317
f6958dab
UH
318 if (des.idVendor != USB_VENDOR || des.idProduct != USB_PRODUCT)
319 continue; /* Not a Saleae Logic... */
a1bb33af 320
f6958dab
UH
321 sdi = sigrok_device_instance_new(devcnt, ST_INITIALIZING,
322 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
323 if (!sdi)
324 return 0;
325 device_instances = g_slist_append(device_instances, sdi);
6f5f21f9 326
f6958dab
UH
327 if (check_conf_profile(devlist[i]) == 0) {
328 /*
329 * Continue on the off chance that the device is in a
330 * working state. TODO: Could maybe try a USB reset,
331 * or uploading the firmware again.
332 */
333 if (upload_firmware(devlist[i]) > 0)
fed16f06
UH
334 g_warning("firmware upload failed for "
335 "device %d", devcnt);
a1bb33af 336
f6958dab 337 sdi->usb = usb_device_instance_new
fed16f06 338 (libusb_get_bus_number(devlist[i]), 0, NULL);
f6958dab
UH
339 } else {
340 /* Already has the firmware, so fix the new address. */
341 sdi->usb = usb_device_instance_new
342 (libusb_get_bus_number(devlist[i]),
fed16f06 343 libusb_get_device_address(devlist[i]), NULL);
a1bb33af 344 }
f6958dab 345 devcnt++;
a1bb33af
UH
346 }
347 libusb_free_device_list(devlist, 1);
348
349 return devcnt;
350}
351
a1bb33af
UH
352static int hw_opendev(int device_index)
353{
354 GTimeVal cur_time;
355 struct sigrok_device_instance *sdi;
356 int timediff, err;
357 unsigned int cur, upd;
358
6f5f21f9
UH
359 if (firmware_updated.tv_sec > 0) {
360 /* Firmware was recently uploaded. */
a1bb33af
UH
361 g_get_current_time(&cur_time);
362 cur = cur_time.tv_sec * 1000 + cur_time.tv_usec / 1000;
6f5f21f9
UH
363 upd = firmware_updated.tv_sec * 1000 +
364 firmware_updated.tv_usec / 1000;
a1bb33af 365 timediff = cur - upd;
6f5f21f9 366 if (timediff < FIRMWARE_RENUM_DELAY) {
a1bb33af 367 timediff = FIRMWARE_RENUM_DELAY - timediff;
e6ac9ac8 368 g_message("saleae: waiting %d ms for device to reset",
6f5f21f9 369 timediff);
a1bb33af
UH
370 g_usleep(timediff * 1000);
371 firmware_updated.tv_sec = 0;
372 }
373 }
374
6f5f21f9 375 if (!(sdi = sl_open_device(device_index))) {
a1bb33af 376 g_warning("unable to open device");
e31b636d 377 return SIGROK_ERR;
a1bb33af
UH
378 }
379
380 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
6f5f21f9 381 if (err != 0) {
a1bb33af 382 g_warning("Unable to claim interface: %d", err);
e31b636d 383 return SIGROK_ERR;
a1bb33af
UH
384 }
385
6f5f21f9
UH
386 if (cur_samplerate == 0) {
387 /* Samplerate hasn't been set; default to the slowest one. */
388 if (hw_set_configuration(device_index, HWCAP_SAMPLERATE,
fed16f06 389 &supported_samplerates[0]) == SIGROK_ERR)
e31b636d 390 return SIGROK_ERR;
a1bb33af
UH
391 }
392
393 return SIGROK_OK;
394}
395
a1bb33af
UH
396static void hw_closedev(int device_index)
397{
398 struct sigrok_device_instance *sdi;
399
6f5f21f9 400 if ((sdi = get_sigrok_device_instance(device_instances, device_index)))
a1bb33af 401 close_device(sdi);
a1bb33af
UH
402}
403
a1bb33af
UH
404static void hw_cleanup(void)
405{
406 GSList *l;
407
6f5f21f9
UH
408 /* Properly close all devices... */
409 for (l = device_instances; l; l = l->next)
410 close_device((struct sigrok_device_instance *)l->data);
a1bb33af 411
6f5f21f9
UH
412 /* ...and free all their memory. */
413 for (l = device_instances; l; l = l->next)
a1bb33af
UH
414 g_free(l->data);
415 g_slist_free(device_instances);
416 device_instances = NULL;
417
6f5f21f9 418 if (usb_context)
a1bb33af
UH
419 libusb_exit(usb_context);
420 usb_context = NULL;
a1bb33af
UH
421}
422
a1bb33af
UH
423static void *hw_get_device_info(int device_index, int device_info_id)
424{
425 struct sigrok_device_instance *sdi;
6f5f21f9 426 void *info = NULL;
a1bb33af 427
6f5f21f9 428 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
a1bb33af
UH
429 return NULL;
430
6f5f21f9 431 switch (device_info_id) {
a1bb33af
UH
432 case DI_INSTANCE:
433 info = sdi;
434 break;
435 case DI_NUM_PROBES:
436 info = GINT_TO_POINTER(NUM_PROBES);
437 break;
438 case DI_SAMPLERATES:
439 info = &samplerates;
440 break;
441 case DI_TRIGGER_TYPES:
442 info = TRIGGER_TYPES;
443 break;
4c100f32
UH
444 case DI_CUR_SAMPLERATE:
445 info = &cur_samplerate;
a1bb33af
UH
446 break;
447 }
448
449 return info;
450}
451
a1bb33af
UH
452static int hw_get_status(int device_index)
453{
454 struct sigrok_device_instance *sdi;
455
456 sdi = get_sigrok_device_instance(device_instances, device_index);
6f5f21f9 457 if (sdi)
a1bb33af
UH
458 return sdi->status;
459 else
460 return ST_NOT_FOUND;
461}
462
a1bb33af
UH
463static int *hw_get_capabilities(void)
464{
a1bb33af
UH
465 return capabilities;
466}
467
6f5f21f9
UH
468static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
469 uint64_t samplerate)
a1bb33af
UH
470{
471 uint8_t divider;
472 int ret, result, i;
473 unsigned char buf[2];
474
6f5f21f9
UH
475 for (i = 0; supported_samplerates[i]; i++) {
476 if (supported_samplerates[i] == samplerate)
a1bb33af
UH
477 break;
478 }
6f5f21f9 479 if (supported_samplerates[i] == 0)
e31b636d 480 return SIGROK_ERR_SAMPLERATE;
a1bb33af 481
eee4890f 482 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
a1bb33af 483
e6ac9ac8 484 g_message("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
6f5f21f9 485 samplerate, divider);
a1bb33af
UH
486 buf[0] = 0x01;
487 buf[1] = divider;
6f5f21f9
UH
488 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
489 buf, 2, &result, 500);
490 if (ret != 0) {
a1bb33af 491 g_warning("failed to set samplerate: %d", ret);
e31b636d 492 return SIGROK_ERR;
a1bb33af 493 }
4c100f32 494 cur_samplerate = samplerate;
a1bb33af
UH
495
496 return SIGROK_OK;
497}
498
a1bb33af
UH
499static int hw_set_configuration(int device_index, int capability, void *value)
500{
501 struct sigrok_device_instance *sdi;
502 int ret;
503 uint64_t *tmp_u64;
504
6f5f21f9 505 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 506 return SIGROK_ERR;
a1bb33af 507
6f5f21f9 508 if (capability == HWCAP_SAMPLERATE) {
a1bb33af
UH
509 tmp_u64 = value;
510 ret = set_configuration_samplerate(sdi, *tmp_u64);
6f5f21f9
UH
511 } else if (capability == HWCAP_PROBECONFIG) {
512 ret = configure_probes((GSList *) value);
513 } else if (capability == HWCAP_LIMIT_SAMPLES) {
2458ea65
BV
514 tmp_u64 = value;
515 limit_samples = *tmp_u64;
a1bb33af 516 ret = SIGROK_OK;
6f5f21f9 517 } else {
e31b636d 518 ret = SIGROK_ERR;
6f5f21f9 519 }
a1bb33af
UH
520
521 return ret;
522}
523
a1bb33af
UH
524static int receive_data(int fd, int revents, void *user_data)
525{
526 struct timeval tv;
527
afc8e4de
UH
528 /* QUICK HACK */
529 fd = fd;
530 revents = revents;
531 user_data = user_data;
532
a1bb33af
UH
533 tv.tv_sec = tv.tv_usec = 0;
534 libusb_handle_events_timeout(usb_context, &tv);
535
536 return TRUE;
537}
538
a1bb33af
UH
539void receive_transfer(struct libusb_transfer *transfer)
540{
541 static int num_samples = 0;
542 static int empty_transfer_count = 0;
a1bb33af
UH
543 struct datafeed_packet packet;
544 void *user_data;
545 int cur_buflen, trigger_offset, i;
546 unsigned char *cur_buf, *new_buf;
547
f6958dab
UH
548 /* hw_stop_acquisition() is telling us to stop. */
549 if (transfer == NULL)
a1bb33af 550 num_samples = -1;
a1bb33af 551
f6958dab
UH
552 /*
553 * If acquisition has already ended, just free any queued up
554 * transfer that come in.
555 */
6f5f21f9 556 if (num_samples == -1) {
9c48090a
BV
557 if (transfer)
558 libusb_free_transfer(transfer);
f6958dab
UH
559 return;
560 }
a1bb33af 561
e6ac9ac8 562 g_message("saleae: receive_transfer(): status %d received %d bytes",
f6958dab
UH
563 transfer->status, transfer->actual_length);
564
565 /* Save incoming transfer before reusing the transfer struct. */
566 cur_buf = transfer->buffer;
567 cur_buflen = transfer->actual_length;
568 user_data = transfer->user_data;
569
570 /* Fire off a new request. */
571 new_buf = g_malloc(4096);
572 transfer->buffer = new_buf;
573 transfer->length = 4096;
574 if (libusb_submit_transfer(transfer) != 0) {
575 /* TODO: Stop session? */
576 g_warning("eek");
577 }
578
579 if (cur_buflen == 0) {
580 empty_transfer_count++;
581 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
582 /*
583 * The FX2 gave up. End the acquisition, the frontend
584 * will work out that the samplecount is short.
585 */
9c48090a 586 hw_stop_acquisition(-1, user_data);
6f5f21f9 587 }
f6958dab
UH
588 return;
589 } else {
590 empty_transfer_count = 0;
591 }
a1bb33af 592
f6958dab
UH
593 trigger_offset = 0;
594 if (trigger_stage >= 0) {
595 for (i = 0; i < cur_buflen; i++) {
b5698bd7
BV
596
597 if ((cur_buf[i] & trigger_mask[trigger_stage]) == trigger_value[trigger_stage]) {
598 /* Match on this trigger stage. */
599 trigger_buffer[trigger_stage] = cur_buf[i];
600 trigger_stage++;
601 if (trigger_stage == NUM_TRIGGER_STAGES || trigger_mask[trigger_stage] == 0) {
602 /* Match on all trigger stages, we're done. */
603 trigger_offset = i + 1;
604
605 /*
606 * TODO: Send pre-trigger buffer to session bus.
607 * Tell the frontend we hit the trigger here.
608 */
609 packet.type = DF_TRIGGER;
610 packet.length = 0;
611 session_bus(user_data, &packet);
612
613 /*
614 * Send the samples that triggered it, since we're
615 * skipping past them.
616 */
4c046c6b 617 packet.type = DF_LOGIC;
b5698bd7 618 packet.length = trigger_stage;
4c046c6b 619 packet.unitsize = 1;
b5698bd7
BV
620 packet.payload = trigger_buffer;
621 session_bus(user_data, &packet);
622
623 trigger_stage = TRIGGER_FIRED;
624 break;
625 }
626 return;
627 }
628
629 /*
630 * We had a match before, but not in the next sample. However, we may
631 * have a match on this stage in the next bit -- trigger on 0001 will
632 * fail on seeing 00001, so we need to go back to stage 0 -- but at
633 * the next sample from the one that matched originally, which the
634 * counter increment at the end of the loop takes care of.
635 */
636 if (trigger_stage > 0) {
637 i -= trigger_stage;
638 if (i < -1)
639 i = -1; /* Oops, went back past this buffer. */
640 /* Reset trigger stage. */
641 trigger_stage = 0;
642 }
643
644
a1bb33af 645 }
f6958dab 646 }
a1bb33af 647
f6958dab
UH
648 if (trigger_stage == TRIGGER_FIRED) {
649 /* Send the incoming transfer to the session bus. */
4c046c6b 650 packet.type = DF_LOGIC;
f6958dab 651 packet.length = cur_buflen - trigger_offset;
4c046c6b 652 packet.unitsize = 1;
f6958dab
UH
653 packet.payload = cur_buf + trigger_offset;
654 session_bus(user_data, &packet);
655 g_free(cur_buf);
656
657 num_samples += cur_buflen;
74b9b438 658 if (limit_samples && (unsigned int) num_samples > limit_samples) {
9c48090a 659 hw_stop_acquisition(-1, user_data);
a1bb33af 660 }
f6958dab
UH
661 } else {
662 /*
663 * TODO: Buffer pre-trigger data in capture
664 * ratio-sized buffer.
665 */
a1bb33af 666 }
a1bb33af
UH
667}
668
a1bb33af
UH
669static int hw_start_acquisition(int device_index, gpointer session_device_id)
670{
671 struct sigrok_device_instance *sdi;
672 struct datafeed_packet *packet;
673 struct datafeed_header *header;
674 struct libusb_transfer *transfer;
675 const struct libusb_pollfd **lupfd;
676 int size, i;
677 unsigned char *buf;
678
6f5f21f9 679 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 680 return SIGROK_ERR;
a1bb33af
UH
681
682 packet = g_malloc(sizeof(struct datafeed_packet));
683 header = g_malloc(sizeof(struct datafeed_header));
6f5f21f9 684 if (!packet || !header)
e31b636d 685 return SIGROK_ERR;
a1bb33af 686
6f5f21f9 687 /* Start with 2K transfer, subsequently increased to 4K. */
a1bb33af 688 size = 2048;
6f5f21f9 689 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
a1bb33af
UH
690 buf = g_malloc(size);
691 transfer = libusb_alloc_transfer(0);
6f5f21f9
UH
692 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
693 2 | LIBUSB_ENDPOINT_IN, buf, size,
a1bb33af 694 receive_transfer, session_device_id, 40);
6f5f21f9
UH
695 if (libusb_submit_transfer(transfer) != 0) {
696 /* TODO: Free them all. */
a1bb33af
UH
697 libusb_free_transfer(transfer);
698 g_free(buf);
e31b636d 699 return SIGROK_ERR;
a1bb33af
UH
700 }
701 size = 4096;
702 }
703
704 lupfd = libusb_get_pollfds(usb_context);
6f5f21f9 705 for (i = 0; lupfd[i]; i++)
9c48090a 706 source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
6f5f21f9 707 NULL);
a1bb33af
UH
708 free(lupfd);
709
710 packet->type = DF_HEADER;
711 packet->length = sizeof(struct datafeed_header);
6f5f21f9 712 packet->payload = (unsigned char *)header;
a1bb33af
UH
713 header->feed_version = 1;
714 gettimeofday(&header->starttime, NULL);
4c100f32 715 header->samplerate = cur_samplerate;
a1bb33af 716 header->protocol_id = PROTO_RAW;
c2616fb9
DR
717 header->num_logic_probes = NUM_PROBES;
718 header->num_analog_probes = 0;
a1bb33af
UH
719 session_bus(session_device_id, packet);
720 g_free(header);
721 g_free(packet);
722
723 return SIGROK_OK;
724}
725
6f5f21f9 726/* This stops acquisition on ALL devices, ignoring device_index. */
a1bb33af
UH
727static void hw_stop_acquisition(int device_index, gpointer session_device_id)
728{
729 struct datafeed_packet packet;
730
afc8e4de
UH
731 /* QUICK HACK */
732 device_index = device_index;
733
a1bb33af
UH
734 packet.type = DF_END;
735 session_bus(session_device_id, &packet);
736
737 receive_transfer(NULL);
738
6f5f21f9 739 /* TODO: Need to cancel and free any queued up transfers. */
a1bb33af
UH
740}
741
a1bb33af
UH
742struct device_plugin saleae_logic_plugin_info = {
743 "saleae-logic",
744 1,
745 hw_init,
746 hw_cleanup,
747
748 hw_opendev,
749 hw_closedev,
750 hw_get_device_info,
751 hw_get_status,
752 hw_get_capabilities,
753 hw_set_configuration,
754 hw_start_acquisition,
6f5f21f9 755 hw_stop_acquisition,
a1bb33af 756};