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