]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/alsa.c
Change all sigrok_ prefixes to sr_.
[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 #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
32 static int capabilities[] = {
33         HWCAP_SAMPLERATE,
34         HWCAP_LIMIT_SAMPLES,
35         HWCAP_CONTINUOUS,
36 };
37
38 static GSList *device_instances = NULL;
39
40 struct 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
48 static int hw_init(char *deviceinfo)
49 {
50         struct sr_device_instance *sdi;
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
61         sdi = sr_device_instance_new(0, ST_ACTIVE, "alsa", NULL, NULL);
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;
70 free_alsa:
71         free(alsa);
72         return 0;
73 }
74
75 static int hw_opendev(int device_index)
76 {
77         struct sr_device_instance *sdi;
78         struct alsa *alsa;
79         int err;
80
81         if (!(sdi = get_sr_device_instance(device_instances, device_index)))
82                 return SR_ERR;
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) {
88                 g_warning("cannot open audio device %s (%s)", AUDIO_DEV,
89                                 snd_strerror(err));
90                 return SR_ERR;
91         }
92
93         err = snd_pcm_hw_params_malloc(&alsa->hw_params);
94         if (err < 0) {
95                 g_warning("cannot allocate hardware parameter structure (%s)",
96                                 snd_strerror(err));
97                 return SR_ERR;
98         }
99
100         err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
101         if (err < 0) {
102                 g_warning("cannot initialize hardware parameter structure (%s)",
103                                 snd_strerror(err));
104                 return SR_ERR;
105         }
106
107         return SR_OK;
108 }
109
110 static void hw_closedev(int device_index)
111 {
112         struct sr_device_instance *sdi;
113         struct alsa *alsa;
114
115         if (!(sdi = get_sr_device_instance(device_instances, device_index)))
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
127 static void hw_cleanup(void)
128 {
129         struct sr_device_instance *sdi;
130
131         if (!(sdi = get_sr_device_instance(device_instances, 0)))
132                 return;
133
134         free(sdi->priv);
135         sr_device_instance_free(sdi);
136 }
137
138 static void *hw_get_device_info(int device_index, int device_info_id)
139 {
140         struct sr_device_instance *sdi;
141         struct alsa *alsa;
142         void *info = NULL;
143
144         if (!(sdi = get_sr_device_instance(device_instances, device_index)))
145                 return NULL;
146         alsa = sdi->priv;
147
148         switch (device_info_id) {
149         case DI_INSTANCE:
150                 info = sdi;
151                 break;
152         case DI_NUM_PROBES:
153                 info = GINT_TO_POINTER(NUM_PROBES);
154                 break;
155         case DI_CUR_SAMPLERATE:
156                 info = &alsa->cur_rate;
157                 break;
158         // case DI_PROBE_TYPE:
159         //      info = GINT_TO_POINTER(PROBE_TYPE_ANALOG);
160         //      break;
161         }
162
163         return info;
164 }
165
166 static int hw_get_status(int device_index)
167 {
168         /* Avoid compiler warnings. */
169         device_index = device_index;
170
171         return ST_ACTIVE;
172 }
173
174 static int *hw_get_capabilities(void)
175 {
176         return capabilities;
177 }
178
179 static int hw_set_configuration(int device_index, int capability, void *value)
180 {
181         struct sr_device_instance *sdi;
182         struct alsa *alsa;
183
184         if (!(sdi = get_sr_device_instance(device_instances, device_index)))
185                 return SR_ERR;
186         alsa = sdi->priv;
187
188         switch (capability) {
189         case HWCAP_PROBECONFIG:
190                 return SR_OK;
191         case HWCAP_SAMPLERATE:
192                 alsa->cur_rate = *(uint64_t *) value;
193                 return SR_OK;
194         case HWCAP_LIMIT_SAMPLES:
195                 alsa->limit_samples = *(uint64_t *) value;
196                 return SR_OK;
197         default:
198                 return SR_ERR;
199         }
200 }
201
202 static int receive_data(int fd, int revents, void *user_data)
203 {
204         struct sr_device_instance *sdi = user_data;
205         struct alsa *alsa = sdi->priv;
206         struct datafeed_packet packet;
207         struct analog_sample *sample;
208         unsigned int sample_size = sizeof(struct analog_sample) +
209                 (NUM_PROBES * sizeof(struct analog_probe));
210         char *outb;
211         char inb[4096];
212         int i, x, count;
213
214         fd = fd;
215         revents = revents;
216
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) {
222                         g_warning("Failed to read samples");
223                         return FALSE;
224                 }
225
226                 outb = malloc(sample_size * count);
227                 if (!outb)
228                         return FALSE;
229
230                 for (i = 0; i < count; i++) {
231                         sample = (struct analog_sample *)
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                         }
241                 }
242
243                 packet.type = DF_ANALOG;
244                 packet.length = count * sample_size;
245                 packet.unitsize = sample_size;
246                 packet.payload = outb;
247                 session_bus(user_data, &packet);
248                 free(outb);
249                 alsa->limit_samples -= count;
250
251         } while (alsa->limit_samples > 0);
252
253         packet.type = DF_END;
254         session_bus(user_data, &packet);
255
256         return TRUE;
257 }
258
259 static int hw_start_acquisition(int device_index, gpointer session_device_id)
260 {
261         struct sr_device_instance *sdi;
262         struct alsa *alsa;
263         struct datafeed_packet packet;
264         struct datafeed_header header;
265         struct pollfd *ufds;
266         int count;
267         int err;
268
269         if (!(sdi = get_sr_device_instance(device_instances, device_index)))
270                 return SR_ERR;
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) {
276                 g_warning("cannot set access type (%s)", snd_strerror(err));
277                 return SR_ERR;
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) {
284                 g_warning("cannot set sample format (%s)", snd_strerror(err));
285                 return SR_ERR;
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) {
291                 g_warning("cannot set sample rate (%s)", snd_strerror(err));
292                 return SR_ERR;
293         }
294
295         err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
296                         alsa->hw_params, NUM_PROBES);
297         if (err < 0) {
298                 g_warning("cannot set channel count (%s)", snd_strerror(err));
299                 return SR_ERR;
300         }
301
302         err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
303         if (err < 0) {
304                 g_warning("cannot set parameters (%s)", snd_strerror(err));
305                 return SR_ERR;
306         }
307
308         err = snd_pcm_prepare(alsa->capture_handle);
309         if (err < 0) {
310                 g_warning("cannot prepare audio interface for use (%s)",
311                                 snd_strerror(err));
312                 return SR_ERR;
313         }
314
315         count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
316         if (count < 1) {
317                 g_warning("Unable to obtain poll descriptors count");
318                 return SR_ERR;
319         }
320
321         ufds = malloc(count * sizeof(struct pollfd));
322         if (!ufds)
323                 return SR_ERR_MALLOC;
324
325         err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
326         if (err < 0) {
327                 g_warning("Unable to obtain poll descriptors (%s)",
328                                 snd_strerror(err));
329                 free(ufds);
330                 return SR_ERR;
331         }
332
333         alsa->session_id = session_device_id;
334         source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
335
336         packet.type = DF_HEADER;
337         packet.length = sizeof(struct datafeed_header);
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;
344         header.protocol_id = PROTO_RAW;
345         session_bus(session_device_id, &packet);
346         free(ufds);
347
348         return SR_OK;
349 }
350
351 static 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
358 struct device_plugin alsa_plugin_info = {
359         "alsa",
360         1,
361         hw_init,
362         hw_cleanup,
363         hw_opendev,
364         hw_closedev,
365         hw_get_device_info,
366         hw_get_status,
367         hw_get_capabilities,
368         hw_set_configuration,
369         hw_start_acquisition,
370         hw_stop_acquisition,
371 };