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