1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Sun audio(7I) and mixer(7I) personality. 28 * 29 * There are some "undocumented" details of how legacy Sun audio 30 * interfaces work. The following "rules" were derived from reading the 31 * legacy Sun mixer code, and to the best of our knowledge are not 32 * documented elsewhere. 33 * 34 * - We create a "fake" audio device, which behaves like a classic 35 * exclusive audio device, for each PID, as determined during open(2). 36 * 37 * - Different processes don't interfere with each other. Even though 38 * they are running concurrently, they each think they have exclusive 39 * control over the audio device. 40 * 41 * - Read and write directions operate independent of each other. That 42 * is, a device open for reading won't intefere with a future open for 43 * writing, and vice versa. This is true even within the same process. 44 * 45 * - Because the virtualization is by PID, strange behavior may occur 46 * if a process tries to open an audio device at the same time it 47 * has already received a file descriptor from another process (such 48 * through inheritence via fork()). 49 * 50 * - The "fake" audio device has no control over physical settings. 51 * It sees only the software attenuation-based volumes for play and 52 * record, and has no support for alternate input or output ports or 53 * access to the monitoring features of the hardware. 54 * 55 * - Explicit notificaton signals (SIGPOLL) are only ever sent up the 56 * audioctl node -- never up a regular audio node. (The stream head 57 * may still issue SIGPOLL based on readability/writability of 58 * course.) 59 * 60 * - Corollary: processes that want asynch. notifications will open 61 * /dev/audioctl as well as /dev/audio. 62 * 63 * - We don't support the MIXER mode at all. 64 * 65 * - By corollary, a process is only allowed to open /dev/audio once 66 * (in each direction.) 67 * 68 * - Attempts to open /dev/audio in duplex mode (O_RDWR) fail (EBUSY) 69 * if the device cannot support duplex operation. 70 * 71 * - Attempts to open a device with FREAD set fail if the device is not 72 * capable of recording. (Likewise for FWRITE and playback.) 73 * 74 * - No data transfer is permitted for audioctl nodes. (No actual 75 * record or play.) 76 * 77 * - Sun audio does not support any formats other than linear and 78 * ULAW/ALAW. I.e. it will never support AC3 or other "opaque" 79 * streams which require special handling. 80 * 81 * - Sun audio only supports stereo or monophonic data streams. 82 */ 83 84 #include <sys/types.h> 85 #include <sys/open.h> 86 #include <sys/errno.h> 87 #include <sys/audio.h> 88 #include <sys/mixer.h> 89 #include <sys/file.h> 90 #include <sys/stropts.h> 91 #include <sys/strsun.h> 92 #include <sys/sysmacros.h> 93 #include <sys/list.h> 94 #include <sys/note.h> 95 #include <sys/stat.h> 96 #include <sys/ddi.h> 97 #include <sys/sunddi.h> 98 #include "audio_client.h" 99 100 typedef struct daclient daclient_t; 101 typedef struct dadev dadev_t; 102 typedef struct daproc daproc_t; 103 104 /* common structure shared between both audioctl and audio nodes */ 105 struct daclient { 106 daproc_t *dc_proc; 107 dadev_t *dc_dev; 108 audio_client_t *dc_client; 109 queue_t *dc_wq; 110 unsigned dc_eof; 111 list_t dc_eofcnt; 112 kmutex_t dc_lock; 113 mblk_t *dc_draining; 114 }; 115 116 struct eofcnt { 117 list_node_t linkage; 118 uint64_t tail; 119 }; 120 121 struct dadev { 122 audio_dev_t *d_dev; 123 124 list_t d_procs; 125 kmutex_t d_mx; 126 kcondvar_t d_cv; 127 }; 128 129 struct daproc { 130 pid_t p_id; 131 struct audio_info p_info; 132 int p_refcnt; 133 int p_oflag; 134 list_node_t p_linkage; 135 dadev_t *p_dev; 136 audio_client_t *p_writer; 137 audio_client_t *p_reader; 138 }; 139 140 int devaudio_proc_hold(audio_client_t *, int); 141 void devaudio_proc_release(audio_client_t *); 142 static void devaudio_proc_update(daproc_t *); 143 144 145 static int 146 devaudio_compose_format(audio_prinfo_t *prinfo) 147 { 148 switch (prinfo->precision) { 149 case 8: 150 switch (prinfo->encoding) { 151 case AUDIO_ENCODING_ULAW: 152 return (AUDIO_FORMAT_ULAW); 153 case AUDIO_ENCODING_ALAW: 154 return (AUDIO_FORMAT_ALAW); 155 case AUDIO_ENCODING_LINEAR8: 156 return (AUDIO_FORMAT_U8); 157 case AUDIO_ENCODING_LINEAR: 158 return (AUDIO_FORMAT_S8); 159 } 160 break; 161 case 16: 162 if (prinfo->encoding == AUDIO_ENCODING_LINEAR) 163 return (AUDIO_FORMAT_S16_NE); 164 break; 165 case 32: 166 if (prinfo->encoding == AUDIO_ENCODING_LINEAR) 167 return (AUDIO_FORMAT_S32_NE); 168 break; 169 } 170 return (AUDIO_FORMAT_NONE); 171 172 } 173 174 static void 175 devaudio_decompose_format(audio_prinfo_t *prinfo, int afmt) 176 { 177 int e, p; 178 179 /* 180 * N.B.: Even though some of the formats below can't be set by 181 * this personality, reporting them (using the closest match) 182 * allows this personality to roughly approximate settings for 183 * other streams. It would be incredibly poor form for any 184 * personality to modify the format settings for a different 185 * personality, so we don't worry about that case. 186 */ 187 188 switch (afmt) { 189 case AUDIO_FORMAT_ULAW: 190 e = AUDIO_ENCODING_ULAW; 191 p = 8; 192 break; 193 194 case AUDIO_FORMAT_ALAW: 195 e = AUDIO_ENCODING_ALAW; 196 p = 8; 197 break; 198 199 case AUDIO_FORMAT_U8: 200 e = AUDIO_ENCODING_LINEAR8; 201 p = 8; 202 break; 203 204 case AUDIO_FORMAT_S8: 205 e = AUDIO_ENCODING_LINEAR; 206 p = 8; 207 break; 208 209 case AUDIO_FORMAT_S16_NE: 210 case AUDIO_FORMAT_S16_OE: 211 case AUDIO_FORMAT_U16_NE: 212 case AUDIO_FORMAT_U16_OE: 213 e = AUDIO_ENCODING_LINEAR; 214 p = 16; 215 break; 216 217 case AUDIO_FORMAT_S24_NE: 218 case AUDIO_FORMAT_S24_OE: 219 case AUDIO_FORMAT_S24_PACKED: 220 e = AUDIO_ENCODING_LINEAR; 221 p = 24; 222 break; 223 224 case AUDIO_FORMAT_S32_NE: 225 case AUDIO_FORMAT_S32_OE: 226 e = AUDIO_ENCODING_LINEAR; 227 p = 32; 228 break; 229 230 default: 231 /* all other formats (e.g. AC3) are uninterpreted */ 232 e = AUDIO_ENCODING_NONE; 233 p = 32; 234 break; 235 } 236 237 prinfo->encoding = e; 238 prinfo->precision = p; 239 } 240 241 static daproc_t * 242 devaudio_proc_alloc(audio_client_t *c) 243 { 244 audio_info_t *info; 245 audio_prinfo_t *prinfo; 246 uint32_t caps; 247 daproc_t *proc; 248 249 if ((proc = kmem_zalloc(sizeof (*proc), KM_NOSLEEP)) == NULL) { 250 return (NULL); 251 } 252 info = &proc->p_info; 253 254 /* 255 * audio(7I) says: Upon the initial open() of the audio 256 * device, the driver resets the data format of the device to 257 * the default state of 8-bit, 8Khz, mono u-Law data. 258 */ 259 prinfo = &info->play; 260 prinfo->channels = 1; 261 prinfo->sample_rate = 8000; 262 prinfo->encoding = AUDIO_ENCODING_ULAW; 263 prinfo->precision = 8; 264 prinfo->gain = AUDIO_MAX_GAIN; 265 prinfo->balance = AUDIO_MID_BALANCE; 266 prinfo->buffer_size = 8192; 267 prinfo->pause = B_FALSE; 268 prinfo->waiting = B_FALSE; 269 prinfo->open = B_FALSE; 270 prinfo->active = B_FALSE; 271 prinfo->samples = 0; 272 prinfo->eof = 0; 273 prinfo->error = 0; 274 prinfo->minordev = 0; 275 prinfo->port = AUDIO_SPEAKER; 276 prinfo->avail_ports = AUDIO_SPEAKER; 277 prinfo->mod_ports = AUDIO_NONE; 278 prinfo->_xxx = 0; 279 280 prinfo = &info->record; 281 prinfo->channels = 1; 282 prinfo->sample_rate = 8000; 283 prinfo->encoding = AUDIO_ENCODING_ULAW; 284 prinfo->precision = 8; 285 prinfo->gain = AUDIO_MAX_GAIN; 286 prinfo->balance = AUDIO_MID_BALANCE; 287 prinfo->buffer_size = 8192; 288 prinfo->waiting = B_FALSE; 289 prinfo->open = B_FALSE; 290 prinfo->active = B_FALSE; 291 prinfo->samples = 0; 292 prinfo->eof = 0; 293 prinfo->error = 0; 294 prinfo->minordev = 0; 295 prinfo->port = AUDIO_MICROPHONE; 296 prinfo->avail_ports = AUDIO_MICROPHONE; 297 prinfo->mod_ports = AUDIO_MICROPHONE; 298 299 info->output_muted = B_FALSE; 300 /* pretend we don't have a software mixer - we don't support the API */ 301 info->hw_features = 0; 302 info->sw_features = 0; 303 info->sw_features_enabled = 0; 304 305 caps = auclnt_get_dev_capab(auclnt_get_dev(c)); 306 if (caps & AUDIO_CLIENT_CAP_PLAY) 307 info->hw_features |= AUDIO_HWFEATURE_PLAY; 308 if (caps & AUDIO_CLIENT_CAP_RECORD) 309 info->hw_features |= AUDIO_HWFEATURE_RECORD; 310 if (caps & AUDIO_CLIENT_CAP_DUPLEX) 311 info->hw_features |= AUDIO_HWFEATURE_DUPLEX; 312 313 return (proc); 314 } 315 316 static void 317 devaudio_proc_free(daproc_t *proc) 318 { 319 kmem_free(proc, sizeof (*proc)); 320 } 321 322 int 323 devaudio_proc_hold(audio_client_t *c, int oflag) 324 { 325 pid_t pid; 326 daproc_t *proc; 327 dadev_t *dev; 328 daclient_t *dc; 329 list_t *l; 330 audio_dev_t *adev; 331 int rv; 332 333 adev = auclnt_get_dev(c); 334 335 /* first allocate and initialize the daclient private data */ 336 if ((dc = kmem_zalloc(sizeof (*dc), KM_NOSLEEP)) == NULL) { 337 return (ENOMEM); 338 } 339 340 mutex_init(&dc->dc_lock, NULL, MUTEX_DRIVER, NULL); 341 list_create(&dc->dc_eofcnt, sizeof (struct eofcnt), 342 offsetof(struct eofcnt, linkage)); 343 auclnt_set_private(c, dc); 344 345 dev = auclnt_get_dev_minor_data(adev, AUDIO_MINOR_DEVAUDIO); 346 l = &dev->d_procs; 347 pid = auclnt_get_pid(c); 348 349 /* set a couple of common fields */ 350 dc->dc_client = c; 351 dc->dc_dev = dev; 352 353 mutex_enter(&dev->d_mx); 354 for (proc = list_head(l); proc != NULL; proc = list_next(l, proc)) { 355 if (proc->p_id == pid) { 356 proc->p_refcnt++; 357 break; 358 } 359 } 360 if (proc == NULL) { 361 if ((proc = devaudio_proc_alloc(c)) == NULL) { 362 rv = ENOMEM; 363 goto failed; 364 } 365 proc->p_refcnt = 1; 366 proc->p_id = pid; 367 proc->p_dev = dev; 368 list_insert_tail(l, proc); 369 } 370 371 while (proc->p_oflag & oflag) { 372 373 if (oflag & (FNDELAY|FNONBLOCK)) { 374 rv = EBUSY; 375 goto failed; 376 } 377 if (oflag & FWRITE) 378 proc->p_info.play.waiting++; 379 if (oflag & FREAD) 380 proc->p_info.record.waiting++; 381 if (cv_wait_sig(&dev->d_cv, &dev->d_mx) == 0) { 382 /* interrupted! */ 383 if (oflag & FWRITE) 384 proc->p_info.play.waiting--; 385 if (oflag & FREAD) 386 proc->p_info.record.waiting--; 387 rv = EINTR; 388 goto failed; 389 } 390 if (oflag & FWRITE) 391 proc->p_info.play.waiting--; 392 if (oflag & FREAD) 393 proc->p_info.record.waiting--; 394 } 395 396 if (oflag & FWRITE) { 397 audio_prinfo_t *play = &proc->p_info.play; 398 audio_stream_t *sp = auclnt_output_stream(c); 399 400 if (((rv = auclnt_set_rate(sp, 8000)) != 0) || 401 ((rv = auclnt_set_format(sp, AUDIO_FORMAT_ULAW)) != 0) || 402 ((rv = auclnt_set_channels(sp, 1)) != 0)) { 403 goto failed; 404 } 405 406 auclnt_set_samples(sp, 0); 407 auclnt_set_errors(sp, 0); 408 play->eof = 0; 409 play->buffer_size = 8192; 410 411 auclnt_set_gain(sp, ((play->gain * 100) / AUDIO_MAX_GAIN)); 412 auclnt_set_muted(sp, proc->p_info.output_muted); 413 play->open = B_TRUE; 414 proc->p_writer = c; 415 proc->p_oflag |= FWRITE; 416 } 417 418 if (oflag & FREAD) { 419 audio_prinfo_t *rec = &proc->p_info.record; 420 audio_stream_t *sp = auclnt_input_stream(c); 421 422 if (((rv = auclnt_set_rate(sp, 8000)) != 0) || 423 ((rv = auclnt_set_format(sp, AUDIO_FORMAT_ULAW)) != 0) || 424 ((rv = auclnt_set_channels(sp, 1)) != 0)) { 425 goto failed; 426 } 427 428 auclnt_set_samples(sp, 0); 429 auclnt_set_errors(sp, 0); 430 rec->eof = 0; 431 rec->buffer_size = 8192; 432 433 auclnt_set_gain(sp, ((rec->gain * 100) / AUDIO_MAX_GAIN)); 434 rec->open = B_TRUE; 435 proc->p_reader = c; 436 proc->p_oflag |= FREAD; 437 } 438 439 440 dc->dc_wq = auclnt_get_wq(c); 441 442 /* we update the s_proc last to avoid a race */ 443 dc->dc_proc = proc; 444 445 devaudio_proc_update(proc); 446 447 mutex_exit(&dev->d_mx); 448 449 return (0); 450 451 failed: 452 mutex_exit(&dev->d_mx); 453 devaudio_proc_release(c); 454 return (rv); 455 456 } 457 458 static void 459 devaudio_clear_eof(audio_client_t *c) 460 { 461 struct eofcnt *eof; 462 daclient_t *dc; 463 464 dc = auclnt_get_private(c); 465 mutex_enter(&dc->dc_lock); 466 while ((eof = list_remove_head(&dc->dc_eofcnt)) != NULL) { 467 kmem_free(eof, sizeof (*eof)); 468 } 469 mutex_exit(&dc->dc_lock); 470 } 471 472 void 473 devaudio_proc_release(audio_client_t *c) 474 { 475 daproc_t *proc; 476 dadev_t *dev; 477 mblk_t *mp; 478 daclient_t *dc; 479 480 dc = auclnt_get_private(c); 481 proc = dc->dc_proc; 482 dev = dc->dc_dev; 483 dc->dc_proc = NULL; 484 485 mutex_enter(&dev->d_mx); 486 487 if (proc != NULL) { 488 proc->p_refcnt--; 489 ASSERT(proc->p_refcnt >= 0); 490 491 if (c == proc->p_writer) { 492 proc->p_oflag &= ~FWRITE; 493 proc->p_writer = NULL; 494 } 495 if (c == proc->p_reader) { 496 proc->p_oflag &= ~FREAD; 497 proc->p_reader = NULL; 498 } 499 cv_broadcast(&dev->d_cv); 500 501 if (proc->p_refcnt == 0) { 502 list_remove(&dev->d_procs, proc); 503 devaudio_proc_free(proc); 504 } 505 dc->dc_proc = NULL; 506 } 507 508 mutex_exit(&dev->d_mx); 509 510 devaudio_clear_eof(c); 511 512 while ((mp = dc->dc_draining) != NULL) { 513 dc->dc_draining = mp->b_next; 514 mp->b_next = NULL; 515 freemsg(mp); 516 } 517 518 mutex_destroy(&dc->dc_lock); 519 list_destroy(&dc->dc_eofcnt); 520 kmem_free(dc, sizeof (*dc)); 521 } 522 523 static void 524 devaudio_input(audio_client_t *c) 525 { 526 audio_stream_t *sp = auclnt_input_stream(c); 527 daclient_t *dc = auclnt_get_private(c); 528 unsigned framesz = auclnt_get_framesz(sp); 529 queue_t *rq = auclnt_get_rq(c); 530 mblk_t *mp; 531 unsigned nbytes = dc->dc_proc->p_info.record.buffer_size; 532 unsigned count = nbytes / framesz; 533 534 /* 535 * Potentially send a message upstream with the record data. 536 * We collect this up in chunks of the buffer size requested 537 * by the client. 538 */ 539 540 while (auclnt_get_count(sp) >= count) { 541 542 if ((!canputnext(rq)) || 543 ((mp = allocb(nbytes, BPRI_MED)) == NULL)) { 544 /* 545 * This will apply back pressure to the 546 * buffer. We haven't yet lost any data, we 547 * just can't send it up. The point at which 548 * we have an unrecoverable overrun is in the 549 * buffer, not in the streams queue. So, no 550 * need to do anything right now. 551 * 552 * Note that since recording is enabled, we 553 * expect that the callback routine will be 554 * called repeatedly & regularly, so we don't 555 * have to worry about leaving data orphaned 556 * in the queue. 557 */ 558 break; 559 } 560 561 (void) auclnt_consume_data(sp, (caddr_t)mp->b_wptr, count); 562 mp->b_wptr += nbytes; 563 putnext(rq, mp); 564 } 565 } 566 567 static void 568 devaudio_proc_update(daproc_t *proc) 569 { 570 audio_info_t *info; 571 audio_stream_t *sp; 572 audio_client_t *c; 573 574 info = &proc->p_info; 575 576 ASSERT(mutex_owned(&proc->p_dev->d_mx)); 577 578 if ((c = proc->p_writer) != NULL) { 579 sp = auclnt_output_stream(c); 580 581 info->play.sample_rate = auclnt_get_rate(sp); 582 info->play.channels = auclnt_get_channels(sp); 583 devaudio_decompose_format(&info->play, auclnt_get_format(sp)); 584 585 info->play.gain = 586 (auclnt_get_gain(sp) * AUDIO_MAX_GAIN) / 100; 587 info->play.pause = auclnt_is_paused(sp); 588 info->play.active = !info->play.pause; 589 info->play.samples = auclnt_get_samples(sp); 590 info->play.error = auclnt_get_errors(sp) ? B_TRUE : B_FALSE; 591 info->output_muted = auclnt_get_muted(sp); 592 } 593 594 if ((c = proc->p_reader) != NULL) { 595 sp = auclnt_input_stream(c); 596 597 info->record.sample_rate = auclnt_get_rate(sp); 598 info->record.channels = auclnt_get_channels(sp); 599 devaudio_decompose_format(&info->record, auclnt_get_format(sp)); 600 601 info->record.gain = 602 (auclnt_get_gain(sp) * AUDIO_MAX_GAIN) / 100; 603 info->record.pause = auclnt_is_paused(sp); 604 info->record.active = !info->record.pause; 605 info->record.samples = auclnt_get_samples(sp); 606 info->record.error = auclnt_get_errors(sp) ? B_TRUE : B_FALSE; 607 } 608 } 609 610 static void 611 devaudio_ioc_getinfo(queue_t *wq, audio_client_t *c, mblk_t *mp) 612 { 613 daclient_t *dc = auclnt_get_private(c); 614 daproc_t *proc = dc->dc_proc; 615 mblk_t *bcont; 616 617 if ((bcont = allocb(sizeof (audio_info_t), BPRI_MED)) == NULL) { 618 miocnak(wq, mp, 0, ENOMEM); 619 return; 620 } 621 622 mutex_enter(&dc->dc_dev->d_mx); 623 devaudio_proc_update(proc); 624 bcopy(&proc->p_info, bcont->b_wptr, sizeof (audio_info_t)); 625 mutex_exit(&dc->dc_dev->d_mx); 626 627 bcont->b_wptr += sizeof (audio_info_t); 628 629 mcopyout(mp, NULL, sizeof (audio_info_t), NULL, bcont); 630 qreply(wq, mp); 631 } 632 633 #define CHANGED(new, old, field) \ 634 ((new->field != ((uint32_t)~0)) && (new->field != old->field)) 635 #define CHANGED8(new, old, field) \ 636 ((new->field != ((uint8_t)~0)) && (new->field != old->field)) 637 638 static void 639 devaudio_ioc_setinfo(queue_t *wq, audio_client_t *c, mblk_t *mp) 640 { 641 daclient_t *dc; 642 daproc_t *proc; 643 audio_info_t *oinfo; 644 audio_info_t *ninfo; 645 audio_prinfo_t *npr; 646 audio_prinfo_t *opr; 647 648 int pfmt = AUDIO_FORMAT_NONE; 649 int rfmt = AUDIO_FORMAT_NONE; 650 651 boolean_t reader; 652 boolean_t writer; 653 boolean_t isctl; 654 audio_stream_t *sp; 655 int rv; 656 caddr_t uaddr; 657 mblk_t *bcont; 658 659 struct copyresp *csp; 660 661 if (DB_TYPE(mp) == M_IOCTL) { 662 /* the special value "1" indicates that this is a copyin */ 663 uaddr = *(caddr_t *)(void *)mp->b_cont->b_rptr; 664 665 mcopyin(mp, uaddr, sizeof (audio_info_t), NULL); 666 qreply(wq, mp); 667 return; 668 } 669 670 ASSERT(DB_TYPE(mp) == M_IOCDATA); 671 if (((bcont = mp->b_cont) == NULL) || 672 (MBLKL(mp->b_cont) != sizeof (audio_info_t))) { 673 miocnak(wq, mp, 0, EINVAL); 674 return; 675 } 676 677 mp->b_cont = NULL; 678 csp = (void *)mp->b_rptr; 679 uaddr = (void *)csp->cp_private; 680 dc = auclnt_get_private(c); 681 ninfo = (void *)bcont->b_rptr; 682 683 mutex_enter(&dc->dc_dev->d_mx); 684 685 proc = dc->dc_proc; 686 oinfo = &proc->p_info; 687 688 if (auclnt_get_minor_type(c) == AUDIO_MINOR_DEVAUDIOCTL) { 689 /* control node can do both read and write fields */ 690 isctl = B_TRUE; 691 reader = B_TRUE; 692 writer = B_TRUE; 693 } else { 694 isctl = B_FALSE; 695 writer = (c == proc->p_writer); 696 reader = (c == proc->p_reader); 697 } 698 699 /* 700 * Start by validating settings. 701 */ 702 npr = &ninfo->play; 703 opr = &oinfo->play; 704 705 if (writer && CHANGED(npr, opr, sample_rate)) { 706 if ((isctl) || 707 (npr->sample_rate < 5500) || (npr->sample_rate > 48000)) { 708 rv = EINVAL; 709 goto err; 710 } 711 } 712 if (writer && CHANGED(npr, opr, channels)) { 713 if ((isctl) || (npr->channels < 1) || (npr->channels > 2)) { 714 rv = EINVAL; 715 goto err; 716 } 717 } 718 if (writer && 719 (CHANGED(npr, opr, encoding) || CHANGED(npr, opr, precision))) { 720 if (npr->encoding == (uint32_t)~0) 721 npr->encoding = opr->encoding; 722 if (npr->precision == (uint32_t)~0) 723 npr->precision = opr->precision; 724 pfmt = devaudio_compose_format(npr); 725 if ((isctl) || (pfmt == AUDIO_FORMAT_NONE)) { 726 rv = EINVAL; 727 goto err; 728 } 729 } 730 731 /* play fields that anyone can modify */ 732 if (CHANGED(npr, opr, gain)) { 733 if (npr->gain > AUDIO_MAX_GAIN) { 734 rv = EINVAL; 735 goto err; 736 } 737 } 738 739 740 npr = &ninfo->record; 741 opr = &oinfo->record; 742 743 if (reader && CHANGED(npr, opr, sample_rate)) { 744 if ((isctl) || 745 (npr->sample_rate < 5500) || (npr->sample_rate > 48000)) { 746 rv = EINVAL; 747 goto err; 748 } 749 } 750 if (reader && CHANGED(npr, opr, channels)) { 751 if ((isctl) || (npr->channels < 1) || (npr->channels > 2)) { 752 rv = EINVAL; 753 goto err; 754 } 755 } 756 if (reader && 757 (CHANGED(npr, opr, encoding) || CHANGED(npr, opr, precision))) { 758 if (npr->encoding == (uint32_t)~0) 759 npr->encoding = opr->encoding; 760 if (npr->precision == (uint32_t)~0) 761 npr->precision = opr->precision; 762 rfmt = devaudio_compose_format(npr); 763 if ((isctl) || (rfmt == AUDIO_FORMAT_NONE)) { 764 rv = EINVAL; 765 goto err; 766 } 767 } 768 if (reader && CHANGED(npr, opr, buffer_size)) { 769 if (isctl) { 770 rv = EINVAL; 771 goto err; 772 } 773 /* make sure we can support 16-bit stereo samples */ 774 if ((npr->buffer_size % 4) != 0) { 775 npr->buffer_size = (npr->buffer_size + 3) & ~3; 776 } 777 /* limit the maximum buffer size somewhat */ 778 if (npr->buffer_size > 16384) { 779 npr->buffer_size = 16384; 780 } 781 } 782 783 /* record fields that anyone can modify */ 784 if (CHANGED(npr, opr, gain)) { 785 if (npr->gain > AUDIO_MAX_GAIN) { 786 rv = EINVAL; 787 goto err; 788 } 789 } 790 791 /* 792 * Now apply the changes. 793 */ 794 if (proc->p_writer != NULL) { 795 sp = auclnt_output_stream(proc->p_writer); 796 npr = &ninfo->play; 797 opr = &oinfo->play; 798 799 if (CHANGED(npr, opr, sample_rate)) { 800 if ((rv = auclnt_set_rate(sp, npr->sample_rate)) != 0) 801 goto err; 802 } 803 if (CHANGED(npr, opr, channels)) { 804 if ((rv = auclnt_set_channels(sp, npr->channels)) != 0) 805 goto err; 806 } 807 if (pfmt != AUDIO_FORMAT_NONE) { 808 if ((rv = auclnt_set_format(sp, pfmt)) != 0) 809 goto err; 810 } 811 if (CHANGED(npr, opr, samples)) { 812 auclnt_set_samples(sp, npr->samples); 813 } 814 if (CHANGED(npr, opr, eof)) { 815 /* 816 * This ugly special case code is required to 817 * prevent problems with realaudio. 818 */ 819 if (npr->eof == 0) { 820 devaudio_clear_eof(proc->p_writer); 821 } 822 opr->eof = npr->eof; 823 } 824 if (CHANGED8(npr, opr, pause)) { 825 if (npr->pause) { 826 auclnt_set_paused(sp); 827 } else { 828 auclnt_clear_paused(sp); 829 830 /* qenable to start up the playback */ 831 qenable(auclnt_get_wq(proc->p_writer)); 832 } 833 } 834 if (CHANGED8(npr, opr, waiting) && (npr->waiting)) { 835 opr->waiting = npr->waiting; 836 } 837 if (CHANGED8(npr, opr, error)) { 838 auclnt_set_errors(sp, npr->error); 839 } 840 if (CHANGED(npr, opr, gain)) { 841 auclnt_set_gain(sp, (npr->gain * 100) / AUDIO_MAX_GAIN); 842 } 843 if (CHANGED8(ninfo, oinfo, output_muted)) { 844 auclnt_set_muted(sp, ninfo->output_muted); 845 } 846 if (CHANGED(npr, opr, buffer_size)) { 847 /* 848 * No checks on the buffer size are performed 849 * for play side. The value of the buffer size 850 * is meaningless for play side anyway. 851 */ 852 opr->buffer_size = npr->buffer_size; 853 } 854 } else { 855 /* these vaalues are preserved even if /dev/audio not open */ 856 if (CHANGED(npr, opr, gain)) { 857 opr->gain = npr->gain; 858 } 859 if (CHANGED8(ninfo, oinfo, output_muted)) { 860 oinfo->output_muted = ninfo->output_muted; 861 } 862 } 863 864 if (proc->p_reader != NULL) { 865 sp = auclnt_input_stream(proc->p_reader); 866 npr = &ninfo->record; 867 opr = &oinfo->record; 868 869 if (CHANGED(npr, opr, sample_rate)) { 870 if ((rv = auclnt_set_rate(sp, npr->sample_rate)) != 0) 871 goto err; 872 } 873 if (CHANGED(npr, opr, channels)) { 874 if ((rv = auclnt_set_channels(sp, npr->channels)) != 0) 875 goto err; 876 } 877 if (rfmt != AUDIO_FORMAT_NONE) { 878 if ((rv = auclnt_set_format(sp, rfmt)) != 0) 879 goto err; 880 } 881 if (CHANGED(npr, opr, samples)) { 882 auclnt_set_samples(sp, npr->samples); 883 } 884 if (CHANGED(npr, opr, eof)) { 885 opr->eof = npr->eof; 886 } 887 if (CHANGED8(npr, opr, pause)) { 888 if (npr->pause) { 889 auclnt_set_paused(sp); 890 } else { 891 auclnt_clear_paused(sp); 892 auclnt_start(sp); 893 } 894 } 895 if (CHANGED8(npr, opr, waiting) && (npr->waiting)) { 896 opr->waiting = npr->waiting; 897 } 898 if (CHANGED8(npr, opr, error)) { 899 auclnt_set_errors(sp, npr->error); 900 } 901 if (CHANGED(npr, opr, buffer_size)) { 902 opr->buffer_size = npr->buffer_size; 903 } 904 if (CHANGED(npr, opr, gain)) { 905 auclnt_set_gain(sp, (npr->gain * 100) / AUDIO_MAX_GAIN); 906 } 907 } else { 908 /* these values are preserved even if /dev/audio not open */ 909 if (CHANGED(npr, opr, gain)) { 910 opr->gain = npr->gain; 911 } 912 } 913 914 devaudio_proc_update(dc->dc_proc); 915 bcopy(&dc->dc_proc->p_info, ninfo, sizeof (*ninfo)); 916 917 mutex_exit(&dc->dc_dev->d_mx); 918 mcopyout(mp, NULL, sizeof (audio_info_t), uaddr, bcont); 919 qreply(wq, mp); 920 return; 921 922 err: 923 mutex_exit(&dc->dc_dev->d_mx); 924 miocnak(wq, mp, 0, rv); 925 } 926 927 static void 928 devaudio_ioc_getdev(queue_t *wq, audio_client_t *c, mblk_t *mp) 929 { 930 audio_dev_t *d = auclnt_get_dev(c); 931 mblk_t *bcont; 932 audio_device_t *a; 933 934 if ((bcont = allocb(sizeof (*a), BPRI_MED)) == NULL) { 935 miocnak(wq, mp, 0, ENOMEM); 936 return; 937 } 938 939 a = (void *)bcont->b_wptr; 940 (void) snprintf(a->name, sizeof (a->name), 941 "SUNW,%s", auclnt_get_dev_name(d)); 942 (void) strlcpy(a->config, 943 auclnt_get_dev_description(d), sizeof (a->config)); 944 (void) strlcpy(a->version, 945 auclnt_get_dev_version(d), sizeof (a->version)); 946 bcont->b_wptr += sizeof (*a); 947 948 mcopyout(mp, NULL, sizeof (*a), NULL, bcont); 949 qreply(wq, mp); 950 } 951 952 static int 953 devaudio_sigpoll(audio_client_t *c, void *arg) 954 { 955 daproc_t *proc = arg; 956 daclient_t *dc; 957 958 if (auclnt_get_minor_type(c) == AUDIO_MINOR_DEVAUDIOCTL) { 959 dc = auclnt_get_private(c); 960 /* we only need to notify peers in our own process */ 961 if ((dc != NULL) && (dc->dc_proc == proc)) { 962 (void) putnextctl1(auclnt_get_rq(c), M_PCSIG, SIGPOLL); 963 } 964 } 965 return (AUDIO_WALK_CONTINUE); 966 } 967 968 static void 969 devaudio_drain(audio_client_t *c) 970 { 971 daclient_t *dc = auclnt_get_private(c); 972 mblk_t *mplist, *mp; 973 974 mutex_enter(&dc->dc_lock); 975 mplist = dc->dc_draining; 976 dc->dc_draining = NULL; 977 mutex_exit(&dc->dc_lock); 978 979 while ((mp = mplist) != NULL) { 980 mplist = mp->b_next; 981 mp->b_next = NULL; 982 miocack(auclnt_get_wq(c), mp, 0, 0); 983 } 984 } 985 986 static void 987 devaudio_output(audio_client_t *c) 988 { 989 daclient_t *dc = auclnt_get_private(c); 990 daproc_t *proc = dc->dc_proc; 991 uint64_t tail; 992 struct eofcnt *eof; 993 int eofs = 0; 994 995 tail = auclnt_get_tail(auclnt_output_stream(c)); 996 997 /* get more data! (do this early) */ 998 qenable(auclnt_get_wq(c)); 999 1000 mutex_enter(&dc->dc_lock); 1001 while (((eof = list_head(&dc->dc_eofcnt)) != NULL) && 1002 (eof->tail < tail)) { 1003 list_remove(&dc->dc_eofcnt, eof); 1004 kmem_free(eof, sizeof (*eof)); 1005 eofs++; 1006 } 1007 proc->p_info.play.eof += eofs; 1008 mutex_exit(&dc->dc_lock); 1009 1010 if (eofs) { 1011 auclnt_dev_walk_clients(auclnt_get_dev(c), 1012 devaudio_sigpoll, proc); 1013 } 1014 } 1015 1016 static void * 1017 devaudio_init(audio_dev_t *adev) 1018 { 1019 dadev_t *dev; 1020 unsigned cap; 1021 1022 cap = auclnt_get_dev_capab(adev); 1023 /* if not a play or record device, don't bother initializing it */ 1024 if ((cap & (AUDIO_CLIENT_CAP_PLAY | AUDIO_CLIENT_CAP_RECORD)) == 0) { 1025 return (NULL); 1026 } 1027 1028 dev = kmem_zalloc(sizeof (*dev), KM_SLEEP); 1029 dev->d_dev = adev; 1030 mutex_init(&dev->d_mx, NULL, MUTEX_DRIVER, NULL); 1031 cv_init(&dev->d_cv, NULL, CV_DRIVER, NULL); 1032 list_create(&dev->d_procs, sizeof (struct daproc), 1033 offsetof(struct daproc, p_linkage)); 1034 1035 return (dev); 1036 } 1037 1038 static void 1039 devaudio_fini(void *arg) 1040 { 1041 dadev_t *dev = arg; 1042 1043 if (dev != NULL) { 1044 1045 mutex_destroy(&dev->d_mx); 1046 cv_destroy(&dev->d_cv); 1047 list_destroy(&dev->d_procs); 1048 kmem_free(dev, sizeof (*dev)); 1049 } 1050 } 1051 1052 static int 1053 devaudio_open(audio_client_t *c, int oflag) 1054 { 1055 int rv; 1056 1057 if ((rv = auclnt_open(c, AUDIO_FORMAT_PCM, oflag)) != 0) { 1058 return (rv); 1059 } 1060 1061 if ((rv = devaudio_proc_hold(c, oflag)) != 0) { 1062 auclnt_close(c); 1063 return (rv); 1064 } 1065 1066 /* start up the input */ 1067 if (oflag & FREAD) { 1068 auclnt_start(auclnt_input_stream(c)); 1069 } 1070 1071 return (0); 1072 } 1073 1074 static int 1075 devaudioctl_open(audio_client_t *c, int oflag) 1076 { 1077 int rv; 1078 1079 _NOTE(ARGUNUSED(oflag)); 1080 1081 oflag &= ~(FWRITE | FREAD); 1082 1083 if ((rv = auclnt_open(c, AUDIO_FORMAT_NONE, 0)) != 0) { 1084 return (rv); 1085 } 1086 1087 if ((rv = devaudio_proc_hold(c, oflag)) != 0) { 1088 auclnt_close(c); 1089 return (rv); 1090 } 1091 1092 return (0); 1093 } 1094 1095 static void 1096 devaudio_close(audio_client_t *c) 1097 { 1098 auclnt_stop(auclnt_output_stream(c)); 1099 auclnt_stop(auclnt_input_stream(c)); 1100 1101 auclnt_close(c); 1102 devaudio_proc_release(c); 1103 } 1104 1105 static void 1106 devaudioctl_close(audio_client_t *c) 1107 { 1108 auclnt_close(c); 1109 devaudio_proc_release(c); 1110 } 1111 1112 static void 1113 devaudio_miocdata(audio_client_t *c, mblk_t *mp) 1114 { 1115 struct copyresp *csp; 1116 queue_t *wq; 1117 1118 csp = (void *)mp->b_rptr; 1119 wq = auclnt_get_wq(c); 1120 1121 /* 1122 * If a transfer error occurred, the framework already 1123 * MIOCNAK'd it. 1124 */ 1125 if (csp->cp_rval != 0) { 1126 freemsg(mp); 1127 return; 1128 } 1129 1130 /* 1131 * If no state, then this is a response to M_COPYOUT, and we 1132 * are done. (Audio ioctls just copyout a single structure at 1133 * completion of work.) 1134 */ 1135 if (csp->cp_private == NULL) { 1136 miocack(wq, mp, 0, 0); 1137 return; 1138 } 1139 1140 /* now, call the handler ioctl */ 1141 switch (csp->cp_cmd) { 1142 case AUDIO_SETINFO: 1143 devaudio_ioc_setinfo(wq, c, mp); 1144 break; 1145 default: 1146 miocnak(wq, mp, 0, EINVAL); 1147 break; 1148 } 1149 } 1150 1151 static void 1152 devaudio_mioctl(audio_client_t *c, mblk_t *mp) 1153 { 1154 struct iocblk *iocp = (void *)mp->b_rptr; 1155 queue_t *wq = auclnt_get_wq(c); 1156 1157 /* BSD legacy here: we only support transparent ioctls */ 1158 if (iocp->ioc_count != TRANSPARENT) { 1159 miocnak(wq, mp, 0, EINVAL); 1160 return; 1161 } 1162 1163 switch (iocp->ioc_cmd) { 1164 case AUDIO_GETINFO: 1165 devaudio_ioc_getinfo(wq, c, mp); 1166 break; 1167 1168 case AUDIO_SETINFO: 1169 devaudio_ioc_setinfo(wq, c, mp); 1170 break; 1171 1172 case AUDIO_GETDEV: 1173 devaudio_ioc_getdev(wq, c, mp); 1174 break; 1175 1176 case AUDIO_DIAG_LOOPBACK: 1177 /* we don't support this one */ 1178 miocnak(wq, mp, 0, ENOTTY); 1179 break; 1180 1181 case AUDIO_MIXERCTL_GET_MODE: 1182 case AUDIO_MIXERCTL_SET_MODE: 1183 case AUDIO_MIXERCTL_GET_CHINFO: 1184 case AUDIO_MIXERCTL_SET_CHINFO: 1185 case AUDIO_MIXERCTL_GETINFO: 1186 case AUDIO_MIXERCTL_SETINFO: 1187 case AUDIO_GET_NUM_CHS: 1188 case AUDIO_GET_CH_NUMBER: 1189 case AUDIO_GET_CH_TYPE: 1190 case AUDIO_MIXER_SINGLE_OPEN: 1191 case AUDIO_MIXER_MULTIPLE_OPEN: 1192 case AUDIO_MIXER_GET_SAMPLE_RATES: 1193 default: 1194 miocnak(wq, mp, 0, EINVAL); 1195 break; 1196 } 1197 } 1198 1199 static void 1200 devaudioctl_wput(audio_client_t *c, mblk_t *mp) 1201 { 1202 queue_t *wq = auclnt_get_wq(c); 1203 1204 switch (DB_TYPE(mp)) { 1205 case M_IOCTL: 1206 /* Drain ioctl needs to be handled on the service queue */ 1207 devaudio_mioctl(c, mp); 1208 break; 1209 1210 case M_IOCDATA: 1211 devaudio_miocdata(c, mp); 1212 break; 1213 1214 case M_FLUSH: 1215 /* 1216 * We don't flush the engine. The reason is that 1217 * other streams might be using the engine. This is 1218 * fundamentally no different from the case where the 1219 * engine hardware has data buffered in an 1220 * inaccessible FIFO. 1221 * 1222 * Clients that want to ensure no more data is coming 1223 * should stop the stream before flushing. 1224 */ 1225 if (*mp->b_rptr & FLUSHW) { 1226 *mp->b_rptr &= ~FLUSHW; 1227 } 1228 if (*mp->b_rptr & FLUSHR) { 1229 qreply(wq, mp); 1230 } else { 1231 freemsg(mp); 1232 } 1233 break; 1234 1235 case M_DATA: 1236 /* 1237 * No audio data on control nodes! 1238 */ 1239 freemsg(mp); 1240 1241 default: 1242 freemsg(mp); 1243 break; 1244 } 1245 } 1246 1247 static void 1248 devaudio_wput(audio_client_t *c, mblk_t *mp) 1249 { 1250 queue_t *wq = auclnt_get_wq(c); 1251 1252 switch (DB_TYPE(mp)) { 1253 case M_IOCTL: 1254 /* Drain ioctl needs to be handled on the service queue */ 1255 if (*(int *)(void *)mp->b_rptr == AUDIO_DRAIN) { 1256 (void) putq(wq, mp); 1257 } else { 1258 devaudio_mioctl(c, mp); 1259 } 1260 break; 1261 1262 case M_IOCDATA: 1263 devaudio_miocdata(c, mp); 1264 break; 1265 1266 case M_FLUSH: 1267 /* 1268 * We don't flush the engine. The reason is that 1269 * other streams might be using the engine. This is 1270 * fundamentally no different from the case where the 1271 * engine hardware has data buffered in an 1272 * inaccessible FIFO. 1273 * 1274 * Clients that want to ensure no more data is coming 1275 * should stop the stream before flushing. 1276 */ 1277 if (*mp->b_rptr & FLUSHW) { 1278 flushq(wq, FLUSHALL); 1279 auclnt_flush(auclnt_output_stream(c)); 1280 *mp->b_rptr &= ~FLUSHW; 1281 } 1282 if (*mp->b_rptr & FLUSHR) { 1283 flushq(RD(wq), FLUSHALL); 1284 auclnt_flush(auclnt_input_stream(c)); 1285 qreply(wq, mp); 1286 } else { 1287 freemsg(mp); 1288 } 1289 break; 1290 1291 case M_DATA: 1292 /* 1293 * Defer processing to the queue. This keeps the data 1294 * ordered, and allows the wsrv routine to gather 1295 * multiple mblks at once. 1296 */ 1297 if (mp->b_cont != NULL) { 1298 1299 /* 1300 * If we need to pullup, do it here to 1301 * simplify the rest of the processing later. 1302 * This should rarely (if ever) be necessary. 1303 */ 1304 mblk_t *nmp; 1305 1306 if ((nmp = msgpullup(mp, -1)) == NULL) { 1307 freemsg(mp); 1308 } else { 1309 freemsg(mp); 1310 (void) putq(wq, nmp); 1311 } 1312 } else { 1313 (void) putq(wq, mp); 1314 } 1315 break; 1316 1317 default: 1318 freemsg(mp); 1319 break; 1320 } 1321 } 1322 1323 static void 1324 devaudio_wsrv(audio_client_t *c) 1325 { 1326 queue_t *wq = auclnt_get_wq(c); 1327 daclient_t *dc = auclnt_get_private(c); 1328 audio_stream_t *sp; 1329 mblk_t *mp; 1330 unsigned framesz; 1331 1332 sp = auclnt_output_stream(c); 1333 1334 framesz = auclnt_get_framesz(sp); 1335 1336 while ((mp = getq(wq)) != NULL) { 1337 1338 unsigned count; 1339 1340 /* got a message */ 1341 1342 /* if its a drain ioctl, we need to process it here */ 1343 if (DB_TYPE(mp) == M_IOCTL) { 1344 ASSERT((*(int *)(void *)mp->b_rptr) == AUDIO_DRAIN); 1345 mutex_enter(&dc->dc_lock); 1346 mp->b_next = dc->dc_draining; 1347 dc->dc_draining = mp; 1348 mutex_exit(&dc->dc_lock); 1349 1350 if (auclnt_start_drain(c) != 0) { 1351 devaudio_drain(c); 1352 } 1353 continue; 1354 } 1355 1356 ASSERT(DB_TYPE(mp) == M_DATA); 1357 1358 /* 1359 * Empty mblk require special handling, since they 1360 * indicate EOF. We treat them separate from the main 1361 * processing loop. 1362 */ 1363 if (MBLKL(mp) == 0) { 1364 struct eofcnt *eof; 1365 1366 eof = kmem_zalloc(sizeof (*eof), KM_NOSLEEP); 1367 if (eof != NULL) { 1368 eof->tail = auclnt_get_head(sp); 1369 mutex_enter(&dc->dc_lock); 1370 list_insert_tail(&dc->dc_eofcnt, eof); 1371 mutex_exit(&dc->dc_lock); 1372 } 1373 freemsg(mp); 1374 continue; 1375 } 1376 1377 count = auclnt_produce_data(sp, (caddr_t)mp->b_rptr, 1378 MBLKL(mp) / framesz); 1379 1380 mp->b_rptr += count * framesz; 1381 1382 if (MBLKL(mp) >= framesz) { 1383 (void) putbq(wq, mp); 1384 break; 1385 } else { 1386 freemsg(mp); 1387 } 1388 } 1389 1390 /* if the stream isn't running yet, start it up */ 1391 if (!auclnt_is_paused(sp)) 1392 auclnt_start(sp); 1393 } 1394 1395 static struct audio_client_ops devaudio_ops = { 1396 "sound,audio", 1397 devaudio_init, 1398 devaudio_fini, 1399 devaudio_open, 1400 devaudio_close, 1401 NULL, /* read */ 1402 NULL, /* write */ 1403 NULL, /* ioctl */ 1404 NULL, /* chpoll */ 1405 NULL, /* mmap */ 1406 devaudio_input, 1407 devaudio_output, 1408 NULL, /* notify */ 1409 devaudio_drain, 1410 devaudio_wput, 1411 devaudio_wsrv 1412 }; 1413 1414 static struct audio_client_ops devaudioctl_ops = { 1415 "sound,audioctl", 1416 NULL, /* dev_init */ 1417 NULL, /* dev_fini */ 1418 devaudioctl_open, 1419 devaudioctl_close, 1420 NULL, /* read */ 1421 NULL, /* write */ 1422 NULL, /* ioctl */ 1423 NULL, /* chpoll */ 1424 NULL, /* mmap */ 1425 NULL, /* output */ 1426 NULL, /* input */ 1427 NULL, /* notify */ 1428 NULL, /* drain */ 1429 devaudioctl_wput, 1430 NULL, 1431 }; 1432 1433 void 1434 auimpl_sun_init(void) 1435 { 1436 auclnt_register_ops(AUDIO_MINOR_DEVAUDIO, &devaudio_ops); 1437 auclnt_register_ops(AUDIO_MINOR_DEVAUDIOCTL, &devaudioctl_ops); 1438 } 1439