Skip to content

Commit 332682b

Browse files
cladischJaroslav Kysela
authored andcommitted
[ALSA] dynamic minors (4/6): dynamic minor number allocation
Modules: ALSA Core,ALSA Minor Numbers Add an option to allocate device file minor numbers dynamically. Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
1 parent f87135f commit 332682b

3 files changed

Lines changed: 82 additions & 23 deletions

File tree

‎include/sound/minors.h‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626
#define SNDRV_MINOR_DEVICE(minor) ((minor) & 0x001f)
2727
#define SNDRV_MINOR(card, dev) (((card) << 5) | (dev))
2828

29-
#define SNDRV_MINOR_CONTROL 0 /* 0 - 0 */
29+
/* these minors can still be used for autoloading devices (/dev/aload*) */
30+
#define SNDRV_MINOR_CONTROL 0 /* 0 */
3031
#define SNDRV_MINOR_GLOBAL 1 /* 1 */
3132
#define SNDRV_MINOR_SEQUENCER (SNDRV_MINOR_GLOBAL + 0 * 32)
3233
#define SNDRV_MINOR_TIMER (SNDRV_MINOR_GLOBAL + 1 * 32)
34+
35+
#ifndef CONFIG_SND_DYNAMIC_MINORS
36+
/* 2 - 3 (reserved) */
3337
#define SNDRV_MINOR_HWDEP 4 /* 4 - 7 */
34-
#define SNDRV_MINOR_HWDEPS 4
3538
#define SNDRV_MINOR_RAWMIDI 8 /* 8 - 15 */
36-
#define SNDRV_MINOR_RAWMIDIS 8
3739
#define SNDRV_MINOR_PCM_PLAYBACK 16 /* 16 - 23 */
3840
#define SNDRV_MINOR_PCM_CAPTURE 24 /* 24 - 31 */
39-
#define SNDRV_MINOR_PCMS 8
4041

42+
/* same as first respective minor number to make minor allocation easier */
4143
#define SNDRV_DEVICE_TYPE_CONTROL SNDRV_MINOR_CONTROL
4244
#define SNDRV_DEVICE_TYPE_HWDEP SNDRV_MINOR_HWDEP
4345
#define SNDRV_DEVICE_TYPE_RAWMIDI SNDRV_MINOR_RAWMIDI
@@ -46,6 +48,25 @@
4648
#define SNDRV_DEVICE_TYPE_SEQUENCER SNDRV_MINOR_SEQUENCER
4749
#define SNDRV_DEVICE_TYPE_TIMER SNDRV_MINOR_TIMER
4850

51+
#else /* CONFIG_SND_DYNAMIC_MINORS */
52+
53+
enum {
54+
SNDRV_DEVICE_TYPE_CONTROL,
55+
SNDRV_DEVICE_TYPE_SEQUENCER,
56+
SNDRV_DEVICE_TYPE_TIMER,
57+
SNDRV_DEVICE_TYPE_HWDEP,
58+
SNDRV_DEVICE_TYPE_RAWMIDI,
59+
SNDRV_DEVICE_TYPE_PCM_PLAYBACK,
60+
SNDRV_DEVICE_TYPE_PCM_CAPTURE,
61+
};
62+
63+
#endif /* CONFIG_SND_DYNAMIC_MINORS */
64+
65+
#define SNDRV_MINOR_HWDEPS 4
66+
#define SNDRV_MINOR_RAWMIDIS 8
67+
#define SNDRV_MINOR_PCMS 8
68+
69+
4970
#ifdef CONFIG_SND_OSSEMUL
5071

5172
#define SNDRV_MINOR_OSS_DEVICES 16

‎sound/core/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ config SND_SEQ_RTCTIMER_DEFAULT
111111

112112
If in doubt, say Y.
113113

114+
config SND_DYNAMIC_MINORS
115+
bool "Dynamic device file minor numbers (EXPERIMENTAL)"
116+
depends on SND && EXPERIMENTAL
117+
help
118+
If you say Y here, the minor numbers of ALSA device files in
119+
/dev/snd/ are allocated dynamically. This allows you to have
120+
more than 8 sound cards, but requires a dynamic device file
121+
system like udev.
122+
123+
If you are unsure about this, say N here.
124+
114125
config SND_VERBOSE_PRINTK
115126
bool "Verbose printk"
116127
depends on SND

‎sound/core/sound.c‎

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,34 @@ void *snd_lookup_minor_data(unsigned int minor, int type)
133133

134134
static int snd_open(struct inode *inode, struct file *file)
135135
{
136-
int minor = iminor(inode);
137-
int card = SNDRV_MINOR_CARD(minor);
138-
int dev = SNDRV_MINOR_DEVICE(minor);
136+
unsigned int minor = iminor(inode);
139137
struct snd_minor *mptr = NULL;
140138
struct file_operations *old_fops;
141139
int err = 0;
142140

143-
if (dev != SNDRV_MINOR_GLOBAL) {
144-
if (snd_cards[card] == NULL) {
141+
if (minor > ARRAY_SIZE(snd_minors))
142+
return -ENODEV;
143+
mptr = snd_minors[minor];
144+
if (mptr == NULL) {
145145
#ifdef CONFIG_KMOD
146-
snd_request_card(card);
146+
int dev = SNDRV_MINOR_DEVICE(minor);
147+
if (dev == SNDRV_MINOR_CONTROL) {
148+
/* /dev/aloadC? */
149+
int card = SNDRV_MINOR_CARD(minor);
147150
if (snd_cards[card] == NULL)
148-
#endif
149-
return -ENODEV;
150-
}
151-
} else {
152-
#ifdef CONFIG_KMOD
153-
if ((mptr = snd_minors[minor]) == NULL)
151+
snd_request_card(card);
152+
} else if (dev == SNDRV_MINOR_GLOBAL) {
153+
/* /dev/aloadSEQ */
154154
snd_request_other(minor);
155+
}
156+
#ifndef CONFIG_SND_DYNAMIC_MINORS
157+
/* /dev/snd/{controlC?,seq} */
158+
mptr = snd_minors[minor];
159+
if (mptr == NULL)
160+
#endif
155161
#endif
162+
return -ENODEV;
156163
}
157-
if (mptr == NULL && (mptr = snd_minors[minor]) == NULL)
158-
return -ENODEV;
159164
old_fops = file->f_op;
160165
file->f_op = fops_get(mptr->f_ops);
161166
if (file->f_op->open)
@@ -174,6 +179,22 @@ static struct file_operations snd_fops =
174179
.open = snd_open
175180
};
176181

182+
#ifdef CONFIG_SND_DYNAMIC_MINORS
183+
static int snd_find_free_minor(void)
184+
{
185+
int minor;
186+
187+
for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
188+
/* skip minors still used statically for autoloading devices */
189+
if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL ||
190+
minor == SNDRV_MINOR_SEQUENCER)
191+
continue;
192+
if (!snd_minors[minor])
193+
return minor;
194+
}
195+
return -EBUSY;
196+
}
197+
#else
177198
static int snd_kernel_minor(int type, struct snd_card *card, int dev)
178199
{
179200
int minor;
@@ -200,6 +221,7 @@ static int snd_kernel_minor(int type, struct snd_card *card, int dev)
200221
snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
201222
return minor;
202223
}
224+
#endif
203225

204226
/**
205227
* snd_register_device - Register the ALSA device file for the card
@@ -219,12 +241,10 @@ int snd_register_device(int type, struct snd_card *card, int dev,
219241
struct file_operations *f_ops, void *private_data,
220242
const char *name)
221243
{
222-
int minor = snd_kernel_minor(type, card, dev);
244+
int minor;
223245
struct snd_minor *preg;
224246
struct device *device = NULL;
225247

226-
if (minor < 0)
227-
return minor;
228248
snd_assert(name, return -EINVAL);
229249
preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
230250
if (preg == NULL)
@@ -236,10 +256,17 @@ int snd_register_device(int type, struct snd_card *card, int dev,
236256
preg->private_data = private_data;
237257
strcpy(preg->name, name);
238258
down(&sound_mutex);
239-
if (snd_minors[minor]) {
259+
#ifdef CONFIG_SND_DYNAMIC_MINORS
260+
minor = snd_find_free_minor();
261+
#else
262+
minor = snd_kernel_minor(type, card, dev);
263+
if (minor >= 0 && snd_minors[minor])
264+
minor = -EBUSY;
265+
#endif
266+
if (minor < 0) {
240267
up(&sound_mutex);
241268
kfree(preg);
242-
return -EBUSY;
269+
return minor;
243270
}
244271
snd_minors[minor] = preg;
245272
if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)

0 commit comments

Comments
 (0)