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