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