]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
162d084c26f1a6605342eceda2d621549af8f143
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 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 <string.h>
23 #include <sys/time.h>
24 #include <inttypes.h>
25 #include <glib.h>
26 #include <libusb.h>
27 #include "config.h"
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
30 #include "analyzer.h"
31
32 #define USB_VENDOR                      0x0c12
33
34 #define VENDOR_NAME                     "ZEROPLUS"
35 #define MODEL_NAME                      "Logic Cube LAP-C"
36 #define MODEL_VERSION                   NULL
37
38 #define NUM_PROBES                      16
39 #define USB_INTERFACE                   0
40 #define USB_CONFIGURATION               1
41 #define NUM_TRIGGER_STAGES              4
42 #define TRIGGER_TYPES                   "01"
43
44 #define PACKET_SIZE                     2048    /* ?? */
45
46 typedef struct {
47         unsigned short pid;
48         char model_name[64];
49         unsigned int channels;
50         unsigned int sample_depth;      /* In Ksamples/channel */
51         unsigned int max_sampling_freq;
52 } model_t;
53
54 /*
55  * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
56  * same 128K sample depth.
57  */
58 static model_t zeroplus_models[] = {
59         {0x7009, "LAP-C(16064)",  16, 64,   100},
60         {0x700A, "LAP-C(16128)",  16, 128,  200},
61         {0x700B, "LAP-C(32128)",  32, 128,  200},
62         {0x700C, "LAP-C(321000)", 32, 1024, 200},
63         {0x700D, "LAP-C(322000)", 32, 2048, 200},
64         {0x700E, "LAP-C(16032)",  16, 32,   100},
65         {0x7016, "LAP-C(162000)", 16, 2048, 200},
66 };
67
68 static const int hwcaps[] = {
69         SR_HWCAP_LOGIC_ANALYZER,
70         SR_HWCAP_SAMPLERATE,
71         SR_HWCAP_PROBECONFIG,
72         SR_HWCAP_CAPTURE_RATIO,
73
74         /* These are really implemented in the driver, not the hardware. */
75         SR_HWCAP_LIMIT_SAMPLES,
76         0,
77 };
78
79 /*
80  * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
81  * We currently ignore other untested/unsupported devices here.
82  */
83 static const char *probe_names[NUM_PROBES + 1] = {
84         "A0",
85         "A1",
86         "A2",
87         "A3",
88         "A4",
89         "A5",
90         "A6",
91         "A7",
92         "B0",
93         "B1",
94         "B2",
95         "B3",
96         "B4",
97         "B5",
98         "B6",
99         "B7",
100         NULL,
101 };
102
103 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
104 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
105 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
106
107 static libusb_context *usb_context = NULL;
108
109 /*
110  * The hardware supports more samplerates than these, but these are the
111  * options hardcoded into the vendor's Windows GUI.
112  */
113
114 /*
115  * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
116  * that high.
117  */
118 static const uint64_t supported_samplerates[] = {
119         SR_HZ(100),
120         SR_HZ(500),
121         SR_KHZ(1),
122         SR_KHZ(5),
123         SR_KHZ(25),
124         SR_KHZ(50),
125         SR_KHZ(100),
126         SR_KHZ(200),
127         SR_KHZ(400),
128         SR_KHZ(800),
129         SR_MHZ(1),
130         SR_MHZ(10),
131         SR_MHZ(25),
132         SR_MHZ(50),
133         SR_MHZ(80),
134         SR_MHZ(100),
135         SR_MHZ(150),
136         SR_MHZ(200),
137         0,
138 };
139
140 static const struct sr_samplerates samplerates = {
141         0,
142         0,
143         0,
144         supported_samplerates,
145 };
146
147 /* Private, per-device-instance driver context. */
148 struct context {
149         uint64_t cur_samplerate;
150         uint64_t limit_samples;
151         int num_channels; /* TODO: This isn't initialized before it's needed :( */
152         uint64_t memory_size;
153         uint8_t probe_mask;
154         uint8_t trigger_mask[NUM_TRIGGER_STAGES];
155         uint8_t trigger_value[NUM_TRIGGER_STAGES];
156         // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
157
158         struct sr_usb_dev_inst *usb;
159 };
160
161 static int hw_dev_config_set(int dev_index, int hwcap, const void *value);
162
163 static unsigned int get_memory_size(int type)
164 {
165         if (type == MEMORY_SIZE_8K)
166                 return 8 * 1024;
167         else if (type == MEMORY_SIZE_64K)
168                 return 64 * 1024;
169         else if (type == MEMORY_SIZE_128K)
170                 return 128 * 1024;
171         else if (type == MEMORY_SIZE_512K)
172                 return 512 * 1024;
173         else
174                 return 0;
175 }
176
177 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
178                     struct libusb_device_descriptor *des)
179 {
180         struct context *ctx;
181         unsigned int i;
182         int ret;
183
184         /* Note: sdi is non-NULL, the caller already checked this. */
185
186         if (!(ctx = (*sdi)->priv)) {
187                 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
188                 return -1;
189         }
190
191         if ((ret = libusb_get_device_descriptor(dev, des))) {
192                 sr_err("zp: failed to get device descriptor: %d", ret);
193                 return -1;
194         }
195
196         if (des->idVendor != USB_VENDOR)
197                 return 0;
198
199         if (libusb_get_bus_number(dev) == ctx->usb->bus
200             && libusb_get_device_address(dev) == ctx->usb->address) {
201
202                 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
203                         if (!(des->idProduct == zeroplus_models[i].pid))
204                                 continue;
205
206                         sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
207                                 des->idProduct, zeroplus_models[i].model_name);
208                         ctx->num_channels = zeroplus_models[i].channels;
209                         ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
210                         break;
211                 }
212
213                 if (ctx->num_channels == 0) {
214                         sr_err("zp: Unknown ZEROPLUS device 0x%04x",
215                                des->idProduct);
216                         return -2;
217                 }
218
219                 /* Found it. */
220                 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
221                         (*sdi)->status = SR_ST_ACTIVE;
222                         sr_info("zp: opened device %d on %d.%d interface %d",
223                                 (*sdi)->index, ctx->usb->bus,
224                                 ctx->usb->address, USB_INTERFACE);
225                 } else {
226                         sr_err("zp: failed to open device: %d", ret);
227                         *sdi = NULL;
228                 }
229         }
230
231         return 0;
232 }
233
234 static struct sr_dev_inst *zp_open_dev(int dev_index)
235 {
236         struct sr_dev_inst *sdi;
237         libusb_device **devlist;
238         struct libusb_device_descriptor des;
239         int i;
240
241         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index)))
242                 return NULL;
243
244         libusb_get_device_list(usb_context, &devlist);
245         if (sdi->status == SR_ST_INACTIVE) {
246                 /* Find the device by vendor, product, bus and address. */
247                 libusb_get_device_list(usb_context, &devlist);
248                 for (i = 0; devlist[i]; i++) {
249                         /* TODO: Error handling. */
250                         opendev4(&sdi, devlist[i], &des);
251                 }
252         } else {
253                 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
254                 sdi = NULL;
255         }
256         libusb_free_device_list(devlist, 1);
257
258         if (sdi && sdi->status != SR_ST_ACTIVE)
259                 sdi = NULL;
260
261         return sdi;
262 }
263
264 static void close_dev(struct sr_dev_inst *sdi)
265 {
266         struct context *ctx;
267
268         if (!(ctx = sdi->priv)) {
269                 sr_err("zp: %s: sdi->priv was NULL", __func__);
270                 return; /* FIXME */
271         }
272
273         if (!ctx->usb->devhdl)
274                 return;
275
276         sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
277                 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
278         libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
279         libusb_reset_device(ctx->usb->devhdl);
280         libusb_close(ctx->usb->devhdl);
281         ctx->usb->devhdl = NULL;
282         /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
283         sdi->status = SR_ST_INACTIVE;
284 }
285
286 static int configure_probes(struct sr_dev_inst *sdi, const GSList *probes)
287 {
288         struct context *ctx;
289         const struct sr_probe *probe;
290         const GSList *l;
291         int probe_bit, stage, i;
292         char *tc;
293
294         /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
295         ctx = sdi->priv;
296
297         ctx->probe_mask = 0;
298         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
299                 ctx->trigger_mask[i] = 0;
300                 ctx->trigger_value[i] = 0;
301         }
302
303         stage = -1;
304         for (l = probes; l; l = l->next) {
305                 probe = (struct sr_probe *)l->data;
306                 if (probe->enabled == FALSE)
307                         continue;
308                 probe_bit = 1 << (probe->index - 1);
309                 ctx->probe_mask |= probe_bit;
310
311                 if (probe->trigger) {
312                         stage = 0;
313                         for (tc = probe->trigger; *tc; tc++) {
314                                 ctx->trigger_mask[stage] |= probe_bit;
315                                 if (*tc == '1')
316                                         ctx->trigger_value[stage] |= probe_bit;
317                                 stage++;
318                                 if (stage > NUM_TRIGGER_STAGES)
319                                         return SR_ERR;
320                         }
321                 }
322         }
323
324         return SR_OK;
325 }
326
327 /*
328  * API callbacks
329  */
330
331 static int hw_init(void)
332 {
333
334         /* Nothing to do. */
335
336         return SR_OK;
337 }
338
339 static int hw_scan(void)
340 {
341         struct sr_dev_inst *sdi;
342         struct libusb_device_descriptor des;
343         libusb_device **devlist;
344         int ret, devcnt, i;
345         struct context *ctx;
346
347         /* Allocate memory for our private driver context. */
348         if (!(ctx = g_try_malloc(sizeof(struct context)))) {
349                 sr_err("zp: %s: ctx malloc failed", __func__);
350                 return 0;
351         }
352
353         /* Set some sane defaults. */
354         ctx->cur_samplerate = 0;
355         ctx->limit_samples = 0;
356         /* TODO: num_channels isn't initialized before it's needed :( */
357         ctx->num_channels = NUM_PROBES;
358         ctx->memory_size = 0;
359         ctx->probe_mask = 0;
360         memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
361         memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
362         // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
363
364         if (libusb_init(&usb_context) != 0) {
365                 sr_err("zp: Failed to initialize USB.");
366                 return 0;
367         }
368
369         /* Find all ZEROPLUS analyzers and add them to device list. */
370         devcnt = 0;
371         libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
372
373         for (i = 0; devlist[i]; i++) {
374                 ret = libusb_get_device_descriptor(devlist[i], &des);
375                 if (ret != 0) {
376                         sr_err("zp: failed to get device descriptor: %d", ret);
377                         continue;
378                 }
379
380                 if (des.idVendor == USB_VENDOR) {
381                         /*
382                          * Definitely a ZEROPLUS.
383                          * TODO: Any way to detect specific model/version in
384                          * the ZEROPLUS range?
385                          */
386                         /* Register the device with libsigrok. */
387                         if (!(sdi = sr_dev_inst_new(devcnt,
388                                         SR_ST_INACTIVE, VENDOR_NAME,
389                                         MODEL_NAME, MODEL_VERSION))) {
390                                 sr_err("zp: %s: sr_dev_inst_new failed",
391                                        __func__);
392                                 return 0;
393                         }
394
395                         sdi->priv = ctx;
396
397                         zdi->instances = g_slist_append(zdi->instances, sdi);
398                         ctx->usb = sr_usb_dev_inst_new(
399                                 libusb_get_bus_number(devlist[i]),
400                                 libusb_get_device_address(devlist[i]), NULL);
401                         devcnt++;
402                 }
403         }
404         libusb_free_device_list(devlist, 1);
405
406         return devcnt;
407 }
408
409 static int hw_dev_open(int dev_index)
410 {
411         struct sr_dev_inst *sdi;
412         struct context *ctx;
413         int ret;
414
415         if (!(sdi = zp_open_dev(dev_index))) {
416                 sr_err("zp: unable to open device");
417                 return SR_ERR;
418         }
419
420         /* TODO: Note: sdi is retrieved in zp_open_dev(). */
421
422         if (!(ctx = sdi->priv)) {
423                 sr_err("zp: %s: sdi->priv was NULL", __func__);
424                 return SR_ERR_ARG;
425         }
426
427         ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
428         if (ret < 0) {
429                 sr_err("zp: Unable to set USB configuration %d: %d",
430                        USB_CONFIGURATION, ret);
431                 return SR_ERR;
432         }
433
434         ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
435         if (ret != 0) {
436                 sr_err("zp: Unable to claim interface: %d", ret);
437                 return SR_ERR;
438         }
439
440         analyzer_reset(ctx->usb->devhdl);
441         analyzer_initialize(ctx->usb->devhdl);
442
443         analyzer_set_memory_size(MEMORY_SIZE_512K);
444         // analyzer_set_freq(g_freq, g_freq_scale);
445         analyzer_set_trigger_count(1);
446         // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
447         // * get_memory_size(g_memory_size)) / 100) >> 2);
448         analyzer_set_ramsize_trigger_address(
449                 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
450
451 #if 0
452         if (g_double_mode == 1)
453                 analyzer_set_compression(COMPRESSION_DOUBLE);
454         else if (g_compression == 1)
455                 analyzer_set_compression(COMPRESSION_ENABLE);
456         else
457 #endif
458         analyzer_set_compression(COMPRESSION_NONE);
459
460         if (ctx->cur_samplerate == 0) {
461                 /* Samplerate hasn't been set. Default to the slowest one. */
462                 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
463                      &samplerates.list[0]) == SR_ERR)
464                         return SR_ERR;
465         }
466
467         return SR_OK;
468 }
469
470 static int hw_dev_close(int dev_index)
471 {
472         struct sr_dev_inst *sdi;
473
474         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
475                 sr_err("zp: %s: sdi was NULL", __func__);
476                 return SR_ERR; /* TODO: SR_ERR_ARG? */
477         }
478
479         /* TODO */
480         close_dev(sdi);
481
482         return SR_OK;
483 }
484
485 static int hw_cleanup(void)
486 {
487         GSList *l;
488         struct sr_dev_inst *sdi;
489
490         for (l = zdi->instances; l; l = l->next) {
491                 sdi = l->data;
492                 /* Properly close all devices... */
493                 close_dev(sdi);
494                 /* ...and free all their memory. */
495                 sr_dev_inst_free(sdi);
496         }
497         g_slist_free(zdi->instances);
498         zdi->instances = NULL;
499
500         if (usb_context)
501                 libusb_exit(usb_context);
502         usb_context = NULL;
503
504         return SR_OK;
505 }
506
507 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
508 {
509         struct sr_dev_inst *sdi;
510         struct context *ctx;
511         const void *info;
512
513         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
514                 sr_err("zp: %s: sdi was NULL", __func__);
515                 return NULL;
516         }
517
518         if (!(ctx = sdi->priv)) {
519                 sr_err("zp: %s: sdi->priv was NULL", __func__);
520                 return NULL;
521         }
522
523         sr_spew("zp: %s: dev_index %d, dev_info_id %d.", __func__,
524                 dev_index, dev_info_id);
525
526         switch (dev_info_id) {
527         case SR_DI_INST:
528                 info = sdi;
529                 sr_spew("zp: %s: Returning sdi.", __func__);
530                 break;
531         case SR_DI_NUM_PROBES:
532                 info = GINT_TO_POINTER(ctx->num_channels);
533                 sr_spew("zp: %s: Returning number of probes: %d.", __func__,
534                         NUM_PROBES);
535                 break;
536         case SR_DI_PROBE_NAMES:
537                 info = probe_names;
538                 sr_spew("zp: %s: Returning probenames.", __func__);
539                 break;
540         case SR_DI_SAMPLERATES:
541                 info = &samplerates;
542                 sr_spew("zp: %s: Returning samplerates.", __func__);
543                 break;
544         case SR_DI_TRIGGER_TYPES:
545                 info = TRIGGER_TYPES;
546                 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, info);
547                 break;
548         case SR_DI_CUR_SAMPLERATE:
549                 info = &ctx->cur_samplerate;
550                 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
551                         __func__, ctx->cur_samplerate);
552                 break;
553         default:
554                 /* Unknown device info ID, return NULL. */
555                 sr_err("zp: %s: Unknown device info ID", __func__);
556                 info = NULL;
557                 break;
558         }
559
560         return info;
561 }
562
563 static int hw_dev_status_get(int dev_index)
564 {
565         struct sr_dev_inst *sdi;
566
567         sdi = sr_dev_inst_get(zdi->instances, dev_index);
568         if (sdi)
569                 return sdi->status;
570         else
571                 return SR_ST_NOT_FOUND;
572 }
573
574 static const int *hw_hwcap_get_all(void)
575 {
576         return hwcaps;
577 }
578
579 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
580 {
581         struct context *ctx;
582
583         if (!sdi) {
584                 sr_err("zp: %s: sdi was NULL", __func__);
585                 return SR_ERR_ARG;
586         }
587
588         if (!(ctx = sdi->priv)) {
589                 sr_err("zp: %s: sdi->priv was NULL", __func__);
590                 return SR_ERR_ARG;
591         }
592
593         sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
594
595         if (samplerate > SR_MHZ(1))
596                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
597         else if (samplerate > SR_KHZ(1))
598                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
599         else
600                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
601
602         ctx->cur_samplerate = samplerate;
603
604         return SR_OK;
605 }
606
607 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
608 {
609         struct sr_dev_inst *sdi;
610         struct context *ctx;
611
612         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
613                 sr_err("zp: %s: sdi was NULL", __func__);
614                 return SR_ERR;
615         }
616
617         if (!(ctx = sdi->priv)) {
618                 sr_err("zp: %s: sdi->priv was NULL", __func__);
619                 return SR_ERR_ARG;
620         }
621
622         switch (hwcap) {
623         case SR_HWCAP_SAMPLERATE:
624                 return set_samplerate(sdi, *(const uint64_t *)value);
625         case SR_HWCAP_PROBECONFIG:
626                 return configure_probes(sdi, (const GSList *)value);
627         case SR_HWCAP_LIMIT_SAMPLES:
628                 ctx->limit_samples = *(const uint64_t *)value;
629                 return SR_OK;
630         default:
631                 return SR_ERR;
632         }
633 }
634
635 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
636 {
637         struct sr_dev_inst *sdi;
638         struct sr_datafeed_packet packet;
639         struct sr_datafeed_logic logic;
640         struct sr_datafeed_header header;
641         struct sr_datafeed_meta_logic meta;
642         uint64_t samples_read;
643         int res;
644         unsigned int packet_num;
645         unsigned char *buf;
646         struct context *ctx;
647
648         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
649                 sr_err("zp: %s: sdi was NULL", __func__);
650                 return SR_ERR;
651         }
652
653         if (!(ctx = sdi->priv)) {
654                 sr_err("zp: %s: sdi->priv was NULL", __func__);
655                 return SR_ERR_ARG;
656         }
657
658         /* push configured settings to device */
659         analyzer_configure(ctx->usb->devhdl);
660
661         analyzer_start(ctx->usb->devhdl);
662         sr_info("zp: Waiting for data");
663         analyzer_wait_data(ctx->usb->devhdl);
664
665         sr_info("zp: Stop address    = 0x%x",
666                 analyzer_get_stop_address(ctx->usb->devhdl));
667         sr_info("zp: Now address     = 0x%x",
668                 analyzer_get_now_address(ctx->usb->devhdl));
669         sr_info("zp: Trigger address = 0x%x",
670                 analyzer_get_trigger_address(ctx->usb->devhdl));
671
672         packet.type = SR_DF_HEADER;
673         packet.payload = &header;
674         header.feed_version = 1;
675         gettimeofday(&header.starttime, NULL);
676         sr_session_send(cb_data, &packet);
677
678         /* Send metadata about the SR_DF_LOGIC packets to come. */
679         packet.type = SR_DF_META_LOGIC;
680         packet.payload = &meta;
681         meta.samplerate = ctx->cur_samplerate;
682         meta.num_probes = ctx->num_channels;
683         sr_session_send(cb_data, &packet);
684
685         if (!(buf = g_try_malloc(PACKET_SIZE))) {
686                 sr_err("zp: %s: buf malloc failed", __func__);
687                 return SR_ERR_MALLOC;
688         }
689
690         samples_read = 0;
691         analyzer_read_start(ctx->usb->devhdl);
692         /* Send the incoming transfer to the session bus. */
693         for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
694              packet_num++) {
695                 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
696                 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
697                         PACKET_SIZE, res);
698
699                 packet.type = SR_DF_LOGIC;
700                 packet.payload = &logic;
701                 logic.length = PACKET_SIZE;
702                 logic.unitsize = 4;
703                 logic.data = buf;
704                 sr_session_send(cb_data, &packet);
705                 samples_read += res / 4;
706         }
707         analyzer_read_stop(ctx->usb->devhdl);
708         g_free(buf);
709
710         packet.type = SR_DF_END;
711         sr_session_send(cb_data, &packet);
712
713         return SR_OK;
714 }
715
716 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
717 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
718 {
719         struct sr_datafeed_packet packet;
720         struct sr_dev_inst *sdi;
721         struct context *ctx;
722
723         packet.type = SR_DF_END;
724         sr_session_send(cb_data, &packet);
725
726         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
727                 sr_err("zp: %s: sdi was NULL", __func__);
728                 return SR_ERR_BUG;
729         }
730
731         if (!(ctx = sdi->priv)) {
732                 sr_err("zp: %s: sdi->priv was NULL", __func__);
733                 return SR_ERR_BUG;
734         }
735
736         analyzer_reset(ctx->usb->devhdl);
737         /* TODO: Need to cancel and free any queued up transfers. */
738
739         return SR_OK;
740 }
741
742 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
743         .name = "zeroplus-logic-cube",
744         .longname = "ZEROPLUS Logic Cube LAP-C series",
745         .api_version = 1,
746         .init = hw_init,
747         .cleanup = hw_cleanup,
748         .scan = hw_scan,
749         .dev_open = hw_dev_open,
750         .dev_close = hw_dev_close,
751         .dev_info_get = hw_dev_info_get,
752         .dev_status_get = hw_dev_status_get,
753         .hwcap_get_all = hw_hwcap_get_all,
754         .dev_config_set = hw_dev_config_set,
755         .dev_acquisition_start = hw_dev_acquisition_start,
756         .dev_acquisition_stop = hw_dev_acquisition_stop,
757         .instances = NULL,
758 };