blob: 0202d463065c1d755f3602497536f2fb505e3526 [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* pcm.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <stdarg.h>
33#include <string.h>
34#include <errno.h>
35#include <unistd.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070036#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40#include <sys/time.h>
Dima Krasner696c4482016-03-05 19:50:02 +020041#include <time.h>
Liam Girdwood6be28f12011-10-13 12:59:51 -070042#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070043
44#include <linux/ioctl.h>
45#define __force
46#define __bitwise
47#define __user
48#include <sound/asound.h>
49
50#include <tinyalsa/asoundlib.h>
51
52#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
Liam Girdwood6be28f12011-10-13 12:59:51 -070053#define SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP (1<<2)
Simon Wilson79d39652011-05-25 13:44:23 -070054
55static inline int param_is_mask(int p)
56{
57 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
58 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
59}
60
61static inline int param_is_interval(int p)
62{
63 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
64 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
65}
66
67static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
68{
69 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
70}
71
72static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
73{
74 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
75}
76
77static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
78{
79 if (bit >= SNDRV_MASK_MAX)
80 return;
81 if (param_is_mask(n)) {
82 struct snd_mask *m = param_to_mask(p, n);
83 m->bits[0] = 0;
84 m->bits[1] = 0;
85 m->bits[bit >> 5] |= (1 << (bit & 31));
86 }
87}
88
89static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
90{
91 if (param_is_interval(n)) {
92 struct snd_interval *i = param_to_interval(p, n);
93 i->min = val;
94 }
95}
96
Simon Wilson43544882012-10-31 12:52:39 -070097static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
98{
99 if (param_is_interval(n)) {
100 struct snd_interval *i = param_to_interval(p, n);
101 return i->min;
102 }
103 return 0;
104}
105
106static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
107{
108 if (param_is_interval(n)) {
109 struct snd_interval *i = param_to_interval(p, n);
110 return i->max;
111 }
112 return 0;
113}
114
Simon Wilson79d39652011-05-25 13:44:23 -0700115static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
116{
117 if (param_is_interval(n)) {
118 struct snd_interval *i = param_to_interval(p, n);
119 i->min = val;
120 i->max = val;
121 i->integer = 1;
122 }
123}
124
Liam Girdwood6be28f12011-10-13 12:59:51 -0700125static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
126{
127 if (param_is_interval(n)) {
128 struct snd_interval *i = param_to_interval(p, n);
129 if (i->integer)
130 return i->max;
131 }
132 return 0;
133}
134
Simon Wilson79d39652011-05-25 13:44:23 -0700135static void param_init(struct snd_pcm_hw_params *p)
136{
137 int n;
Simon Wilson98c1f162011-06-07 16:12:32 -0700138
Simon Wilson79d39652011-05-25 13:44:23 -0700139 memset(p, 0, sizeof(*p));
140 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
141 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
142 struct snd_mask *m = param_to_mask(p, n);
143 m->bits[0] = ~0;
144 m->bits[1] = ~0;
145 }
146 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
147 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
148 struct snd_interval *i = param_to_interval(p, n);
149 i->min = 0;
150 i->max = ~0;
151 }
Simon Wilson43544882012-10-31 12:52:39 -0700152 p->rmask = ~0U;
153 p->cmask = 0;
154 p->info = ~0U;
Simon Wilson79d39652011-05-25 13:44:23 -0700155}
156
157#define PCM_ERROR_MAX 128
158
159struct pcm {
160 int fd;
161 unsigned int flags;
162 int running:1;
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530163 int prepared:1;
Simon Wilson79d39652011-05-25 13:44:23 -0700164 int underruns;
165 unsigned int buffer_size;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700166 unsigned int boundary;
Simon Wilson79d39652011-05-25 13:44:23 -0700167 char error[PCM_ERROR_MAX];
168 struct pcm_config config;
Eric Laurent40b018e2011-06-18 10:10:23 -0700169 struct snd_pcm_mmap_status *mmap_status;
170 struct snd_pcm_mmap_control *mmap_control;
171 struct snd_pcm_sync_ptr *sync_ptr;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700172 void *mmap_buffer;
173 unsigned int noirq_frames_per_msec;
Hardik T Shah9ecb93f2014-04-10 18:03:52 +0530174 long pcm_delay;
David Wagner4cddf192014-04-02 15:12:54 +0200175 unsigned int subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700176};
177
Simon Wilson851aa5c2011-05-30 21:18:26 -0700178unsigned int pcm_get_buffer_size(struct pcm *pcm)
Simon Wilson79d39652011-05-25 13:44:23 -0700179{
180 return pcm->buffer_size;
181}
182
Taylor Holbertonbb402602016-08-03 10:15:46 -0400183int pcm_get_file_descriptor(struct pcm *pcm)
184{
185 return pcm->fd;
186}
187
Simon Wilson79d39652011-05-25 13:44:23 -0700188const char* pcm_get_error(struct pcm *pcm)
189{
190 return pcm->error;
191}
192
David Wagner4cddf192014-04-02 15:12:54 +0200193unsigned int pcm_get_subdevice(struct pcm *pcm)
194{
195 return pcm->subdevice;
196}
197
Simon Wilson79d39652011-05-25 13:44:23 -0700198static int oops(struct pcm *pcm, int e, const char *fmt, ...)
199{
200 va_list ap;
201 int sz;
202
203 va_start(ap, fmt);
204 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
205 va_end(ap);
206 sz = strlen(pcm->error);
207
208 if (errno)
209 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
210 ": %s", strerror(e));
211 return -1;
212}
213
Simon Wilsonbc03b622011-06-15 17:19:01 -0700214static unsigned int pcm_format_to_alsa(enum pcm_format format)
215{
216 switch (format) {
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400217
Gabriel M. Beddingfield2a274a12012-05-02 11:51:20 -0500218 case PCM_FORMAT_S8:
219 return SNDRV_PCM_FORMAT_S8;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400220
Simon Wilsonbc03b622011-06-15 17:19:01 -0700221 default:
222 case PCM_FORMAT_S16_LE:
223 return SNDRV_PCM_FORMAT_S16_LE;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400224 case PCM_FORMAT_S16_BE:
225 return SNDRV_PCM_FORMAT_S16_BE;
226
227 case PCM_FORMAT_S24_LE:
228 return SNDRV_PCM_FORMAT_S24_LE;
229 case PCM_FORMAT_S24_BE:
230 return SNDRV_PCM_FORMAT_S24_BE;
231
232 case PCM_FORMAT_S24_3LE:
233 return SNDRV_PCM_FORMAT_S24_3LE;
234 case PCM_FORMAT_S24_3BE:
235 return SNDRV_PCM_FORMAT_S24_3BE;
236
237 case PCM_FORMAT_S32_LE:
238 return SNDRV_PCM_FORMAT_S32_LE;
239 case PCM_FORMAT_S32_BE:
240 return SNDRV_PCM_FORMAT_S32_BE;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700241 };
242}
243
Simon Wilson7136cf72013-07-17 10:30:35 -0700244unsigned int pcm_format_to_bits(enum pcm_format format)
Simon Wilsonbc03b622011-06-15 17:19:01 -0700245{
246 switch (format) {
247 case PCM_FORMAT_S32_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400248 case PCM_FORMAT_S32_BE:
Simon Wilson7136cf72013-07-17 10:30:35 -0700249 case PCM_FORMAT_S24_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400250 case PCM_FORMAT_S24_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700251 return 32;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400252 case PCM_FORMAT_S24_3LE:
253 case PCM_FORMAT_S24_3BE:
254 return 24;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700255 default:
256 case PCM_FORMAT_S16_LE:
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400257 case PCM_FORMAT_S16_BE:
Simon Wilsonbc03b622011-06-15 17:19:01 -0700258 return 16;
Taylor Holbertonc01d4a32016-10-01 12:22:43 -0400259 case PCM_FORMAT_S8:
260 return 8;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700261 };
262}
263
Liam Girdwood6be28f12011-10-13 12:59:51 -0700264unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
265{
266 return bytes / (pcm->config.channels *
267 (pcm_format_to_bits(pcm->config.format) >> 3));
268}
269
270unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
271{
272 return frames * pcm->config.channels *
273 (pcm_format_to_bits(pcm->config.format) >> 3);
274}
275
Taylor Holberton4f556062016-09-16 09:54:36 -0400276static int pcm_sync_ptr(struct pcm *pcm, int flags)
277{
Eric Laurent40b018e2011-06-18 10:10:23 -0700278 if (pcm->sync_ptr) {
279 pcm->sync_ptr->flags = flags;
280 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
281 return -1;
282 }
283 return 0;
284}
285
Taylor Holberton4f556062016-09-16 09:54:36 -0400286static int pcm_hw_mmap_status(struct pcm *pcm)
287{
Eric Laurent40b018e2011-06-18 10:10:23 -0700288 if (pcm->sync_ptr)
289 return 0;
290
291 int page_size = sysconf(_SC_PAGE_SIZE);
292 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
293 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
294 if (pcm->mmap_status == MAP_FAILED)
295 pcm->mmap_status = NULL;
296 if (!pcm->mmap_status)
297 goto mmap_error;
298
299 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
300 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
301 if (pcm->mmap_control == MAP_FAILED)
302 pcm->mmap_control = NULL;
303 if (!pcm->mmap_control) {
304 munmap(pcm->mmap_status, page_size);
305 pcm->mmap_status = NULL;
306 goto mmap_error;
307 }
308 pcm->mmap_control->avail_min = 1;
309
310 return 0;
311
312mmap_error:
313
314 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
315 if (!pcm->sync_ptr)
316 return -ENOMEM;
317 pcm->mmap_status = &pcm->sync_ptr->s.status;
318 pcm->mmap_control = &pcm->sync_ptr->c.control;
319 pcm->mmap_control->avail_min = 1;
320 pcm_sync_ptr(pcm, 0);
321
322 return 0;
323}
324
325static void pcm_hw_munmap_status(struct pcm *pcm) {
326 if (pcm->sync_ptr) {
327 free(pcm->sync_ptr);
328 pcm->sync_ptr = NULL;
329 } else {
330 int page_size = sysconf(_SC_PAGE_SIZE);
331 if (pcm->mmap_status)
332 munmap(pcm->mmap_status, page_size);
333 if (pcm->mmap_control)
334 munmap(pcm->mmap_control, page_size);
335 }
336 pcm->mmap_status = NULL;
337 pcm->mmap_control = NULL;
338}
339
Liam Girdwood6be28f12011-10-13 12:59:51 -0700340static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700341 char *buf, unsigned int src_offset,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700342 unsigned int frames)
343{
344 int size_bytes = pcm_frames_to_bytes(pcm, frames);
345 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
346 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
347
348 /* interleaved only atm */
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700349 if (pcm->flags & PCM_IN)
350 memcpy(buf + src_offset_bytes,
351 (char*)pcm->mmap_buffer + pcm_offset_bytes,
352 size_bytes);
353 else
354 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
355 buf + src_offset_bytes,
356 size_bytes);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700357 return 0;
358}
359
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700360static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
Liam Girdwood6be28f12011-10-13 12:59:51 -0700361 unsigned int offset, unsigned int size)
362{
363 void *pcm_areas;
364 int commit;
365 unsigned int pcm_offset, frames, count = 0;
366
367 while (size > 0) {
368 frames = size;
369 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700370 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -0700371 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
372 if (commit < 0) {
373 oops(pcm, commit, "failed to commit %d frames\n", frames);
374 return commit;
375 }
376
377 offset += commit;
378 count += commit;
379 size -= commit;
380 }
381 return count;
382}
383
Eric Laurent40b018e2011-06-18 10:10:23 -0700384int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
385 struct timespec *tstamp)
386{
387 int frames;
388 int rc;
389 snd_pcm_uframes_t hw_ptr;
390
391 if (!pcm_is_ready(pcm))
392 return -1;
393
394 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
395 if (rc < 0)
396 return -1;
397
Eric Laurent7db48582011-11-17 11:47:59 -0800398 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
399 (pcm->mmap_status->state != PCM_STATE_DRAINING))
Eric Laurentee9ba872011-11-15 19:04:03 -0800400 return -1;
401
Eric Laurent40b018e2011-06-18 10:10:23 -0700402 *tstamp = pcm->mmap_status->tstamp;
403 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
404 return -1;
405
406 hw_ptr = pcm->mmap_status->hw_ptr;
407 if (pcm->flags & PCM_IN)
408 frames = hw_ptr - pcm->mmap_control->appl_ptr;
409 else
410 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
411
412 if (frames < 0)
413 return -1;
414
415 *avail = (unsigned int)frames;
416
417 return 0;
418}
419
Mark Brown6bbe77a2012-02-10 22:07:09 +0000420int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
Simon Wilson79d39652011-05-25 13:44:23 -0700421{
422 struct snd_xferi x;
423
424 if (pcm->flags & PCM_IN)
425 return -EINVAL;
426
Mark Brown6bbe77a2012-02-10 22:07:09 +0000427 x.buf = (void*)data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700428 x.frames = count / (pcm->config.channels *
429 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700430
431 for (;;) {
432 if (!pcm->running) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530433 int prepare_error = pcm_prepare(pcm);
434 if (prepare_error)
435 return prepare_error;
Simon Wilson79d39652011-05-25 13:44:23 -0700436 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
437 return oops(pcm, errno, "cannot write initial data");
438 pcm->running = 1;
439 return 0;
440 }
441 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530442 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700443 pcm->running = 0;
444 if (errno == EPIPE) {
John Grossmanb6db70a2012-04-03 16:37:38 -0700445 /* we failed to make our window -- try to restart if we are
446 * allowed to do so. Otherwise, simply allow the EPIPE error to
447 * propagate up to the app level */
Simon Wilson79d39652011-05-25 13:44:23 -0700448 pcm->underruns++;
John Grossmanb6db70a2012-04-03 16:37:38 -0700449 if (pcm->flags & PCM_NORESTART)
450 return -EPIPE;
Simon Wilson79d39652011-05-25 13:44:23 -0700451 continue;
452 }
453 return oops(pcm, errno, "cannot write stream data");
454 }
455 return 0;
456 }
457}
458
459int pcm_read(struct pcm *pcm, void *data, unsigned int count)
460{
461 struct snd_xferi x;
462
463 if (!(pcm->flags & PCM_IN))
464 return -EINVAL;
465
466 x.buf = data;
Simon Wilsonbc03b622011-06-15 17:19:01 -0700467 x.frames = count / (pcm->config.channels *
468 pcm_format_to_bits(pcm->config.format) / 8);
Simon Wilson79d39652011-05-25 13:44:23 -0700469
470 for (;;) {
471 if (!pcm->running) {
Keunyoung2581a1e2012-05-10 10:50:00 -0700472 if (pcm_start(pcm) < 0) {
473 fprintf(stderr, "start error");
474 return -errno;
475 }
Simon Wilson79d39652011-05-25 13:44:23 -0700476 }
477 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530478 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700479 pcm->running = 0;
480 if (errno == EPIPE) {
481 /* we failed to make our window -- try to restart */
482 pcm->underruns++;
483 continue;
484 }
485 return oops(pcm, errno, "cannot read stream data");
486 }
487 return 0;
488 }
489}
490
491static struct pcm bad_pcm = {
492 .fd = -1,
493};
494
Simon Wilson43544882012-10-31 12:52:39 -0700495struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
496 unsigned int flags)
497{
498 struct snd_pcm_hw_params *params;
499 char fn[256];
500 int fd;
501
502 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
503 flags & PCM_IN ? 'c' : 'p');
504
505 fd = open(fn, O_RDWR);
506 if (fd < 0) {
507 fprintf(stderr, "cannot open device '%s'\n", fn);
508 goto err_open;
509 }
510
511 params = calloc(1, sizeof(struct snd_pcm_hw_params));
512 if (!params)
513 goto err_calloc;
514
515 param_init(params);
516 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
517 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
518 goto err_hw_refine;
519 }
520
521 close(fd);
522
523 return (struct pcm_params *)params;
524
525err_hw_refine:
526 free(params);
527err_calloc:
528 close(fd);
529err_open:
530 return NULL;
531}
532
533void pcm_params_free(struct pcm_params *pcm_params)
534{
535 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
536
537 if (params)
538 free(params);
539}
540
541static int pcm_param_to_alsa(enum pcm_param param)
542{
543 switch (param) {
Andy Hungad807622014-03-10 18:08:15 -0700544 case PCM_PARAM_ACCESS:
545 return SNDRV_PCM_HW_PARAM_ACCESS;
546 case PCM_PARAM_FORMAT:
547 return SNDRV_PCM_HW_PARAM_FORMAT;
548 case PCM_PARAM_SUBFORMAT:
549 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
Simon Wilson43544882012-10-31 12:52:39 -0700550 case PCM_PARAM_SAMPLE_BITS:
551 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
552 break;
553 case PCM_PARAM_FRAME_BITS:
554 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
555 break;
556 case PCM_PARAM_CHANNELS:
557 return SNDRV_PCM_HW_PARAM_CHANNELS;
558 break;
559 case PCM_PARAM_RATE:
560 return SNDRV_PCM_HW_PARAM_RATE;
561 break;
562 case PCM_PARAM_PERIOD_TIME:
563 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
564 break;
565 case PCM_PARAM_PERIOD_SIZE:
566 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
567 break;
568 case PCM_PARAM_PERIOD_BYTES:
569 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
570 break;
571 case PCM_PARAM_PERIODS:
572 return SNDRV_PCM_HW_PARAM_PERIODS;
573 break;
574 case PCM_PARAM_BUFFER_TIME:
575 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
576 break;
577 case PCM_PARAM_BUFFER_SIZE:
578 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
579 break;
580 case PCM_PARAM_BUFFER_BYTES:
581 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
582 break;
583 case PCM_PARAM_TICK_TIME:
584 return SNDRV_PCM_HW_PARAM_TICK_TIME;
585 break;
586
587 default:
588 return -1;
589 }
590}
591
Andy Hungad807622014-03-10 18:08:15 -0700592struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
593 enum pcm_param param)
594{
595 int p;
596 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
597 if (params == NULL) {
598 return NULL;
599 }
600
601 p = pcm_param_to_alsa(param);
602 if (p < 0 || !param_is_mask(p)) {
603 return NULL;
604 }
605
606 return (struct pcm_mask *)param_to_mask(params, p);
607}
608
Simon Wilson43544882012-10-31 12:52:39 -0700609unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
610 enum pcm_param param)
611{
612 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
613 int p;
614
615 if (!params)
616 return 0;
617
618 p = pcm_param_to_alsa(param);
619 if (p < 0)
620 return 0;
621
622 return param_get_min(params, p);
623}
624
625unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
626 enum pcm_param param)
627{
628 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
629 int p;
630
631 if (!params)
632 return 0;
633
634 p = pcm_param_to_alsa(param);
635 if (p < 0)
636 return 0;
637
638 return param_get_max(params, p);
639}
640
Simon Wilson79d39652011-05-25 13:44:23 -0700641int pcm_close(struct pcm *pcm)
642{
643 if (pcm == &bad_pcm)
644 return 0;
645
Eric Laurent40b018e2011-06-18 10:10:23 -0700646 pcm_hw_munmap_status(pcm);
647
Liam Girdwood6be28f12011-10-13 12:59:51 -0700648 if (pcm->flags & PCM_MMAP) {
649 pcm_stop(pcm);
650 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
651 }
652
Simon Wilson79d39652011-05-25 13:44:23 -0700653 if (pcm->fd >= 0)
654 close(pcm->fd);
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530655 pcm->prepared = 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700656 pcm->running = 0;
657 pcm->buffer_size = 0;
658 pcm->fd = -1;
Eric Laurent40b018e2011-06-18 10:10:23 -0700659 free(pcm);
Simon Wilson79d39652011-05-25 13:44:23 -0700660 return 0;
661}
662
Simon Wilson1bd580f2011-06-02 15:58:41 -0700663struct pcm *pcm_open(unsigned int card, unsigned int device,
664 unsigned int flags, struct pcm_config *config)
Simon Wilson79d39652011-05-25 13:44:23 -0700665{
Simon Wilson79d39652011-05-25 13:44:23 -0700666 struct pcm *pcm;
667 struct snd_pcm_info info;
668 struct snd_pcm_hw_params params;
669 struct snd_pcm_sw_params sparams;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700670 char fn[256];
Eric Laurent40b018e2011-06-18 10:10:23 -0700671 int rc;
Simon Wilson79d39652011-05-25 13:44:23 -0700672
673 pcm = calloc(1, sizeof(struct pcm));
674 if (!pcm || !config)
675 return &bad_pcm; /* TODO: could support default config here */
676
677 pcm->config = *config;
678
Simon Wilson1bd580f2011-06-02 15:58:41 -0700679 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
680 flags & PCM_IN ? 'c' : 'p');
Simon Wilson79d39652011-05-25 13:44:23 -0700681
682 pcm->flags = flags;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700683 pcm->fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700684 if (pcm->fd < 0) {
Simon Wilson1bd580f2011-06-02 15:58:41 -0700685 oops(pcm, errno, "cannot open device '%s'", fn);
Simon Wilson79d39652011-05-25 13:44:23 -0700686 return pcm;
687 }
688
689 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
Simon Wilson851aa5c2011-05-30 21:18:26 -0700690 oops(pcm, errno, "cannot get info");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700691 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700692 }
David Wagner4cddf192014-04-02 15:12:54 +0200693 pcm->subdevice = info.subdevice;
Simon Wilson79d39652011-05-25 13:44:23 -0700694
695 param_init(&params);
Simon Wilson79d39652011-05-25 13:44:23 -0700696 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
697 pcm_format_to_alsa(config->format));
698 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
699 SNDRV_PCM_SUBFORMAT_STD);
700 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
701 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
702 pcm_format_to_bits(config->format));
703 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
704 pcm_format_to_bits(config->format) * config->channels);
705 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
706 config->channels);
707 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
708 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
709
Liam Girdwood6be28f12011-10-13 12:59:51 -0700710 if (flags & PCM_NOIRQ) {
711
712 if (!(flags & PCM_MMAP)) {
713 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
714 goto fail;
715 }
716
717 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
718 pcm->noirq_frames_per_msec = config->rate / 1000;
719 }
720
721 if (flags & PCM_MMAP)
722 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
723 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
724 else
725 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
726 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
727
Simon Wilson79d39652011-05-25 13:44:23 -0700728 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
729 oops(pcm, errno, "cannot set hw params");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700730 goto fail_close;
Simon Wilson79d39652011-05-25 13:44:23 -0700731 }
732
Liam Girdwood6be28f12011-10-13 12:59:51 -0700733 /* get our refined hw_params */
734 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
735 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
736 pcm->buffer_size = config->period_count * config->period_size;
737
738 if (flags & PCM_MMAP) {
739 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
740 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
741 if (pcm->mmap_buffer == MAP_FAILED) {
742 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
743 pcm_frames_to_bytes(pcm, pcm->buffer_size));
744 goto fail_close;
745 }
746 }
747
748
Simon Wilson79d39652011-05-25 13:44:23 -0700749 memset(&sparams, 0, sizeof(sparams));
Eric Laurent40b018e2011-06-18 10:10:23 -0700750 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
Simon Wilson79d39652011-05-25 13:44:23 -0700751 sparams.period_step = 1;
752 sparams.avail_min = 1;
John Grossman3bb114a2011-07-21 10:59:55 -0700753
Eric Laurent93e7b672012-08-22 16:18:14 -0700754 if (!config->start_threshold) {
755 if (pcm->flags & PCM_IN)
756 pcm->config.start_threshold = sparams.start_threshold = 1;
757 else
758 pcm->config.start_threshold = sparams.start_threshold =
759 config->period_count * config->period_size / 2;
760 } else
John Grossman3bb114a2011-07-21 10:59:55 -0700761 sparams.start_threshold = config->start_threshold;
762
Liam Girdwood6be28f12011-10-13 12:59:51 -0700763 /* pick a high stop threshold - todo: does this need further tuning */
Eric Laurent35021132012-01-30 11:31:56 -0800764 if (!config->stop_threshold) {
765 if (pcm->flags & PCM_IN)
766 pcm->config.stop_threshold = sparams.stop_threshold =
767 config->period_count * config->period_size * 10;
768 else
769 pcm->config.stop_threshold = sparams.stop_threshold =
770 config->period_count * config->period_size;
771 }
John Grossman3bb114a2011-07-21 10:59:55 -0700772 else
773 sparams.stop_threshold = config->stop_threshold;
774
Simon Wilson79d39652011-05-25 13:44:23 -0700775 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
776 sparams.silence_size = 0;
John Grossman3bb114a2011-07-21 10:59:55 -0700777 sparams.silence_threshold = config->silence_threshold;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700778 pcm->boundary = sparams.boundary = pcm->buffer_size;
John Grossman3bb114a2011-07-21 10:59:55 -0700779
Gabriel M. Beddingfield80085d42012-02-08 16:53:32 -0600780 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700781 pcm->boundary *= 2;
Simon Wilson79d39652011-05-25 13:44:23 -0700782
783 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
784 oops(pcm, errno, "cannot set sw params");
785 goto fail;
786 }
787
Eric Laurent40b018e2011-06-18 10:10:23 -0700788 rc = pcm_hw_mmap_status(pcm);
789 if (rc < 0) {
790 oops(pcm, rc, "mmap status failed");
791 goto fail;
792 }
793
Glenn Kasten81012402013-08-22 15:11:48 -0700794#ifdef SNDRV_PCM_IOCTL_TTSTAMP
795 if (pcm->flags & PCM_MONOTONIC) {
796 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
797 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
798 if (rc < 0) {
799 oops(pcm, rc, "cannot set timestamp type");
800 goto fail;
801 }
802 }
803#endif
804
Simon Wilson79d39652011-05-25 13:44:23 -0700805 pcm->underruns = 0;
806 return pcm;
807
808fail:
Liam Girdwood6be28f12011-10-13 12:59:51 -0700809 if (flags & PCM_MMAP)
810 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
811fail_close:
Simon Wilson79d39652011-05-25 13:44:23 -0700812 close(pcm->fd);
813 pcm->fd = -1;
814 return pcm;
815}
816
817int pcm_is_ready(struct pcm *pcm)
818{
819 return pcm->fd >= 0;
820}
Simon Wilsond6458e62011-06-21 14:58:11 -0700821
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530822int pcm_prepare(struct pcm *pcm)
Simon Wilsond6458e62011-06-21 14:58:11 -0700823{
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530824 if (pcm->prepared)
825 return 0;
826
Simon Wilsond6458e62011-06-21 14:58:11 -0700827 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
828 return oops(pcm, errno, "cannot prepare channel");
Liam Girdwood6be28f12011-10-13 12:59:51 -0700829
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530830 pcm->prepared = 1;
831 return 0;
832}
833
834int pcm_start(struct pcm *pcm)
835{
836 int prepare_error = pcm_prepare(pcm);
837 if (prepare_error)
838 return prepare_error;
839
Liam Girdwood6be28f12011-10-13 12:59:51 -0700840 if (pcm->flags & PCM_MMAP)
841 pcm_sync_ptr(pcm, 0);
842
Simon Wilsond6458e62011-06-21 14:58:11 -0700843 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
844 return oops(pcm, errno, "cannot start channel");
845
Liam Girdwood6be28f12011-10-13 12:59:51 -0700846 pcm->running = 1;
Simon Wilsond6458e62011-06-21 14:58:11 -0700847 return 0;
848}
849
850int pcm_stop(struct pcm *pcm)
851{
852 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
853 return oops(pcm, errno, "cannot stop channel");
854
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +0530855 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700856 pcm->running = 0;
Simon Wilsond6458e62011-06-21 14:58:11 -0700857 return 0;
858}
859
Liam Girdwood6be28f12011-10-13 12:59:51 -0700860static inline int pcm_mmap_playback_avail(struct pcm *pcm)
861{
862 int avail;
863
864 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
865
866 if (avail < 0)
867 avail += pcm->boundary;
StevenNANb0fc3e92014-03-17 11:14:49 +0800868 else if (avail >= (int)pcm->boundary)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700869 avail -= pcm->boundary;
870
871 return avail;
872}
873
874static inline int pcm_mmap_capture_avail(struct pcm *pcm)
875{
876 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
877 if (avail < 0)
878 avail += pcm->boundary;
879 return avail;
880}
881
882static inline int pcm_mmap_avail(struct pcm *pcm)
883{
884 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
885 if (pcm->flags & PCM_IN)
886 return pcm_mmap_capture_avail(pcm);
887 else
888 return pcm_mmap_playback_avail(pcm);
889}
890
891static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
892{
893 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
894 appl_ptr += frames;
895
896 /* check for boundary wrap */
897 if (appl_ptr > pcm->boundary)
898 appl_ptr -= pcm->boundary;
899 pcm->mmap_control->appl_ptr = appl_ptr;
900}
901
902int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
903 unsigned int *frames)
904{
905 unsigned int continuous, copy_frames, avail;
906
907 /* return the mmap buffer */
908 *areas = pcm->mmap_buffer;
909
910 /* and the application offset in frames */
911 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
912
913 avail = pcm_mmap_avail(pcm);
914 if (avail > pcm->buffer_size)
915 avail = pcm->buffer_size;
916 continuous = pcm->buffer_size - *offset;
917
918 /* we can only copy frames if the are availabale and continuos */
919 copy_frames = *frames;
920 if (copy_frames > avail)
921 copy_frames = avail;
922 if (copy_frames > continuous)
923 copy_frames = continuous;
924 *frames = copy_frames;
925
926 return 0;
927}
928
929int pcm_mmap_commit(struct pcm *pcm, unsigned int offset, unsigned int frames)
930{
931 /* update the application pointer in userspace and kernel */
932 pcm_mmap_appl_forward(pcm, frames);
933 pcm_sync_ptr(pcm, 0);
934
935 return frames;
936}
937
938int pcm_avail_update(struct pcm *pcm)
939{
940 pcm_sync_ptr(pcm, 0);
941 return pcm_mmap_avail(pcm);
942}
943
944int pcm_state(struct pcm *pcm)
945{
946 int err = pcm_sync_ptr(pcm, 0);
947 if (err < 0)
948 return err;
949
950 return pcm->mmap_status->state;
951}
952
953int pcm_wait(struct pcm *pcm, int timeout)
954{
955 struct pollfd pfd;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700956 int err;
957
958 pfd.fd = pcm->fd;
Apelete Seketeli84889d02014-02-14 14:34:32 +0100959 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
Liam Girdwood6be28f12011-10-13 12:59:51 -0700960
961 do {
962 /* let's wait for avail or timeout */
963 err = poll(&pfd, 1, timeout);
964 if (err < 0)
965 return -errno;
966
967 /* timeout ? */
968 if (err == 0)
969 return 0;
970
971 /* have we been interrupted ? */
972 if (errno == -EINTR)
973 continue;
974
975 /* check for any errors */
976 if (pfd.revents & (POLLERR | POLLNVAL)) {
977 switch (pcm_state(pcm)) {
978 case PCM_STATE_XRUN:
979 return -EPIPE;
980 case PCM_STATE_SUSPENDED:
981 return -ESTRPIPE;
982 case PCM_STATE_DISCONNECTED:
983 return -ENODEV;
984 default:
985 return -EIO;
986 }
987 }
988 /* poll again if fd not ready for IO */
989 } while (!(pfd.revents & (POLLIN | POLLOUT)));
990
991 return 1;
992}
993
Eric Laurentbb7c5df2013-09-16 14:31:17 -0700994int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
Liam Girdwood6be28f12011-10-13 12:59:51 -0700995{
996 int err = 0, frames, avail;
997 unsigned int offset = 0, count;
998
999 if (bytes == 0)
1000 return 0;
1001
1002 count = pcm_bytes_to_frames(pcm, bytes);
1003
1004 while (count > 0) {
1005
1006 /* get the available space for writing new frames */
1007 avail = pcm_avail_update(pcm);
1008 if (avail < 0) {
1009 fprintf(stderr, "cannot determine available mmap frames");
1010 return err;
1011 }
1012
1013 /* start the audio if we reach the threshold */
1014 if (!pcm->running &&
1015 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1016 if (pcm_start(pcm) < 0) {
1017 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1018 (unsigned int)pcm->mmap_status->hw_ptr,
1019 (unsigned int)pcm->mmap_control->appl_ptr,
1020 avail);
1021 return -errno;
1022 }
1023 }
1024
1025 /* sleep until we have space to write new frames */
1026 if (pcm->running &&
1027 (unsigned int)avail < pcm->mmap_control->avail_min) {
1028 int time = -1;
1029
1030 if (pcm->flags & PCM_NOIRQ)
1031 time = (pcm->buffer_size - avail - pcm->mmap_control->avail_min)
1032 / pcm->noirq_frames_per_msec;
1033
1034 err = pcm_wait(pcm, time);
1035 if (err < 0) {
Omair Mohammed Abdullahc9032a02013-01-31 16:35:39 +05301036 pcm->prepared = 0;
Liam Girdwood6be28f12011-10-13 12:59:51 -07001037 pcm->running = 0;
1038 fprintf(stderr, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1039 (unsigned int)pcm->mmap_status->hw_ptr,
1040 (unsigned int)pcm->mmap_control->appl_ptr,
1041 avail);
1042 pcm->mmap_control->appl_ptr = 0;
1043 return err;
1044 }
1045 continue;
1046 }
1047
1048 frames = count;
1049 if (frames > avail)
1050 frames = avail;
1051
1052 if (!frames)
1053 break;
1054
1055 /* copy frames from buffer */
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001056 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
Liam Girdwood6be28f12011-10-13 12:59:51 -07001057 if (frames < 0) {
1058 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1059 (unsigned int)pcm->mmap_status->hw_ptr,
1060 (unsigned int)pcm->mmap_control->appl_ptr,
1061 avail);
1062 return frames;
1063 }
1064
1065 offset += frames;
1066 count -= frames;
1067 }
1068
Liam Girdwood6be28f12011-10-13 12:59:51 -07001069 return 0;
1070}
Eric Laurentbb7c5df2013-09-16 14:31:17 -07001071
1072int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1073{
1074 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1075 return -ENOSYS;
1076
1077 return pcm_mmap_transfer(pcm, (void *)data, count);
1078}
1079
1080int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1081{
1082 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1083 return -ENOSYS;
1084
1085 return pcm_mmap_transfer(pcm, data, count);
1086}
Hardik T Shah9ecb93f2014-04-10 18:03:52 +05301087
1088long pcm_get_delay(struct pcm *pcm)
1089{
1090 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DELAY, &pcm->pcm_delay) < 0)
1091 return -1;
1092
1093 return pcm->pcm_delay;
1094}