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