Skip to content

Commit 6983b72

Browse files
cladischJaroslav Kysela
authored andcommitted
[ALSA] dynamic minors (2/6): simplify storage of snd_minor structures
Modules: ALSA Core Store the snd_minor structure pointers in one array instead of using a separate list for each card. This simplifies the mapping from device files to minor struct by removing the need to know about the encoding of the card number in the minor number. Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
1 parent 2af677f commit 6983b72

3 files changed

Lines changed: 49 additions & 92 deletions

File tree

‎include/sound/core.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ static inline int snd_power_wait(struct snd_card *card, unsigned int state, stru
183183
#endif /* CONFIG_PM */
184184

185185
struct snd_minor {
186-
struct list_head list; /* list of all minors per card */
187-
int number; /* minor number */
188186
int type; /* SNDRV_DEVICE_TYPE_XXX */
187+
int card; /* card number */
189188
int device; /* device number */
190189
struct file_operations *f_ops; /* file operations */
191190
char name[0]; /* device name (keep at the end of
@@ -217,11 +216,9 @@ int snd_minor_info_done(void);
217216
#ifdef CONFIG_SND_OSSEMUL
218217
int snd_minor_info_oss_init(void);
219218
int snd_minor_info_oss_done(void);
220-
int snd_oss_init_module(void);
221219
#else
222220
#define snd_minor_info_oss_init() /*NOP*/
223221
#define snd_minor_info_oss_done() /*NOP*/
224-
#define snd_oss_init_module() 0
225222
#endif
226223

227224
/* memory.c */

‎sound/core/sound.c‎

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
5959
*/
6060
int snd_ecards_limit;
6161

62-
static struct list_head snd_minors_hash[SNDRV_CARDS];
62+
static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
6363

6464
static DECLARE_MUTEX(sound_mutex);
6565

@@ -107,19 +107,6 @@ static void snd_request_other(int minor)
107107

108108
#endif /* request_module support */
109109

110-
static struct snd_minor *snd_minor_search(int minor)
111-
{
112-
struct list_head *list;
113-
struct snd_minor *mptr;
114-
115-
list_for_each(list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]) {
116-
mptr = list_entry(list, struct snd_minor, list);
117-
if (mptr->number == minor)
118-
return mptr;
119-
}
120-
return NULL;
121-
}
122-
123110
static int snd_open(struct inode *inode, struct file *file)
124111
{
125112
int minor = iminor(inode);
@@ -139,11 +126,11 @@ static int snd_open(struct inode *inode, struct file *file)
139126
}
140127
} else {
141128
#ifdef CONFIG_KMOD
142-
if ((mptr = snd_minor_search(minor)) == NULL)
129+
if ((mptr = snd_minors[minor]) == NULL)
143130
snd_request_other(minor);
144131
#endif
145132
}
146-
if (mptr == NULL && (mptr = snd_minor_search(minor)) == NULL)
133+
if (mptr == NULL && (mptr = snd_minors[minor]) == NULL)
147134
return -ENODEV;
148135
old_fops = file->f_op;
149136
file->f_op = fops_get(mptr->f_ops);
@@ -213,22 +200,22 @@ int snd_register_device(int type, struct snd_card *card, int dev,
213200
if (minor < 0)
214201
return minor;
215202
snd_assert(name, return -EINVAL);
216-
preg = kzalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
203+
preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
217204
if (preg == NULL)
218205
return -ENOMEM;
219-
preg->number = minor;
220206
preg->type = type;
207+
preg->card = card ? card->number : -1;
221208
preg->device = dev;
222209
preg->f_ops = f_ops;
223210
strcpy(preg->name, name);
224211
down(&sound_mutex);
225-
if (snd_minor_search(minor)) {
212+
if (snd_minors[minor]) {
226213
up(&sound_mutex);
227214
kfree(preg);
228215
return -EBUSY;
229216
}
230-
list_add_tail(&preg->list, &snd_minors_hash[SNDRV_MINOR_CARD(minor)]);
231-
if (strncmp(name, "controlC", 8) || card->number >= cards_limit)
217+
snd_minors[minor] = preg;
218+
if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
232219
devfs_mk_cdev(MKDEV(major, minor), S_IFCHR | device_mode, "snd/%s", name);
233220
if (card)
234221
device = card->dev;
@@ -257,16 +244,17 @@ int snd_unregister_device(int type, struct snd_card *card, int dev)
257244
if (minor < 0)
258245
return minor;
259246
down(&sound_mutex);
260-
if ((mptr = snd_minor_search(minor)) == NULL) {
247+
if ((mptr = snd_minors[minor]) == NULL) {
261248
up(&sound_mutex);
262249
return -EINVAL;
263250
}
264251

265-
if (strncmp(mptr->name, "controlC", 8) || card->number >= cards_limit) /* created in sound.c */
252+
if (mptr->type != SNDRV_DEVICE_TYPE_CONTROL ||
253+
mptr->card >= cards_limit) /* created in sound.c */
266254
devfs_remove("snd/%s", mptr->name);
267255
class_device_destroy(sound_class, MKDEV(major, minor));
268256

269-
list_del(&mptr->list);
257+
snd_minors[minor] = NULL;
270258
up(&sound_mutex);
271259
kfree(mptr);
272260
return 0;
@@ -302,23 +290,25 @@ static const char *snd_device_type_name(int type)
302290

303291
static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
304292
{
305-
int card, device;
306-
struct list_head *list;
293+
int minor;
307294
struct snd_minor *mptr;
308295

309296
down(&sound_mutex);
310-
for (card = 0; card < SNDRV_CARDS; card++) {
311-
list_for_each(list, &snd_minors_hash[card]) {
312-
mptr = list_entry(list, struct snd_minor, list);
313-
if (SNDRV_MINOR_DEVICE(mptr->number) != SNDRV_MINOR_GLOBAL) {
314-
if ((device = mptr->device) >= 0)
315-
snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, device, snd_device_type_name(mptr->type));
316-
else
317-
snd_iprintf(buffer, "%3i: [%i] : %s\n", mptr->number, card, snd_device_type_name(mptr->type));
318-
} else {
319-
snd_iprintf(buffer, "%3i: : %s\n", mptr->number, snd_device_type_name(mptr->type));
320-
}
321-
}
297+
for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
298+
if (!(mptr = snd_minors[minor]))
299+
continue;
300+
if (mptr->card >= 0) {
301+
if (mptr->device >= 0)
302+
snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n",
303+
minor, mptr->card, mptr->device,
304+
snd_device_type_name(mptr->type));
305+
else
306+
snd_iprintf(buffer, "%3i: [%i] : %s\n",
307+
minor, mptr->card,
308+
snd_device_type_name(mptr->type));
309+
} else
310+
snd_iprintf(buffer, "%3i: : %s\n", minor,
311+
snd_device_type_name(mptr->type));
322312
}
323313
up(&sound_mutex);
324314
}
@@ -354,15 +344,9 @@ int __exit snd_minor_info_done(void)
354344
static int __init alsa_sound_init(void)
355345
{
356346
short controlnum;
357-
int err;
358-
int card;
359347

360348
snd_major = major;
361349
snd_ecards_limit = cards_limit;
362-
for (card = 0; card < SNDRV_CARDS; card++)
363-
INIT_LIST_HEAD(&snd_minors_hash[card]);
364-
if ((err = snd_oss_init_module()) < 0)
365-
return err;
366350
devfs_mk_dir("snd");
367351
if (register_chrdev(major, "alsa", &snd_fops)) {
368352
snd_printk(KERN_ERR "unable to register native major device number %d\n", major);

‎sound/core/sound_oss.c‎

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,12 @@
3535
#include <sound/info.h>
3636
#include <linux/sound.h>
3737

38-
#define SNDRV_OS_MINORS 256
38+
#define SNDRV_OSS_MINORS 128
3939

40-
static struct list_head snd_oss_minors_hash[SNDRV_CARDS];
40+
static struct snd_minor *snd_oss_minors[SNDRV_OSS_MINORS];
4141

4242
static DECLARE_MUTEX(sound_oss_mutex);
4343

44-
static struct snd_minor *snd_oss_minor_search(int minor)
45-
{
46-
struct list_head *list;
47-
struct snd_minor *mptr;
48-
49-
list_for_each(list, &snd_oss_minors_hash[SNDRV_MINOR_OSS_CARD(minor)]) {
50-
mptr = list_entry(list, struct snd_minor, list);
51-
if (mptr->number == minor)
52-
return mptr;
53-
}
54-
return NULL;
55-
}
56-
5744
static int snd_oss_kernel_minor(int type, struct snd_card *card, int dev)
5845
{
5946
int minor;
@@ -86,7 +73,7 @@ static int snd_oss_kernel_minor(int type, struct snd_card *card, int dev)
8673
default:
8774
return -EINVAL;
8875
}
89-
snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
76+
snd_assert(minor >= 0 && minor < SNDRV_OSS_MINORS, return -EINVAL);
9077
return minor;
9178
}
9279

@@ -103,15 +90,15 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev,
10390

10491
if (minor < 0)
10592
return minor;
106-
preg = kzalloc(sizeof(struct snd_minor), GFP_KERNEL);
93+
preg = kmalloc(sizeof(struct snd_minor), GFP_KERNEL);
10794
if (preg == NULL)
10895
return -ENOMEM;
109-
preg->number = minor;
11096
preg->type = type;
97+
preg->card = card ? card->number : -1;
11198
preg->device = dev;
11299
preg->f_ops = f_ops;
113100
down(&sound_oss_mutex);
114-
list_add_tail(&preg->list, &snd_oss_minors_hash[cidx]);
101+
snd_oss_minors[minor] = preg;
115102
minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
116103
switch (minor_unit) {
117104
case SNDRV_MINOR_OSS_PCM:
@@ -143,7 +130,7 @@ int snd_register_oss_device(int type, struct snd_card *card, int dev,
143130
unregister_sound_special(register2);
144131
if (register1 >= 0)
145132
unregister_sound_special(register1);
146-
list_del(&preg->list);
133+
snd_oss_minors[minor] = NULL;
147134
up(&sound_oss_mutex);
148135
kfree(preg);
149136
return -EBUSY;
@@ -159,7 +146,7 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
159146
if (minor < 0)
160147
return minor;
161148
down(&sound_oss_mutex);
162-
mptr = snd_oss_minor_search(minor);
149+
mptr = snd_oss_minors[minor];
163150
if (mptr == NULL) {
164151
up(&sound_oss_mutex);
165152
return -ENOENT;
@@ -178,7 +165,7 @@ int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
178165
}
179166
if (track2 >= 0)
180167
unregister_sound_special(track2);
181-
list_del(&mptr->list);
168+
snd_oss_minors[minor] = NULL;
182169
up(&sound_oss_mutex);
183170
kfree(mptr);
184171
return 0;
@@ -214,22 +201,20 @@ static const char *snd_oss_device_type_name(int type)
214201
static void snd_minor_info_oss_read(struct snd_info_entry *entry,
215202
struct snd_info_buffer *buffer)
216203
{
217-
int card, dev;
218-
struct list_head *list;
204+
int minor;
219205
struct snd_minor *mptr;
220206

221207
down(&sound_oss_mutex);
222-
for (card = 0; card < SNDRV_CARDS; card++) {
223-
list_for_each(list, &snd_oss_minors_hash[card]) {
224-
mptr = list_entry(list, struct snd_minor, list);
225-
dev = SNDRV_MINOR_OSS_DEVICE(mptr->number);
226-
if (dev != SNDRV_MINOR_OSS_SNDSTAT &&
227-
dev != SNDRV_MINOR_OSS_SEQUENCER &&
228-
dev != SNDRV_MINOR_OSS_MUSIC)
229-
snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, dev, snd_oss_device_type_name(mptr->type));
230-
else
231-
snd_iprintf(buffer, "%3i: : %s\n", mptr->number, snd_oss_device_type_name(mptr->type));
232-
}
208+
for (minor = 0; minor < SNDRV_OSS_MINORS; ++minor) {
209+
if (!(mptr = snd_oss_minors[minor]))
210+
continue;
211+
if (mptr->card >= 0)
212+
snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", minor,
213+
mptr->card, mptr->device,
214+
snd_oss_device_type_name(mptr->type));
215+
else
216+
snd_iprintf(buffer, "%3i: : %s\n", minor,
217+
snd_oss_device_type_name(mptr->type));
233218
}
234219
up(&sound_oss_mutex);
235220
}
@@ -264,13 +249,4 @@ int __exit snd_minor_info_oss_done(void)
264249
return 0;
265250
}
266251

267-
int __init snd_oss_init_module(void)
268-
{
269-
int card;
270-
271-
for (card = 0; card < SNDRV_CARDS; card++)
272-
INIT_LIST_HEAD(&snd_oss_minors_hash[card]);
273-
return 0;
274-
}
275-
276252
#endif /* CONFIG_SND_OSSEMUL */

0 commit comments

Comments
 (0)