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