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