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_HWCAP_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 struct drv_context *drvc;
57 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
58 sr_err("Driver context malloc failed.");
62 drvc->sr_ctx = sr_ctx;
68 static GSList *hw_scan(GSList *options)
70 return alsa_scan(options, di);
73 static GSList *hw_dev_list(void)
75 struct drv_context *drvc;
79 return drvc->instances;
82 static int hw_dev_open(struct sr_dev_inst *sdi)
84 struct dev_context *devc;
90 sr_err("devc->hwdev was NULL.");
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);
98 sr_err("Can't open audio device: %s.", snd_strerror(ret));
102 sr_dbg("Initializing hardware parameter structure.");
103 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
105 sr_err("Can't initialize hardware parameter structure: %s.",
113 static int hw_dev_close(struct sr_dev_inst *sdi)
116 struct dev_context *devc;
120 sr_dbg("Closing device.");
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.",
127 devc->capture_handle = NULL;
130 sr_dbg("No capture handle, no need to close audio device.");
136 static int hw_cleanup(void)
143 static int hw_info_get(int info_id, const void **data,
144 const struct sr_dev_inst *sdi)
146 struct dev_context *devc;
148 if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
155 case SR_DI_CUR_SAMPLERATE:
156 *data = &devc->cur_samplerate;
158 case SR_DI_SAMPLERATES:
159 if (!devc->supp_rates.list) {
160 sr_err("Instance did not contain a samplerate list.");
163 *data = &devc->supp_rates;
166 sr_err("Invalid info_id: %d.", info_id);
173 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
176 struct dev_context *devc;
181 case SR_HWCAP_SAMPLERATE:
182 alsa_set_samplerate(sdi, *(const uint64_t *)value);
184 case SR_HWCAP_LIMIT_SAMPLES:
185 devc->limit_samples = *(const uint64_t *)value;
188 sr_err("Unknown capability: %d.", hwcap);
195 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
198 struct sr_datafeed_packet packet;
199 struct sr_datafeed_header header;
200 struct sr_datafeed_meta_analog meta;
201 struct dev_context *devc;
206 devc->cb_data = cb_data;
207 devc->num_samples = 0;
209 sr_dbg("Setting audio access type to RW/interleaved.");
210 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
211 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
213 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
217 /* FIXME: Hardcoded for 16bits. */
218 if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE)
219 endianness = "little endian";
221 endianness = "big endian";
222 sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness);
223 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
227 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
231 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
232 devc->cur_samplerate);
233 ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
234 (unsigned int)devc->cur_samplerate, 0);
236 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
240 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
241 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
242 devc->hw_params, devc->num_probes);
244 sr_err("Can't set channel count: %s.", snd_strerror(ret));
248 sr_dbg("Setting audio parameters.");
249 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
251 sr_err("Can't set parameters: %s.", snd_strerror(ret));
255 sr_dbg("Preparing audio interface for use.");
256 ret = snd_pcm_prepare(devc->capture_handle);
258 sr_err("Can't prepare audio interface for use: %s.",
263 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
265 sr_err("Unable to obtain poll descriptors count.");
269 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
270 sr_err("Failed to malloc ufds.");
271 return SR_ERR_MALLOC;
274 sr_err("Getting %d poll descriptors.", count);
275 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
277 sr_err("Unable to obtain poll descriptors: %s.",
283 /* Send header packet to the session bus. */
284 sr_dbg("Sending SR_DF_HEADER packet.");
285 packet.type = SR_DF_HEADER;
286 packet.payload = (uint8_t *)&header;
287 header.feed_version = 1;
288 gettimeofday(&header.starttime, NULL);
289 sr_session_send(devc->cb_data, &packet);
291 /* Send metadata about the SR_DF_ANALOG packets to come. */
292 sr_dbg("Sending SR_DF_META_ANALOG packet.");
293 packet.type = SR_DF_META_ANALOG;
294 packet.payload = &meta;
295 meta.num_probes = devc->num_probes;
296 sr_session_send(devc->cb_data, &packet);
298 /* Poll every 10ms, or whenever some data comes in. */
299 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
300 alsa_receive_data, (void *)sdi);
302 // g_free(devc->ufds); /* FIXME */
307 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
309 struct sr_datafeed_packet packet;
310 struct dev_context *devc;
313 devc->cb_data = cb_data;
315 sr_source_remove(devc->ufds[0].fd);
317 /* Send end packet to the session bus. */
318 sr_dbg("Sending SR_DF_END packet.");
319 packet.type = SR_DF_END;
320 sr_session_send(cb_data, &packet);
325 SR_PRIV struct sr_dev_driver alsa_driver_info = {
327 .longname = "ALSA driver",
330 .cleanup = hw_cleanup,
332 .dev_list = hw_dev_list,
333 .dev_clear = clear_instances,
334 .dev_open = hw_dev_open,
335 .dev_close = hw_dev_close,
336 .info_get = hw_info_get,
337 .dev_config_set = hw_dev_config_set,
338 .dev_acquisition_start = hw_dev_acquisition_start,
339 .dev_acquisition_stop = hw_dev_acquisition_stop,