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
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
31 #define DEFAULT_PROBES 2
32 #define SAMPLE_WIDTH 16
33 #define DEFAULT_SAMPLERATE 44100
35 static const int hwcaps[] = {
37 SR_HWCAP_LIMIT_SAMPLES,
42 static const char *probe_names[] = {
43 "Channel 0", "Channel 1",
47 SR_PRIV struct sr_dev_driver alsa_driver_info;
48 static struct sr_dev_driver *di = &alsa_driver_info;
50 static int clear_instances(void)
52 struct drv_context *drvc;
54 if (!(drvc = di->priv))
57 g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
58 drvc->instances = NULL;
63 static int hw_init(struct sr_context *sr_ctx)
65 struct drv_context *drvc;
67 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
68 sr_err("Driver context malloc failed.");
72 drvc->sr_ctx = sr_ctx;
78 static GSList *hw_scan(GSList *options)
80 return alsa_scan(options, di);
83 static GSList *hw_dev_list(void)
85 struct drv_context *drvc;
89 return drvc->instances;
92 static int hw_dev_open(struct sr_dev_inst *sdi)
94 struct dev_context *devc;
100 sr_err("devc->hwdev was NULL.");
104 sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev);
105 ret = snd_pcm_open(&devc->capture_handle, devc->hwdev,
106 SND_PCM_STREAM_CAPTURE, 0);
108 sr_err("Can't open audio device: %s.", snd_strerror(ret));
112 sr_dbg("Initializing hardware parameter structure.");
113 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
115 sr_err("Can't initialize hardware parameter structure: %s.",
123 static int hw_dev_close(struct sr_dev_inst *sdi)
126 struct dev_context *devc;
130 sr_dbg("Closing device.");
132 if (devc->capture_handle) {
133 sr_dbg("Closing PCM device.");
134 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
135 sr_err("Failed to close device: %s.",
137 devc->capture_handle = NULL;
140 sr_dbg("No capture handle, no need to close audio device.");
146 static int hw_cleanup(void)
153 static int hw_info_get(int info_id, const void **data,
154 const struct sr_dev_inst *sdi)
156 struct dev_context *devc;
158 if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
165 case SR_DI_NUM_PROBES:
166 *data = &devc->num_probes;
168 case SR_DI_PROBE_NAMES:
171 case SR_DI_CUR_SAMPLERATE:
172 *data = &devc->cur_samplerate;
175 sr_err("Invalid info_id: %d.", info_id);
182 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
185 struct dev_context *devc;
190 case SR_HWCAP_SAMPLERATE:
191 devc->cur_samplerate = *(const uint64_t *)value;
193 case SR_HWCAP_LIMIT_SAMPLES:
194 devc->limit_samples = *(const uint64_t *)value;
197 sr_err("Unknown capability: %d.", hwcap);
204 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
207 struct sr_datafeed_packet packet;
208 struct sr_datafeed_header header;
209 struct sr_datafeed_meta_analog meta;
210 struct dev_context *devc;
214 devc->cb_data = cb_data;
216 sr_dbg("Setting audio access type to RW/interleaved.");
217 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
218 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
220 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
224 /* FIXME: Hardcoded for 16bits. */
225 sr_dbg("Setting audio sample format to signed 16bit (little endian).");
226 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
227 devc->hw_params, SND_PCM_FORMAT_S16_LE);
229 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
233 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
234 devc->cur_samplerate);
235 ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
236 (unsigned int)devc->cur_samplerate, 0);
238 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
242 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
243 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
244 devc->hw_params, devc->num_probes);
246 sr_err("Can't set channel count: %s.", snd_strerror(ret));
250 sr_dbg("Setting audio parameters.");
251 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
253 sr_err("Can't set parameters: %s.", snd_strerror(ret));
257 sr_dbg("Preparing audio interface for use.");
258 ret = snd_pcm_prepare(devc->capture_handle);
260 sr_err("Can't prepare audio interface for use: %s.",
265 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
267 sr_err("Unable to obtain poll descriptors count.");
270 sr_spew("Obtained poll descriptor count: %d.", count);
272 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
273 sr_err("Failed to malloc ufds.");
274 return SR_ERR_MALLOC;
277 sr_err("Getting %d poll descriptors.", count);
278 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
280 sr_err("Unable to obtain poll descriptors: %s.",
286 /* Send header packet to the session bus. */
287 sr_dbg("Sending SR_DF_HEADER packet.");
288 packet.type = SR_DF_HEADER;
289 packet.payload = (uint8_t *)&header;
290 header.feed_version = 1;
291 gettimeofday(&header.starttime, NULL);
292 sr_session_send(devc->cb_data, &packet);
294 /* Send metadata about the SR_DF_ANALOG packets to come. */
295 sr_dbg("Sending SR_DF_META_ANALOG packet.");
296 packet.type = SR_DF_META_ANALOG;
297 packet.payload = &meta;
298 meta.num_probes = devc->num_probes;
299 sr_session_send(devc->cb_data, &packet);
301 /* Poll every 10ms, or whenever some data comes in. */
302 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
303 alsa_receive_data, (void *)sdi);
305 // g_free(devc->ufds); /* FIXME */
310 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
312 struct sr_datafeed_packet packet;
313 struct dev_context *devc;
316 devc->cb_data = cb_data;
318 sr_source_remove(devc->ufds[0].fd);
320 /* Send end packet to the session bus. */
321 sr_dbg("Sending SR_DF_END packet.");
322 packet.type = SR_DF_END;
323 sr_session_send(cb_data, &packet);
328 SR_PRIV struct sr_dev_driver alsa_driver_info = {
330 .longname = "ALSA driver",
333 .cleanup = hw_cleanup,
335 .dev_list = hw_dev_list,
336 .dev_clear = clear_instances,
337 .dev_open = hw_dev_open,
338 .dev_close = hw_dev_close,
339 .info_get = hw_info_get,
340 .dev_config_set = hw_dev_config_set,
341 .dev_acquisition_start = hw_dev_acquisition_start,
342 .dev_acquisition_stop = hw_dev_acquisition_stop,