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