1 /* mixer_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 <stdarg.h>
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <ctype.h>
40 #include <poll.h>
41 
42 #include <sys/ioctl.h>
43 
44 #include <linux/ioctl.h>
45 #include <sound/asound.h>
46 
47 #include "mixer_io.h"
48 
49 /** Store the hardware (kernel interface) mixer data */
50 struct mixer_hw_data {
51     /* Card number for the mixer */
52     unsigned int card;
53     /* File descriptor of the mixer device node */
54     int fd;
55 };
56 
mixer_hw_close(void * data)57 static void mixer_hw_close(void *data)
58 {
59     struct mixer_hw_data *hw_data = data;
60 
61     if (!hw_data)
62         return;
63 
64     if (hw_data->fd >= 0)
65         close(hw_data->fd);
66 
67     hw_data->fd = -1;
68     free(hw_data);
69     hw_data = NULL;
70 }
71 
mixer_hw_ioctl(void * data,unsigned int cmd,...)72 static int mixer_hw_ioctl(void *data, unsigned int cmd, ...)
73 {
74     struct mixer_hw_data *hw_data = data;
75     va_list ap;
76     void *arg;
77 
78     va_start(ap, cmd);
79     arg = va_arg(ap, void *);
80     va_end(ap);
81 
82     return ioctl(hw_data->fd, cmd, arg);
83 }
84 
mixer_hw_read_event(void * data,struct snd_ctl_event * ev,size_t size)85 static ssize_t mixer_hw_read_event(void *data, struct snd_ctl_event *ev,
86                                    size_t size)
87 {
88     struct mixer_hw_data *hw_data = data;
89 
90     return read(hw_data->fd, ev, size);
91 }
92 
93 static const struct mixer_ops mixer_hw_ops = {
94     .close = mixer_hw_close,
95     .ioctl = mixer_hw_ioctl,
96     .read_event = mixer_hw_read_event,
97 };
98 
mixer_hw_open(unsigned int card,void ** data,const struct mixer_ops ** ops)99 int mixer_hw_open(unsigned int card, void **data,
100                   const struct mixer_ops **ops)
101 {
102     struct mixer_hw_data *hw_data;
103     int fd;
104     char fn[256];
105 
106     snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
107     fd = open(fn, O_RDWR);
108     if (fd < 0)
109         return fd;
110 
111     hw_data = calloc(1, sizeof(*hw_data));
112     if (!hw_data)
113         return -1;
114 
115     hw_data->card = card;
116     hw_data->fd = fd;
117     *data = hw_data;
118     *ops = &mixer_hw_ops;
119 
120     return fd;
121 }
122