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