Merge tag 'hda-switcheroo' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
authorLinus Torvalds <torvalds@linux-foundation.org>
Fri, 25 May 2012 15:38:26 +0000 (08:38 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 25 May 2012 15:38:26 +0000 (08:38 -0700)
Pull VGA-switcheroo audio client support for HD-audio from Takashi Iwai.

This depended on the recent drm pull.

* tag 'hda-switcheroo' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound:
  ALSA: hda - unlock on error in azx_interrupt()
  ALSA: hda - Support VGA-switcheroo
  ALSA: hda - Export snd_hda_lock_devices()
  ALSA: hda - Check the dead HDMI audio controller by vga-switcheroo

1  2 
sound/pci/hda/hda_codec.c
sound/pci/hda/hda_codec.h
sound/pci/hda/hda_intel.c

index eb09a3348325358b0a741d3408d48cc4e5e8fffd,2fb935d9dccc0cb48adfa0d4d61a96b789b12c1f..41ca803a1fff9d1a4c60baa3889cc3171a990bf2
@@@ -334,67 -334,78 +334,67 @@@ static hda_nid_t *lookup_conn_list(stru
        return NULL;
  }
  
 +/* read the connection and add to the cache */
 +static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
 +{
 +      hda_nid_t list[HDA_MAX_CONNECTIONS];
 +      int len;
 +
 +      len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
 +      if (len < 0)
 +              return len;
 +      return snd_hda_override_conn_list(codec, nid, len, list);
 +}
 +
  /**
 - * snd_hda_get_conn_list - get connection list
 + * snd_hda_get_connections - copy connection list
   * @codec: the HDA codec
   * @nid: NID to parse
 - * @listp: the pointer to store NID list
 + * @conn_list: connection list array; when NULL, checks only the size
 + * @max_conns: max. number of connections to store
   *
   * Parses the connection list of the given widget and stores the list
   * of NIDs.
   *
   * Returns the number of connections, or a negative error code.
   */
 -int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
 -                        const hda_nid_t **listp)
 +int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
 +                          hda_nid_t *conn_list, int max_conns)
  {
        struct snd_array *array = &codec->conn_lists;
 -      int len, err;
 -      hda_nid_t list[HDA_MAX_CONNECTIONS];
 +      int len;
        hda_nid_t *p;
        bool added = false;
  
   again:
 +      mutex_lock(&codec->hash_mutex);
 +      len = -1;
        /* if the connection-list is already cached, read it */
        p = lookup_conn_list(array, nid);
        if (p) {
 -              if (listp)
 -                      *listp = p + 2;
 -              return p[1];
 +              len = p[1];
 +              if (conn_list && len > max_conns) {
 +                      snd_printk(KERN_ERR "hda_codec: "
 +                                 "Too many connections %d for NID 0x%x\n",
 +                                 len, nid);
 +                      mutex_unlock(&codec->hash_mutex);
 +                      return -EINVAL;
 +              }
 +              if (conn_list && len)
 +                      memcpy(conn_list, p + 2, len * sizeof(hda_nid_t));
        }
 +      mutex_unlock(&codec->hash_mutex);
 +      if (len >= 0)
 +              return len;
        if (snd_BUG_ON(added))
                return -EINVAL;
  
 -      /* read the connection and add to the cache */
 -      len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
 +      len = read_and_add_raw_conns(codec, nid);
        if (len < 0)
                return len;
 -      err = snd_hda_override_conn_list(codec, nid, len, list);
 -      if (err < 0)
 -              return err;
        added = true;
        goto again;
  }
 -EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
 -
 -/**
 - * snd_hda_get_connections - copy connection list
 - * @codec: the HDA codec
 - * @nid: NID to parse
 - * @conn_list: connection list array
 - * @max_conns: max. number of connections to store
 - *
 - * Parses the connection list of the given widget and stores the list
 - * of NIDs.
 - *
 - * Returns the number of connections, or a negative error code.
 - */
 -int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
 -                           hda_nid_t *conn_list, int max_conns)
 -{
 -      const hda_nid_t *list;
 -      int len = snd_hda_get_conn_list(codec, nid, &list);
 -
 -      if (len <= 0)
 -              return len;
 -      if (len > max_conns) {
 -              snd_printk(KERN_ERR "hda_codec: "
 -                         "Too many connections %d for NID 0x%x\n",
 -                         len, nid);
 -              return -EINVAL;
 -      }
 -      memcpy(conn_list, list, len * sizeof(hda_nid_t));
 -      return len;
 -}
  EXPORT_SYMBOL_HDA(snd_hda_get_connections);
  
  /**
@@@ -532,7 -543,6 +532,7 @@@ int snd_hda_override_conn_list(struct h
        hda_nid_t *p;
        int i, old_used;
  
 +      mutex_lock(&codec->hash_mutex);
        p = lookup_conn_list(array, nid);
        if (p)
                *p = -1; /* invalidate the old entry */
        for (i = 0; i < len; i++)
                if (!add_conn_list(array, list[i]))
                        goto error_add;
 +      mutex_unlock(&codec->hash_mutex);
        return 0;
  
   error_add:
        array->used = old_used;
 +      mutex_unlock(&codec->hash_mutex);
        return -ENOMEM;
  }
  EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
@@@ -2239,24 -2247,50 +2239,50 @@@ void snd_hda_ctls_clear(struct hda_code
  /* pseudo device locking
   * toggle card->shutdown to allow/disallow the device access (as a hack)
   */
static int hda_lock_devices(struct snd_card *card)
int snd_hda_lock_devices(struct hda_bus *bus)
  {
+       struct snd_card *card = bus->card;
+       struct hda_codec *codec;
        spin_lock(&card->files_lock);
-       if (card->shutdown) {
-               spin_unlock(&card->files_lock);
-               return -EINVAL;
-       }
+       if (card->shutdown)
+               goto err_unlock;
        card->shutdown = 1;
+       if (!list_empty(&card->ctl_files))
+               goto err_clear;
+       list_for_each_entry(codec, &bus->codec_list, list) {
+               int pcm;
+               for (pcm = 0; pcm < codec->num_pcms; pcm++) {
+                       struct hda_pcm *cpcm = &codec->pcm_info[pcm];
+                       if (!cpcm->pcm)
+                               continue;
+                       if (cpcm->pcm->streams[0].substream_opened ||
+                           cpcm->pcm->streams[1].substream_opened)
+                               goto err_clear;
+               }
+       }
        spin_unlock(&card->files_lock);
        return 0;
+  err_clear:
+       card->shutdown = 0;
+  err_unlock:
+       spin_unlock(&card->files_lock);
+       return -EINVAL;
  }
+ EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
  
static void hda_unlock_devices(struct snd_card *card)
void snd_hda_unlock_devices(struct hda_bus *bus)
  {
+       struct snd_card *card = bus->card;
+       card = bus->card;
        spin_lock(&card->files_lock);
        card->shutdown = 0;
        spin_unlock(&card->files_lock);
  }
+ EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
  
  /**
   * snd_hda_codec_reset - Clear all objects assigned to the codec
   */
  int snd_hda_codec_reset(struct hda_codec *codec)
  {
-       struct snd_card *card = codec->bus->card;
-       int i, pcm;
+       struct hda_bus *bus = codec->bus;
+       struct snd_card *card = bus->card;
+       int i;
  
-       if (hda_lock_devices(card) < 0)
-               return -EBUSY;
-       /* check whether the codec isn't used by any mixer or PCM streams */
-       if (!list_empty(&card->ctl_files)) {
-               hda_unlock_devices(card);
+       if (snd_hda_lock_devices(bus) < 0)
                return -EBUSY;
-       }
-       for (pcm = 0; pcm < codec->num_pcms; pcm++) {
-               struct hda_pcm *cpcm = &codec->pcm_info[pcm];
-               if (!cpcm->pcm)
-                       continue;
-               if (cpcm->pcm->streams[0].substream_opened ||
-                   cpcm->pcm->streams[1].substream_opened) {
-                       hda_unlock_devices(card);
-                       return -EBUSY;
-               }
-       }
  
        /* OK, let it free */
  
        codec->power_on = 0;
        codec->power_transition = 0;
        codec->power_jiffies = jiffies;
-       flush_workqueue(codec->bus->workq);
+       flush_workqueue(bus->workq);
  #endif
        snd_hda_ctls_clear(codec);
        /* relase PCMs */
                if (codec->pcm_info[i].pcm) {
                        snd_device_free(card, codec->pcm_info[i].pcm);
                        clear_bit(codec->pcm_info[i].device,
-                                 codec->bus->pcm_dev_bits);
+                                 bus->pcm_dev_bits);
                }
        }
        if (codec->patch_ops.free)
        codec->owner = NULL;
  
        /* allow device access again */
-       hda_unlock_devices(card);
+       snd_hda_unlock_devices(bus);
        return 0;
  }
  
index 54b52819fb47acf5ef8b2342643a1ece7d9a8c57,be91cdbb1621e7c1f3642ae8c39342a12d40f7c5..4fc3960c85917837508ef32b1d954737061fc926
@@@ -911,13 -911,10 +911,13 @@@ int snd_hda_get_sub_nodes(struct hda_co
                          hda_nid_t *start_id);
  int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
                            hda_nid_t *conn_list, int max_conns);
 +static inline int
 +snd_hda_get_num_conns(struct hda_codec *codec, hda_nid_t nid)
 +{
 +      return snd_hda_get_connections(codec, nid, NULL, 0);
 +}
  int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
                            hda_nid_t *conn_list, int max_conns);
 -int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
 -                        const hda_nid_t **listp);
  int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int nums,
                          const hda_nid_t *list);
  int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
@@@ -1023,6 -1020,9 +1023,9 @@@ void snd_hda_codec_set_power_to_all(str
                                    unsigned int power_state,
                                    bool eapd_workaround);
  
+ int snd_hda_lock_devices(struct hda_bus *bus);
+ void snd_hda_unlock_devices(struct hda_bus *bus);
  /*
   * power management
   */
index 4ab8102f87ea63ccb83ccbb6c97df2d17fa13eb2,d0627723f7a83cf73753cb2c8c2dbc89bb2a9b85..2b6392be451c688830cf9d42e346d0eee61ac1dc
@@@ -53,6 -53,8 +53,8 @@@
  #endif
  #include <sound/core.h>
  #include <sound/initval.h>
+ #include <linux/vgaarb.h>
+ #include <linux/vga_switcheroo.h>
  #include "hda_codec.h"
  
  
@@@ -175,6 -177,13 +177,13 @@@ MODULE_DESCRIPTION("Intel HDA driver")
  #define SFX   "hda-intel: "
  #endif
  
+ #if defined(CONFIG_PM) && defined(CONFIG_VGA_SWITCHEROO)
+ #ifdef CONFIG_SND_HDA_CODEC_HDMI
+ #define SUPPORT_VGA_SWITCHEROO
+ #endif
+ #endif
  /*
   * registers
   */
@@@ -472,6 -481,12 +481,12 @@@ struct azx 
        unsigned int probing :1; /* codec probing phase */
        unsigned int snoop:1;
        unsigned int align_buffer_size:1;
+       unsigned int region_requested:1;
+       /* VGA-switcheroo setup */
+       unsigned int use_vga_switcheroo:1;
+       unsigned int init_failed:1; /* delayed init failed */
+       unsigned int disabled:1; /* disabled by VGA-switcher */
  
        /* for debugging */
        unsigned int last_cmd[AZX_MAX_CODECS];
@@@ -538,7 -553,20 +553,20 @@@ enum 
  #define AZX_DCAPS_PRESET_CTHDA \
        (AZX_DCAPS_NO_MSI | AZX_DCAPS_POSFIX_LPIB | AZX_DCAPS_4K_BDLE_BOUNDARY)
  
- static char *driver_short_names[] __devinitdata = {
+ /*
+  * VGA-switcher support
+  */
+ #ifdef SUPPORT_VGA_SWITCHEROO
+ #define DELAYED_INIT_MARK
+ #define DELAYED_INITDATA_MARK
+ #define use_vga_switcheroo(chip)      ((chip)->use_vga_switcheroo)
+ #else
+ #define DELAYED_INIT_MARK     __devinit
+ #define DELAYED_INITDATA_MARK __devinitdata
+ #define use_vga_switcheroo(chip)      0
+ #endif
+ static char *driver_short_names[] DELAYED_INITDATA_MARK = {
        [AZX_DRIVER_ICH] = "HDA Intel",
        [AZX_DRIVER_PCH] = "HDA Intel PCH",
        [AZX_DRIVER_SCH] = "HDA Intel MID",
@@@ -789,13 -817,11 +817,13 @@@ static unsigned int azx_rirb_get_respon
  {
        struct azx *chip = bus->private_data;
        unsigned long timeout;
 +      unsigned long loopcounter;
        int do_poll = 0;
  
   again:
        timeout = jiffies + msecs_to_jiffies(1000);
 -      for (;;) {
 +
 +      for (loopcounter = 0;; loopcounter++) {
                if (chip->polling_mode || do_poll) {
                        spin_lock_irq(&chip->reg_lock);
                        azx_update_rirb(chip);
                }
                if (time_after(jiffies, timeout))
                        break;
 -              if (bus->needs_damn_long_delay)
 +              if (bus->needs_damn_long_delay || loopcounter > 3000)
                        msleep(2); /* temporary workaround */
                else {
                        udelay(10);
@@@ -959,6 -985,8 +987,8 @@@ static int azx_send_cmd(struct hda_bus 
  {
        struct azx *chip = bus->private_data;
  
+       if (chip->disabled)
+               return 0;
        chip->last_cmd[azx_command_addr(val)] = val;
        if (chip->single_cmd)
                return azx_single_send_cmd(bus, val);
@@@ -971,6 -999,8 +1001,8 @@@ static unsigned int azx_get_response(st
                                     unsigned int addr)
  {
        struct azx *chip = bus->private_data;
+       if (chip->disabled)
+               return 0;
        if (chip->single_cmd)
                return azx_single_get_response(bus, addr);
        else
@@@ -1236,6 -1266,11 +1268,11 @@@ static irqreturn_t azx_interrupt(int ir
  
        spin_lock(&chip->reg_lock);
  
+       if (chip->disabled) {
+               spin_unlock(&chip->reg_lock);
+               return IRQ_NONE;
+       }
        status = azx_readl(chip, INTSTS);
        if (status == 0) {
                spin_unlock(&chip->reg_lock);
@@@ -1521,12 -1556,12 +1558,12 @@@ static void azx_bus_reset(struct hda_bu
   */
  
  /* number of codec slots for each chipset: 0 = default slots (i.e. 4) */
- static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] __devinitdata = {
+ static unsigned int azx_max_codecs[AZX_NUM_DRIVERS] DELAYED_INITDATA_MARK = {
        [AZX_DRIVER_NVIDIA] = 8,
        [AZX_DRIVER_TERA] = 1,
  };
  
- static int __devinit azx_codec_create(struct azx *chip, const char *model)
+ static int DELAYED_INIT_MARK azx_codec_create(struct azx *chip, const char *model)
  {
        struct hda_bus_template bus_temp;
        int c, codecs, err;
@@@ -2444,6 -2479,105 +2481,105 @@@ static void azx_notifier_unregister(str
                unregister_reboot_notifier(&chip->reboot_notifier);
  }
  
+ static int DELAYED_INIT_MARK azx_first_init(struct azx *chip);
+ static int DELAYED_INIT_MARK azx_probe_continue(struct azx *chip);
+ static struct pci_dev __devinit *get_bound_vga(struct pci_dev *pci);
+ #ifdef SUPPORT_VGA_SWITCHEROO
+ static void azx_vs_set_state(struct pci_dev *pci,
+                            enum vga_switcheroo_state state)
+ {
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct azx *chip = card->private_data;
+       bool disabled;
+       if (chip->init_failed)
+               return;
+       disabled = (state == VGA_SWITCHEROO_OFF);
+       if (chip->disabled == disabled)
+               return;
+       if (!chip->bus) {
+               chip->disabled = disabled;
+               if (!disabled) {
+                       snd_printk(KERN_INFO SFX
+                                  "%s: Start delayed initialization\n",
+                                  pci_name(chip->pci));
+                       if (azx_first_init(chip) < 0 ||
+                           azx_probe_continue(chip) < 0) {
+                               snd_printk(KERN_ERR SFX
+                                          "%s: initialization error\n",
+                                          pci_name(chip->pci));
+                               chip->init_failed = true;
+                       }
+               }
+       } else {
+               snd_printk(KERN_INFO SFX
+                          "%s %s via VGA-switcheroo\n",
+                          disabled ? "Disabling" : "Enabling",
+                          pci_name(chip->pci));
+               if (disabled) {
+                       azx_suspend(pci, PMSG_FREEZE);
+                       chip->disabled = true;
+                       snd_hda_lock_devices(chip->bus);
+               } else {
+                       snd_hda_unlock_devices(chip->bus);
+                       chip->disabled = false;
+                       azx_resume(pci);
+               }
+       }
+ }
+ static bool azx_vs_can_switch(struct pci_dev *pci)
+ {
+       struct snd_card *card = pci_get_drvdata(pci);
+       struct azx *chip = card->private_data;
+       if (chip->init_failed)
+               return false;
+       if (chip->disabled || !chip->bus)
+               return true;
+       if (snd_hda_lock_devices(chip->bus))
+               return false;
+       snd_hda_unlock_devices(chip->bus);
+       return true;
+ }
+ static void __devinit init_vga_switcheroo(struct azx *chip)
+ {
+       struct pci_dev *p = get_bound_vga(chip->pci);
+       if (p) {
+               snd_printk(KERN_INFO SFX
+                          "%s: Handle VGA-switcheroo audio client\n",
+                          pci_name(chip->pci));
+               chip->use_vga_switcheroo = 1;
+               pci_dev_put(p);
+       }
+ }
+ static const struct vga_switcheroo_client_ops azx_vs_ops = {
+       .set_gpu_state = azx_vs_set_state,
+       .can_switch = azx_vs_can_switch,
+ };
+ static int __devinit register_vga_switcheroo(struct azx *chip)
+ {
+       if (!chip->use_vga_switcheroo)
+               return 0;
+       /* FIXME: currently only handling DIS controller
+        * is there any machine with two switchable HDMI audio controllers?
+        */
+       return vga_switcheroo_register_audio_client(chip->pci, &azx_vs_ops,
+                                                   VGA_SWITCHEROO_DIS,
+                                                   chip->bus != NULL);
+ }
+ #else
+ #define init_vga_switcheroo(chip)             /* NOP */
+ #define register_vga_switcheroo(chip)         0
+ #endif /* SUPPORT_VGA_SWITCHER */
  /*
   * destructor
   */
@@@ -2453,6 -2587,12 +2589,12 @@@ static int azx_free(struct azx *chip
  
        azx_notifier_unregister(chip);
  
+       if (use_vga_switcheroo(chip)) {
+               if (chip->disabled && chip->bus)
+                       snd_hda_unlock_devices(chip->bus);
+               vga_switcheroo_unregister_client(chip->pci);
+       }
        if (chip->initialized) {
                azx_clear_irq_pending(chip);
                for (i = 0; i < chip->num_streams; i++)
                mark_pages_wc(chip, &chip->posbuf, false);
                snd_dma_free_pages(&chip->posbuf);
        }
-       pci_release_regions(chip->pci);
+       if (chip->region_requested)
+               pci_release_regions(chip->pci);
        pci_disable_device(chip->pci);
        kfree(chip->azx_dev);
        kfree(chip);
@@@ -2495,6 -2636,45 +2638,45 @@@ static int azx_dev_free(struct snd_devi
        return azx_free(device->device_data);
  }
  
+ /*
+  * Check of disabled HDMI controller by vga-switcheroo
+  */
+ static struct pci_dev __devinit *get_bound_vga(struct pci_dev *pci)
+ {
+       struct pci_dev *p;
+       /* check only discrete GPU */
+       switch (pci->vendor) {
+       case PCI_VENDOR_ID_ATI:
+       case PCI_VENDOR_ID_AMD:
+       case PCI_VENDOR_ID_NVIDIA:
+               if (pci->devfn == 1) {
+                       p = pci_get_domain_bus_and_slot(pci_domain_nr(pci->bus),
+                                                       pci->bus->number, 0);
+                       if (p) {
+                               if ((p->class >> 8) == PCI_CLASS_DISPLAY_VGA)
+                                       return p;
+                               pci_dev_put(p);
+                       }
+               }
+               break;
+       }
+       return NULL;
+ }
+ static bool __devinit check_hdmi_disabled(struct pci_dev *pci)
+ {
+       bool vga_inactive = false;
+       struct pci_dev *p = get_bound_vga(pci);
+       if (p) {
+               if (vga_default_device() && p != vga_default_device())
+                       vga_inactive = true;
+               pci_dev_put(p);
+       }
+       return vga_inactive;
+ }
  /*
   * white/black-listing for position_fix
   */
@@@ -2566,8 -2746,6 +2748,8 @@@ static struct snd_pci_quirk probe_mask_
        /* forced codec slots */
        SND_PCI_QUIRK(0x1043, 0x1262, "ASUS W5Fm", 0x103),
        SND_PCI_QUIRK(0x1046, 0x1262, "ASUS W5F", 0x103),
 +      /* WinFast VP200 H (Teradici) user reported broken communication */
 +      SND_PCI_QUIRK(0x3a21, 0x040d, "WinFast VP200 H", 0x101),
        {}
  };
  
@@@ -2672,12 -2850,11 +2854,11 @@@ static int __devinit azx_create(struct 
                                int dev, unsigned int driver_caps,
                                struct azx **rchip)
  {
-       struct azx *chip;
-       int i, err;
-       unsigned short gcap;
        static struct snd_device_ops ops = {
                .dev_free = azx_dev_free,
        };
+       struct azx *chip;
+       int err;
  
        *rchip = NULL;
  
        chip->dev_index = dev;
        INIT_WORK(&chip->irq_pending_work, azx_irq_pending_work);
        INIT_LIST_HEAD(&chip->pcm_list);
+       init_vga_switcheroo(chip);
  
        chip->position_fix[0] = chip->position_fix[1] =
                check_position_fix(chip, position_fix[dev]);
                }
        }
  
+       if (check_hdmi_disabled(pci)) {
+               snd_printk(KERN_INFO SFX "VGA controller for %s is disabled\n",
+                          pci_name(pci));
+               if (use_vga_switcheroo(chip)) {
+                       snd_printk(KERN_INFO SFX "Delaying initialization\n");
+                       chip->disabled = true;
+                       goto ok;
+               }
+               kfree(chip);
+               pci_disable_device(pci);
+               return -ENXIO;
+       }
+       err = azx_first_init(chip);
+       if (err < 0) {
+               azx_free(chip);
+               return err;
+       }
+  ok:
+       err = register_vga_switcheroo(chip);
+       if (err < 0) {
+               snd_printk(KERN_ERR SFX
+                          "Error registering VGA-switcheroo client\n");
+               azx_free(chip);
+               return err;
+       }
+       err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
+       if (err < 0) {
+               snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
+               azx_free(chip);
+               return err;
+       }
+       *rchip = chip;
+       return 0;
+ }
+ static int DELAYED_INIT_MARK azx_first_init(struct azx *chip)
+ {
+       int dev = chip->dev_index;
+       struct pci_dev *pci = chip->pci;
+       struct snd_card *card = chip->card;
+       int i, err;
+       unsigned short gcap;
  #if BITS_PER_LONG != 64
        /* Fix up base address on ULI M5461 */
        if (chip->driver_type == AZX_DRIVER_ULI) {
  #endif
  
        err = pci_request_regions(pci, "ICH HD audio");
-       if (err < 0) {
-               kfree(chip);
-               pci_disable_device(pci);
+       if (err < 0)
                return err;
-       }
+       chip->region_requested = 1;
  
        chip->addr = pci_resource_start(pci, 0);
        chip->remap_addr = pci_ioremap_bar(pci, 0);
        if (chip->remap_addr == NULL) {
                snd_printk(KERN_ERR SFX "ioremap error\n");
-               err = -ENXIO;
-               goto errout;
+               return -ENXIO;
        }
  
        if (chip->msi)
                if (pci_enable_msi(pci) < 0)
                        chip->msi = 0;
  
-       if (azx_acquire_irq(chip, 0) < 0) {
-               err = -EBUSY;
-               goto errout;
-       }
+       if (azx_acquire_irq(chip, 0) < 0)
+               return -EBUSY;
  
        pci_set_master(pci);
        synchronize_irq(chip->irq);
                                GFP_KERNEL);
        if (!chip->azx_dev) {
                snd_printk(KERN_ERR SFX "cannot malloc azx_dev\n");
-               goto errout;
+               return -ENOMEM;
        }
  
        for (i = 0; i < chip->num_streams; i++) {
                                          BDL_SIZE, &chip->azx_dev[i].bdl);
                if (err < 0) {
                        snd_printk(KERN_ERR SFX "cannot allocate BDL\n");
-                       goto errout;
+                       return -ENOMEM;
                }
                mark_pages_wc(chip, &chip->azx_dev[i].bdl, true);
        }
                                  chip->num_streams * 8, &chip->posbuf);
        if (err < 0) {
                snd_printk(KERN_ERR SFX "cannot allocate posbuf\n");
-               goto errout;
+               return -ENOMEM;
        }
        mark_pages_wc(chip, &chip->posbuf, true);
        /* allocate CORB/RIRB */
        err = azx_alloc_cmd_io(chip);
        if (err < 0)
-               goto errout;
+               return err;
  
        /* initialize streams */
        azx_init_stream(chip);
        /* codec detection */
        if (!chip->codec_mask) {
                snd_printk(KERN_ERR SFX "no codecs found!\n");
-               err = -ENODEV;
-               goto errout;
-       }
-       err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
-       if (err <0) {
-               snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
-               goto errout;
+               return -ENODEV;
        }
  
        strcpy(card->driver, "HDA-Intel");
                 "%s at 0x%lx irq %i",
                 card->shortname, chip->addr, chip->irq);
  
-       *rchip = chip;
        return 0;
-  errout:
-       azx_free(chip);
-       return err;
  }
  
  static void power_down_all_codecs(struct azx *chip)
@@@ -2946,6 -3154,27 +3158,27 @@@ static int __devinit azx_probe(struct p
                goto out_free;
        card->private_data = chip;
  
+       if (!chip->disabled) {
+               err = azx_probe_continue(chip);
+               if (err < 0)
+                       goto out_free;
+       }
+       pci_set_drvdata(pci, card);
+       dev++;
+       return 0;
+ out_free:
+       snd_card_free(card);
+       return err;
+ }
+ static int DELAYED_INIT_MARK azx_probe_continue(struct azx *chip)
+ {
+       int dev = chip->dev_index;
+       int err;
  #ifdef CONFIG_SND_HDA_INPUT_BEEP
        chip->beep_mode = beep_mode[dev];
  #endif
        if (err < 0)
                goto out_free;
  
-       err = snd_card_register(card);
+       err = snd_card_register(chip->card);
        if (err < 0)
                goto out_free;
  
-       pci_set_drvdata(pci, card);
        chip->running = 1;
        power_down_all_codecs(chip);
        azx_notifier_register(chip);
  
-       dev++;
-       return err;
+       return 0;
  out_free:
-       snd_card_free(card);
+       chip->init_failed = 1;
        return err;
  }
  
  static void __devexit azx_remove(struct pci_dev *pci)
  {
-       snd_card_free(pci_get_drvdata(pci));
+       struct snd_card *card = pci_get_drvdata(pci);
+       if (card)
+               snd_card_free(card);
        pci_set_drvdata(pci, NULL);
  }
  
@@@ -3156,7 -3386,7 +3390,7 @@@ static DEFINE_PCI_DEVICE_TABLE(azx_ids
  MODULE_DEVICE_TABLE(pci, azx_ids);
  
  /* pci_driver definition */
 -static struct pci_driver driver = {
 +static struct pci_driver azx_driver = {
        .name = KBUILD_MODNAME,
        .id_table = azx_ids,
        .probe = azx_probe,
  #endif
  };
  
 -static int __init alsa_card_azx_init(void)
 -{
 -      return pci_register_driver(&driver);
 -}
 -
 -static void __exit alsa_card_azx_exit(void)
 -{
 -      pci_unregister_driver(&driver);
 -}
 -
 -module_init(alsa_card_azx_init)
 -module_exit(alsa_card_azx_exit)
 +module_pci_driver(azx_driver);