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