]>
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 int hwcaps[] = { | |
31 | SR_CONF_SAMPLERATE, | |
32 | SR_CONF_LIMIT_SAMPLES, | |
33 | SR_CONF_CONTINUOUS, | |
34 | 0, | |
35 | }; | |
36 | ||
37 | SR_PRIV struct sr_dev_driver alsa_driver_info; | |
38 | static struct sr_dev_driver *di = &alsa_driver_info; | |
39 | ||
40 | static int clear_instances(void) | |
41 | { | |
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; | |
49 | ||
50 | return SR_OK; | |
51 | } | |
52 | ||
53 | static int hw_init(struct sr_context *sr_ctx) | |
54 | { | |
55 | struct drv_context *drvc; | |
56 | ||
57 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
58 | sr_err("Driver context malloc failed."); | |
59 | return SR_ERR_MALLOC; | |
60 | } | |
61 | ||
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 | { | |
70 | return alsa_scan(options, di); | |
71 | } | |
72 | ||
73 | static GSList *hw_dev_list(void) | |
74 | { | |
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; | |
85 | int ret; | |
86 | ||
87 | devc = sdi->priv; | |
88 | ||
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, | |
96 | SND_PCM_STREAM_CAPTURE, 0); | |
97 | if (ret < 0) { | |
98 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); | |
99 | return SR_ERR; | |
100 | } | |
101 | ||
102 | sr_dbg("Initializing hardware parameter structure."); | |
103 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
104 | if (ret < 0) { | |
105 | sr_err("Can't initialize hardware parameter structure: %s.", | |
106 | snd_strerror(ret)); | |
107 | return SR_ERR; | |
108 | } | |
109 | ||
110 | return SR_OK; | |
111 | } | |
112 | ||
113 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
114 | { | |
115 | int ret; | |
116 | struct dev_context *devc; | |
117 | ||
118 | devc = sdi->priv; | |
119 | ||
120 | sr_dbg("Closing device."); | |
121 | ||
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)); | |
127 | devc->capture_handle = NULL; | |
128 | } | |
129 | } else { | |
130 | sr_dbg("No capture handle, no need to close audio device."); | |
131 | } | |
132 | ||
133 | return SR_OK; | |
134 | } | |
135 | ||
136 | static int hw_cleanup(void) | |
137 | { | |
138 | clear_instances(); | |
139 | ||
140 | return SR_OK; | |
141 | } | |
142 | ||
143 | static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) | |
144 | { | |
145 | struct dev_context *devc; | |
146 | ||
147 | if (id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */ | |
148 | devc = sdi->priv; | |
149 | ||
150 | switch (id) { | |
151 | case SR_DI_HWCAPS: | |
152 | *data = hwcaps; | |
153 | break; | |
154 | case SR_DI_CUR_SAMPLERATE: | |
155 | *data = &devc->cur_samplerate; | |
156 | break; | |
157 | case SR_DI_SAMPLERATES: | |
158 | if (!devc->supp_rates.list) { | |
159 | sr_err("Instance did not contain a samplerate list."); | |
160 | return SR_ERR_ARG; | |
161 | } | |
162 | *data = &devc->supp_rates; | |
163 | break; | |
164 | default: | |
165 | return SR_ERR_ARG; | |
166 | } | |
167 | ||
168 | return SR_OK; | |
169 | } | |
170 | ||
171 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) | |
172 | { | |
173 | struct dev_context *devc; | |
174 | ||
175 | devc = sdi->priv; | |
176 | ||
177 | switch (id) { | |
178 | case SR_CONF_SAMPLERATE: | |
179 | alsa_set_samplerate(sdi, *(const uint64_t *)value); | |
180 | break; | |
181 | case SR_CONF_LIMIT_SAMPLES: | |
182 | devc->limit_samples = *(const uint64_t *)value; | |
183 | break; | |
184 | default: | |
185 | sr_err("Unknown capability: %d.", id); | |
186 | return SR_ERR; | |
187 | } | |
188 | ||
189 | return SR_OK; | |
190 | } | |
191 | ||
192 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) | |
193 | { | |
194 | struct dev_context *devc; | |
195 | ||
196 | (void)sdi; | |
197 | ||
198 | switch (key) { | |
199 | case SR_CONF_SAMPLERATE: | |
200 | if (!sdi || !sdi->priv) | |
201 | return SR_ERR_ARG; | |
202 | devc = sdi->priv; | |
203 | if (!devc->supp_rates.list) { | |
204 | sr_err("Instance did not contain a samplerate list."); | |
205 | return SR_ERR_ARG; | |
206 | } | |
207 | *data = &devc->supp_rates; | |
208 | break; | |
209 | default: | |
210 | return SR_ERR_ARG; | |
211 | } | |
212 | ||
213 | return SR_OK; | |
214 | } | |
215 | ||
216 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
217 | void *cb_data) | |
218 | { | |
219 | struct sr_datafeed_packet packet; | |
220 | struct sr_datafeed_header header; | |
221 | struct dev_context *devc; | |
222 | int count, ret; | |
223 | char *endianness; | |
224 | ||
225 | devc = sdi->priv; | |
226 | devc->cb_data = cb_data; | |
227 | devc->num_samples = 0; | |
228 | ||
229 | sr_dbg("Setting audio access type to RW/interleaved."); | |
230 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
231 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
232 | if (ret < 0) { | |
233 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); | |
234 | return SR_ERR; | |
235 | } | |
236 | ||
237 | /* FIXME: Hardcoded for 16bits. */ | |
238 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) | |
239 | endianness = "little endian"; | |
240 | else | |
241 | endianness = "big endian"; | |
242 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
243 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, | |
244 | devc->hw_params, | |
245 | SND_PCM_FORMAT_S16); | |
246 | if (ret < 0) { | |
247 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); | |
248 | return SR_ERR; | |
249 | } | |
250 | ||
251 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", | |
252 | devc->cur_samplerate); | |
253 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, | |
254 | (unsigned int)devc->cur_samplerate, 0); | |
255 | if (ret < 0) { | |
256 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); | |
257 | return SR_ERR; | |
258 | } | |
259 | ||
260 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); | |
261 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, | |
262 | devc->hw_params, devc->num_probes); | |
263 | if (ret < 0) { | |
264 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); | |
265 | return SR_ERR; | |
266 | } | |
267 | ||
268 | sr_dbg("Setting audio parameters."); | |
269 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
270 | if (ret < 0) { | |
271 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); | |
272 | return SR_ERR; | |
273 | } | |
274 | ||
275 | sr_dbg("Preparing audio interface for use."); | |
276 | ret = snd_pcm_prepare(devc->capture_handle); | |
277 | if (ret < 0) { | |
278 | sr_err("Can't prepare audio interface for use: %s.", | |
279 | snd_strerror(ret)); | |
280 | return SR_ERR; | |
281 | } | |
282 | ||
283 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); | |
284 | if (count < 1) { | |
285 | sr_err("Unable to obtain poll descriptors count."); | |
286 | return SR_ERR; | |
287 | } | |
288 | ||
289 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { | |
290 | sr_err("Failed to malloc ufds."); | |
291 | return SR_ERR_MALLOC; | |
292 | } | |
293 | ||
294 | sr_spew("Getting %d poll descriptors.", count); | |
295 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); | |
296 | if (ret < 0) { | |
297 | sr_err("Unable to obtain poll descriptors: %s.", | |
298 | snd_strerror(ret)); | |
299 | g_free(devc->ufds); | |
300 | return SR_ERR; | |
301 | } | |
302 | ||
303 | /* Send header packet to the session bus. */ | |
304 | sr_dbg("Sending SR_DF_HEADER packet."); | |
305 | packet.type = SR_DF_HEADER; | |
306 | packet.payload = (uint8_t *)&header; | |
307 | header.feed_version = 1; | |
308 | gettimeofday(&header.starttime, NULL); | |
309 | sr_session_send(devc->cb_data, &packet); | |
310 | ||
311 | /* Poll every 10ms, or whenever some data comes in. */ | |
312 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
313 | alsa_receive_data, (void *)sdi); | |
314 | ||
315 | // g_free(devc->ufds); /* FIXME */ | |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
321 | { | |
322 | struct sr_datafeed_packet packet; | |
323 | struct dev_context *devc; | |
324 | ||
325 | devc = sdi->priv; | |
326 | devc->cb_data = cb_data; | |
327 | ||
328 | sr_source_remove(devc->ufds[0].fd); | |
329 | ||
330 | /* Send end packet to the session bus. */ | |
331 | sr_dbg("Sending SR_DF_END packet."); | |
332 | packet.type = SR_DF_END; | |
333 | sr_session_send(cb_data, &packet); | |
334 | ||
335 | return SR_OK; | |
336 | } | |
337 | ||
338 | SR_PRIV struct sr_dev_driver alsa_driver_info = { | |
339 | .name = "alsa", | |
340 | .longname = "ALSA driver", | |
341 | .api_version = 1, | |
342 | .init = hw_init, | |
343 | .cleanup = hw_cleanup, | |
344 | .scan = hw_scan, | |
345 | .dev_list = hw_dev_list, | |
346 | .dev_clear = clear_instances, | |
347 | .config_get = config_get, | |
348 | .config_set = config_set, | |
349 | .config_list = config_list, | |
350 | .dev_open = hw_dev_open, | |
351 | .dev_close = hw_dev_close, | |
352 | .dev_acquisition_start = hw_dev_acquisition_start, | |
353 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
354 | .priv = NULL, | |
355 | }; |