• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sound/asound.h>
45 #include <tinyalsa/asoundlib.h>
46 
47 #include "pcm_io.h"
48 
49 struct pcm_hw_data {
50     /** Card number of the pcm device */
51     unsigned int card;
52     /** Device number for the pcm device */
53     unsigned int device;
54     /** File descriptor to the pcm device file node */
55     int fd;
56     /** Pointer to the pcm node from snd card definiton */
57     struct snd_node *node;
58 };
59 
pcm_hw_close(void * data)60 static void pcm_hw_close(void *data)
61 {
62     struct pcm_hw_data *hw_data = data;
63 
64     if (hw_data->fd >= 0)
65         close(hw_data->fd);
66 
67     free(hw_data);
68 }
69 
pcm_hw_ioctl(void * data,unsigned int cmd,...)70 static int pcm_hw_ioctl(void *data, unsigned int cmd, ...)
71 {
72     struct pcm_hw_data *hw_data = data;
73     va_list ap;
74     void *arg;
75 
76     va_start(ap, cmd);
77     arg = va_arg(ap, void *);
78     va_end(ap);
79 
80     return ioctl(hw_data->fd, cmd, arg);
81 }
82 
pcm_hw_poll(void * data,struct pollfd * pfd,nfds_t nfds,int timeout)83 static int pcm_hw_poll(void *data __attribute__((unused)),
84                         struct pollfd *pfd, nfds_t nfds, int timeout)
85 {
86     return poll(pfd, nfds, timeout);
87 }
88 
pcm_hw_mmap(void * data,void * addr,size_t length,int prot,int flags,off_t offset)89 static void *pcm_hw_mmap(void *data, void *addr, size_t length, int prot,
90                        int flags, off_t offset)
91 {
92     struct pcm_hw_data *hw_data = data;
93 
94     return mmap(addr, length, prot, flags, hw_data->fd, offset);
95 }
96 
pcm_hw_munmap(void * data,void * addr,size_t length)97 static int pcm_hw_munmap(void *data __attribute__((unused)), void *addr, size_t length)
98 {
99     return munmap(addr, length);
100 }
101 
pcm_hw_open(unsigned int card,unsigned int device,unsigned int flags,void ** data,struct snd_node * node)102 static int pcm_hw_open(unsigned int card, unsigned int device,
103                 unsigned int flags, void **data, struct snd_node *node)
104 {
105     struct pcm_hw_data *hw_data;
106     char fn[256];
107     int fd;
108 
109     hw_data = calloc(1, sizeof(*hw_data));
110     if (!hw_data) {
111         return -ENOMEM;
112     }
113 
114     snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
115              flags & PCM_IN ? 'c' : 'p');
116     // Open the device with non-blocking flag to avoid to be blocked in kernel when all of the
117     //   substreams of this PCM device are opened by others.
118     fd = open(fn, O_RDWR | O_NONBLOCK);
119 
120     if (fd < 0) {
121         free(hw_data);
122         return fd;
123     }
124 
125     if ((flags & PCM_NONBLOCK) == 0) {
126         // Set the file descriptor to blocking mode.
127         if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK) < 0) {
128             fprintf(stderr, "failed to set to blocking mode on %s", fn);
129             close(fd);
130             free(hw_data);
131             return -ENODEV;
132         }
133     }
134 
135     hw_data->card = card;
136     hw_data->device = device;
137     hw_data->fd = fd;
138     hw_data->node = node;
139 
140     *data = hw_data;
141 
142     return fd;
143 }
144 
145 const struct pcm_ops hw_ops = {
146     .open = pcm_hw_open,
147     .close = pcm_hw_close,
148     .ioctl = pcm_hw_ioctl,
149     .mmap = pcm_hw_mmap,
150     .munmap = pcm_hw_munmap,
151     .poll = pcm_hw_poll,
152 };
153 
154