Skip to content

Commit 289ca02

Browse files
committed
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: Takashi Iwai <tiwai@suse.de>
1 parent 71e2e1c commit 289ca02

3 files changed

Lines changed: 50 additions & 58 deletions

File tree

‎include/sound/core.h‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,22 @@ struct completion;
4646

4747
/* device allocation stuff */
4848

49-
#define SNDRV_DEV_TYPE_RANGE_SIZE 0x1000
50-
49+
/* type of the object used in snd_device_*()
50+
* this also defines the calling order
51+
*/
5152
enum snd_device_type {
52-
SNDRV_DEV_TOPLEVEL = 0,
53-
SNDRV_DEV_CONTROL = 1,
54-
SNDRV_DEV_LOWLEVEL_PRE = 2,
55-
SNDRV_DEV_LOWLEVEL_NORMAL = 0x1000,
53+
SNDRV_DEV_LOWLEVEL,
54+
SNDRV_DEV_CONTROL,
55+
SNDRV_DEV_INFO,
56+
SNDRV_DEV_BUS,
57+
SNDRV_DEV_CODEC,
5658
SNDRV_DEV_PCM,
59+
SNDRV_DEV_COMPRESS,
5760
SNDRV_DEV_RAWMIDI,
5861
SNDRV_DEV_TIMER,
5962
SNDRV_DEV_SEQUENCER,
6063
SNDRV_DEV_HWDEP,
61-
SNDRV_DEV_INFO,
62-
SNDRV_DEV_BUS,
63-
SNDRV_DEV_CODEC,
6464
SNDRV_DEV_JACK,
65-
SNDRV_DEV_COMPRESS,
66-
SNDRV_DEV_LOWLEVEL = 0x2000,
6765
};
6866

6967
enum snd_device_state {
@@ -72,12 +70,6 @@ enum snd_device_state {
7270
SNDRV_DEV_DISCONNECTED,
7371
};
7472

75-
enum snd_device_cmd {
76-
SNDRV_DEV_CMD_PRE,
77-
SNDRV_DEV_CMD_NORMAL,
78-
SNDRV_DEV_CMD_POST,
79-
};
80-
8173
struct snd_device;
8274

8375
struct snd_device_ops {
@@ -320,7 +312,7 @@ int snd_device_register_all(struct snd_card *card);
320312
int snd_device_disconnect(struct snd_card *card, void *device_data);
321313
int snd_device_disconnect_all(struct snd_card *card);
322314
int snd_device_free(struct snd_card *card, void *device_data);
323-
int snd_device_free_all(struct snd_card *card, enum snd_device_cmd cmd);
315+
int snd_device_free_all(struct snd_card *card);
324316

325317
/* isadma.c */
326318

‎sound/core/device.c‎

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
4545
void *device_data, struct snd_device_ops *ops)
4646
{
4747
struct snd_device *dev;
48+
struct list_head *p;
4849

4950
if (snd_BUG_ON(!card || !device_data || !ops))
5051
return -ENXIO;
@@ -53,17 +54,36 @@ int snd_device_new(struct snd_card *card, enum snd_device_type type,
5354
dev_err(card->dev, "Cannot allocate device, type=%d\n", type);
5455
return -ENOMEM;
5556
}
57+
INIT_LIST_HEAD(&dev->list);
5658
dev->card = card;
5759
dev->type = type;
5860
dev->state = SNDRV_DEV_BUILD;
5961
dev->device_data = device_data;
6062
dev->ops = ops;
61-
list_add(&dev->list, &card->devices); /* add to the head of list */
63+
64+
/* insert the entry in an incrementally sorted list */
65+
list_for_each_prev(p, &card->devices) {
66+
struct snd_device *pdev = list_entry(p, struct snd_device, list);
67+
if ((unsigned int)pdev->type <= (unsigned int)type)
68+
break;
69+
}
70+
71+
list_add(&dev->list, p);
6272
return 0;
6373
}
64-
6574
EXPORT_SYMBOL(snd_device_new);
6675

76+
static struct snd_device *look_for_dev(struct snd_card *card, void *device_data)
77+
{
78+
struct snd_device *dev;
79+
80+
list_for_each_entry(dev, &card->devices, list)
81+
if (dev->device_data == device_data)
82+
return dev;
83+
84+
return NULL;
85+
}
86+
6787
/**
6888
* snd_device_free - release the device from the card
6989
* @card: the card instance
@@ -82,9 +102,8 @@ int snd_device_free(struct snd_card *card, void *device_data)
82102

83103
if (snd_BUG_ON(!card || !device_data))
84104
return -ENXIO;
85-
list_for_each_entry(dev, &card->devices, list) {
86-
if (dev->device_data != device_data)
87-
continue;
105+
dev = look_for_dev(card, device_data);
106+
if (dev) {
88107
/* unlink */
89108
list_del(&dev->list);
90109
if (dev->state == SNDRV_DEV_REGISTERED &&
@@ -103,7 +122,6 @@ int snd_device_free(struct snd_card *card, void *device_data)
103122
device_data, __builtin_return_address(0));
104123
return -ENXIO;
105124
}
106-
107125
EXPORT_SYMBOL(snd_device_free);
108126

109127
/**
@@ -125,9 +143,8 @@ int snd_device_disconnect(struct snd_card *card, void *device_data)
125143

126144
if (snd_BUG_ON(!card || !device_data))
127145
return -ENXIO;
128-
list_for_each_entry(dev, &card->devices, list) {
129-
if (dev->device_data != device_data)
130-
continue;
146+
dev = look_for_dev(card, device_data);
147+
if (dev) {
131148
if (dev->state == SNDRV_DEV_REGISTERED &&
132149
dev->ops->dev_disconnect) {
133150
if (dev->ops->dev_disconnect(dev))
@@ -162,9 +179,8 @@ int snd_device_register(struct snd_card *card, void *device_data)
162179

163180
if (snd_BUG_ON(!card || !device_data))
164181
return -ENXIO;
165-
list_for_each_entry(dev, &card->devices, list) {
166-
if (dev->device_data != device_data)
167-
continue;
182+
dev = look_for_dev(card, device_data);
183+
if (dev) {
168184
if (dev->state == SNDRV_DEV_BUILD && dev->ops->dev_register) {
169185
if ((err = dev->ops->dev_register(dev)) < 0)
170186
return err;
@@ -177,7 +193,6 @@ int snd_device_register(struct snd_card *card, void *device_data)
177193
snd_BUG();
178194
return -ENXIO;
179195
}
180-
181196
EXPORT_SYMBOL(snd_device_register);
182197

183198
/*
@@ -212,7 +227,7 @@ int snd_device_disconnect_all(struct snd_card *card)
212227

213228
if (snd_BUG_ON(!card))
214229
return -ENXIO;
215-
list_for_each_entry(dev, &card->devices, list) {
230+
list_for_each_entry_reverse(dev, &card->devices, list) {
216231
if (snd_device_disconnect(card, dev->device_data) < 0)
217232
err = -ENXIO;
218233
}
@@ -223,24 +238,17 @@ int snd_device_disconnect_all(struct snd_card *card)
223238
* release all the devices on the card.
224239
* called from init.c
225240
*/
226-
int snd_device_free_all(struct snd_card *card, enum snd_device_cmd cmd)
241+
int snd_device_free_all(struct snd_card *card)
227242
{
228-
struct snd_device *dev;
229-
int err;
230-
unsigned int range_low, range_high, type;
243+
struct snd_device *dev, *next;
244+
int ret = 0;
231245

232246
if (snd_BUG_ON(!card))
233247
return -ENXIO;
234-
range_low = (unsigned int)cmd * SNDRV_DEV_TYPE_RANGE_SIZE;
235-
range_high = range_low + SNDRV_DEV_TYPE_RANGE_SIZE - 1;
236-
__again:
237-
list_for_each_entry(dev, &card->devices, list) {
238-
type = (unsigned int)dev->type;
239-
if (type >= range_low && type <= range_high) {
240-
if ((err = snd_device_free(card, dev->device_data)) < 0)
241-
return err;
242-
goto __again;
243-
}
248+
list_for_each_entry_safe_reverse(dev, next, &card->devices, list) {
249+
int err = snd_device_free(card, dev->device_data);
250+
if (err < 0)
251+
ret = err;
244252
}
245-
return 0;
253+
return ret;
246254
}

‎sound/core/init.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
266266
return 0;
267267

268268
__error_ctl:
269-
snd_device_free_all(card, SNDRV_DEV_CMD_PRE);
269+
snd_device_free_all(card);
270270
__error:
271271
put_device(&card->card_dev);
272272
return err;
@@ -454,16 +454,8 @@ static int snd_card_do_free(struct snd_card *card)
454454
if (snd_mixer_oss_notify_callback)
455455
snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
456456
#endif
457-
if (snd_device_free_all(card, SNDRV_DEV_CMD_PRE) < 0) {
458-
dev_err(card->dev, "unable to free all devices (pre)\n");
459-
/* Fatal, but this situation should never occur */
460-
}
461-
if (snd_device_free_all(card, SNDRV_DEV_CMD_NORMAL) < 0) {
462-
dev_err(card->dev, "unable to free all devices (normal)\n");
463-
/* Fatal, but this situation should never occur */
464-
}
465-
if (snd_device_free_all(card, SNDRV_DEV_CMD_POST) < 0) {
466-
dev_err(card->dev, "unable to free all devices (post)\n");
457+
if (snd_device_free_all(card) < 0) {
458+
dev_err(card->dev, "unable to free all devices\n");
467459
/* Fatal, but this situation should never occur */
468460
}
469461
if (card->private_free)

0 commit comments

Comments
 (0)