]> sigrok.org Git - libsigrok.git/blame - hardware/alsa/alsa.c
LA8: Always use glib's memory allocation functions.
[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
21#include <stdlib.h>
22#include <unistd.h>
23#include <string.h>
24#include <sigrok.h>
25#include <alsa/asoundlib.h>
26#include "config.h"
27
28#define NUM_PROBES 2
29#define SAMPLE_WIDTH 16
30#define AUDIO_DEV "plughw:0,0"
31
32static int capabilities[] = {
5a2326a7
UH
33 SR_HWCAP_SAMPLERATE,
34 SR_HWCAP_LIMIT_SAMPLES,
35 SR_HWCAP_CONTINUOUS,
6ed4f044
DR
36};
37
38static GSList *device_instances = NULL;
39
40struct alsa {
41 uint64_t cur_rate;
42 uint64_t limit_samples;
43 snd_pcm_t *capture_handle;
44 snd_pcm_hw_params_t *hw_params;
45 gpointer session_id;
46};
47
54ac5277 48static int hw_init(const char *deviceinfo)
6ed4f044 49{
a00ba012 50 struct sr_device_instance *sdi;
6ed4f044
DR
51 struct alsa *alsa;
52
53 /* Avoid compiler warnings. */
54 deviceinfo = deviceinfo;
55
56 alsa = malloc(sizeof(struct alsa));
57 if (!alsa)
58 return 0;
59 memset(alsa, 0, sizeof(struct alsa));
60
5a2326a7 61 sdi = sr_device_instance_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
6ed4f044
DR
62 if (!sdi)
63 goto free_alsa;
64
65 sdi->priv = alsa;
66
67 device_instances = g_slist_append(device_instances, sdi);
68
69 return 1;
70free_alsa:
71 free(alsa);
72 return 0;
73}
74
75static int hw_opendev(int device_index)
76{
a00ba012 77 struct sr_device_instance *sdi;
6ed4f044
DR
78 struct alsa *alsa;
79 int err;
80
d32d961d 81 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 82 return SR_ERR;
6ed4f044
DR
83 alsa = sdi->priv;
84
85 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
86 SND_PCM_STREAM_CAPTURE, 0);
87 if (err < 0) {
b08024a8 88 sr_warn("cannot open audio device %s (%s)", AUDIO_DEV,
6ed4f044 89 snd_strerror(err));
e46b8fb1 90 return SR_ERR;
6ed4f044
DR
91 }
92
93 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
94 if (err < 0) {
b08024a8 95 sr_warn("cannot allocate hardware parameter structure (%s)",
6ed4f044 96 snd_strerror(err));
e46b8fb1 97 return SR_ERR;
6ed4f044
DR
98 }
99
100 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
101 if (err < 0) {
b08024a8 102 sr_warn("cannot initialize hardware parameter structure (%s)",
6ed4f044 103 snd_strerror(err));
e46b8fb1 104 return SR_ERR;
6ed4f044
DR
105 }
106
e46b8fb1 107 return SR_OK;
6ed4f044
DR
108}
109
110static void hw_closedev(int device_index)
111{
a00ba012 112 struct sr_device_instance *sdi;
6ed4f044
DR
113 struct alsa *alsa;
114
d32d961d 115 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
6ed4f044
DR
116 return;
117 alsa = sdi->priv;
118 if (!alsa)
119 return;
120
121 if (alsa->hw_params)
122 snd_pcm_hw_params_free(alsa->hw_params);
123 if (alsa->capture_handle)
124 snd_pcm_close(alsa->capture_handle);
125}
126
127static void hw_cleanup(void)
128{
a00ba012 129 struct sr_device_instance *sdi;
6ed4f044 130
d32d961d 131 if (!(sdi = sr_get_device_instance(device_instances, 0)))
6ed4f044
DR
132 return;
133
134 free(sdi->priv);
a00ba012 135 sr_device_instance_free(sdi);
6ed4f044
DR
136}
137
138static void *hw_get_device_info(int device_index, int device_info_id)
139{
a00ba012 140 struct sr_device_instance *sdi;
6ed4f044
DR
141 struct alsa *alsa;
142 void *info = NULL;
143
d32d961d 144 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
6ed4f044
DR
145 return NULL;
146 alsa = sdi->priv;
147
148 switch (device_info_id) {
5a2326a7 149 case SR_DI_INSTANCE:
6ed4f044
DR
150 info = sdi;
151 break;
5a2326a7 152 case SR_DI_NUM_PROBES:
6ed4f044
DR
153 info = GINT_TO_POINTER(NUM_PROBES);
154 break;
5a2326a7 155 case SR_DI_CUR_SAMPLERATE:
6ed4f044
DR
156 info = &alsa->cur_rate;
157 break;
5a2326a7
UH
158 // case SR_DI_PROBE_TYPE:
159 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
da692373 160 // break;
6ed4f044
DR
161 }
162
163 return info;
164}
165
166static int hw_get_status(int device_index)
167{
168 /* Avoid compiler warnings. */
169 device_index = device_index;
170
5a2326a7 171 return SR_ST_ACTIVE;
6ed4f044
DR
172}
173
174static int *hw_get_capabilities(void)
175{
176 return capabilities;
177}
178
179static int hw_set_configuration(int device_index, int capability, void *value)
180{
a00ba012 181 struct sr_device_instance *sdi;
6ed4f044
DR
182 struct alsa *alsa;
183
d32d961d 184 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 185 return SR_ERR;
6ed4f044
DR
186 alsa = sdi->priv;
187
188 switch (capability) {
5a2326a7 189 case SR_HWCAP_PROBECONFIG:
e46b8fb1 190 return SR_OK;
5a2326a7 191 case SR_HWCAP_SAMPLERATE:
6ed4f044 192 alsa->cur_rate = *(uint64_t *) value;
e46b8fb1 193 return SR_OK;
5a2326a7 194 case SR_HWCAP_LIMIT_SAMPLES:
6ed4f044 195 alsa->limit_samples = *(uint64_t *) value;
e46b8fb1 196 return SR_OK;
6ed4f044 197 default:
e46b8fb1 198 return SR_ERR;
6ed4f044
DR
199 }
200}
201
6ed4f044
DR
202static int receive_data(int fd, int revents, void *user_data)
203{
a00ba012 204 struct sr_device_instance *sdi = user_data;
6ed4f044 205 struct alsa *alsa = sdi->priv;
b9c735a2 206 struct sr_datafeed_packet packet;
809c5f20
UH
207 struct sr_analog_sample *sample;
208 unsigned int sample_size = sizeof(struct sr_analog_sample) +
209 (NUM_PROBES * sizeof(struct sr_analog_probe));
58330ab8
DR
210 char *outb;
211 char inb[4096];
212 int i, x, count;
213
cdbc51d9
DR
214 fd = fd;
215 revents = revents;
216
58330ab8
DR
217 do {
218 memset(inb, 0, sizeof(inb));
219 count = snd_pcm_readi(alsa->capture_handle, inb,
220 MIN(4096/4, alsa->limit_samples));
221 if (count < 1) {
b08024a8 222 sr_warn("Failed to read samples");
58330ab8
DR
223 return FALSE;
224 }
6ed4f044 225
58330ab8
DR
226 outb = malloc(sample_size * count);
227 if (!outb)
6ed4f044 228 return FALSE;
58330ab8
DR
229
230 for (i = 0; i < count; i++) {
809c5f20 231 sample = (struct sr_analog_sample *)
58330ab8
DR
232 (outb + (i * sample_size));
233 sample->num_probes = NUM_PROBES;
234
235 for (x = 0; x < NUM_PROBES; x++) {
236 sample->probes[x].val =
237 *(uint16_t *) (inb + (i * 4) + (x * 2));
238 sample->probes[x].val &= ((1 << 16) - 1);
239 sample->probes[x].res = 16;
240 }
6ed4f044 241 }
58330ab8 242
5a2326a7 243 packet.type = SR_DF_ANALOG;
58330ab8
DR
244 packet.length = count * sample_size;
245 packet.unitsize = sample_size;
246 packet.payload = outb;
8a2efef2 247 sr_session_bus(user_data, &packet);
58330ab8
DR
248 free(outb);
249 alsa->limit_samples -= count;
250
251 } while (alsa->limit_samples > 0);
252
5a2326a7 253 packet.type = SR_DF_END;
8a2efef2 254 sr_session_bus(user_data, &packet);
6ed4f044
DR
255
256 return TRUE;
257}
258
259static int hw_start_acquisition(int device_index, gpointer session_device_id)
260{
a00ba012 261 struct sr_device_instance *sdi;
6ed4f044 262 struct alsa *alsa;
b9c735a2
UH
263 struct sr_datafeed_packet packet;
264 struct sr_datafeed_header header;
6ed4f044
DR
265 struct pollfd *ufds;
266 int count;
267 int err;
268
d32d961d 269 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 270 return SR_ERR;
6ed4f044
DR
271 alsa = sdi->priv;
272
273 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
274 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
275 if (err < 0) {
b08024a8 276 sr_warn("cannot set access type (%s)", snd_strerror(err));
e46b8fb1 277 return SR_ERR;
6ed4f044
DR
278 }
279
280 /* FIXME: Hardcoded for 16bits */
281 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
282 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
283 if (err < 0) {
b08024a8 284 sr_warn("cannot set sample format (%s)", snd_strerror(err));
e46b8fb1 285 return SR_ERR;
6ed4f044
DR
286 }
287
288 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
289 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
290 if (err < 0) {
b08024a8 291 sr_warn("cannot set sample rate (%s)", snd_strerror(err));
e46b8fb1 292 return SR_ERR;
6ed4f044
DR
293 }
294
295 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
296 alsa->hw_params, NUM_PROBES);
297 if (err < 0) {
b08024a8 298 sr_warn("cannot set channel count (%s)", snd_strerror(err));
e46b8fb1 299 return SR_ERR;
6ed4f044
DR
300 }
301
302 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
303 if (err < 0) {
b08024a8 304 sr_warn("cannot set parameters (%s)", snd_strerror(err));
e46b8fb1 305 return SR_ERR;
6ed4f044
DR
306 }
307
308 err = snd_pcm_prepare(alsa->capture_handle);
309 if (err < 0) {
b08024a8 310 sr_warn("cannot prepare audio interface for use (%s)",
6ed4f044 311 snd_strerror(err));
e46b8fb1 312 return SR_ERR;
6ed4f044
DR
313 }
314
315 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
316 if (count < 1) {
b08024a8 317 sr_warn("Unable to obtain poll descriptors count");
e46b8fb1 318 return SR_ERR;
6ed4f044
DR
319 }
320
321 ufds = malloc(count * sizeof(struct pollfd));
322 if (!ufds)
e46b8fb1 323 return SR_ERR_MALLOC;
6ed4f044
DR
324
325 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
326 if (err < 0) {
b08024a8 327 sr_warn("Unable to obtain poll descriptors (%s)",
6ed4f044
DR
328 snd_strerror(err));
329 free(ufds);
e46b8fb1 330 return SR_ERR;
6ed4f044
DR
331 }
332
333 alsa->session_id = session_device_id;
6f1be0a2 334 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
6ed4f044 335
5a2326a7 336 packet.type = SR_DF_HEADER;
b9c735a2 337 packet.length = sizeof(struct sr_datafeed_header);
6ed4f044
DR
338 packet.payload = (unsigned char *) &header;
339 header.feed_version = 1;
340 gettimeofday(&header.starttime, NULL);
341 header.samplerate = alsa->cur_rate;
342 header.num_analog_probes = NUM_PROBES;
343 header.num_logic_probes = 0;
5a2326a7 344 header.protocol_id = SR_PROTO_RAW;
8a2efef2 345 sr_session_bus(session_device_id, &packet);
6ed4f044
DR
346 free(ufds);
347
e46b8fb1 348 return SR_OK;
6ed4f044
DR
349}
350
351static void hw_stop_acquisition(int device_index, gpointer session_device_id)
352{
353 /* Avoid compiler warnings. */
354 device_index = device_index;
355 session_device_id = session_device_id;
356}
357
5c2d46d1 358struct sr_device_plugin alsa_plugin_info = {
6ed4f044 359 "alsa",
9f8274a5 360 "ALSA driver",
6ed4f044
DR
361 1,
362 hw_init,
363 hw_cleanup,
364 hw_opendev,
365 hw_closedev,
366 hw_get_device_info,
367 hw_get_status,
368 hw_get_capabilities,
369 hw_set_configuration,
370 hw_start_acquisition,
371 hw_stop_acquisition,
372};