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