]>
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 | ||
8f996b89 ML |
131 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, |
132 | const struct sr_probe_group *probe_group) | |
6ed4f044 | 133 | { |
0254651d | 134 | struct dev_context *devc; |
6ed4f044 | 135 | |
8f996b89 ML |
136 | (void)probe_group; |
137 | ||
035a1078 | 138 | switch (id) { |
123e1313 | 139 | case SR_CONF_SAMPLERATE: |
9a6517d1 | 140 | devc = sdi->priv; |
aa0dbd68 | 141 | *data = g_variant_new_uint64(devc->cur_samplerate); |
6ed4f044 | 142 | break; |
0254651d | 143 | default: |
bd6fbf62 | 144 | return SR_ERR_NA; |
6ed4f044 DR |
145 | } |
146 | ||
0254651d | 147 | return SR_OK; |
6ed4f044 DR |
148 | } |
149 | ||
8f996b89 ML |
150 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
151 | const struct sr_probe_group *probe_group) | |
6ed4f044 | 152 | { |
0254651d | 153 | struct dev_context *devc; |
6ed4f044 | 154 | |
8f996b89 ML |
155 | (void)probe_group; |
156 | ||
e73ffd42 BV |
157 | if (sdi->status != SR_ST_ACTIVE) |
158 | return SR_ERR_DEV_CLOSED; | |
159 | ||
0254651d | 160 | devc = sdi->priv; |
6ed4f044 | 161 | |
035a1078 | 162 | switch (id) { |
1953564a | 163 | case SR_CONF_SAMPLERATE: |
aa0dbd68 | 164 | alsa_set_samplerate(sdi, g_variant_get_uint64(data)); |
0254651d | 165 | break; |
1953564a | 166 | case SR_CONF_LIMIT_SAMPLES: |
aa0dbd68 | 167 | devc->limit_samples = g_variant_get_uint64(data); |
0254651d | 168 | break; |
6ed4f044 | 169 | default: |
bd6fbf62 | 170 | return SR_ERR_NA; |
6ed4f044 | 171 | } |
0254651d UH |
172 | |
173 | return SR_OK; | |
6ed4f044 DR |
174 | } |
175 | ||
8f996b89 ML |
176 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
177 | const struct sr_probe_group *probe_group) | |
a1c743fc BV |
178 | { |
179 | struct dev_context *devc; | |
aa0dbd68 BV |
180 | GVariant *gvar; |
181 | GVariantBuilder gvb; | |
182 | int i; | |
a1c743fc | 183 | |
8f996b89 ML |
184 | (void)probe_group; |
185 | ||
a1c743fc | 186 | switch (key) { |
9a6517d1 | 187 | case SR_CONF_DEVICE_OPTIONS: |
aa0dbd68 BV |
188 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
189 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 190 | break; |
a1c743fc BV |
191 | case SR_CONF_SAMPLERATE: |
192 | if (!sdi || !sdi->priv) | |
193 | return SR_ERR_ARG; | |
194 | devc = sdi->priv; | |
aa0dbd68 | 195 | if (!devc->samplerates) { |
a1c743fc BV |
196 | sr_err("Instance did not contain a samplerate list."); |
197 | return SR_ERR_ARG; | |
198 | } | |
aa0dbd68 BV |
199 | for (i = 0; devc->samplerates[i]; i++) |
200 | ; | |
201 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
202 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
203 | devc->samplerates, i, sizeof(uint64_t)); | |
204 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
205 | *data = g_variant_builder_end(&gvb); | |
a1c743fc BV |
206 | break; |
207 | default: | |
bd6fbf62 | 208 | return SR_ERR_NA; |
a1c743fc BV |
209 | } |
210 | ||
211 | return SR_OK; | |
212 | } | |
213 | ||
6078d2c9 | 214 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 215 | { |
0254651d UH |
216 | struct dev_context *devc; |
217 | int count, ret; | |
721ecf3d | 218 | char *endianness; |
6ed4f044 | 219 | |
e73ffd42 BV |
220 | if (sdi->status != SR_ST_ACTIVE) |
221 | return SR_ERR_DEV_CLOSED; | |
222 | ||
0254651d UH |
223 | devc = sdi->priv; |
224 | devc->cb_data = cb_data; | |
729850c9 | 225 | devc->num_samples = 0; |
6ed4f044 | 226 | |
0254651d UH |
227 | sr_dbg("Setting audio access type to RW/interleaved."); |
228 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
229 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
ebc34738 | 230 | if (ret < 0) { |
0254651d | 231 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); |
e46b8fb1 | 232 | return SR_ERR; |
6ed4f044 DR |
233 | } |
234 | ||
0254651d | 235 | /* FIXME: Hardcoded for 16bits. */ |
729850c9 | 236 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) |
721ecf3d | 237 | endianness = "little endian"; |
729850c9 | 238 | else |
721ecf3d UH |
239 | endianness = "big endian"; |
240 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
0254651d | 241 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, |
729850c9 AG |
242 | devc->hw_params, |
243 | SND_PCM_FORMAT_S16); | |
ebc34738 | 244 | if (ret < 0) { |
0254651d | 245 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); |
e46b8fb1 | 246 | return SR_ERR; |
6ed4f044 DR |
247 | } |
248 | ||
0254651d UH |
249 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", |
250 | devc->cur_samplerate); | |
0d6ff103 AG |
251 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, |
252 | (unsigned int)devc->cur_samplerate, 0); | |
ebc34738 | 253 | if (ret < 0) { |
0254651d | 254 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); |
e46b8fb1 | 255 | return SR_ERR; |
6ed4f044 DR |
256 | } |
257 | ||
9cd9f6b7 | 258 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); |
0254651d | 259 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, |
9cd9f6b7 | 260 | devc->hw_params, devc->num_probes); |
ebc34738 | 261 | if (ret < 0) { |
0254651d | 262 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); |
e46b8fb1 | 263 | return SR_ERR; |
6ed4f044 DR |
264 | } |
265 | ||
0254651d UH |
266 | sr_dbg("Setting audio parameters."); |
267 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
ebc34738 | 268 | if (ret < 0) { |
0254651d | 269 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); |
e46b8fb1 | 270 | return SR_ERR; |
6ed4f044 DR |
271 | } |
272 | ||
0254651d UH |
273 | sr_dbg("Preparing audio interface for use."); |
274 | ret = snd_pcm_prepare(devc->capture_handle); | |
ebc34738 | 275 | if (ret < 0) { |
0254651d | 276 | sr_err("Can't prepare audio interface for use: %s.", |
ebc34738 | 277 | snd_strerror(ret)); |
e46b8fb1 | 278 | return SR_ERR; |
6ed4f044 DR |
279 | } |
280 | ||
0254651d | 281 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); |
6ed4f044 | 282 | if (count < 1) { |
472bbb46 | 283 | sr_err("Unable to obtain poll descriptors count."); |
e46b8fb1 | 284 | return SR_ERR; |
6ed4f044 DR |
285 | } |
286 | ||
0254651d UH |
287 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { |
288 | sr_err("Failed to malloc ufds."); | |
e46b8fb1 | 289 | return SR_ERR_MALLOC; |
92b3101c | 290 | } |
6ed4f044 | 291 | |
3d6de074 | 292 | sr_spew("Getting %d poll descriptors.", count); |
0254651d | 293 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); |
ebc34738 | 294 | if (ret < 0) { |
0254651d | 295 | sr_err("Unable to obtain poll descriptors: %s.", |
ebc34738 | 296 | snd_strerror(ret)); |
0254651d | 297 | g_free(devc->ufds); |
e46b8fb1 | 298 | return SR_ERR; |
6ed4f044 DR |
299 | } |
300 | ||
0254651d | 301 | /* Send header packet to the session bus. */ |
29a27196 | 302 | std_session_send_df_header(cb_data, LOG_PREFIX); |
0254651d | 303 | |
0254651d UH |
304 | /* Poll every 10ms, or whenever some data comes in. */ |
305 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
9cd9f6b7 | 306 | alsa_receive_data, (void *)sdi); |
0254651d UH |
307 | |
308 | // g_free(devc->ufds); /* FIXME */ | |
6ed4f044 | 309 | |
e46b8fb1 | 310 | return SR_OK; |
6ed4f044 DR |
311 | } |
312 | ||
6078d2c9 | 313 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 314 | { |
0254651d UH |
315 | struct sr_datafeed_packet packet; |
316 | struct dev_context *devc; | |
317 | ||
318 | devc = sdi->priv; | |
319 | devc->cb_data = cb_data; | |
320 | ||
321 | sr_source_remove(devc->ufds[0].fd); | |
322 | ||
323 | /* Send end packet to the session bus. */ | |
324 | sr_dbg("Sending SR_DF_END packet."); | |
325 | packet.type = SR_DF_END; | |
326 | sr_session_send(cb_data, &packet); | |
3010f21c UH |
327 | |
328 | return SR_OK; | |
6ed4f044 DR |
329 | } |
330 | ||
c09f0b57 | 331 | SR_PRIV struct sr_dev_driver alsa_driver_info = { |
e519ba86 UH |
332 | .name = "alsa", |
333 | .longname = "ALSA driver", | |
334 | .api_version = 1, | |
6078d2c9 UH |
335 | .init = init, |
336 | .cleanup = cleanup, | |
337 | .scan = scan, | |
338 | .dev_list = dev_list, | |
3b412e3a | 339 | .dev_clear = dev_clear, |
035a1078 BV |
340 | .config_get = config_get, |
341 | .config_set = config_set, | |
a1c743fc | 342 | .config_list = config_list, |
6078d2c9 UH |
343 | .dev_open = dev_open, |
344 | .dev_close = dev_close, | |
345 | .dev_acquisition_start = dev_acquisition_start, | |
346 | .dev_acquisition_stop = dev_acquisition_stop, | |
0254651d | 347 | .priv = NULL, |
6ed4f044 | 348 | }; |