xref: /openbmc/linux/sound/core/sound_oss.c (revision 1da177e4)
1 /*
2  *  Advanced Linux Sound Architecture
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 
22 #include <sound/driver.h>
23 
24 #ifdef CONFIG_SND_OSSEMUL
25 
26 #if !defined(CONFIG_SOUND) && !(defined(MODULE) && defined(CONFIG_SOUND_MODULE))
27 #error "Enable the OSS soundcore multiplexer (CONFIG_SOUND) in the kernel."
28 #endif
29 
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <sound/core.h>
34 #include <sound/minors.h>
35 #include <sound/info.h>
36 #include <linux/sound.h>
37 
38 #define SNDRV_OS_MINORS		256
39 
40 static struct list_head snd_oss_minors_hash[SNDRV_CARDS];
41 
42 static DECLARE_MUTEX(sound_oss_mutex);
43 
44 static snd_minor_t *snd_oss_minor_search(int minor)
45 {
46 	struct list_head *list;
47 	snd_minor_t *mptr;
48 
49 	list_for_each(list, &snd_oss_minors_hash[SNDRV_MINOR_OSS_CARD(minor)]) {
50 		mptr = list_entry(list, snd_minor_t, list);
51 		if (mptr->number == minor)
52 			return mptr;
53 	}
54 	return NULL;
55 }
56 
57 static int snd_oss_kernel_minor(int type, snd_card_t * card, int dev)
58 {
59 	int minor;
60 
61 	switch (type) {
62 	case SNDRV_OSS_DEVICE_TYPE_MIXER:
63 		snd_assert(card != NULL && dev <= 1, return -EINVAL);
64 		minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIXER1 : SNDRV_MINOR_OSS_MIXER));
65 		break;
66 	case SNDRV_OSS_DEVICE_TYPE_SEQUENCER:
67 		minor = SNDRV_MINOR_OSS_SEQUENCER;
68 		break;
69 	case SNDRV_OSS_DEVICE_TYPE_MUSIC:
70 		minor = SNDRV_MINOR_OSS_MUSIC;
71 		break;
72 	case SNDRV_OSS_DEVICE_TYPE_PCM:
73 		snd_assert(card != NULL && dev <= 1, return -EINVAL);
74 		minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_PCM1 : SNDRV_MINOR_OSS_PCM));
75 		break;
76 	case SNDRV_OSS_DEVICE_TYPE_MIDI:
77 		snd_assert(card != NULL && dev <= 1, return -EINVAL);
78 		minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIDI1 : SNDRV_MINOR_OSS_MIDI));
79 		break;
80 	case SNDRV_OSS_DEVICE_TYPE_DMFM:
81 		minor = SNDRV_MINOR_OSS(card->number, SNDRV_MINOR_OSS_DMFM);
82 		break;
83 	case SNDRV_OSS_DEVICE_TYPE_SNDSTAT:
84 		minor = SNDRV_MINOR_OSS_SNDSTAT;
85 		break;
86 	default:
87 		return -EINVAL;
88 	}
89 	snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
90 	return minor;
91 }
92 
93 int snd_register_oss_device(int type, snd_card_t * card, int dev, snd_minor_t * reg, const char *name)
94 {
95 	int minor = snd_oss_kernel_minor(type, card, dev);
96 	int minor_unit;
97 	snd_minor_t *preg;
98 	int cidx = SNDRV_MINOR_OSS_CARD(minor);
99 	int track2 = -1;
100 	int register1 = -1, register2 = -1;
101 
102 	if (minor < 0)
103 		return minor;
104 	preg = (snd_minor_t *)kmalloc(sizeof(snd_minor_t), GFP_KERNEL);
105 	if (preg == NULL)
106 		return -ENOMEM;
107 	*preg = *reg;
108 	preg->number = minor;
109 	preg->device = dev;
110 	down(&sound_oss_mutex);
111 	list_add_tail(&preg->list, &snd_oss_minors_hash[cidx]);
112 	minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
113 	switch (minor_unit) {
114 	case SNDRV_MINOR_OSS_PCM:
115 		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
116 		break;
117 	case SNDRV_MINOR_OSS_MIDI:
118 		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
119 		break;
120 	case SNDRV_MINOR_OSS_MIDI1:
121 		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
122 		break;
123 	}
124 	register1 = register_sound_special(reg->f_ops, minor);
125 	if (register1 != minor)
126 		goto __end;
127 	if (track2 >= 0) {
128 		register2 = register_sound_special(reg->f_ops, track2);
129 		if (register2 != track2)
130 			goto __end;
131 	}
132 	up(&sound_oss_mutex);
133 	return 0;
134 
135       __end:
136       	if (register2 >= 0)
137       		unregister_sound_special(register2);
138       	if (register1 >= 0)
139       		unregister_sound_special(register1);
140       	list_del(&preg->list);
141 	up(&sound_oss_mutex);
142 	kfree(preg);
143       	return -EBUSY;
144 }
145 
146 int snd_unregister_oss_device(int type, snd_card_t * card, int dev)
147 {
148 	int minor = snd_oss_kernel_minor(type, card, dev);
149 	int cidx = SNDRV_MINOR_OSS_CARD(minor);
150 	int track2 = -1;
151 	snd_minor_t *mptr;
152 
153 	if (minor < 0)
154 		return minor;
155 	down(&sound_oss_mutex);
156 	mptr = snd_oss_minor_search(minor);
157 	if (mptr == NULL) {
158 		up(&sound_oss_mutex);
159 		return -ENOENT;
160 	}
161 	unregister_sound_special(minor);
162 	switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
163 	case SNDRV_MINOR_OSS_PCM:
164 		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
165 		break;
166 	case SNDRV_MINOR_OSS_MIDI:
167 		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
168 		break;
169 	case SNDRV_MINOR_OSS_MIDI1:
170 		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
171 		break;
172 	}
173 	if (track2 >= 0)
174 		unregister_sound_special(track2);
175 	list_del(&mptr->list);
176 	up(&sound_oss_mutex);
177 	kfree(mptr);
178 	return 0;
179 }
180 
181 /*
182  *  INFO PART
183  */
184 
185 #ifdef CONFIG_PROC_FS
186 
187 static snd_info_entry_t *snd_minor_info_oss_entry = NULL;
188 
189 static void snd_minor_info_oss_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
190 {
191 	int card, dev;
192 	struct list_head *list;
193 	snd_minor_t *mptr;
194 
195 	down(&sound_oss_mutex);
196 	for (card = 0; card < SNDRV_CARDS; card++) {
197 		list_for_each(list, &snd_oss_minors_hash[card]) {
198 			mptr = list_entry(list, snd_minor_t, list);
199 			dev = SNDRV_MINOR_OSS_DEVICE(mptr->number);
200 		        if (dev != SNDRV_MINOR_OSS_SNDSTAT &&
201 			    dev != SNDRV_MINOR_OSS_SEQUENCER &&
202 			    dev != SNDRV_MINOR_OSS_MUSIC)
203 				snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, dev, mptr->comment);
204 			else
205 				snd_iprintf(buffer, "%3i:       : %s\n", mptr->number, mptr->comment);
206 		}
207 	}
208 	up(&sound_oss_mutex);
209 }
210 
211 #endif /* CONFIG_PROC_FS */
212 
213 int __init snd_minor_info_oss_init(void)
214 {
215 #ifdef CONFIG_PROC_FS
216 	snd_info_entry_t *entry;
217 
218 	entry = snd_info_create_module_entry(THIS_MODULE, "devices", snd_oss_root);
219 	if (entry) {
220 		entry->c.text.read_size = PAGE_SIZE;
221 		entry->c.text.read = snd_minor_info_oss_read;
222 		if (snd_info_register(entry) < 0) {
223 			snd_info_free_entry(entry);
224 			entry = NULL;
225 		}
226 	}
227 	snd_minor_info_oss_entry = entry;
228 #endif
229 	return 0;
230 }
231 
232 int __exit snd_minor_info_oss_done(void)
233 {
234 #ifdef CONFIG_PROC_FS
235 	if (snd_minor_info_oss_entry)
236 		snd_info_unregister(snd_minor_info_oss_entry);
237 #endif
238 	return 0;
239 }
240 
241 int __init snd_oss_init_module(void)
242 {
243 	int card;
244 
245 	for (card = 0; card < SNDRV_CARDS; card++)
246 		INIT_LIST_HEAD(&snd_oss_minors_hash[card]);
247 	return 0;
248 }
249 
250 #endif /* CONFIG_SND_OSSEMUL */
251