644 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * ALSA card-level driver for Turtle Beach Wavefront cards 4 * (Maui,Tropez,Tropez+)5 *6 * Copyright (c) 1997-1999 by Paul Barton-Davis <pbd@op.net>7 */8 9#include <linux/init.h>10#include <linux/interrupt.h>11#include <linux/err.h>12#include <linux/isa.h>13#include <linux/pnp.h>14#include <linux/module.h>15#include <sound/core.h>16#include <sound/initval.h>17#include <sound/opl3.h>18#include <sound/wss.h>19#include <sound/snd_wavefront.h>20 21MODULE_AUTHOR("Paul Barton-Davis <pbd@op.net>");22MODULE_DESCRIPTION("Turtle Beach Wavefront");23MODULE_LICENSE("GPL");24 25static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */26static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */27static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */28#ifdef CONFIG_PNP29static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};30#endif31static long cs4232_pcm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */32static int cs4232_pcm_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,11,12,15 */33static long cs4232_mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */34static int cs4232_mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 9,11,12,15 */35static long ics2115_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */36static int ics2115_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 2,9,11,12,15 */37static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* PnP setup */38static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */39static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3,5,6,7 */40static bool use_cs4232_midi[SNDRV_CARDS];41 42module_param_array(index, int, NULL, 0444);43MODULE_PARM_DESC(index, "Index value for WaveFront soundcard.");44module_param_array(id, charp, NULL, 0444);45MODULE_PARM_DESC(id, "ID string for WaveFront soundcard.");46module_param_array(enable, bool, NULL, 0444);47MODULE_PARM_DESC(enable, "Enable WaveFront soundcard.");48#ifdef CONFIG_PNP49module_param_array(isapnp, bool, NULL, 0444);50MODULE_PARM_DESC(isapnp, "ISA PnP detection for WaveFront soundcards.");51#endif52module_param_hw_array(cs4232_pcm_port, long, ioport, NULL, 0444);53MODULE_PARM_DESC(cs4232_pcm_port, "Port # for CS4232 PCM interface.");54module_param_hw_array(cs4232_pcm_irq, int, irq, NULL, 0444);55MODULE_PARM_DESC(cs4232_pcm_irq, "IRQ # for CS4232 PCM interface.");56module_param_hw_array(dma1, int, dma, NULL, 0444);57MODULE_PARM_DESC(dma1, "DMA1 # for CS4232 PCM interface.");58module_param_hw_array(dma2, int, dma, NULL, 0444);59MODULE_PARM_DESC(dma2, "DMA2 # for CS4232 PCM interface.");60module_param_hw_array(cs4232_mpu_port, long, ioport, NULL, 0444);61MODULE_PARM_DESC(cs4232_mpu_port, "port # for CS4232 MPU-401 interface.");62module_param_hw_array(cs4232_mpu_irq, int, irq, NULL, 0444);63MODULE_PARM_DESC(cs4232_mpu_irq, "IRQ # for CS4232 MPU-401 interface.");64module_param_hw_array(ics2115_irq, int, irq, NULL, 0444);65MODULE_PARM_DESC(ics2115_irq, "IRQ # for ICS2115.");66module_param_hw_array(ics2115_port, long, ioport, NULL, 0444);67MODULE_PARM_DESC(ics2115_port, "Port # for ICS2115.");68module_param_hw_array(fm_port, long, ioport, NULL, 0444);69MODULE_PARM_DESC(fm_port, "FM port #.");70module_param_array(use_cs4232_midi, bool, NULL, 0444);71MODULE_PARM_DESC(use_cs4232_midi, "Use CS4232 MPU-401 interface (inaccessibly located inside your computer)");72 73#ifdef CONFIG_PNP74static int isa_registered;75static int pnp_registered;76 77static const struct pnp_card_device_id snd_wavefront_pnpids[] = {78 /* Tropez */79 { .id = "CSC7532", .devs = { { "CSC0000" }, { "CSC0010" }, { "PnPb006" }, { "CSC0004" } } },80 /* Tropez+ */81 { .id = "CSC7632", .devs = { { "CSC0000" }, { "CSC0010" }, { "PnPb006" }, { "CSC0004" } } },82 { .id = "" }83};84 85MODULE_DEVICE_TABLE(pnp_card, snd_wavefront_pnpids);86 87static int88snd_wavefront_pnp (int dev, snd_wavefront_card_t *acard, struct pnp_card_link *card,89 const struct pnp_card_device_id *id)90{91 struct pnp_dev *pdev;92 int err;93 94 /* Check for each logical device. */95 96 /* CS4232 chip (aka "windows sound system") is logical device 0 */97 98 acard->wss = pnp_request_card_device(card, id->devs[0].id, NULL);99 if (acard->wss == NULL)100 return -EBUSY;101 102 /* there is a game port at logical device 1, but we ignore it completely */103 104 /* the control interface is logical device 2, but we ignore it105 completely. in fact, nobody even seems to know what it106 does.107 */108 109 /* Only configure the CS4232 MIDI interface if its been110 specifically requested. It is logical device 3.111 */112 113 if (use_cs4232_midi[dev]) {114 acard->mpu = pnp_request_card_device(card, id->devs[2].id, NULL);115 if (acard->mpu == NULL)116 return -EBUSY;117 }118 119 /* The ICS2115 synth is logical device 4 */120 121 acard->synth = pnp_request_card_device(card, id->devs[3].id, NULL);122 if (acard->synth == NULL)123 return -EBUSY;124 125 /* PCM/FM initialization */126 127 pdev = acard->wss;128 129 /* An interesting note from the Tropez+ FAQ:130 131 Q. [Ports] Why is the base address of the WSS I/O ports off by 4?132 133 A. WSS I/O requires a block of 8 I/O addresses ("ports"). Of these, the first134 4 are used to identify and configure the board. With the advent of PnP,135 these first 4 addresses have become obsolete, and software applications136 only use the last 4 addresses to control the codec chip. Therefore, the137 base address setting "skips past" the 4 unused addresses.138 139 */140 141 err = pnp_activate_dev(pdev);142 if (err < 0) {143 dev_err(&pdev->dev, "PnP WSS pnp configure failure\n");144 return err;145 }146 147 cs4232_pcm_port[dev] = pnp_port_start(pdev, 0);148 fm_port[dev] = pnp_port_start(pdev, 1);149 dma1[dev] = pnp_dma(pdev, 0);150 dma2[dev] = pnp_dma(pdev, 1);151 cs4232_pcm_irq[dev] = pnp_irq(pdev, 0);152 153 /* Synth initialization */154 155 pdev = acard->synth;156 157 err = pnp_activate_dev(pdev);158 if (err < 0) {159 dev_err(&pdev->dev, "PnP ICS2115 pnp configure failure\n");160 return err;161 }162 163 ics2115_port[dev] = pnp_port_start(pdev, 0);164 ics2115_irq[dev] = pnp_irq(pdev, 0);165 166 /* CS4232 MPU initialization. Configure this only if167 explicitly requested, since its physically inaccessible and168 consumes another IRQ.169 */170 171 if (use_cs4232_midi[dev]) {172 173 pdev = acard->mpu;174 175 err = pnp_activate_dev(pdev);176 if (err < 0) {177 dev_err(&pdev->dev, "PnP MPU401 pnp configure failure\n");178 cs4232_mpu_port[dev] = SNDRV_AUTO_PORT;179 } else {180 cs4232_mpu_port[dev] = pnp_port_start(pdev, 0);181 cs4232_mpu_irq[dev] = pnp_irq(pdev, 0);182 }183 184 dev_info(&pdev->dev, "CS4232 MPU: port=0x%lx, irq=%i\n",185 cs4232_mpu_port[dev],186 cs4232_mpu_irq[dev]);187 }188 189 dev_dbg(&pdev->dev,190 "CS4232: pcm port=0x%lx, fm port=0x%lx, dma1=%i, dma2=%i, irq=%i\nICS2115: port=0x%lx, irq=%i\n",191 cs4232_pcm_port[dev],192 fm_port[dev],193 dma1[dev],194 dma2[dev],195 cs4232_pcm_irq[dev],196 ics2115_port[dev],197 ics2115_irq[dev]);198 199 return 0;200}201 202#endif /* CONFIG_PNP */203 204static irqreturn_t snd_wavefront_ics2115_interrupt(int irq, void *dev_id)205{206 snd_wavefront_card_t *acard;207 208 acard = (snd_wavefront_card_t *) dev_id;209 210 if (acard == NULL) 211 return IRQ_NONE;212 213 if (acard->wavefront.interrupts_are_midi) {214 snd_wavefront_midi_interrupt (acard);215 } else {216 snd_wavefront_internal_interrupt (acard);217 }218 return IRQ_HANDLED;219}220 221static struct snd_hwdep *snd_wavefront_new_synth(struct snd_card *card,222 int hw_dev,223 snd_wavefront_card_t *acard)224{225 struct snd_hwdep *wavefront_synth;226 227 if (snd_wavefront_detect (acard) < 0) {228 return NULL;229 }230 231 if (snd_wavefront_start (&acard->wavefront) < 0) {232 return NULL;233 }234 235 if (snd_hwdep_new(card, "WaveFront", hw_dev, &wavefront_synth) < 0)236 return NULL;237 strcpy (wavefront_synth->name, 238 "WaveFront (ICS2115) wavetable synthesizer");239 wavefront_synth->ops.open = snd_wavefront_synth_open;240 wavefront_synth->ops.release = snd_wavefront_synth_release;241 wavefront_synth->ops.ioctl = snd_wavefront_synth_ioctl;242 243 return wavefront_synth;244}245 246static struct snd_hwdep *snd_wavefront_new_fx(struct snd_card *card,247 int hw_dev,248 snd_wavefront_card_t *acard,249 unsigned long port)250 251{252 struct snd_hwdep *fx_processor;253 254 if (snd_wavefront_fx_start (&acard->wavefront)) {255 dev_err(card->dev, "cannot initialize YSS225 FX processor");256 return NULL;257 }258 259 if (snd_hwdep_new (card, "YSS225", hw_dev, &fx_processor) < 0)260 return NULL;261 sprintf (fx_processor->name, "YSS225 FX Processor at 0x%lx", port);262 fx_processor->ops.open = snd_wavefront_fx_open;263 fx_processor->ops.release = snd_wavefront_fx_release;264 fx_processor->ops.ioctl = snd_wavefront_fx_ioctl;265 266 return fx_processor;267}268 269static snd_wavefront_mpu_id internal_id = internal_mpu;270static snd_wavefront_mpu_id external_id = external_mpu;271 272static struct snd_rawmidi *snd_wavefront_new_midi(struct snd_card *card,273 int midi_dev,274 snd_wavefront_card_t *acard,275 unsigned long port,276 snd_wavefront_mpu_id mpu)277 278{279 struct snd_rawmidi *rmidi;280 static int first = 1;281 282 if (first) {283 first = 0;284 acard->wavefront.midi.base = port;285 if (snd_wavefront_midi_start (acard)) {286 dev_err(card->dev, "cannot initialize MIDI interface\n");287 return NULL;288 }289 }290 291 if (snd_rawmidi_new (card, "WaveFront MIDI", midi_dev, 1, 1, &rmidi) < 0)292 return NULL;293 294 if (mpu == internal_mpu) {295 strcpy(rmidi->name, "WaveFront MIDI (Internal)");296 rmidi->private_data = &internal_id;297 } else {298 strcpy(rmidi->name, "WaveFront MIDI (External)");299 rmidi->private_data = &external_id;300 }301 302 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_wavefront_midi_output);303 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_wavefront_midi_input);304 305 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |306 SNDRV_RAWMIDI_INFO_INPUT |307 SNDRV_RAWMIDI_INFO_DUPLEX;308 309 return rmidi;310}311 312static int snd_wavefront_card_new(struct device *pdev, int dev,313 struct snd_card **cardp)314{315 struct snd_card *card;316 snd_wavefront_card_t *acard;317 int err;318 319 err = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,320 sizeof(snd_wavefront_card_t), &card);321 if (err < 0)322 return err;323 324 acard = card->private_data;325 acard->wavefront.irq = -1;326 spin_lock_init(&acard->wavefront.irq_lock);327 init_waitqueue_head(&acard->wavefront.interrupt_sleeper);328 spin_lock_init(&acard->wavefront.midi.open);329 spin_lock_init(&acard->wavefront.midi.virtual);330 acard->wavefront.card = card;331 332 *cardp = card;333 return 0;334}335 336static int337snd_wavefront_probe (struct snd_card *card, int dev)338{339 snd_wavefront_card_t *acard = card->private_data;340 struct snd_wss *chip;341 struct snd_hwdep *wavefront_synth;342 struct snd_rawmidi *ics2115_internal_rmidi = NULL;343 struct snd_rawmidi *ics2115_external_rmidi = NULL;344 struct snd_hwdep *fx_processor;345 int hw_dev = 0, midi_dev = 0, err;346 347 /* --------- PCM --------------- */348 349 err = snd_wss_create(card, cs4232_pcm_port[dev], -1,350 cs4232_pcm_irq[dev], dma1[dev], dma2[dev],351 WSS_HW_DETECT, 0, &chip);352 if (err < 0) {353 dev_err(card->dev, "can't allocate WSS device\n");354 return err;355 }356 357 err = snd_wss_pcm(chip, 0);358 if (err < 0)359 return err;360 361 err = snd_wss_timer(chip, 0);362 if (err < 0)363 return err;364 365 /* ---------- OPL3 synth --------- */366 367 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {368 struct snd_opl3 *opl3;369 370 err = snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,371 OPL3_HW_OPL3_CS, 0, &opl3);372 if (err < 0) {373 dev_err(card->dev, "can't allocate or detect OPL3 synth\n");374 return err;375 }376 377 err = snd_opl3_hwdep_new(opl3, hw_dev, 1, NULL);378 if (err < 0)379 return err;380 hw_dev++;381 }382 383 /* ------- ICS2115 Wavetable synth ------- */384 385 acard->wavefront.res_base =386 devm_request_region(card->dev, ics2115_port[dev], 16,387 "ICS2115");388 if (acard->wavefront.res_base == NULL) {389 dev_err(card->dev, "unable to grab ICS2115 i/o region 0x%lx-0x%lx\n",390 ics2115_port[dev], ics2115_port[dev] + 16 - 1);391 return -EBUSY;392 }393 if (devm_request_irq(card->dev, ics2115_irq[dev],394 snd_wavefront_ics2115_interrupt,395 0, "ICS2115", acard)) {396 dev_err(card->dev, "unable to use ICS2115 IRQ %d\n", ics2115_irq[dev]);397 return -EBUSY;398 }399 400 acard->wavefront.irq = ics2115_irq[dev];401 card->sync_irq = acard->wavefront.irq;402 acard->wavefront.base = ics2115_port[dev];403 404 wavefront_synth = snd_wavefront_new_synth(card, hw_dev, acard);405 if (wavefront_synth == NULL) {406 dev_err(card->dev, "can't create WaveFront synth device\n");407 return -ENOMEM;408 }409 410 strcpy (wavefront_synth->name, "ICS2115 Wavetable MIDI Synthesizer");411 wavefront_synth->iface = SNDRV_HWDEP_IFACE_ICS2115;412 hw_dev++;413 414 /* --------- Mixer ------------ */415 416 err = snd_wss_mixer(chip);417 if (err < 0) {418 dev_err(card->dev, "can't allocate mixer device\n");419 return err;420 }421 422 /* -------- CS4232 MPU-401 interface -------- */423 424 if (cs4232_mpu_port[dev] > 0 && cs4232_mpu_port[dev] != SNDRV_AUTO_PORT) {425 err = snd_mpu401_uart_new(card, midi_dev, MPU401_HW_CS4232,426 cs4232_mpu_port[dev], 0,427 cs4232_mpu_irq[dev], NULL);428 if (err < 0) {429 dev_err(card->dev, "can't allocate CS4232 MPU-401 device\n");430 return err;431 }432 midi_dev++;433 }434 435 /* ------ ICS2115 internal MIDI ------------ */436 437 if (ics2115_port[dev] > 0 && ics2115_port[dev] != SNDRV_AUTO_PORT) {438 ics2115_internal_rmidi = 439 snd_wavefront_new_midi (card, 440 midi_dev,441 acard,442 ics2115_port[dev],443 internal_mpu);444 if (ics2115_internal_rmidi == NULL) {445 dev_err(card->dev, "can't setup ICS2115 internal MIDI device\n");446 return -ENOMEM;447 }448 midi_dev++;449 }450 451 /* ------ ICS2115 external MIDI ------------ */452 453 if (ics2115_port[dev] > 0 && ics2115_port[dev] != SNDRV_AUTO_PORT) {454 ics2115_external_rmidi = 455 snd_wavefront_new_midi (card, 456 midi_dev,457 acard,458 ics2115_port[dev],459 external_mpu);460 if (ics2115_external_rmidi == NULL) {461 dev_err(card->dev, "can't setup ICS2115 external MIDI device\n");462 return -ENOMEM;463 }464 midi_dev++;465 }466 467 /* FX processor for Tropez+ */468 469 if (acard->wavefront.has_fx) {470 fx_processor = snd_wavefront_new_fx (card,471 hw_dev,472 acard,473 ics2115_port[dev]);474 if (fx_processor == NULL) {475 dev_err(card->dev, "can't setup FX device\n");476 return -ENOMEM;477 }478 479 hw_dev++;480 481 strcpy(card->driver, "Tropez+");482 strcpy(card->shortname, "Turtle Beach Tropez+");483 } else {484 /* Need a way to distinguish between Maui and Tropez */485 strcpy(card->driver, "WaveFront");486 strcpy(card->shortname, "Turtle Beach WaveFront");487 }488 489 /* ----- Register the card --------- */490 491 /* Not safe to include "Turtle Beach" in longname, due to 492 length restrictions493 */494 495 sprintf(card->longname, "%s PCM 0x%lx irq %d dma %d",496 card->driver,497 chip->port,498 cs4232_pcm_irq[dev],499 dma1[dev]);500 501 if (dma2[dev] >= 0 && dma2[dev] < 8)502 sprintf(card->longname + strlen(card->longname), "&%d", dma2[dev]);503 504 if (cs4232_mpu_port[dev] > 0 && cs4232_mpu_port[dev] != SNDRV_AUTO_PORT) {505 sprintf (card->longname + strlen (card->longname), 506 " MPU-401 0x%lx irq %d",507 cs4232_mpu_port[dev],508 cs4232_mpu_irq[dev]);509 }510 511 sprintf (card->longname + strlen (card->longname), 512 " SYNTH 0x%lx irq %d",513 ics2115_port[dev],514 ics2115_irq[dev]);515 516 return snd_card_register(card);517} 518 519static int snd_wavefront_isa_match(struct device *pdev,520 unsigned int dev)521{522 if (!enable[dev])523 return 0;524#ifdef CONFIG_PNP525 if (isapnp[dev])526 return 0;527#endif528 if (cs4232_pcm_port[dev] == SNDRV_AUTO_PORT) {529 dev_err(pdev, "specify CS4232 port\n");530 return 0;531 }532 if (ics2115_port[dev] == SNDRV_AUTO_PORT) {533 dev_err(pdev, "specify ICS2115 port\n");534 return 0;535 }536 return 1;537}538 539static int snd_wavefront_isa_probe(struct device *pdev,540 unsigned int dev)541{542 struct snd_card *card;543 int err;544 545 err = snd_wavefront_card_new(pdev, dev, &card);546 if (err < 0)547 return err;548 err = snd_wavefront_probe(card, dev);549 if (err < 0)550 return err;551 552 dev_set_drvdata(pdev, card);553 return 0;554}555 556#define DEV_NAME "wavefront"557 558static struct isa_driver snd_wavefront_driver = {559 .match = snd_wavefront_isa_match,560 .probe = snd_wavefront_isa_probe,561 /* FIXME: suspend, resume */562 .driver = {563 .name = DEV_NAME564 },565};566 567 568#ifdef CONFIG_PNP569static int snd_wavefront_pnp_detect(struct pnp_card_link *pcard,570 const struct pnp_card_device_id *pid)571{572 static int dev;573 struct snd_card *card;574 int res;575 576 for ( ; dev < SNDRV_CARDS; dev++) {577 if (enable[dev] && isapnp[dev])578 break;579 }580 if (dev >= SNDRV_CARDS)581 return -ENODEV;582 583 res = snd_wavefront_card_new(&pcard->card->dev, dev, &card);584 if (res < 0)585 return res;586 587 if (snd_wavefront_pnp (dev, card->private_data, pcard, pid) < 0) {588 if (cs4232_pcm_port[dev] == SNDRV_AUTO_PORT) {589 dev_err(card->dev, "isapnp detection failed\n");590 return -ENODEV;591 }592 }593 594 res = snd_wavefront_probe(card, dev);595 if (res < 0)596 return res;597 598 pnp_set_card_drvdata(pcard, card);599 dev++;600 return 0;601}602 603static struct pnp_card_driver wavefront_pnpc_driver = {604 .flags = PNP_DRIVER_RES_DISABLE,605 .name = "wavefront",606 .id_table = snd_wavefront_pnpids,607 .probe = snd_wavefront_pnp_detect,608 /* FIXME: suspend,resume */609};610 611#endif /* CONFIG_PNP */612 613static int __init alsa_card_wavefront_init(void)614{615 int err;616 617 err = isa_register_driver(&snd_wavefront_driver, SNDRV_CARDS);618#ifdef CONFIG_PNP619 if (!err)620 isa_registered = 1;621 622 err = pnp_register_card_driver(&wavefront_pnpc_driver);623 if (!err)624 pnp_registered = 1;625 626 if (isa_registered)627 err = 0;628#endif629 return err;630}631 632static void __exit alsa_card_wavefront_exit(void)633{634#ifdef CONFIG_PNP635 if (pnp_registered)636 pnp_unregister_card_driver(&wavefront_pnpc_driver);637 if (isa_registered)638#endif639 isa_unregister_driver(&snd_wavefront_driver);640}641 642module_init(alsa_card_wavefront_init)643module_exit(alsa_card_wavefront_exit)644