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