Merge branch 'topic/core-cleanup' into for-linus
[safe/jmp/linux-2.6] / sound / pci / als4000.c
index 7c61561..6cf1de8 100644 (file)
@@ -1,19 +1,36 @@
 /*
  *  card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
  *  Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
- *                       Jaroslav Kysela <perex@suse.cz>
- *  Copyright (C) 2002 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
+ *                       Jaroslav Kysela <perex@perex.cz>
+ *  Copyright (C) 2002, 2008 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
  *
  *  Framework borrowed from Massimo Piccioni's card-als100.c.
  *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *
  * NOTES
  *
  *  Since Avance does not provide any meaningful documentation, and I
  *  bought an ALS4000 based soundcard, I was forced to base this driver
  *  on reverse engineering.
  *
- *  Note: this is no longer true. Pretty verbose chip docu (ALS4000a.PDF)
- *  can be found on the ALSA web site.
+ *  Note: this is no longer true (thank you!):
+ *  pretty verbose chip docu (ALS4000a.PDF) can be found on the ALSA web site.
+ *  Page numbers stated anywhere below with the "SPECS_PAGE:" tag
+ *  refer to: ALS4000a.PDF specs Ver 1.0, May 28th, 1998.
  *
  *  The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
  *  ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport 
  * Set KSound:
  * - value -> some port 0x0c0d
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
-
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ * ToDo:
+ * - by default, don't enable legacy game and use PCI game I/O
+ * - power management? (card can do voice wakeup according to datasheet!!)
  */
 
-#include <sound/driver.h>
 #include <asm/io.h>
 #include <linux/init.h>
 #include <linux/pci.h>
-#include <linux/slab.h>
 #include <linux/gameport.h>
 #include <linux/moduleparam.h>
+#include <linux/dma-mapping.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/rawmidi.h>
@@ -73,7 +79,7 @@
 #include <sound/sb.h>
 #include <sound/initval.h>
 
-MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
+MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>, Andreas Mohr");
 MODULE_DESCRIPTION("Avance Logic ALS4000");
 MODULE_LICENSE("GPL");
 MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
@@ -100,44 +106,186 @@ module_param_array(joystick_port, int, NULL, 0444);
 MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
 #endif
 
-typedef struct {
+struct snd_card_als4000 {
+       /* most frequent access first */
+       unsigned long iobase;
        struct pci_dev *pci;
-       unsigned long gcr;
+       struct snd_sb *chip;
 #ifdef SUPPORT_JOYSTICK
        struct gameport *gameport;
 #endif
-} snd_card_als4000_t;
+};
 
-static struct pci_device_id snd_als4000_ids[] = {
+static DEFINE_PCI_DEVICE_TABLE(snd_als4000_ids) = {
        { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* ALS4000 */
        { 0, }
 };
 
 MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
 
-static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
+enum als4k_iobase_t {
+       /* IOx: B == Byte, W = Word, D = DWord; SPECS_PAGE: 37 */
+       ALS4K_IOD_00_AC97_ACCESS = 0x00,
+       ALS4K_IOW_04_AC97_READ = 0x04,
+       ALS4K_IOB_06_AC97_STATUS = 0x06,
+       ALS4K_IOB_07_IRQSTATUS = 0x07,
+       ALS4K_IOD_08_GCR_DATA = 0x08,
+       ALS4K_IOB_0C_GCR_INDEX = 0x0c,
+       ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU = 0x0e,
+       ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
+       ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
+       ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
+       ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
+       ALS4K_IOB_14_MIXER_INDEX = 0x14,
+       ALS4K_IOB_15_MIXER_DATA = 0x15,
+       ALS4K_IOB_16_ESP_RESET = 0x16,
+       ALS4K_IOB_16_ACK_FOR_CR1E = 0x16, /* 2nd function */
+       ALS4K_IOB_18_OPL_ADDR0 = 0x18,
+       ALS4K_IOB_19_OPL_ADDR1 = 0x19,
+       ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
+       ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
+       ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
+       ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
+       ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
+       ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
+       ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
+       ALS4K_IOB_30_MIDI_DATA = 0x30,
+       ALS4K_IOB_31_MIDI_STATUS = 0x31,
+       ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
+};
+
+enum als4k_iobase_0e_t {
+       ALS4K_IOB_0E_MPU_IRQ = 0x10,
+       ALS4K_IOB_0E_CR1E_IRQ = 0x40,
+       ALS4K_IOB_0E_SB_DMA_IRQ = 0x80,
+};
+
+enum als4k_gcr_t { /* all registers 32bit wide; SPECS_PAGE: 38 to 42 */
+       ALS4K_GCR8C_MISC_CTRL = 0x8c,
+       ALS4K_GCR90_TEST_MODE_REG = 0x90,
+       ALS4K_GCR91_DMA0_ADDR = 0x91,
+       ALS4K_GCR92_DMA0_MODE_COUNT = 0x92,
+       ALS4K_GCR93_DMA1_ADDR = 0x93,
+       ALS4K_GCR94_DMA1_MODE_COUNT = 0x94,
+       ALS4K_GCR95_DMA3_ADDR = 0x95,
+       ALS4K_GCR96_DMA3_MODE_COUNT = 0x96,
+       ALS4K_GCR99_DMA_EMULATION_CTRL = 0x99,
+       ALS4K_GCRA0_FIFO1_CURRENT_ADDR = 0xa0,
+       ALS4K_GCRA1_FIFO1_STATUS_BYTECOUNT = 0xa1,
+       ALS4K_GCRA2_FIFO2_PCIADDR = 0xa2,
+       ALS4K_GCRA3_FIFO2_COUNT = 0xa3,
+       ALS4K_GCRA4_FIFO2_CURRENT_ADDR = 0xa4,
+       ALS4K_GCRA5_FIFO1_STATUS_BYTECOUNT = 0xa5,
+       ALS4K_GCRA6_PM_CTRL = 0xa6,
+       ALS4K_GCRA7_PCI_ACCESS_STORAGE = 0xa7,
+       ALS4K_GCRA8_LEGACY_CFG1 = 0xa8,
+       ALS4K_GCRA9_LEGACY_CFG2 = 0xa9,
+       ALS4K_GCRFF_DUMMY_SCRATCH = 0xff,
+};
+
+enum als4k_gcr8c_t {
+       ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
+       ALS4K_GCR8C_CHIP_REV_MASK = 0xf0000
+};
+
+static inline void snd_als4k_iobase_writeb(unsigned long iobase,
+                                               enum als4k_iobase_t reg,
+                                               u8 val)
 {
-       outb(reg, port+0x0c);
-       outl(val, port+0x08);
+       outb(val, iobase + reg);
 }
 
-static inline void snd_als4000_gcr_write(sb_t *sb, u32 reg, u32 val)
+static inline void snd_als4k_iobase_writel(unsigned long iobase,
+                                               enum als4k_iobase_t reg,
+                                               u32 val)
 {
-       snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
+       outl(val, iobase + reg);
+}
+
+static inline u8 snd_als4k_iobase_readb(unsigned long iobase,
+                                               enum als4k_iobase_t reg)
+{
+       return inb(iobase + reg);
+}
+
+static inline u32 snd_als4k_iobase_readl(unsigned long iobase,
+                                               enum als4k_iobase_t reg)
+{
+       return inl(iobase + reg);
+}
+
+static inline void snd_als4k_gcr_write_addr(unsigned long iobase,
+                                                enum als4k_gcr_t reg,
+                                                u32 val)
+{
+       snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
+       snd_als4k_iobase_writel(iobase, ALS4K_IOD_08_GCR_DATA, val);
+}
+
+static inline void snd_als4k_gcr_write(struct snd_sb *sb,
+                                        enum als4k_gcr_t reg,
+                                        u32 val)
+{
+       snd_als4k_gcr_write_addr(sb->alt_port, reg, val);
 }      
 
-static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
+static inline u32 snd_als4k_gcr_read_addr(unsigned long iobase,
+                                                enum als4k_gcr_t reg)
+{
+       /* SPECS_PAGE: 37/38 */
+       snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
+       return snd_als4k_iobase_readl(iobase, ALS4K_IOD_08_GCR_DATA);
+}
+
+static inline u32 snd_als4k_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
+{
+       return snd_als4k_gcr_read_addr(sb->alt_port, reg);
+}
+
+enum als4k_cr_t { /* all registers 8bit wide; SPECS_PAGE: 20 to 23 */
+       ALS4K_CR0_SB_CONFIG = 0x00,
+       ALS4K_CR2_MISC_CONTROL = 0x02,
+       ALS4K_CR3_CONFIGURATION = 0x03,
+       ALS4K_CR17_FIFO_STATUS = 0x17,
+       ALS4K_CR18_ESP_MAJOR_VERSION = 0x18,
+       ALS4K_CR19_ESP_MINOR_VERSION = 0x19,
+       ALS4K_CR1A_MPU401_UART_MODE_CONTROL = 0x1a,
+       ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO = 0x1c,
+       ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI = 0x1d,
+       ALS4K_CR1E_FIFO2_CONTROL = 0x1e, /* secondary PCM FIFO (recording) */
+       ALS4K_CR3A_MISC_CONTROL = 0x3a,
+       ALS4K_CR3B_CRC32_BYTE0 = 0x3b, /* for testing, activate via CR3A */
+       ALS4K_CR3C_CRC32_BYTE1 = 0x3c,
+       ALS4K_CR3D_CRC32_BYTE2 = 0x3d,
+       ALS4K_CR3E_CRC32_BYTE3 = 0x3e,
+};
+
+enum als4k_cr0_t {
+       ALS4K_CR0_DMA_CONTIN_MODE_CTRL = 0x02, /* IRQ/FIFO controlled for 0/1 */
+       ALS4K_CR0_DMA_90H_MODE_CTRL = 0x04, /* IRQ/FIFO controlled for 0/1 */
+       ALS4K_CR0_MX80_81_REG_WRITE_ENABLE = 0x80,
+};
+
+static inline void snd_als4_cr_write(struct snd_sb *chip,
+                                       enum als4k_cr_t reg,
+                                       u8 data)
 {
-       outb(reg, port+0x0c);
-       return inl(port+0x08);
+       /* Control Register is reg | 0xc0 (bit 7, 6 set) on sbmixer_index
+        * NOTE: assumes chip->mixer_lock to be locked externally already!
+        * SPECS_PAGE: 6 */
+       snd_sbmixer_write(chip, reg | 0xc0, data);
 }
 
-static inline u32 snd_als4000_gcr_read(sb_t *sb, u32 reg)
+static inline u8 snd_als4_cr_read(struct snd_sb *chip,
+                                       enum als4k_cr_t reg)
 {
-       return snd_als4000_gcr_read_addr(sb->alt_port, reg);
+       /* NOTE: assumes chip->mixer_lock to be locked externally already! */
+       return snd_sbmixer_read(chip, reg | 0xc0);
 }
 
-static void snd_als4000_set_rate(sb_t *chip, unsigned int rate)
+
+
+static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
 {
        if (!(chip->mode & SB_RATE_LOCK)) {
                snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
@@ -146,23 +294,29 @@ static void snd_als4000_set_rate(sb_t *chip, unsigned int rate)
        }
 }
 
-static void snd_als4000_set_capture_dma(sb_t *chip, dma_addr_t addr, unsigned size)
+static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
+                                              dma_addr_t addr, unsigned size)
 {
-       snd_als4000_gcr_write(chip, 0xa2, addr);
-       snd_als4000_gcr_write(chip, 0xa3, (size-1));
+       /* SPECS_PAGE: 40 */
+       snd_als4k_gcr_write(chip, ALS4K_GCRA2_FIFO2_PCIADDR, addr);
+       snd_als4k_gcr_write(chip, ALS4K_GCRA3_FIFO2_COUNT, (size-1));
 }
 
-static void snd_als4000_set_playback_dma(sb_t *chip, dma_addr_t addr, unsigned size)
+static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
+                                               dma_addr_t addr,
+                                               unsigned size)
 {
-       snd_als4000_gcr_write(chip, 0x91, addr);
-       snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
+       /* SPECS_PAGE: 38 */
+       snd_als4k_gcr_write(chip, ALS4K_GCR91_DMA0_ADDR, addr);
+       snd_als4k_gcr_write(chip, ALS4K_GCR92_DMA0_MODE_COUNT,
+                                                       (size-1)|0x180000);
 }
 
 #define ALS4000_FORMAT_SIGNED  (1<<0)
 #define ALS4000_FORMAT_16BIT   (1<<1)
 #define ALS4000_FORMAT_STEREO  (1<<2)
 
-static int snd_als4000_get_format(snd_pcm_runtime_t *runtime)
+static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
 {
        int result;
 
@@ -177,7 +331,7 @@ static int snd_als4000_get_format(snd_pcm_runtime_t *runtime)
 }
 
 /* structure for setting up playback */
-static struct {
+static const struct {
        unsigned char dsp_cmd, dma_on, dma_off, format;
 } playback_cmd_vals[]={
 /* ALS4000_FORMAT_U8_MONO */
@@ -201,7 +355,7 @@ static struct {
 
 /* structure for setting up capture */
 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
-static unsigned char capture_cmd_vals[]=
+static const unsigned char capture_cmd_vals[]=
 {
 CMD_WIDTH8|CMD_MONO,                   /* ALS4000_FORMAT_U8_MONO */
 CMD_WIDTH8|CMD_SIGNED|CMD_MONO,                /* ALS4000_FORMAT_S8_MONO */    
@@ -214,23 +368,22 @@ CMD_SIGNED|CMD_STEREO,                    /* ALS4000_FORMAT_S16L_STEREO */
 };     
 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
 
-static int snd_als4000_hw_params(snd_pcm_substream_t * substream,
-                                snd_pcm_hw_params_t * hw_params)
+static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
+                                struct snd_pcm_hw_params *hw_params)
 {
        return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
 }
 
-static int snd_als4000_hw_free(snd_pcm_substream_t * substream)
+static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
 {
        snd_pcm_lib_free_pages(substream);
        return 0;
 }
 
-static int snd_als4000_capture_prepare(snd_pcm_substream_t * substream)
+static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
 {
-       unsigned long flags;
-       sb_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
        unsigned long size;
        unsigned count;
 
@@ -240,25 +393,24 @@ static int snd_als4000_capture_prepare(snd_pcm_substream_t * substream)
        count = snd_pcm_lib_period_bytes(substream);
        
        if (chip->capture_format & ALS4000_FORMAT_16BIT)
-               count >>=1;
+               count >>= 1;
        count--;
 
-       spin_lock_irqsave(&chip->reg_lock, flags);
+       spin_lock_irq(&chip->reg_lock);
        snd_als4000_set_rate(chip, runtime->rate);
        snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
-       spin_lock_irqsave(&chip->mixer_lock, flags );
-       snd_sbmixer_write(chip, 0xdc, count);
-       snd_sbmixer_write(chip, 0xdd, count>>8);
-       spin_unlock_irqrestore(&chip->mixer_lock, flags );
+       spin_unlock_irq(&chip->reg_lock);
+       spin_lock_irq(&chip->mixer_lock);
+       snd_als4_cr_write(chip, ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO, count & 0xff);
+       snd_als4_cr_write(chip, ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI, count >> 8);
+       spin_unlock_irq(&chip->mixer_lock);
        return 0;
 }
 
-static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
+static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
 {
-       unsigned long flags;
-       sb_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
        unsigned long size;
        unsigned count;
 
@@ -268,7 +420,7 @@ static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
        count = snd_pcm_lib_period_bytes(substream);
        
        if (chip->playback_format & ALS4000_FORMAT_16BIT)
-               count >>=1;
+               count >>= 1;
        count--;
        
        /* FIXME: from second playback on, there's a lot more clicks and pops
@@ -277,7 +429,7 @@ static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
         * reordering, ...). Something seems to get enabled on playback
         * that I haven't found out how to disable again, which then causes
         * the switching pops to reach the speakers the next time here. */
-       spin_lock_irqsave(&chip->reg_lock, flags);
+       spin_lock_irq(&chip->reg_lock);
        snd_als4000_set_rate(chip, runtime->rate);
        snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
        
@@ -285,110 +437,162 @@ static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
        /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
        snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
        snd_sbdsp_command(chip, playback_cmd(chip).format);
-       snd_sbdsp_command(chip, count);
-       snd_sbdsp_command(chip, count>>8);
+       snd_sbdsp_command(chip, count & 0xff);
+       snd_sbdsp_command(chip, count >> 8);
        snd_sbdsp_command(chip, playback_cmd(chip).dma_off);    
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock_irq(&chip->reg_lock);
        
        return 0;
 }
 
-static int snd_als4000_capture_trigger(snd_pcm_substream_t * substream, int cmd)
+static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        int result = 0;
        
+       /* FIXME race condition in here!!!
+          chip->mode non-atomic update gets consistently protected
+          by reg_lock always, _except_ for this place!!
+          Probably need to take reg_lock as outer (or inner??) lock, too.
+          (or serialize both lock operations? probably not, though... - racy?)
+       */
        spin_lock(&chip->mixer_lock);
-       if (cmd == SNDRV_PCM_TRIGGER_START) {
+       switch (cmd) {
+       case SNDRV_PCM_TRIGGER_START:
+       case SNDRV_PCM_TRIGGER_RESUME:
                chip->mode |= SB_RATE_LOCK_CAPTURE;
-               snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
-       } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
+               snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
+                                                        capture_cmd(chip));
+               break;
+       case SNDRV_PCM_TRIGGER_STOP:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
                chip->mode &= ~SB_RATE_LOCK_CAPTURE;
-               snd_sbmixer_write(chip, 0xde, 0);
-       } else {
+               snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
+                                                        capture_cmd(chip));
+               break;
+       default:
                result = -EINVAL;
+               break;
        }
        spin_unlock(&chip->mixer_lock);
        return result;
 }
 
-static int snd_als4000_playback_trigger(snd_pcm_substream_t * substream, int cmd)
+static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        int result = 0;
 
        spin_lock(&chip->reg_lock);
-       if (cmd == SNDRV_PCM_TRIGGER_START) {
+       switch (cmd) {
+       case SNDRV_PCM_TRIGGER_START:
+       case SNDRV_PCM_TRIGGER_RESUME:
                chip->mode |= SB_RATE_LOCK_PLAYBACK;
                snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
-       } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
+               break;
+       case SNDRV_PCM_TRIGGER_STOP:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
                snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
                chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
-       } else {
+               break;
+       default:
                result = -EINVAL;
+               break;
        }
        spin_unlock(&chip->reg_lock);
        return result;
 }
 
-static snd_pcm_uframes_t snd_als4000_capture_pointer(snd_pcm_substream_t * substream)
+static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        unsigned int result;
 
        spin_lock(&chip->reg_lock);     
-       result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
+       result = snd_als4k_gcr_read(chip, ALS4K_GCRA4_FIFO2_CURRENT_ADDR);
        spin_unlock(&chip->reg_lock);
+       result &= 0xffff;
        return bytes_to_frames( substream->runtime, result );
 }
 
-static snd_pcm_uframes_t snd_als4000_playback_pointer(snd_pcm_substream_t * substream)
+static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        unsigned result;
 
        spin_lock(&chip->reg_lock);     
-       result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
+       result = snd_als4k_gcr_read(chip, ALS4K_GCRA0_FIFO1_CURRENT_ADDR);
        spin_unlock(&chip->reg_lock);
+       result &= 0xffff;
        return bytes_to_frames( substream->runtime, result );
 }
 
-static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+/* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
+ * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
+ * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
+ * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
+ * register (alt_port + ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU). Probably something
+ * could be optimized here to query/write one register only...
+ * And even if both registers need to be queried, then there's still the
+ * question of whether it's actually correct to ACK PCI IRQ before reading
+ * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
+ * SB IRQ status.
+ * (hmm, SPECS_PAGE: 38 mentions it the other way around!)
+ * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
+ * */
+static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
 {
-       sb_t *chip = dev_id;
-       unsigned gcr_status;
-       unsigned sb_status;
-
-       /* find out which bit of the ALS4000 produced the interrupt */
-       gcr_status = inb(chip->alt_port + 0xe);
-
-       if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
+       struct snd_sb *chip = dev_id;
+       unsigned pci_irqstatus;
+       unsigned sb_irqstatus;
+
+       /* find out which bit of the ALS4000 PCI block produced the interrupt,
+          SPECS_PAGE: 38, 5 */
+       pci_irqstatus = snd_als4k_iobase_readb(chip->alt_port,
+                                ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU);
+       if ((pci_irqstatus & ALS4K_IOB_0E_SB_DMA_IRQ)
+        && (chip->playback_substream)) /* playback */
                snd_pcm_period_elapsed(chip->playback_substream);
-       if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
+       if ((pci_irqstatus & ALS4K_IOB_0E_CR1E_IRQ)
+        && (chip->capture_substream)) /* capturing */
                snd_pcm_period_elapsed(chip->capture_substream);
-       if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
-               snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
-       /* release the gcr */
-       outb(gcr_status, chip->alt_port + 0xe);
+       if ((pci_irqstatus & ALS4K_IOB_0E_MPU_IRQ)
+        && (chip->rmidi)) /* MPU401 interrupt */
+               snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
+       /* ACK the PCI block IRQ */
+       snd_als4k_iobase_writeb(chip->alt_port,
+                        ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU, pci_irqstatus);
        
        spin_lock(&chip->mixer_lock);
-       sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
+       /* SPECS_PAGE: 20 */
+       sb_irqstatus = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
        spin_unlock(&chip->mixer_lock);
        
-       if (sb_status & SB_IRQTYPE_8BIT) 
+       if (sb_irqstatus & SB_IRQTYPE_8BIT)
                snd_sb_ack_8bit(chip);
-       if (sb_status & SB_IRQTYPE_16BIT) 
+       if (sb_irqstatus & SB_IRQTYPE_16BIT)
                snd_sb_ack_16bit(chip);
-       if (sb_status & SB_IRQTYPE_MPUIN)
+       if (sb_irqstatus & SB_IRQTYPE_MPUIN)
                inb(chip->mpu_port);
-       if (sb_status & 0x20)
-               inb(SBP(chip, RESET));
-       return IRQ_HANDLED;
+       if (sb_irqstatus & ALS4K_IRQTYPE_CR1E_DMA)
+               snd_als4k_iobase_readb(chip->alt_port,
+                                       ALS4K_IOB_16_ACK_FOR_CR1E);
+
+       /* printk(KERN_INFO "als4000: irq 0x%04x 0x%04x\n",
+                                        pci_irqstatus, sb_irqstatus); */
+
+       /* only ack the things we actually handled above */
+       return IRQ_RETVAL(
+            (pci_irqstatus & (ALS4K_IOB_0E_SB_DMA_IRQ|ALS4K_IOB_0E_CR1E_IRQ|
+                               ALS4K_IOB_0E_MPU_IRQ))
+         || (sb_irqstatus & (SB_IRQTYPE_8BIT|SB_IRQTYPE_16BIT|
+                               SB_IRQTYPE_MPUIN|ALS4K_IRQTYPE_CR1E_DMA))
+       );
 }
 
 /*****************************************************************/
 
-static snd_pcm_hardware_t snd_als4000_playback =
+static struct snd_pcm_hardware snd_als4000_playback =
 {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                                 SNDRV_PCM_INFO_MMAP_VALID),
@@ -407,7 +611,7 @@ static snd_pcm_hardware_t snd_als4000_playback =
        .fifo_size =            0
 };
 
-static snd_pcm_hardware_t snd_als4000_capture =
+static struct snd_pcm_hardware snd_als4000_capture =
 {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                                 SNDRV_PCM_INFO_MMAP_VALID),
@@ -428,38 +632,38 @@ static snd_pcm_hardware_t snd_als4000_capture =
 
 /*****************************************************************/
 
-static int snd_als4000_playback_open(snd_pcm_substream_t * substream)
+static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
 
        chip->playback_substream = substream;
        runtime->hw = snd_als4000_playback;
        return 0;
 }
 
-static int snd_als4000_playback_close(snd_pcm_substream_t * substream)
+static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
 
        chip->playback_substream = NULL;
        snd_pcm_lib_free_pages(substream);
        return 0;
 }
 
-static int snd_als4000_capture_open(snd_pcm_substream_t * substream)
+static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
 
        chip->capture_substream = substream;
        runtime->hw = snd_als4000_capture;
        return 0;
 }
 
-static int snd_als4000_capture_close(snd_pcm_substream_t * substream)
+static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
 {
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
 
        chip->capture_substream = NULL;
        snd_pcm_lib_free_pages(substream);
@@ -468,7 +672,7 @@ static int snd_als4000_capture_close(snd_pcm_substream_t * substream)
 
 /******************************************************************/
 
-static snd_pcm_ops_t snd_als4000_playback_ops = {
+static struct snd_pcm_ops snd_als4000_playback_ops = {
        .open =         snd_als4000_playback_open,
        .close =        snd_als4000_playback_close,
        .ioctl =        snd_pcm_lib_ioctl,
@@ -479,7 +683,7 @@ static snd_pcm_ops_t snd_als4000_playback_ops = {
        .pointer =      snd_als4000_playback_pointer
 };
 
-static snd_pcm_ops_t snd_als4000_capture_ops = {
+static struct snd_pcm_ops snd_als4000_capture_ops = {
        .open =         snd_als4000_capture_open,
        .close =        snd_als4000_capture_close,
        .ioctl =        snd_pcm_lib_ioctl,
@@ -490,21 +694,14 @@ static snd_pcm_ops_t snd_als4000_capture_ops = {
        .pointer =      snd_als4000_capture_pointer
 };
 
-static void snd_als4000_pcm_free(snd_pcm_t *pcm)
+static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
 {
-       sb_t *chip = pcm->private_data;
-       chip->pcm = NULL;
-       snd_pcm_lib_preallocate_free_for_all(pcm);
-}
-
-static int __devinit snd_als4000_pcm(sb_t *chip, int device)
-{
-       snd_pcm_t *pcm;
+       struct snd_pcm *pcm;
        int err;
 
-       if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
+       err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm);
+       if (err < 0)
                return err;
-       pcm->private_free = snd_als4000_pcm_free;
        pcm->private_data = chip;
        pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
@@ -520,53 +717,60 @@ static int __devinit snd_als4000_pcm(sb_t *chip, int device)
 
 /******************************************************************/
 
-static void snd_als4000_set_addr(unsigned long gcr,
-                                       unsigned int sb,
-                                       unsigned int mpu,
-                                       unsigned int opl,
-                                       unsigned int game)
+static void snd_als4000_set_addr(unsigned long iobase,
+                                       unsigned int sb_io,
+                                       unsigned int mpu_io,
+                                       unsigned int opl_io,
+                                       unsigned int game_io)
 {
-       u32 confA = 0;
-       u32 confB = 0;
-
-       if (mpu > 0)
-               confB |= (mpu | 1) << 16;
-       if (sb > 0)
-               confB |= (sb | 1);
-       if (game > 0)
-               confA |= (game | 1) << 16;
-       if (opl > 0)    
-               confA |= (opl | 1);
-       snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
-       snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
+       u32 cfg1 = 0;
+       u32 cfg2 = 0;
+
+       if (mpu_io > 0)
+               cfg2 |= (mpu_io | 1) << 16;
+       if (sb_io > 0)
+               cfg2 |= (sb_io | 1);
+       if (game_io > 0)
+               cfg1 |= (game_io | 1) << 16;
+       if (opl_io > 0)
+               cfg1 |= (opl_io | 1);
+       snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA8_LEGACY_CFG1, cfg1);
+       snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA9_LEGACY_CFG2, cfg2);
 }
 
-static void __devinit snd_als4000_configure(sb_t *chip)
+static void snd_als4000_configure(struct snd_sb *chip)
 {
-       unsigned tmp;
+       u8 tmp;
        int i;
 
        /* do some more configuration */
        spin_lock_irq(&chip->mixer_lock);
-       tmp = snd_sbmixer_read(chip, 0xc0);
-       snd_sbmixer_write(chip, 0xc0, tmp|0x80);
-       /* always select DMA channel 0, since we do not actually use DMA */
+       tmp = snd_als4_cr_read(chip, ALS4K_CR0_SB_CONFIG);
+       snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
+                               tmp|ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
+       /* always select DMA channel 0, since we do not actually use DMA
+        * SPECS_PAGE: 19/20 */
        snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
-       snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
+       snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
+                                tmp & ~ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
        spin_unlock_irq(&chip->mixer_lock);
        
        spin_lock_irq(&chip->reg_lock);
-       /* magic number. Enables interrupts(?) */
-       snd_als4000_gcr_write(chip, 0x8c, 0x28000);
-       for(i = 0x91; i <= 0x96; ++i)
-               snd_als4000_gcr_write(chip, i, 0);
+       /* enable interrupts */
+       snd_als4k_gcr_write(chip, ALS4K_GCR8C_MISC_CTRL,
+                                       ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE);
+
+       /* SPECS_PAGE: 39 */
+       for (i = ALS4K_GCR91_DMA0_ADDR; i <= ALS4K_GCR96_DMA3_MODE_COUNT; ++i)
+               snd_als4k_gcr_write(chip, i, 0);
        
-       snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
+       snd_als4k_gcr_write(chip, ALS4K_GCR99_DMA_EMULATION_CTRL,
+               snd_als4k_gcr_read(chip, ALS4K_GCR99_DMA_EMULATION_CTRL));
        spin_unlock_irq(&chip->reg_lock);
 }
 
 #ifdef SUPPORT_JOYSTICK
-static int __devinit snd_als4000_create_gameport(snd_card_als4000_t *acard, int dev)
+static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
 {
        struct gameport *gp;
        struct resource *r;
@@ -605,14 +809,14 @@ static int __devinit snd_als4000_create_gameport(snd_card_als4000_t *acard, int
        gameport_set_port_data(gp, r);
 
        /* Enable legacy joystick port */
-       snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
+       snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
 
        gameport_register_port(acard->gameport);
 
        return 0;
 }
 
-static void snd_als4000_free_gameport(snd_card_als4000_t *acard)
+static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
 {
        if (acard->gameport) {
                struct resource *r = gameport_get_port_data(acard->gameport);
@@ -620,21 +824,23 @@ static void snd_als4000_free_gameport(snd_card_als4000_t *acard)
                gameport_unregister_port(acard->gameport);
                acard->gameport = NULL;
 
-               snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
+               /* disable joystick */
+               snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
+
                release_and_free_resource(r);
        }
 }
 #else
-static inline int snd_als4000_create_gameport(snd_card_als4000_t *acard, int dev) { return -ENOSYS; }
-static inline void snd_als4000_free_gameport(snd_card_als4000_t *acard) { }
+static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
+static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
 #endif
 
-static void snd_card_als4000_free( snd_card_t *card )
+static void snd_card_als4000_free( struct snd_card *card )
 {
-       snd_card_als4000_t * acard = (snd_card_als4000_t *)card->private_data;
+       struct snd_card_als4000 *acard = card->private_data;
 
        /* make sure that interrupts are disabled */
-       snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
+       snd_als4k_gcr_write_addr(acard->iobase, ALS4K_GCR8C_MISC_CTRL, 0);
        /* free resources */
        snd_als4000_free_gameport(acard);
        pci_release_regions(acard->pci);
@@ -645,11 +851,11 @@ static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
                                          const struct pci_device_id *pci_id)
 {
        static int dev;
-       snd_card_t *card;
-       snd_card_als4000_t *acard;
-       unsigned long gcr;
-       sb_t *chip;
-       opl3_t *opl3;
+       struct snd_card *card;
+       struct snd_card_als4000 *acard;
+       unsigned long iobase;
+       struct snd_sb *chip;
+       struct snd_opl3 *opl3;
        unsigned short word;
        int err;
 
@@ -665,8 +871,8 @@ static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
                return err;
        }
        /* check, if we can restrict PCI DMA transfers to 24 bits */
-       if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
-           pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
+       if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
+           pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
                snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
                pci_disable_device(pci);
                return -ENXIO;
@@ -676,42 +882,44 @@ static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
                pci_disable_device(pci);
                return err;
        }
-       gcr = pci_resource_start(pci, 0);
+       iobase = pci_resource_start(pci, 0);
 
        pci_read_config_word(pci, PCI_COMMAND, &word);
        pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
        pci_set_master(pci);
        
-       card = snd_card_new(index[dev], id[dev], THIS_MODULE, 
-                           sizeof( snd_card_als4000_t ) );
-       if (card == NULL) {
+       err = snd_card_create(index[dev], id[dev], THIS_MODULE, 
+                             sizeof(*acard) /* private_data: acard */,
+                             &card);
+       if (err < 0) {
                pci_release_regions(pci);
                pci_disable_device(pci);
-               return -ENOMEM;
+               return err;
        }
 
-       acard = (snd_card_als4000_t *)card->private_data;
+       acard = card->private_data;
        acard->pci = pci;
-       acard->gcr = gcr;
+       acard->iobase = iobase;
        card->private_free = snd_card_als4000_free;
 
        /* disable all legacy ISA stuff */
-       snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
+       snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
 
        if ((err = snd_sbdsp_create(card,
-                                   gcr + 0x10,
+                                   iobase + ALS4K_IOB_10_ADLIB_ADDR0,
                                    pci->irq,
+               /* internally registered as IRQF_SHARED in case of ALS4000 SB */
                                    snd_als4000_interrupt,
                                    -1,
                                    -1,
                                    SB_HW_ALS4000,
                                    &chip)) < 0) {
-               snd_card_free(card);
-               return err;
+               goto out_err;
        }
+       acard->chip = chip;
 
        chip->pci = pci;
-       chip->alt_port = gcr;
+       chip->alt_port = iobase;
        snd_card_set_dev(card, &pci->dev);
 
        snd_als4000_configure(chip);
@@ -722,42 +930,54 @@ static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
                card->shortname, chip->alt_port, chip->irq);
 
        if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
-                                       gcr+0x30, 1, pci->irq, 0,
-                                       &chip->rmidi)) < 0) {
-               snd_card_free(card);
-               printk(KERN_ERR "als4000: no MPU-401device at 0x%lx ?\n", gcr+0x30);
-               return err;
+                                       iobase + ALS4K_IOB_30_MIDI_DATA,
+                                       MPU401_INFO_INTEGRATED,
+                                       pci->irq, 0, &chip->rmidi)) < 0) {
+               printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n",
+                               iobase + ALS4K_IOB_30_MIDI_DATA);
+               goto out_err;
        }
+       /* FIXME: ALS4000 has interesting MPU401 configuration features
+        * at ALS4K_CR1A_MPU401_UART_MODE_CONTROL
+        * (pass-thru / UART switching, fast MIDI clock, etc.),
+        * however there doesn't seem to be an ALSA API for this...
+        * SPECS_PAGE: 21 */
 
        if ((err = snd_als4000_pcm(chip, 0)) < 0) {
-               snd_card_free(card);
-               return err;
+               goto out_err;
        }
        if ((err = snd_sbmixer_new(chip)) < 0) {
-               snd_card_free(card);
-               return err;
+               goto out_err;
        }           
 
-       if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
+       if (snd_opl3_create(card,
+                               iobase + ALS4K_IOB_10_ADLIB_ADDR0,
+                               iobase + ALS4K_IOB_12_ADLIB_ADDR2,
                            OPL3_HW_AUTO, 1, &opl3) < 0) {
-               printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx ?\n",
-                          gcr+0x10, gcr+0x12 );
+               printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
+                          iobase + ALS4K_IOB_10_ADLIB_ADDR0,
+                          iobase + ALS4K_IOB_12_ADLIB_ADDR2);
        } else {
                if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
-                       snd_card_free(card);
-                       return err;
+                       goto out_err;
                }
        }
 
        snd_als4000_create_gameport(acard, dev);
 
        if ((err = snd_card_register(card)) < 0) {
-               snd_card_free(card);
-               return err;
+               goto out_err;
        }
        pci_set_drvdata(pci, card);
        dev++;
-       return 0;
+       err = 0;
+       goto out;
+
+out_err:
+       snd_card_free(card);
+       
+out:
+       return err;
 }
 
 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
@@ -766,12 +986,64 @@ static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
        pci_set_drvdata(pci, NULL);
 }
 
+#ifdef CONFIG_PM
+static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
+{
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct snd_card_als4000 *acard = card->private_data;
+       struct snd_sb *chip = acard->chip;
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+       
+       snd_pcm_suspend_all(chip->pcm);
+       snd_sbmixer_suspend(chip);
+
+       pci_disable_device(pci);
+       pci_save_state(pci);
+       pci_set_power_state(pci, pci_choose_state(pci, state));
+       return 0;
+}
+
+static int snd_als4000_resume(struct pci_dev *pci)
+{
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct snd_card_als4000 *acard = card->private_data;
+       struct snd_sb *chip = acard->chip;
+
+       pci_set_power_state(pci, PCI_D0);
+       pci_restore_state(pci);
+       if (pci_enable_device(pci) < 0) {
+               printk(KERN_ERR "als4000: pci_enable_device failed, "
+                      "disabling device\n");
+               snd_card_disconnect(card);
+               return -EIO;
+       }
+       pci_set_master(pci);
+
+       snd_als4000_configure(chip);
+       snd_sbdsp_reset(chip);
+       snd_sbmixer_resume(chip);
+
+#ifdef SUPPORT_JOYSTICK
+       if (acard->gameport)
+               snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
+#endif
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       return 0;
+}
+#endif /* CONFIG_PM */
+
+
 static struct pci_driver driver = {
        .name = "ALS4000",
-       .owner = THIS_MODULE,
        .id_table = snd_als4000_ids,
        .probe = snd_card_als4000_probe,
        .remove = __devexit_p(snd_card_als4000_remove),
+#ifdef CONFIG_PM
+       .suspend = snd_als4000_suspend,
+       .resume = snd_als4000_resume,
+#endif
 };
 
 static int __init alsa_card_als4000_init(void)