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