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