]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/zeroplus.c
32bf026884dcacef44fb3a8960ec966927fbb25b
[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         return ((struct drv_context *)(di->priv))->instances;
386 }
387
388 static int hw_dev_open(struct sr_dev_inst *sdi)
389 {
390         struct dev_context *devc;
391         struct drv_context *drvc = di->priv;
392         libusb_device **devlist, *dev;
393         struct libusb_device_descriptor des;
394         int device_count, ret, i;
395
396         if (!(devc = sdi->priv)) {
397                 sr_err("%s: sdi->priv was NULL", __func__);
398                 return SR_ERR_ARG;
399         }
400
401         device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
402                                               &devlist);
403         if (device_count < 0) {
404                 sr_err("Failed to retrieve device list.");
405                 return SR_ERR;
406         }
407
408         dev = NULL;
409         for (i = 0; i < device_count; i++) {
410                 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
411                         sr_err("Failed to get device descriptor: %s.",
412                                libusb_error_name(ret));
413                         continue;
414                 }
415                 if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
416                     && libusb_get_device_address(devlist[i]) == devc->usb->address) {
417                         dev = devlist[i];
418                         break;
419                 }
420         }
421         if (!dev) {
422                 sr_err("Device on bus %d address %d disappeared!",
423                        devc->usb->bus, devc->usb->address);
424                 return SR_ERR;
425         }
426
427         if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) {
428                 sdi->status = SR_ST_ACTIVE;
429                 sr_info("Opened device %d on %d.%d interface %d.",
430                         sdi->index, devc->usb->bus,
431                         devc->usb->address, USB_INTERFACE);
432         } else {
433                 sr_err("Failed to open device: %s.", libusb_error_name(ret));
434                 return SR_ERR;
435         }
436
437         ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
438         if (ret < 0) {
439                 sr_err("Unable to set USB configuration %d: %s.",
440                        USB_CONFIGURATION, libusb_error_name(ret));
441                 return SR_ERR;
442         }
443
444         ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
445         if (ret != 0) {
446                 sr_err("Unable to claim interface: %s.",
447                        libusb_error_name(ret));
448                 return SR_ERR;
449         }
450
451         /* Set default configuration after power on */
452         if (analyzer_read_status(devc->usb->devhdl) == 0)
453                 analyzer_configure(devc->usb->devhdl);
454
455         analyzer_reset(devc->usb->devhdl);
456         analyzer_initialize(devc->usb->devhdl);
457
458         //analyzer_set_memory_size(MEMORY_SIZE_512K);
459         // analyzer_set_freq(g_freq, g_freq_scale);
460         analyzer_set_trigger_count(1);
461         // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
462         // * get_memory_size(g_memory_size)) / 100) >> 2);
463
464 #if 0
465         if (g_double_mode == 1)
466                 analyzer_set_compression(COMPRESSION_DOUBLE);
467         else if (g_compression == 1)
468                 analyzer_set_compression(COMPRESSION_ENABLE);
469         else
470 #endif
471         analyzer_set_compression(COMPRESSION_NONE);
472
473         if (devc->cur_samplerate == 0) {
474                 /* Samplerate hasn't been set. Default to 1MHz. */
475                 analyzer_set_freq(1, FREQ_SCALE_MHZ);
476                 devc->cur_samplerate = SR_MHZ(1);
477         }
478
479         return SR_OK;
480 }
481
482 static int hw_dev_close(struct sr_dev_inst *sdi)
483 {
484         struct dev_context *devc;
485
486         devc = sdi->priv;
487
488         if (!devc->usb->devhdl)
489                 return SR_ERR;
490
491         sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
492                 devc->usb->bus, devc->usb->address, USB_INTERFACE);
493         libusb_release_interface(devc->usb->devhdl, USB_INTERFACE);
494         libusb_reset_device(devc->usb->devhdl);
495         libusb_close(devc->usb->devhdl);
496         devc->usb->devhdl = NULL;
497         sdi->status = SR_ST_INACTIVE;
498
499         return SR_OK;
500 }
501
502 static int hw_cleanup(void)
503 {
504         struct drv_context *drvc;
505
506         if (!(drvc = di->priv))
507                 return SR_OK;
508
509         clear_instances();
510
511         return SR_OK;
512 }
513
514 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
515 {
516         struct dev_context *devc;
517
518         switch (id) {
519         case SR_CONF_SAMPLERATE:
520                 if (sdi) {
521                         devc = sdi->priv;
522                         *data = &devc->cur_samplerate;
523                         sr_spew("Returning samplerate: %" PRIu64 "Hz.",
524                                 devc->cur_samplerate);
525                 } else
526                         return SR_ERR;
527                 break;
528         default:
529                 return SR_ERR_ARG;
530         }
531
532         return SR_OK;
533 }
534
535 static int set_samplerate(struct dev_context *devc, uint64_t samplerate)
536 {
537         int i;
538
539         for (i = 0; supported_samplerates[i]; i++)
540                 if (samplerate == supported_samplerates[i])
541                         break;
542
543         if (!supported_samplerates[i] || samplerate > devc->max_samplerate) {
544                 sr_err("Unsupported samplerate.");
545                 return SR_ERR_ARG;
546         }
547
548         sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
549
550         if (samplerate >= SR_MHZ(1))
551                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
552         else if (samplerate >= SR_KHZ(1))
553                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
554         else
555                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
556
557         devc->cur_samplerate = samplerate;
558
559         return SR_OK;
560 }
561
562 static int set_limit_samples(struct dev_context *devc, uint64_t samples)
563 {
564         devc->limit_samples = samples;
565
566         if (samples <= 2 * 1024)
567                 devc->memory_size = MEMORY_SIZE_8K;
568         else if (samples <= 16 * 1024)
569                 devc->memory_size = MEMORY_SIZE_64K;
570         else if (samples <= 32 * 1024 ||
571                  devc->max_memory_size <= 32 * 1024)
572                 devc->memory_size = MEMORY_SIZE_128K;
573         else
574                 devc->memory_size = MEMORY_SIZE_512K;
575
576         sr_info("Setting memory size to %dK.",
577                 get_memory_size(devc->memory_size) / 1024);
578
579         analyzer_set_memory_size(devc->memory_size);
580
581         return SR_OK;
582 }
583
584 static int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
585 {
586         if (ratio > 100) {
587                 sr_err("Invalid capture ratio: %" PRIu64 ".", ratio);
588                 return SR_ERR_ARG;
589         }
590
591         devc->capture_ratio = ratio;
592
593         sr_info("Setting capture ratio to %d%%.", devc->capture_ratio);
594
595         return SR_OK;
596 }
597
598 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
599 {
600         struct dev_context *devc;
601
602         if (!sdi) {
603                 sr_err("%s: sdi was NULL", __func__);
604                 return SR_ERR_ARG;
605         }
606
607         if (!(devc = sdi->priv)) {
608                 sr_err("%s: sdi->priv was NULL", __func__);
609                 return SR_ERR_ARG;
610         }
611
612         switch (id) {
613         case SR_CONF_SAMPLERATE:
614                 return set_samplerate(devc, *(const uint64_t *)value);
615         case SR_CONF_LIMIT_SAMPLES:
616                 return set_limit_samples(devc, *(const uint64_t *)value);
617         case SR_CONF_CAPTURE_RATIO:
618                 return set_capture_ratio(devc, *(const uint64_t *)value);
619         default:
620                 return SR_ERR;
621         }
622 }
623
624 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
625 {
626         (void)sdi;
627
628         switch (key) {
629         case SR_CONF_DEVICE_OPTIONS:
630                 *data = hwcaps;
631                 break;
632         case SR_CONF_SAMPLERATE:
633                 *data = &samplerates;
634                 break;
635         case SR_CONF_TRIGGER_TYPE:
636                 *data = TRIGGER_TYPE;
637                 break;
638         default:
639                 return SR_ERR_ARG;
640         }
641
642         return SR_OK;
643 }
644
645 static void set_triggerbar(struct dev_context *devc)
646 {
647         unsigned int ramsize;
648         unsigned int n;
649         unsigned int triggerbar;
650
651         ramsize = get_memory_size(devc->memory_size) / 4;
652         if (devc->trigger) {
653                 n = ramsize;
654                 if (devc->max_memory_size < n)
655                         n = devc->max_memory_size;
656                 if (devc->limit_samples < n)
657                         n = devc->limit_samples;
658                 n = n * devc->capture_ratio / 100;
659                 if (n > ramsize - 8)
660                         triggerbar = ramsize - 8;
661                 else
662                         triggerbar = n;
663         } else {
664                 triggerbar = 0;
665         }
666         analyzer_set_triggerbar_address(triggerbar);
667         analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
668
669         sr_dbg("triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
670         sr_dbg("ramsize_triggerbar_address = %d(0x%x)",
671                ramsize - triggerbar, ramsize - triggerbar);
672 }
673
674 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
675                 void *cb_data)
676 {
677         struct sr_datafeed_packet packet;
678         struct sr_datafeed_logic logic;
679         struct sr_datafeed_header header;
680         //uint64_t samples_read;
681         int res;
682         unsigned int packet_num;
683         unsigned int n;
684         unsigned char *buf;
685         struct dev_context *devc;
686
687         if (!(devc = sdi->priv)) {
688                 sr_err("%s: sdi->priv was NULL", __func__);
689                 return SR_ERR_ARG;
690         }
691
692         if (configure_probes(sdi) != SR_OK) {
693                 sr_err("Failed to configure probes.");
694                 return SR_ERR;
695         }
696
697         set_triggerbar(devc);
698
699         /* push configured settings to device */
700         analyzer_configure(devc->usb->devhdl);
701
702         analyzer_start(devc->usb->devhdl);
703         sr_info("Waiting for data.");
704         analyzer_wait_data(devc->usb->devhdl);
705
706         sr_info("Stop address    = 0x%x.",
707                 analyzer_get_stop_address(devc->usb->devhdl));
708         sr_info("Now address     = 0x%x.",
709                 analyzer_get_now_address(devc->usb->devhdl));
710         sr_info("Trigger address = 0x%x.",
711                 analyzer_get_trigger_address(devc->usb->devhdl));
712
713         packet.type = SR_DF_HEADER;
714         packet.payload = &header;
715         header.feed_version = 1;
716         gettimeofday(&header.starttime, NULL);
717         sr_session_send(cb_data, &packet);
718
719         if (!(buf = g_try_malloc(PACKET_SIZE))) {
720                 sr_err("Packet buffer malloc failed.");
721                 return SR_ERR_MALLOC;
722         }
723
724         //samples_read = 0;
725         analyzer_read_start(devc->usb->devhdl);
726         /* Send the incoming transfer to the session bus. */
727         n = get_memory_size(devc->memory_size);
728         if (devc->max_memory_size * 4 < n)
729                 n = devc->max_memory_size * 4;
730         for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
731                 res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE);
732                 sr_info("Tried to read %d bytes, actually read %d bytes.",
733                         PACKET_SIZE, res);
734
735                 packet.type = SR_DF_LOGIC;
736                 packet.payload = &logic;
737                 logic.length = PACKET_SIZE;
738                 logic.unitsize = 4;
739                 logic.data = buf;
740                 sr_session_send(cb_data, &packet);
741                 //samples_read += res / 4;
742         }
743         analyzer_read_stop(devc->usb->devhdl);
744         g_free(buf);
745
746         packet.type = SR_DF_END;
747         sr_session_send(cb_data, &packet);
748
749         return SR_OK;
750 }
751
752 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
753 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
754 {
755         struct sr_datafeed_packet packet;
756         struct dev_context *devc;
757
758         packet.type = SR_DF_END;
759         sr_session_send(cb_data, &packet);
760
761         if (!(devc = sdi->priv)) {
762                 sr_err("%s: sdi->priv was NULL", __func__);
763                 return SR_ERR_BUG;
764         }
765
766         analyzer_reset(devc->usb->devhdl);
767         /* TODO: Need to cancel and free any queued up transfers. */
768
769         return SR_OK;
770 }
771
772 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
773         .name = "zeroplus-logic-cube",
774         .longname = "ZEROPLUS Logic Cube LAP-C series",
775         .api_version = 1,
776         .init = hw_init,
777         .cleanup = hw_cleanup,
778         .scan = hw_scan,
779         .dev_list = hw_dev_list,
780         .dev_clear = hw_cleanup,
781         .config_get = config_get,
782         .config_set = config_set,
783         .config_list = config_list,
784         .dev_open = hw_dev_open,
785         .dev_close = hw_dev_close,
786         .dev_acquisition_start = hw_dev_acquisition_start,
787         .dev_acquisition_stop = hw_dev_acquisition_stop,
788         .priv = NULL,
789 };