]> sigrok.org Git - libsigrok.git/blob - src/hardware/sysclk-lwla/api.c
sysclk-lwla: Clean up open/closed state handling
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
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
29 static const uint32_t scanopts[] = {
30         SR_CONF_CONN,
31 };
32
33 static 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
45 static 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  */
55 static 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  */
68 static 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  */
73 static const char *const signal_edge_names[] = { "r", "f" };
74
75 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info;
76 static struct sr_dev_driver *const di = &sysclk_lwla_driver_info;
77
78 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
79 {
80         return std_init(sr_ctx, di, LOG_PREFIX);
81 }
82
83 static 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
113 static 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
158 static 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
167 static 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
179 static int dev_clear(const struct sr_dev_driver *di)
180 {
181         return std_dev_clear(di, &clear_dev_context);
182 }
183
184 static 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         ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
207         if (ret < 0) {
208                 sr_err("Failed to claim interface: %s.",
209                         libusb_error_name(ret));
210                 sr_usb_close(usb);
211                 return SR_ERR;
212         }
213         sdi->status = SR_ST_ACTIVE;
214
215         ret = lwla_init_device(sdi);
216         if (ret != SR_OK) {
217                 sr_usb_close(usb);
218                 sdi->status = SR_ST_INACTIVE;
219         }
220         return ret;
221 }
222
223 static int dev_close(struct sr_dev_inst *sdi)
224 {
225         struct sr_usb_dev_inst *usb;
226         struct dev_context *devc;
227         int ret;
228
229         if (!di->context) {
230                 sr_err("Driver was not initialized.");
231                 return SR_ERR;
232         }
233         usb = sdi->conn;
234         devc = sdi->priv;
235
236         if (sdi->status == SR_ST_INACTIVE)
237                 return SR_OK;
238
239         if (devc && devc->acquisition) {
240                 sr_err("Attempt to close device during acquisition.");
241                 return SR_ERR;
242         }
243         sdi->status = SR_ST_INACTIVE;
244
245         /* Trigger download of the shutdown bitstream. */
246         ret = lwla_set_clock_config(sdi);
247         if (ret != SR_OK)
248                 sr_err("Unable to shut down device.");
249
250         libusb_release_interface(usb->devhdl, USB_INTERFACE);
251         sr_usb_close(usb);
252
253         return ret;
254 }
255
256 static int cleanup(const struct sr_dev_driver *di)
257 {
258         return dev_clear(di);
259 }
260
261 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
262                       const struct sr_channel_group *cg)
263 {
264         struct dev_context *devc;
265         size_t idx;
266
267         (void)cg;
268
269         if (!sdi)
270                 return SR_ERR_ARG;
271
272         devc = sdi->priv;
273
274         switch (key) {
275         case SR_CONF_SAMPLERATE:
276                 *data = g_variant_new_uint64(devc->samplerate);
277                 break;
278         case SR_CONF_LIMIT_MSEC:
279                 *data = g_variant_new_uint64(devc->limit_msec);
280                 break;
281         case SR_CONF_LIMIT_SAMPLES:
282                 *data = g_variant_new_uint64(devc->limit_samples);
283                 break;
284         case SR_CONF_EXTERNAL_CLOCK:
285                 *data = g_variant_new_boolean(devc->cfg_clock_source
286                                                 == CLOCK_EXT_CLK);
287                 break;
288         case SR_CONF_CLOCK_EDGE:
289                 idx = devc->cfg_clock_edge;
290                 if (idx >= ARRAY_SIZE(signal_edge_names))
291                         return SR_ERR_BUG;
292                 *data = g_variant_new_string(signal_edge_names[idx]);
293                 break;
294         case SR_CONF_TRIGGER_SOURCE:
295                 idx = devc->cfg_trigger_source;
296                 if (idx >= ARRAY_SIZE(trigger_source_names))
297                         return SR_ERR_BUG;
298                 *data = g_variant_new_string(trigger_source_names[idx]);
299                 break;
300         case SR_CONF_TRIGGER_SLOPE:
301                 idx = devc->cfg_trigger_slope;
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         default:
307                 return SR_ERR_NA;
308         }
309
310         return SR_OK;
311 }
312
313 /* Helper for mapping a string-typed configuration value to an index
314  * within a table of possible values.
315  */
316 static int lookup_index(GVariant *value, const char *const *table, int len)
317 {
318         const char *entry;
319         int i;
320
321         entry = g_variant_get_string(value, NULL);
322         if (!entry)
323                 return -1;
324
325         /* Linear search is fine for very small tables. */
326         for (i = 0; i < len; ++i) {
327                 if (strcmp(entry, table[i]) == 0)
328                         return i;
329         }
330         return -1;
331 }
332
333 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
334                       const struct sr_channel_group *cg)
335 {
336         uint64_t value;
337         struct dev_context *devc;
338         int idx;
339
340         (void)cg;
341
342         devc = sdi->priv;
343         if (!devc)
344                 return SR_ERR_DEV_CLOSED;
345
346         switch (key) {
347         case SR_CONF_SAMPLERATE:
348                 value = g_variant_get_uint64(data);
349                 if (value < samplerates[ARRAY_SIZE(samplerates) - 1]
350                                 || value > samplerates[0])
351                         return SR_ERR_SAMPLERATE;
352                 devc->samplerate = value;
353                 break;
354         case SR_CONF_LIMIT_MSEC:
355                 value = g_variant_get_uint64(data);
356                 if (value > MAX_LIMIT_MSEC)
357                         return SR_ERR_ARG;
358                 devc->limit_msec = value;
359                 break;
360         case SR_CONF_LIMIT_SAMPLES:
361                 value = g_variant_get_uint64(data);
362                 if (value > MAX_LIMIT_SAMPLES)
363                         return SR_ERR_ARG;
364                 devc->limit_samples = value;
365                 break;
366         case SR_CONF_EXTERNAL_CLOCK:
367                 devc->cfg_clock_source = (g_variant_get_boolean(data))
368                         ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
369                 break;
370         case SR_CONF_CLOCK_EDGE:
371                 idx = lookup_index(data, signal_edge_names,
372                                    ARRAY_SIZE(signal_edge_names));
373                 if (idx < 0)
374                         return SR_ERR_ARG;
375                 devc->cfg_clock_edge = idx;
376                 break;
377         case SR_CONF_TRIGGER_SOURCE:
378                 idx = lookup_index(data, trigger_source_names,
379                                    ARRAY_SIZE(trigger_source_names));
380                 if (idx < 0)
381                         return SR_ERR_ARG;
382                 devc->cfg_trigger_source = idx;
383                 break;
384         case SR_CONF_TRIGGER_SLOPE:
385                 idx = lookup_index(data, signal_edge_names,
386                                    ARRAY_SIZE(signal_edge_names));
387                 if (idx < 0)
388                         return SR_ERR_ARG;
389                 devc->cfg_trigger_slope = idx;
390                 break;
391         default:
392                 return SR_ERR_NA;
393         }
394
395         return SR_OK;
396 }
397
398 static int config_channel_set(const struct sr_dev_inst *sdi,
399                 struct sr_channel *ch, unsigned int changes)
400 {
401         uint64_t channel_bit;
402         struct dev_context *devc;
403
404         devc = sdi->priv;
405         if (!devc)
406                 return SR_ERR_DEV_CLOSED;
407
408         if (ch->index < 0 || ch->index >= NUM_CHANNELS) {
409                 sr_err("Channel index %d out of range.", ch->index);
410                 return SR_ERR_BUG;
411         }
412         channel_bit = (uint64_t)1 << ch->index;
413
414         if ((changes & SR_CHANNEL_SET_ENABLED) != 0) {
415                 /* Enable or disable input channel for this channel. */
416                 if (ch->enabled)
417                         devc->channel_mask |= channel_bit;
418                 else
419                         devc->channel_mask &= ~channel_bit;
420         }
421
422         return SR_OK;
423 }
424
425 static int prepare_trigger_masks(const struct sr_dev_inst *sdi)
426 {
427         uint64_t trigger_mask;
428         uint64_t trigger_values;
429         uint64_t trigger_edge_mask;
430         uint64_t channel_bit;
431         struct dev_context *devc;
432         struct sr_trigger *trigger;
433         struct sr_trigger_stage *stage;
434         struct sr_trigger_match *match;
435         const GSList *node;
436
437         devc = sdi->priv;
438
439         trigger = sr_session_trigger_get(sdi->session);
440         if (!trigger || !trigger->stages)
441                 return SR_OK;
442
443         if (trigger->stages->next) {
444                 sr_err("This device only supports 1 trigger stage.");
445                 return SR_ERR_ARG;
446         }
447         stage = trigger->stages->data;
448
449         trigger_mask = 0;
450         trigger_values = 0;
451         trigger_edge_mask = 0;
452
453         for (node = stage->matches; node; node = node->next) {
454                 match = node->data;
455
456                 if (!match->channel->enabled)
457                         continue; /* ignore disabled channel */
458
459                 channel_bit = (uint64_t)1 << match->channel->index;
460                 trigger_mask |= channel_bit;
461
462                 switch (match->match) {
463                 case SR_TRIGGER_ZERO:
464                         break;
465                 case SR_TRIGGER_ONE:
466                         trigger_values |= channel_bit;
467                         break;
468                 case SR_TRIGGER_RISING:
469                         trigger_values |= channel_bit;
470                         /* Fall through for edge mask. */
471                 case SR_TRIGGER_FALLING:
472                         trigger_edge_mask |= channel_bit;
473                         break;
474                 default:
475                         sr_err("Unsupported trigger match for CH%d.",
476                                 match->channel->index + 1);
477                         return SR_ERR_ARG;
478                 }
479         }
480         devc->trigger_mask = trigger_mask;
481         devc->trigger_values = trigger_values;
482         devc->trigger_edge_mask = trigger_edge_mask;
483
484         return SR_OK;
485 }
486
487 static int config_commit(const struct sr_dev_inst *sdi)
488 {
489         struct dev_context *devc;
490         int rc;
491
492         if (sdi->status != SR_ST_ACTIVE)
493                 return SR_ERR_DEV_CLOSED;
494
495         devc = sdi->priv;
496         if (devc->acquisition) {
497                 sr_err("Acquisition still in progress?");
498                 return SR_ERR;
499         }
500         rc = prepare_trigger_masks(sdi);
501         if (rc != SR_OK)
502                 return rc;
503
504         return lwla_set_clock_config(sdi);
505 }
506
507 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
508                        const struct sr_channel_group *cg)
509 {
510         GVariant *gvar;
511         GVariantBuilder gvb;
512
513         (void)sdi;
514         (void)cg;
515
516         switch (key) {
517         case SR_CONF_SCAN_OPTIONS:
518                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
519                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
520                 break;
521         case SR_CONF_DEVICE_OPTIONS:
522                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
523                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
524                 break;
525         case SR_CONF_SAMPLERATE:
526                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
527                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
528                                 samplerates, ARRAY_SIZE(samplerates),
529                                 sizeof(uint64_t));
530                 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
531                 *data = g_variant_builder_end(&gvb);
532                 break;
533         case SR_CONF_TRIGGER_MATCH:
534                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
535                                 trigger_matches, ARRAY_SIZE(trigger_matches),
536                                 sizeof(int32_t));
537                 break;
538         case SR_CONF_TRIGGER_SOURCE:
539                 *data = g_variant_new_strv(trigger_source_names,
540                                            ARRAY_SIZE(trigger_source_names));
541                 break;
542         case SR_CONF_TRIGGER_SLOPE:
543         case SR_CONF_CLOCK_EDGE:
544                 *data = g_variant_new_strv(signal_edge_names,
545                                            ARRAY_SIZE(signal_edge_names));
546                 break;
547         default:
548                 return SR_ERR_NA;
549         }
550
551         return SR_OK;
552 }
553
554 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
555 {
556         struct drv_context *drvc;
557         struct dev_context *devc;
558         struct acquisition_state *acq;
559         int ret;
560
561         (void)cb_data;
562
563         if (sdi->status != SR_ST_ACTIVE)
564                 return SR_ERR_DEV_CLOSED;
565
566         devc = sdi->priv;
567         drvc = di->context;
568
569         if (devc->acquisition) {
570                 sr_err("Acquisition still in progress?");
571                 return SR_ERR;
572         }
573         acq = lwla_alloc_acquisition_state();
574         if (!acq)
575                 return SR_ERR_MALLOC;
576
577         devc->cancel_requested = FALSE;
578         devc->stopping_in_progress = FALSE;
579         devc->transfer_error = FALSE;
580
581         sr_info("Starting acquisition.");
582
583         devc->acquisition = acq;
584         ret = lwla_setup_acquisition(sdi);
585         if (ret != SR_OK) {
586                 sr_err("Failed to set up acquisition.");
587                 devc->acquisition = NULL;
588                 lwla_free_acquisition_state(acq);
589                 return ret;
590         }
591
592         ret = lwla_start_acquisition(sdi);
593         if (ret != SR_OK) {
594                 sr_err("Failed to start acquisition.");
595                 devc->acquisition = NULL;
596                 lwla_free_acquisition_state(acq);
597                 return ret;
598         }
599         usb_source_add(sdi->session, drvc->sr_ctx, 100, &lwla_receive_data,
600                        (struct sr_dev_inst *)sdi);
601
602         sr_info("Waiting for data.");
603
604         /* Send header packet to the session bus. */
605         std_session_send_df_header(sdi, LOG_PREFIX);
606
607         return SR_OK;
608 }
609
610 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
611 {
612         struct dev_context *devc;
613
614         (void)cb_data;
615         devc = sdi->priv;
616
617         if (sdi->status != SR_ST_ACTIVE)
618                 return SR_ERR_DEV_CLOSED;
619
620         if (devc->acquisition && !devc->cancel_requested) {
621                 devc->cancel_requested = TRUE;
622                 sr_dbg("Stopping acquisition.");
623         }
624         return SR_OK;
625 }
626
627 SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
628         .name = "sysclk-lwla",
629         .longname = "SysClk LWLA series",
630         .api_version = 1,
631         .init = init,
632         .cleanup = cleanup,
633         .scan = scan,
634         .dev_list = dev_list,
635         .dev_clear = dev_clear,
636         .config_get = config_get,
637         .config_set = config_set,
638         .config_channel_set = config_channel_set,
639         .config_commit = config_commit,
640         .config_list = config_list,
641         .dev_open = dev_open,
642         .dev_close = dev_close,
643         .dev_acquisition_start = dev_acquisition_start,
644         .dev_acquisition_stop = dev_acquisition_stop,
645         .context = NULL,
646 };