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