2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Kumar Abhishek <abhishek@theembeddedkitchen.net>
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.
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.
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/>.
20 #ifndef BEAGLELOGIC_H_
21 #define BEAGLELOGIC_H_
25 #include <sys/types.h>
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
31 /* BeagleLogic device node name */
32 #define BEAGLELOGIC_DEV_NODE "/dev/beaglelogic"
33 #define BEAGLELOGIC_SYSFS_ATTR(a) "/sys/devices/virtual/misc/beaglelogic/" #a
35 /* Reproduced verbatim from beaglelogic.h in the kernel tree until the kernel
36 * module hits the mainline. Contains the ABI, so DO NOT TOUCH this section */
38 /* ioctl calls that can be issued on /dev/beaglelogic */
39 #define IOCTL_BL_GET_VERSION _IOR('k', 0x20, uint32_t)
41 #define IOCTL_BL_GET_SAMPLE_RATE _IOR('k', 0x21, uint32_t)
42 #define IOCTL_BL_SET_SAMPLE_RATE _IOW('k', 0x21, uint32_t)
44 #define IOCTL_BL_GET_SAMPLE_UNIT _IOR('k', 0x22, uint32_t)
45 #define IOCTL_BL_SET_SAMPLE_UNIT _IOW('k', 0x22, uint32_t)
47 #define IOCTL_BL_GET_TRIGGER_FLAGS _IOR('k', 0x23, uint32_t)
48 #define IOCTL_BL_SET_TRIGGER_FLAGS _IOW('k', 0x23, uint32_t)
50 #define IOCTL_BL_CACHE_INVALIDATE _IO('k', 0x25)
52 #define IOCTL_BL_GET_BUFFER_SIZE _IOR('k', 0x26, uint32_t)
53 #define IOCTL_BL_SET_BUFFER_SIZE _IOW('k', 0x26, uint32_t)
55 #define IOCTL_BL_GET_BUFUNIT_SIZE _IOR('k', 0x27, uint32_t)
57 #define IOCTL_BL_FILL_TEST_PATTERN _IO('k', 0x28)
59 #define IOCTL_BL_START _IO('k', 0x29)
60 #define IOCTL_BL_STOP _IO('k', 0x2A)
62 /* Possible States of BeagleLogic */
63 enum beaglelogic_states {
64 STATE_BL_DISABLED, /* Powered off (at module start) */
65 STATE_BL_INITIALIZED, /* Powered on */
66 STATE_BL_MEMALLOCD, /* Buffers allocated */
67 STATE_BL_ARMED, /* All Buffers DMA-mapped and configuration done */
68 STATE_BL_RUNNING, /* Data being captured */
69 STATE_BL_REQUEST_STOP, /* Stop requested */
70 STATE_BL_ERROR /* Buffer overrun */
73 /* Setting attributes */
74 enum beaglelogic_triggerflags {
75 BL_TRIGGERFLAGS_ONESHOT = 0,
76 BL_TRIGGERFLAGS_CONTINUOUS
79 /* Possible sample unit / formats */
80 enum beaglelogic_sampleunit {
81 BL_SAMPLEUNIT_16_BITS = 0,
84 /* END beaglelogic.h */
86 /* For all the functions below:
88 * devc : Device context structure to operate on
93 SR_PRIV int beaglelogic_open_nonblock(struct dev_context *devc);
94 SR_PRIV int beaglelogic_close(struct dev_context *devc);
96 SR_PRIV int beaglelogic_get_buffersize(struct dev_context *devc);
97 SR_PRIV int beaglelogic_set_buffersize(struct dev_context *devc);
99 SR_PRIV int beaglelogic_get_samplerate(struct dev_context *devc);
100 SR_PRIV int beaglelogic_set_samplerate(struct dev_context *devc);
102 SR_PRIV int beaglelogic_get_sampleunit(struct dev_context *devc);
103 SR_PRIV int beaglelogic_set_sampleunit(struct dev_context *devc);
105 SR_PRIV int beaglelogic_get_triggerflags(struct dev_context *devc);
106 SR_PRIV int beaglelogic_set_triggerflags(struct dev_context *devc);
108 /* Start and stop the capture operation */
109 SR_PRIV int beaglelogic_start(struct dev_context *devc);
110 SR_PRIV int beaglelogic_stop(struct dev_context *devc);
112 /* Get the last error size */
113 SR_PRIV int beaglelogic_getlasterror(struct dev_context *devc);
115 /* Gets the unit size of the capture buffer (usually 4 or 8 MB) */
116 SR_PRIV int beaglelogic_get_bufunitsize(struct dev_context *devc);
118 SR_PRIV int beaglelogic_mmap(struct dev_context *devc);
119 SR_PRIV int beaglelogic_munmap(struct dev_context *devc);
122 SR_PRIV inline int beaglelogic_open_nonblock(struct dev_context *devc) {
123 devc->fd = open(BEAGLELOGIC_DEV_NODE, O_RDONLY | O_NONBLOCK);
124 return (devc->fd == -1 ? SR_ERR : SR_OK);
127 SR_PRIV inline int beaglelogic_close(struct dev_context *devc) {
128 return close(devc->fd);
131 SR_PRIV inline int beaglelogic_get_buffersize(struct dev_context *devc) {
132 return ioctl(devc->fd, IOCTL_BL_GET_BUFFER_SIZE, &devc->buffersize);
135 SR_PRIV inline int beaglelogic_set_buffersize(struct dev_context *devc) {
136 return ioctl(devc->fd, IOCTL_BL_SET_BUFFER_SIZE, devc->buffersize);
139 /* This is treated differently as it gets a uint64_t while a uint32_t is read */
140 SR_PRIV inline int beaglelogic_get_samplerate(struct dev_context *devc) {
142 err = ioctl(devc->fd, IOCTL_BL_GET_SAMPLE_RATE, &arg);
143 devc->cur_samplerate = arg;
147 SR_PRIV inline int beaglelogic_set_samplerate(struct dev_context *devc) {
148 return ioctl(devc->fd, IOCTL_BL_SET_SAMPLE_RATE,
149 (uint32_t)devc->cur_samplerate);
152 SR_PRIV inline int beaglelogic_get_sampleunit(struct dev_context *devc) {
153 return ioctl(devc->fd, IOCTL_BL_GET_SAMPLE_UNIT, &devc->sampleunit);
156 SR_PRIV inline int beaglelogic_set_sampleunit(struct dev_context *devc) {
157 return ioctl(devc->fd, IOCTL_BL_SET_SAMPLE_UNIT, devc->sampleunit);
160 SR_PRIV inline int beaglelogic_get_triggerflags(struct dev_context *devc) {
161 return ioctl(devc->fd, IOCTL_BL_GET_TRIGGER_FLAGS, &devc->triggerflags);
164 SR_PRIV inline int beaglelogic_set_triggerflags(struct dev_context *devc) {
165 return ioctl(devc->fd, IOCTL_BL_SET_TRIGGER_FLAGS, devc->triggerflags);
168 SR_PRIV int beaglelogic_getlasterror(struct dev_context *devc) {
173 if ((fd = open(BEAGLELOGIC_SYSFS_ATTR(lasterror), O_RDONLY)) == -1)
176 if ((ret = read(fd, buf, 16)) < 0)
180 devc->last_error = strtoul(buf, NULL, 10);
185 SR_PRIV inline int beaglelogic_start(struct dev_context *devc) {
186 return ioctl(devc->fd, IOCTL_BL_START);
189 SR_PRIV inline int beaglelogic_stop(struct dev_context *devc) {
190 return ioctl(devc->fd, IOCTL_BL_STOP);
193 SR_PRIV int beaglelogic_get_bufunitsize(struct dev_context *devc) {
194 return ioctl(devc->fd, IOCTL_BL_GET_BUFUNIT_SIZE, &devc->bufunitsize);
197 SR_PRIV int beaglelogic_mmap(struct dev_context *devc) {
198 if (!devc->buffersize)
199 beaglelogic_get_buffersize(devc);
200 devc->sample_buf = mmap(NULL, devc->buffersize,
201 PROT_READ, MAP_SHARED, devc->fd, 0);
202 return (devc->sample_buf == MAP_FAILED ? -1 : SR_OK);
205 SR_PRIV int beaglelogic_munmap(struct dev_context *devc) {
206 return munmap(devc->sample_buf, devc->buffersize);