]> sigrok.org Git - libsigrok.git/blob - hardware/saleae-logic/saleae-logic.c
support for multiple FX2 devices
[libsigrok.git] / hardware / saleae-logic / saleae-logic.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <inttypes.h>
25 #include <glib.h>
26 #include <libusb.h>
27 #include <sigrok.h>
28 #include <sigrok-internal.h>
29 #include "saleae-logic.h"
30
31 static struct fx2_profile supported_fx2[] = {
32         /* Saleae Logic */
33         { 0x0925, 0x3881, 0x0925, 0x3881, "Saleae", "Logic", NULL, 8 },
34         /* default Cypress FX2 without EEPROM */
35         { 0x04b4, 0x8613, 0x0925, 0x3881, "Cypress", "FX2", NULL, 16 },
36         { 0, 0, 0, 0, 0, 0, 0, 0 }
37 };
38
39 static int capabilities[] = {
40         SR_HWCAP_LOGIC_ANALYZER,
41         SR_HWCAP_SAMPLERATE,
42
43         /* These are really implemented in the driver, not the hardware. */
44         SR_HWCAP_LIMIT_SAMPLES,
45         SR_HWCAP_CONTINUOUS,
46         0,
47 };
48
49 static uint64_t supported_samplerates[] = {
50         SR_KHZ(200),
51         SR_KHZ(250),
52         SR_KHZ(500),
53         SR_MHZ(1),
54         SR_MHZ(2),
55         SR_MHZ(4),
56         SR_MHZ(8),
57         SR_MHZ(12),
58         SR_MHZ(16),
59         SR_MHZ(24),
60         0,
61 };
62
63 static struct sr_samplerates samplerates = {
64         SR_KHZ(200),
65         SR_MHZ(24),
66         SR_HZ(0),
67         supported_samplerates,
68 };
69
70 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
71 static GSList *device_instances = NULL;
72 static libusb_context *usb_context = NULL;
73
74 static int hw_set_configuration(int device_index, int capability, void *value);
75 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
76
77 /**
78  * Check the USB configuration to determine if this is a Saleae Logic.
79  *
80  * @return 1 if the device's configuration profile match the Logic firmware's
81  *         configuration, 0 otherwise.
82  */
83 static int check_conf_profile(libusb_device *dev)
84 {
85         struct libusb_device_descriptor des;
86         struct libusb_config_descriptor *conf_dsc = NULL;
87         const struct libusb_interface_descriptor *intf_dsc;
88         int ret = -1;
89
90         while (ret == -1) {
91                 /* Assume it's not a Saleae Logic unless proven wrong. */
92                 ret = 0;
93
94                 if (libusb_get_device_descriptor(dev, &des) != 0)
95                         break;
96
97                 if (des.bNumConfigurations != 1)
98                         /* Need exactly 1 configuration. */
99                         break;
100
101                 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
102                         break;
103
104                 if (conf_dsc->bNumInterfaces != 1)
105                         /* Need exactly 1 interface. */
106                         break;
107
108                 if (conf_dsc->interface[0].num_altsetting != 1)
109                         /* Need just one alternate setting. */
110                         break;
111
112                 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
113                 if (intf_dsc->bNumEndpoints != 2)
114                         /* Need 2 endpoints. */
115                         break;
116
117                 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
118                     (1 | LIBUSB_ENDPOINT_OUT))
119                         /* First endpoint should be 1 (outbound). */
120                         break;
121
122                 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
123                     (2 | LIBUSB_ENDPOINT_IN))
124                         /* First endpoint should be 2 (inbound). */
125                         break;
126
127                 /* If we made it here, it must be a Saleae Logic. */
128                 ret = 1;
129         }
130
131         if (conf_dsc)
132                 libusb_free_config_descriptor(conf_dsc);
133
134         return ret;
135 }
136
137 static int sl_open_device(int device_index)
138 {
139         libusb_device **devlist;
140         struct libusb_device_descriptor des;
141         struct sr_device_instance *sdi;
142         struct fx2_device *fx2;
143         int err, skip, i;
144
145         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
146                 return SR_ERR;
147         fx2 = sdi->priv;
148
149         if (sdi->status == SR_ST_ACTIVE)
150                 /* already in use */
151                 return SR_ERR;
152
153         skip = 0;
154         libusb_get_device_list(usb_context, &devlist);
155         for (i = 0; devlist[i]; i++) {
156                 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
157                         sr_warn("failed to get device descriptor: %d", err);
158                         continue;
159                 }
160
161                 if (des.idVendor != fx2->profile->fw_vid || des.idProduct != fx2->profile->fw_pid)
162                         continue;
163
164                 if (sdi->status == SR_ST_INITIALIZING) {
165                         if (skip != device_index) {
166                                 /* Skip devices of this type that aren't the one we want. */
167                                 skip += 1;
168                                 continue;
169                         }
170                 } else if (sdi->status == SR_ST_INACTIVE) {
171                         /*
172                          * This device is fully enumerated, so we need to find this
173                          * device by vendor, product, bus and address.
174                          */
175                         if (libusb_get_bus_number(devlist[i]) != sdi->usb->bus
176                                 || libusb_get_device_address(devlist[i]) != sdi->usb->address)
177                                 /* this is not the one */
178                                 continue;
179                 }
180
181                 if (!(err = libusb_open(devlist[i], &sdi->usb->devhdl))) {
182                         if (sdi->usb->address == 0xff)
183                                 /*
184                                  * first time we touch this device after firmware upload,
185                                  * so we don't know the address yet.
186                                  */
187                                 sdi->usb->address = libusb_get_device_address(devlist[i]);
188
189                         sdi->status = SR_ST_ACTIVE;
190                         sr_info("saleae: opened device %d on %d.%d interface %d",
191                                   sdi->index, sdi->usb->bus,
192                                   sdi->usb->address, USB_INTERFACE);
193                 } else {
194                         sr_warn("failed to open device: %d", err);
195                 }
196
197                 /* if we made it here, we handled the device one way or another */
198                 break;
199         }
200         libusb_free_device_list(devlist, 1);
201
202         if (sdi->status != SR_ST_ACTIVE)
203                 return SR_ERR;
204
205         return SR_OK;
206 }
207
208 static void close_device(struct sr_device_instance *sdi)
209 {
210         if (sdi->usb->devhdl == NULL)
211                 return;
212
213         sr_info("saleae: closing device %d on %d.%d interface %d", sdi->index,
214                 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
215         libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
216         libusb_close(sdi->usb->devhdl);
217         sdi->usb->devhdl = NULL;
218         sdi->status = SR_ST_INACTIVE;
219 }
220
221 static int configure_probes(struct fx2_device *fx2, GSList *probes)
222 {
223         struct sr_probe *probe;
224         GSList *l;
225         int probe_bit, stage, i;
226         char *tc;
227
228         fx2->probe_mask = 0;
229         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
230                 fx2->trigger_mask[i] = 0;
231                 fx2->trigger_value[i] = 0;
232         }
233
234         stage = -1;
235         for (l = probes; l; l = l->next) {
236                 probe = (struct sr_probe *)l->data;
237                 if (probe->enabled == FALSE)
238                         continue;
239                 probe_bit = 1 << (probe->index - 1);
240                 fx2->probe_mask |= probe_bit;
241                 if (!(probe->trigger))
242                         continue;
243
244                 stage = 0;
245                 for (tc = probe->trigger; *tc; tc++) {
246                         fx2->trigger_mask[stage] |= probe_bit;
247                         if (*tc == '1')
248                                 fx2->trigger_value[stage] |= probe_bit;
249                         stage++;
250                         if (stage > NUM_TRIGGER_STAGES)
251                                 return SR_ERR;
252                 }
253         }
254
255         if (stage == -1)
256                 /*
257                  * We didn't configure any triggers, make sure acquisition
258                  * doesn't wait for any.
259                  */
260                 fx2->trigger_stage = TRIGGER_FIRED;
261         else
262                 fx2->trigger_stage = 0;
263
264         return SR_OK;
265 }
266
267 static struct fx2_device *fx2_device_new(void)
268 {
269         struct fx2_device *fx2;
270
271         if (!(fx2 = g_try_malloc0(sizeof(struct fx2_device)))) {
272                 sr_err("saleae: %s: saleae malloc failed", __func__);
273                 return NULL;
274         }
275         fx2->trigger_stage = TRIGGER_FIRED;
276
277         return fx2;
278 }
279
280
281 /*
282  * API callbacks
283  */
284
285 static int hw_init(const char *deviceinfo)
286 {
287         struct sr_device_instance *sdi;
288         struct libusb_device_descriptor des;
289         struct fx2_profile *fx2_prof;
290         struct fx2_device *fx2;
291         libusb_device **devlist;
292         int err, devcnt, i, j;
293
294         /* Avoid compiler warnings. */
295         deviceinfo = deviceinfo;
296
297         if (libusb_init(&usb_context) != 0) {
298                 sr_warn("Failed to initialize USB.");
299                 return 0;
300         }
301
302         /* Find all Saleae Logic devices and upload firmware to all of them. */
303         devcnt = 0;
304         libusb_get_device_list(usb_context, &devlist);
305         for (i = 0; devlist[i]; i++) {
306                 fx2_prof = NULL;
307                 err = libusb_get_device_descriptor(devlist[i], &des);
308                 if (err != 0) {
309                         sr_warn("failed to get device descriptor: %d", err);
310                         continue;
311                 }
312
313                 for (j = 0; supported_fx2[j].orig_vid; j++) {
314                         if (des.idVendor == supported_fx2[j].orig_vid
315                                 && des.idProduct == supported_fx2[j].orig_pid) {
316                                 fx2_prof = &supported_fx2[j];
317                                 break;
318                         }
319                 }
320                 if (!fx2_prof)
321                         /* not a supported VID/PID */
322                         continue;
323
324                 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
325                         fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version);
326                 if (!sdi)
327                         return 0;
328                 fx2 = fx2_device_new();
329                 fx2->profile = fx2_prof;
330                 sdi->priv = fx2;
331                 device_instances = g_slist_append(device_instances, sdi);
332
333                 if (check_conf_profile(devlist[i])) {
334                         /* Already has the firmware, so fix the new address. */
335                         sdi->status = SR_ST_INACTIVE;
336                         sdi->usb = sr_usb_device_instance_new
337                             (libusb_get_bus_number(devlist[i]),
338                              libusb_get_device_address(devlist[i]), NULL);
339                 } else {
340                         if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
341                                 /* Remember when the firmware on this device was updated */
342                                 g_get_current_time(&fx2->fw_updated);
343                         else
344                                 sr_warn("firmware upload failed for device %d", devcnt);
345                         sdi->usb = sr_usb_device_instance_new
346                                 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
347                 }
348                 devcnt++;
349         }
350         libusb_free_device_list(devlist, 1);
351
352         return devcnt;
353 }
354
355 static int hw_opendev(int device_index)
356 {
357         GTimeVal cur_time;
358         struct sr_device_instance *sdi;
359         struct fx2_device *fx2;
360         int timediff, err;
361
362         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
363                 return SR_ERR;
364         fx2 = sdi->priv;
365
366         /*
367          * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
368          * for the FX2 to renumerate
369          */
370         err = 0;
371         if (GTV_TO_MSEC(fx2->fw_updated) > 0) {
372                 sr_info("saleae: waiting for device to reset");
373                 /* takes at least 300ms for the FX2 to be gone from the USB bus */
374                 g_usleep(300*1000);
375                 timediff = 0;
376                 while (timediff < MAX_RENUM_DELAY) {
377                         if ((err = sl_open_device(device_index)) == SR_OK)
378                                 break;
379                         g_usleep(100*1000);
380                         g_get_current_time(&cur_time);
381                         timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fx2->fw_updated);
382                 }
383                 sr_info("saleae: device came back after %d ms", timediff);
384         } else {
385                 err = sl_open_device(device_index);
386         }
387
388         if (err != SR_OK) {
389                 sr_warn("unable to open device");
390                 return SR_ERR;
391         }
392         fx2 = sdi->priv;
393
394         err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
395         if (err != 0) {
396                 sr_warn("Unable to claim interface: %d", err);
397                 return SR_ERR;
398         }
399
400         if (fx2->cur_samplerate == 0) {
401                 /* Samplerate hasn't been set; default to the slowest one. */
402                 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
403                     &supported_samplerates[0]) == SR_ERR)
404                         return SR_ERR;
405         }
406
407         return SR_OK;
408 }
409
410 static int hw_closedev(int device_index)
411 {
412         struct sr_device_instance *sdi;
413
414         if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
415                 sr_err("logic: %s: sdi was NULL", __func__);
416                 return SR_ERR; /* TODO: SR_ERR_ARG? */
417         }
418
419         /* TODO */
420         close_device(sdi);
421
422         return SR_OK;
423 }
424
425 static void hw_cleanup(void)
426 {
427         GSList *l;
428
429         /* Properly close all devices... */
430         for (l = device_instances; l; l = l->next)
431                 close_device((struct sr_device_instance *)l->data);
432
433         /* ...and free all their memory. */
434         for (l = device_instances; l; l = l->next)
435                 g_free(l->data);
436         g_slist_free(device_instances);
437         device_instances = NULL;
438
439         if (usb_context)
440                 libusb_exit(usb_context);
441         usb_context = NULL;
442 }
443
444 static void *hw_get_device_info(int device_index, int device_info_id)
445 {
446         struct sr_device_instance *sdi;
447         struct fx2_device *fx2;
448         void *info = NULL;
449
450         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
451                 return NULL;
452         fx2 = sdi->priv;
453
454         switch (device_info_id) {
455         case SR_DI_INSTANCE:
456                 info = sdi;
457                 break;
458         case SR_DI_NUM_PROBES:
459                 info = GINT_TO_POINTER(fx2->profile->num_probes);
460                 break;
461         case SR_DI_SAMPLERATES:
462                 info = &samplerates;
463                 break;
464         case SR_DI_TRIGGER_TYPES:
465                 info = TRIGGER_TYPES;
466                 break;
467         case SR_DI_CUR_SAMPLERATE:
468                 info = &fx2->cur_samplerate;
469                 break;
470         }
471
472         return info;
473 }
474
475 static int hw_get_status(int device_index)
476 {
477         struct sr_device_instance *sdi;
478
479         sdi = sr_get_device_instance(device_instances, device_index);
480         if (sdi)
481                 return sdi->status;
482         else
483                 return SR_ST_NOT_FOUND;
484 }
485
486 static int *hw_get_capabilities(void)
487 {
488         return capabilities;
489 }
490
491 static int set_configuration_samplerate(struct sr_device_instance *sdi,
492                                         uint64_t samplerate)
493 {
494         struct fx2_device *fx2;
495         uint8_t divider;
496         int ret, result, i;
497         unsigned char buf[2];
498
499         fx2 = sdi->priv;
500         for (i = 0; supported_samplerates[i]; i++) {
501                 if (supported_samplerates[i] == samplerate)
502                         break;
503         }
504         if (supported_samplerates[i] == 0)
505                 return SR_ERR_SAMPLERATE;
506
507         divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
508
509         sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
510                 samplerate, divider);
511         buf[0] = 0x01;
512         buf[1] = divider;
513         ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
514                                    buf, 2, &result, 500);
515         if (ret != 0) {
516                 sr_warn("failed to set samplerate: %d", ret);
517                 return SR_ERR;
518         }
519         fx2->cur_samplerate = samplerate;
520
521         return SR_OK;
522 }
523
524 static int hw_set_configuration(int device_index, int capability, void *value)
525 {
526         struct sr_device_instance *sdi;
527         struct fx2_device *fx2;
528         int ret;
529         uint64_t *tmp_u64;
530
531         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
532                 return SR_ERR;
533         fx2 = sdi->priv;
534
535         if (capability == SR_HWCAP_SAMPLERATE) {
536                 tmp_u64 = value;
537                 ret = set_configuration_samplerate(sdi, *tmp_u64);
538         } else if (capability == SR_HWCAP_PROBECONFIG) {
539                 ret = configure_probes(fx2, (GSList *) value);
540         } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
541                 tmp_u64 = value;
542                 fx2->limit_samples = *tmp_u64;
543                 ret = SR_OK;
544         } else {
545                 ret = SR_ERR;
546         }
547
548         return ret;
549 }
550
551 static int receive_data(int fd, int revents, void *user_data)
552 {
553         struct timeval tv;
554
555         /* Avoid compiler warnings. */
556         fd = fd;
557         revents = revents;
558         user_data = user_data;
559
560         tv.tv_sec = tv.tv_usec = 0;
561         libusb_handle_events_timeout(usb_context, &tv);
562
563         return TRUE;
564 }
565
566 void receive_transfer(struct libusb_transfer *transfer)
567 {
568         static int num_samples = 0;
569         static int empty_transfer_count = 0;
570         struct sr_datafeed_packet packet;
571         struct fx2_device *fx2;
572         int cur_buflen, trigger_offset, i;
573         unsigned char *cur_buf, *new_buf;
574
575         /* hw_stop_acquisition() is telling us to stop. */
576         if (transfer == NULL)
577                 num_samples = -1;
578
579         /*
580          * If acquisition has already ended, just free any queued up
581          * transfer that come in.
582          */
583         if (num_samples == -1) {
584                 if (transfer)
585                         libusb_free_transfer(transfer);
586                 return;
587         }
588
589         sr_info("saleae: receive_transfer(): status %d received %d bytes",
590                 transfer->status, transfer->actual_length);
591
592         /* Save incoming transfer before reusing the transfer struct. */
593         cur_buf = transfer->buffer;
594         cur_buflen = transfer->actual_length;
595         fx2 = transfer->user_data;
596
597         /* Fire off a new request. */
598         if (!(new_buf = g_try_malloc(4096))) {
599                 sr_err("saleae: %s: new_buf malloc failed", __func__);
600                 // return SR_ERR_MALLOC;
601                 return; /* FIXME */
602         }
603
604         transfer->buffer = new_buf;
605         transfer->length = 4096;
606         if (libusb_submit_transfer(transfer) != 0) {
607                 /* TODO: Stop session? */
608                 sr_warn("eek");
609         }
610
611         if (cur_buflen == 0) {
612                 empty_transfer_count++;
613                 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
614                         /*
615                          * The FX2 gave up. End the acquisition, the frontend
616                          * will work out that the samplecount is short.
617                          */
618                         hw_stop_acquisition(-1, fx2->session_data);
619                 }
620                 return;
621         } else {
622                 empty_transfer_count = 0;
623         }
624
625         trigger_offset = 0;
626         if (fx2->trigger_stage >= 0) {
627                 for (i = 0; i < cur_buflen; i++) {
628
629                         if ((cur_buf[i] & fx2->trigger_mask[fx2->trigger_stage]) == fx2->trigger_value[fx2->trigger_stage]) {
630                                 /* Match on this trigger stage. */
631                                 fx2->trigger_buffer[fx2->trigger_stage] = cur_buf[i];
632                                 fx2->trigger_stage++;
633                                 if (fx2->trigger_stage == NUM_TRIGGER_STAGES || fx2->trigger_mask[fx2->trigger_stage] == 0) {
634                                         /* Match on all trigger stages, we're done. */
635                                         trigger_offset = i + 1;
636
637                                         /*
638                                          * TODO: Send pre-trigger buffer to session bus.
639                                          * Tell the frontend we hit the trigger here.
640                                          */
641                                         packet.type = SR_DF_TRIGGER;
642                                         packet.length = 0;
643                                         sr_session_bus(fx2->session_data, &packet);
644
645                                         /*
646                                          * Send the samples that triggered it, since we're
647                                          * skipping past them.
648                                          */
649                                         packet.type = SR_DF_LOGIC;
650                                         packet.length = fx2->trigger_stage;
651                                         packet.unitsize = 1;
652                                         packet.payload = fx2->trigger_buffer;
653                                         sr_session_bus(fx2->session_data, &packet);
654
655                                         fx2->trigger_stage = TRIGGER_FIRED;
656                                         break;
657                                 }
658                                 return;
659                         }
660
661                         /*
662                          * We had a match before, but not in the next sample. However, we may
663                          * have a match on this stage in the next bit -- trigger on 0001 will
664                          * fail on seeing 00001, so we need to go back to stage 0 -- but at
665                          * the next sample from the one that matched originally, which the
666                          * counter increment at the end of the loop takes care of.
667                          */
668                         if (fx2->trigger_stage > 0) {
669                                 i -= fx2->trigger_stage;
670                                 if (i < -1)
671                                         i = -1; /* Oops, went back past this buffer. */
672                                 /* Reset trigger stage. */
673                                 fx2->trigger_stage = 0;
674                         }
675                 }
676         }
677
678         if (fx2->trigger_stage == TRIGGER_FIRED) {
679                 /* Send the incoming transfer to the session bus. */
680                 packet.type = SR_DF_LOGIC;
681                 packet.length = cur_buflen - trigger_offset;
682                 packet.unitsize = 1;
683                 packet.payload = cur_buf + trigger_offset;
684                 sr_session_bus(fx2->session_data, &packet);
685                 g_free(cur_buf);
686
687                 num_samples += cur_buflen;
688                 if (fx2->limit_samples && (unsigned int) num_samples > fx2->limit_samples) {
689                         hw_stop_acquisition(-1, fx2->session_data);
690                 }
691         } else {
692                 /*
693                  * TODO: Buffer pre-trigger data in capture
694                  * ratio-sized buffer.
695                  */
696         }
697 }
698
699 static int hw_start_acquisition(int device_index, gpointer session_data)
700 {
701         struct sr_device_instance *sdi;
702         struct sr_datafeed_packet *packet;
703         struct sr_datafeed_header *header;
704         struct fx2_device *fx2;
705         struct libusb_transfer *transfer;
706         const struct libusb_pollfd **lupfd;
707         int size, i;
708         unsigned char *buf;
709
710         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
711                 return SR_ERR;
712         fx2 = sdi->priv;
713         fx2->session_data = session_data;
714
715         if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
716                 sr_err("saleae: %s: packet malloc failed", __func__);
717                 return SR_ERR_MALLOC;
718         }
719         
720         if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
721                 sr_err("saleae: %s: header malloc failed", __func__);
722                 return SR_ERR_MALLOC;
723         }
724
725         /* Start with 2K transfer, subsequently increased to 4K. */
726         size = 2048;
727         for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
728                 if (!(buf = g_try_malloc(size))) {
729                         sr_err("saleae: %s: buf malloc failed", __func__);
730                         return SR_ERR_MALLOC;
731                 }
732                 transfer = libusb_alloc_transfer(0);
733                 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
734                                 2 | LIBUSB_ENDPOINT_IN, buf, size,
735                                 receive_transfer, fx2, 40);
736                 if (libusb_submit_transfer(transfer) != 0) {
737                         /* TODO: Free them all. */
738                         libusb_free_transfer(transfer);
739                         g_free(buf);
740                         return SR_ERR;
741                 }
742                 size = 4096;
743         }
744
745         lupfd = libusb_get_pollfds(usb_context);
746         for (i = 0; lupfd[i]; i++)
747                 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
748                               NULL);
749         free(lupfd);
750
751         packet->type = SR_DF_HEADER;
752         packet->length = sizeof(struct sr_datafeed_header);
753         packet->payload = (unsigned char *)header;
754         header->feed_version = 1;
755         gettimeofday(&header->starttime, NULL);
756         header->samplerate = fx2->cur_samplerate;
757         header->protocol_id = SR_PROTO_RAW;
758         header->num_logic_probes = fx2->profile->num_probes;
759         header->num_analog_probes = 0;
760         sr_session_bus(session_data, packet);
761         g_free(header);
762         g_free(packet);
763
764         return SR_OK;
765 }
766
767 /* This stops acquisition on ALL devices, ignoring device_index. */
768 static void hw_stop_acquisition(int device_index, gpointer session_data)
769 {
770         struct sr_datafeed_packet packet;
771
772         /* Avoid compiler warnings. */
773         device_index = device_index;
774
775         packet.type = SR_DF_END;
776         sr_session_bus(session_data, &packet);
777
778         receive_transfer(NULL);
779
780         /* TODO: Need to cancel and free any queued up transfers. */
781 }
782
783 struct sr_device_plugin saleae_logic_plugin_info = {
784         .name = "saleae-logic",
785         .longname = "Saleae Logic",
786         .api_version = 1,
787         .init = hw_init,
788         .cleanup = hw_cleanup,
789         .opendev = hw_opendev,
790         .closedev = hw_closedev,
791         .get_device_info = hw_get_device_info,
792         .get_status = hw_get_status,
793         .get_capabilities = hw_get_capabilities,
794         .set_configuration = hw_set_configuration,
795         .start_acquisition = hw_start_acquisition,
796         .stop_acquisition = hw_stop_acquisition,
797 };