• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* mixer_hw.c
2 **
3 ** Copyright (c) 2019, The Linux Foundation. All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are
7 ** met:
8 **   * Redistributions of source code must retain the above copyright
9 **     notice, this list of conditions and the following disclaimer.
10 **   * Redistributions in binary form must reproduce the above
11 **     copyright notice, this list of conditions and the following
12 **     disclaimer in the documentation and/or other materials provided
13 **     with the distribution.
14 **   * Neither the name of The Linux Foundation nor the names of its
15 **     contributors may be used to endorse or promote products derived
16 **     from this software without specific prior written permission.
17 **
18 ** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
21 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 **/
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <ctype.h>
41 #include <poll.h>
42 
43 #include <sys/ioctl.h>
44 
45 #include <linux/ioctl.h>
46 #include <sound/asound.h>
47 
48 #include "mixer_io.h"
49 
50 struct mixer_hw_data {
51     unsigned int card;
52     int fd;
53 };
54 
mixer_hw_close(void * data)55 static void mixer_hw_close(void *data)
56 {
57     struct mixer_hw_data *hw_data = data;
58 
59     if (!hw_data)
60         return;
61 
62     if (hw_data->fd >= 0)
63         close(hw_data->fd);
64 
65     hw_data->fd = -1;
66     free(hw_data);
67     hw_data = NULL;
68 }
69 
mixer_hw_ioctl(void * data,unsigned int cmd,...)70 static int mixer_hw_ioctl(void *data, unsigned int cmd, ...)
71 {
72     struct mixer_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 
mixer_hw_read_event(void * data,struct snd_ctl_event * ev,size_t size)83 static ssize_t mixer_hw_read_event(void *data, struct snd_ctl_event *ev,
84                                    size_t size)
85 {
86     struct mixer_hw_data *hw_data = data;
87 
88     return read(hw_data->fd, ev, size);
89 }
90 
91 static struct mixer_ops mixer_hw_ops = {
92     .close = mixer_hw_close,
93     .get_poll_fd = NULL,
94     .read_event = mixer_hw_read_event,
95     .ioctl = mixer_hw_ioctl,
96 };
97 
mixer_hw_open(unsigned int card,void ** data,struct mixer_ops ** ops)98 int mixer_hw_open(unsigned int card, void **data,
99                   struct mixer_ops **ops)
100 {
101     struct mixer_hw_data *hw_data;
102     int fd;
103     char fn[256];
104 
105     snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
106     fd = open(fn, O_RDWR);
107     if (fd < 0)
108         return fd;
109 
110     hw_data = calloc(1, sizeof(*hw_data));
111     if (!hw_data) {
112         close(fd);
113         return -1;
114     }
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