]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
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 | ||
23 | #include <stdlib.h> | |
24 | #include <unistd.h> | |
25 | #include <string.h> | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | #include "protocol.h" | |
29 | ||
30 | static const int32_t hwcaps[] = { | |
31 | SR_CONF_SAMPLERATE, | |
32 | SR_CONF_LIMIT_SAMPLES, | |
33 | SR_CONF_CONTINUOUS, | |
34 | }; | |
35 | ||
36 | SR_PRIV struct sr_dev_driver alsa_driver_info; | |
37 | static struct sr_dev_driver *di = &alsa_driver_info; | |
38 | ||
39 | static int clear_instances(void) | |
40 | { | |
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; | |
48 | ||
49 | return SR_OK; | |
50 | } | |
51 | ||
52 | static int hw_init(struct sr_context *sr_ctx) | |
53 | { | |
54 | return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); | |
55 | } | |
56 | ||
57 | static GSList *hw_scan(GSList *options) | |
58 | { | |
59 | return alsa_scan(options, di); | |
60 | } | |
61 | ||
62 | static GSList *hw_dev_list(void) | |
63 | { | |
64 | return ((struct drv_context *)(di->priv))->instances; | |
65 | } | |
66 | ||
67 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
68 | { | |
69 | struct dev_context *devc; | |
70 | int ret; | |
71 | ||
72 | devc = sdi->priv; | |
73 | ||
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, | |
81 | SND_PCM_STREAM_CAPTURE, 0); | |
82 | if (ret < 0) { | |
83 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); | |
84 | return SR_ERR; | |
85 | } | |
86 | ||
87 | sr_dbg("Initializing hardware parameter structure."); | |
88 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
89 | if (ret < 0) { | |
90 | sr_err("Can't initialize hardware parameter structure: %s.", | |
91 | snd_strerror(ret)); | |
92 | return SR_ERR; | |
93 | } | |
94 | ||
95 | return SR_OK; | |
96 | } | |
97 | ||
98 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
99 | { | |
100 | int ret; | |
101 | struct dev_context *devc; | |
102 | ||
103 | devc = sdi->priv; | |
104 | ||
105 | if (devc->capture_handle) { | |
106 | sr_dbg("Closing PCM device."); | |
107 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) { | |
108 | sr_err("Failed to close device: %s.", | |
109 | snd_strerror(ret)); | |
110 | devc->capture_handle = NULL; | |
111 | } | |
112 | } else { | |
113 | sr_dbg("No capture handle, no need to close audio device."); | |
114 | } | |
115 | ||
116 | return SR_OK; | |
117 | } | |
118 | ||
119 | static int hw_cleanup(void) | |
120 | { | |
121 | clear_instances(); | |
122 | ||
123 | return SR_OK; | |
124 | } | |
125 | ||
126 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) | |
127 | { | |
128 | struct dev_context *devc; | |
129 | ||
130 | switch (id) { | |
131 | case SR_CONF_SAMPLERATE: | |
132 | devc = sdi->priv; | |
133 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
134 | break; | |
135 | default: | |
136 | return SR_ERR_ARG; | |
137 | } | |
138 | ||
139 | return SR_OK; | |
140 | } | |
141 | ||
142 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) | |
143 | { | |
144 | struct dev_context *devc; | |
145 | ||
146 | devc = sdi->priv; | |
147 | ||
148 | switch (id) { | |
149 | case SR_CONF_SAMPLERATE: | |
150 | alsa_set_samplerate(sdi, g_variant_get_uint64(data)); | |
151 | break; | |
152 | case SR_CONF_LIMIT_SAMPLES: | |
153 | devc->limit_samples = g_variant_get_uint64(data); | |
154 | break; | |
155 | default: | |
156 | sr_err("Unknown capability: %d.", id); | |
157 | return SR_ERR; | |
158 | } | |
159 | ||
160 | return SR_OK; | |
161 | } | |
162 | ||
163 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) | |
164 | { | |
165 | struct dev_context *devc; | |
166 | GVariant *gvar; | |
167 | GVariantBuilder gvb; | |
168 | int i; | |
169 | ||
170 | (void)sdi; | |
171 | ||
172 | switch (key) { | |
173 | case SR_CONF_DEVICE_OPTIONS: | |
174 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
175 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
176 | break; | |
177 | case SR_CONF_SAMPLERATE: | |
178 | if (!sdi || !sdi->priv) | |
179 | return SR_ERR_ARG; | |
180 | devc = sdi->priv; | |
181 | if (!devc->samplerates) { | |
182 | sr_err("Instance did not contain a samplerate list."); | |
183 | return SR_ERR_ARG; | |
184 | } | |
185 | for (i = 0; devc->samplerates[i]; i++) | |
186 | ; | |
187 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
188 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
189 | devc->samplerates, i, sizeof(uint64_t)); | |
190 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
191 | *data = g_variant_builder_end(&gvb); | |
192 | break; | |
193 | default: | |
194 | return SR_ERR_ARG; | |
195 | } | |
196 | ||
197 | return SR_OK; | |
198 | } | |
199 | ||
200 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
201 | void *cb_data) | |
202 | { | |
203 | struct dev_context *devc; | |
204 | int count, ret; | |
205 | char *endianness; | |
206 | ||
207 | devc = sdi->priv; | |
208 | devc->cb_data = cb_data; | |
209 | devc->num_samples = 0; | |
210 | ||
211 | sr_dbg("Setting audio access type to RW/interleaved."); | |
212 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
213 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
214 | if (ret < 0) { | |
215 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); | |
216 | return SR_ERR; | |
217 | } | |
218 | ||
219 | /* FIXME: Hardcoded for 16bits. */ | |
220 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) | |
221 | endianness = "little endian"; | |
222 | else | |
223 | endianness = "big endian"; | |
224 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
225 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, | |
226 | devc->hw_params, | |
227 | SND_PCM_FORMAT_S16); | |
228 | if (ret < 0) { | |
229 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); | |
230 | return SR_ERR; | |
231 | } | |
232 | ||
233 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", | |
234 | devc->cur_samplerate); | |
235 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, | |
236 | (unsigned int)devc->cur_samplerate, 0); | |
237 | if (ret < 0) { | |
238 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); | |
239 | return SR_ERR; | |
240 | } | |
241 | ||
242 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); | |
243 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, | |
244 | devc->hw_params, devc->num_probes); | |
245 | if (ret < 0) { | |
246 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); | |
247 | return SR_ERR; | |
248 | } | |
249 | ||
250 | sr_dbg("Setting audio parameters."); | |
251 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
252 | if (ret < 0) { | |
253 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); | |
254 | return SR_ERR; | |
255 | } | |
256 | ||
257 | sr_dbg("Preparing audio interface for use."); | |
258 | ret = snd_pcm_prepare(devc->capture_handle); | |
259 | if (ret < 0) { | |
260 | sr_err("Can't prepare audio interface for use: %s.", | |
261 | snd_strerror(ret)); | |
262 | return SR_ERR; | |
263 | } | |
264 | ||
265 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); | |
266 | if (count < 1) { | |
267 | sr_err("Unable to obtain poll descriptors count."); | |
268 | return SR_ERR; | |
269 | } | |
270 | ||
271 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { | |
272 | sr_err("Failed to malloc ufds."); | |
273 | return SR_ERR_MALLOC; | |
274 | } | |
275 | ||
276 | sr_spew("Getting %d poll descriptors.", count); | |
277 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); | |
278 | if (ret < 0) { | |
279 | sr_err("Unable to obtain poll descriptors: %s.", | |
280 | snd_strerror(ret)); | |
281 | g_free(devc->ufds); | |
282 | return SR_ERR; | |
283 | } | |
284 | ||
285 | /* Send header packet to the session bus. */ | |
286 | std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); | |
287 | ||
288 | /* Poll every 10ms, or whenever some data comes in. */ | |
289 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
290 | alsa_receive_data, (void *)sdi); | |
291 | ||
292 | // g_free(devc->ufds); /* FIXME */ | |
293 | ||
294 | return SR_OK; | |
295 | } | |
296 | ||
297 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
298 | { | |
299 | struct sr_datafeed_packet packet; | |
300 | struct dev_context *devc; | |
301 | ||
302 | devc = sdi->priv; | |
303 | devc->cb_data = cb_data; | |
304 | ||
305 | sr_source_remove(devc->ufds[0].fd); | |
306 | ||
307 | /* Send end packet to the session bus. */ | |
308 | sr_dbg("Sending SR_DF_END packet."); | |
309 | packet.type = SR_DF_END; | |
310 | sr_session_send(cb_data, &packet); | |
311 | ||
312 | return SR_OK; | |
313 | } | |
314 | ||
315 | SR_PRIV struct sr_dev_driver alsa_driver_info = { | |
316 | .name = "alsa", | |
317 | .longname = "ALSA driver", | |
318 | .api_version = 1, | |
319 | .init = hw_init, | |
320 | .cleanup = hw_cleanup, | |
321 | .scan = hw_scan, | |
322 | .dev_list = hw_dev_list, | |
323 | .dev_clear = clear_instances, | |
324 | .config_get = config_get, | |
325 | .config_set = config_set, | |
326 | .config_list = config_list, | |
327 | .dev_open = hw_dev_open, | |
328 | .dev_close = hw_dev_close, | |
329 | .dev_acquisition_start = hw_dev_acquisition_start, | |
330 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
331 | .priv = NULL, | |
332 | }; |