Alsa-Devel Archive on lore.kernel.org
 help / color / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: alsa-devel@alsa-project.org
Subject: [PATCH 19/20] ALSA: Use priority list for managing device list
Date: Wed, 12 Feb 2014 11:52:26 +0100
Message-ID: <1392202347-11774-20-git-send-email-tiwai@suse.de> (raw)
In-Reply-To: <1392202347-11774-1-git-send-email-tiwai@suse.de>

Basically, the device type specifies the priority of the device to be
registered / freed, too.  However, the priority value isn't well
utilized but only it's checked as a group.  This results in
inconsistent register and free order (where each of them should be in
reversed direction).

This patch simplifies the device list management code by simply
inserting a list entry at creation time in an incremental order for
the priority value.  Since we can just follow the link for register,
disconnect and free calls, we don't have to specify the group; so the
whole enum definitions are also simplified as well.

The visible change to outside is that now the SNDRV_DEV_CARD object is
registered before others (control, PCM, etc) and, in return, released
after others.  The priority of some components
(e.g. SNDRV_DEV_COMPRESS) are also adjusted as well.

Also, the unused SNDRV_DEV_TOPLEVEL, SNDRV_DEV_LOWLEVEL_PRE and
SNDRV_DEV_LOWLEVEL_NORMAL are removed as a cleanup.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 include/sound/core.h | 28 ++++++++--------------
 sound/core/device.c  | 66 +++++++++++++++++++++++++++++-----------------------
 sound/core/init.c    | 14 +++--------
 3 files changed, 50 insertions(+), 58 deletions(-)

diff --git a/include/sound/core.h b/include/sound/core.h
index 127b908a2422..07a7ab9a49bf 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -46,25 +46,23 @@ struct device_attribute;
 
 /* device allocation stuff */
 
-#define SNDRV_DEV_TYPE_RANGE_SIZE		0x1000
-
+/* type of the object used in snd_device_*()
+ * this also defines the calling order
+ */
 enum snd_device_type {
-	SNDRV_DEV_TOPLEVEL	= 0,
-	SNDRV_DEV_CONTROL	= 1,
-	SNDRV_DEV_LOWLEVEL_PRE	= 2,
-	SNDRV_DEV_LOWLEVEL_NORMAL = 0x1000,
+	SNDRV_DEV_CARD,
+	SNDRV_DEV_BUS,
+	SNDRV_DEV_CODEC,
+	SNDRV_DEV_CONTROL,
 	SNDRV_DEV_PCM,
+	SNDRV_DEV_COMPRESS,
 	SNDRV_DEV_RAWMIDI,
 	SNDRV_DEV_TIMER,
 	SNDRV_DEV_SEQUENCER,
 	SNDRV_DEV_HWDEP,
 	SNDRV_DEV_INFO,
-	SNDRV_DEV_BUS,
-	SNDRV_DEV_CODEC,
 	SNDRV_DEV_JACK,
-	SNDRV_DEV_COMPRESS,
-	SNDRV_DEV_LOWLEVEL	= 0x2000,
-	SNDRV_DEV_CARD = SNDRV_DEV_LOWLEVEL,
+	SNDRV_DEV_LOWLEVEL,
 };
 
 enum snd_device_state {
@@ -73,12 +71,6 @@ enum snd_device_state {
 	SNDRV_DEV_DISCONNECTED,
 };
 
-enum snd_device_cmd {
-	SNDRV_DEV_CMD_PRE,
-	SNDRV_DEV_CMD_NORMAL,
-	SNDRV_DEV_CMD_POST,
-};
-
 struct snd_device;
 
 struct snd_device_ops {
@@ -321,7 +313,7 @@ int snd_device_register_all(struct snd_card *card);
 int snd_device_disconnect(struct snd_card *card, void *device_data);
 int snd_device_disconnect_all(struct snd_card *card);
 int snd_device_free(struct snd_card *card, void *device_data);
-int snd_device_free_all(struct snd_card *card, enum snd_device_cmd cmd);
+int snd_device_free_all(struct snd_card *card);
 
 /* isadma.c */
 
diff --git a/sound/core/device.c b/sound/core/device.c
index 6f734fe50296..b6f9d6702c69 100644
--- a/sound/core/device.c
+++ b/sound/core/device.c
@@ -45,6 +45,7 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
 		   void *device_data, struct snd_device_ops *ops)
 {
 	struct snd_device *dev;
+	struct list_head *p;
 
 	if (snd_BUG_ON(!card || !device_data || !ops))
 		return -ENXIO;
@@ -53,17 +54,36 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
 		snd_printk(KERN_ERR "Cannot allocate device\n");
 		return -ENOMEM;
 	}
+	INIT_LIST_HEAD(&dev->list);
 	dev->card = card;
 	dev->type = type;
 	dev->state = SNDRV_DEV_BUILD;
 	dev->device_data = device_data;
 	dev->ops = ops;
-	list_add(&dev->list, &card->devices);	/* add to the head of list */
+
+	/* insert the entry in an incrementally sorted list */
+	list_for_each_prev(p, &card->devices) {
+		struct snd_device *pdev = list_entry(p, struct snd_device, list);
+		if ((unsigned int)pdev->type <= (unsigned int)type)
+			break;
+	}
+
+	list_add(&dev->list, p);
 	return 0;
 }
-
 EXPORT_SYMBOL(snd_device_new);
 
+static struct snd_device *look_for_dev(struct snd_card *card, void *device_data)
+{
+	struct snd_device *dev;
+
+	list_for_each_entry(dev, &card->devices, list)
+		if (dev->device_data == device_data)
+			return dev;
+
+	return NULL;
+}
+
 /**
  * snd_device_free - release the device from the card
  * @card: the card instance
@@ -82,9 +102,8 @@ int snd_device_free(struct snd_card *card, void *device_data)
 	
 	if (snd_BUG_ON(!card || !device_data))
 		return -ENXIO;
-	list_for_each_entry(dev, &card->devices, list) {
-		if (dev->device_data != device_data)
-			continue;
+	dev = look_for_dev(card, device_data);
+	if (dev) {
 		/* unlink */
 		list_del(&dev->list);
 		if (dev->state == SNDRV_DEV_REGISTERED &&
@@ -103,7 +122,6 @@ int snd_device_free(struct snd_card *card, void *device_data)
 		   __builtin_return_address(0));
 	return -ENXIO;
 }
-
 EXPORT_SYMBOL(snd_device_free);
 
 /**
@@ -125,9 +143,8 @@ int snd_device_disconnect(struct snd_card *card, void *device_data)
 
 	if (snd_BUG_ON(!card || !device_data))
 		return -ENXIO;
-	list_for_each_entry(dev, &card->devices, list) {
-		if (dev->device_data != device_data)
-			continue;
+	dev = look_for_dev(card, device_data);
+	if (dev) {
 		if (dev->state == SNDRV_DEV_REGISTERED &&
 		    dev->ops->dev_disconnect) {
 			if (dev->ops->dev_disconnect(dev))
@@ -161,9 +178,8 @@ int snd_device_register(struct snd_card *card, void *device_data)
 
 	if (snd_BUG_ON(!card || !device_data))
 		return -ENXIO;
-	list_for_each_entry(dev, &card->devices, list) {
-		if (dev->device_data != device_data)
-			continue;
+	dev = look_for_dev(card, device_data);
+	if (dev) {
 		if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
 			if ((err = dev->ops->dev_register(dev)) < 0)
 				return err;
@@ -176,7 +192,6 @@ int snd_device_register(struct snd_card *card, void *device_data)
 	snd_BUG();
 	return -ENXIO;
 }
-
 EXPORT_SYMBOL(snd_device_register);
 
 /*
@@ -211,7 +226,7 @@ int snd_device_disconnect_all(struct snd_card *card)
 
 	if (snd_BUG_ON(!card))
 		return -ENXIO;
-	list_for_each_entry(dev, &card->devices, list) {
+	list_for_each_entry_reverse(dev, &card->devices, list) {
 		if (snd_device_disconnect(card, dev->device_data) < 0)
 			err = -ENXIO;
 	}
@@ -222,24 +237,17 @@ int snd_device_disconnect_all(struct snd_card *card)
  * release all the devices on the card.
  * called from init.c
  */
-int snd_device_free_all(struct snd_card *card, enum snd_device_cmd cmd)
+int snd_device_free_all(struct snd_card *card)
 {
-	struct snd_device *dev;
-	int err;
-	unsigned int range_low, range_high, type;
+	struct snd_device *dev, *next;
+	int ret = 0;
 
 	if (snd_BUG_ON(!card))
 		return -ENXIO;
-	range_low = (unsigned int)cmd * SNDRV_DEV_TYPE_RANGE_SIZE;
-	range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1;
-      __again:
-	list_for_each_entry(dev, &card->devices, list) {
-		type = (unsigned int)dev->type;
-		if (type >= range_low && type <= range_high) {
-			if ((err = snd_device_free(card, dev->device_data)) < 0)
-				return err;
-			goto __again;
-		}
+	list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
+		int err = snd_device_free(card, dev->device_data);
+		if (err < 0)
+			ret = err;
 	}
-	return 0;
+	return ret;
 }
diff --git a/sound/core/init.c b/sound/core/init.c
index f4d3ac633ff8..0115034914c9 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -248,7 +248,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
 	return 0;
 
       __error_ctl:
-	snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
+	snd_device_free_all(card);
       __error:
 	kfree(card);
   	return err;
@@ -436,16 +436,8 @@ static int snd_card_do_free(struct snd_card *card)
 	if (snd_mixer_oss_notify_callback)
 		snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
 #endif
-	if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
-		snd_printk(KERN_ERR "unable to free all devices (pre)\n");
-		/* Fatal, but this situation should never occur */
-	}
-	if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
-		snd_printk(KERN_ERR "unable to free all devices (normal)\n");
-		/* Fatal, but this situation should never occur */
-	}
-	if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
-		snd_printk(KERN_ERR "unable to free all devices (post)\n");
+	if (snd_device_free_all(card) < 0) {
+		snd_printk(KERN_ERR "unable to free all devices\n");
 		/* Fatal, but this situation should never occur */
 	}
 	if (card->private_free)
-- 
1.8.5.2

  parent reply index

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-12 10:52 [PATCH RFC 00/20] snd_device_* cleanups Takashi Iwai
2014-02-12 10:52 ` [PATCH 01/20] ALSA: Drop __bitwise and typedefs for snd_device attributes Takashi Iwai
2014-02-12 10:52 ` [PATCH 02/20] ALSA: Add SNDRV_DEV_CARD type Takashi Iwai
2014-02-12 10:52 ` [PATCH 03/20] ALSA: pci: Use SNDRV_DEV_CARD for card objects Takashi Iwai
2014-02-12 10:52 ` [PATCH 04/20] ALSA: isa: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 05/20] ALSA: drivers: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 06/20] ALSA: pcmcia: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 07/20] ALSA: usb-audio: Use proper SNDRV_DEV_* type Takashi Iwai
2014-02-12 10:52 ` [PATCH 08/20] ALSA: aoa: Use SNDRV_DEV_CODEC for AOA codec objects Takashi Iwai
2014-02-12 10:52 ` [PATCH 09/20] ALSA: mips: Use SNDRV_DEV_CARD for card objects Takashi Iwai
2014-02-12 10:52 ` [PATCH 10/20] ALSA: parisc: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 11/20] ALSA: ppc: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 12/20] ALSA: sh: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 13/20] ALSA: sparc: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 14/20] ALSA: i2c/ak413x: Use SNDRV_DEV_CODEC for ak413x codec objects Takashi Iwai
2014-02-12 10:52 ` [PATCH 15/20] ALSA: spi: Use SNDRV_DEV_CARD for card objects Takashi Iwai
2014-02-12 10:52 ` [PATCH 16/20] HID: prodikeys: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 17/20] staging: " Takashi Iwai
2014-02-12 10:52 ` [PATCH 18/20] usb: gadget: " Takashi Iwai
2014-02-12 10:52 ` Takashi Iwai [this message]
2014-02-12 10:52 ` [PATCH 20/20] ALSA: Clean up snd_device_*() codes Takashi Iwai
2014-02-14 11:59 ` [PATCH RFC 00/20] snd_device_* cleanups Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1392202347-11774-20-git-send-email-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Alsa-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/alsa-devel/0 alsa-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 alsa-devel alsa-devel/ https://lore.kernel.org/alsa-devel \
		alsa-devel@alsa-project.org
	public-inbox-index alsa-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.alsa-project.alsa-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git