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/smp_lock.h> 185 186#include <asm/uaccess.h> 187 188#include "dmasound.h" 189 190#define DMASOUND_CORE_REVISION 1 191#define DMASOUND_CORE_EDITION 6 192 193 /* 194 * Declarations 195 */ 196 197int dmasound_catchRadius = 0; 198module_param(dmasound_catchRadius, int, 0); 199 200static unsigned int numWriteBufs = DEFAULT_N_BUFFERS; 201module_param(numWriteBufs, int, 0); 202static unsigned int writeBufSize = DEFAULT_BUFF_SIZE ; /* in bytes */ 203module_param(writeBufSize, int, 0); 204 205#ifdef HAS_RECORD 206static unsigned int numReadBufs = DEFAULT_N_BUFFERS; 207module_param(numReadBufs, int, 0); 208static unsigned int readBufSize = DEFAULT_BUFF_SIZE; /* in bytes */ 209module_param(readBufSize, int, 0); 210#endif 211 212MODULE_LICENSE("GPL"); 213 214#ifdef MODULE 215static int sq_unit = -1; 216static int mixer_unit = -1; 217static int state_unit = -1; 218static int irq_installed; 219#endif /* MODULE */ 220 221/* software implemented recording volume! */ 222uint software_input_volume = SW_INPUT_VOLUME_SCALE * SW_INPUT_VOLUME_DEFAULT; 223EXPORT_SYMBOL(software_input_volume); 224 225/* control over who can modify resources shared between play/record */ 226static mode_t shared_resource_owner; 227static int shared_resources_initialised; 228 229 /* 230 * Mid level stuff 231 */ 232 233struct sound_settings dmasound = { .lock = SPIN_LOCK_UNLOCKED }; 234 235static inline void sound_silence(void) 236{ 237 dmasound.mach.silence(); /* _MUST_ stop DMA */ 238} 239 240static inline int sound_set_format(int format) 241{ 242 return dmasound.mach.setFormat(format); 243} 244 245 246static int sound_set_speed(int speed) 247{ 248 if (speed < 0) 249 return dmasound.soft.speed; 250 251 /* trap out-of-range speed settings. 252 at present we allow (arbitrarily) low rates - using soft 253 up-conversion - but we can't allow > max because there is 254 no soft down-conversion. 255 */ 256 if (dmasound.mach.max_dsp_speed && 257 (speed > dmasound.mach.max_dsp_speed)) 258 speed = dmasound.mach.max_dsp_speed ; 259 260 dmasound.soft.speed = speed; 261 262 if (dmasound.minDev == SND_DEV_DSP) 263 dmasound.dsp.speed = dmasound.soft.speed; 264 265 return dmasound.soft.speed; 266} 267 268static int sound_set_stereo(int stereo) 269{ 270 if (stereo < 0) 271 return dmasound.soft.stereo; 272 273 stereo = !!stereo; /* should be 0 or 1 now */ 274 275 dmasound.soft.stereo = stereo; 276 if (dmasound.minDev == SND_DEV_DSP) 277 dmasound.dsp.stereo = stereo; 278 279 return stereo; 280} 281 282static ssize_t sound_copy_translate(TRANS *trans, const u_char __user *userPtr, 283 size_t userCount, u_char frame[], 284 ssize_t *frameUsed, ssize_t frameLeft) 285{ 286 ssize_t (*ct_func)(const u_char __user *, size_t, u_char *, ssize_t *, ssize_t); 287 288 switch (dmasound.soft.format) { 289 case AFMT_MU_LAW: 290 ct_func = trans->ct_ulaw; 291 break; 292 case AFMT_A_LAW: 293 ct_func = trans->ct_alaw; 294 break; 295 case AFMT_S8: 296 ct_func = trans->ct_s8; 297 break; 298 case AFMT_U8: 299 ct_func = trans->ct_u8; 300 break; 301 case AFMT_S16_BE: 302 ct_func = trans->ct_s16be; 303 break; 304 case AFMT_U16_BE: 305 ct_func = trans->ct_u16be; 306 break; 307 case AFMT_S16_LE: 308 ct_func = trans->ct_s16le; 309 break; 310 case AFMT_U16_LE: 311 ct_func = trans->ct_u16le; 312 break; 313 default: 314 return 0; 315 } 316 /* if the user has requested a non-existent translation don't try 317 to call it but just return 0 bytes moved 318 */ 319 if (ct_func) 320 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft); 321 return 0; 322} 323 324 /* 325 * /dev/mixer abstraction 326 */ 327 328static struct { 329 int busy; 330 int modify_counter; 331} mixer; 332 333static int mixer_open(struct inode *inode, struct file *file) 334{ 335 if (!try_module_get(dmasound.mach.owner)) 336 return -ENODEV; 337 mixer.busy = 1; 338 return 0; 339} 340 341static int mixer_release(struct inode *inode, struct file *file) 342{ 343 lock_kernel(); 344 mixer.busy = 0; 345 module_put(dmasound.mach.owner); 346 unlock_kernel(); 347 return 0; 348} 349static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd, 350 u_long arg) 351{ 352 if (_SIOC_DIR(cmd) & _SIOC_WRITE) 353 mixer.modify_counter++; 354 switch (cmd) { 355 case OSS_GETVERSION: 356 return IOCTL_OUT(arg, SOUND_VERSION); 357 case SOUND_MIXER_INFO: 358 { 359 mixer_info info; 360 memset(&info, 0, sizeof(info)); 361 strlcpy(info.id, dmasound.mach.name2, sizeof(info.id)); 362 strlcpy(info.name, dmasound.mach.name2, sizeof(info.name)); 363 info.modify_counter = mixer.modify_counter; 364 if (copy_to_user((void __user *)arg, &info, sizeof(info))) 365 return -EFAULT; 366 return 0; 367 } 368 } 369 if (dmasound.mach.mixer_ioctl) 370 return dmasound.mach.mixer_ioctl(cmd, arg); 371 return -EINVAL; 372} 373 374static const struct file_operations mixer_fops = 375{ 376 .owner = THIS_MODULE, 377 .llseek = no_llseek, 378 .ioctl = mixer_ioctl, 379 .open = mixer_open, 380 .release = mixer_release, 381}; 382 383static void mixer_init(void) 384{ 385#ifndef MODULE 386 int mixer_unit; 387#endif 388 mixer_unit = register_sound_mixer(&mixer_fops, -1); 389 if (mixer_unit < 0) 390 return; 391 392 mixer.busy = 0; 393 dmasound.treble = 0; 394 dmasound.bass = 0; 395 if (dmasound.mach.mixer_init) 396 dmasound.mach.mixer_init(); 397} 398 399 400 /* 401 * Sound queue stuff, the heart of the driver 402 */ 403 404struct sound_queue dmasound_write_sq; 405static void sq_reset_output(void) ; 406#ifdef HAS_RECORD 407struct sound_queue dmasound_read_sq; 408static void sq_reset_input(void) ; 409#endif 410 411static int sq_allocate_buffers(struct sound_queue *sq, int num, int size) 412{ 413 int i; 414 415 if (sq->buffers) 416 return 0; 417 sq->numBufs = num; 418 sq->bufSize = size; 419 sq->buffers = kmalloc (num * sizeof(char *), GFP_KERNEL); 420 if (!sq->buffers) 421 return -ENOMEM; 422 for (i = 0; i < num; i++) { 423 sq->buffers[i] = dmasound.mach.dma_alloc(size, GFP_KERNEL); 424 if (!sq->buffers[i]) { 425 while (i--) 426 dmasound.mach.dma_free(sq->buffers[i], size); 427 kfree(sq->buffers); 428 sq->buffers = NULL; 429 return -ENOMEM; 430 } 431 } 432 return 0; 433} 434 435static void sq_release_buffers(struct sound_queue *sq) 436{ 437 int i; 438 439 if (sq->buffers) { 440 for (i = 0; i < sq->numBufs; i++) 441 dmasound.mach.dma_free(sq->buffers[i], sq->bufSize); 442 kfree(sq->buffers); 443 sq->buffers = NULL; 444 } 445} 446 447 448static int sq_setup(struct sound_queue *sq) 449{ 450 int (*setup_func)(void) = NULL; 451 int hard_frame ; 452 453 if (sq->locked) { /* are we already set? - and not changeable */ 454#ifdef DEBUG_DMASOUND 455printk("dmasound_core: tried to sq_setup a locked queue\n") ; 456#endif 457 return -EINVAL ; 458 } 459 sq->locked = 1 ; /* don't think we have a race prob. here _check_ */ 460 461 /* make sure that the parameters are set up 462 This should have been done already... 463 */ 464 465 dmasound.mach.init(); 466 467 /* OK. If the user has set fragment parameters explicitly, then we 468 should leave them alone... as long as they are valid. 469 Invalid user fragment params can occur if we allow the whole buffer 470 to be used when the user requests the fragments sizes (with no soft 471 x-lation) and then the user subsequently sets a soft x-lation that 472 requires increased internal buffering. 473 474 Othwerwise (if the user did not set them) OSS says that we should 475 select frag params on the basis of 0.5 s output & 0.1 s input 476 latency. (TODO. For now we will copy in the defaults.) 477 */ 478 479 if (sq->user_frags <= 0) { 480 sq->max_count = sq->numBufs ; 481 sq->max_active = sq->numBufs ; 482 sq->block_size = sq->bufSize; 483 /* set up the user info */ 484 sq->user_frags = sq->numBufs ; 485 sq->user_frag_size = sq->bufSize ; 486 sq->user_frag_size *= 487 (dmasound.soft.size * (dmasound.soft.stereo+1) ) ; 488 sq->user_frag_size /= 489 (dmasound.hard.size * (dmasound.hard.stereo+1) ) ; 490 } else { 491 /* work out requested block size */ 492 sq->block_size = sq->user_frag_size ; 493 sq->block_size *= 494 (dmasound.hard.size * (dmasound.hard.stereo+1) ) ; 495 sq->block_size /= 496 (dmasound.soft.size * (dmasound.soft.stereo+1) ) ; 497 /* the user wants to write frag-size chunks */ 498 sq->block_size *= dmasound.hard.speed ; 499 sq->block_size /= dmasound.soft.speed ; 500 /* this only works for size values which are powers of 2 */ 501 hard_frame = 502 (dmasound.hard.size * (dmasound.hard.stereo+1))/8 ; 503 sq->block_size += (hard_frame - 1) ; 504 sq->block_size &= ~(hard_frame - 1) ; /* make sure we are aligned */ 505 /* let's just check for obvious mistakes */ 506 if ( sq->block_size <= 0 || sq->block_size > sq->bufSize) { 507#ifdef DEBUG_DMASOUND 508printk("dmasound_core: invalid frag size (user set %d)\n", sq->user_frag_size) ; 509#endif 510 sq->block_size = sq->bufSize ; 511 } 512 if ( sq->user_frags <= sq->numBufs ) { 513 sq->max_count = sq->user_frags ; 514 /* if user has set max_active - then use it */ 515 sq->max_active = (sq->max_active <= sq->max_count) ? 516 sq->max_active : sq->max_count ; 517 } else { 518#ifdef DEBUG_DMASOUND 519printk("dmasound_core: invalid frag count (user set %d)\n", sq->user_frags) ; 520#endif 521 sq->max_count = 522 sq->max_active = sq->numBufs ; 523 } 524 } 525 sq->front = sq->count = sq->rear_size = 0; 526 sq->syncing = 0; 527 sq->active = 0; 528 529 if (sq == &write_sq) { 530 sq->rear = -1; 531 setup_func = dmasound.mach.write_sq_setup; 532 } 533#ifdef HAS_RECORD 534 else { 535 sq->rear = 0; 536 setup_func = dmasound.mach.read_sq_setup; 537 } 538#endif 539 if (setup_func) 540 return setup_func(); 541 return 0 ; 542} 543 544static inline void sq_play(void) 545{ 546 dmasound.mach.play(); 547} 548 549static ssize_t sq_write(struct file *file, const char __user *src, size_t uLeft, 550 loff_t *ppos) 551{ 552 ssize_t uWritten = 0; 553 u_char *dest; 554 ssize_t uUsed = 0, bUsed, bLeft; 555 unsigned long flags ; 556 557 /* ++TeSche: Is something like this necessary? 558 * Hey, that's an honest question! Or does any other part of the 559 * filesystem already checks this situation? I really don't know. 560 */ 561 if (uLeft == 0) 562 return 0; 563 564 /* implement any changes we have made to the soft/hard params. 565 this is not satisfactory really, all we have done up to now is to 566 say what we would like - there hasn't been any real checking of capability 567 */ 568 569 if (shared_resources_initialised == 0) { 570 dmasound.mach.init() ; 571 shared_resources_initialised = 1 ; 572 } 573 574 /* set up the sq if it is not already done. This may seem a dumb place 575 to do it - but it is what OSS requires. It means that write() can 576 return memory allocation errors. To avoid this possibility use the 577 GETBLKSIZE or GETOSPACE ioctls (after you've fiddled with all the 578 params you want to change) - these ioctls also force the setup. 579 */ 580 581 if (write_sq.locked == 0) { 582 if ((uWritten = sq_setup(&write_sq)) < 0) return uWritten ; 583 uWritten = 0 ; 584 } 585 586 /* The interrupt doesn't start to play the last, incomplete frame. 587 * Thus we can append to it without disabling the interrupts! (Note 588 * also that write_sq.rear isn't affected by the interrupt.) 589 */ 590 591 /* as of 1.6 this behaviour changes if SNDCTL_DSP_POST has been issued: 592 this will mimic the behaviour of syncing and allow the sq_play() to 593 queue a partial fragment. Since sq_play() may/will be called from 594 the IRQ handler - at least on Pmac we have to deal with it. 595 The strategy - possibly not optimum - is to kill _POST status if we 596 get here. This seems, at least, reasonable - in the sense that POST 597 is supposed to indicate that we might not write before the queue 598 is drained - and if we get here in time then it does not apply. 599 */ 600 601 spin_lock_irqsave(&dmasound.lock, flags); 602 write_sq.syncing &= ~2 ; /* take out POST status */ 603 spin_unlock_irqrestore(&dmasound.lock, flags); 604 605 if (write_sq.count > 0 && 606 (bLeft = write_sq.block_size-write_sq.rear_size) > 0) { 607 dest = write_sq.buffers[write_sq.rear]; 608 bUsed = write_sq.rear_size; 609 uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft, 610 dest, &bUsed, bLeft); 611 if (uUsed <= 0) 612 return uUsed; 613 src += uUsed; 614 uWritten += uUsed; 615 uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */ 616 write_sq.rear_size = bUsed; 617 } 618 619 while (uLeft) { 620 while (write_sq.count >= write_sq.max_active) { 621 sq_play(); 622 if (write_sq.open_mode & O_NONBLOCK) 623 return uWritten > 0 ? uWritten : -EAGAIN; 624 SLEEP(write_sq.action_queue); 625 if (signal_pending(current)) 626 return uWritten > 0 ? uWritten : -EINTR; 627 } 628 629 /* Here, we can avoid disabling the interrupt by first 630 * copying and translating the data, and then updating 631 * the write_sq variables. Until this is done, the interrupt 632 * won't see the new frame and we can work on it 633 * undisturbed. 634 */ 635 636 dest = write_sq.buffers[(write_sq.rear+1) % write_sq.max_count]; 637 bUsed = 0; 638 bLeft = write_sq.block_size; 639 uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft, 640 dest, &bUsed, bLeft); 641 if (uUsed <= 0) 642 break; 643 src += uUsed; 644 uWritten += uUsed; 645 uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */ 646 if (bUsed) { 647 write_sq.rear = (write_sq.rear+1) % write_sq.max_count; 648 write_sq.rear_size = bUsed; 649 write_sq.count++; 650 } 651 } /* uUsed may have been 0 */ 652 653 sq_play(); 654 655 return uUsed < 0? uUsed: uWritten; 656} 657 658static unsigned int sq_poll(struct file *file, struct poll_table_struct *wait) 659{ 660 unsigned int mask = 0; 661 int retVal; 662 663 if (write_sq.locked == 0) { 664 if ((retVal = sq_setup(&write_sq)) < 0) 665 return retVal; 666 return 0; 667 } 668 if (file->f_mode & FMODE_WRITE ) 669 poll_wait(file, &write_sq.action_queue, wait); 670#ifdef HAS_RECORD 671 if (file->f_mode & FMODE_READ) 672 poll_wait(file, &read_sq.action_queue, wait); 673 if (file->f_mode & FMODE_READ) 674 if (read_sq.block_size - read_sq.rear_size > 0) 675 mask |= POLLIN | POLLRDNORM; 676#endif 677 if (file->f_mode & FMODE_WRITE) 678 if (write_sq.count < write_sq.max_active || write_sq.block_size - write_sq.rear_size > 0) 679 mask |= POLLOUT | POLLWRNORM; 680 return mask; 681 682} 683 684#ifdef HAS_RECORD 685 /* 686 * Here is how the values are used for reading. 687 * The value 'active' simply indicates the DMA is running. This is done 688 * so the driver semantics are DMA starts when the first read is posted. 689 * The value 'front' indicates the buffer we should next send to the user. 690 * The value 'rear' indicates the buffer the DMA is currently filling. 691 * When 'front' == 'rear' the buffer "ring" is empty (we always have an 692 * empty available). The 'rear_size' is used to track partial offsets 693 * into the buffer we are currently returning to the user. 694 695 * This level (> [1.5]) doesn't care what strategy the LL driver uses with 696 * DMA on over-run. It can leave it running (and keep active == 1) or it 697 * can kill it and set active == 0 in which case this routine will spot 698 * it and restart the DMA. 699 */ 700 701static ssize_t sq_read(struct file *file, char __user *dst, size_t uLeft, 702 loff_t *ppos) 703{ 704 705 ssize_t uRead, bLeft, bUsed, uUsed; 706 707 if (uLeft == 0) 708 return 0; 709 710 /* cater for the compatibility mode - record compiled in but no LL */ 711 if (dmasound.mach.record == NULL) 712 return -EINVAL ; 713 714 /* see comment in sq_write() 715 */ 716 717 if( shared_resources_initialised == 0) { 718 dmasound.mach.init() ; 719 shared_resources_initialised = 1 ; 720 } 721 722 /* set up the sq if it is not already done. see comments in sq_write(). 723 */ 724 725 if (read_sq.locked == 0) { 726 if ((uRead = sq_setup(&read_sq)) < 0) 727 return uRead ; 728 } 729 730 uRead = 0; 731 732 /* Move what the user requests, depending upon other options. 733 */ 734 while (uLeft > 0) { 735 736 /* we happened to get behind and the LL driver killed DMA 737 then we should set it going again. This also sets it 738 going the first time through. 739 */ 740 if ( !read_sq.active ) 741 dmasound.mach.record(); 742 743 /* When front == rear, the DMA is not done yet. 744 */ 745 while (read_sq.front == read_sq.rear) { 746 if (read_sq.open_mode & O_NONBLOCK) { 747 return uRead > 0 ? uRead : -EAGAIN; 748 } 749 SLEEP(read_sq.action_queue); 750 if (signal_pending(current)) 751 return uRead > 0 ? uRead : -EINTR; 752 } 753 754 /* The amount we move is either what is left in the 755 * current buffer or what the user wants. 756 */ 757 bLeft = read_sq.block_size - read_sq.rear_size; 758 bUsed = read_sq.rear_size; 759 uUsed = sound_copy_translate(dmasound.trans_read, dst, uLeft, 760 read_sq.buffers[read_sq.front], 761 &bUsed, bLeft); 762 if (uUsed <= 0) 763 return uUsed; 764 dst += uUsed; 765 uRead += uUsed; 766 uLeft -= uUsed; 767 read_sq.rear_size += bUsed; 768 if (read_sq.rear_size >= read_sq.block_size) { 769 read_sq.rear_size = 0; 770 read_sq.front++; 771 if (read_sq.front >= read_sq.max_active) 772 read_sq.front = 0; 773 } 774 } 775 return uRead; 776} 777#endif /* HAS_RECORD */ 778 779static inline void sq_init_waitqueue(struct sound_queue *sq) 780{ 781 init_waitqueue_head(&sq->action_queue); 782 init_waitqueue_head(&sq->open_queue); 783 init_waitqueue_head(&sq->sync_queue); 784 sq->busy = 0; 785} 786 787 788static int sq_open2(struct sound_queue *sq, struct file *file, mode_t mode, 789 int numbufs, int bufsize) 790{ 791 int rc = 0; 792 793 if (file->f_mode & mode) { 794 if (sq->busy) { 795 /* OSS manual says we will return EBUSY regardless 796 of O_NOBLOCK. 797 */ 798 return -EBUSY ; 799 } 800 sq->busy = 1; /* Let's play spot-the-race-condition */ 801 802 /* allocate the default number & size of buffers. 803 (i.e. specified in _setup() or as module params) 804 can't be changed at the moment - but _could_ be perhaps 805 in the setfragments ioctl. 806 */ 807 if (( rc = sq_allocate_buffers(sq, numbufs, bufsize))) { 808 sq->busy = 0 ; 809 return rc; 810 } 811 812 sq->open_mode = file->f_mode; 813 } 814 return rc; 815} 816 817#define write_sq_init_waitqueue() sq_init_waitqueue(&write_sq) 818#define write_sq_release_buffers() sq_release_buffers(&write_sq) 819#define write_sq_open(file) \ 820 sq_open2(&write_sq, file, FMODE_WRITE, numWriteBufs, writeBufSize ) 821 822#ifdef HAS_RECORD 823#define read_sq_init_waitqueue() sq_init_waitqueue(&read_sq) 824#define read_sq_release_buffers() sq_release_buffers(&read_sq) 825#define read_sq_open(file) \ 826 sq_open2(&read_sq, file, FMODE_READ, numReadBufs, readBufSize ) 827#else 828#define read_sq_init_waitqueue() do {} while (0) 829#define read_sq_release_buffers() do {} while (0) 830#define sq_reset_input() do {} while (0) 831#endif 832 833static int sq_open(struct inode *inode, struct file *file) 834{ 835 int rc; 836 837 if (!try_module_get(dmasound.mach.owner)) 838 return -ENODEV; 839 840 rc = write_sq_open(file); /* checks the f_mode */ 841 if (rc) 842 goto out; 843#ifdef HAS_RECORD 844 if (dmasound.mach.record) { 845 rc = read_sq_open(file); /* checks the f_mode */ 846 if (rc) 847 goto out; 848 } else { /* no record function installed; in compat mode */ 849 if (file->f_mode & FMODE_READ) { 850 /* TODO: if O_RDWR, release any resources grabbed by write part */ 851 rc = -ENXIO; 852 goto out; 853 } 854 } 855#else /* !HAS_RECORD */ 856 if (file->f_mode & FMODE_READ) { 857 /* TODO: if O_RDWR, release any resources grabbed by write part */ 858 rc = -ENXIO ; /* I think this is what is required by open(2) */ 859 goto out; 860 } 861#endif /* HAS_RECORD */ 862 863 if (dmasound.mach.sq_open) 864 dmasound.mach.sq_open(file->f_mode); 865 866 /* CHECK whether this is sensible - in the case that dsp0 could be opened 867 O_RDONLY and dsp1 could be opened O_WRONLY 868 */ 869 870 dmasound.minDev = iminor(inode) & 0x0f; 871 872 /* OK. - we should make some attempt at consistency. At least the H'ware 873 options should be set with a valid mode. We will make it that the LL 874 driver must supply defaults for hard & soft params. 875 */ 876 877 if (shared_resource_owner == 0) { 878 /* you can make this AFMT_U8/mono/8K if you want to mimic old 879 OSS behaviour - while we still have soft translations ;-) */ 880 dmasound.soft = dmasound.mach.default_soft ; 881 dmasound.dsp = dmasound.mach.default_soft ; 882 dmasound.hard = dmasound.mach.default_hard ; 883 } 884 885#ifndef DMASOUND_STRICT_OSS_COMPLIANCE 886 /* none of the current LL drivers can actually do this "native" at the moment 887 OSS does not really require us to supply /dev/audio if we can't do it. 888 */ 889 if (dmasound.minDev == SND_DEV_AUDIO) { 890 sound_set_speed(8000); 891 sound_set_stereo(0); 892 sound_set_format(AFMT_MU_LAW); 893 } 894#endif 895 896 return 0; 897 out: 898 module_put(dmasound.mach.owner); 899 return rc; 900} 901 902static void sq_reset_output(void) 903{ 904 sound_silence(); /* this _must_ stop DMA, we might be about to lose the buffers */ 905 write_sq.active = 0; 906 write_sq.count = 0; 907 write_sq.rear_size = 0; 908 /* write_sq.front = (write_sq.rear+1) % write_sq.max_count;*/ 909 write_sq.front = 0 ; 910 write_sq.rear = -1 ; /* same as for set-up */ 911 912 /* OK - we can unlock the parameters and fragment settings */ 913 write_sq.locked = 0 ; 914 write_sq.user_frags = 0 ; 915 write_sq.user_frag_size = 0 ; 916} 917 918#ifdef HAS_RECORD 919 920static void sq_reset_input(void) 921{ 922 if (dmasound.mach.record && read_sq.active) { 923 if (dmasound.mach.abort_read) { /* this routine must really be present */ 924 read_sq.syncing = 1 ; 925 /* this can use the read_sq.sync_queue to sleep if 926 necessary - it should not return until DMA 927 is really stopped - because we might deallocate 928 the buffers as the next action... 929 */ 930 dmasound.mach.abort_read() ; 931 } else { 932 printk(KERN_ERR 933 "dmasound_core: %s has no abort_read()!! all bets are off\n", 934 dmasound.mach.name) ; 935 } 936 } 937 read_sq.syncing = 938 read_sq.active = 939 read_sq.front = 940 read_sq.count = 941 read_sq.rear = 0 ; 942 943 /* OK - we can unlock the parameters and fragment settings */ 944 read_sq.locked = 0 ; 945 read_sq.user_frags = 0 ; 946 read_sq.user_frag_size = 0 ; 947} 948 949#endif 950 951static void sq_reset(void) 952{ 953 sq_reset_output() ; 954 sq_reset_input() ; 955 /* we could consider resetting the shared_resources_owner here... but I 956 think it is probably still rather non-obvious to application writer 957 */ 958 959 /* we release everything else though */ 960 shared_resources_initialised = 0 ; 961} 962 963static int sq_fsync(struct file *filp, struct dentry *dentry) 964{ 965 int rc = 0; 966 int timeout = 5; 967 968 write_sq.syncing |= 1; 969 sq_play(); /* there may be an incomplete frame waiting */ 970 971 while (write_sq.active) { 972 SLEEP(write_sq.sync_queue); 973 if (signal_pending(current)) { 974 /* While waiting for audio output to drain, an 975 * interrupt occurred. Stop audio output immediately 976 * and clear the queue. */ 977 sq_reset_output(); 978 rc = -EINTR; 979 break; 980 } 981 if (!--timeout) { 982 printk(KERN_WARNING "dmasound: Timeout draining output\n"); 983 sq_reset_output(); 984 rc = -EIO; 985 break; 986 } 987 } 988 989 /* flag no sync regardless of whether we had a DSP_POST or not */ 990 write_sq.syncing = 0 ; 991 return rc; 992} 993 994static int sq_release(struct inode *inode, struct file *file) 995{ 996 int rc = 0; 997 998 lock_kernel(); 999 1000#ifdef HAS_RECORD 1001 /* probably best to do the read side first - so that time taken to do it 1002 overlaps with playing any remaining output samples. 1003 */ 1004 if (file->f_mode & FMODE_READ) { 1005 sq_reset_input() ; /* make sure dma is stopped and all is quiet */ 1006 read_sq_release_buffers(); 1007 read_sq.busy = 0; 1008 } 1009#endif 1010 1011 if (file->f_mode & FMODE_WRITE) { 1012 if (write_sq.busy) 1013 rc = sq_fsync(file, file->f_path.dentry); 1014 1015 sq_reset_output() ; /* make sure dma is stopped and all is quiet */ 1016 write_sq_release_buffers(); 1017 write_sq.busy = 0; 1018 } 1019 1020 if (file->f_mode & shared_resource_owner) { /* it's us that has them */ 1021 shared_resource_owner = 0 ; 1022 shared_resources_initialised = 0 ; 1023 dmasound.hard = dmasound.mach.default_hard ; 1024 } 1025 1026 module_put(dmasound.mach.owner); 1027 1028 1029 unlock_kernel(); 1030 1031 return rc; 1032} 1033 1034/* here we see if we have a right to modify format, channels, size and so on 1035 if no-one else has claimed it already then we do... 1036 1037 TODO: We might change this to mask O_RDWR such that only one or the other channel 1038 is the owner - if we have problems. 1039*/ 1040 1041static int shared_resources_are_mine(mode_t md) 1042{ 1043 if (shared_resource_owner) 1044 return (shared_resource_owner & md ) ; 1045 else { 1046 shared_resource_owner = md ; 1047 return 1 ; 1048 } 1049} 1050 1051/* if either queue is locked we must deny the right to change shared params 1052*/ 1053 1054static int queues_are_quiescent(void) 1055{ 1056#ifdef HAS_RECORD 1057 if (dmasound.mach.record) 1058 if (read_sq.locked) 1059 return 0 ; 1060#endif 1061 if (write_sq.locked) 1062 return 0 ; 1063 return 1 ; 1064} 1065 1066/* check and set a queue's fragments per user's wishes... 1067 we will check against the pre-defined literals and the actual sizes. 1068 This is a bit fraught - because soft translations can mess with our 1069 buffer requirements *after* this call - OSS says "call setfrags first" 1070*/ 1071 1072/* It is possible to replace all the -EINVAL returns with an override that 1073 just puts the allowable value in. This may be what many OSS apps require 1074*/ 1075 1076static int set_queue_frags(struct sound_queue *sq, int bufs, int size) 1077{ 1078 if (sq->locked) { 1079#ifdef DEBUG_DMASOUND 1080printk("dmasound_core: tried to set_queue_frags on a locked queue\n") ; 1081#endif 1082 return -EINVAL ; 1083 } 1084 1085 if ((size < MIN_FRAG_SIZE) || (size > MAX_FRAG_SIZE)) 1086 return -EINVAL ; 1087 size = (1<<size) ; /* now in bytes */ 1088 if (size > sq->bufSize) 1089 return -EINVAL ; /* this might still not work */ 1090 1091 if (bufs <= 0) 1092 return -EINVAL ; 1093 if (bufs > sq->numBufs) /* the user is allowed say "don't care" with 0x7fff */ 1094 bufs = sq->numBufs ; 1095 1096 /* there is, currently, no way to specify max_active separately 1097 from max_count. This could be a LL driver issue - I guess 1098 if there is a requirement for these values to be different then 1099 we will have to pass that info. up to this level. 1100 */ 1101 sq->user_frags = 1102 sq->max_active = bufs ; 1103 sq->user_frag_size = size ; 1104 1105 return 0 ; 1106} 1107 1108static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd, 1109 u_long arg) 1110{ 1111 int val, result; 1112 u_long fmt; 1113 int data; 1114 int size, nbufs; 1115 audio_buf_info info; 1116 1117 switch (cmd) { 1118 case SNDCTL_DSP_RESET: 1119 sq_reset(); 1120 return 0; 1121 break ; 1122 case SNDCTL_DSP_GETFMTS: 1123 fmt = dmasound.mach.hardware_afmts ; /* this is what OSS says.. */ 1124 return IOCTL_OUT(arg, fmt); 1125 break ; 1126 case SNDCTL_DSP_GETBLKSIZE: 1127 /* this should tell the caller about bytes that the app can 1128 read/write - the app doesn't care about our internal buffers. 1129 We force sq_setup() here as per OSS 1.1 (which should 1130 compute the values necessary). 1131 Since there is no mechanism to specify read/write separately, for 1132 fds opened O_RDWR, the write_sq values will, arbitrarily, overwrite 1133 the read_sq ones. 1134 */ 1135 size = 0 ; 1136#ifdef HAS_RECORD 1137 if (dmasound.mach.record && (file->f_mode & FMODE_READ)) { 1138 if ( !read_sq.locked ) 1139 sq_setup(&read_sq) ; /* set params */ 1140 size = read_sq.user_frag_size ; 1141 } 1142#endif 1143 if (file->f_mode & FMODE_WRITE) { 1144 if ( !write_sq.locked ) 1145 sq_setup(&write_sq) ; 1146 size = write_sq.user_frag_size ; 1147 } 1148 return IOCTL_OUT(arg, size); 1149 break ; 1150 case SNDCTL_DSP_POST: 1151 /* all we are going to do is to tell the LL that any 1152 partial frags can be queued for output. 1153 The LL will have to clear this flag when last output 1154 is queued. 1155 */ 1156 write_sq.syncing |= 0x2 ; 1157 sq_play() ; 1158 return 0 ; 1159 case SNDCTL_DSP_SYNC: 1160 /* This call, effectively, has the same behaviour as SNDCTL_DSP_RESET 1161 except that it waits for output to finish before resetting 1162 everything - read, however, is killed imediately. 1163 */ 1164 result = 0 ; 1165 if ((file->f_mode & FMODE_READ) && dmasound.mach.record) 1166 sq_reset_input() ; 1167 if (file->f_mode & FMODE_WRITE) { 1168 result = sq_fsync(file, file->f_path.dentry); 1169 sq_reset_output() ; 1170 } 1171 /* if we are the shared resource owner then release them */ 1172 if (file->f_mode & shared_resource_owner) 1173 shared_resources_initialised = 0 ; 1174 return result ; 1175 break ; 1176 case SOUND_PCM_READ_RATE: 1177 return IOCTL_OUT(arg, dmasound.soft.speed); 1178 case SNDCTL_DSP_SPEED: 1179 /* changing this on the fly will have weird effects on the sound. 1180 Where there are rate conversions implemented in soft form - it 1181 will cause the _ctx_xxx() functions to be substituted. 1182 However, there doesn't appear to be any reason to dis-allow it from 1183 a driver pov. 1184 */ 1185 if (shared_resources_are_mine(file->f_mode)) { 1186 IOCTL_IN(arg, data); 1187 data = sound_set_speed(data) ; 1188 shared_resources_initialised = 0 ; 1189 return IOCTL_OUT(arg, data); 1190 } else 1191 return -EINVAL ; 1192 break ; 1193 /* OSS says these next 4 actions are undefined when the device is 1194 busy/active - we will just return -EINVAL. 1195 To be allowed to change one - (a) you have to own the right 1196 (b) the queue(s) must be quiescent 1197 */ 1198 case SNDCTL_DSP_STEREO: 1199 if (shared_resources_are_mine(file->f_mode) && 1200 queues_are_quiescent()) { 1201 IOCTL_IN(arg, data); 1202 shared_resources_initialised = 0 ; 1203 return IOCTL_OUT(arg, sound_set_stereo(data)); 1204 } else 1205 return -EINVAL ; 1206 break ; 1207 case SOUND_PCM_WRITE_CHANNELS: 1208 if (shared_resources_are_mine(file->f_mode) && 1209 queues_are_quiescent()) { 1210 IOCTL_IN(arg, data); 1211 /* the user might ask for 20 channels, we will return 1 or 2 */ 1212 shared_resources_initialised = 0 ; 1213 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1); 1214 } else 1215 return -EINVAL ; 1216 break ; 1217 case SNDCTL_DSP_SETFMT: 1218 if (shared_resources_are_mine(file->f_mode) && 1219 queues_are_quiescent()) { 1220 int format; 1221 IOCTL_IN(arg, data); 1222 shared_resources_initialised = 0 ; 1223 format = sound_set_format(data); 1224 result = IOCTL_OUT(arg, format); 1225 if (result < 0) 1226 return result; 1227 if (format != data && data != AFMT_QUERY) 1228 return -EINVAL; 1229 return 0; 1230 } else 1231 return -EINVAL ; 1232 case SNDCTL_DSP_SUBDIVIDE: 1233 return -EINVAL ; 1234 case SNDCTL_DSP_SETFRAGMENT: 1235 /* we can do this independently for the two queues - with the 1236 proviso that for fds opened O_RDWR we cannot separate the 1237 actions and both queues will be set per the last call. 1238 NOTE: this does *NOT* actually set the queue up - merely 1239 registers our intentions. 1240 */ 1241 IOCTL_IN(arg, data); 1242 result = 0 ; 1243 nbufs = (data >> 16) & 0x7fff ; /* 0x7fff is 'use maximum' */ 1244 size = data & 0xffff; 1245#ifdef HAS_RECORD 1246 if ((file->f_mode & FMODE_READ) && dmasound.mach.record) { 1247 result = set_queue_frags(&read_sq, nbufs, size) ; 1248 if (result) 1249 return result ; 1250 } 1251#endif 1252 if (file->f_mode & FMODE_WRITE) { 1253 result = set_queue_frags(&write_sq, nbufs, size) ; 1254 if (result) 1255 return result ; 1256 } 1257 /* NOTE: this return value is irrelevant - OSS specifically says that 1258 the value is 'random' and that the user _must_ check the actual 1259 frags values using SNDCTL_DSP_GETBLKSIZE or similar */ 1260 return IOCTL_OUT(arg, data); 1261 break ; 1262 case SNDCTL_DSP_GETOSPACE: 1263 /* 1264 */ 1265 if (file->f_mode & FMODE_WRITE) { 1266 if ( !write_sq.locked ) 1267 sq_setup(&write_sq) ; 1268 info.fragments = write_sq.max_active - write_sq.count; 1269 info.fragstotal = write_sq.max_active; 1270 info.fragsize = write_sq.user_frag_size; 1271 info.bytes = info.fragments * info.fragsize; 1272 if (copy_to_user((void __user *)arg, &info, sizeof(info))) 1273 return -EFAULT; 1274 return 0; 1275 } else 1276 return -EINVAL ; 1277 break ; 1278 case SNDCTL_DSP_GETCAPS: 1279 val = dmasound.mach.capabilities & 0xffffff00; 1280 return IOCTL_OUT(arg,val); 1281 1282 default: 1283 return mixer_ioctl(inode, file, cmd, arg); 1284 } 1285 return -EINVAL; 1286} 1287 1288static const struct file_operations sq_fops = 1289{ 1290 .owner = THIS_MODULE, 1291 .llseek = no_llseek, 1292 .write = sq_write, 1293 .poll = sq_poll, 1294 .ioctl = sq_ioctl, 1295 .open = sq_open, 1296 .release = sq_release, 1297}; 1298 1299#ifdef HAS_RECORD 1300static const struct file_operations sq_fops_record = 1301{ 1302 .owner = THIS_MODULE, 1303 .llseek = no_llseek, 1304 .write = sq_write, 1305 .poll = sq_poll, 1306 .ioctl = sq_ioctl, 1307 .open = sq_open, 1308 .release = sq_release, 1309 .read = sq_read, 1310}; 1311#endif 1312 1313static int sq_init(void) 1314{ 1315 const struct file_operations *fops = &sq_fops; 1316#ifndef MODULE 1317 int sq_unit; 1318#endif 1319 1320#ifdef HAS_RECORD 1321 if (dmasound.mach.record) 1322 fops = &sq_fops_record; 1323#endif 1324 sq_unit = register_sound_dsp(fops, -1); 1325 if (sq_unit < 0) { 1326 printk(KERN_ERR "dmasound_core: couldn't register fops\n") ; 1327 return sq_unit ; 1328 } 1329 1330 write_sq_init_waitqueue(); 1331 read_sq_init_waitqueue(); 1332 1333 /* These parameters will be restored for every clean open() 1334 * in the case of multiple open()s (e.g. dsp0 & dsp1) they 1335 * will be set so long as the shared resources have no owner. 1336 */ 1337 1338 if (shared_resource_owner == 0) { 1339 dmasound.soft = dmasound.mach.default_soft ; 1340 dmasound.hard = dmasound.mach.default_hard ; 1341 dmasound.dsp = dmasound.mach.default_soft ; 1342 shared_resources_initialised = 0 ; 1343 } 1344 return 0 ; 1345} 1346 1347 1348 /* 1349 * /dev/sndstat 1350 */ 1351 1352/* we allow more space for record-enabled because there are extra output lines. 1353 the number here must include the amount we are prepared to give to the low-level 1354 driver. 1355*/ 1356 1357#ifdef HAS_RECORD 1358#define STAT_BUFF_LEN 1024 1359#else 1360#define STAT_BUFF_LEN 768 1361#endif 1362 1363/* this is how much space we will allow the low-level driver to use 1364 in the stat buffer. Currently, 2 * (80 character line + <NL>). 1365 We do not police this (it is up to the ll driver to be honest). 1366*/ 1367 1368#define LOW_LEVEL_STAT_ALLOC 162 1369 1370static struct { 1371 int busy; 1372 char buf[STAT_BUFF_LEN]; /* state.buf should not overflow! */ 1373 int len, ptr; 1374} state; 1375 1376/* publish this function for use by low-level code, if required */ 1377 1378char *get_afmt_string(int afmt) 1379{ 1380 switch(afmt) { 1381 case AFMT_MU_LAW: 1382 return "mu-law"; 1383 break; 1384 case AFMT_A_LAW: 1385 return "A-law"; 1386 break; 1387 case AFMT_U8: 1388 return "unsigned 8 bit"; 1389 break; 1390 case AFMT_S8: 1391 return "signed 8 bit"; 1392 break; 1393 case AFMT_S16_BE: 1394 return "signed 16 bit BE"; 1395 break; 1396 case AFMT_U16_BE: 1397 return "unsigned 16 bit BE"; 1398 break; 1399 case AFMT_S16_LE: 1400 return "signed 16 bit LE"; 1401 break; 1402 case AFMT_U16_LE: 1403 return "unsigned 16 bit LE"; 1404 break; 1405 case 0: 1406 return "format not set" ; 1407 break ; 1408 default: 1409 break ; 1410 } 1411 return "ERROR: Unsupported AFMT_XXXX code" ; 1412} 1413 1414static int state_open(struct inode *inode, struct file *file) 1415{ 1416 char *buffer = state.buf; 1417 int len = 0; 1418 1419 if (state.busy) 1420 return -EBUSY; 1421 1422 if (!try_module_get(dmasound.mach.owner)) 1423 return -ENODEV; 1424 state.ptr = 0; 1425 state.busy = 1; 1426 1427 len += sprintf(buffer+len, "%sDMA sound driver rev %03d :\n", 1428 dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) + 1429 ((dmasound.mach.version>>8) & 0x0f)); 1430 len += sprintf(buffer+len, 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 1435 /* call the low-level module to fill in any stat info. that it has 1436 if present. Maximum buffer usage is specified. 1437 */ 1438 1439 if (dmasound.mach.state_info) 1440 len += dmasound.mach.state_info(buffer+len, 1441 (size_t) LOW_LEVEL_STAT_ALLOC) ; 1442 1443 /* make usage of the state buffer as deterministic as poss. 1444 exceptional conditions could cause overrun - and this is flagged as 1445 a kernel error. 1446 */ 1447 1448 /* formats and settings */ 1449 1450 len += sprintf(buffer+len,"\t\t === Formats & settings ===\n") ; 1451 len += sprintf(buffer+len,"Parameter %20s%20s\n","soft","hard") ; 1452 len += sprintf(buffer+len,"Format :%20s%20s\n", 1453 get_afmt_string(dmasound.soft.format), 1454 get_afmt_string(dmasound.hard.format)); 1455 1456 len += sprintf(buffer+len,"Samp Rate:%14d s/sec%14d s/sec\n", 1457 dmasound.soft.speed, dmasound.hard.speed); 1458 1459 len += sprintf(buffer+len,"Channels :%20s%20s\n", 1460 dmasound.soft.stereo ? "stereo" : "mono", 1461 dmasound.hard.stereo ? "stereo" : "mono" ); 1462 1463 /* sound queue status */ 1464 1465 len += sprintf(buffer+len,"\t\t === Sound Queue status ===\n"); 1466 len += sprintf(buffer+len,"Allocated:%8s%6s\n","Buffers","Size") ; 1467 len += sprintf(buffer+len,"%9s:%8d%6d\n", 1468 "write", write_sq.numBufs, write_sq.bufSize) ; 1469#ifdef HAS_RECORD 1470 if (dmasound.mach.record) 1471 len += sprintf(buffer+len,"%9s:%8d%6d\n", 1472 "read", read_sq.numBufs, read_sq.bufSize) ; 1473#endif 1474 len += sprintf(buffer+len, 1475 "Current : MaxFrg FragSiz MaxAct Frnt Rear " 1476 "Cnt RrSize A B S L xruns\n") ; 1477 len += sprintf(buffer+len,"%9s:%7d%8d%7d%5d%5d%4d%7d%2d%2d%2d%2d%7d\n", 1478 "write", write_sq.max_count, write_sq.block_size, 1479 write_sq.max_active, write_sq.front, write_sq.rear, 1480 write_sq.count, write_sq.rear_size, write_sq.active, 1481 write_sq.busy, write_sq.syncing, write_sq.locked, write_sq.xruns) ; 1482#ifdef HAS_RECORD 1483 if (dmasound.mach.record) 1484 len += sprintf(buffer+len,"%9s:%7d%8d%7d%5d%5d%4d%7d%2d%2d%2d%2d%7d\n", 1485 "read", read_sq.max_count, read_sq.block_size, 1486 read_sq.max_active, read_sq.front, read_sq.rear, 1487 read_sq.count, read_sq.rear_size, read_sq.active, 1488 read_sq.busy, read_sq.syncing, read_sq.locked, read_sq.xruns) ; 1489#endif 1490#ifdef DEBUG_DMASOUND 1491printk("dmasound: stat buffer used %d bytes\n", len) ; 1492#endif 1493 1494 if (len >= STAT_BUFF_LEN) 1495 printk(KERN_ERR "dmasound_core: stat buffer overflowed!\n"); 1496 1497 state.len = len; 1498 return 0; 1499} 1500 1501static int state_release(struct inode *inode, struct file *file) 1502{ 1503 lock_kernel(); 1504 state.busy = 0; 1505 module_put(dmasound.mach.owner); 1506 unlock_kernel(); 1507 return 0; 1508} 1509 1510static ssize_t state_read(struct file *file, char __user *buf, size_t count, 1511 loff_t *ppos) 1512{ 1513 int n = state.len - state.ptr; 1514 if (n > count) 1515 n = count; 1516 if (n <= 0) 1517 return 0; 1518 if (copy_to_user(buf, &state.buf[state.ptr], n)) 1519 return -EFAULT; 1520 state.ptr += n; 1521 return n; 1522} 1523 1524static const struct file_operations state_fops = { 1525 .owner = THIS_MODULE, 1526 .llseek = no_llseek, 1527 .read = state_read, 1528 .open = state_open, 1529 .release = state_release, 1530}; 1531 1532static int state_init(void) 1533{ 1534#ifndef MODULE 1535 int state_unit; 1536#endif 1537 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS); 1538 if (state_unit < 0) 1539 return state_unit ; 1540 state.busy = 0; 1541 return 0 ; 1542} 1543 1544 1545 /* 1546 * Config & Setup 1547 * 1548 * This function is called by _one_ chipset-specific driver 1549 */ 1550 1551int dmasound_init(void) 1552{ 1553 int res ; 1554#ifdef MODULE 1555 if (irq_installed) 1556 return -EBUSY; 1557#endif 1558 1559 /* Set up sound queue, /dev/audio and /dev/dsp. */ 1560 1561 /* Set default settings. */ 1562 if ((res = sq_init()) < 0) 1563 return res ; 1564 1565 /* Set up /dev/sndstat. */ 1566 if ((res = state_init()) < 0) 1567 return res ; 1568 1569 /* Set up /dev/mixer. */ 1570 mixer_init(); 1571 1572 if (!dmasound.mach.irqinit()) { 1573 printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n"); 1574 return -ENODEV; 1575 } 1576#ifdef MODULE 1577 irq_installed = 1; 1578#endif 1579 1580 printk(KERN_INFO "%s DMA sound driver rev %03d installed\n", 1581 dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) + 1582 ((dmasound.mach.version>>8) & 0x0f)); 1583 printk(KERN_INFO 1584 "Core driver edition %02d.%02d : %s driver edition %02d.%02d\n", 1585 DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2, 1586 (dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ; 1587 printk(KERN_INFO "Write will use %4d fragments of %7d bytes as default\n", 1588 numWriteBufs, writeBufSize) ; 1589#ifdef HAS_RECORD 1590 if (dmasound.mach.record) 1591 printk(KERN_INFO 1592 "Read will use %4d fragments of %7d bytes as default\n", 1593 numReadBufs, readBufSize) ; 1594#endif 1595 1596 return 0; 1597} 1598 1599#ifdef MODULE 1600 1601void dmasound_deinit(void) 1602{ 1603 if (irq_installed) { 1604 sound_silence(); 1605 dmasound.mach.irqcleanup(); 1606 irq_installed = 0; 1607 } 1608 1609 write_sq_release_buffers(); 1610 read_sq_release_buffers(); 1611 1612 if (mixer_unit >= 0) 1613 unregister_sound_mixer(mixer_unit); 1614 if (state_unit >= 0) 1615 unregister_sound_special(state_unit); 1616 if (sq_unit >= 0) 1617 unregister_sound_dsp(sq_unit); 1618} 1619 1620#else /* !MODULE */ 1621 1622static int dmasound_setup(char *str) 1623{ 1624 int ints[6], size; 1625 1626 str = get_options(str, ARRAY_SIZE(ints), ints); 1627 1628 /* check the bootstrap parameter for "dmasound=" */ 1629 1630 1631 switch (ints[0]) { 1632#ifdef HAS_RECORD 1633 case 5: 1634 if ((ints[5] < 0) || (ints[5] > MAX_CATCH_RADIUS)) 1635 printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius); 1636 else 1637 catchRadius = ints[5]; 1638 /* fall through */ 1639 case 4: 1640 if (ints[4] < MIN_BUFFERS) 1641 printk("dmasound_setup: invalid number of read buffers, using default = %d\n", 1642 numReadBufs); 1643 else 1644 numReadBufs = ints[4]; 1645 /* fall through */ 1646 case 3: 1647 if ((size = ints[3]) < 256) /* check for small buffer specs */ 1648 size <<= 10 ; 1649 if (size < MIN_BUFSIZE || size > MAX_BUFSIZE) 1650 printk("dmasound_setup: invalid read buffer size, using default = %d\n", readBufSize); 1651 else 1652 readBufSize = size; 1653 /* fall through */ 1654#else 1655 case 3: 1656 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS)) 1657 printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius); 1658 else 1659 catchRadius = ints[3]; 1660 /* fall through */ 1661#endif 1662 case 2: 1663 if (ints[1] < MIN_BUFFERS) 1664 printk("dmasound_setup: invalid number of buffers, using default = %d\n", numWriteBufs); 1665 else 1666 numWriteBufs = ints[1]; 1667 /* fall through */ 1668 case 1: 1669 if ((size = ints[2]) < 256) /* check for small buffer specs */ 1670 size <<= 10 ; 1671 if (size < MIN_BUFSIZE || size > MAX_BUFSIZE) 1672 printk("dmasound_setup: invalid write buffer size, using default = %d\n", writeBufSize); 1673 else 1674 writeBufSize = size; 1675 case 0: 1676 break; 1677 default: 1678 printk("dmasound_setup: invalid number of arguments\n"); 1679 return 0; 1680 } 1681 return 1; 1682} 1683 1684__setup("dmasound=", dmasound_setup); 1685 1686#endif /* !MODULE */ 1687 1688 /* 1689 * Conversion tables 1690 */ 1691 1692#ifdef HAS_8BIT_TABLES 1693/* 8 bit mu-law */ 1694 1695char dmasound_ulaw2dma8[] = { 1696 -126, -122, -118, -114, -110, -106, -102, -98, 1697 -94, -90, -86, -82, -78, -74, -70, -66, 1698 -63, -61, -59, -57, -55, -53, -51, -49, 1699 -47, -45, -43, -41, -39, -37, -35, -33, 1700 -31, -30, -29, -28, -27, -26, -25, -24, 1701 -23, -22, -21, -20, -19, -18, -17, -16, 1702 -16, -15, -15, -14, -14, -13, -13, -12, 1703 -12, -11, -11, -10, -10, -9, -9, -8, 1704 -8, -8, -7, -7, -7, -7, -6, -6, 1705 -6, -6, -5, -5, -5, -5, -4, -4, 1706 -4, -4, -4, -4, -3, -3, -3, -3, 1707 -3, -3, -3, -3, -2, -2, -2, -2, 1708 -2, -2, -2, -2, -2, -2, -2, -2, 1709 -1, -1, -1, -1, -1, -1, -1, -1, 1710 -1, -1, -1, -1, -1, -1, -1, -1, 1711 -1, -1, -1, -1, -1, -1, -1, 0, 1712 125, 121, 117, 113, 109, 105, 101, 97, 1713 93, 89, 85, 81, 77, 73, 69, 65, 1714 62, 60, 58, 56, 54, 52, 50, 48, 1715 46, 44, 42, 40, 38, 36, 34, 32, 1716 30, 29, 28, 27, 26, 25, 24, 23, 1717 22, 21, 20, 19, 18, 17, 16, 15, 1718 15, 14, 14, 13, 13, 12, 12, 11, 1719 11, 10, 10, 9, 9, 8, 8, 7, 1720 7, 7, 6, 6, 6, 6, 5, 5, 1721 5, 5, 4, 4, 4, 4, 3, 3, 1722 3, 3, 3, 3, 2, 2, 2, 2, 1723 2, 2, 2, 2, 1, 1, 1, 1, 1724 1, 1, 1, 1, 1, 1, 1, 1, 1725 0, 0, 0, 0, 0, 0, 0, 0, 1726 0, 0, 0, 0, 0, 0, 0, 0, 1727 0, 0, 0, 0, 0, 0, 0, 0 1728}; 1729 1730/* 8 bit A-law */ 1731 1732char dmasound_alaw2dma8[] = { 1733 -22, -21, -24, -23, -18, -17, -20, -19, 1734 -30, -29, -32, -31, -26, -25, -28, -27, 1735 -11, -11, -12, -12, -9, -9, -10, -10, 1736 -15, -15, -16, -16, -13, -13, -14, -14, 1737 -86, -82, -94, -90, -70, -66, -78, -74, 1738 -118, -114, -126, -122, -102, -98, -110, -106, 1739 -43, -41, -47, -45, -35, -33, -39, -37, 1740 -59, -57, -63, -61, -51, -49, -55, -53, 1741 -2, -2, -2, -2, -2, -2, -2, -2, 1742 -2, -2, -2, -2, -2, -2, -2, -2, 1743 -1, -1, -1, -1, -1, -1, -1, -1, 1744 -1, -1, -1, -1, -1, -1, -1, -1, 1745 -6, -6, -6, -6, -5, -5, -5, -5, 1746 -8, -8, -8, -8, -7, -7, -7, -7, 1747 -3, -3, -3, -3, -3, -3, -3, -3, 1748 -4, -4, -4, -4, -4, -4, -4, -4, 1749 21, 20, 23, 22, 17, 16, 19, 18, 1750 29, 28, 31, 30, 25, 24, 27, 26, 1751 10, 10, 11, 11, 8, 8, 9, 9, 1752 14, 14, 15, 15, 12, 12, 13, 13, 1753 86, 82, 94, 90, 70, 66, 78, 74, 1754 118, 114, 126, 122, 102, 98, 110, 106, 1755 43, 41, 47, 45, 35, 33, 39, 37, 1756 59, 57, 63, 61, 51, 49, 55, 53, 1757 1, 1, 1, 1, 1, 1, 1, 1, 1758 1, 1, 1, 1, 1, 1, 1, 1, 1759 0, 0, 0, 0, 0, 0, 0, 0, 1760 0, 0, 0, 0, 0, 0, 0, 0, 1761 5, 5, 5, 5, 4, 4, 4, 4, 1762 7, 7, 7, 7, 6, 6, 6, 6, 1763 2, 2, 2, 2, 2, 2, 2, 2, 1764 3, 3, 3, 3, 3, 3, 3, 3 1765}; 1766#endif /* HAS_8BIT_TABLES */ 1767 1768 /* 1769 * Visible symbols for modules 1770 */ 1771 1772EXPORT_SYMBOL(dmasound); 1773EXPORT_SYMBOL(dmasound_init); 1774#ifdef MODULE 1775EXPORT_SYMBOL(dmasound_deinit); 1776#endif 1777EXPORT_SYMBOL(dmasound_write_sq); 1778#ifdef HAS_RECORD 1779EXPORT_SYMBOL(dmasound_read_sq); 1780#endif 1781EXPORT_SYMBOL(dmasound_catchRadius); 1782#ifdef HAS_8BIT_TABLES 1783EXPORT_SYMBOL(dmasound_ulaw2dma8); 1784EXPORT_SYMBOL(dmasound_alaw2dma8); 1785#endif 1786EXPORT_SYMBOL(get_afmt_string) ; 1787