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