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"
27 static void alsa_scan_handle_dev(GSList **devices,
28 const char *cardname, const char *alsaname,
29 struct sr_dev_driver *di,
30 snd_pcm_info_t *pcminfo)
32 struct drv_context *drvc = NULL;
33 struct sr_dev_inst *sdi = NULL;
34 struct dev_context *devc = NULL;
35 struct sr_probe *probe;
37 unsigned int i, channels;
38 snd_pcm_t *temp_handle = NULL;
39 snd_pcm_hw_params_t *hw_params = NULL;
44 * Get the number of input channels. Those are our sigrok probes.
45 * Getting this information needs a detour. We need to open the device,
46 * then query it for the number of channels. A side-effect of is that we
47 * create a snd_pcm_hw_params_t object. We take advantage of the
48 * situation, and pass this object in our dev_context->hw_params,
49 * eliminating the need to free() it and malloc() it later.
51 ret = snd_pcm_open(&temp_handle, alsaname, SND_PCM_STREAM_CAPTURE, 0);
53 sr_err("Cannot open device: %s.", snd_strerror(ret));
54 goto scan_error_cleanup;
57 ret = snd_pcm_hw_params_malloc(&hw_params);
59 sr_err("Error allocating hardware parameter structure: %s.",
61 goto scan_error_cleanup;
64 ret = snd_pcm_hw_params_any(temp_handle, hw_params);
66 sr_err("Error initializing hardware parameter structure: %s.",
68 goto scan_error_cleanup;
71 snd_pcm_hw_params_get_channels_max(hw_params, &channels);
73 snd_pcm_close(temp_handle);
77 * Now we are done querying the number of channels
78 * If we made it here, then it's time to create our sigrok device.
80 sr_info("Device %s has %d channels.", alsaname, channels);
81 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "ALSA:",
82 cardname, snd_pcm_info_get_name(pcminfo)))) {
83 sr_err("Device instance malloc failed.");
84 goto scan_error_cleanup;
86 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
87 sr_err("Device context malloc failed.");
88 goto scan_error_cleanup;
91 devc->hwdev = g_strdup(alsaname);
92 devc->num_probes = channels;
93 devc->hw_params = hw_params;
97 for (i = 0; i < devc->num_probes; i++) {
99 snprintf(p_name, sizeof(p_name), "Ch_%d", i);
100 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, p_name)))
101 goto scan_error_cleanup;
102 sdi->probes = g_slist_append(sdi->probes, probe);
105 drvc->instances = g_slist_append(drvc->instances, sdi);
106 *devices = g_slist_append(*devices, sdi);
112 g_free((void*)devc->hwdev);
116 sr_dev_inst_free(sdi);
118 snd_pcm_hw_params_free(hw_params);
120 snd_pcm_close(temp_handle);
125 * \brief Scan all alsa devices, and translate them to sigrok devices
127 * Each alsa device (not alsa card) gets its own sigrok device
129 * hw:1,0 == sigrok device 0
130 * hw:1,1 == sigrok device 1
131 * hw:2,0 == sigrok device 2
132 * hw:2,1 == sigrok device 3
133 * hw:2,2 == sigrok device 4
136 * We don't currently look at alsa subdevices. We only use subdevice 0.
137 * Every input device will have a its own channels (Left, Right, etc). Each of
138 * those channels gets mapped to a different sigrok probe. A device with 4
139 * channels will have 4 probes from sigrok's perspective.
141 SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
143 GSList *devices = NULL;
146 snd_ctl_card_info_t *info;
147 snd_pcm_info_t *pcminfo;
148 const char* cardname;
152 if (snd_ctl_card_info_malloc(&info) < 0) {
153 sr_err("Cannot malloc card info.");
156 if (snd_pcm_info_malloc(&pcminfo) < 0) {
157 sr_err("Cannot malloc pcm info.");
162 while (snd_card_next(&card) >= 0 && card >= 0) {
164 snprintf(hwcard, sizeof(hwcard), "hw:%d", card);
165 if ((ret = snd_ctl_open(&handle, hwcard, 0)) < 0) {
166 sr_err("Cannot open (%i): %s", card, snd_strerror(ret));
169 if ((ret = snd_ctl_card_info(handle, info)) < 0) {
170 sr_err("Cannot get hardware info (%i): %s",
171 card, snd_strerror(ret));
172 snd_ctl_close(handle);
176 while (snd_ctl_pcm_next_device(handle, &dev) >= 0 && dev >= 0) {
178 snprintf(hwdev, sizeof(hwdev), "%s,%d", hwcard, dev);
180 * TODO: We always use subdevice 0, but we have yet to
181 * explore the possibilities opened up by other
182 * subdevices. Most hardware only has subdevice 0.
184 snd_pcm_info_set_device(pcminfo, dev);
185 snd_pcm_info_set_subdevice(pcminfo, 0);
186 snd_pcm_info_set_stream(pcminfo,
187 SND_PCM_STREAM_CAPTURE);
188 if ((ret = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
189 sr_err("Cannot get device info: %s",
194 cardname = snd_ctl_card_info_get_name(info);
195 sr_info("card %i: %s [%s], device %i: %s [%s]",
196 card, snd_ctl_card_info_get_id(info), cardname,
197 dev, snd_pcm_info_get_id(pcminfo),
198 snd_pcm_info_get_name(pcminfo));
200 alsa_scan_handle_dev(&devices, cardname, hwdev,
203 snd_ctl_close(handle);
206 snd_pcm_info_free(pcminfo);
207 snd_ctl_card_info_free(info);
213 * Helper to be used with g_slist_free_full(); for properly freeing an alsa
216 SR_PRIV void alsa_dev_inst_clear(struct sr_dev_inst *sdi)
218 struct dev_context *devc;
220 if (!(devc = sdi->priv))
223 snd_pcm_hw_params_free(devc->hw_params);
224 sr_dev_inst_free(sdi);
227 SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
229 struct sr_dev_inst *sdi;
230 struct dev_context *devc;
231 struct sr_datafeed_packet packet;
232 struct sr_datafeed_analog analog;
234 int i, x, count, offset, samples_to_get;
243 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
244 memset(inbuf, 0, sizeof(inbuf));
246 samples_to_get = MIN(4096 / 4, devc->limit_samples);
248 sr_spew("Getting %d samples from audio device.", samples_to_get);
249 count = snd_pcm_readi(devc->capture_handle, inbuf, samples_to_get);
252 sr_err("Failed to read samples: %s.", snd_strerror(count));
254 } else if (count != samples_to_get) {
255 sr_spew("Only got %d/%d samples.", count, samples_to_get);
258 analog.data = g_try_malloc0(count * sizeof(float) * devc->num_probes);
260 sr_err("Failed to malloc sample buffer.");
266 for (i = 0; i < count; i++) {
267 for (x = 0; x < devc->num_probes; x++) {
268 tmp16 = *(uint16_t *)(inbuf + (i * 4) + (x * 2));
269 analog.data[offset++] = (float)tmp16;
273 /* Send a sample packet with the analog values. */
274 analog.num_samples = count;
275 analog.mq = SR_MQ_VOLTAGE; /* FIXME */
276 analog.unit = SR_UNIT_VOLT; /* FIXME */
277 packet.type = SR_DF_ANALOG;
278 packet.payload = &analog;
279 sr_session_send(devc->cb_data, &packet);
283 devc->num_samples += count;
285 /* Stop acquisition if we acquired enough samples. */
286 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
287 sr_info("Requested number of samples reached.");
288 sdi->driver->dev_acquisition_stop(sdi, cb_data);