]>
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 | |
aa0dbd68 | 30 | static const int32_t hwcaps[] = { |
1953564a BV |
31 | SR_CONF_SAMPLERATE, |
32 | SR_CONF_LIMIT_SAMPLES, | |
33 | SR_CONF_CONTINUOUS, | |
6ed4f044 DR |
34 | }; |
35 | ||
0254651d UH |
36 | SR_PRIV struct sr_dev_driver alsa_driver_info; |
37 | static struct sr_dev_driver *di = &alsa_driver_info; | |
6ed4f044 | 38 | |
c43cf4d4 | 39 | static void clear_helper(void *priv) |
0254651d | 40 | { |
c43cf4d4 | 41 | struct dev_context *devc; |
6944b2d0 | 42 | |
c43cf4d4 | 43 | devc = priv; |
6944b2d0 | 44 | |
c43cf4d4 | 45 | snd_pcm_hw_params_free(devc->hw_params); |
e11a1eba UH |
46 | devc->hw_params = NULL; |
47 | ||
c43cf4d4 | 48 | g_free((void *)devc->samplerates); |
e11a1eba | 49 | devc->samplerates = NULL; |
c43cf4d4 | 50 | } |
0254651d | 51 | |
3b412e3a | 52 | static int dev_clear(void) |
c43cf4d4 UH |
53 | { |
54 | return std_dev_clear(di, clear_helper); | |
0254651d UH |
55 | } |
56 | ||
6078d2c9 | 57 | static int init(struct sr_context *sr_ctx) |
6ed4f044 | 58 | { |
f6beaac5 | 59 | return std_init(sr_ctx, di, LOG_PREFIX); |
0254651d UH |
60 | } |
61 | ||
6078d2c9 | 62 | static GSList *scan(GSList *options) |
0254651d | 63 | { |
6944b2d0 | 64 | return alsa_scan(options, di); |
6ed4f044 DR |
65 | } |
66 | ||
6078d2c9 | 67 | static GSList *dev_list(void) |
6ed4f044 | 68 | { |
0e94d524 | 69 | return ((struct drv_context *)(di->priv))->instances; |
0254651d UH |
70 | } |
71 | ||
6078d2c9 | 72 | static int dev_open(struct sr_dev_inst *sdi) |
0254651d UH |
73 | { |
74 | struct dev_context *devc; | |
ebc34738 | 75 | int ret; |
6ed4f044 | 76 | |
0254651d | 77 | devc = sdi->priv; |
6ed4f044 | 78 | |
6944b2d0 AG |
79 | if (!(devc->hwdev)) { |
80 | sr_err("devc->hwdev was NULL."); | |
81 | return SR_ERR_BUG; | |
82 | } | |
83 | ||
84 | sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev); | |
85 | ret = snd_pcm_open(&devc->capture_handle, devc->hwdev, | |
ea9cfed7 | 86 | SND_PCM_STREAM_CAPTURE, 0); |
ebc34738 | 87 | if (ret < 0) { |
0254651d | 88 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); |
e46b8fb1 | 89 | return SR_ERR; |
6ed4f044 DR |
90 | } |
91 | ||
0254651d UH |
92 | sr_dbg("Initializing hardware parameter structure."); |
93 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
ebc34738 | 94 | if (ret < 0) { |
0254651d | 95 | sr_err("Can't initialize hardware parameter structure: %s.", |
472bbb46 | 96 | snd_strerror(ret)); |
e46b8fb1 | 97 | return SR_ERR; |
6ed4f044 DR |
98 | } |
99 | ||
e73ffd42 BV |
100 | sdi->status = SR_ST_ACTIVE; |
101 | ||
e46b8fb1 | 102 | return SR_OK; |
6ed4f044 DR |
103 | } |
104 | ||
6078d2c9 | 105 | static int dev_close(struct sr_dev_inst *sdi) |
6ed4f044 | 106 | { |
0254651d UH |
107 | int ret; |
108 | struct dev_context *devc; | |
6ed4f044 | 109 | |
0254651d UH |
110 | devc = sdi->priv; |
111 | ||
0254651d UH |
112 | if (devc->capture_handle) { |
113 | sr_dbg("Closing PCM device."); | |
e11a1eba | 114 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) |
0254651d UH |
115 | sr_err("Failed to close device: %s.", |
116 | snd_strerror(ret)); | |
e11a1eba UH |
117 | devc->capture_handle = NULL; |
118 | sdi->status = SR_ST_INACTIVE; | |
0254651d UH |
119 | } else { |
120 | sr_dbg("No capture handle, no need to close audio device."); | |
121 | } | |
697785d1 UH |
122 | |
123 | return SR_OK; | |
6ed4f044 DR |
124 | } |
125 | ||
6078d2c9 | 126 | static int cleanup(void) |
6ed4f044 | 127 | { |
3b412e3a | 128 | return dev_clear(); |
6ed4f044 DR |
129 | } |
130 | ||
aa0dbd68 | 131 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) |
6ed4f044 | 132 | { |
0254651d | 133 | struct dev_context *devc; |
6ed4f044 | 134 | |
035a1078 | 135 | switch (id) { |
123e1313 | 136 | case SR_CONF_SAMPLERATE: |
9a6517d1 | 137 | devc = sdi->priv; |
aa0dbd68 | 138 | *data = g_variant_new_uint64(devc->cur_samplerate); |
6ed4f044 | 139 | break; |
0254651d | 140 | default: |
bd6fbf62 | 141 | return SR_ERR_NA; |
6ed4f044 DR |
142 | } |
143 | ||
0254651d | 144 | return SR_OK; |
6ed4f044 DR |
145 | } |
146 | ||
aa0dbd68 | 147 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
6ed4f044 | 148 | { |
0254651d | 149 | struct dev_context *devc; |
6ed4f044 | 150 | |
e73ffd42 BV |
151 | if (sdi->status != SR_ST_ACTIVE) |
152 | return SR_ERR_DEV_CLOSED; | |
153 | ||
0254651d | 154 | devc = sdi->priv; |
6ed4f044 | 155 | |
035a1078 | 156 | switch (id) { |
1953564a | 157 | case SR_CONF_SAMPLERATE: |
aa0dbd68 | 158 | alsa_set_samplerate(sdi, g_variant_get_uint64(data)); |
0254651d | 159 | break; |
1953564a | 160 | case SR_CONF_LIMIT_SAMPLES: |
aa0dbd68 | 161 | devc->limit_samples = g_variant_get_uint64(data); |
0254651d | 162 | break; |
6ed4f044 | 163 | default: |
bd6fbf62 | 164 | return SR_ERR_NA; |
6ed4f044 | 165 | } |
0254651d UH |
166 | |
167 | return SR_OK; | |
6ed4f044 DR |
168 | } |
169 | ||
aa0dbd68 | 170 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc BV |
171 | { |
172 | struct dev_context *devc; | |
aa0dbd68 BV |
173 | GVariant *gvar; |
174 | GVariantBuilder gvb; | |
175 | int i; | |
a1c743fc | 176 | |
a1c743fc | 177 | switch (key) { |
9a6517d1 | 178 | case SR_CONF_DEVICE_OPTIONS: |
aa0dbd68 BV |
179 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
180 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 181 | break; |
a1c743fc BV |
182 | case SR_CONF_SAMPLERATE: |
183 | if (!sdi || !sdi->priv) | |
184 | return SR_ERR_ARG; | |
185 | devc = sdi->priv; | |
aa0dbd68 | 186 | if (!devc->samplerates) { |
a1c743fc BV |
187 | sr_err("Instance did not contain a samplerate list."); |
188 | return SR_ERR_ARG; | |
189 | } | |
aa0dbd68 BV |
190 | for (i = 0; devc->samplerates[i]; i++) |
191 | ; | |
192 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
193 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
194 | devc->samplerates, i, sizeof(uint64_t)); | |
195 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
196 | *data = g_variant_builder_end(&gvb); | |
a1c743fc BV |
197 | break; |
198 | default: | |
bd6fbf62 | 199 | return SR_ERR_NA; |
a1c743fc BV |
200 | } |
201 | ||
202 | return SR_OK; | |
203 | } | |
204 | ||
6078d2c9 | 205 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 206 | { |
0254651d UH |
207 | struct dev_context *devc; |
208 | int count, ret; | |
721ecf3d | 209 | char *endianness; |
6ed4f044 | 210 | |
e73ffd42 BV |
211 | if (sdi->status != SR_ST_ACTIVE) |
212 | return SR_ERR_DEV_CLOSED; | |
213 | ||
0254651d UH |
214 | devc = sdi->priv; |
215 | devc->cb_data = cb_data; | |
729850c9 | 216 | devc->num_samples = 0; |
6ed4f044 | 217 | |
0254651d UH |
218 | sr_dbg("Setting audio access type to RW/interleaved."); |
219 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
220 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
ebc34738 | 221 | if (ret < 0) { |
0254651d | 222 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); |
e46b8fb1 | 223 | return SR_ERR; |
6ed4f044 DR |
224 | } |
225 | ||
0254651d | 226 | /* FIXME: Hardcoded for 16bits. */ |
729850c9 | 227 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) |
721ecf3d | 228 | endianness = "little endian"; |
729850c9 | 229 | else |
721ecf3d UH |
230 | endianness = "big endian"; |
231 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
0254651d | 232 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, |
729850c9 AG |
233 | devc->hw_params, |
234 | SND_PCM_FORMAT_S16); | |
ebc34738 | 235 | if (ret < 0) { |
0254651d | 236 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); |
e46b8fb1 | 237 | return SR_ERR; |
6ed4f044 DR |
238 | } |
239 | ||
0254651d UH |
240 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", |
241 | devc->cur_samplerate); | |
0d6ff103 AG |
242 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, |
243 | (unsigned int)devc->cur_samplerate, 0); | |
ebc34738 | 244 | if (ret < 0) { |
0254651d | 245 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); |
e46b8fb1 | 246 | return SR_ERR; |
6ed4f044 DR |
247 | } |
248 | ||
9cd9f6b7 | 249 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); |
0254651d | 250 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, |
9cd9f6b7 | 251 | devc->hw_params, devc->num_probes); |
ebc34738 | 252 | if (ret < 0) { |
0254651d | 253 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); |
e46b8fb1 | 254 | return SR_ERR; |
6ed4f044 DR |
255 | } |
256 | ||
0254651d UH |
257 | sr_dbg("Setting audio parameters."); |
258 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
ebc34738 | 259 | if (ret < 0) { |
0254651d | 260 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); |
e46b8fb1 | 261 | return SR_ERR; |
6ed4f044 DR |
262 | } |
263 | ||
0254651d UH |
264 | sr_dbg("Preparing audio interface for use."); |
265 | ret = snd_pcm_prepare(devc->capture_handle); | |
ebc34738 | 266 | if (ret < 0) { |
0254651d | 267 | sr_err("Can't prepare audio interface for use: %s.", |
ebc34738 | 268 | snd_strerror(ret)); |
e46b8fb1 | 269 | return SR_ERR; |
6ed4f044 DR |
270 | } |
271 | ||
0254651d | 272 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); |
6ed4f044 | 273 | if (count < 1) { |
472bbb46 | 274 | sr_err("Unable to obtain poll descriptors count."); |
e46b8fb1 | 275 | return SR_ERR; |
6ed4f044 DR |
276 | } |
277 | ||
0254651d UH |
278 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { |
279 | sr_err("Failed to malloc ufds."); | |
e46b8fb1 | 280 | return SR_ERR_MALLOC; |
92b3101c | 281 | } |
6ed4f044 | 282 | |
3d6de074 | 283 | sr_spew("Getting %d poll descriptors.", count); |
0254651d | 284 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); |
ebc34738 | 285 | if (ret < 0) { |
0254651d | 286 | sr_err("Unable to obtain poll descriptors: %s.", |
ebc34738 | 287 | snd_strerror(ret)); |
0254651d | 288 | g_free(devc->ufds); |
e46b8fb1 | 289 | return SR_ERR; |
6ed4f044 DR |
290 | } |
291 | ||
0254651d | 292 | /* Send header packet to the session bus. */ |
29a27196 | 293 | std_session_send_df_header(cb_data, LOG_PREFIX); |
0254651d | 294 | |
0254651d UH |
295 | /* Poll every 10ms, or whenever some data comes in. */ |
296 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
9cd9f6b7 | 297 | alsa_receive_data, (void *)sdi); |
0254651d UH |
298 | |
299 | // g_free(devc->ufds); /* FIXME */ | |
6ed4f044 | 300 | |
e46b8fb1 | 301 | return SR_OK; |
6ed4f044 DR |
302 | } |
303 | ||
6078d2c9 | 304 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 305 | { |
0254651d UH |
306 | struct sr_datafeed_packet packet; |
307 | struct dev_context *devc; | |
308 | ||
309 | devc = sdi->priv; | |
310 | devc->cb_data = cb_data; | |
311 | ||
312 | sr_source_remove(devc->ufds[0].fd); | |
313 | ||
314 | /* Send end packet to the session bus. */ | |
315 | sr_dbg("Sending SR_DF_END packet."); | |
316 | packet.type = SR_DF_END; | |
317 | sr_session_send(cb_data, &packet); | |
3010f21c UH |
318 | |
319 | return SR_OK; | |
6ed4f044 DR |
320 | } |
321 | ||
c09f0b57 | 322 | SR_PRIV struct sr_dev_driver alsa_driver_info = { |
e519ba86 UH |
323 | .name = "alsa", |
324 | .longname = "ALSA driver", | |
325 | .api_version = 1, | |
6078d2c9 UH |
326 | .init = init, |
327 | .cleanup = cleanup, | |
328 | .scan = scan, | |
329 | .dev_list = dev_list, | |
3b412e3a | 330 | .dev_clear = dev_clear, |
035a1078 BV |
331 | .config_get = config_get, |
332 | .config_set = config_set, | |
a1c743fc | 333 | .config_list = config_list, |
6078d2c9 UH |
334 | .dev_open = dev_open, |
335 | .dev_close = dev_close, | |
336 | .dev_acquisition_start = dev_acquisition_start, | |
337 | .dev_acquisition_stop = dev_acquisition_stop, | |
0254651d | 338 | .priv = NULL, |
6ed4f044 | 339 | }; |