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