Merge commit 'v2.6.30' into for-2.6.31
[safe/jmp/linux-2.6] / sound / core / init.c
index f045f7d..fd56afe 100644 (file)
@@ -46,17 +46,24 @@ static char *slots[SNDRV_CARDS];
 module_param_array(slots, charp, NULL, 0444);
 MODULE_PARM_DESC(slots, "Module names assigned to the slots.");
 
-/* return non-zero if the given index is already reserved for another
+/* return non-zero if the given index is reserved for the given
  * module via slots option
  */
-static int module_slot_mismatch(struct module *module, int idx)
+static int module_slot_match(struct module *module, int idx)
 {
+       int match = 1;
 #ifdef MODULE
-       char *s1, *s2;
+       const char *s1, *s2;
+
        if (!module || !module->name || !slots[idx])
                return 0;
-       s1 = slots[idx];
-       s2 = module->name;
+
+       s1 = module->name;
+       s2 = slots[idx];
+       if (*s2 == '!') {
+               match = 0; /* negative match */
+               s2++;
+       }
        /* compare module name strings
         * hyphens are handled as equivalent with underscore
         */
@@ -68,12 +75,12 @@ static int module_slot_mismatch(struct module *module, int idx)
                if (c2 == '-')
                        c2 = '_';
                if (c1 != c2)
-                       return 1;
+                       return !match;
                if (!c1)
                        break;
        }
-#endif
-       return 0;
+#endif /* MODULE */
+       return match;
 }
 
 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
@@ -114,65 +121,84 @@ static inline int init_info_for_card(struct snd_card *card)
 #endif
 
 /**
- *  snd_card_new - create and initialize a soundcard structure
+ *  snd_card_create - create and initialize a soundcard structure
  *  @idx: card index (address) [0 ... (SNDRV_CARDS-1)]
  *  @xid: card identification (ASCII string)
  *  @module: top level module for locking
  *  @extra_size: allocate this extra size after the main soundcard structure
+ *  @card_ret: the pointer to store the created card instance
  *
  *  Creates and initializes a soundcard structure.
  *
- *  Returns kmallocated snd_card structure. Creates the ALSA control interface
- *  (which is blocked until snd_card_register function is called).
+ *  The function allocates snd_card instance via kzalloc with the given
+ *  space for the driver to use freely.  The allocated struct is stored
+ *  in the given card_ret pointer.
+ *
+ *  Returns zero if successful or a negative error code.
  */
-struct snd_card *snd_card_new(int idx, const char *xid,
-                        struct module *module, int extra_size)
+int snd_card_create(int idx, const char *xid,
+                   struct module *module, int extra_size,
+                   struct snd_card **card_ret)
 {
        struct snd_card *card;
-       int err;
+       int err, idx2;
+
+       if (snd_BUG_ON(!card_ret))
+               return -EINVAL;
+       *card_ret = NULL;
 
        if (extra_size < 0)
                extra_size = 0;
        card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
-       if (card == NULL)
-               return NULL;
+       if (!card)
+               return -ENOMEM;
        if (xid) {
-               if (!snd_info_check_reserved_words(xid))
+               if (!snd_info_check_reserved_words(xid)) {
+                       snd_printk(KERN_ERR
+                                  "given id string '%s' is reserved.\n", xid);
+                       err = -EBUSY;
                        goto __error;
+               }
                strlcpy(card->id, xid, sizeof(card->id));
        }
        err = 0;
        mutex_lock(&snd_card_mutex);
        if (idx < 0) {
-               int idx2;
                for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
                        /* idx == -1 == 0xffff means: take any free slot */
                        if (~snd_cards_lock & idx & 1<<idx2) {
-                               if (module_slot_mismatch(module, idx2))
-                                       continue;
-                               idx = idx2;
-                               if (idx >= snd_ecards_limit)
-                                       snd_ecards_limit = idx + 1;
-                               break;
+                               if (module_slot_match(module, idx2)) {
+                                       idx = idx2;
+                                       break;
+                               }
+                       }
+       }
+       if (idx < 0) {
+               for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)
+                       /* idx == -1 == 0xffff means: take any free slot */
+                       if (~snd_cards_lock & idx & 1<<idx2) {
+                               if (!slots[idx2] || !*slots[idx2]) {
+                                       idx = idx2;
+                                       break;
+                               }
                        }
-       } else {
-                if (idx < snd_ecards_limit) {
-                       if (snd_cards_lock & (1 << idx))
-                               err = -EBUSY;   /* invalid */
-               } else {
-                       if (idx < SNDRV_CARDS)
-                               snd_ecards_limit = idx + 1; /* increase the limit */
-                       else
-                               err = -ENODEV;
-               }
        }
-       if (idx < 0 || err < 0) {
+       if (idx < 0)
+               err = -ENODEV;
+       else if (idx < snd_ecards_limit) {
+               if (snd_cards_lock & (1 << idx))
+                       err = -EBUSY;   /* invalid */
+       } else if (idx >= SNDRV_CARDS)
+               err = -ENODEV;
+       if (err < 0) {
                mutex_unlock(&snd_card_mutex);
                snd_printk(KERN_ERR "cannot find the slot for index %d (range 0-%i), error: %d\n",
                         idx, snd_ecards_limit - 1, err);
                goto __error;
        }
        snd_cards_lock |= 1 << idx;             /* lock it */
+       if (idx >= snd_ecards_limit)
+               snd_ecards_limit = idx + 1; /* increase the limit */
        mutex_unlock(&snd_card_mutex);
        card->number = idx;
        card->module = module;
@@ -182,6 +208,7 @@ struct snd_card *snd_card_new(int idx, const char *xid,
        INIT_LIST_HEAD(&card->controls);
        INIT_LIST_HEAD(&card->ctl_files);
        spin_lock_init(&card->files_lock);
+       INIT_LIST_HEAD(&card->files_list);
        init_waitqueue_head(&card->shutdown_sleep);
 #ifdef CONFIG_PM
        mutex_init(&card->power_lock);
@@ -189,26 +216,28 @@ struct snd_card *snd_card_new(int idx, const char *xid,
 #endif
        /* the control interface cannot be accessed from the user space until */
        /* snd_cards_bitmask and snd_cards are set with snd_card_register */
-       if ((err = snd_ctl_create(card)) < 0) {
-               snd_printd("unable to register control minors\n");
+       err = snd_ctl_create(card);
+       if (err < 0) {
+               snd_printk(KERN_ERR "unable to register control minors\n");
                goto __error;
        }
-       if ((err = snd_info_card_create(card)) < 0) {
-               snd_printd("unable to create card info\n");
+       err = snd_info_card_create(card);
+       if (err < 0) {
+               snd_printk(KERN_ERR "unable to create card info\n");
                goto __error_ctl;
        }
        if (extra_size > 0)
                card->private_data = (char *)card + sizeof(struct snd_card);
-       return card;
+       *card_ret = card;
+       return 0;
 
       __error_ctl:
        snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
       __error:
        kfree(card);
-       return NULL;
+       return err;
 }
-
-EXPORT_SYMBOL(snd_card_new);
+EXPORT_SYMBOL(snd_card_create);
 
 /* return non-zero if a card is already locked */
 int snd_card_locked(int card)
@@ -246,13 +275,17 @@ static int snd_disconnect_release(struct inode *inode, struct file *file)
        list_for_each_entry(_df, &shutdown_files, shutdown_list) {
                if (_df->file == file) {
                        df = _df;
+                       list_del_init(&df->shutdown_list);
                        break;
                }
        }
        spin_unlock(&shutdown_lock);
 
-       if (likely(df))
+       if (likely(df)) {
+               if ((file->f_flags & FASYNC) && df->disconnected_f_op->fasync)
+                       df->disconnected_f_op->fasync(-1, file, 0);
                return df->disconnected_f_op->release(inode, file);
+       }
 
        panic("%s(%p, %p) failed!", __func__, inode, file);
 }
@@ -311,6 +344,9 @@ int snd_card_disconnect(struct snd_card *card)
        struct file *file;
        int err;
 
+       if (!card)
+               return -EINVAL;
+
        spin_lock(&card->files_lock);
        if (card->shutdown) {
                spin_unlock(&card->files_lock);
@@ -322,13 +358,13 @@ int snd_card_disconnect(struct snd_card *card)
        /* phase 1: disable fops (user space) operations for ALSA API */
        mutex_lock(&snd_card_mutex);
        snd_cards[card->number] = NULL;
+       snd_cards_lock &= ~(1 << card->number);
        mutex_unlock(&snd_card_mutex);
        
        /* phase 2: replace file->f_op with special dummy operations */
        
        spin_lock(&card->files_lock);
-       mfile = card->files;
-       while (mfile) {
+       list_for_each_entry(mfile, &card->files_list, list) {
                file = mfile->file;
 
                /* it's critical part, use endless loop */
@@ -341,8 +377,6 @@ int snd_card_disconnect(struct snd_card *card)
 
                mfile->file->f_op = &snd_shutdown_f_ops;
                fops_get(mfile->file->f_op);
-               
-               mfile = mfile->next;
        }
        spin_unlock(&card->files_lock); 
 
@@ -360,6 +394,15 @@ int snd_card_disconnect(struct snd_card *card)
                snd_printk(KERN_ERR "not all devices for card %i can be disconnected\n", card->number);
 
        snd_info_card_disconnect(card);
+#ifndef CONFIG_SYSFS_DEPRECATED
+       if (card->card_dev) {
+               device_unregister(card->card_dev);
+               card->card_dev = NULL;
+       }
+#endif
+#ifdef CONFIG_PM
+       wake_up(&card->power_sleep);
+#endif
        return 0;       
 }
 
@@ -401,38 +444,19 @@ static int snd_card_do_free(struct snd_card *card)
                snd_printk(KERN_WARNING "unable to free card info\n");
                /* Not fatal error */
        }
-#ifndef CONFIG_SYSFS_DEPRECATED
-       if (card->card_dev)
-               device_unregister(card->card_dev);
-#endif
        kfree(card);
        return 0;
 }
 
-static int snd_card_free_prepare(struct snd_card *card)
-{
-       if (card == NULL)
-               return -EINVAL;
-       (void) snd_card_disconnect(card);
-       mutex_lock(&snd_card_mutex);
-       snd_cards[card->number] = NULL;
-       snd_cards_lock &= ~(1 << card->number);
-       mutex_unlock(&snd_card_mutex);
-#ifdef CONFIG_PM
-       wake_up(&card->power_sleep);
-#endif
-       return 0;
-}
-
 int snd_card_free_when_closed(struct snd_card *card)
 {
        int free_now = 0;
-       int ret = snd_card_free_prepare(card);
+       int ret = snd_card_disconnect(card);
        if (ret)
                return ret;
 
        spin_lock(&card->files_lock);
-       if (card->files == NULL)
+       if (list_empty(&card->files_list))
                free_now = 1;
        else
                card->free_on_last_close = 1;
@@ -447,12 +471,12 @@ EXPORT_SYMBOL(snd_card_free_when_closed);
 
 int snd_card_free(struct snd_card *card)
 {
-       int ret = snd_card_free_prepare(card);
+       int ret = snd_card_disconnect(card);
        if (ret)
                return ret;
 
        /* wait, until all devices are ready for the free operation */
-       wait_event(card->shutdown_sleep, card->files == NULL);
+       wait_event(card->shutdown_sleep, list_empty(&card->files_list));
        snd_card_do_free(card);
        return 0;
 }
@@ -523,6 +547,65 @@ static void choose_default_id(struct snd_card *card)
        }
 }
 
+#ifndef CONFIG_SYSFS_DEPRECATED
+static ssize_t
+card_id_show_attr(struct device *dev,
+                 struct device_attribute *attr, char *buf)
+{
+       struct snd_card *card = dev_get_drvdata(dev);
+       return snprintf(buf, PAGE_SIZE, "%s\n", card ? card->id : "(null)");
+}
+
+static ssize_t
+card_id_store_attr(struct device *dev, struct device_attribute *attr,
+                  const char *buf, size_t count)
+{
+       struct snd_card *card = dev_get_drvdata(dev);
+       char buf1[sizeof(card->id)];
+       size_t copy = count > sizeof(card->id) - 1 ?
+                                       sizeof(card->id) - 1 : count;
+       size_t idx;
+       int c;
+
+       for (idx = 0; idx < copy; idx++) {
+               c = buf[idx];
+               if (!isalnum(c) && c != '_' && c != '-')
+                       return -EINVAL;
+       }
+       memcpy(buf1, buf, copy);
+       buf1[copy] = '\0';
+       mutex_lock(&snd_card_mutex);
+       if (!snd_info_check_reserved_words(buf1)) {
+            __exist:
+               mutex_unlock(&snd_card_mutex);
+               return -EEXIST;
+       }
+       for (idx = 0; idx < snd_ecards_limit; idx++) {
+               if (snd_cards[idx] && !strcmp(snd_cards[idx]->id, buf1))
+                       goto __exist;
+       }
+       strcpy(card->id, buf1);
+       snd_info_card_id_change(card);
+       mutex_unlock(&snd_card_mutex);
+
+       return count;
+}
+
+static struct device_attribute card_id_attrs =
+       __ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
+
+static ssize_t
+card_number_show_attr(struct device *dev,
+                    struct device_attribute *attr, char *buf)
+{
+       struct snd_card *card = dev_get_drvdata(dev);
+       return snprintf(buf, PAGE_SIZE, "%i\n", card ? card->number : -1);
+}
+
+static struct device_attribute card_number_attrs =
+       __ATTR(number, S_IRUGO, card_number_show_attr, NULL);
+#endif /* CONFIG_SYSFS_DEPRECATED */
+
 /**
  *  snd_card_register - register the soundcard
  *  @card: soundcard structure
@@ -538,10 +621,12 @@ int snd_card_register(struct snd_card *card)
 {
        int err;
 
-       snd_assert(card != NULL, return -EINVAL);
+       if (snd_BUG_ON(!card))
+               return -EINVAL;
 #ifndef CONFIG_SYSFS_DEPRECATED
        if (!card->card_dev) {
-               card->card_dev = device_create(sound_class, card->dev, 0,
+               card->card_dev = device_create(sound_class, card->dev,
+                                              MKDEV(0, 0), card,
                                               "card%i", card->number);
                if (IS_ERR(card->card_dev))
                        card->card_dev = NULL;
@@ -564,6 +649,16 @@ int snd_card_register(struct snd_card *card)
        if (snd_mixer_oss_notify_callback)
                snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
 #endif
+#ifndef CONFIG_SYSFS_DEPRECATED
+       if (card->card_dev) {
+               err = device_create_file(card->card_dev, &card_id_attrs);
+               if (err < 0)
+                       return err;
+               err = device_create_file(card->card_dev, &card_number_attrs);
+               if (err < 0)
+                       return err;
+       }
+#endif
        return 0;
 }
 
@@ -728,15 +823,13 @@ int snd_card_file_add(struct snd_card *card, struct file *file)
                return -ENOMEM;
        mfile->file = file;
        mfile->disconnected_f_op = NULL;
-       mfile->next = NULL;
        spin_lock(&card->files_lock);
        if (card->shutdown) {
                spin_unlock(&card->files_lock);
                kfree(mfile);
                return -ENODEV;
        }
-       mfile->next = card->files;
-       card->files = mfile;
+       list_add(&mfile->list, &card->files_list);
        spin_unlock(&card->files_lock);
        return 0;
 }
@@ -758,29 +851,20 @@ EXPORT_SYMBOL(snd_card_file_add);
  */
 int snd_card_file_remove(struct snd_card *card, struct file *file)
 {
-       struct snd_monitor_file *mfile, *pfile = NULL;
+       struct snd_monitor_file *mfile, *found = NULL;
        int last_close = 0;
 
        spin_lock(&card->files_lock);
-       mfile = card->files;
-       while (mfile) {
+       list_for_each_entry(mfile, &card->files_list, list) {
                if (mfile->file == file) {
-                       if (pfile)
-                               pfile->next = mfile->next;
-                       else
-                               card->files = mfile->next;
+                       list_del(&mfile->list);
+                       if (mfile->disconnected_f_op)
+                               fops_put(mfile->disconnected_f_op);
+                       found = mfile;
                        break;
                }
-               pfile = mfile;
-               mfile = mfile->next;
-       }
-       if (mfile && mfile->disconnected_f_op) {
-               fops_put(mfile->disconnected_f_op);
-               spin_lock(&shutdown_lock);
-               list_del(&mfile->shutdown_list);
-               spin_unlock(&shutdown_lock);
        }
-       if (card->files == NULL)
+       if (list_empty(&card->files_list))
                last_close = 1;
        spin_unlock(&card->files_lock);
        if (last_close) {
@@ -788,11 +872,11 @@ int snd_card_file_remove(struct snd_card *card, struct file *file)
                if (card->free_on_last_close)
                        snd_card_do_free(card);
        }
-       if (!mfile) {
+       if (!found) {
                snd_printk(KERN_ERR "ALSA card file remove problem (%p)\n", file);
                return -ENOENT;
        }
-       kfree(mfile);
+       kfree(found);
        return 0;
 }