]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
zeroplus-logic-cube: init and scan fixes
[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         /* TODO: this belongs in the device instance */
159         struct sr_usb_dev_inst *usb;
160 };
161
162 static int hw_dev_config_set(int dev_index, int hwcap, const void *value);
163
164 static unsigned int get_memory_size(int type)
165 {
166         if (type == MEMORY_SIZE_8K)
167                 return 8 * 1024;
168         else if (type == MEMORY_SIZE_64K)
169                 return 64 * 1024;
170         else if (type == MEMORY_SIZE_128K)
171                 return 128 * 1024;
172         else if (type == MEMORY_SIZE_512K)
173                 return 512 * 1024;
174         else
175                 return 0;
176 }
177
178 static int opendev4(struct sr_dev_inst **sdi, libusb_device *dev,
179                     struct libusb_device_descriptor *des)
180 {
181         struct context *ctx;
182         unsigned int i;
183         int ret;
184
185         /* Note: sdi is non-NULL, the caller already checked this. */
186
187         if (!(ctx = (*sdi)->priv)) {
188                 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
189                 return -1;
190         }
191
192         if ((ret = libusb_get_device_descriptor(dev, des))) {
193                 sr_err("zp: failed to get device descriptor: %d", ret);
194                 return -1;
195         }
196
197         if (des->idVendor != USB_VENDOR)
198                 return 0;
199
200         if (libusb_get_bus_number(dev) == ctx->usb->bus
201             && libusb_get_device_address(dev) == ctx->usb->address) {
202
203                 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
204                         if (!(des->idProduct == zeroplus_models[i].pid))
205                                 continue;
206
207                         sr_info("zp: Found ZEROPLUS device 0x%04x (%s)",
208                                 des->idProduct, zeroplus_models[i].model_name);
209                         ctx->num_channels = zeroplus_models[i].channels;
210                         ctx->memory_size = zeroplus_models[i].sample_depth * 1024;
211                         break;
212                 }
213
214                 if (ctx->num_channels == 0) {
215                         sr_err("zp: Unknown ZEROPLUS device 0x%04x",
216                                des->idProduct);
217                         return -2;
218                 }
219
220                 /* Found it. */
221                 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
222                         (*sdi)->status = SR_ST_ACTIVE;
223                         sr_info("zp: opened device %d on %d.%d interface %d",
224                                 (*sdi)->index, ctx->usb->bus,
225                                 ctx->usb->address, USB_INTERFACE);
226                 } else {
227                         sr_err("zp: failed to open device: %d", ret);
228                         *sdi = NULL;
229                 }
230         }
231
232         return 0;
233 }
234
235 static struct sr_dev_inst *zp_open_dev(int dev_index)
236 {
237         struct sr_dev_inst *sdi;
238         libusb_device **devlist;
239         struct libusb_device_descriptor des;
240         int i;
241
242         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index)))
243                 return NULL;
244
245         libusb_get_device_list(usb_context, &devlist);
246         if (sdi->status == SR_ST_INACTIVE) {
247                 /* Find the device by vendor, product, bus and address. */
248                 libusb_get_device_list(usb_context, &devlist);
249                 for (i = 0; devlist[i]; i++) {
250                         /* TODO: Error handling. */
251                         opendev4(&sdi, devlist[i], &des);
252                 }
253         } else {
254                 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
255                 sdi = NULL;
256         }
257         libusb_free_device_list(devlist, 1);
258
259         if (sdi && sdi->status != SR_ST_ACTIVE)
260                 sdi = NULL;
261
262         return sdi;
263 }
264
265 static void close_dev(struct sr_dev_inst *sdi)
266 {
267         struct context *ctx;
268
269         if (!(ctx = sdi->priv)) {
270                 sr_err("zp: %s: sdi->priv was NULL", __func__);
271                 return; /* FIXME */
272         }
273
274         if (!ctx->usb->devhdl)
275                 return;
276
277         sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
278                 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
279         libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
280         libusb_reset_device(ctx->usb->devhdl);
281         libusb_close(ctx->usb->devhdl);
282         ctx->usb->devhdl = NULL;
283         /* TODO: Call libusb_exit() here or only in hw_cleanup()? */
284         sdi->status = SR_ST_INACTIVE;
285 }
286
287 static int configure_probes(struct sr_dev_inst *sdi, const GSList *probes)
288 {
289         struct context *ctx;
290         const struct sr_probe *probe;
291         const GSList *l;
292         int probe_bit, stage, i;
293         char *tc;
294
295         /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
296         ctx = sdi->priv;
297
298         ctx->probe_mask = 0;
299         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
300                 ctx->trigger_mask[i] = 0;
301                 ctx->trigger_value[i] = 0;
302         }
303
304         stage = -1;
305         for (l = probes; l; l = l->next) {
306                 probe = (struct sr_probe *)l->data;
307                 if (probe->enabled == FALSE)
308                         continue;
309                 probe_bit = 1 << (probe->index - 1);
310                 ctx->probe_mask |= probe_bit;
311
312                 if (probe->trigger) {
313                         stage = 0;
314                         for (tc = probe->trigger; *tc; tc++) {
315                                 ctx->trigger_mask[stage] |= probe_bit;
316                                 if (*tc == '1')
317                                         ctx->trigger_value[stage] |= probe_bit;
318                                 stage++;
319                                 if (stage > NUM_TRIGGER_STAGES)
320                                         return SR_ERR;
321                         }
322                 }
323         }
324
325         return SR_OK;
326 }
327
328 static void clear_instances(void)
329 {
330         GSList *l;
331         struct sr_dev_inst *sdi;
332
333         for (l = zdi->instances; l; l = l->next) {
334                 sdi = l->data;
335                 /* Properly close all devices... */
336                 close_dev(sdi);
337                 /* ...and free all their memory. */
338                 sr_dev_inst_free(sdi);
339         }
340         g_slist_free(zdi->instances);
341         zdi->instances = NULL;
342
343 }
344
345 /*
346  * API callbacks
347  */
348
349 static int hw_init(void)
350 {
351
352         if (libusb_init(&usb_context) != 0) {
353                 sr_err("zp: Failed to initialize USB.");
354                 return 0;
355         }
356
357         return SR_OK;
358 }
359
360 static GSList *hw_scan(GSList *options)
361 {
362         struct sr_dev_inst *sdi;
363         struct libusb_device_descriptor des;
364         GSList *devices;
365         libusb_device **devlist;
366         int ret, devcnt, i;
367         struct context *ctx;
368
369         (void)options;
370         devices = NULL;
371
372         clear_instances();
373
374         /* Allocate memory for our private driver context. */
375         if (!(ctx = g_try_malloc(sizeof(struct context)))) {
376                 sr_err("zp: %s: ctx malloc failed", __func__);
377                 return 0;
378         }
379
380         /* Set some sane defaults. */
381         ctx->cur_samplerate = 0;
382         ctx->limit_samples = 0;
383         /* TODO: num_channels isn't initialized before it's needed :( */
384         ctx->num_channels = NUM_PROBES;
385         ctx->memory_size = 0;
386         ctx->probe_mask = 0;
387         memset(ctx->trigger_mask, 0, NUM_TRIGGER_STAGES);
388         memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES);
389         // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
390
391         /* Find all ZEROPLUS analyzers and add them to device list. */
392         devcnt = 0;
393         libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
394
395         for (i = 0; devlist[i]; i++) {
396                 ret = libusb_get_device_descriptor(devlist[i], &des);
397                 if (ret != 0) {
398                         sr_err("zp: failed to get device descriptor: %d", ret);
399                         continue;
400                 }
401
402                 if (des.idVendor == USB_VENDOR) {
403                         /*
404                          * Definitely a ZEROPLUS.
405                          * TODO: Any way to detect specific model/version in
406                          * the ZEROPLUS range?
407                          */
408                         /* Register the device with libsigrok. */
409                         if (!(sdi = sr_dev_inst_new(devcnt,
410                                         SR_ST_INACTIVE, VENDOR_NAME,
411                                         MODEL_NAME, MODEL_VERSION))) {
412                                 sr_err("zp: %s: sr_dev_inst_new failed",
413                                        __func__);
414                                 return 0;
415                         }
416
417                         sdi->priv = ctx;
418
419                         devices = g_slist_append(devices, sdi);
420                         zdi->instances = g_slist_append(zdi->instances, sdi);
421                         ctx->usb = sr_usb_dev_inst_new(
422                                 libusb_get_bus_number(devlist[i]),
423                                 libusb_get_device_address(devlist[i]), NULL);
424                         devcnt++;
425                 }
426         }
427         libusb_free_device_list(devlist, 1);
428
429         return devices;
430 }
431
432 static int hw_dev_open(int dev_index)
433 {
434         struct sr_dev_inst *sdi;
435         struct context *ctx;
436         int ret;
437
438         if (!(sdi = zp_open_dev(dev_index))) {
439                 sr_err("zp: unable to open device");
440                 return SR_ERR;
441         }
442
443         /* TODO: Note: sdi is retrieved in zp_open_dev(). */
444
445         if (!(ctx = sdi->priv)) {
446                 sr_err("zp: %s: sdi->priv was NULL", __func__);
447                 return SR_ERR_ARG;
448         }
449
450         ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
451         if (ret < 0) {
452                 sr_err("zp: Unable to set USB configuration %d: %d",
453                        USB_CONFIGURATION, ret);
454                 return SR_ERR;
455         }
456
457         ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
458         if (ret != 0) {
459                 sr_err("zp: Unable to claim interface: %d", ret);
460                 return SR_ERR;
461         }
462
463         analyzer_reset(ctx->usb->devhdl);
464         analyzer_initialize(ctx->usb->devhdl);
465
466         analyzer_set_memory_size(MEMORY_SIZE_512K);
467         // analyzer_set_freq(g_freq, g_freq_scale);
468         analyzer_set_trigger_count(1);
469         // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
470         // * get_memory_size(g_memory_size)) / 100) >> 2);
471         analyzer_set_ramsize_trigger_address(
472                 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
473
474 #if 0
475         if (g_double_mode == 1)
476                 analyzer_set_compression(COMPRESSION_DOUBLE);
477         else if (g_compression == 1)
478                 analyzer_set_compression(COMPRESSION_ENABLE);
479         else
480 #endif
481         analyzer_set_compression(COMPRESSION_NONE);
482
483         if (ctx->cur_samplerate == 0) {
484                 /* Samplerate hasn't been set. Default to the slowest one. */
485                 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
486                      &samplerates.list[0]) == SR_ERR)
487                         return SR_ERR;
488         }
489
490         return SR_OK;
491 }
492
493 static int hw_dev_close(int dev_index)
494 {
495         struct sr_dev_inst *sdi;
496
497         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
498                 sr_err("zp: %s: sdi was NULL", __func__);
499                 return SR_ERR; /* TODO: SR_ERR_ARG? */
500         }
501
502         /* TODO */
503         close_dev(sdi);
504
505         return SR_OK;
506 }
507
508 static int hw_cleanup(void)
509 {
510
511         clear_instances();
512
513         if (usb_context)
514                 libusb_exit(usb_context);
515         usb_context = NULL;
516
517         return SR_OK;
518 }
519
520 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
521 {
522         struct sr_dev_inst *sdi;
523         struct context *ctx;
524         const void *info;
525
526         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
527                 sr_err("zp: %s: sdi was NULL", __func__);
528                 return NULL;
529         }
530
531         if (!(ctx = sdi->priv)) {
532                 sr_err("zp: %s: sdi->priv was NULL", __func__);
533                 return NULL;
534         }
535
536         sr_spew("zp: %s: dev_index %d, dev_info_id %d.", __func__,
537                 dev_index, dev_info_id);
538
539         switch (dev_info_id) {
540         case SR_DI_INST:
541                 info = sdi;
542                 sr_spew("zp: %s: Returning sdi.", __func__);
543                 break;
544         case SR_DI_NUM_PROBES:
545                 info = GINT_TO_POINTER(ctx->num_channels);
546                 sr_spew("zp: %s: Returning number of probes: %d.", __func__,
547                         NUM_PROBES);
548                 break;
549         case SR_DI_PROBE_NAMES:
550                 info = probe_names;
551                 sr_spew("zp: %s: Returning probenames.", __func__);
552                 break;
553         case SR_DI_SAMPLERATES:
554                 info = &samplerates;
555                 sr_spew("zp: %s: Returning samplerates.", __func__);
556                 break;
557         case SR_DI_TRIGGER_TYPES:
558                 info = TRIGGER_TYPES;
559                 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, info);
560                 break;
561         case SR_DI_CUR_SAMPLERATE:
562                 info = &ctx->cur_samplerate;
563                 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
564                         __func__, ctx->cur_samplerate);
565                 break;
566         default:
567                 /* Unknown device info ID, return NULL. */
568                 sr_err("zp: %s: Unknown device info ID", __func__);
569                 info = NULL;
570                 break;
571         }
572
573         return info;
574 }
575
576 static int hw_dev_status_get(int dev_index)
577 {
578         struct sr_dev_inst *sdi;
579
580         sdi = sr_dev_inst_get(zdi->instances, dev_index);
581         if (sdi)
582                 return sdi->status;
583         else
584                 return SR_ST_NOT_FOUND;
585 }
586
587 static const int *hw_hwcap_get_all(void)
588 {
589         return hwcaps;
590 }
591
592 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
593 {
594         struct context *ctx;
595
596         if (!sdi) {
597                 sr_err("zp: %s: sdi was NULL", __func__);
598                 return SR_ERR_ARG;
599         }
600
601         if (!(ctx = sdi->priv)) {
602                 sr_err("zp: %s: sdi->priv was NULL", __func__);
603                 return SR_ERR_ARG;
604         }
605
606         sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
607
608         if (samplerate > SR_MHZ(1))
609                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
610         else if (samplerate > SR_KHZ(1))
611                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
612         else
613                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
614
615         ctx->cur_samplerate = samplerate;
616
617         return SR_OK;
618 }
619
620 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
621 {
622         struct sr_dev_inst *sdi;
623         struct context *ctx;
624
625         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
626                 sr_err("zp: %s: sdi was NULL", __func__);
627                 return SR_ERR;
628         }
629
630         if (!(ctx = sdi->priv)) {
631                 sr_err("zp: %s: sdi->priv was NULL", __func__);
632                 return SR_ERR_ARG;
633         }
634
635         switch (hwcap) {
636         case SR_HWCAP_SAMPLERATE:
637                 return set_samplerate(sdi, *(const uint64_t *)value);
638         case SR_HWCAP_PROBECONFIG:
639                 return configure_probes(sdi, (const GSList *)value);
640         case SR_HWCAP_LIMIT_SAMPLES:
641                 ctx->limit_samples = *(const uint64_t *)value;
642                 return SR_OK;
643         default:
644                 return SR_ERR;
645         }
646 }
647
648 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
649 {
650         struct sr_dev_inst *sdi;
651         struct sr_datafeed_packet packet;
652         struct sr_datafeed_logic logic;
653         struct sr_datafeed_header header;
654         struct sr_datafeed_meta_logic meta;
655         uint64_t samples_read;
656         int res;
657         unsigned int packet_num;
658         unsigned char *buf;
659         struct context *ctx;
660
661         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
662                 sr_err("zp: %s: sdi was NULL", __func__);
663                 return SR_ERR;
664         }
665
666         if (!(ctx = sdi->priv)) {
667                 sr_err("zp: %s: sdi->priv was NULL", __func__);
668                 return SR_ERR_ARG;
669         }
670
671         /* push configured settings to device */
672         analyzer_configure(ctx->usb->devhdl);
673
674         analyzer_start(ctx->usb->devhdl);
675         sr_info("zp: Waiting for data");
676         analyzer_wait_data(ctx->usb->devhdl);
677
678         sr_info("zp: Stop address    = 0x%x",
679                 analyzer_get_stop_address(ctx->usb->devhdl));
680         sr_info("zp: Now address     = 0x%x",
681                 analyzer_get_now_address(ctx->usb->devhdl));
682         sr_info("zp: Trigger address = 0x%x",
683                 analyzer_get_trigger_address(ctx->usb->devhdl));
684
685         packet.type = SR_DF_HEADER;
686         packet.payload = &header;
687         header.feed_version = 1;
688         gettimeofday(&header.starttime, NULL);
689         sr_session_send(cb_data, &packet);
690
691         /* Send metadata about the SR_DF_LOGIC packets to come. */
692         packet.type = SR_DF_META_LOGIC;
693         packet.payload = &meta;
694         meta.samplerate = ctx->cur_samplerate;
695         meta.num_probes = ctx->num_channels;
696         sr_session_send(cb_data, &packet);
697
698         if (!(buf = g_try_malloc(PACKET_SIZE))) {
699                 sr_err("zp: %s: buf malloc failed", __func__);
700                 return SR_ERR_MALLOC;
701         }
702
703         samples_read = 0;
704         analyzer_read_start(ctx->usb->devhdl);
705         /* Send the incoming transfer to the session bus. */
706         for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
707              packet_num++) {
708                 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
709                 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
710                         PACKET_SIZE, res);
711
712                 packet.type = SR_DF_LOGIC;
713                 packet.payload = &logic;
714                 logic.length = PACKET_SIZE;
715                 logic.unitsize = 4;
716                 logic.data = buf;
717                 sr_session_send(cb_data, &packet);
718                 samples_read += res / 4;
719         }
720         analyzer_read_stop(ctx->usb->devhdl);
721         g_free(buf);
722
723         packet.type = SR_DF_END;
724         sr_session_send(cb_data, &packet);
725
726         return SR_OK;
727 }
728
729 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
730 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
731 {
732         struct sr_datafeed_packet packet;
733         struct sr_dev_inst *sdi;
734         struct context *ctx;
735
736         packet.type = SR_DF_END;
737         sr_session_send(cb_data, &packet);
738
739         if (!(sdi = sr_dev_inst_get(zdi->instances, dev_index))) {
740                 sr_err("zp: %s: sdi was NULL", __func__);
741                 return SR_ERR_BUG;
742         }
743
744         if (!(ctx = sdi->priv)) {
745                 sr_err("zp: %s: sdi->priv was NULL", __func__);
746                 return SR_ERR_BUG;
747         }
748
749         analyzer_reset(ctx->usb->devhdl);
750         /* TODO: Need to cancel and free any queued up transfers. */
751
752         return SR_OK;
753 }
754
755 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
756         .name = "zeroplus-logic-cube",
757         .longname = "ZEROPLUS Logic Cube LAP-C series",
758         .api_version = 1,
759         .init = hw_init,
760         .cleanup = hw_cleanup,
761         .scan = hw_scan,
762         .dev_open = hw_dev_open,
763         .dev_close = hw_dev_close,
764         .dev_info_get = hw_dev_info_get,
765         .dev_status_get = hw_dev_status_get,
766         .hwcap_get_all = hw_hwcap_get_all,
767         .dev_config_set = hw_dev_config_set,
768         .dev_acquisition_start = hw_dev_acquisition_start,
769         .dev_acquisition_stop = hw_dev_acquisition_stop,
770         .instances = NULL,
771 };