15325 bhyve upstream sync 2023 January
[illumos-gate.git] / usr / src / cmd / bhyve / audio.c
blob165face5862ce2475678dee2dde474fc6cc8bf4b
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #ifndef WITHOUT_CAPSICUM
34 #include <sys/capsicum.h>
35 #include <capsicum_helpers.h>
36 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
41 #include <sys/ioctl.h>
42 #include <unistd.h>
43 #include <assert.h>
44 #include <errno.h>
45 #include <err.h>
46 #include <sysexits.h>
48 #include "audio.h"
49 #include "pci_hda.h"
52 * Audio Player internal data structures
55 struct audio {
56 int fd;
57 uint8_t dir;
58 uint8_t inited;
59 char dev_name[64];
63 * Audio Player module function definitions
67 * audio_init - initialize an instance of audio player
68 * @dev_name - the backend sound device used to play / capture
69 * @dir - dir = 1 for write mode, dir = 0 for read mode
71 struct audio *
72 audio_init(const char *dev_name, uint8_t dir)
74 struct audio *aud = NULL;
75 #ifndef WITHOUT_CAPSICUM
76 cap_rights_t rights;
77 cap_ioctl_t cmds[] = {
78 SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_CHANNELS,
79 SNDCTL_DSP_SPEED,
80 #ifdef DEBUG_HDA
81 SNDCTL_DSP_GETOSPACE, SNDCTL_DSP_GETISPACE,
82 #endif
84 #endif
86 assert(dev_name);
88 aud = calloc(1, sizeof(*aud));
89 if (!aud)
90 return NULL;
92 if (strlen(dev_name) < sizeof(aud->dev_name))
93 memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
94 else {
95 DPRINTF("dev_name too big");
96 free(aud);
97 return NULL;
100 aud->dir = dir;
102 aud->fd = open(aud->dev_name, aud->dir ? O_WRONLY : O_RDONLY, 0);
103 if (aud->fd == -1) {
104 DPRINTF("Failed to open dev: %s, errno: %d",
105 aud->dev_name, errno);
106 free(aud);
107 return (NULL);
110 #ifndef WITHOUT_CAPSICUM
111 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
112 if (caph_rights_limit(aud->fd, &rights) == -1)
113 errx(EX_OSERR, "Unable to apply rights for sandbox");
114 if (caph_ioctls_limit(aud->fd, cmds, nitems(cmds)) == -1)
115 errx(EX_OSERR, "Unable to limit ioctl rights for sandbox");
116 #endif
118 return aud;
122 * audio_set_params - reset the sound device and set the audio params
123 * @aud - the audio player to be configured
124 * @params - the audio parameters to be set
127 audio_set_params(struct audio *aud, struct audio_params *params)
129 int audio_fd;
130 int format, channels, rate;
131 int err;
132 #if DEBUG_HDA == 1
133 audio_buf_info info;
134 #endif
136 assert(aud);
137 assert(params);
139 if ((audio_fd = aud->fd) < 0) {
140 DPRINTF("Incorrect audio device descriptor for %s",
141 aud->dev_name);
142 return (-1);
145 /* Reset the device if it was previously opened */
146 if (aud->inited) {
147 err = ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
148 if (err == -1) {
149 DPRINTF("Failed to reset fd: %d, errno: %d",
150 aud->fd, errno);
151 return (-1);
153 } else
154 aud->inited = 1;
156 /* Set the Format (Bits per Sample) */
157 format = params->format;
158 err = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
159 if (err == -1) {
160 DPRINTF("Fail to set fmt: 0x%x errno: %d",
161 params->format, errno);
162 return -1;
165 /* The device does not support the requested audio format */
166 if (format != params->format) {
167 DPRINTF("Mismatch format: 0x%x params->format: 0x%x",
168 format, params->format);
169 return -1;
172 /* Set the Number of Channels */
173 channels = params->channels;
174 err = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels);
175 if (err == -1) {
176 DPRINTF("Fail to set channels: %d errno: %d",
177 params->channels, errno);
178 return -1;
181 /* The device does not support the requested no. of channels */
182 if (channels != params->channels) {
183 DPRINTF("Mismatch channels: %d params->channels: %d",
184 channels, params->channels);
185 return -1;
188 /* Set the Sample Rate / Speed */
189 rate = params->rate;
190 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
191 if (err == -1) {
192 DPRINTF("Fail to set speed: %d errno: %d",
193 params->rate, errno);
194 return -1;
197 /* The device does not support the requested rate / speed */
198 if (rate != params->rate) {
199 DPRINTF("Mismatch rate: %d params->rate: %d",
200 rate, params->rate);
201 return -1;
204 #if DEBUG_HDA == 1
205 err = ioctl(audio_fd, aud->dir ? SNDCTL_DSP_GETOSPACE :
206 SNDCTL_DSP_GETISPACE, &info);
207 if (err == -1) {
208 DPRINTF("Fail to get audio buf info errno: %d", errno);
209 return -1;
211 DPRINTF("fragstotal: 0x%x fragsize: 0x%x",
212 info.fragstotal, info.fragsize);
213 #endif
214 return 0;
218 * audio_playback - plays samples to the sound device using blocking operations
219 * @aud - the audio player used to play the samples
220 * @buf - the buffer containing the samples
221 * @count - the number of bytes in buffer
224 audio_playback(struct audio *aud, const uint8_t *buf, size_t count)
226 ssize_t len;
227 size_t total;
228 int audio_fd;
230 assert(aud);
231 assert(aud->dir);
232 assert(buf);
234 audio_fd = aud->fd;
235 assert(audio_fd != -1);
237 for (total = 0; total < count; total += len) {
238 len = write(audio_fd, buf + total, count - total);
239 if (len < 0) {
240 DPRINTF("Fail to write to fd: %d, errno: %d",
241 audio_fd, errno);
242 return -1;
246 return 0;
250 * audio_record - records samples from the sound device using
251 * blocking operations.
252 * @aud - the audio player used to capture the samples
253 * @buf - the buffer to receive the samples
254 * @count - the number of bytes to capture in buffer
255 * Returns -1 on error and 0 on success
258 audio_record(struct audio *aud, uint8_t *buf, size_t count)
260 ssize_t len;
261 size_t total;
262 int audio_fd;
264 assert(aud);
265 assert(!aud->dir);
266 assert(buf);
268 audio_fd = aud->fd;
269 assert(audio_fd != -1);
271 for (total = 0; total < count; total += len) {
272 len = read(audio_fd, buf + total, count - total);
273 if (len < 0) {
274 DPRINTF("Fail to write to fd: %d, errno: %d",
275 audio_fd, errno);
276 return -1;
280 return 0;