blob: e0dc7bb7fce2798e50169185c66a163b773ff032 [file] [log] [blame]
Steve Muckle0a9c0872022-02-16 05:58:07 +00001/*******************************************************************************
2* Copyright (C) 2018 Cadence Design Systems, Inc.
3*
4* Permission is hereby granted, free of charge, to any person obtaining
5* a copy of this software and associated documentation files (the
6* "Software"), to use this Software with Cadence processor cores only and
7* not with any other processors and platforms, subject to
8* the following conditions:
9*
10* The above copyright notice and this permission notice shall be included
11* in all copies or substantial portions of the Software.
12*
13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21******************************************************************************/
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <fcntl.h>
26#include <stdarg.h>
27#include <string.h>
28#include <errno.h>
29#include <unistd.h>
30#include <poll.h>
31
32#include <sys/ioctl.h>
33#include <sys/mman.h>
34#include <sys/time.h>
35#include <limits.h>
36
37#include <linux/ioctl.h>
38#define __force
39//#define __bitwise
40#define __user
41#include <sound/asound.h>
42
43#include <tinyalsa/asoundlib.h>
44
45#define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
46
47/* Logs information into a string; follows snprintf() in that
48 * offset may be greater than size, and though no characters are copied
49 * into string, characters are still counted into offset. */
50#define STRLOG(string, offset, size, ...) \
51 do { int temp, clipoffset = offset > size ? size : offset; \
52 temp = snprintf(string + clipoffset, size - clipoffset, __VA_ARGS__); \
53 if (temp > 0) offset += temp; } while (0)
54
55#ifndef ARRAY_SIZE
56#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
57#endif
58
59/* refer to SNDRV_PCM_ACCESS_##index in sound/asound.h. */
60static const char * const access_lookup[] = {
61 "MMAP_INTERLEAVED",
62 "MMAP_NONINTERLEAVED",
63 "MMAP_COMPLEX",
64 "RW_INTERLEAVED",
65 "RW_NONINTERLEAVED",
66};
67
68/* refer to SNDRV_PCM_FORMAT_##index in sound/asound.h. */
69static const char * const format_lookup[] = {
70 /*[0] =*/ "S8",
71 "U8",
72 "S16_LE",
73 "S16_BE",
74 "U16_LE",
75 "U16_BE",
76 "S24_LE",
77 "S24_BE",
78 "U24_LE",
79 "U24_BE",
80 "S32_LE",
81 "S32_BE",
82 "U32_LE",
83 "U32_BE",
84 "FLOAT_LE",
85 "FLOAT_BE",
86 "FLOAT64_LE",
87 "FLOAT64_BE",
88 "IEC958_SUBFRAME_LE",
89 "IEC958_SUBFRAME_BE",
90 "MU_LAW",
91 "A_LAW",
92 "IMA_ADPCM",
93 "MPEG",
94 /*[24] =*/ "GSM",
95 /* gap */
96 [31] = "SPECIAL",
97 "S24_3LE",
98 "S24_3BE",
99 "U24_3LE",
100 "U24_3BE",
101 "S20_3LE",
102 "S20_3BE",
103 "U20_3LE",
104 "U20_3BE",
105 "S18_3LE",
106 "S18_3BE",
107 "U18_3LE",
108 /*[43] =*/ "U18_3BE",
109#if 0
110 /* recent additions, may not be present on local asound.h */
111 "G723_24",
112 "G723_24_1B",
113 "G723_40",
114 "G723_40_1B",
115 "DSD_U8",
116 "DSD_U16_LE",
117#endif
118};
119
120/* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
121static const char * const subformat_lookup[] = {
122 "STD",
123};
124
125static inline int param_is_mask(int p)
126{
127 return (p >= SNDRV_PCM_HW_PARAM_FIRST_MASK) &&
128 (p <= SNDRV_PCM_HW_PARAM_LAST_MASK);
129}
130
131static inline int param_is_interval(int p)
132{
133 return (p >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL) &&
134 (p <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL);
135}
136
137static inline struct snd_interval *param_to_interval(struct snd_pcm_hw_params *p, int n)
138{
139 return &(p->intervals[n - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL]);
140}
141
142static inline struct snd_mask *param_to_mask(struct snd_pcm_hw_params *p, int n)
143{
144 return &(p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK]);
145}
146
147static void param_set_mask(struct snd_pcm_hw_params *p, int n, unsigned int bit)
148{
149 if (bit >= SNDRV_MASK_MAX)
150 return;
151 if (param_is_mask(n)) {
152 struct snd_mask *m = param_to_mask(p, n);
153 m->bits[0] = 0;
154 m->bits[1] = 0;
155 m->bits[bit >> 5] |= (1 << (bit & 31));
156 }
157}
158
159static void param_set_min(struct snd_pcm_hw_params *p, int n, unsigned int val)
160{
161 if (param_is_interval(n)) {
162 struct snd_interval *i = param_to_interval(p, n);
163 i->min = val;
164 }
165}
166
167static unsigned int param_get_min(struct snd_pcm_hw_params *p, int n)
168{
169 if (param_is_interval(n)) {
170 struct snd_interval *i = param_to_interval(p, n);
171 return i->min;
172 }
173 return 0;
174}
175
176static void param_set_max(struct snd_pcm_hw_params *p, int n, unsigned int val)
177{
178 if (param_is_interval(n)) {
179 struct snd_interval *i = param_to_interval(p, n);
180 i->max = val;
181 }
182}
183
184static unsigned int param_get_max(struct snd_pcm_hw_params *p, int n)
185{
186 if (param_is_interval(n)) {
187 struct snd_interval *i = param_to_interval(p, n);
188 return i->max;
189 }
190 return 0;
191}
192
193static void param_set_int(struct snd_pcm_hw_params *p, int n, unsigned int val)
194{
195 if (param_is_interval(n)) {
196 struct snd_interval *i = param_to_interval(p, n);
197 i->min = val;
198 i->max = val;
199 i->integer = 1;
200 }
201}
202
203static unsigned int param_get_int(struct snd_pcm_hw_params *p, int n)
204{
205 if (param_is_interval(n)) {
206 struct snd_interval *i = param_to_interval(p, n);
207 if (i->integer)
208 return i->max;
209 }
210 return 0;
211}
212
213static void param_init(struct snd_pcm_hw_params *p)
214{
215 int n;
216
217 memset(p, 0, sizeof(*p));
218 for (n = SNDRV_PCM_HW_PARAM_FIRST_MASK;
219 n <= SNDRV_PCM_HW_PARAM_LAST_MASK; n++) {
220 struct snd_mask *m = param_to_mask(p, n);
221 m->bits[0] = ~0;
222 m->bits[1] = ~0;
223 }
224 for (n = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
225 n <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; n++) {
226 struct snd_interval *i = param_to_interval(p, n);
227 i->min = 0;
228 i->max = ~0;
229 }
230 p->rmask = ~0U;
231 p->cmask = 0;
232 p->info = ~0U;
233}
234
235#define PCM_ERROR_MAX 128
236
237struct pcm {
238 int fd;
239 unsigned int flags;
240 int running:1;
241 int prepared:1;
242 int underruns;
243 unsigned int buffer_size;
244 unsigned int boundary;
245 char error[PCM_ERROR_MAX];
246 struct pcm_config config;
247 struct snd_pcm_mmap_status *mmap_status;
248 struct snd_pcm_mmap_control *mmap_control;
249 struct snd_pcm_sync_ptr *sync_ptr;
250 void *mmap_buffer;
251 unsigned int noirq_frames_per_msec;
252 int wait_for_avail_min;
253 unsigned int subdevice;
254};
255
256unsigned int pcm_get_buffer_size(struct pcm *pcm)
257{
258 return pcm->buffer_size;
259}
260
261const char* pcm_get_error(struct pcm *pcm)
262{
263 return pcm->error;
264}
265
266unsigned int pcm_get_subdevice(struct pcm *pcm)
267{
268 return pcm->subdevice;
269}
270
271static int oops(struct pcm *pcm, int e, const char *fmt, ...)
272{
273 va_list ap;
274 int sz;
275
276 va_start(ap, fmt);
277 vsnprintf(pcm->error, PCM_ERROR_MAX, fmt, ap);
278 va_end(ap);
279 sz = strlen(pcm->error);
280
281 if (errno)
282 snprintf(pcm->error + sz, PCM_ERROR_MAX - sz,
283 ": %s", strerror(e));
284 return -1;
285}
286
287static unsigned int pcm_format_to_alsa(enum pcm_format format)
288{
289 switch (format) {
290 case PCM_FORMAT_S32_LE:
291 return SNDRV_PCM_FORMAT_S32_LE;
292 case PCM_FORMAT_S8:
293 return SNDRV_PCM_FORMAT_S8;
294 case PCM_FORMAT_S24_3LE:
295 return SNDRV_PCM_FORMAT_S24_3LE;
296 case PCM_FORMAT_S24_LE:
297 return SNDRV_PCM_FORMAT_S24_LE;
298 default:
299 case PCM_FORMAT_S16_LE:
300 return SNDRV_PCM_FORMAT_S16_LE;
301 };
302}
303
304unsigned int pcm_format_to_bits(enum pcm_format format)
305{
306 switch (format) {
307 case PCM_FORMAT_S32_LE:
308 case PCM_FORMAT_S24_LE:
309 return 32;
310 case PCM_FORMAT_S24_3LE:
311 return 24;
312 default:
313 case PCM_FORMAT_S16_LE:
314 return 16;
315 };
316}
317
318unsigned int pcm_bytes_to_frames(struct pcm *pcm, unsigned int bytes)
319{
320 return bytes / (pcm->config.channels *
321 (pcm_format_to_bits(pcm->config.format) >> 3));
322}
323
324unsigned int pcm_frames_to_bytes(struct pcm *pcm, unsigned int frames)
325{
326 return frames * pcm->config.channels *
327 (pcm_format_to_bits(pcm->config.format) >> 3);
328}
329
330static int pcm_sync_ptr(struct pcm *pcm, int flags) {
331 if (pcm->sync_ptr) {
332 pcm->sync_ptr->flags = flags;
333 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
334 return -1;
335 }
336 return 0;
337}
338
339static int pcm_hw_mmap_status(struct pcm *pcm) {
340
341 if (pcm->sync_ptr)
342 return 0;
343
344 int page_size = sysconf(_SC_PAGE_SIZE);
345 pcm->mmap_status = mmap(NULL, page_size, PROT_READ, MAP_FILE | MAP_SHARED,
346 pcm->fd, SNDRV_PCM_MMAP_OFFSET_STATUS);
347 if (pcm->mmap_status == MAP_FAILED)
348 pcm->mmap_status = NULL;
349 if (!pcm->mmap_status)
350 goto mmap_error;
351
352 pcm->mmap_control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
353 MAP_FILE | MAP_SHARED, pcm->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
354 if (pcm->mmap_control == MAP_FAILED)
355 pcm->mmap_control = NULL;
356 if (!pcm->mmap_control) {
357 munmap(pcm->mmap_status, page_size);
358 pcm->mmap_status = NULL;
359 goto mmap_error;
360 }
361 if (pcm->flags & PCM_MMAP)
362 pcm->mmap_control->avail_min = pcm->config.avail_min;
363 else
364 pcm->mmap_control->avail_min = 1;
365
366 return 0;
367
368mmap_error:
369
370 pcm->sync_ptr = calloc(1, sizeof(*pcm->sync_ptr));
371 if (!pcm->sync_ptr)
372 return -ENOMEM;
373 pcm->mmap_status = &pcm->sync_ptr->s.status;
374 pcm->mmap_control = &pcm->sync_ptr->c.control;
375 if (pcm->flags & PCM_MMAP)
376 pcm->mmap_control->avail_min = pcm->config.avail_min;
377 else
378 pcm->mmap_control->avail_min = 1;
379
380 pcm_sync_ptr(pcm, 0);
381
382 return 0;
383}
384
385static void pcm_hw_munmap_status(struct pcm *pcm) {
386 if (pcm->sync_ptr) {
387 free(pcm->sync_ptr);
388 pcm->sync_ptr = NULL;
389 } else {
390 int page_size = sysconf(_SC_PAGE_SIZE);
391 if (pcm->mmap_status)
392 munmap(pcm->mmap_status, page_size);
393 if (pcm->mmap_control)
394 munmap(pcm->mmap_control, page_size);
395 }
396 pcm->mmap_status = NULL;
397 pcm->mmap_control = NULL;
398}
399
400static int pcm_areas_copy(struct pcm *pcm, unsigned int pcm_offset,
401 char *buf, unsigned int src_offset,
402 unsigned int frames)
403{
404 int size_bytes = pcm_frames_to_bytes(pcm, frames);
405 int pcm_offset_bytes = pcm_frames_to_bytes(pcm, pcm_offset);
406 int src_offset_bytes = pcm_frames_to_bytes(pcm, src_offset);
407
408 /* interleaved only atm */
409 if (pcm->flags & PCM_IN)
410 memcpy(buf + src_offset_bytes,
411 (char*)pcm->mmap_buffer + pcm_offset_bytes,
412 size_bytes);
413 else
414 memcpy((char*)pcm->mmap_buffer + pcm_offset_bytes,
415 buf + src_offset_bytes,
416 size_bytes);
417 return 0;
418}
419
420static int pcm_mmap_transfer_areas(struct pcm *pcm, char *buf,
421 unsigned int offset, unsigned int size)
422{
423 void *pcm_areas;
424 int commit;
425 unsigned int pcm_offset, frames, count = 0;
426
427 while (size > 0) {
428 frames = size;
429 pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
430 pcm_areas_copy(pcm, pcm_offset, buf, offset, frames);
431 commit = pcm_mmap_commit(pcm, pcm_offset, frames);
432 if (commit < 0) {
433 oops(pcm, commit, "failed to commit %d frames\n", frames);
434 return commit;
435 }
436
437 offset += commit;
438 count += commit;
439 size -= commit;
440 }
441 return count;
442}
443
444int pcm_get_htimestamp(struct pcm *pcm, unsigned int *avail,
445 struct timespec *tstamp)
446{
447 int frames;
448 int rc;
449 snd_pcm_uframes_t hw_ptr;
450
451 if (!pcm_is_ready(pcm))
452 return -1;
453
454 rc = pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_APPL|SNDRV_PCM_SYNC_PTR_HWSYNC);
455 if (rc < 0)
456 return -1;
457
458 if ((pcm->mmap_status->state != PCM_STATE_RUNNING) &&
459 (pcm->mmap_status->state != PCM_STATE_DRAINING))
460 return -1;
461
462 *tstamp = pcm->mmap_status->tstamp;
463 if (tstamp->tv_sec == 0 && tstamp->tv_nsec == 0)
464 return -1;
465
466 hw_ptr = pcm->mmap_status->hw_ptr;
467 if (pcm->flags & PCM_IN)
468 frames = hw_ptr - pcm->mmap_control->appl_ptr;
469 else
470 frames = hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
471
472 if (frames < 0)
473 frames += pcm->boundary;
474 else if (frames > (int)pcm->boundary)
475 frames -= pcm->boundary;
476
477 *avail = (unsigned int)frames;
478
479 return 0;
480}
481
482int pcm_write(struct pcm *pcm, const void *data, unsigned int count)
483{
484 struct snd_xferi x;
485
486 if (pcm->flags & PCM_IN)
487 return -EINVAL;
488
489 x.buf = (void*)data;
490 x.frames = count / (pcm->config.channels *
491 pcm_format_to_bits(pcm->config.format) / 8);
492
493 for (;;) {
494 if (!pcm->running) {
495 int prepare_error = pcm_prepare(pcm);
496 if (prepare_error)
497 return prepare_error;
498 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
499 return oops(pcm, errno, "cannot write initial data");
500 pcm->running = 1;
501 return 0;
502 }
503 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
504 pcm->prepared = 0;
505 pcm->running = 0;
506 if (errno == EPIPE) {
507 /* we failed to make our window -- try to restart if we are
508 * allowed to do so. Otherwise, simply allow the EPIPE error to
509 * propagate up to the app level */
510 pcm->underruns++;
511 if (pcm->flags & PCM_NORESTART)
512 return -EPIPE;
513 continue;
514 }
515 return oops(pcm, errno, "cannot write stream data");
516 }
517 return 0;
518 }
519}
520
521int pcm_read(struct pcm *pcm, void *data, unsigned int count)
522{
523 struct snd_xferi x;
524
525 if (!(pcm->flags & PCM_IN))
526 return -EINVAL;
527
528 x.buf = data;
529 x.frames = count / (pcm->config.channels *
530 pcm_format_to_bits(pcm->config.format) / 8);
531
532 for (;;) {
533 if (!pcm->running) {
534 if (pcm_start(pcm) < 0) {
535 fprintf(stderr, "start error");
536 return -errno;
537 }
538 }
539 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
540 pcm->prepared = 0;
541 pcm->running = 0;
542 if (errno == EPIPE) {
543 /* we failed to make our window -- try to restart */
544 pcm->underruns++;
545 continue;
546 }
547 return oops(pcm, errno, "cannot read stream data");
548 }
549 return 0;
550 }
551}
552
553static struct pcm bad_pcm = {
554 .fd = -1,
555};
556
557struct pcm_params *pcm_params_get(unsigned int card, unsigned int device,
558 unsigned int flags)
559{
560 struct snd_pcm_hw_params *params;
561 char fn[256];
562 int fd;
563
564 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
565 flags & PCM_IN ? 'c' : 'p');
566
567 fd = open(fn, O_RDWR);
568 if (fd < 0) {
569 fprintf(stderr, "cannot open device '%s'\n", fn);
570 goto err_open;
571 }
572
573 params = calloc(1, sizeof(struct snd_pcm_hw_params));
574 if (!params)
575 goto err_calloc;
576
577 param_init(params);
578 if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
579 fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
580 goto err_hw_refine;
581 }
582
583 close(fd);
584
585 return (struct pcm_params *)params;
586
587err_hw_refine:
588 free(params);
589err_calloc:
590 close(fd);
591err_open:
592 return NULL;
593}
594
595void pcm_params_free(struct pcm_params *pcm_params)
596{
597 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
598
599 if (params)
600 free(params);
601}
602
603static int pcm_param_to_alsa(enum pcm_param param)
604{
605 switch (param) {
606 case PCM_PARAM_ACCESS:
607 return SNDRV_PCM_HW_PARAM_ACCESS;
608 case PCM_PARAM_FORMAT:
609 return SNDRV_PCM_HW_PARAM_FORMAT;
610 case PCM_PARAM_SUBFORMAT:
611 return SNDRV_PCM_HW_PARAM_SUBFORMAT;
612 case PCM_PARAM_SAMPLE_BITS:
613 return SNDRV_PCM_HW_PARAM_SAMPLE_BITS;
614 break;
615 case PCM_PARAM_FRAME_BITS:
616 return SNDRV_PCM_HW_PARAM_FRAME_BITS;
617 break;
618 case PCM_PARAM_CHANNELS:
619 return SNDRV_PCM_HW_PARAM_CHANNELS;
620 break;
621 case PCM_PARAM_RATE:
622 return SNDRV_PCM_HW_PARAM_RATE;
623 break;
624 case PCM_PARAM_PERIOD_TIME:
625 return SNDRV_PCM_HW_PARAM_PERIOD_TIME;
626 break;
627 case PCM_PARAM_PERIOD_SIZE:
628 return SNDRV_PCM_HW_PARAM_PERIOD_SIZE;
629 break;
630 case PCM_PARAM_PERIOD_BYTES:
631 return SNDRV_PCM_HW_PARAM_PERIOD_BYTES;
632 break;
633 case PCM_PARAM_PERIODS:
634 return SNDRV_PCM_HW_PARAM_PERIODS;
635 break;
636 case PCM_PARAM_BUFFER_TIME:
637 return SNDRV_PCM_HW_PARAM_BUFFER_TIME;
638 break;
639 case PCM_PARAM_BUFFER_SIZE:
640 return SNDRV_PCM_HW_PARAM_BUFFER_SIZE;
641 break;
642 case PCM_PARAM_BUFFER_BYTES:
643 return SNDRV_PCM_HW_PARAM_BUFFER_BYTES;
644 break;
645 case PCM_PARAM_TICK_TIME:
646 return SNDRV_PCM_HW_PARAM_TICK_TIME;
647 break;
648
649 default:
650 return -1;
651 }
652}
653
654struct pcm_mask *pcm_params_get_mask(struct pcm_params *pcm_params,
655 enum pcm_param param)
656{
657 int p;
658 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
659 if (params == NULL) {
660 return NULL;
661 }
662
663 p = pcm_param_to_alsa(param);
664 if (p < 0 || !param_is_mask(p)) {
665 return NULL;
666 }
667
668 return (struct pcm_mask *)param_to_mask(params, p);
669}
670
671unsigned int pcm_params_get_min(struct pcm_params *pcm_params,
672 enum pcm_param param)
673{
674 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
675 int p;
676
677 if (!params)
678 return 0;
679
680 p = pcm_param_to_alsa(param);
681 if (p < 0)
682 return 0;
683
684 return param_get_min(params, p);
685}
686
687void pcm_params_set_min(struct pcm_params *pcm_params,
688 enum pcm_param param, unsigned int val)
689{
690 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
691 int p;
692
693 if (!params)
694 return;
695
696 p = pcm_param_to_alsa(param);
697 if (p < 0)
698 return;
699
700 param_set_min(params, p, val);
701}
702
703unsigned int pcm_params_get_max(struct pcm_params *pcm_params,
704 enum pcm_param param)
705{
706 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
707 int p;
708
709 if (!params)
710 return 0;
711
712 p = pcm_param_to_alsa(param);
713 if (p < 0)
714 return 0;
715
716 return param_get_max(params, p);
717}
718
719void pcm_params_set_max(struct pcm_params *pcm_params,
720 enum pcm_param param, unsigned int val)
721{
722 struct snd_pcm_hw_params *params = (struct snd_pcm_hw_params *)pcm_params;
723 int p;
724
725 if (!params)
726 return;
727
728 p = pcm_param_to_alsa(param);
729 if (p < 0)
730 return;
731
732 param_set_max(params, p, val);
733}
734
735static int pcm_mask_test(struct pcm_mask *m, unsigned int index)
736{
737 const unsigned int bitshift = 5; /* for 32 bit integer */
738 const unsigned int bitmask = (1 << bitshift) - 1;
739 unsigned int element;
740
741 element = index >> bitshift;
742 if (element >= ARRAY_SIZE(m->bits))
743 return 0; /* for safety, but should never occur */
744 return (m->bits[element] >> (index & bitmask)) & 1;
745}
746
747static int pcm_mask_to_string(struct pcm_mask *m, char *string, unsigned int size,
748 char *mask_name,
749 const char * const *bit_array_name, size_t bit_array_size)
750{
751 unsigned int i;
752 unsigned int offset = 0;
753
754 if (m == NULL)
755 return 0;
756 if (bit_array_size < 32) {
757 STRLOG(string, offset, size, "%12s:\t%#08x\n", mask_name, m->bits[0]);
758 } else { /* spans two or more bitfields, print with an array index */
759 for (i = 0; i < (bit_array_size + 31) >> 5; ++i) {
760 STRLOG(string, offset, size, "%9s[%d]:\t%#08x\n",
761 mask_name, i, m->bits[i]);
762 }
763 }
764 for (i = 0; i < bit_array_size; ++i) {
765 if (pcm_mask_test(m, i)) {
766 STRLOG(string, offset, size, "%12s \t%s\n", "", bit_array_name[i]);
767 }
768 }
769 return offset;
770}
771
772int pcm_params_to_string(struct pcm_params *params, char *string, unsigned int size)
773{
774 struct pcm_mask *m;
775 unsigned int min, max;
776 unsigned int clipoffset, offset;
777
778 m = pcm_params_get_mask(params, PCM_PARAM_ACCESS);
779 offset = pcm_mask_to_string(m, string, size,
780 "Access", access_lookup, ARRAY_SIZE(access_lookup));
781 m = pcm_params_get_mask(params, PCM_PARAM_FORMAT);
782 clipoffset = offset > size ? size : offset;
783 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
784 "Format", format_lookup, ARRAY_SIZE(format_lookup));
785 m = pcm_params_get_mask(params, PCM_PARAM_SUBFORMAT);
786 clipoffset = offset > size ? size : offset;
787 offset += pcm_mask_to_string(m, string + clipoffset, size - clipoffset,
788 "Subformat", subformat_lookup, ARRAY_SIZE(subformat_lookup));
789 min = pcm_params_get_min(params, PCM_PARAM_RATE);
790 max = pcm_params_get_max(params, PCM_PARAM_RATE);
791 STRLOG(string, offset, size, " Rate:\tmin=%uHz\tmax=%uHz\n", min, max);
792 min = pcm_params_get_min(params, PCM_PARAM_CHANNELS);
793 max = pcm_params_get_max(params, PCM_PARAM_CHANNELS);
794 STRLOG(string, offset, size, " Channels:\tmin=%u\t\tmax=%u\n", min, max);
795 min = pcm_params_get_min(params, PCM_PARAM_SAMPLE_BITS);
796 max = pcm_params_get_max(params, PCM_PARAM_SAMPLE_BITS);
797 STRLOG(string, offset, size, " Sample bits:\tmin=%u\t\tmax=%u\n", min, max);
798 min = pcm_params_get_min(params, PCM_PARAM_PERIOD_SIZE);
799 max = pcm_params_get_max(params, PCM_PARAM_PERIOD_SIZE);
800 STRLOG(string, offset, size, " Period size:\tmin=%u\t\tmax=%u\n", min, max);
801 min = pcm_params_get_min(params, PCM_PARAM_PERIODS);
802 max = pcm_params_get_max(params, PCM_PARAM_PERIODS);
803 STRLOG(string, offset, size, "Period count:\tmin=%u\t\tmax=%u\n", min, max);
804 return offset;
805}
806
807int pcm_params_format_test(struct pcm_params *params, enum pcm_format format)
808{
809 unsigned int alsa_format = pcm_format_to_alsa(format);
810
811 if (alsa_format == SNDRV_PCM_FORMAT_S16_LE && format != PCM_FORMAT_S16_LE)
812 return 0; /* caution: format not recognized is equivalent to S16_LE */
813 return pcm_mask_test(pcm_params_get_mask(params, PCM_PARAM_FORMAT), alsa_format);
814}
815
816int pcm_close(struct pcm *pcm)
817{
818 if (pcm == &bad_pcm)
819 return 0;
820
821 pcm_hw_munmap_status(pcm);
822
823 if (pcm->flags & PCM_MMAP) {
824 pcm_stop(pcm);
825 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
826 }
827
828 if (pcm->fd >= 0)
829 close(pcm->fd);
830 pcm->prepared = 0;
831 pcm->running = 0;
832 pcm->buffer_size = 0;
833 pcm->fd = -1;
834 free(pcm);
835 return 0;
836}
837
838struct pcm *pcm_open(unsigned int card, unsigned int device,
839 unsigned int flags, struct pcm_config *config)
840{
841 struct pcm *pcm;
842 struct snd_pcm_info info;
843 struct snd_pcm_hw_params params;
844 struct snd_pcm_sw_params sparams;
845 char fn[256];
846 int rc;
847
848 pcm = calloc(1, sizeof(struct pcm));
849 if (!pcm || !config)
850 return &bad_pcm; /* TODO: could support default config here */
851
852 pcm->config = *config;
853
854 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
855 flags & PCM_IN ? 'c' : 'p');
856
857 pcm->flags = flags;
858 pcm->fd = open(fn, O_RDWR|O_NONBLOCK);
859 if (pcm->fd < 0) {
860 oops(pcm, errno, "cannot open device '%s'", fn);
861 return pcm;
862 }
863
864 if (fcntl(pcm->fd, F_SETFL, fcntl(pcm->fd, F_GETFL) &
865 ~O_NONBLOCK) < 0) {
866 oops(pcm, errno, "failed to reset blocking mode '%s'", fn);
867 goto fail_close;
868 }
869
870 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
871 oops(pcm, errno, "cannot get info");
872 goto fail_close;
873 }
874 pcm->subdevice = info.subdevice;
875
876 param_init(&params);
877 param_set_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT,
878 pcm_format_to_alsa(config->format));
879 param_set_mask(&params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
880 SNDRV_PCM_SUBFORMAT_STD);
881 param_set_min(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, config->period_size);
882 param_set_int(&params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
883 pcm_format_to_bits(config->format));
884 param_set_int(&params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
885 pcm_format_to_bits(config->format) * config->channels);
886 param_set_int(&params, SNDRV_PCM_HW_PARAM_CHANNELS,
887 config->channels);
888 param_set_int(&params, SNDRV_PCM_HW_PARAM_PERIODS, config->period_count);
889 param_set_int(&params, SNDRV_PCM_HW_PARAM_RATE, config->rate);
890
891 if (flags & PCM_NOIRQ) {
892 if (!(flags & PCM_MMAP)) {
893 oops(pcm, -EINVAL, "noirq only currently supported with mmap().");
894 goto fail_close;
895 }
896
897 params.flags |= SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP;
898 pcm->noirq_frames_per_msec = config->rate / 1000;
899 }
900
901 if (flags & PCM_MMAP)
902 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
903 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
904 else
905 param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
906 SNDRV_PCM_ACCESS_RW_INTERLEAVED);
907
908 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
909 oops(pcm, errno, "cannot set hw params");
910 goto fail_close;
911 }
912
913 /* get our refined hw_params */
914 config->period_size = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
915 config->period_count = param_get_int(&params, SNDRV_PCM_HW_PARAM_PERIODS);
916 pcm->buffer_size = config->period_count * config->period_size;
917
918 if (flags & PCM_MMAP) {
919 pcm->mmap_buffer = mmap(NULL, pcm_frames_to_bytes(pcm, pcm->buffer_size),
920 PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, pcm->fd, 0);
921 if (pcm->mmap_buffer == MAP_FAILED) {
922 oops(pcm, -errno, "failed to mmap buffer %d bytes\n",
923 pcm_frames_to_bytes(pcm, pcm->buffer_size));
924 goto fail_close;
925 }
926 }
927
928 memset(&sparams, 0, sizeof(sparams));
929 sparams.tstamp_mode = SNDRV_PCM_TSTAMP_ENABLE;
930 sparams.period_step = 1;
931
932 if (!config->start_threshold) {
933 if (pcm->flags & PCM_IN)
934 pcm->config.start_threshold = sparams.start_threshold = 1;
935 else
936 pcm->config.start_threshold = sparams.start_threshold =
937 config->period_count * config->period_size / 2;
938 } else
939 sparams.start_threshold = config->start_threshold;
940
941 /* pick a high stop threshold - todo: does this need further tuning */
942 if (!config->stop_threshold) {
943 if (pcm->flags & PCM_IN)
944 pcm->config.stop_threshold = sparams.stop_threshold =
945 config->period_count * config->period_size * 10;
946 else
947 pcm->config.stop_threshold = sparams.stop_threshold =
948 config->period_count * config->period_size;
949 }
950 else
951 sparams.stop_threshold = config->stop_threshold;
952
953 if (!pcm->config.avail_min) {
954 if (pcm->flags & PCM_MMAP)
955 pcm->config.avail_min = sparams.avail_min = pcm->config.period_size;
956 else
957 pcm->config.avail_min = sparams.avail_min = 1;
958 } else
959 sparams.avail_min = config->avail_min;
960
961 sparams.xfer_align = config->period_size / 2; /* needed for old kernels */
962 sparams.silence_threshold = config->silence_threshold;
963 sparams.silence_size = config->silence_size;
964 pcm->boundary = sparams.boundary = pcm->buffer_size;
965
966 while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
967 pcm->boundary *= 2;
968
969 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
970 oops(pcm, errno, "cannot set sw params");
971 goto fail;
972 }
973
974 rc = pcm_hw_mmap_status(pcm);
975 if (rc < 0) {
976 oops(pcm, rc, "mmap status failed");
977 goto fail;
978 }
979
980#ifdef SNDRV_PCM_IOCTL_TTSTAMP
981 if (pcm->flags & PCM_MONOTONIC) {
982 int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
983 rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
984 if (rc < 0) {
985 oops(pcm, rc, "cannot set timestamp type");
986 goto fail;
987 }
988 }
989#endif
990
991 pcm->underruns = 0;
992 return pcm;
993
994fail:
995 if (flags & PCM_MMAP)
996 munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
997fail_close:
998 close(pcm->fd);
999 pcm->fd = -1;
1000 return pcm;
1001}
1002
1003int pcm_is_ready(struct pcm *pcm)
1004{
1005 return pcm->fd >= 0;
1006}
1007
1008int pcm_prepare(struct pcm *pcm)
1009{
1010 if (pcm->prepared)
1011 return 0;
1012
1013 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
1014 return oops(pcm, errno, "cannot prepare channel");
1015
1016 pcm->prepared = 1;
1017 return 0;
1018}
1019
1020int pcm_start(struct pcm *pcm)
1021{
1022 int prepare_error = pcm_prepare(pcm);
1023 if (prepare_error)
1024 return prepare_error;
1025
1026 if (pcm->flags & PCM_MMAP)
1027 pcm_sync_ptr(pcm, 0);
1028
1029 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
1030 return oops(pcm, errno, "cannot start channel");
1031
1032 pcm->running = 1;
1033 return 0;
1034}
1035
1036int pcm_stop(struct pcm *pcm)
1037{
1038 if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
1039 return oops(pcm, errno, "cannot stop channel");
1040
1041 pcm->prepared = 0;
1042 pcm->running = 0;
1043 return 0;
1044}
1045
1046static inline int pcm_mmap_playback_avail(struct pcm *pcm)
1047{
1048 int avail;
1049
1050 avail = pcm->mmap_status->hw_ptr + pcm->buffer_size - pcm->mmap_control->appl_ptr;
1051
1052 if (avail < 0)
1053 avail += pcm->boundary;
1054 else if (avail > (int)pcm->boundary)
1055 avail -= pcm->boundary;
1056
1057 return avail;
1058}
1059
1060static inline int pcm_mmap_capture_avail(struct pcm *pcm)
1061{
1062 int avail = pcm->mmap_status->hw_ptr - pcm->mmap_control->appl_ptr;
1063 if (avail < 0)
1064 avail += pcm->boundary;
1065 return avail;
1066}
1067
1068int pcm_mmap_avail(struct pcm *pcm)
1069{
1070 pcm_sync_ptr(pcm, SNDRV_PCM_SYNC_PTR_HWSYNC);
1071 if (pcm->flags & PCM_IN)
1072 return pcm_mmap_capture_avail(pcm);
1073 else
1074 return pcm_mmap_playback_avail(pcm);
1075}
1076
1077static void pcm_mmap_appl_forward(struct pcm *pcm, int frames)
1078{
1079 unsigned int appl_ptr = pcm->mmap_control->appl_ptr;
1080 appl_ptr += frames;
1081
1082 /* check for boundary wrap */
1083 if (appl_ptr > pcm->boundary)
1084 appl_ptr -= pcm->boundary;
1085 pcm->mmap_control->appl_ptr = appl_ptr;
1086}
1087
1088int pcm_mmap_begin(struct pcm *pcm, void **areas, unsigned int *offset,
1089 unsigned int *frames)
1090{
1091 unsigned int continuous, copy_frames, avail;
1092
1093 /* return the mmap buffer */
1094 *areas = pcm->mmap_buffer;
1095
1096 /* and the application offset in frames */
1097 *offset = pcm->mmap_control->appl_ptr % pcm->buffer_size;
1098
1099 avail = pcm_mmap_avail(pcm);
1100 if (avail > pcm->buffer_size)
1101 avail = pcm->buffer_size;
1102 continuous = pcm->buffer_size - *offset;
1103
1104 /* we can only copy frames if the are availabale and continuos */
1105 copy_frames = *frames;
1106 if (copy_frames > avail)
1107 copy_frames = avail;
1108 if (copy_frames > continuous)
1109 copy_frames = continuous;
1110 *frames = copy_frames;
1111
1112 return 0;
1113}
1114
1115int pcm_mmap_commit(struct pcm *pcm, unsigned int offset __attribute__((unused)), unsigned int frames)
1116{
1117 /* update the application pointer in userspace and kernel */
1118 pcm_mmap_appl_forward(pcm, frames);
1119 pcm_sync_ptr(pcm, 0);
1120
1121 return frames;
1122}
1123
1124int pcm_avail_update(struct pcm *pcm)
1125{
1126 pcm_sync_ptr(pcm, 0);
1127 return pcm_mmap_avail(pcm);
1128}
1129
1130int pcm_state(struct pcm *pcm)
1131{
1132 int err = pcm_sync_ptr(pcm, 0);
1133 if (err < 0)
1134 return err;
1135
1136 return pcm->mmap_status->state;
1137}
1138
1139int pcm_set_avail_min(struct pcm *pcm, int avail_min)
1140{
1141 if ((~pcm->flags) & (PCM_MMAP | PCM_NOIRQ))
1142 return -ENOSYS;
1143
1144 pcm->config.avail_min = avail_min;
1145 return 0;
1146}
1147
1148int pcm_wait(struct pcm *pcm, int timeout)
1149{
1150 struct pollfd pfd;
1151 int err;
1152
1153 pfd.fd = pcm->fd;
1154 pfd.events = POLLOUT | POLLERR | POLLNVAL;
1155
1156 do {
1157 /* let's wait for avail or timeout */
1158 err = poll(&pfd, 1, timeout);
1159 if (err < 0)
1160 return -errno;
1161
1162 /* timeout ? */
1163 if (err == 0)
1164 return 0;
1165
1166 /* have we been interrupted ? */
1167 if (errno == -EINTR)
1168 continue;
1169
1170 /* check for any errors */
1171 if (pfd.revents & (POLLERR | POLLNVAL)) {
1172 switch (pcm_state(pcm)) {
1173 case PCM_STATE_XRUN:
1174 return -EPIPE;
1175 case PCM_STATE_SUSPENDED:
1176 return -ESTRPIPE;
1177 case PCM_STATE_DISCONNECTED:
1178 return -ENODEV;
1179 default:
1180 return -EIO;
1181 }
1182 }
1183 /* poll again if fd not ready for IO */
1184 } while (!(pfd.revents & (POLLIN | POLLOUT)));
1185
1186 return 1;
1187}
1188
1189int pcm_get_poll_fd(struct pcm *pcm)
1190{
1191 return pcm->fd;
1192}
1193
1194int pcm_mmap_transfer(struct pcm *pcm, const void *buffer, unsigned int bytes)
1195{
1196 int err = 0, frames, avail;
1197 unsigned int offset = 0, count;
1198
1199 if (bytes == 0)
1200 return 0;
1201
1202 count = pcm_bytes_to_frames(pcm, bytes);
1203
1204 while (count > 0) {
1205
1206 /* get the available space for writing new frames */
1207 avail = pcm_avail_update(pcm);
1208 if (avail < 0) {
1209 fprintf(stderr, "cannot determine available mmap frames");
1210 return err;
1211 }
1212
1213 /* start the audio if we reach the threshold */
1214 if (!pcm->running &&
1215 (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
1216 if (pcm_start(pcm) < 0) {
1217 fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
1218 (unsigned int)pcm->mmap_status->hw_ptr,
1219 (unsigned int)pcm->mmap_control->appl_ptr,
1220 avail);
1221 return -errno;
1222 }
1223 pcm->wait_for_avail_min = 0;
1224 }
1225
1226 /* sleep until we have space to write new frames */
1227 if (pcm->running) {
1228 /* enable waiting for avail_min threshold when less frames than we have to write
1229 * are available. */
1230 if (!pcm->wait_for_avail_min && (count > (unsigned int)avail))
1231 pcm->wait_for_avail_min = 1;
1232
1233 if (pcm->wait_for_avail_min && (avail < pcm->config.avail_min)) {
1234 int time = -1;
1235
1236 /* disable waiting for avail_min threshold to allow small amounts of data to be
1237 * written without waiting as long as there is enough room in buffer. */
1238 pcm->wait_for_avail_min = 0;
1239
1240 if (pcm->flags & PCM_NOIRQ)
1241 time = (pcm->config.avail_min - avail) / pcm->noirq_frames_per_msec;
1242
1243 err = pcm_wait(pcm, time);
1244 if (err < 0) {
1245 pcm->prepared = 0;
1246 pcm->running = 0;
1247 oops(pcm, err, "wait error: hw 0x%x app 0x%x avail 0x%x\n",
1248 (unsigned int)pcm->mmap_status->hw_ptr,
1249 (unsigned int)pcm->mmap_control->appl_ptr,
1250 avail);
1251 pcm->mmap_control->appl_ptr = 0;
1252 return err;
1253 }
1254 continue;
1255 }
1256 }
1257
1258 frames = count;
1259 if (frames > avail)
1260 frames = avail;
1261
1262 if (!frames)
1263 break;
1264
1265 /* copy frames from buffer */
1266 frames = pcm_mmap_transfer_areas(pcm, (void *)buffer, offset, frames);
1267 if (frames < 0) {
1268 fprintf(stderr, "write error: hw 0x%x app 0x%x avail 0x%x\n",
1269 (unsigned int)pcm->mmap_status->hw_ptr,
1270 (unsigned int)pcm->mmap_control->appl_ptr,
1271 avail);
1272 return frames;
1273 }
1274
1275 offset += frames;
1276 count -= frames;
1277 }
1278
1279 return 0;
1280}
1281
1282int pcm_mmap_write(struct pcm *pcm, const void *data, unsigned int count)
1283{
1284 if ((~pcm->flags) & (PCM_OUT | PCM_MMAP))
1285 return -ENOSYS;
1286
1287 return pcm_mmap_transfer(pcm, (void *)data, count);
1288}
1289
1290int pcm_mmap_read(struct pcm *pcm, void *data, unsigned int count)
1291{
1292 if ((~pcm->flags) & (PCM_IN | PCM_MMAP))
1293 return -ENOSYS;
1294
1295 return pcm_mmap_transfer(pcm, data, count);
1296}
1297
1298int pcm_ioctl(struct pcm *pcm, int request, ...)
1299{
1300 va_list ap;
1301 void * arg;
1302
1303 if (!pcm_is_ready(pcm))
1304 return -1;
1305
1306 va_start(ap, request);
1307 arg = va_arg(ap, void *);
1308 va_end(ap);
1309
1310 return ioctl(pcm->fd, request, arg);
1311}