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