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