1 /* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * 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 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/kernel.h> 32 33 #include <dev/sound/pcm/sound.h> 34 35 static int getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch); 36 37 static pcm_channel * 38 allocchn(snddev_info *d, int direction) 39 { 40 pcm_channel *chns = (direction == PCMDIR_PLAY)? d->play : d->rec; 41 int i, cnt = (direction == PCMDIR_PLAY)? d->playcount : d->reccount; 42 for (i = 0; i < cnt; i++) { 43 if (!(chns[i].flags & CHN_F_BUSY)) { 44 chns[i].flags |= CHN_F_BUSY; 45 return &chns[i]; 46 } 47 } 48 return NULL; 49 } 50 51 static int 52 getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch) 53 { 54 if ((d->flags & SD_F_PRIO_SET) == SD_F_PRIO_SET) 55 panic("read and write both prioritised"); 56 if (d->flags & SD_F_SIMPLEX) { 57 *rdch = (d->flags & SD_F_PRIO_RD)? d->arec[chan] : &d->fakechan; 58 *wrch = (d->flags & SD_F_PRIO_WR)? d->aplay[chan] : &d->fakechan; 59 } else { 60 *rdch = d->arec[chan]; 61 *wrch = d->aplay[chan]; 62 } 63 return 0; 64 } 65 66 static void 67 setchns(snddev_info *d, int chan) 68 { 69 if ((d->flags & SD_F_PRIO_SET) == SD_F_PRIO_SET) 70 panic("read and write both prioritised"); 71 d->flags |= SD_F_DIR_SET; 72 if (d->flags & SD_F_EVILSB16) { 73 if ((d->flags & SD_F_PRIO_RD) && (d->aplay[chan])) { 74 pcm_channel *tmp; 75 tmp = d->arec[chan]; 76 d->arec[chan] = d->aplay[chan]; 77 d->aplay[chan] = tmp; 78 } 79 if (d->aplay[chan]) chn_setdir(d->aplay[chan], PCMDIR_PLAY); 80 if (d->arec[chan]) chn_setdir(d->arec[chan], PCMDIR_REC); 81 } 82 } 83 84 int 85 dsp_open(snddev_info *d, int chan, int oflags, int devtype) 86 { 87 pcm_channel *rdch = NULL, *wrch = NULL; 88 u_int32_t fmt; 89 90 if (chan >= d->chancount) return ENODEV; 91 if (d->aplay[chan] || d->arec[chan]) return EBUSY; 92 if (oflags & FREAD) { 93 rdch = allocchn(d, PCMDIR_REC); 94 if (!rdch) return EBUSY; 95 } 96 if (oflags & FWRITE) { 97 wrch = allocchn(d, PCMDIR_PLAY); 98 if (!wrch) { 99 if (rdch) rdch->flags &= ~CHN_F_BUSY; 100 return EBUSY; 101 } 102 } 103 d->aplay[chan] = wrch; 104 d->arec[chan] = rdch; 105 switch (devtype) { 106 case SND_DEV_DSP16: 107 fmt = AFMT_S16_LE; 108 break; 109 110 case SND_DEV_DSP: 111 fmt = AFMT_U8; 112 break; 113 114 case SND_DEV_AUDIO: 115 fmt = AFMT_MU_LAW; 116 break; 117 118 case SND_DEV_NORESET: 119 fmt = 0; 120 break; 121 122 default: 123 return ENXIO; 124 } 125 126 if (rdch) { 127 chn_reset(rdch); 128 if (oflags & O_NONBLOCK) rdch->flags |= CHN_F_NBIO; 129 if (fmt) { 130 rdch->volume = (100 << 8) | 100; 131 rdch->format = fmt; 132 rdch->speed = DSP_DEFAULT_SPEED; 133 rdch->blocksize = 2048; 134 } 135 } 136 if (wrch) { 137 chn_reset(wrch); 138 if (oflags & O_NONBLOCK) wrch->flags |= CHN_F_NBIO; 139 if (fmt) { 140 wrch->volume = (100 << 8) | 100; 141 wrch->format = fmt; 142 wrch->speed = DSP_DEFAULT_SPEED; 143 wrch->blocksize = 2048; 144 } 145 } 146 return 0; 147 } 148 149 int 150 dsp_close(snddev_info *d, int chan, int devtype) 151 { 152 pcm_channel *rdch, *wrch; 153 154 d->flags &= ~SD_F_TRANSIENT; 155 rdch = d->arec[chan]; 156 wrch = d->aplay[chan]; 157 158 if (rdch) { 159 chn_abort(rdch); 160 rdch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED); 161 } 162 if (wrch) wrch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED); 163 d->aplay[chan] = NULL; 164 d->arec[chan] = NULL; 165 return 0; 166 } 167 168 int 169 dsp_read(snddev_info *d, int chan, struct uio *buf, int flag) 170 { 171 pcm_channel *rdch, *wrch; 172 173 if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_RD; 174 if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan); 175 getchns(d, chan, &rdch, &wrch); 176 if (!rdch || !(rdch->flags & CHN_F_BUSY)) 177 panic("dsp_read: non%s channel", rdch? "busy" : "existant"); 178 if (rdch->flags & CHN_F_MAPPED) return EINVAL; 179 if (!(rdch->flags & CHN_F_RUNNING)) { 180 rdch->flags |= CHN_F_RUNNING; 181 chn_reinit(rdch); 182 } 183 return chn_read(rdch, buf); 184 } 185 186 int 187 dsp_write(snddev_info *d, int chan, struct uio *buf, int flag) 188 { 189 pcm_channel *rdch, *wrch; 190 191 if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_WR; 192 if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan); 193 getchns(d, chan, &rdch, &wrch); 194 if (!wrch || !(wrch->flags & CHN_F_BUSY)) 195 panic("dsp_write: non%s channel", wrch? "busy" : "existant"); 196 if (wrch->flags & CHN_F_MAPPED) return EINVAL; 197 if (!(wrch->flags & CHN_F_RUNNING)) { 198 wrch->flags |= CHN_F_RUNNING; 199 chn_reinit(wrch); 200 } 201 return chn_write(wrch, buf); 202 } 203 204 int 205 dsp_ioctl(snddev_info *d, int chan, u_long cmd, caddr_t arg) 206 { 207 int ret = 0, *arg_i = (int *)arg; 208 u_long s; 209 pcm_channel *wrch = NULL, *rdch = NULL; 210 211 rdch = d->arec[chan]; 212 wrch = d->aplay[chan]; 213 214 /* 215 * all routines are called with int. blocked. Make sure that 216 * ints are re-enabled when calling slow or blocking functions! 217 */ 218 s = spltty(); 219 switch(cmd) { 220 221 /* 222 * we start with the new ioctl interface. 223 */ 224 case AIONWRITE: /* how many bytes can write ? */ 225 if (wrch && wrch->buffer.dl) chn_dmaupdate(wrch); 226 *arg_i = wrch? wrch->buffer.fl : 0; 227 break; 228 229 case AIOSSIZE: /* set the current blocksize */ 230 { 231 struct snd_size *p = (struct snd_size *)arg; 232 splx(s); 233 if (wrch) chn_setblocksize(wrch, p->play_size); 234 if (rdch) chn_setblocksize(rdch, p->rec_size); 235 } 236 /* FALLTHROUGH */ 237 case AIOGSIZE: /* get the current blocksize */ 238 { 239 struct snd_size *p = (struct snd_size *)arg; 240 if (wrch) p->play_size = wrch->blocksize; 241 if (rdch) p->rec_size = rdch->blocksize; 242 } 243 break; 244 245 case AIOSFMT: 246 { 247 snd_chan_param *p = (snd_chan_param *)arg; 248 splx(s); 249 if (wrch) { 250 chn_setformat(wrch, p->play_format); 251 chn_setspeed(wrch, p->play_rate); 252 } 253 if (rdch) { 254 chn_setformat(rdch, p->rec_format); 255 chn_setspeed(rdch, p->rec_rate); 256 } 257 } 258 /* FALLTHROUGH */ 259 260 case AIOGFMT: 261 { 262 snd_chan_param *p = (snd_chan_param *)arg; 263 p->play_rate = wrch? wrch->speed : 0; 264 p->rec_rate = rdch? rdch->speed : 0; 265 p->play_format = wrch? wrch->format : 0; 266 p->rec_format = rdch? rdch->format : 0; 267 } 268 break; 269 270 case AIOGCAP: /* get capabilities */ 271 { 272 snd_capabilities *p = (snd_capabilities *)arg; 273 pcmchan_caps *pcaps = NULL, *rcaps = NULL; 274 if (rdch) rcaps = chn_getcaps(rdch); 275 if (wrch) pcaps = chn_getcaps(wrch); 276 p->rate_min = max(rcaps? rcaps->minspeed : 0, 277 pcaps? pcaps->minspeed : 0); 278 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000, 279 pcaps? pcaps->maxspeed : 1000000); 280 p->bufsize = min(rdch? rdch->buffer.bufsize : 1000000, 281 wrch? wrch->buffer.bufsize : 1000000); 282 /* XXX bad on sb16 */ 283 p->formats = (rcaps? rcaps->formats : 0xffffffff) & 284 (pcaps? pcaps->formats : 0xffffffff); 285 p->mixers = 1; /* default: one mixer */ 286 p->inputs = d->mixer.devs; 287 p->left = p->right = 100; 288 } 289 break; 290 291 case AIOSTOP: 292 if (*arg_i == AIOSYNC_PLAY && wrch) *arg_i = chn_abort(wrch); 293 else if (*arg_i == AIOSYNC_CAPTURE && rdch) *arg_i = chn_abort(rdch); 294 else { 295 splx(s); 296 printf("AIOSTOP: bad channel 0x%x\n", *arg_i); 297 *arg_i = 0; 298 } 299 break; 300 301 case AIOSYNC: 302 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n", 303 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos); 304 break; 305 /* 306 * here follow the standard ioctls (filio.h etc.) 307 */ 308 case FIONREAD: /* get # bytes to read */ 309 if (rdch && rdch->buffer.dl) chn_dmaupdate(rdch); 310 *arg_i = rdch? rdch->buffer.rl : 0; 311 break; 312 313 case FIOASYNC: /*set/clear async i/o */ 314 DEB( printf("FIOASYNC\n") ; ) 315 break; 316 317 case SNDCTL_DSP_NONBLOCK: 318 case FIONBIO: /* set/clear non-blocking i/o */ 319 if (rdch) rdch->flags &= ~CHN_F_NBIO; 320 if (wrch) wrch->flags &= ~CHN_F_NBIO; 321 if (*arg_i) { 322 if (rdch) rdch->flags |= CHN_F_NBIO; 323 if (wrch) wrch->flags |= CHN_F_NBIO; 324 } 325 break; 326 327 /* 328 * Finally, here is the linux-compatible ioctl interface 329 */ 330 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int) 331 case THE_REAL_SNDCTL_DSP_GETBLKSIZE: 332 case SNDCTL_DSP_GETBLKSIZE: 333 *arg_i = wrch? wrch->blocksize : 0; /* XXX rdch? */ 334 break ; 335 336 case SNDCTL_DSP_SETBLKSIZE: 337 splx(s); 338 if (wrch) chn_setblocksize(wrch, *arg_i); 339 if (rdch) chn_setblocksize(rdch, *arg_i); 340 break; 341 342 case SNDCTL_DSP_RESET: 343 DEB(printf("dsp reset\n")); 344 if (wrch) chn_abort(wrch); 345 if (rdch) chn_abort(rdch); 346 break; 347 348 case SNDCTL_DSP_SYNC: 349 DEB(printf("dsp sync\n")); 350 splx(s); 351 if (wrch) chn_sync(wrch, wrch->buffer.bufsize - 4); 352 break; 353 354 case SNDCTL_DSP_SPEED: 355 splx(s); 356 if (wrch) chn_setspeed(wrch, *arg_i); 357 if (rdch) chn_setspeed(rdch, *arg_i); 358 /* fallthru */ 359 360 case SOUND_PCM_READ_RATE: 361 *arg_i = wrch? wrch->speed : rdch->speed; 362 break; 363 364 case SNDCTL_DSP_STEREO: 365 splx(s); 366 if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | 367 ((*arg_i)? AFMT_STEREO : 0)); 368 if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | 369 ((*arg_i)? AFMT_STEREO : 0)); 370 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 1 : 0; 371 break; 372 373 case SOUND_PCM_WRITE_CHANNELS: 374 splx(s); 375 if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | 376 ((*arg_i == 2)? AFMT_STEREO : 0)); 377 if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | 378 ((*arg_i == 2)? AFMT_STEREO : 0)); 379 /* fallthru */ 380 381 case SOUND_PCM_READ_CHANNELS: 382 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1; 383 break; 384 385 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */ 386 *arg_i = wrch? chn_getcaps(wrch)->formats : chn_getcaps(rdch)->formats; 387 break ; 388 389 case SNDCTL_DSP_SETFMT: /* sets _one_ format */ 390 splx(s); 391 if (wrch) chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO)); 392 if (rdch) chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO)); 393 *arg_i = (wrch? wrch->format: rdch->format) & ~AFMT_STEREO; 394 break; 395 396 case SNDCTL_DSP_SUBDIVIDE: 397 /* XXX watch out, this is RW! */ 398 DEB(printf("SNDCTL_DSP_SUBDIVIDE unimplemented\n");) 399 break; 400 401 case SNDCTL_DSP_SETFRAGMENT: 402 /* XXX watch out, this is RW! */ 403 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg)); 404 { 405 int bytes = 1 << min(*arg_i & 0xffff, 16); 406 int count = (*arg_i >> 16) & 0xffff; 407 pcm_channel *c = wrch? wrch : rdch; 408 splx(s); 409 if (rdch) chn_setblocksize(rdch, bytes); 410 if (wrch) chn_setblocksize(wrch, bytes); 411 412 /* eg: 4dwave can only interrupt at buffer midpoint, so 413 * it will force blocksize == bufsize/2 414 */ 415 count = c->buffer.bufsize / c->blocksize; 416 bytes = ffs(c->blocksize) - 1; 417 *arg_i = (count << 16) | bytes; 418 } 419 break; 420 421 case SNDCTL_DSP_GETISPACE: 422 /* return space available in the input queue */ 423 { 424 audio_buf_info *a = (audio_buf_info *)arg; 425 if (rdch) { 426 snd_dbuf *b = &rdch->buffer; 427 if (b->dl) chn_dmaupdate(rdch); 428 a->bytes = b->fl; 429 a->fragments = 1; 430 a->fragstotal = b->bufsize / rdch->blocksize; 431 a->fragsize = rdch->blocksize; 432 } 433 } 434 break; 435 436 case SNDCTL_DSP_GETOSPACE: 437 /* return space available in the output queue */ 438 { 439 audio_buf_info *a = (audio_buf_info *)arg; 440 if (wrch) { 441 snd_dbuf *b = &wrch->buffer; 442 if (b->dl) chn_dmaupdate(wrch); 443 a->bytes = b->fl; 444 a->fragments = 1; 445 a->fragstotal = b->bufsize / wrch->blocksize; 446 a->fragsize = wrch->blocksize; 447 } 448 } 449 break; 450 451 case SNDCTL_DSP_GETIPTR: 452 { 453 count_info *a = (count_info *)arg; 454 if (rdch) { 455 snd_dbuf *b = &rdch->buffer; 456 if (b->dl) chn_dmaupdate(rdch); 457 a->bytes = b->total; 458 a->blocks = (b->total - b->prev_total) / rdch->blocksize; 459 a->ptr = b->fp; 460 b->prev_total += a->blocks * rdch->blocksize; 461 } else ret = EINVAL; 462 } 463 break; 464 465 case SNDCTL_DSP_GETOPTR: 466 { 467 count_info *a = (count_info *)arg; 468 if (wrch) { 469 snd_dbuf *b = &wrch->buffer; 470 if (b->dl) chn_dmaupdate(wrch); 471 a->bytes = b->total; 472 a->blocks = (b->total - b->prev_total) / wrch->blocksize; 473 a->ptr = b->rp; 474 b->prev_total += a->blocks * wrch->blocksize; 475 } else ret = EINVAL; 476 } 477 break; 478 479 case SNDCTL_DSP_GETCAPS: 480 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER; 481 if (rdch && wrch && !(d->flags & SD_F_SIMPLEX)) 482 *arg_i |= DSP_CAP_DUPLEX; 483 break; 484 485 case SOUND_PCM_READ_BITS: 486 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8; 487 break; 488 489 case SNDCTL_DSP_SETTRIGGER: 490 if (rdch) { 491 rdch->flags &= ~CHN_F_TRIGGERED; 492 if (*arg_i & PCM_ENABLE_INPUT) 493 rdch->flags |= CHN_F_TRIGGERED; 494 chn_intr(rdch); 495 } 496 if (wrch) { 497 wrch->flags &= ~CHN_F_TRIGGERED; 498 if (*arg_i & PCM_ENABLE_OUTPUT) 499 wrch->flags |= CHN_F_TRIGGERED; 500 chn_intr(wrch); 501 } 502 break; 503 504 case SNDCTL_DSP_GETTRIGGER: 505 *arg_i = 0; 506 if (wrch && wrch->flags & CHN_F_TRIGGERED) 507 *arg_i |= PCM_ENABLE_OUTPUT; 508 if (rdch && rdch->flags & CHN_F_TRIGGERED) 509 *arg_i |= PCM_ENABLE_INPUT; 510 break; 511 512 case SNDCTL_DSP_GETODELAY: 513 if (wrch) { 514 snd_dbuf *b = &wrch->buffer; 515 if (b->dl) 516 chn_dmaupdate(wrch); 517 *arg = b->total; 518 } else 519 ret = EINVAL; 520 break; 521 522 case SNDCTL_DSP_MAPINBUF: 523 case SNDCTL_DSP_MAPOUTBUF: 524 case SNDCTL_DSP_SETSYNCRO: 525 /* undocumented */ 526 527 case SNDCTL_DSP_POST: 528 case SOUND_PCM_WRITE_FILTER: 529 case SOUND_PCM_READ_FILTER: 530 /* dunno what these do, don't sound important */ 531 default: 532 DEB(printf("default ioctl snd%d fn 0x%08x fail\n", unit, cmd)); 533 ret = EINVAL; 534 break; 535 } 536 splx(s); 537 return ret; 538 } 539 540 int 541 dsp_poll(snddev_info *d, int chan, int events, struct proc *p) 542 { 543 int ret = 0, e; 544 pcm_channel *wrch = NULL, *rdch = NULL; 545 546 getchns(d, chan, &rdch, &wrch); 547 e = events & (POLLOUT | POLLWRNORM); 548 if (wrch && e) ret |= chn_poll(wrch, e, p); 549 e = events & (POLLIN | POLLRDNORM); 550 if (rdch && e) ret |= chn_poll(rdch, e, p); 551 return ret; 552 } 553 554 int 555 dsp_mmap(snddev_info *d, int chan, vm_offset_t offset, int nprot) 556 { 557 pcm_channel *wrch = NULL, *rdch = NULL, *c = NULL; 558 559 getchns(d, chan, &rdch, &wrch); 560 /* XXX this is broken by line 204 of vm/device_pager.c, so force write buffer */ 561 if (1 || (wrch && (nprot & PROT_WRITE))) c = wrch; 562 else if (rdch && (nprot & PROT_READ)) c = rdch; 563 if (c) { 564 c->flags |= CHN_F_MAPPED; 565 return atop(vtophys(c->buffer.buf + offset)); 566 } 567 return -1; 568 } 569 570