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