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