1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Low-level ALSA driver for the ENSONIQ SoundScape 4 * Copyright (c) by Chris Rankin 5 * 6 * This driver was written in part using information obtained from 7 * the OSS/Free SoundScape driver, written by Hannu Savolainen. 8 */ 9 10 #include <linux/init.h> 11 #include <linux/err.h> 12 #include <linux/io.h> 13 #include <linux/isa.h> 14 #include <linux/delay.h> 15 #include <linux/firmware.h> 16 #include <linux/pnp.h> 17 #include <linux/spinlock.h> 18 #include <linux/module.h> 19 #include <asm/dma.h> 20 #include <sound/core.h> 21 #include <sound/wss.h> 22 #include <sound/mpu401.h> 23 #include <sound/initval.h> 24 25 26 MODULE_AUTHOR("Chris Rankin"); 27 MODULE_DESCRIPTION("ENSONIQ SoundScape driver"); 28 MODULE_LICENSE("GPL"); 29 MODULE_FIRMWARE("sndscape.co0"); 30 MODULE_FIRMWARE("sndscape.co1"); 31 MODULE_FIRMWARE("sndscape.co2"); 32 MODULE_FIRMWARE("sndscape.co3"); 33 MODULE_FIRMWARE("sndscape.co4"); 34 MODULE_FIRMWARE("scope.cod"); 35 36 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 37 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 38 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; 39 static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; 40 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; 41 static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; 42 static int dma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; 43 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; 44 static bool joystick[SNDRV_CARDS]; 45 46 module_param_array(index, int, NULL, 0444); 47 MODULE_PARM_DESC(index, "Index number for SoundScape soundcard"); 48 49 module_param_array(id, charp, NULL, 0444); 50 MODULE_PARM_DESC(id, "Description for SoundScape card"); 51 52 module_param_hw_array(port, long, ioport, NULL, 0444); 53 MODULE_PARM_DESC(port, "Port # for SoundScape driver."); 54 55 module_param_hw_array(wss_port, long, ioport, NULL, 0444); 56 MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver."); 57 58 module_param_hw_array(irq, int, irq, NULL, 0444); 59 MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver."); 60 61 module_param_hw_array(mpu_irq, int, irq, NULL, 0444); 62 MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver."); 63 64 module_param_hw_array(dma, int, dma, NULL, 0444); 65 MODULE_PARM_DESC(dma, "DMA # for SoundScape driver."); 66 67 module_param_hw_array(dma2, int, dma, NULL, 0444); 68 MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver."); 69 70 module_param_array(joystick, bool, NULL, 0444); 71 MODULE_PARM_DESC(joystick, "Enable gameport."); 72 73 #ifdef CONFIG_PNP 74 static int isa_registered; 75 static int pnp_registered; 76 77 static const struct pnp_card_device_id sscape_pnpids[] = { 78 { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */ 79 { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */ 80 { .id = "" } /* end */ 81 }; 82 83 MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids); 84 #endif 85 86 87 #define HOST_CTRL_IO(i) ((i) + 2) 88 #define HOST_DATA_IO(i) ((i) + 3) 89 #define ODIE_ADDR_IO(i) ((i) + 4) 90 #define ODIE_DATA_IO(i) ((i) + 5) 91 #define CODEC_IO(i) ((i) + 8) 92 93 #define IC_ODIE 1 94 #define IC_OPUS 2 95 96 #define RX_READY 0x01 97 #define TX_READY 0x02 98 99 #define CMD_ACK 0x80 100 #define CMD_SET_MIDI_VOL 0x84 101 #define CMD_GET_MIDI_VOL 0x85 102 #define CMD_XXX_MIDI_VOL 0x86 103 #define CMD_SET_EXTMIDI 0x8a 104 #define CMD_GET_EXTMIDI 0x8b 105 #define CMD_SET_MT32 0x8c 106 #define CMD_GET_MT32 0x8d 107 108 enum GA_REG { 109 GA_INTSTAT_REG = 0, 110 GA_INTENA_REG, 111 GA_DMAA_REG, 112 GA_DMAB_REG, 113 GA_INTCFG_REG, 114 GA_DMACFG_REG, 115 GA_CDCFG_REG, 116 GA_SMCFGA_REG, 117 GA_SMCFGB_REG, 118 GA_HMCTL_REG 119 }; 120 121 #define DMA_8BIT 0x80 122 123 124 enum card_type { 125 MEDIA_FX, /* Sequoia S-1000 */ 126 SSCAPE, /* Sequoia S-2000 */ 127 SSCAPE_PNP, 128 SSCAPE_VIVO, 129 }; 130 131 struct soundscape { 132 spinlock_t lock; 133 unsigned io_base; 134 unsigned long wss_base; 135 int irq; 136 int mpu_irq; 137 int dma1; 138 int dma2; 139 int ic_type; 140 enum card_type type; 141 struct resource *io_res; 142 struct resource *wss_res; 143 struct snd_wss *chip; 144 145 unsigned char midi_vol; 146 bool joystick; 147 bool midi_enabled; 148 struct device *dev; 149 }; 150 151 #define INVALID_IRQ ((unsigned)-1) 152 153 154 static inline struct soundscape *get_card_soundscape(struct snd_card *c) 155 { 156 return (struct soundscape *) (c->private_data); 157 } 158 159 /* 160 * Store the resolved board settings in the per-card state so that 161 * the same configuration can be replayed later if necessary. 162 */ 163 static void sscape_store_settings(struct soundscape *sscape, int dev) 164 { 165 sscape->io_base = port[dev]; 166 sscape->wss_base = wss_port[dev]; 167 sscape->irq = irq[dev]; 168 sscape->mpu_irq = mpu_irq[dev]; 169 sscape->dma1 = dma[dev]; 170 sscape->dma2 = dma2[dev]; 171 sscape->joystick = joystick[dev]; 172 } 173 174 /* 175 * Allocates some kernel memory that we can use for DMA. 176 * I think this means that the memory has to map to 177 * contiguous pages of physical memory. 178 */ 179 static struct snd_dma_buffer *get_dmabuf(struct soundscape *s, 180 struct snd_dma_buffer *buf, 181 unsigned long size) 182 { 183 if (buf) { 184 if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, 185 s->chip->card->dev, 186 size, buf) < 0) { 187 dev_err(s->dev, 188 "sscape: Failed to allocate %lu bytes for DMA\n", 189 size); 190 return NULL; 191 } 192 } 193 194 return buf; 195 } 196 197 /* 198 * Release the DMA-able kernel memory ... 199 */ 200 static void free_dmabuf(struct snd_dma_buffer *buf) 201 { 202 if (buf && buf->area) 203 snd_dma_free_pages(buf); 204 } 205 206 /* 207 * This function writes to the SoundScape's control registers, 208 * but doesn't do any locking. It's up to the caller to do that. 209 * This is why this function is "unsafe" ... 210 */ 211 static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, 212 unsigned char val) 213 { 214 outb(reg, ODIE_ADDR_IO(io_base)); 215 outb(val, ODIE_DATA_IO(io_base)); 216 } 217 218 /* 219 * Write to the SoundScape's control registers, and do the 220 * necessary locking ... 221 */ 222 static void sscape_write(struct soundscape *s, enum GA_REG reg, 223 unsigned char val) 224 { 225 guard(spinlock_irqsave)(&s->lock); 226 sscape_write_unsafe(s->io_base, reg, val); 227 } 228 229 /* 230 * Read from the SoundScape's control registers, but leave any 231 * locking to the caller. This is why the function is "unsafe" ... 232 */ 233 static inline unsigned char sscape_read_unsafe(unsigned io_base, 234 enum GA_REG reg) 235 { 236 outb(reg, ODIE_ADDR_IO(io_base)); 237 return inb(ODIE_DATA_IO(io_base)); 238 } 239 240 /* 241 * Puts the SoundScape into "host" mode, as compared to "MIDI" mode 242 */ 243 static inline void set_host_mode_unsafe(unsigned io_base) 244 { 245 outb(0x0, HOST_CTRL_IO(io_base)); 246 } 247 248 /* 249 * Puts the SoundScape into "MIDI" mode, as compared to "host" mode 250 */ 251 static inline void set_midi_mode_unsafe(unsigned io_base) 252 { 253 outb(0x3, HOST_CTRL_IO(io_base)); 254 } 255 256 /* 257 * Read the SoundScape's host-mode control register, but leave 258 * any locking issues to the caller ... 259 */ 260 static inline int host_read_unsafe(unsigned io_base) 261 { 262 int data = -1; 263 if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) 264 data = inb(HOST_DATA_IO(io_base)); 265 266 return data; 267 } 268 269 /* 270 * Read the SoundScape's host-mode control register, performing 271 * a limited amount of busy-waiting if the register isn't ready. 272 * Also leaves all locking-issues to the caller ... 273 */ 274 static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout) 275 { 276 int data; 277 278 while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) { 279 udelay(100); 280 --timeout; 281 } /* while */ 282 283 return data; 284 } 285 286 /* 287 * Write to the SoundScape's host-mode control registers, but 288 * leave any locking issues to the caller. Returns true if 289 * the write succeeded. 290 */ 291 static inline bool host_write_unsafe(unsigned int io_base, unsigned char data) 292 { 293 if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) { 294 outb(data, HOST_DATA_IO(io_base)); 295 return true; 296 } 297 298 return false; 299 } 300 301 /* 302 * Write to the SoundScape's host-mode control registers, performing 303 * a limited amount of busy-waiting if the register isn't ready. 304 * Also leaves all locking-issues to the caller. Returns true if 305 * the write succeeded before timing out. 306 */ 307 static bool host_write_ctrl_unsafe(unsigned int io_base, unsigned char data, 308 unsigned int timeout) 309 { 310 bool written; 311 312 while (!(written = host_write_unsafe(io_base, data)) && timeout != 0) { 313 udelay(100); 314 --timeout; 315 } /* while */ 316 317 return written; 318 } 319 320 321 /* 322 * Check that the MIDI subsystem is operational. If it isn't, 323 * then we will hang the computer if we try to use it ... 324 * 325 * NOTE: This check is based upon observation, not documentation. 326 */ 327 static inline int verify_mpu401(const struct snd_mpu401 *mpu) 328 { 329 return ((inb(MPU401C(mpu)) & 0xc0) == 0x80); 330 } 331 332 /* 333 * This is apparently the standard way to initialise an MPU-401 334 */ 335 static inline void initialise_mpu401(const struct snd_mpu401 *mpu) 336 { 337 outb(0, MPU401D(mpu)); 338 } 339 340 /* 341 * Tell the SoundScape to activate the AD1845 chip (I think). 342 * The AD1845 detection fails if we *don't* do this, so I 343 * think that this is a good idea ... 344 */ 345 static void activate_ad1845_unsafe(unsigned io_base) 346 { 347 unsigned char val = sscape_read_unsafe(io_base, GA_HMCTL_REG); 348 sscape_write_unsafe(io_base, GA_HMCTL_REG, (val & 0xcf) | 0x10); 349 sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80); 350 } 351 352 /* 353 * Tell the SoundScape to begin a DMA transfer using the given channel. 354 * All locking issues are left to the caller. 355 */ 356 static void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg) 357 { 358 sscape_write_unsafe(io_base, reg, 359 sscape_read_unsafe(io_base, reg) | 0x01); 360 sscape_write_unsafe(io_base, reg, 361 sscape_read_unsafe(io_base, reg) & 0xfe); 362 } 363 364 /* 365 * Wait for a DMA transfer to complete. This is a "limited busy-wait", 366 * and all locking issues are left to the caller. 367 */ 368 static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, 369 unsigned timeout) 370 { 371 while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) { 372 udelay(100); 373 --timeout; 374 } /* while */ 375 376 return sscape_read_unsafe(io_base, reg) & 0x01; 377 } 378 379 /* 380 * Wait for the On-Board Processor to return its start-up 381 * acknowledgement sequence. This wait is too long for 382 * us to perform "busy-waiting", and so we must sleep. 383 * This in turn means that we must not be holding any 384 * spinlocks when we call this function. 385 */ 386 static int obp_startup_ack(struct soundscape *s, unsigned timeout) 387 { 388 unsigned long end_time = jiffies + msecs_to_jiffies(timeout); 389 390 do { 391 int x; 392 393 scoped_guard(spinlock_irqsave, &s->lock) { 394 x = host_read_unsafe(s->io_base); 395 } 396 if (x == 0xfe || x == 0xff) 397 return 1; 398 399 msleep(10); 400 } while (time_before(jiffies, end_time)); 401 402 return 0; 403 } 404 405 /* 406 * Wait for the host to return its start-up acknowledgement 407 * sequence. This wait is too long for us to perform 408 * "busy-waiting", and so we must sleep. This in turn means 409 * that we must not be holding any spinlocks when we call 410 * this function. 411 */ 412 static int host_startup_ack(struct soundscape *s, unsigned timeout) 413 { 414 unsigned long end_time = jiffies + msecs_to_jiffies(timeout); 415 416 do { 417 int x; 418 419 scoped_guard(spinlock_irqsave, &s->lock) { 420 x = host_read_unsafe(s->io_base); 421 } 422 if (x == 0xfe) 423 return 1; 424 425 msleep(10); 426 } while (time_before(jiffies, end_time)); 427 428 return 0; 429 } 430 431 /* 432 * Upload a byte-stream into the SoundScape using DMA channel A. 433 */ 434 static int upload_dma_data(struct soundscape *s, const unsigned char *data, 435 size_t size) 436 { 437 struct snd_dma_buffer dma; 438 int ret; 439 unsigned char val; 440 441 if (!get_dmabuf(s, &dma, PAGE_ALIGN(32 * 1024))) 442 return -ENOMEM; 443 444 scoped_guard(spinlock_irqsave, &s->lock) { 445 446 /* 447 * Reset the board ... 448 */ 449 val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); 450 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val & 0x3f); 451 452 /* 453 * Enable the DMA channels and configure them ... 454 */ 455 val = (s->chip->dma1 << 4) | DMA_8BIT; 456 sscape_write_unsafe(s->io_base, GA_DMAA_REG, val); 457 sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20); 458 459 /* 460 * Take the board out of reset ... 461 */ 462 val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); 463 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val | 0x80); 464 465 /* 466 * Upload the firmware to the SoundScape 467 * board through the DMA channel ... 468 */ 469 while (size != 0) { 470 unsigned long len; 471 472 len = min(size, dma.bytes); 473 memcpy(dma.area, data, len); 474 data += len; 475 size -= len; 476 477 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE); 478 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG); 479 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) { 480 dev_err(s->dev, "sscape: DMA upload has timed out\n"); 481 ret = -EAGAIN; 482 goto _release_dma; 483 } 484 } /* while */ 485 486 set_host_mode_unsafe(s->io_base); 487 outb(0x0, s->io_base); 488 489 /* 490 * Boot the board ... (I think) 491 */ 492 val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); 493 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val | 0x40); 494 } 495 496 /* 497 * If all has gone well, then the board should acknowledge 498 * the new upload and tell us that it has rebooted OK. We 499 * give it 5 seconds (max) ... 500 */ 501 ret = 0; 502 if (!obp_startup_ack(s, 5000)) { 503 dev_err(s->dev, 504 "sscape: No response from on-board processor after upload\n"); 505 ret = -EAGAIN; 506 } else if (!host_startup_ack(s, 5000)) { 507 dev_err(s->dev, "sscape: SoundScape failed to initialise\n"); 508 ret = -EAGAIN; 509 } 510 511 _release_dma: 512 /* 513 * NOTE!!! We are NOT holding any spinlocks at this point !!! 514 */ 515 sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_OPUS ? 0x40 : 0x70)); 516 free_dmabuf(&dma); 517 518 return ret; 519 } 520 521 /* 522 * Upload the bootblock(?) into the SoundScape. The only 523 * purpose of this block of code seems to be to tell 524 * us which version of the microcode we should be using. 525 */ 526 static int sscape_upload_bootblock(struct snd_card *card) 527 { 528 struct soundscape *sscape = get_card_soundscape(card); 529 const struct firmware *init_fw = NULL; 530 int data = 0; 531 int ret; 532 533 ret = request_firmware(&init_fw, "scope.cod", card->dev); 534 if (ret < 0) { 535 dev_err(card->dev, "sscape: Error loading scope.cod"); 536 return ret; 537 } 538 ret = upload_dma_data(sscape, init_fw->data, init_fw->size); 539 540 release_firmware(init_fw); 541 542 guard(spinlock_irqsave)(&sscape->lock); 543 if (ret == 0) 544 data = host_read_ctrl_unsafe(sscape->io_base, 100); 545 546 if (data & 0x10) 547 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2f); 548 549 data &= 0xf; 550 if (ret == 0 && data > 7) { 551 dev_err(card->dev, 552 "sscape: timeout reading firmware version\n"); 553 ret = -EAGAIN; 554 } 555 556 return (ret == 0) ? data : ret; 557 } 558 559 /* 560 * Upload the microcode into the SoundScape. 561 */ 562 static int sscape_upload_microcode(struct snd_card *card, int version) 563 { 564 struct soundscape *sscape = get_card_soundscape(card); 565 const struct firmware *init_fw = NULL; 566 char name[14]; 567 int err; 568 569 scnprintf(name, sizeof(name), "sndscape.co%d", version); 570 571 err = request_firmware(&init_fw, name, card->dev); 572 if (err < 0) { 573 dev_err(card->dev, "sscape: Error loading sndscape.co%d", 574 version); 575 return err; 576 } 577 err = upload_dma_data(sscape, init_fw->data, init_fw->size); 578 if (err == 0) 579 dev_info(card->dev, "sscape: MIDI firmware loaded %zu KBs\n", 580 init_fw->size >> 10); 581 582 release_firmware(init_fw); 583 584 return err; 585 } 586 587 /* 588 * Restore the SoundScape's MIDI control state after the firmware 589 * upload has made the host interface available again. 590 */ 591 static int sscape_restore_midi_state(struct soundscape *sscape) 592 { 593 bool success; 594 595 guard(spinlock_irqsave)(&sscape->lock); 596 set_host_mode_unsafe(sscape->io_base); 597 598 success = host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100) && 599 host_write_ctrl_unsafe(sscape->io_base, sscape->midi_vol, 100) && 600 host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100) && 601 host_write_ctrl_unsafe(sscape->io_base, sscape->midi_vol, 100) && 602 host_write_ctrl_unsafe(sscape->io_base, CMD_SET_EXTMIDI, 100) && 603 host_write_ctrl_unsafe(sscape->io_base, 0, 100) && 604 host_write_ctrl_unsafe(sscape->io_base, CMD_ACK, 100); 605 606 set_midi_mode_unsafe(sscape->io_base); 607 608 return success ? 0 : -EIO; 609 } 610 611 /* 612 * Mixer control for the SoundScape's MIDI device. 613 */ 614 static int sscape_midi_info(struct snd_kcontrol *ctl, 615 struct snd_ctl_elem_info *uinfo) 616 { 617 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 618 uinfo->count = 1; 619 uinfo->value.integer.min = 0; 620 uinfo->value.integer.max = 127; 621 return 0; 622 } 623 624 static int sscape_midi_get(struct snd_kcontrol *kctl, 625 struct snd_ctl_elem_value *uctl) 626 { 627 struct snd_wss *chip = snd_kcontrol_chip(kctl); 628 struct snd_card *card = chip->card; 629 register struct soundscape *s = get_card_soundscape(card); 630 631 guard(spinlock_irqsave)(&s->lock); 632 uctl->value.integer.value[0] = s->midi_vol; 633 return 0; 634 } 635 636 static int sscape_midi_put(struct snd_kcontrol *kctl, 637 struct snd_ctl_elem_value *uctl) 638 { 639 struct snd_wss *chip = snd_kcontrol_chip(kctl); 640 struct snd_card *card = chip->card; 641 struct soundscape *s = get_card_soundscape(card); 642 int change; 643 unsigned char new_val; 644 645 guard(spinlock_irqsave)(&s->lock); 646 647 new_val = uctl->value.integer.value[0] & 127; 648 /* 649 * We need to put the board into HOST mode before we 650 * can send any volume-changing HOST commands ... 651 */ 652 set_host_mode_unsafe(s->io_base); 653 654 /* 655 * To successfully change the MIDI volume setting, you seem to 656 * have to write a volume command, write the new volume value, 657 * and then perform another volume-related command. Perhaps the 658 * first command is an "open" and the second command is a "close"? 659 */ 660 if (s->midi_vol == new_val) { 661 change = 0; 662 goto __skip_change; 663 } 664 change = host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100) 665 && host_write_ctrl_unsafe(s->io_base, new_val, 100) 666 && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100) 667 && host_write_ctrl_unsafe(s->io_base, new_val, 100); 668 s->midi_vol = new_val; 669 __skip_change: 670 671 /* 672 * Take the board out of HOST mode and back into MIDI mode ... 673 */ 674 set_midi_mode_unsafe(s->io_base); 675 676 return change; 677 } 678 679 static const struct snd_kcontrol_new midi_mixer_ctl = { 680 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 681 .name = "MIDI", 682 .info = sscape_midi_info, 683 .get = sscape_midi_get, 684 .put = sscape_midi_put 685 }; 686 687 /* 688 * The SoundScape can use two IRQs from a possible set of four. 689 * These IRQs are encoded as bit patterns so that they can be 690 * written to the control registers. 691 */ 692 static unsigned get_irq_config(int sscape_type, int irq) 693 { 694 static const int valid_irq[] = { 9, 5, 7, 10 }; 695 static const int old_irq[] = { 9, 7, 5, 15 }; 696 unsigned cfg; 697 698 if (sscape_type == MEDIA_FX) { 699 for (cfg = 0; cfg < ARRAY_SIZE(old_irq); ++cfg) 700 if (irq == old_irq[cfg]) 701 return cfg; 702 } else { 703 for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) 704 if (irq == valid_irq[cfg]) 705 return cfg; 706 } 707 708 return INVALID_IRQ; 709 } 710 711 /* 712 * Program the SoundScape's board-specific routing and enable the 713 * codec path using the resolved IRQ, DMA and joystick settings. 714 */ 715 static int sscape_configure_board(struct soundscape *sscape) 716 { 717 unsigned int dma_cfg; 718 unsigned int irq_cfg; 719 unsigned int mpu_irq_cfg; 720 int val; 721 722 irq_cfg = get_irq_config(sscape->type, sscape->irq); 723 if (irq_cfg == INVALID_IRQ) 724 return -ENXIO; 725 726 mpu_irq_cfg = get_irq_config(sscape->type, sscape->mpu_irq); 727 if (mpu_irq_cfg == INVALID_IRQ) 728 return -ENXIO; 729 730 scoped_guard(spinlock_irqsave, &sscape->lock) { 731 if (sscape->ic_type == IC_OPUS) 732 activate_ad1845_unsafe(sscape->io_base); 733 734 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e); 735 sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00); 736 737 /* 738 * Enable and configure the DMA channels ... 739 */ 740 sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50); 741 dma_cfg = (sscape->ic_type == IC_OPUS ? 0x40 : 0x70); 742 sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg); 743 sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20); 744 745 mpu_irq_cfg |= mpu_irq_cfg << 2; 746 val = sscape_read_unsafe(sscape->io_base, GA_HMCTL_REG) & 0xf7; 747 if (sscape->joystick) 748 val |= 0x08; 749 sscape_write_unsafe(sscape->io_base, GA_HMCTL_REG, val | 0xd0); 750 sscape_write_unsafe(sscape->io_base, GA_INTCFG_REG, 751 0xf0 | mpu_irq_cfg); 752 sscape_write_unsafe(sscape->io_base, GA_CDCFG_REG, 753 0x09 | DMA_8BIT | 754 (sscape->dma1 << 4) | (irq_cfg << 1)); 755 /* 756 * Enable the master IRQ ... 757 */ 758 sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x80); 759 } 760 761 return 0; 762 } 763 764 /* 765 * Perform certain arcane port-checks to see whether there 766 * is a SoundScape board lurking behind the given ports. 767 */ 768 static int detect_sscape(struct soundscape *s, long wss_io) 769 { 770 unsigned long flags; 771 unsigned d; 772 int retval = 0; 773 774 spin_lock_irqsave(&s->lock, flags); 775 776 /* 777 * The following code is lifted from the original OSS driver, 778 * and as I don't have a datasheet I cannot really comment 779 * on what it is doing... 780 */ 781 if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0) 782 goto _done; 783 784 d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0; 785 if ((d & 0x80) != 0) 786 goto _done; 787 788 if (d == 0) 789 s->ic_type = IC_ODIE; 790 else if ((d & 0x60) != 0) 791 s->ic_type = IC_OPUS; 792 else 793 goto _done; 794 795 outb(0xfa, ODIE_ADDR_IO(s->io_base)); 796 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a) 797 goto _done; 798 799 outb(0xfe, ODIE_ADDR_IO(s->io_base)); 800 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e) 801 goto _done; 802 803 outb(0xfe, ODIE_ADDR_IO(s->io_base)); 804 d = inb(ODIE_DATA_IO(s->io_base)); 805 if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e) 806 goto _done; 807 808 if (s->ic_type == IC_OPUS) 809 activate_ad1845_unsafe(s->io_base); 810 811 if (s->type == SSCAPE_VIVO) 812 wss_io += 4; 813 814 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); 815 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0); 816 817 /* wait for WSS codec */ 818 for (d = 0; d < 500; d++) { 819 if ((inb(wss_io) & 0x80) == 0) 820 break; 821 spin_unlock_irqrestore(&s->lock, flags); 822 msleep(1); 823 spin_lock_irqsave(&s->lock, flags); 824 } 825 826 if ((inb(wss_io) & 0x80) != 0) 827 goto _done; 828 829 if (inb(wss_io + 2) == 0xff) 830 goto _done; 831 832 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f; 833 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d); 834 835 if ((inb(wss_io) & 0x80) != 0) 836 s->type = MEDIA_FX; 837 838 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG); 839 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0); 840 /* wait for WSS codec */ 841 for (d = 0; d < 500; d++) { 842 if ((inb(wss_io) & 0x80) == 0) 843 break; 844 spin_unlock_irqrestore(&s->lock, flags); 845 msleep(1); 846 spin_lock_irqsave(&s->lock, flags); 847 } 848 849 /* 850 * SoundScape successfully detected! 851 */ 852 retval = 1; 853 854 _done: 855 spin_unlock_irqrestore(&s->lock, flags); 856 return retval; 857 } 858 859 /* 860 * ALSA callback function, called when attempting to open the MIDI device. 861 * Check that the MIDI firmware has been loaded, because we don't want 862 * to crash the machine. Also check that someone isn't using the hardware 863 * IOCTL device. 864 */ 865 static int mpu401_open(struct snd_mpu401 *mpu) 866 { 867 if (!verify_mpu401(mpu)) { 868 dev_err(mpu->rmidi->card->dev, 869 "sscape: MIDI disabled, please load firmware\n"); 870 return -ENODEV; 871 } 872 873 return 0; 874 } 875 876 /* 877 * Initialise an MPU-401 subdevice for MIDI support on the SoundScape. 878 */ 879 static int create_mpu401(struct snd_card *card, int devnum, 880 unsigned long port, int irq) 881 { 882 struct soundscape *sscape = get_card_soundscape(card); 883 struct snd_rawmidi *rawmidi; 884 int err; 885 886 err = snd_mpu401_uart_new(card, devnum, MPU401_HW_MPU401, port, 887 MPU401_INFO_INTEGRATED, irq, &rawmidi); 888 if (err == 0) { 889 struct snd_mpu401 *mpu = rawmidi->private_data; 890 mpu->open_input = mpu401_open; 891 mpu->open_output = mpu401_open; 892 mpu->private_data = sscape; 893 894 initialise_mpu401(mpu); 895 } 896 897 return err; 898 } 899 900 901 /* 902 * Create an AD1845 PCM subdevice on the SoundScape. The AD1845 903 * is very much like a CS4231, with a few extra bits. We will 904 * try to support at least some of the extra bits by overriding 905 * some of the CS4231 callback. 906 */ 907 static int create_ad1845(struct snd_card *card, unsigned port, 908 int irq, int dma1, int dma2) 909 { 910 register struct soundscape *sscape = get_card_soundscape(card); 911 struct snd_wss *chip; 912 int err; 913 int codec_type = WSS_HW_DETECT; 914 915 switch (sscape->type) { 916 case MEDIA_FX: 917 case SSCAPE: 918 /* 919 * There are some freak examples of early Soundscape cards 920 * with CS4231 instead of AD1848/CS4248. Unfortunately, the 921 * CS4231 works only in CS4248 compatibility mode on 922 * these cards so force it. 923 */ 924 if (sscape->ic_type != IC_OPUS) 925 codec_type = WSS_HW_AD1848; 926 break; 927 928 case SSCAPE_VIVO: 929 port += 4; 930 break; 931 default: 932 break; 933 } 934 935 err = snd_wss_create(card, port, -1, irq, dma1, dma2, 936 codec_type, WSS_HWSHARE_DMA1, &chip); 937 if (!err) { 938 if (sscape->type != SSCAPE_VIVO) { 939 /* 940 * The input clock frequency on the SoundScape must 941 * be 14.31818 MHz, because we must set this register 942 * to get the playback to sound correct ... 943 */ 944 snd_wss_mce_up(chip); 945 scoped_guard(spinlock_irqsave, &chip->reg_lock) { 946 snd_wss_out(chip, AD1845_CLOCK, 0x20); 947 } 948 snd_wss_mce_down(chip); 949 950 } 951 952 err = snd_wss_pcm(chip, 0); 953 if (err < 0) { 954 dev_err(card->dev, 955 "sscape: No PCM device for AD1845 chip\n"); 956 goto _error; 957 } 958 959 err = snd_wss_mixer(chip); 960 if (err < 0) { 961 dev_err(card->dev, 962 "sscape: No mixer device for AD1845 chip\n"); 963 goto _error; 964 } 965 if (chip->hardware != WSS_HW_AD1848) { 966 err = snd_wss_timer(chip, 0); 967 if (err < 0) { 968 dev_err(card->dev, 969 "sscape: No timer device for AD1845 chip\n"); 970 goto _error; 971 } 972 } 973 974 if (sscape->type != SSCAPE_VIVO) { 975 err = snd_ctl_add(card, 976 snd_ctl_new1(&midi_mixer_ctl, chip)); 977 if (err < 0) { 978 dev_err(card->dev, 979 "sscape: Could not create MIDI mixer control\n"); 980 goto _error; 981 } 982 } 983 984 sscape->chip = chip; 985 } 986 987 _error: 988 return err; 989 } 990 991 992 /* 993 * Create an ALSA soundcard entry for the SoundScape, using 994 * the resolved port, IRQ and DMA resources. 995 */ 996 static int create_sscape(struct snd_card *card) 997 { 998 struct soundscape *sscape = get_card_soundscape(card); 999 struct resource *io_res; 1000 struct resource *wss_res; 1001 int err; 1002 const char *name; 1003 1004 /* 1005 * Grab IO ports that we will need to probe so that we 1006 * can detect and control this hardware ... 1007 */ 1008 io_res = devm_request_region(card->dev, sscape->io_base, 8, "SoundScape"); 1009 if (!io_res) { 1010 dev_err(card->dev, 1011 "sscape: can't grab port 0x%x\n", sscape->io_base); 1012 return -EBUSY; 1013 } 1014 wss_res = NULL; 1015 if (sscape->type == SSCAPE_VIVO) { 1016 wss_res = devm_request_region(card->dev, sscape->wss_base, 4, 1017 "SoundScape"); 1018 if (!wss_res) { 1019 dev_err(card->dev, "sscape: can't grab port 0x%lx\n", 1020 sscape->wss_base); 1021 return -EBUSY; 1022 } 1023 } 1024 1025 /* 1026 * Grab one DMA channel ... 1027 */ 1028 err = snd_devm_request_dma(card->dev, sscape->dma1, "SoundScape"); 1029 if (err < 0) { 1030 dev_err(card->dev, "sscape: can't grab DMA %d\n", sscape->dma1); 1031 return err; 1032 } 1033 1034 spin_lock_init(&sscape->lock); 1035 sscape->io_res = io_res; 1036 sscape->wss_res = wss_res; 1037 1038 if (!detect_sscape(sscape, sscape->wss_base)) { 1039 dev_err(card->dev, "sscape: hardware not detected at 0x%x\n", 1040 sscape->io_base); 1041 return -ENODEV; 1042 } 1043 1044 switch (sscape->type) { 1045 case MEDIA_FX: 1046 name = "MediaFX/SoundFX"; 1047 break; 1048 case SSCAPE: 1049 name = "Soundscape"; 1050 break; 1051 case SSCAPE_PNP: 1052 name = "Soundscape PnP"; 1053 break; 1054 case SSCAPE_VIVO: 1055 name = "Soundscape VIVO"; 1056 break; 1057 default: 1058 name = "unknown Soundscape"; 1059 break; 1060 } 1061 1062 dev_info(card->dev, "sscape: %s card detected at 0x%x, using IRQ %d, DMA %d\n", 1063 name, sscape->io_base, sscape->irq, sscape->dma1); 1064 1065 /* 1066 * Tell the on-board devices where their resources are (I think - 1067 * I can't be sure without a datasheet ... So many magic values!) 1068 */ 1069 err = sscape_configure_board(sscape); 1070 if (err < 0) { 1071 dev_err(card->dev, "sscape: Invalid IRQ configuration\n"); 1072 return err; 1073 } 1074 1075 /* 1076 * We have now enabled the codec chip, and so we should 1077 * detect the AD1845 device ... 1078 */ 1079 err = create_ad1845(card, sscape->wss_base, sscape->irq, 1080 sscape->dma1, sscape->dma2); 1081 if (err < 0) { 1082 dev_err(card->dev, 1083 "sscape: No AD1845 device at 0x%lx, IRQ %d\n", 1084 sscape->wss_base, sscape->irq); 1085 return err; 1086 } 1087 strscpy(card->driver, "SoundScape"); 1088 strscpy(card->shortname, name); 1089 snprintf(card->longname, sizeof(card->longname), 1090 "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n", 1091 name, sscape->chip->port, sscape->chip->irq, 1092 sscape->chip->dma1, sscape->chip->dma2); 1093 1094 #define MIDI_DEVNUM 0 1095 if (sscape->type != SSCAPE_VIVO) { 1096 err = sscape_upload_bootblock(card); 1097 if (err >= 0) 1098 err = sscape_upload_microcode(card, err); 1099 1100 if (err == 0) { 1101 err = create_mpu401(card, MIDI_DEVNUM, sscape->io_base, 1102 sscape->mpu_irq); 1103 if (err < 0) { 1104 dev_err(card->dev, 1105 "sscape: Failed to create MPU-401 device at 0x%lx\n", 1106 (unsigned long)sscape->io_base); 1107 return err; 1108 } 1109 1110 sscape->midi_vol = 0; 1111 sscape->midi_enabled = true; 1112 err = sscape_restore_midi_state(sscape); 1113 if (err < 0) 1114 dev_warn(card->dev, 1115 "sscape: MIDI init incomplete: %d\n", 1116 err); 1117 } 1118 } 1119 1120 return 0; 1121 } 1122 1123 #ifdef CONFIG_PM 1124 /* 1125 * Reload the MIDI firmware and restore the saved MIDI state for 1126 * boards whose MPU-401 side was enabled during probe. 1127 */ 1128 static int sscape_resume_midi(struct snd_card *card) 1129 { 1130 struct soundscape *sscape = get_card_soundscape(card); 1131 int err, version; 1132 1133 if (!sscape->midi_enabled) 1134 return 0; 1135 1136 version = sscape_upload_bootblock(card); 1137 if (version < 0) 1138 return version; 1139 1140 err = sscape_upload_microcode(card, version); 1141 if (err < 0) 1142 return err; 1143 1144 outb(0, sscape->io_base); 1145 1146 return sscape_restore_midi_state(sscape); 1147 } 1148 1149 /* 1150 * Save the WSS codec state before the SoundScape is suspended. 1151 */ 1152 static int snd_sscape_suspend_card(struct snd_card *card) 1153 { 1154 struct soundscape *sscape = get_card_soundscape(card); 1155 1156 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 1157 sscape->chip->suspend(sscape->chip); 1158 return 0; 1159 } 1160 1161 /* 1162 * Restore the board-specific state before resuming the WSS codec. 1163 */ 1164 static int snd_sscape_resume_card(struct snd_card *card) 1165 { 1166 struct soundscape *sscape = get_card_soundscape(card); 1167 int err; 1168 1169 err = sscape_configure_board(sscape); 1170 if (err < 0) 1171 return err; 1172 1173 err = sscape_resume_midi(card); 1174 if (err < 0) 1175 dev_warn(card->dev, "sscape: MIDI restore failed: %d\n", err); 1176 1177 sscape->chip->resume(sscape->chip); 1178 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 1179 return 0; 1180 } 1181 1182 static int snd_sscape_suspend(struct device *dev, unsigned int n, 1183 pm_message_t state) 1184 { 1185 return snd_sscape_suspend_card(dev_get_drvdata(dev)); 1186 } 1187 1188 static int snd_sscape_resume(struct device *dev, unsigned int n) 1189 { 1190 return snd_sscape_resume_card(dev_get_drvdata(dev)); 1191 } 1192 #endif 1193 1194 1195 static int snd_sscape_match(struct device *pdev, unsigned int i) 1196 { 1197 /* 1198 * Make sure we were given ALL of the other parameters. 1199 */ 1200 if (port[i] == SNDRV_AUTO_PORT) 1201 return 0; 1202 1203 if (irq[i] == SNDRV_AUTO_IRQ || 1204 mpu_irq[i] == SNDRV_AUTO_IRQ || 1205 dma[i] == SNDRV_AUTO_DMA) { 1206 dev_info(pdev, 1207 "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n"); 1208 return 0; 1209 } 1210 1211 return 1; 1212 } 1213 1214 static int snd_sscape_probe(struct device *pdev, unsigned int dev) 1215 { 1216 struct snd_card *card; 1217 struct soundscape *sscape; 1218 int ret; 1219 1220 ret = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE, 1221 sizeof(struct soundscape), &card); 1222 if (ret < 0) 1223 return ret; 1224 1225 sscape = get_card_soundscape(card); 1226 sscape->dev = pdev; 1227 sscape->type = SSCAPE; 1228 1229 dma[dev] &= 0x03; 1230 sscape_store_settings(sscape, dev); 1231 1232 ret = create_sscape(card); 1233 if (ret < 0) 1234 return ret; 1235 1236 ret = snd_card_register(card); 1237 if (ret < 0) { 1238 dev_err(pdev, "sscape: Failed to register sound card\n"); 1239 return ret; 1240 } 1241 dev_set_drvdata(pdev, card); 1242 return 0; 1243 } 1244 1245 #define DEV_NAME "sscape" 1246 1247 static struct isa_driver snd_sscape_driver = { 1248 .match = snd_sscape_match, 1249 .probe = snd_sscape_probe, 1250 #ifdef CONFIG_PM 1251 .suspend = snd_sscape_suspend, 1252 .resume = snd_sscape_resume, 1253 #endif 1254 .driver = { 1255 .name = DEV_NAME 1256 }, 1257 }; 1258 1259 #ifdef CONFIG_PNP 1260 static inline int get_next_autoindex(int i) 1261 { 1262 while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT) 1263 ++i; 1264 return i; 1265 } 1266 1267 1268 static int sscape_pnp_detect(struct pnp_card_link *pcard, 1269 const struct pnp_card_device_id *pid) 1270 { 1271 static int idx = 0; 1272 struct pnp_dev *dev; 1273 struct snd_card *card; 1274 struct soundscape *sscape; 1275 int ret; 1276 1277 /* 1278 * Allow this function to fail *quietly* if all the ISA PnP 1279 * devices were configured using module parameters instead. 1280 */ 1281 idx = get_next_autoindex(idx); 1282 if (idx >= SNDRV_CARDS) 1283 return -ENOSPC; 1284 1285 /* 1286 * Check that we still have room for another sound card ... 1287 */ 1288 dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL); 1289 if (!dev) 1290 return -ENODEV; 1291 1292 if (!pnp_is_active(dev)) { 1293 if (pnp_activate_dev(dev) < 0) { 1294 dev_info(&dev->dev, "sscape: device is inactive\n"); 1295 return -EBUSY; 1296 } 1297 } 1298 1299 /* 1300 * Create a new ALSA sound card entry, in anticipation 1301 * of detecting our hardware ... 1302 */ 1303 ret = snd_devm_card_new(&pcard->card->dev, 1304 index[idx], id[idx], THIS_MODULE, 1305 sizeof(struct soundscape), &card); 1306 if (ret < 0) 1307 return ret; 1308 1309 sscape = get_card_soundscape(card); 1310 sscape->dev = card->dev; 1311 1312 /* 1313 * Identify card model ... 1314 */ 1315 if (!strncmp("ENS4081", pid->id, 7)) 1316 sscape->type = SSCAPE_VIVO; 1317 else 1318 sscape->type = SSCAPE_PNP; 1319 1320 /* 1321 * Read the correct parameters off the ISA PnP bus ... 1322 */ 1323 port[idx] = pnp_port_start(dev, 0); 1324 irq[idx] = pnp_irq(dev, 0); 1325 mpu_irq[idx] = pnp_irq(dev, 1); 1326 dma[idx] = pnp_dma(dev, 0) & 0x03; 1327 if (sscape->type == SSCAPE_PNP) { 1328 dma2[idx] = dma[idx]; 1329 wss_port[idx] = CODEC_IO(port[idx]); 1330 } else { 1331 wss_port[idx] = pnp_port_start(dev, 1); 1332 dma2[idx] = pnp_dma(dev, 1); 1333 } 1334 sscape_store_settings(sscape, idx); 1335 1336 ret = create_sscape(card); 1337 if (ret < 0) 1338 return ret; 1339 1340 ret = snd_card_register(card); 1341 if (ret < 0) { 1342 dev_err(card->dev, "sscape: Failed to register sound card\n"); 1343 return ret; 1344 } 1345 1346 pnp_set_card_drvdata(pcard, card); 1347 ++idx; 1348 return 0; 1349 } 1350 1351 #ifdef CONFIG_PM 1352 static int sscape_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state) 1353 { 1354 return snd_sscape_suspend_card(pnp_get_card_drvdata(pcard)); 1355 } 1356 1357 static int sscape_pnp_resume(struct pnp_card_link *pcard) 1358 { 1359 return snd_sscape_resume_card(pnp_get_card_drvdata(pcard)); 1360 } 1361 #endif 1362 1363 static struct pnp_card_driver sscape_pnpc_driver = { 1364 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE, 1365 .name = "sscape", 1366 .id_table = sscape_pnpids, 1367 .probe = sscape_pnp_detect, 1368 #ifdef CONFIG_PM 1369 .suspend = sscape_pnp_suspend, 1370 .resume = sscape_pnp_resume, 1371 #endif 1372 }; 1373 1374 #endif /* CONFIG_PNP */ 1375 1376 static int __init sscape_init(void) 1377 { 1378 int err; 1379 1380 err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS); 1381 #ifdef CONFIG_PNP 1382 if (!err) 1383 isa_registered = 1; 1384 1385 err = pnp_register_card_driver(&sscape_pnpc_driver); 1386 if (!err) 1387 pnp_registered = 1; 1388 1389 if (isa_registered) 1390 err = 0; 1391 #endif 1392 return err; 1393 } 1394 1395 static void __exit sscape_exit(void) 1396 { 1397 #ifdef CONFIG_PNP 1398 if (pnp_registered) 1399 pnp_unregister_card_driver(&sscape_pnpc_driver); 1400 if (isa_registered) 1401 #endif 1402 isa_unregister_driver(&snd_sscape_driver); 1403 } 1404 1405 module_init(sscape_init); 1406 module_exit(sscape_exit); 1407