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