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