]> sigrok.org Git - libsigrok.git/blame - src/hardware/chronovu-la/api.c
chronovu-la: rephrase conn= checking in the scan routine
[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 128 struct drv_context *drvc;
204dd31f
GS
129 GSList *devices;
130 const char *conn;
131 int ret;
132 GSList *conn_devices, *l;
133 size_t i;
67f890d5 134 struct sr_usb_dev_inst *usb;
204dd31f 135 uint8_t bus, addr;
67f890d5
UH
136 struct libusb_device_descriptor des;
137 libusb_device **devlist;
138 struct libusb_device_handle *hdl;
67f890d5 139 char product[64], serial_num[64], connection_id[64];
204dd31f 140 int model;
00910580 141
67f890d5 142 drvc = di->context;
204dd31f 143 devices = NULL;
67f890d5
UH
144
145 conn = NULL;
204dd31f
GS
146 (void)sr_serial_extract_options(options, &conn, NULL);
147 conn_devices = NULL;
67f890d5
UH
148 if (conn)
149 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
00910580 150
67f890d5 151 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
67f890d5 152 for (i = 0; devlist[i]; i++) {
204dd31f
GS
153 bus = libusb_get_bus_number(devlist[i]);
154 addr = libusb_get_device_address(devlist[i]);
67f890d5 155 if (conn) {
204dd31f 156 /* Check if the connection matches the user spec. */
67f890d5
UH
157 for (l = conn_devices; l; l = l->next) {
158 usb = l->data;
204dd31f 159 if (usb->bus == bus && usb->address == addr)
67f890d5
UH
160 break;
161 }
162 if (!l)
67f890d5
UH
163 continue;
164 }
165
166 libusb_get_device_descriptor(devlist[i], &des);
167
64414853
MA
168 /* See https://sigrok.org/bugzilla/show_bug.cgi?id=1115 and
169 * https://github.com/sigrokproject/libsigrok/pull/166 */
170 if (des.idVendor != CHRONOVU_VENDOR)
171 continue;
172
67f890d5
UH
173 if ((ret = libusb_open(devlist[i], &hdl)) < 0)
174 continue;
175
176 if (des.iProduct == 0) {
177 product[0] = '\0';
178 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
179 des.iProduct, (unsigned char *)product,
180 sizeof(product))) < 0) {
181 sr_warn("Failed to get product string descriptor: %s.",
182 libusb_error_name(ret));
183 continue;
184 }
185
186 if (des.iSerialNumber == 0) {
187 serial_num[0] = '\0';
188 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
189 des.iSerialNumber, (unsigned char *)serial_num,
190 sizeof(serial_num))) < 0) {
191 sr_warn("Failed to get serial number string descriptor: %s.",
192 libusb_error_name(ret));
193 continue;
194 }
195
67f890d5
UH
196 libusb_close(hdl);
197
6c1a76d1
RT
198 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
199 continue;
7bbe5a2b 200
7bf81cb7 201 if (!strcmp(product, "ChronoVu LA8"))
67f890d5 202 model = 0;
7bf81cb7 203 else if (!strcmp(product, "ChronoVu LA16"))
67f890d5 204 model = 1;
7bf81cb7
UH
205 else
206 continue; /* Unknown iProduct string, ignore. */
67f890d5
UH
207
208 sr_dbg("Found %s (%04x:%04x, %d.%d, %s).",
209 product, des.idVendor, des.idProduct,
210 libusb_get_bus_number(devlist[i]),
211 libusb_get_device_address(devlist[i]), connection_id);
212
15a5bfe4 213 if ((ret = add_device(model, &des, serial_num, connection_id,
67f890d5 214 devlist[i], &devices)) < 0) {
00910580 215 sr_dbg("Failed to add device: %d.", ret);
67f890d5 216 }
00910580 217 }
67f890d5
UH
218 libusb_free_device_list(devlist, 1);
219 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
00910580 220
15a5bfe4 221 return std_scan_complete(di, devices);
b908f067
UH
222}
223
6078d2c9 224static int dev_open(struct sr_dev_inst *sdi)
b908f067 225{
1644fb24 226 struct dev_context *devc;
25a0f108 227 int ret;
b908f067 228
61c90858 229 devc = sdi->priv;
b908f067 230
00910580
UH
231 if (!(devc->ftdic = ftdi_new())) {
232 sr_err("Failed to initialize libftdi.");
233 return SR_ERR;
234 }
235
236 sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname,
237 devc->usb_vid, devc->usb_pid);
b908f067 238
00910580
UH
239 if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid,
240 devc->usb_pid, devc->prof->iproduct, NULL)) < 0) {
b172c130
UH
241 sr_err("Failed to open FTDI device (%d): %s.",
242 ret, ftdi_get_error_string(devc->ftdic));
00910580 243 goto err_ftdi_free;
b908f067 244 }
b908f067 245
e1a712ca 246 if ((ret = PURGE_FTDI_BOTH(devc->ftdic)) < 0) {
b172c130
UH
247 sr_err("Failed to purge FTDI buffers (%d): %s.",
248 ret, ftdi_get_error_string(devc->ftdic));
00910580 249 goto err_ftdi_free;
b908f067 250 }
b908f067 251
1644fb24 252 if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) {
b172c130
UH
253 sr_err("Failed to enable FTDI flow control (%d): %s.",
254 ret, ftdi_get_error_string(devc->ftdic));
00910580 255 goto err_ftdi_free;
b908f067 256 }
b908f067 257
b908f067
UH
258 g_usleep(100 * 1000);
259
7e463623 260 return SR_OK;
b908f067 261
00910580
UH
262err_ftdi_free:
263 ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
264 devc->ftdic = NULL;
7e463623 265 return SR_ERR;
b908f067
UH
266}
267
6078d2c9 268static int dev_close(struct sr_dev_inst *sdi)
b908f067 269{
00910580 270 int ret;
1644fb24 271 struct dev_context *devc;
b908f067 272
00910580 273 devc = sdi->priv;
b908f067 274
f1ba6b4b
UH
275 if (!devc->ftdic)
276 return SR_ERR_BUG;
277
278 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
00910580
UH
279 sr_err("Failed to close FTDI device (%d): %s.",
280 ret, ftdi_get_error_string(devc->ftdic));
093e1cba 281
f1ba6b4b 282 return (ret == 0) ? SR_OK : SR_ERR;
b908f067
UH
283}
284
dd7a72ea
UH
285static int config_get(uint32_t key, GVariant **data,
286 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
b908f067 287{
1644fb24 288 struct dev_context *devc;
67f890d5 289 struct sr_usb_dev_inst *usb;
b908f067 290
53b4680f 291 (void)cg;
8f996b89 292
584560f1 293 switch (key) {
67f890d5
UH
294 case SR_CONF_CONN:
295 if (!sdi || !(usb = sdi->conn))
296 return SR_ERR_ARG;
95c1fe62 297 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
67f890d5 298 break;
123e1313 299 case SR_CONF_SAMPLERATE:
709468ba 300 if (!sdi)
b172c130 301 return SR_ERR_BUG;
709468ba 302 devc = sdi->priv;
b172c130 303 *data = g_variant_new_uint64(devc->cur_samplerate);
b908f067 304 break;
cfe8a84d 305 default:
bd6fbf62 306 return SR_ERR_NA;
b908f067
UH
307 }
308
6a2761fd 309 return SR_OK;
b908f067
UH
310}
311
dd7a72ea
UH
312static int config_set(uint32_t key, GVariant *data,
313 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
b908f067 314{
1644fb24 315 struct dev_context *devc;
b908f067 316
53b4680f 317 (void)cg;
8f996b89 318
b0baddef 319 devc = sdi->priv;
b908f067 320
584560f1 321 switch (key) {
1953564a 322 case SR_CONF_SAMPLERATE:
00910580 323 if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0)
b908f067 324 return SR_ERR;
b908f067 325 break;
1953564a 326 case SR_CONF_LIMIT_MSEC:
1bec72d2 327 devc->limit_msec = g_variant_get_uint64(data);
b908f067 328 break;
1953564a 329 case SR_CONF_LIMIT_SAMPLES:
1bec72d2 330 devc->limit_samples = g_variant_get_uint64(data);
b908f067
UH
331 break;
332 default:
bd6fbf62 333 return SR_ERR_NA;
b908f067
UH
334 }
335
336 return SR_OK;
337}
338
dd7a72ea
UH
339static int config_list(uint32_t key, GVariant **data,
340 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
a1c743fc 341{
00910580 342 struct dev_context *devc;
a1c743fc 343
e66d1892 344 devc = (sdi) ? sdi->priv : NULL;
a1c743fc
BV
345
346 switch (key) {
67f890d5 347 case SR_CONF_SCAN_OPTIONS:
67f890d5 348 case SR_CONF_DEVICE_OPTIONS:
e66d1892 349 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
a1c743fc 350 case SR_CONF_SAMPLERATE:
00910580 351 cv_fill_samplerates_if_needed(sdi);
53012da6 352 *data = std_gvar_samplerates(ARRAY_AND_SIZE(devc->samplerates));
a1c743fc 353 break;
f0de2dd0 354 case SR_CONF_LIMIT_SAMPLES:
755eb221 355 if (!devc || !devc->prof)
69bdcd8b 356 return SR_ERR_BUG;
a162eeb2 357 *data = std_gvar_tuple_u64(0, (devc->prof->model == CHRONOVU_LA8) ? MAX_NUM_SAMPLES : MAX_NUM_SAMPLES / 2);
f0de2dd0 358 break;
aeff7fa2 359 case SR_CONF_TRIGGER_MATCH:
755eb221 360 if (!devc || !devc->prof)
13dd25bd 361 return SR_ERR_BUG;
105df674 362 *data = std_gvar_array_i32(trigger_matches, devc->prof->num_trigger_matches);
c50277a6 363 break;
a1c743fc 364 default:
bd6fbf62 365 return SR_ERR_NA;
a1c743fc
BV
366 }
367
368 return SR_OK;
369}
370
b908f067
UH
371static int receive_data(int fd, int revents, void *cb_data)
372{
373 int i, ret;
374 struct sr_dev_inst *sdi;
1644fb24 375 struct dev_context *devc;
b908f067 376
b908f067
UH
377 (void)fd;
378 (void)revents;
379
380 if (!(sdi = cb_data)) {
b172c130 381 sr_err("cb_data was NULL.");
b908f067
UH
382 return FALSE;
383 }
384
1644fb24 385 if (!(devc = sdi->priv)) {
b172c130 386 sr_err("sdi->priv was NULL.");
b908f067
UH
387 return FALSE;
388 }
389
1644fb24 390 if (!devc->ftdic) {
b172c130 391 sr_err("devc->ftdic was NULL.");
b908f067
UH
392 return FALSE;
393 }
394
395 /* Get one block of data. */
b172c130
UH
396 if ((ret = cv_read_block(devc)) < 0) {
397 sr_err("Failed to read data block: %d.", ret);
d2f7c417 398 sr_dev_acquisition_stop(sdi);
b908f067
UH
399 return FALSE;
400 }
401
402 /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */
1644fb24
BV
403 if (devc->block_counter != (NUM_BLOCKS - 1)) {
404 devc->block_counter++;
b908f067
UH
405 return TRUE;
406 }
407
f3a35908 408 sr_dbg("Sampling finished, sending data to session bus now.");
b908f067 409
b0efc84e
UH
410 /*
411 * All data was received and demangled, send it to the session bus.
412 *
413 * Note: Due to the method how data is spread across the 8MByte of
414 * SDRAM, we can _not_ send it to the session bus in a streaming
415 * manner while we receive it. We have to receive and de-mangle the
416 * full 8MByte first, only then the whole buffer contains valid data.
417 */
b908f067 418 for (i = 0; i < NUM_BLOCKS; i++)
695dc859 419 cv_send_block_to_session_bus(sdi, i);
b908f067 420
d2f7c417 421 sr_dev_acquisition_stop(sdi);
b908f067 422
b908f067
UH
423 return TRUE;
424}
425
695dc859 426static int dev_acquisition_start(const struct sr_dev_inst *sdi)
b908f067 427{
1644fb24 428 struct dev_context *devc;
00910580
UH
429 uint8_t buf[8];
430 int bytes_to_write, bytes_written;
b908f067 431
208c1d35 432 devc = sdi->priv;
b908f067 433
1644fb24 434 if (!devc->ftdic) {
b172c130 435 sr_err("devc->ftdic was NULL.");
b908f067
UH
436 return SR_ERR_BUG;
437 }
438
00910580 439 devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate);
1644fb24 440 if (devc->divcount == 0xff) {
b172c130 441 sr_err("Invalid divcount/samplerate.");
b908f067
UH
442 return SR_ERR;
443 }
444
aeff7fa2
BV
445 if (cv_convert_trigger(sdi) != SR_OK) {
446 sr_err("Failed to configure trigger.");
014359e3
BV
447 return SR_ERR;
448 }
449
b908f067 450 /* Fill acquisition parameters into buf[]. */
00910580
UH
451 if (devc->prof->model == CHRONOVU_LA8) {
452 buf[0] = devc->divcount;
453 buf[1] = 0xff; /* This byte must always be 0xff. */
454 buf[2] = devc->trigger_pattern & 0xff;
455 buf[3] = devc->trigger_mask & 0xff;
456 bytes_to_write = 4;
457 } else {
458 buf[0] = devc->divcount;
459 buf[1] = 0xff; /* This byte must always be 0xff. */
460 buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */
461 buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */
462 buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */
463 buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */
464 buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */
465 buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */
466 bytes_to_write = 8;
467 }
b908f067
UH
468
469 /* Start acquisition. */
00910580 470 bytes_written = cv_write(devc, buf, bytes_to_write);
b908f067 471
00910580
UH
472 if (bytes_written < 0 || bytes_written != bytes_to_write) {
473 sr_err("Acquisition failed to start.");
afc88319 474 return SR_ERR;
b908f067
UH
475 }
476
bee2b016 477 std_session_send_df_header(sdi);
b908f067 478
b908f067 479 /* Time when we should be done (for detecting trigger timeouts). */
00910580
UH
480 devc->done = (devc->divcount + 1) * devc->prof->trigger_constant +
481 g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND);
1644fb24
BV
482 devc->block_counter = 0;
483 devc->trigger_found = 0;
b908f067 484
b172c130 485 /* Hook up a dummy handler to receive data from the device. */
c650d3ec 486 sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi);
b908f067
UH
487
488 return SR_OK;
489}
490
695dc859 491static int dev_acquisition_stop(struct sr_dev_inst *sdi)
b908f067 492{
102f1239 493 sr_session_source_remove(sdi->session, -1);
bee2b016 494 std_session_send_df_end(sdi);
b908f067
UH
495
496 return SR_OK;
497}
498
dd5c48a6 499static struct sr_dev_driver chronovu_la_driver_info = {
7b356712
UH
500 .name = "chronovu-la",
501 .longname = "ChronoVu LA8/LA16",
b908f067 502 .api_version = 1,
c2fdcc25 503 .init = std_init,
700d6b64 504 .cleanup = std_cleanup,
6078d2c9 505 .scan = scan,
c01bf34c 506 .dev_list = std_dev_list,
3b412e3a 507 .dev_clear = dev_clear,
035a1078
BV
508 .config_get = config_get,
509 .config_set = config_set,
a1c743fc 510 .config_list = config_list,
6078d2c9
UH
511 .dev_open = dev_open,
512 .dev_close = dev_close,
513 .dev_acquisition_start = dev_acquisition_start,
514 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 515 .context = NULL,
b908f067 516};
dd5c48a6 517SR_REGISTER_DEV_DRIVER(chronovu_la_driver_info);