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