sound.c 12.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 23 24
 *
 *
 *   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>
#include <linux/slab.h>
#include <linux/time.h>
T
Takashi Iwai 已提交
25
#include <linux/device.h>
26
#include <linux/module.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32
#include <sound/core.h>
#include <sound/minors.h>
#include <sound/info.h>
#include <sound/control.h>
#include <sound/initval.h>
#include <linux/kmod.h>
33
#include <linux/mutex.h>
L
Linus Torvalds 已提交
34 35 36

static int major = CONFIG_SND_MAJOR;
int snd_major;
37 38
EXPORT_SYMBOL(snd_major);

L
Linus Torvalds 已提交
39 40
static int cards_limit = 1;

41
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
MODULE_DESCRIPTION("Advanced Linux Sound Architecture driver for soundcards.");
MODULE_LICENSE("GPL");
module_param(major, int, 0444);
MODULE_PARM_DESC(major, "Major # for sound driver.");
module_param(cards_limit, int, 0444);
MODULE_PARM_DESC(cards_limit, "Count of auto-loadable soundcards.");
MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);

/* this one holds the actual max. card number currently available.
 * as default, it's identical with cards_limit option.  when more
 * modules are loaded manually, this limit number increases, too.
 */
int snd_ecards_limit;
55
EXPORT_SYMBOL(snd_ecards_limit);
L
Linus Torvalds 已提交
56

57
static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
58
static DEFINE_MUTEX(sound_mutex);
L
Linus Torvalds 已提交
59

60
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
61 62 63 64 65 66

/**
 * snd_request_card - try to load the card module
 * @card: the card number
 *
 * Tries to load the module "snd-card-X" for the given card number
67
 * via request_module.  Returns immediately if already loaded.
L
Linus Torvalds 已提交
68 69 70
 */
void snd_request_card(int card)
{
71
	if (snd_card_locked(card))
L
Linus Torvalds 已提交
72 73 74 75 76 77
		return;
	if (card < 0 || card >= cards_limit)
		return;
	request_module("snd-card-%i", card);
}

78 79
EXPORT_SYMBOL(snd_request_card);

L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91
static void snd_request_other(int minor)
{
	char *str;

	switch (minor) {
	case SNDRV_MINOR_SEQUENCER:	str = "snd-seq";	break;
	case SNDRV_MINOR_TIMER:		str = "snd-timer";	break;
	default:			return;
	}
	request_module(str);
}

92
#endif	/* modular kernel */
L
Linus Torvalds 已提交
93

94 95 96 97 98 99 100
/**
 * snd_lookup_minor_data - get user data of a registered device
 * @minor: the minor number
 * @type: device type (SNDRV_DEVICE_TYPE_XXX)
 *
 * Checks that a minor device with the specified type is registered, and returns
 * its user data pointer.
101 102 103 104
 *
 * This function increments the reference counter of the card instance
 * if an associated instance with the given minor number and type is found.
 * The caller must call snd_card_unref() appropriately later.
105 106 107
 *
 * Return: The user data pointer if the specified device is found. %NULL
 * otherwise.
108 109 110 111 112 113
 */
void *snd_lookup_minor_data(unsigned int minor, int type)
{
	struct snd_minor *mreg;
	void *private_data;

114
	if (minor >= ARRAY_SIZE(snd_minors))
115
		return NULL;
116
	mutex_lock(&sound_mutex);
117
	mreg = snd_minors[minor];
118
	if (mreg && mreg->type == type) {
119
		private_data = mreg->private_data;
T
Takashi Iwai 已提交
120
		if (private_data && mreg->card_ptr)
121 122
			atomic_inc(&mreg->card_ptr->refcount);
	} else
123
		private_data = NULL;
124
	mutex_unlock(&sound_mutex);
125 126 127
	return private_data;
}

128 129
EXPORT_SYMBOL(snd_lookup_minor_data);

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#ifdef CONFIG_MODULES
static struct snd_minor *autoload_device(unsigned int minor)
{
	int dev;
	mutex_unlock(&sound_mutex); /* release lock temporarily */
	dev = SNDRV_MINOR_DEVICE(minor);
	if (dev == SNDRV_MINOR_CONTROL) {
		/* /dev/aloadC? */
		int card = SNDRV_MINOR_CARD(minor);
		if (snd_cards[card] == NULL)
			snd_request_card(card);
	} else if (dev == SNDRV_MINOR_GLOBAL) {
		/* /dev/aloadSEQ */
		snd_request_other(minor);
	}
	mutex_lock(&sound_mutex); /* reacuire lock */
	return snd_minors[minor];
}
#else /* !CONFIG_MODULES */
#define autoload_device(minor)	NULL
#endif /* CONFIG_MODULES */

static int snd_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
153
{
154
	unsigned int minor = iminor(inode);
155
	struct snd_minor *mptr = NULL;
156
	const struct file_operations *old_fops;
L
Linus Torvalds 已提交
157 158
	int err = 0;

159
	if (minor >= ARRAY_SIZE(snd_minors))
160
		return -ENODEV;
161
	mutex_lock(&sound_mutex);
162 163
	mptr = snd_minors[minor];
	if (mptr == NULL) {
164 165 166
		mptr = autoload_device(minor);
		if (!mptr) {
			mutex_unlock(&sound_mutex);
167
			return -ENODEV;
168
		}
L
Linus Torvalds 已提交
169 170 171
	}
	old_fops = file->f_op;
	file->f_op = fops_get(mptr->f_ops);
L
Laurent Pinchart 已提交
172 173
	if (file->f_op == NULL) {
		file->f_op = old_fops;
174
		err = -ENODEV;
L
Laurent Pinchart 已提交
175
	}
176 177 178 179 180
	mutex_unlock(&sound_mutex);
	if (err < 0)
		return err;

	if (file->f_op->open) {
L
Linus Torvalds 已提交
181
		err = file->f_op->open(inode, file);
182 183 184 185
		if (err) {
			fops_put(file->f_op);
			file->f_op = fops_get(old_fops);
		}
L
Linus Torvalds 已提交
186 187 188 189 190
	}
	fops_put(old_fops);
	return err;
}

191
static const struct file_operations snd_fops =
L
Linus Torvalds 已提交
192 193
{
	.owner =	THIS_MODULE,
194 195
	.open =		snd_open,
	.llseek =	noop_llseek,
L
Linus Torvalds 已提交
196 197
};

198
#ifdef CONFIG_SND_DYNAMIC_MINORS
199
static int snd_find_free_minor(int type)
200 201 202
{
	int minor;

203 204 205 206 207 208
	/* static minors for module auto loading */
	if (type == SNDRV_DEVICE_TYPE_SEQUENCER)
		return SNDRV_MINOR_SEQUENCER;
	if (type == SNDRV_DEVICE_TYPE_TIMER)
		return SNDRV_MINOR_TIMER;

209
	for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
210 211 212 213 214
		/* skip static minors still used for module auto loading */
		if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL)
			continue;
		if (minor == SNDRV_MINOR_SEQUENCER ||
		    minor == SNDRV_MINOR_TIMER)
215 216 217 218 219 220 221
			continue;
		if (!snd_minors[minor])
			return minor;
	}
	return -EBUSY;
}
#else
222
static int snd_kernel_minor(int type, struct snd_card *card, int dev)
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230 231
{
	int minor;

	switch (type) {
	case SNDRV_DEVICE_TYPE_SEQUENCER:
	case SNDRV_DEVICE_TYPE_TIMER:
		minor = type;
		break;
	case SNDRV_DEVICE_TYPE_CONTROL:
232 233
		if (snd_BUG_ON(!card))
			return -EINVAL;
L
Linus Torvalds 已提交
234 235 236 237 238 239
		minor = SNDRV_MINOR(card->number, type);
		break;
	case SNDRV_DEVICE_TYPE_HWDEP:
	case SNDRV_DEVICE_TYPE_RAWMIDI:
	case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
	case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
240
	case SNDRV_DEVICE_TYPE_COMPRESS:
241 242
		if (snd_BUG_ON(!card))
			return -EINVAL;
L
Linus Torvalds 已提交
243 244 245 246 247
		minor = SNDRV_MINOR(card->number, type + dev);
		break;
	default:
		return -EINVAL;
	}
248 249
	if (snd_BUG_ON(minor < 0 || minor >= SNDRV_OS_MINORS))
		return -EINVAL;
L
Linus Torvalds 已提交
250 251
	return minor;
}
252
#endif
L
Linus Torvalds 已提交
253 254

/**
255
 * snd_register_device_for_dev - Register the ALSA device file for the card
L
Linus Torvalds 已提交
256 257 258
 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
 * @card: the card instance
 * @dev: the device index
259
 * @f_ops: the file operations
260
 * @private_data: user pointer for f_ops->open()
L
Linus Torvalds 已提交
261
 * @name: the device file name
262
 * @device: the &struct device to link this new device to
L
Linus Torvalds 已提交
263 264 265 266
 *
 * Registers an ALSA device file for the given card.
 * The operators have to be set in reg parameter.
 *
267
 * Return: Zero if successful, or a negative error code on failure.
L
Linus Torvalds 已提交
268
 */
269 270 271 272
int snd_register_device_for_dev(int type, struct snd_card *card, int dev,
				const struct file_operations *f_ops,
				void *private_data,
				const char *name, struct device *device)
L
Linus Torvalds 已提交
273
{
274
	int minor;
275
	struct snd_minor *preg;
L
Linus Torvalds 已提交
276

277 278
	if (snd_BUG_ON(!name))
		return -EINVAL;
279
	preg = kmalloc(sizeof *preg, GFP_KERNEL);
L
Linus Torvalds 已提交
280 281
	if (preg == NULL)
		return -ENOMEM;
282
	preg->type = type;
283
	preg->card = card ? card->number : -1;
L
Linus Torvalds 已提交
284
	preg->device = dev;
285
	preg->f_ops = f_ops;
286
	preg->private_data = private_data;
287
	preg->card_ptr = card;
288
	mutex_lock(&sound_mutex);
289
#ifdef CONFIG_SND_DYNAMIC_MINORS
290
	minor = snd_find_free_minor(type);
291 292 293 294 295 296
#else
	minor = snd_kernel_minor(type, card, dev);
	if (minor >= 0 && snd_minors[minor])
		minor = -EBUSY;
#endif
	if (minor < 0) {
297
		mutex_unlock(&sound_mutex);
L
Linus Torvalds 已提交
298
		kfree(preg);
299
		return minor;
L
Linus Torvalds 已提交
300
	}
301
	snd_minors[minor] = preg;
302 303
	preg->dev = device_create(sound_class, device, MKDEV(major, minor),
				  private_data, "%s", name);
304 305 306 307 308 309 310 311
	if (IS_ERR(preg->dev)) {
		snd_minors[minor] = NULL;
		mutex_unlock(&sound_mutex);
		minor = PTR_ERR(preg->dev);
		kfree(preg);
		return minor;
	}

312
	mutex_unlock(&sound_mutex);
L
Linus Torvalds 已提交
313 314 315
	return 0;
}

316
EXPORT_SYMBOL(snd_register_device_for_dev);
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
/* find the matching minor record
 * return the index of snd_minor, or -1 if not found
 */
static int find_snd_minor(int type, struct snd_card *card, int dev)
{
	int cardnum, minor;
	struct snd_minor *mptr;

	cardnum = card ? card->number : -1;
	for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
		if ((mptr = snd_minors[minor]) != NULL &&
		    mptr->type == type &&
		    mptr->card == cardnum &&
		    mptr->device == dev)
			return minor;
	return -1;
}

L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344
/**
 * snd_unregister_device - unregister the device on the given card
 * @type: the device type, SNDRV_DEVICE_TYPE_XXX
 * @card: the card instance
 * @dev: the device index
 *
 * Unregisters the device file already registered via
 * snd_register_device().
 *
345
 * Return: Zero if successful, or a negative error code on failure.
L
Linus Torvalds 已提交
346
 */
347
int snd_unregister_device(int type, struct snd_card *card, int dev)
L
Linus Torvalds 已提交
348
{
349
	int minor;
L
Linus Torvalds 已提交
350

351
	mutex_lock(&sound_mutex);
352 353
	minor = find_snd_minor(type, card, dev);
	if (minor < 0) {
354
		mutex_unlock(&sound_mutex);
L
Linus Torvalds 已提交
355 356 357
		return -EINVAL;
	}

358
	device_destroy(sound_class, MKDEV(major, minor));
L
Linus Torvalds 已提交
359

360
	kfree(snd_minors[minor]);
361
	snd_minors[minor] = NULL;
362
	mutex_unlock(&sound_mutex);
L
Linus Torvalds 已提交
363 364 365
	return 0;
}

366 367
EXPORT_SYMBOL(snd_unregister_device);

368
int snd_add_device_sysfs_file(int type, struct snd_card *card, int dev,
369
			      struct device_attribute *attr)
370 371
{
	int minor, ret = -EINVAL;
372
	struct device *d;
373 374 375

	mutex_lock(&sound_mutex);
	minor = find_snd_minor(type, card, dev);
376 377
	if (minor >= 0 && (d = snd_minors[minor]->dev) != NULL)
		ret = device_create_file(d, attr);
378 379 380 381 382 383 384
	mutex_unlock(&sound_mutex);
	return ret;

}

EXPORT_SYMBOL(snd_add_device_sysfs_file);

385
#ifdef CONFIG_PROC_FS
L
Linus Torvalds 已提交
386 387 388 389
/*
 *  INFO PART
 */

390
static struct snd_info_entry *snd_minor_info_entry;
L
Linus Torvalds 已提交
391

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
static const char *snd_device_type_name(int type)
{
	switch (type) {
	case SNDRV_DEVICE_TYPE_CONTROL:
		return "control";
	case SNDRV_DEVICE_TYPE_HWDEP:
		return "hardware dependent";
	case SNDRV_DEVICE_TYPE_RAWMIDI:
		return "raw midi";
	case SNDRV_DEVICE_TYPE_PCM_PLAYBACK:
		return "digital audio playback";
	case SNDRV_DEVICE_TYPE_PCM_CAPTURE:
		return "digital audio capture";
	case SNDRV_DEVICE_TYPE_SEQUENCER:
		return "sequencer";
	case SNDRV_DEVICE_TYPE_TIMER:
		return "timer";
	default:
		return "?";
	}
}

414
static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
415
{
416
	int minor;
417
	struct snd_minor *mptr;
L
Linus Torvalds 已提交
418

419
	mutex_lock(&sound_mutex);
420 421 422 423 424
	for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
		if (!(mptr = snd_minors[minor]))
			continue;
		if (mptr->card >= 0) {
			if (mptr->device >= 0)
425
				snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n",
426 427 428
					    minor, mptr->card, mptr->device,
					    snd_device_type_name(mptr->type));
			else
429
				snd_iprintf(buffer, "%3i: [%2i]   : %s\n",
430 431 432
					    minor, mptr->card,
					    snd_device_type_name(mptr->type));
		} else
433
			snd_iprintf(buffer, "%3i:        : %s\n", minor,
434
				    snd_device_type_name(mptr->type));
L
Linus Torvalds 已提交
435
	}
436
	mutex_unlock(&sound_mutex);
L
Linus Torvalds 已提交
437 438 439 440
}

int __init snd_minor_info_init(void)
{
441
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

	entry = snd_info_create_module_entry(THIS_MODULE, "devices", NULL);
	if (entry) {
		entry->c.text.read = snd_minor_info_read;
		if (snd_info_register(entry) < 0) {
			snd_info_free_entry(entry);
			entry = NULL;
		}
	}
	snd_minor_info_entry = entry;
	return 0;
}

int __exit snd_minor_info_done(void)
{
457
	snd_info_free_entry(snd_minor_info_entry);
L
Linus Torvalds 已提交
458 459
	return 0;
}
460
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

/*
 *  INIT PART
 */

static int __init alsa_sound_init(void)
{
	snd_major = major;
	snd_ecards_limit = cards_limit;
	if (register_chrdev(major, "alsa", &snd_fops)) {
		snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
		return -EIO;
	}
	if (snd_info_init() < 0) {
		unregister_chrdev(major, "alsa");
		return -ENOMEM;
	}
	snd_info_minor_register();
#ifndef MODULE
480
	printk(KERN_INFO "Advanced Linux Sound Architecture Driver Initialized.\n");
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488
#endif
	return 0;
}

static void __exit alsa_sound_exit(void)
{
	snd_info_minor_unregister();
	snd_info_done();
489
	unregister_chrdev(major, "alsa");
L
Linus Torvalds 已提交
490 491
}

492 493
subsys_initcall(alsa_sound_init);
module_exit(alsa_sound_exit);