]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/zeroplus.c
sr: zp: Add missing <string.h> #include.
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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>
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"
b7f09cf8
UH
28#include "sigrok.h"
29#include "sigrok-internal.h"
a1bb33af
UH
30#include "analyzer.h"
31
fed16f06
UH
32#define USB_VENDOR 0x0c12
33#define USB_VENDOR_NAME "Zeroplus"
a1bb33af
UH
34#define USB_MODEL_NAME "Logic Cube"
35#define USB_MODEL_VERSION ""
36
37#define USB_INTERFACE 0
38#define USB_CONFIGURATION 1
39#define NUM_TRIGGER_STAGES 4
40#define TRIGGER_TYPES "01"
41
fed16f06 42#define PACKET_SIZE 2048 /* ?? */
a1bb33af
UH
43
44typedef struct {
45 unsigned short pid;
46 char model_name[64];
47 unsigned int channels;
fed16f06 48 unsigned int sample_depth; /* In Ksamples/channel */
a1bb33af
UH
49 unsigned int max_sampling_freq;
50} model_t;
51
fed16f06
UH
52/*
53 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
54 * same 128K sample depth.
55 */
29cbfeaf 56static model_t zeroplus_models[] = {
a1bb33af
UH
57 {0x7009, "LAP-C(16064)", 16, 64, 100},
58 {0x700A, "LAP-C(16128)", 16, 128, 200},
59 {0x700B, "LAP-C(32128)", 32, 128, 200},
60 {0x700C, "LAP-C(321000)", 32, 1024, 200},
61 {0x700D, "LAP-C(322000)", 32, 2048, 200},
62 {0x700E, "LAP-C(16032)", 16, 32, 100},
63 {0x7016, "LAP-C(162000)", 16, 2048, 200},
64};
65
66static int capabilities[] = {
5a2326a7
UH
67 SR_HWCAP_LOGIC_ANALYZER,
68 SR_HWCAP_SAMPLERATE,
69 SR_HWCAP_PROBECONFIG,
70 SR_HWCAP_CAPTURE_RATIO,
a1bb33af 71
fed16f06 72 /* These are really implemented in the driver, not the hardware. */
5a2326a7 73 SR_HWCAP_LIMIT_SAMPLES,
fed16f06 74 0,
a1bb33af
UH
75};
76
c37d2b1b 77static const char *probe_names[] = {
464d12c7
KS
78 "0",
79 "1",
80 "2",
81 "3",
82 "4",
83 "5",
84 "6",
85 "7",
86 "8",
87 "9",
88 "10",
89 "11",
90 "12",
91 "13",
92 "14",
93 "15",
94 "16",
95 "17",
96 "18",
97 "19",
98 "20",
99 "21",
100 "22",
101 "23",
102 "24",
103 "25",
104 "26",
105 "27",
106 "28",
107 "29",
108 "30",
109 "31",
110 NULL,
111};
112
a00ba012 113/* List of struct sr_device_instance, maintained by opendev()/closedev(). */
a1bb33af
UH
114static GSList *device_instances = NULL;
115
116static libusb_context *usb_context = NULL;
117
fed16f06
UH
118/*
119 * The hardware supports more samplerates than these, but these are the
120 * options hardcoded into the vendor's Windows GUI.
121 */
a1bb33af 122
fed16f06
UH
123/*
124 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
125 * that high.
126 */
a1bb33af 127static uint64_t supported_samplerates[] = {
c9140419
UH
128 SR_HZ(100),
129 SR_HZ(500),
59df0c77
UH
130 SR_KHZ(1),
131 SR_KHZ(5),
132 SR_KHZ(25),
133 SR_KHZ(50),
134 SR_KHZ(100),
135 SR_KHZ(200),
136 SR_KHZ(400),
137 SR_KHZ(800),
138 SR_MHZ(1),
139 SR_MHZ(10),
140 SR_MHZ(25),
141 SR_MHZ(50),
142 SR_MHZ(80),
143 SR_MHZ(100),
144 SR_MHZ(150),
145 SR_MHZ(200),
fed16f06 146 0,
a1bb33af
UH
147};
148
60679b18 149static struct sr_samplerates samplerates = {
c9140419
UH
150 SR_HZ(0),
151 SR_HZ(0),
152 SR_HZ(0),
fed16f06 153 supported_samplerates,
a1bb33af
UH
154};
155
bf43ea23
UH
156struct zp {
157 uint64_t cur_samplerate;
158 uint64_t period_ps;
159 uint64_t limit_samples;
160 int num_channels; /* TODO: This isn't initialized before it's needed :( */
161 uint64_t memory_size;
162 uint8_t probe_mask;
163 uint8_t trigger_mask[NUM_TRIGGER_STAGES];
164 uint8_t trigger_value[NUM_TRIGGER_STAGES];
165 // uint8_t trigger_buffer[NUM_TRIGGER_STAGES];
166};
a1bb33af
UH
167
168static int hw_set_configuration(int device_index, int capability, void *value);
169
170static unsigned int get_memory_size(int type)
171{
fed16f06
UH
172 if (type == MEMORY_SIZE_8K)
173 return 8 * 1024;
174 else if (type == MEMORY_SIZE_64K)
175 return 64 * 1024;
176 else if (type == MEMORY_SIZE_128K)
177 return 128 * 1024;
178 else if (type == MEMORY_SIZE_512K)
179 return 512 * 1024;
180 else
181 return 0;
a1bb33af
UH
182}
183
a00ba012 184static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
408e7199
UH
185 struct libusb_device_descriptor *des)
186{
bf43ea23 187 struct zp *zp;
9a498834
UH
188 unsigned int i;
189 int err;
408e7199 190
bf43ea23
UH
191 /* Note: sdi is non-NULL, the caller already checked this. */
192
193 if (!(zp = (*sdi)->priv)) {
194 sr_err("zp: %s: (*sdi)->priv was NULL", __func__);
195 return -1;
196 }
197
408e7199 198 if ((err = libusb_get_device_descriptor(dev, des))) {
b08024a8 199 sr_warn("failed to get device descriptor: %d", err);
408e7199
UH
200 return -1;
201 }
202
9a498834 203 if (des->idVendor != USB_VENDOR)
408e7199
UH
204 return 0;
205
206 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
207 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
208
9a498834
UH
209 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
210 if (!(des->idProduct == zeroplus_models[i].pid))
408e7199
UH
211 continue;
212
b08024a8
UH
213 sr_info("Found PID=%04X (%s)", des->idProduct,
214 zeroplus_models[i].model_name);
bf43ea23
UH
215 zp->num_channels = zeroplus_models[i].channels;
216 zp->memory_size = zeroplus_models[i].sample_depth * 1024;
408e7199
UH
217 break;
218 }
219
bf43ea23 220 if (zp->num_channels == 0) {
b08024a8 221 sr_warn("Unknown ZeroPlus device %04X", des->idProduct);
408e7199
UH
222 return -2;
223 }
224
225 /* Found it. */
226 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
5a2326a7 227 (*sdi)->status = SR_ST_ACTIVE;
b08024a8
UH
228 sr_info("opened device %d on %d.%d interface %d",
229 (*sdi)->index, (*sdi)->usb->bus,
230 (*sdi)->usb->address, USB_INTERFACE);
408e7199 231 } else {
b08024a8 232 sr_warn("failed to open device: %d", err);
408e7199
UH
233 *sdi = NULL;
234 }
235 }
236
237 return 0;
238}
239
29cbfeaf 240static struct sr_device_instance *zp_open_device(int device_index)
a1bb33af 241{
a00ba012 242 struct sr_device_instance *sdi;
a1bb33af
UH
243 libusb_device **devlist;
244 struct libusb_device_descriptor des;
afc8e4de 245 int err, i;
a1bb33af 246
d32d961d 247 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af
UH
248 return NULL;
249
250 libusb_get_device_list(usb_context, &devlist);
5a2326a7 251 if (sdi->status == SR_ST_INACTIVE) {
fed16f06 252 /* Find the device by vendor, product, bus and address. */
a1bb33af 253 libusb_get_device_list(usb_context, &devlist);
fed16f06 254 for (i = 0; devlist[i]; i++) {
408e7199 255 /* TODO: Error handling. */
9a5c6dcf 256 err = opendev4(&sdi, devlist[i], &des);
a1bb33af 257 }
fed16f06 258 } else {
5a2326a7 259 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
a1bb33af
UH
260 sdi = NULL;
261 }
262 libusb_free_device_list(devlist, 1);
263
5a2326a7 264 if (sdi && sdi->status != SR_ST_ACTIVE)
a1bb33af
UH
265 sdi = NULL;
266
267 return sdi;
268}
269
a00ba012 270static void close_device(struct sr_device_instance *sdi)
a1bb33af 271{
408e7199
UH
272 if (!sdi->usb->devhdl)
273 return;
274
b08024a8
UH
275 sr_info("closing device %d on %d.%d interface %d", sdi->index,
276 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
408e7199
UH
277 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
278 libusb_close(sdi->usb->devhdl);
279 sdi->usb->devhdl = NULL;
5a2326a7 280 sdi->status = SR_ST_INACTIVE;
a1bb33af
UH
281}
282
bf43ea23 283static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
a1bb33af 284{
bf43ea23 285 struct zp *zp;
1afe8989 286 struct sr_probe *probe;
a1bb33af
UH
287 GSList *l;
288 int probe_bit, stage, i;
289 char *tc;
290
bf43ea23
UH
291 /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */
292 zp = sdi->priv;
293
294 zp->probe_mask = 0;
fed16f06 295 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
bf43ea23
UH
296 zp->trigger_mask[i] = 0;
297 zp->trigger_value[i] = 0;
a1bb33af
UH
298 }
299
300 stage = -1;
fed16f06 301 for (l = probes; l; l = l->next) {
1afe8989 302 probe = (struct sr_probe *)l->data;
fed16f06 303 if (probe->enabled == FALSE)
a1bb33af
UH
304 continue;
305 probe_bit = 1 << (probe->index - 1);
bf43ea23 306 zp->probe_mask |= probe_bit;
fed16f06
UH
307
308 if (probe->trigger) {
a1bb33af 309 stage = 0;
fed16f06 310 for (tc = probe->trigger; *tc; tc++) {
bf43ea23 311 zp->trigger_mask[stage] |= probe_bit;
fed16f06 312 if (*tc == '1')
bf43ea23 313 zp->trigger_value[stage] |= probe_bit;
a1bb33af 314 stage++;
fed16f06 315 if (stage > NUM_TRIGGER_STAGES)
e46b8fb1 316 return SR_ERR;
a1bb33af
UH
317 }
318 }
319 }
320
e46b8fb1 321 return SR_OK;
a1bb33af
UH
322}
323
a1bb33af
UH
324/*
325 * API callbacks
326 */
327
54ac5277 328static int hw_init(const char *deviceinfo)
a1bb33af 329{
a00ba012 330 struct sr_device_instance *sdi;
a1bb33af
UH
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
333 int err, devcnt, i;
bf43ea23 334 struct zp *zp;
a1bb33af 335
17e1afcb 336 /* Avoid compiler warnings. */
cb93f8a9 337 (void)deviceinfo;
afc8e4de 338
bf43ea23
UH
339 /* Allocate memory for our private driver context. */
340 if (!(zp = g_try_malloc(sizeof(struct zp)))) {
341 sr_err("zp: %s: struct zp malloc failed", __func__);
342 return 0;
343 }
344
345 /* Set some sane defaults. */
346 zp->cur_samplerate = 0;
347 zp->period_ps = 0;
348 zp->limit_samples = 0;
349 zp->num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
350 zp->memory_size = 0;
351 zp->probe_mask = 0;
352 memset(zp->trigger_mask, 0, NUM_TRIGGER_STAGES);
353 memset(zp->trigger_value, 0, NUM_TRIGGER_STAGES);
354 // memset(zp->trigger_buffer, 0, NUM_TRIGGER_STAGES);
355
fed16f06 356 if (libusb_init(&usb_context) != 0) {
b08024a8 357 sr_warn("Failed to initialize USB.");
a1bb33af
UH
358 return 0;
359 }
360
fed16f06 361 /* Find all ZeroPlus analyzers and add them to device list. */
a1bb33af
UH
362 devcnt = 0;
363 libusb_get_device_list(usb_context, &devlist);
fed16f06
UH
364
365 for (i = 0; devlist[i]; i++) {
a1bb33af 366 err = libusb_get_device_descriptor(devlist[i], &des);
fed16f06 367 if (err != 0) {
b08024a8 368 sr_warn("failed to get device descriptor: %d", err);
a1bb33af
UH
369 continue;
370 }
371
fed16f06
UH
372 if (des.idVendor == USB_VENDOR) {
373 /*
374 * Definitely a Zeroplus.
375 * TODO: Any way to detect specific model/version in
376 * the zeroplus range?
377 */
bf43ea23 378 /* Register the device with libsigrok. */
a00ba012 379 sdi = sr_device_instance_new(devcnt,
5a2326a7 380 SR_ST_INACTIVE, USB_VENDOR_NAME,
fed16f06 381 USB_MODEL_NAME, USB_MODEL_VERSION);
bf43ea23
UH
382 if (!sdi) {
383 sr_err("zp: %s: sr_device_instance_new failed",
384 __func__);
a1bb33af 385 return 0;
bf43ea23
UH
386 }
387
388 sdi->priv = zp;
389
fed16f06
UH
390 device_instances =
391 g_slist_append(device_instances, sdi);
6c290072 392 sdi->usb = sr_usb_device_instance_new(
fed16f06
UH
393 libusb_get_bus_number(devlist[i]),
394 libusb_get_device_address(devlist[i]), NULL);
a1bb33af
UH
395 devcnt++;
396 }
397 }
398 libusb_free_device_list(devlist, 1);
399
400 return devcnt;
401}
402
a1bb33af
UH
403static int hw_opendev(int device_index)
404{
a00ba012 405 struct sr_device_instance *sdi;
bf43ea23 406 struct zp *zp;
a1bb33af
UH
407 int err;
408
fed16f06 409 if (!(sdi = zp_open_device(device_index))) {
b08024a8 410 sr_warn("unable to open device");
e46b8fb1 411 return SR_ERR;
a1bb33af
UH
412 }
413
bf43ea23
UH
414 /* TODO: Note: sdi is retrieved in zp_open_device(). */
415
416 if (!(zp = sdi->priv)) {
417 sr_err("zp: %s: sdi->priv was NULL", __func__);
418 return SR_ERR_ARG;
419 }
420
a1bb33af 421 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
fed16f06 422 if (err != 0) {
b08024a8 423 sr_warn("Unable to claim interface: %d", err);
e46b8fb1 424 return SR_ERR;
a1bb33af
UH
425 }
426 analyzer_reset(sdi->usb->devhdl);
427 analyzer_initialize(sdi->usb->devhdl);
a1bb33af
UH
428
429 analyzer_set_memory_size(MEMORY_SIZE_512K);
fed16f06 430 // analyzer_set_freq(g_freq, g_freq_scale);
a1bb33af 431 analyzer_set_trigger_count(1);
408e7199
UH
432 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
433 // * get_memory_size(g_memory_size)) / 100) >> 2);
fed16f06
UH
434 analyzer_set_ramsize_trigger_address(
435 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
a1bb33af 436
fed16f06
UH
437#if 0
438 if (g_double_mode == 1)
a1bb33af
UH
439 analyzer_set_compression(COMPRESSION_DOUBLE);
440 else if (g_compression == 1)
441 analyzer_set_compression(COMPRESSION_ENABLE);
fed16f06
UH
442 else
443#endif
444 analyzer_set_compression(COMPRESSION_NONE);
a1bb33af 445
bf43ea23 446 if (zp->cur_samplerate == 0) {
408e7199 447 /* Samplerate hasn't been set. Default to the slowest one. */
5a2326a7 448 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
73017cf9 449 &samplerates.list[0]) == SR_ERR)
e46b8fb1 450 return SR_ERR;
a1bb33af
UH
451 }
452
e46b8fb1 453 return SR_OK;
a1bb33af
UH
454}
455
697785d1 456static int hw_closedev(int device_index)
a1bb33af 457{
a00ba012 458 struct sr_device_instance *sdi;
a1bb33af 459
697785d1 460 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
bf43ea23 461 sr_err("zp: %s: sdi was NULL", __func__);
697785d1
UH
462 return SR_ERR; /* TODO: SR_ERR_ARG? */
463 }
464
465 /* TODO */
466 close_device(sdi);
467
468 return SR_OK;
a1bb33af
UH
469}
470
a1bb33af
UH
471static void hw_cleanup(void)
472{
473 GSList *l;
474
408e7199 475 /* Properly close all devices... */
fed16f06 476 for (l = device_instances; l; l = l->next)
a00ba012 477 close_device((struct sr_device_instance *)l->data);
a1bb33af 478
408e7199 479 /* ...and free all their memory. */
fed16f06 480 for (l = device_instances; l; l = l->next)
a1bb33af
UH
481 g_free(l->data);
482 g_slist_free(device_instances);
483 device_instances = NULL;
484
fed16f06 485 if (usb_context)
a1bb33af
UH
486 libusb_exit(usb_context);
487 usb_context = NULL;
a1bb33af
UH
488}
489
a1bb33af
UH
490static void *hw_get_device_info(int device_index, int device_info_id)
491{
a00ba012 492 struct sr_device_instance *sdi;
bf43ea23
UH
493 struct zp *zp;
494 void *info;
a1bb33af 495
bf43ea23
UH
496 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
497 sr_err("zp: %s: sdi was NULL", __func__);
a1bb33af 498 return NULL;
bf43ea23
UH
499 }
500
501 if (!(zp = sdi->priv)) {
502 sr_err("zp: %s: sdi->priv was NULL", __func__);
503 return NULL;
504 }
a1bb33af 505
fed16f06 506 switch (device_info_id) {
5a2326a7 507 case SR_DI_INSTANCE:
a1bb33af
UH
508 info = sdi;
509 break;
5a2326a7 510 case SR_DI_NUM_PROBES:
bf43ea23 511 info = GINT_TO_POINTER(zp->num_channels);
a1bb33af 512 break;
464d12c7
KS
513 case SR_DI_PROBE_NAMES:
514 info = probe_names;
515 break;
5a2326a7 516 case SR_DI_SAMPLERATES:
a1bb33af
UH
517 info = &samplerates;
518 break;
5a2326a7 519 case SR_DI_TRIGGER_TYPES:
a1bb33af
UH
520 info = TRIGGER_TYPES;
521 break;
5a2326a7 522 case SR_DI_CUR_SAMPLERATE:
bf43ea23
UH
523 info = &zp->cur_samplerate;
524 break;
525 default:
526 /* Unknown device info ID, return NULL. */
527 sr_err("zp: %s: Unknown device info ID", __func__);
528 info = NULL;
a1bb33af
UH
529 break;
530 }
531
532 return info;
533}
534
a1bb33af
UH
535static int hw_get_status(int device_index)
536{
a00ba012 537 struct sr_device_instance *sdi;
a1bb33af 538
d32d961d 539 sdi = sr_get_device_instance(device_instances, device_index);
fed16f06 540 if (sdi)
a1bb33af
UH
541 return sdi->status;
542 else
5a2326a7 543 return SR_ST_NOT_FOUND;
a1bb33af
UH
544}
545
a1bb33af
UH
546static int *hw_get_capabilities(void)
547{
a1bb33af
UH
548 return capabilities;
549}
550
bf43ea23
UH
551static int set_configuration_samplerate(struct sr_device_instance *sdi,
552 uint64_t samplerate)
a1bb33af 553{
bf43ea23
UH
554 struct zp *zp;
555
556 if (!sdi) {
557 sr_err("zp: %s: sdi was NULL", __func__);
558 return SR_ERR_ARG;
559 }
560
561 if (!(zp = sdi->priv)) {
562 sr_err("zp: %s: sdi->priv was NULL", __func__);
563 return SR_ERR_ARG;
564 }
565
73017cf9
UH
566 sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate);
567
59df0c77
UH
568 if (samplerate > SR_MHZ(1))
569 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
570 else if (samplerate > SR_KHZ(1))
571 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
a1bb33af 572 else
fed16f06 573 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
a1bb33af 574
bf43ea23
UH
575 zp->cur_samplerate = samplerate;
576 zp->period_ps = 1000000000000 / samplerate;
a1bb33af 577
e46b8fb1 578 return SR_OK;
a1bb33af
UH
579}
580
581static int hw_set_configuration(int device_index, int capability, void *value)
582{
a00ba012 583 struct sr_device_instance *sdi;
a1bb33af 584 uint64_t *tmp_u64;
bf43ea23 585 struct zp *zp;
a1bb33af 586
bf43ea23
UH
587 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
588 sr_err("zp: %s: sdi was NULL", __func__);
e46b8fb1 589 return SR_ERR;
bf43ea23
UH
590 }
591
592 if (!(zp = sdi->priv)) {
593 sr_err("zp: %s: sdi->priv was NULL", __func__);
594 return SR_ERR_ARG;
595 }
a1bb33af
UH
596
597 switch (capability) {
5a2326a7 598 case SR_HWCAP_SAMPLERATE:
fed16f06 599 tmp_u64 = value;
bf43ea23 600 return set_configuration_samplerate(sdi, *tmp_u64);
5a2326a7 601 case SR_HWCAP_PROBECONFIG:
bf43ea23 602 return configure_probes(sdi, (GSList *)value);
5a2326a7 603 case SR_HWCAP_LIMIT_SAMPLES:
2458ea65 604 tmp_u64 = value;
bf43ea23 605 zp->limit_samples = *tmp_u64;
e46b8fb1 606 return SR_OK;
fed16f06 607 default:
e46b8fb1 608 return SR_ERR;
a1bb33af
UH
609 }
610}
611
9c939c51 612static int hw_start_acquisition(int device_index, gpointer session_data)
a1bb33af 613{
a00ba012 614 struct sr_device_instance *sdi;
b9c735a2 615 struct sr_datafeed_packet packet;
9c939c51 616 struct sr_datafeed_logic logic;
b9c735a2 617 struct sr_datafeed_header header;
9c939c51 618 uint64_t samples_read;
a1bb33af 619 int res;
afc8e4de 620 unsigned int packet_num;
a1bb33af 621 unsigned char *buf;
bf43ea23 622 struct zp *zp;
a1bb33af 623
bf43ea23
UH
624 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
625 sr_err("zp: %s: sdi was NULL", __func__);
e46b8fb1 626 return SR_ERR;
bf43ea23
UH
627 }
628
629 if (!(zp = sdi->priv)) {
630 sr_err("zp: %s: sdi->priv was NULL", __func__);
631 return SR_ERR_ARG;
632 }
a1bb33af 633
a143e4e5
BV
634 /* push configured settings to device */
635 analyzer_configure(sdi->usb->devhdl);
636
a1bb33af 637 analyzer_start(sdi->usb->devhdl);
b08024a8 638 sr_info("Waiting for data");
a1bb33af
UH
639 analyzer_wait_data(sdi->usb->devhdl);
640
9c939c51
BV
641 sr_info("Stop address = 0x%x", analyzer_get_stop_address(sdi->usb->devhdl));
642 sr_info("Now address = 0x%x", analyzer_get_now_address(sdi->usb->devhdl));
643 sr_info("Trigger address = 0x%x", analyzer_get_trigger_address(sdi->usb->devhdl));
a1bb33af 644
5a2326a7 645 packet.type = SR_DF_HEADER;
9c939c51 646 packet.payload = &header;
a1bb33af
UH
647 header.feed_version = 1;
648 gettimeofday(&header.starttime, NULL);
bf43ea23
UH
649 header.samplerate = zp->cur_samplerate;
650 header.num_logic_probes = zp->num_channels;
c2616fb9 651 header.num_analog_probes = 0;
9c939c51 652 sr_session_bus(session_data, &packet);
a1bb33af 653
b53738ba 654 if (!(buf = g_try_malloc(PACKET_SIZE))) {
bf43ea23 655 sr_err("zp: %s: buf malloc failed", __func__);
b53738ba
UH
656 return SR_ERR_MALLOC;
657 }
658
9c939c51 659 samples_read = 0;
a1bb33af 660 analyzer_read_start(sdi->usb->devhdl);
fed16f06 661 /* Send the incoming transfer to the session bus. */
bf43ea23 662 for (packet_num = 0; packet_num < (zp->memory_size * 4 / PACKET_SIZE);
fed16f06 663 packet_num++) {
a1bb33af 664 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
b08024a8
UH
665 sr_info("Tried to read %llx bytes, actually read %x bytes",
666 PACKET_SIZE, res);
a1bb33af 667
5a2326a7 668 packet.type = SR_DF_LOGIC;
bf43ea23
UH
669 packet.timeoffset = samples_read * zp->period_ps;
670 packet.duration = res / 4 * zp->period_ps;
9c939c51
BV
671 packet.payload = &logic;
672 logic.length = PACKET_SIZE;
673 logic.unitsize = 4;
674 logic.data = buf;
675 sr_session_bus(session_data, &packet);
676 samples_read += res / 4;
a1bb33af
UH
677 }
678 analyzer_read_stop(sdi->usb->devhdl);
679 g_free(buf);
680
5a2326a7 681 packet.type = SR_DF_END;
9c939c51 682 sr_session_bus(session_data, &packet);
a1bb33af 683
e46b8fb1 684 return SR_OK;
a1bb33af
UH
685}
686
fed16f06 687/* This stops acquisition on ALL devices, ignoring device_index. */
a1bb33af
UH
688static void hw_stop_acquisition(int device_index, gpointer session_device_id)
689{
b9c735a2 690 struct sr_datafeed_packet packet;
a00ba012 691 struct sr_device_instance *sdi;
a1bb33af 692
5a2326a7 693 packet.type = SR_DF_END;
8a2efef2 694 sr_session_bus(session_device_id, &packet);
a1bb33af 695
d32d961d 696 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
fed16f06 697 return; /* TODO: Cry? */
a1bb33af
UH
698
699 analyzer_reset(sdi->usb->devhdl);
fed16f06 700 /* TODO: Need to cancel and free any queued up transfers. */
a1bb33af
UH
701}
702
5c2d46d1 703struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
e519ba86
UH
704 .name = "zeroplus-logic-cube",
705 .longname = "Zeroplus Logic Cube LAP-C series",
706 .api_version = 1,
707 .init = hw_init,
708 .cleanup = hw_cleanup,
86f5e3d8
UH
709 .opendev = hw_opendev,
710 .closedev = hw_closedev,
e519ba86
UH
711 .get_device_info = hw_get_device_info,
712 .get_status = hw_get_status,
713 .get_capabilities = hw_get_capabilities,
714 .set_configuration = hw_set_configuration,
715 .start_acquisition = hw_start_acquisition,
716 .stop_acquisition = hw_stop_acquisition,
a1bb33af 717};