]> sigrok.org Git - libsigrok.git/blame - hardware/alsa/api.c
rs9lcd: Add missing 'break;'
[libsigrok.git] / hardware / alsa / api.c
CommitLineData
6ed4f044
DR
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
0254651d 5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6ed4f044
DR
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
9cd9f6b7 22#include "protocol.h"
45c59c8b
BV
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
6ed4f044 25
9cd9f6b7
AG
26#include <stdlib.h>
27#include <unistd.h>
28#include <string.h>
472bbb46 29
9cd9f6b7 30#define DEFAULT_PROBES 2
0254651d
UH
31#define SAMPLE_WIDTH 16
32#define DEFAULT_SAMPLERATE 44100
33// #define AUDIO_DEV "plughw:0,0"
34#define AUDIO_DEV "default"
a4cfb10f 35
915f7cc8 36static const int hwcaps[] = {
5a2326a7
UH
37 SR_HWCAP_SAMPLERATE,
38 SR_HWCAP_LIMIT_SAMPLES,
39 SR_HWCAP_CONTINUOUS,
0254651d 40 0,
6ed4f044
DR
41};
42
0254651d 43static const char *probe_names[] = {
78693401 44 "Channel 0", "Channel 1",
464d12c7
KS
45 NULL,
46};
47
0254651d
UH
48SR_PRIV struct sr_dev_driver alsa_driver_info;
49static struct sr_dev_driver *di = &alsa_driver_info;
6ed4f044 50
0254651d
UH
51static int clear_instances(void)
52{
53 /* TODO */
54
55 return SR_OK;
56}
57
34f06b90 58static int hw_init(struct sr_context *sr_ctx)
6ed4f044 59{
0254651d 60 struct drv_context *drvc;
6ed4f044 61
0254651d
UH
62 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
63 sr_err("Driver context malloc failed.");
886a52b6 64 return SR_ERR_MALLOC;
92b3101c 65 }
6ed4f044 66
0254651d
UH
67 drvc->sr_ctx = sr_ctx;
68 di->priv = drvc;
69
70 return SR_OK;
71}
72
73static GSList *hw_scan(GSList *options)
74{
75 struct drv_context *drvc;
76 struct dev_context *devc;
77 struct sr_dev_inst *sdi;
78 struct sr_probe *probe;
79 GSList *devices;
80 int i;
81
82 (void)options;
83
84 drvc = di->priv;
85 drvc->instances = NULL;
86
87 devices = NULL;
88
89 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
90 sr_err("Device context malloc failed.");
91 return NULL;
92 }
93
ea9cfed7 94 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
0254651d
UH
95 sr_err("Failed to create device instance.");
96 return NULL;
ea9cfed7 97 }
6ed4f044 98
0254651d
UH
99 /* Set the samplerate to a default value for now. */
100 devc->cur_samplerate = DEFAULT_SAMPLERATE;
9cd9f6b7 101 devc->num_probes = DEFAULT_PROBES;
0254651d
UH
102
103 sdi->priv = devc;
104 sdi->driver = di;
6ed4f044 105
0254651d
UH
106 for (i = 0; probe_names[i]; i++) {
107 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
108 probe_names[i]))) {
109 sr_err("Failed to create probe.");
110 return NULL;
111 }
112 sdi->probes = g_slist_append(sdi->probes, probe);
113 }
6ed4f044 114
0254651d
UH
115 drvc->instances = g_slist_append(drvc->instances, sdi);
116 devices = g_slist_append(devices, sdi);
ffedd0bf 117
0254651d 118 return devices;
6ed4f044
DR
119}
120
0254651d 121static GSList *hw_dev_list(void)
6ed4f044 122{
0254651d
UH
123 struct drv_context *drvc;
124
125 drvc = di->priv;
126
127 return drvc->instances;
128}
129
130static int hw_dev_open(struct sr_dev_inst *sdi)
131{
132 struct dev_context *devc;
ebc34738 133 int ret;
6ed4f044 134
0254651d 135 devc = sdi->priv;
6ed4f044 136
0254651d
UH
137 sr_dbg("Opening audio device '%s' for stream capture.", AUDIO_DEV);
138 ret = snd_pcm_open(&devc->capture_handle, AUDIO_DEV,
ea9cfed7 139 SND_PCM_STREAM_CAPTURE, 0);
ebc34738 140 if (ret < 0) {
0254651d 141 sr_err("Can't open audio device: %s.", snd_strerror(ret));
e46b8fb1 142 return SR_ERR;
6ed4f044
DR
143 }
144
0254651d
UH
145 sr_dbg("Allocating hardware parameter structure.");
146 ret = snd_pcm_hw_params_malloc(&devc->hw_params);
ebc34738 147 if (ret < 0) {
0254651d 148 sr_err("Can't allocate hardware parameter structure: %s.",
ebc34738 149 snd_strerror(ret));
886a52b6 150 return SR_ERR_MALLOC;
6ed4f044
DR
151 }
152
0254651d
UH
153 sr_dbg("Initializing hardware parameter structure.");
154 ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
ebc34738 155 if (ret < 0) {
0254651d 156 sr_err("Can't initialize hardware parameter structure: %s.",
472bbb46 157 snd_strerror(ret));
e46b8fb1 158 return SR_ERR;
6ed4f044
DR
159 }
160
e46b8fb1 161 return SR_OK;
6ed4f044
DR
162}
163
0254651d 164static int hw_dev_close(struct sr_dev_inst *sdi)
6ed4f044 165{
0254651d
UH
166 int ret;
167 struct dev_context *devc;
6ed4f044 168
0254651d
UH
169 devc = sdi->priv;
170
171 sr_dbg("Closing device.");
6ed4f044 172
0254651d
UH
173 if (devc->hw_params) {
174 sr_dbg("Freeing hardware parameters.");
175 snd_pcm_hw_params_free(devc->hw_params);
176 } else {
177 sr_dbg("No hardware parameters, no need to free.");
697785d1
UH
178 }
179
0254651d
UH
180 if (devc->capture_handle) {
181 sr_dbg("Closing PCM device.");
182 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
183 sr_err("Failed to close device: %s.",
184 snd_strerror(ret));
185 }
186 } else {
187 sr_dbg("No capture handle, no need to close audio device.");
188 }
697785d1
UH
189
190 return SR_OK;
6ed4f044
DR
191}
192
57ab7d9f 193static int hw_cleanup(void)
6ed4f044 194{
0254651d 195 clear_instances();
57ab7d9f
UH
196
197 return SR_OK;
6ed4f044
DR
198}
199
0254651d
UH
200static int hw_info_get(int info_id, const void **data,
201 const struct sr_dev_inst *sdi)
6ed4f044 202{
0254651d 203 struct dev_context *devc;
6ed4f044 204
0254651d
UH
205 if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
206 devc = sdi->priv;
6ed4f044 207
0254651d
UH
208 switch (info_id) {
209 case SR_DI_HWCAPS:
210 *data = hwcaps;
6ed4f044 211 break;
5a2326a7 212 case SR_DI_NUM_PROBES:
9cd9f6b7 213 *data = GINT_TO_POINTER(DEFAULT_PROBES);
6ed4f044 214 break;
464d12c7 215 case SR_DI_PROBE_NAMES:
0254651d 216 *data = probe_names;
464d12c7 217 break;
5a2326a7 218 case SR_DI_CUR_SAMPLERATE:
0254651d 219 *data = &devc->cur_samplerate;
6ed4f044 220 break;
0254651d
UH
221 default:
222 sr_err("Invalid info_id: %d.", info_id);
223 return SR_ERR_ARG;
6ed4f044
DR
224 }
225
0254651d 226 return SR_OK;
6ed4f044
DR
227}
228
0254651d
UH
229static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
230 const void *value)
6ed4f044 231{
0254651d 232 struct dev_context *devc;
6ed4f044 233
0254651d 234 devc = sdi->priv;
6ed4f044 235
ffedd0bf 236 switch (hwcap) {
5a2326a7 237 case SR_HWCAP_SAMPLERATE:
0254651d
UH
238 devc->cur_samplerate = *(const uint64_t *)value;
239 break;
5a2326a7 240 case SR_HWCAP_LIMIT_SAMPLES:
0254651d
UH
241 devc->limit_samples = *(const uint64_t *)value;
242 break;
6ed4f044 243 default:
0254651d 244 sr_err("Unknown capability: %d.", hwcap);
e46b8fb1 245 return SR_ERR;
6ed4f044 246 }
0254651d
UH
247
248 return SR_OK;
6ed4f044
DR
249}
250
0254651d
UH
251static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
252 void *cb_data)
6ed4f044 253{
b9c735a2
UH
254 struct sr_datafeed_packet packet;
255 struct sr_datafeed_header header;
0254651d
UH
256 struct sr_datafeed_meta_analog meta;
257 struct dev_context *devc;
258 int count, ret;
6ed4f044 259
0254651d
UH
260 devc = sdi->priv;
261 devc->cb_data = cb_data;
6ed4f044 262
0254651d
UH
263 sr_dbg("Setting audio access type to RW/interleaved.");
264 ret = snd_pcm_hw_params_set_access(devc->capture_handle,
265 devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
ebc34738 266 if (ret < 0) {
0254651d 267 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
e46b8fb1 268 return SR_ERR;
6ed4f044
DR
269 }
270
0254651d
UH
271 /* FIXME: Hardcoded for 16bits. */
272 sr_dbg("Setting audio sample format to signed 16bit (little endian).");
273 ret = snd_pcm_hw_params_set_format(devc->capture_handle,
274 devc->hw_params, SND_PCM_FORMAT_S16_LE);
ebc34738 275 if (ret < 0) {
0254651d 276 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
e46b8fb1 277 return SR_ERR;
6ed4f044
DR
278 }
279
0254651d
UH
280 sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
281 devc->cur_samplerate);
282 ret = snd_pcm_hw_params_set_rate_near(devc->capture_handle,
283 devc->hw_params, (unsigned int *)&devc->cur_samplerate, 0);
ebc34738 284 if (ret < 0) {
0254651d 285 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
e46b8fb1 286 return SR_ERR;
6ed4f044
DR
287 }
288
9cd9f6b7 289 sr_dbg("Setting audio channel count to %d.", devc->num_probes);
0254651d 290 ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
9cd9f6b7 291 devc->hw_params, devc->num_probes);
ebc34738 292 if (ret < 0) {
0254651d 293 sr_err("Can't set channel count: %s.", snd_strerror(ret));
e46b8fb1 294 return SR_ERR;
6ed4f044
DR
295 }
296
0254651d
UH
297 sr_dbg("Setting audio parameters.");
298 ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
ebc34738 299 if (ret < 0) {
0254651d 300 sr_err("Can't set parameters: %s.", snd_strerror(ret));
e46b8fb1 301 return SR_ERR;
6ed4f044
DR
302 }
303
0254651d
UH
304 sr_dbg("Preparing audio interface for use.");
305 ret = snd_pcm_prepare(devc->capture_handle);
ebc34738 306 if (ret < 0) {
0254651d 307 sr_err("Can't prepare audio interface for use: %s.",
ebc34738 308 snd_strerror(ret));
e46b8fb1 309 return SR_ERR;
6ed4f044
DR
310 }
311
0254651d 312 count = snd_pcm_poll_descriptors_count(devc->capture_handle);
6ed4f044 313 if (count < 1) {
472bbb46 314 sr_err("Unable to obtain poll descriptors count.");
e46b8fb1 315 return SR_ERR;
6ed4f044 316 }
0254651d 317 sr_spew("Obtained poll descriptor count: %d.", count);
6ed4f044 318
0254651d
UH
319 if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
320 sr_err("Failed to malloc ufds.");
e46b8fb1 321 return SR_ERR_MALLOC;
92b3101c 322 }
6ed4f044 323
0254651d
UH
324 sr_err("Getting %d poll descriptors.", count);
325 ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
ebc34738 326 if (ret < 0) {
0254651d 327 sr_err("Unable to obtain poll descriptors: %s.",
ebc34738 328 snd_strerror(ret));
0254651d 329 g_free(devc->ufds);
e46b8fb1 330 return SR_ERR;
6ed4f044
DR
331 }
332
0254651d
UH
333 /* Send header packet to the session bus. */
334 sr_dbg("Sending SR_DF_HEADER packet.");
5a2326a7 335 packet.type = SR_DF_HEADER;
0254651d 336 packet.payload = (uint8_t *)&header;
6ed4f044
DR
337 header.feed_version = 1;
338 gettimeofday(&header.starttime, NULL);
0254651d
UH
339 sr_session_send(devc->cb_data, &packet);
340
341 /* Send metadata about the SR_DF_ANALOG packets to come. */
342 sr_dbg("Sending SR_DF_META_ANALOG packet.");
343 packet.type = SR_DF_META_ANALOG;
344 packet.payload = &meta;
9cd9f6b7 345 meta.num_probes = devc->num_probes;
0254651d
UH
346 sr_session_send(devc->cb_data, &packet);
347
348 /* Poll every 10ms, or whenever some data comes in. */
349 sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
9cd9f6b7 350 alsa_receive_data, (void *)sdi);
0254651d
UH
351
352 // g_free(devc->ufds); /* FIXME */
6ed4f044 353
e46b8fb1 354 return SR_OK;
6ed4f044
DR
355}
356
0254651d 357static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
6ed4f044 358{
0254651d
UH
359 struct sr_datafeed_packet packet;
360 struct dev_context *devc;
361
362 devc = sdi->priv;
363 devc->cb_data = cb_data;
364
365 sr_source_remove(devc->ufds[0].fd);
366
367 /* Send end packet to the session bus. */
368 sr_dbg("Sending SR_DF_END packet.");
369 packet.type = SR_DF_END;
370 sr_session_send(cb_data, &packet);
3010f21c
UH
371
372 return SR_OK;
6ed4f044
DR
373}
374
c09f0b57 375SR_PRIV struct sr_dev_driver alsa_driver_info = {
e519ba86
UH
376 .name = "alsa",
377 .longname = "ALSA driver",
378 .api_version = 1,
379 .init = hw_init,
380 .cleanup = hw_cleanup,
0254651d
UH
381 .scan = hw_scan,
382 .dev_list = hw_dev_list,
383 .dev_clear = clear_instances,
e7eb703f
UH
384 .dev_open = hw_dev_open,
385 .dev_close = hw_dev_close,
0254651d 386 .info_get = hw_info_get,
a9a245b4 387 .dev_config_set = hw_dev_config_set,
69040b7c
UH
388 .dev_acquisition_start = hw_dev_acquisition_start,
389 .dev_acquisition_stop = hw_dev_acquisition_stop,
0254651d 390 .priv = NULL,
6ed4f044 391};