]>
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 UH |
45 | snd_pcm_hw_params_free(devc->hw_params); |
46 | g_free((void *)devc->samplerates); | |
47 | } | |
0254651d | 48 | |
3b412e3a | 49 | static int dev_clear(void) |
c43cf4d4 UH |
50 | { |
51 | return std_dev_clear(di, clear_helper); | |
0254651d UH |
52 | } |
53 | ||
6078d2c9 | 54 | static int init(struct sr_context *sr_ctx) |
6ed4f044 | 55 | { |
f6beaac5 | 56 | return std_init(sr_ctx, di, LOG_PREFIX); |
0254651d UH |
57 | } |
58 | ||
6078d2c9 | 59 | static GSList *scan(GSList *options) |
0254651d | 60 | { |
6944b2d0 | 61 | return alsa_scan(options, di); |
6ed4f044 DR |
62 | } |
63 | ||
6078d2c9 | 64 | static GSList *dev_list(void) |
6ed4f044 | 65 | { |
0e94d524 | 66 | return ((struct drv_context *)(di->priv))->instances; |
0254651d UH |
67 | } |
68 | ||
6078d2c9 | 69 | static int dev_open(struct sr_dev_inst *sdi) |
0254651d UH |
70 | { |
71 | struct dev_context *devc; | |
ebc34738 | 72 | int ret; |
6ed4f044 | 73 | |
0254651d | 74 | devc = sdi->priv; |
6ed4f044 | 75 | |
6944b2d0 AG |
76 | if (!(devc->hwdev)) { |
77 | sr_err("devc->hwdev was NULL."); | |
78 | return SR_ERR_BUG; | |
79 | } | |
80 | ||
81 | sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev); | |
82 | ret = snd_pcm_open(&devc->capture_handle, devc->hwdev, | |
ea9cfed7 | 83 | SND_PCM_STREAM_CAPTURE, 0); |
ebc34738 | 84 | if (ret < 0) { |
0254651d | 85 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); |
e46b8fb1 | 86 | return SR_ERR; |
6ed4f044 DR |
87 | } |
88 | ||
0254651d UH |
89 | sr_dbg("Initializing hardware parameter structure."); |
90 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
ebc34738 | 91 | if (ret < 0) { |
0254651d | 92 | sr_err("Can't initialize hardware parameter structure: %s.", |
472bbb46 | 93 | snd_strerror(ret)); |
e46b8fb1 | 94 | return SR_ERR; |
6ed4f044 DR |
95 | } |
96 | ||
e73ffd42 BV |
97 | sdi->status = SR_ST_ACTIVE; |
98 | ||
e46b8fb1 | 99 | return SR_OK; |
6ed4f044 DR |
100 | } |
101 | ||
6078d2c9 | 102 | static int dev_close(struct sr_dev_inst *sdi) |
6ed4f044 | 103 | { |
0254651d UH |
104 | int ret; |
105 | struct dev_context *devc; | |
6ed4f044 | 106 | |
0254651d UH |
107 | devc = sdi->priv; |
108 | ||
0254651d UH |
109 | if (devc->capture_handle) { |
110 | sr_dbg("Closing PCM device."); | |
111 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) { | |
112 | sr_err("Failed to close device: %s.", | |
113 | snd_strerror(ret)); | |
6944b2d0 | 114 | devc->capture_handle = NULL; |
e73ffd42 | 115 | sdi->status = SR_ST_INACTIVE; |
0254651d UH |
116 | } |
117 | } else { | |
118 | sr_dbg("No capture handle, no need to close audio device."); | |
119 | } | |
697785d1 UH |
120 | |
121 | return SR_OK; | |
6ed4f044 DR |
122 | } |
123 | ||
6078d2c9 | 124 | static int cleanup(void) |
6ed4f044 | 125 | { |
3b412e3a | 126 | return dev_clear(); |
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 | ||
6078d2c9 | 203 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 204 | { |
0254651d UH |
205 | struct dev_context *devc; |
206 | int count, ret; | |
721ecf3d | 207 | char *endianness; |
6ed4f044 | 208 | |
e73ffd42 BV |
209 | if (sdi->status != SR_ST_ACTIVE) |
210 | return SR_ERR_DEV_CLOSED; | |
211 | ||
0254651d UH |
212 | devc = sdi->priv; |
213 | devc->cb_data = cb_data; | |
729850c9 | 214 | devc->num_samples = 0; |
6ed4f044 | 215 | |
0254651d UH |
216 | sr_dbg("Setting audio access type to RW/interleaved."); |
217 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
218 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
ebc34738 | 219 | if (ret < 0) { |
0254651d | 220 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); |
e46b8fb1 | 221 | return SR_ERR; |
6ed4f044 DR |
222 | } |
223 | ||
0254651d | 224 | /* FIXME: Hardcoded for 16bits. */ |
729850c9 | 225 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) |
721ecf3d | 226 | endianness = "little endian"; |
729850c9 | 227 | else |
721ecf3d UH |
228 | endianness = "big endian"; |
229 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
0254651d | 230 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, |
729850c9 AG |
231 | devc->hw_params, |
232 | SND_PCM_FORMAT_S16); | |
ebc34738 | 233 | if (ret < 0) { |
0254651d | 234 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); |
e46b8fb1 | 235 | return SR_ERR; |
6ed4f044 DR |
236 | } |
237 | ||
0254651d UH |
238 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", |
239 | devc->cur_samplerate); | |
0d6ff103 AG |
240 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, |
241 | (unsigned int)devc->cur_samplerate, 0); | |
ebc34738 | 242 | if (ret < 0) { |
0254651d | 243 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); |
e46b8fb1 | 244 | return SR_ERR; |
6ed4f044 DR |
245 | } |
246 | ||
9cd9f6b7 | 247 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); |
0254651d | 248 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, |
9cd9f6b7 | 249 | devc->hw_params, devc->num_probes); |
ebc34738 | 250 | if (ret < 0) { |
0254651d | 251 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); |
e46b8fb1 | 252 | return SR_ERR; |
6ed4f044 DR |
253 | } |
254 | ||
0254651d UH |
255 | sr_dbg("Setting audio parameters."); |
256 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
ebc34738 | 257 | if (ret < 0) { |
0254651d | 258 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); |
e46b8fb1 | 259 | return SR_ERR; |
6ed4f044 DR |
260 | } |
261 | ||
0254651d UH |
262 | sr_dbg("Preparing audio interface for use."); |
263 | ret = snd_pcm_prepare(devc->capture_handle); | |
ebc34738 | 264 | if (ret < 0) { |
0254651d | 265 | sr_err("Can't prepare audio interface for use: %s.", |
ebc34738 | 266 | snd_strerror(ret)); |
e46b8fb1 | 267 | return SR_ERR; |
6ed4f044 DR |
268 | } |
269 | ||
0254651d | 270 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); |
6ed4f044 | 271 | if (count < 1) { |
472bbb46 | 272 | sr_err("Unable to obtain poll descriptors count."); |
e46b8fb1 | 273 | return SR_ERR; |
6ed4f044 DR |
274 | } |
275 | ||
0254651d UH |
276 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { |
277 | sr_err("Failed to malloc ufds."); | |
e46b8fb1 | 278 | return SR_ERR_MALLOC; |
92b3101c | 279 | } |
6ed4f044 | 280 | |
3d6de074 | 281 | sr_spew("Getting %d poll descriptors.", count); |
0254651d | 282 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); |
ebc34738 | 283 | if (ret < 0) { |
0254651d | 284 | sr_err("Unable to obtain poll descriptors: %s.", |
ebc34738 | 285 | snd_strerror(ret)); |
0254651d | 286 | g_free(devc->ufds); |
e46b8fb1 | 287 | return SR_ERR; |
6ed4f044 DR |
288 | } |
289 | ||
0254651d | 290 | /* Send header packet to the session bus. */ |
29a27196 | 291 | std_session_send_df_header(cb_data, LOG_PREFIX); |
0254651d | 292 | |
0254651d UH |
293 | /* Poll every 10ms, or whenever some data comes in. */ |
294 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
9cd9f6b7 | 295 | alsa_receive_data, (void *)sdi); |
0254651d UH |
296 | |
297 | // g_free(devc->ufds); /* FIXME */ | |
6ed4f044 | 298 | |
e46b8fb1 | 299 | return SR_OK; |
6ed4f044 DR |
300 | } |
301 | ||
6078d2c9 | 302 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 303 | { |
0254651d UH |
304 | struct sr_datafeed_packet packet; |
305 | struct dev_context *devc; | |
306 | ||
307 | devc = sdi->priv; | |
308 | devc->cb_data = cb_data; | |
309 | ||
310 | sr_source_remove(devc->ufds[0].fd); | |
311 | ||
312 | /* Send end packet to the session bus. */ | |
313 | sr_dbg("Sending SR_DF_END packet."); | |
314 | packet.type = SR_DF_END; | |
315 | sr_session_send(cb_data, &packet); | |
3010f21c UH |
316 | |
317 | return SR_OK; | |
6ed4f044 DR |
318 | } |
319 | ||
c09f0b57 | 320 | SR_PRIV struct sr_dev_driver alsa_driver_info = { |
e519ba86 UH |
321 | .name = "alsa", |
322 | .longname = "ALSA driver", | |
323 | .api_version = 1, | |
6078d2c9 UH |
324 | .init = init, |
325 | .cleanup = cleanup, | |
326 | .scan = scan, | |
327 | .dev_list = dev_list, | |
3b412e3a | 328 | .dev_clear = dev_clear, |
035a1078 BV |
329 | .config_get = config_get, |
330 | .config_set = config_set, | |
a1c743fc | 331 | .config_list = config_list, |
6078d2c9 UH |
332 | .dev_open = dev_open, |
333 | .dev_close = dev_close, | |
334 | .dev_acquisition_start = dev_acquisition_start, | |
335 | .dev_acquisition_stop = dev_acquisition_stop, | |
0254651d | 336 | .priv = NULL, |
6ed4f044 | 337 | }; |