]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/api.c
Change sr_dev_inst_new() to take no parameters.
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
CommitLineData
aeaad0b0
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
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 "protocol.h"
5874e88d
DE
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23#include <glib.h>
24#include <libusb.h>
25#include <stdlib.h>
26#include <string.h>
27
a0e0bb41 28static const uint32_t scanopts[] = {
99c76642
DE
29 SR_CONF_CONN,
30};
31
f254bc4b 32static const uint32_t devopts[] = {
5874e88d 33 SR_CONF_LOGIC_ANALYZER,
5827f61b
BV
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
40 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
5874e88d
DE
42};
43
bbe7e48a
BV
44static const int32_t trigger_matches[] = {
45 SR_TRIGGER_ZERO,
46 SR_TRIGGER_ONE,
47 SR_TRIGGER_RISING,
48 SR_TRIGGER_FALLING,
49};
50
5874e88d
DE
51/* The hardware supports more samplerates than these, but these are the
52 * options hardcoded into the vendor's Windows GUI.
53 */
54static const uint64_t samplerates[] = {
55 SR_MHZ(125), SR_MHZ(100),
56 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
57 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
58 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
59 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
60 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
61 SR_HZ(500), SR_HZ(200), SR_HZ(100),
62};
aeaad0b0 63
e6e54bd2
DE
64/* Names assigned to available trigger sources. Indices must match
65 * trigger_source enum values.
66 */
67static const char *const trigger_source_names[] = { "CH", "TRG" };
68
69/* Names assigned to available trigger slope choices. Indices must
6358f0a9 70 * match the signal_edge enum values.
e6e54bd2 71 */
6358f0a9 72static const char *const signal_edge_names[] = { "r", "f" };
e6e54bd2 73
aeaad0b0 74SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
5874e88d 75static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
aeaad0b0
DE
76
77static int init(struct sr_context *sr_ctx)
78{
79 return std_init(sr_ctx, di, LOG_PREFIX);
80}
81
ba7dd8bb 82static GSList *gen_channel_list(int num_channels)
5874e88d
DE
83{
84 GSList *list;
ba7dd8bb 85 struct sr_channel *ch;
5874e88d
DE
86 int i;
87 char name[8];
88
89 list = NULL;
90
ba7dd8bb
UH
91 for (i = num_channels; i > 0; --i) {
92 /* The LWLA series simply number channels from CH1 to CHxx. */
1f98295d 93 g_snprintf(name, sizeof(name), "CH%d", i);
5874e88d 94
3f239f08 95 ch = sr_channel_new(i - 1, SR_CHANNEL_LOGIC, TRUE, name);
ba7dd8bb 96 list = g_slist_prepend(list, ch);
5874e88d
DE
97 }
98
99 return list;
100}
101
0af636be 102static struct sr_dev_inst *dev_inst_new(void)
43db3436
DE
103{
104 struct sr_dev_inst *sdi;
105 struct dev_context *devc;
106
107 /* Allocate memory for our private driver context. */
108 devc = g_try_new0(struct dev_context, 1);
109 if (!devc) {
110 sr_err("Device context malloc failed.");
111 return NULL;
112 }
113
114 /* Register the device with libsigrok. */
0af636be
UH
115 sdi = sr_dev_inst_new();
116 sdi->status = SR_ST_INACTIVE;
117 sdi->vendor = g_strdup(VENDOR_NAME);
118 sdi->model = g_strdup(MODEL_NAME);
43db3436 119
ba7dd8bb 120 /* Enable all channels to match the default channel configuration. */
43db3436
DE
121 devc->channel_mask = ALL_CHANNELS_MASK;
122 devc->samplerate = DEFAULT_SAMPLERATE;
123
124 sdi->priv = devc;
3f239f08 125 sdi->channels = gen_channel_list(NUM_CHANNELS);
43db3436
DE
126
127 return sdi;
128}
129
aeaad0b0
DE
130static GSList *scan(GSList *options)
131{
5874e88d 132 GSList *usb_devices, *devices, *node;
aeaad0b0 133 struct drv_context *drvc;
5874e88d 134 struct sr_dev_inst *sdi;
5874e88d 135 struct sr_usb_dev_inst *usb;
7ebe9b9e
DE
136 struct sr_config *src;
137 const char *conn;
aeaad0b0 138
aeaad0b0 139 drvc = di->priv;
7ebe9b9e 140 conn = USB_VID_PID;
5874e88d 141
7ebe9b9e
DE
142 for (node = options; node != NULL; node = node->next) {
143 src = node->data;
144 if (src->key == SR_CONF_CONN) {
145 conn = g_variant_get_string(src->data, NULL);
146 break;
147 }
148 }
149 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
150 devices = NULL;
5874e88d
DE
151
152 for (node = usb_devices; node != NULL; node = node->next) {
153 usb = node->data;
154
43db3436 155 /* Create sigrok device instance. */
c79d4444 156 sdi = dev_inst_new();
5874e88d 157 if (!sdi) {
5874e88d
DE
158 sr_usb_dev_inst_free(usb);
159 continue;
160 }
161 sdi->driver = di;
5874e88d
DE
162 sdi->inst_type = SR_INST_USB;
163 sdi->conn = usb;
5874e88d 164
43db3436 165 /* Register device instance with driver. */
5874e88d
DE
166 drvc->instances = g_slist_append(drvc->instances, sdi);
167 devices = g_slist_append(devices, sdi);
168 }
aeaad0b0 169
5874e88d 170 g_slist_free(usb_devices);
aeaad0b0
DE
171
172 return devices;
173}
174
175static GSList *dev_list(void)
176{
5874e88d
DE
177 struct drv_context *drvc;
178
179 drvc = di->priv;
180
181 return drvc->instances;
182}
183
184static void clear_dev_context(void *priv)
185{
186 struct dev_context *devc;
187
188 devc = priv;
189
190 sr_dbg("Device context cleared.");
191
192 lwla_free_acquisition_state(devc->acquisition);
193 g_free(devc);
aeaad0b0
DE
194}
195
196static int dev_clear(void)
197{
5874e88d 198 return std_dev_clear(di, &clear_dev_context);
aeaad0b0
DE
199}
200
201static int dev_open(struct sr_dev_inst *sdi)
202{
5874e88d 203 struct drv_context *drvc;
5874e88d
DE
204 struct sr_usb_dev_inst *usb;
205 int ret;
aeaad0b0 206
5874e88d 207 drvc = di->priv;
aeaad0b0 208
5874e88d
DE
209 if (!drvc) {
210 sr_err("Driver was not initialized.");
211 return SR_ERR;
212 }
aeaad0b0 213
43db3436 214 usb = sdi->conn;
5874e88d
DE
215
216 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
217 if (ret != SR_OK)
218 return ret;
219
220 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
221 if (ret < 0) {
222 sr_err("Failed to claim interface: %s.",
223 libusb_error_name(ret));
224 return SR_ERR;
225 }
226
227 sdi->status = SR_ST_INITIALIZING;
228
5874e88d
DE
229 ret = lwla_init_device(sdi);
230
231 if (ret == SR_OK)
232 sdi->status = SR_ST_ACTIVE;
233
234 return ret;
aeaad0b0
DE
235}
236
237static int dev_close(struct sr_dev_inst *sdi)
238{
5874e88d 239 struct sr_usb_dev_inst *usb;
5874e88d
DE
240
241 if (!di->priv) {
242 sr_err("Driver was not initialized.");
243 return SR_ERR;
244 }
245
6358f0a9 246 usb = sdi->conn;
5874e88d
DE
247 if (!usb->devhdl)
248 return SR_OK;
aeaad0b0 249
6358f0a9 250 sdi->status = SR_ST_INACTIVE;
5874e88d 251
6358f0a9
DE
252 /* Trigger download of the shutdown bitstream. */
253 if (lwla_set_clock_config(sdi) != SR_OK)
5874e88d
DE
254 sr_err("Unable to shut down device.");
255
256 libusb_release_interface(usb->devhdl, USB_INTERFACE);
257 libusb_close(usb->devhdl);
258
259 usb->devhdl = NULL;
aeaad0b0
DE
260
261 return SR_OK;
262}
263
264static int cleanup(void)
265{
5874e88d 266 return dev_clear();
aeaad0b0
DE
267}
268
584560f1 269static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 270 const struct sr_channel_group *cg)
aeaad0b0 271{
5874e88d 272 struct dev_context *devc;
e6e54bd2 273 size_t idx;
aeaad0b0 274
53b4680f 275 (void)cg;
aeaad0b0 276
5874e88d
DE
277 if (!sdi)
278 return SR_ERR_ARG;
279
280 devc = sdi->priv;
281
aeaad0b0 282 switch (key) {
5874e88d
DE
283 case SR_CONF_SAMPLERATE:
284 *data = g_variant_new_uint64(devc->samplerate);
285 break;
29d58767
DE
286 case SR_CONF_LIMIT_MSEC:
287 *data = g_variant_new_uint64(devc->limit_msec);
288 break;
5874e88d
DE
289 case SR_CONF_LIMIT_SAMPLES:
290 *data = g_variant_new_uint64(devc->limit_samples);
291 break;
292 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
293 *data = g_variant_new_boolean(devc->cfg_clock_source
294 == CLOCK_EXT_CLK);
295 break;
296 case SR_CONF_CLOCK_EDGE:
297 idx = devc->cfg_clock_edge;
298 if (idx >= G_N_ELEMENTS(signal_edge_names))
299 return SR_ERR_BUG;
300 *data = g_variant_new_string(signal_edge_names[idx]);
5874e88d 301 break;
e6e54bd2
DE
302 case SR_CONF_TRIGGER_SOURCE:
303 idx = devc->cfg_trigger_source;
304 if (idx >= G_N_ELEMENTS(trigger_source_names))
305 return SR_ERR_BUG;
306 *data = g_variant_new_string(trigger_source_names[idx]);
307 break;
308 case SR_CONF_TRIGGER_SLOPE:
309 idx = devc->cfg_trigger_slope;
6358f0a9 310 if (idx >= G_N_ELEMENTS(signal_edge_names))
e6e54bd2 311 return SR_ERR_BUG;
6358f0a9 312 *data = g_variant_new_string(signal_edge_names[idx]);
e6e54bd2 313 break;
aeaad0b0
DE
314 default:
315 return SR_ERR_NA;
316 }
317
5874e88d 318 return SR_OK;
aeaad0b0
DE
319}
320
e6e54bd2
DE
321/* Helper for mapping a string-typed configuration value to an index
322 * within a table of possible values.
323 */
324static int lookup_index(GVariant *value, const char *const *table, int len)
325{
326 const char *entry;
327 int i;
328
329 entry = g_variant_get_string(value, NULL);
330 if (!entry)
331 return -1;
332
333 /* Linear search is fine for very small tables. */
334 for (i = 0; i < len; ++i) {
335 if (strcmp(entry, table[i]) == 0)
336 return i;
337 }
338 return -1;
339}
340
584560f1 341static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 342 const struct sr_channel_group *cg)
aeaad0b0 343{
29d58767 344 uint64_t value;
5874e88d 345 struct dev_context *devc;
e6e54bd2 346 int idx;
aeaad0b0 347
53b4680f 348 (void)cg;
aeaad0b0 349
5874e88d
DE
350 devc = sdi->priv;
351 if (!devc)
aeaad0b0
DE
352 return SR_ERR_DEV_CLOSED;
353
aeaad0b0 354 switch (key) {
5874e88d 355 case SR_CONF_SAMPLERATE:
29d58767 356 value = g_variant_get_uint64(data);
29d58767
DE
357 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
358 || value > samplerates[0])
5874e88d 359 return SR_ERR_SAMPLERATE;
29d58767
DE
360 devc->samplerate = value;
361 break;
362 case SR_CONF_LIMIT_MSEC:
363 value = g_variant_get_uint64(data);
364 if (value > MAX_LIMIT_MSEC)
365 return SR_ERR_ARG;
366 devc->limit_msec = value;
5874e88d
DE
367 break;
368 case SR_CONF_LIMIT_SAMPLES:
29d58767
DE
369 value = g_variant_get_uint64(data);
370 if (value > MAX_LIMIT_SAMPLES)
371 return SR_ERR_ARG;
372 devc->limit_samples = value;
5874e88d
DE
373 break;
374 case SR_CONF_EXTERNAL_CLOCK:
6358f0a9
DE
375 devc->cfg_clock_source = (g_variant_get_boolean(data))
376 ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
377 break;
378 case SR_CONF_CLOCK_EDGE:
379 idx = lookup_index(data, signal_edge_names,
380 G_N_ELEMENTS(signal_edge_names));
381 if (idx < 0)
382 return SR_ERR_ARG;
383 devc->cfg_clock_edge = idx;
5874e88d 384 break;
e6e54bd2
DE
385 case SR_CONF_TRIGGER_SOURCE:
386 idx = lookup_index(data, trigger_source_names,
387 G_N_ELEMENTS(trigger_source_names));
388 if (idx < 0)
389 return SR_ERR_ARG;
390 devc->cfg_trigger_source = idx;
391 break;
392 case SR_CONF_TRIGGER_SLOPE:
6358f0a9
DE
393 idx = lookup_index(data, signal_edge_names,
394 G_N_ELEMENTS(signal_edge_names));
e6e54bd2
DE
395 if (idx < 0)
396 return SR_ERR_ARG;
397 devc->cfg_trigger_slope = idx;
398 break;
aeaad0b0 399 default:
5874e88d 400 return SR_ERR_NA;
aeaad0b0
DE
401 }
402
5874e88d 403 return SR_OK;
aeaad0b0
DE
404}
405
f3ca73ed 406static int config_channel_set(const struct sr_dev_inst *sdi,
bbe7e48a 407 struct sr_channel *ch, unsigned int changes)
43db3436 408{
ba7dd8bb 409 uint64_t channel_bit;
43db3436
DE
410 struct dev_context *devc;
411
412 devc = sdi->priv;
413 if (!devc)
414 return SR_ERR_DEV_CLOSED;
415
3f239f08 416 if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
ba7dd8bb 417 sr_err("Channel index %d out of range.", ch->index);
43db3436
DE
418 return SR_ERR_BUG;
419 }
ba7dd8bb 420 channel_bit = (uint64_t)1 << ch->index;
43db3436 421
3f239f08 422 if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
ba7dd8bb
UH
423 /* Enable or disable input channel for this channel. */
424 if (ch->enabled)
425 devc->channel_mask |= channel_bit;
43db3436 426 else
ba7dd8bb 427 devc->channel_mask &= ~channel_bit;
43db3436
DE
428 }
429
43db3436
DE
430 return SR_OK;
431}
432
ee38c8ba
DE
433static int config_commit(const struct sr_dev_inst *sdi)
434{
435 if (sdi->status != SR_ST_ACTIVE) {
436 sr_err("Device not ready (status %d).", (int)sdi->status);
437 return SR_ERR;
438 }
439
6358f0a9 440 return lwla_set_clock_config(sdi);
ee38c8ba
DE
441}
442
584560f1 443static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 444 const struct sr_channel_group *cg)
aeaad0b0 445{
5874e88d
DE
446 GVariant *gvar;
447 GVariantBuilder gvb;
aeaad0b0
DE
448
449 (void)sdi;
53b4680f 450 (void)cg;
aeaad0b0 451
aeaad0b0 452 switch (key) {
99c76642 453 case SR_CONF_SCAN_OPTIONS:
584560f1 454 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 455 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
99c76642 456 break;
5874e88d 457 case SR_CONF_DEVICE_OPTIONS:
584560f1 458 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 459 devopts, G_N_ELEMENTS(devopts), sizeof(uint32_t));
5874e88d
DE
460 break;
461 case SR_CONF_SAMPLERATE:
462 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
463 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
99c76642 464 samplerates, G_N_ELEMENTS(samplerates),
5874e88d
DE
465 sizeof(uint64_t));
466 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
467 *data = g_variant_builder_end(&gvb);
468 break;
bbe7e48a
BV
469 case SR_CONF_TRIGGER_MATCH:
470 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
471 trigger_matches, ARRAY_SIZE(trigger_matches),
472 sizeof(int32_t));
5874e88d 473 break;
e6e54bd2
DE
474 case SR_CONF_TRIGGER_SOURCE:
475 *data = g_variant_new_strv(trigger_source_names,
476 G_N_ELEMENTS(trigger_source_names));
477 break;
478 case SR_CONF_TRIGGER_SLOPE:
6358f0a9
DE
479 case SR_CONF_CLOCK_EDGE:
480 *data = g_variant_new_strv(signal_edge_names,
481 G_N_ELEMENTS(signal_edge_names));
e6e54bd2 482 break;
aeaad0b0
DE
483 default:
484 return SR_ERR_NA;
485 }
486
5874e88d 487 return SR_OK;
aeaad0b0
DE
488}
489
5874e88d
DE
490static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
491{
492 struct drv_context *drvc;
493 struct dev_context *devc;
494 struct acquisition_state *acq;
495 int ret;
496
aeaad0b0
DE
497 (void)cb_data;
498
499 if (sdi->status != SR_ST_ACTIVE)
500 return SR_ERR_DEV_CLOSED;
501
5874e88d
DE
502 devc = sdi->priv;
503 drvc = di->priv;
504
505 if (devc->acquisition) {
506 sr_err("Acquisition still in progress?");
507 return SR_ERR;
508 }
509 acq = lwla_alloc_acquisition_state();
510 if (!acq)
511 return SR_ERR_MALLOC;
512
513 devc->stopping_in_progress = FALSE;
514 devc->transfer_error = FALSE;
515
5874e88d
DE
516 sr_info("Starting acquisition.");
517
518 devc->acquisition = acq;
bbe7e48a 519 lwla_convert_trigger(sdi);
5874e88d
DE
520 ret = lwla_setup_acquisition(sdi);
521 if (ret != SR_OK) {
a84f6ad3 522 sr_err("Failed to set up acquisition.");
5874e88d
DE
523 devc->acquisition = NULL;
524 lwla_free_acquisition_state(acq);
525 return ret;
526 }
527
528 ret = lwla_start_acquisition(sdi);
529 if (ret != SR_OK) {
a84f6ad3 530 sr_err("Failed to start acquisition.");
5874e88d
DE
531 devc->acquisition = NULL;
532 lwla_free_acquisition_state(acq);
533 return ret;
534 }
102f1239 535 usb_source_add(sdi->session, drvc->sr_ctx, 100, &lwla_receive_data,
5874e88d
DE
536 (struct sr_dev_inst *)sdi);
537
538 sr_info("Waiting for data.");
539
540 /* Send header packet to the session bus. */
541 std_session_send_df_header(sdi, LOG_PREFIX);
aeaad0b0
DE
542
543 return SR_OK;
544}
545
546static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
547{
548 (void)cb_data;
549
550 if (sdi->status != SR_ST_ACTIVE)
551 return SR_ERR_DEV_CLOSED;
552
5874e88d
DE
553 sr_dbg("Stopping acquisition.");
554
555 sdi->status = SR_ST_STOPPING;
aeaad0b0
DE
556
557 return SR_OK;
558}
559
560SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
561 .name = "sysclk-lwla",
5874e88d 562 .longname = "SysClk LWLA series",
aeaad0b0
DE
563 .api_version = 1,
564 .init = init,
565 .cleanup = cleanup,
566 .scan = scan,
567 .dev_list = dev_list,
568 .dev_clear = dev_clear,
569 .config_get = config_get,
570 .config_set = config_set,
f3ca73ed 571 .config_channel_set = config_channel_set,
ee38c8ba 572 .config_commit = config_commit,
aeaad0b0
DE
573 .config_list = config_list,
574 .dev_open = dev_open,
575 .dev_close = dev_close,
576 .dev_acquisition_start = dev_acquisition_start,
577 .dev_acquisition_stop = dev_acquisition_stop,
578 .priv = NULL,
579};