| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: ossaudio.c,v 1.39 2020/04/15 14:54:34 nia Exp $ */ | | 1 | /* $NetBSD: ossaudio.c,v 1.40 2020/04/15 15:25:33 nia Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1997 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 1997 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -17,27 +17,27 @@ | | | @@ -17,27 +17,27 @@ |
17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 17 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 18 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | __RCSID("$NetBSD: ossaudio.c,v 1.39 2020/04/15 14:54:34 nia Exp $"); | | 30 | __RCSID("$NetBSD: ossaudio.c,v 1.40 2020/04/15 15:25:33 nia Exp $"); |
31 | | | 31 | |
32 | /* | | 32 | /* |
33 | * This is an OSS (Linux) sound API emulator. | | 33 | * This is an OSS (Linux) sound API emulator. |
34 | * It provides the essentials of the API. | | 34 | * It provides the essentials of the API. |
35 | */ | | 35 | */ |
36 | | | 36 | |
37 | /* XXX This file is essentially the same as sys/compat/ossaudio.c. | | 37 | /* XXX This file is essentially the same as sys/compat/ossaudio.c. |
38 | * With some preprocessor magic it could be the same file. | | 38 | * With some preprocessor magic it could be the same file. |
39 | */ | | 39 | */ |
40 | | | 40 | |
41 | #include <string.h> | | 41 | #include <string.h> |
42 | #include <sys/types.h> | | 42 | #include <sys/types.h> |
43 | #include <sys/ioctl.h> | | 43 | #include <sys/ioctl.h> |
| @@ -228,60 +228,82 @@ audio_ioctl(int fd, unsigned long com, v | | | @@ -228,60 +228,82 @@ audio_ioctl(int fd, unsigned long com, v |
228 | break; | | 228 | break; |
229 | case AFMT_U16_LE: | | 229 | case AFMT_U16_LE: |
230 | tmpinfo.play.precision = | | 230 | tmpinfo.play.precision = |
231 | tmpinfo.record.precision = 16; | | 231 | tmpinfo.record.precision = 16; |
232 | tmpinfo.play.encoding = | | 232 | tmpinfo.play.encoding = |
233 | tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE; | | 233 | tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE; |
234 | break; | | 234 | break; |
235 | case AFMT_U16_BE: | | 235 | case AFMT_U16_BE: |
236 | tmpinfo.play.precision = | | 236 | tmpinfo.play.precision = |
237 | tmpinfo.record.precision = 16; | | 237 | tmpinfo.record.precision = 16; |
238 | tmpinfo.play.encoding = | | 238 | tmpinfo.play.encoding = |
239 | tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE; | | 239 | tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE; |
240 | break; | | 240 | break; |
| | | 241 | /* |
| | | 242 | * XXX: When the kernel supports 24-bit LPCM by default, |
| | | 243 | * the 24-bit formats should be handled properly instead |
| | | 244 | * of falling back to 32 bits. |
| | | 245 | */ |
241 | case AFMT_S24_LE: | | 246 | case AFMT_S24_LE: |
242 | tmpinfo.play.precision = | | | |
243 | tmpinfo.record.precision = 24; | | | |
244 | tmpinfo.play.encoding = | | | |
245 | tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE; | | | |
246 | break; | | | |
247 | case AFMT_S24_BE: | | | |
248 | tmpinfo.play.precision = | | | |
249 | tmpinfo.record.precision = 24; | | | |
250 | tmpinfo.play.encoding = | | | |
251 | tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE; | | | |
252 | break; | | | |
253 | case AFMT_S32_LE: | | 247 | case AFMT_S32_LE: |
254 | tmpinfo.play.precision = | | 248 | tmpinfo.play.precision = |
255 | tmpinfo.record.precision = 32; | | 249 | tmpinfo.record.precision = 32; |
256 | tmpinfo.play.encoding = | | 250 | tmpinfo.play.encoding = |
257 | tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE; | | 251 | tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE; |
258 | break; | | 252 | break; |
| | | 253 | case AFMT_S24_BE: |
259 | case AFMT_S32_BE: | | 254 | case AFMT_S32_BE: |
260 | tmpinfo.play.precision = | | 255 | tmpinfo.play.precision = |
261 | tmpinfo.record.precision = 32; | | 256 | tmpinfo.record.precision = 32; |
262 | tmpinfo.play.encoding = | | 257 | tmpinfo.play.encoding = |
263 | tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE; | | 258 | tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE; |
264 | break; | | 259 | break; |
265 | case AFMT_AC3: | | 260 | case AFMT_AC3: |
266 | tmpinfo.play.precision = | | 261 | tmpinfo.play.precision = |
267 | tmpinfo.record.precision = 16; | | 262 | tmpinfo.record.precision = 16; |
268 | tmpinfo.play.encoding = | | 263 | tmpinfo.play.encoding = |
269 | tmpinfo.record.encoding = AUDIO_ENCODING_AC3; | | 264 | tmpinfo.record.encoding = AUDIO_ENCODING_AC3; |
270 | break; | | 265 | break; |
271 | default: | | 266 | default: |
272 | return EINVAL; | | 267 | /* |
| | | 268 | * OSSv4 specifies that if an invalid format is chosen |
| | | 269 | * by an application then a sensible format supported |
| | | 270 | * by the hardware is returned. |
| | | 271 | * |
| | | 272 | * In this case, we pick the current hardware format. |
| | | 273 | */ |
| | | 274 | retval = ioctl(fd, AUDIO_GETFORMAT, &hwfmt); |
| | | 275 | if (retval < 0) |
| | | 276 | return retval; |
| | | 277 | retval = ioctl(fd, AUDIO_GETINFO, &tmpinfo); |
| | | 278 | if (retval < 0) |
| | | 279 | return retval; |
| | | 280 | tmpinfo.play.encoding = |
| | | 281 | tmpinfo.record.encoding = |
| | | 282 | (tmpinfo.mode == AUMODE_RECORD) ? |
| | | 283 | hwfmt.record.encoding : hwfmt.play.encoding; |
| | | 284 | tmpinfo.play.precision = |
| | | 285 | tmpinfo.record.precision = |
| | | 286 | (tmpinfo.mode == AUMODE_RECORD) ? |
| | | 287 | hwfmt.record.precision : hwfmt.play.precision ; |
| | | 288 | break; |
273 | } | | 289 | } |
274 | (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo); | | 290 | /* |
| | | 291 | * In the post-kernel-mixer world, assume that any error means |
| | | 292 | * it's fatal rather than an unsupported format being selected. |
| | | 293 | */ |
| | | 294 | retval = ioctl(fd, AUDIO_SETINFO, &tmpinfo); |
| | | 295 | if (retval < 0) |
| | | 296 | return retval; |
275 | /* FALLTHRU */ | | 297 | /* FALLTHRU */ |
276 | case SOUND_PCM_READ_BITS: | | 298 | case SOUND_PCM_READ_BITS: |
277 | retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo); | | 299 | retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo); |
278 | if (retval < 0) | | 300 | if (retval < 0) |
279 | return retval; | | 301 | return retval; |
280 | encoding = GETPRINFO(&tmpinfo, encoding); | | 302 | encoding = GETPRINFO(&tmpinfo, encoding); |
281 | precision = GETPRINFO(&tmpinfo, precision); | | 303 | precision = GETPRINFO(&tmpinfo, precision); |
282 | switch (encoding) { | | 304 | switch (encoding) { |
283 | case AUDIO_ENCODING_ULAW: | | 305 | case AUDIO_ENCODING_ULAW: |
284 | idat = AFMT_MU_LAW; | | 306 | idat = AFMT_MU_LAW; |
285 | break; | | 307 | break; |
286 | case AUDIO_ENCODING_ALAW: | | 308 | case AUDIO_ENCODING_ALAW: |
287 | idat = AFMT_A_LAW; | | 309 | idat = AFMT_A_LAW; |