| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Advanced Linux Sound Architecture |
| 4 |
* Copyright (c) by Jaroslav Kysela <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/init.h> |
| 8 |
#include <linux/export.h> |
| 9 |
#include <linux/slab.h> |
| 10 |
#include <linux/time.h> |
| 11 |
#include <sound/core.h> |
| 12 |
#include <sound/minors.h> |
| 13 |
#include <sound/info.h> |
| 14 |
#include <linux/sound.h> |
| 15 |
#include <linux/mutex.h> |
| 16 |
|
| 17 |
#define SNDRV_OSS_MINORS 256 |
| 18 |
|
| 19 |
static struct snd_minor *snd_oss_minors[SNDRV_OSS_MINORS]; |
| 20 |
static DEFINE_MUTEX(sound_oss_mutex); |
| 21 |
|
| 22 |
/* NOTE: This function increments the refcount of the associated card like |
| 23 |
* snd_lookup_minor_data(); the caller must call snd_card_unref() appropriately |
| 24 |
*/ |
| 25 |
void *snd_lookup_oss_minor_data(unsigned int minor, int type) |
| 26 |
{ |
| 27 |
struct snd_minor *mreg; |
| 28 |
void *private_data; |
| 29 |
|
| 30 |
if (minor >= ARRAY_SIZE(snd_oss_minors)) |
| 31 |
return NULL; |
| 32 |
guard(mutex)(&sound_oss_mutex); |
| 33 |
mreg = snd_oss_minors[minor]; |
| 34 |
if (mreg && mreg->type == type) { |
| 35 |
private_data = mreg->private_data; |
| 36 |
if (private_data && mreg->card_ptr) |
| 37 |
get_device(&mreg->card_ptr->card_dev); |
| 38 |
} else |
| 39 |
private_data = NULL; |
| 40 |
return private_data; |
| 41 |
} |
| 42 |
EXPORT_SYMBOL(snd_lookup_oss_minor_data); |
| 43 |
|
| 44 |
static int snd_oss_kernel_minor(int type, struct snd_card *card, int dev) |
| 45 |
{ |
| 46 |
int minor; |
| 47 |
|
| 48 |
switch (type) { |
| 49 |
case SNDRV_OSS_DEVICE_TYPE_MIXER: |
| 50 |
if (snd_BUG_ON(!card || dev < 0 || dev > 1)) |
| 51 |
return -EINVAL; |
| 52 |
minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIXER1 : SNDRV_MINOR_OSS_MIXER)); |
| 53 |
break; |
| 54 |
case SNDRV_OSS_DEVICE_TYPE_SEQUENCER: |
| 55 |
minor = SNDRV_MINOR_OSS_SEQUENCER; |
| 56 |
break; |
| 57 |
case SNDRV_OSS_DEVICE_TYPE_MUSIC: |
| 58 |
minor = SNDRV_MINOR_OSS_MUSIC; |
| 59 |
break; |
| 60 |
case SNDRV_OSS_DEVICE_TYPE_PCM: |
| 61 |
if (snd_BUG_ON(!card || dev < 0 || dev > 1)) |
| 62 |
return -EINVAL; |
| 63 |
minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_PCM1 : SNDRV_MINOR_OSS_PCM)); |
| 64 |
break; |
| 65 |
case SNDRV_OSS_DEVICE_TYPE_MIDI: |
| 66 |
if (snd_BUG_ON(!card || dev < 0 || dev > 1)) |
| 67 |
return -EINVAL; |
| 68 |
minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIDI1 : SNDRV_MINOR_OSS_MIDI)); |
| 69 |
break; |
| 70 |
case SNDRV_OSS_DEVICE_TYPE_DMFM: |
| 71 |
minor = SNDRV_MINOR_OSS(card->number, SNDRV_MINOR_OSS_DMFM); |
| 72 |
break; |
| 73 |
case SNDRV_OSS_DEVICE_TYPE_SNDSTAT: |
| 74 |
minor = SNDRV_MINOR_OSS_SNDSTAT; |
| 75 |
break; |
| 76 |
default: |
| 77 |
return -EINVAL; |
| 78 |
} |
| 79 |
if (minor < 0 || minor >= SNDRV_OSS_MINORS) |
| 80 |
return -EINVAL; |
| 81 |
return minor; |
| 82 |
} |
| 83 |
|
| 84 |
int snd_register_oss_device(int type, struct snd_card *card, int dev, |
| 85 |
const struct file_operations *f_ops, void *private_data) |
| 86 |
{ |
| 87 |
int minor = snd_oss_kernel_minor(type, card, dev); |
| 88 |
int minor_unit; |
| 89 |
struct snd_minor *preg; |
| 90 |
int cidx = SNDRV_MINOR_OSS_CARD(minor); |
| 91 |
int track2 = -1; |
| 92 |
int register1 = -1, register2 = -1; |
| 93 |
struct device *carddev = snd_card_get_device_link(card); |
| 94 |
|
| 95 |
if (card && card->number >= SNDRV_MINOR_OSS_DEVICES) |
| 96 |
return 0; /* ignore silently */ |
| 97 |
if (minor < 0) |
| 98 |
return minor; |
| 99 |
preg = kmalloc_obj(struct snd_minor); |
| 100 |
if (preg == NULL) |
| 101 |
return -ENOMEM; |
| 102 |
preg->type = type; |
| 103 |
preg->card = card ? card->number : -1; |
| 104 |
preg->device = dev; |
| 105 |
preg->f_ops = f_ops; |
| 106 |
preg->private_data = private_data; |
| 107 |
preg->card_ptr = card; |
| 108 |
guard(mutex)(&sound_oss_mutex); |
| 109 |
snd_oss_minors[minor] = preg; |
| 110 |
minor_unit = SNDRV_MINOR_OSS_DEVICE(minor); |
| 111 |
switch (minor_unit) { |
| 112 |
case SNDRV_MINOR_OSS_PCM: |
| 113 |
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO); |
| 114 |
break; |
| 115 |
case SNDRV_MINOR_OSS_MIDI: |
| 116 |
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI); |
| 117 |
break; |
| 118 |
case SNDRV_MINOR_OSS_MIDI1: |
| 119 |
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1); |
| 120 |
break; |
| 121 |
} |
| 122 |
register1 = register_sound_special_device(f_ops, minor, carddev); |
| 123 |
if (register1 != minor) |
| 124 |
goto __end; |
| 125 |
if (track2 >= 0) { |
| 126 |
register2 = register_sound_special_device(f_ops, track2, |
| 127 |
carddev); |
| 128 |
if (register2 != track2) |
| 129 |
goto __end; |
| 130 |
snd_oss_minors[track2] = preg; |
| 131 |
} |
| 132 |
return 0; |
| 133 |
|
| 134 |
__end: |
| 135 |
if (register2 >= 0) |
| 136 |
unregister_sound_special(register2); |
| 137 |
if (register1 >= 0) |
| 138 |
unregister_sound_special(register1); |
| 139 |
snd_oss_minors[minor] = NULL; |
| 140 |
kfree(preg); |
| 141 |
return -EBUSY; |
| 142 |
} |
| 143 |
EXPORT_SYMBOL(snd_register_oss_device); |
| 144 |
|
| 145 |
int snd_unregister_oss_device(int type, struct snd_card *card, int dev) |
| 146 |
{ |
| 147 |
int minor = snd_oss_kernel_minor(type, card, dev); |
| 148 |
int cidx = SNDRV_MINOR_OSS_CARD(minor); |
| 149 |
int track2 = -1; |
| 150 |
struct snd_minor *mptr; |
| 151 |
|
| 152 |
if (card && card->number >= SNDRV_MINOR_OSS_DEVICES) |
| 153 |
return 0; |
| 154 |
if (minor < 0) |
| 155 |
return minor; |
| 156 |
guard(mutex)(&sound_oss_mutex); |
| 157 |
mptr = snd_oss_minors[minor]; |
| 158 |
if (mptr == NULL) |
| 159 |
return -ENOENT; |
| 160 |
switch (SNDRV_MINOR_OSS_DEVICE(minor)) { |
| 161 |
case SNDRV_MINOR_OSS_PCM: |
| 162 |
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO); |
| 163 |
break; |
| 164 |
case SNDRV_MINOR_OSS_MIDI: |
| 165 |
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI); |
| 166 |
break; |
| 167 |
case SNDRV_MINOR_OSS_MIDI1: |
| 168 |
track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1); |
| 169 |
break; |
| 170 |
} |
| 171 |
if (track2 >= 0) |
| 172 |
snd_oss_minors[track2] = NULL; |
| 173 |
snd_oss_minors[minor] = NULL; |
| 174 |
|
| 175 |
/* call unregister_sound_special() outside sound_oss_mutex; |
| 176 |
* otherwise may deadlock, as it can trigger the release of a card |
| 177 |
*/ |
| 178 |
unregister_sound_special(minor); |
| 179 |
if (track2 >= 0) |
| 180 |
unregister_sound_special(track2); |
| 181 |
|
| 182 |
kfree(mptr); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
EXPORT_SYMBOL(snd_unregister_oss_device); |
| 186 |
|
| 187 |
/* |
| 188 |
* INFO PART |
| 189 |
*/ |
| 190 |
|
| 191 |
#ifdef CONFIG_SND_PROC_FS |
| 192 |
static const char *snd_oss_device_type_name(int type) |
| 193 |
{ |
| 194 |
switch (type) { |
| 195 |
case SNDRV_OSS_DEVICE_TYPE_MIXER: |
| 196 |
return "mixer"; |
| 197 |
case SNDRV_OSS_DEVICE_TYPE_SEQUENCER: |
| 198 |
case SNDRV_OSS_DEVICE_TYPE_MUSIC: |
| 199 |
return "sequencer"; |
| 200 |
case SNDRV_OSS_DEVICE_TYPE_PCM: |
| 201 |
return "digital audio"; |
| 202 |
case SNDRV_OSS_DEVICE_TYPE_MIDI: |
| 203 |
return "raw midi"; |
| 204 |
case SNDRV_OSS_DEVICE_TYPE_DMFM: |
| 205 |
return "hardware dependent"; |
| 206 |
default: |
| 207 |
return "?"; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
static void snd_minor_info_oss_read(struct snd_info_entry *entry, |
| 212 |
struct snd_info_buffer *buffer) |
| 213 |
{ |
| 214 |
int minor; |
| 215 |
struct snd_minor *mptr; |
| 216 |
|
| 217 |
guard(mutex)(&sound_oss_mutex); |
| 218 |
for (minor = 0; minor < SNDRV_OSS_MINORS; ++minor) { |
| 219 |
mptr = snd_oss_minors[minor]; |
| 220 |
if (!mptr) |
| 221 |
continue; |
| 222 |
if (mptr->card >= 0) |
| 223 |
snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", minor, |
| 224 |
mptr->card, mptr->device, |
| 225 |
snd_oss_device_type_name(mptr->type)); |
| 226 |
else |
| 227 |
snd_iprintf(buffer, "%3i: : %s\n", minor, |
| 228 |
snd_oss_device_type_name(mptr->type)); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
int __init snd_minor_info_oss_init(void) |
| 234 |
{ |
| 235 |
struct snd_info_entry *entry; |
| 236 |
|
| 237 |
entry = snd_info_create_module_entry(THIS_MODULE, "devices", snd_oss_root); |
| 238 |
if (!entry) |
| 239 |
return -ENOMEM; |
| 240 |
entry->c.text.read = snd_minor_info_oss_read; |
| 241 |
return snd_info_register(entry); /* freed in error path */ |
| 242 |
} |
| 243 |
#endif /* CONFIG_SND_PROC_FS */ |