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