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