Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / sound / pci / fm801.c
index b4c16b3..d72fc28 100644 (file)
 #include <linux/interrupt.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
+#include <linux/moduleparam.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/ac97_codec.h>
 #include <sound/mpu401.h>
 #include <sound/opl3.h>
-#define SNDRV_GET_ID
 #include <sound/initval.h>
 
 #include <asm/io.h>
 #define TEA575X_RADIO 1
 #endif
 
-#define chip_t fm801_t
-
 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
 MODULE_DESCRIPTION("ForteMedia FM801");
 MODULE_LICENSE("GPL");
-MODULE_CLASSES("{sound}");
-MODULE_DEVICES("{{ForteMedia,FM801},"
+MODULE_SUPPORTED_DEVICE("{{ForteMedia,FM801},"
                "{Genius,SoundMaker Live 5.1}}");
 
 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;     /* Index 0-MAX */
@@ -61,18 +58,14 @@ static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;  /* Enable this card *
  */
 static int tea575x_tuner[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 0 };
 
-MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(index, int, NULL, 0444);
 MODULE_PARM_DESC(index, "Index value for the FM801 soundcard.");
-MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
-MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
+module_param_array(id, charp, NULL, 0444);
 MODULE_PARM_DESC(id, "ID string for the FM801 soundcard.");
-MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
-MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(enable, bool, NULL, 0444);
 MODULE_PARM_DESC(enable, "Enable FM801 soundcard.");
-MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
-MODULE_PARM(tea575x_tuner, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(tea575x_tuner, int, NULL, 0444);
 MODULE_PARM_DESC(tea575x_tuner, "Enable TEA575x tuner.");
-MODULE_PARM_SYNTAX(tea575x_tuner, SNDRV_ENABLE_DESC);
 
 /*
  *  Direct registers
@@ -110,7 +103,11 @@ MODULE_PARM_SYNTAX(tea575x_tuner, SNDRV_ENABLE_DESC);
 #define FM801_OPL3_DATA1       0x6b    /* OPL3 Bank 1 Write */
 #define FM801_POWERDOWN                0x70    /* Blocks Power Down Control */
 
-#define FM801_AC97_ADDR_SHIFT  10
+/* codec access */
+#define FM801_AC97_READ                (1<<7)  /* read=1, write=0 */
+#define FM801_AC97_VALID       (1<<8)  /* port valid=1 */
+#define FM801_AC97_BUSY                (1<<9)  /* busy=1 */
+#define FM801_AC97_ADDR_SHIFT  10      /* codec id (2bit) */
 
 /* playback and record control register bits */
 #define FM801_BUF1_LAST                (1<<1)
@@ -154,13 +151,10 @@ MODULE_PARM_SYNTAX(tea575x_tuner, SNDRV_ENABLE_DESC);
 
  */
 
-typedef struct _snd_fm801 fm801_t;
-
-struct _snd_fm801 {
+struct fm801 {
        int irq;
 
        unsigned long port;     /* I/O port number */
-       struct resource *res_port;
        unsigned int multichannel: 1,   /* multichannel support */
                     secondary: 1;      /* secondary codec */
        unsigned char secondary_addr;   /* address of the secondary codec */
@@ -180,29 +174,34 @@ struct _snd_fm801 {
        unsigned int cap_size;
        unsigned int cap_pos;
 
-       ac97_bus_t *ac97_bus;
-       ac97_t *ac97;
-       ac97_t *ac97_sec;
+       struct snd_ac97_bus *ac97_bus;
+       struct snd_ac97 *ac97;
+       struct snd_ac97 *ac97_sec;
 
        struct pci_dev *pci;
-       snd_card_t *card;
-       snd_pcm_t *pcm;
-       snd_rawmidi_t *rmidi;
-       snd_pcm_substream_t *playback_substream;
-       snd_pcm_substream_t *capture_substream;
+       struct snd_card *card;
+       struct snd_pcm *pcm;
+       struct snd_rawmidi *rmidi;
+       struct snd_pcm_substream *playback_substream;
+       struct snd_pcm_substream *capture_substream;
        unsigned int p_dma_size;
        unsigned int c_dma_size;
 
        spinlock_t reg_lock;
-       snd_info_entry_t *proc_entry;
+       struct snd_info_entry *proc_entry;
 
 #ifdef TEA575X_RADIO
-       tea575x_t tea;
+       struct snd_tea575x tea;
+#endif
+
+#ifdef CONFIG_PM
+       u16 saved_regs[0x20];
 #endif
 };
 
-static struct pci_device_id snd_fm801_ids[] = {
+static struct pci_device_id snd_fm801_ids[] __devinitdata = {
        { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* FM801 */
+       { 0x5213, 0x0510, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, 0, },   /* Gallant Odyssey Sound 4 */
        { 0, }
 };
 
@@ -212,38 +211,39 @@ MODULE_DEVICE_TABLE(pci, snd_fm801_ids);
  *  common I/O routines
  */
 
-static int snd_fm801_update_bits(fm801_t *chip, unsigned short reg,
+static int snd_fm801_update_bits(struct fm801 *chip, unsigned short reg,
                                 unsigned short mask, unsigned short value)
 {
        int change;
+       unsigned long flags;
        unsigned short old, new;
 
-       spin_lock(&chip->reg_lock);
+       spin_lock_irqsave(&chip->reg_lock, flags);
        old = inw(chip->port + reg);
        new = (old & ~mask) | value;
        change = old != new;
        if (change)
                outw(new, chip->port + reg);
-       spin_unlock(&chip->reg_lock);
+       spin_unlock_irqrestore(&chip->reg_lock, flags);
        return change;
 }
 
-static void snd_fm801_codec_write(ac97_t *ac97,
+static void snd_fm801_codec_write(struct snd_ac97 *ac97,
                                  unsigned short reg,
                                  unsigned short val)
 {
-       fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
+       struct fm801 *chip = ac97->private_data;
        int idx;
 
        /*
         *  Wait until the codec interface is not ready..
         */
        for (idx = 0; idx < 100; idx++) {
-               if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
+               if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
                        goto ok1;
                udelay(10);
        }
-       snd_printk("AC'97 interface is busy (1)\n");
+       snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
        return;
 
  ok1:
@@ -254,47 +254,48 @@ static void snd_fm801_codec_write(ac97_t *ac97,
         *  Wait until the write command is not completed..
          */
        for (idx = 0; idx < 1000; idx++) {
-               if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
+               if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
                        return;
                udelay(10);
        }
-       snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
+       snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
 }
 
-static unsigned short snd_fm801_codec_read(ac97_t *ac97, unsigned short reg)
+static unsigned short snd_fm801_codec_read(struct snd_ac97 *ac97, unsigned short reg)
 {
-       fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return -ENXIO);
+       struct fm801 *chip = ac97->private_data;
        int idx;
 
        /*
         *  Wait until the codec interface is not ready..
         */
        for (idx = 0; idx < 100; idx++) {
-               if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
+               if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
                        goto ok1;
                udelay(10);
        }
-       snd_printk("AC'97 interface is busy (1)\n");
+       snd_printk(KERN_ERR "AC'97 interface is busy (1)\n");
        return 0;
 
  ok1:
        /* read command */
-       outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | (1<<7), FM801_REG(chip, AC97_CMD));
+       outw(reg | (ac97->addr << FM801_AC97_ADDR_SHIFT) | FM801_AC97_READ,
+            FM801_REG(chip, AC97_CMD));
        for (idx = 0; idx < 100; idx++) {
-               if (!(inw(FM801_REG(chip, AC97_CMD)) & (1<<9)))
+               if (!(inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_BUSY))
                        goto ok2;
                udelay(10);
        }
-       snd_printk("AC'97 interface #%d is busy (2)\n", ac97->num);
+       snd_printk(KERN_ERR "AC'97 interface #%d is busy (2)\n", ac97->num);
        return 0;
 
  ok2:
        for (idx = 0; idx < 1000; idx++) {
-               if (inw(FM801_REG(chip, AC97_CMD)) & (1<<8))
+               if (inw(FM801_REG(chip, AC97_CMD)) & FM801_AC97_VALID)
                        goto ok3;
                udelay(10);
        }
-       snd_printk("AC'97 interface #%d is not valid (2)\n", ac97->num);
+       snd_printk(KERN_ERR "AC'97 interface #%d is not valid (2)\n", ac97->num);
        return 0;
 
  ok3:
@@ -307,10 +308,8 @@ static unsigned int rates[] = {
   38400, 44100, 48000
 };
 
-#define RATES sizeof(rates) / sizeof(rates[0])
-
-static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
-       .count = RATES,
+static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
+       .count = ARRAY_SIZE(rates),
        .list = rates,
        .mask = 0,
 };
@@ -321,7 +320,7 @@ static unsigned int channels[] = {
 
 #define CHANNELS sizeof(channels) / sizeof(channels[0])
 
-static snd_pcm_hw_constraint_list_t hw_constraints_channels = {
+static struct snd_pcm_hw_constraint_list hw_constraints_channels = {
        .count = CHANNELS,
        .list = channels,
        .mask = 0,
@@ -335,21 +334,21 @@ static unsigned short snd_fm801_rate_bits(unsigned int rate)
 {
        unsigned int idx;
 
-       for (idx = 0; idx < 11; idx++)
+       for (idx = 0; idx < ARRAY_SIZE(rates); idx++)
                if (rates[idx] == rate)
                        return idx;
        snd_BUG();
-       return RATES - 1;
+       return ARRAY_SIZE(rates) - 1;
 }
 
 /*
  *  PCM part
  */
 
-static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
+static int snd_fm801_playback_trigger(struct snd_pcm_substream *substream,
                                      int cmd)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
 
        spin_lock(&chip->reg_lock);
        switch (cmd) {
@@ -364,9 +363,11 @@ static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
                chip->ply_ctrl &= ~(FM801_START | FM801_PAUSE);
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
                chip->ply_ctrl |= FM801_PAUSE;
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+       case SNDRV_PCM_TRIGGER_RESUME:
                chip->ply_ctrl &= ~FM801_PAUSE;
                break;
        default:
@@ -379,10 +380,10 @@ static int snd_fm801_playback_trigger(snd_pcm_substream_t * substream,
        return 0;
 }
 
-static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
+static int snd_fm801_capture_trigger(struct snd_pcm_substream *substream,
                                     int cmd)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
 
        spin_lock(&chip->reg_lock);
        switch (cmd) {
@@ -397,9 +398,11 @@ static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
                chip->cap_ctrl &= ~(FM801_START | FM801_PAUSE);
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+       case SNDRV_PCM_TRIGGER_SUSPEND:
                chip->cap_ctrl |= FM801_PAUSE;
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+       case SNDRV_PCM_TRIGGER_RESUME:
                chip->cap_ctrl &= ~FM801_PAUSE;
                break;
        default:
@@ -412,26 +415,25 @@ static int snd_fm801_capture_trigger(snd_pcm_substream_t * substream,
        return 0;
 }
 
-static int snd_fm801_hw_params(snd_pcm_substream_t * substream,
-                              snd_pcm_hw_params_t * hw_params)
+static int snd_fm801_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_fm801_hw_free(snd_pcm_substream_t * substream)
+static int snd_fm801_hw_free(struct snd_pcm_substream *substream)
 {
        return snd_pcm_lib_free_pages(substream);
 }
 
-static int snd_fm801_playback_prepare(snd_pcm_substream_t * substream)
+static int snd_fm801_playback_prepare(struct snd_pcm_substream *substream)
 {
-       unsigned long flags;
-       fm801_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
 
        chip->ply_size = snd_pcm_lib_buffer_bytes(substream);
        chip->ply_count = snd_pcm_lib_period_bytes(substream);
-       spin_lock_irqsave(&chip->reg_lock, flags);
+       spin_lock_irq(&chip->reg_lock);
        chip->ply_ctrl &= ~(FM801_START | FM801_16BIT |
                             FM801_STEREO | FM801_RATE_MASK |
                             FM801_CHANNELS_MASK);
@@ -452,19 +454,18 @@ static int snd_fm801_playback_prepare(snd_pcm_substream_t * substream)
        chip->ply_pos = 0;
        outl(chip->ply_buffer, FM801_REG(chip, PLY_BUF1));
        outl(chip->ply_buffer + (chip->ply_count % chip->ply_size), FM801_REG(chip, PLY_BUF2));
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock_irq(&chip->reg_lock);
        return 0;
 }
 
-static int snd_fm801_capture_prepare(snd_pcm_substream_t * substream)
+static int snd_fm801_capture_prepare(struct snd_pcm_substream *substream)
 {
-       unsigned long flags;
-       fm801_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
 
        chip->cap_size = snd_pcm_lib_buffer_bytes(substream);
        chip->cap_count = snd_pcm_lib_period_bytes(substream);
-       spin_lock_irqsave(&chip->reg_lock, flags);
+       spin_lock_irq(&chip->reg_lock);
        chip->cap_ctrl &= ~(FM801_START | FM801_16BIT |
                             FM801_STEREO | FM801_RATE_MASK);
        if (snd_pcm_format_width(runtime->format) == 16)
@@ -479,49 +480,47 @@ static int snd_fm801_capture_prepare(snd_pcm_substream_t * substream)
        chip->cap_pos = 0;
        outl(chip->cap_buffer, FM801_REG(chip, CAP_BUF1));
        outl(chip->cap_buffer + (chip->cap_count % chip->cap_size), FM801_REG(chip, CAP_BUF2));
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock_irq(&chip->reg_lock);
        return 0;
 }
 
-static snd_pcm_uframes_t snd_fm801_playback_pointer(snd_pcm_substream_t * substream)
+static snd_pcm_uframes_t snd_fm801_playback_pointer(struct snd_pcm_substream *substream)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
-       unsigned long flags;
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
        size_t ptr;
 
        if (!(chip->ply_ctrl & FM801_START))
                return 0;
-       spin_lock_irqsave(&chip->reg_lock, flags);
+       spin_lock(&chip->reg_lock);
        ptr = chip->ply_pos + (chip->ply_count - 1) - inw(FM801_REG(chip, PLY_COUNT));
        if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_PLAYBACK) {
                ptr += chip->ply_count;
                ptr %= chip->ply_size;
        }
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock(&chip->reg_lock);
        return bytes_to_frames(substream->runtime, ptr);
 }
 
-static snd_pcm_uframes_t snd_fm801_capture_pointer(snd_pcm_substream_t * substream)
+static snd_pcm_uframes_t snd_fm801_capture_pointer(struct snd_pcm_substream *substream)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
-       unsigned long flags;
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
        size_t ptr;
 
        if (!(chip->cap_ctrl & FM801_START))
                return 0;
-       spin_lock_irqsave(&chip->reg_lock, flags);
+       spin_lock(&chip->reg_lock);
        ptr = chip->cap_pos + (chip->cap_count - 1) - inw(FM801_REG(chip, CAP_COUNT));
        if (inw(FM801_REG(chip, IRQ_STATUS)) & FM801_IRQ_CAPTURE) {
                ptr += chip->cap_count;
                ptr %= chip->cap_size;
        }
-       spin_unlock_irqrestore(&chip->reg_lock, flags);
+       spin_unlock(&chip->reg_lock);
        return bytes_to_frames(substream->runtime, ptr);
 }
 
 static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 {
-       fm801_t *chip = snd_magic_cast(fm801_t, dev_id, return IRQ_NONE);
+       struct fm801 *chip = dev_id;
        unsigned short status;
        unsigned int tmp;
 
@@ -567,11 +566,11 @@ static irqreturn_t snd_fm801_interrupt(int irq, void *dev_id, struct pt_regs *re
        return IRQ_HANDLED;
 }
 
-static snd_pcm_hardware_t snd_fm801_playback =
+static struct snd_pcm_hardware snd_fm801_playback =
 {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
-                                SNDRV_PCM_INFO_PAUSE |
+                                SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
                                 SNDRV_PCM_INFO_MMAP_VALID),
        .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
        .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
@@ -587,11 +586,11 @@ static snd_pcm_hardware_t snd_fm801_playback =
        .fifo_size =            0,
 };
 
-static snd_pcm_hardware_t snd_fm801_capture =
+static struct snd_pcm_hardware snd_fm801_capture =
 {
        .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
                                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
-                                SNDRV_PCM_INFO_PAUSE |
+                                SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME |
                                 SNDRV_PCM_INFO_MMAP_VALID),
        .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
        .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
@@ -607,55 +606,59 @@ static snd_pcm_hardware_t snd_fm801_capture =
        .fifo_size =            0,
 };
 
-static int snd_fm801_playback_open(snd_pcm_substream_t * substream)
+static int snd_fm801_playback_open(struct snd_pcm_substream *substream)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
        int err;
 
        chip->playback_substream = substream;
        runtime->hw = snd_fm801_playback;
-       snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
+       snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+                                  &hw_constraints_rates);
        if (chip->multichannel) {
                runtime->hw.channels_max = 6;
-               snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels);
+               snd_pcm_hw_constraint_list(runtime, 0,
+                                          SNDRV_PCM_HW_PARAM_CHANNELS,
+                                          &hw_constraints_channels);
        }
        if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
                return err;
        return 0;
 }
 
-static int snd_fm801_capture_open(snd_pcm_substream_t * substream)
+static int snd_fm801_capture_open(struct snd_pcm_substream *substream)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
-       snd_pcm_runtime_t *runtime = substream->runtime;
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
+       struct snd_pcm_runtime *runtime = substream->runtime;
        int err;
 
        chip->capture_substream = substream;
        runtime->hw = snd_fm801_capture;
-       snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
+       snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
+                                  &hw_constraints_rates);
        if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
                return err;
        return 0;
 }
 
-static int snd_fm801_playback_close(snd_pcm_substream_t * substream)
+static int snd_fm801_playback_close(struct snd_pcm_substream *substream)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
 
        chip->playback_substream = NULL;
        return 0;
 }
 
-static int snd_fm801_capture_close(snd_pcm_substream_t * substream)
+static int snd_fm801_capture_close(struct snd_pcm_substream *substream)
 {
-       fm801_t *chip = snd_pcm_substream_chip(substream);
+       struct fm801 *chip = snd_pcm_substream_chip(substream);
 
        chip->capture_substream = NULL;
        return 0;
 }
 
-static snd_pcm_ops_t snd_fm801_playback_ops = {
+static struct snd_pcm_ops snd_fm801_playback_ops = {
        .open =         snd_fm801_playback_open,
        .close =        snd_fm801_playback_close,
        .ioctl =        snd_pcm_lib_ioctl,
@@ -666,7 +669,7 @@ static snd_pcm_ops_t snd_fm801_playback_ops = {
        .pointer =      snd_fm801_playback_pointer,
 };
 
-static snd_pcm_ops_t snd_fm801_capture_ops = {
+static struct snd_pcm_ops snd_fm801_capture_ops = {
        .open =         snd_fm801_capture_open,
        .close =        snd_fm801_capture_close,
        .ioctl =        snd_pcm_lib_ioctl,
@@ -677,16 +680,9 @@ static snd_pcm_ops_t snd_fm801_capture_ops = {
        .pointer =      snd_fm801_capture_pointer,
 };
 
-static void snd_fm801_pcm_free(snd_pcm_t *pcm)
-{
-       fm801_t *chip = snd_magic_cast(fm801_t, pcm->private_data, return);
-       chip->pcm = NULL;
-       snd_pcm_lib_preallocate_free_for_all(pcm);
-}
-
-static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
+static int __devinit snd_fm801_pcm(struct fm801 *chip, int device, struct snd_pcm ** rpcm)
 {
-       snd_pcm_t *pcm;
+       struct snd_pcm *pcm;
        int err;
 
        if (rpcm)
@@ -698,7 +694,6 @@ static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_fm801_capture_ops);
 
        pcm->private_data = chip;
-       pcm->private_free = snd_fm801_pcm_free;
        pcm->info_flags = 0;
        strcpy(pcm->name, "FM801");
        chip->pcm = pcm;
@@ -723,9 +718,9 @@ static int __devinit snd_fm801_pcm(fm801_t *chip, int device, snd_pcm_t ** rpcm)
 #define TEA_256PCS_WRITE_ENABLE                2       /* inverted */
 #define TEA_256PCS_BUS_CLOCK           3
 
-static void snd_fm801_tea575x_256pcs_write(tea575x_t *tea, unsigned int val)
+static void snd_fm801_tea575x_256pcs_write(struct snd_tea575x *tea, unsigned int val)
 {
-       fm801_t *chip = tea->private_data;
+       struct fm801 *chip = tea->private_data;
        unsigned short reg;
        int i = 25;
 
@@ -767,9 +762,9 @@ static void snd_fm801_tea575x_256pcs_write(tea575x_t *tea, unsigned int val)
        spin_unlock_irq(&chip->reg_lock);
 }
 
-static unsigned int snd_fm801_tea575x_256pcs_read(tea575x_t *tea)
+static unsigned int snd_fm801_tea575x_256pcs_read(struct snd_tea575x *tea)
 {
-       fm801_t *chip = tea->private_data;
+       struct fm801 *chip = tea->private_data;
        unsigned short reg;
        unsigned int val = 0;
        int i;
@@ -811,9 +806,9 @@ static unsigned int snd_fm801_tea575x_256pcs_read(tea575x_t *tea)
 #define TEA_256PCPR_DATA               1
 #define TEA_256PCPR_WRITE_ENABLE       2       /* inverted */
 
-static void snd_fm801_tea575x_256pcpr_write(tea575x_t *tea, unsigned int val)
+static void snd_fm801_tea575x_256pcpr_write(struct snd_tea575x *tea, unsigned int val)
 {
-       fm801_t *chip = tea->private_data;
+       struct fm801 *chip = tea->private_data;
        unsigned short reg;
        int i = 25;
 
@@ -855,9 +850,9 @@ static void snd_fm801_tea575x_256pcpr_write(tea575x_t *tea, unsigned int val)
        spin_unlock_irq(&chip->reg_lock);
 }
 
-static unsigned int snd_fm801_tea575x_256pcpr_read(tea575x_t *tea)
+static unsigned int snd_fm801_tea575x_256pcpr_read(struct snd_tea575x *tea)
 {
-       fm801_t *chip = tea->private_data;
+       struct fm801 *chip = tea->private_data;
        unsigned short reg;
        unsigned int val = 0;
        int i;
@@ -899,9 +894,9 @@ static unsigned int snd_fm801_tea575x_256pcpr_read(tea575x_t *tea)
 #define TEA_64PCR_WRITE_ENABLE         1       /* inverted */
 #define TEA_64PCR_DATA                 2
 
-static void snd_fm801_tea575x_64pcr_write(tea575x_t *tea, unsigned int val)
+static void snd_fm801_tea575x_64pcr_write(struct snd_tea575x *tea, unsigned int val)
 {
-       fm801_t *chip = tea->private_data;
+       struct fm801 *chip = tea->private_data;
        unsigned short reg;
        int i = 25;
 
@@ -943,9 +938,9 @@ static void snd_fm801_tea575x_64pcr_write(tea575x_t *tea, unsigned int val)
        spin_unlock_irq(&chip->reg_lock);
 }
 
-static unsigned int snd_fm801_tea575x_64pcr_read(tea575x_t *tea)
+static unsigned int snd_fm801_tea575x_64pcr_read(struct snd_tea575x *tea)
 {
-       fm801_t *chip = tea->private_data;
+       struct fm801 *chip = tea->private_data;
        unsigned short reg;
        unsigned int val = 0;
        int i;
@@ -1010,7 +1005,8 @@ static struct snd_tea575x_ops snd_fm801_tea_ops[3] = {
   .get = snd_fm801_get_single, .put = snd_fm801_put_single, \
   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
 
-static int snd_fm801_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_fm801_info_single(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_info *uinfo)
 {
        int mask = (kcontrol->private_value >> 16) & 0xff;
 
@@ -1021,9 +1017,10 @@ static int snd_fm801_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *
        return 0;
 }
 
-static int snd_fm801_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_fm801_get_single(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_value *ucontrol)
 {
-       fm801_t *chip = snd_kcontrol_chip(kcontrol);
+       struct fm801 *chip = snd_kcontrol_chip(kcontrol);
        int reg = kcontrol->private_value & 0xff;
        int shift = (kcontrol->private_value >> 8) & 0xff;
        int mask = (kcontrol->private_value >> 16) & 0xff;
@@ -1035,9 +1032,10 @@ static int snd_fm801_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t
        return 0;
 }
 
-static int snd_fm801_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_fm801_put_single(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_value *ucontrol)
 {
-       fm801_t *chip = snd_kcontrol_chip(kcontrol);
+       struct fm801 *chip = snd_kcontrol_chip(kcontrol);
        int reg = kcontrol->private_value & 0xff;
        int shift = (kcontrol->private_value >> 8) & 0xff;
        int mask = (kcontrol->private_value >> 16) & 0xff;
@@ -1055,7 +1053,8 @@ static int snd_fm801_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t
   .get = snd_fm801_get_double, .put = snd_fm801_put_double, \
   .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
 
-static int snd_fm801_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_fm801_info_double(struct snd_kcontrol *kcontrol,
+                                struct snd_ctl_elem_info *uinfo)
 {
        int mask = (kcontrol->private_value >> 16) & 0xff;
 
@@ -1066,19 +1065,20 @@ static int snd_fm801_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *
        return 0;
 }
 
-static int snd_fm801_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_fm801_get_double(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_value *ucontrol)
 {
-       fm801_t *chip = snd_kcontrol_chip(kcontrol);
+       struct fm801 *chip = snd_kcontrol_chip(kcontrol);
         int reg = kcontrol->private_value & 0xff;
        int shift_left = (kcontrol->private_value >> 8) & 0x0f;
        int shift_right = (kcontrol->private_value >> 12) & 0x0f;
        int mask = (kcontrol->private_value >> 16) & 0xff;
        int invert = (kcontrol->private_value >> 24) & 0xff;
 
-       spin_lock(&chip->reg_lock);
+       spin_lock_irq(&chip->reg_lock);
        ucontrol->value.integer.value[0] = (inw(chip->port + reg) >> shift_left) & mask;
        ucontrol->value.integer.value[1] = (inw(chip->port + reg) >> shift_right) & mask;
-       spin_unlock(&chip->reg_lock);
+       spin_unlock_irq(&chip->reg_lock);
        if (invert) {
                ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
                ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
@@ -1086,9 +1086,10 @@ static int snd_fm801_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t
        return 0;
 }
 
-static int snd_fm801_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_fm801_put_double(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_value *ucontrol)
 {
-       fm801_t *chip = snd_kcontrol_chip(kcontrol);
+       struct fm801 *chip = snd_kcontrol_chip(kcontrol);
        int reg = kcontrol->private_value & 0xff;
        int shift_left = (kcontrol->private_value >> 8) & 0x0f;
        int shift_right = (kcontrol->private_value >> 12) & 0x0f;
@@ -1107,7 +1108,8 @@ static int snd_fm801_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t
                                     (val1 << shift_left ) | (val2 << shift_right));
 }
 
-static int snd_fm801_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
+static int snd_fm801_info_mux(struct snd_kcontrol *kcontrol,
+                             struct snd_ctl_elem_info *uinfo)
 {
        static char *texts[5] = {
                "AC97 Primary", "FM", "I2S", "PCM", "AC97 Secondary"
@@ -1122,9 +1124,10 @@ static int snd_fm801_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * ui
        return 0;
 }
 
-static int snd_fm801_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_fm801_get_mux(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
 {
-       fm801_t *chip = snd_kcontrol_chip(kcontrol);
+       struct fm801 *chip = snd_kcontrol_chip(kcontrol);
         unsigned short val;
  
        val = inw(FM801_REG(chip, REC_SRC)) & 7;
@@ -1134,9 +1137,10 @@ static int snd_fm801_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * u
         return 0;
 }
 
-static int snd_fm801_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
+static int snd_fm801_put_mux(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
 {
-       fm801_t *chip = snd_kcontrol_chip(kcontrol);
+       struct fm801 *chip = snd_kcontrol_chip(kcontrol);
         unsigned short val;
  
         if ((val = ucontrol->value.enumerated.item[0]) > 4)
@@ -1144,9 +1148,9 @@ static int snd_fm801_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * u
        return snd_fm801_update_bits(chip, FM801_REC_SRC, 7, val);
 }
 
-#define FM801_CONTROLS (sizeof(snd_fm801_controls)/sizeof(snd_kcontrol_new_t))
+#define FM801_CONTROLS ARRAY_SIZE(snd_fm801_controls)
 
-static snd_kcontrol_new_t snd_fm801_controls[] __devinitdata = {
+static struct snd_kcontrol_new snd_fm801_controls[] __devinitdata = {
 FM801_DOUBLE("Wave Playback Volume", FM801_PCM_VOL, 0, 8, 31, 1),
 FM801_SINGLE("Wave Playback Switch", FM801_PCM_VOL, 15, 1, 1),
 FM801_DOUBLE("I2S Playback Volume", FM801_I2S_VOL, 0, 8, 31, 1),
@@ -1162,26 +1166,26 @@ FM801_SINGLE("FM Playback Switch", FM801_FM_VOL, 15, 1, 1),
 }
 };
 
-#define FM801_CONTROLS_MULTI (sizeof(snd_fm801_controls_multi)/sizeof(snd_kcontrol_new_t))
+#define FM801_CONTROLS_MULTI ARRAY_SIZE(snd_fm801_controls_multi)
 
-static snd_kcontrol_new_t snd_fm801_controls_multi[] __devinitdata = {
+static struct snd_kcontrol_new snd_fm801_controls_multi[] __devinitdata = {
 FM801_SINGLE("AC97 2ch->4ch Copy Switch", FM801_CODEC_CTRL, 7, 1, 0),
 FM801_SINGLE("AC97 18-bit Switch", FM801_CODEC_CTRL, 10, 1, 0),
-FM801_SINGLE("IEC958 Capture Switch", FM801_I2S_MODE, 8, 1, 0),
-FM801_SINGLE("IEC958 Raw Data Playback Switch", FM801_I2S_MODE, 9, 1, 0),
-FM801_SINGLE("IEC958 Raw Data Capture Switch", FM801_I2S_MODE, 10, 1, 0),
-FM801_SINGLE("IEC958 Playback Switch", FM801_GEN_CTRL, 2, 1, 0),
+FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), FM801_I2S_MODE, 8, 1, 0),
+FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",PLAYBACK,SWITCH), FM801_I2S_MODE, 9, 1, 0),
+FM801_SINGLE(SNDRV_CTL_NAME_IEC958("Raw Data ",CAPTURE,SWITCH), FM801_I2S_MODE, 10, 1, 0),
+FM801_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), FM801_GEN_CTRL, 2, 1, 0),
 };
 
-static void snd_fm801_mixer_free_ac97_bus(ac97_bus_t *bus)
+static void snd_fm801_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
 {
-       fm801_t *chip = snd_magic_cast(fm801_t, bus->private_data, return);
+       struct fm801 *chip = bus->private_data;
        chip->ac97_bus = NULL;
 }
 
-static void snd_fm801_mixer_free_ac97(ac97_t *ac97)
+static void snd_fm801_mixer_free_ac97(struct snd_ac97 *ac97)
 {
-       fm801_t *chip = snd_magic_cast(fm801_t, ac97->private_data, return);
+       struct fm801 *chip = ac97->private_data;
        if (ac97->num == 0) {
                chip->ac97 = NULL;
        } else {
@@ -1189,20 +1193,19 @@ static void snd_fm801_mixer_free_ac97(ac97_t *ac97)
        }
 }
 
-static int __devinit snd_fm801_mixer(fm801_t *chip)
+static int __devinit snd_fm801_mixer(struct fm801 *chip)
 {
-       ac97_bus_t bus;
-       ac97_t ac97;
+       struct snd_ac97_template ac97;
        unsigned int i;
        int err;
+       static struct snd_ac97_bus_ops ops = {
+               .write = snd_fm801_codec_write,
+               .read = snd_fm801_codec_read,
+       };
 
-       memset(&bus, 0, sizeof(bus));
-       bus.write = snd_fm801_codec_write;
-       bus.read = snd_fm801_codec_read;
-       bus.private_data = chip;
-       bus.private_free = snd_fm801_mixer_free_ac97_bus;
-       if ((err = snd_ac97_bus(chip->card, &bus, &chip->ac97_bus)) < 0)
+       if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
                return err;
+       chip->ac97_bus->private_free = snd_fm801_mixer_free_ac97_bus;
 
        memset(&ac97, 0, sizeof(ac97));
        ac97.private_data = chip;
@@ -1228,7 +1231,86 @@ static int __devinit snd_fm801_mixer(fm801_t *chip)
  *  initialization routines
  */
 
-static int snd_fm801_free(fm801_t *chip)
+static int wait_for_codec(struct fm801 *chip, unsigned int codec_id,
+                         unsigned short reg, unsigned long waits)
+{
+       unsigned long timeout = jiffies + waits;
+
+       outw(FM801_AC97_READ | (codec_id << FM801_AC97_ADDR_SHIFT) | reg,
+            FM801_REG(chip, AC97_CMD));
+       udelay(5);
+       do {
+               if ((inw(FM801_REG(chip, AC97_CMD)) & (FM801_AC97_VALID|FM801_AC97_BUSY))
+                   == FM801_AC97_VALID)
+                       return 0;
+               schedule_timeout_uninterruptible(1);
+       } while (time_after(timeout, jiffies));
+       return -EIO;
+}
+
+static int snd_fm801_chip_init(struct fm801 *chip, int resume)
+{
+       int id;
+       unsigned short cmdw;
+
+       /* codec cold reset + AC'97 warm reset */
+       outw((1<<5) | (1<<6), FM801_REG(chip, CODEC_CTRL));
+       inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
+       udelay(100);
+       outw(0, FM801_REG(chip, CODEC_CTRL));
+
+       if (wait_for_codec(chip, 0, AC97_RESET, msecs_to_jiffies(750)) < 0) {
+               snd_printk(KERN_ERR "Primary AC'97 codec not found\n");
+               if (! resume)
+                       return -EIO;
+       }
+
+       if (chip->multichannel) {
+               if (chip->secondary_addr) {
+                       wait_for_codec(chip, chip->secondary_addr,
+                                      AC97_VENDOR_ID1, msecs_to_jiffies(50));
+               } else {
+                       /* my card has the secondary codec */
+                       /* at address #3, so the loop is inverted */
+                       for (id = 3; id > 0; id--) {
+                               if (! wait_for_codec(chip, id, AC97_VENDOR_ID1,
+                                                    msecs_to_jiffies(50))) {
+                                       cmdw = inw(FM801_REG(chip, AC97_DATA));
+                                       if (cmdw != 0xffff && cmdw != 0) {
+                                               chip->secondary = 1;
+                                               chip->secondary_addr = id;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               /* the recovery phase, it seems that probing for non-existing codec might */
+               /* cause timeout problems */
+               wait_for_codec(chip, 0, AC97_VENDOR_ID1, msecs_to_jiffies(750));
+       }
+
+       /* init volume */
+       outw(0x0808, FM801_REG(chip, PCM_VOL));
+       outw(0x9f1f, FM801_REG(chip, FM_VOL));
+       outw(0x8808, FM801_REG(chip, I2S_VOL));
+
+       /* I2S control - I2S mode */
+       outw(0x0003, FM801_REG(chip, I2S_MODE));
+
+       /* interrupt setup - unmask MPU, PLAYBACK & CAPTURE */
+       cmdw = inw(FM801_REG(chip, IRQ_MASK));
+       cmdw &= ~0x0083;
+       outw(cmdw, FM801_REG(chip, IRQ_MASK));
+
+       /* interrupt clear */
+       outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
+
+       return 0;
+}
+
+
+static int snd_fm801_free(struct fm801 *chip)
 {
        unsigned short cmdw;
 
@@ -1244,55 +1326,54 @@ static int snd_fm801_free(fm801_t *chip)
 #ifdef TEA575X_RADIO
        snd_tea575x_exit(&chip->tea);
 #endif
-       if (chip->res_port) {
-               release_resource(chip->res_port);
-               kfree_nocheck(chip->res_port);
-       }
        if (chip->irq >= 0)
-               free_irq(chip->irq, (void *)chip);
+               free_irq(chip->irq, chip);
+       pci_release_regions(chip->pci);
+       pci_disable_device(chip->pci);
 
-       snd_magic_kfree(chip);
+       kfree(chip);
        return 0;
 }
 
-static int snd_fm801_dev_free(snd_device_t *device)
+static int snd_fm801_dev_free(struct snd_device *device)
 {
-       fm801_t *chip = snd_magic_cast(fm801_t, device->device_data, return -ENXIO);
+       struct fm801 *chip = device->device_data;
        return snd_fm801_free(chip);
 }
 
-static int __devinit snd_fm801_create(snd_card_t * card,
+static int __devinit snd_fm801_create(struct snd_card *card,
                                      struct pci_dev * pci,
                                      int tea575x_tuner,
-                                     fm801_t ** rchip)
+                                     struct fm801 ** rchip)
 {
-       fm801_t *chip;
-       unsigned char rev, id;
-       unsigned short cmdw;
-       unsigned long timeout;
+       struct fm801 *chip;
+       unsigned char rev;
        int err;
-       static snd_device_ops_t ops = {
+       static struct snd_device_ops ops = {
                .dev_free =     snd_fm801_dev_free,
        };
 
        *rchip = NULL;
        if ((err = pci_enable_device(pci)) < 0)
                return err;
-       chip = snd_magic_kcalloc(fm801_t, 0, GFP_KERNEL);
-       if (chip == NULL)
+       chip = kzalloc(sizeof(*chip), GFP_KERNEL);
+       if (chip == NULL) {
+               pci_disable_device(pci);
                return -ENOMEM;
+       }
        spin_lock_init(&chip->reg_lock);
        chip->card = card;
        chip->pci = pci;
        chip->irq = -1;
-       chip->port = pci_resource_start(pci, 0);
-       if ((chip->res_port = request_region(chip->port, 0x80, "FM801")) == NULL) {
-               snd_printk("unable to grab region 0x%lx-0x%lx\n", chip->port, chip->port + 0x80 - 1);
-               snd_fm801_free(chip);
-               return -EBUSY;
+       if ((err = pci_request_regions(pci, "FM801")) < 0) {
+               kfree(chip);
+               pci_disable_device(pci);
+               return err;
        }
-       if (request_irq(pci->irq, snd_fm801_interrupt, SA_INTERRUPT|SA_SHIRQ, "FM801", (void *)chip)) {
-               snd_printk("unable to grab IRQ %d\n", chip->irq);
+       chip->port = pci_resource_start(pci, 0);
+       if (request_irq(pci->irq, snd_fm801_interrupt, SA_INTERRUPT|SA_SHIRQ,
+                       "FM801", chip)) {
+               snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->irq);
                snd_fm801_free(chip);
                return -EBUSY;
        }
@@ -1303,83 +1384,7 @@ static int __devinit snd_fm801_create(snd_card_t * card,
        if (rev >= 0xb1)        /* FM801-AU */
                chip->multichannel = 1;
 
-       /* codec cold reset + AC'97 warm reset */
-       outw((1<<5)|(1<<6), FM801_REG(chip, CODEC_CTRL));
-       inw(FM801_REG(chip, CODEC_CTRL)); /* flush posting data */
-       udelay(100);
-       outw(0, FM801_REG(chip, CODEC_CTRL));
-
-       timeout = (jiffies + (3 * HZ) / 4) + 1;         /* min 750ms */
-
-       outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
-       udelay(5);
-       do {
-               if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
-                       goto __ac97_secondary;
-               set_current_state(TASK_UNINTERRUPTIBLE);
-               schedule_timeout(1);
-       } while (time_after(timeout, jiffies));
-       snd_printk("Primary AC'97 codec not found\n");
-       snd_fm801_free(chip);
-       return -EIO;
-
-      __ac97_secondary:
-       if (!chip->multichannel)        /* lookup is not required */
-               goto __ac97_ok;
-       for (id = 3; id > 0; id--) {    /* my card has the secondary codec */
-                                       /* at address #3, so the loop is inverted */
-
-               timeout = jiffies + HZ / 20;
-
-               outw((1<<7) | (id << FM801_AC97_ADDR_SHIFT) | AC97_VENDOR_ID1, FM801_REG(chip, AC97_CMD));
-               udelay(5);
-               do {
-                       if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8)) {
-                               cmdw = inw(FM801_REG(chip, AC97_DATA));
-                               if (cmdw != 0xffff && cmdw != 0) {
-                                       chip->secondary = 1;
-                                       chip->secondary_addr = id;
-                                       goto __ac97_ok;
-                               }
-                       }
-                       set_current_state(TASK_UNINTERRUPTIBLE);
-                       schedule_timeout(1);
-               } while (time_after(timeout, jiffies));
-       }
-
-       /* the recovery phase, it seems that probing for non-existing codec might */
-       /* cause timeout problems */
-       timeout = (jiffies + (3 * HZ) / 4) + 1;         /* min 750ms */
-
-       outw((1<<7) | (0 << FM801_AC97_ADDR_SHIFT), FM801_REG(chip, AC97_CMD));
-       udelay(5);
-       do {
-               if ((inw(FM801_REG(chip, AC97_CMD)) & (3<<8)) == (1<<8))
-                       goto __ac97_ok;
-               set_current_state(TASK_UNINTERRUPTIBLE);
-               schedule_timeout(1);
-       } while (time_after(timeout, jiffies));
-       snd_printk("Primary AC'97 codec not responding\n");
-       snd_fm801_free(chip);
-       return -EIO;
-
-      __ac97_ok:
-
-       /* init volume */
-       outw(0x0808, FM801_REG(chip, PCM_VOL));
-       outw(0x9f1f, FM801_REG(chip, FM_VOL));
-       outw(0x8808, FM801_REG(chip, I2S_VOL));
-
-       /* I2S control - I2S mode */
-       outw(0x0003, FM801_REG(chip, I2S_MODE));
-
-       /* interrupt setup - unmask MPU, PLAYBACK & CAPTURE */
-       cmdw = inw(FM801_REG(chip, IRQ_MASK));
-       cmdw &= ~0x0083;
-       outw(cmdw, FM801_REG(chip, IRQ_MASK));
-
-       /* interrupt clear */
-       outw(FM801_IRQ_PLAYBACK|FM801_IRQ_CAPTURE|FM801_IRQ_MPU, FM801_REG(chip, IRQ_STATUS));
+       snd_fm801_chip_init(chip, 0);
 
        if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
                snd_fm801_free(chip);
@@ -1407,9 +1412,9 @@ static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
                                          const struct pci_device_id *pci_id)
 {
        static int dev;
-       snd_card_t *card;
-       fm801_t *chip;
-       opl3_t *opl3;
+       struct snd_card *card;
+       struct fm801 *chip;
+       struct snd_opl3 *opl3;
        int err;
 
         if (dev >= SNDRV_CARDS)
@@ -1426,6 +1431,7 @@ static int __devinit snd_card_fm801_probe(struct pci_dev *pci,
                snd_card_free(card);
                return err;
        }
+       card->private_data = chip;
 
        strcpy(card->driver, "FM801");
        strcpy(card->shortname, "ForteMedia FM801-");
@@ -1473,24 +1479,70 @@ static void __devexit snd_card_fm801_remove(struct pci_dev *pci)
        pci_set_drvdata(pci, NULL);
 }
 
+#ifdef CONFIG_PM
+static unsigned char saved_regs[] = {
+       FM801_PCM_VOL, FM801_I2S_VOL, FM801_FM_VOL, FM801_REC_SRC,
+       FM801_PLY_CTRL, FM801_PLY_COUNT, FM801_PLY_BUF1, FM801_PLY_BUF2,
+       FM801_CAP_CTRL, FM801_CAP_COUNT, FM801_CAP_BUF1, FM801_CAP_BUF2,
+       FM801_CODEC_CTRL, FM801_I2S_MODE, FM801_VOLUME, FM801_GEN_CTRL,
+};
+
+static int snd_fm801_suspend(struct pci_dev *pci, pm_message_t state)
+{
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct fm801 *chip = card->private_data;
+       int i;
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
+       snd_pcm_suspend_all(chip->pcm);
+       snd_ac97_suspend(chip->ac97);
+       snd_ac97_suspend(chip->ac97_sec);
+       for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
+               chip->saved_regs[i] = inw(chip->port + saved_regs[i]);
+       /* FIXME: tea575x suspend */
+
+       pci_set_power_state(pci, PCI_D3hot);
+       pci_disable_device(pci);
+       pci_save_state(pci);
+       return 0;
+}
+
+static int snd_fm801_resume(struct pci_dev *pci)
+{
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct fm801 *chip = card->private_data;
+       int i;
+
+       pci_restore_state(pci);
+       pci_enable_device(pci);
+       pci_set_power_state(pci, PCI_D0);
+       pci_set_master(pci);
+
+       snd_fm801_chip_init(chip, 1);
+       snd_ac97_resume(chip->ac97);
+       snd_ac97_resume(chip->ac97_sec);
+       for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
+               outw(chip->saved_regs[i], chip->port + saved_regs[i]);
+
+       snd_power_change_state(card, SNDRV_CTL_POWER_D0);
+       return 0;
+}
+#endif
+
 static struct pci_driver driver = {
        .name = "FM801",
        .id_table = snd_fm801_ids,
        .probe = snd_card_fm801_probe,
        .remove = __devexit_p(snd_card_fm801_remove),
+#ifdef CONFIG_PM
+       .suspend = snd_fm801_suspend,
+       .resume = snd_fm801_resume,
+#endif
 };
 
 static int __init alsa_card_fm801_init(void)
 {
-       int err;
-
-       if ((err = pci_module_init(&driver)) < 0) {
-#ifdef MODULE
-               printk(KERN_ERR "ForteMedia FM801 soundcard not found or device busy\n");
-#endif
-               return err;
-       }
-       return 0;
+       return pci_register_driver(&driver);
 }
 
 static void __exit alsa_card_fm801_exit(void)
@@ -1500,25 +1552,3 @@ static void __exit alsa_card_fm801_exit(void)
 
 module_init(alsa_card_fm801_init)
 module_exit(alsa_card_fm801_exit)
-
-#ifndef MODULE
-
-/* format is: snd-fm801=enable,index,id,tea575x_tuner */
-
-static int __init alsa_card_fm801_setup(char *str)
-{
-       static unsigned __initdata nr_dev = 0;
-
-       if (nr_dev >= SNDRV_CARDS)
-               return 0;
-       (void)(get_option(&str,&enable[nr_dev]) == 2 &&
-              get_option(&str,&index[nr_dev]) == 2 &&
-              get_id(&str,&id[nr_dev]) == 2 &&
-              get_option(&str,&tea575x_tuner[nr_dev]));
-       nr_dev++;
-       return 1;
-}
-
-__setup("snd-fm801=", alsa_card_fm801_setup);
-
-#endif /* ifndef MODULE */