]> sigrok.org Git - libsigrok.git/blame - src/hardware/chronovu-la/api.c
juntek-jds6600: implement device support, tested with Joy-IT JDS6600
[libsigrok.git] / src / hardware / chronovu-la / api.c
CommitLineData
b908f067 1/*
50985c20 2 * This file is part of the libsigrok project.
b908f067 3 *
67f890d5 4 * Copyright (C) 2011-2015 Uwe Hermann <uwe@hermann-uwe.de>
b908f067
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 2 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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
b908f067
UH
18 */
19
6ec6c43b 20#include <config.h>
45e080b6 21#include "protocol.h"
b908f067 22
64414853
MA
23#define CHRONOVU_VENDOR (0x0403)
24
67f890d5
UH
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
27};
28
55fb76b3
UH
29static const uint32_t drvopts[] = {
30 SR_CONF_LOGIC_ANALYZER,
31};
32
67f890d5 33static const uint32_t devopts[] = {
5827f61b
BV
34 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
67f890d5 36 SR_CONF_CONN | SR_CONF_GET,
5827f61b
BV
37 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
1bec72d2
BV
39};
40
aeff7fa2
BV
41static const int32_t trigger_matches[] = {
42 SR_TRIGGER_ZERO,
43 SR_TRIGGER_ONE,
44 SR_TRIGGER_RISING,
45 SR_TRIGGER_FALLING,
46};
47
3553451f 48static void clear_helper(struct dev_context *devc)
c4f3ed4b 49{
97900799
UH
50 ftdi_free(devc->ftdic);
51 g_free(devc->final_buf);
52}
c4f3ed4b 53
4f840ce9 54static int dev_clear(const struct sr_dev_driver *di)
97900799 55{
3553451f 56 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
c4f3ed4b
BV
57}
58
15a5bfe4
LPC
59static int add_device(int model, struct libusb_device_descriptor *des,
60 const char *serial_num, const char *connection_id, libusb_device *usbdev,
61 GSList **devices)
b908f067 62{
00910580
UH
63 int ret;
64 unsigned int i;
b908f067 65 struct sr_dev_inst *sdi;
1644fb24 66 struct dev_context *devc;
c4f3ed4b 67
00910580 68 ret = SR_OK;
f3a35908 69
f57d8ffe 70 devc = g_malloc0(sizeof(struct dev_context));
b908f067
UH
71
72 /* Set some sane defaults. */
00910580
UH
73 devc->prof = &cv_profiles[model];
74 devc->ftdic = NULL; /* Will be set in the open() API call. */
75 devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */
1644fb24
BV
76 devc->limit_msec = 0;
77 devc->limit_samples = 0;
1644fb24
BV
78 memset(devc->mangled_buf, 0, BS);
79 devc->final_buf = NULL;
00910580
UH
80 devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */
81 devc->trigger_mask = 0x0000; /* All channels: "don't care". */
82 devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */
1644fb24
BV
83 devc->trigger_found = 0;
84 devc->done = 0;
85 devc->block_counter = 0;
00910580 86 devc->divcount = 0;
67f890d5
UH
87 devc->usb_vid = des->idVendor;
88 devc->usb_pid = des->idProduct;
00910580 89 memset(devc->samplerates, 0, sizeof(uint64_t) * 255);
b908f067
UH
90
91 /* Allocate memory where we'll store the de-mangled data. */
1644fb24 92 if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) {
b172c130 93 sr_err("Failed to allocate memory for sample buffer.");
00910580 94 ret = SR_ERR_MALLOC;
1644fb24 95 goto err_free_devc;
b908f067
UH
96 }
97
00910580
UH
98 /* We now know the device, set its max. samplerate as default. */
99 devc->cur_samplerate = devc->prof->max_samplerate;
b908f067 100
aac29cc1 101 sdi = g_malloc0(sizeof(struct sr_dev_inst));
45884333 102 sdi->status = SR_ST_INACTIVE;
0af636be
UH
103 sdi->vendor = g_strdup("ChronoVu");
104 sdi->model = g_strdup(devc->prof->modelname);
67f890d5
UH
105 sdi->serial_num = g_strdup(serial_num);
106 sdi->connection_id = g_strdup(connection_id);
107 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev),
108 libusb_get_device_address(usbdev), NULL);
1644fb24 109 sdi->priv = devc;
b908f067 110
5e23fcab
ML
111 for (i = 0; i < devc->prof->num_channels; i++)
112 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
0f34cb47 113 cv_channel_names[i]);
87ca93c5 114
00910580 115 *devices = g_slist_append(*devices, sdi);
b908f067 116
65c8d48f
BV
117 if (ret == SR_OK)
118 return SR_OK;
b908f067 119
1644fb24
BV
120err_free_devc:
121 g_free(devc);
b908f067 122
00910580
UH
123 return ret;
124}
125
4f840ce9 126static GSList *scan(struct sr_dev_driver *di, GSList *options)
00910580 127{
67f890d5
UH
128 int i, ret, model;
129 struct drv_context *drvc;
130 GSList *devices, *conn_devices, *l;
131 struct sr_usb_dev_inst *usb;
132 struct sr_config *src;
133 struct libusb_device_descriptor des;
134 libusb_device **devlist;
135 struct libusb_device_handle *hdl;
136 const char *conn;
137 char product[64], serial_num[64], connection_id[64];
00910580 138
67f890d5 139 drvc = di->context;
67f890d5
UH
140
141 conn = NULL;
142 for (l = options; l; l = l->next) {
143 src = l->data;
144 switch (src->key) {
145 case SR_CONF_CONN:
146 conn = g_variant_get_string(src->data, NULL);
147 break;
148 }
00910580 149 }
67f890d5
UH
150 if (conn)
151 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
152 else
153 conn_devices = NULL;
00910580 154
67f890d5
UH
155 devices = NULL;
156 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
157
158 for (i = 0; devlist[i]; i++) {
159 if (conn) {
160 for (l = conn_devices; l; l = l->next) {
161 usb = l->data;
162 if (usb->bus == libusb_get_bus_number(devlist[i])
163 && usb->address == libusb_get_device_address(devlist[i]))
164 break;
165 }
166 if (!l)
167 /* This device matched none of the ones that
168 * matched the conn specification. */
169 continue;
170 }
171
172 libusb_get_device_descriptor(devlist[i], &des);
173
64414853
MA
174 /* See https://sigrok.org/bugzilla/show_bug.cgi?id=1115 and
175 * https://github.com/sigrokproject/libsigrok/pull/166 */
176 if (des.idVendor != CHRONOVU_VENDOR)
177 continue;
178
67f890d5
UH
179 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
180 continue;
181
182 if (des.iProduct == 0) {
183 product[0] = '\0';
184 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
185 des.iProduct, (unsigned char *)product,
186 sizeof(product))) < 0) {
187 sr_warn("Failed to get product string descriptor: %s.",
188 libusb_error_name(ret));
189 continue;
190 }
191
192 if (des.iSerialNumber == 0) {
193 serial_num[0] = '\0';
194 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
195 des.iSerialNumber, (unsigned char *)serial_num,
196 sizeof(serial_num))) < 0) {
197 sr_warn("Failed to get serial number string descriptor: %s.",
198 libusb_error_name(ret));
199 continue;
200 }
201
67f890d5
UH
202 libusb_close(hdl);
203
6c1a76d1
RT
204 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
205 continue;
7bbe5a2b 206
7bf81cb7 207 if (!strcmp(product, "ChronoVu LA8"))
67f890d5 208 model = 0;
7bf81cb7 209 else if (!strcmp(product, "ChronoVu LA16"))
67f890d5 210 model = 1;
7bf81cb7
UH
211 else
212 continue; /* Unknown iProduct string, ignore. */
67f890d5
UH
213
214 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
215 product, des.idVendor, des.idProduct,
216 libusb_get_bus_number(devlist[i]),
217 libusb_get_device_address(devlist[i]), connection_id);
218
15a5bfe4 219 if ((ret = add_device(model, &des, serial_num, connection_id,
67f890d5 220 devlist[i], &devices)) < 0) {
00910580 221 sr_dbg("Failed to add device: %d.", ret);
67f890d5 222 }
00910580
UH
223 }
224
67f890d5
UH
225 libusb_free_device_list(devlist, 1);
226 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
00910580 227
15a5bfe4 228 return std_scan_complete(di, devices);
b908f067
UH
229}
230
6078d2c9 231static int dev_open(struct sr_dev_inst *sdi)
b908f067 232{
1644fb24 233 struct dev_context *devc;
25a0f108 234 int ret;
b908f067 235
61c90858 236 devc = sdi->priv;
b908f067 237
00910580
UH
238 if (!(devc->ftdic = ftdi_new())) {
239 sr_err("Failed to initialize libftdi.");
240 return SR_ERR;
241 }
242
243 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
244 devc->usb_vid, devc->usb_pid);
b908f067 245
00910580
UH
246 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
247 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
b172c130
UH
248 sr_err("Failed to open FTDI device (%d): %s.",
249 ret, ftdi_get_error_string(devc->ftdic));
00910580 250 goto err_ftdi_free;
b908f067 251 }
b908f067 252
e1a712ca 253 if ((ret = PURGE_FTDI_BOTH(devc->ftdic)) < 0) {
b172c130
UH
254 sr_err("Failed to purge FTDI buffers (%d): %s.",
255 ret, ftdi_get_error_string(devc->ftdic));
00910580 256 goto err_ftdi_free;
b908f067 257 }
b908f067 258
1644fb24 259 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
b172c130
UH
260 sr_err("Failed to enable FTDI flow control (%d): %s.",
261 ret, ftdi_get_error_string(devc->ftdic));
00910580 262 goto err_ftdi_free;
b908f067 263 }
b908f067 264
b908f067
UH
265 g_usleep(100 * 1000);
266
7e463623 267 return SR_OK;
b908f067 268
00910580
UH
269err_ftdi_free:
270 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
271 devc->ftdic = NULL;
7e463623 272 return SR_ERR;
b908f067
UH
273}
274
6078d2c9 275static int dev_close(struct sr_dev_inst *sdi)
b908f067 276{
00910580 277 int ret;
1644fb24 278 struct dev_context *devc;
b908f067 279
00910580 280 devc = sdi->priv;
b908f067 281
f1ba6b4b
UH
282 if (!devc->ftdic)
283 return SR_ERR_BUG;
284
285 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
00910580
UH
286 sr_err("Failed to close FTDI device (%d): %s.",
287 ret, ftdi_get_error_string(devc->ftdic));
093e1cba 288
f1ba6b4b 289 return (ret == 0) ? SR_OK : SR_ERR;
b908f067
UH
290}
291
dd7a72ea
UH
292static int config_get(uint32_t key, GVariant **data,
293 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
b908f067 294{
1644fb24 295 struct dev_context *devc;
67f890d5 296 struct sr_usb_dev_inst *usb;
b908f067 297
53b4680f 298 (void)cg;
8f996b89 299
584560f1 300 switch (key) {
67f890d5
UH
301 case SR_CONF_CONN:
302 if (!sdi || !(usb = sdi->conn))
303 return SR_ERR_ARG;
95c1fe62 304 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
67f890d5 305 break;
123e1313 306 case SR_CONF_SAMPLERATE:
709468ba 307 if (!sdi)
b172c130 308 return SR_ERR_BUG;
709468ba 309 devc = sdi->priv;
b172c130 310 *data = g_variant_new_uint64(devc->cur_samplerate);
b908f067 311 break;
cfe8a84d 312 default:
bd6fbf62 313 return SR_ERR_NA;
b908f067
UH
314 }
315
6a2761fd 316 return SR_OK;
b908f067
UH
317}
318
dd7a72ea
UH
319static int config_set(uint32_t key, GVariant *data,
320 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
b908f067 321{
1644fb24 322 struct dev_context *devc;
b908f067 323
53b4680f 324 (void)cg;
8f996b89 325
b0baddef 326 devc = sdi->priv;
b908f067 327
584560f1 328 switch (key) {
1953564a 329 case SR_CONF_SAMPLERATE:
00910580 330 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
b908f067 331 return SR_ERR;
b908f067 332 break;
1953564a 333 case SR_CONF_LIMIT_MSEC:
1bec72d2 334 devc->limit_msec = g_variant_get_uint64(data);
b908f067 335 break;
1953564a 336 case SR_CONF_LIMIT_SAMPLES:
1bec72d2 337 devc->limit_samples = g_variant_get_uint64(data);
b908f067
UH
338 break;
339 default:
bd6fbf62 340 return SR_ERR_NA;
b908f067
UH
341 }
342
343 return SR_OK;
344}
345
dd7a72ea
UH
346static int config_list(uint32_t key, GVariant **data,
347 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a1c743fc 348{
00910580 349 struct dev_context *devc;
a1c743fc 350
e66d1892 351 devc = (sdi) ? sdi->priv : NULL;
a1c743fc
BV
352
353 switch (key) {
67f890d5 354 case SR_CONF_SCAN_OPTIONS:
67f890d5 355 case SR_CONF_DEVICE_OPTIONS:
e66d1892 356 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
a1c743fc 357 case SR_CONF_SAMPLERATE:
00910580 358 cv_fill_samplerates_if_needed(sdi);
53012da6 359 *data = std_gvar_samplerates(ARRAY_AND_SIZE(devc->samplerates));
a1c743fc 360 break;
f0de2dd0 361 case SR_CONF_LIMIT_SAMPLES:
755eb221 362 if (!devc || !devc->prof)
69bdcd8b 363 return SR_ERR_BUG;
a162eeb2 364 *data = std_gvar_tuple_u64(0, (devc->prof->model == CHRONOVU_LA8) ? MAX_NUM_SAMPLES : MAX_NUM_SAMPLES / 2);
f0de2dd0 365 break;
aeff7fa2 366 case SR_CONF_TRIGGER_MATCH:
755eb221 367 if (!devc || !devc->prof)
13dd25bd 368 return SR_ERR_BUG;
105df674 369 *data = std_gvar_array_i32(trigger_matches, devc->prof->num_trigger_matches);
c50277a6 370 break;
a1c743fc 371 default:
bd6fbf62 372 return SR_ERR_NA;
a1c743fc
BV
373 }
374
375 return SR_OK;
376}
377
b908f067
UH
378static int receive_data(int fd, int revents, void *cb_data)
379{
380 int i, ret;
381 struct sr_dev_inst *sdi;
1644fb24 382 struct dev_context *devc;
b908f067 383
b908f067
UH
384 (void)fd;
385 (void)revents;
386
387 if (!(sdi = cb_data)) {
b172c130 388 sr_err("cb_data was NULL.");
b908f067
UH
389 return FALSE;
390 }
391
1644fb24 392 if (!(devc = sdi->priv)) {
b172c130 393 sr_err("sdi->priv was NULL.");
b908f067
UH
394 return FALSE;
395 }
396
1644fb24 397 if (!devc->ftdic) {
b172c130 398 sr_err("devc->ftdic was NULL.");
b908f067
UH
399 return FALSE;
400 }
401
402 /* Get one block of data. */
b172c130
UH
403 if ((ret = cv_read_block(devc)) < 0) {
404 sr_err("Failed to read data block: %d.", ret);
d2f7c417 405 sr_dev_acquisition_stop(sdi);
b908f067
UH
406 return FALSE;
407 }
408
409 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1644fb24
BV
410 if (devc->block_counter != (NUM_BLOCKS - 1)) {
411 devc->block_counter++;
b908f067
UH
412 return TRUE;
413 }
414
f3a35908 415 sr_dbg("Sampling finished, sending data to session bus now.");
b908f067 416
b0efc84e
UH
417 /*
418 * All data was received and demangled, send it to the session bus.
419 *
420 * Note: Due to the method how data is spread across the 8MByte of
421 * SDRAM, we can _not_ send it to the session bus in a streaming
422 * manner while we receive it. We have to receive and de-mangle the
423 * full 8MByte first, only then the whole buffer contains valid data.
424 */
b908f067 425 for (i = 0; i < NUM_BLOCKS; i++)
695dc859 426 cv_send_block_to_session_bus(sdi, i);
b908f067 427
d2f7c417 428 sr_dev_acquisition_stop(sdi);
b908f067 429
b908f067
UH
430 return TRUE;
431}
432
695dc859 433static int dev_acquisition_start(const struct sr_dev_inst *sdi)
b908f067 434{
1644fb24 435 struct dev_context *devc;
00910580
UH
436 uint8_t buf[8];
437 int bytes_to_write, bytes_written;
b908f067 438
208c1d35 439 devc = sdi->priv;
b908f067 440
1644fb24 441 if (!devc->ftdic) {
b172c130 442 sr_err("devc->ftdic was NULL.");
b908f067
UH
443 return SR_ERR_BUG;
444 }
445
00910580 446 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
1644fb24 447 if (devc->divcount == 0xff) {
b172c130 448 sr_err("Invalid divcount/samplerate.");
b908f067
UH
449 return SR_ERR;
450 }
451
aeff7fa2
BV
452 if (cv_convert_trigger(sdi) != SR_OK) {
453 sr_err("Failed to configure trigger.");
014359e3
BV
454 return SR_ERR;
455 }
456
b908f067 457 /* Fill acquisition parameters into buf[]. */
00910580
UH
458 if (devc->prof->model == CHRONOVU_LA8) {
459 buf[0] = devc->divcount;
460 buf[1] = 0xff; /* This byte must always be 0xff. */
461 buf[2] = devc->trigger_pattern & 0xff;
462 buf[3] = devc->trigger_mask & 0xff;
463 bytes_to_write = 4;
464 } else {
465 buf[0] = devc->divcount;
466 buf[1] = 0xff; /* This byte must always be 0xff. */
467 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
468 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
469 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
470 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
471 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
472 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
473 bytes_to_write = 8;
474 }
b908f067
UH
475
476 /* Start acquisition. */
00910580 477 bytes_written = cv_write(devc, buf, bytes_to_write);
b908f067 478
00910580
UH
479 if (bytes_written < 0 || bytes_written != bytes_to_write) {
480 sr_err("Acquisition failed to start.");
afc88319 481 return SR_ERR;
b908f067
UH
482 }
483
bee2b016 484 std_session_send_df_header(sdi);
b908f067 485
b908f067 486 /* Time when we should be done (for detecting trigger timeouts). */
00910580
UH
487 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
488 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
1644fb24
BV
489 devc->block_counter = 0;
490 devc->trigger_found = 0;
b908f067 491
b172c130 492 /* Hook up a dummy handler to receive data from the device. */
c650d3ec 493 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
b908f067
UH
494
495 return SR_OK;
496}
497
695dc859 498static int dev_acquisition_stop(struct sr_dev_inst *sdi)
b908f067 499{
102f1239 500 sr_session_source_remove(sdi->session, -1);
bee2b016 501 std_session_send_df_end(sdi);
b908f067
UH
502
503 return SR_OK;
504}
505
dd5c48a6 506static struct sr_dev_driver chronovu_la_driver_info = {
7b356712
UH
507 .name = "chronovu-la",
508 .longname = "ChronoVu LA8/LA16",
b908f067 509 .api_version = 1,
c2fdcc25 510 .init = std_init,
700d6b64 511 .cleanup = std_cleanup,
6078d2c9 512 .scan = scan,
c01bf34c 513 .dev_list = std_dev_list,
3b412e3a 514 .dev_clear = dev_clear,
035a1078
BV
515 .config_get = config_get,
516 .config_set = config_set,
a1c743fc 517 .config_list = config_list,
6078d2c9
UH
518 .dev_open = dev_open,
519 .dev_close = dev_close,
520 .dev_acquisition_start = dev_acquisition_start,
521 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 522 .context = NULL,
b908f067 523};
dd5c48a6 524SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);