linux/sound/isa/msnd/msnd_pinnacle.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*********************************************************************
   3 *
   4 * Linux multisound pinnacle/fiji driver for ALSA.
   5 *
   6 * 2002/06/30 Karsten Wiese:
   7 *      for now this is only used to build a pinnacle / fiji driver.
   8 *      the OSS parent of this code is designed to also support
   9 *      the multisound classic via the file msnd_classic.c.
  10 *      to make it easier for some brave heart to implemt classic
  11 *      support in alsa, i left all the MSND_CLASSIC tokens in this file.
  12 *      but for now this untested & undone.
  13 *
  14 * ripped from linux kernel 2.4.18 by Karsten Wiese.
  15 *
  16 * the following is a copy of the 2.4.18 OSS FREE file-heading comment:
  17 *
  18 * Turtle Beach MultiSound Sound Card Driver for Linux
  19 * msnd_pinnacle.c / msnd_classic.c
  20 *
  21 * -- If MSND_CLASSIC is defined:
  22 *
  23 *     -> driver for Turtle Beach Classic/Monterey/Tahiti
  24 *
  25 * -- Else
  26 *
  27 *     -> driver for Turtle Beach Pinnacle/Fiji
  28 *
  29 * 12-3-2000  Modified IO port validation  Steve Sycamore
  30 *
  31 * Copyright (C) 1998 Andrew Veliath
  32 *
  33 ********************************************************************/
  34
  35#include <linux/kernel.h>
  36#include <linux/module.h>
  37#include <linux/interrupt.h>
  38#include <linux/types.h>
  39#include <linux/delay.h>
  40#include <linux/ioport.h>
  41#include <linux/firmware.h>
  42#include <linux/isa.h>
  43#include <linux/isapnp.h>
  44#include <linux/irq.h>
  45#include <linux/io.h>
  46
  47#include <sound/core.h>
  48#include <sound/initval.h>
  49#include <sound/asound.h>
  50#include <sound/pcm.h>
  51#include <sound/mpu401.h>
  52
  53#ifdef MSND_CLASSIC
  54# ifndef __alpha__
  55#  define SLOWIO
  56# endif
  57#endif
  58#include "msnd.h"
  59#ifdef MSND_CLASSIC
  60#  include "msnd_classic.h"
  61#  define LOGNAME                       "msnd_classic"
  62#  define DEV_NAME                      "msnd-classic"
  63#else
  64#  include "msnd_pinnacle.h"
  65#  define LOGNAME                       "snd_msnd_pinnacle"
  66#  define DEV_NAME                      "msnd-pinnacle"
  67#endif
  68
  69static void set_default_audio_parameters(struct snd_msnd *chip)
  70{
  71        chip->play_sample_size = snd_pcm_format_width(DEFSAMPLESIZE);
  72        chip->play_sample_rate = DEFSAMPLERATE;
  73        chip->play_channels = DEFCHANNELS;
  74        chip->capture_sample_size = snd_pcm_format_width(DEFSAMPLESIZE);
  75        chip->capture_sample_rate = DEFSAMPLERATE;
  76        chip->capture_channels = DEFCHANNELS;
  77}
  78
  79static void snd_msnd_eval_dsp_msg(struct snd_msnd *chip, u16 wMessage)
  80{
  81        switch (HIBYTE(wMessage)) {
  82        case HIMT_PLAY_DONE: {
  83                if (chip->banksPlayed < 3)
  84                        snd_printdd("%08X: HIMT_PLAY_DONE: %i\n",
  85                                (unsigned)jiffies, LOBYTE(wMessage));
  86
  87                if (chip->last_playbank == LOBYTE(wMessage)) {
  88                        snd_printdd("chip.last_playbank == LOBYTE(wMessage)\n");
  89                        break;
  90                }
  91                chip->banksPlayed++;
  92
  93                if (test_bit(F_WRITING, &chip->flags))
  94                        snd_msnd_DAPQ(chip, 0);
  95
  96                chip->last_playbank = LOBYTE(wMessage);
  97                chip->playDMAPos += chip->play_period_bytes;
  98                if (chip->playDMAPos > chip->playLimit)
  99                        chip->playDMAPos = 0;
 100                snd_pcm_period_elapsed(chip->playback_substream);
 101
 102                break;
 103        }
 104        case HIMT_RECORD_DONE:
 105                if (chip->last_recbank == LOBYTE(wMessage))
 106                        break;
 107                chip->last_recbank = LOBYTE(wMessage);
 108                chip->captureDMAPos += chip->capturePeriodBytes;
 109                if (chip->captureDMAPos > (chip->captureLimit))
 110                        chip->captureDMAPos = 0;
 111
 112                if (test_bit(F_READING, &chip->flags))
 113                        snd_msnd_DARQ(chip, chip->last_recbank);
 114
 115                snd_pcm_period_elapsed(chip->capture_substream);
 116                break;
 117
 118        case HIMT_DSP:
 119                switch (LOBYTE(wMessage)) {
 120#ifndef MSND_CLASSIC
 121                case HIDSP_PLAY_UNDER:
 122#endif
 123                case HIDSP_INT_PLAY_UNDER:
 124                        snd_printd(KERN_WARNING LOGNAME ": Play underflow %i\n",
 125                                chip->banksPlayed);
 126                        if (chip->banksPlayed > 2)
 127                                clear_bit(F_WRITING, &chip->flags);
 128                        break;
 129
 130                case HIDSP_INT_RECORD_OVER:
 131                        snd_printd(KERN_WARNING LOGNAME ": Record overflow\n");
 132                        clear_bit(F_READING, &chip->flags);
 133                        break;
 134
 135                default:
 136                        snd_printd(KERN_WARNING LOGNAME
 137                                   ": DSP message %d 0x%02x\n",
 138                                   LOBYTE(wMessage), LOBYTE(wMessage));
 139                        break;
 140                }
 141                break;
 142
 143        case HIMT_MIDI_IN_UCHAR:
 144                if (chip->msndmidi_mpu)
 145                        snd_msndmidi_input_read(chip->msndmidi_mpu);
 146                break;
 147
 148        default:
 149                snd_printd(KERN_WARNING LOGNAME ": HIMT message %d 0x%02x\n",
 150                           HIBYTE(wMessage), HIBYTE(wMessage));
 151                break;
 152        }
 153}
 154
 155static irqreturn_t snd_msnd_interrupt(int irq, void *dev_id)
 156{
 157        struct snd_msnd *chip = dev_id;
 158        void __iomem *pwDSPQData = chip->mappedbase + DSPQ_DATA_BUFF;
 159        u16 head, tail, size;
 160
 161        /* Send ack to DSP */
 162        /* inb(chip->io + HP_RXL); */
 163
 164        /* Evaluate queued DSP messages */
 165        head = readw(chip->DSPQ + JQS_wHead);
 166        tail = readw(chip->DSPQ + JQS_wTail);
 167        size = readw(chip->DSPQ + JQS_wSize);
 168        if (head > size || tail > size)
 169                goto out;
 170        while (head != tail) {
 171                snd_msnd_eval_dsp_msg(chip, readw(pwDSPQData + 2 * head));
 172                if (++head > size)
 173                        head = 0;
 174                writew(head, chip->DSPQ + JQS_wHead);
 175        }
 176 out:
 177        /* Send ack to DSP */
 178        inb(chip->io + HP_RXL);
 179        return IRQ_HANDLED;
 180}
 181
 182
 183static int snd_msnd_reset_dsp(long io, unsigned char *info)
 184{
 185        int timeout = 100;
 186
 187        outb(HPDSPRESET_ON, io + HP_DSPR);
 188        msleep(1);
 189#ifndef MSND_CLASSIC
 190        if (info)
 191                *info = inb(io + HP_INFO);
 192#endif
 193        outb(HPDSPRESET_OFF, io + HP_DSPR);
 194        msleep(1);
 195        while (timeout-- > 0) {
 196                if (inb(io + HP_CVR) == HP_CVR_DEF)
 197                        return 0;
 198                msleep(1);
 199        }
 200        snd_printk(KERN_ERR LOGNAME ": Cannot reset DSP\n");
 201
 202        return -EIO;
 203}
 204
 205static int snd_msnd_probe(struct snd_card *card)
 206{
 207        struct snd_msnd *chip = card->private_data;
 208        unsigned char info;
 209#ifndef MSND_CLASSIC
 210        char *xv, *rev = NULL;
 211        char *pin = "TB Pinnacle", *fiji = "TB Fiji";
 212        char *pinfiji = "TB Pinnacle/Fiji";
 213#endif
 214
 215        if (!request_region(chip->io, DSP_NUMIO, "probing")) {
 216                snd_printk(KERN_ERR LOGNAME ": I/O port conflict\n");
 217                return -ENODEV;
 218        }
 219
 220        if (snd_msnd_reset_dsp(chip->io, &info) < 0) {
 221                release_region(chip->io, DSP_NUMIO);
 222                return -ENODEV;
 223        }
 224
 225#ifdef MSND_CLASSIC
 226        strcpy(card->shortname, "Classic/Tahiti/Monterey");
 227        strcpy(card->longname, "Turtle Beach Multisound");
 228        printk(KERN_INFO LOGNAME ": %s, "
 229               "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n",
 230               card->shortname,
 231               chip->io, chip->io + DSP_NUMIO - 1,
 232               chip->irq,
 233               chip->base, chip->base + 0x7fff);
 234#else
 235        switch (info >> 4) {
 236        case 0xf:
 237                xv = "<= 1.15";
 238                break;
 239        case 0x1:
 240                xv = "1.18/1.2";
 241                break;
 242        case 0x2:
 243                xv = "1.3";
 244                break;
 245        case 0x3:
 246                xv = "1.4";
 247                break;
 248        default:
 249                xv = "unknown";
 250                break;
 251        }
 252
 253        switch (info & 0x7) {
 254        case 0x0:
 255                rev = "I";
 256                strcpy(card->shortname, pin);
 257                break;
 258        case 0x1:
 259                rev = "F";
 260                strcpy(card->shortname, pin);
 261                break;
 262        case 0x2:
 263                rev = "G";
 264                strcpy(card->shortname, pin);
 265                break;
 266        case 0x3:
 267                rev = "H";
 268                strcpy(card->shortname, pin);
 269                break;
 270        case 0x4:
 271                rev = "E";
 272                strcpy(card->shortname, fiji);
 273                break;
 274        case 0x5:
 275                rev = "C";
 276                strcpy(card->shortname, fiji);
 277                break;
 278        case 0x6:
 279                rev = "D";
 280                strcpy(card->shortname, fiji);
 281                break;
 282        case 0x7:
 283                rev = "A-B (Fiji) or A-E (Pinnacle)";
 284                strcpy(card->shortname, pinfiji);
 285                break;
 286        }
 287        strcpy(card->longname, "Turtle Beach Multisound Pinnacle");
 288        printk(KERN_INFO LOGNAME ": %s revision %s, Xilinx version %s, "
 289               "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n",
 290               card->shortname,
 291               rev, xv,
 292               chip->io, chip->io + DSP_NUMIO - 1,
 293               chip->irq,
 294               chip->base, chip->base + 0x7fff);
 295#endif
 296
 297        release_region(chip->io, DSP_NUMIO);
 298        return 0;
 299}
 300
 301static int snd_msnd_init_sma(struct snd_msnd *chip)
 302{
 303        static int initted;
 304        u16 mastVolLeft, mastVolRight;
 305        unsigned long flags;
 306
 307#ifdef MSND_CLASSIC
 308        outb(chip->memid, chip->io + HP_MEMM);
 309#endif
 310        outb(HPBLKSEL_0, chip->io + HP_BLKS);
 311        /* Motorola 56k shared memory base */
 312        chip->SMA = chip->mappedbase + SMA_STRUCT_START;
 313
 314        if (initted) {
 315                mastVolLeft = readw(chip->SMA + SMA_wCurrMastVolLeft);
 316                mastVolRight = readw(chip->SMA + SMA_wCurrMastVolRight);
 317        } else
 318                mastVolLeft = mastVolRight = 0;
 319        memset_io(chip->mappedbase, 0, 0x8000);
 320
 321        /* Critical section: bank 1 access */
 322        spin_lock_irqsave(&chip->lock, flags);
 323        outb(HPBLKSEL_1, chip->io + HP_BLKS);
 324        memset_io(chip->mappedbase, 0, 0x8000);
 325        outb(HPBLKSEL_0, chip->io + HP_BLKS);
 326        spin_unlock_irqrestore(&chip->lock, flags);
 327
 328        /* Digital audio play queue */
 329        chip->DAPQ = chip->mappedbase + DAPQ_OFFSET;
 330        snd_msnd_init_queue(chip->DAPQ, DAPQ_DATA_BUFF, DAPQ_BUFF_SIZE);
 331
 332        /* Digital audio record queue */
 333        chip->DARQ = chip->mappedbase + DARQ_OFFSET;
 334        snd_msnd_init_queue(chip->DARQ, DARQ_DATA_BUFF, DARQ_BUFF_SIZE);
 335
 336        /* MIDI out queue */
 337        chip->MODQ = chip->mappedbase + MODQ_OFFSET;
 338        snd_msnd_init_queue(chip->MODQ, MODQ_DATA_BUFF, MODQ_BUFF_SIZE);
 339
 340        /* MIDI in queue */
 341        chip->MIDQ = chip->mappedbase + MIDQ_OFFSET;
 342        snd_msnd_init_queue(chip->MIDQ, MIDQ_DATA_BUFF, MIDQ_BUFF_SIZE);
 343
 344        /* DSP -> host message queue */
 345        chip->DSPQ = chip->mappedbase + DSPQ_OFFSET;
 346        snd_msnd_init_queue(chip->DSPQ, DSPQ_DATA_BUFF, DSPQ_BUFF_SIZE);
 347
 348        /* Setup some DSP values */
 349#ifndef MSND_CLASSIC
 350        writew(1, chip->SMA + SMA_wCurrPlayFormat);
 351        writew(chip->play_sample_size, chip->SMA + SMA_wCurrPlaySampleSize);
 352        writew(chip->play_channels, chip->SMA + SMA_wCurrPlayChannels);
 353        writew(chip->play_sample_rate, chip->SMA + SMA_wCurrPlaySampleRate);
 354#endif
 355        writew(chip->play_sample_rate, chip->SMA + SMA_wCalFreqAtoD);
 356        writew(mastVolLeft, chip->SMA + SMA_wCurrMastVolLeft);
 357        writew(mastVolRight, chip->SMA + SMA_wCurrMastVolRight);
 358#ifndef MSND_CLASSIC
 359        writel(0x00010000, chip->SMA + SMA_dwCurrPlayPitch);
 360        writel(0x00000001, chip->SMA + SMA_dwCurrPlayRate);
 361#endif
 362        writew(0x303, chip->SMA + SMA_wCurrInputTagBits);
 363
 364        initted = 1;
 365
 366        return 0;
 367}
 368
 369
 370static int upload_dsp_code(struct snd_card *card)
 371{
 372        struct snd_msnd *chip = card->private_data;
 373        const struct firmware *init_fw = NULL, *perm_fw = NULL;
 374        int err;
 375
 376        outb(HPBLKSEL_0, chip->io + HP_BLKS);
 377
 378        err = request_firmware(&init_fw, INITCODEFILE, card->dev);
 379        if (err < 0) {
 380                printk(KERN_ERR LOGNAME ": Error loading " INITCODEFILE);
 381                goto cleanup1;
 382        }
 383        err = request_firmware(&perm_fw, PERMCODEFILE, card->dev);
 384        if (err < 0) {
 385                printk(KERN_ERR LOGNAME ": Error loading " PERMCODEFILE);
 386                goto cleanup;
 387        }
 388
 389        memcpy_toio(chip->mappedbase, perm_fw->data, perm_fw->size);
 390        if (snd_msnd_upload_host(chip, init_fw->data, init_fw->size) < 0) {
 391                printk(KERN_WARNING LOGNAME ": Error uploading to DSP\n");
 392                err = -ENODEV;
 393                goto cleanup;
 394        }
 395        printk(KERN_INFO LOGNAME ": DSP firmware uploaded\n");
 396        err = 0;
 397
 398cleanup:
 399        release_firmware(perm_fw);
 400cleanup1:
 401        release_firmware(init_fw);
 402        return err;
 403}
 404
 405#ifdef MSND_CLASSIC
 406static void reset_proteus(struct snd_msnd *chip)
 407{
 408        outb(HPPRORESET_ON, chip->io + HP_PROR);
 409        msleep(TIME_PRO_RESET);
 410        outb(HPPRORESET_OFF, chip->io + HP_PROR);
 411        msleep(TIME_PRO_RESET_DONE);
 412}
 413#endif
 414
 415static int snd_msnd_initialize(struct snd_card *card)
 416{
 417        struct snd_msnd *chip = card->private_data;
 418        int err, timeout;
 419
 420#ifdef MSND_CLASSIC
 421        outb(HPWAITSTATE_0, chip->io + HP_WAIT);
 422        outb(HPBITMODE_16, chip->io + HP_BITM);
 423
 424        reset_proteus(chip);
 425#endif
 426        err = snd_msnd_init_sma(chip);
 427        if (err < 0) {
 428                printk(KERN_WARNING LOGNAME ": Cannot initialize SMA\n");
 429                return err;
 430        }
 431
 432        err = snd_msnd_reset_dsp(chip->io, NULL);
 433        if (err < 0)
 434                return err;
 435
 436        err = upload_dsp_code(card);
 437        if (err < 0) {
 438                printk(KERN_WARNING LOGNAME ": Cannot upload DSP code\n");
 439                return err;
 440        }
 441
 442        timeout = 200;
 443
 444        while (readw(chip->mappedbase)) {
 445                msleep(1);
 446                if (!timeout--) {
 447                        snd_printd(KERN_ERR LOGNAME ": DSP reset timeout\n");
 448                        return -EIO;
 449                }
 450        }
 451
 452        snd_msndmix_setup(chip);
 453        return 0;
 454}
 455
 456static int snd_msnd_dsp_full_reset(struct snd_card *card)
 457{
 458        struct snd_msnd *chip = card->private_data;
 459        int rv;
 460
 461        if (test_bit(F_RESETTING, &chip->flags) || ++chip->nresets > 10)
 462                return 0;
 463
 464        set_bit(F_RESETTING, &chip->flags);
 465        snd_msnd_dsp_halt(chip, NULL);  /* Unconditionally halt */
 466
 467        rv = snd_msnd_initialize(card);
 468        if (rv)
 469                printk(KERN_WARNING LOGNAME ": DSP reset failed\n");
 470        snd_msndmix_force_recsrc(chip, 0);
 471        clear_bit(F_RESETTING, &chip->flags);
 472        return rv;
 473}
 474
 475static int snd_msnd_dev_free(struct snd_device *device)
 476{
 477        snd_printdd("snd_msnd_chip_free()\n");
 478        return 0;
 479}
 480
 481static int snd_msnd_send_dsp_cmd_chk(struct snd_msnd *chip, u8 cmd)
 482{
 483        if (snd_msnd_send_dsp_cmd(chip, cmd) == 0)
 484                return 0;
 485        snd_msnd_dsp_full_reset(chip->card);
 486        return snd_msnd_send_dsp_cmd(chip, cmd);
 487}
 488
 489static int snd_msnd_calibrate_adc(struct snd_msnd *chip, u16 srate)
 490{
 491        snd_printdd("snd_msnd_calibrate_adc(%i)\n", srate);
 492        writew(srate, chip->SMA + SMA_wCalFreqAtoD);
 493        if (chip->calibrate_signal == 0)
 494                writew(readw(chip->SMA + SMA_wCurrHostStatusFlags)
 495                       | 0x0001, chip->SMA + SMA_wCurrHostStatusFlags);
 496        else
 497                writew(readw(chip->SMA + SMA_wCurrHostStatusFlags)
 498                       & ~0x0001, chip->SMA + SMA_wCurrHostStatusFlags);
 499        if (snd_msnd_send_word(chip, 0, 0, HDEXAR_CAL_A_TO_D) == 0 &&
 500            snd_msnd_send_dsp_cmd_chk(chip, HDEX_AUX_REQ) == 0) {
 501                schedule_timeout_interruptible(msecs_to_jiffies(333));
 502                return 0;
 503        }
 504        printk(KERN_WARNING LOGNAME ": ADC calibration failed\n");
 505        return -EIO;
 506}
 507
 508/*
 509 * ALSA callback function, called when attempting to open the MIDI device.
 510 */
 511static int snd_msnd_mpu401_open(struct snd_mpu401 *mpu)
 512{
 513        snd_msnd_enable_irq(mpu->private_data);
 514        snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_START);
 515        return 0;
 516}
 517
 518static void snd_msnd_mpu401_close(struct snd_mpu401 *mpu)
 519{
 520        snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_STOP);
 521        snd_msnd_disable_irq(mpu->private_data);
 522}
 523
 524static long mpu_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 525static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
 526
 527static int snd_msnd_attach(struct snd_card *card)
 528{
 529        struct snd_msnd *chip = card->private_data;
 530        int err;
 531        static struct snd_device_ops ops = {
 532                .dev_free =      snd_msnd_dev_free,
 533                };
 534
 535        err = request_irq(chip->irq, snd_msnd_interrupt, 0, card->shortname,
 536                          chip);
 537        if (err < 0) {
 538                printk(KERN_ERR LOGNAME ": Couldn't grab IRQ %d\n", chip->irq);
 539                return err;
 540        }
 541        if (request_region(chip->io, DSP_NUMIO, card->shortname) == NULL) {
 542                free_irq(chip->irq, chip);
 543                return -EBUSY;
 544        }
 545
 546        if (!request_mem_region(chip->base, BUFFSIZE, card->shortname)) {
 547                printk(KERN_ERR LOGNAME
 548                        ": unable to grab memory region 0x%lx-0x%lx\n",
 549                        chip->base, chip->base + BUFFSIZE - 1);
 550                release_region(chip->io, DSP_NUMIO);
 551                free_irq(chip->irq, chip);
 552                return -EBUSY;
 553        }
 554        chip->mappedbase = ioremap_nocache(chip->base, 0x8000);
 555        if (!chip->mappedbase) {
 556                printk(KERN_ERR LOGNAME
 557                        ": unable to map memory region 0x%lx-0x%lx\n",
 558                        chip->base, chip->base + BUFFSIZE - 1);
 559                err = -EIO;
 560                goto err_release_region;
 561        }
 562
 563        err = snd_msnd_dsp_full_reset(card);
 564        if (err < 0)
 565                goto err_release_region;
 566
 567        /* Register device */
 568        err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
 569        if (err < 0)
 570                goto err_release_region;
 571
 572        err = snd_msnd_pcm(card, 0);
 573        if (err < 0) {
 574                printk(KERN_ERR LOGNAME ": error creating new PCM device\n");
 575                goto err_release_region;
 576        }
 577
 578        err = snd_msndmix_new(card);
 579        if (err < 0) {
 580                printk(KERN_ERR LOGNAME ": error creating new Mixer device\n");
 581                goto err_release_region;
 582        }
 583
 584
 585        if (mpu_io[0] != SNDRV_AUTO_PORT) {
 586                struct snd_mpu401 *mpu;
 587
 588                err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
 589                                          mpu_io[0],
 590                                          MPU401_MODE_INPUT |
 591                                          MPU401_MODE_OUTPUT,
 592                                          mpu_irq[0],
 593                                          &chip->rmidi);
 594                if (err < 0) {
 595                        printk(KERN_ERR LOGNAME
 596                                ": error creating new Midi device\n");
 597                        goto err_release_region;
 598                }
 599                mpu = chip->rmidi->private_data;
 600
 601                mpu->open_input = snd_msnd_mpu401_open;
 602                mpu->close_input = snd_msnd_mpu401_close;
 603                mpu->private_data = chip;
 604        }
 605
 606        disable_irq(chip->irq);
 607        snd_msnd_calibrate_adc(chip, chip->play_sample_rate);
 608        snd_msndmix_force_recsrc(chip, 0);
 609
 610        err = snd_card_register(card);
 611        if (err < 0)
 612                goto err_release_region;
 613
 614        return 0;
 615
 616err_release_region:
 617        iounmap(chip->mappedbase);
 618        release_mem_region(chip->base, BUFFSIZE);
 619        release_region(chip->io, DSP_NUMIO);
 620        free_irq(chip->irq, chip);
 621        return err;
 622}
 623
 624
 625static void snd_msnd_unload(struct snd_card *card)
 626{
 627        struct snd_msnd *chip = card->private_data;
 628
 629        iounmap(chip->mappedbase);
 630        release_mem_region(chip->base, BUFFSIZE);
 631        release_region(chip->io, DSP_NUMIO);
 632        free_irq(chip->irq, chip);
 633        snd_card_free(card);
 634}
 635
 636#ifndef MSND_CLASSIC
 637
 638/* Pinnacle/Fiji Logical Device Configuration */
 639
 640static int snd_msnd_write_cfg(int cfg, int reg, int value)
 641{
 642        outb(reg, cfg);
 643        outb(value, cfg + 1);
 644        if (value != inb(cfg + 1)) {
 645                printk(KERN_ERR LOGNAME ": snd_msnd_write_cfg: I/O error\n");
 646                return -EIO;
 647        }
 648        return 0;
 649}
 650
 651static int snd_msnd_write_cfg_io0(int cfg, int num, u16 io)
 652{
 653        if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
 654                return -EIO;
 655        if (snd_msnd_write_cfg(cfg, IREG_IO0_BASEHI, HIBYTE(io)))
 656                return -EIO;
 657        if (snd_msnd_write_cfg(cfg, IREG_IO0_BASELO, LOBYTE(io)))
 658                return -EIO;
 659        return 0;
 660}
 661
 662static int snd_msnd_write_cfg_io1(int cfg, int num, u16 io)
 663{
 664        if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
 665                return -EIO;
 666        if (snd_msnd_write_cfg(cfg, IREG_IO1_BASEHI, HIBYTE(io)))
 667                return -EIO;
 668        if (snd_msnd_write_cfg(cfg, IREG_IO1_BASELO, LOBYTE(io)))
 669                return -EIO;
 670        return 0;
 671}
 672
 673static int snd_msnd_write_cfg_irq(int cfg, int num, u16 irq)
 674{
 675        if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
 676                return -EIO;
 677        if (snd_msnd_write_cfg(cfg, IREG_IRQ_NUMBER, LOBYTE(irq)))
 678                return -EIO;
 679        if (snd_msnd_write_cfg(cfg, IREG_IRQ_TYPE, IRQTYPE_EDGE))
 680                return -EIO;
 681        return 0;
 682}
 683
 684static int snd_msnd_write_cfg_mem(int cfg, int num, int mem)
 685{
 686        u16 wmem;
 687
 688        mem >>= 8;
 689        wmem = (u16)(mem & 0xfff);
 690        if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
 691                return -EIO;
 692        if (snd_msnd_write_cfg(cfg, IREG_MEMBASEHI, HIBYTE(wmem)))
 693                return -EIO;
 694        if (snd_msnd_write_cfg(cfg, IREG_MEMBASELO, LOBYTE(wmem)))
 695                return -EIO;
 696        if (wmem && snd_msnd_write_cfg(cfg, IREG_MEMCONTROL,
 697                                       MEMTYPE_HIADDR | MEMTYPE_16BIT))
 698                return -EIO;
 699        return 0;
 700}
 701
 702static int snd_msnd_activate_logical(int cfg, int num)
 703{
 704        if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
 705                return -EIO;
 706        if (snd_msnd_write_cfg(cfg, IREG_ACTIVATE, LD_ACTIVATE))
 707                return -EIO;
 708        return 0;
 709}
 710
 711static int snd_msnd_write_cfg_logical(int cfg, int num, u16 io0,
 712                                      u16 io1, u16 irq, int mem)
 713{
 714        if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
 715                return -EIO;
 716        if (snd_msnd_write_cfg_io0(cfg, num, io0))
 717                return -EIO;
 718        if (snd_msnd_write_cfg_io1(cfg, num, io1))
 719                return -EIO;
 720        if (snd_msnd_write_cfg_irq(cfg, num, irq))
 721                return -EIO;
 722        if (snd_msnd_write_cfg_mem(cfg, num, mem))
 723                return -EIO;
 724        if (snd_msnd_activate_logical(cfg, num))
 725                return -EIO;
 726        return 0;
 727}
 728
 729static int snd_msnd_pinnacle_cfg_reset(int cfg)
 730{
 731        int i;
 732
 733        /* Reset devices if told to */
 734        printk(KERN_INFO LOGNAME ": Resetting all devices\n");
 735        for (i = 0; i < 4; ++i)
 736                if (snd_msnd_write_cfg_logical(cfg, i, 0, 0, 0, 0))
 737                        return -EIO;
 738
 739        return 0;
 740}
 741#endif
 742
 743static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
 744static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
 745
 746module_param_array(index, int, NULL, 0444);
 747MODULE_PARM_DESC(index, "Index value for msnd_pinnacle soundcard.");
 748module_param_array(id, charp, NULL, 0444);
 749MODULE_PARM_DESC(id, "ID string for msnd_pinnacle soundcard.");
 750
 751static long io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 752static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
 753static long mem[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 754
 755#ifndef MSND_CLASSIC
 756static long cfg[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 757
 758/* Extra Peripheral Configuration (Default: Disable) */
 759static long ide_io0[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 760static long ide_io1[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 761static int ide_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
 762
 763static long joystick_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
 764/* If we have the digital daugherboard... */
 765static int digital[SNDRV_CARDS];
 766
 767/* Extra Peripheral Configuration */
 768static int reset[SNDRV_CARDS];
 769#endif
 770
 771static int write_ndelay[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
 772
 773static int calibrate_signal;
 774
 775#ifdef CONFIG_PNP
 776static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
 777module_param_array(isapnp, bool, NULL, 0444);
 778MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
 779#define has_isapnp(x) isapnp[x]
 780#else
 781#define has_isapnp(x) 0
 782#endif
 783
 784MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>");
 785MODULE_DESCRIPTION("Turtle Beach " LONGNAME " Linux Driver");
 786MODULE_LICENSE("GPL");
 787MODULE_FIRMWARE(INITCODEFILE);
 788MODULE_FIRMWARE(PERMCODEFILE);
 789
 790module_param_hw_array(io, long, ioport, NULL, 0444);
 791MODULE_PARM_DESC(io, "IO port #");
 792module_param_hw_array(irq, int, irq, NULL, 0444);
 793module_param_hw_array(mem, long, iomem, NULL, 0444);
 794module_param_array(write_ndelay, int, NULL, 0444);
 795module_param(calibrate_signal, int, 0444);
 796#ifndef MSND_CLASSIC
 797module_param_array(digital, int, NULL, 0444);
 798module_param_hw_array(cfg, long, ioport, NULL, 0444);
 799module_param_array(reset, int, NULL, 0444);
 800module_param_hw_array(mpu_io, long, ioport, NULL, 0444);
 801module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
 802module_param_hw_array(ide_io0, long, ioport, NULL, 0444);
 803module_param_hw_array(ide_io1, long, ioport, NULL, 0444);
 804module_param_hw_array(ide_irq, int, irq, NULL, 0444);
 805module_param_hw_array(joystick_io, long, ioport, NULL, 0444);
 806#endif
 807
 808
 809static int snd_msnd_isa_match(struct device *pdev, unsigned int i)
 810{
 811        if (io[i] == SNDRV_AUTO_PORT)
 812                return 0;
 813
 814        if (irq[i] == SNDRV_AUTO_PORT || mem[i] == SNDRV_AUTO_PORT) {
 815                printk(KERN_WARNING LOGNAME ": io, irq and mem must be set\n");
 816                return 0;
 817        }
 818
 819#ifdef MSND_CLASSIC
 820        if (!(io[i] == 0x290 ||
 821              io[i] == 0x260 ||
 822              io[i] == 0x250 ||
 823              io[i] == 0x240 ||
 824              io[i] == 0x230 ||
 825              io[i] == 0x220 ||
 826              io[i] == 0x210 ||
 827              io[i] == 0x3e0)) {
 828                printk(KERN_ERR LOGNAME ": \"io\" - DSP I/O base must be set "
 829                        " to 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x290, "
 830                        "or 0x3E0\n");
 831                return 0;
 832        }
 833#else
 834        if (io[i] < 0x100 || io[i] > 0x3e0 || (io[i] % 0x10) != 0) {
 835                printk(KERN_ERR LOGNAME
 836                        ": \"io\" - DSP I/O base must within the range 0x100 "
 837                        "to 0x3E0 and must be evenly divisible by 0x10\n");
 838                return 0;
 839        }
 840#endif /* MSND_CLASSIC */
 841
 842        if (!(irq[i] == 5 ||
 843              irq[i] == 7 ||
 844              irq[i] == 9 ||
 845              irq[i] == 10 ||
 846              irq[i] == 11 ||
 847              irq[i] == 12)) {
 848                printk(KERN_ERR LOGNAME
 849                        ": \"irq\" - must be set to 5, 7, 9, 10, 11 or 12\n");
 850                return 0;
 851        }
 852
 853        if (!(mem[i] == 0xb0000 ||
 854              mem[i] == 0xc8000 ||
 855              mem[i] == 0xd0000 ||
 856              mem[i] == 0xd8000 ||
 857              mem[i] == 0xe0000 ||
 858              mem[i] == 0xe8000)) {
 859                printk(KERN_ERR LOGNAME ": \"mem\" - must be set to "
 860                       "0xb0000, 0xc8000, 0xd0000, 0xd8000, 0xe0000 or "
 861                       "0xe8000\n");
 862                return 0;
 863        }
 864
 865#ifndef MSND_CLASSIC
 866        if (cfg[i] == SNDRV_AUTO_PORT) {
 867                printk(KERN_INFO LOGNAME ": Assuming PnP mode\n");
 868        } else if (cfg[i] != 0x250 && cfg[i] != 0x260 && cfg[i] != 0x270) {
 869                printk(KERN_INFO LOGNAME
 870                        ": Config port must be 0x250, 0x260 or 0x270 "
 871                        "(or unspecified for PnP mode)\n");
 872                return 0;
 873        }
 874#endif /* MSND_CLASSIC */
 875
 876        return 1;
 877}
 878
 879static int snd_msnd_isa_probe(struct device *pdev, unsigned int idx)
 880{
 881        int err;
 882        struct snd_card *card;
 883        struct snd_msnd *chip;
 884
 885        if (has_isapnp(idx)
 886#ifndef MSND_CLASSIC
 887            || cfg[idx] == SNDRV_AUTO_PORT
 888#endif
 889            ) {
 890                printk(KERN_INFO LOGNAME ": Assuming PnP mode\n");
 891                return -ENODEV;
 892        }
 893
 894        err = snd_card_new(pdev, index[idx], id[idx], THIS_MODULE,
 895                           sizeof(struct snd_msnd), &card);
 896        if (err < 0)
 897                return err;
 898
 899        chip = card->private_data;
 900        chip->card = card;
 901
 902#ifdef MSND_CLASSIC
 903        switch (irq[idx]) {
 904        case 5:
 905                chip->irqid = HPIRQ_5; break;
 906        case 7:
 907                chip->irqid = HPIRQ_7; break;
 908        case 9:
 909                chip->irqid = HPIRQ_9; break;
 910        case 10:
 911                chip->irqid = HPIRQ_10; break;
 912        case 11:
 913                chip->irqid = HPIRQ_11; break;
 914        case 12:
 915                chip->irqid = HPIRQ_12; break;
 916        }
 917
 918        switch (mem[idx]) {
 919        case 0xb0000:
 920                chip->memid = HPMEM_B000; break;
 921        case 0xc8000:
 922                chip->memid = HPMEM_C800; break;
 923        case 0xd0000:
 924                chip->memid = HPMEM_D000; break;
 925        case 0xd8000:
 926                chip->memid = HPMEM_D800; break;
 927        case 0xe0000:
 928                chip->memid = HPMEM_E000; break;
 929        case 0xe8000:
 930                chip->memid = HPMEM_E800; break;
 931        }
 932#else
 933        printk(KERN_INFO LOGNAME ": Non-PnP mode: configuring at port 0x%lx\n",
 934                        cfg[idx]);
 935
 936        if (!request_region(cfg[idx], 2, "Pinnacle/Fiji Config")) {
 937                printk(KERN_ERR LOGNAME ": Config port 0x%lx conflict\n",
 938                           cfg[idx]);
 939                snd_card_free(card);
 940                return -EIO;
 941        }
 942        if (reset[idx])
 943                if (snd_msnd_pinnacle_cfg_reset(cfg[idx])) {
 944                        err = -EIO;
 945                        goto cfg_error;
 946                }
 947
 948        /* DSP */
 949        err = snd_msnd_write_cfg_logical(cfg[idx], 0,
 950                                         io[idx], 0,
 951                                         irq[idx], mem[idx]);
 952
 953        if (err)
 954                goto cfg_error;
 955
 956        /* The following are Pinnacle specific */
 957
 958        /* MPU */
 959        if (mpu_io[idx] != SNDRV_AUTO_PORT
 960            && mpu_irq[idx] != SNDRV_AUTO_IRQ) {
 961                printk(KERN_INFO LOGNAME
 962                       ": Configuring MPU to I/O 0x%lx IRQ %d\n",
 963                       mpu_io[idx], mpu_irq[idx]);
 964                err = snd_msnd_write_cfg_logical(cfg[idx], 1,
 965                                                 mpu_io[idx], 0,
 966                                                 mpu_irq[idx], 0);
 967
 968                if (err)
 969                        goto cfg_error;
 970        }
 971
 972        /* IDE */
 973        if (ide_io0[idx] != SNDRV_AUTO_PORT
 974            && ide_io1[idx] != SNDRV_AUTO_PORT
 975            && ide_irq[idx] != SNDRV_AUTO_IRQ) {
 976                printk(KERN_INFO LOGNAME
 977                       ": Configuring IDE to I/O 0x%lx, 0x%lx IRQ %d\n",
 978                       ide_io0[idx], ide_io1[idx], ide_irq[idx]);
 979                err = snd_msnd_write_cfg_logical(cfg[idx], 2,
 980                                                 ide_io0[idx], ide_io1[idx],
 981                                                 ide_irq[idx], 0);
 982
 983                if (err)
 984                        goto cfg_error;
 985        }
 986
 987        /* Joystick */
 988        if (joystick_io[idx] != SNDRV_AUTO_PORT) {
 989                printk(KERN_INFO LOGNAME
 990                       ": Configuring joystick to I/O 0x%lx\n",
 991                       joystick_io[idx]);
 992                err = snd_msnd_write_cfg_logical(cfg[idx], 3,
 993                                                 joystick_io[idx], 0,
 994                                                 0, 0);
 995
 996                if (err)
 997                        goto cfg_error;
 998        }
 999        release_region(cfg[idx], 2);
1000
1001#endif /* MSND_CLASSIC */
1002
1003        set_default_audio_parameters(chip);
1004#ifdef MSND_CLASSIC
1005        chip->type = msndClassic;
1006#else
1007        chip->type = msndPinnacle;
1008#endif
1009        chip->io = io[idx];
1010        chip->irq = irq[idx];
1011        chip->base = mem[idx];
1012
1013        chip->calibrate_signal = calibrate_signal ? 1 : 0;
1014        chip->recsrc = 0;
1015        chip->dspq_data_buff = DSPQ_DATA_BUFF;
1016        chip->dspq_buff_size = DSPQ_BUFF_SIZE;
1017        if (write_ndelay[idx])
1018                clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1019        else
1020                set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1021#ifndef MSND_CLASSIC
1022        if (digital[idx])
1023                set_bit(F_HAVEDIGITAL, &chip->flags);
1024#endif
1025        spin_lock_init(&chip->lock);
1026        err = snd_msnd_probe(card);
1027        if (err < 0) {
1028                printk(KERN_ERR LOGNAME ": Probe failed\n");
1029                snd_card_free(card);
1030                return err;
1031        }
1032
1033        err = snd_msnd_attach(card);
1034        if (err < 0) {
1035                printk(KERN_ERR LOGNAME ": Attach failed\n");
1036                snd_card_free(card);
1037                return err;
1038        }
1039        dev_set_drvdata(pdev, card);
1040
1041        return 0;
1042
1043#ifndef MSND_CLASSIC
1044cfg_error:
1045        release_region(cfg[idx], 2);
1046        snd_card_free(card);
1047        return err;
1048#endif
1049}
1050
1051static int snd_msnd_isa_remove(struct device *pdev, unsigned int dev)
1052{
1053        snd_msnd_unload(dev_get_drvdata(pdev));
1054        return 0;
1055}
1056
1057static struct isa_driver snd_msnd_driver = {
1058        .match          = snd_msnd_isa_match,
1059        .probe          = snd_msnd_isa_probe,
1060        .remove         = snd_msnd_isa_remove,
1061        /* FIXME: suspend, resume */
1062        .driver         = {
1063                .name   = DEV_NAME
1064        },
1065};
1066
1067#ifdef CONFIG_PNP
1068static int snd_msnd_pnp_detect(struct pnp_card_link *pcard,
1069                               const struct pnp_card_device_id *pid)
1070{
1071        static int idx;
1072        struct pnp_dev *pnp_dev;
1073        struct pnp_dev *mpu_dev;
1074        struct snd_card *card;
1075        struct snd_msnd *chip;
1076        int ret;
1077
1078        for ( ; idx < SNDRV_CARDS; idx++) {
1079                if (has_isapnp(idx))
1080                        break;
1081        }
1082        if (idx >= SNDRV_CARDS)
1083                return -ENODEV;
1084
1085        /*
1086         * Check that we still have room for another sound card ...
1087         */
1088        pnp_dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1089        if (!pnp_dev)
1090                return -ENODEV;
1091
1092        mpu_dev = pnp_request_card_device(pcard, pid->devs[1].id, NULL);
1093        if (!mpu_dev)
1094                return -ENODEV;
1095
1096        if (!pnp_is_active(pnp_dev) && pnp_activate_dev(pnp_dev) < 0) {
1097                printk(KERN_INFO "msnd_pinnacle: device is inactive\n");
1098                return -EBUSY;
1099        }
1100
1101        if (!pnp_is_active(mpu_dev) && pnp_activate_dev(mpu_dev) < 0) {
1102                printk(KERN_INFO "msnd_pinnacle: MPU device is inactive\n");
1103                return -EBUSY;
1104        }
1105
1106        /*
1107         * Create a new ALSA sound card entry, in anticipation
1108         * of detecting our hardware ...
1109         */
1110        ret = snd_card_new(&pcard->card->dev,
1111                           index[idx], id[idx], THIS_MODULE,
1112                           sizeof(struct snd_msnd), &card);
1113        if (ret < 0)
1114                return ret;
1115
1116        chip = card->private_data;
1117        chip->card = card;
1118
1119        /*
1120         * Read the correct parameters off the ISA PnP bus ...
1121         */
1122        io[idx] = pnp_port_start(pnp_dev, 0);
1123        irq[idx] = pnp_irq(pnp_dev, 0);
1124        mem[idx] = pnp_mem_start(pnp_dev, 0);
1125        mpu_io[idx] = pnp_port_start(mpu_dev, 0);
1126        mpu_irq[idx] = pnp_irq(mpu_dev, 0);
1127
1128        set_default_audio_parameters(chip);
1129#ifdef MSND_CLASSIC
1130        chip->type = msndClassic;
1131#else
1132        chip->type = msndPinnacle;
1133#endif
1134        chip->io = io[idx];
1135        chip->irq = irq[idx];
1136        chip->base = mem[idx];
1137
1138        chip->calibrate_signal = calibrate_signal ? 1 : 0;
1139        chip->recsrc = 0;
1140        chip->dspq_data_buff = DSPQ_DATA_BUFF;
1141        chip->dspq_buff_size = DSPQ_BUFF_SIZE;
1142        if (write_ndelay[idx])
1143                clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1144        else
1145                set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
1146#ifndef MSND_CLASSIC
1147        if (digital[idx])
1148                set_bit(F_HAVEDIGITAL, &chip->flags);
1149#endif
1150        spin_lock_init(&chip->lock);
1151        ret = snd_msnd_probe(card);
1152        if (ret < 0) {
1153                printk(KERN_ERR LOGNAME ": Probe failed\n");
1154                goto _release_card;
1155        }
1156
1157        ret = snd_msnd_attach(card);
1158        if (ret < 0) {
1159                printk(KERN_ERR LOGNAME ": Attach failed\n");
1160                goto _release_card;
1161        }
1162
1163        pnp_set_card_drvdata(pcard, card);
1164        ++idx;
1165        return 0;
1166
1167_release_card:
1168        snd_card_free(card);
1169        return ret;
1170}
1171
1172static void snd_msnd_pnp_remove(struct pnp_card_link *pcard)
1173{
1174        snd_msnd_unload(pnp_get_card_drvdata(pcard));
1175        pnp_set_card_drvdata(pcard, NULL);
1176}
1177
1178static int isa_registered;
1179static int pnp_registered;
1180
1181static const struct pnp_card_device_id msnd_pnpids[] = {
1182        /* Pinnacle PnP */
1183        { .id = "BVJ0440", .devs = { { "TBS0000" }, { "TBS0001" } } },
1184        { .id = "" }    /* end */
1185};
1186
1187MODULE_DEVICE_TABLE(pnp_card, msnd_pnpids);
1188
1189static struct pnp_card_driver msnd_pnpc_driver = {
1190        .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1191        .name = "msnd_pinnacle",
1192        .id_table = msnd_pnpids,
1193        .probe = snd_msnd_pnp_detect,
1194        .remove = snd_msnd_pnp_remove,
1195};
1196#endif /* CONFIG_PNP */
1197
1198static int __init snd_msnd_init(void)
1199{
1200        int err;
1201
1202        err = isa_register_driver(&snd_msnd_driver, SNDRV_CARDS);
1203#ifdef CONFIG_PNP
1204        if (!err)
1205                isa_registered = 1;
1206
1207        err = pnp_register_card_driver(&msnd_pnpc_driver);
1208        if (!err)
1209                pnp_registered = 1;
1210
1211        if (isa_registered)
1212                err = 0;
1213#endif
1214        return err;
1215}
1216
1217static void __exit snd_msnd_exit(void)
1218{
1219#ifdef CONFIG_PNP
1220        if (pnp_registered)
1221                pnp_unregister_card_driver(&msnd_pnpc_driver);
1222        if (isa_registered)
1223#endif
1224                isa_unregister_driver(&snd_msnd_driver);
1225}
1226
1227module_init(snd_msnd_init);
1228module_exit(snd_msnd_exit);
1229
1230