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 if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) { 974 snd_printk(KERN_ERR "unable to find a free MPU401 port\n"); 975 return -EBUSY; 976 } 977 } 978 if (irq == SNDRV_AUTO_IRQ) { 979 if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) { 980 snd_printk(KERN_ERR "unable to find a free IRQ\n"); 981 return -EBUSY; 982 } 983 } 984 if (mpu_irq == SNDRV_AUTO_IRQ) { 985 if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) { 986 snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n"); 987 return -EBUSY; 988 } 989 } 990 if (dma1 == SNDRV_AUTO_DMA) { 991 if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) { 992 snd_printk(KERN_ERR "unable to find a free DMA1\n"); 993 return -EBUSY; 994 } 995 } 996 #if defined(CS4231) || defined(OPTi93X) 997 if (dma2 == SNDRV_AUTO_DMA) { 998 if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) { 999 snd_printk(KERN_ERR "unable to find a free DMA2\n"); 1000 return -EBUSY; 1001 } 1002 } 1003 #endif 1004 1005 error = snd_opti9xx_card_new(devptr, &card); 1006 if (error < 0) 1007 return error; 1008 1009 if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) { 1010 snd_card_free(card); 1011 return error; 1012 } 1013 if ((error = snd_opti9xx_probe(card)) < 0) { 1014 snd_card_free(card); 1015 return error; 1016 } 1017 dev_set_drvdata(devptr, card); 1018 return 0; 1019 } 1020 1021 static void snd_opti9xx_isa_remove(struct device *devptr, 1022 unsigned int dev) 1023 { 1024 snd_card_free(dev_get_drvdata(devptr)); 1025 } 1026 1027 #ifdef CONFIG_PM 1028 static int snd_opti9xx_suspend(struct snd_card *card) 1029 { 1030 struct snd_opti9xx *chip = card->private_data; 1031 1032 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1033 chip->codec->suspend(chip->codec); 1034 return 0; 1035 } 1036 1037 static int snd_opti9xx_resume(struct snd_card *card) 1038 { 1039 struct snd_opti9xx *chip = card->private_data; 1040 int error, xdma2; 1041 #if defined(CS4231) || defined(OPTi93X) 1042 xdma2 = dma2; 1043 #else 1044 xdma2 = -1; 1045 #endif 1046 1047 error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2, 1048 mpu_port, mpu_irq); 1049 if (error) 1050 return error; 1051 chip->codec->resume(chip->codec); 1052 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1053 return 0; 1054 } 1055 1056 static int snd_opti9xx_isa_suspend(struct device *dev, unsigned int n, 1057 pm_message_t state) 1058 { 1059 return snd_opti9xx_suspend(dev_get_drvdata(dev)); 1060 } 1061 1062 static int snd_opti9xx_isa_resume(struct device *dev, unsigned int n) 1063 { 1064 return snd_opti9xx_resume(dev_get_drvdata(dev)); 1065 } 1066 #endif 1067 1068 static struct isa_driver snd_opti9xx_driver = { 1069 .match = snd_opti9xx_isa_match, 1070 .probe = snd_opti9xx_isa_probe, 1071 .remove = snd_opti9xx_isa_remove, 1072 #ifdef CONFIG_PM 1073 .suspend = snd_opti9xx_isa_suspend, 1074 .resume = snd_opti9xx_isa_resume, 1075 #endif 1076 .driver = { 1077 .name = DEV_NAME 1078 }, 1079 }; 1080 1081 #ifdef CONFIG_PNP 1082 static int snd_opti9xx_pnp_probe(struct pnp_card_link *pcard, 1083 const struct pnp_card_device_id *pid) 1084 { 1085 struct snd_card *card; 1086 int error, hw; 1087 struct snd_opti9xx *chip; 1088 1089 if (snd_opti9xx_pnp_is_probed) 1090 return -EBUSY; 1091 if (! isapnp) 1092 return -ENODEV; 1093 error = snd_opti9xx_card_new(&pcard->card->dev, &card); 1094 if (error < 0) 1095 return error; 1096 chip = card->private_data; 1097 1098 hw = snd_card_opti9xx_pnp(chip, pcard, pid); 1099 switch (hw) { 1100 case 0x0924: 1101 hw = OPTi9XX_HW_82C924; 1102 break; 1103 case 0x0925: 1104 hw = OPTi9XX_HW_82C925; 1105 break; 1106 case 0x0931: 1107 hw = OPTi9XX_HW_82C931; 1108 break; 1109 default: 1110 snd_card_free(card); 1111 return -ENODEV; 1112 } 1113 1114 if ((error = snd_opti9xx_init(chip, hw))) { 1115 snd_card_free(card); 1116 return error; 1117 } 1118 error = snd_opti9xx_read_check(chip); 1119 if (error) { 1120 snd_printk(KERN_ERR "OPTI chip not found\n"); 1121 snd_card_free(card); 1122 return error; 1123 } 1124 if ((error = snd_opti9xx_probe(card)) < 0) { 1125 snd_card_free(card); 1126 return error; 1127 } 1128 pnp_set_card_drvdata(pcard, card); 1129 snd_opti9xx_pnp_is_probed = 1; 1130 return 0; 1131 } 1132 1133 static void snd_opti9xx_pnp_remove(struct pnp_card_link *pcard) 1134 { 1135 snd_card_free(pnp_get_card_drvdata(pcard)); 1136 pnp_set_card_drvdata(pcard, NULL); 1137 snd_opti9xx_pnp_is_probed = 0; 1138 } 1139 1140 #ifdef CONFIG_PM 1141 static int snd_opti9xx_pnp_suspend(struct pnp_card_link *pcard, 1142 pm_message_t state) 1143 { 1144 return snd_opti9xx_suspend(pnp_get_card_drvdata(pcard)); 1145 } 1146 1147 static int snd_opti9xx_pnp_resume(struct pnp_card_link *pcard) 1148 { 1149 return snd_opti9xx_resume(pnp_get_card_drvdata(pcard)); 1150 } 1151 #endif 1152 1153 static struct pnp_card_driver opti9xx_pnpc_driver = { 1154 .flags = PNP_DRIVER_RES_DISABLE, 1155 .name = DEV_NAME, 1156 .id_table = snd_opti9xx_pnpids, 1157 .probe = snd_opti9xx_pnp_probe, 1158 .remove = snd_opti9xx_pnp_remove, 1159 #ifdef CONFIG_PM 1160 .suspend = snd_opti9xx_pnp_suspend, 1161 .resume = snd_opti9xx_pnp_resume, 1162 #endif 1163 }; 1164 #endif 1165 1166 #ifdef OPTi93X 1167 #define CHIP_NAME "82C93x" 1168 #else 1169 #define CHIP_NAME "82C92x" 1170 #endif 1171 1172 static int __init alsa_card_opti9xx_init(void) 1173 { 1174 #ifdef CONFIG_PNP 1175 pnp_register_card_driver(&opti9xx_pnpc_driver); 1176 if (snd_opti9xx_pnp_is_probed) 1177 return 0; 1178 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1179 #endif 1180 return isa_register_driver(&snd_opti9xx_driver, 1); 1181 } 1182 1183 static void __exit alsa_card_opti9xx_exit(void) 1184 { 1185 if (!snd_opti9xx_pnp_is_probed) { 1186 isa_unregister_driver(&snd_opti9xx_driver); 1187 return; 1188 } 1189 #ifdef CONFIG_PNP 1190 pnp_unregister_card_driver(&opti9xx_pnpc_driver); 1191 #endif 1192 } 1193 1194 module_init(alsa_card_opti9xx_init) 1195 module_exit(alsa_card_opti9xx_exit) 1196