]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/api.c
alsa: Split into api.c and protocol.c
[libsigrok.git] / hardware / alsa / api.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include "protocol.h"
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
25
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 #define DEFAULT_PROBES          2
31 #define SAMPLE_WIDTH            16
32 #define DEFAULT_SAMPLERATE      44100
33 // #define AUDIO_DEV            "plughw:0,0"
34 #define AUDIO_DEV               "default"
35
36 static const int hwcaps[] = {
37         SR_HWCAP_SAMPLERATE,
38         SR_HWCAP_LIMIT_SAMPLES,
39         SR_HWCAP_CONTINUOUS,
40         0,
41 };
42
43 static const char *probe_names[] = {
44         "Channel 0",
45         "Channel 1",
46         NULL,
47 };
48
49 SR_PRIV struct sr_dev_driver alsa_driver_info;
50 static struct sr_dev_driver *di = &alsa_driver_info;
51
52 static int clear_instances(void)
53 {
54         /* TODO */
55
56         return SR_OK;
57 }
58
59 static int hw_init(struct sr_context *sr_ctx)
60 {
61         struct drv_context *drvc;
62
63         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
64                 sr_err("Driver context malloc failed.");
65                 return SR_ERR_MALLOC;
66         }
67
68         drvc->sr_ctx = sr_ctx;
69         di->priv = drvc;
70
71         return SR_OK;
72 }
73
74 static GSList *hw_scan(GSList *options)
75 {
76         struct drv_context *drvc;
77         struct dev_context *devc;
78         struct sr_dev_inst *sdi;
79         struct sr_probe *probe;
80         GSList *devices;
81         int i;
82
83         (void)options;
84
85         drvc = di->priv;
86         drvc->instances = NULL;
87
88         devices = NULL;
89
90         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
91                 sr_err("Device context malloc failed.");
92                 return NULL;
93         }
94
95         if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
96                 sr_err("Failed to create device instance.");
97                 return NULL;
98         }
99
100         /* Set the samplerate to a default value for now. */
101         devc->cur_samplerate = DEFAULT_SAMPLERATE;
102         devc->num_probes = DEFAULT_PROBES;
103
104         sdi->priv = devc;
105         sdi->driver = di;
106
107         for (i = 0; probe_names[i]; i++) {
108                 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
109                                            probe_names[i]))) {
110                         sr_err("Failed to create probe.");
111                         return NULL;
112                 }
113                 sdi->probes = g_slist_append(sdi->probes, probe);
114         }
115
116         drvc->instances = g_slist_append(drvc->instances, sdi);
117         devices = g_slist_append(devices, sdi);
118
119         return devices;
120 }
121
122 static GSList *hw_dev_list(void)
123 {
124         struct drv_context *drvc;
125
126         drvc = di->priv;
127
128         return drvc->instances;
129 }
130
131 static int hw_dev_open(struct sr_dev_inst *sdi)
132 {
133         struct dev_context *devc;
134         int ret;
135
136         devc = sdi->priv;
137
138         sr_dbg("Opening audio device '%s' for stream capture.", AUDIO_DEV);
139         ret = snd_pcm_open(&devc->capture_handle, AUDIO_DEV,
140                            SND_PCM_STREAM_CAPTURE, 0);
141         if (ret < 0) {
142                 sr_err("Can't open audio device: %s.", snd_strerror(ret));
143                 return SR_ERR;
144         }
145
146         sr_dbg("Allocating hardware parameter structure.");
147         ret = snd_pcm_hw_params_malloc(&devc->hw_params);
148         if (ret < 0) {
149                 sr_err("Can't allocate hardware parameter structure: %s.",
150                        snd_strerror(ret));
151                 return SR_ERR_MALLOC;
152         }
153
154         sr_dbg("Initializing hardware parameter structure.");
155         ret = snd_pcm_hw_params_any(devc->capture_handle, devc->hw_params);
156         if (ret < 0) {
157                 sr_err("Can't initialize hardware parameter structure: %s.",
158                        snd_strerror(ret));
159                 return SR_ERR;
160         }
161
162         return SR_OK;
163 }
164
165 static int hw_dev_close(struct sr_dev_inst *sdi)
166 {
167         int ret;
168         struct dev_context *devc;
169
170         devc = sdi->priv;
171
172         sr_dbg("Closing device.");
173
174         if (devc->hw_params) {
175                 sr_dbg("Freeing hardware parameters.");
176                 snd_pcm_hw_params_free(devc->hw_params);
177         } else {
178                 sr_dbg("No hardware parameters, no need to free.");
179         }
180
181         if (devc->capture_handle) {
182                 sr_dbg("Closing PCM device.");
183                 if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
184                         sr_err("Failed to close device: %s.",
185                                snd_strerror(ret));
186                 }
187         } else {
188                 sr_dbg("No capture handle, no need to close audio device.");
189         }
190
191         return SR_OK;
192 }
193
194 static int hw_cleanup(void)
195 {
196         clear_instances();
197
198         return SR_OK;
199 }
200
201 static int hw_info_get(int info_id, const void **data,
202                        const struct sr_dev_inst *sdi)
203 {
204         struct dev_context *devc;
205
206         if (info_id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
207                 devc = sdi->priv;
208
209         switch (info_id) {
210         case SR_DI_HWCAPS:
211                 *data = hwcaps;
212                 break;
213         case SR_DI_NUM_PROBES:
214                 *data = GINT_TO_POINTER(DEFAULT_PROBES);
215                 break;
216         case SR_DI_PROBE_NAMES:
217                 *data = probe_names;
218                 break;
219         case SR_DI_CUR_SAMPLERATE:
220                 *data = &devc->cur_samplerate;
221                 break;
222         default:
223                 sr_err("Invalid info_id: %d.", info_id);
224                 return SR_ERR_ARG;
225         }
226
227         return SR_OK;
228 }
229
230 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
231                              const void *value)
232 {
233         struct dev_context *devc;
234
235         devc = sdi->priv;
236
237         switch (hwcap) {
238         case SR_HWCAP_SAMPLERATE:
239                 devc->cur_samplerate = *(const uint64_t *)value;
240                 break;
241         case SR_HWCAP_LIMIT_SAMPLES:
242                 devc->limit_samples = *(const uint64_t *)value;
243                 break;
244         default:
245                 sr_err("Unknown capability: %d.", hwcap);
246                 return SR_ERR;
247         }
248
249         return SR_OK;
250 }
251
252 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
253                                     void *cb_data)
254 {
255         struct sr_datafeed_packet packet;
256         struct sr_datafeed_header header;
257         struct sr_datafeed_meta_analog meta;
258         struct dev_context *devc;
259         int count, ret;
260
261         devc = sdi->priv;
262         devc->cb_data = cb_data;
263
264         sr_dbg("Setting audio access type to RW/interleaved.");
265         ret = snd_pcm_hw_params_set_access(devc->capture_handle,
266                         devc->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
267         if (ret < 0) {
268                 sr_err("Can't set audio access type: %s.", snd_strerror(ret));
269                 return SR_ERR;
270         }
271
272         /* FIXME: Hardcoded for 16bits. */
273         sr_dbg("Setting audio sample format to signed 16bit (little endian).");
274         ret = snd_pcm_hw_params_set_format(devc->capture_handle,
275                         devc->hw_params, SND_PCM_FORMAT_S16_LE);
276         if (ret < 0) {
277                 sr_err("Can't set audio sample format: %s.", snd_strerror(ret));
278                 return SR_ERR;
279         }
280
281         sr_dbg("Setting audio samplerate to %" PRIu64 "Hz.",
282                devc->cur_samplerate);
283         ret = snd_pcm_hw_params_set_rate_near(devc->capture_handle,
284                 devc->hw_params, (unsigned int *)&devc->cur_samplerate, 0);
285         if (ret < 0) {
286                 sr_err("Can't set audio sample rate: %s.", snd_strerror(ret));
287                 return SR_ERR;
288         }
289
290         sr_dbg("Setting audio channel count to %d.", devc->num_probes);
291         ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
292                                              devc->hw_params, devc->num_probes);
293         if (ret < 0) {
294                 sr_err("Can't set channel count: %s.", snd_strerror(ret));
295                 return SR_ERR;
296         }
297
298         sr_dbg("Setting audio parameters.");
299         ret = snd_pcm_hw_params(devc->capture_handle, devc->hw_params);
300         if (ret < 0) {
301                 sr_err("Can't set parameters: %s.", snd_strerror(ret));
302                 return SR_ERR;
303         }
304
305         sr_dbg("Preparing audio interface for use.");
306         ret = snd_pcm_prepare(devc->capture_handle);
307         if (ret < 0) {
308                 sr_err("Can't prepare audio interface for use: %s.",
309                        snd_strerror(ret));
310                 return SR_ERR;
311         }
312
313         count = snd_pcm_poll_descriptors_count(devc->capture_handle);
314         if (count < 1) {
315                 sr_err("Unable to obtain poll descriptors count.");
316                 return SR_ERR;
317         }
318         sr_spew("Obtained poll descriptor count: %d.", count);
319
320         if (!(devc->ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
321                 sr_err("Failed to malloc ufds.");
322                 return SR_ERR_MALLOC;
323         }
324
325         sr_err("Getting %d poll descriptors.", count);
326         ret = snd_pcm_poll_descriptors(devc->capture_handle, devc->ufds, count);
327         if (ret < 0) {
328                 sr_err("Unable to obtain poll descriptors: %s.",
329                        snd_strerror(ret));
330                 g_free(devc->ufds);
331                 return SR_ERR;
332         }
333
334         /* Send header packet to the session bus. */
335         sr_dbg("Sending SR_DF_HEADER packet.");
336         packet.type = SR_DF_HEADER;
337         packet.payload = (uint8_t *)&header;
338         header.feed_version = 1;
339         gettimeofday(&header.starttime, NULL);
340         sr_session_send(devc->cb_data, &packet);
341
342         /* Send metadata about the SR_DF_ANALOG packets to come. */
343         sr_dbg("Sending SR_DF_META_ANALOG packet.");
344         packet.type = SR_DF_META_ANALOG;
345         packet.payload = &meta;
346         meta.num_probes = devc->num_probes;
347         sr_session_send(devc->cb_data, &packet);
348
349         /* Poll every 10ms, or whenever some data comes in. */
350         sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
351                       alsa_receive_data, (void *)sdi);
352
353         // g_free(devc->ufds); /* FIXME */
354
355         return SR_OK;
356 }
357
358 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
359 {
360         struct sr_datafeed_packet packet;
361         struct dev_context *devc;
362
363         devc = sdi->priv;
364         devc->cb_data = cb_data;
365
366         sr_source_remove(devc->ufds[0].fd);
367
368         /* Send end packet to the session bus. */
369         sr_dbg("Sending SR_DF_END packet.");
370         packet.type = SR_DF_END;
371         sr_session_send(cb_data, &packet);
372
373         return SR_OK;
374 }
375
376 SR_PRIV struct sr_dev_driver alsa_driver_info = {
377         .name = "alsa",
378         .longname = "ALSA driver",
379         .api_version = 1,
380         .init = hw_init,
381         .cleanup = hw_cleanup,
382         .scan = hw_scan,
383         .dev_list = hw_dev_list,
384         .dev_clear = clear_instances,
385         .dev_open = hw_dev_open,
386         .dev_close = hw_dev_close,
387         .info_get = hw_info_get,
388         .dev_config_set = hw_dev_config_set,
389         .dev_acquisition_start = hw_dev_acquisition_start,
390         .dev_acquisition_stop = hw_dev_acquisition_stop,
391         .priv = NULL,
392 };