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