]> Pileus Git - ~andy/linux/blob - sound/core/sound_oss.c
[ALSA] Decentralize PM control
[~andy/linux] / sound / core / sound_oss.c
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 struct snd_minor *snd_oss_minor_search(int minor)
45 {
46         struct list_head *list;
47         struct snd_minor *mptr;
48
49         list_for_each(list, &snd_oss_minors_hash[SNDRV_MINOR_OSS_CARD(minor)]) {
50                 mptr = list_entry(list, struct snd_minor, list);
51                 if (mptr->number == minor)
52                         return mptr;
53         }
54         return NULL;
55 }
56
57 static int snd_oss_kernel_minor(int type, struct snd_card *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, struct snd_card *card, int dev,
94                             struct snd_minor * reg, const char *name)
95 {
96         int minor = snd_oss_kernel_minor(type, card, dev);
97         int minor_unit;
98         struct snd_minor *preg;
99         int cidx = SNDRV_MINOR_OSS_CARD(minor);
100         int track2 = -1;
101         int register1 = -1, register2 = -1;
102         struct device *carddev = NULL;
103
104         if (minor < 0)
105                 return minor;
106         preg = (struct snd_minor *)kmalloc(sizeof(struct snd_minor), GFP_KERNEL);
107         if (preg == NULL)
108                 return -ENOMEM;
109         *preg = *reg;
110         preg->number = minor;
111         preg->device = dev;
112         down(&sound_oss_mutex);
113         list_add_tail(&preg->list, &snd_oss_minors_hash[cidx]);
114         minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
115         switch (minor_unit) {
116         case SNDRV_MINOR_OSS_PCM:
117                 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
118                 break;
119         case SNDRV_MINOR_OSS_MIDI:
120                 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
121                 break;
122         case SNDRV_MINOR_OSS_MIDI1:
123                 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
124                 break;
125         }
126         if (card)
127                 carddev = card->dev;
128         register1 = register_sound_special_device(reg->f_ops, minor, carddev);
129         if (register1 != minor)
130                 goto __end;
131         if (track2 >= 0) {
132                 register2 = register_sound_special_device(reg->f_ops, track2, carddev);
133                 if (register2 != track2)
134                         goto __end;
135         }
136         up(&sound_oss_mutex);
137         return 0;
138
139       __end:
140         if (register2 >= 0)
141                 unregister_sound_special(register2);
142         if (register1 >= 0)
143                 unregister_sound_special(register1);
144         list_del(&preg->list);
145         up(&sound_oss_mutex);
146         kfree(preg);
147         return -EBUSY;
148 }
149
150 int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
151 {
152         int minor = snd_oss_kernel_minor(type, card, dev);
153         int cidx = SNDRV_MINOR_OSS_CARD(minor);
154         int track2 = -1;
155         struct snd_minor *mptr;
156
157         if (minor < 0)
158                 return minor;
159         down(&sound_oss_mutex);
160         mptr = snd_oss_minor_search(minor);
161         if (mptr == NULL) {
162                 up(&sound_oss_mutex);
163                 return -ENOENT;
164         }
165         unregister_sound_special(minor);
166         switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
167         case SNDRV_MINOR_OSS_PCM:
168                 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
169                 break;
170         case SNDRV_MINOR_OSS_MIDI:
171                 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
172                 break;
173         case SNDRV_MINOR_OSS_MIDI1:
174                 track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
175                 break;
176         }
177         if (track2 >= 0)
178                 unregister_sound_special(track2);
179         list_del(&mptr->list);
180         up(&sound_oss_mutex);
181         kfree(mptr);
182         return 0;
183 }
184
185 /*
186  *  INFO PART
187  */
188
189 #ifdef CONFIG_PROC_FS
190
191 static struct snd_info_entry *snd_minor_info_oss_entry = NULL;
192
193 static void snd_minor_info_oss_read(struct snd_info_entry *entry,
194                                     struct snd_info_buffer *buffer)
195 {
196         int card, dev;
197         struct list_head *list;
198         struct snd_minor *mptr;
199
200         down(&sound_oss_mutex);
201         for (card = 0; card < SNDRV_CARDS; card++) {
202                 list_for_each(list, &snd_oss_minors_hash[card]) {
203                         mptr = list_entry(list, struct snd_minor, list);
204                         dev = SNDRV_MINOR_OSS_DEVICE(mptr->number);
205                         if (dev != SNDRV_MINOR_OSS_SNDSTAT &&
206                             dev != SNDRV_MINOR_OSS_SEQUENCER &&
207                             dev != SNDRV_MINOR_OSS_MUSIC)
208                                 snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", mptr->number, card, dev, mptr->comment);
209                         else
210                                 snd_iprintf(buffer, "%3i:       : %s\n", mptr->number, mptr->comment);
211                 }
212         }
213         up(&sound_oss_mutex);
214 }
215
216 #endif /* CONFIG_PROC_FS */
217
218 int __init snd_minor_info_oss_init(void)
219 {
220 #ifdef CONFIG_PROC_FS
221         struct snd_info_entry *entry;
222
223         entry = snd_info_create_module_entry(THIS_MODULE, "devices", snd_oss_root);
224         if (entry) {
225                 entry->c.text.read_size = PAGE_SIZE;
226                 entry->c.text.read = snd_minor_info_oss_read;
227                 if (snd_info_register(entry) < 0) {
228                         snd_info_free_entry(entry);
229                         entry = NULL;
230                 }
231         }
232         snd_minor_info_oss_entry = entry;
233 #endif
234         return 0;
235 }
236
237 int __exit snd_minor_info_oss_done(void)
238 {
239 #ifdef CONFIG_PROC_FS
240         if (snd_minor_info_oss_entry)
241                 snd_info_unregister(snd_minor_info_oss_entry);
242 #endif
243         return 0;
244 }
245
246 int __init snd_oss_init_module(void)
247 {
248         int card;
249         
250         for (card = 0; card < SNDRV_CARDS; card++)
251                 INIT_LIST_HEAD(&snd_oss_minors_hash[card]);
252         return 0;
253 }
254
255 #endif /* CONFIG_SND_OSSEMUL */