Commit 289ca025 authored by Takashi Iwai's avatar Takashi Iwai

ALSA: Use priority list for managing device list

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 the priorities of some object
types are revisited.  For example, now the SNDRV_DEV_LOWLEVEL object
is registered before others (control, PCM, etc) and, in return,
released after others.  Similarly, SNDRV_DEV_CODEC is in a lower
priority than SNDRV_DEV_BUS for ensuring the dependency.

Also, the unused SNDRV_DEV_TOPLEVEL, SNDRV_DEV_LOWLEVEL_PRE and
SNDRV_DEV_LOWLEVEL_NORMAL are removed as a cleanup.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 71e2e1c1
...@@ -46,24 +46,22 @@ struct completion; ...@@ -46,24 +46,22 @@ struct completion;
/* device allocation stuff */ /* 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 { enum snd_device_type {
SNDRV_DEV_TOPLEVEL = 0, SNDRV_DEV_LOWLEVEL,
SNDRV_DEV_CONTROL = 1, SNDRV_DEV_CONTROL,
SNDRV_DEV_LOWLEVEL_PRE = 2, SNDRV_DEV_INFO,
SNDRV_DEV_LOWLEVEL_NORMAL = 0x1000, SNDRV_DEV_BUS,
SNDRV_DEV_CODEC,
SNDRV_DEV_PCM, SNDRV_DEV_PCM,
SNDRV_DEV_COMPRESS,
SNDRV_DEV_RAWMIDI, SNDRV_DEV_RAWMIDI,
SNDRV_DEV_TIMER, SNDRV_DEV_TIMER,
SNDRV_DEV_SEQUENCER, SNDRV_DEV_SEQUENCER,
SNDRV_DEV_HWDEP, SNDRV_DEV_HWDEP,
SNDRV_DEV_INFO,
SNDRV_DEV_BUS,
SNDRV_DEV_CODEC,
SNDRV_DEV_JACK, SNDRV_DEV_JACK,
SNDRV_DEV_COMPRESS,
SNDRV_DEV_LOWLEVEL = 0x2000,
}; };
enum snd_device_state { enum snd_device_state {
...@@ -72,12 +70,6 @@ enum snd_device_state { ...@@ -72,12 +70,6 @@ enum snd_device_state {
SNDRV_DEV_DISCONNECTED, 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;
struct snd_device_ops { struct snd_device_ops {
...@@ -320,7 +312,7 @@ int snd_device_register_all(struct snd_card *card); ...@@ -320,7 +312,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(struct snd_card *card, void *device_data);
int snd_device_disconnect_all(struct snd_card *card); int snd_device_disconnect_all(struct snd_card *card);
int snd_device_free(struct snd_card *card, void *device_data); 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 */ /* isadma.c */
......
...@@ -45,6 +45,7 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type, ...@@ -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) void *device_data, struct snd_device_ops *ops)
{ {
struct snd_device *dev; struct snd_device *dev;
struct list_head *p;
if (snd_BUG_ON(!card || !device_data || !ops)) if (snd_BUG_ON(!card || !device_data || !ops))
return -ENXIO; return -ENXIO;
...@@ -53,17 +54,36 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type, ...@@ -53,17 +54,36 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
dev_err(card->dev, "Cannot allocate device, type=%d\n", type); dev_err(card->dev, "Cannot allocate device, type=%d\n", type);
return -ENOMEM; return -ENOMEM;
} }
INIT_LIST_HEAD(&dev->list);
dev->card = card; dev->card = card;
dev->type = type; dev->type = type;
dev->state = SNDRV_DEV_BUILD; dev->state = SNDRV_DEV_BUILD;
dev->device_data = device_data; dev->device_data = device_data;
dev->ops = ops; 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; return 0;
} }
EXPORT_SYMBOL(snd_device_new); 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 * snd_device_free - release the device from the card
* @card: the card instance * @card: the card instance
...@@ -82,9 +102,8 @@ int snd_device_free(struct snd_card *card, void *device_data) ...@@ -82,9 +102,8 @@ int snd_device_free(struct snd_card *card, void *device_data)
if (snd_BUG_ON(!card || !device_data)) if (snd_BUG_ON(!card || !device_data))
return -ENXIO; return -ENXIO;
list_for_each_entry(dev, &card->devices, list) { dev = look_for_dev(card, device_data);
if (dev->device_data != device_data) if (dev) {
continue;
/* unlink */ /* unlink */
list_del(&dev->list); list_del(&dev->list);
if (dev->state == SNDRV_DEV_REGISTERED && if (dev->state == SNDRV_DEV_REGISTERED &&
...@@ -103,7 +122,6 @@ int snd_device_free(struct snd_card *card, void *device_data) ...@@ -103,7 +122,6 @@ int snd_device_free(struct snd_card *card, void *device_data)
device_data, __builtin_return_address(0)); device_data, __builtin_return_address(0));
return -ENXIO; return -ENXIO;
} }
EXPORT_SYMBOL(snd_device_free); EXPORT_SYMBOL(snd_device_free);
/** /**
...@@ -125,9 +143,8 @@ int snd_device_disconnect(struct snd_card *card, void *device_data) ...@@ -125,9 +143,8 @@ int snd_device_disconnect(struct snd_card *card, void *device_data)
if (snd_BUG_ON(!card || !device_data)) if (snd_BUG_ON(!card || !device_data))
return -ENXIO; return -ENXIO;
list_for_each_entry(dev, &card->devices, list) { dev = look_for_dev(card, device_data);
if (dev->device_data != device_data) if (dev) {
continue;
if (dev->state == SNDRV_DEV_REGISTERED && if (dev->state == SNDRV_DEV_REGISTERED &&
dev->ops->dev_disconnect) { dev->ops->dev_disconnect) {
if (dev->ops->dev_disconnect(dev)) if (dev->ops->dev_disconnect(dev))
...@@ -162,9 +179,8 @@ int snd_device_register(struct snd_card *card, void *device_data) ...@@ -162,9 +179,8 @@ int snd_device_register(struct snd_card *card, void *device_data)
if (snd_BUG_ON(!card || !device_data)) if (snd_BUG_ON(!card || !device_data))
return -ENXIO; return -ENXIO;
list_for_each_entry(dev, &card->devices, list) { dev = look_for_dev(card, device_data);
if (dev->device_data != device_data) if (dev) {
continue;
if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) { if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
if ((err = dev->ops->dev_register(dev)) < 0) if ((err = dev->ops->dev_register(dev)) < 0)
return err; return err;
...@@ -177,7 +193,6 @@ int snd_device_register(struct snd_card *card, void *device_data) ...@@ -177,7 +193,6 @@ int snd_device_register(struct snd_card *card, void *device_data)
snd_BUG(); snd_BUG();
return -ENXIO; return -ENXIO;
} }
EXPORT_SYMBOL(snd_device_register); EXPORT_SYMBOL(snd_device_register);
/* /*
...@@ -212,7 +227,7 @@ int snd_device_disconnect_all(struct snd_card *card) ...@@ -212,7 +227,7 @@ int snd_device_disconnect_all(struct snd_card *card)
if (snd_BUG_ON(!card)) if (snd_BUG_ON(!card))
return -ENXIO; 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) if (snd_device_disconnect(card, dev->device_data) < 0)
err = -ENXIO; err = -ENXIO;
} }
...@@ -223,24 +238,17 @@ int snd_device_disconnect_all(struct snd_card *card) ...@@ -223,24 +238,17 @@ int snd_device_disconnect_all(struct snd_card *card)
* release all the devices on the card. * release all the devices on the card.
* called from init.c * 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; struct snd_device *dev, *next;
int err; int ret = 0;
unsigned int range_low, range_high, type;
if (snd_BUG_ON(!card)) if (snd_BUG_ON(!card))
return -ENXIO; return -ENXIO;
range_low = (unsigned int)cmd * SNDRV_DEV_TYPE_RANGE_SIZE; list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1; int err = snd_device_free(card, dev->device_data);
__again: if (err < 0)
list_for_each_entry(dev, &card->devices, list) { ret = err;
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;
}
} }
return 0; return ret;
} }
...@@ -266,7 +266,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid, ...@@ -266,7 +266,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
return 0; return 0;
__error_ctl: __error_ctl:
snd_device_free_all(card, SNDRV_DEV_CMD_PRE); snd_device_free_all(card);
__error: __error:
put_device(&card->card_dev); put_device(&card->card_dev);
return err; return err;
...@@ -454,16 +454,8 @@ static int snd_card_do_free(struct snd_card *card) ...@@ -454,16 +454,8 @@ static int snd_card_do_free(struct snd_card *card)
if (snd_mixer_oss_notify_callback) if (snd_mixer_oss_notify_callback)
snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE); snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
#endif #endif
if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) { if (snd_device_free_all(card) < 0) {
dev_err(card->dev, "unable to free all devices (pre)\n"); dev_err(card->dev, "unable to free all devices\n");
/* Fatal, but this situation should never occur */
}
if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
dev_err(card->dev, "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) {
dev_err(card->dev, "unable to free all devices (post)\n");
/* Fatal, but this situation should never occur */ /* Fatal, but this situation should never occur */
} }
if (card->private_free) if (card->private_free)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment