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