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