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