]> sigrok.org Git - libsigrok.git/blame - hardware/saleae-logic/saleae-logic.c
saleae-logic: use new logging system
[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
22b02383 20#include "config.h"
a1bb33af
UH
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/time.h>
24#include <inttypes.h>
25#include <glib.h>
26#include <libusb.h>
408e7199 27#include <sigrok.h>
058b7035 28#include <sigrok-internal.h>
a1bb33af 29
74b9b438
BV
30#define USB_INTERFACE 0
31#define USB_CONFIGURATION 1
74b9b438
BV
32#define NUM_TRIGGER_STAGES 4
33#define TRIGGER_TYPES "01"
34#define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw"
e10d6e32 35#define GTV_TO_MSEC(gtv) (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
a1bb33af
UH
36
37/* delay in ms */
e10d6e32 38#define MAX_RENUM_DELAY 3000
74b9b438
BV
39#define NUM_SIMUL_TRANSFERS 10
40#define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2)
a1bb33af 41
fed16f06 42/* Software trigger implementation: positive values indicate trigger stage. */
74b9b438 43#define TRIGGER_FIRED -1
a1bb33af 44
e10d6e32
BV
45struct fx2_device {
46 /* VID/PID when first found */
47 uint16_t orig_vid;
48 uint16_t orig_pid;
49 /* VID/PID after firmware upload */
50 uint16_t fw_vid;
51 uint16_t fw_pid;
52 char *vendor;
53 char *model;
54 char *model_version;
55 int num_probes;
56};
57
58static struct fx2_device supported_fx2[] = {
59 /* Saleae Logic */
60 { 0x0925, 0x3881, 0x0925, 0x3881, "Saleae", "Logic", NULL, 8 },
61 /* default Cypress FX2 without EEPROM */
62 { 0x04b4, 0x8613, 0x0925, 0x3881, "Cypress", "FX2", NULL, 16 },
63 { 0, 0, 0, 0, NULL, NULL, NULL, 0 }
64};
65
6f5f21f9 66/* There is only one model Saleae Logic, and this is what it supports: */
a1bb33af 67static int capabilities[] = {
5a2326a7
UH
68 SR_HWCAP_LOGIC_ANALYZER,
69 SR_HWCAP_SAMPLERATE,
a1bb33af 70
6f5f21f9 71 /* These are really implemented in the driver, not the hardware. */
5a2326a7
UH
72 SR_HWCAP_LIMIT_SAMPLES,
73 SR_HWCAP_CONTINUOUS,
6f5f21f9 74 0,
a1bb33af
UH
75};
76
a00ba012 77/* List of struct sr_device_instance, maintained by opendev()/closedev(). */
a1bb33af
UH
78static GSList *device_instances = NULL;
79
6f5f21f9
UH
80/*
81 * Since we can't keep track of a Saleae Logic device after upgrading the
a1bb33af
UH
82 * firmware -- it re-enumerates into a different device address after the
83 * upgrade -- this is like a global lock. No device will open until a proper
84 * delay after the last device was upgraded.
85 */
e10d6e32 86static GTimeVal fw_updated = { 0, 0 };
a1bb33af
UH
87
88static libusb_context *usb_context = NULL;
89
90static uint64_t supported_samplerates[] = {
59df0c77
UH
91 SR_KHZ(200),
92 SR_KHZ(250),
93 SR_KHZ(500),
94 SR_MHZ(1),
95 SR_MHZ(2),
96 SR_MHZ(4),
97 SR_MHZ(8),
98 SR_MHZ(12),
99 SR_MHZ(16),
100 SR_MHZ(24),
6f5f21f9 101 0,
a1bb33af
UH
102};
103
60679b18 104static struct sr_samplerates samplerates = {
59df0c77
UH
105 SR_KHZ(200),
106 SR_MHZ(24),
c9140419 107 SR_HZ(0),
6f5f21f9 108 supported_samplerates,
a1bb33af
UH
109};
110
6f5f21f9 111/* TODO: All of these should go in a device-specific struct. */
4c100f32 112static uint64_t cur_samplerate = 0;
a1bb33af 113static uint64_t limit_samples = 0;
6f5f21f9
UH
114static uint8_t probe_mask = 0;
115static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
116static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
117static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
e10d6e32 118static struct fx2_device *fx2 = NULL;
e5d1717e 119static int trigger_stage = TRIGGER_FIRED;
a1bb33af 120
a1bb33af 121static int hw_set_configuration(int device_index, int capability, void *value);
9c48090a 122static void hw_stop_acquisition(int device_index, gpointer session_device_id);
a1bb33af 123
28fc6de0
UH
124/**
125 * Check the USB configuration to determine if this is a Saleae Logic.
126 *
127 * @return 1 if the device's configuration profile match the Logic firmware's
128 * configuration, 0 otherwise.
a1bb33af 129 */
e5d1717e 130static int check_conf_profile(libusb_device *dev)
a1bb33af
UH
131{
132 struct libusb_device_descriptor des;
6f5f21f9 133 struct libusb_config_descriptor *conf_dsc = NULL;
a1bb33af 134 const struct libusb_interface_descriptor *intf_dsc;
6f5f21f9 135 int ret = -1;
a1bb33af 136
6f5f21f9
UH
137 while (ret == -1) {
138 /* Assume it's not a Saleae Logic unless proven wrong. */
a1bb33af
UH
139 ret = 0;
140
6f5f21f9 141 if (libusb_get_device_descriptor(dev, &des) != 0)
a1bb33af
UH
142 break;
143
6f5f21f9
UH
144 if (des.bNumConfigurations != 1)
145 /* Need exactly 1 configuration. */
a1bb33af
UH
146 break;
147
6f5f21f9 148 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
a1bb33af
UH
149 break;
150
6f5f21f9
UH
151 if (conf_dsc->bNumInterfaces != 1)
152 /* Need exactly 1 interface. */
a1bb33af
UH
153 break;
154
6f5f21f9
UH
155 if (conf_dsc->interface[0].num_altsetting != 1)
156 /* Need just one alternate setting. */
a1bb33af
UH
157 break;
158
159 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
6f5f21f9
UH
160 if (intf_dsc->bNumEndpoints != 2)
161 /* Need 2 endpoints. */
a1bb33af
UH
162 break;
163
6f5f21f9
UH
164 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
165 (1 | LIBUSB_ENDPOINT_OUT))
166 /* First endpoint should be 1 (outbound). */
a1bb33af
UH
167 break;
168
6f5f21f9
UH
169 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
170 (2 | LIBUSB_ENDPOINT_IN))
171 /* First endpoint should be 2 (inbound). */
a1bb33af
UH
172 break;
173
6f5f21f9 174 /* If we made it here, it must be a Saleae Logic. */
a1bb33af
UH
175 ret = 1;
176 }
6f5f21f9
UH
177
178 if (conf_dsc)
a1bb33af
UH
179 libusb_free_config_descriptor(conf_dsc);
180
181 return ret;
182}
183
e5d1717e 184static struct sr_device_instance *sl_open_device(int device_index)
a1bb33af 185{
a1bb33af
UH
186 libusb_device **devlist;
187 struct libusb_device_descriptor des;
e10d6e32 188 struct sr_device_instance *sdi;
a1bb33af
UH
189 int err, skip, i;
190
d32d961d 191 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af
UH
192 return NULL;
193
e10d6e32
BV
194 if (sdi->status == SR_ST_ACTIVE)
195 /* already in use */
196 return NULL;
197
198 skip = 0;
a1bb33af 199 libusb_get_device_list(usb_context, &devlist);
e10d6e32
BV
200 for (i = 0; devlist[i]; i++) {
201 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
e53c830f 202 sr_warn("failed to get device descriptor: %d", err);
e10d6e32 203 continue;
a1bb33af 204 }
e10d6e32
BV
205
206 if (des.idVendor != fx2->fw_vid || des.idProduct != fx2->fw_pid)
207 continue;
208
209 if (sdi->status == SR_ST_INITIALIZING) {
210 if (skip != device_index) {
211 /* Skip devices of this type that aren't the one we want. */
212 skip += 1;
213 continue;
214 }
215 } else if (sdi->status == SR_ST_INACTIVE) {
216 /*
217 * This device is fully enumerated, so we need to find this
218 * device by vendor, product, bus and address.
219 */
220 if (libusb_get_bus_number(devlist[i]) != sdi->usb->bus
221 || libusb_get_device_address(devlist[i]) != sdi->usb->address)
222 /* this is not the one */
223 continue;
224 }
225
226 if (!(err = libusb_open(devlist[i], &sdi->usb->devhdl))) {
227 if (sdi->usb->address == 0xff)
228 /*
229 * first time we touch this device after firmware upload,
230 * so we don't know the address yet.
231 */
232 sdi->usb->address = libusb_get_device_address(devlist[i]);
233
234 sdi->status = SR_ST_ACTIVE;
e53c830f 235 sr_info("saleae: opened device %d on %d.%d interface %d",
e10d6e32
BV
236 sdi->index, sdi->usb->bus,
237 sdi->usb->address, USB_INTERFACE);
238 } else {
e53c830f 239 sr_warn("failed to open device: %d", err);
e10d6e32 240 sdi = NULL;
a1bb33af 241 }
a1bb33af
UH
242 }
243 libusb_free_device_list(devlist, 1);
244
5a2326a7 245 if (sdi && sdi->status != SR_ST_ACTIVE)
a1bb33af
UH
246 sdi = NULL;
247
248 return sdi;
249}
250
e5d1717e 251static int upload_firmware(libusb_device *dev)
a1bb33af 252{
edf60d05 253 int ret;
a1bb33af 254
08cfe6a2
UH
255 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
256 if (ret != 0)
a1bb33af
UH
257 return 1;
258
6f5f21f9 259 /* Remember when the last firmware update was done. */
e10d6e32 260 g_get_current_time(&fw_updated);
a1bb33af
UH
261
262 return 0;
263}
264
a00ba012 265static void close_device(struct sr_device_instance *sdi)
a1bb33af 266{
f6958dab
UH
267 if (sdi->usb->devhdl == NULL)
268 return;
269
b08024a8
UH
270 sr_info("saleae: closing device %d on %d.%d interface %d", sdi->index,
271 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
f6958dab
UH
272 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
273 libusb_close(sdi->usb->devhdl);
274 sdi->usb->devhdl = NULL;
5a2326a7 275 sdi->status = SR_ST_INACTIVE;
a1bb33af
UH
276}
277
fed16f06 278static int configure_probes(GSList *probes)
a1bb33af 279{
1afe8989 280 struct sr_probe *probe;
a1bb33af
UH
281 GSList *l;
282 int probe_bit, stage, i;
283 char *tc;
284
285 probe_mask = 0;
6f5f21f9 286 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
a1bb33af
UH
287 trigger_mask[i] = 0;
288 trigger_value[i] = 0;
289 }
290
291 stage = -1;
6f5f21f9 292 for (l = probes; l; l = l->next) {
1afe8989 293 probe = (struct sr_probe *)l->data;
6f5f21f9 294 if (probe->enabled == FALSE)
a1bb33af
UH
295 continue;
296 probe_bit = 1 << (probe->index - 1);
297 probe_mask |= probe_bit;
989938f6
UH
298 if (!(probe->trigger))
299 continue;
300
301 stage = 0;
302 for (tc = probe->trigger; *tc; tc++) {
303 trigger_mask[stage] |= probe_bit;
304 if (*tc == '1')
305 trigger_value[stage] |= probe_bit;
306 stage++;
307 if (stage > NUM_TRIGGER_STAGES)
e46b8fb1 308 return SR_ERR;
a1bb33af
UH
309 }
310 }
311
6f5f21f9
UH
312 if (stage == -1)
313 /*
314 * We didn't configure any triggers, make sure acquisition
315 * doesn't wait for any.
316 */
a1bb33af
UH
317 trigger_stage = TRIGGER_FIRED;
318 else
319 trigger_stage = 0;
320
e46b8fb1 321 return SR_OK;
a1bb33af
UH
322}
323
a1bb33af
UH
324/*
325 * API callbacks
326 */
327
54ac5277 328static int hw_init(const char *deviceinfo)
a1bb33af 329{
a00ba012 330 struct sr_device_instance *sdi;
a1bb33af
UH
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
e10d6e32 333 int err, devcnt, i, j;
a1bb33af 334
17e1afcb 335 /* Avoid compiler warnings. */
afc8e4de
UH
336 deviceinfo = deviceinfo;
337
6f5f21f9 338 if (libusb_init(&usb_context) != 0) {
b08024a8 339 sr_warn("Failed to initialize USB.");
a1bb33af
UH
340 return 0;
341 }
a1bb33af 342
6f5f21f9 343 /* Find all Saleae Logic devices and upload firmware to all of them. */
e10d6e32 344 fx2 = NULL;
a1bb33af
UH
345 devcnt = 0;
346 libusb_get_device_list(usb_context, &devlist);
6f5f21f9 347 for (i = 0; devlist[i]; i++) {
a1bb33af 348 err = libusb_get_device_descriptor(devlist[i], &des);
6f5f21f9 349 if (err != 0) {
b08024a8 350 sr_warn("failed to get device descriptor: %d", err);
a1bb33af
UH
351 continue;
352 }
353
e10d6e32
BV
354 for (j = 0; supported_fx2[j].orig_vid; j++) {
355 if (des.idVendor == supported_fx2[j].orig_vid
356 && des.idProduct == supported_fx2[j].orig_pid) {
357 fx2 = &supported_fx2[j];
358 break;
359 }
360 }
361 if (!fx2)
362 /* not a supported VID/PID */
363 continue;
a1bb33af 364
5a2326a7 365 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
e10d6e32 366 fx2->vendor, fx2->model, fx2->model_version);
f6958dab
UH
367 if (!sdi)
368 return 0;
369 device_instances = g_slist_append(device_instances, sdi);
6f5f21f9 370
f6958dab
UH
371 if (check_conf_profile(devlist[i]) == 0) {
372 /*
373 * Continue on the off chance that the device is in a
374 * working state. TODO: Could maybe try a USB reset,
375 * or uploading the firmware again.
376 */
377 if (upload_firmware(devlist[i]) > 0)
b08024a8
UH
378 sr_warn("firmware upload failed for device %d",
379 devcnt);
a1bb33af 380
6c290072 381 sdi->usb = sr_usb_device_instance_new
e10d6e32 382 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
f6958dab
UH
383 } else {
384 /* Already has the firmware, so fix the new address. */
e10d6e32 385 sdi->status = SR_ST_INACTIVE;
6c290072 386 sdi->usb = sr_usb_device_instance_new
f6958dab 387 (libusb_get_bus_number(devlist[i]),
fed16f06 388 libusb_get_device_address(devlist[i]), NULL);
a1bb33af 389 }
f6958dab 390 devcnt++;
e10d6e32
BV
391
392 /* not supporting multiple FX2s in this driver yet */
393 break;
a1bb33af
UH
394 }
395 libusb_free_device_list(devlist, 1);
396
397 return devcnt;
398}
399
a1bb33af
UH
400static int hw_opendev(int device_index)
401{
402 GTimeVal cur_time;
a00ba012 403 struct sr_device_instance *sdi;
a1bb33af 404 int timediff, err;
e10d6e32
BV
405
406 /*
407 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
408 * for the FX2 to renumerate
409 */
410 sdi = NULL;
411 if (fw_updated.tv_sec > 0) {
e53c830f 412 sr_info("saleae: waiting for device to reset");
e10d6e32
BV
413 /* takes at least 300ms for the FX2 to be gone from the USB bus */
414 g_usleep(300*1000);
415 timediff = 0;
416 while (timediff < MAX_RENUM_DELAY) {
417 if ((sdi = sl_open_device(device_index)))
418 break;
419 g_usleep(100*1000);
420 g_get_current_time(&cur_time);
421 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fw_updated);
a1bb33af 422 }
e53c830f 423 sr_info("saleae: device came back after %d ms", timediff);
e10d6e32
BV
424 } else {
425 sdi = sl_open_device(device_index);
a1bb33af
UH
426 }
427
e10d6e32 428 if (!sdi) {
b08024a8 429 sr_warn("unable to open device");
e46b8fb1 430 return SR_ERR;
a1bb33af
UH
431 }
432
433 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
6f5f21f9 434 if (err != 0) {
b08024a8 435 sr_warn("Unable to claim interface: %d", err);
e46b8fb1 436 return SR_ERR;
a1bb33af
UH
437 }
438
6f5f21f9
UH
439 if (cur_samplerate == 0) {
440 /* Samplerate hasn't been set; default to the slowest one. */
5a2326a7 441 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
e46b8fb1
UH
442 &supported_samplerates[0]) == SR_ERR)
443 return SR_ERR;
a1bb33af
UH
444 }
445
e46b8fb1 446 return SR_OK;
a1bb33af
UH
447}
448
697785d1 449static int hw_closedev(int device_index)
a1bb33af 450{
a00ba012 451 struct sr_device_instance *sdi;
a1bb33af 452
697785d1
UH
453 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
454 sr_err("logic: %s: sdi was NULL", __func__);
455 return SR_ERR; /* TODO: SR_ERR_ARG? */
456 }
457
458 /* TODO */
459 close_device(sdi);
460
461 return SR_OK;
a1bb33af
UH
462}
463
a1bb33af
UH
464static void hw_cleanup(void)
465{
466 GSList *l;
467
6f5f21f9
UH
468 /* Properly close all devices... */
469 for (l = device_instances; l; l = l->next)
a00ba012 470 close_device((struct sr_device_instance *)l->data);
a1bb33af 471
6f5f21f9
UH
472 /* ...and free all their memory. */
473 for (l = device_instances; l; l = l->next)
a1bb33af
UH
474 g_free(l->data);
475 g_slist_free(device_instances);
476 device_instances = NULL;
477
6f5f21f9 478 if (usb_context)
a1bb33af
UH
479 libusb_exit(usb_context);
480 usb_context = NULL;
a1bb33af
UH
481}
482
a1bb33af
UH
483static void *hw_get_device_info(int device_index, int device_info_id)
484{
a00ba012 485 struct sr_device_instance *sdi;
6f5f21f9 486 void *info = NULL;
a1bb33af 487
d32d961d 488 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af
UH
489 return NULL;
490
6f5f21f9 491 switch (device_info_id) {
5a2326a7 492 case SR_DI_INSTANCE:
a1bb33af
UH
493 info = sdi;
494 break;
5a2326a7 495 case SR_DI_NUM_PROBES:
e10d6e32 496 info = GINT_TO_POINTER(fx2->num_probes);
a1bb33af 497 break;
5a2326a7 498 case SR_DI_SAMPLERATES:
a1bb33af
UH
499 info = &samplerates;
500 break;
5a2326a7 501 case SR_DI_TRIGGER_TYPES:
a1bb33af
UH
502 info = TRIGGER_TYPES;
503 break;
5a2326a7 504 case SR_DI_CUR_SAMPLERATE:
4c100f32 505 info = &cur_samplerate;
a1bb33af
UH
506 break;
507 }
508
509 return info;
510}
511
a1bb33af
UH
512static int hw_get_status(int device_index)
513{
a00ba012 514 struct sr_device_instance *sdi;
a1bb33af 515
d32d961d 516 sdi = sr_get_device_instance(device_instances, device_index);
6f5f21f9 517 if (sdi)
a1bb33af
UH
518 return sdi->status;
519 else
5a2326a7 520 return SR_ST_NOT_FOUND;
a1bb33af
UH
521}
522
a1bb33af
UH
523static int *hw_get_capabilities(void)
524{
a1bb33af
UH
525 return capabilities;
526}
527
a00ba012 528static int set_configuration_samplerate(struct sr_device_instance *sdi,
6f5f21f9 529 uint64_t samplerate)
a1bb33af
UH
530{
531 uint8_t divider;
532 int ret, result, i;
533 unsigned char buf[2];
534
6f5f21f9
UH
535 for (i = 0; supported_samplerates[i]; i++) {
536 if (supported_samplerates[i] == samplerate)
a1bb33af
UH
537 break;
538 }
6f5f21f9 539 if (supported_samplerates[i] == 0)
e46b8fb1 540 return SR_ERR_SAMPLERATE;
a1bb33af 541
eee4890f 542 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
a1bb33af 543
b08024a8
UH
544 sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
545 samplerate, divider);
a1bb33af
UH
546 buf[0] = 0x01;
547 buf[1] = divider;
6f5f21f9
UH
548 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
549 buf, 2, &result, 500);
550 if (ret != 0) {
b08024a8 551 sr_warn("failed to set samplerate: %d", ret);
e46b8fb1 552 return SR_ERR;
a1bb33af 553 }
4c100f32 554 cur_samplerate = samplerate;
a1bb33af 555
e46b8fb1 556 return SR_OK;
a1bb33af
UH
557}
558
a1bb33af
UH
559static int hw_set_configuration(int device_index, int capability, void *value)
560{
a00ba012 561 struct sr_device_instance *sdi;
a1bb33af
UH
562 int ret;
563 uint64_t *tmp_u64;
564
d32d961d 565 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 566 return SR_ERR;
a1bb33af 567
5a2326a7 568 if (capability == SR_HWCAP_SAMPLERATE) {
a1bb33af
UH
569 tmp_u64 = value;
570 ret = set_configuration_samplerate(sdi, *tmp_u64);
5a2326a7 571 } else if (capability == SR_HWCAP_PROBECONFIG) {
6f5f21f9 572 ret = configure_probes((GSList *) value);
5a2326a7 573 } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
2458ea65
BV
574 tmp_u64 = value;
575 limit_samples = *tmp_u64;
e46b8fb1 576 ret = SR_OK;
6f5f21f9 577 } else {
e46b8fb1 578 ret = SR_ERR;
6f5f21f9 579 }
a1bb33af
UH
580
581 return ret;
582}
583
a1bb33af
UH
584static int receive_data(int fd, int revents, void *user_data)
585{
586 struct timeval tv;
587
17e1afcb 588 /* Avoid compiler warnings. */
afc8e4de
UH
589 fd = fd;
590 revents = revents;
591 user_data = user_data;
592
a1bb33af
UH
593 tv.tv_sec = tv.tv_usec = 0;
594 libusb_handle_events_timeout(usb_context, &tv);
595
596 return TRUE;
597}
598
a1bb33af
UH
599void receive_transfer(struct libusb_transfer *transfer)
600{
601 static int num_samples = 0;
602 static int empty_transfer_count = 0;
b9c735a2 603 struct sr_datafeed_packet packet;
a1bb33af
UH
604 void *user_data;
605 int cur_buflen, trigger_offset, i;
606 unsigned char *cur_buf, *new_buf;
607
f6958dab
UH
608 /* hw_stop_acquisition() is telling us to stop. */
609 if (transfer == NULL)
a1bb33af 610 num_samples = -1;
a1bb33af 611
f6958dab
UH
612 /*
613 * If acquisition has already ended, just free any queued up
614 * transfer that come in.
615 */
6f5f21f9 616 if (num_samples == -1) {
9c48090a
BV
617 if (transfer)
618 libusb_free_transfer(transfer);
f6958dab
UH
619 return;
620 }
a1bb33af 621
b08024a8
UH
622 sr_info("saleae: receive_transfer(): status %d received %d bytes",
623 transfer->status, transfer->actual_length);
f6958dab
UH
624
625 /* Save incoming transfer before reusing the transfer struct. */
626 cur_buf = transfer->buffer;
627 cur_buflen = transfer->actual_length;
628 user_data = transfer->user_data;
629
630 /* Fire off a new request. */
b53738ba
UH
631 if (!(new_buf = g_try_malloc(4096))) {
632 sr_err("saleae: %s: new_buf malloc failed", __func__);
633 // return SR_ERR_MALLOC;
634 return; /* FIXME */
635 }
636
f6958dab
UH
637 transfer->buffer = new_buf;
638 transfer->length = 4096;
639 if (libusb_submit_transfer(transfer) != 0) {
640 /* TODO: Stop session? */
b08024a8 641 sr_warn("eek");
f6958dab
UH
642 }
643
644 if (cur_buflen == 0) {
645 empty_transfer_count++;
646 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
647 /*
648 * The FX2 gave up. End the acquisition, the frontend
649 * will work out that the samplecount is short.
650 */
9c48090a 651 hw_stop_acquisition(-1, user_data);
6f5f21f9 652 }
f6958dab
UH
653 return;
654 } else {
655 empty_transfer_count = 0;
656 }
a1bb33af 657
f6958dab
UH
658 trigger_offset = 0;
659 if (trigger_stage >= 0) {
660 for (i = 0; i < cur_buflen; i++) {
b5698bd7
BV
661
662 if ((cur_buf[i] & trigger_mask[trigger_stage]) == trigger_value[trigger_stage]) {
663 /* Match on this trigger stage. */
664 trigger_buffer[trigger_stage] = cur_buf[i];
665 trigger_stage++;
666 if (trigger_stage == NUM_TRIGGER_STAGES || trigger_mask[trigger_stage] == 0) {
667 /* Match on all trigger stages, we're done. */
668 trigger_offset = i + 1;
669
670 /*
671 * TODO: Send pre-trigger buffer to session bus.
672 * Tell the frontend we hit the trigger here.
673 */
5a2326a7 674 packet.type = SR_DF_TRIGGER;
b5698bd7 675 packet.length = 0;
8a2efef2 676 sr_session_bus(user_data, &packet);
b5698bd7
BV
677
678 /*
679 * Send the samples that triggered it, since we're
680 * skipping past them.
681 */
5a2326a7 682 packet.type = SR_DF_LOGIC;
b5698bd7 683 packet.length = trigger_stage;
4c046c6b 684 packet.unitsize = 1;
b5698bd7 685 packet.payload = trigger_buffer;
8a2efef2 686 sr_session_bus(user_data, &packet);
b5698bd7
BV
687
688 trigger_stage = TRIGGER_FIRED;
689 break;
690 }
691 return;
692 }
693
694 /*
695 * We had a match before, but not in the next sample. However, we may
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 if (trigger_stage > 0) {
702 i -= trigger_stage;
703 if (i < -1)
704 i = -1; /* Oops, went back past this buffer. */
705 /* Reset trigger stage. */
706 trigger_stage = 0;
707 }
708
709
a1bb33af 710 }
f6958dab 711 }
a1bb33af 712
f6958dab
UH
713 if (trigger_stage == TRIGGER_FIRED) {
714 /* Send the incoming transfer to the session bus. */
5a2326a7 715 packet.type = SR_DF_LOGIC;
f6958dab 716 packet.length = cur_buflen - trigger_offset;
4c046c6b 717 packet.unitsize = 1;
f6958dab 718 packet.payload = cur_buf + trigger_offset;
8a2efef2 719 sr_session_bus(user_data, &packet);
f6958dab
UH
720 g_free(cur_buf);
721
722 num_samples += cur_buflen;
74b9b438 723 if (limit_samples && (unsigned int) num_samples > limit_samples) {
9c48090a 724 hw_stop_acquisition(-1, user_data);
a1bb33af 725 }
f6958dab
UH
726 } else {
727 /*
728 * TODO: Buffer pre-trigger data in capture
729 * ratio-sized buffer.
730 */
a1bb33af 731 }
a1bb33af
UH
732}
733
a1bb33af
UH
734static int hw_start_acquisition(int device_index, gpointer session_device_id)
735{
a00ba012 736 struct sr_device_instance *sdi;
b9c735a2
UH
737 struct sr_datafeed_packet *packet;
738 struct sr_datafeed_header *header;
a1bb33af
UH
739 struct libusb_transfer *transfer;
740 const struct libusb_pollfd **lupfd;
741 int size, i;
742 unsigned char *buf;
743
d32d961d 744 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 745 return SR_ERR;
a1bb33af 746
b53738ba
UH
747 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
748 sr_err("saleae: %s: packet malloc failed", __func__);
749 return SR_ERR_MALLOC;
750 }
751
752 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
753 sr_err("saleae: %s: header malloc failed", __func__);
754 return SR_ERR_MALLOC;
755 }
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++) {
b53738ba
UH
760 if (!(buf = g_try_malloc(size))) {
761 sr_err("saleae: %s: buf malloc failed", __func__);
762 return SR_ERR_MALLOC;
763 }
a1bb33af 764 transfer = libusb_alloc_transfer(0);
6f5f21f9
UH
765 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
766 2 | LIBUSB_ENDPOINT_IN, buf, size,
a1bb33af 767 receive_transfer, session_device_id, 40);
6f5f21f9
UH
768 if (libusb_submit_transfer(transfer) != 0) {
769 /* TODO: Free them all. */
a1bb33af
UH
770 libusb_free_transfer(transfer);
771 g_free(buf);
e46b8fb1 772 return SR_ERR;
a1bb33af
UH
773 }
774 size = 4096;
775 }
776
777 lupfd = libusb_get_pollfds(usb_context);
6f5f21f9 778 for (i = 0; lupfd[i]; i++)
6f1be0a2
UH
779 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
780 NULL);
a1bb33af
UH
781 free(lupfd);
782
5a2326a7 783 packet->type = SR_DF_HEADER;
b9c735a2 784 packet->length = sizeof(struct sr_datafeed_header);
6f5f21f9 785 packet->payload = (unsigned char *)header;
a1bb33af
UH
786 header->feed_version = 1;
787 gettimeofday(&header->starttime, NULL);
4c100f32 788 header->samplerate = cur_samplerate;
5a2326a7 789 header->protocol_id = SR_PROTO_RAW;
e10d6e32 790 header->num_logic_probes = fx2->num_probes;
c2616fb9 791 header->num_analog_probes = 0;
8a2efef2 792 sr_session_bus(session_device_id, packet);
a1bb33af
UH
793 g_free(header);
794 g_free(packet);
795
e46b8fb1 796 return SR_OK;
a1bb33af
UH
797}
798
6f5f21f9 799/* This stops acquisition on ALL devices, ignoring device_index. */
a1bb33af
UH
800static void hw_stop_acquisition(int device_index, gpointer session_device_id)
801{
b9c735a2 802 struct sr_datafeed_packet packet;
a1bb33af 803
17e1afcb 804 /* Avoid compiler warnings. */
afc8e4de
UH
805 device_index = device_index;
806
5a2326a7 807 packet.type = SR_DF_END;
8a2efef2 808 sr_session_bus(session_device_id, &packet);
a1bb33af
UH
809
810 receive_transfer(NULL);
811
6f5f21f9 812 /* TODO: Need to cancel and free any queued up transfers. */
a1bb33af
UH
813}
814
5c2d46d1 815struct sr_device_plugin saleae_logic_plugin_info = {
e519ba86
UH
816 .name = "saleae-logic",
817 .longname = "Saleae Logic",
818 .api_version = 1,
819 .init = hw_init,
820 .cleanup = hw_cleanup,
86f5e3d8
UH
821 .opendev = hw_opendev,
822 .closedev = hw_closedev,
e519ba86
UH
823 .get_device_info = hw_get_device_info,
824 .get_status = hw_get_status,
825 .get_capabilities = hw_get_capabilities,
826 .set_configuration = hw_set_configuration,
827 .start_acquisition = hw_start_acquisition,
828 .stop_acquisition = hw_stop_acquisition,
a1bb33af 829};