]> sigrok.org Git - libsigrok.git/blame - hardware/alsa/alsa.c
sr/cli/gtk: A few more s/instance/inst/.
[libsigrok.git] / hardware / alsa / alsa.c
CommitLineData
6ed4f044
DR
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
a4cfb10f
UH
21/* Note: This driver doesn't compile, analog support in sigrok is WIP. */
22
6ed4f044
DR
23#include <stdlib.h>
24#include <unistd.h>
25#include <string.h>
6ed4f044 26#include <alsa/asoundlib.h>
b7f09cf8
UH
27#include "sigrok.h"
28#include "sigrok-internal.h"
6ed4f044
DR
29
30#define NUM_PROBES 2
31#define SAMPLE_WIDTH 16
32#define AUDIO_DEV "plughw:0,0"
33
a4cfb10f
UH
34struct sr_analog_probe {
35 uint8_t att;
36 uint8_t res; /* Needs to be a power of 2, FIXME */
37 uint16_t val; /* Max hardware ADC width is 16bits */
38};
39
40struct sr_analog_sample {
41 uint8_t num_probes; /* Max hardware probes is 256 */
42 struct sr_analog_probe probes[];
43};
44
6ed4f044 45static int capabilities[] = {
5a2326a7
UH
46 SR_HWCAP_SAMPLERATE,
47 SR_HWCAP_LIMIT_SAMPLES,
48 SR_HWCAP_CONTINUOUS,
6ed4f044
DR
49};
50
c37d2b1b 51static const char *probe_names[NUM_PROBES + 1] = {
464d12c7
KS
52 "0",
53 "1",
54 NULL,
55};
56
d68e2d1a 57static GSList *dev_insts = NULL;
6ed4f044
DR
58
59struct alsa {
60 uint64_t cur_rate;
61 uint64_t limit_samples;
62 snd_pcm_t *capture_handle;
63 snd_pcm_hw_params_t *hw_params;
64 gpointer session_id;
65};
66
bb7ef793 67static int hw_init(const char *devinfo)
6ed4f044 68{
d68e2d1a 69 struct sr_dev_inst *sdi;
6ed4f044
DR
70 struct alsa *alsa;
71
72 /* Avoid compiler warnings. */
bb7ef793 73 devinfo = devinfo;
6ed4f044 74
92b3101c
UH
75 if (!(alsa = g_try_malloc0(sizeof(struct alsa)))) {
76 sr_err("alsa: %s: alsa malloc failed", __func__);
6ed4f044 77 return 0;
92b3101c 78 }
6ed4f044 79
d3683c42 80 sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
6ed4f044
DR
81 if (!sdi)
82 goto free_alsa;
83
84 sdi->priv = alsa;
85
d68e2d1a 86 dev_insts = g_slist_append(dev_insts, sdi);
6ed4f044
DR
87
88 return 1;
89free_alsa:
92b3101c 90 g_free(alsa);
6ed4f044
DR
91 return 0;
92}
93
bb7ef793 94static int hw_opendev(int dev_index)
6ed4f044 95{
d68e2d1a 96 struct sr_dev_inst *sdi;
6ed4f044
DR
97 struct alsa *alsa;
98 int err;
99
bb7ef793 100 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
e46b8fb1 101 return SR_ERR;
6ed4f044
DR
102 alsa = sdi->priv;
103
104 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
105 SND_PCM_STREAM_CAPTURE, 0);
106 if (err < 0) {
7b48d6e1 107 sr_err("alsa: can't open audio device %s (%s)", AUDIO_DEV,
133a37bf 108 snd_strerror(err));
e46b8fb1 109 return SR_ERR;
6ed4f044
DR
110 }
111
112 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
113 if (err < 0) {
7b48d6e1 114 sr_err("alsa: can't allocate hardware parameter structure (%s)",
133a37bf 115 snd_strerror(err));
e46b8fb1 116 return SR_ERR;
6ed4f044
DR
117 }
118
119 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
120 if (err < 0) {
7b48d6e1
UH
121 sr_err("alsa: can't initialize hardware parameter structure "
122 "(%s)", snd_strerror(err));
e46b8fb1 123 return SR_ERR;
6ed4f044
DR
124 }
125
e46b8fb1 126 return SR_OK;
6ed4f044
DR
127}
128
bb7ef793 129static int hw_closedev(int dev_index)
6ed4f044 130{
d68e2d1a 131 struct sr_dev_inst *sdi;
6ed4f044
DR
132 struct alsa *alsa;
133
bb7ef793 134 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
697785d1
UH
135 sr_err("alsa: %s: sdi was NULL", __func__);
136 return SR_ERR; /* TODO: SR_ERR_ARG? */
137 }
6ed4f044 138
697785d1
UH
139 if (!(alsa = sdi->priv)) {
140 sr_err("alsa: %s: sdi->priv was NULL", __func__);
141 return SR_ERR; /* TODO: SR_ERR_ARG? */
142 }
143
144 // TODO: Return values of snd_*?
6ed4f044
DR
145 if (alsa->hw_params)
146 snd_pcm_hw_params_free(alsa->hw_params);
147 if (alsa->capture_handle)
148 snd_pcm_close(alsa->capture_handle);
697785d1
UH
149
150 return SR_OK;
6ed4f044
DR
151}
152
57ab7d9f 153static int hw_cleanup(void)
6ed4f044 154{
d68e2d1a 155 struct sr_dev_inst *sdi;
6ed4f044 156
d68e2d1a 157 if (!(sdi = sr_dev_inst_get(dev_insts, 0))) {
57ab7d9f
UH
158 sr_err("alsa: %s: sdi was NULL", __func__);
159 return SR_ERR_BUG;
160 }
6ed4f044 161
d3683c42 162 sr_dev_inst_free(sdi);
57ab7d9f
UH
163
164 return SR_OK;
6ed4f044
DR
165}
166
bb7ef793 167static void *hw_get_dev_info(int dev_index, int dev_info_id)
6ed4f044 168{
d68e2d1a 169 struct sr_dev_inst *sdi;
6ed4f044
DR
170 struct alsa *alsa;
171 void *info = NULL;
172
bb7ef793 173 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
6ed4f044
DR
174 return NULL;
175 alsa = sdi->priv;
176
bb7ef793 177 switch (dev_info_id) {
1d9a8a5f 178 case SR_DI_INST:
6ed4f044
DR
179 info = sdi;
180 break;
5a2326a7 181 case SR_DI_NUM_PROBES:
6ed4f044
DR
182 info = GINT_TO_POINTER(NUM_PROBES);
183 break;
464d12c7
KS
184 case SR_DI_PROBE_NAMES:
185 info = probe_names;
186 break;
5a2326a7 187 case SR_DI_CUR_SAMPLERATE:
6ed4f044
DR
188 info = &alsa->cur_rate;
189 break;
5a2326a7
UH
190 // case SR_DI_PROBE_TYPE:
191 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
da692373 192 // break;
6ed4f044
DR
193 }
194
195 return info;
196}
197
bb7ef793 198static int hw_get_status(int dev_index)
6ed4f044
DR
199{
200 /* Avoid compiler warnings. */
bb7ef793 201 dev_index = dev_index;
6ed4f044 202
5a2326a7 203 return SR_ST_ACTIVE;
6ed4f044
DR
204}
205
206static int *hw_get_capabilities(void)
207{
208 return capabilities;
209}
210
bb7ef793 211static int hw_set_configuration(int dev_index, int capability, void *value)
6ed4f044 212{
d68e2d1a 213 struct sr_dev_inst *sdi;
6ed4f044
DR
214 struct alsa *alsa;
215
bb7ef793 216 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
e46b8fb1 217 return SR_ERR;
6ed4f044
DR
218 alsa = sdi->priv;
219
220 switch (capability) {
5a2326a7 221 case SR_HWCAP_PROBECONFIG:
e46b8fb1 222 return SR_OK;
5a2326a7 223 case SR_HWCAP_SAMPLERATE:
6ed4f044 224 alsa->cur_rate = *(uint64_t *) value;
e46b8fb1 225 return SR_OK;
5a2326a7 226 case SR_HWCAP_LIMIT_SAMPLES:
6ed4f044 227 alsa->limit_samples = *(uint64_t *) value;
e46b8fb1 228 return SR_OK;
6ed4f044 229 default:
e46b8fb1 230 return SR_ERR;
6ed4f044
DR
231 }
232}
233
6ed4f044
DR
234static int receive_data(int fd, int revents, void *user_data)
235{
d68e2d1a 236 struct sr_dev_inst *sdi = user_data;
6ed4f044 237 struct alsa *alsa = sdi->priv;
b9c735a2 238 struct sr_datafeed_packet packet;
809c5f20
UH
239 struct sr_analog_sample *sample;
240 unsigned int sample_size = sizeof(struct sr_analog_sample) +
241 (NUM_PROBES * sizeof(struct sr_analog_probe));
58330ab8
DR
242 char *outb;
243 char inb[4096];
244 int i, x, count;
245
cdbc51d9
DR
246 fd = fd;
247 revents = revents;
248
58330ab8
DR
249 do {
250 memset(inb, 0, sizeof(inb));
251 count = snd_pcm_readi(alsa->capture_handle, inb,
252 MIN(4096/4, alsa->limit_samples));
253 if (count < 1) {
7b48d6e1 254 sr_err("alsa: Failed to read samples");
58330ab8
DR
255 return FALSE;
256 }
6ed4f044 257
92b3101c
UH
258 if (!(outb = g_try_malloc(sample_size * count))) {
259 sr_err("alsa: %s: outb malloc failed", __func__);
6ed4f044 260 return FALSE;
92b3101c 261 }
58330ab8
DR
262
263 for (i = 0; i < count; i++) {
809c5f20 264 sample = (struct sr_analog_sample *)
58330ab8
DR
265 (outb + (i * sample_size));
266 sample->num_probes = NUM_PROBES;
267
268 for (x = 0; x < NUM_PROBES; x++) {
269 sample->probes[x].val =
270 *(uint16_t *) (inb + (i * 4) + (x * 2));
271 sample->probes[x].val &= ((1 << 16) - 1);
272 sample->probes[x].res = 16;
273 }
6ed4f044 274 }
58330ab8 275
5a2326a7 276 packet.type = SR_DF_ANALOG;
58330ab8
DR
277 packet.length = count * sample_size;
278 packet.unitsize = sample_size;
279 packet.payload = outb;
8a2efef2 280 sr_session_bus(user_data, &packet);
92b3101c 281 g_free(outb);
58330ab8
DR
282 alsa->limit_samples -= count;
283
284 } while (alsa->limit_samples > 0);
285
5a2326a7 286 packet.type = SR_DF_END;
8a2efef2 287 sr_session_bus(user_data, &packet);
6ed4f044
DR
288
289 return TRUE;
290}
291
bb7ef793 292static int hw_start_acquisition(int dev_index, gpointer session_dev_id)
6ed4f044 293{
d68e2d1a 294 struct sr_dev_inst *sdi;
6ed4f044 295 struct alsa *alsa;
b9c735a2
UH
296 struct sr_datafeed_packet packet;
297 struct sr_datafeed_header header;
6ed4f044
DR
298 struct pollfd *ufds;
299 int count;
300 int err;
301
bb7ef793 302 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
e46b8fb1 303 return SR_ERR;
6ed4f044
DR
304 alsa = sdi->priv;
305
306 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
307 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
308 if (err < 0) {
7b48d6e1 309 sr_err("alsa: can't set access type (%s)", snd_strerror(err));
e46b8fb1 310 return SR_ERR;
6ed4f044
DR
311 }
312
313 /* FIXME: Hardcoded for 16bits */
314 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
315 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
316 if (err < 0) {
7b48d6e1 317 sr_err("alsa: can't set sample format (%s)", snd_strerror(err));
e46b8fb1 318 return SR_ERR;
6ed4f044
DR
319 }
320
321 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
322 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
323 if (err < 0) {
7b48d6e1 324 sr_err("alsa: can't set sample rate (%s)", snd_strerror(err));
e46b8fb1 325 return SR_ERR;
6ed4f044
DR
326 }
327
328 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
329 alsa->hw_params, NUM_PROBES);
330 if (err < 0) {
7b48d6e1 331 sr_err("alsa: can't set channel count (%s)", snd_strerror(err));
e46b8fb1 332 return SR_ERR;
6ed4f044
DR
333 }
334
335 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
336 if (err < 0) {
7b48d6e1 337 sr_err("alsa: can't set parameters (%s)", snd_strerror(err));
e46b8fb1 338 return SR_ERR;
6ed4f044
DR
339 }
340
341 err = snd_pcm_prepare(alsa->capture_handle);
342 if (err < 0) {
7b48d6e1 343 sr_err("alsa: can't prepare audio interface for use (%s)",
133a37bf 344 snd_strerror(err));
e46b8fb1 345 return SR_ERR;
6ed4f044
DR
346 }
347
348 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
349 if (count < 1) {
7b48d6e1 350 sr_err("alsa: Unable to obtain poll descriptors count");
e46b8fb1 351 return SR_ERR;
6ed4f044
DR
352 }
353
92b3101c 354 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
133a37bf 355 sr_err("alsa: %s: ufds malloc failed", __func__);
e46b8fb1 356 return SR_ERR_MALLOC;
92b3101c 357 }
6ed4f044
DR
358
359 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
360 if (err < 0) {
7b48d6e1 361 sr_err("alsa: Unable to obtain poll descriptors (%s)",
133a37bf 362 snd_strerror(err));
92b3101c 363 g_free(ufds);
e46b8fb1 364 return SR_ERR;
6ed4f044
DR
365 }
366
bb7ef793 367 alsa->session_id = session_dev_id;
6f1be0a2 368 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
6ed4f044 369
5a2326a7 370 packet.type = SR_DF_HEADER;
b9c735a2 371 packet.length = sizeof(struct sr_datafeed_header);
6ed4f044
DR
372 packet.payload = (unsigned char *) &header;
373 header.feed_version = 1;
374 gettimeofday(&header.starttime, NULL);
375 header.samplerate = alsa->cur_rate;
376 header.num_analog_probes = NUM_PROBES;
377 header.num_logic_probes = 0;
5a2326a7 378 header.protocol_id = SR_PROTO_RAW;
bb7ef793 379 sr_session_bus(session_dev_id, &packet);
92b3101c 380 g_free(ufds);
6ed4f044 381
e46b8fb1 382 return SR_OK;
6ed4f044
DR
383}
384
bb7ef793 385static int hw_stop_acquisition(int dev_index, gpointer session_dev_id)
6ed4f044
DR
386{
387 /* Avoid compiler warnings. */
bb7ef793
UH
388 dev_index = dev_index;
389 session_dev_id = session_dev_id;
3010f21c
UH
390
391 return SR_OK;
6ed4f044
DR
392}
393
bb7ef793 394SR_PRIV struct sr_dev_plugin alsa_plugin_info = {
e519ba86
UH
395 .name = "alsa",
396 .longname = "ALSA driver",
397 .api_version = 1,
398 .init = hw_init,
399 .cleanup = hw_cleanup,
86f5e3d8
UH
400 .opendev = hw_opendev,
401 .closedev = hw_closedev,
bb7ef793 402 .get_dev_info = hw_get_dev_info,
e519ba86
UH
403 .get_status = hw_get_status,
404 .get_capabilities = hw_get_capabilities,
405 .set_configuration = hw_set_configuration,
406 .start_acquisition = hw_start_acquisition,
407 .stop_acquisition = hw_stop_acquisition,
6ed4f044 408};