1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards. 4 Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it> 5 6 Part of this code was developed at the Italian Ministry of Air Defence, 7 Sixth Division (oh, che pace ...), Rome. 8 9 Thanks to Maria Grazia Pollarini, Salvatore Vassallo. 10 11 */ 12 13 14 #include <linux/init.h> 15 #include <linux/err.h> 16 #include <linux/isa.h> 17 #include <linux/delay.h> 18 #include <linux/pnp.h> 19 #include <linux/module.h> 20 #include <linux/io.h> 21 #include <asm/dma.h> 22 #include <sound/core.h> 23 #include <sound/tlv.h> 24 #include <sound/wss.h> 25 #include <sound/mpu401.h> 26 #include <sound/opl3.h> 27 #ifndef OPTi93X 28 #include <sound/opl4.h> 29 #endif 30 #define SNDRV_LEGACY_FIND_FREE_IOPORT 31 #define SNDRV_LEGACY_FIND_FREE_IRQ 32 #define SNDRV_LEGACY_FIND_FREE_DMA 33 #include <sound/initval.h> 34 35 MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>"); 36 MODULE_LICENSE("GPL"); 37 #ifdef OPTi93X 38 MODULE_DESCRIPTION("OPTi93X"); 39 #else /* OPTi93X */ 40 #ifdef CS4231 41 MODULE_DESCRIPTION("OPTi92X - CS4231"); 42 #else /* CS4231 */ 43 MODULE_DESCRIPTION("OPTi92X - AD1848"); 44 #endif /* CS4231 */ 45 #endif /* OPTi93X */ 46 47 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */ 48 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */ 49 //static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */ 50 #ifdef CONFIG_PNP 51 static bool isapnp = true; /* Enable ISA PnP detection */ 52 #endif 53 static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */ 54 static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */ 55 static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */ 56 static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */ 57 static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */ 58 static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ 59 #if defined(CS4231) || defined(OPTi93X) 60 static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */ 61 #endif /* CS4231 || OPTi93X */ 62 63 module_param(index, int, 0444); 64 MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard."); 65 module_param(id, charp, 0444); 66 MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard."); 67 //module_param(enable, bool, 0444); 68 //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard."); 69 #ifdef CONFIG_PNP 70 module_param(isapnp, bool, 0444); 71 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard."); 72 #endif 73 module_param_hw(port, long, ioport, 0444); 74 MODULE_PARM_DESC(port, "WSS port # for opti9xx driver."); 75 module_param_hw(mpu_port, long, ioport, 0444); 76 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver."); 77 module_param_hw(fm_port, long, ioport, 0444); 78 MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver."); 79 module_param_hw(irq, int, irq, 0444); 80 MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver."); 81 module_param_hw(mpu_irq, int, irq, 0444); 82 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver."); 83 module_param_hw(dma1, int, dma, 0444); 84 MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver."); 85 #if defined(CS4231) || defined(OPTi93X) 86 module_param_hw(dma2, int, dma, 0444); 87 MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver."); 88 #endif /* CS4231 || OPTi93X */ 89 90 #define OPTi9XX_HW_82C928 1 91 #define OPTi9XX_HW_82C929 2 92 #define OPTi9XX_HW_82C924 3 93 #define OPTi9XX_HW_82C925 4 94 #define OPTi9XX_HW_82C930 5 95 #define OPTi9XX_HW_82C931 6 96 #define OPTi9XX_HW_82C933 7 97 #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933 98 99 #define OPTi9XX_MC_REG(n) n 100 101 #ifdef OPTi93X 102 103 #define OPTi93X_STATUS 0x02 104 #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r) 105 106 #define OPTi93X_IRQ_PLAYBACK 0x04 107 #define OPTi93X_IRQ_CAPTURE 0x08 108 109 #endif /* OPTi93X */ 110 111 struct snd_opti9xx { 112 unsigned short hardware; 113 unsigned char password; 114 char name[7]; 115 116 unsigned long mc_base; 117 struct resource *res_mc_base; 118 unsigned long mc_base_size; 119 #ifdef OPTi93X 120 unsigned long mc_indir_index; 121 struct resource *res_mc_indir; 122 #endif /* OPTi93X */ 123 struct snd_wss *codec; 124 unsigned long pwd_reg; 125 126 spinlock_t lock; 127 128 long wss_base; 129 int irq; 130 }; 131 132 static int snd_opti9xx_pnp_is_probed; 133 134 #ifdef CONFIG_PNP 135 136 static const struct pnp_card_device_id snd_opti9xx_pnpids[] = { 137 #ifndef OPTi93X 138 /* OPTi 82C924 */ 139 { .id = "OPT0924", 140 .devs = { { "OPT0000" }, { "OPT0002" }, { "OPT0005" } }, 141 .driver_data = 0x0924 }, 142 /* OPTi 82C925 */ 143 { .id = "OPT0925", 144 .devs = { { "OPT9250" }, { "OPT0002" }, { "OPT0005" } }, 145 .driver_data = 0x0925 }, 146 #else 147 /* OPTi 82C931/3 */ 148 { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, 149 .driver_data = 0x0931 }, 150 #endif /* OPTi93X */ 151 { .id = "" } 152 }; 153 154 MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids); 155 156 #endif /* CONFIG_PNP */ 157 158 #define DEV_NAME KBUILD_MODNAME 159 160 static const char * const snd_opti9xx_names[] = { 161 "unknown", 162 "82C928", "82C929", 163 "82C924", "82C925", 164 "82C930", "82C931", "82C933" 165 }; 166 167 static int snd_opti9xx_init(struct snd_opti9xx *chip, 168 unsigned short hardware) 169 { 170 static const int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2}; 171 172 chip->hardware = hardware; 173 strcpy(chip->name, snd_opti9xx_names[hardware]); 174 175 spin_lock_init(&chip->lock); 176 177 chip->irq = -1; 178 179 #ifndef OPTi93X 180 #ifdef CONFIG_PNP 181 if (isapnp && chip->mc_base) 182 /* PnP resource gives the least 10 bits */ 183 chip->mc_base |= 0xc00; 184 else 185 #endif /* CONFIG_PNP */ 186 { 187 chip->mc_base = 0xf8c; 188 chip->mc_base_size = opti9xx_mc_size[hardware]; 189 } 190 #else 191 chip->mc_base_size = opti9xx_mc_size[hardware]; 192 #endif 193 194 switch (hardware) { 195 #ifndef OPTi93X 196 case OPTi9XX_HW_82C928: 197 case OPTi9XX_HW_82C929: 198 chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3; 199 chip->pwd_reg = 3; 200 break; 201 202 case OPTi9XX_HW_82C924: 203 case OPTi9XX_HW_82C925: 204 chip->password = 0xe5; 205 chip->pwd_reg = 3; 206 break; 207 #else /* OPTi93X */ 208 209 case OPTi9XX_HW_82C930: 210 case OPTi9XX_HW_82C931: 211 case OPTi9XX_HW_82C933: 212 chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d; 213 if (!chip->mc_indir_index) 214 chip->mc_indir_index = 0xe0e; 215 chip->password = 0xe4; 216 chip->pwd_reg = 0; 217 break; 218 #endif /* OPTi93X */ 219 220 default: 221 snd_printk(KERN_ERR "chip %d not supported\n", hardware); 222 return -ENODEV; 223 } 224 return 0; 225 } 226 227 static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip, 228 unsigned char reg) 229 { 230 unsigned long flags; 231 unsigned char retval = 0xff; 232 233 spin_lock_irqsave(&chip->lock, flags); 234 outb(chip->password, chip->mc_base + chip->pwd_reg); 235 236 switch (chip->hardware) { 237 #ifndef OPTi93X 238 case OPTi9XX_HW_82C924: 239 case OPTi9XX_HW_82C925: 240 if (reg > 7) { 241 outb(reg, chip->mc_base + 8); 242 outb(chip->password, chip->mc_base + chip->pwd_reg); 243 retval = inb(chip->mc_base + 9); 244 break; 245 } 246 fallthrough; 247 248 case OPTi9XX_HW_82C928: 249 case OPTi9XX_HW_82C929: 250 retval = inb(chip->mc_base + reg); 251 break; 252 #else /* OPTi93X */ 253 254 case OPTi9XX_HW_82C930: 255 case OPTi9XX_HW_82C931: 256 case OPTi9XX_HW_82C933: 257 outb(reg, chip->mc_indir_index); 258 outb(chip->password, chip->mc_base + chip->pwd_reg); 259 retval = inb(chip->mc_indir_index + 1); 260 break; 261 #endif /* OPTi93X */ 262 263 default: 264 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); 265 } 266 267 spin_unlock_irqrestore(&chip->lock, flags); 268 return retval; 269 } 270 271 static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg, 272 unsigned char value) 273 { 274 unsigned long flags; 275 276 spin_lock_irqsave(&chip->lock, flags); 277 outb(chip->password, chip->mc_base + chip->pwd_reg); 278 279 switch (chip->hardware) { 280 #ifndef OPTi93X 281 case OPTi9XX_HW_82C924: 282 case OPTi9XX_HW_82C925: 283 if (reg > 7) { 284 outb(reg, chip->mc_base + 8); 285 outb(chip->password, chip->mc_base + chip->pwd_reg); 286 outb(value, chip->mc_base + 9); 287 break; 288 } 289 fallthrough; 290 291 case OPTi9XX_HW_82C928: 292 case OPTi9XX_HW_82C929: 293 outb(value, chip->mc_base + reg); 294 break; 295 #else /* OPTi93X */ 296 297 case OPTi9XX_HW_82C930: 298 case OPTi9XX_HW_82C931: 299 case OPTi9XX_HW_82C933: 300 outb(reg, chip->mc_indir_index); 301 outb(chip->password, chip->mc_base + chip->pwd_reg); 302 outb(value, chip->mc_indir_index + 1); 303 break; 304 #endif /* OPTi93X */ 305 306 default: 307 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); 308 } 309 310 spin_unlock_irqrestore(&chip->lock, flags); 311 } 312 313 314 static inline void snd_opti9xx_write_mask(struct snd_opti9xx *chip, 315 unsigned char reg, unsigned char value, unsigned char mask) 316 { 317 unsigned char oldval = snd_opti9xx_read(chip, reg); 318 319 snd_opti9xx_write(chip, reg, (oldval & ~mask) | (value & mask)); 320 } 321 322 static int snd_opti9xx_configure(struct snd_opti9xx *chip, 323 long port, 324 int irq, int dma1, int dma2, 325 long mpu_port, int mpu_irq) 326 { 327 unsigned char wss_base_bits; 328 unsigned char irq_bits; 329 unsigned char dma_bits; 330 unsigned char mpu_port_bits = 0; 331 unsigned char mpu_irq_bits; 332 333 switch (chip->hardware) { 334 #ifndef OPTi93X 335 case OPTi9XX_HW_82C924: 336 /* opti 929 mode (?), OPL3 clock output, audio enable */ 337 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc); 338 /* enable wave audio */ 339 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02); 340 fallthrough; 341 342 case OPTi9XX_HW_82C925: 343 /* enable WSS mode */ 344 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); 345 /* OPL3 FM synthesis */ 346 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); 347 /* disable Sound Blaster IRQ and DMA */ 348 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff); 349 #ifdef CS4231 350 /* cs4231/4248 fix enabled */ 351 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); 352 #else 353 /* cs4231/4248 fix disabled */ 354 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); 355 #endif /* CS4231 */ 356 break; 357 358 case OPTi9XX_HW_82C928: 359 case OPTi9XX_HW_82C929: 360 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80); 361 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20); 362 /* 363 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae); 364 */ 365 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c); 366 #ifdef CS4231 367 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02); 368 #else 369 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02); 370 #endif /* CS4231 */ 371 break; 372 373 #else /* OPTi93X */ 374 case OPTi9XX_HW_82C931: 375 /* disable 3D sound (set GPIO1 as output, low) */ 376 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(20), 0x04, 0x0c); 377 fallthrough; 378 379 case OPTi9XX_HW_82C933: 380 /* 381 * The BTC 1817DW has QS1000 wavetable which is connected 382 * to the serial digital input of the OPTI931. 383 */ 384 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff); 385 /* 386 * This bit sets OPTI931 to automaticaly select FM 387 * or digital input signal. 388 */ 389 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01); 390 fallthrough; 391 392 case OPTi9XX_HW_82C930: 393 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03); 394 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff); 395 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 | 396 (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04), 397 0x34); 398 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf); 399 break; 400 #endif /* OPTi93X */ 401 402 default: 403 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware); 404 return -EINVAL; 405 } 406 407 /* PnP resource says it decodes only 10 bits of address */ 408 switch (port & 0x3ff) { 409 case 0x130: 410 chip->wss_base = 0x530; 411 wss_base_bits = 0x00; 412 break; 413 case 0x204: 414 chip->wss_base = 0x604; 415 wss_base_bits = 0x03; 416 break; 417 case 0x280: 418 chip->wss_base = 0xe80; 419 wss_base_bits = 0x01; 420 break; 421 case 0x340: 422 chip->wss_base = 0xf40; 423 wss_base_bits = 0x02; 424 break; 425 default: 426 snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", port); 427 goto __skip_base; 428 } 429 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30); 430 431 __skip_base: 432 switch (irq) { 433 //#ifdef OPTi93X 434 case 5: 435 irq_bits = 0x05; 436 break; 437 //#endif /* OPTi93X */ 438 case 7: 439 irq_bits = 0x01; 440 break; 441 case 9: 442 irq_bits = 0x02; 443 break; 444 case 10: 445 irq_bits = 0x03; 446 break; 447 case 11: 448 irq_bits = 0x04; 449 break; 450 default: 451 snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq); 452 goto __skip_resources; 453 } 454 455 switch (dma1) { 456 case 0: 457 dma_bits = 0x01; 458 break; 459 case 1: 460 dma_bits = 0x02; 461 break; 462 case 3: 463 dma_bits = 0x03; 464 break; 465 default: 466 snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1); 467 goto __skip_resources; 468 } 469 470 #if defined(CS4231) || defined(OPTi93X) 471 if (dma1 == dma2) { 472 snd_printk(KERN_ERR "don't want to share dmas\n"); 473 return -EBUSY; 474 } 475 476 switch (dma2) { 477 case 0: 478 case 1: 479 break; 480 default: 481 snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2); 482 goto __skip_resources; 483 } 484 dma_bits |= 0x04; 485 #endif /* CS4231 || OPTi93X */ 486 487 #ifndef OPTi93X 488 outb(irq_bits << 3 | dma_bits, chip->wss_base); 489 #else /* OPTi93X */ 490 snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits)); 491 #endif /* OPTi93X */ 492 493 __skip_resources: 494 if (chip->hardware > OPTi9XX_HW_82C928) { 495 switch (mpu_port) { 496 case 0: 497 case -1: 498 break; 499 case 0x300: 500 mpu_port_bits = 0x03; 501 break; 502 case 0x310: 503 mpu_port_bits = 0x02; 504 break; 505 case 0x320: 506 mpu_port_bits = 0x01; 507 break; 508 case 0x330: 509 mpu_port_bits = 0x00; 510 break; 511 default: 512 snd_printk(KERN_WARNING 513 "MPU-401 port 0x%lx not valid\n", mpu_port); 514 goto __skip_mpu; 515 } 516 517 switch (mpu_irq) { 518 case 5: 519 mpu_irq_bits = 0x02; 520 break; 521 case 7: 522 mpu_irq_bits = 0x03; 523 break; 524 case 9: 525 mpu_irq_bits = 0x00; 526 break; 527 case 10: 528 mpu_irq_bits = 0x01; 529 break; 530 default: 531 snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n", 532 mpu_irq); 533 goto __skip_mpu; 534 } 535 536 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 537 (mpu_port <= 0) ? 0x00 : 538 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3, 539 0xf8); 540 } 541 __skip_mpu: 542 543 return 0; 544 } 545 546 #ifdef OPTi93X 547 548 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0); 549 static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0); 550 static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0); 551 552 static const struct snd_kcontrol_new snd_opti93x_controls[] = { 553 WSS_DOUBLE("Master Playback Switch", 0, 554 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1), 555 WSS_DOUBLE_TLV("Master Playback Volume", 0, 556 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1, 557 db_scale_5bit_3db_step), 558 WSS_DOUBLE_TLV("PCM Playback Volume", 0, 559 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1, 560 db_scale_5bit), 561 WSS_DOUBLE_TLV("FM Playback Volume", 0, 562 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1, 563 db_scale_4bit_12db_max), 564 WSS_DOUBLE("Line Playback Switch", 0, 565 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1), 566 WSS_DOUBLE_TLV("Line Playback Volume", 0, 567 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1, 568 db_scale_4bit_12db_max), 569 WSS_DOUBLE("Mic Playback Switch", 0, 570 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1), 571 WSS_DOUBLE_TLV("Mic Playback Volume", 0, 572 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1, 573 db_scale_4bit_12db_max), 574 WSS_DOUBLE_TLV("CD Playback Volume", 0, 575 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1, 576 db_scale_4bit_12db_max), 577 WSS_DOUBLE("Aux Playback Switch", 0, 578 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1), 579 WSS_DOUBLE_TLV("Aux Playback Volume", 0, 580 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1, 581 db_scale_4bit_12db_max), 582 }; 583 584 static int snd_opti93x_mixer(struct snd_wss *chip) 585 { 586 struct snd_card *card; 587 unsigned int idx; 588 struct snd_ctl_elem_id id1, id2; 589 int err; 590 591 if (snd_BUG_ON(!chip || !chip->pcm)) 592 return -EINVAL; 593 594 card = chip->card; 595 596 strcpy(card->mixername, chip->pcm->name); 597 598 memset(&id1, 0, sizeof(id1)); 599 memset(&id2, 0, sizeof(id2)); 600 id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 601 /* reassign AUX0 switch to CD */ 602 strcpy(id1.name, "Aux Playback Switch"); 603 strcpy(id2.name, "CD Playback Switch"); 604 err = snd_ctl_rename_id(card, &id1, &id2); 605 if (err < 0) { 606 snd_printk(KERN_ERR "Cannot rename opti93x control\n"); 607 return err; 608 } 609 /* reassign AUX1 switch to FM */ 610 strcpy(id1.name, "Aux Playback Switch"); id1.index = 1; 611 strcpy(id2.name, "FM Playback Switch"); 612 err = snd_ctl_rename_id(card, &id1, &id2); 613 if (err < 0) { 614 snd_printk(KERN_ERR "Cannot rename opti93x control\n"); 615 return err; 616 } 617 /* remove AUX1 volume */ 618 strcpy(id1.name, "Aux Playback Volume"); id1.index = 1; 619 snd_ctl_remove_id(card, &id1); 620 621 /* Replace WSS volume controls with OPTi93x volume controls */ 622 id1.index = 0; 623 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) { 624 strcpy(id1.name, snd_opti93x_controls[idx].name); 625 snd_ctl_remove_id(card, &id1); 626 627 err = snd_ctl_add(card, 628 snd_ctl_new1(&snd_opti93x_controls[idx], chip)); 629 if (err < 0) 630 return err; 631 } 632 return 0; 633 } 634 635 static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id) 636 { 637 struct snd_opti9xx *chip = dev_id; 638 struct snd_wss *codec = chip->codec; 639 unsigned char status; 640 641 if (!codec) 642 return IRQ_HANDLED; 643 644 status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11)); 645 if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream) 646 snd_pcm_period_elapsed(codec->playback_substream); 647 if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) { 648 snd_wss_overrange(codec); 649 snd_pcm_period_elapsed(codec->capture_substream); 650 } 651 outb(0x00, OPTi93X_PORT(codec, STATUS)); 652 return IRQ_HANDLED; 653 } 654 655 #endif /* OPTi93X */ 656 657 static int snd_opti9xx_read_check(struct snd_opti9xx *chip) 658 { 659 unsigned char value; 660 #ifdef OPTi93X 661 unsigned long flags; 662 #endif 663 664 chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, 665 "OPTi9xx MC"); 666 if (chip->res_mc_base == NULL) 667 return -EBUSY; 668 #ifndef OPTi93X 669 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)); 670 if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1))) 671 if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1))) 672 return 0; 673 #else /* OPTi93X */ 674 chip->res_mc_indir = request_region(chip->mc_indir_index, 2, 675 "OPTi93x MC"); 676 if (chip->res_mc_indir == NULL) 677 return -EBUSY; 678 679 spin_lock_irqsave(&chip->lock, flags); 680 outb(chip->password, chip->mc_base + chip->pwd_reg); 681 outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base); 682 spin_unlock_irqrestore(&chip->lock, flags); 683 684 value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)); 685 snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value); 686 if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value) 687 return 0; 688 689 release_and_free_resource(chip->res_mc_indir); 690 chip->res_mc_indir = NULL; 691 #endif /* OPTi93X */ 692 release_and_free_resource(chip->res_mc_base); 693 chip->res_mc_base = NULL; 694 695 return -ENODEV; 696 } 697 698 static int snd_card_opti9xx_detect(struct snd_card *card, 699 struct snd_opti9xx *chip) 700 { 701 int i, err; 702 703 #ifndef OPTi93X 704 for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) { 705 #else 706 for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) { 707 #endif 708 err = snd_opti9xx_init(chip, i); 709 if (err < 0) 710 return err; 711 712 err = snd_opti9xx_read_check(chip); 713 if (err == 0) 714 return 1; 715 #ifdef OPTi93X 716 chip->mc_indir_index = 0; 717 #endif 718 } 719 return -ENODEV; 720 } 721 722 #ifdef CONFIG_PNP 723 static int snd_card_opti9xx_pnp(struct snd_opti9xx *chip, 724 struct pnp_card_link *card, 725 const struct pnp_card_device_id *pid) 726 { 727 struct pnp_dev *pdev; 728 int err; 729 struct pnp_dev *devmpu; 730 #ifndef OPTi93X 731 struct pnp_dev *devmc; 732 #endif 733 734 pdev = pnp_request_card_device(card, pid->devs[0].id, NULL); 735 if (pdev == NULL) 736 return -EBUSY; 737 738 err = pnp_activate_dev(pdev); 739 if (err < 0) { 740 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err); 741 return err; 742 } 743 744 #ifdef OPTi93X 745 port = pnp_port_start(pdev, 0) - 4; 746 fm_port = pnp_port_start(pdev, 1) + 8; 747 /* adjust mc_indir_index - some cards report it at 0xe?d, 748 other at 0xe?c but it really is always at 0xe?e */ 749 chip->mc_indir_index = (pnp_port_start(pdev, 3) & ~0xf) | 0xe; 750 #else 751 devmc = pnp_request_card_device(card, pid->devs[2].id, NULL); 752 if (devmc == NULL) 753 return -EBUSY; 754 755 err = pnp_activate_dev(devmc); 756 if (err < 0) { 757 snd_printk(KERN_ERR "MC pnp configure failure: %d\n", err); 758 return err; 759 } 760 761 port = pnp_port_start(pdev, 1); 762 fm_port = pnp_port_start(pdev, 2) + 8; 763 /* 764 * The MC(0) is never accessed and card does not 765 * include it in the PnP resource range. OPTI93x include it. 766 */ 767 chip->mc_base = pnp_port_start(devmc, 0) - 1; 768 chip->mc_base_size = pnp_port_len(devmc, 0) + 1; 769 #endif /* OPTi93X */ 770 irq = pnp_irq(pdev, 0); 771 dma1 = pnp_dma(pdev, 0); 772 #if defined(CS4231) || defined(OPTi93X) 773 dma2 = pnp_dma(pdev, 1); 774 #endif /* CS4231 || OPTi93X */ 775 776 devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL); 777 778 if (devmpu && mpu_port > 0) { 779 err = pnp_activate_dev(devmpu); 780 if (err < 0) { 781 snd_printk(KERN_ERR "MPU401 pnp configure failure\n"); 782 mpu_port = -1; 783 } else { 784 mpu_port = pnp_port_start(devmpu, 0); 785 mpu_irq = pnp_irq(devmpu, 0); 786 } 787 } 788 return pid->driver_data; 789 } 790 #endif /* CONFIG_PNP */ 791 792 static void snd_card_opti9xx_free(struct snd_card *card) 793 { 794 struct snd_opti9xx *chip = card->private_data; 795 796 if (chip) { 797 #ifdef OPTi93X 798 if (chip->irq > 0) { 799 disable_irq(chip->irq); 800 free_irq(chip->irq, chip); 801 } 802 release_and_free_resource(chip->res_mc_indir); 803 #endif 804 release_and_free_resource(chip->res_mc_base); 805 } 806 } 807 808 static int snd_opti9xx_probe(struct snd_card *card) 809 { 810 static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1}; 811 int error; 812 int xdma2; 813 struct snd_opti9xx *chip = card->private_data; 814 struct snd_wss *codec; 815 struct snd_rawmidi *rmidi; 816 struct snd_hwdep *synth; 817 818 #if defined(CS4231) || defined(OPTi93X) 819 xdma2 = dma2; 820 #else 821 xdma2 = -1; 822 #endif 823 824 if (port == SNDRV_AUTO_PORT) { 825 port = snd_legacy_find_free_ioport(possible_ports, 4); 826 if (port < 0) { 827 snd_printk(KERN_ERR "unable to find a free WSS port\n"); 828 return -EBUSY; 829 } 830 } 831 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 832 mpu_port, mpu_irq); 833 if (error) 834 return error; 835 836 error = snd_wss_create(card, chip->wss_base + 4, -1, irq, dma1, xdma2, 837 #ifdef OPTi93X 838 WSS_HW_OPTI93X, WSS_HWSHARE_IRQ, 839 #else 840 WSS_HW_DETECT, 0, 841 #endif 842 &codec); 843 if (error < 0) 844 return error; 845 chip->codec = codec; 846 error = snd_wss_pcm(codec, 0); 847 if (error < 0) 848 return error; 849 error = snd_wss_mixer(codec); 850 if (error < 0) 851 return error; 852 #ifdef OPTi93X 853 error = snd_opti93x_mixer(codec); 854 if (error < 0) 855 return error; 856 #endif 857 #ifdef CS4231 858 error = snd_wss_timer(codec, 0); 859 if (error < 0) 860 return error; 861 #endif 862 #ifdef OPTi93X 863 error = request_irq(irq, snd_opti93x_interrupt, 864 0, DEV_NAME" - WSS", chip); 865 if (error < 0) { 866 snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq); 867 return error; 868 } 869 #endif 870 chip->irq = irq; 871 card->sync_irq = chip->irq; 872 strcpy(card->driver, chip->name); 873 sprintf(card->shortname, "OPTi %s", card->driver); 874 #if defined(CS4231) || defined(OPTi93X) 875 snprintf(card->longname, sizeof(card->longname), 876 "%s, %s at 0x%lx, irq %d, dma %d&%d", 877 card->shortname, codec->pcm->name, 878 chip->wss_base + 4, irq, dma1, xdma2); 879 #else 880 snprintf(card->longname, sizeof(card->longname), 881 "%s, %s at 0x%lx, irq %d, dma %d", 882 card->shortname, codec->pcm->name, chip->wss_base + 4, irq, 883 dma1); 884 #endif /* CS4231 || OPTi93X */ 885 886 if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT) 887 rmidi = NULL; 888 else { 889 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, 890 mpu_port, 0, mpu_irq, &rmidi); 891 if (error) 892 snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n", 893 mpu_port); 894 } 895 896 if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) { 897 struct snd_opl3 *opl3 = NULL; 898 #ifndef OPTi93X 899 if (chip->hardware == OPTi9XX_HW_82C928 || 900 chip->hardware == OPTi9XX_HW_82C929 || 901 chip->hardware == OPTi9XX_HW_82C924) { 902 struct snd_opl4 *opl4; 903 /* assume we have an OPL4 */ 904 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 905 0x20, 0x20); 906 if (snd_opl4_create(card, fm_port, fm_port - 8, 907 2, &opl3, &opl4) < 0) { 908 /* no luck, use OPL3 instead */ 909 snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 910 0x00, 0x20); 911 } 912 } 913 #endif /* !OPTi93X */ 914 if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2, 915 OPL3_HW_AUTO, 0, &opl3) < 0) { 916 snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n", 917 fm_port, fm_port + 4 - 1); 918 } 919 if (opl3) { 920 error = snd_opl3_hwdep_new(opl3, 0, 1, &synth); 921 if (error < 0) 922 return error; 923 } 924 } 925 926 return snd_card_register(card); 927 } 928 929 static int snd_opti9xx_card_new(struct device *pdev, struct snd_card **cardp) 930 { 931 struct snd_card *card; 932 int err; 933 934 err = snd_card_new(pdev, index, id, THIS_MODULE, 935 sizeof(struct snd_opti9xx), &card); 936 if (err < 0) 937 return err; 938 card->private_free = snd_card_opti9xx_free; 939 *cardp = card; 940 return 0; 941 } 942 943 static int snd_opti9xx_isa_match(struct device *devptr, 944 unsigned int dev) 945 { 946 #ifdef CONFIG_PNP 947 if (snd_opti9xx_pnp_is_probed) 948 return 0; 949 if (isapnp) 950 return 0; 951 #endif 952 return 1; 953 } 954 955 static int snd_opti9xx_isa_probe(struct device *devptr, 956 unsigned int dev) 957 { 958 struct snd_card *card; 959 int error; 960 static const long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1}; 961 #ifdef OPTi93X 962 static const int possible_irqs[] = {5, 9, 10, 11, 7, -1}; 963 #else 964 static const int possible_irqs[] = {9, 10, 11, 7, -1}; 965 #endif /* OPTi93X */ 966 static const int possible_mpu_irqs[] = {5, 9, 10, 7, -1}; 967 static const int possible_dma1s[] = {3, 1, 0, -1}; 968 #if defined(CS4231) || defined(OPTi93X) 969 static const int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}}; 970 #endif /* CS4231 || OPTi93X */ 971 972 if (mpu_port == SNDRV_AUTO_PORT) { 973 mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2); 974 if (mpu_port < 0) { 975 snd_printk(KERN_ERR "unable to find a free MPU401 port\n"); 976 return -EBUSY; 977 } 978 } 979 if (irq == SNDRV_AUTO_IRQ) { 980 irq = snd_legacy_find_free_irq(possible_irqs); 981 if (irq < 0) { 982 snd_printk(KERN_ERR "unable to find a free IRQ\n"); 983 return -EBUSY; 984 } 985 } 986 if (mpu_irq == SNDRV_AUTO_IRQ) { 987 mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs); 988 if (mpu_irq < 0) { 989 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n"); 990 return -EBUSY; 991 } 992 } 993 if (dma1 == SNDRV_AUTO_DMA) { 994 dma1 = snd_legacy_find_free_dma(possible_dma1s); 995 if (dma1 < 0) { 996 snd_printk(KERN_ERR "unable to find a free DMA1\n"); 997 return -EBUSY; 998 } 999 } 1000 #if defined(CS4231) || defined(OPTi93X) 1001 if (dma2 == SNDRV_AUTO_DMA) { 1002 dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]); 1003 if (dma2 < 0) { 1004 snd_printk(KERN_ERR "unable to find a free DMA2\n"); 1005 return -EBUSY; 1006 } 1007 } 1008 #endif 1009 1010 error = snd_opti9xx_card_new(devptr, &card); 1011 if (error < 0) 1012 return error; 1013 1014 error = snd_card_opti9xx_detect(card, card->private_data); 1015 if (error < 0) { 1016 snd_card_free(card); 1017 return error; 1018 } 1019 error = snd_opti9xx_probe(card); 1020 if (error < 0) { 1021 snd_card_free(card); 1022 return error; 1023 } 1024 dev_set_drvdata(devptr, card); 1025 return 0; 1026 } 1027 1028 static void snd_opti9xx_isa_remove(struct device *devptr, 1029 unsigned int dev) 1030 { 1031 snd_card_free(dev_get_drvdata(devptr)); 1032 } 1033 1034 #ifdef CONFIG_PM 1035 static int snd_opti9xx_suspend(struct snd_card *card) 1036 { 1037 struct snd_opti9xx *chip = card->private_data; 1038 1039 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1040 chip->codec->suspend(chip->codec); 1041 return 0; 1042 } 1043 1044 static int snd_opti9xx_resume(struct snd_card *card) 1045 { 1046 struct snd_opti9xx *chip = card->private_data; 1047 int error, xdma2; 1048 #if defined(CS4231) || defined(OPTi93X) 1049 xdma2 = dma2; 1050 #else 1051 xdma2 = -1; 1052 #endif 1053 1054 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 1055 mpu_port, mpu_irq); 1056 if (error) 1057 return error; 1058 chip->codec->resume(chip->codec); 1059 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1060 return 0; 1061 } 1062 1063 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n, 1064 pm_message_t state) 1065 { 1066 return snd_opti9xx_suspend(dev_get_drvdata(dev)); 1067 } 1068 1069 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n) 1070 { 1071 return snd_opti9xx_resume(dev_get_drvdata(dev)); 1072 } 1073 #endif 1074 1075 static struct isa_driver snd_opti9xx_driver = { 1076 .match = snd_opti9xx_isa_match, 1077 .probe = snd_opti9xx_isa_probe, 1078 .remove = snd_opti9xx_isa_remove, 1079 #ifdef CONFIG_PM 1080 .suspend = snd_opti9xx_isa_suspend, 1081 .resume = snd_opti9xx_isa_resume, 1082 #endif 1083 .driver = { 1084 .name = DEV_NAME 1085 }, 1086 }; 1087 1088 #ifdef CONFIG_PNP 1089 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard, 1090 const struct pnp_card_device_id *pid) 1091 { 1092 struct snd_card *card; 1093 int error, hw; 1094 struct snd_opti9xx *chip; 1095 1096 if (snd_opti9xx_pnp_is_probed) 1097 return -EBUSY; 1098 if (! isapnp) 1099 return -ENODEV; 1100 error = snd_opti9xx_card_new(&pcard->card->dev, &card); 1101 if (error < 0) 1102 return error; 1103 chip = card->private_data; 1104 1105 hw = snd_card_opti9xx_pnp(chip, pcard, pid); 1106 switch (hw) { 1107 case 0x0924: 1108 hw = OPTi9XX_HW_82C924; 1109 break; 1110 case 0x0925: 1111 hw = OPTi9XX_HW_82C925; 1112 break; 1113 case 0x0931: 1114 hw = OPTi9XX_HW_82C931; 1115 break; 1116 default: 1117 snd_card_free(card); 1118 return -ENODEV; 1119 } 1120 1121 error = snd_opti9xx_init(chip, hw); 1122 if (error) { 1123 snd_card_free(card); 1124 return error; 1125 } 1126 error = snd_opti9xx_read_check(chip); 1127 if (error) { 1128 snd_printk(KERN_ERR "OPTI chip not found\n"); 1129 snd_card_free(card); 1130 return error; 1131 } 1132 error = snd_opti9xx_probe(card); 1133 if (error < 0) { 1134 snd_card_free(card); 1135 return error; 1136 } 1137 pnp_set_card_drvdata(pcard, card); 1138 snd_opti9xx_pnp_is_probed = 1; 1139 return 0; 1140 } 1141 1142 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard) 1143 { 1144 snd_card_free(pnp_get_card_drvdata(pcard)); 1145 pnp_set_card_drvdata(pcard, NULL); 1146 snd_opti9xx_pnp_is_probed = 0; 1147 } 1148 1149 #ifdef CONFIG_PM 1150 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard, 1151 pm_message_t state) 1152 { 1153 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard)); 1154 } 1155 1156 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard) 1157 { 1158 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard)); 1159 } 1160 #endif 1161 1162 static struct pnp_card_driver opti9xx_pnpc_driver = { 1163 .flags = PNP_DRIVER_RES_DISABLE, 1164 .name = DEV_NAME, 1165 .id_table = snd_opti9xx_pnpids, 1166 .probe = snd_opti9xx_pnp_probe, 1167 .remove = snd_opti9xx_pnp_remove, 1168 #ifdef CONFIG_PM 1169 .suspend = snd_opti9xx_pnp_suspend, 1170 .resume = snd_opti9xx_pnp_resume, 1171 #endif 1172 }; 1173 #endif 1174 1175 #ifdef OPTi93X 1176 #define CHIP_NAME "82C93x" 1177 #else 1178 #define CHIP_NAME "82C92x" 1179 #endif 1180 1181 static int __init alsa_card_opti9xx_init(void) 1182 { 1183 #ifdef CONFIG_PNP 1184 pnp_register_card_driver(&opti9xx_pnpc_driver); 1185 if (snd_opti9xx_pnp_is_probed) 1186 return 0; 1187 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1188 #endif 1189 return isa_register_driver(&snd_opti9xx_driver, 1); 1190 } 1191 1192 static void __exit alsa_card_opti9xx_exit(void) 1193 { 1194 if (!snd_opti9xx_pnp_is_probed) { 1195 isa_unregister_driver(&snd_opti9xx_driver); 1196 return; 1197 } 1198 #ifdef CONFIG_PNP 1199 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1200 #endif 1201 } 1202 1203 module_init(alsa_card_opti9xx_init) 1204 module_exit(alsa_card_opti9xx_exit) 1205