blob: 4d189ffaa764302f72225ecb908994a96f93d16b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
40static struct list_head snd_oss_minors_hash[SNDRV_CARDS];
41
42static DECLARE_MUTEX(sound_oss_mutex);
43
Takashi Iwai174c1f62005-11-17 14:00:19 +010044static struct snd_minor *snd_oss_minor_search(int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct list_head *list;
Takashi Iwai174c1f62005-11-17 14:00:19 +010047 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 list_for_each(list, &snd_oss_minors_hash[SNDRV_MINOR_OSS_CARD(minor)]) {
Takashi Iwai174c1f62005-11-17 14:00:19 +010050 mptr = list_entry(list, struct snd_minor, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (mptr->number == minor)
52 return mptr;
53 }
54 return NULL;
55}
56
Takashi Iwai174c1f62005-11-17 14:00:19 +010057static int snd_oss_kernel_minor(int type, struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
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
Takashi Iwai174c1f62005-11-17 14:00:19 +010093int snd_register_oss_device(int type, struct snd_card *card, int dev,
Clemens Ladisch2af677f2005-11-20 14:03:48 +010094 struct file_operations *f_ops, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int minor = snd_oss_kernel_minor(type, card, dev);
97 int minor_unit;
Takashi Iwai174c1f62005-11-17 14:00:19 +010098 struct snd_minor *preg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int cidx = SNDRV_MINOR_OSS_CARD(minor);
100 int track2 = -1;
101 int register1 = -1, register2 = -1;
Takashi Iwaid5681212005-08-30 08:58:37 +0200102 struct device *carddev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (minor < 0)
105 return minor;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100106 preg = kzalloc(sizeof(struct snd_minor), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (preg == NULL)
108 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 preg->number = minor;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100110 preg->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 preg->device = dev;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100112 preg->f_ops = f_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 down(&sound_oss_mutex);
114 list_add_tail(&preg->list, &snd_oss_minors_hash[cidx]);
115 minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
116 switch (minor_unit) {
117 case SNDRV_MINOR_OSS_PCM:
118 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
119 break;
120 case SNDRV_MINOR_OSS_MIDI:
121 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
122 break;
123 case SNDRV_MINOR_OSS_MIDI1:
124 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
125 break;
126 }
Takashi Iwaid5681212005-08-30 08:58:37 +0200127 if (card)
128 carddev = card->dev;
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100129 register1 = register_sound_special_device(f_ops, minor, carddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (register1 != minor)
131 goto __end;
132 if (track2 >= 0) {
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100133 register2 = register_sound_special_device(f_ops, track2,
134 carddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (register2 != track2)
136 goto __end;
137 }
138 up(&sound_oss_mutex);
139 return 0;
140
141 __end:
142 if (register2 >= 0)
143 unregister_sound_special(register2);
144 if (register1 >= 0)
145 unregister_sound_special(register1);
146 list_del(&preg->list);
147 up(&sound_oss_mutex);
148 kfree(preg);
149 return -EBUSY;
150}
151
Takashi Iwai174c1f62005-11-17 14:00:19 +0100152int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int minor = snd_oss_kernel_minor(type, card, dev);
155 int cidx = SNDRV_MINOR_OSS_CARD(minor);
156 int track2 = -1;
Takashi Iwai174c1f62005-11-17 14:00:19 +0100157 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (minor < 0)
160 return minor;
161 down(&sound_oss_mutex);
162 mptr = snd_oss_minor_search(minor);
163 if (mptr == NULL) {
164 up(&sound_oss_mutex);
165 return -ENOENT;
166 }
167 unregister_sound_special(minor);
168 switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
169 case SNDRV_MINOR_OSS_PCM:
170 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
171 break;
172 case SNDRV_MINOR_OSS_MIDI:
173 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
174 break;
175 case SNDRV_MINOR_OSS_MIDI1:
176 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
177 break;
178 }
179 if (track2 >= 0)
180 unregister_sound_special(track2);
181 list_del(&mptr->list);
182 up(&sound_oss_mutex);
183 kfree(mptr);
184 return 0;
185}
186
187/*
188 * INFO PART
189 */
190
191#ifdef CONFIG_PROC_FS
192
Takashi Iwai174c1f62005-11-17 14:00:19 +0100193static struct snd_info_entry *snd_minor_info_oss_entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100195static const char *snd_oss_device_type_name(int type)
196{
197 switch (type) {
198 case SNDRV_OSS_DEVICE_TYPE_MIXER:
199 return "mixer";
200 case SNDRV_OSS_DEVICE_TYPE_SEQUENCER:
201 case SNDRV_OSS_DEVICE_TYPE_MUSIC:
202 return "sequencer";
203 case SNDRV_OSS_DEVICE_TYPE_PCM:
204 return "digital audio";
205 case SNDRV_OSS_DEVICE_TYPE_MIDI:
206 return "raw midi";
207 case SNDRV_OSS_DEVICE_TYPE_DMFM:
208 return "hardware dependent";
209 default:
210 return "?";
211 }
212}
213
Takashi Iwai174c1f62005-11-17 14:00:19 +0100214static void snd_minor_info_oss_read(struct snd_info_entry *entry,
215 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int card, dev;
218 struct list_head *list;
Takashi Iwai174c1f62005-11-17 14:00:19 +0100219 struct snd_minor *mptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 down(&sound_oss_mutex);
222 for (card = 0; card < SNDRV_CARDS; card++) {
223 list_for_each(list, &snd_oss_minors_hash[card]) {
Takashi Iwai174c1f62005-11-17 14:00:19 +0100224 mptr = list_entry(list, struct snd_minor, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 dev = SNDRV_MINOR_OSS_DEVICE(mptr->number);
226 if (dev != SNDRV_MINOR_OSS_SNDSTAT &&
227 dev != SNDRV_MINOR_OSS_SEQUENCER &&
228 dev != SNDRV_MINOR_OSS_MUSIC)
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100229 snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, dev, snd_oss_device_type_name(mptr->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 else
Clemens Ladisch2af677f2005-11-20 14:03:48 +0100231 snd_iprintf(buffer, "%3i: : %s\n", mptr->number, snd_oss_device_type_name(mptr->type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
234 up(&sound_oss_mutex);
235}
236
237#endif /* CONFIG_PROC_FS */
238
239int __init snd_minor_info_oss_init(void)
240{
241#ifdef CONFIG_PROC_FS
Takashi Iwai174c1f62005-11-17 14:00:19 +0100242 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 entry = snd_info_create_module_entry(THIS_MODULE, "devices", snd_oss_root);
245 if (entry) {
246 entry->c.text.read_size = PAGE_SIZE;
247 entry->c.text.read = snd_minor_info_oss_read;
248 if (snd_info_register(entry) < 0) {
249 snd_info_free_entry(entry);
250 entry = NULL;
251 }
252 }
253 snd_minor_info_oss_entry = entry;
254#endif
255 return 0;
256}
257
258int __exit snd_minor_info_oss_done(void)
259{
260#ifdef CONFIG_PROC_FS
261 if (snd_minor_info_oss_entry)
262 snd_info_unregister(snd_minor_info_oss_entry);
263#endif
264 return 0;
265}
266
267int __init snd_oss_init_module(void)
268{
269 int card;
270
271 for (card = 0; card < SNDRV_CARDS; card++)
272 INIT_LIST_HEAD(&snd_oss_minors_hash[card]);
273 return 0;
274}
275
276#endif /* CONFIG_SND_OSSEMUL */