]>
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_CONF_SAMPLERATE: | |
155 | *data = &devc->cur_samplerate; | |
156 | break; | |
157 | default: | |
158 | return SR_ERR_ARG; | |
159 | } | |
160 | ||
161 | return SR_OK; | |
162 | } | |
163 | ||
164 | static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) | |
165 | { | |
166 | struct dev_context *devc; | |
167 | ||
168 | devc = sdi->priv; | |
169 | ||
170 | switch (id) { | |
171 | case SR_CONF_SAMPLERATE: | |
172 | alsa_set_samplerate(sdi, *(const uint64_t *)value); | |
173 | break; | |
174 | case SR_CONF_LIMIT_SAMPLES: | |
175 | devc->limit_samples = *(const uint64_t *)value; | |
176 | break; | |
177 | default: | |
178 | sr_err("Unknown capability: %d.", id); | |
179 | return SR_ERR; | |
180 | } | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
185 | static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) | |
186 | { | |
187 | struct dev_context *devc; | |
188 | ||
189 | (void)sdi; | |
190 | ||
191 | switch (key) { | |
192 | case SR_CONF_SAMPLERATE: | |
193 | if (!sdi || !sdi->priv) | |
194 | return SR_ERR_ARG; | |
195 | devc = sdi->priv; | |
196 | if (!devc->supp_rates.list) { | |
197 | sr_err("Instance did not contain a samplerate list."); | |
198 | return SR_ERR_ARG; | |
199 | } | |
200 | *data = &devc->supp_rates; | |
201 | break; | |
202 | default: | |
203 | return SR_ERR_ARG; | |
204 | } | |
205 | ||
206 | return SR_OK; | |
207 | } | |
208 | ||
209 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
210 | void *cb_data) | |
211 | { | |
212 | struct sr_datafeed_packet packet; | |
213 | struct sr_datafeed_header header; | |
214 | struct dev_context *devc; | |
215 | int count, ret; | |
216 | char *endianness; | |
217 | ||
218 | devc = sdi->priv; | |
219 | devc->cb_data = cb_data; | |
220 | devc->num_samples = 0; | |
221 | ||
222 | sr_dbg("Setting audio access type to RW/interleaved."); | |
223 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
224 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
225 | if (ret < 0) { | |
226 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); | |
227 | return SR_ERR; | |
228 | } | |
229 | ||
230 | /* FIXME: Hardcoded for 16bits. */ | |
231 | if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE) | |
232 | endianness = "little endian"; | |
233 | else | |
234 | endianness = "big endian"; | |
235 | sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness); | |
236 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, | |
237 | devc->hw_params, | |
238 | SND_PCM_FORMAT_S16); | |
239 | if (ret < 0) { | |
240 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); | |
241 | return SR_ERR; | |
242 | } | |
243 | ||
244 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", | |
245 | devc->cur_samplerate); | |
246 | ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params, | |
247 | (unsigned int)devc->cur_samplerate, 0); | |
248 | if (ret < 0) { | |
249 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); | |
250 | return SR_ERR; | |
251 | } | |
252 | ||
253 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); | |
254 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, | |
255 | devc->hw_params, devc->num_probes); | |
256 | if (ret < 0) { | |
257 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); | |
258 | return SR_ERR; | |
259 | } | |
260 | ||
261 | sr_dbg("Setting audio parameters."); | |
262 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
263 | if (ret < 0) { | |
264 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); | |
265 | return SR_ERR; | |
266 | } | |
267 | ||
268 | sr_dbg("Preparing audio interface for use."); | |
269 | ret = snd_pcm_prepare(devc->capture_handle); | |
270 | if (ret < 0) { | |
271 | sr_err("Can't prepare audio interface for use: %s.", | |
272 | snd_strerror(ret)); | |
273 | return SR_ERR; | |
274 | } | |
275 | ||
276 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); | |
277 | if (count < 1) { | |
278 | sr_err("Unable to obtain poll descriptors count."); | |
279 | return SR_ERR; | |
280 | } | |
281 | ||
282 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { | |
283 | sr_err("Failed to malloc ufds."); | |
284 | return SR_ERR_MALLOC; | |
285 | } | |
286 | ||
287 | sr_spew("Getting %d poll descriptors.", count); | |
288 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); | |
289 | if (ret < 0) { | |
290 | sr_err("Unable to obtain poll descriptors: %s.", | |
291 | snd_strerror(ret)); | |
292 | g_free(devc->ufds); | |
293 | return SR_ERR; | |
294 | } | |
295 | ||
296 | /* Send header packet to the session bus. */ | |
297 | sr_dbg("Sending SR_DF_HEADER packet."); | |
298 | packet.type = SR_DF_HEADER; | |
299 | packet.payload = (uint8_t *)&header; | |
300 | header.feed_version = 1; | |
301 | gettimeofday(&header.starttime, NULL); | |
302 | sr_session_send(devc->cb_data, &packet); | |
303 | ||
304 | /* Poll every 10ms, or whenever some data comes in. */ | |
305 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
306 | alsa_receive_data, (void *)sdi); | |
307 | ||
308 | // g_free(devc->ufds); /* FIXME */ | |
309 | ||
310 | return SR_OK; | |
311 | } | |
312 | ||
313 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
314 | { | |
315 | struct sr_datafeed_packet packet; | |
316 | struct dev_context *devc; | |
317 | ||
318 | devc = sdi->priv; | |
319 | devc->cb_data = cb_data; | |
320 | ||
321 | sr_source_remove(devc->ufds[0].fd); | |
322 | ||
323 | /* Send end packet to the session bus. */ | |
324 | sr_dbg("Sending SR_DF_END packet."); | |
325 | packet.type = SR_DF_END; | |
326 | sr_session_send(cb_data, &packet); | |
327 | ||
328 | return SR_OK; | |
329 | } | |
330 | ||
331 | SR_PRIV struct sr_dev_driver alsa_driver_info = { | |
332 | .name = "alsa", | |
333 | .longname = "ALSA driver", | |
334 | .api_version = 1, | |
335 | .init = hw_init, | |
336 | .cleanup = hw_cleanup, | |
337 | .scan = hw_scan, | |
338 | .dev_list = hw_dev_list, | |
339 | .dev_clear = clear_instances, | |
340 | .config_get = config_get, | |
341 | .config_set = config_set, | |
342 | .config_list = config_list, | |
343 | .dev_open = hw_dev_open, | |
344 | .dev_close = hw_dev_close, | |
345 | .dev_acquisition_start = hw_dev_acquisition_start, | |
346 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
347 | .priv = NULL, | |
348 | }; |