]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/zeroplus.c
zeroplus-logic-cube: fix scan to correctly find model
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
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>
c31e9ef4 22#include <string.h>
a1bb33af
UH
23#include <sys/time.h>
24#include <inttypes.h>
25#include <glib.h>
26#include <libusb.h>
c31e9ef4 27#include "config.h"
45c59c8b
BV
28#include "libsigrok.h"
29#include "libsigrok-internal.h"
a1bb33af
UH
30#include "analyzer.h"
31
fed16f06 32#define USB_VENDOR 0x0c12
8fdecced
UH
33
34#define VENDOR_NAME "ZEROPLUS"
35#define MODEL_NAME "Logic Cube LAP-C"
36#define MODEL_VERSION NULL
a1bb33af 37
6752905e 38#define NUM_PROBES 16
a1bb33af
UH
39#define USB_INTERFACE 0
40#define USB_CONFIGURATION 1
41#define NUM_TRIGGER_STAGES 4
42#define TRIGGER_TYPES "01"
43
fed16f06 44#define PACKET_SIZE 2048 /* ?? */
a1bb33af
UH
45
46typedef struct {
428edbe1 47 unsigned short vid;
a1bb33af 48 unsigned short pid;
428edbe1 49 char *model_name;
a1bb33af 50 unsigned int channels;
fed16f06 51 unsigned int sample_depth; /* In Ksamples/channel */
a1bb33af
UH
52 unsigned int max_sampling_freq;
53} model_t;
54
fed16f06
UH
55/*
56 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
57 * same 128K sample depth.
58 */
29cbfeaf 59static model_t zeroplus_models[] = {
428edbe1
BV
60 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
61 {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200},
62 {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200},
63 {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200},
64 {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200},
65 {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100},
66 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
67 { 0, 0, 0, 0, 0, 0 }
a1bb33af
UH
68};
69
915f7cc8 70static const int hwcaps[] = {
5a2326a7
UH
71 SR_HWCAP_LOGIC_ANALYZER,
72 SR_HWCAP_SAMPLERATE,
73 SR_HWCAP_PROBECONFIG,
74 SR_HWCAP_CAPTURE_RATIO,
a1bb33af 75
fed16f06 76 /* These are really implemented in the driver, not the hardware. */
5a2326a7 77 SR_HWCAP_LIMIT_SAMPLES,
fed16f06 78 0,
a1bb33af
UH
79};
80
d261dbbf
UH
81/*
82 * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7.
83 * We currently ignore other untested/unsupported devices here.
84 */
6752905e 85static const char *probe_names[NUM_PROBES + 1] = {
d261dbbf
UH
86 "A0",
87 "A1",
88 "A2",
89 "A3",
90 "A4",
91 "A5",
92 "A6",
93 "A7",
94 "B0",
95 "B1",
96 "B2",
97 "B3",
98 "B4",
99 "B5",
100 "B6",
101 "B7",
464d12c7
KS
102 NULL,
103};
104
e7eb703f 105/* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
32756547
BV
106SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
107static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
a1bb33af
UH
108
109static libusb_context *usb_context = NULL;
110
fed16f06
UH
111/*
112 * The hardware supports more samplerates than these, but these are the
113 * options hardcoded into the vendor's Windows GUI.
114 */
a1bb33af 115
fed16f06
UH
116/*
117 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
118 * that high.
119 */
a533743d 120static const uint64_t supported_samplerates[] = {
c9140419
UH
121 SR_HZ(100),
122 SR_HZ(500),
59df0c77
UH
123 SR_KHZ(1),
124 SR_KHZ(5),
125 SR_KHZ(25),
126 SR_KHZ(50),
127 SR_KHZ(100),
128 SR_KHZ(200),
129 SR_KHZ(400),
130 SR_KHZ(800),
131 SR_MHZ(1),
132 SR_MHZ(10),
133 SR_MHZ(25),
134 SR_MHZ(50),
135 SR_MHZ(80),
136 SR_MHZ(100),
137 SR_MHZ(150),
138 SR_MHZ(200),
fed16f06 139 0,
a1bb33af
UH
140};
141
a533743d 142static const struct sr_samplerates samplerates = {
590b9f9a
UH
143 0,
144 0,
145 0,
fed16f06 146 supported_samplerates,
a1bb33af
UH
147};
148
ea9cfed7
UH
149/* Private, per-device-instance driver context. */
150struct context {
bf43ea23 151 uint64_t cur_samplerate;
bf43ea23
UH
152 uint64_t limit_samples;
153 int num_channels; /* TODO: This isn't initialized before it's needed :( */
154 uint64_t memory_size;
155 uint8_t probe_mask;
156 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
157 uint8_t trigger_value[NUM_TRIGGER_STAGES];
158 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
69890f73 159
7da6f9d5 160 /* TODO: this belongs in the device instance */
d68e2d1a 161 struct sr_usb_dev_inst *usb;
bf43ea23 162};
a1bb33af 163
6f4b1868
BV
164static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
165 const void *value);
25a0f108 166static int hw_dev_close(struct sr_dev_inst *sdi);
a1bb33af
UH
167
168static unsigned int get_memory_size(int type)
169{
fed16f06
UH
170 if (type == MEMORY_SIZE_8K)
171 return 8 * 1024;
172 else if (type == MEMORY_SIZE_64K)
173 return 64 * 1024;
174 else if (type == MEMORY_SIZE_128K)
175 return 128 * 1024;
176 else if (type == MEMORY_SIZE_512K)
177 return 512 * 1024;
178 else
179 return 0;
a1bb33af
UH
180}
181
6f4b1868 182static int configure_probes(const struct sr_dev_inst *sdi, const GSList *probes)
a1bb33af 183{
ea9cfed7 184 struct context *ctx;
1b79df2f
JH
185 const struct sr_probe *probe;
186 const GSList *l;
a1bb33af
UH
187 int probe_bit, stage, i;
188 char *tc;
189
bf43ea23 190 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
ea9cfed7 191 ctx = sdi->priv;
bf43ea23 192
ea9cfed7 193 ctx->probe_mask = 0;
fed16f06 194 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
ea9cfed7
UH
195 ctx->trigger_mask[i] = 0;
196 ctx->trigger_value[i] = 0;
a1bb33af
UH
197 }
198
199 stage = -1;
fed16f06 200 for (l = probes; l; l = l->next) {
1afe8989 201 probe = (struct sr_probe *)l->data;
fed16f06 202 if (probe->enabled == FALSE)
a1bb33af
UH
203 continue;
204 probe_bit = 1 << (probe->index - 1);
ea9cfed7 205 ctx->probe_mask |= probe_bit;
fed16f06
UH
206
207 if (probe->trigger) {
a1bb33af 208 stage = 0;
fed16f06 209 for (tc = probe->trigger; *tc; tc++) {
ea9cfed7 210 ctx->trigger_mask[stage] |= probe_bit;
fed16f06 211 if (*tc == '1')
ea9cfed7 212 ctx->trigger_value[stage] |= probe_bit;
a1bb33af 213 stage++;
fed16f06 214 if (stage > NUM_TRIGGER_STAGES)
e46b8fb1 215 return SR_ERR;
a1bb33af
UH
216 }
217 }
218 }
219
e46b8fb1 220 return SR_OK;
a1bb33af
UH
221}
222
7da6f9d5
BV
223static void clear_instances(void)
224{
225 GSList *l;
226 struct sr_dev_inst *sdi;
227
228 for (l = zdi->instances; l; l = l->next) {
229 sdi = l->data;
230 /* Properly close all devices... */
25a0f108 231 hw_dev_close(sdi);
7da6f9d5
BV
232 /* ...and free all their memory. */
233 sr_dev_inst_free(sdi);
234 }
235 g_slist_free(zdi->instances);
236 zdi->instances = NULL;
237
238}
239
a1bb33af
UH
240/*
241 * API callbacks
242 */
243
40dda2c3 244static int hw_init(void)
61136ea6
BV
245{
246
7da6f9d5
BV
247 if (libusb_init(&usb_context) != 0) {
248 sr_err("zp: Failed to initialize USB.");
249 return 0;
250 }
61136ea6
BV
251
252 return SR_OK;
253}
254
4ca38984 255static GSList *hw_scan(GSList *options)
a1bb33af 256{
d68e2d1a 257 struct sr_dev_inst *sdi;
428edbe1
BV
258 struct sr_probe *probe;
259 struct context *ctx;
260 model_t *prof;
a1bb33af
UH
261 struct libusb_device_descriptor des;
262 libusb_device **devlist;
428edbe1
BV
263 GSList *devices;
264 int ret, devcnt, i, j;
a1bb33af 265
4ca38984
BV
266 (void)options;
267 devices = NULL;
268
7da6f9d5
BV
269 clear_instances();
270
8fdecced 271 /* Find all ZEROPLUS analyzers and add them to device list. */
a1bb33af 272 devcnt = 0;
185ae2c5 273 libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
fed16f06
UH
274
275 for (i = 0; devlist[i]; i++) {
ebc34738
UH
276 ret = libusb_get_device_descriptor(devlist[i], &des);
277 if (ret != 0) {
278 sr_err("zp: failed to get device descriptor: %d", ret);
a1bb33af
UH
279 continue;
280 }
281
428edbe1
BV
282 prof = NULL;
283 for (j = 0; j < zeroplus_models[j].vid; j++) {
284 if (des.idVendor == zeroplus_models[j].vid &&
285 des.idProduct == zeroplus_models[j].pid) {
286 prof = &zeroplus_models[j];
bf43ea23 287 }
a1bb33af 288 }
428edbe1
BV
289 /* Skip if the device was not found */
290 if (!prof)
291 continue;
292 sr_info("zp: Found ZEROPLUS model %s", prof->model_name);
293
294 /* Register the device with libsigrok. */
295 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
296 VENDOR_NAME, prof->model_name, NULL))) {
297 sr_err("zp: %s: sr_dev_inst_new failed", __func__);
298 return NULL;
299 }
300 sdi->driver = zdi;
301
302 /* Allocate memory for our private driver context. */
303 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
304 sr_err("zp: %s: ctx malloc failed", __func__);
305 return 0;
306 }
307 sdi->priv = ctx;
308 ctx->num_channels = prof->channels;
309 ctx->memory_size = prof->sample_depth * 1024;
310 // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES);
311
312 /* Fill in probelist according to this device's profile. */
313 for (j = 0; j < ctx->num_channels; j++) {
314 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
315 probe_names[j])))
316 return NULL;
317 sdi->probes = g_slist_append(sdi->probes, probe);
318 }
319
320 devices = g_slist_append(devices, sdi);
321 zdi->instances = g_slist_append(zdi->instances, sdi);
322 ctx->usb = sr_usb_dev_inst_new(
323 libusb_get_bus_number(devlist[i]),
324 libusb_get_device_address(devlist[i]), NULL);
325 devcnt++;
326
a1bb33af
UH
327 }
328 libusb_free_device_list(devlist, 1);
329
4ca38984 330 return devices;
a1bb33af
UH
331}
332
25a0f108 333static int hw_dev_open(struct sr_dev_inst *sdi)
a1bb33af 334{
ea9cfed7 335 struct context *ctx;
428edbe1
BV
336 libusb_device **devlist, *dev;
337 struct libusb_device_descriptor des;
338 int device_count, ret, i;
a1bb33af 339
ea9cfed7 340 if (!(ctx = sdi->priv)) {
bf43ea23
UH
341 sr_err("zp: %s: sdi->priv was NULL", __func__);
342 return SR_ERR_ARG;
343 }
344
428edbe1
BV
345 device_count = libusb_get_device_list(usb_context, &devlist);
346 if (device_count < 0) {
347 sr_err("zp: Failed to retrieve device list");
348 return SR_ERR;
349 }
350
351 dev = NULL;
352 for (i = 0; i < device_count; i++) {
353 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
354 sr_err("fx2lafw: Failed to get device descriptor: %d.",
355 ret);
356 continue;
357 }
358 if (libusb_get_bus_number(devlist[i]) == ctx->usb->bus
359 && libusb_get_device_address(devlist[i]) == ctx->usb->address) {
360 dev = devlist[i];
361 break;
362 }
363 }
364 if (!dev) {
365 sr_err("device on bus %d address %d disappeared!",
366 ctx->usb->bus, ctx->usb->address);
367 return SR_ERR;
368 }
369
370 if (!(ret = libusb_open(dev, &(ctx->usb->devhdl)))) {
371 sdi->status = SR_ST_ACTIVE;
372 sr_info("zp: opened device %d on %d.%d interface %d",
373 sdi->index, ctx->usb->bus,
374 ctx->usb->address, USB_INTERFACE);
375 } else {
376 sr_err("zp: failed to open device: %d", ret);
377 return SR_ERR;
378 }
379
ebc34738
UH
380 ret = libusb_set_configuration(ctx->usb->devhdl, USB_CONFIGURATION);
381 if (ret < 0) {
133a37bf 382 sr_err("zp: Unable to set USB configuration %d: %d",
ebc34738 383 USB_CONFIGURATION, ret);
185ae2c5
UH
384 return SR_ERR;
385 }
386
ebc34738
UH
387 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
388 if (ret != 0) {
389 sr_err("zp: Unable to claim interface: %d", ret);
e46b8fb1 390 return SR_ERR;
a1bb33af 391 }
185ae2c5 392
ea9cfed7
UH
393 analyzer_reset(ctx->usb->devhdl);
394 analyzer_initialize(ctx->usb->devhdl);
a1bb33af
UH
395
396 analyzer_set_memory_size(MEMORY_SIZE_512K);
fed16f06 397 // analyzer_set_freq(g_freq, g_freq_scale);
a1bb33af 398 analyzer_set_trigger_count(1);
408e7199
UH
399 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
400 // * get_memory_size(g_memory_size)) / 100) >> 2);
fed16f06
UH
401 analyzer_set_ramsize_trigger_address(
402 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
a1bb33af 403
fed16f06
UH
404#if 0
405 if (g_double_mode == 1)
a1bb33af
UH
406 analyzer_set_compression(COMPRESSION_DOUBLE);
407 else if (g_compression == 1)
408 analyzer_set_compression(COMPRESSION_ENABLE);
fed16f06
UH
409 else
410#endif
411 analyzer_set_compression(COMPRESSION_NONE);
a1bb33af 412
ea9cfed7 413 if (ctx->cur_samplerate == 0) {
408e7199 414 /* Samplerate hasn't been set. Default to the slowest one. */
6f4b1868 415 if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
73017cf9 416 &samplerates.list[0]) == SR_ERR)
e46b8fb1 417 return SR_ERR;
a1bb33af
UH
418 }
419
e46b8fb1 420 return SR_OK;
a1bb33af
UH
421}
422
25a0f108 423static int hw_dev_close(struct sr_dev_inst *sdi)
a1bb33af 424{
25a0f108 425 struct context *ctx;
a1bb33af 426
25a0f108
BV
427 if (!(ctx = sdi->priv)) {
428 sr_err("zp: %s: sdi->priv was NULL", __func__);
429 return SR_ERR;
697785d1
UH
430 }
431
25a0f108
BV
432 if (!ctx->usb->devhdl)
433 return SR_ERR;
434
435 sr_info("zp: closing device %d on %d.%d interface %d", sdi->index,
436 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
437 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
438 libusb_reset_device(ctx->usb->devhdl);
439 libusb_close(ctx->usb->devhdl);
440 ctx->usb->devhdl = NULL;
441 sdi->status = SR_ST_INACTIVE;
697785d1
UH
442
443 return SR_OK;
a1bb33af
UH
444}
445
57ab7d9f 446static int hw_cleanup(void)
a1bb33af 447{
a1bb33af 448
7da6f9d5 449 clear_instances();
a1bb33af 450
fed16f06 451 if (usb_context)
a1bb33af
UH
452 libusb_exit(usb_context);
453 usb_context = NULL;
57ab7d9f
UH
454
455 return SR_OK;
a1bb33af
UH
456}
457
626409ab
BV
458static int hw_info_get(int info_id, const void **data,
459 const struct sr_dev_inst *sdi)
a1bb33af 460{
ea9cfed7 461 struct context *ctx;
a1bb33af 462
626409ab 463 switch (info_id) {
1d9a8a5f 464 case SR_DI_INST:
626409ab 465 *data = sdi;
6752905e 466 sr_spew("zp: %s: Returning sdi.", __func__);
a1bb33af 467 break;
444adea2
BV
468 case SR_DI_HWCAPS:
469 *data = hwcaps;
470 break;
5a2326a7 471 case SR_DI_NUM_PROBES:
626409ab
BV
472 if (sdi) {
473 ctx = sdi->priv;
474 *data = GINT_TO_POINTER(ctx->num_channels);
475 sr_spew("zp: %s: Returning number of channels: %d.",
476 __func__, ctx->num_channels);
477 } else
478 return SR_ERR;
a1bb33af 479 break;
464d12c7 480 case SR_DI_PROBE_NAMES:
626409ab 481 *data = probe_names;
6752905e 482 sr_spew("zp: %s: Returning probenames.", __func__);
464d12c7 483 break;
5a2326a7 484 case SR_DI_SAMPLERATES:
626409ab 485 *data = &samplerates;
6752905e 486 sr_spew("zp: %s: Returning samplerates.", __func__);
a1bb33af 487 break;
5a2326a7 488 case SR_DI_TRIGGER_TYPES:
626409ab
BV
489 *data = TRIGGER_TYPES;
490 sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES);
a1bb33af 491 break;
5a2326a7 492 case SR_DI_CUR_SAMPLERATE:
626409ab
BV
493 if (sdi) {
494 ctx = sdi->priv;
495 *data = &ctx->cur_samplerate;
496 sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.",
497 __func__, ctx->cur_samplerate);
498 } else
499 return SR_ERR;
bf43ea23
UH
500 break;
501 default:
626409ab 502 return SR_ERR_ARG;
a1bb33af
UH
503 }
504
626409ab 505 return SR_OK;
a1bb33af
UH
506}
507
e7eb703f 508static int hw_dev_status_get(int dev_index)
a1bb33af 509{
d68e2d1a 510 struct sr_dev_inst *sdi;
a1bb33af 511
32756547 512 sdi = sr_dev_inst_get(zdi->instances, dev_index);
fed16f06 513 if (sdi)
a1bb33af
UH
514 return sdi->status;
515 else
5a2326a7 516 return SR_ST_NOT_FOUND;
a1bb33af
UH
517}
518
6f4b1868 519static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
a1bb33af 520{
ea9cfed7 521 struct context *ctx;
bf43ea23
UH
522
523 if (!sdi) {
524 sr_err("zp: %s: sdi was NULL", __func__);
525 return SR_ERR_ARG;
526 }
527
ea9cfed7 528 if (!(ctx = sdi->priv)) {
bf43ea23
UH
529 sr_err("zp: %s: sdi->priv was NULL", __func__);
530 return SR_ERR_ARG;
531 }
532
73017cf9
UH
533 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
534
59df0c77
UH
535 if (samplerate > SR_MHZ(1))
536 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
537 else if (samplerate > SR_KHZ(1))
538 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
a1bb33af 539 else
fed16f06 540 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
a1bb33af 541
ea9cfed7 542 ctx->cur_samplerate = samplerate;
a1bb33af 543
e46b8fb1 544 return SR_OK;
a1bb33af
UH
545}
546
6f4b1868
BV
547static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
548 const void *value)
a1bb33af 549{
ea9cfed7 550 struct context *ctx;
a1bb33af 551
ea9cfed7 552 if (!(ctx = sdi->priv)) {
bf43ea23
UH
553 sr_err("zp: %s: sdi->priv was NULL", __func__);
554 return SR_ERR_ARG;
555 }
a1bb33af 556
ffedd0bf 557 switch (hwcap) {
5a2326a7 558 case SR_HWCAP_SAMPLERATE:
1b79df2f 559 return set_samplerate(sdi, *(const uint64_t *)value);
5a2326a7 560 case SR_HWCAP_PROBECONFIG:
1b79df2f 561 return configure_probes(sdi, (const GSList *)value);
5a2326a7 562 case SR_HWCAP_LIMIT_SAMPLES:
1b79df2f 563 ctx->limit_samples = *(const uint64_t *)value;
e46b8fb1 564 return SR_OK;
fed16f06 565 default:
e46b8fb1 566 return SR_ERR;
a1bb33af
UH
567 }
568}
569
3ffb6964
BV
570static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
571 void *cb_data)
a1bb33af 572{
b9c735a2 573 struct sr_datafeed_packet packet;
9c939c51 574 struct sr_datafeed_logic logic;
b9c735a2 575 struct sr_datafeed_header header;
f366e86c 576 struct sr_datafeed_meta_logic meta;
9c939c51 577 uint64_t samples_read;
a1bb33af 578 int res;
afc8e4de 579 unsigned int packet_num;
a1bb33af 580 unsigned char *buf;
ea9cfed7 581 struct context *ctx;
a1bb33af 582
ea9cfed7 583 if (!(ctx = sdi->priv)) {
bf43ea23
UH
584 sr_err("zp: %s: sdi->priv was NULL", __func__);
585 return SR_ERR_ARG;
586 }
a1bb33af 587
a143e4e5 588 /* push configured settings to device */
ea9cfed7 589 analyzer_configure(ctx->usb->devhdl);
a143e4e5 590
ea9cfed7 591 analyzer_start(ctx->usb->devhdl);
7b48d6e1 592 sr_info("zp: Waiting for data");
ea9cfed7 593 analyzer_wait_data(ctx->usb->devhdl);
a1bb33af 594
7b48d6e1 595 sr_info("zp: Stop address = 0x%x",
ea9cfed7 596 analyzer_get_stop_address(ctx->usb->devhdl));
7b48d6e1 597 sr_info("zp: Now address = 0x%x",
ea9cfed7 598 analyzer_get_now_address(ctx->usb->devhdl));
7b48d6e1 599 sr_info("zp: Trigger address = 0x%x",
ea9cfed7 600 analyzer_get_trigger_address(ctx->usb->devhdl));
a1bb33af 601
5a2326a7 602 packet.type = SR_DF_HEADER;
9c939c51 603 packet.payload = &header;
a1bb33af
UH
604 header.feed_version = 1;
605 gettimeofday(&header.starttime, NULL);
f366e86c
BV
606 sr_session_send(cb_data, &packet);
607
608 /* Send metadata about the SR_DF_LOGIC packets to come. */
609 packet.type = SR_DF_META_LOGIC;
610 packet.payload = &meta;
611 meta.samplerate = ctx->cur_samplerate;
612 meta.num_probes = ctx->num_channels;
3cd3a20b 613 sr_session_send(cb_data, &packet);
a1bb33af 614
b53738ba 615 if (!(buf = g_try_malloc(PACKET_SIZE))) {
bf43ea23 616 sr_err("zp: %s: buf malloc failed", __func__);
b53738ba
UH
617 return SR_ERR_MALLOC;
618 }
619
9c939c51 620 samples_read = 0;
ea9cfed7 621 analyzer_read_start(ctx->usb->devhdl);
fed16f06 622 /* Send the incoming transfer to the session bus. */
ea9cfed7 623 for (packet_num = 0; packet_num < (ctx->memory_size * 4 / PACKET_SIZE);
fed16f06 624 packet_num++) {
ea9cfed7 625 res = analyzer_read_data(ctx->usb->devhdl, buf, PACKET_SIZE);
7b48d6e1 626 sr_info("zp: Tried to read %llx bytes, actually read %x bytes",
b08024a8 627 PACKET_SIZE, res);
a1bb33af 628
5a2326a7 629 packet.type = SR_DF_LOGIC;
9c939c51
BV
630 packet.payload = &logic;
631 logic.length = PACKET_SIZE;
632 logic.unitsize = 4;
633 logic.data = buf;
3cd3a20b 634 sr_session_send(cb_data, &packet);
9c939c51 635 samples_read += res / 4;
a1bb33af 636 }
ea9cfed7 637 analyzer_read_stop(ctx->usb->devhdl);
a1bb33af
UH
638 g_free(buf);
639
5a2326a7 640 packet.type = SR_DF_END;
3cd3a20b 641 sr_session_send(cb_data, &packet);
a1bb33af 642
e46b8fb1 643 return SR_OK;
a1bb33af
UH
644}
645
3cd3a20b 646/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
3ffb6964
BV
647static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
648 void *cb_data)
a1bb33af 649{
b9c735a2 650 struct sr_datafeed_packet packet;
ea9cfed7 651 struct context *ctx;
a1bb33af 652
5a2326a7 653 packet.type = SR_DF_END;
3cd3a20b 654 sr_session_send(cb_data, &packet);
a1bb33af 655
ea9cfed7 656 if (!(ctx = sdi->priv)) {
69890f73 657 sr_err("zp: %s: sdi->priv was NULL", __func__);
3010f21c 658 return SR_ERR_BUG;
69890f73 659 }
a1bb33af 660
ea9cfed7 661 analyzer_reset(ctx->usb->devhdl);
fed16f06 662 /* TODO: Need to cancel and free any queued up transfers. */
3010f21c
UH
663
664 return SR_OK;
a1bb33af
UH
665}
666
c09f0b57 667SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
e519ba86 668 .name = "zeroplus-logic-cube",
8fdecced 669 .longname = "ZEROPLUS Logic Cube LAP-C series",
e519ba86
UH
670 .api_version = 1,
671 .init = hw_init,
672 .cleanup = hw_cleanup,
61136ea6 673 .scan = hw_scan,
e7eb703f
UH
674 .dev_open = hw_dev_open,
675 .dev_close = hw_dev_close,
626409ab 676 .info_get = hw_info_get,
e7eb703f 677 .dev_status_get = hw_dev_status_get,
a9a245b4 678 .dev_config_set = hw_dev_config_set,
69040b7c
UH
679 .dev_acquisition_start = hw_dev_acquisition_start,
680 .dev_acquisition_stop = hw_dev_acquisition_stop,
32756547 681 .instances = NULL,
a1bb33af 682};