]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/api.c
alsa: Improved error reporting.
[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 <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 static const int hwcaps[] = {
31         SR_HWCAP_SAMPLERATE,
32         SR_HWCAP_LIMIT_SAMPLES,
33         SR_HWCAP_CONTINUOUS,
34         0,
35 };
36
37 static const char *probe_names[] = {
38         "Channel 0", "Channel 1",
39         NULL,
40 };
41
42 SR_PRIV struct sr_dev_driver alsa_driver_info;
43 static struct sr_dev_driver *di = &alsa_driver_info;
44
45 static int clear_instances(void)
46 {
47         struct drv_context *drvc;
48
49         if (!(drvc = di->priv))
50                 return SR_OK;
51
52         g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
53         drvc->instances = NULL;
54
55         return SR_OK;
56 }
57
58 static int hw_init(struct sr_context *sr_ctx)
59 {
60         struct drv_context *drvc;
61
62         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
63                 sr_err("Driver context malloc failed.");
64                 return SR_ERR_MALLOC;
65         }
66
67         drvc->sr_ctx = sr_ctx;
68         di->priv = drvc;
69
70         return SR_OK;
71 }
72
73 static GSList *hw_scan(GSList *options)
74 {
75         return alsa_scan(options, di);
76 }
77
78 static GSList *hw_dev_list(void)
79 {
80         struct drv_context *drvc;
81
82         drvc = di->priv;
83
84         return drvc->instances;
85 }
86
87 static int hw_dev_open(struct sr_dev_inst *sdi)
88 {
89         struct dev_context *devc;
90         int ret;
91
92         devc = sdi->priv;
93
94         if (!(devc->hwdev)) {
95                 sr_err("devc->hwdev was NULL.");
96                 return SR_ERR_BUG;
97         }
98
99         sr_dbg("Opening audio device '%s' for stream capture.", devc->hwdev);
100         ret = snd_pcm_open(&devc->capture_handle, devc->hwdev,
101                            SND_PCM_STREAM_CAPTURE, 0);
102         if (ret < 0) {
103                 sr_err("Can't open audio device: %s.", snd_strerror(ret));
104                 return SR_ERR;
105         }
106
107         sr_dbg("Initializing hardware parameter structure.");
108         ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
109         if (ret < 0) {
110                 sr_err("Can't initialize hardware parameter structure: %s.",
111                        snd_strerror(ret));
112                 return SR_ERR;
113         }
114
115         return SR_OK;
116 }
117
118 static int hw_dev_close(struct sr_dev_inst *sdi)
119 {
120         int ret;
121         struct dev_context *devc;
122
123         devc = sdi->priv;
124
125         sr_dbg("Closing device.");
126
127         if (devc->capture_handle) {
128                 sr_dbg("Closing PCM device.");
129                 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
130                         sr_err("Failed to close device: %s.",
131                                snd_strerror(ret));
132                         devc->capture_handle = NULL;
133                 }
134         } else {
135                 sr_dbg("No capture handle, no need to close audio device.");
136         }
137
138         return SR_OK;
139 }
140
141 static int hw_cleanup(void)
142 {
143         clear_instances();
144
145         return SR_OK;
146 }
147
148 static int hw_info_get(int info_id, const void **data,
149                        const struct sr_dev_inst *sdi)
150 {
151         struct dev_context *devc;
152
153         if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
154                 devc = sdi->priv;
155
156         switch (info_id) {
157         case SR_DI_HWCAPS:
158                 *data = hwcaps;
159                 break;
160         case SR_DI_NUM_PROBES:
161                 *data = &devc->num_probes;
162                 break;
163         case SR_DI_PROBE_NAMES:
164                 *data = probe_names;
165                 break;
166         case SR_DI_CUR_SAMPLERATE:
167                 *data = &devc->cur_samplerate;
168                 break;
169         case SR_DI_SAMPLERATES:
170                 if (!devc->supp_rates.list) {
171                         sr_err("Instance did not contain a samplerate list.");
172                         return SR_ERR_ARG;
173                 }
174                 *data = &devc->supp_rates;
175                 break;
176         default:
177                 sr_err("Invalid info_id: %d.", info_id);
178                 return SR_ERR_ARG;
179         }
180
181         return SR_OK;
182 }
183
184 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
185                              const void *value)
186 {
187         struct dev_context *devc;
188
189         devc = sdi->priv;
190
191         switch (hwcap) {
192         case SR_HWCAP_SAMPLERATE:
193                 alsa_set_samplerate(sdi, *(const uint64_t *)value);
194                 break;
195         case SR_HWCAP_LIMIT_SAMPLES:
196                 devc->limit_samples = *(const uint64_t *)value;
197                 break;
198         default:
199                 sr_err("Unknown capability: %d.", hwcap);
200                 return SR_ERR;
201         }
202
203         return SR_OK;
204 }
205
206 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
207                                     void *cb_data)
208 {
209         struct sr_datafeed_packet packet;
210         struct sr_datafeed_header header;
211         struct sr_datafeed_meta_analog meta;
212         struct dev_context *devc;
213         int count, ret;
214         char *endianness;
215
216         devc = sdi->priv;
217         devc->cb_data = cb_data;
218         devc->num_samples = 0;
219
220         sr_dbg("Setting audio access type to RW/interleaved.");
221         ret = snd_pcm_hw_params_set_access(devc->capture_handle,
222                         devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
223         if (ret < 0) {
224                 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
225                 return SR_ERR;
226         }
227
228         /* FIXME: Hardcoded for 16bits. */
229         if (SND_PCM_FORMAT_S16 == SND_PCM_FORMAT_S16_LE)
230                 endianness = "little endian";
231         else
232                 endianness = "big endian";
233         sr_dbg("Setting audio sample format to signed 16bit (%s).", endianness);
234         ret = snd_pcm_hw_params_set_format(devc->capture_handle,
235                                            devc->hw_params,
236                                            SND_PCM_FORMAT_S16);
237         if (ret < 0) {
238                 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
239                 return SR_ERR;
240         }
241
242         sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
243                devc->cur_samplerate);
244         ret = snd_pcm_hw_params_set_rate(devc->capture_handle, devc->hw_params,
245                                          (unsigned int)devc->cur_samplerate, 0);
246         if (ret < 0) {
247                 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
248                 return SR_ERR;
249         }
250
251         sr_dbg("Setting audio channel count to %d.", devc->num_probes);
252         ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
253                                              devc->hw_params, devc->num_probes);
254         if (ret < 0) {
255                 sr_err("Can't set channel count: %s.", snd_strerror(ret));
256                 return SR_ERR;
257         }
258
259         sr_dbg("Setting audio parameters.");
260         ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
261         if (ret < 0) {
262                 sr_err("Can't set parameters: %s.", snd_strerror(ret));
263                 return SR_ERR;
264         }
265
266         sr_dbg("Preparing audio interface for use.");
267         ret = snd_pcm_prepare(devc->capture_handle);
268         if (ret < 0) {
269                 sr_err("Can't prepare audio interface for use: %s.",
270                        snd_strerror(ret));
271                 return SR_ERR;
272         }
273
274         count = snd_pcm_poll_descriptors_count(devc->capture_handle);
275         if (count < 1) {
276                 sr_err("Unable to obtain poll descriptors count.");
277                 return SR_ERR;
278         }
279
280         if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
281                 sr_err("Failed to malloc ufds.");
282                 return SR_ERR_MALLOC;
283         }
284
285         sr_err("Getting %d poll descriptors.", count);
286         ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
287         if (ret < 0) {
288                 sr_err("Unable to obtain poll descriptors: %s.",
289                        snd_strerror(ret));
290                 g_free(devc->ufds);
291                 return SR_ERR;
292         }
293
294         /* Send header packet to the session bus. */
295         sr_dbg("Sending SR_DF_HEADER packet.");
296         packet.type = SR_DF_HEADER;
297         packet.payload = (uint8_t *)&header;
298         header.feed_version = 1;
299         gettimeofday(&header.starttime, NULL);
300         sr_session_send(devc->cb_data, &packet);
301
302         /* Send metadata about the SR_DF_ANALOG packets to come. */
303         sr_dbg("Sending SR_DF_META_ANALOG packet.");
304         packet.type = SR_DF_META_ANALOG;
305         packet.payload = &meta;
306         meta.num_probes = devc->num_probes;
307         sr_session_send(devc->cb_data, &packet);
308
309         /* Poll every 10ms, or whenever some data comes in. */
310         sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
311                       alsa_receive_data, (void *)sdi);
312
313         // g_free(devc->ufds); /* FIXME */
314
315         return SR_OK;
316 }
317
318 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
319 {
320         struct sr_datafeed_packet packet;
321         struct dev_context *devc;
322
323         devc = sdi->priv;
324         devc->cb_data = cb_data;
325
326         sr_source_remove(devc->ufds[0].fd);
327
328         /* Send end packet to the session bus. */
329         sr_dbg("Sending SR_DF_END packet.");
330         packet.type = SR_DF_END;
331         sr_session_send(cb_data, &packet);
332
333         return SR_OK;
334 }
335
336 SR_PRIV struct sr_dev_driver alsa_driver_info = {
337         .name = "alsa",
338         .longname = "ALSA driver",
339         .api_version = 1,
340         .init = hw_init,
341         .cleanup = hw_cleanup,
342         .scan = hw_scan,
343         .dev_list = hw_dev_list,
344         .dev_clear = clear_instances,
345         .dev_open = hw_dev_open,
346         .dev_close = hw_dev_close,
347         .info_get = hw_info_get,
348         .dev_config_set = hw_dev_config_set,
349         .dev_acquisition_start = hw_dev_acquisition_start,
350         .dev_acquisition_stop = hw_dev_acquisition_stop,
351         .priv = NULL,
352 };