Skip to content

Commit f87135f

Browse files
cladischJaroslav Kysela
authored andcommitted
[ALSA] dynamic minors (3/6): store device-specific object pointers dynamically
Instead of storing the pointers to the device-specific structures in an array, put them into the struct snd_minor, and look them up dynamically. This makes the device type modules independent of the minor number encoding. Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
1 parent 6983b72 commit f87135f

17 files changed

Lines changed: 278 additions & 197 deletions

File tree

‎include/sound/core.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ struct snd_minor {
187187
int card; /* card number */
188188
int device; /* device number */
189189
struct file_operations *f_ops; /* file operations */
190+
void *private_data; /* private data for f_ops->open */
190191
char name[0]; /* device name (keep at the end of
191192
structure) */
192193
};
@@ -199,13 +200,17 @@ extern int snd_ecards_limit;
199200
void snd_request_card(int card);
200201

201202
int snd_register_device(int type, struct snd_card *card, int dev,
202-
struct file_operations *f_ops, const char *name);
203+
struct file_operations *f_ops, void *private_data,
204+
const char *name);
203205
int snd_unregister_device(int type, struct snd_card *card, int dev);
206+
void *snd_lookup_minor_data(unsigned int minor, int type);
204207

205208
#ifdef CONFIG_SND_OSSEMUL
206209
int snd_register_oss_device(int type, struct snd_card *card, int dev,
207-
struct file_operations *f_ops, const char *name);
210+
struct file_operations *f_ops, void *private_data,
211+
const char *name);
208212
int snd_unregister_oss_device(int type, struct snd_card *card, int dev);
213+
void *snd_lookup_oss_minor_data(unsigned int minor, int type);
209214
#endif
210215

211216
int snd_minor_info_init(void);

‎include/sound/hwdep.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct snd_hwdep_ops {
4343

4444
struct snd_hwdep {
4545
struct snd_card *card;
46+
struct list_head list;
4647
int device;
4748
char id[32];
4849
char name[80];

‎include/sound/pcm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ struct snd_pcm_str {
412412

413413
struct snd_pcm {
414414
struct snd_card *card;
415+
struct list_head list;
415416
unsigned int device; /* device number */
416417
unsigned int info_flags;
417418
unsigned short dev_class;
@@ -439,7 +440,6 @@ struct snd_pcm_notify {
439440
* Registering
440441
*/
441442

442-
extern struct snd_pcm *snd_pcm_devices[];
443443
extern struct file_operations snd_pcm_f_ops[2];
444444

445445
int snd_pcm_new(struct snd_card *card, char *id, int device,

‎include/sound/rawmidi.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct snd_rawmidi_str {
113113

114114
struct snd_rawmidi {
115115
struct snd_card *card;
116-
116+
struct list_head list;
117117
unsigned int device; /* device number */
118118
unsigned int info_flags; /* SNDRV_RAWMIDI_INFO_XXXX */
119119
char id[64];
@@ -165,8 +165,8 @@ int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
165165
/* main midi functions */
166166

167167
int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info);
168-
int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice, int mode,
169-
struct snd_rawmidi_file *rfile);
168+
int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
169+
int mode, struct snd_rawmidi_file *rfile);
170170
int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile);
171171
int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
172172
struct snd_rawmidi_params *params);

‎sound/core/control.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ static LIST_HEAD(snd_control_compat_ioctls);
4747

4848
static int snd_ctl_open(struct inode *inode, struct file *file)
4949
{
50-
int cardnum = SNDRV_MINOR_CARD(iminor(inode));
5150
unsigned long flags;
5251
struct snd_card *card;
5352
struct snd_ctl_file *ctl;
5453
int err;
5554

56-
card = snd_cards[cardnum];
55+
card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
5756
if (!card) {
5857
err = -ENODEV;
5958
goto __error1;
@@ -1277,8 +1276,8 @@ static int snd_ctl_dev_register(struct snd_device *device)
12771276
cardnum = card->number;
12781277
snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
12791278
sprintf(name, "controlC%i", cardnum);
1280-
if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL,
1281-
card, -1, &snd_ctl_f_ops, name)) < 0)
1279+
if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1280+
&snd_ctl_f_ops, card, name)) < 0)
12821281
return err;
12831282
return 0;
12841283
}

‎sound/core/hwdep.c‎

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,27 @@ MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
3535
MODULE_DESCRIPTION("Hardware dependent layer");
3636
MODULE_LICENSE("GPL");
3737

38-
static struct snd_hwdep *snd_hwdep_devices[SNDRV_CARDS * SNDRV_MINOR_HWDEPS];
39-
38+
static LIST_HEAD(snd_hwdep_devices);
4039
static DECLARE_MUTEX(register_mutex);
4140

4241
static int snd_hwdep_free(struct snd_hwdep *hwdep);
4342
static int snd_hwdep_dev_free(struct snd_device *device);
4443
static int snd_hwdep_dev_register(struct snd_device *device);
4544
static int snd_hwdep_dev_unregister(struct snd_device *device);
4645

47-
/*
4846

49-
*/
47+
static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
48+
{
49+
struct list_head *p;
50+
struct snd_hwdep *hwdep;
51+
52+
list_for_each(p, &snd_hwdep_devices) {
53+
hwdep = list_entry(p, struct snd_hwdep, list);
54+
if (hwdep->card == card && hwdep->device == device)
55+
return hwdep;
56+
}
57+
return NULL;
58+
}
5059

5160
static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
5261
{
@@ -77,34 +86,25 @@ static ssize_t snd_hwdep_write(struct file * file, const char __user *buf,
7786
static int snd_hwdep_open(struct inode *inode, struct file * file)
7887
{
7988
int major = imajor(inode);
80-
int cardnum;
81-
int device;
8289
struct snd_hwdep *hw;
8390
int err;
8491
wait_queue_t wait;
8592

8693
if (major == snd_major) {
87-
cardnum = SNDRV_MINOR_CARD(iminor(inode));
88-
device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_HWDEP;
94+
hw = snd_lookup_minor_data(iminor(inode),
95+
SNDRV_DEVICE_TYPE_HWDEP);
8996
#ifdef CONFIG_SND_OSSEMUL
9097
} else if (major == SOUND_MAJOR) {
91-
cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
92-
device = 0;
98+
hw = snd_lookup_oss_minor_data(iminor(inode),
99+
SNDRV_OSS_DEVICE_TYPE_DMFM);
93100
#endif
94101
} else
95102
return -ENXIO;
96-
cardnum %= SNDRV_CARDS;
97-
device %= SNDRV_MINOR_HWDEPS;
98-
hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
99103
if (hw == NULL)
100104
return -ENODEV;
101105

102106
if (!hw->ops.open)
103107
return -ENXIO;
104-
#ifdef CONFIG_SND_OSSEMUL
105-
if (major == SOUND_MAJOR && hw->oss_type < 0)
106-
return -ENXIO;
107-
#endif
108108

109109
if (!try_module_get(hw->card->module))
110110
return -EFAULT;
@@ -265,42 +265,43 @@ static int snd_hwdep_control_ioctl(struct snd_card *card,
265265
struct snd_ctl_file * control,
266266
unsigned int cmd, unsigned long arg)
267267
{
268-
unsigned int tmp;
269-
270-
tmp = card->number * SNDRV_MINOR_HWDEPS;
271268
switch (cmd) {
272269
case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
273270
{
274271
int device;
275272

276273
if (get_user(device, (int __user *)arg))
277274
return -EFAULT;
275+
down(&register_mutex);
278276
device = device < 0 ? 0 : device + 1;
279277
while (device < SNDRV_MINOR_HWDEPS) {
280-
if (snd_hwdep_devices[tmp + device])
278+
if (snd_hwdep_search(card, device))
281279
break;
282280
device++;
283281
}
284282
if (device >= SNDRV_MINOR_HWDEPS)
285283
device = -1;
284+
up(&register_mutex);
286285
if (put_user(device, (int __user *)arg))
287286
return -EFAULT;
288287
return 0;
289288
}
290289
case SNDRV_CTL_IOCTL_HWDEP_INFO:
291290
{
292291
struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
293-
int device;
292+
int device, err;
294293
struct snd_hwdep *hwdep;
295294

296295
if (get_user(device, &info->device))
297296
return -EFAULT;
298-
if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
299-
return -ENXIO;
300-
hwdep = snd_hwdep_devices[tmp + device];
301-
if (hwdep == NULL)
302-
return -ENXIO;
303-
return snd_hwdep_info(hwdep, info);
297+
down(&register_mutex);
298+
hwdep = snd_hwdep_search(card, device);
299+
if (hwdep)
300+
err = snd_hwdep_info(hwdep, info);
301+
else
302+
err = -ENXIO;
303+
up(&register_mutex);
304+
return err;
304305
}
305306
}
306307
return -ENOIOCTLCMD;
@@ -397,23 +398,22 @@ static int snd_hwdep_dev_free(struct snd_device *device)
397398
static int snd_hwdep_dev_register(struct snd_device *device)
398399
{
399400
struct snd_hwdep *hwdep = device->device_data;
400-
int idx, err;
401+
int err;
401402
char name[32];
402403

403404
down(&register_mutex);
404-
idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
405-
if (snd_hwdep_devices[idx]) {
405+
if (snd_hwdep_search(hwdep->card, hwdep->device)) {
406406
up(&register_mutex);
407407
return -EBUSY;
408408
}
409-
snd_hwdep_devices[idx] = hwdep;
409+
list_add_tail(&hwdep->list, &snd_hwdep_devices);
410410
sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
411411
if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
412412
hwdep->card, hwdep->device,
413-
&snd_hwdep_f_ops, name)) < 0) {
413+
&snd_hwdep_f_ops, hwdep, name)) < 0) {
414414
snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
415415
hwdep->card->number, hwdep->device);
416-
snd_hwdep_devices[idx] = NULL;
416+
list_del(&hwdep->list);
417417
up(&register_mutex);
418418
return err;
419419
}
@@ -425,7 +425,7 @@ static int snd_hwdep_dev_register(struct snd_device *device)
425425
} else {
426426
if (snd_register_oss_device(hwdep->oss_type,
427427
hwdep->card, hwdep->device,
428-
&snd_hwdep_f_ops,
428+
&snd_hwdep_f_ops, hwdep,
429429
hwdep->oss_dev) < 0) {
430430
snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
431431
hwdep->card->number, hwdep->device);
@@ -441,12 +441,10 @@ static int snd_hwdep_dev_register(struct snd_device *device)
441441
static int snd_hwdep_dev_unregister(struct snd_device *device)
442442
{
443443
struct snd_hwdep *hwdep = device->device_data;
444-
int idx;
445444

446445
snd_assert(hwdep != NULL, return -ENXIO);
447446
down(&register_mutex);
448-
idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
449-
if (snd_hwdep_devices[idx] != hwdep) {
447+
if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
450448
up(&register_mutex);
451449
return -EINVAL;
452450
}
@@ -455,7 +453,7 @@ static int snd_hwdep_dev_unregister(struct snd_device *device)
455453
snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
456454
#endif
457455
snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
458-
snd_hwdep_devices[idx] = NULL;
456+
list_del(&hwdep->list);
459457
up(&register_mutex);
460458
return snd_hwdep_free(hwdep);
461459
}
@@ -467,18 +465,14 @@ static int snd_hwdep_dev_unregister(struct snd_device *device)
467465
static void snd_hwdep_proc_read(struct snd_info_entry *entry,
468466
struct snd_info_buffer *buffer)
469467
{
470-
int idx;
468+
struct list_head *p;
471469
struct snd_hwdep *hwdep;
472470

473471
down(&register_mutex);
474-
for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
475-
hwdep = snd_hwdep_devices[idx];
476-
if (hwdep == NULL)
477-
continue;
472+
list_for_each(p, &snd_hwdep_devices) {
473+
hwdep = list_entry(p, struct snd_hwdep, list);
478474
snd_iprintf(buffer, "%02i-%02i: %s\n",
479-
idx / SNDRV_MINOR_HWDEPS,
480-
idx % SNDRV_MINOR_HWDEPS,
481-
hwdep->name);
475+
hwdep->card->number, hwdep->device, hwdep->name);
482476
}
483477
up(&register_mutex);
484478
}
@@ -493,9 +487,8 @@ static int __init alsa_hwdep_init(void)
493487
{
494488
struct snd_info_entry *entry;
495489

496-
memset(snd_hwdep_devices, 0, sizeof(snd_hwdep_devices));
497490
if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
498-
entry->c.text.read_size = 512;
491+
entry->c.text.read_size = PAGE_SIZE;
499492
entry->c.text.read = snd_hwdep_proc_read;
500493
if (snd_info_register(entry) < 0) {
501494
snd_info_free_entry(entry);

‎sound/core/oss/mixer_oss.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_MIXER);
4141

4242
static int snd_mixer_oss_open(struct inode *inode, struct file *file)
4343
{
44-
int cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
4544
struct snd_card *card;
4645
struct snd_mixer_oss_file *fmixer;
4746
int err;
4847

49-
if ((card = snd_cards[cardnum]) == NULL)
48+
card = snd_lookup_oss_minor_data(iminor(inode),
49+
SNDRV_OSS_DEVICE_TYPE_MIXER);
50+
if (card == NULL)
5051
return -ENODEV;
5152
if (card->mixer_oss == NULL)
5253
return -ENODEV;
@@ -1286,7 +1287,7 @@ static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd)
12861287
sprintf(name, "mixer%i%i", card->number, 0);
12871288
if ((err = snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIXER,
12881289
card, 0,
1289-
&snd_mixer_oss_f_ops,
1290+
&snd_mixer_oss_f_ops, card,
12901291
name)) < 0) {
12911292
snd_printk(KERN_ERR "unable to register OSS mixer device %i:%i\n",
12921293
card->number, 0);

‎sound/core/oss/pcm_oss.c‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,9 +1834,6 @@ static int snd_task_name(struct task_struct *task, char *name, size_t size)
18341834

18351835
static int snd_pcm_oss_open(struct inode *inode, struct file *file)
18361836
{
1837-
int minor = iminor(inode);
1838-
int cardnum = SNDRV_MINOR_OSS_CARD(minor);
1839-
int device;
18401837
int err;
18411838
char task_name[32];
18421839
struct snd_pcm *pcm;
@@ -1845,11 +1842,8 @@ static int snd_pcm_oss_open(struct inode *inode, struct file *file)
18451842
int nonblock;
18461843
wait_queue_t wait;
18471844

1848-
snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
1849-
device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
1850-
adsp_map[cardnum] : dsp_map[cardnum];
1851-
1852-
pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
1845+
pcm = snd_lookup_oss_minor_data(iminor(inode),
1846+
SNDRV_OSS_DEVICE_TYPE_PCM);
18531847
if (pcm == NULL) {
18541848
err = -ENODEV;
18551849
goto __error1;
@@ -1890,7 +1884,7 @@ static int snd_pcm_oss_open(struct inode *inode, struct file *file)
18901884
down(&pcm->open_mutex);
18911885
while (1) {
18921886
err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
1893-
minor, psetup, csetup);
1887+
iminor(inode), psetup, csetup);
18941888
if (err >= 0)
18951889
break;
18961890
if (err == -EAGAIN) {
@@ -2450,7 +2444,7 @@ static void register_oss_dsp(struct snd_pcm *pcm, int index)
24502444
sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
24512445
if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
24522446
pcm->card, index, &snd_pcm_oss_f_reg,
2453-
name) < 0) {
2447+
pcm, name) < 0) {
24542448
snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
24552449
pcm->card->number, pcm->device);
24562450
}

0 commit comments

Comments
 (0)