]> sigrok.org Git - libsigrok.git/blame - hardware/sysclk-lwla/api.c
sysclk-lwla: Do not reset drv_context.instances on scan.
[libsigrok.git] / 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
99c76642
DE
28static const int32_t hwopts[] = {
29 SR_CONF_CONN,
30};
31
5874e88d
DE
32static const int32_t hwcaps[] = {
33 SR_CONF_LOGIC_ANALYZER,
34 SR_CONF_SAMPLERATE,
35 SR_CONF_EXTERNAL_CLOCK,
36 SR_CONF_TRIGGER_TYPE,
29d58767 37 SR_CONF_LIMIT_MSEC,
5874e88d
DE
38 SR_CONF_LIMIT_SAMPLES,
39};
40
41/* The hardware supports more samplerates than these, but these are the
42 * options hardcoded into the vendor's Windows GUI.
43 */
44static const uint64_t samplerates[] = {
45 SR_MHZ(125), SR_MHZ(100),
46 SR_MHZ(50), SR_MHZ(20), SR_MHZ(10),
47 SR_MHZ(5), SR_MHZ(2), SR_MHZ(1),
48 SR_KHZ(500), SR_KHZ(200), SR_KHZ(100),
49 SR_KHZ(50), SR_KHZ(20), SR_KHZ(10),
50 SR_KHZ(5), SR_KHZ(2), SR_KHZ(1),
51 SR_HZ(500), SR_HZ(200), SR_HZ(100),
52};
aeaad0b0
DE
53
54SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
5874e88d 55static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
aeaad0b0
DE
56
57static int init(struct sr_context *sr_ctx)
58{
59 return std_init(sr_ctx, di, LOG_PREFIX);
60}
61
5874e88d
DE
62static GSList *gen_probe_list(int num_probes)
63{
64 GSList *list;
65 struct sr_probe *probe;
66 int i;
67 char name[8];
68
69 list = NULL;
70
71 for (i = num_probes; i > 0; --i) {
72 /* The LWLA series simply number probes from CH1 to CHxx. */
1f98295d 73 g_snprintf(name, sizeof(name), "CH%d", i);
5874e88d
DE
74
75 probe = sr_probe_new(i - 1, SR_PROBE_LOGIC, TRUE, name);
76 list = g_slist_prepend(list, probe);
77 }
78
79 return list;
80}
81
aeaad0b0
DE
82static GSList *scan(GSList *options)
83{
5874e88d 84 GSList *usb_devices, *devices, *node;
aeaad0b0 85 struct drv_context *drvc;
5874e88d
DE
86 struct sr_dev_inst *sdi;
87 struct dev_context *devc;
88 struct sr_usb_dev_inst *usb;
7ebe9b9e
DE
89 struct sr_config *src;
90 const char *conn;
5874e88d 91 int device_index;
aeaad0b0 92
aeaad0b0 93 drvc = di->priv;
7ebe9b9e 94 conn = USB_VID_PID;
5874e88d 95
7ebe9b9e
DE
96 for (node = options; node != NULL; node = node->next) {
97 src = node->data;
98 if (src->key == SR_CONF_CONN) {
99 conn = g_variant_get_string(src->data, NULL);
100 break;
101 }
102 }
103 usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
104 devices = NULL;
50cad98d 105 device_index = g_slist_length(drvc->instances);
5874e88d
DE
106
107 for (node = usb_devices; node != NULL; node = node->next) {
108 usb = node->data;
109
110 /* Allocate memory for our private driver context. */
111 devc = g_try_new0(struct dev_context, 1);
112 if (!devc) {
113 sr_err("Device context malloc failed.");
114 sr_usb_dev_inst_free(usb);
115 continue;
116 }
117 /* Register the device with libsigrok. */
118 sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE,
119 VENDOR_NAME, MODEL_NAME, NULL);
120 if (!sdi) {
121 sr_err("Failed to instantiate device.");
122 g_free(devc);
123 sr_usb_dev_inst_free(usb);
124 continue;
125 }
126 sdi->driver = di;
127 sdi->priv = devc;
128 sdi->inst_type = SR_INST_USB;
129 sdi->conn = usb;
130 sdi->probes = gen_probe_list(NUM_PROBES);
131
132 drvc->instances = g_slist_append(drvc->instances, sdi);
133 devices = g_slist_append(devices, sdi);
134 }
aeaad0b0 135
5874e88d 136 g_slist_free(usb_devices);
aeaad0b0
DE
137
138 return devices;
139}
140
141static GSList *dev_list(void)
142{
5874e88d
DE
143 struct drv_context *drvc;
144
145 drvc = di->priv;
146
147 return drvc->instances;
148}
149
150static void clear_dev_context(void *priv)
151{
152 struct dev_context *devc;
153
154 devc = priv;
155
156 sr_dbg("Device context cleared.");
157
158 lwla_free_acquisition_state(devc->acquisition);
159 g_free(devc);
aeaad0b0
DE
160}
161
162static int dev_clear(void)
163{
5874e88d 164 return std_dev_clear(di, &clear_dev_context);
aeaad0b0
DE
165}
166
167static int dev_open(struct sr_dev_inst *sdi)
168{
5874e88d
DE
169 struct drv_context *drvc;
170 struct dev_context *devc;
171 struct sr_usb_dev_inst *usb;
172 int ret;
aeaad0b0 173
5874e88d 174 drvc = di->priv;
aeaad0b0 175
5874e88d
DE
176 if (!drvc) {
177 sr_err("Driver was not initialized.");
178 return SR_ERR;
179 }
aeaad0b0 180
5874e88d
DE
181 usb = sdi->conn;
182 devc = sdi->priv;
183
184 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
185 if (ret != SR_OK)
186 return ret;
187
188 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
189 if (ret < 0) {
190 sr_err("Failed to claim interface: %s.",
191 libusb_error_name(ret));
192 return SR_ERR;
193 }
194
195 sdi->status = SR_ST_INITIALIZING;
196
197 if (devc->samplerate == 0)
198 /* Apply default if the samplerate hasn't been set yet. */
199 devc->samplerate = DEFAULT_SAMPLERATE;
200
201 ret = lwla_init_device(sdi);
202
203 if (ret == SR_OK)
204 sdi->status = SR_ST_ACTIVE;
205
206 return ret;
aeaad0b0
DE
207}
208
209static int dev_close(struct sr_dev_inst *sdi)
210{
5874e88d
DE
211 struct sr_usb_dev_inst *usb;
212 struct dev_context *devc;
213
214 if (!di->priv) {
215 sr_err("Driver was not initialized.");
216 return SR_ERR;
217 }
218
219 usb = sdi->conn;
220 devc = sdi->priv;
aeaad0b0 221
5874e88d
DE
222 if (!usb->devhdl)
223 return SR_OK;
aeaad0b0 224
5874e88d
DE
225 /* Trigger download of the shutdown bitstream. */
226 devc->selected_clock_source = CLOCK_SOURCE_NONE;
227
228 if (lwla_set_clock_source(sdi) != SR_OK)
229 sr_err("Unable to shut down device.");
230
231 libusb_release_interface(usb->devhdl, USB_INTERFACE);
232 libusb_close(usb->devhdl);
233
234 usb->devhdl = NULL;
aeaad0b0
DE
235 sdi->status = SR_ST_INACTIVE;
236
237 return SR_OK;
238}
239
240static int cleanup(void)
241{
5874e88d 242 return dev_clear();
aeaad0b0
DE
243}
244
245static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
5874e88d 246 const struct sr_probe_group *probe_group)
aeaad0b0 247{
5874e88d 248 struct dev_context *devc;
aeaad0b0 249
aeaad0b0
DE
250 (void)probe_group;
251
5874e88d
DE
252 if (!sdi)
253 return SR_ERR_ARG;
254
255 devc = sdi->priv;
256
aeaad0b0 257 switch (key) {
5874e88d
DE
258 case SR_CONF_SAMPLERATE:
259 *data = g_variant_new_uint64(devc->samplerate);
260 break;
29d58767
DE
261 case SR_CONF_LIMIT_MSEC:
262 *data = g_variant_new_uint64(devc->limit_msec);
263 break;
5874e88d
DE
264 case SR_CONF_LIMIT_SAMPLES:
265 *data = g_variant_new_uint64(devc->limit_samples);
266 break;
267 case SR_CONF_EXTERNAL_CLOCK:
268 *data = g_variant_new_boolean(devc->selected_clock_source
269 >= CLOCK_SOURCE_EXT_RISE);
270 break;
aeaad0b0
DE
271 default:
272 return SR_ERR_NA;
273 }
274
5874e88d 275 return SR_OK;
aeaad0b0
DE
276}
277
278static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
5874e88d 279 const struct sr_probe_group *probe_group)
aeaad0b0 280{
29d58767 281 uint64_t value;
5874e88d 282 struct dev_context *devc;
aeaad0b0 283
aeaad0b0
DE
284 (void)probe_group;
285
5874e88d
DE
286 devc = sdi->priv;
287 if (!devc)
aeaad0b0
DE
288 return SR_ERR_DEV_CLOSED;
289
aeaad0b0 290 switch (key) {
5874e88d 291 case SR_CONF_SAMPLERATE:
29d58767 292 value = g_variant_get_uint64(data);
29d58767
DE
293 if (value < samplerates[G_N_ELEMENTS(samplerates) - 1]
294 || value > samplerates[0])
5874e88d 295 return SR_ERR_SAMPLERATE;
29d58767
DE
296 devc->samplerate = value;
297 break;
298 case SR_CONF_LIMIT_MSEC:
299 value = g_variant_get_uint64(data);
300 if (value > MAX_LIMIT_MSEC)
301 return SR_ERR_ARG;
302 devc->limit_msec = value;
5874e88d
DE
303 break;
304 case SR_CONF_LIMIT_SAMPLES:
29d58767
DE
305 value = g_variant_get_uint64(data);
306 if (value > MAX_LIMIT_SAMPLES)
307 return SR_ERR_ARG;
308 devc->limit_samples = value;
5874e88d
DE
309 break;
310 case SR_CONF_EXTERNAL_CLOCK:
311 if (g_variant_get_boolean(data)) {
312 sr_info("Enabling external clock.");
313 /* TODO: Allow the external clock to be inverted */
314 devc->selected_clock_source = CLOCK_SOURCE_EXT_RISE;
315 } else {
316 sr_info("Disabling external clock.");
317 devc->selected_clock_source = CLOCK_SOURCE_INT;
318 }
319 if (sdi->status == SR_ST_ACTIVE)
320 return lwla_set_clock_source(sdi);
321 break;
aeaad0b0 322 default:
5874e88d 323 return SR_ERR_NA;
aeaad0b0
DE
324 }
325
5874e88d 326 return SR_OK;
aeaad0b0
DE
327}
328
329static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
5874e88d 330 const struct sr_probe_group *probe_group)
aeaad0b0 331{
5874e88d
DE
332 GVariant *gvar;
333 GVariantBuilder gvb;
aeaad0b0
DE
334
335 (void)sdi;
aeaad0b0
DE
336 (void)probe_group;
337
aeaad0b0 338 switch (key) {
99c76642
DE
339 case SR_CONF_SCAN_OPTIONS:
340 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
341 hwopts, G_N_ELEMENTS(hwopts), sizeof(int32_t));
342 break;
5874e88d
DE
343 case SR_CONF_DEVICE_OPTIONS:
344 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
99c76642 345 hwcaps, G_N_ELEMENTS(hwcaps), sizeof(int32_t));
5874e88d
DE
346 break;
347 case SR_CONF_SAMPLERATE:
348 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
349 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
99c76642 350 samplerates, G_N_ELEMENTS(samplerates),
5874e88d
DE
351 sizeof(uint64_t));
352 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
353 *data = g_variant_builder_end(&gvb);
354 break;
355 case SR_CONF_TRIGGER_TYPE:
356 *data = g_variant_new_string(TRIGGER_TYPES);
357 break;
aeaad0b0
DE
358 default:
359 return SR_ERR_NA;
360 }
361
5874e88d 362 return SR_OK;
aeaad0b0
DE
363}
364
5874e88d 365static int configure_probes(const struct sr_dev_inst *sdi)
aeaad0b0 366{
5874e88d
DE
367 struct dev_context *devc;
368 const struct sr_probe *probe;
369 const GSList *node;
370 uint64_t probe_bit;
371
372 devc = sdi->priv;
373
374 devc->channel_mask = 0;
375 devc->trigger_mask = 0;
376 devc->trigger_edge_mask = 0;
377 devc->trigger_values = 0;
378
0b92c32c 379 for (node = sdi->probes; node != NULL; node = node->next) {
5874e88d
DE
380 probe = node->data;
381 if (!probe || !probe->enabled)
382 continue;
383
0b92c32c
DE
384 if (probe->index >= NUM_PROBES) {
385 sr_err("Channel index %d out of range.", probe->index);
386 return SR_ERR_BUG;
387 }
388 probe_bit = (uint64_t)1 << probe->index;
389
5874e88d
DE
390 /* Enable input channel for this probe. */
391 devc->channel_mask |= probe_bit;
392
393 if (!probe->trigger || probe->trigger[0] == '\0')
394 continue;
395
396 if (probe->trigger[1] != '\0') {
397 sr_err("Only one trigger stage is supported.");
398 return SR_ERR;
399 }
400 /* Enable trigger for this probe. */
401 devc->trigger_mask |= probe_bit;
402
403 /* Configure edge mask and trigger value. */
404 switch (probe->trigger[0]) {
405 case '1': devc->trigger_values |= probe_bit;
406 case '0': break;
407
408 case 'r': devc->trigger_values |= probe_bit;
409 case 'f': devc->trigger_edge_mask |= probe_bit;
410 break;
411 default:
412 sr_err("Trigger type '%c' is not supported.",
413 probe->trigger[0]);
414 return SR_ERR;
415 }
416 }
417 return SR_OK;
418}
419
420static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
421{
422 struct drv_context *drvc;
423 struct dev_context *devc;
424 struct acquisition_state *acq;
425 int ret;
426
aeaad0b0
DE
427 (void)cb_data;
428
429 if (sdi->status != SR_ST_ACTIVE)
430 return SR_ERR_DEV_CLOSED;
431
5874e88d
DE
432 devc = sdi->priv;
433 drvc = di->priv;
434
435 if (devc->acquisition) {
436 sr_err("Acquisition still in progress?");
437 return SR_ERR;
438 }
439 acq = lwla_alloc_acquisition_state();
440 if (!acq)
441 return SR_ERR_MALLOC;
442
443 devc->stopping_in_progress = FALSE;
444 devc->transfer_error = FALSE;
445
446 ret = configure_probes(sdi);
447 if (ret != SR_OK) {
448 sr_err("Failed to configure probes.");
449 lwla_free_acquisition_state(acq);
450 return ret;
451 }
452
453 sr_info("Starting acquisition.");
454
455 devc->acquisition = acq;
456 ret = lwla_setup_acquisition(sdi);
457 if (ret != SR_OK) {
458 sr_err("Failed to set up aquisition.");
459 devc->acquisition = NULL;
460 lwla_free_acquisition_state(acq);
461 return ret;
462 }
463
464 ret = lwla_start_acquisition(sdi);
465 if (ret != SR_OK) {
466 sr_err("Failed to start aquisition.");
467 devc->acquisition = NULL;
468 lwla_free_acquisition_state(acq);
469 return ret;
470 }
471 usb_source_add(drvc->sr_ctx, 100, &lwla_receive_data,
472 (struct sr_dev_inst *)sdi);
473
474 sr_info("Waiting for data.");
475
476 /* Send header packet to the session bus. */
477 std_session_send_df_header(sdi, LOG_PREFIX);
aeaad0b0
DE
478
479 return SR_OK;
480}
481
482static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
483{
484 (void)cb_data;
485
486 if (sdi->status != SR_ST_ACTIVE)
487 return SR_ERR_DEV_CLOSED;
488
5874e88d
DE
489 sr_dbg("Stopping acquisition.");
490
491 sdi->status = SR_ST_STOPPING;
aeaad0b0
DE
492
493 return SR_OK;
494}
495
496SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
497 .name = "sysclk-lwla",
5874e88d 498 .longname = "SysClk LWLA series",
aeaad0b0
DE
499 .api_version = 1,
500 .init = init,
501 .cleanup = cleanup,
502 .scan = scan,
503 .dev_list = dev_list,
504 .dev_clear = dev_clear,
505 .config_get = config_get,
506 .config_set = config_set,
507 .config_list = config_list,
508 .dev_open = dev_open,
509 .dev_close = dev_close,
510 .dev_acquisition_start = dev_acquisition_start,
511 .dev_acquisition_stop = dev_acquisition_stop,
512 .priv = NULL,
513};