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