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