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