]> sigrok.org Git - libsigrok.git/blob - hardware/alsa/protocol.c
1c1d8849dd2a626d69bc752c106aedb60a95bf8a
[libsigrok.git] / hardware / alsa / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include "protocol.h"
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26
27 static void alsa_scan_handle_dev(GSList **devices,
28                                  const char *cardname, const char *alsaname,
29                                  struct sr_dev_driver *di,
30                                  snd_pcm_info_t *pcminfo)
31 {
32         struct drv_context *drvc = NULL;
33         struct sr_dev_inst *sdi = NULL;
34         struct dev_context *devc = NULL;
35         struct sr_probe *probe;
36         int ret;
37         unsigned int i, channels;
38         snd_pcm_t *temp_handle = NULL;
39         snd_pcm_hw_params_t *hw_params = NULL;
40
41         drvc = di->priv;
42
43         /*
44          * Get the number of input channels. Those are our sigrok probes.
45          * Getting this information needs a detour. We need to open the device,
46          * then query it for the number of channels. A side-effect of is that we
47          * create a snd_pcm_hw_params_t object. We take advantage of the
48          * situation, and pass this object in our dev_context->hw_params,
49          * eliminating the need to free() it and malloc() it later.
50          */
51         ret = snd_pcm_open(&temp_handle, alsaname, SND_PCM_STREAM_CAPTURE, 0);
52         if (ret < 0) {
53                 sr_err("Cannot open device: %s.", snd_strerror(ret));
54                 goto scan_error_cleanup;
55         }
56
57         ret = snd_pcm_hw_params_malloc(&hw_params);
58         if (ret < 0) {
59                 sr_err("Error allocating hardware parameter structure: %s.",
60                        snd_strerror(ret));
61                 goto scan_error_cleanup;
62         }
63
64         ret = snd_pcm_hw_params_any(temp_handle, hw_params);
65         if (ret < 0) {
66                 sr_err("Error initializing hardware parameter structure: %s.",
67                        snd_strerror(ret));
68                 goto scan_error_cleanup;
69         }
70
71         snd_pcm_hw_params_get_channels_max(hw_params, &channels);
72
73         snd_pcm_close(temp_handle);
74         temp_handle = NULL;
75
76         /*
77          * Now we are done querying the number of channels
78          * If we made it here, then it's time to create our sigrok device.
79          */
80         sr_info("Device %s has %d channels.", alsaname, channels);
81         if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "ALSA:",
82                 cardname, snd_pcm_info_get_name(pcminfo)))) {
83                 sr_err("Device instance malloc failed.");
84                 goto scan_error_cleanup;
85         }
86         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
87                 sr_err("Device context malloc failed.");
88                 goto scan_error_cleanup;
89         }
90
91         devc->hwdev = g_strdup(alsaname);
92         devc->num_probes = channels;
93         devc->hw_params = hw_params;
94         sdi->priv = devc;
95         sdi->driver = di;
96
97         for (i = 0; i < devc->num_probes; i++) {
98                 char p_name[32];
99                 snprintf(p_name, sizeof(p_name), "Ch_%d", i);
100                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, p_name)))
101                         goto scan_error_cleanup;
102                 sdi->probes = g_slist_append(sdi->probes, probe);
103         }
104
105         drvc->instances = g_slist_append(drvc->instances, sdi);
106         *devices = g_slist_append(*devices, sdi);
107         return;
108
109 scan_error_cleanup:
110         if (devc) {
111                 if (devc->hwdev)
112                         g_free((void*)devc->hwdev);
113                 g_free(devc);
114         }
115         if (sdi)
116                 sr_dev_inst_free(sdi);
117         if (hw_params)
118                 snd_pcm_hw_params_free(hw_params);
119         if (temp_handle)
120                 snd_pcm_close(temp_handle);
121         return;
122 }
123
124 /**
125  * \brief Scan all alsa devices, and translate them to sigrok devices
126  *
127  * Each alsa device (not alsa card) gets its own sigrok device
128  * For example,
129  *     hw:1,0 == sigrok device 0
130  *     hw:1,1 == sigrok device 1
131  *     hw:2,0 == sigrok device 2
132  *     hw:2,1 == sigrok device 3
133  *     hw:2,2 == sigrok device 4
134  *     [...]
135  * \n
136  * We don't currently look at alsa subdevices. We only use subdevice 0.
137  * Every input device will have a its own channels (Left, Right, etc). Each of
138  * those channels gets mapped to a different sigrok probe. A device with 4
139  * channels will have 4 probes from sigrok's perspective.
140  */
141 SR_PRIV GSList *alsa_scan(GSList *options, struct sr_dev_driver *di)
142 {
143         GSList *devices = NULL;
144         snd_ctl_t *handle;
145         int card, ret, dev;
146         snd_ctl_card_info_t *info;
147         snd_pcm_info_t *pcminfo;
148         const char* cardname;
149         /* TODO */
150         (void)options;
151
152         if (snd_ctl_card_info_malloc(&info) < 0) {
153                 sr_err("Cannot malloc card info.");
154                 return NULL;
155         }
156         if (snd_pcm_info_malloc(&pcminfo) < 0) {
157                 sr_err("Cannot malloc pcm info.");
158                 return NULL;
159         }
160
161         card = -1;
162         while (snd_card_next(&card) >= 0 && card >= 0) {
163                 char hwcard[32];
164                 snprintf(hwcard, sizeof(hwcard), "hw:%d", card);
165                 if ((ret = snd_ctl_open(&handle, hwcard, 0)) < 0) {
166                         sr_err("Cannot open (%i): %s", card, snd_strerror(ret));
167                         continue;
168                 }
169                 if ((ret = snd_ctl_card_info(handle, info)) < 0) {
170                         sr_err("Cannot get hardware info (%i): %s",
171                                card, snd_strerror(ret));
172                         snd_ctl_close(handle);
173                         continue;
174                 }
175                 dev = -1;
176                 while (snd_ctl_pcm_next_device(handle, &dev) >= 0 && dev >= 0) {
177                         char hwdev[32];
178                         snprintf(hwdev, sizeof(hwdev), "%s,%d", hwcard, dev);
179                         /*
180                          * TODO: We always use subdevice 0, but we have yet to
181                          * explore the possibilities opened up by other
182                          * subdevices. Most hardware only has subdevice 0.
183                          */
184                         snd_pcm_info_set_device(pcminfo, dev);
185                         snd_pcm_info_set_subdevice(pcminfo, 0);
186                         snd_pcm_info_set_stream(pcminfo,
187                                                 SND_PCM_STREAM_CAPTURE);
188                         if ((ret = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
189                                 sr_err("Cannot get device info: %s",
190                                        snd_strerror(ret));
191                                 continue;
192                         }
193
194                         cardname = snd_ctl_card_info_get_name(info);
195                         sr_info("card %i: %s [%s], device %i: %s [%s]",
196                                card, snd_ctl_card_info_get_id(info), cardname,
197                                dev, snd_pcm_info_get_id(pcminfo),
198                                snd_pcm_info_get_name(pcminfo));
199
200                         alsa_scan_handle_dev(&devices, cardname, hwdev,
201                                              di, pcminfo);
202                 }
203                 snd_ctl_close(handle);
204         }
205
206         snd_pcm_info_free(pcminfo);
207         snd_ctl_card_info_free(info);
208
209         return devices;
210 }
211
212 /*
213  * Helper to be used with g_slist_free_full(); for properly freeing an alsa
214  * dev instance.
215  */
216 SR_PRIV void alsa_dev_inst_clear(struct sr_dev_inst *sdi)
217 {
218         struct dev_context *devc;
219
220         if (!(devc = sdi->priv))
221                 return;
222
223         snd_pcm_hw_params_free(devc->hw_params);
224         sr_dev_inst_free(sdi);
225 }
226
227 SR_PRIV int alsa_receive_data(int fd, int revents, void *cb_data)
228 {
229         struct sr_dev_inst *sdi;
230         struct dev_context *devc;
231         struct sr_datafeed_packet packet;
232         struct sr_datafeed_analog analog;
233         char inbuf[4096];
234         int i, x, count, offset, samples_to_get;
235         uint16_t tmp16;
236
237         (void)fd;
238         (void)revents;
239
240         sdi = cb_data;
241         devc = sdi->priv;
242
243         memset(&analog, 0, sizeof(struct sr_datafeed_analog));
244         memset(inbuf, 0, sizeof(inbuf));
245
246         samples_to_get = MIN(4096 / 4, devc->limit_samples);
247
248         sr_spew("Getting %d samples from audio device.", samples_to_get);
249         count = snd_pcm_readi(devc->capture_handle, inbuf, samples_to_get);
250
251         if (count < 0) {
252                 sr_err("Failed to read samples: %s.", snd_strerror(count));
253                 return FALSE;
254         } else if (count != samples_to_get) {
255                 sr_spew("Only got %d/%d samples.", count, samples_to_get);
256         }
257
258         analog.data = g_try_malloc0(count * sizeof(float) * devc->num_probes);
259         if (!analog.data) {
260                 sr_err("Failed to malloc sample buffer.");
261                 return FALSE;
262         }
263
264         offset = 0;
265
266         for (i = 0; i < count; i++) {
267                 for (x = 0; x < devc->num_probes; x++) {
268                         tmp16 = *(uint16_t *)(inbuf + (i * 4) + (x * 2));
269                         analog.data[offset++] = (float)tmp16;
270                 }
271         }
272
273         /* Send a sample packet with the analog values. */
274         analog.num_samples = count;
275         analog.mq = SR_MQ_VOLTAGE; /* FIXME */
276         analog.unit = SR_UNIT_VOLT; /* FIXME */
277         packet.type = SR_DF_ANALOG;
278         packet.payload = &analog;
279         sr_session_send(devc->cb_data, &packet);
280
281         g_free(analog.data);
282
283         devc->num_samples += count;
284
285         /* Stop acquisition if we acquired enough samples. */
286         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
287                 sr_info("Requested number of samples reached.");
288                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
289         }
290
291         return TRUE;
292 }
293