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