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