]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/api.c
alsa: Scan all soundcards and create a sigrok device per input
[libsigrok.git] / hardware / alsa / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
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>
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "protocol.h"
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #define DEFAULT_PROBES          2
32 #define SAMPLE_WIDTH            16
33 #define DEFAULT_SAMPLERATE      44100
34
35 static const int hwcaps[] = {
36         SR_HWCAP_SAMPLERATE,
37         SR_HWCAP_LIMIT_SAMPLES,
38         SR_HWCAP_CONTINUOUS,
39         0,
40 };
41
42 static const char *probe_names[] = {
43         "Channel 0", "Channel 1",
44         NULL,
45 };
46
47 SR_PRIV struct sr_dev_driver alsa_driver_info;
48 static struct sr_dev_driver *di = &alsa_driver_info;
49
50 static int clear_instances(void)
51 {
52         struct drv_context *drvc;
53
54         if (!(drvc = di->priv))
55                 return SR_OK;
56
57         g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
58         drvc->instances = NULL;
59
60         return SR_OK;
61 }
62
63 static int hw_init(struct sr_context *sr_ctx)
64 {
65         struct drv_context *drvc;
66
67         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
68                 sr_err("Driver context malloc failed.");
69                 return SR_ERR_MALLOC;
70         }
71
72         drvc->sr_ctx = sr_ctx;
73         di->priv = drvc;
74
75         return SR_OK;
76 }
77
78 static GSList *hw_scan(GSList *options)
79 {
80         return alsa_scan(options, di);
81 }
82
83 static GSList *hw_dev_list(void)
84 {
85         struct drv_context *drvc;
86
87         drvc = di->priv;
88
89         return drvc->instances;
90 }
91
92 static int hw_dev_open(struct sr_dev_inst *sdi)
93 {
94         struct dev_context *devc;
95         int ret;
96
97         devc = sdi->priv;
98
99         if (!(devc->hwdev)) {
100                 sr_err("devc->hwdev was NULL.");
101                 return SR_ERR_BUG;
102         }
103
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);
107         if (ret < 0) {
108                 sr_err("Can't open audio device: %s.", snd_strerror(ret));
109                 return SR_ERR;
110         }
111
112         sr_dbg("Initializing hardware parameter structure.");
113         ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
114         if (ret < 0) {
115                 sr_err("Can't initialize hardware parameter structure: %s.",
116                        snd_strerror(ret));
117                 return SR_ERR;
118         }
119
120         return SR_OK;
121 }
122
123 static int hw_dev_close(struct sr_dev_inst *sdi)
124 {
125         int ret;
126         struct dev_context *devc;
127
128         devc = sdi->priv;
129
130         sr_dbg("Closing device.");
131
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.",
136                                snd_strerror(ret));
137                         devc->capture_handle = NULL;
138                 }
139         } else {
140                 sr_dbg("No capture handle, no need to close audio device.");
141         }
142
143         return SR_OK;
144 }
145
146 static int hw_cleanup(void)
147 {
148         clear_instances();
149
150         return SR_OK;
151 }
152
153 static int hw_info_get(int info_id, const void **data,
154                        const struct sr_dev_inst *sdi)
155 {
156         struct dev_context *devc;
157
158         if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
159                 devc = sdi->priv;
160
161         switch (info_id) {
162         case SR_DI_HWCAPS:
163                 *data = hwcaps;
164                 break;
165         case SR_DI_NUM_PROBES:
166                 *data = &devc->num_probes;
167                 break;
168         case SR_DI_PROBE_NAMES:
169                 *data = probe_names;
170                 break;
171         case SR_DI_CUR_SAMPLERATE:
172                 *data = &devc->cur_samplerate;
173                 break;
174         default:
175                 sr_err("Invalid info_id: %d.", info_id);
176                 return SR_ERR_ARG;
177         }
178
179         return SR_OK;
180 }
181
182 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
183                              const void *value)
184 {
185         struct dev_context *devc;
186
187         devc = sdi->priv;
188
189         switch (hwcap) {
190         case SR_HWCAP_SAMPLERATE:
191                 devc->cur_samplerate = *(const uint64_t *)value;
192                 break;
193         case SR_HWCAP_LIMIT_SAMPLES:
194                 devc->limit_samples = *(const uint64_t *)value;
195                 break;
196         default:
197                 sr_err("Unknown capability: %d.", hwcap);
198                 return SR_ERR;
199         }
200
201         return SR_OK;
202 }
203
204 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
205                                     void *cb_data)
206 {
207         struct sr_datafeed_packet packet;
208         struct sr_datafeed_header header;
209         struct sr_datafeed_meta_analog meta;
210         struct dev_context *devc;
211         int count, ret;
212
213         devc = sdi->priv;
214         devc->cb_data = cb_data;
215
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);
219         if (ret < 0) {
220                 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
221                 return SR_ERR;
222         }
223
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);
228         if (ret < 0) {
229                 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
230                 return SR_ERR;
231         }
232
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);
237         if (ret < 0) {
238                 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
239                 return SR_ERR;
240         }
241
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);
245         if (ret < 0) {
246                 sr_err("Can't set channel count: %s.", snd_strerror(ret));
247                 return SR_ERR;
248         }
249
250         sr_dbg("Setting audio parameters.");
251         ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
252         if (ret < 0) {
253                 sr_err("Can't set parameters: %s.", snd_strerror(ret));
254                 return SR_ERR;
255         }
256
257         sr_dbg("Preparing audio interface for use.");
258         ret = snd_pcm_prepare(devc->capture_handle);
259         if (ret < 0) {
260                 sr_err("Can't prepare audio interface for use: %s.",
261                        snd_strerror(ret));
262                 return SR_ERR;
263         }
264
265         count = snd_pcm_poll_descriptors_count(devc->capture_handle);
266         if (count < 1) {
267                 sr_err("Unable to obtain poll descriptors count.");
268                 return SR_ERR;
269         }
270         sr_spew("Obtained poll descriptor count: %d.", count);
271
272         if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
273                 sr_err("Failed to malloc ufds.");
274                 return SR_ERR_MALLOC;
275         }
276
277         sr_err("Getting %d poll descriptors.", count);
278         ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
279         if (ret < 0) {
280                 sr_err("Unable to obtain poll descriptors: %s.",
281                        snd_strerror(ret));
282                 g_free(devc->ufds);
283                 return SR_ERR;
284         }
285
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);
293
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);
300
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);
304
305         // g_free(devc->ufds); /* FIXME */
306
307         return SR_OK;
308 }
309
310 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
311 {
312         struct sr_datafeed_packet packet;
313         struct dev_context *devc;
314
315         devc = sdi->priv;
316         devc->cb_data = cb_data;
317
318         sr_source_remove(devc->ufds[0].fd);
319
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);
324
325         return SR_OK;
326 }
327
328 SR_PRIV struct sr_dev_driver alsa_driver_info = {
329         .name = "alsa",
330         .longname = "ALSA driver",
331         .api_version = 1,
332         .init = hw_init,
333         .cleanup = hw_cleanup,
334         .scan = hw_scan,
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,
343         .priv = NULL,
344 };