]>
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 | { |
0254651d UH |
65 | struct drv_context *drvc; |
66 | ||
67 | drvc = di->priv; | |
68 | ||
69 | return drvc->instances; | |
70 | } | |
71 | ||
72 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
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 | ||
e46b8fb1 | 100 | return SR_OK; |
6ed4f044 DR |
101 | } |
102 | ||
0254651d | 103 | static int hw_dev_close(struct sr_dev_inst *sdi) |
6ed4f044 | 104 | { |
0254651d UH |
105 | int ret; |
106 | struct dev_context *devc; | |
6ed4f044 | 107 | |
0254651d UH |
108 | devc = sdi->priv; |
109 | ||
110 | sr_dbg("Closing device."); | |
6ed4f044 | 111 | |
0254651d UH |
112 | if (devc->capture_handle) { |
113 | sr_dbg("Closing PCM device."); | |
114 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) { | |
115 | sr_err("Failed to close device: %s.", | |
116 | snd_strerror(ret)); | |
6944b2d0 | 117 | devc->capture_handle = NULL; |
0254651d UH |
118 | } |
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 | ||
57ab7d9f | 126 | static int hw_cleanup(void) |
6ed4f044 | 127 | { |
0254651d | 128 | clear_instances(); |
57ab7d9f UH |
129 | |
130 | return SR_OK; | |
6ed4f044 DR |
131 | } |
132 | ||
035a1078 | 133 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) |
6ed4f044 | 134 | { |
0254651d | 135 | struct dev_context *devc; |
6ed4f044 | 136 | |
035a1078 | 137 | switch (id) { |
123e1313 | 138 | case SR_CONF_SAMPLERATE: |
9a6517d1 | 139 | devc = sdi->priv; |
0254651d | 140 | *data = &devc->cur_samplerate; |
6ed4f044 | 141 | break; |
0254651d | 142 | default: |
0254651d | 143 | return SR_ERR_ARG; |
6ed4f044 DR |
144 | } |
145 | ||
0254651d | 146 | return SR_OK; |
6ed4f044 DR |
147 | } |
148 | ||
035a1078 | 149 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) |
6ed4f044 | 150 | { |
0254651d | 151 | struct dev_context *devc; |
6ed4f044 | 152 | |
0254651d | 153 | devc = sdi->priv; |
6ed4f044 | 154 | |
035a1078 | 155 | switch (id) { |
1953564a | 156 | case SR_CONF_SAMPLERATE: |
65faa197 | 157 | alsa_set_samplerate(sdi, *(const uint64_t *)value); |
0254651d | 158 | break; |
1953564a | 159 | case SR_CONF_LIMIT_SAMPLES: |
0254651d UH |
160 | devc->limit_samples = *(const uint64_t *)value; |
161 | break; | |
6ed4f044 | 162 | default: |
035a1078 | 163 | sr_err("Unknown capability: %d.", id); |
e46b8fb1 | 164 | return SR_ERR; |
6ed4f044 | 165 | } |
0254651d UH |
166 | |
167 | return SR_OK; | |
6ed4f044 DR |
168 | } |
169 | ||
a1c743fc BV |
170 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) |
171 | { | |
172 | struct dev_context *devc; | |
173 | ||
174 | (void)sdi; | |
175 | ||
176 | switch (key) { | |
9a6517d1 BV |
177 | case SR_CONF_DEVICE_OPTIONS: |
178 | *data = hwcaps; | |
179 | break; | |
a1c743fc BV |
180 | case SR_CONF_SAMPLERATE: |
181 | if (!sdi || !sdi->priv) | |
182 | return SR_ERR_ARG; | |
183 | devc = sdi->priv; | |
184 | if (!devc->supp_rates.list) { | |
185 | sr_err("Instance did not contain a samplerate list."); | |
186 | return SR_ERR_ARG; | |
187 | } | |
188 | *data = &devc->supp_rates; | |
189 | break; | |
190 | default: | |
191 | return SR_ERR_ARG; | |
192 | } | |
193 | ||
194 | return SR_OK; | |
195 | } | |
196 | ||
0254651d UH |
197 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
198 | void *cb_data) | |
6ed4f044 | 199 | { |
b9c735a2 UH |
200 | struct sr_datafeed_packet packet; |
201 | struct sr_datafeed_header header; | |
0254651d UH |
202 | struct dev_context *devc; |
203 | int count, ret; | |
721ecf3d | 204 | char *endianness; |
6ed4f044 | 205 | |
0254651d UH |
206 | devc = sdi->priv; |
207 | devc->cb_data = cb_data; | |
729850c9 | 208 | devc->num_samples = 0; |
6ed4f044 | 209 | |
0254651d UH |
210 | sr_dbg("Setting audio access type to RW/interleaved."); |
211 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
212 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
ebc34738 | 213 | if (ret < 0) { |
0254651d | 214 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); |
e46b8fb1 | 215 | return SR_ERR; |
6ed4f044 DR |
216 | } |
217 | ||
0254651d | 218 | /* FIXME: Hardcoded for 16bits. */ |
729850c9 | 219 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) |
721ecf3d | 220 | endianness = "little endian"; |
729850c9 | 221 | else |
721ecf3d UH |
222 | endianness = "big endian"; |
223 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
0254651d | 224 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, |
729850c9 AG |
225 | devc->hw_params, |
226 | SND_PCM_FORMAT_S16); | |
ebc34738 | 227 | if (ret < 0) { |
0254651d | 228 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); |
e46b8fb1 | 229 | return SR_ERR; |
6ed4f044 DR |
230 | } |
231 | ||
0254651d UH |
232 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", |
233 | devc->cur_samplerate); | |
0d6ff103 AG |
234 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, |
235 | (unsigned int)devc->cur_samplerate, 0); | |
ebc34738 | 236 | if (ret < 0) { |
0254651d | 237 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); |
e46b8fb1 | 238 | return SR_ERR; |
6ed4f044 DR |
239 | } |
240 | ||
9cd9f6b7 | 241 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); |
0254651d | 242 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, |
9cd9f6b7 | 243 | devc->hw_params, devc->num_probes); |
ebc34738 | 244 | if (ret < 0) { |
0254651d | 245 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); |
e46b8fb1 | 246 | return SR_ERR; |
6ed4f044 DR |
247 | } |
248 | ||
0254651d UH |
249 | sr_dbg("Setting audio parameters."); |
250 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
ebc34738 | 251 | if (ret < 0) { |
0254651d | 252 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); |
e46b8fb1 | 253 | return SR_ERR; |
6ed4f044 DR |
254 | } |
255 | ||
0254651d UH |
256 | sr_dbg("Preparing audio interface for use."); |
257 | ret = snd_pcm_prepare(devc->capture_handle); | |
ebc34738 | 258 | if (ret < 0) { |
0254651d | 259 | sr_err("Can't prepare audio interface for use: %s.", |
ebc34738 | 260 | snd_strerror(ret)); |
e46b8fb1 | 261 | return SR_ERR; |
6ed4f044 DR |
262 | } |
263 | ||
0254651d | 264 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); |
6ed4f044 | 265 | if (count < 1) { |
472bbb46 | 266 | sr_err("Unable to obtain poll descriptors count."); |
e46b8fb1 | 267 | return SR_ERR; |
6ed4f044 DR |
268 | } |
269 | ||
0254651d UH |
270 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { |
271 | sr_err("Failed to malloc ufds."); | |
e46b8fb1 | 272 | return SR_ERR_MALLOC; |
92b3101c | 273 | } |
6ed4f044 | 274 | |
3d6de074 | 275 | sr_spew("Getting %d poll descriptors.", count); |
0254651d | 276 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); |
ebc34738 | 277 | if (ret < 0) { |
0254651d | 278 | sr_err("Unable to obtain poll descriptors: %s.", |
ebc34738 | 279 | snd_strerror(ret)); |
0254651d | 280 | g_free(devc->ufds); |
e46b8fb1 | 281 | return SR_ERR; |
6ed4f044 DR |
282 | } |
283 | ||
0254651d UH |
284 | /* Send header packet to the session bus. */ |
285 | sr_dbg("Sending SR_DF_HEADER packet."); | |
5a2326a7 | 286 | packet.type = SR_DF_HEADER; |
0254651d | 287 | packet.payload = (uint8_t *)&header; |
6ed4f044 DR |
288 | header.feed_version = 1; |
289 | gettimeofday(&header.starttime, NULL); | |
0254651d UH |
290 | sr_session_send(devc->cb_data, &packet); |
291 | ||
0254651d UH |
292 | /* Poll every 10ms, or whenever some data comes in. */ |
293 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
9cd9f6b7 | 294 | alsa_receive_data, (void *)sdi); |
0254651d UH |
295 | |
296 | // g_free(devc->ufds); /* FIXME */ | |
6ed4f044 | 297 | |
e46b8fb1 | 298 | return SR_OK; |
6ed4f044 DR |
299 | } |
300 | ||
0254651d | 301 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 302 | { |
0254651d UH |
303 | struct sr_datafeed_packet packet; |
304 | struct dev_context *devc; | |
305 | ||
306 | devc = sdi->priv; | |
307 | devc->cb_data = cb_data; | |
308 | ||
309 | sr_source_remove(devc->ufds[0].fd); | |
310 | ||
311 | /* Send end packet to the session bus. */ | |
312 | sr_dbg("Sending SR_DF_END packet."); | |
313 | packet.type = SR_DF_END; | |
314 | sr_session_send(cb_data, &packet); | |
3010f21c UH |
315 | |
316 | return SR_OK; | |
6ed4f044 DR |
317 | } |
318 | ||
c09f0b57 | 319 | SR_PRIV struct sr_dev_driver alsa_driver_info = { |
e519ba86 UH |
320 | .name = "alsa", |
321 | .longname = "ALSA driver", | |
322 | .api_version = 1, | |
323 | .init = hw_init, | |
324 | .cleanup = hw_cleanup, | |
0254651d UH |
325 | .scan = hw_scan, |
326 | .dev_list = hw_dev_list, | |
327 | .dev_clear = clear_instances, | |
035a1078 BV |
328 | .config_get = config_get, |
329 | .config_set = config_set, | |
a1c743fc | 330 | .config_list = config_list, |
e7eb703f UH |
331 | .dev_open = hw_dev_open, |
332 | .dev_close = hw_dev_close, | |
69040b7c UH |
333 | .dev_acquisition_start = hw_dev_acquisition_start, |
334 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
0254651d | 335 | .priv = NULL, |
6ed4f044 | 336 | }; |