]> sigrok.org Git - libsigrok.git/blame - src/hardware/zeroplus-logic-cube/api.c
Fix #442 by renaming sr_dev_driver.priv to .context
[libsigrok.git] / src / hardware / zeroplus-logic-cube / api.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 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
6d116114 20#include "protocol.h"
a1bb33af 21
8fdecced 22#define VENDOR_NAME "ZEROPLUS"
a1bb33af
UH
23#define USB_INTERFACE 0
24#define USB_CONFIGURATION 1
25#define NUM_TRIGGER_STAGES 4
fed16f06 26#define PACKET_SIZE 2048 /* ?? */
a1bb33af 27
0ab0cb94
TY
28//#define ZP_EXPERIMENTAL
29
e495a676
UH
30struct zp_model {
31 uint16_t vid;
32 uint16_t pid;
428edbe1 33 char *model_name;
a1bb33af 34 unsigned int channels;
fed16f06 35 unsigned int sample_depth; /* In Ksamples/channel */
a1bb33af 36 unsigned int max_sampling_freq;
e495a676 37};
a1bb33af 38
fed16f06
UH
39/*
40 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
41 * same 128K sample depth.
42 */
e495a676 43static const struct zp_model zeroplus_models[] = {
9e5670d0 44 {0x0c12, 0x7002, "LAP-16128U", 16, 128, 200},
428edbe1 45 {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100},
e495a676 46 {0x0c12, 0x700a, "LAP-C(16128)", 16, 128, 200},
5db0c668
RD
47 {0x0c12, 0x700b, "LAP-C(32128)", 32, 128, 200},
48 {0x0c12, 0x700c, "LAP-C(321000)", 32, 1024, 200},
49 {0x0c12, 0x700d, "LAP-C(322000)", 32, 2048, 200},
e495a676 50 {0x0c12, 0x700e, "LAP-C(16032)", 16, 32, 100},
428edbe1 51 {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200},
e3594306 52 {0x0c12, 0x7100, "AKIP-9101", 16, 256, 200},
428edbe1 53 { 0, 0, 0, 0, 0, 0 }
a1bb33af
UH
54};
55
f254bc4b 56static const uint32_t devopts[] = {
1953564a 57 SR_CONF_LOGIC_ANALYZER,
5827f61b
BV
58 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
61 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
62 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
a1bb33af
UH
63};
64
28731bab
BV
65static const int32_t trigger_matches[] = {
66 SR_TRIGGER_ZERO,
67 SR_TRIGGER_ONE,
68};
69
d261dbbf 70/*
ba7dd8bb 71 * ZEROPLUS LAP-C (16032) numbers the 16 channels A0-A7 and B0-B7.
d261dbbf
UH
72 * We currently ignore other untested/unsupported devices here.
73 */
ba7dd8bb 74static const char *channel_names[] = {
78693401
UH
75 "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
76 "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
5db0c668
RD
77 "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",
78 "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
464d12c7
KS
79};
80
32756547 81SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
a1bb33af 82
fed16f06
UH
83/*
84 * The hardware supports more samplerates than these, but these are the
85 * options hardcoded into the vendor's Windows GUI.
86 */
a1bb33af 87
8386096f 88static const uint64_t samplerates_100[] = {
17548571
UH
89 SR_HZ(100),
90 SR_HZ(500),
91 SR_KHZ(1),
92 SR_KHZ(5),
93 SR_KHZ(25),
94 SR_KHZ(50),
95 SR_KHZ(100),
96 SR_KHZ(200),
97 SR_KHZ(400),
98 SR_KHZ(800),
99 SR_MHZ(1),
100 SR_MHZ(10),
101 SR_MHZ(25),
102 SR_MHZ(50),
103 SR_MHZ(80),
104 SR_MHZ(100),
17548571
UH
105};
106
8386096f 107const uint64_t samplerates_200[] = {
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),
bf43ea23 126};
a1bb33af 127
6078d2c9 128static int dev_close(struct sr_dev_inst *sdi);
a1bb33af 129
3316e149
BV
130SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
131{
132 int i;
133
134 for (i = 0; ARRAY_SIZE(samplerates_200); i++)
135 if (samplerate == samplerates_200[i])
136 break;
137
138 if (i == ARRAY_SIZE(samplerates_200) || samplerate > devc->max_samplerate) {
139 sr_err("Unsupported samplerate: %" PRIu64 "Hz.", samplerate);
140 return SR_ERR_ARG;
141 }
142
143 sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
144
145 if (samplerate >= SR_MHZ(1))
146 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
147 else if (samplerate >= SR_KHZ(1))
148 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
149 else
150 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
151
152 devc->cur_samplerate = samplerate;
153
154 return SR_OK;
155}
156
4f840ce9 157static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
61136ea6 158{
f6beaac5 159 return std_init(sr_ctx, di, LOG_PREFIX);
61136ea6
BV
160}
161
4f840ce9 162static GSList *scan(struct sr_dev_driver *di, GSList *options)
a1bb33af 163{
d68e2d1a 164 struct sr_dev_inst *sdi;
310e9e9b
BV
165 struct drv_context *drvc;
166 struct dev_context *devc;
e495a676 167 const struct zp_model *prof;
a1bb33af 168 struct libusb_device_descriptor des;
ee4f9bb1 169 struct libusb_device_handle *hdl;
a1bb33af 170 libusb_device **devlist;
428edbe1 171 GSList *devices;
ee4f9bb1
SA
172 int ret, i, j;
173 char serial_num[64], connection_id[64];
a1bb33af 174
4ca38984 175 (void)options;
64d33dc2 176
41812aca 177 drvc = di->context;
4b97c74e 178
4ca38984
BV
179 devices = NULL;
180
8fdecced 181 /* Find all ZEROPLUS analyzers and add them to device list. */
d4abb463 182 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
fed16f06
UH
183
184 for (i = 0; devlist[i]; i++) {
ebc34738
UH
185 ret = libusb_get_device_descriptor(devlist[i], &des);
186 if (ret != 0) {
6d116114 187 sr_err("Failed to get device descriptor: %s.",
d4928d71 188 libusb_error_name(ret));
a1bb33af
UH
189 continue;
190 }
191
ee4f9bb1
SA
192 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
193 continue;
194
195 if (des.iSerialNumber == 0) {
196 serial_num[0] = '\0';
197 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
198 des.iSerialNumber, (unsigned char *) serial_num,
199 sizeof(serial_num))) < 0) {
200 sr_warn("Failed to get serial number string descriptor: %s.",
201 libusb_error_name(ret));
202 continue;
203 }
204
205 libusb_close(hdl);
206
207 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
208
428edbe1
BV
209 prof = NULL;
210 for (j = 0; j < zeroplus_models[j].vid; j++) {
211 if (des.idVendor == zeroplus_models[j].vid &&
212 des.idProduct == zeroplus_models[j].pid) {
213 prof = &zeroplus_models[j];
bf43ea23 214 }
a1bb33af 215 }
e495a676 216 /* Skip if the device was not found. */
428edbe1
BV
217 if (!prof)
218 continue;
e495a676 219 sr_info("Found ZEROPLUS %s.", prof->model_name);
428edbe1
BV
220
221 /* Register the device with libsigrok. */
aac29cc1 222 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
223 sdi->status = SR_ST_INACTIVE;
224 sdi->vendor = g_strdup(VENDOR_NAME);
225 sdi->model = g_strdup(prof->model_name);
a873c594 226 sdi->driver = di;
ee4f9bb1
SA
227 sdi->serial_num = g_strdup(serial_num);
228 sdi->connection_id = g_strdup(connection_id);
428edbe1
BV
229
230 /* Allocate memory for our private driver context. */
f57d8ffe 231 devc = g_malloc0(sizeof(struct dev_context));
310e9e9b 232 sdi->priv = devc;
17548571 233 devc->prof = prof;
310e9e9b 234 devc->num_channels = prof->channels;
0ab0cb94 235#ifdef ZP_EXPERIMENTAL
e93fb98b 236 devc->max_sample_depth = 128 * 1024;
0ab0cb94
TY
237 devc->max_samplerate = 200;
238#else
e93fb98b 239 devc->max_sample_depth = prof->sample_depth * 1024;
0ab0cb94
TY
240 devc->max_samplerate = prof->max_sampling_freq;
241#endif
242 devc->max_samplerate *= SR_MHZ(1);
243 devc->memory_size = MEMORY_SIZE_8K;
310e9e9b 244 // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES);
428edbe1 245
ba7dd8bb 246 /* Fill in channellist according to this device's profile. */
5e23fcab
ML
247 for (j = 0; j < devc->num_channels; j++)
248 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
c368e6f3 249 channel_names[j]);
428edbe1
BV
250
251 devices = g_slist_append(devices, sdi);
310e9e9b 252 drvc->instances = g_slist_append(drvc->instances, sdi);
8111446a 253 sdi->inst_type = SR_INST_USB;
609bfd75 254 sdi->conn = sr_usb_dev_inst_new(
428edbe1
BV
255 libusb_get_bus_number(devlist[i]),
256 libusb_get_device_address(devlist[i]), NULL);
a1bb33af
UH
257 }
258 libusb_free_device_list(devlist, 1);
259
4ca38984 260 return devices;
a1bb33af
UH
261}
262
4f840ce9 263static GSList *dev_list(const struct sr_dev_driver *di)
811deee4 264{
41812aca 265 return ((struct drv_context *)(di->context))->instances;
811deee4
BV
266}
267
6078d2c9 268static int dev_open(struct sr_dev_inst *sdi)
a1bb33af 269{
4f840ce9 270 struct sr_dev_driver *di = sdi->driver;
310e9e9b 271 struct dev_context *devc;
e495a676 272 struct drv_context *drvc;
609bfd75 273 struct sr_usb_dev_inst *usb;
428edbe1 274 libusb_device **devlist, *dev;
428edbe1 275 int device_count, ret, i;
ee4f9bb1 276 char connection_id[64];
a1bb33af 277
41812aca 278 drvc = di->context;
609bfd75 279 usb = sdi->conn;
e495a676 280
310e9e9b 281 if (!(devc = sdi->priv)) {
6d116114 282 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
283 return SR_ERR_ARG;
284 }
285
d4abb463
PS
286 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
287 &devlist);
428edbe1 288 if (device_count < 0) {
6d116114 289 sr_err("Failed to retrieve device list.");
428edbe1
BV
290 return SR_ERR;
291 }
292
293 dev = NULL;
294 for (i = 0; i < device_count; i++) {
ee4f9bb1
SA
295 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
296 if (!strcmp(sdi->connection_id, connection_id)) {
428edbe1
BV
297 dev = devlist[i];
298 break;
299 }
300 }
301 if (!dev) {
ee4f9bb1
SA
302 sr_err("Device on %d.%d (logical) / %s (physical) disappeared!",
303 usb->bus, usb->address, sdi->connection_id);
428edbe1
BV
304 return SR_ERR;
305 }
306
609bfd75 307 if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
428edbe1 308 sdi->status = SR_ST_ACTIVE;
ee4f9bb1
SA
309 sr_info("Opened device on %d.%d (logical) / %s (physical) interface %d.",
310 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
428edbe1 311 } else {
6d116114 312 sr_err("Failed to open device: %s.", libusb_error_name(ret));
428edbe1
BV
313 return SR_ERR;
314 }
315
609bfd75 316 ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
ebc34738 317 if (ret < 0) {
6d116114 318 sr_err("Unable to set USB configuration %d: %s.",
d4928d71 319 USB_CONFIGURATION, libusb_error_name(ret));
185ae2c5
UH
320 return SR_ERR;
321 }
322
609bfd75 323 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
ebc34738 324 if (ret != 0) {
6d116114 325 sr_err("Unable to claim interface: %s.",
d4928d71 326 libusb_error_name(ret));
e46b8fb1 327 return SR_ERR;
a1bb33af 328 }
185ae2c5 329
e495a676 330 /* Set default configuration after power on. */
609bfd75
BV
331 if (analyzer_read_status(usb->devhdl) == 0)
332 analyzer_configure(usb->devhdl);
0ab0cb94 333
609bfd75
BV
334 analyzer_reset(usb->devhdl);
335 analyzer_initialize(usb->devhdl);
a1bb33af 336
0ab0cb94 337 //analyzer_set_memory_size(MEMORY_SIZE_512K);
fed16f06 338 // analyzer_set_freq(g_freq, g_freq_scale);
a1bb33af 339 analyzer_set_trigger_count(1);
408e7199
UH
340 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
341 // * get_memory_size(g_memory_size)) / 100) >> 2);
a1bb33af 342
fed16f06
UH
343#if 0
344 if (g_double_mode == 1)
a1bb33af
UH
345 analyzer_set_compression(COMPRESSION_DOUBLE);
346 else if (g_compression == 1)
347 analyzer_set_compression(COMPRESSION_ENABLE);
fed16f06
UH
348 else
349#endif
350 analyzer_set_compression(COMPRESSION_NONE);
a1bb33af 351
310e9e9b 352 if (devc->cur_samplerate == 0) {
0ab0cb94
TY
353 /* Samplerate hasn't been set. Default to 1MHz. */
354 analyzer_set_freq(1, FREQ_SCALE_MHZ);
355 devc->cur_samplerate = SR_MHZ(1);
a1bb33af
UH
356 }
357
7142d6b9
RD
358 if (devc->cur_threshold == 0)
359 set_voltage_threshold(devc, 1.5);
360
e46b8fb1 361 return SR_OK;
a1bb33af
UH
362}
363
6078d2c9 364static int dev_close(struct sr_dev_inst *sdi)
a1bb33af 365{
609bfd75 366 struct sr_usb_dev_inst *usb;
a1bb33af 367
609bfd75 368 usb = sdi->conn;
697785d1 369
609bfd75 370 if (!usb->devhdl)
25a0f108
BV
371 return SR_ERR;
372
ee4f9bb1
SA
373 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
374 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
609bfd75
BV
375 libusb_release_interface(usb->devhdl, USB_INTERFACE);
376 libusb_reset_device(usb->devhdl);
377 libusb_close(usb->devhdl);
378 usb->devhdl = NULL;
25a0f108 379 sdi->status = SR_ST_INACTIVE;
697785d1
UH
380
381 return SR_OK;
a1bb33af
UH
382}
383
4f840ce9 384static int cleanup(const struct sr_dev_driver *di)
a1bb33af 385{
a6630742 386 return std_dev_clear(di, NULL);
a1bb33af
UH
387}
388
584560f1 389static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 390 const struct sr_channel_group *cg)
a1bb33af 391{
310e9e9b 392 struct dev_context *devc;
dcd438ee 393 GVariant *range[2];
a1bb33af 394
53b4680f 395 (void)cg;
8f996b89 396
dcd438ee
UH
397 if (!sdi)
398 return SR_ERR_ARG;
399
400 devc = sdi->priv;
401
584560f1 402 switch (key) {
123e1313 403 case SR_CONF_SAMPLERATE:
dcd438ee 404 *data = g_variant_new_uint64(devc->cur_samplerate);
bf43ea23 405 break;
05f853b5 406 case SR_CONF_CAPTURE_RATIO:
dcd438ee 407 *data = g_variant_new_uint64(devc->capture_ratio);
05f853b5 408 break;
7142d6b9 409 case SR_CONF_VOLTAGE_THRESHOLD:
dcd438ee
UH
410 range[0] = g_variant_new_double(devc->cur_threshold);
411 range[1] = g_variant_new_double(devc->cur_threshold);
412 *data = g_variant_new_tuple(range, 2);
67055d4c 413 break;
bf43ea23 414 default:
bd6fbf62 415 return SR_ERR_NA;
a1bb33af
UH
416 }
417
626409ab 418 return SR_OK;
a1bb33af
UH
419}
420
584560f1 421static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 422 const struct sr_channel_group *cg)
a1bb33af 423{
310e9e9b 424 struct dev_context *devc;
7142d6b9 425 gdouble low, high;
a1bb33af 426
53b4680f 427 (void)cg;
8f996b89 428
e73ffd42
BV
429 if (sdi->status != SR_ST_ACTIVE)
430 return SR_ERR_DEV_CLOSED;
0ab0cb94 431
310e9e9b 432 if (!(devc = sdi->priv)) {
6d116114 433 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
434 return SR_ERR_ARG;
435 }
a1bb33af 436
584560f1 437 switch (key) {
1953564a 438 case SR_CONF_SAMPLERATE:
8386096f 439 return zp_set_samplerate(devc, g_variant_get_uint64(data));
1953564a 440 case SR_CONF_LIMIT_SAMPLES:
8386096f 441 return set_limit_samples(devc, g_variant_get_uint64(data));
1953564a 442 case SR_CONF_CAPTURE_RATIO:
8386096f 443 return set_capture_ratio(devc, g_variant_get_uint64(data));
7142d6b9
RD
444 case SR_CONF_VOLTAGE_THRESHOLD:
445 g_variant_get(data, "(dd)", &low, &high);
446 return set_voltage_threshold(devc, (low + high) / 2.0);
fed16f06 447 default:
bd6fbf62 448 return SR_ERR_NA;
a1bb33af 449 }
e495a676
UH
450
451 return SR_OK;
a1bb33af
UH
452}
453
584560f1 454static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 455 const struct sr_channel_group *cg)
a1c743fc 456{
17548571 457 struct dev_context *devc;
f0de2dd0 458 GVariant *gvar, *grange[2];
8386096f 459 GVariantBuilder gvb;
7142d6b9
RD
460 double v;
461 GVariant *range[2];
a1c743fc 462
53b4680f 463 (void)cg;
8f996b89 464
a1c743fc 465 switch (key) {
9a6517d1 466 case SR_CONF_DEVICE_OPTIONS:
584560f1 467 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 468 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 469 break;
a1c743fc 470 case SR_CONF_SAMPLERATE:
17548571 471 devc = sdi->priv;
8386096f 472 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
17548571 473 if (devc->prof->max_sampling_freq == 100) {
8386096f
BV
474 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
475 samplerates_100, ARRAY_SIZE(samplerates_100),
476 sizeof(uint64_t));
17548571 477 } else if (devc->prof->max_sampling_freq == 200) {
8386096f
BV
478 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
479 samplerates_200, ARRAY_SIZE(samplerates_200),
480 sizeof(uint64_t));
17548571
UH
481 } else {
482 sr_err("Internal error: Unknown max. samplerate: %d.",
483 devc->prof->max_sampling_freq);
484 return SR_ERR_ARG;
485 }
8386096f
BV
486 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
487 *data = g_variant_builder_end(&gvb);
a1c743fc 488 break;
28731bab
BV
489 case SR_CONF_TRIGGER_MATCH:
490 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
491 trigger_matches, ARRAY_SIZE(trigger_matches),
492 sizeof(int32_t));
c50277a6 493 break;
7142d6b9
RD
494 case SR_CONF_VOLTAGE_THRESHOLD:
495 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
496 for (v = -6.0; v <= 6.0; v += 0.1) {
497 range[0] = g_variant_new_double(v);
498 range[1] = g_variant_new_double(v);
499 gvar = g_variant_new_tuple(range, 2);
500 g_variant_builder_add_value(&gvb, gvar);
501 }
502 *data = g_variant_builder_end(&gvb);
503 break;
f0de2dd0
BV
504 case SR_CONF_LIMIT_SAMPLES:
505 if (!sdi)
506 return SR_ERR_ARG;
507 devc = sdi->priv;
508 grange[0] = g_variant_new_uint64(0);
509 grange[1] = g_variant_new_uint64(devc->max_sample_depth);
510 *data = g_variant_new_tuple(grange, 2);
511 break;
a1c743fc 512 default:
bd6fbf62 513 return SR_ERR_NA;
a1c743fc
BV
514 }
515
516 return SR_OK;
517}
518
6078d2c9 519static int dev_acquisition_start(const struct sr_dev_inst *sdi,
3ffb6964 520 void *cb_data)
a1bb33af 521{
609bfd75
BV
522 struct dev_context *devc;
523 struct sr_usb_dev_inst *usb;
b9c735a2 524 struct sr_datafeed_packet packet;
9c939c51 525 struct sr_datafeed_logic logic;
42ceb777 526 unsigned int samples_read;
a1bb33af 527 int res;
e495a676 528 unsigned int packet_num, n;
a1bb33af 529 unsigned char *buf;
42ceb777
RD
530 unsigned int status;
531 unsigned int stop_address;
532 unsigned int now_address;
533 unsigned int trigger_address;
534 unsigned int trigger_offset;
535 unsigned int triggerbar;
536 unsigned int ramsize_trigger;
537 unsigned int memory_size;
538 unsigned int valid_samples;
539 unsigned int discard;
540 int trigger_now;
a1bb33af 541
e73ffd42
BV
542 if (sdi->status != SR_ST_ACTIVE)
543 return SR_ERR_DEV_CLOSED;
544
310e9e9b 545 if (!(devc = sdi->priv)) {
6d116114 546 sr_err("%s: sdi->priv was NULL", __func__);
bf43ea23
UH
547 return SR_ERR_ARG;
548 }
a1bb33af 549
28731bab
BV
550 if (analyzer_add_triggers(sdi) != SR_OK) {
551 sr_err("Failed to configure triggers.");
014359e3
BV
552 return SR_ERR;
553 }
554
609bfd75
BV
555 usb = sdi->conn;
556
0ab0cb94
TY
557 set_triggerbar(devc);
558
e495a676 559 /* Push configured settings to device. */
609bfd75 560 analyzer_configure(usb->devhdl);
a143e4e5 561
609bfd75 562 analyzer_start(usb->devhdl);
6d116114 563 sr_info("Waiting for data.");
609bfd75 564 analyzer_wait_data(usb->devhdl);
a1bb33af 565
42ceb777
RD
566 status = analyzer_read_status(usb->devhdl);
567 stop_address = analyzer_get_stop_address(usb->devhdl);
568 now_address = analyzer_get_now_address(usb->devhdl);
569 trigger_address = analyzer_get_trigger_address(usb->devhdl);
570
571 triggerbar = analyzer_get_triggerbar_address();
572 ramsize_trigger = analyzer_get_ramsize_trigger_address();
573
574 n = get_memory_size(devc->memory_size);
575 memory_size = n / 4;
576
577 sr_info("Status = 0x%x.", status);
578 sr_info("Stop address = 0x%x.", stop_address);
579 sr_info("Now address = 0x%x.", now_address);
580 sr_info("Trigger address = 0x%x.", trigger_address);
581 sr_info("Triggerbar address = 0x%x.", triggerbar);
582 sr_info("Ramsize trigger = 0x%x.", ramsize_trigger);
583 sr_info("Memory size = 0x%x.", memory_size);
a1bb33af 584
4afdfd46 585 /* Send header packet to the session bus. */
29a27196 586 std_session_send_df_header(cb_data, LOG_PREFIX);
f366e86c 587
42ceb777
RD
588 /* Check for empty capture */
589 if ((status & STATUS_READY) && !stop_address) {
590 packet.type = SR_DF_END;
591 sr_session_send(cb_data, &packet);
592 return SR_OK;
593 }
594
a95f142e 595 buf = g_malloc(PACKET_SIZE);
b53738ba 596
42ceb777
RD
597 /* Check if the trigger is in the samples we are throwing away */
598 trigger_now = now_address == trigger_address ||
599 ((now_address + 1) % memory_size) == trigger_address;
600
601 /*
602 * STATUS_READY doesn't clear until now_address advances past
603 * addr 0, but for our logic, clear it in that case
604 */
605 if (!now_address)
606 status &= ~STATUS_READY;
607
609bfd75 608 analyzer_read_start(usb->devhdl);
42ceb777
RD
609
610 /* Calculate how much data to discard */
611 discard = 0;
612 if (status & STATUS_READY) {
613 /*
614 * We haven't wrapped around, we need to throw away data from
615 * our current position to the end of the buffer.
616 * Additionally, the first two samples captured are always
617 * bogus.
618 */
619 discard += memory_size - now_address + 2;
620 now_address = 2;
621 }
622
623 /* If we have more samples than we need, discard them */
624 valid_samples = (stop_address - now_address) % memory_size;
625 if (valid_samples > ramsize_trigger + triggerbar) {
626 discard += valid_samples - (ramsize_trigger + triggerbar);
627 now_address += valid_samples - (ramsize_trigger + triggerbar);
628 }
629
630 sr_info("Need to discard %d samples.", discard);
631
632 /* Calculate how far in the trigger is */
633 if (trigger_now)
634 trigger_offset = 0;
635 else
636 trigger_offset = (trigger_address - now_address) % memory_size;
637
638 /* Recalculate the number of samples available */
639 valid_samples = (stop_address - now_address) % memory_size;
640
fed16f06 641 /* Send the incoming transfer to the session bus. */
42ceb777 642 samples_read = 0;
0ab0cb94 643 for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) {
42ceb777
RD
644 unsigned int len;
645 unsigned int buf_offset;
646
609bfd75 647 res = analyzer_read_data(usb->devhdl, buf, PACKET_SIZE);
6d116114 648 sr_info("Tried to read %d bytes, actually read %d bytes.",
b08024a8 649 PACKET_SIZE, res);
a1bb33af 650
42ceb777
RD
651 if (discard >= PACKET_SIZE / 4) {
652 discard -= PACKET_SIZE / 4;
653 continue;
654 }
655
656 len = PACKET_SIZE - discard * 4;
657 buf_offset = discard * 4;
658 discard = 0;
659
660 /* Check if we've read all the samples */
661 if (samples_read + len / 4 >= valid_samples)
662 len = (valid_samples - samples_read) * 4;
663 if (!len)
664 break;
665
666 if (samples_read < trigger_offset &&
667 samples_read + len / 4 > trigger_offset) {
668 /* Send out samples remaining before trigger */
669 packet.type = SR_DF_LOGIC;
670 packet.payload = &logic;
671 logic.length = (trigger_offset - samples_read) * 4;
672 logic.unitsize = 4;
673 logic.data = buf + buf_offset;
674 sr_session_send(cb_data, &packet);
675 len -= logic.length;
676 samples_read += logic.length / 4;
677 buf_offset += logic.length;
678 }
679
680 if (samples_read == trigger_offset) {
681 /* Send out trigger */
682 packet.type = SR_DF_TRIGGER;
683 packet.payload = NULL;
684 sr_session_send(cb_data, &packet);
685 }
686
687 /* Send out data (or data after trigger) */
5a2326a7 688 packet.type = SR_DF_LOGIC;
9c939c51 689 packet.payload = &logic;
42ceb777 690 logic.length = len;
9c939c51 691 logic.unitsize = 4;
42ceb777 692 logic.data = buf + buf_offset;
3cd3a20b 693 sr_session_send(cb_data, &packet);
42ceb777 694 samples_read += len / 4;
a1bb33af 695 }
609bfd75 696 analyzer_read_stop(usb->devhdl);
a1bb33af
UH
697 g_free(buf);
698
5a2326a7 699 packet.type = SR_DF_END;
3cd3a20b 700 sr_session_send(cb_data, &packet);
a1bb33af 701
e46b8fb1 702 return SR_OK;
a1bb33af
UH
703}
704
3cd3a20b 705/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
6078d2c9 706static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
a1bb33af 707{
310e9e9b 708 struct dev_context *devc;
609bfd75
BV
709 struct sr_usb_dev_inst *usb;
710 struct sr_datafeed_packet packet;
a1bb33af 711
5a2326a7 712 packet.type = SR_DF_END;
3cd3a20b 713 sr_session_send(cb_data, &packet);
a1bb33af 714
310e9e9b 715 if (!(devc = sdi->priv)) {
6d116114 716 sr_err("%s: sdi->priv was NULL", __func__);
3010f21c 717 return SR_ERR_BUG;
69890f73 718 }
a1bb33af 719
609bfd75
BV
720 usb = sdi->conn;
721 analyzer_reset(usb->devhdl);
fed16f06 722 /* TODO: Need to cancel and free any queued up transfers. */
3010f21c
UH
723
724 return SR_OK;
a1bb33af
UH
725}
726
c09f0b57 727SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = {
e519ba86 728 .name = "zeroplus-logic-cube",
8fdecced 729 .longname = "ZEROPLUS Logic Cube LAP-C series",
e519ba86 730 .api_version = 1,
6078d2c9
UH
731 .init = init,
732 .cleanup = cleanup,
733 .scan = scan,
734 .dev_list = dev_list,
a6630742 735 .dev_clear = NULL,
035a1078
BV
736 .config_get = config_get,
737 .config_set = config_set,
a1c743fc 738 .config_list = config_list,
6078d2c9
UH
739 .dev_open = dev_open,
740 .dev_close = dev_close,
741 .dev_acquisition_start = dev_acquisition_start,
742 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 743 .context = NULL,
a1bb33af 744};