Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | /* * sound/opl3sa2.c * * A low level driver for Yamaha OPL3-SA2 and SA3 cards. * SAx cards should work, as they are just variants of the SA3. * * Copyright 1998, 1999, 2000 Scott Murray <scott@spiteful.org> * * Originally based on the CS4232 driver (in cs4232.c) by Hannu Savolainen * and others. Now incorporates code/ideas from pss.c, also by Hannu * Savolainen. Both of those files are distributed with the following * license: * * "Copyright (C) by Hannu Savolainen 1993-1997 * * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL) * Version 2 (June 1991). See the "COPYING" file distributed with this software * for more info." * * As such, in accordance with the above license, this file, opl3sa2.c, is * distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2 (June 1991). * See the "COPYING" file distributed with this software for more information. * * Change History * -------------- * Scott Murray Original driver (Jun 14, 1998) * Paul J.Y. Lahaie Changed probing / attach code order * Scott Murray Added mixer support (Dec 03, 1998) * Scott Murray Changed detection code to be more forgiving, * added force option as last resort, * fixed ioctl return values. (Dec 30, 1998) * Scott Murray Simpler detection code should work all the time now * (with thanks to Ben Hutchings for the heuristic), * removed now unnecessary force option. (Jan 5, 1999) * Scott Murray Updated e-mail address. (Mar 18, 2000) * */ #include <linux/config.h> #include <linux/module.h> #include "sound_config.h" #include "soundmodule.h" /* Useful control port indexes: */ #define OPL3SA2_MASTER_LEFT 0x07 #define OPL3SA2_MASTER_RIGHT 0x08 #define OPL3SA2_MIC 0x09 #define OPL3SA2_MISC 0x0A #define OPL3SA3_WIDE 0x14 #define OPL3SA3_BASS 0x15 #define OPL3SA3_TREBLE 0x16 /* Useful constants: */ #define DEFAULT_VOLUME 50 #define DEFAULT_MIC 50 #define DEFAULT_TIMBRE 0 #define CHIPSET_UNKNOWN -1 /* * These are used both as masks against what the card returns, * and as constants. */ #define CHIPSET_OPL3SA2 1 #define CHIPSET_OPL3SA3 2 #define CHIPSET_OPL3SAX 4 #ifdef CONFIG_OPL3SA2 /* What's my version? */ static int chipset = CHIPSET_UNKNOWN; /* Oh well, let's just cache the name */ static char chipset_name[16]; /* Where's my mixer */ static int opl3sa2_mixer = -1; /* Bag o' mixer data */ typedef struct opl3sa2_mixerdata { unsigned short cfg_port; unsigned short padding; int ad_mixer_dev; unsigned int volume_l; unsigned int volume_r; unsigned int mic; unsigned int bass; unsigned int treble; } opl3sa2_mixerdata; #ifdef CONFIG_OPL3SA2_CTRL_BASE /* Set control port if compiled into the kernel */ static opl3sa2_mixerdata opl3sa2_data = { CONFIG_OPL3SA2_CTRL_BASE, }; #else static opl3sa2_mixerdata opl3sa2_data; #endif static opl3sa2_mixerdata *devc = &opl3sa2_data; /* Standard read and write functions */ static void opl3sa2_write(unsigned short port, unsigned char index, unsigned char data) { outb_p(index, port); outb(data, port + 1); } static void opl3sa2_read(unsigned short port, unsigned char index, unsigned char* data) { outb_p(index, port); *data = inb(port + 1); } /* All of the mixer functions... */ static void opl3sa2_set_volume(opl3sa2_mixerdata *devc, int left, int right) { static unsigned char scale[101] = { 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00 }; unsigned char vol; vol = scale[left]; /* If level is zero, turn on mute */ if(!left) vol |= 0x80; opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol); vol = scale[right]; /* If level is zero, turn on mute */ if(!right) vol |= 0x80; opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol); } static void opl3sa2_set_mic(opl3sa2_mixerdata *devc, int level) { unsigned char vol = 0x1F; if((level >= 0) && (level <= 100)) vol = 0x1F - (unsigned char) (0x1F * level / 100L); /* If level is zero, turn on mute */ if(!level) vol |= 0x80; opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol); } static void opl3sa3_set_bass(opl3sa2_mixerdata *devc, int level) { unsigned char bass; bass = level ? ((unsigned char) (0x07 * level / 100L)) : 0; bass |= (bass << 4); opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass); } static void opl3sa3_set_treble(opl3sa2_mixerdata *devc, int level) { unsigned char treble; treble = level ? ((unsigned char) (0x07 * level / 100L)) : 0; treble |= (treble << 4); opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble); } static void opl3sa2_mixer_reset(opl3sa2_mixerdata *devc) { if(devc) { opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME); devc->volume_l = devc->volume_r = DEFAULT_VOLUME; opl3sa2_set_mic(devc, DEFAULT_MIC); devc->mic = DEFAULT_MIC; opl3sa3_set_bass(devc, DEFAULT_TIMBRE); opl3sa3_set_treble(devc, DEFAULT_TIMBRE); devc->bass = devc->treble = DEFAULT_TIMBRE; } } static void arg_to_volume_mono(unsigned int volume, int *aleft) { int left; left = volume & 0x00ff; if (left > 100) left = 100; *aleft = left; } static void arg_to_volume_stereo(unsigned int volume, int *aleft, int *aright) { arg_to_volume_mono(volume, aleft); arg_to_volume_mono(volume >> 8, aright); } static int ret_vol_mono(int left) { return ((left << 8) | left); } static int ret_vol_stereo(int left, int right) { return ((right << 8) | left); } static int call_ad_mixer(opl3sa2_mixerdata *devc, unsigned int cmd, caddr_t arg) { if(devc->ad_mixer_dev != -1) return mixer_devs[devc->ad_mixer_dev]->ioctl(devc->ad_mixer_dev, cmd, arg); else return -EINVAL; } static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg) { int cmdf = cmd & 0xff; opl3sa2_mixerdata* devc = (opl3sa2_mixerdata*) mixer_devs[dev]->devc; switch(cmdf) { case SOUND_MIXER_VOLUME: case SOUND_MIXER_MIC: case SOUND_MIXER_BASS: case SOUND_MIXER_TREBLE: case SOUND_MIXER_DEVMASK: case SOUND_MIXER_STEREODEVS: case SOUND_MIXER_RECMASK: case SOUND_MIXER_CAPS: case SOUND_MIXER_RECSRC: break; default: return call_ad_mixer(devc, cmd, arg); } if(((cmd >> 8) & 0xff) != 'M') return -EINVAL; if(_SIOC_DIR (cmd) & _SIOC_WRITE) { switch (cmdf) { case SOUND_MIXER_RECSRC: if(devc->ad_mixer_dev != -1) return call_ad_mixer(devc, cmd, arg); else { if(*(int*)arg != 0) return -EINVAL; return 0; } case SOUND_MIXER_VOLUME: arg_to_volume_stereo(*(unsigned int*)arg, &devc->volume_l, &devc->volume_r); opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r); *(int*)arg = ret_vol_stereo(devc->volume_l, devc->volume_r); return 0; case SOUND_MIXER_MIC: arg_to_volume_mono(*(unsigned int*)arg, &devc->mic); opl3sa2_set_mic(devc, devc->mic); *(int*)arg = ret_vol_mono(devc->mic); return 0; case SOUND_MIXER_BASS: if(chipset != CHIPSET_OPL3SA2) { arg_to_volume_mono(*(unsigned int*)arg, &devc->bass); opl3sa3_set_bass(devc, devc->bass); *(int*)arg = ret_vol_mono(devc->bass); return 0; } return -EINVAL; case SOUND_MIXER_TREBLE: if(chipset != CHIPSET_OPL3SA2) { arg_to_volume_mono(*(unsigned int *)arg, &devc->treble); opl3sa3_set_treble(devc, devc->treble); *(int*)arg = ret_vol_mono(devc->treble); return 0; } return -EINVAL; default: return -EINVAL; } } else { /* * Return parameters */ switch (cmdf) { case SOUND_MIXER_DEVMASK: if(call_ad_mixer(devc, cmd, arg) == -EINVAL) *(int*)arg = 0; /* no mixer devices */ *(int*)arg |= (SOUND_MASK_VOLUME | SOUND_MASK_MIC); /* OPL3-SA2 has no bass and treble mixers */ if(chipset != CHIPSET_OPL3SA2) *(int*)arg |= (SOUND_MASK_BASS | SOUND_MASK_TREBLE); return 0; case SOUND_MIXER_STEREODEVS: if(call_ad_mixer(devc, cmd, arg) == -EINVAL) *(int*)arg = 0; /* no stereo devices */ *(int*)arg |= SOUND_MASK_VOLUME; return 0; case SOUND_MIXER_RECMASK: if(devc->ad_mixer_dev != -1) { return call_ad_mixer(devc, cmd, arg); } else { /* No recording devices */ return (*(int*)arg = 0); } case SOUND_MIXER_CAPS: if(devc->ad_mixer_dev != -1) { return call_ad_mixer(devc, cmd, arg); } else { *(int*)arg = SOUND_CAP_EXCL_INPUT; return 0; } case SOUND_MIXER_RECSRC: if(devc->ad_mixer_dev != -1) { return call_ad_mixer(devc, cmd, arg); } else { /* No recording source */ return (*(int*)arg = 0); } case SOUND_MIXER_VOLUME: *(int*)arg = ret_vol_stereo(devc->volume_l, devc->volume_r); return 0; case SOUND_MIXER_MIC: *(int*)arg = ret_vol_mono(devc->mic); return 0; case SOUND_MIXER_BASS: if(chipset != CHIPSET_OPL3SA2) { *(int*)arg = ret_vol_mono(devc->bass); return 0; } else { return -EINVAL; } case SOUND_MIXER_TREBLE: if(chipset != CHIPSET_OPL3SA2) { *(int*)arg = ret_vol_mono(devc->treble); return 0; } else { return -EINVAL; } default: return -EINVAL; } } } static struct mixer_operations opl3sa2_mixer_operations = { "Yamaha", "", opl3sa2_mixer_ioctl }; /* End of mixer-related stuff */ int probe_opl3sa2_mpu(struct address_info *hw_config) { #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI) return probe_mpu401(hw_config); #else return 0; #endif } void attach_opl3sa2_mpu(struct address_info *hw_config) { #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI) attach_mpu401(hw_config); #endif } void unload_opl3sa2_mpu(struct address_info *hw_config) { #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI) unload_mpu401(hw_config); #endif } int probe_opl3sa2_mss(struct address_info *hw_config) { return probe_ms_sound(hw_config); } void attach_opl3sa2_mss(struct address_info *hw_config) { char mixer_name[64]; /* Create pretty names for mixer stuff */ strncpy(mixer_name, chipset_name, 16); strncat(mixer_name, " and AD1848 (through MSS)", 64); strncpy(opl3sa2_mixer_operations.name, chipset_name, 16); strncat(opl3sa2_mixer_operations.name, "-AD1848", 64); /* Install master mixer */ devc->ad_mixer_dev = -1; if((opl3sa2_mixer = sound_install_mixer(MIXER_DRIVER_VERSION, mixer_name, &opl3sa2_mixer_operations, sizeof(struct mixer_operations), devc)) < 0) { printk(KERN_ERR "Could not install %s master mixer\n", chipset_name); return; } opl3sa2_mixer_reset(devc); attach_ms_sound(hw_config); /* Slot 0 */ if(hw_config->slots[0] != -1) { /* Did the MSS driver install? */ if(num_mixers == (opl3sa2_mixer + 2)) { /* The MSS mixer is installed */ devc->ad_mixer_dev = audio_devs[hw_config->slots[0]]->mixer_dev; /* Reroute mixers appropiately */ AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD); AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH); AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE); } } } void unload_opl3sa2_mss(struct address_info *hw_config) { unload_ms_sound(hw_config); } int probe_opl3sa2(struct address_info *hw_config) { unsigned char version = 0; char tag; /* * Verify that the I/O port range is free. */ if(check_region(hw_config->io_base, 2)) { printk(KERN_ERR "%s: Control I/O port 0x%03x not free\n", __FILE__, hw_config->io_base); return 0; } /* * Determine chipset type (SA2, SA3, or SAx) */ /* * Look at chipset version in lower 3 bits of index 0x0A, miscellaneous */ opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, (unsigned char*) &version); version &= 0x07; /* Match version number to appropiate chipset */ if(version & CHIPSET_OPL3SAX) { chipset = CHIPSET_OPL3SAX; tag = 'x'; printk(KERN_INFO "Found OPL3-SAx (YMF719)\n"); } else { if(version & CHIPSET_OPL3SA3) { chipset = CHIPSET_OPL3SA3; tag = '3'; printk(KERN_INFO "Found OPL3-SA3 (YMF715)\n"); } else { if(version & CHIPSET_OPL3SA2) { chipset = CHIPSET_OPL3SA2; tag = '2'; printk(KERN_INFO "Found OPL3-SA2 (YMF711)\n"); } else { chipset = CHIPSET_UNKNOWN; tag = '?'; printk(KERN_ERR "Unknown Yamaha audio controller version\n"); printk(KERN_INFO "%s: chipset version = %x\n", __FILE__, version); } } } if(chipset != CHIPSET_UNKNOWN) { /* Generate a pretty name */ sprintf(chipset_name, "OPL3-SA%c", tag); #if defined(CONFIG_OPL3SA2_MPU_BASE) && !defined(MODULE) sound_getconf(SNDCARD_OPL3SA2_MPU)->always_detect = 1; #endif return 1; } return 0; } void attach_opl3sa2(struct address_info *hw_config) { request_region(hw_config->io_base, 2, chipset_name); devc->cfg_port = hw_config->io_base; } void unload_opl3sa2(struct address_info *hw_config) { /* Release control ports */ release_region(hw_config->io_base, 2); /* Unload mixer */ if(opl3sa2_mixer >= 0) sound_unload_mixerdev(opl3sa2_mixer); } #ifdef MODULE int io = -1; int mss_io = -1; int mpu_io = -1; int irq = -1; int dma = -1; int dma2 = -1; MODULE_PARM(io, "i"); MODULE_PARM_DESC(io, "Set i/o base of OPL3-SA2 or SA3 card (usually 0x370)"); MODULE_PARM(mss_io, "i"); MODULE_PARM_DESC(mss_io, "Set MSS (audio) I/O base (0x530, 0xE80, or other. Address must end in 0 or 4 and must be from 0x530 to 0xF48)"); MODULE_PARM(mpu_io, "i"); MODULE_PARM_DESC(mpu_io, "Set MIDI I/O base (0x330 or other. Address must be on 4 location boundaries and must be from 0x300 to 0x334)"); MODULE_PARM(irq, "i"); MODULE_PARM_DESC(mss_irq, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)"); MODULE_PARM(dma, "i"); MODULE_PARM_DESC(dma, "Set MSS (audio) first DMA channel (0, 1, 3)"); MODULE_PARM(dma2, "i"); MODULE_PARM_DESC(dma2, "Set MSS (audio) second DMA channel (0, 1, 3)"); MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver)."); MODULE_AUTHOR("Scott Murray <scott@spiteful.org>"); EXPORT_NO_SYMBOLS; struct address_info cfg; struct address_info mss_cfg; struct address_info mpu_cfg; /* * Install a OPL3SA2 based card. * * Need to have ad1848 and mpu401 loaded ready. */ int init_module(void) { int i; if(io == -1 || irq == -1 || dma == -1 || dma2 == -1 || mss_io == -1) { printk(KERN_ERR "%s: io, mss_io, irq, dma, and dma2 must be set.\n", __FILE__); return -EINVAL; } /* Our own config: */ cfg.io_base = io; cfg.irq = irq; cfg.dma = dma; cfg.dma2 = dma2; /* The MSS config: */ mss_cfg.io_base = mss_io; mss_cfg.irq = irq; mss_cfg.dma = dma; mss_cfg.dma2 = dma2; mss_cfg.card_subtype = 1; /* No IRQ or DMA setup */ /* Call me paranoid: */ for(i = 0; i < 6; i++) { cfg.slots[i] = mss_cfg.slots[i] = mpu_cfg.slots[i] = -1; } if(probe_opl3sa2(&cfg) == 0) { return -ENODEV; } if(probe_opl3sa2_mss(&mss_cfg) == 0) { return -ENODEV; } attach_opl3sa2(&cfg); attach_opl3sa2_mss(&mss_cfg); #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI) if(mpu_io != -1) { /* MPU config: */ mpu_cfg.io_base = mpu_io; mpu_cfg.irq = irq; mpu_cfg.dma = dma; mpu_cfg.always_detect = 1; /* It's there, so use shared IRQs */ if(probe_opl3sa2_mpu(&mpu_cfg)) { attach_opl3sa2_mpu(&mpu_cfg); } } #endif SOUND_LOCK; return 0; } void cleanup_module(void) { #if (defined(CONFIG_MPU401) || defined(CONFIG_MPU_EMU)) && defined(CONFIG_MIDI) if(mpu_cfg.slots[1] != -1) { unload_opl3sa2_mpu(&mpu_cfg); } #endif unload_opl3sa2_mss(&mss_cfg); unload_opl3sa2(&cfg); SOUND_LOCK_END; } #endif /* MODULE */ #endif /* CONFIG_OPL3SA2 */ |