]> sigrok.org Git - libsigrok.git/blame - hardware/alsa/alsa.c
sr: adjust copyright year
[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
6ed4f044
DR
57static GSList *device_instances = NULL;
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
54ac5277 67static int hw_init(const char *deviceinfo)
6ed4f044 68{
a00ba012 69 struct sr_device_instance *sdi;
6ed4f044
DR
70 struct alsa *alsa;
71
72 /* Avoid compiler warnings. */
73 deviceinfo = deviceinfo;
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
5a2326a7 80 sdi = sr_device_instance_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
6ed4f044
DR
81 if (!sdi)
82 goto free_alsa;
83
84 sdi->priv = alsa;
85
86 device_instances = g_slist_append(device_instances, sdi);
87
88 return 1;
89free_alsa:
92b3101c 90 g_free(alsa);
6ed4f044
DR
91 return 0;
92}
93
94static int hw_opendev(int device_index)
95{
a00ba012 96 struct sr_device_instance *sdi;
6ed4f044
DR
97 struct alsa *alsa;
98 int err;
99
d32d961d 100 if (!(sdi = sr_get_device_instance(device_instances, device_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) {
133a37bf
UH
107 sr_err("cannot open audio device %s (%s)", AUDIO_DEV,
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) {
133a37bf
UH
114 sr_err("cannot allocate hardware parameter structure (%s)",
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) {
133a37bf
UH
121 sr_err("cannot initialize hardware parameter structure (%s)",
122 snd_strerror(err));
e46b8fb1 123 return SR_ERR;
6ed4f044
DR
124 }
125
e46b8fb1 126 return SR_OK;
6ed4f044
DR
127}
128
697785d1 129static int hw_closedev(int device_index)
6ed4f044 130{
a00ba012 131 struct sr_device_instance *sdi;
6ed4f044
DR
132 struct alsa *alsa;
133
697785d1
UH
134 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
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
153static void hw_cleanup(void)
154{
a00ba012 155 struct sr_device_instance *sdi;
6ed4f044 156
d32d961d 157 if (!(sdi = sr_get_device_instance(device_instances, 0)))
6ed4f044
DR
158 return;
159
133a37bf 160 g_free(sdi->priv);
a00ba012 161 sr_device_instance_free(sdi);
6ed4f044
DR
162}
163
164static void *hw_get_device_info(int device_index, int device_info_id)
165{
a00ba012 166 struct sr_device_instance *sdi;
6ed4f044
DR
167 struct alsa *alsa;
168 void *info = NULL;
169
d32d961d 170 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
6ed4f044
DR
171 return NULL;
172 alsa = sdi->priv;
173
174 switch (device_info_id) {
5a2326a7 175 case SR_DI_INSTANCE:
6ed4f044
DR
176 info = sdi;
177 break;
5a2326a7 178 case SR_DI_NUM_PROBES:
6ed4f044
DR
179 info = GINT_TO_POINTER(NUM_PROBES);
180 break;
464d12c7
KS
181 case SR_DI_PROBE_NAMES:
182 info = probe_names;
183 break;
5a2326a7 184 case SR_DI_CUR_SAMPLERATE:
6ed4f044
DR
185 info = &alsa->cur_rate;
186 break;
5a2326a7
UH
187 // case SR_DI_PROBE_TYPE:
188 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
da692373 189 // break;
6ed4f044
DR
190 }
191
192 return info;
193}
194
195static int hw_get_status(int device_index)
196{
197 /* Avoid compiler warnings. */
198 device_index = device_index;
199
5a2326a7 200 return SR_ST_ACTIVE;
6ed4f044
DR
201}
202
203static int *hw_get_capabilities(void)
204{
205 return capabilities;
206}
207
208static int hw_set_configuration(int device_index, int capability, void *value)
209{
a00ba012 210 struct sr_device_instance *sdi;
6ed4f044
DR
211 struct alsa *alsa;
212
d32d961d 213 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 214 return SR_ERR;
6ed4f044
DR
215 alsa = sdi->priv;
216
217 switch (capability) {
5a2326a7 218 case SR_HWCAP_PROBECONFIG:
e46b8fb1 219 return SR_OK;
5a2326a7 220 case SR_HWCAP_SAMPLERATE:
6ed4f044 221 alsa->cur_rate = *(uint64_t *) value;
e46b8fb1 222 return SR_OK;
5a2326a7 223 case SR_HWCAP_LIMIT_SAMPLES:
6ed4f044 224 alsa->limit_samples = *(uint64_t *) value;
e46b8fb1 225 return SR_OK;
6ed4f044 226 default:
e46b8fb1 227 return SR_ERR;
6ed4f044
DR
228 }
229}
230
6ed4f044
DR
231static int receive_data(int fd, int revents, void *user_data)
232{
a00ba012 233 struct sr_device_instance *sdi = user_data;
6ed4f044 234 struct alsa *alsa = sdi->priv;
b9c735a2 235 struct sr_datafeed_packet packet;
809c5f20
UH
236 struct sr_analog_sample *sample;
237 unsigned int sample_size = sizeof(struct sr_analog_sample) +
238 (NUM_PROBES * sizeof(struct sr_analog_probe));
58330ab8
DR
239 char *outb;
240 char inb[4096];
241 int i, x, count;
242
cdbc51d9
DR
243 fd = fd;
244 revents = revents;
245
58330ab8
DR
246 do {
247 memset(inb, 0, sizeof(inb));
248 count = snd_pcm_readi(alsa->capture_handle, inb,
249 MIN(4096/4, alsa->limit_samples));
250 if (count < 1) {
133a37bf 251 sr_err("Failed to read samples");
58330ab8
DR
252 return FALSE;
253 }
6ed4f044 254
92b3101c
UH
255 if (!(outb = g_try_malloc(sample_size * count))) {
256 sr_err("alsa: %s: outb malloc failed", __func__);
6ed4f044 257 return FALSE;
92b3101c 258 }
58330ab8
DR
259
260 for (i = 0; i < count; i++) {
809c5f20 261 sample = (struct sr_analog_sample *)
58330ab8
DR
262 (outb + (i * sample_size));
263 sample->num_probes = NUM_PROBES;
264
265 for (x = 0; x < NUM_PROBES; x++) {
266 sample->probes[x].val =
267 *(uint16_t *) (inb + (i * 4) + (x * 2));
268 sample->probes[x].val &= ((1 << 16) - 1);
269 sample->probes[x].res = 16;
270 }
6ed4f044 271 }
58330ab8 272
5a2326a7 273 packet.type = SR_DF_ANALOG;
58330ab8
DR
274 packet.length = count * sample_size;
275 packet.unitsize = sample_size;
276 packet.payload = outb;
8a2efef2 277 sr_session_bus(user_data, &packet);
92b3101c 278 g_free(outb);
58330ab8
DR
279 alsa->limit_samples -= count;
280
281 } while (alsa->limit_samples > 0);
282
5a2326a7 283 packet.type = SR_DF_END;
8a2efef2 284 sr_session_bus(user_data, &packet);
6ed4f044
DR
285
286 return TRUE;
287}
288
289static int hw_start_acquisition(int device_index, gpointer session_device_id)
290{
a00ba012 291 struct sr_device_instance *sdi;
6ed4f044 292 struct alsa *alsa;
b9c735a2
UH
293 struct sr_datafeed_packet packet;
294 struct sr_datafeed_header header;
6ed4f044
DR
295 struct pollfd *ufds;
296 int count;
297 int err;
298
d32d961d 299 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 300 return SR_ERR;
6ed4f044
DR
301 alsa = sdi->priv;
302
303 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
304 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
305 if (err < 0) {
133a37bf 306 sr_err("cannot set access type (%s)", snd_strerror(err));
e46b8fb1 307 return SR_ERR;
6ed4f044
DR
308 }
309
310 /* FIXME: Hardcoded for 16bits */
311 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
312 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
313 if (err < 0) {
133a37bf 314 sr_err("cannot set sample format (%s)", snd_strerror(err));
e46b8fb1 315 return SR_ERR;
6ed4f044
DR
316 }
317
318 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
319 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
320 if (err < 0) {
133a37bf 321 sr_err("cannot set sample rate (%s)", snd_strerror(err));
e46b8fb1 322 return SR_ERR;
6ed4f044
DR
323 }
324
325 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
326 alsa->hw_params, NUM_PROBES);
327 if (err < 0) {
133a37bf 328 sr_err("cannot set channel count (%s)", snd_strerror(err));
e46b8fb1 329 return SR_ERR;
6ed4f044
DR
330 }
331
332 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
333 if (err < 0) {
133a37bf 334 sr_err("cannot set parameters (%s)", snd_strerror(err));
e46b8fb1 335 return SR_ERR;
6ed4f044
DR
336 }
337
338 err = snd_pcm_prepare(alsa->capture_handle);
339 if (err < 0) {
133a37bf
UH
340 sr_err("cannot prepare audio interface for use (%s)",
341 snd_strerror(err));
e46b8fb1 342 return SR_ERR;
6ed4f044
DR
343 }
344
345 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
346 if (count < 1) {
133a37bf 347 sr_err("Unable to obtain poll descriptors count");
e46b8fb1 348 return SR_ERR;
6ed4f044
DR
349 }
350
92b3101c 351 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
133a37bf 352 sr_err("alsa: %s: ufds malloc failed", __func__);
e46b8fb1 353 return SR_ERR_MALLOC;
92b3101c 354 }
6ed4f044
DR
355
356 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
357 if (err < 0) {
133a37bf
UH
358 sr_err("Unable to obtain poll descriptors (%s)",
359 snd_strerror(err));
92b3101c 360 g_free(ufds);
e46b8fb1 361 return SR_ERR;
6ed4f044
DR
362 }
363
364 alsa->session_id = session_device_id;
6f1be0a2 365 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
6ed4f044 366
5a2326a7 367 packet.type = SR_DF_HEADER;
b9c735a2 368 packet.length = sizeof(struct sr_datafeed_header);
6ed4f044
DR
369 packet.payload = (unsigned char *) &header;
370 header.feed_version = 1;
371 gettimeofday(&header.starttime, NULL);
372 header.samplerate = alsa->cur_rate;
373 header.num_analog_probes = NUM_PROBES;
374 header.num_logic_probes = 0;
5a2326a7 375 header.protocol_id = SR_PROTO_RAW;
8a2efef2 376 sr_session_bus(session_device_id, &packet);
92b3101c 377 g_free(ufds);
6ed4f044 378
e46b8fb1 379 return SR_OK;
6ed4f044
DR
380}
381
382static void hw_stop_acquisition(int device_index, gpointer session_device_id)
383{
384 /* Avoid compiler warnings. */
385 device_index = device_index;
386 session_device_id = session_device_id;
387}
388
ca070ed9 389SR_PRIV struct sr_device_plugin alsa_plugin_info = {
e519ba86
UH
390 .name = "alsa",
391 .longname = "ALSA driver",
392 .api_version = 1,
393 .init = hw_init,
394 .cleanup = hw_cleanup,
86f5e3d8
UH
395 .opendev = hw_opendev,
396 .closedev = hw_closedev,
e519ba86
UH
397 .get_device_info = hw_get_device_info,
398 .get_status = hw_get_status,
399 .get_capabilities = hw_get_capabilities,
400 .set_configuration = hw_set_configuration,
401 .start_acquisition = hw_start_acquisition,
402 .stop_acquisition = hw_stop_acquisition,
6ed4f044 403};