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