]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014-2017 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" | |
21 | #include "beaglelogic.h" | |
22 | ||
23 | static int beaglelogic_open_nonblock(struct dev_context *devc) | |
24 | { | |
25 | devc->fd = open(BEAGLELOGIC_DEV_NODE, O_RDONLY | O_NONBLOCK); | |
26 | ||
27 | return ((devc->fd == -1) ? SR_ERR : SR_OK); | |
28 | } | |
29 | ||
30 | static int beaglelogic_close(struct dev_context *devc) | |
31 | { | |
32 | return close(devc->fd); | |
33 | } | |
34 | ||
35 | static int beaglelogic_get_buffersize(struct dev_context *devc) | |
36 | { | |
37 | return ioctl(devc->fd, IOCTL_BL_GET_BUFFER_SIZE, &devc->buffersize); | |
38 | } | |
39 | ||
40 | static int beaglelogic_set_buffersize(struct dev_context *devc) | |
41 | { | |
42 | return ioctl(devc->fd, IOCTL_BL_SET_BUFFER_SIZE, devc->buffersize); | |
43 | } | |
44 | ||
45 | /* This is treated differently as it gets a uint64_t while a uint32_t is read */ | |
46 | static int beaglelogic_get_samplerate(struct dev_context *devc) | |
47 | { | |
48 | uint32_t arg, err; | |
49 | ||
50 | err = ioctl(devc->fd, IOCTL_BL_GET_SAMPLE_RATE, &arg); | |
51 | devc->cur_samplerate = arg; | |
52 | ||
53 | return err; | |
54 | } | |
55 | ||
56 | static int beaglelogic_set_samplerate(struct dev_context *devc) | |
57 | { | |
58 | return ioctl(devc->fd, IOCTL_BL_SET_SAMPLE_RATE, | |
59 | (uint32_t)devc->cur_samplerate); | |
60 | } | |
61 | ||
62 | static int beaglelogic_get_sampleunit(struct dev_context *devc) | |
63 | { | |
64 | return ioctl(devc->fd, IOCTL_BL_GET_SAMPLE_UNIT, &devc->sampleunit); | |
65 | } | |
66 | ||
67 | static int beaglelogic_set_sampleunit(struct dev_context *devc) | |
68 | { | |
69 | return ioctl(devc->fd, IOCTL_BL_SET_SAMPLE_UNIT, devc->sampleunit); | |
70 | } | |
71 | ||
72 | static int beaglelogic_get_triggerflags(struct dev_context *devc) | |
73 | { | |
74 | return ioctl(devc->fd, IOCTL_BL_GET_TRIGGER_FLAGS, &devc->triggerflags); | |
75 | } | |
76 | ||
77 | static int beaglelogic_set_triggerflags(struct dev_context *devc) | |
78 | { | |
79 | return ioctl(devc->fd, IOCTL_BL_SET_TRIGGER_FLAGS, devc->triggerflags); | |
80 | } | |
81 | ||
82 | static int beaglelogic_get_lasterror(struct dev_context *devc) | |
83 | { | |
84 | int fd; | |
85 | char buf[16]; | |
86 | int ret; | |
87 | ||
88 | if ((fd = open(BEAGLELOGIC_SYSFS_ATTR(lasterror), O_RDONLY)) == -1) | |
89 | return SR_ERR; | |
90 | ||
91 | if ((ret = read(fd, buf, 16)) < 0) | |
92 | return SR_ERR; | |
93 | ||
94 | close(fd); | |
95 | devc->last_error = strtoul(buf, NULL, 10); | |
96 | ||
97 | return SR_OK; | |
98 | } | |
99 | ||
100 | static int beaglelogic_start(struct dev_context *devc) | |
101 | { | |
102 | return ioctl(devc->fd, IOCTL_BL_START); | |
103 | } | |
104 | ||
105 | static int beaglelogic_stop(struct dev_context *devc) | |
106 | { | |
107 | return ioctl(devc->fd, IOCTL_BL_STOP); | |
108 | } | |
109 | ||
110 | static int beaglelogic_get_bufunitsize(struct dev_context *devc) | |
111 | { | |
112 | return ioctl(devc->fd, IOCTL_BL_GET_BUFUNIT_SIZE, &devc->bufunitsize); | |
113 | } | |
114 | ||
115 | static int beaglelogic_set_bufunitsize(struct dev_context *devc) | |
116 | { | |
117 | return ioctl(devc->fd, IOCTL_BL_SET_BUFUNIT_SIZE, devc->bufunitsize); | |
118 | } | |
119 | ||
120 | static int beaglelogic_mmap(struct dev_context *devc) | |
121 | { | |
122 | if (!devc->buffersize) | |
123 | beaglelogic_get_buffersize(devc); | |
124 | devc->sample_buf = mmap(NULL, devc->buffersize, | |
125 | PROT_READ, MAP_SHARED, devc->fd, 0); | |
126 | ||
127 | return ((devc->sample_buf == MAP_FAILED) ? -1 : SR_OK); | |
128 | } | |
129 | ||
130 | static int beaglelogic_munmap(struct dev_context *devc) | |
131 | { | |
132 | return munmap(devc->sample_buf, devc->buffersize); | |
133 | } | |
134 | ||
135 | SR_PRIV const struct beaglelogic_ops beaglelogic_native_ops = { | |
136 | .open = beaglelogic_open_nonblock, | |
137 | .close = beaglelogic_close, | |
138 | .get_buffersize = beaglelogic_get_buffersize, | |
139 | .set_buffersize = beaglelogic_set_buffersize, | |
140 | .get_samplerate = beaglelogic_get_samplerate, | |
141 | .set_samplerate = beaglelogic_set_samplerate, | |
142 | .get_sampleunit = beaglelogic_get_sampleunit, | |
143 | .set_sampleunit = beaglelogic_set_sampleunit, | |
144 | .get_triggerflags = beaglelogic_get_triggerflags, | |
145 | .set_triggerflags = beaglelogic_set_triggerflags, | |
146 | .start = beaglelogic_start, | |
147 | .stop = beaglelogic_stop, | |
148 | .get_lasterror = beaglelogic_get_lasterror, | |
149 | .get_bufunitsize = beaglelogic_get_bufunitsize, | |
150 | .set_bufunitsize = beaglelogic_set_bufunitsize, | |
151 | .mmap = beaglelogic_mmap, | |
152 | .munmap = beaglelogic_munmap, | |
153 | }; |