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