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