sound_oss.c 7.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Advanced Linux Sound Architecture
3
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/init.h>
23
#include <linux/export.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29
#include <linux/slab.h>
#include <linux/time.h>
#include <sound/core.h>
#include <sound/minors.h>
#include <sound/info.h>
#include <linux/sound.h>
30
#include <linux/mutex.h>
L
Linus Torvalds 已提交
31

32
#define SNDRV_OSS_MINORS 256
L
Linus Torvalds 已提交
33

34
static struct snd_minor *snd_oss_minors[SNDRV_OSS_MINORS];
35
static DEFINE_MUTEX(sound_oss_mutex);
L
Linus Torvalds 已提交
36

37 38 39
/* NOTE: This function increments the refcount of the associated card like
 * snd_lookup_minor_data(); the caller must call snd_card_unref() appropriately
 */
40 41 42 43 44
void *snd_lookup_oss_minor_data(unsigned int minor, int type)
{
	struct snd_minor *mreg;
	void *private_data;

45
	if (minor >= ARRAY_SIZE(snd_oss_minors))
46
		return NULL;
47
	mutex_lock(&sound_oss_mutex);
48
	mreg = snd_oss_minors[minor];
49
	if (mreg && mreg->type == type) {
50
		private_data = mreg->private_data;
T
Takashi Iwai 已提交
51
		if (private_data && mreg->card_ptr)
52
			get_device(&mreg->card_ptr->card_dev);
53
	} else
54
		private_data = NULL;
55
	mutex_unlock(&sound_oss_mutex);
56 57 58
	return private_data;
}

59 60
EXPORT_SYMBOL(snd_lookup_oss_minor_data);

61
static int snd_oss_kernel_minor(int type, struct snd_card *card, int dev)
L
Linus Torvalds 已提交
62 63 64 65 66
{
	int minor;

	switch (type) {
	case SNDRV_OSS_DEVICE_TYPE_MIXER:
67 68
		if (snd_BUG_ON(!card || dev < 0 || dev > 1))
			return -EINVAL;
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77
		minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIXER1 : SNDRV_MINOR_OSS_MIXER));
		break;
	case SNDRV_OSS_DEVICE_TYPE_SEQUENCER:
		minor = SNDRV_MINOR_OSS_SEQUENCER;
		break;
	case SNDRV_OSS_DEVICE_TYPE_MUSIC:
		minor = SNDRV_MINOR_OSS_MUSIC;
		break;
	case SNDRV_OSS_DEVICE_TYPE_PCM:
78 79
		if (snd_BUG_ON(!card || dev < 0 || dev > 1))
			return -EINVAL;
L
Linus Torvalds 已提交
80 81 82
		minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_PCM1 : SNDRV_MINOR_OSS_PCM));
		break;
	case SNDRV_OSS_DEVICE_TYPE_MIDI:
83 84
		if (snd_BUG_ON(!card || dev < 0 || dev > 1))
			return -EINVAL;
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95
		minor = SNDRV_MINOR_OSS(card->number, (dev ? SNDRV_MINOR_OSS_MIDI1 : SNDRV_MINOR_OSS_MIDI));
		break;
	case SNDRV_OSS_DEVICE_TYPE_DMFM:
		minor = SNDRV_MINOR_OSS(card->number, SNDRV_MINOR_OSS_DMFM);
		break;
	case SNDRV_OSS_DEVICE_TYPE_SNDSTAT:
		minor = SNDRV_MINOR_OSS_SNDSTAT;
		break;
	default:
		return -EINVAL;
	}
96
	if (minor < 0 || minor >= SNDRV_OSS_MINORS)
97
		return -EINVAL;
L
Linus Torvalds 已提交
98 99 100
	return minor;
}

101
int snd_register_oss_device(int type, struct snd_card *card, int dev,
102
			    const struct file_operations *f_ops, void *private_data)
L
Linus Torvalds 已提交
103 104 105
{
	int minor = snd_oss_kernel_minor(type, card, dev);
	int minor_unit;
106
	struct snd_minor *preg;
L
Linus Torvalds 已提交
107 108 109
	int cidx = SNDRV_MINOR_OSS_CARD(minor);
	int track2 = -1;
	int register1 = -1, register2 = -1;
T
Takashi Iwai 已提交
110
	struct device *carddev = snd_card_get_device_link(card);
L
Linus Torvalds 已提交
111

112
	if (card && card->number >= SNDRV_MINOR_OSS_DEVICES)
113
		return 0; /* ignore silently */
L
Linus Torvalds 已提交
114 115
	if (minor < 0)
		return minor;
116
	preg = kmalloc(sizeof(struct snd_minor), GFP_KERNEL);
L
Linus Torvalds 已提交
117 118
	if (preg == NULL)
		return -ENOMEM;
119
	preg->type = type;
120
	preg->card = card ? card->number : -1;
L
Linus Torvalds 已提交
121
	preg->device = dev;
122
	preg->f_ops = f_ops;
123
	preg->private_data = private_data;
124
	preg->card_ptr = card;
125
	mutex_lock(&sound_oss_mutex);
126
	snd_oss_minors[minor] = preg;
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136 137 138
	minor_unit = SNDRV_MINOR_OSS_DEVICE(minor);
	switch (minor_unit) {
	case SNDRV_MINOR_OSS_PCM:
		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
		break;
	case SNDRV_MINOR_OSS_MIDI:
		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
		break;
	case SNDRV_MINOR_OSS_MIDI1:
		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
		break;
	}
139
	register1 = register_sound_special_device(f_ops, minor, carddev);
L
Linus Torvalds 已提交
140 141 142
	if (register1 != minor)
		goto __end;
	if (track2 >= 0) {
143 144
		register2 = register_sound_special_device(f_ops, track2,
							  carddev);
L
Linus Torvalds 已提交
145 146
		if (register2 != track2)
			goto __end;
147
		snd_oss_minors[track2] = preg;
L
Linus Torvalds 已提交
148
	}
149
	mutex_unlock(&sound_oss_mutex);
L
Linus Torvalds 已提交
150 151 152 153 154 155 156
	return 0;

      __end:
      	if (register2 >= 0)
      		unregister_sound_special(register2);
      	if (register1 >= 0)
      		unregister_sound_special(register1);
157
	snd_oss_minors[minor] = NULL;
158
	mutex_unlock(&sound_oss_mutex);
L
Linus Torvalds 已提交
159 160 161 162
	kfree(preg);
      	return -EBUSY;
}

163 164
EXPORT_SYMBOL(snd_register_oss_device);

165
int snd_unregister_oss_device(int type, struct snd_card *card, int dev)
L
Linus Torvalds 已提交
166 167 168 169
{
	int minor = snd_oss_kernel_minor(type, card, dev);
	int cidx = SNDRV_MINOR_OSS_CARD(minor);
	int track2 = -1;
170
	struct snd_minor *mptr;
L
Linus Torvalds 已提交
171

172
	if (card && card->number >= SNDRV_MINOR_OSS_DEVICES)
173
		return 0;
L
Linus Torvalds 已提交
174 175
	if (minor < 0)
		return minor;
176
	mutex_lock(&sound_oss_mutex);
177
	mptr = snd_oss_minors[minor];
L
Linus Torvalds 已提交
178
	if (mptr == NULL) {
179
		mutex_unlock(&sound_oss_mutex);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193
		return -ENOENT;
	}
	unregister_sound_special(minor);
	switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
	case SNDRV_MINOR_OSS_PCM:
		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_AUDIO);
		break;
	case SNDRV_MINOR_OSS_MIDI:
		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI);
		break;
	case SNDRV_MINOR_OSS_MIDI1:
		track2 = SNDRV_MINOR_OSS(cidx, SNDRV_MINOR_OSS_DMMIDI1);
		break;
	}
194
	if (track2 >= 0) {
L
Linus Torvalds 已提交
195
		unregister_sound_special(track2);
196 197
		snd_oss_minors[track2] = NULL;
	}
198
	snd_oss_minors[minor] = NULL;
199
	mutex_unlock(&sound_oss_mutex);
L
Linus Torvalds 已提交
200 201 202 203
	kfree(mptr);
	return 0;
}

204 205
EXPORT_SYMBOL(snd_unregister_oss_device);

L
Linus Torvalds 已提交
206 207 208 209
/*
 *  INFO PART
 */

210
#ifdef CONFIG_SND_PROC_FS
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static const char *snd_oss_device_type_name(int type)
{
	switch (type) {
	case SNDRV_OSS_DEVICE_TYPE_MIXER:
		return "mixer";
	case SNDRV_OSS_DEVICE_TYPE_SEQUENCER:
	case SNDRV_OSS_DEVICE_TYPE_MUSIC:
		return "sequencer";
	case SNDRV_OSS_DEVICE_TYPE_PCM:
		return "digital audio";
	case SNDRV_OSS_DEVICE_TYPE_MIDI:
		return "raw midi";
	case SNDRV_OSS_DEVICE_TYPE_DMFM:
		return "hardware dependent";
	default:
		return "?";
	}
}

230 231
static void snd_minor_info_oss_read(struct snd_info_entry *entry,
				    struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
232
{
233
	int minor;
234
	struct snd_minor *mptr;
L
Linus Torvalds 已提交
235

236
	mutex_lock(&sound_oss_mutex);
237 238 239 240 241 242 243 244 245 246
	for (minor = 0; minor < SNDRV_OSS_MINORS; ++minor) {
		if (!(mptr = snd_oss_minors[minor]))
			continue;
		if (mptr->card >= 0)
			snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n", minor,
				    mptr->card, mptr->device,
				    snd_oss_device_type_name(mptr->type));
		else
			snd_iprintf(buffer, "%3i:       : %s\n", minor,
				    snd_oss_device_type_name(mptr->type));
L
Linus Torvalds 已提交
247
	}
248
	mutex_unlock(&sound_oss_mutex);
L
Linus Torvalds 已提交
249 250 251 252 253
}


int __init snd_minor_info_oss_init(void)
{
254
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
255 256

	entry = snd_info_create_module_entry(THIS_MODULE, "devices", snd_oss_root);
257 258 259 260
	if (!entry)
		return -ENOMEM;
	entry->c.text.read = snd_minor_info_oss_read;
	return snd_info_register(entry); /* freed in error path */
L
Linus Torvalds 已提交
261
}
262
#endif /* CONFIG_SND_PROC_FS */