]>
Commit | Line | Data |
---|---|---|
6ed4f044 | 1 | /* |
6944b2d0 | 2 | * This file is part of the libsigrok project. |
6ed4f044 DR |
3 | * |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
0254651d | 5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> |
6944b2d0 | 6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
6ed4f044 DR |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
9cd9f6b7 AG |
23 | #include <stdlib.h> |
24 | #include <unistd.h> | |
25 | #include <string.h> | |
721ecf3d UH |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
28 | #include "protocol.h" | |
472bbb46 | 29 | |
915f7cc8 | 30 | static const int hwcaps[] = { |
1953564a BV |
31 | SR_CONF_SAMPLERATE, |
32 | SR_CONF_LIMIT_SAMPLES, | |
33 | SR_CONF_CONTINUOUS, | |
0254651d | 34 | 0, |
6ed4f044 DR |
35 | }; |
36 | ||
0254651d UH |
37 | SR_PRIV struct sr_dev_driver alsa_driver_info; |
38 | static struct sr_dev_driver *di = &alsa_driver_info; | |
6ed4f044 | 39 | |
0254651d UH |
40 | static int clear_instances(void) |
41 | { | |
6944b2d0 AG |
42 | struct drv_context *drvc; |
43 | ||
44 | if (!(drvc = di->priv)) | |
45 | return SR_OK; | |
46 | ||
47 | g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear); | |
48 | drvc->instances = NULL; | |
0254651d UH |
49 | |
50 | return SR_OK; | |
51 | } | |
52 | ||
34f06b90 | 53 | static int hw_init(struct sr_context *sr_ctx) |
6ed4f044 | 54 | { |
063e7aef | 55 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); |
0254651d UH |
56 | } |
57 | ||
58 | static GSList *hw_scan(GSList *options) | |
59 | { | |
6944b2d0 | 60 | return alsa_scan(options, di); |
6ed4f044 DR |
61 | } |
62 | ||
0254651d | 63 | static GSList *hw_dev_list(void) |
6ed4f044 | 64 | { |
0e94d524 | 65 | return ((struct drv_context *)(di->priv))->instances; |
0254651d UH |
66 | } |
67 | ||
68 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
69 | { | |
70 | struct dev_context *devc; | |
ebc34738 | 71 | int ret; |
6ed4f044 | 72 | |
0254651d | 73 | devc = sdi->priv; |
6ed4f044 | 74 | |
6944b2d0 AG |
75 | if (!(devc->hwdev)) { |
76 | sr_err("devc->hwdev was NULL."); | |
77 | return SR_ERR_BUG; | |
78 | } | |
79 | ||
80 | sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev); | |
81 | ret = snd_pcm_open(&devc->capture_handle, devc->hwdev, | |
ea9cfed7 | 82 | SND_PCM_STREAM_CAPTURE, 0); |
ebc34738 | 83 | if (ret < 0) { |
0254651d | 84 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); |
e46b8fb1 | 85 | return SR_ERR; |
6ed4f044 DR |
86 | } |
87 | ||
0254651d UH |
88 | sr_dbg("Initializing hardware parameter structure."); |
89 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
ebc34738 | 90 | if (ret < 0) { |
0254651d | 91 | sr_err("Can't initialize hardware parameter structure: %s.", |
472bbb46 | 92 | snd_strerror(ret)); |
e46b8fb1 | 93 | return SR_ERR; |
6ed4f044 DR |
94 | } |
95 | ||
e46b8fb1 | 96 | return SR_OK; |
6ed4f044 DR |
97 | } |
98 | ||
0254651d | 99 | static int hw_dev_close(struct sr_dev_inst *sdi) |
6ed4f044 | 100 | { |
0254651d UH |
101 | int ret; |
102 | struct dev_context *devc; | |
6ed4f044 | 103 | |
0254651d UH |
104 | devc = sdi->priv; |
105 | ||
0254651d UH |
106 | if (devc->capture_handle) { |
107 | sr_dbg("Closing PCM device."); | |
108 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) { | |
109 | sr_err("Failed to close device: %s.", | |
110 | snd_strerror(ret)); | |
6944b2d0 | 111 | devc->capture_handle = NULL; |
0254651d UH |
112 | } |
113 | } else { | |
114 | sr_dbg("No capture handle, no need to close audio device."); | |
115 | } | |
697785d1 UH |
116 | |
117 | return SR_OK; | |
6ed4f044 DR |
118 | } |
119 | ||
57ab7d9f | 120 | static int hw_cleanup(void) |
6ed4f044 | 121 | { |
0254651d | 122 | clear_instances(); |
57ab7d9f UH |
123 | |
124 | return SR_OK; | |
6ed4f044 DR |
125 | } |
126 | ||
035a1078 | 127 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) |
6ed4f044 | 128 | { |
0254651d | 129 | struct dev_context *devc; |
6ed4f044 | 130 | |
035a1078 | 131 | switch (id) { |
123e1313 | 132 | case SR_CONF_SAMPLERATE: |
9a6517d1 | 133 | devc = sdi->priv; |
0254651d | 134 | *data = &devc->cur_samplerate; |
6ed4f044 | 135 | break; |
0254651d | 136 | default: |
0254651d | 137 | return SR_ERR_ARG; |
6ed4f044 DR |
138 | } |
139 | ||
0254651d | 140 | return SR_OK; |
6ed4f044 DR |
141 | } |
142 | ||
035a1078 | 143 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
6ed4f044 | 144 | { |
0254651d | 145 | struct dev_context *devc; |
6ed4f044 | 146 | |
0254651d | 147 | devc = sdi->priv; |
6ed4f044 | 148 | |
035a1078 | 149 | switch (id) { |
1953564a | 150 | case SR_CONF_SAMPLERATE: |
65faa197 | 151 | alsa_set_samplerate(sdi, *(const uint64_t *)value); |
0254651d | 152 | break; |
1953564a | 153 | case SR_CONF_LIMIT_SAMPLES: |
0254651d UH |
154 | devc->limit_samples = *(const uint64_t *)value; |
155 | break; | |
6ed4f044 | 156 | default: |
035a1078 | 157 | sr_err("Unknown capability: %d.", id); |
e46b8fb1 | 158 | return SR_ERR; |
6ed4f044 | 159 | } |
0254651d UH |
160 | |
161 | return SR_OK; | |
6ed4f044 DR |
162 | } |
163 | ||
a1c743fc BV |
164 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
165 | { | |
166 | struct dev_context *devc; | |
167 | ||
168 | (void)sdi; | |
169 | ||
170 | switch (key) { | |
9a6517d1 BV |
171 | case SR_CONF_DEVICE_OPTIONS: |
172 | *data = hwcaps; | |
173 | break; | |
a1c743fc BV |
174 | case SR_CONF_SAMPLERATE: |
175 | if (!sdi || !sdi->priv) | |
176 | return SR_ERR_ARG; | |
177 | devc = sdi->priv; | |
178 | if (!devc->supp_rates.list) { | |
179 | sr_err("Instance did not contain a samplerate list."); | |
180 | return SR_ERR_ARG; | |
181 | } | |
182 | *data = &devc->supp_rates; | |
183 | break; | |
184 | default: | |
185 | return SR_ERR_ARG; | |
186 | } | |
187 | ||
188 | return SR_OK; | |
189 | } | |
190 | ||
0254651d UH |
191 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
192 | void *cb_data) | |
6ed4f044 | 193 | { |
b9c735a2 UH |
194 | struct sr_datafeed_packet packet; |
195 | struct sr_datafeed_header header; | |
0254651d UH |
196 | struct dev_context *devc; |
197 | int count, ret; | |
721ecf3d | 198 | char *endianness; |
6ed4f044 | 199 | |
0254651d UH |
200 | devc = sdi->priv; |
201 | devc->cb_data = cb_data; | |
729850c9 | 202 | devc->num_samples = 0; |
6ed4f044 | 203 | |
0254651d UH |
204 | sr_dbg("Setting audio access type to RW/interleaved."); |
205 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
206 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
ebc34738 | 207 | if (ret < 0) { |
0254651d | 208 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); |
e46b8fb1 | 209 | return SR_ERR; |
6ed4f044 DR |
210 | } |
211 | ||
0254651d | 212 | /* FIXME: Hardcoded for 16bits. */ |
729850c9 | 213 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) |
721ecf3d | 214 | endianness = "little endian"; |
729850c9 | 215 | else |
721ecf3d UH |
216 | endianness = "big endian"; |
217 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
0254651d | 218 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, |
729850c9 AG |
219 | devc->hw_params, |
220 | SND_PCM_FORMAT_S16); | |
ebc34738 | 221 | if (ret < 0) { |
0254651d | 222 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); |
e46b8fb1 | 223 | return SR_ERR; |
6ed4f044 DR |
224 | } |
225 | ||
0254651d UH |
226 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", |
227 | devc->cur_samplerate); | |
0d6ff103 AG |
228 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, |
229 | (unsigned int)devc->cur_samplerate, 0); | |
ebc34738 | 230 | if (ret < 0) { |
0254651d | 231 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); |
e46b8fb1 | 232 | return SR_ERR; |
6ed4f044 DR |
233 | } |
234 | ||
9cd9f6b7 | 235 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); |
0254651d | 236 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, |
9cd9f6b7 | 237 | devc->hw_params, devc->num_probes); |
ebc34738 | 238 | if (ret < 0) { |
0254651d | 239 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); |
e46b8fb1 | 240 | return SR_ERR; |
6ed4f044 DR |
241 | } |
242 | ||
0254651d UH |
243 | sr_dbg("Setting audio parameters."); |
244 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
ebc34738 | 245 | if (ret < 0) { |
0254651d | 246 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); |
e46b8fb1 | 247 | return SR_ERR; |
6ed4f044 DR |
248 | } |
249 | ||
0254651d UH |
250 | sr_dbg("Preparing audio interface for use."); |
251 | ret = snd_pcm_prepare(devc->capture_handle); | |
ebc34738 | 252 | if (ret < 0) { |
0254651d | 253 | sr_err("Can't prepare audio interface for use: %s.", |
ebc34738 | 254 | snd_strerror(ret)); |
e46b8fb1 | 255 | return SR_ERR; |
6ed4f044 DR |
256 | } |
257 | ||
0254651d | 258 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); |
6ed4f044 | 259 | if (count < 1) { |
472bbb46 | 260 | sr_err("Unable to obtain poll descriptors count."); |
e46b8fb1 | 261 | return SR_ERR; |
6ed4f044 DR |
262 | } |
263 | ||
0254651d UH |
264 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { |
265 | sr_err("Failed to malloc ufds."); | |
e46b8fb1 | 266 | return SR_ERR_MALLOC; |
92b3101c | 267 | } |
6ed4f044 | 268 | |
3d6de074 | 269 | sr_spew("Getting %d poll descriptors.", count); |
0254651d | 270 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); |
ebc34738 | 271 | if (ret < 0) { |
0254651d | 272 | sr_err("Unable to obtain poll descriptors: %s.", |
ebc34738 | 273 | snd_strerror(ret)); |
0254651d | 274 | g_free(devc->ufds); |
e46b8fb1 | 275 | return SR_ERR; |
6ed4f044 DR |
276 | } |
277 | ||
0254651d UH |
278 | /* Send header packet to the session bus. */ |
279 | sr_dbg("Sending SR_DF_HEADER packet."); | |
5a2326a7 | 280 | packet.type = SR_DF_HEADER; |
0254651d | 281 | packet.payload = (uint8_t *)&header; |
6ed4f044 DR |
282 | header.feed_version = 1; |
283 | gettimeofday(&header.starttime, NULL); | |
0254651d UH |
284 | sr_session_send(devc->cb_data, &packet); |
285 | ||
0254651d UH |
286 | /* Poll every 10ms, or whenever some data comes in. */ |
287 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
9cd9f6b7 | 288 | alsa_receive_data, (void *)sdi); |
0254651d UH |
289 | |
290 | // g_free(devc->ufds); /* FIXME */ | |
6ed4f044 | 291 | |
e46b8fb1 | 292 | return SR_OK; |
6ed4f044 DR |
293 | } |
294 | ||
0254651d | 295 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 296 | { |
0254651d UH |
297 | struct sr_datafeed_packet packet; |
298 | struct dev_context *devc; | |
299 | ||
300 | devc = sdi->priv; | |
301 | devc->cb_data = cb_data; | |
302 | ||
303 | sr_source_remove(devc->ufds[0].fd); | |
304 | ||
305 | /* Send end packet to the session bus. */ | |
306 | sr_dbg("Sending SR_DF_END packet."); | |
307 | packet.type = SR_DF_END; | |
308 | sr_session_send(cb_data, &packet); | |
3010f21c UH |
309 | |
310 | return SR_OK; | |
6ed4f044 DR |
311 | } |
312 | ||
c09f0b57 | 313 | SR_PRIV struct sr_dev_driver alsa_driver_info = { |
e519ba86 UH |
314 | .name = "alsa", |
315 | .longname = "ALSA driver", | |
316 | .api_version = 1, | |
317 | .init = hw_init, | |
318 | .cleanup = hw_cleanup, | |
0254651d UH |
319 | .scan = hw_scan, |
320 | .dev_list = hw_dev_list, | |
321 | .dev_clear = clear_instances, | |
035a1078 BV |
322 | .config_get = config_get, |
323 | .config_set = config_set, | |
a1c743fc | 324 | .config_list = config_list, |
e7eb703f UH |
325 | .dev_open = hw_dev_open, |
326 | .dev_close = hw_dev_close, | |
69040b7c UH |
327 | .dev_acquisition_start = hw_dev_acquisition_start, |
328 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
0254651d | 329 | .priv = NULL, |
6ed4f044 | 330 | }; |