1 /* pcm_hw.c
2 **
3 ** Copyright (c) 2019, The Linux Foundation.
4 ** Copyright 2021, The Android Open Source Project
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
8 ** met:
9 ** * Redistributions of source code must retain the above copyright
10 ** notice, this list of conditions and the following disclaimer.
11 ** * Redistributions in binary form must reproduce the above
12 ** copyright notice, this list of conditions and the following
13 ** disclaimer in the documentation and/or other materials provided
14 ** with the distribution.
15 ** * Neither the name of The Linux Foundation nor the names of its
16 ** contributors may be used to endorse or promote products derived
17 ** from this software without specific prior written permission.
18 **
19 ** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
20 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
22 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
23 ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 **/
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <poll.h>
40
41 #include <sys/ioctl.h>
42 #include <sys/mman.h>
43 #include <linux/ioctl.h>
44 #include <time.h>
45 #include <sound/asound.h>
46 #include <tinyalsa/asoundlib.h>
47
48 #include "pcm_io.h"
49
50 struct pcm_hw_data {
51 /** Card number of the pcm device */
52 unsigned int card;
53 /** Device number for the pcm device */
54 unsigned int device;
55 /** File descriptor to the pcm device file node */
56 int fd;
57 /** Pointer to the pcm node from snd card definiton */
58 struct snd_node *node;
59 };
60
pcm_hw_close(void * data)61 static void pcm_hw_close(void *data)
62 {
63 struct pcm_hw_data *hw_data = data;
64
65 if (hw_data->fd >= 0)
66 close(hw_data->fd);
67
68 free(hw_data);
69 }
70
pcm_hw_ioctl(void * data,unsigned int cmd,...)71 static int pcm_hw_ioctl(void *data, unsigned int cmd, ...)
72 {
73 struct pcm_hw_data *hw_data = data;
74 va_list ap;
75 void *arg;
76
77 va_start(ap, cmd);
78 arg = va_arg(ap, void *);
79 va_end(ap);
80
81 return ioctl(hw_data->fd, cmd, arg);
82 }
83
pcm_hw_poll(void * data,struct pollfd * pfd,nfds_t nfds,int timeout)84 static int pcm_hw_poll(void *data __attribute__((unused)),
85 struct pollfd *pfd, nfds_t nfds, int timeout)
86 {
87 return poll(pfd, nfds, timeout);
88 }
89
pcm_hw_mmap(void * data,void * addr,size_t length,int prot,int flags,off_t offset)90 static void *pcm_hw_mmap(void *data, void *addr, size_t length, int prot,
91 int flags, off_t offset)
92 {
93 struct pcm_hw_data *hw_data = data;
94
95 return mmap(addr, length, prot, flags, hw_data->fd, offset);
96 }
97
pcm_hw_munmap(void * data,void * addr,size_t length)98 static int pcm_hw_munmap(void *data __attribute__((unused)), void *addr, size_t length)
99 {
100 return munmap(addr, length);
101 }
102
pcm_hw_open(unsigned int card,unsigned int device,unsigned int flags,void ** data,struct snd_node * node)103 static int pcm_hw_open(unsigned int card, unsigned int device,
104 unsigned int flags, void **data, struct snd_node *node)
105 {
106 struct pcm_hw_data *hw_data;
107 char fn[256];
108 int fd;
109
110 hw_data = calloc(1, sizeof(*hw_data));
111 if (!hw_data) {
112 return -ENOMEM;
113 }
114
115 snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
116 flags & PCM_IN ? 'c' : 'p');
117 // Open the device with non-blocking flag to avoid to be blocked in kernel when all of the
118 // substreams of this PCM device are opened by others.
119 fd = open(fn, O_RDWR | O_NONBLOCK);
120
121 if (fd < 0) {
122 free(hw_data);
123 return fd;
124 }
125
126 if ((flags & PCM_NONBLOCK) == 0) {
127 // Set the file descriptor to blocking mode.
128 if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK) < 0) {
129 fprintf(stderr, "failed to set to blocking mode on %s", fn);
130 close(fd);
131 free(hw_data);
132 return -ENODEV;
133 }
134 }
135
136 hw_data->card = card;
137 hw_data->device = device;
138 hw_data->fd = fd;
139 hw_data->node = node;
140
141 *data = hw_data;
142
143 return fd;
144 }
145
146 const struct pcm_ops hw_ops = {
147 .open = pcm_hw_open,
148 .close = pcm_hw_close,
149 .ioctl = pcm_hw_ioctl,
150 .mmap = pcm_hw_mmap,
151 .munmap = pcm_hw_munmap,
152 .poll = pcm_hw_poll,
153 };
154
155