]> sigrok.org Git - libsigrok.git/blob - src/hardware/beaglelogic/api.c
Introduce standard cleanup helper
[libsigrok.git] / src / hardware / beaglelogic / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Kumar Abhishek <abhishek@theembeddedkitchen.net>
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 "protocol.h"
22 #include "beaglelogic.h"
23
24 SR_PRIV struct sr_dev_driver beaglelogic_driver_info;
25
26 /* Scan options */
27 static const uint32_t scanopts[] = {
28         SR_CONF_NUM_LOGIC_CHANNELS,
29 };
30
31 /* Hardware capabilities */
32 static const uint32_t devopts[] = {
33         SR_CONF_LOGIC_ANALYZER,
34         SR_CONF_CONTINUOUS,
35         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36         SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET,
37         SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
38         SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
39         SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_GET,
40 };
41
42 /* Trigger matching capabilities */
43 static const int32_t soft_trigger_matches[] = {
44         SR_TRIGGER_ZERO,
45         SR_TRIGGER_ONE,
46         SR_TRIGGER_RISING,
47         SR_TRIGGER_FALLING,
48         SR_TRIGGER_EDGE,
49 };
50
51 SR_PRIV const char *channel_names[] = {
52         "P8_45", "P8_46", "P8_43", "P8_44", "P8_41", "P8_42", "P8_39",
53         "P8_40", "P8_27", "P8_29", "P8_28", "P8_30", "P8_21", "P8_20",
54 };
55
56 /* Possible sample rates : 10 Hz to 100 MHz = (100 / x) MHz */
57 static const uint64_t samplerates[] = {
58         SR_HZ(10),
59         SR_MHZ(100),
60         SR_HZ(1),
61 };
62
63 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
64 {
65         return std_init(sr_ctx, di, LOG_PREFIX);
66 }
67
68 static struct dev_context *beaglelogic_devc_alloc(void)
69 {
70         struct dev_context *devc;
71
72         devc = g_malloc0(sizeof(struct dev_context));
73
74         /* Default non-zero values (if any) */
75         devc->fd = -1;
76         devc->limit_samples = (uint64_t)-1;
77
78         return devc;
79 }
80
81 static GSList *scan(struct sr_dev_driver *di, GSList *options)
82 {
83         struct drv_context *drvc;
84         GSList *devices, *l;
85         struct sr_config *src;
86         struct sr_dev_inst *sdi;
87         struct dev_context *devc;
88         int i, maxch;
89
90         devices = NULL;
91         drvc = di->context;
92         drvc->instances = NULL;
93
94         /* Probe for /dev/beaglelogic */
95         if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
96                 return NULL;
97
98         sdi = g_malloc0(sizeof(struct sr_dev_inst));
99         sdi->status = SR_ST_INACTIVE;
100         sdi->model = g_strdup("BeagleLogic");
101         sdi->version = g_strdup("1.0");
102         sdi->driver = di;
103
104         /* Unless explicitly specified, keep max channels to 8 only */
105         maxch = 8;
106         for (l = options; l; l = l->next) {
107                 src = l->data;
108                 if (src->key == SR_CONF_NUM_LOGIC_CHANNELS)
109                         maxch = g_variant_get_int32(src->data);
110         }
111
112         /* We need to test for number of channels by opening the node */
113         devc = beaglelogic_devc_alloc();
114
115         if (beaglelogic_open_nonblock(devc) != SR_OK) {
116                 g_free(devc);
117                 sr_dev_inst_free(sdi);
118
119                 return NULL;
120         }
121
122         if (maxch > 8) {
123                 maxch = NUM_CHANNELS;
124                 devc->sampleunit = BL_SAMPLEUNIT_16_BITS;
125         } else {
126                 maxch = 8;
127                 devc->sampleunit = BL_SAMPLEUNIT_8_BITS;
128         }
129
130         beaglelogic_set_sampleunit(devc);
131         beaglelogic_close(devc);
132
133         /* Signal */
134         sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);
135
136         /* Fill the channels */
137         for (i = 0; i < maxch; i++)
138                 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
139                                 channel_names[i]);
140
141         sdi->priv = devc;
142         drvc->instances = g_slist_append(drvc->instances, sdi);
143         devices = g_slist_append(devices, sdi);
144
145         return devices;
146 }
147
148 static GSList *dev_list(const struct sr_dev_driver *di)
149 {
150         return ((struct drv_context *)(di->context))->instances;
151 }
152
153 static int dev_clear(const struct sr_dev_driver *di)
154 {
155         return std_dev_clear(di, NULL);
156 }
157
158 static int dev_open(struct sr_dev_inst *sdi)
159 {
160         struct dev_context *devc = sdi->priv;
161
162         /* Open BeagleLogic */
163         if (beaglelogic_open_nonblock(devc))
164                 return SR_ERR;
165
166         /* Set fd and local attributes */
167         devc->pollfd.fd = devc->fd;
168         devc->pollfd.events = G_IO_IN;
169         devc->pollfd.revents = 0;
170
171         /* Get the default attributes */
172         beaglelogic_get_samplerate(devc);
173         beaglelogic_get_sampleunit(devc);
174         beaglelogic_get_triggerflags(devc);
175         beaglelogic_get_buffersize(devc);
176         beaglelogic_get_bufunitsize(devc);
177
178         /* Map the kernel capture FIFO for reads, saves 1 level of memcpy */
179         if (beaglelogic_mmap(devc) != SR_OK) {
180                 sr_err("Unable to map capture buffer");
181                 beaglelogic_close(devc);
182                 return SR_ERR;
183         }
184
185         /* We're good to go now */
186         sdi->status = SR_ST_ACTIVE;
187         return SR_OK;
188 }
189
190 static int dev_close(struct sr_dev_inst *sdi)
191 {
192         struct dev_context *devc = sdi->priv;
193
194         if (sdi->status == SR_ST_ACTIVE) {
195                 /* Close the memory mapping and the file */
196                 beaglelogic_munmap(devc);
197                 beaglelogic_close(devc);
198         }
199         sdi->status = SR_ST_INACTIVE;
200         return SR_OK;
201 }
202
203 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
204                 const struct sr_channel_group *cg)
205 {
206         struct dev_context *devc = sdi->priv;
207
208         (void)cg;
209
210         switch (key) {
211         case SR_CONF_LIMIT_SAMPLES:
212                 *data = g_variant_new_uint64(devc->limit_samples);
213                 break;
214         case SR_CONF_SAMPLERATE:
215                 *data = g_variant_new_uint64(devc->cur_samplerate);
216                 break;
217         case SR_CONF_CAPTURE_RATIO:
218                 *data = g_variant_new_uint64(devc->capture_ratio);
219                 break;
220         case SR_CONF_NUM_LOGIC_CHANNELS:
221                 *data = g_variant_new_uint32(g_slist_length(sdi->channels));
222                 break;
223         default:
224                 return SR_ERR_NA;
225         }
226
227         return SR_OK;
228 }
229
230 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
231                 const struct sr_channel_group *cg)
232 {
233         struct dev_context *devc = sdi->priv;
234         uint64_t tmp_u64;
235
236         (void)cg;
237
238         if (sdi->status != SR_ST_ACTIVE)
239                 return SR_ERR_DEV_CLOSED;
240
241         switch (key) {
242         case SR_CONF_SAMPLERATE:
243                 devc->cur_samplerate = g_variant_get_uint64(data);
244                 return beaglelogic_set_samplerate(devc);
245         case SR_CONF_LIMIT_SAMPLES:
246                 tmp_u64 = g_variant_get_uint64(data);
247                 devc->limit_samples = tmp_u64;
248                 devc->triggerflags = BL_TRIGGERFLAGS_ONESHOT;
249
250                 /* Check if we have sufficient buffer size */
251                 tmp_u64 *= SAMPLEUNIT_TO_BYTES(devc->sampleunit);
252                 if (tmp_u64 > devc->buffersize) {
253                         sr_warn("Insufficient buffer space has been allocated.");
254                         sr_warn("Please use \'echo <size in bytes> > "\
255                                 BEAGLELOGIC_SYSFS_ATTR(memalloc) \
256                                 "\' as root to increase the buffer size, this"\
257                                 " capture is now truncated to %d Msamples",
258                                 devc->buffersize /
259                                 (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000));
260                 }
261                 return beaglelogic_set_triggerflags(devc);
262         case SR_CONF_CAPTURE_RATIO:
263                 devc->capture_ratio = g_variant_get_uint64(data);
264                 if (devc->capture_ratio > 100)
265                         return SR_ERR;
266                 return SR_OK;
267         default:
268                 return SR_ERR_NA;
269         }
270
271         return SR_OK;
272 }
273
274 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
275                 const struct sr_channel_group *cg)
276 {
277         int ret;
278         GVariant *gvar;
279         GVariantBuilder gvb;
280
281         (void)sdi;
282         (void)cg;
283
284         ret = SR_OK;
285         switch (key) {
286         case SR_CONF_SCAN_OPTIONS:
287                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
288                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
289                 break;
290         case SR_CONF_DEVICE_OPTIONS:
291                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
292                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
293                 break;
294         case SR_CONF_SAMPLERATE:
295                 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
296                 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
297                         samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
298                 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
299                 *data = g_variant_builder_end(&gvb);
300                 break;
301         case SR_CONF_TRIGGER_MATCH:
302                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
303                                 soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
304                                 sizeof(int32_t));
305                 break;
306         default:
307                 return SR_ERR_NA;
308         }
309
310         return ret;
311 }
312
313 /* get a sane timeout for poll() */
314 #define BUFUNIT_TIMEOUT_MS(devc)        (100 + ((devc->bufunitsize * 1000) / \
315                                 (uint32_t)(devc->cur_samplerate)))
316
317 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
318 {
319         struct dev_context *devc = sdi->priv;
320         struct sr_trigger *trigger;
321
322         if (sdi->status != SR_ST_ACTIVE)
323                 return SR_ERR_DEV_CLOSED;
324
325         /* Clear capture state */
326         devc->bytes_read = 0;
327         devc->offset = 0;
328
329         /* Configure channels */
330         devc->sampleunit = g_slist_length(sdi->channels) > 8 ?
331                         BL_SAMPLEUNIT_16_BITS : BL_SAMPLEUNIT_8_BITS;
332         beaglelogic_set_sampleunit(devc);
333
334         /* Configure triggers & send header packet */
335         if ((trigger = sr_session_trigger_get(sdi->session))) {
336                 int pre_trigger_samples = 0;
337                 if (devc->limit_samples > 0)
338                         pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
339                 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
340                 if (!devc->stl)
341                         return SR_ERR_MALLOC;
342                 devc->trigger_fired = FALSE;
343         } else
344                 devc->trigger_fired = TRUE;
345         std_session_send_df_header(sdi, LOG_PREFIX);
346
347         /* Trigger and add poll on file */
348         beaglelogic_start(devc);
349         sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
350                         BUFUNIT_TIMEOUT_MS(devc), beaglelogic_receive_data,
351                         (void *)sdi);
352
353         return SR_OK;
354 }
355
356 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
357 {
358         struct dev_context *devc = sdi->priv;
359
360         if (sdi->status != SR_ST_ACTIVE)
361                 return SR_ERR_DEV_CLOSED;
362
363         /* Execute a stop on BeagleLogic */
364         beaglelogic_stop(devc);
365
366         /* lseek to offset 0, flushes the cache */
367         lseek(devc->fd, 0, SEEK_SET);
368
369         /* Remove session source and send EOT packet */
370         sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
371         std_session_send_df_end(sdi, LOG_PREFIX);
372
373         return SR_OK;
374 }
375
376 SR_PRIV struct sr_dev_driver beaglelogic_driver_info = {
377         .name = "beaglelogic",
378         .longname = "BeagleLogic",
379         .api_version = 1,
380         .init = init,
381         .cleanup = std_cleanup,
382         .scan = scan,
383         .dev_list = dev_list,
384         .dev_clear = dev_clear,
385         .config_get = config_get,
386         .config_set = config_set,
387         .config_list = config_list,
388         .dev_open = dev_open,
389         .dev_close = dev_close,
390         .dev_acquisition_start = dev_acquisition_start,
391         .dev_acquisition_stop = dev_acquisition_stop,
392         .context = NULL,
393 };