]>
Commit | Line | Data |
---|---|---|
6ed4f044 DR |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
0254651d | 5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> |
6ed4f044 DR |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
9cd9f6b7 | 22 | #include "protocol.h" |
45c59c8b BV |
23 | #include "libsigrok.h" |
24 | #include "libsigrok-internal.h" | |
6ed4f044 | 25 | |
9cd9f6b7 AG |
26 | #include <stdlib.h> |
27 | #include <unistd.h> | |
28 | #include <string.h> | |
472bbb46 | 29 | |
9cd9f6b7 | 30 | #define DEFAULT_PROBES 2 |
0254651d UH |
31 | #define SAMPLE_WIDTH 16 |
32 | #define DEFAULT_SAMPLERATE 44100 | |
33 | // #define AUDIO_DEV "plughw:0,0" | |
34 | #define AUDIO_DEV "default" | |
a4cfb10f | 35 | |
915f7cc8 | 36 | static const int hwcaps[] = { |
5a2326a7 UH |
37 | SR_HWCAP_SAMPLERATE, |
38 | SR_HWCAP_LIMIT_SAMPLES, | |
39 | SR_HWCAP_CONTINUOUS, | |
0254651d | 40 | 0, |
6ed4f044 DR |
41 | }; |
42 | ||
0254651d UH |
43 | static const char *probe_names[] = { |
44 | "Channel 0", | |
45 | "Channel 1", | |
464d12c7 KS |
46 | NULL, |
47 | }; | |
48 | ||
0254651d UH |
49 | SR_PRIV struct sr_dev_driver alsa_driver_info; |
50 | static struct sr_dev_driver *di = &alsa_driver_info; | |
6ed4f044 | 51 | |
0254651d UH |
52 | static int clear_instances(void) |
53 | { | |
54 | /* TODO */ | |
55 | ||
56 | return SR_OK; | |
57 | } | |
58 | ||
34f06b90 | 59 | static int hw_init(struct sr_context *sr_ctx) |
6ed4f044 | 60 | { |
0254651d | 61 | struct drv_context *drvc; |
6ed4f044 | 62 | |
0254651d UH |
63 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { |
64 | sr_err("Driver context malloc failed."); | |
886a52b6 | 65 | return SR_ERR_MALLOC; |
92b3101c | 66 | } |
6ed4f044 | 67 | |
0254651d UH |
68 | drvc->sr_ctx = sr_ctx; |
69 | di->priv = drvc; | |
70 | ||
71 | return SR_OK; | |
72 | } | |
73 | ||
74 | static GSList *hw_scan(GSList *options) | |
75 | { | |
76 | struct drv_context *drvc; | |
77 | struct dev_context *devc; | |
78 | struct sr_dev_inst *sdi; | |
79 | struct sr_probe *probe; | |
80 | GSList *devices; | |
81 | int i; | |
82 | ||
83 | (void)options; | |
84 | ||
85 | drvc = di->priv; | |
86 | drvc->instances = NULL; | |
87 | ||
88 | devices = NULL; | |
89 | ||
90 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
91 | sr_err("Device context malloc failed."); | |
92 | return NULL; | |
93 | } | |
94 | ||
ea9cfed7 | 95 | if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) { |
0254651d UH |
96 | sr_err("Failed to create device instance."); |
97 | return NULL; | |
ea9cfed7 | 98 | } |
6ed4f044 | 99 | |
0254651d UH |
100 | /* Set the samplerate to a default value for now. */ |
101 | devc->cur_samplerate = DEFAULT_SAMPLERATE; | |
9cd9f6b7 | 102 | devc->num_probes = DEFAULT_PROBES; |
0254651d UH |
103 | |
104 | sdi->priv = devc; | |
105 | sdi->driver = di; | |
6ed4f044 | 106 | |
0254651d UH |
107 | for (i = 0; probe_names[i]; i++) { |
108 | if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, | |
109 | probe_names[i]))) { | |
110 | sr_err("Failed to create probe."); | |
111 | return NULL; | |
112 | } | |
113 | sdi->probes = g_slist_append(sdi->probes, probe); | |
114 | } | |
6ed4f044 | 115 | |
0254651d UH |
116 | drvc->instances = g_slist_append(drvc->instances, sdi); |
117 | devices = g_slist_append(devices, sdi); | |
ffedd0bf | 118 | |
0254651d | 119 | return devices; |
6ed4f044 DR |
120 | } |
121 | ||
0254651d | 122 | static GSList *hw_dev_list(void) |
6ed4f044 | 123 | { |
0254651d UH |
124 | struct drv_context *drvc; |
125 | ||
126 | drvc = di->priv; | |
127 | ||
128 | return drvc->instances; | |
129 | } | |
130 | ||
131 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
132 | { | |
133 | struct dev_context *devc; | |
ebc34738 | 134 | int ret; |
6ed4f044 | 135 | |
0254651d | 136 | devc = sdi->priv; |
6ed4f044 | 137 | |
0254651d UH |
138 | sr_dbg("Opening audio device '%s' for stream capture.", AUDIO_DEV); |
139 | ret = snd_pcm_open(&devc->capture_handle, AUDIO_DEV, | |
ea9cfed7 | 140 | SND_PCM_STREAM_CAPTURE, 0); |
ebc34738 | 141 | if (ret < 0) { |
0254651d | 142 | sr_err("Can't open audio device: %s.", snd_strerror(ret)); |
e46b8fb1 | 143 | return SR_ERR; |
6ed4f044 DR |
144 | } |
145 | ||
0254651d UH |
146 | sr_dbg("Allocating hardware parameter structure."); |
147 | ret = snd_pcm_hw_params_malloc(&devc->hw_params); | |
ebc34738 | 148 | if (ret < 0) { |
0254651d | 149 | sr_err("Can't allocate hardware parameter structure: %s.", |
ebc34738 | 150 | snd_strerror(ret)); |
886a52b6 | 151 | return SR_ERR_MALLOC; |
6ed4f044 DR |
152 | } |
153 | ||
0254651d UH |
154 | sr_dbg("Initializing hardware parameter structure."); |
155 | ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params); | |
ebc34738 | 156 | if (ret < 0) { |
0254651d | 157 | sr_err("Can't initialize hardware parameter structure: %s.", |
472bbb46 | 158 | snd_strerror(ret)); |
e46b8fb1 | 159 | return SR_ERR; |
6ed4f044 DR |
160 | } |
161 | ||
e46b8fb1 | 162 | return SR_OK; |
6ed4f044 DR |
163 | } |
164 | ||
0254651d | 165 | static int hw_dev_close(struct sr_dev_inst *sdi) |
6ed4f044 | 166 | { |
0254651d UH |
167 | int ret; |
168 | struct dev_context *devc; | |
6ed4f044 | 169 | |
0254651d UH |
170 | devc = sdi->priv; |
171 | ||
172 | sr_dbg("Closing device."); | |
6ed4f044 | 173 | |
0254651d UH |
174 | if (devc->hw_params) { |
175 | sr_dbg("Freeing hardware parameters."); | |
176 | snd_pcm_hw_params_free(devc->hw_params); | |
177 | } else { | |
178 | sr_dbg("No hardware parameters, no need to free."); | |
697785d1 UH |
179 | } |
180 | ||
0254651d UH |
181 | if (devc->capture_handle) { |
182 | sr_dbg("Closing PCM device."); | |
183 | if ((ret = snd_pcm_close(devc->capture_handle)) < 0) { | |
184 | sr_err("Failed to close device: %s.", | |
185 | snd_strerror(ret)); | |
186 | } | |
187 | } else { | |
188 | sr_dbg("No capture handle, no need to close audio device."); | |
189 | } | |
697785d1 UH |
190 | |
191 | return SR_OK; | |
6ed4f044 DR |
192 | } |
193 | ||
57ab7d9f | 194 | static int hw_cleanup(void) |
6ed4f044 | 195 | { |
0254651d | 196 | clear_instances(); |
57ab7d9f UH |
197 | |
198 | return SR_OK; | |
6ed4f044 DR |
199 | } |
200 | ||
0254651d UH |
201 | static int hw_info_get(int info_id, const void **data, |
202 | const struct sr_dev_inst *sdi) | |
6ed4f044 | 203 | { |
0254651d | 204 | struct dev_context *devc; |
6ed4f044 | 205 | |
0254651d UH |
206 | if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */ |
207 | devc = sdi->priv; | |
6ed4f044 | 208 | |
0254651d UH |
209 | switch (info_id) { |
210 | case SR_DI_HWCAPS: | |
211 | *data = hwcaps; | |
6ed4f044 | 212 | break; |
5a2326a7 | 213 | case SR_DI_NUM_PROBES: |
9cd9f6b7 | 214 | *data = GINT_TO_POINTER(DEFAULT_PROBES); |
6ed4f044 | 215 | break; |
464d12c7 | 216 | case SR_DI_PROBE_NAMES: |
0254651d | 217 | *data = probe_names; |
464d12c7 | 218 | break; |
5a2326a7 | 219 | case SR_DI_CUR_SAMPLERATE: |
0254651d | 220 | *data = &devc->cur_samplerate; |
6ed4f044 | 221 | break; |
0254651d UH |
222 | default: |
223 | sr_err("Invalid info_id: %d.", info_id); | |
224 | return SR_ERR_ARG; | |
6ed4f044 DR |
225 | } |
226 | ||
0254651d | 227 | return SR_OK; |
6ed4f044 DR |
228 | } |
229 | ||
0254651d UH |
230 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, |
231 | const void *value) | |
6ed4f044 | 232 | { |
0254651d | 233 | struct dev_context *devc; |
6ed4f044 | 234 | |
0254651d | 235 | devc = sdi->priv; |
6ed4f044 | 236 | |
ffedd0bf | 237 | switch (hwcap) { |
5a2326a7 | 238 | case SR_HWCAP_SAMPLERATE: |
0254651d UH |
239 | devc->cur_samplerate = *(const uint64_t *)value; |
240 | break; | |
5a2326a7 | 241 | case SR_HWCAP_LIMIT_SAMPLES: |
0254651d UH |
242 | devc->limit_samples = *(const uint64_t *)value; |
243 | break; | |
6ed4f044 | 244 | default: |
0254651d | 245 | sr_err("Unknown capability: %d.", hwcap); |
e46b8fb1 | 246 | return SR_ERR; |
6ed4f044 | 247 | } |
0254651d UH |
248 | |
249 | return SR_OK; | |
6ed4f044 DR |
250 | } |
251 | ||
0254651d UH |
252 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
253 | void *cb_data) | |
6ed4f044 | 254 | { |
b9c735a2 UH |
255 | struct sr_datafeed_packet packet; |
256 | struct sr_datafeed_header header; | |
0254651d UH |
257 | struct sr_datafeed_meta_analog meta; |
258 | struct dev_context *devc; | |
259 | int count, ret; | |
6ed4f044 | 260 | |
0254651d UH |
261 | devc = sdi->priv; |
262 | devc->cb_data = cb_data; | |
6ed4f044 | 263 | |
0254651d UH |
264 | sr_dbg("Setting audio access type to RW/interleaved."); |
265 | ret = snd_pcm_hw_params_set_access(devc->capture_handle, | |
266 | devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); | |
ebc34738 | 267 | if (ret < 0) { |
0254651d | 268 | sr_err("Can't set audio access type: %s.", snd_strerror(ret)); |
e46b8fb1 | 269 | return SR_ERR; |
6ed4f044 DR |
270 | } |
271 | ||
0254651d UH |
272 | /* FIXME: Hardcoded for 16bits. */ |
273 | sr_dbg("Setting audio sample format to signed 16bit (little endian)."); | |
274 | ret = snd_pcm_hw_params_set_format(devc->capture_handle, | |
275 | devc->hw_params, SND_PCM_FORMAT_S16_LE); | |
ebc34738 | 276 | if (ret < 0) { |
0254651d | 277 | sr_err("Can't set audio sample format: %s.", snd_strerror(ret)); |
e46b8fb1 | 278 | return SR_ERR; |
6ed4f044 DR |
279 | } |
280 | ||
0254651d UH |
281 | sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.", |
282 | devc->cur_samplerate); | |
283 | ret = snd_pcm_hw_params_set_rate_near(devc->capture_handle, | |
284 | devc->hw_params, (unsigned int *)&devc->cur_samplerate, 0); | |
ebc34738 | 285 | if (ret < 0) { |
0254651d | 286 | sr_err("Can't set audio sample rate: %s.", snd_strerror(ret)); |
e46b8fb1 | 287 | return SR_ERR; |
6ed4f044 DR |
288 | } |
289 | ||
9cd9f6b7 | 290 | sr_dbg("Setting audio channel count to %d.", devc->num_probes); |
0254651d | 291 | ret = snd_pcm_hw_params_set_channels(devc->capture_handle, |
9cd9f6b7 | 292 | devc->hw_params, devc->num_probes); |
ebc34738 | 293 | if (ret < 0) { |
0254651d | 294 | sr_err("Can't set channel count: %s.", snd_strerror(ret)); |
e46b8fb1 | 295 | return SR_ERR; |
6ed4f044 DR |
296 | } |
297 | ||
0254651d UH |
298 | sr_dbg("Setting audio parameters."); |
299 | ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params); | |
ebc34738 | 300 | if (ret < 0) { |
0254651d | 301 | sr_err("Can't set parameters: %s.", snd_strerror(ret)); |
e46b8fb1 | 302 | return SR_ERR; |
6ed4f044 DR |
303 | } |
304 | ||
0254651d UH |
305 | sr_dbg("Preparing audio interface for use."); |
306 | ret = snd_pcm_prepare(devc->capture_handle); | |
ebc34738 | 307 | if (ret < 0) { |
0254651d | 308 | sr_err("Can't prepare audio interface for use: %s.", |
ebc34738 | 309 | snd_strerror(ret)); |
e46b8fb1 | 310 | return SR_ERR; |
6ed4f044 DR |
311 | } |
312 | ||
0254651d | 313 | count = snd_pcm_poll_descriptors_count(devc->capture_handle); |
6ed4f044 | 314 | if (count < 1) { |
472bbb46 | 315 | sr_err("Unable to obtain poll descriptors count."); |
e46b8fb1 | 316 | return SR_ERR; |
6ed4f044 | 317 | } |
0254651d | 318 | sr_spew("Obtained poll descriptor count: %d.", count); |
6ed4f044 | 319 | |
0254651d UH |
320 | if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) { |
321 | sr_err("Failed to malloc ufds."); | |
e46b8fb1 | 322 | return SR_ERR_MALLOC; |
92b3101c | 323 | } |
6ed4f044 | 324 | |
0254651d UH |
325 | sr_err("Getting %d poll descriptors.", count); |
326 | ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count); | |
ebc34738 | 327 | if (ret < 0) { |
0254651d | 328 | sr_err("Unable to obtain poll descriptors: %s.", |
ebc34738 | 329 | snd_strerror(ret)); |
0254651d | 330 | g_free(devc->ufds); |
e46b8fb1 | 331 | return SR_ERR; |
6ed4f044 DR |
332 | } |
333 | ||
0254651d UH |
334 | /* Send header packet to the session bus. */ |
335 | sr_dbg("Sending SR_DF_HEADER packet."); | |
5a2326a7 | 336 | packet.type = SR_DF_HEADER; |
0254651d | 337 | packet.payload = (uint8_t *)&header; |
6ed4f044 DR |
338 | header.feed_version = 1; |
339 | gettimeofday(&header.starttime, NULL); | |
0254651d UH |
340 | sr_session_send(devc->cb_data, &packet); |
341 | ||
342 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
343 | sr_dbg("Sending SR_DF_META_ANALOG packet."); | |
344 | packet.type = SR_DF_META_ANALOG; | |
345 | packet.payload = &meta; | |
9cd9f6b7 | 346 | meta.num_probes = devc->num_probes; |
0254651d UH |
347 | sr_session_send(devc->cb_data, &packet); |
348 | ||
349 | /* Poll every 10ms, or whenever some data comes in. */ | |
350 | sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10, | |
9cd9f6b7 | 351 | alsa_receive_data, (void *)sdi); |
0254651d UH |
352 | |
353 | // g_free(devc->ufds); /* FIXME */ | |
6ed4f044 | 354 | |
e46b8fb1 | 355 | return SR_OK; |
6ed4f044 DR |
356 | } |
357 | ||
0254651d | 358 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6ed4f044 | 359 | { |
0254651d UH |
360 | struct sr_datafeed_packet packet; |
361 | struct dev_context *devc; | |
362 | ||
363 | devc = sdi->priv; | |
364 | devc->cb_data = cb_data; | |
365 | ||
366 | sr_source_remove(devc->ufds[0].fd); | |
367 | ||
368 | /* Send end packet to the session bus. */ | |
369 | sr_dbg("Sending SR_DF_END packet."); | |
370 | packet.type = SR_DF_END; | |
371 | sr_session_send(cb_data, &packet); | |
3010f21c UH |
372 | |
373 | return SR_OK; | |
6ed4f044 DR |
374 | } |
375 | ||
c09f0b57 | 376 | SR_PRIV struct sr_dev_driver alsa_driver_info = { |
e519ba86 UH |
377 | .name = "alsa", |
378 | .longname = "ALSA driver", | |
379 | .api_version = 1, | |
380 | .init = hw_init, | |
381 | .cleanup = hw_cleanup, | |
0254651d UH |
382 | .scan = hw_scan, |
383 | .dev_list = hw_dev_list, | |
384 | .dev_clear = clear_instances, | |
e7eb703f UH |
385 | .dev_open = hw_dev_open, |
386 | .dev_close = hw_dev_close, | |
0254651d | 387 | .info_get = hw_info_get, |
a9a245b4 | 388 | .dev_config_set = hw_dev_config_set, |
69040b7c UH |
389 | .dev_acquisition_start = hw_dev_acquisition_start, |
390 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
0254651d | 391 | .priv = NULL, |
6ed4f044 | 392 | }; |