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