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