1*9cab9fdeSChristos Margiolis /*-
2*9cab9fdeSChristos Margiolis * Copyright (c) 2015-2019 Hans Petter Selasky
3*9cab9fdeSChristos Margiolis *
4*9cab9fdeSChristos Margiolis * Redistribution and use in source and binary forms, with or without
5*9cab9fdeSChristos Margiolis * modification, are permitted provided that the following conditions
6*9cab9fdeSChristos Margiolis * are met:
7*9cab9fdeSChristos Margiolis * 1. Redistributions of source code must retain the above copyright
8*9cab9fdeSChristos Margiolis * notice, this list of conditions and the following disclaimer.
9*9cab9fdeSChristos Margiolis * 2. Redistributions in binary form must reproduce the above copyright
10*9cab9fdeSChristos Margiolis * notice, this list of conditions and the following disclaimer in the
11*9cab9fdeSChristos Margiolis * documentation and/or other materials provided with the distribution.
12*9cab9fdeSChristos Margiolis *
13*9cab9fdeSChristos Margiolis * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*9cab9fdeSChristos Margiolis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*9cab9fdeSChristos Margiolis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*9cab9fdeSChristos Margiolis * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*9cab9fdeSChristos Margiolis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*9cab9fdeSChristos Margiolis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*9cab9fdeSChristos Margiolis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*9cab9fdeSChristos Margiolis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*9cab9fdeSChristos Margiolis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*9cab9fdeSChristos Margiolis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*9cab9fdeSChristos Margiolis * SUCH DAMAGE.
24*9cab9fdeSChristos Margiolis */
25*9cab9fdeSChristos Margiolis
26*9cab9fdeSChristos Margiolis #include <sys/queue.h>
27*9cab9fdeSChristos Margiolis #include <sys/filio.h>
28*9cab9fdeSChristos Margiolis #include <sys/soundcard.h>
29*9cab9fdeSChristos Margiolis
30*9cab9fdeSChristos Margiolis #include <stdint.h>
31*9cab9fdeSChristos Margiolis #include <stdlib.h>
32*9cab9fdeSChristos Margiolis #include <string.h>
33*9cab9fdeSChristos Margiolis #include <fcntl.h>
34*9cab9fdeSChristos Margiolis #include <unistd.h>
35*9cab9fdeSChristos Margiolis #include <err.h>
36*9cab9fdeSChristos Margiolis #include <poll.h>
37*9cab9fdeSChristos Margiolis
38*9cab9fdeSChristos Margiolis #include "backend.h"
39*9cab9fdeSChristos Margiolis #include "int.h"
40*9cab9fdeSChristos Margiolis
41*9cab9fdeSChristos Margiolis static int
oss_set_format(int fd,int * format)42*9cab9fdeSChristos Margiolis oss_set_format(int fd, int *format)
43*9cab9fdeSChristos Margiolis {
44*9cab9fdeSChristos Margiolis int value[6];
45*9cab9fdeSChristos Margiolis int error;
46*9cab9fdeSChristos Margiolis int fmt;
47*9cab9fdeSChristos Margiolis int i;
48*9cab9fdeSChristos Margiolis
49*9cab9fdeSChristos Margiolis value[0] = *format & VPREFERRED_SNE_AFMT;
50*9cab9fdeSChristos Margiolis value[1] = *format & VPREFERRED_UNE_AFMT;
51*9cab9fdeSChristos Margiolis value[2] = *format & VPREFERRED_SLE_AFMT;
52*9cab9fdeSChristos Margiolis value[3] = *format & VPREFERRED_SBE_AFMT;
53*9cab9fdeSChristos Margiolis value[4] = *format & VPREFERRED_ULE_AFMT;
54*9cab9fdeSChristos Margiolis value[5] = *format & VPREFERRED_UBE_AFMT;
55*9cab9fdeSChristos Margiolis
56*9cab9fdeSChristos Margiolis for (i = 0; i != 6; i++) {
57*9cab9fdeSChristos Margiolis fmt = value[i];
58*9cab9fdeSChristos Margiolis if (fmt == 0)
59*9cab9fdeSChristos Margiolis continue;
60*9cab9fdeSChristos Margiolis error = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
61*9cab9fdeSChristos Margiolis /* make sure we got the format we asked for */
62*9cab9fdeSChristos Margiolis if (error == 0 && fmt == value[i]) {
63*9cab9fdeSChristos Margiolis *format = fmt;
64*9cab9fdeSChristos Margiolis return (0);
65*9cab9fdeSChristos Margiolis }
66*9cab9fdeSChristos Margiolis }
67*9cab9fdeSChristos Margiolis return (-1);
68*9cab9fdeSChristos Margiolis }
69*9cab9fdeSChristos Margiolis
70*9cab9fdeSChristos Margiolis static void
oss_close(struct voss_backend * pbe)71*9cab9fdeSChristos Margiolis oss_close(struct voss_backend *pbe)
72*9cab9fdeSChristos Margiolis {
73*9cab9fdeSChristos Margiolis if (pbe->fd > -1) {
74*9cab9fdeSChristos Margiolis close(pbe->fd);
75*9cab9fdeSChristos Margiolis pbe->fd = -1;
76*9cab9fdeSChristos Margiolis }
77*9cab9fdeSChristos Margiolis }
78*9cab9fdeSChristos Margiolis
79*9cab9fdeSChristos Margiolis static int
oss_open(struct voss_backend * pbe,const char * devname,int samplerate,int bufsize,int * pchannels,int * pformat,int attr,int fionbio)80*9cab9fdeSChristos Margiolis oss_open(struct voss_backend *pbe, const char *devname, int samplerate,
81*9cab9fdeSChristos Margiolis int bufsize, int *pchannels, int *pformat, int attr, int fionbio)
82*9cab9fdeSChristos Margiolis {
83*9cab9fdeSChristos Margiolis int temp;
84*9cab9fdeSChristos Margiolis int err;
85*9cab9fdeSChristos Margiolis
86*9cab9fdeSChristos Margiolis pbe->fd = open(devname, attr);
87*9cab9fdeSChristos Margiolis if (pbe->fd < 0) {
88*9cab9fdeSChristos Margiolis warn("Could not open DSP device '%s'", devname);
89*9cab9fdeSChristos Margiolis return (-1);
90*9cab9fdeSChristos Margiolis }
91*9cab9fdeSChristos Margiolis err = ioctl(pbe->fd, FIONBIO, &fionbio);
92*9cab9fdeSChristos Margiolis if (err < 0) {
93*9cab9fdeSChristos Margiolis warn("Could not set blocking mode on DSP");
94*9cab9fdeSChristos Margiolis goto error;
95*9cab9fdeSChristos Margiolis }
96*9cab9fdeSChristos Margiolis err = oss_set_format(pbe->fd, pformat);
97*9cab9fdeSChristos Margiolis if (err < 0) {
98*9cab9fdeSChristos Margiolis warn("Could not set sample format 0x%08x", *pformat);
99*9cab9fdeSChristos Margiolis goto error;
100*9cab9fdeSChristos Margiolis }
101*9cab9fdeSChristos Margiolis temp = *pchannels;
102*9cab9fdeSChristos Margiolis bufsize /= temp; /* get buffer size per channel */
103*9cab9fdeSChristos Margiolis do {
104*9cab9fdeSChristos Margiolis err = ioctl(pbe->fd, SOUND_PCM_WRITE_CHANNELS, &temp);
105*9cab9fdeSChristos Margiolis } while (err < 0 && --temp > 0);
106*9cab9fdeSChristos Margiolis
107*9cab9fdeSChristos Margiolis err = ioctl(pbe->fd, SOUND_PCM_READ_CHANNELS, &temp);
108*9cab9fdeSChristos Margiolis if (err < 0 || temp <= 0 || temp > *pchannels) {
109*9cab9fdeSChristos Margiolis warn("Could not set DSP channels: %d / %d", temp, *pchannels);
110*9cab9fdeSChristos Margiolis goto error;
111*9cab9fdeSChristos Margiolis }
112*9cab9fdeSChristos Margiolis *pchannels = temp;
113*9cab9fdeSChristos Margiolis
114*9cab9fdeSChristos Margiolis temp = samplerate;
115*9cab9fdeSChristos Margiolis err = ioctl(pbe->fd, SNDCTL_DSP_SPEED, &temp);
116*9cab9fdeSChristos Margiolis if (err < 0 || temp != samplerate) {
117*9cab9fdeSChristos Margiolis warn("Could not set sample rate to %d / %d Hz", temp, samplerate);
118*9cab9fdeSChristos Margiolis goto error;
119*9cab9fdeSChristos Margiolis }
120*9cab9fdeSChristos Margiolis
121*9cab9fdeSChristos Margiolis temp = bufsize * (*pchannels);
122*9cab9fdeSChristos Margiolis err = ioctl(pbe->fd, SNDCTL_DSP_SETBLKSIZE, &temp);
123*9cab9fdeSChristos Margiolis if (err < 0) {
124*9cab9fdeSChristos Margiolis warn("Could not set block size to %d", temp);
125*9cab9fdeSChristos Margiolis goto error;
126*9cab9fdeSChristos Margiolis }
127*9cab9fdeSChristos Margiolis return (0);
128*9cab9fdeSChristos Margiolis error:
129*9cab9fdeSChristos Margiolis close(pbe->fd);
130*9cab9fdeSChristos Margiolis pbe->fd = -1;
131*9cab9fdeSChristos Margiolis return (-1);
132*9cab9fdeSChristos Margiolis }
133*9cab9fdeSChristos Margiolis
134*9cab9fdeSChristos Margiolis static int
oss_rec_open(struct voss_backend * pbe,const char * devname,int samplerate,int bufsize,int * pchannels,int * pformat)135*9cab9fdeSChristos Margiolis oss_rec_open(struct voss_backend *pbe, const char *devname, int samplerate,
136*9cab9fdeSChristos Margiolis int bufsize, int *pchannels, int *pformat)
137*9cab9fdeSChristos Margiolis {
138*9cab9fdeSChristos Margiolis return (oss_open(pbe, devname, samplerate, bufsize, pchannels, pformat, O_RDONLY, 0));
139*9cab9fdeSChristos Margiolis }
140*9cab9fdeSChristos Margiolis
141*9cab9fdeSChristos Margiolis static int
oss_play_open(struct voss_backend * pbe,const char * devname,int samplerate,int bufsize,int * pchannels,int * pformat)142*9cab9fdeSChristos Margiolis oss_play_open(struct voss_backend *pbe, const char *devname, int samplerate,
143*9cab9fdeSChristos Margiolis int bufsize, int *pchannels, int *pformat)
144*9cab9fdeSChristos Margiolis {
145*9cab9fdeSChristos Margiolis bufsize *= 4; /* XXX allow extra space for jitter */
146*9cab9fdeSChristos Margiolis return (oss_open(pbe, devname, samplerate, bufsize, pchannels, pformat, O_WRONLY, 0));
147*9cab9fdeSChristos Margiolis }
148*9cab9fdeSChristos Margiolis
149*9cab9fdeSChristos Margiolis static int
oss_rec_transfer(struct voss_backend * pbe,void * ptr,int len)150*9cab9fdeSChristos Margiolis oss_rec_transfer(struct voss_backend *pbe, void *ptr, int len)
151*9cab9fdeSChristos Margiolis {
152*9cab9fdeSChristos Margiolis struct pollfd fds = { .fd = pbe->fd, .events = POLLIN | POLLRDNORM };
153*9cab9fdeSChristos Margiolis int err;
154*9cab9fdeSChristos Margiolis
155*9cab9fdeSChristos Margiolis /* wait at maximum 2 seconds for data, else something is wrong */
156*9cab9fdeSChristos Margiolis err = poll(&fds, 1, 2000);
157*9cab9fdeSChristos Margiolis if (err < 1)
158*9cab9fdeSChristos Margiolis return (-1);
159*9cab9fdeSChristos Margiolis return (read(pbe->fd, ptr, len));
160*9cab9fdeSChristos Margiolis }
161*9cab9fdeSChristos Margiolis
162*9cab9fdeSChristos Margiolis static int
oss_play_transfer(struct voss_backend * pbe,void * ptr,int len)163*9cab9fdeSChristos Margiolis oss_play_transfer(struct voss_backend *pbe, void *ptr, int len)
164*9cab9fdeSChristos Margiolis {
165*9cab9fdeSChristos Margiolis return (write(pbe->fd, ptr, len));
166*9cab9fdeSChristos Margiolis }
167*9cab9fdeSChristos Margiolis
168*9cab9fdeSChristos Margiolis static void
oss_rec_delay(struct voss_backend * pbe,int * pdelay)169*9cab9fdeSChristos Margiolis oss_rec_delay(struct voss_backend *pbe, int *pdelay)
170*9cab9fdeSChristos Margiolis {
171*9cab9fdeSChristos Margiolis if (ioctl(pbe->fd, FIONREAD, pdelay) != 0)
172*9cab9fdeSChristos Margiolis *pdelay = -1;
173*9cab9fdeSChristos Margiolis }
174*9cab9fdeSChristos Margiolis
175*9cab9fdeSChristos Margiolis static void
oss_play_delay(struct voss_backend * pbe,int * pdelay)176*9cab9fdeSChristos Margiolis oss_play_delay(struct voss_backend *pbe, int *pdelay)
177*9cab9fdeSChristos Margiolis {
178*9cab9fdeSChristos Margiolis if (voss_has_synchronization != 0 ||
179*9cab9fdeSChristos Margiolis ioctl(pbe->fd, SNDCTL_DSP_GETODELAY, pdelay) != 0)
180*9cab9fdeSChristos Margiolis *pdelay = -1;
181*9cab9fdeSChristos Margiolis }
182*9cab9fdeSChristos Margiolis
183*9cab9fdeSChristos Margiolis struct voss_backend voss_backend_oss_rec = {
184*9cab9fdeSChristos Margiolis .open = oss_rec_open,
185*9cab9fdeSChristos Margiolis .close = oss_close,
186*9cab9fdeSChristos Margiolis .transfer = oss_rec_transfer,
187*9cab9fdeSChristos Margiolis .delay = oss_rec_delay,
188*9cab9fdeSChristos Margiolis .fd = -1,
189*9cab9fdeSChristos Margiolis };
190*9cab9fdeSChristos Margiolis
191*9cab9fdeSChristos Margiolis struct voss_backend voss_backend_oss_play = {
192*9cab9fdeSChristos Margiolis .open = oss_play_open,
193*9cab9fdeSChristos Margiolis .close = oss_close,
194*9cab9fdeSChristos Margiolis .transfer = oss_play_transfer,
195*9cab9fdeSChristos Margiolis .delay = oss_play_delay,
196*9cab9fdeSChristos Margiolis .fd = -1,
197*9cab9fdeSChristos Margiolis };
198