From patchwork Wed Feb 12 11:20:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 3637301 X-Patchwork-Delegate: tiwai@suse.de Return-Path: X-Original-To: patchwork-alsa-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 30BA79F369 for ; Wed, 12 Feb 2014 11:30:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 56D8520145 for ; Wed, 12 Feb 2014 11:30:32 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.kernel.org (Postfix) with ESMTP id 1E836201CE for ; Wed, 12 Feb 2014 11:30:25 +0000 (UTC) Received: by alsa0.perex.cz (Postfix, from userid 1000) id 1D752265196; Wed, 12 Feb 2014 12:30:24 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NO_DNS_FOR_FROM, UNPARSEABLE_RELAY autolearn=no version=3.3.1 Received: from alsa0.perex.cz (localhost [IPv6:::1]) by alsa0.perex.cz (Postfix) with ESMTP id 1C0B2265062; Wed, 12 Feb 2014 12:27:49 +0100 (CET) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 986AE265012; Wed, 12 Feb 2014 12:27:46 +0100 (CET) Received: from mx2.suse.de (cantor2.suse.de [195.135.220.15]) by alsa0.perex.cz (Postfix) with ESMTP id 9EC292650C2 for ; Wed, 12 Feb 2014 12:20:14 +0100 (CET) Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 8D4A5ABF8 for ; Wed, 12 Feb 2014 11:20:14 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Date: Wed, 12 Feb 2014 12:20:11 +0100 Message-Id: <1392204013-12334-2-git-send-email-tiwai@suse.de> X-Mailer: git-send-email 1.8.5.2 In-Reply-To: <1392204013-12334-1-git-send-email-tiwai@suse.de> References: <1392204013-12334-1-git-send-email-tiwai@suse.de> Subject: [alsa-devel] [PATCH 1/3] ALSA: Embed card device into struct snd_card X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP As prepared in the previous patch, we are ready to create a device struct for the card object in snd_card_create() now. This patch changes the scheme from the old style to: - embed a device struct for the card object into snd_card struct, - initialize the card device in snd_card_create() (but not register), - registration is done in snd_card_register() via device_add() The actual card device is stored in card->card_dev. The card->dev pointer is kept unchanged and pointing to the parent device as before for compatibility reason. Signed-off-by: Takashi Iwai --- include/sound/core.h | 10 ++++++---- sound/core/init.c | 54 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/include/sound/core.h b/include/sound/core.h index fb05573215d3..f072e596f21a 100644 --- a/include/sound/core.h +++ b/include/sound/core.h @@ -22,6 +22,7 @@ * */ +#include #include /* wake_up() */ #include /* struct mutex */ #include /* struct rw_semaphore */ @@ -41,8 +42,6 @@ /* forward declarations */ struct pci_dev; struct module; -struct device; -struct device_attribute; /* device allocation stuff */ @@ -131,7 +130,8 @@ struct snd_card { wait_queue_head_t shutdown_sleep; atomic_t refcount; /* refcount for disconnection */ struct device *dev; /* device assigned to this card */ - struct device *card_dev; /* cardX object for sysfs */ + struct device card_dev; /* cardX object for sysfs */ + bool registered; /* card_dev is registered? */ #ifdef CONFIG_PM unsigned int power_state; /* power state */ @@ -145,6 +145,8 @@ struct snd_card { #endif }; +#define dev_to_snd_card(p) container_of(p, struct snd_card, card_dev) + #ifdef CONFIG_PM static inline void snd_power_lock(struct snd_card *card) { @@ -193,7 +195,7 @@ struct snd_minor { /* return a device pointer linked to each sound device as a parent */ static inline struct device *snd_card_get_device_link(struct snd_card *card) { - return card ? card->card_dev : NULL; + return card ? &card->card_dev : NULL; } /* sound.c */ diff --git a/sound/core/init.c b/sound/core/init.c index 0115034914c9..2c0301811828 100644 --- a/sound/core/init.c +++ b/sound/core/init.c @@ -156,6 +156,13 @@ static int get_slot_from_bitmask(int mask, int (*check)(struct module *, int), return mask; /* unchanged */ } +static int snd_card_do_free(struct snd_card *card); + +static void release_card_device(struct device *dev) +{ + snd_card_do_free(dev_to_snd_card(dev)); +} + /** * snd_card_new - create and initialize a soundcard structure * @parent: the parent device object @@ -189,6 +196,8 @@ int snd_card_new(struct device *parent, int idx, const char *xid, card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL); if (!card) return -ENOMEM; + if (extra_size > 0) + card->private_data = (char *)card + sizeof(struct snd_card); if (xid) strlcpy(card->id, xid, sizeof(card->id)); err = 0; @@ -208,7 +217,8 @@ int snd_card_new(struct device *parent, int idx, const char *xid, 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; + kfree(card); + return err; } set_bit(idx, snd_cards_lock); /* lock it */ if (idx >= snd_ecards_limit) @@ -230,6 +240,15 @@ int snd_card_new(struct device *parent, int idx, const char *xid, mutex_init(&card->power_lock); init_waitqueue_head(&card->power_sleep); #endif + + device_initialize(&card->card_dev); + card->card_dev.parent = parent; + card->card_dev.class = sound_class; + card->card_dev.release = release_card_device; + err = kobject_set_name(&card->card_dev.kobj, "card%d", idx); + if (err < 0) + goto __error; + /* the control interface cannot be accessed from the user space until */ /* snd_cards_bitmask and snd_cards are set with snd_card_register */ err = snd_ctl_create(card); @@ -242,15 +261,13 @@ int snd_card_new(struct device *parent, int idx, const char *xid, 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); *card_ret = card; return 0; __error_ctl: snd_device_free_all(card); __error: - kfree(card); + put_device(&card->card_dev); return err; } EXPORT_SYMBOL(snd_card_new); @@ -407,9 +424,9 @@ 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); - if (card->card_dev) { - device_unregister(card->card_dev); - card->card_dev = NULL; + if (card->registered) { + device_del(&card->card_dev); + card->registered = false; } #ifdef CONFIG_PM wake_up(&card->power_sleep); @@ -463,7 +480,7 @@ void snd_card_unref(struct snd_card *card) if (atomic_dec_and_test(&card->refcount)) { wake_up(&card->shutdown_sleep); if (card->free_on_last_close) - snd_card_do_free(card); + put_device(&card->card_dev); } } EXPORT_SYMBOL(snd_card_unref); @@ -481,7 +498,7 @@ int snd_card_free_when_closed(struct snd_card *card) card->free_on_last_close = 1; if (atomic_dec_and_test(&card->refcount)) - snd_card_do_free(card); + put_device(&card->card_dev); return 0; } @@ -495,7 +512,7 @@ int snd_card_free(struct snd_card *card) /* wait, until all devices are ready for the free operation */ wait_event(card->shutdown_sleep, !atomic_read(&card->refcount)); - snd_card_do_free(card); + put_device(&card->card_dev); return 0; } @@ -686,12 +703,11 @@ int snd_card_register(struct snd_card *card) if (snd_BUG_ON(!card)) return -EINVAL; - if (!card->card_dev) { - 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; + if (!card->registered) { + err = device_add(&card->card_dev); + if (err < 0) + return err; + card->registered = true; } if ((err = snd_device_register_all(card)) < 0) @@ -721,11 +737,11 @@ 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 - if (card->card_dev) { - err = device_create_file(card->card_dev, &card_id_attrs); + if (card->registered) { + 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); + err = device_create_file(&card->card_dev, &card_number_attrs); if (err < 0) return err; }