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