2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <alsa/asoundlib.h>
29 #define SAMPLE_WIDTH 16
30 #define AUDIO_DEV "plughw:0,0"
32 static int capabilities[] = {
38 static GSList *device_instances = NULL;
42 uint64_t limit_samples;
43 snd_pcm_t *capture_handle;
44 snd_pcm_hw_params_t *hw_params;
48 static int hw_init(char *deviceinfo)
50 struct sigrok_device_instance *sdi;
53 /* Avoid compiler warnings. */
54 deviceinfo = deviceinfo;
56 alsa = malloc(sizeof(struct alsa));
59 memset(alsa, 0, sizeof(struct alsa));
61 sdi = sigrok_device_instance_new(0, ST_ACTIVE, "alsa", NULL, NULL);
67 device_instances = g_slist_append(device_instances, sdi);
75 static int hw_opendev(int device_index)
77 struct sigrok_device_instance *sdi;
81 if (!(sdi = get_sigrok_device_instance(device_instances, 0)))
85 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
86 SND_PCM_STREAM_CAPTURE, 0);
88 g_warning("cannot open audio device %s (%s)", AUDIO_DEV,
93 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
95 g_warning("cannot allocate hardware parameter structure (%s)",
100 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
102 g_warning("cannot initialize hardware parameter structure (%s)",
110 static void hw_closedev(int device_index)
112 struct sigrok_device_instance *sdi;
115 if (!(sdi = get_sigrok_device_instance(device_instances, 0)))
122 snd_pcm_hw_params_free(alsa->hw_params);
123 if (alsa->capture_handle)
124 snd_pcm_close(alsa->capture_handle);
127 static void hw_cleanup(void)
129 struct sigrok_device_instance *sdi;
131 if (!(sdi = get_sigrok_device_instance(device_instances, 0)))
135 sigrok_device_instance_free(sdi);
138 static void *hw_get_device_info(int device_index, int device_info_id)
140 struct sigrok_device_instance *sdi;
144 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
148 switch (device_info_id) {
153 info = GINT_TO_POINTER(NUM_PROBES);
155 case DI_CUR_SAMPLERATE:
156 info = &alsa->cur_rate;
159 info = GINT_TO_POINTER(PROBE_TYPE_ANALOG);
166 static int hw_get_status(int device_index)
168 /* Avoid compiler warnings. */
169 device_index = device_index;
174 static int *hw_get_capabilities(void)
179 static int hw_set_configuration(int device_index, int capability, void *value)
181 struct sigrok_device_instance *sdi;
184 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
188 switch (capability) {
189 case HWCAP_PROBECONFIG:
191 case HWCAP_SAMPLERATE:
192 alsa->cur_rate = *(uint64_t *) value;
194 case HWCAP_LIMIT_SAMPLES:
195 alsa->limit_samples = *(uint64_t *) value;
203 * FIXME: This is incomplete, i couldn't get poll() to work with alsa fds yet.
205 static int receive_data(int fd, int revents, void *user_data)
207 struct sigrok_device_instance *sdi = user_data;
208 struct alsa *alsa = sdi->priv;
209 struct datafeed_packet packet;
213 g_warning("entered receive_data");
215 for (i = 0; i < 10; i++) {
216 if ((err = snd_pcm_readi(alsa->capture_handle, buf, 128)) != 128) {
217 g_warning("read from audio interface failed (%s)\n",
221 g_warning("data read ok");
227 static int hw_start_acquisition(int device_index, gpointer session_device_id)
229 struct sigrok_device_instance *sdi;
231 struct datafeed_packet packet;
232 struct datafeed_header header;
237 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
241 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
242 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
244 g_warning("cannot set access type (%s)", snd_strerror(err));
248 /* FIXME: Hardcoded for 16bits */
249 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
250 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
252 g_warning("cannot set sample format (%s)", snd_strerror(err));
256 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
257 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
259 g_warning("cannot set sample rate (%s)", snd_strerror(err));
263 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
264 alsa->hw_params, NUM_PROBES);
266 g_warning("cannot set channel count (%s)", snd_strerror(err));
270 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
272 g_warning("cannot set parameters (%s)", snd_strerror(err));
276 err = snd_pcm_prepare(alsa->capture_handle);
278 g_warning("cannot prepare audio interface for use (%s)",
283 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
285 g_warning("Unable to obtain poll descriptors count");
289 ufds = malloc(count * sizeof(struct pollfd));
291 return SIGROK_ERR_MALLOC;
293 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
295 g_warning("Unable to obtain poll descriptors (%s)",
301 alsa->session_id = session_device_id;
302 source_add(ufds[0].fd, ufds[0].revents, -1, receive_data, sdi);
304 packet.type = DF_HEADER;
305 packet.length = sizeof(struct datafeed_header);
306 packet.payload = (unsigned char *) &header;
307 header.feed_version = 1;
308 gettimeofday(&header.starttime, NULL);
309 header.samplerate = alsa->cur_rate;
310 header.num_analog_probes = NUM_PROBES;
311 header.num_logic_probes = 0;
312 header.protocol_id = PROTO_RAW;
313 session_bus(session_device_id, &packet);
319 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
321 /* Avoid compiler warnings. */
322 device_index = device_index;
323 session_device_id = session_device_id;
326 struct device_plugin alsa_plugin_info = {
336 hw_set_configuration,
337 hw_start_acquisition,