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