fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / sound / pci / als4000.c
index 73b1d3a..8fb55d3 100644 (file)
@@ -6,6 +6,21 @@
  *
  *  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
  * 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:
+ * - Proper shared IRQ handling?
+ * - power management? (card can do voice wakeup according to datasheet!!)
  */
 
 #include <sound/driver.h>
@@ -65,6 +70,7 @@
 #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>
@@ -76,8 +82,7 @@
 MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
 MODULE_DESCRIPTION("Avance Logic ALS4000");
 MODULE_LICENSE("GPL");
-MODULE_CLASSES("{sound}");
-MODULE_DEVICES("{{Avance Logic,ALS4000}}");
+MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
 
 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
 #define SUPPORT_JOYSTICK 1
@@ -89,33 +94,27 @@ static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;  /* Enable this card *
 #ifdef SUPPORT_JOYSTICK
 static int joystick_port[SNDRV_CARDS];
 #endif
-static int boot_devs;
 
-module_param_array(index, int, boot_devs, 0444);
+module_param_array(index, int, NULL, 0444);
 MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
-MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
-module_param_array(id, charp, boot_devs, 0444);
+module_param_array(id, charp, NULL, 0444);
 MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
-MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
-module_param_array(enable, bool, boot_devs, 0444);
+module_param_array(enable, bool, NULL, 0444);
 MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
-MODULE_PARM_SYNTAX(enable, SNDRV_INDEX_DESC);
 #ifdef SUPPORT_JOYSTICK
-module_param_array(joystick_port, int, boot_devs, 0444);
+module_param_array(joystick_port, int, NULL, 0444);
 MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
-MODULE_PARM_SYNTAX(joystick_port, SNDRV_ENABLED);
 #endif
 
-#define chip_t sb_t
-
-typedef struct {
+struct snd_card_als4000 {
+       /* most frequent access first */
        unsigned long gcr;
-       struct resource *res_gcr;
+       struct pci_dev *pci;
+       struct snd_sb *chip;
 #ifdef SUPPORT_JOYSTICK
-       struct gameport gameport;
-       struct resource *res_joystick;
+       struct gameport *gameport;
 #endif
-} snd_card_als4000_t;
+};
 
 static struct pci_device_id snd_als4000_ids[] = {
        { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* ALS4000 */
@@ -130,7 +129,7 @@ static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 v
        outl(val, port+0x08);
 }
 
-static inline void snd_als4000_gcr_write(sb_t *sb, u32 reg, u32 val)
+static inline void snd_als4000_gcr_write(struct snd_sb *sb, u32 reg, u32 val)
 {
        snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
 }      
@@ -141,12 +140,12 @@ static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
        return inl(port+0x08);
 }
 
-static inline u32 snd_als4000_gcr_read(sb_t *sb, u32 reg)
+static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, u32 reg)
 {
        return snd_als4000_gcr_read_addr(sb->alt_port, reg);
 }
 
-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);
@@ -155,13 +154,15 @@ 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));
 }
 
-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);
@@ -171,7 +172,7 @@ static void snd_als4000_set_playback_dma(sb_t *chip, dma_addr_t addr, unsigned s
 #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;
 
@@ -186,7 +187,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 */
@@ -210,7 +211,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 */    
@@ -223,23 +224,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;
 
@@ -252,22 +252,21 @@ static int snd_als4000_capture_prepare(snd_pcm_substream_t * substream)
                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 );
+       spin_unlock_irq(&chip->reg_lock);
+       spin_lock_irq(&chip->mixer_lock);
        snd_sbmixer_write(chip, 0xdc, count);
        snd_sbmixer_write(chip, 0xdd, count>>8);
-       spin_unlock_irqrestore(&chip->mixer_lock, flags );
+       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;
 
@@ -286,7 +285,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);
        
@@ -297,79 +296,98 @@ static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
        snd_sbdsp_command(chip, count);
        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)
 {
-       unsigned long flags;
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        int result = 0;
        
-       spin_lock_irqsave(&chip->mixer_lock, flags);
-       if (cmd == SNDRV_PCM_TRIGGER_START) {
+       spin_lock(&chip->mixer_lock);
+       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) {
+               break;
+       case SNDRV_PCM_TRIGGER_STOP:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
                chip->mode &= ~SB_RATE_LOCK_CAPTURE;
                snd_sbmixer_write(chip, 0xde, 0);
-       } else {
+               break;
+       default:
                result = -EINVAL;
+               break;
        }
-       spin_unlock_irqrestore(&chip->mixer_lock, flags);
+       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)
 {
-       unsigned long flags;
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        int result = 0;
 
-       spin_lock_irqsave(&chip->reg_lock, flags);
-       if (cmd == SNDRV_PCM_TRIGGER_START) {
+       spin_lock(&chip->reg_lock);
+       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_irqrestore(&chip->reg_lock, flags);
+       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)
 {
-       unsigned long flags;
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        unsigned int result;
 
-       spin_lock_irqsave(&chip->reg_lock, flags);      
+       spin_lock(&chip->reg_lock);     
        result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock(&chip->reg_lock);
        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)
 {
-       unsigned long flags;
-       sb_t *chip = snd_pcm_substream_chip(substream);
+       struct snd_sb *chip = snd_pcm_substream_chip(substream);
        unsigned result;
 
-       spin_lock_irqsave(&chip->reg_lock, flags);      
+       spin_lock(&chip->reg_lock);     
        result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock(&chip->reg_lock);
        return bytes_to_frames( substream->runtime, result );
 }
 
-static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
-{
-       sb_t *chip = snd_magic_cast(sb_t, dev_id, return IRQ_NONE);
-       unsigned long flags;
+/* 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 + 0x0e). 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.
+ * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
+ * */
+static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
+{
+       struct snd_sb *chip = dev_id;
        unsigned gcr_status;
        unsigned sb_status;
 
@@ -381,13 +399,13 @@ static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *
        if ((gcr_status & 0x40) && (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, regs);
+               snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
        /* release the gcr */
        outb(gcr_status, chip->alt_port + 0xe);
        
-       spin_lock_irqsave(&chip->mixer_lock, flags);
+       spin_lock(&chip->mixer_lock);
        sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
-       spin_unlock_irqrestore(&chip->mixer_lock, flags);
+       spin_unlock(&chip->mixer_lock);
        
        if (sb_status & SB_IRQTYPE_8BIT) 
                snd_sb_ack_8bit(chip);
@@ -402,7 +420,7 @@ static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *
 
 /*****************************************************************/
 
-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),
@@ -421,7 +439,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),
@@ -442,38 +460,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);
@@ -482,7 +500,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,
@@ -493,7 +511,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,
@@ -504,21 +522,13 @@ 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 = snd_magic_cast(sb_t, pcm->private_data, return);
-       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)
                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);
@@ -555,63 +565,117 @@ static void snd_als4000_set_addr(unsigned long gcr,
        snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
 }
 
-static void __devinit snd_als4000_configure(sb_t *chip)
+static void snd_als4000_configure(struct snd_sb *chip)
 {
-       unsigned long flags;
        unsigned tmp;
        int i;
 
        /* do some more configuration */
-       spin_lock_irqsave(&chip->mixer_lock, flags);
+       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 */
        snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
        snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
-       spin_unlock_irqrestore(&chip->mixer_lock, flags);
+       spin_unlock_irq(&chip->mixer_lock);
        
-       spin_lock_irqsave(&chip->reg_lock,flags);
+       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);
        
        snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
-       spin_unlock_irqrestore(&chip->reg_lock,flags);
+       spin_unlock_irq(&chip->reg_lock);
 }
 
-static void snd_card_als4000_free( snd_card_t *card )
-{
-       snd_card_als4000_t * acard = (snd_card_als4000_t *)card->private_data;
-       /* make sure that interrupts are disabled */
-       snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
-       /* free resources */
 #ifdef SUPPORT_JOYSTICK
-       if (acard->res_joystick) {
-               if (acard->gameport.io)
-                       gameport_unregister_port(&acard->gameport);
+static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
+{
+       struct gameport *gp;
+       struct resource *r;
+       int io_port;
+
+       if (joystick_port[dev] == 0)
+               return -ENODEV;
+
+       if (joystick_port[dev] == 1) { /* auto-detect */
+               for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
+                       r = request_region(io_port, 8, "ALS4000 gameport");
+                       if (r)
+                               break;
+               }
+       } else {
+               io_port = joystick_port[dev];
+               r = request_region(io_port, 8, "ALS4000 gameport");
+       }
+
+       if (!r) {
+               printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
+               return -EBUSY;
+       }
+
+       acard->gameport = gp = gameport_allocate_port();
+       if (!gp) {
+               printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
+               release_and_free_resource(r);
+               return -ENOMEM;
+       }
+
+       gameport_set_name(gp, "ALS4000 Gameport");
+       gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
+       gameport_set_dev_parent(gp, &acard->pci->dev);
+       gp->io = io_port;
+       gameport_set_port_data(gp, r);
+
+       /* Enable legacy joystick port */
+       snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
+
+       gameport_register_port(acard->gameport);
+
+       return 0;
+}
+
+static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
+{
+       if (acard->gameport) {
+               struct resource *r = gameport_get_port_data(acard->gameport);
+
+               gameport_unregister_port(acard->gameport);
+               acard->gameport = NULL;
+
                snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
-               release_resource(acard->res_joystick);
-               kfree_nocheck(acard->res_joystick);
+               release_and_free_resource(r);
        }
+}
+#else
+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
-       release_resource(acard->res_gcr);
-       kfree_nocheck(acard->res_gcr);
+
+static void snd_card_als4000_free( struct snd_card *card )
+{
+       struct snd_card_als4000 * acard = (struct snd_card_als4000 *)card->private_data;
+
+       /* make sure that interrupts are disabled */
+       snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
+       /* free resources */
+       snd_als4000_free_gameport(acard);
+       pci_release_regions(acard->pci);
+       pci_disable_device(acard->pci);
 }
 
 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;
+       struct snd_card *card;
+       struct snd_card_als4000 *acard;
        unsigned long gcr;
-       struct resource *res_gcr_port;
-       sb_t *chip;
-       opl3_t *opl3;
+       struct snd_sb *chip;
+       struct snd_opl3 *opl3;
        unsigned short word;
        int err;
-       int joystick = 0;
 
        if (dev >= SNDRV_CARDS)
                return -ENODEV;
@@ -625,55 +689,39 @@ 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) {
-               snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
+       if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
+           pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
+               snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
+               pci_disable_device(pci);
                return -ENXIO;
        }
 
-       gcr = pci_resource_start(pci, 0);
-       if ((res_gcr_port = request_region(gcr, 0x40, "ALS4000")) == NULL) {
-               snd_printk("unable to grab region 0x%lx-0x%lx\n", gcr, gcr + 0x40 - 1);
-               return -EBUSY;
+       if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
+               pci_disable_device(pci);
+               return err;
        }
+       gcr = 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 ) );
+                           sizeof( struct snd_card_als4000 ) );
        if (card == NULL) {
-               release_resource(res_gcr_port);
-               kfree_nocheck(res_gcr_port);
+               pci_release_regions(pci);
+               pci_disable_device(pci);
                return -ENOMEM;
        }
 
-       acard = (snd_card_als4000_t *)card->private_data;
+       acard = (struct snd_card_als4000 *)card->private_data;
+       acard->pci = pci;
        acard->gcr = gcr;
-       acard->res_gcr = res_gcr_port;
        card->private_free = snd_card_als4000_free;
 
-       /* disable all legacy ISA stuff except for joystick */
-#ifdef SUPPORT_JOYSTICK
-       if (joystick_port[dev] == 1) {
-               /* auto-detect */
-               long p;
-               for (p = 0x200; p <= 0x218; p += 8) {
-                       if ((acard->res_joystick = request_region(p, 8, "ALS4000 gameport")) != NULL) {
-                               joystick_port[dev] = p;
-                               break;
-                       }
-               }
-       } else if (joystick_port[dev] > 0)
-               acard->res_joystick = request_region(joystick_port[dev], 8, "ALS4000 gameport");
-       if (acard->res_joystick)
-               joystick = joystick_port[dev];
-       else
-               joystick = 0;
-#endif
-       snd_als4000_set_addr(gcr, 0, 0, 0, joystick);
-       
+       /* disable all legacy ISA stuff */
+       snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
+
        if ((err = snd_sbdsp_create(card,
                                    gcr + 0x10,
                                    pci->irq,
@@ -682,9 +730,9 @@ static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
                                    -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;
@@ -698,47 +746,44 @@ 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;
+                                       gcr+0x30, MPU401_INFO_INTEGRATED,
+                                       pci->irq, 0, &chip->rmidi)) < 0) {
+               printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n", gcr+0x30);
+               goto out_err;
        }
 
        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,
                            OPL3_HW_AUTO, 1, &opl3) < 0) {
-               printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx ?\n",
+               printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
                           gcr+0x10, gcr+0x12 );
        } else {
                if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
-                       snd_card_free(card);
-                       return err;
+                       goto out_err;
                }
        }
 
-#ifdef SUPPORT_JOYSTICK
-       if (acard->res_joystick) {
-               acard->gameport.io = joystick;
-               gameport_register_port(&acard->gameport);
-       }
-#endif
+       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)
@@ -747,16 +792,69 @@ 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->gcr, 0, 0, 0, 1);
+#endif
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       return 0;
+}
+#endif
+
+
 static struct pci_driver driver = {
        .name = "ALS4000",
        .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)
 {
-       return pci_module_init(&driver);
+       return pci_register_driver(&driver);
 }
 
 static void __exit alsa_card_als4000_exit(void)