]> sigrok.org Git - libsigrok.git/blame - src/hardware/beaglelogic/beaglelogic.h
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / beaglelogic / beaglelogic.h
CommitLineData
ad9dbc1c
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#ifndef BEAGLELOGIC_H_
21#define BEAGLELOGIC_H_
22
23#include <fcntl.h>
ad9dbc1c
KA
24#include <sys/mman.h>
25#include <sys/types.h>
26#include <sys/errno.h>
27#include <sys/ioctl.h>
ad9dbc1c 28#include <stdlib.h>
ad9dbc1c
KA
29#include <unistd.h>
30
31/* BeagleLogic device node name */
32#define BEAGLELOGIC_DEV_NODE "/dev/beaglelogic"
33#define BEAGLELOGIC_SYSFS_ATTR(a) "/sys/devices/virtual/misc/beaglelogic/"\
34 __STRING(a)
35
36/* Reproduced verbatim from beaglelogic.h in the kernel tree until the kernel
37 * module hits the mainline. Contains the ABI, so DO NOT TOUCH this section */
38
39/* ioctl calls that can be issued on /dev/beaglelogic */
40#define IOCTL_BL_GET_VERSION _IOR('k', 0x20, uint32_t)
41
42#define IOCTL_BL_GET_SAMPLE_RATE _IOR('k', 0x21, uint32_t)
43#define IOCTL_BL_SET_SAMPLE_RATE _IOW('k', 0x21, uint32_t)
44
45#define IOCTL_BL_GET_SAMPLE_UNIT _IOR('k', 0x22, uint32_t)
46#define IOCTL_BL_SET_SAMPLE_UNIT _IOW('k', 0x22, uint32_t)
47
48#define IOCTL_BL_GET_TRIGGER_FLAGS _IOR('k', 0x23, uint32_t)
49#define IOCTL_BL_SET_TRIGGER_FLAGS _IOW('k', 0x23, uint32_t)
50
51#define IOCTL_BL_CACHE_INVALIDATE _IO('k', 0x25)
52
53#define IOCTL_BL_GET_BUFFER_SIZE _IOR('k', 0x26, uint32_t)
54#define IOCTL_BL_SET_BUFFER_SIZE _IOW('k', 0x26, uint32_t)
55
56#define IOCTL_BL_GET_BUFUNIT_SIZE _IOR('k', 0x27, uint32_t)
57
58#define IOCTL_BL_FILL_TEST_PATTERN _IO('k', 0x28)
59
60#define IOCTL_BL_START _IO('k', 0x29)
61#define IOCTL_BL_STOP _IO('k', 0x2A)
62
63/* Possible States of BeagleLogic */
64enum beaglelogic_states {
65 STATE_BL_DISABLED, /* Powered off (at module start) */
66 STATE_BL_INITIALIZED, /* Powered on */
67 STATE_BL_MEMALLOCD, /* Buffers allocated */
68 STATE_BL_ARMED, /* All Buffers DMA-mapped and configuration done */
69 STATE_BL_RUNNING, /* Data being captured */
70 STATE_BL_REQUEST_STOP, /* Stop requested */
71 STATE_BL_ERROR /* Buffer overrun */
72};
73
74/* Setting attributes */
75enum beaglelogic_triggerflags {
76 BL_TRIGGERFLAGS_ONESHOT = 0,
77 BL_TRIGGERFLAGS_CONTINUOUS
78};
79
80/* Possible sample unit / formats */
81enum beaglelogic_sampleunit {
82 BL_SAMPLEUNIT_16_BITS = 0,
83 BL_SAMPLEUNIT_8_BITS
84};
85/* END beaglelogic.h */
86
87/* For all the functions below:
88 * Parameters:
89 * devc : Device context structure to operate on
90 * Returns:
91 * SR_OK or SR_ERR
92 */
93
94SR_PRIV int beaglelogic_open_nonblock(struct dev_context *devc);
95SR_PRIV int beaglelogic_close(struct dev_context *devc);
96
97SR_PRIV int beaglelogic_get_buffersize(struct dev_context *devc);
98SR_PRIV int beaglelogic_set_buffersize(struct dev_context *devc);
99
100SR_PRIV int beaglelogic_get_samplerate(struct dev_context *devc);
101SR_PRIV int beaglelogic_set_samplerate(struct dev_context *devc);
102
103SR_PRIV int beaglelogic_get_sampleunit(struct dev_context *devc);
104SR_PRIV int beaglelogic_set_sampleunit(struct dev_context *devc);
105
106SR_PRIV int beaglelogic_get_triggerflags(struct dev_context *devc);
107SR_PRIV int beaglelogic_set_triggerflags(struct dev_context *devc);
108
109/* Start and stop the capture operation */
110SR_PRIV int beaglelogic_start(struct dev_context *devc);
111SR_PRIV int beaglelogic_stop(struct dev_context *devc);
112
113/* Get the last error size */
114SR_PRIV int beaglelogic_getlasterror(struct dev_context *devc);
115
116/* Gets the unit size of the capture buffer (usually 4 or 8 MB) */
117SR_PRIV int beaglelogic_get_bufunitsize(struct dev_context *devc);
118
119SR_PRIV int beaglelogic_mmap(struct dev_context *devc);
120SR_PRIV int beaglelogic_munmap(struct dev_context *devc);
121
122/* Sources */
123SR_PRIV inline int beaglelogic_open_nonblock(struct dev_context *devc) {
124 devc->fd = open(BEAGLELOGIC_DEV_NODE, O_RDONLY | O_NONBLOCK);
125 return (devc->fd == -1 ? SR_ERR : SR_OK);
126}
127
128SR_PRIV inline int beaglelogic_close(struct dev_context *devc) {
129 return close(devc->fd);
130}
131
132SR_PRIV inline int beaglelogic_get_buffersize(struct dev_context *devc) {
133 return ioctl(devc->fd, IOCTL_BL_GET_BUFFER_SIZE, &devc->buffersize);
134}
135
136SR_PRIV inline int beaglelogic_set_buffersize(struct dev_context *devc) {
137 return ioctl(devc->fd, IOCTL_BL_SET_BUFFER_SIZE, devc->buffersize);
138}
139
140/* This is treated differently as it gets a uint64_t while a uint32_t is read */
141SR_PRIV inline int beaglelogic_get_samplerate(struct dev_context *devc) {
142 uint32_t arg, err;
143 err = ioctl(devc->fd, IOCTL_BL_GET_SAMPLE_RATE, &arg);
144 devc->cur_samplerate = arg;
145 return err;
146}
147
148SR_PRIV inline int beaglelogic_set_samplerate(struct dev_context *devc) {
149 return ioctl(devc->fd, IOCTL_BL_SET_SAMPLE_RATE,
150 (uint32_t)devc->cur_samplerate);
151}
152
153SR_PRIV inline int beaglelogic_get_sampleunit(struct dev_context *devc) {
154 return ioctl(devc->fd, IOCTL_BL_GET_SAMPLE_UNIT, &devc->sampleunit);
155}
156
157SR_PRIV inline int beaglelogic_set_sampleunit(struct dev_context *devc) {
158 return ioctl(devc->fd, IOCTL_BL_SET_SAMPLE_UNIT, devc->sampleunit);
159}
160
161SR_PRIV inline int beaglelogic_get_triggerflags(struct dev_context *devc) {
162 return ioctl(devc->fd, IOCTL_BL_GET_TRIGGER_FLAGS, &devc->triggerflags);
163}
164
165SR_PRIV inline int beaglelogic_set_triggerflags(struct dev_context *devc) {
166 return ioctl(devc->fd, IOCTL_BL_SET_TRIGGER_FLAGS, devc->triggerflags);
167}
168
169SR_PRIV int beaglelogic_getlasterror(struct dev_context *devc) {
170 int fd;
171 char buf[16];
172 int ret;
173
174 if ((fd = open(BEAGLELOGIC_SYSFS_ATTR(lasterror), O_RDONLY)) == -1)
175 return SR_ERR;
176
177 if ((ret = read(fd, buf, 16)) < 0)
178 return SR_ERR;
179
180 close(fd);
181 devc->last_error = strtoul(buf, NULL, 10);
182
183 return SR_OK;
184}
185
186SR_PRIV inline int beaglelogic_start(struct dev_context *devc) {
187 return ioctl(devc->fd, IOCTL_BL_START);
188}
189
190SR_PRIV inline int beaglelogic_stop(struct dev_context *devc) {
191 return ioctl(devc->fd, IOCTL_BL_STOP);
192}
193
194SR_PRIV int beaglelogic_get_bufunitsize(struct dev_context *devc) {
195 return ioctl(devc->fd, IOCTL_BL_GET_BUFUNIT_SIZE, &devc->bufunitsize);
196}
197
198SR_PRIV int beaglelogic_mmap(struct dev_context *devc) {
199 if (!devc->buffersize)
200 beaglelogic_get_buffersize(devc);
201 devc->sample_buf = mmap(NULL, devc->buffersize,
202 PROT_READ, MAP_SHARED, devc->fd, 0);
203 return (devc->sample_buf == MAP_FAILED ? -1 : SR_OK);
204}
205
206SR_PRIV int beaglelogic_munmap(struct dev_context *devc) {
207 return munmap(devc->sample_buf, devc->buffersize);
208}
209
db24496a 210#endif