]> sigrok.org Git - libsigrok.git/blob - hardware/saleae-logic/saleae-logic.c
saleae-logic: use new logging system
[libsigrok.git] / hardware / saleae-logic / saleae-logic.c
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 "config.h"
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>
27 #include <sigrok.h>
28 #include <sigrok-internal.h>
29
30 #define USB_INTERFACE          0
31 #define USB_CONFIGURATION      1
32 #define NUM_TRIGGER_STAGES     4
33 #define TRIGGER_TYPES          "01"
34 #define FIRMWARE               FIRMWARE_DIR "/saleae-logic.fw"
35 #define GTV_TO_MSEC(gtv)       (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
36
37 /* delay in ms */
38 #define MAX_RENUM_DELAY        3000
39 #define NUM_SIMUL_TRANSFERS    10
40 #define MAX_EMPTY_TRANSFERS    (NUM_SIMUL_TRANSFERS * 2)
41
42 /* Software trigger implementation: positive values indicate trigger stage. */
43 #define TRIGGER_FIRED          -1
44
45 struct 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
58 static 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
66 /* There is only one model Saleae Logic, and this is what it supports: */
67 static int capabilities[] = {
68         SR_HWCAP_LOGIC_ANALYZER,
69         SR_HWCAP_SAMPLERATE,
70
71         /* These are really implemented in the driver, not the hardware. */
72         SR_HWCAP_LIMIT_SAMPLES,
73         SR_HWCAP_CONTINUOUS,
74         0,
75 };
76
77 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
78 static GSList *device_instances = NULL;
79
80 /*
81  * Since we can't keep track of a Saleae Logic device after upgrading the
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  */
86 static GTimeVal fw_updated = { 0, 0 };
87
88 static libusb_context *usb_context = NULL;
89
90 static uint64_t supported_samplerates[] = {
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),
101         0,
102 };
103
104 static struct sr_samplerates samplerates = {
105         SR_KHZ(200),
106         SR_MHZ(24),
107         SR_HZ(0),
108         supported_samplerates,
109 };
110
111 /* TODO: All of these should go in a device-specific struct. */
112 static uint64_t cur_samplerate = 0;
113 static uint64_t limit_samples = 0;
114 static uint8_t probe_mask = 0;
115 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
116 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
117 static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
118 static struct fx2_device *fx2 = NULL;
119 static int trigger_stage = TRIGGER_FIRED;
120
121 static int hw_set_configuration(int device_index, int capability, void *value);
122 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
123
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.
129  */
130 static int check_conf_profile(libusb_device *dev)
131 {
132         struct libusb_device_descriptor des;
133         struct libusb_config_descriptor *conf_dsc = NULL;
134         const struct libusb_interface_descriptor *intf_dsc;
135         int ret = -1;
136
137         while (ret == -1) {
138                 /* Assume it's not a Saleae Logic unless proven wrong. */
139                 ret = 0;
140
141                 if (libusb_get_device_descriptor(dev, &des) != 0)
142                         break;
143
144                 if (des.bNumConfigurations != 1)
145                         /* Need exactly 1 configuration. */
146                         break;
147
148                 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
149                         break;
150
151                 if (conf_dsc->bNumInterfaces != 1)
152                         /* Need exactly 1 interface. */
153                         break;
154
155                 if (conf_dsc->interface[0].num_altsetting != 1)
156                         /* Need just one alternate setting. */
157                         break;
158
159                 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
160                 if (intf_dsc->bNumEndpoints != 2)
161                         /* Need 2 endpoints. */
162                         break;
163
164                 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
165                     (1 | LIBUSB_ENDPOINT_OUT))
166                         /* First endpoint should be 1 (outbound). */
167                         break;
168
169                 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
170                     (2 | LIBUSB_ENDPOINT_IN))
171                         /* First endpoint should be 2 (inbound). */
172                         break;
173
174                 /* If we made it here, it must be a Saleae Logic. */
175                 ret = 1;
176         }
177
178         if (conf_dsc)
179                 libusb_free_config_descriptor(conf_dsc);
180
181         return ret;
182 }
183
184 static struct sr_device_instance *sl_open_device(int device_index)
185 {
186         libusb_device **devlist;
187         struct libusb_device_descriptor des;
188         struct sr_device_instance *sdi;
189         int err, skip, i;
190
191         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
192                 return NULL;
193
194         if (sdi->status == SR_ST_ACTIVE)
195                 /* already in use */
196                 return NULL;
197
198         skip = 0;
199         libusb_get_device_list(usb_context, &devlist);
200         for (i = 0; devlist[i]; i++) {
201                 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
202                         sr_warn("failed to get device descriptor: %d", err);
203                         continue;
204                 }
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;
235                         sr_info("saleae: opened device %d on %d.%d interface %d",
236                                   sdi->index, sdi->usb->bus,
237                                   sdi->usb->address, USB_INTERFACE);
238                 } else {
239                         sr_warn("failed to open device: %d", err);
240                         sdi = NULL;
241                 }
242         }
243         libusb_free_device_list(devlist, 1);
244
245         if (sdi && sdi->status != SR_ST_ACTIVE)
246                 sdi = NULL;
247
248         return sdi;
249 }
250
251 static int upload_firmware(libusb_device *dev)
252 {
253         int ret;
254
255         ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
256         if (ret != 0)
257                 return 1;
258
259         /* Remember when the last firmware update was done. */
260         g_get_current_time(&fw_updated);
261
262         return 0;
263 }
264
265 static void close_device(struct sr_device_instance *sdi)
266 {
267         if (sdi->usb->devhdl == NULL)
268                 return;
269
270         sr_info("saleae: closing device %d on %d.%d interface %d", sdi->index,
271                 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
272         libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
273         libusb_close(sdi->usb->devhdl);
274         sdi->usb->devhdl = NULL;
275         sdi->status = SR_ST_INACTIVE;
276 }
277
278 static int configure_probes(GSList *probes)
279 {
280         struct sr_probe *probe;
281         GSList *l;
282         int probe_bit, stage, i;
283         char *tc;
284
285         probe_mask = 0;
286         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
287                 trigger_mask[i] = 0;
288                 trigger_value[i] = 0;
289         }
290
291         stage = -1;
292         for (l = probes; l; l = l->next) {
293                 probe = (struct sr_probe *)l->data;
294                 if (probe->enabled == FALSE)
295                         continue;
296                 probe_bit = 1 << (probe->index - 1);
297                 probe_mask |= probe_bit;
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)
308                                 return SR_ERR;
309                 }
310         }
311
312         if (stage == -1)
313                 /*
314                  * We didn't configure any triggers, make sure acquisition
315                  * doesn't wait for any.
316                  */
317                 trigger_stage = TRIGGER_FIRED;
318         else
319                 trigger_stage = 0;
320
321         return SR_OK;
322 }
323
324 /*
325  * API callbacks
326  */
327
328 static int hw_init(const char *deviceinfo)
329 {
330         struct sr_device_instance *sdi;
331         struct libusb_device_descriptor des;
332         libusb_device **devlist;
333         int err, devcnt, i, j;
334
335         /* Avoid compiler warnings. */
336         deviceinfo = deviceinfo;
337
338         if (libusb_init(&usb_context) != 0) {
339                 sr_warn("Failed to initialize USB.");
340                 return 0;
341         }
342
343         /* Find all Saleae Logic devices and upload firmware to all of them. */
344         fx2 = NULL;
345         devcnt = 0;
346         libusb_get_device_list(usb_context, &devlist);
347         for (i = 0; devlist[i]; i++) {
348                 err = libusb_get_device_descriptor(devlist[i], &des);
349                 if (err != 0) {
350                         sr_warn("failed to get device descriptor: %d", err);
351                         continue;
352                 }
353
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;
364
365                 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
366                         fx2->vendor, fx2->model, fx2->model_version);
367                 if (!sdi)
368                         return 0;
369                 device_instances = g_slist_append(device_instances, sdi);
370
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)
378                                 sr_warn("firmware upload failed for device %d",
379                                         devcnt);
380
381                         sdi->usb = sr_usb_device_instance_new
382                                 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
383                 } else {
384                         /* Already has the firmware, so fix the new address. */
385                         sdi->status = SR_ST_INACTIVE;
386                         sdi->usb = sr_usb_device_instance_new
387                             (libusb_get_bus_number(devlist[i]),
388                              libusb_get_device_address(devlist[i]), NULL);
389                 }
390                 devcnt++;
391
392                 /* not supporting multiple FX2s in this driver yet */
393                 break;
394         }
395         libusb_free_device_list(devlist, 1);
396
397         return devcnt;
398 }
399
400 static int hw_opendev(int device_index)
401 {
402         GTimeVal cur_time;
403         struct sr_device_instance *sdi;
404         int timediff, err;
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) {
412                 sr_info("saleae: waiting for device to reset");
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);
422                 }
423                 sr_info("saleae: device came back after %d ms", timediff);
424         } else {
425                 sdi = sl_open_device(device_index);
426         }
427
428         if (!sdi) {
429                 sr_warn("unable to open device");
430                 return SR_ERR;
431         }
432
433         err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
434         if (err != 0) {
435                 sr_warn("Unable to claim interface: %d", err);
436                 return SR_ERR;
437         }
438
439         if (cur_samplerate == 0) {
440                 /* Samplerate hasn't been set; default to the slowest one. */
441                 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
442                     &supported_samplerates[0]) == SR_ERR)
443                         return SR_ERR;
444         }
445
446         return SR_OK;
447 }
448
449 static int hw_closedev(int device_index)
450 {
451         struct sr_device_instance *sdi;
452
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;
462 }
463
464 static void hw_cleanup(void)
465 {
466         GSList *l;
467
468         /* Properly close all devices... */
469         for (l = device_instances; l; l = l->next)
470                 close_device((struct sr_device_instance *)l->data);
471
472         /* ...and free all their memory. */
473         for (l = device_instances; l; l = l->next)
474                 g_free(l->data);
475         g_slist_free(device_instances);
476         device_instances = NULL;
477
478         if (usb_context)
479                 libusb_exit(usb_context);
480         usb_context = NULL;
481 }
482
483 static void *hw_get_device_info(int device_index, int device_info_id)
484 {
485         struct sr_device_instance *sdi;
486         void *info = NULL;
487
488         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
489                 return NULL;
490
491         switch (device_info_id) {
492         case SR_DI_INSTANCE:
493                 info = sdi;
494                 break;
495         case SR_DI_NUM_PROBES:
496                 info = GINT_TO_POINTER(fx2->num_probes);
497                 break;
498         case SR_DI_SAMPLERATES:
499                 info = &samplerates;
500                 break;
501         case SR_DI_TRIGGER_TYPES:
502                 info = TRIGGER_TYPES;
503                 break;
504         case SR_DI_CUR_SAMPLERATE:
505                 info = &cur_samplerate;
506                 break;
507         }
508
509         return info;
510 }
511
512 static int hw_get_status(int device_index)
513 {
514         struct sr_device_instance *sdi;
515
516         sdi = sr_get_device_instance(device_instances, device_index);
517         if (sdi)
518                 return sdi->status;
519         else
520                 return SR_ST_NOT_FOUND;
521 }
522
523 static int *hw_get_capabilities(void)
524 {
525         return capabilities;
526 }
527
528 static int set_configuration_samplerate(struct sr_device_instance *sdi,
529                                         uint64_t samplerate)
530 {
531         uint8_t divider;
532         int ret, result, i;
533         unsigned char buf[2];
534
535         for (i = 0; supported_samplerates[i]; i++) {
536                 if (supported_samplerates[i] == samplerate)
537                         break;
538         }
539         if (supported_samplerates[i] == 0)
540                 return SR_ERR_SAMPLERATE;
541
542         divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
543
544         sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
545                 samplerate, divider);
546         buf[0] = 0x01;
547         buf[1] = divider;
548         ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
549                                    buf, 2, &result, 500);
550         if (ret != 0) {
551                 sr_warn("failed to set samplerate: %d", ret);
552                 return SR_ERR;
553         }
554         cur_samplerate = samplerate;
555
556         return SR_OK;
557 }
558
559 static int hw_set_configuration(int device_index, int capability, void *value)
560 {
561         struct sr_device_instance *sdi;
562         int ret;
563         uint64_t *tmp_u64;
564
565         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
566                 return SR_ERR;
567
568         if (capability == SR_HWCAP_SAMPLERATE) {
569                 tmp_u64 = value;
570                 ret = set_configuration_samplerate(sdi, *tmp_u64);
571         } else if (capability == SR_HWCAP_PROBECONFIG) {
572                 ret = configure_probes((GSList *) value);
573         } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
574                 tmp_u64 = value;
575                 limit_samples = *tmp_u64;
576                 ret = SR_OK;
577         } else {
578                 ret = SR_ERR;
579         }
580
581         return ret;
582 }
583
584 static int receive_data(int fd, int revents, void *user_data)
585 {
586         struct timeval tv;
587
588         /* Avoid compiler warnings. */
589         fd = fd;
590         revents = revents;
591         user_data = user_data;
592
593         tv.tv_sec = tv.tv_usec = 0;
594         libusb_handle_events_timeout(usb_context, &tv);
595
596         return TRUE;
597 }
598
599 void receive_transfer(struct libusb_transfer *transfer)
600 {
601         static int num_samples = 0;
602         static int empty_transfer_count = 0;
603         struct sr_datafeed_packet packet;
604         void *user_data;
605         int cur_buflen, trigger_offset, i;
606         unsigned char *cur_buf, *new_buf;
607
608         /* hw_stop_acquisition() is telling us to stop. */
609         if (transfer == NULL)
610                 num_samples = -1;
611
612         /*
613          * If acquisition has already ended, just free any queued up
614          * transfer that come in.
615          */
616         if (num_samples == -1) {
617                 if (transfer)
618                         libusb_free_transfer(transfer);
619                 return;
620         }
621
622         sr_info("saleae: receive_transfer(): status %d received %d bytes",
623                 transfer->status, transfer->actual_length);
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. */
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
637         transfer->buffer = new_buf;
638         transfer->length = 4096;
639         if (libusb_submit_transfer(transfer) != 0) {
640                 /* TODO: Stop session? */
641                 sr_warn("eek");
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                          */
651                         hw_stop_acquisition(-1, user_data);
652                 }
653                 return;
654         } else {
655                 empty_transfer_count = 0;
656         }
657
658         trigger_offset = 0;
659         if (trigger_stage >= 0) {
660                 for (i = 0; i < cur_buflen; i++) {
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                                          */
674                                         packet.type = SR_DF_TRIGGER;
675                                         packet.length = 0;
676                                         sr_session_bus(user_data, &packet);
677
678                                         /*
679                                          * Send the samples that triggered it, since we're
680                                          * skipping past them.
681                                          */
682                                         packet.type = SR_DF_LOGIC;
683                                         packet.length = trigger_stage;
684                                         packet.unitsize = 1;
685                                         packet.payload = trigger_buffer;
686                                         sr_session_bus(user_data, &packet);
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
710                 }
711         }
712
713         if (trigger_stage == TRIGGER_FIRED) {
714                 /* Send the incoming transfer to the session bus. */
715                 packet.type = SR_DF_LOGIC;
716                 packet.length = cur_buflen - trigger_offset;
717                 packet.unitsize = 1;
718                 packet.payload = cur_buf + trigger_offset;
719                 sr_session_bus(user_data, &packet);
720                 g_free(cur_buf);
721
722                 num_samples += cur_buflen;
723                 if (limit_samples && (unsigned int) num_samples > limit_samples) {
724                         hw_stop_acquisition(-1, user_data);
725                 }
726         } else {
727                 /*
728                  * TODO: Buffer pre-trigger data in capture
729                  * ratio-sized buffer.
730                  */
731         }
732 }
733
734 static int hw_start_acquisition(int device_index, gpointer session_device_id)
735 {
736         struct sr_device_instance *sdi;
737         struct sr_datafeed_packet *packet;
738         struct sr_datafeed_header *header;
739         struct libusb_transfer *transfer;
740         const struct libusb_pollfd **lupfd;
741         int size, i;
742         unsigned char *buf;
743
744         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
745                 return SR_ERR;
746
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         }
756
757         /* Start with 2K transfer, subsequently increased to 4K. */
758         size = 2048;
759         for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
760                 if (!(buf = g_try_malloc(size))) {
761                         sr_err("saleae: %s: buf malloc failed", __func__);
762                         return SR_ERR_MALLOC;
763                 }
764                 transfer = libusb_alloc_transfer(0);
765                 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
766                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
767                                 receive_transfer, session_device_id, 40);
768                 if (libusb_submit_transfer(transfer) != 0) {
769                         /* TODO: Free them all. */
770                         libusb_free_transfer(transfer);
771                         g_free(buf);
772                         return SR_ERR;
773                 }
774                 size = 4096;
775         }
776
777         lupfd = libusb_get_pollfds(usb_context);
778         for (i = 0; lupfd[i]; i++)
779                 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
780                               NULL);
781         free(lupfd);
782
783         packet->type = SR_DF_HEADER;
784         packet->length = sizeof(struct sr_datafeed_header);
785         packet->payload = (unsigned char *)header;
786         header->feed_version = 1;
787         gettimeofday(&header->starttime, NULL);
788         header->samplerate = cur_samplerate;
789         header->protocol_id = SR_PROTO_RAW;
790         header->num_logic_probes = fx2->num_probes;
791         header->num_analog_probes = 0;
792         sr_session_bus(session_device_id, packet);
793         g_free(header);
794         g_free(packet);
795
796         return SR_OK;
797 }
798
799 /* This stops acquisition on ALL devices, ignoring device_index. */
800 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
801 {
802         struct sr_datafeed_packet packet;
803
804         /* Avoid compiler warnings. */
805         device_index = device_index;
806
807         packet.type = SR_DF_END;
808         sr_session_bus(session_device_id, &packet);
809
810         receive_transfer(NULL);
811
812         /* TODO: Need to cancel and free any queued up transfers. */
813 }
814
815 struct sr_device_plugin saleae_logic_plugin_info = {
816         .name = "saleae-logic",
817         .longname = "Saleae Logic",
818         .api_version = 1,
819         .init = hw_init,
820         .cleanup = hw_cleanup,
821         .opendev = hw_opendev,
822         .closedev = hw_closedev,
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,
829 };