1dd81b19eSChristos Margiolis /*-
2dd81b19eSChristos Margiolis * SPDX-License-Identifier: BSD-2-Clause
3dd81b19eSChristos Margiolis *
4dd81b19eSChristos Margiolis * Copyright (c) 2025 The FreeBSD Foundation
5dd81b19eSChristos Margiolis *
6dd81b19eSChristos Margiolis * This software was developed by Christos Margiolis <christos@FreeBSD.org>
7dd81b19eSChristos Margiolis * under sponsorship from the FreeBSD Foundation.
8dd81b19eSChristos Margiolis *
9dd81b19eSChristos Margiolis * Redistribution and use in source and binary forms, with or without
10dd81b19eSChristos Margiolis * modification, are permitted provided that the following conditions
11dd81b19eSChristos Margiolis * are met:
12dd81b19eSChristos Margiolis * 1. Redistributions of source code must retain the above copyright
13dd81b19eSChristos Margiolis * notice, this list of conditions and the following disclaimer.
14dd81b19eSChristos Margiolis * 2. Redistributions in binary form must reproduce the above copyright
15dd81b19eSChristos Margiolis * notice, this list of conditions and the following disclaimer in the
16dd81b19eSChristos Margiolis * documentation and/or other materials provided with the distribution.
17dd81b19eSChristos Margiolis *
18dd81b19eSChristos Margiolis * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19dd81b19eSChristos Margiolis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20dd81b19eSChristos Margiolis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21dd81b19eSChristos Margiolis * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22dd81b19eSChristos Margiolis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23dd81b19eSChristos Margiolis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24dd81b19eSChristos Margiolis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25dd81b19eSChristos Margiolis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26dd81b19eSChristos Margiolis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27dd81b19eSChristos Margiolis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28dd81b19eSChristos Margiolis * SUCH DAMAGE.
29dd81b19eSChristos Margiolis */
30dd81b19eSChristos Margiolis
31dd81b19eSChristos Margiolis #include <sys/event.h>
32dd81b19eSChristos Margiolis #include <sys/soundcard.h>
33dd81b19eSChristos Margiolis
34dd81b19eSChristos Margiolis #include <atf-c.h>
35dd81b19eSChristos Margiolis #include <errno.h>
36dd81b19eSChristos Margiolis #include <fcntl.h>
37*fb99c0baSDag-Erling Smørgrav #include <inttypes.h>
38dd81b19eSChristos Margiolis #include <poll.h>
39dd81b19eSChristos Margiolis #include <unistd.h>
40dd81b19eSChristos Margiolis
41dd81b19eSChristos Margiolis #define FMT_ERR(s) s ": %s", strerror(errno)
42dd81b19eSChristos Margiolis
43dd81b19eSChristos Margiolis static int
oss_init(void)44dd81b19eSChristos Margiolis oss_init(void)
45dd81b19eSChristos Margiolis {
46dd81b19eSChristos Margiolis int fd, tmp, rc;
47dd81b19eSChristos Margiolis
48dd81b19eSChristos Margiolis fd = open("/dev/dsp.dummy", O_RDWR);
49dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));
50dd81b19eSChristos Margiolis
51dd81b19eSChristos Margiolis tmp = 2;
52dd81b19eSChristos Margiolis rc = ioctl(fd, SNDCTL_DSP_CHANNELS, &tmp);
53dd81b19eSChristos Margiolis ATF_REQUIRE_EQ_MSG(rc, 0, FMT_ERR("ioctl"));
54dd81b19eSChristos Margiolis
55dd81b19eSChristos Margiolis tmp = AFMT_S16_LE;
56dd81b19eSChristos Margiolis rc = ioctl(fd, SNDCTL_DSP_SETFMT, &tmp);
57dd81b19eSChristos Margiolis ATF_REQUIRE_EQ_MSG(rc, 0, FMT_ERR("ioctl"));
58dd81b19eSChristos Margiolis
59dd81b19eSChristos Margiolis tmp = 48000;
60dd81b19eSChristos Margiolis rc = ioctl(fd, SNDCTL_DSP_SPEED, &tmp);
61dd81b19eSChristos Margiolis ATF_REQUIRE_EQ_MSG(rc, 0, FMT_ERR("ioctl"));
62dd81b19eSChristos Margiolis
63dd81b19eSChristos Margiolis /*
64dd81b19eSChristos Margiolis * See http://manuals.opensound.com/developer/SNDCTL_DSP_SETTRIGGER.html
65dd81b19eSChristos Margiolis */
66dd81b19eSChristos Margiolis tmp = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
67dd81b19eSChristos Margiolis rc = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);
68dd81b19eSChristos Margiolis ATF_REQUIRE_EQ_MSG(rc, 0, FMT_ERR("ioctl"));
69dd81b19eSChristos Margiolis
70dd81b19eSChristos Margiolis return (fd);
71dd81b19eSChristos Margiolis }
72dd81b19eSChristos Margiolis
73dd81b19eSChristos Margiolis ATF_TC(poll_kqueue);
ATF_TC_HEAD(poll_kqueue,tc)74dd81b19eSChristos Margiolis ATF_TC_HEAD(poll_kqueue, tc)
75dd81b19eSChristos Margiolis {
76dd81b19eSChristos Margiolis atf_tc_set_md_var(tc, "descr", "kqueue(2) test");
77dd81b19eSChristos Margiolis atf_tc_set_md_var(tc, "require.kmods", "snd_dummy");
78dd81b19eSChristos Margiolis }
79dd81b19eSChristos Margiolis
ATF_TC_BODY(poll_kqueue,tc)80dd81b19eSChristos Margiolis ATF_TC_BODY(poll_kqueue, tc)
81dd81b19eSChristos Margiolis {
82dd81b19eSChristos Margiolis struct kevent ev;
83dd81b19eSChristos Margiolis int16_t buf[32];
84dd81b19eSChristos Margiolis int fd, kq;
85dd81b19eSChristos Margiolis
86dd81b19eSChristos Margiolis fd = oss_init();
87dd81b19eSChristos Margiolis
88dd81b19eSChristos Margiolis kq = kqueue();
89dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(kq >= 0, FMT_ERR("kqueue"));
90dd81b19eSChristos Margiolis EV_SET(&ev, fd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0);
91dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
92dd81b19eSChristos Margiolis FMT_ERR("kevent"));
93dd81b19eSChristos Margiolis
94dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(kevent(kq, NULL, 0, &ev, 1, NULL) == 1,
95dd81b19eSChristos Margiolis FMT_ERR("kevent"));
96dd81b19eSChristos Margiolis ATF_REQUIRE_MSG((ev.flags & EV_ERROR) == 0, "EV_ERROR is set");
97*fb99c0baSDag-Erling Smørgrav ATF_REQUIRE_MSG(ev.data != 0, "data is %" PRId64, ev.data);
98dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(read(fd, buf, sizeof(buf)) > 0, FMT_ERR("read"));
99dd81b19eSChristos Margiolis
100dd81b19eSChristos Margiolis EV_SET(&ev, fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
101dd81b19eSChristos Margiolis close(kq);
102dd81b19eSChristos Margiolis
103dd81b19eSChristos Margiolis kq = kqueue();
104dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(kq >= 0, FMT_ERR("kqueue"));
105dd81b19eSChristos Margiolis EV_SET(&ev, fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0);
106dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
107dd81b19eSChristos Margiolis FMT_ERR("kevent"));
108dd81b19eSChristos Margiolis
109dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(kevent(kq, NULL, 0, &ev, 1, NULL) == 1,
110dd81b19eSChristos Margiolis FMT_ERR("kevent"));
111dd81b19eSChristos Margiolis ATF_REQUIRE_MSG((ev.flags & EV_ERROR) == 0, "EV_ERROR is set");
112*fb99c0baSDag-Erling Smørgrav ATF_REQUIRE_MSG(ev.data != 0, "data is %" PRId64, ev.data);
113dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) > 0, FMT_ERR("write"));
114dd81b19eSChristos Margiolis
115dd81b19eSChristos Margiolis EV_SET(&ev, fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
116dd81b19eSChristos Margiolis close(kq);
117dd81b19eSChristos Margiolis
118dd81b19eSChristos Margiolis close(fd);
119dd81b19eSChristos Margiolis }
120dd81b19eSChristos Margiolis
121dd81b19eSChristos Margiolis ATF_TC(poll_poll);
ATF_TC_HEAD(poll_poll,tc)122dd81b19eSChristos Margiolis ATF_TC_HEAD(poll_poll, tc)
123dd81b19eSChristos Margiolis {
124dd81b19eSChristos Margiolis atf_tc_set_md_var(tc, "descr", "poll(2) test");
125dd81b19eSChristos Margiolis atf_tc_set_md_var(tc, "require.kmods", "snd_dummy");
126dd81b19eSChristos Margiolis }
127dd81b19eSChristos Margiolis
ATF_TC_BODY(poll_poll,tc)128dd81b19eSChristos Margiolis ATF_TC_BODY(poll_poll, tc)
129dd81b19eSChristos Margiolis {
130dd81b19eSChristos Margiolis struct pollfd pfd[2];
131dd81b19eSChristos Margiolis int16_t buf[32];
132dd81b19eSChristos Margiolis int fd;
133dd81b19eSChristos Margiolis bool rd = false;
134dd81b19eSChristos Margiolis bool wr = false;
135dd81b19eSChristos Margiolis
136dd81b19eSChristos Margiolis fd = oss_init();
137dd81b19eSChristos Margiolis
138dd81b19eSChristos Margiolis while (!rd || !wr) {
139dd81b19eSChristos Margiolis pfd[0].fd = fd;
140dd81b19eSChristos Margiolis pfd[0].events = POLLIN;
141dd81b19eSChristos Margiolis pfd[1].fd = fd;
142dd81b19eSChristos Margiolis pfd[1].events = POLLOUT;
143dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(poll(pfd, sizeof(pfd) / sizeof(struct pollfd),
144dd81b19eSChristos Margiolis -1) > 0, FMT_ERR("poll"));
145dd81b19eSChristos Margiolis
146dd81b19eSChristos Margiolis if (pfd[0].revents) {
147dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(read(fd, buf, sizeof(buf)) > 0,
148dd81b19eSChristos Margiolis FMT_ERR("read"));
149dd81b19eSChristos Margiolis rd = true;
150dd81b19eSChristos Margiolis }
151dd81b19eSChristos Margiolis if (pfd[1].revents) {
152dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) > 0,
153dd81b19eSChristos Margiolis FMT_ERR("write"));
154dd81b19eSChristos Margiolis wr = true;
155dd81b19eSChristos Margiolis }
156dd81b19eSChristos Margiolis }
157dd81b19eSChristos Margiolis close(fd);
158dd81b19eSChristos Margiolis }
159dd81b19eSChristos Margiolis
160dd81b19eSChristos Margiolis ATF_TC(poll_select);
ATF_TC_HEAD(poll_select,tc)161dd81b19eSChristos Margiolis ATF_TC_HEAD(poll_select, tc)
162dd81b19eSChristos Margiolis {
163dd81b19eSChristos Margiolis atf_tc_set_md_var(tc, "descr", "select(2) test");
164dd81b19eSChristos Margiolis atf_tc_set_md_var(tc, "require.kmods", "snd_dummy");
165dd81b19eSChristos Margiolis }
166dd81b19eSChristos Margiolis
ATF_TC_BODY(poll_select,tc)167dd81b19eSChristos Margiolis ATF_TC_BODY(poll_select, tc)
168dd81b19eSChristos Margiolis {
169dd81b19eSChristos Margiolis fd_set fds[2];
170dd81b19eSChristos Margiolis int16_t buf[32];
171dd81b19eSChristos Margiolis int fd;
172dd81b19eSChristos Margiolis bool rd = false;
173dd81b19eSChristos Margiolis bool wr = false;
174dd81b19eSChristos Margiolis
175dd81b19eSChristos Margiolis fd = oss_init();
176dd81b19eSChristos Margiolis
177dd81b19eSChristos Margiolis while (!rd || !wr) {
178dd81b19eSChristos Margiolis FD_ZERO(&fds[0]);
179dd81b19eSChristos Margiolis FD_ZERO(&fds[1]);
180dd81b19eSChristos Margiolis FD_SET(fd, &fds[0]);
181dd81b19eSChristos Margiolis FD_SET(fd, &fds[1]);
18241f2ec3bSChristos Margiolis ATF_REQUIRE_MSG(select(fd + 2, &fds[0], &fds[1], NULL, NULL) > 0,
183dd81b19eSChristos Margiolis FMT_ERR("select"));
184dd81b19eSChristos Margiolis if (FD_ISSET(fd, &fds[0])) {
185dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(read(fd, buf, sizeof(buf)) > 0,
186dd81b19eSChristos Margiolis FMT_ERR("read"));
187dd81b19eSChristos Margiolis rd = true;
188dd81b19eSChristos Margiolis }
189dd81b19eSChristos Margiolis if (FD_ISSET(fd, &fds[1])) {
190dd81b19eSChristos Margiolis ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) > 0,
191dd81b19eSChristos Margiolis FMT_ERR("write"));
192dd81b19eSChristos Margiolis wr = true;
193dd81b19eSChristos Margiolis }
194dd81b19eSChristos Margiolis }
195dd81b19eSChristos Margiolis close(fd);
196dd81b19eSChristos Margiolis }
197dd81b19eSChristos Margiolis
ATF_TP_ADD_TCS(tp)198dd81b19eSChristos Margiolis ATF_TP_ADD_TCS(tp)
199dd81b19eSChristos Margiolis {
200dd81b19eSChristos Margiolis ATF_TP_ADD_TC(tp, poll_kqueue);
201dd81b19eSChristos Margiolis ATF_TP_ADD_TC(tp, poll_poll);
202dd81b19eSChristos Margiolis ATF_TP_ADD_TC(tp, poll_select);
203dd81b19eSChristos Margiolis
204dd81b19eSChristos Margiolis return (atf_no_error());
205dd81b19eSChristos Margiolis }
206