2 * This file is part of the libsigrok project.
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>
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.
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.
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
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 static const int hwcaps[] = {
32 SR_CONF_LIMIT_SAMPLES,
37 SR_PRIV struct sr_dev_driver alsa_driver_info;
38 static struct sr_dev_driver *di = &alsa_driver_info;
40 static int clear_instances(void)
42 struct drv_context *drvc;
44 if (!(drvc = di->priv))
47 g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
48 drvc->instances = NULL;
53 static int hw_init(struct sr_context *sr_ctx)
55 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
58 static GSList *hw_scan(GSList *options)
60 return alsa_scan(options, di);
63 static GSList *hw_dev_list(void)
65 struct drv_context *drvc;
69 return drvc->instances;
72 static int hw_dev_open(struct sr_dev_inst *sdi)
74 struct dev_context *devc;
80 sr_err("devc->hwdev was NULL.");
84 sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev);
85 ret = snd_pcm_open(&devc->capture_handle, devc->hwdev,
86 SND_PCM_STREAM_CAPTURE, 0);
88 sr_err("Can't open audio device: %s.", snd_strerror(ret));
92 sr_dbg("Initializing hardware parameter structure.");
93 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
95 sr_err("Can't initialize hardware parameter structure: %s.",
103 static int hw_dev_close(struct sr_dev_inst *sdi)
106 struct dev_context *devc;
110 sr_dbg("Closing device.");
112 if (devc->capture_handle) {
113 sr_dbg("Closing PCM device.");
114 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
115 sr_err("Failed to close device: %s.",
117 devc->capture_handle = NULL;
120 sr_dbg("No capture handle, no need to close audio device.");
126 static int hw_cleanup(void)
133 static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
135 struct dev_context *devc;
138 case SR_CONF_SAMPLERATE:
140 *data = &devc->cur_samplerate;
149 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
151 struct dev_context *devc;
156 case SR_CONF_SAMPLERATE:
157 alsa_set_samplerate(sdi, *(const uint64_t *)value);
159 case SR_CONF_LIMIT_SAMPLES:
160 devc->limit_samples = *(const uint64_t *)value;
163 sr_err("Unknown capability: %d.", id);
170 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
172 struct dev_context *devc;
177 case SR_CONF_DEVICE_OPTIONS:
180 case SR_CONF_SAMPLERATE:
181 if (!sdi || !sdi->priv)
184 if (!devc->supp_rates.list) {
185 sr_err("Instance did not contain a samplerate list.");
188 *data = &devc->supp_rates;
197 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
200 struct sr_datafeed_packet packet;
201 struct sr_datafeed_header header;
202 struct dev_context *devc;
207 devc->cb_data = cb_data;
208 devc->num_samples = 0;
210 sr_dbg("Setting audio access type to RW/interleaved.");
211 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
212 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
214 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
218 /* FIXME: Hardcoded for 16bits. */
219 if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE)
220 endianness = "little endian";
222 endianness = "big endian";
223 sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness);
224 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
228 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
232 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
233 devc->cur_samplerate);
234 ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
235 (unsigned int)devc->cur_samplerate, 0);
237 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
241 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
242 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
243 devc->hw_params, devc->num_probes);
245 sr_err("Can't set channel count: %s.", snd_strerror(ret));
249 sr_dbg("Setting audio parameters.");
250 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
252 sr_err("Can't set parameters: %s.", snd_strerror(ret));
256 sr_dbg("Preparing audio interface for use.");
257 ret = snd_pcm_prepare(devc->capture_handle);
259 sr_err("Can't prepare audio interface for use: %s.",
264 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
266 sr_err("Unable to obtain poll descriptors count.");
270 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
271 sr_err("Failed to malloc ufds.");
272 return SR_ERR_MALLOC;
275 sr_spew("Getting %d poll descriptors.", count);
276 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
278 sr_err("Unable to obtain poll descriptors: %s.",
284 /* Send header packet to the session bus. */
285 sr_dbg("Sending SR_DF_HEADER packet.");
286 packet.type = SR_DF_HEADER;
287 packet.payload = (uint8_t *)&header;
288 header.feed_version = 1;
289 gettimeofday(&header.starttime, NULL);
290 sr_session_send(devc->cb_data, &packet);
292 /* Poll every 10ms, or whenever some data comes in. */
293 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
294 alsa_receive_data, (void *)sdi);
296 // g_free(devc->ufds); /* FIXME */
301 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
303 struct sr_datafeed_packet packet;
304 struct dev_context *devc;
307 devc->cb_data = cb_data;
309 sr_source_remove(devc->ufds[0].fd);
311 /* Send end packet to the session bus. */
312 sr_dbg("Sending SR_DF_END packet.");
313 packet.type = SR_DF_END;
314 sr_session_send(cb_data, &packet);
319 SR_PRIV struct sr_dev_driver alsa_driver_info = {
321 .longname = "ALSA driver",
324 .cleanup = hw_cleanup,
326 .dev_list = hw_dev_list,
327 .dev_clear = clear_instances,
328 .config_get = config_get,
329 .config_set = config_set,
330 .config_list = config_list,
331 .dev_open = hw_dev_open,
332 .dev_close = hw_dev_close,
333 .dev_acquisition_start = hw_dev_acquisition_start,
334 .dev_acquisition_stop = hw_dev_acquisition_stop,