1 /* 2 * linux/sound/oss/dmasound/dmasound_core.c 3 * 4 * 5 * OSS/Free compatible Atari TT/Falcon and Amiga DMA sound driver for 6 * Linux/m68k 7 * Extended to support Power Macintosh for Linux/ppc by Paul Mackerras 8 * 9 * (c) 1995 by Michael Schlueter & Michael Marte 10 * 11 * Michael Schlueter (michael@duck.syd.de) did the basic structure of the VFS 12 * interface and the u-law to signed byte conversion. 13 * 14 * Michael Marte (marte@informatik.uni-muenchen.de) did the sound queue, 15 * /dev/mixer, /dev/sndstat and complemented the VFS interface. He would like 16 * to thank: 17 * - Michael Schlueter for initial ideas and documentation on the MFP and 18 * the DMA sound hardware. 19 * - Therapy? for their CD 'Troublegum' which really made me rock. 20 * 21 * /dev/sndstat is based on code by Hannu Savolainen, the author of the 22 * VoxWare family of drivers. 23 * 24 * This file is subject to the terms and conditions of the GNU General Public 25 * License. See the file COPYING in the main directory of this archive 26 * for more details. 27 * 28 * History: 29 * 30 * 1995/8/25 First release 31 * 32 * 1995/9/02 Roman Hodek: 33 * - Fixed atari_stram_alloc() call, the timer 34 * programming and several race conditions 35 * 1995/9/14 Roman Hodek: 36 * - After some discussion with Michael Schlueter, 37 * revised the interrupt disabling 38 * - Slightly speeded up U8->S8 translation by using 39 * long operations where possible 40 * - Added 4:3 interpolation for /dev/audio 41 * 42 * 1995/9/20 Torsten Scherer: 43 * - Fixed a bug in sq_write and changed /dev/audio 44 * converting to play at 12517Hz instead of 6258Hz. 45 * 46 * 1995/9/23 Torsten Scherer: 47 * - Changed sq_interrupt() and sq_play() to pre-program 48 * the DMA for another frame while there's still one 49 * running. This allows the IRQ response to be 50 * arbitrarily delayed and playing will still continue. 51 * 52 * 1995/10/14 Guenther Kelleter, Torsten Scherer: 53 * - Better support for Falcon audio (the Falcon doesn't 54 * raise an IRQ at the end of a frame, but at the 55 * beginning instead!). uses 'if (codec_dma)' in lots 56 * of places to simply switch between Falcon and TT 57 * code. 58 * 59 * 1995/11/06 Torsten Scherer: 60 * - Started introducing a hardware abstraction scheme 61 * (may perhaps also serve for Amigas?) 62 * - Can now play samples at almost all frequencies by 63 * means of a more generalized expand routine 64 * - Takes a good deal of care to cut data only at 65 * sample sizes 66 * - Buffer size is now a kernel runtime option 67 * - Implemented fsync() & several minor improvements 68 * Guenther Kelleter: 69 * - Useful hints and bug fixes 70 * - Cross-checked it for Falcons 71 * 72 * 1996/3/9 Geert Uytterhoeven: 73 * - Support added for Amiga, A-law, 16-bit little 74 * endian. 75 * - Unification to drivers/sound/dmasound.c. 76 * 77 * 1996/4/6 Martin Mitchell: 78 * - Updated to 1.3 kernel. 79 * 80 * 1996/6/13 Topi Kanerva: 81 * - Fixed things that were broken (mainly the amiga 82 * 14-bit routines) 83 * - /dev/sndstat shows now the real hardware frequency 84 * - The lowpass filter is disabled by default now 85 * 86 * 1996/9/25 Geert Uytterhoeven: 87 * - Modularization 88 * 89 * 1998/6/10 Andreas Schwab: 90 * - Converted to use sound_core 91 * 92 * 1999/12/28 Richard Zidlicky: 93 * - Added support for Q40 94 * 95 * 2000/2/27 Geert Uytterhoeven: 96 * - Clean up and split the code into 4 parts: 97 * o dmasound_core: machine-independent code 98 * o dmasound_atari: Atari TT and Falcon support 99 * o dmasound_awacs: Apple PowerMac support 100 * o dmasound_paula: Amiga support 101 * 102 * 2000/3/25 Geert Uytterhoeven: 103 * - Integration of dmasound_q40 104 * - Small clean ups 105 * 106 * 2001/01/26 [1.0] Iain Sandoe 107 * - make /dev/sndstat show revision & edition info. 108 * - since dmasound.mach.sq_setup() can fail on pmac 109 * its type has been changed to int and the returns 110 * are checked. 111 * [1.1] - stop missing translations from being called. 112 * 2001/02/08 [1.2] - remove unused translation tables & move machine- 113 * specific tables to low-level. 114 * - return correct info. for SNDCTL_DSP_GETFMTS. 115 * [1.3] - implement SNDCTL_DSP_GETCAPS fully. 116 * [1.4] - make /dev/sndstat text length usage deterministic. 117 * - make /dev/sndstat call to low-level 118 * dmasound.mach.state_info() pass max space to ll driver. 119 * - tidy startup banners and output info. 120 * [1.5] - tidy up a little (removed some unused #defines in 121 * dmasound.h) 122 * - fix up HAS_RECORD conditionalisation. 123 * - add record code in places it is missing... 124 * - change buf-sizes to bytes to allow < 1kb for pmac 125 * if user param entry is < 256 the value is taken to 126 * be in kb > 256 is taken to be in bytes. 127 * - make default buff/frag params conditional on 128 * machine to allow smaller values for pmac. 129 * - made the ioctls, read & write comply with the OSS 130 * rules on setting params. 131 * - added parsing of _setup() params for record. 132 * 2001/04/04 [1.6] - fix bug where sample rates higher than maximum were 133 * being reported as OK. 134 * - fix open() to return -EBUSY as per OSS doc. when 135 * audio is in use - this is independent of O_NOBLOCK. 136 * - fix bug where SNDCTL_DSP_POST was blocking. 137 */ 138 139 /* Record capability notes 30/01/2001: 140 * At present these observations apply only to pmac LL driver (the only one 141 * that can do record, at present). However, if other LL drivers for machines 142 * with record are added they may apply. 143 * 144 * The fragment parameters for the record and play channels are separate. 145 * However, if the driver is opened O_RDWR there is no way (in the current OSS 146 * API) to specify their values independently for the record and playback 147 * channels. Since the only common factor between the input & output is the 148 * sample rate (on pmac) it should be possible to open /dev/dspX O_WRONLY and 149 * /dev/dspY O_RDONLY. The input & output channels could then have different 150 * characteristics (other than the first that sets sample rate claiming the 151 * right to set it for ever). As it stands, the format, channels, number of 152 * bits & sample rate are assumed to be common. In the future perhaps these 153 * should be the responsibility of the LL driver - and then if a card really 154 * does not share items between record & playback they can be specified 155 * separately. 156 */ 157 158 /* Thread-safeness of shared_resources notes: 31/01/2001 159 * If the user opens O_RDWR and then splits record & play between two threads 160 * both of which inherit the fd - and then starts changing things from both 161 * - we will have difficulty telling. 162 * 163 * It's bad application coding - but ... 164 * TODO: think about how to sort this out... without bogging everything down in 165 * semaphores. 166 * 167 * Similarly, the OSS spec says "all changes to parameters must be between 168 * open() and the first read() or write(). - and a bit later on (by 169 * implication) "between SNDCTL_DSP_RESET and the first read() or write() after 170 * it". If the app is multi-threaded and this rule is broken between threads 171 * we will have trouble spotting it - and the fault will be rather obscure :-( 172 * 173 * We will try and put out at least a kmsg if we see it happen... but I think 174 * it will be quite hard to trap it with an -EXXX return... because we can't 175 * see the fault until after the damage is done. 176 */ 177 178 #include <linux/module.h> 179 #include <linux/slab.h> 180 #include <linux/sound.h> 181 #include <linux/init.h> 182 #include <linux/soundcard.h> 183 #include <linux/poll.h> 184 #include <linux/mutex.h> 185 #include <linux/sched/signal.h> 186 187 #include <linux/uaccess.h> 188 189 #include "dmasound.h" 190 191 #define DMASOUND_CORE_REVISION 1 192 #define DMASOUND_CORE_EDITION 6 193 194 /* 195 * Declarations 196 */ 197 198 static DEFINE_MUTEX(dmasound_core_mutex); 199 int dmasound_catchRadius = 0; 200 module_param(dmasound_catchRadius, int, 0); 201 202 static unsigned int numWriteBufs = DEFAULT_N_BUFFERS; 203 module_param(numWriteBufs, int, 0); 204 static unsigned int writeBufSize = DEFAULT_BUFF_SIZE ; /* in bytes */ 205 module_param(writeBufSize, int, 0); 206 207 MODULE_LICENSE("GPL"); 208 209 #ifdef MODULE 210 static int sq_unit = -1; 211 static int mixer_unit = -1; 212 static int state_unit = -1; 213 static int irq_installed; 214 #endif /* MODULE */ 215 216 /* control over who can modify resources shared between play/record */ 217 static fmode_t shared_resource_owner; 218 static int shared_resources_initialised; 219 220 /* 221 * Mid level stuff 222 */ 223 224 struct sound_settings dmasound = { 225 .lock = __SPIN_LOCK_UNLOCKED(dmasound.lock) 226 }; 227 228 static inline void sound_silence(void) 229 { 230 dmasound.mach.silence(); /* _MUST_ stop DMA */ 231 } 232 233 static inline int sound_set_format(int format) 234 { 235 return dmasound.mach.setFormat(format); 236 } 237 238 239 static int sound_set_speed(int speed) 240 { 241 if (speed < 0) 242 return dmasound.soft.speed; 243 244 /* trap out-of-range speed settings. 245 at present we allow (arbitrarily) low rates - using soft 246 up-conversion - but we can't allow > max because there is 247 no soft down-conversion. 248 */ 249 if (dmasound.mach.max_dsp_speed && 250 (speed > dmasound.mach.max_dsp_speed)) 251 speed = dmasound.mach.max_dsp_speed ; 252 253 dmasound.soft.speed = speed; 254 255 if (dmasound.minDev == SND_DEV_DSP) 256 dmasound.dsp.speed = dmasound.soft.speed; 257 258 return dmasound.soft.speed; 259 } 260 261 static int sound_set_stereo(int stereo) 262 { 263 if (stereo < 0) 264 return dmasound.soft.stereo; 265 266 stereo = !!stereo; /* should be 0 or 1 now */ 267 268 dmasound.soft.stereo = stereo; 269 if (dmasound.minDev == SND_DEV_DSP) 270 dmasound.dsp.stereo = stereo; 271 272 return stereo; 273 } 274 275 static ssize_t sound_copy_translate(TRANS *trans, const u_char __user *userPtr, 276 size_t userCount, u_char frame[], 277 ssize_t *frameUsed, ssize_t frameLeft) 278 { 279 ssize_t (*ct_func)(const u_char __user *, size_t, u_char *, ssize_t *, ssize_t); 280 281 switch (dmasound.soft.format) { 282 case AFMT_MU_LAW: 283 ct_func = trans->ct_ulaw; 284 break; 285 case AFMT_A_LAW: 286 ct_func = trans->ct_alaw; 287 break; 288 case AFMT_S8: 289 ct_func = trans->ct_s8; 290 break; 291 case AFMT_U8: 292 ct_func = trans->ct_u8; 293 break; 294 case AFMT_S16_BE: 295 ct_func = trans->ct_s16be; 296 break; 297 case AFMT_U16_BE: 298 ct_func = trans->ct_u16be; 299 break; 300 case AFMT_S16_LE: 301 ct_func = trans->ct_s16le; 302 break; 303 case AFMT_U16_LE: 304 ct_func = trans->ct_u16le; 305 break; 306 default: 307 return 0; 308 } 309 /* if the user has requested a non-existent translation don't try 310 to call it but just return 0 bytes moved 311 */ 312 if (ct_func) 313 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft); 314 return 0; 315 } 316 317 /* 318 * /dev/mixer abstraction 319 */ 320 321 static struct { 322 int busy; 323 int modify_counter; 324 } mixer; 325 326 static int mixer_open(struct inode *inode, struct file *file) 327 { 328 mutex_lock(&dmasound_core_mutex); 329 if (!try_module_get(dmasound.mach.owner)) { 330 mutex_unlock(&dmasound_core_mutex); 331 return -ENODEV; 332 } 333 mixer.busy = 1; 334 mutex_unlock(&dmasound_core_mutex); 335 return 0; 336 } 337 338 static int mixer_release(struct inode *inode, struct file *file) 339 { 340 mutex_lock(&dmasound_core_mutex); 341 mixer.busy = 0; 342 module_put(dmasound.mach.owner); 343 mutex_unlock(&dmasound_core_mutex); 344 return 0; 345 } 346 347 static int mixer_ioctl(struct file *file, u_int cmd, u_long arg) 348 { 349 if (_SIOC_DIR(cmd) & _SIOC_WRITE) 350 mixer.modify_counter++; 351 switch (cmd) { 352 case OSS_GETVERSION: 353 return IOCTL_OUT(arg, SOUND_VERSION); 354 case SOUND_MIXER_INFO: 355 { 356 mixer_info info; 357 memset(&info, 0, sizeof(info)); 358 strlcpy(info.id, dmasound.mach.name2, sizeof(info.id)); 359 strlcpy(info.name, dmasound.mach.name2, sizeof(info.name)); 360 info.modify_counter = mixer.modify_counter; 361 if (copy_to_user((void __user *)arg, &info, sizeof(info))) 362 return -EFAULT; 363 return 0; 364 } 365 } 366 if (dmasound.mach.mixer_ioctl) 367 return dmasound.mach.mixer_ioctl(cmd, arg); 368 return -EINVAL; 369 } 370 371 static long mixer_unlocked_ioctl(struct file *file, u_int cmd, u_long arg) 372 { 373 int ret; 374 375 mutex_lock(&dmasound_core_mutex); 376 ret = mixer_ioctl(file, cmd, arg); 377 mutex_unlock(&dmasound_core_mutex); 378 379 return ret; 380 } 381 382 static const struct file_operations mixer_fops = 383 { 384 .owner = THIS_MODULE, 385 .llseek = no_llseek, 386 .unlocked_ioctl = mixer_unlocked_ioctl, 387 .open = mixer_open, 388 .release = mixer_release, 389 }; 390 391 static void mixer_init(void) 392 { 393 #ifndef MODULE 394 int mixer_unit; 395 #endif 396 mixer_unit = register_sound_mixer(&mixer_fops, -1); 397 if (mixer_unit < 0) 398 return; 399 400 mixer.busy = 0; 401 dmasound.treble = 0; 402 dmasound.bass = 0; 403 if (dmasound.mach.mixer_init) 404 dmasound.mach.mixer_init(); 405 } 406 407 408 /* 409 * Sound queue stuff, the heart of the driver 410 */ 411 412 struct sound_queue dmasound_write_sq; 413 static void sq_reset_output(void) ; 414 415 static int sq_allocate_buffers(struct sound_queue *sq, int num, int size) 416 { 417 int i; 418 419 if (sq->buffers) 420 return 0; 421 sq->numBufs = num; 422 sq->bufSize = size; 423 sq->buffers = kmalloc (num * sizeof(char *), GFP_KERNEL); 424 if (!sq->buffers) 425 return -ENOMEM; 426 for (i = 0; i < num; i++) { 427 sq->buffers[i] = dmasound.mach.dma_alloc(size, GFP_KERNEL); 428 if (!sq->buffers[i]) { 429 while (i--) 430 dmasound.mach.dma_free(sq->buffers[i], size); 431 kfree(sq->buffers); 432 sq->buffers = NULL; 433 return -ENOMEM; 434 } 435 } 436 return 0; 437 } 438 439 static void sq_release_buffers(struct sound_queue *sq) 440 { 441 int i; 442 443 if (sq->buffers) { 444 for (i = 0; i < sq->numBufs; i++) 445 dmasound.mach.dma_free(sq->buffers[i], sq->bufSize); 446 kfree(sq->buffers); 447 sq->buffers = NULL; 448 } 449 } 450 451 452 static int sq_setup(struct sound_queue *sq) 453 { 454 int (*setup_func)(void) = NULL; 455 int hard_frame ; 456 457 if (sq->locked) { /* are we already set? - and not changeable */ 458 #ifdef DEBUG_DMASOUND 459 printk("dmasound_core: tried to sq_setup a locked queue\n") ; 460 #endif 461 return -EINVAL ; 462 } 463 sq->locked = 1 ; /* don't think we have a race prob. here _check_ */ 464 465 /* make sure that the parameters are set up 466 This should have been done already... 467 */ 468 469 dmasound.mach.init(); 470 471 /* OK. If the user has set fragment parameters explicitly, then we 472 should leave them alone... as long as they are valid. 473 Invalid user fragment params can occur if we allow the whole buffer 474 to be used when the user requests the fragments sizes (with no soft 475 x-lation) and then the user subsequently sets a soft x-lation that 476 requires increased internal buffering. 477 478 Othwerwise (if the user did not set them) OSS says that we should 479 select frag params on the basis of 0.5 s output & 0.1 s input 480 latency. (TODO. For now we will copy in the defaults.) 481 */ 482 483 if (sq->user_frags <= 0) { 484 sq->max_count = sq->numBufs ; 485 sq->max_active = sq->numBufs ; 486 sq->block_size = sq->bufSize; 487 /* set up the user info */ 488 sq->user_frags = sq->numBufs ; 489 sq->user_frag_size = sq->bufSize ; 490 sq->user_frag_size *= 491 (dmasound.soft.size * (dmasound.soft.stereo+1) ) ; 492 sq->user_frag_size /= 493 (dmasound.hard.size * (dmasound.hard.stereo+1) ) ; 494 } else { 495 /* work out requested block size */ 496 sq->block_size = sq->user_frag_size ; 497 sq->block_size *= 498 (dmasound.hard.size * (dmasound.hard.stereo+1) ) ; 499 sq->block_size /= 500 (dmasound.soft.size * (dmasound.soft.stereo+1) ) ; 501 /* the user wants to write frag-size chunks */ 502 sq->block_size *= dmasound.hard.speed ; 503 sq->block_size /= dmasound.soft.speed ; 504 /* this only works for size values which are powers of 2 */ 505 hard_frame = 506 (dmasound.hard.size * (dmasound.hard.stereo+1))/8 ; 507 sq->block_size += (hard_frame - 1) ; 508 sq->block_size &= ~(hard_frame - 1) ; /* make sure we are aligned */ 509 /* let's just check for obvious mistakes */ 510 if ( sq->block_size <= 0 || sq->block_size > sq->bufSize) { 511 #ifdef DEBUG_DMASOUND 512 printk("dmasound_core: invalid frag size (user set %d)\n", sq->user_frag_size) ; 513 #endif 514 sq->block_size = sq->bufSize ; 515 } 516 if ( sq->user_frags <= sq->numBufs ) { 517 sq->max_count = sq->user_frags ; 518 /* if user has set max_active - then use it */ 519 sq->max_active = (sq->max_active <= sq->max_count) ? 520 sq->max_active : sq->max_count ; 521 } else { 522 #ifdef DEBUG_DMASOUND 523 printk("dmasound_core: invalid frag count (user set %d)\n", sq->user_frags) ; 524 #endif 525 sq->max_count = 526 sq->max_active = sq->numBufs ; 527 } 528 } 529 sq->front = sq->count = sq->rear_size = 0; 530 sq->syncing = 0; 531 sq->active = 0; 532 533 if (sq == &write_sq) { 534 sq->rear = -1; 535 setup_func = dmasound.mach.write_sq_setup; 536 } 537 if (setup_func) 538 return setup_func(); 539 return 0 ; 540 } 541 542 static inline void sq_play(void) 543 { 544 dmasound.mach.play(); 545 } 546 547 static ssize_t sq_write(struct file *file, const char __user *src, size_t uLeft, 548 loff_t *ppos) 549 { 550 ssize_t uWritten = 0; 551 u_char *dest; 552 ssize_t uUsed = 0, bUsed, bLeft; 553 unsigned long flags ; 554 555 /* ++TeSche: Is something like this necessary? 556 * Hey, that's an honest question! Or does any other part of the 557 * filesystem already checks this situation? I really don't know. 558 */ 559 if (uLeft == 0) 560 return 0; 561 562 /* implement any changes we have made to the soft/hard params. 563 this is not satisfactory really, all we have done up to now is to 564 say what we would like - there hasn't been any real checking of capability 565 */ 566 567 if (shared_resources_initialised == 0) { 568 dmasound.mach.init() ; 569 shared_resources_initialised = 1 ; 570 } 571 572 /* set up the sq if it is not already done. This may seem a dumb place 573 to do it - but it is what OSS requires. It means that write() can 574 return memory allocation errors. To avoid this possibility use the 575 GETBLKSIZE or GETOSPACE ioctls (after you've fiddled with all the 576 params you want to change) - these ioctls also force the setup. 577 */ 578 579 if (write_sq.locked == 0) { 580 if ((uWritten = sq_setup(&write_sq)) < 0) return uWritten ; 581 uWritten = 0 ; 582 } 583 584 /* FIXME: I think that this may be the wrong behaviour when we get strapped 585 for time and the cpu is close to being (or actually) behind in sending data. 586 - because we've lost the time that the N samples, already in the buffer, 587 would have given us to get here with the next lot from the user. 588 */ 589 /* The interrupt doesn't start to play the last, incomplete frame. 590 * Thus we can append to it without disabling the interrupts! (Note 591 * also that write_sq.rear isn't affected by the interrupt.) 592 */ 593 594 /* as of 1.6 this behaviour changes if SNDCTL_DSP_POST has been issued: 595 this will mimic the behaviour of syncing and allow the sq_play() to 596 queue a partial fragment. Since sq_play() may/will be called from 597 the IRQ handler - at least on Pmac we have to deal with it. 598 The strategy - possibly not optimum - is to kill _POST status if we 599 get here. This seems, at least, reasonable - in the sense that POST 600 is supposed to indicate that we might not write before the queue 601 is drained - and if we get here in time then it does not apply. 602 */ 603 604 spin_lock_irqsave(&dmasound.lock, flags); 605 write_sq.syncing &= ~2 ; /* take out POST status */ 606 spin_unlock_irqrestore(&dmasound.lock, flags); 607 608 if (write_sq.count > 0 && 609 (bLeft = write_sq.block_size-write_sq.rear_size) > 0) { 610 dest = write_sq.buffers[write_sq.rear]; 611 bUsed = write_sq.rear_size; 612 uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft, 613 dest, &bUsed, bLeft); 614 if (uUsed <= 0) 615 return uUsed; 616 src += uUsed; 617 uWritten += uUsed; 618 uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */ 619 write_sq.rear_size = bUsed; 620 } 621 622 while (uLeft) { 623 DEFINE_WAIT(wait); 624 625 while (write_sq.count >= write_sq.max_active) { 626 prepare_to_wait(&write_sq.action_queue, &wait, TASK_INTERRUPTIBLE); 627 sq_play(); 628 if (write_sq.non_blocking) { 629 finish_wait(&write_sq.action_queue, &wait); 630 return uWritten > 0 ? uWritten : -EAGAIN; 631 } 632 if (write_sq.count < write_sq.max_active) 633 break; 634 635 schedule_timeout(HZ); 636 if (signal_pending(current)) { 637 finish_wait(&write_sq.action_queue, &wait); 638 return uWritten > 0 ? uWritten : -EINTR; 639 } 640 } 641 642 finish_wait(&write_sq.action_queue, &wait); 643 644 /* Here, we can avoid disabling the interrupt by first 645 * copying and translating the data, and then updating 646 * the write_sq variables. Until this is done, the interrupt 647 * won't see the new frame and we can work on it 648 * undisturbed. 649 */ 650 651 dest = write_sq.buffers[(write_sq.rear+1) % write_sq.max_count]; 652 bUsed = 0; 653 bLeft = write_sq.block_size; 654 uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft, 655 dest, &bUsed, bLeft); 656 if (uUsed <= 0) 657 break; 658 src += uUsed; 659 uWritten += uUsed; 660 uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */ 661 if (bUsed) { 662 write_sq.rear = (write_sq.rear+1) % write_sq.max_count; 663 write_sq.rear_size = bUsed; 664 write_sq.count++; 665 } 666 } /* uUsed may have been 0 */ 667 668 sq_play(); 669 670 return uUsed < 0? uUsed: uWritten; 671 } 672 673 static unsigned int sq_poll(struct file *file, struct poll_table_struct *wait) 674 { 675 unsigned int mask = 0; 676 int retVal; 677 678 if (write_sq.locked == 0) { 679 if ((retVal = sq_setup(&write_sq)) < 0) 680 return retVal; 681 return 0; 682 } 683 if (file->f_mode & FMODE_WRITE ) 684 poll_wait(file, &write_sq.action_queue, wait); 685 if (file->f_mode & FMODE_WRITE) 686 if (write_sq.count < write_sq.max_active || write_sq.block_size - write_sq.rear_size > 0) 687 mask |= POLLOUT | POLLWRNORM; 688 return mask; 689 690 } 691 692 static inline void sq_init_waitqueue(struct sound_queue *sq) 693 { 694 init_waitqueue_head(&sq->action_queue); 695 init_waitqueue_head(&sq->open_queue); 696 init_waitqueue_head(&sq->sync_queue); 697 sq->busy = 0; 698 } 699 700 #if 0 /* blocking open() */ 701 static inline void sq_wake_up(struct sound_queue *sq, struct file *file, 702 fmode_t mode) 703 { 704 if (file->f_mode & mode) { 705 sq->busy = 0; /* CHECK: IS THIS OK??? */ 706 WAKE_UP(sq->open_queue); 707 } 708 } 709 #endif 710 711 static int sq_open2(struct sound_queue *sq, struct file *file, fmode_t mode, 712 int numbufs, int bufsize) 713 { 714 int rc = 0; 715 716 if (file->f_mode & mode) { 717 if (sq->busy) { 718 #if 0 /* blocking open() */ 719 rc = -EBUSY; 720 if (file->f_flags & O_NONBLOCK) 721 return rc; 722 rc = -EINTR; 723 if (wait_event_interruptible(sq->open_queue, !sq->busy)) 724 return rc; 725 rc = 0; 726 #else 727 /* OSS manual says we will return EBUSY regardless 728 of O_NOBLOCK. 729 */ 730 return -EBUSY ; 731 #endif 732 } 733 sq->busy = 1; /* Let's play spot-the-race-condition */ 734 735 /* allocate the default number & size of buffers. 736 (i.e. specified in _setup() or as module params) 737 can't be changed at the moment - but _could_ be perhaps 738 in the setfragments ioctl. 739 */ 740 if (( rc = sq_allocate_buffers(sq, numbufs, bufsize))) { 741 #if 0 /* blocking open() */ 742 sq_wake_up(sq, file, mode); 743 #else 744 sq->busy = 0 ; 745 #endif 746 return rc; 747 } 748 749 sq->non_blocking = file->f_flags & O_NONBLOCK; 750 } 751 return rc; 752 } 753 754 #define write_sq_init_waitqueue() sq_init_waitqueue(&write_sq) 755 #if 0 /* blocking open() */ 756 #define write_sq_wake_up(file) sq_wake_up(&write_sq, file, FMODE_WRITE) 757 #endif 758 #define write_sq_release_buffers() sq_release_buffers(&write_sq) 759 #define write_sq_open(file) \ 760 sq_open2(&write_sq, file, FMODE_WRITE, numWriteBufs, writeBufSize ) 761 762 static int sq_open(struct inode *inode, struct file *file) 763 { 764 int rc; 765 766 mutex_lock(&dmasound_core_mutex); 767 if (!try_module_get(dmasound.mach.owner)) { 768 mutex_unlock(&dmasound_core_mutex); 769 return -ENODEV; 770 } 771 772 rc = write_sq_open(file); /* checks the f_mode */ 773 if (rc) 774 goto out; 775 if (file->f_mode & FMODE_READ) { 776 /* TODO: if O_RDWR, release any resources grabbed by write part */ 777 rc = -ENXIO ; /* I think this is what is required by open(2) */ 778 goto out; 779 } 780 781 if (dmasound.mach.sq_open) 782 dmasound.mach.sq_open(file->f_mode); 783 784 /* CHECK whether this is sensible - in the case that dsp0 could be opened 785 O_RDONLY and dsp1 could be opened O_WRONLY 786 */ 787 788 dmasound.minDev = iminor(inode) & 0x0f; 789 790 /* OK. - we should make some attempt at consistency. At least the H'ware 791 options should be set with a valid mode. We will make it that the LL 792 driver must supply defaults for hard & soft params. 793 */ 794 795 if (shared_resource_owner == 0) { 796 /* you can make this AFMT_U8/mono/8K if you want to mimic old 797 OSS behaviour - while we still have soft translations ;-) */ 798 dmasound.soft = dmasound.mach.default_soft ; 799 dmasound.dsp = dmasound.mach.default_soft ; 800 dmasound.hard = dmasound.mach.default_hard ; 801 } 802 803 #ifndef DMASOUND_STRICT_OSS_COMPLIANCE 804 /* none of the current LL drivers can actually do this "native" at the moment 805 OSS does not really require us to supply /dev/audio if we can't do it. 806 */ 807 if (dmasound.minDev == SND_DEV_AUDIO) { 808 sound_set_speed(8000); 809 sound_set_stereo(0); 810 sound_set_format(AFMT_MU_LAW); 811 } 812 #endif 813 mutex_unlock(&dmasound_core_mutex); 814 return 0; 815 out: 816 module_put(dmasound.mach.owner); 817 mutex_unlock(&dmasound_core_mutex); 818 return rc; 819 } 820 821 static void sq_reset_output(void) 822 { 823 sound_silence(); /* this _must_ stop DMA, we might be about to lose the buffers */ 824 write_sq.active = 0; 825 write_sq.count = 0; 826 write_sq.rear_size = 0; 827 /* write_sq.front = (write_sq.rear+1) % write_sq.max_count;*/ 828 write_sq.front = 0 ; 829 write_sq.rear = -1 ; /* same as for set-up */ 830 831 /* OK - we can unlock the parameters and fragment settings */ 832 write_sq.locked = 0 ; 833 write_sq.user_frags = 0 ; 834 write_sq.user_frag_size = 0 ; 835 } 836 837 static void sq_reset(void) 838 { 839 sq_reset_output() ; 840 /* we could consider resetting the shared_resources_owner here... but I 841 think it is probably still rather non-obvious to application writer 842 */ 843 844 /* we release everything else though */ 845 shared_resources_initialised = 0 ; 846 } 847 848 static int sq_fsync(void) 849 { 850 int rc = 0; 851 int timeout = 5; 852 853 write_sq.syncing |= 1; 854 sq_play(); /* there may be an incomplete frame waiting */ 855 856 while (write_sq.active) { 857 wait_event_interruptible_timeout(write_sq.sync_queue, 858 !write_sq.active, HZ); 859 if (signal_pending(current)) { 860 /* While waiting for audio output to drain, an 861 * interrupt occurred. Stop audio output immediately 862 * and clear the queue. */ 863 sq_reset_output(); 864 rc = -EINTR; 865 break; 866 } 867 if (!--timeout) { 868 printk(KERN_WARNING "dmasound: Timeout draining output\n"); 869 sq_reset_output(); 870 rc = -EIO; 871 break; 872 } 873 } 874 875 /* flag no sync regardless of whether we had a DSP_POST or not */ 876 write_sq.syncing = 0 ; 877 return rc; 878 } 879 880 static int sq_release(struct inode *inode, struct file *file) 881 { 882 int rc = 0; 883 884 mutex_lock(&dmasound_core_mutex); 885 886 if (file->f_mode & FMODE_WRITE) { 887 if (write_sq.busy) 888 rc = sq_fsync(); 889 890 sq_reset_output() ; /* make sure dma is stopped and all is quiet */ 891 write_sq_release_buffers(); 892 write_sq.busy = 0; 893 } 894 895 if (file->f_mode & shared_resource_owner) { /* it's us that has them */ 896 shared_resource_owner = 0 ; 897 shared_resources_initialised = 0 ; 898 dmasound.hard = dmasound.mach.default_hard ; 899 } 900 901 module_put(dmasound.mach.owner); 902 903 #if 0 /* blocking open() */ 904 /* Wake up a process waiting for the queue being released. 905 * Note: There may be several processes waiting for a call 906 * to open() returning. */ 907 908 /* Iain: hmm I don't understand this next comment ... */ 909 /* There is probably a DOS atack here. They change the mode flag. */ 910 /* XXX add check here,*/ 911 read_sq_wake_up(file); /* checks f_mode */ 912 write_sq_wake_up(file); /* checks f_mode */ 913 #endif /* blocking open() */ 914 915 mutex_unlock(&dmasound_core_mutex); 916 917 return rc; 918 } 919 920 /* here we see if we have a right to modify format, channels, size and so on 921 if no-one else has claimed it already then we do... 922 923 TODO: We might change this to mask O_RDWR such that only one or the other channel 924 is the owner - if we have problems. 925 */ 926 927 static int shared_resources_are_mine(fmode_t md) 928 { 929 if (shared_resource_owner) 930 return (shared_resource_owner & md) != 0; 931 else { 932 shared_resource_owner = md ; 933 return 1 ; 934 } 935 } 936 937 /* if either queue is locked we must deny the right to change shared params 938 */ 939 940 static int queues_are_quiescent(void) 941 { 942 if (write_sq.locked) 943 return 0 ; 944 return 1 ; 945 } 946 947 /* check and set a queue's fragments per user's wishes... 948 we will check against the pre-defined literals and the actual sizes. 949 This is a bit fraught - because soft translations can mess with our 950 buffer requirements *after* this call - OSS says "call setfrags first" 951 */ 952 953 /* It is possible to replace all the -EINVAL returns with an override that 954 just puts the allowable value in. This may be what many OSS apps require 955 */ 956 957 static int set_queue_frags(struct sound_queue *sq, int bufs, int size) 958 { 959 if (sq->locked) { 960 #ifdef DEBUG_DMASOUND 961 printk("dmasound_core: tried to set_queue_frags on a locked queue\n") ; 962 #endif 963 return -EINVAL ; 964 } 965 966 if ((size < MIN_FRAG_SIZE) || (size > MAX_FRAG_SIZE)) 967 return -EINVAL ; 968 size = (1<<size) ; /* now in bytes */ 969 if (size > sq->bufSize) 970 return -EINVAL ; /* this might still not work */ 971 972 if (bufs <= 0) 973 return -EINVAL ; 974 if (bufs > sq->numBufs) /* the user is allowed say "don't care" with 0x7fff */ 975 bufs = sq->numBufs ; 976 977 /* there is, currently, no way to specify max_active separately 978 from max_count. This could be a LL driver issue - I guess 979 if there is a requirement for these values to be different then 980 we will have to pass that info. up to this level. 981 */ 982 sq->user_frags = 983 sq->max_active = bufs ; 984 sq->user_frag_size = size ; 985 986 return 0 ; 987 } 988 989 static int sq_ioctl(struct file *file, u_int cmd, u_long arg) 990 { 991 int val, result; 992 u_long fmt; 993 int data; 994 int size, nbufs; 995 audio_buf_info info; 996 997 switch (cmd) { 998 case SNDCTL_DSP_RESET: 999 sq_reset(); 1000 return 0; 1001 break ; 1002 case SNDCTL_DSP_GETFMTS: 1003 fmt = dmasound.mach.hardware_afmts ; /* this is what OSS says.. */ 1004 return IOCTL_OUT(arg, fmt); 1005 break ; 1006 case SNDCTL_DSP_GETBLKSIZE: 1007 /* this should tell the caller about bytes that the app can 1008 read/write - the app doesn't care about our internal buffers. 1009 We force sq_setup() here as per OSS 1.1 (which should 1010 compute the values necessary). 1011 Since there is no mechanism to specify read/write separately, for 1012 fds opened O_RDWR, the write_sq values will, arbitrarily, overwrite 1013 the read_sq ones. 1014 */ 1015 size = 0 ; 1016 if (file->f_mode & FMODE_WRITE) { 1017 if ( !write_sq.locked ) 1018 sq_setup(&write_sq) ; 1019 size = write_sq.user_frag_size ; 1020 } 1021 return IOCTL_OUT(arg, size); 1022 break ; 1023 case SNDCTL_DSP_POST: 1024 /* all we are going to do is to tell the LL that any 1025 partial frags can be queued for output. 1026 The LL will have to clear this flag when last output 1027 is queued. 1028 */ 1029 write_sq.syncing |= 0x2 ; 1030 sq_play() ; 1031 return 0 ; 1032 case SNDCTL_DSP_SYNC: 1033 /* This call, effectively, has the same behaviour as SNDCTL_DSP_RESET 1034 except that it waits for output to finish before resetting 1035 everything - read, however, is killed immediately. 1036 */ 1037 result = 0 ; 1038 if (file->f_mode & FMODE_WRITE) { 1039 result = sq_fsync(); 1040 sq_reset_output() ; 1041 } 1042 /* if we are the shared resource owner then release them */ 1043 if (file->f_mode & shared_resource_owner) 1044 shared_resources_initialised = 0 ; 1045 return result ; 1046 break ; 1047 case SOUND_PCM_READ_RATE: 1048 return IOCTL_OUT(arg, dmasound.soft.speed); 1049 case SNDCTL_DSP_SPEED: 1050 /* changing this on the fly will have weird effects on the sound. 1051 Where there are rate conversions implemented in soft form - it 1052 will cause the _ctx_xxx() functions to be substituted. 1053 However, there doesn't appear to be any reason to dis-allow it from 1054 a driver pov. 1055 */ 1056 if (shared_resources_are_mine(file->f_mode)) { 1057 IOCTL_IN(arg, data); 1058 data = sound_set_speed(data) ; 1059 shared_resources_initialised = 0 ; 1060 return IOCTL_OUT(arg, data); 1061 } else 1062 return -EINVAL ; 1063 break ; 1064 /* OSS says these next 4 actions are undefined when the device is 1065 busy/active - we will just return -EINVAL. 1066 To be allowed to change one - (a) you have to own the right 1067 (b) the queue(s) must be quiescent 1068 */ 1069 case SNDCTL_DSP_STEREO: 1070 if (shared_resources_are_mine(file->f_mode) && 1071 queues_are_quiescent()) { 1072 IOCTL_IN(arg, data); 1073 shared_resources_initialised = 0 ; 1074 return IOCTL_OUT(arg, sound_set_stereo(data)); 1075 } else 1076 return -EINVAL ; 1077 break ; 1078 case SOUND_PCM_WRITE_CHANNELS: 1079 if (shared_resources_are_mine(file->f_mode) && 1080 queues_are_quiescent()) { 1081 IOCTL_IN(arg, data); 1082 /* the user might ask for 20 channels, we will return 1 or 2 */ 1083 shared_resources_initialised = 0 ; 1084 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1); 1085 } else 1086 return -EINVAL ; 1087 break ; 1088 case SNDCTL_DSP_SETFMT: 1089 if (shared_resources_are_mine(file->f_mode) && 1090 queues_are_quiescent()) { 1091 int format; 1092 IOCTL_IN(arg, data); 1093 shared_resources_initialised = 0 ; 1094 format = sound_set_format(data); 1095 result = IOCTL_OUT(arg, format); 1096 if (result < 0) 1097 return result; 1098 if (format != data && data != AFMT_QUERY) 1099 return -EINVAL; 1100 return 0; 1101 } else 1102 return -EINVAL ; 1103 case SNDCTL_DSP_SUBDIVIDE: 1104 return -EINVAL ; 1105 case SNDCTL_DSP_SETFRAGMENT: 1106 /* we can do this independently for the two queues - with the 1107 proviso that for fds opened O_RDWR we cannot separate the 1108 actions and both queues will be set per the last call. 1109 NOTE: this does *NOT* actually set the queue up - merely 1110 registers our intentions. 1111 */ 1112 IOCTL_IN(arg, data); 1113 result = 0 ; 1114 nbufs = (data >> 16) & 0x7fff ; /* 0x7fff is 'use maximum' */ 1115 size = data & 0xffff; 1116 if (file->f_mode & FMODE_WRITE) { 1117 result = set_queue_frags(&write_sq, nbufs, size) ; 1118 if (result) 1119 return result ; 1120 } 1121 /* NOTE: this return value is irrelevant - OSS specifically says that 1122 the value is 'random' and that the user _must_ check the actual 1123 frags values using SNDCTL_DSP_GETBLKSIZE or similar */ 1124 return IOCTL_OUT(arg, data); 1125 break ; 1126 case SNDCTL_DSP_GETOSPACE: 1127 /* 1128 */ 1129 if (file->f_mode & FMODE_WRITE) { 1130 if ( !write_sq.locked ) 1131 sq_setup(&write_sq) ; 1132 info.fragments = write_sq.max_active - write_sq.count; 1133 info.fragstotal = write_sq.max_active; 1134 info.fragsize = write_sq.user_frag_size; 1135 info.bytes = info.fragments * info.fragsize; 1136 if (copy_to_user((void __user *)arg, &info, sizeof(info))) 1137 return -EFAULT; 1138 return 0; 1139 } else 1140 return -EINVAL ; 1141 break ; 1142 case SNDCTL_DSP_GETCAPS: 1143 val = dmasound.mach.capabilities & 0xffffff00; 1144 return IOCTL_OUT(arg,val); 1145 1146 default: 1147 return mixer_ioctl(file, cmd, arg); 1148 } 1149 return -EINVAL; 1150 } 1151 1152 static long sq_unlocked_ioctl(struct file *file, u_int cmd, u_long arg) 1153 { 1154 int ret; 1155 1156 mutex_lock(&dmasound_core_mutex); 1157 ret = sq_ioctl(file, cmd, arg); 1158 mutex_unlock(&dmasound_core_mutex); 1159 1160 return ret; 1161 } 1162 1163 static const struct file_operations sq_fops = 1164 { 1165 .owner = THIS_MODULE, 1166 .llseek = no_llseek, 1167 .write = sq_write, 1168 .poll = sq_poll, 1169 .unlocked_ioctl = sq_unlocked_ioctl, 1170 .open = sq_open, 1171 .release = sq_release, 1172 }; 1173 1174 static int sq_init(void) 1175 { 1176 const struct file_operations *fops = &sq_fops; 1177 #ifndef MODULE 1178 int sq_unit; 1179 #endif 1180 1181 sq_unit = register_sound_dsp(fops, -1); 1182 if (sq_unit < 0) { 1183 printk(KERN_ERR "dmasound_core: couldn't register fops\n") ; 1184 return sq_unit ; 1185 } 1186 1187 write_sq_init_waitqueue(); 1188 1189 /* These parameters will be restored for every clean open() 1190 * in the case of multiple open()s (e.g. dsp0 & dsp1) they 1191 * will be set so long as the shared resources have no owner. 1192 */ 1193 1194 if (shared_resource_owner == 0) { 1195 dmasound.soft = dmasound.mach.default_soft ; 1196 dmasound.hard = dmasound.mach.default_hard ; 1197 dmasound.dsp = dmasound.mach.default_soft ; 1198 shared_resources_initialised = 0 ; 1199 } 1200 return 0 ; 1201 } 1202 1203 1204 /* 1205 * /dev/sndstat 1206 */ 1207 1208 /* we allow more space for record-enabled because there are extra output lines. 1209 the number here must include the amount we are prepared to give to the low-level 1210 driver. 1211 */ 1212 1213 #define STAT_BUFF_LEN 768 1214 1215 /* this is how much space we will allow the low-level driver to use 1216 in the stat buffer. Currently, 2 * (80 character line + <NL>). 1217 We do not police this (it is up to the ll driver to be honest). 1218 */ 1219 1220 #define LOW_LEVEL_STAT_ALLOC 162 1221 1222 static struct { 1223 int busy; 1224 char buf[STAT_BUFF_LEN]; /* state.buf should not overflow! */ 1225 int len, ptr; 1226 } state; 1227 1228 /* publish this function for use by low-level code, if required */ 1229 1230 static char *get_afmt_string(int afmt) 1231 { 1232 switch(afmt) { 1233 case AFMT_MU_LAW: 1234 return "mu-law"; 1235 break; 1236 case AFMT_A_LAW: 1237 return "A-law"; 1238 break; 1239 case AFMT_U8: 1240 return "unsigned 8 bit"; 1241 break; 1242 case AFMT_S8: 1243 return "signed 8 bit"; 1244 break; 1245 case AFMT_S16_BE: 1246 return "signed 16 bit BE"; 1247 break; 1248 case AFMT_U16_BE: 1249 return "unsigned 16 bit BE"; 1250 break; 1251 case AFMT_S16_LE: 1252 return "signed 16 bit LE"; 1253 break; 1254 case AFMT_U16_LE: 1255 return "unsigned 16 bit LE"; 1256 break; 1257 case 0: 1258 return "format not set" ; 1259 break ; 1260 default: 1261 break ; 1262 } 1263 return "ERROR: Unsupported AFMT_XXXX code" ; 1264 } 1265 1266 static int state_open(struct inode *inode, struct file *file) 1267 { 1268 char *buffer = state.buf; 1269 int len = 0; 1270 int ret; 1271 1272 mutex_lock(&dmasound_core_mutex); 1273 ret = -EBUSY; 1274 if (state.busy) 1275 goto out; 1276 1277 ret = -ENODEV; 1278 if (!try_module_get(dmasound.mach.owner)) 1279 goto out; 1280 1281 state.ptr = 0; 1282 state.busy = 1; 1283 1284 len += sprintf(buffer+len, "%sDMA sound driver rev %03d :\n", 1285 dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) + 1286 ((dmasound.mach.version>>8) & 0x0f)); 1287 len += sprintf(buffer+len, 1288 "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n", 1289 DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2, 1290 (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ; 1291 1292 /* call the low-level module to fill in any stat info. that it has 1293 if present. Maximum buffer usage is specified. 1294 */ 1295 1296 if (dmasound.mach.state_info) 1297 len += dmasound.mach.state_info(buffer+len, 1298 (size_t) LOW_LEVEL_STAT_ALLOC) ; 1299 1300 /* make usage of the state buffer as deterministic as poss. 1301 exceptional conditions could cause overrun - and this is flagged as 1302 a kernel error. 1303 */ 1304 1305 /* formats and settings */ 1306 1307 len += sprintf(buffer+len,"\t\t === Formats & settings ===\n") ; 1308 len += sprintf(buffer+len,"Parameter %20s%20s\n","soft","hard") ; 1309 len += sprintf(buffer+len,"Format :%20s%20s\n", 1310 get_afmt_string(dmasound.soft.format), 1311 get_afmt_string(dmasound.hard.format)); 1312 1313 len += sprintf(buffer+len,"Samp Rate:%14d s/sec%14d s/sec\n", 1314 dmasound.soft.speed, dmasound.hard.speed); 1315 1316 len += sprintf(buffer+len,"Channels :%20s%20s\n", 1317 dmasound.soft.stereo ? "stereo" : "mono", 1318 dmasound.hard.stereo ? "stereo" : "mono" ); 1319 1320 /* sound queue status */ 1321 1322 len += sprintf(buffer+len,"\t\t === Sound Queue status ===\n"); 1323 len += sprintf(buffer+len,"Allocated:%8s%6s\n","Buffers","Size") ; 1324 len += sprintf(buffer+len,"%9s:%8d%6d\n", 1325 "write", write_sq.numBufs, write_sq.bufSize) ; 1326 len += sprintf(buffer+len, 1327 "Current : MaxFrg FragSiz MaxAct Frnt Rear " 1328 "Cnt RrSize A B S L xruns\n") ; 1329 len += sprintf(buffer+len,"%9s:%7d%8d%7d%5d%5d%4d%7d%2d%2d%2d%2d%7d\n", 1330 "write", write_sq.max_count, write_sq.block_size, 1331 write_sq.max_active, write_sq.front, write_sq.rear, 1332 write_sq.count, write_sq.rear_size, write_sq.active, 1333 write_sq.busy, write_sq.syncing, write_sq.locked, write_sq.xruns) ; 1334 #ifdef DEBUG_DMASOUND 1335 printk("dmasound: stat buffer used %d bytes\n", len) ; 1336 #endif 1337 1338 if (len >= STAT_BUFF_LEN) 1339 printk(KERN_ERR "dmasound_core: stat buffer overflowed!\n"); 1340 1341 state.len = len; 1342 ret = 0; 1343 out: 1344 mutex_unlock(&dmasound_core_mutex); 1345 return ret; 1346 } 1347 1348 static int state_release(struct inode *inode, struct file *file) 1349 { 1350 mutex_lock(&dmasound_core_mutex); 1351 state.busy = 0; 1352 module_put(dmasound.mach.owner); 1353 mutex_unlock(&dmasound_core_mutex); 1354 return 0; 1355 } 1356 1357 static ssize_t state_read(struct file *file, char __user *buf, size_t count, 1358 loff_t *ppos) 1359 { 1360 int n = state.len - state.ptr; 1361 if (n > count) 1362 n = count; 1363 if (n <= 0) 1364 return 0; 1365 if (copy_to_user(buf, &state.buf[state.ptr], n)) 1366 return -EFAULT; 1367 state.ptr += n; 1368 return n; 1369 } 1370 1371 static const struct file_operations state_fops = { 1372 .owner = THIS_MODULE, 1373 .llseek = no_llseek, 1374 .read = state_read, 1375 .open = state_open, 1376 .release = state_release, 1377 }; 1378 1379 static int state_init(void) 1380 { 1381 #ifndef MODULE 1382 int state_unit; 1383 #endif 1384 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS); 1385 if (state_unit < 0) 1386 return state_unit ; 1387 state.busy = 0; 1388 return 0 ; 1389 } 1390 1391 1392 /* 1393 * Config & Setup 1394 * 1395 * This function is called by _one_ chipset-specific driver 1396 */ 1397 1398 int dmasound_init(void) 1399 { 1400 int res ; 1401 #ifdef MODULE 1402 if (irq_installed) 1403 return -EBUSY; 1404 #endif 1405 1406 /* Set up sound queue, /dev/audio and /dev/dsp. */ 1407 1408 /* Set default settings. */ 1409 if ((res = sq_init()) < 0) 1410 return res ; 1411 1412 /* Set up /dev/sndstat. */ 1413 if ((res = state_init()) < 0) 1414 return res ; 1415 1416 /* Set up /dev/mixer. */ 1417 mixer_init(); 1418 1419 if (!dmasound.mach.irqinit()) { 1420 printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n"); 1421 return -ENODEV; 1422 } 1423 #ifdef MODULE 1424 irq_installed = 1; 1425 #endif 1426 1427 printk(KERN_INFO "%s DMA sound driver rev %03d installed\n", 1428 dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) + 1429 ((dmasound.mach.version>>8) & 0x0f)); 1430 printk(KERN_INFO 1431 "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n", 1432 DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2, 1433 (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ; 1434 printk(KERN_INFO "Write will use %4d fragments of %7d bytes as default\n", 1435 numWriteBufs, writeBufSize) ; 1436 return 0; 1437 } 1438 1439 #ifdef MODULE 1440 1441 void dmasound_deinit(void) 1442 { 1443 if (irq_installed) { 1444 sound_silence(); 1445 dmasound.mach.irqcleanup(); 1446 irq_installed = 0; 1447 } 1448 1449 write_sq_release_buffers(); 1450 1451 if (mixer_unit >= 0) 1452 unregister_sound_mixer(mixer_unit); 1453 if (state_unit >= 0) 1454 unregister_sound_special(state_unit); 1455 if (sq_unit >= 0) 1456 unregister_sound_dsp(sq_unit); 1457 } 1458 1459 #else /* !MODULE */ 1460 1461 static int dmasound_setup(char *str) 1462 { 1463 int ints[6], size; 1464 1465 str = get_options(str, ARRAY_SIZE(ints), ints); 1466 1467 /* check the bootstrap parameter for "dmasound=" */ 1468 1469 /* FIXME: other than in the most naive of cases there is no sense in these 1470 * buffers being other than powers of two. This is not checked yet. 1471 */ 1472 1473 switch (ints[0]) { 1474 case 3: 1475 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS)) 1476 printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius); 1477 else 1478 catchRadius = ints[3]; 1479 /* fall through */ 1480 case 2: 1481 if (ints[1] < MIN_BUFFERS) 1482 printk("dmasound_setup: invalid number of buffers, using default = %d\n", numWriteBufs); 1483 else 1484 numWriteBufs = ints[1]; 1485 /* fall through */ 1486 case 1: 1487 if ((size = ints[2]) < 256) /* check for small buffer specs */ 1488 size <<= 10 ; 1489 if (size < MIN_BUFSIZE || size > MAX_BUFSIZE) 1490 printk("dmasound_setup: invalid write buffer size, using default = %d\n", writeBufSize); 1491 else 1492 writeBufSize = size; 1493 case 0: 1494 break; 1495 default: 1496 printk("dmasound_setup: invalid number of arguments\n"); 1497 return 0; 1498 } 1499 return 1; 1500 } 1501 1502 __setup("dmasound=", dmasound_setup); 1503 1504 #endif /* !MODULE */ 1505 1506 /* 1507 * Conversion tables 1508 */ 1509 1510 #ifdef HAS_8BIT_TABLES 1511 /* 8 bit mu-law */ 1512 1513 char dmasound_ulaw2dma8[] = { 1514 -126, -122, -118, -114, -110, -106, -102, -98, 1515 -94, -90, -86, -82, -78, -74, -70, -66, 1516 -63, -61, -59, -57, -55, -53, -51, -49, 1517 -47, -45, -43, -41, -39, -37, -35, -33, 1518 -31, -30, -29, -28, -27, -26, -25, -24, 1519 -23, -22, -21, -20, -19, -18, -17, -16, 1520 -16, -15, -15, -14, -14, -13, -13, -12, 1521 -12, -11, -11, -10, -10, -9, -9, -8, 1522 -8, -8, -7, -7, -7, -7, -6, -6, 1523 -6, -6, -5, -5, -5, -5, -4, -4, 1524 -4, -4, -4, -4, -3, -3, -3, -3, 1525 -3, -3, -3, -3, -2, -2, -2, -2, 1526 -2, -2, -2, -2, -2, -2, -2, -2, 1527 -1, -1, -1, -1, -1, -1, -1, -1, 1528 -1, -1, -1, -1, -1, -1, -1, -1, 1529 -1, -1, -1, -1, -1, -1, -1, 0, 1530 125, 121, 117, 113, 109, 105, 101, 97, 1531 93, 89, 85, 81, 77, 73, 69, 65, 1532 62, 60, 58, 56, 54, 52, 50, 48, 1533 46, 44, 42, 40, 38, 36, 34, 32, 1534 30, 29, 28, 27, 26, 25, 24, 23, 1535 22, 21, 20, 19, 18, 17, 16, 15, 1536 15, 14, 14, 13, 13, 12, 12, 11, 1537 11, 10, 10, 9, 9, 8, 8, 7, 1538 7, 7, 6, 6, 6, 6, 5, 5, 1539 5, 5, 4, 4, 4, 4, 3, 3, 1540 3, 3, 3, 3, 2, 2, 2, 2, 1541 2, 2, 2, 2, 1, 1, 1, 1, 1542 1, 1, 1, 1, 1, 1, 1, 1, 1543 0, 0, 0, 0, 0, 0, 0, 0, 1544 0, 0, 0, 0, 0, 0, 0, 0, 1545 0, 0, 0, 0, 0, 0, 0, 0 1546 }; 1547 1548 /* 8 bit A-law */ 1549 1550 char dmasound_alaw2dma8[] = { 1551 -22, -21, -24, -23, -18, -17, -20, -19, 1552 -30, -29, -32, -31, -26, -25, -28, -27, 1553 -11, -11, -12, -12, -9, -9, -10, -10, 1554 -15, -15, -16, -16, -13, -13, -14, -14, 1555 -86, -82, -94, -90, -70, -66, -78, -74, 1556 -118, -114, -126, -122, -102, -98, -110, -106, 1557 -43, -41, -47, -45, -35, -33, -39, -37, 1558 -59, -57, -63, -61, -51, -49, -55, -53, 1559 -2, -2, -2, -2, -2, -2, -2, -2, 1560 -2, -2, -2, -2, -2, -2, -2, -2, 1561 -1, -1, -1, -1, -1, -1, -1, -1, 1562 -1, -1, -1, -1, -1, -1, -1, -1, 1563 -6, -6, -6, -6, -5, -5, -5, -5, 1564 -8, -8, -8, -8, -7, -7, -7, -7, 1565 -3, -3, -3, -3, -3, -3, -3, -3, 1566 -4, -4, -4, -4, -4, -4, -4, -4, 1567 21, 20, 23, 22, 17, 16, 19, 18, 1568 29, 28, 31, 30, 25, 24, 27, 26, 1569 10, 10, 11, 11, 8, 8, 9, 9, 1570 14, 14, 15, 15, 12, 12, 13, 13, 1571 86, 82, 94, 90, 70, 66, 78, 74, 1572 118, 114, 126, 122, 102, 98, 110, 106, 1573 43, 41, 47, 45, 35, 33, 39, 37, 1574 59, 57, 63, 61, 51, 49, 55, 53, 1575 1, 1, 1, 1, 1, 1, 1, 1, 1576 1, 1, 1, 1, 1, 1, 1, 1, 1577 0, 0, 0, 0, 0, 0, 0, 0, 1578 0, 0, 0, 0, 0, 0, 0, 0, 1579 5, 5, 5, 5, 4, 4, 4, 4, 1580 7, 7, 7, 7, 6, 6, 6, 6, 1581 2, 2, 2, 2, 2, 2, 2, 2, 1582 3, 3, 3, 3, 3, 3, 3, 3 1583 }; 1584 #endif /* HAS_8BIT_TABLES */ 1585 1586 /* 1587 * Visible symbols for modules 1588 */ 1589 1590 EXPORT_SYMBOL(dmasound); 1591 EXPORT_SYMBOL(dmasound_init); 1592 #ifdef MODULE 1593 EXPORT_SYMBOL(dmasound_deinit); 1594 #endif 1595 EXPORT_SYMBOL(dmasound_write_sq); 1596 EXPORT_SYMBOL(dmasound_catchRadius); 1597 #ifdef HAS_8BIT_TABLES 1598 EXPORT_SYMBOL(dmasound_ulaw2dma8); 1599 EXPORT_SYMBOL(dmasound_alaw2dma8); 1600 #endif 1601