blob: 46c0d03dbecced68ca318dbbe20df52d1da4ab0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/sound/oss/soundcard.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Sound card driver for Linux
5 *
6 *
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
12 *
13 *
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
15 * integrated sound_switch.c
16 * Stefan Reinauer : integrated /proc/sound (equals to /dev/sndstat,
17 * which should disappear in the near future)
18 * Eric Dumas : devfs support (22-Jan-98) <dumas@linux.eu.org> with
19 * fixups by C. Scott Ananian <cananian@alumni.princeton.edu>
20 * Richard Gooch : moved common (non OSS-specific) devices to sound_core.c
21 * Rob Riggs : Added persistent DMA buffers support (1998/10/17)
22 * Christoph Hellwig : Some cleanup work (2000/03/01)
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "sound_config.h"
27#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/errno.h>
30#include <linux/signal.h>
31#include <linux/fcntl.h>
32#include <linux/ctype.h>
33#include <linux/stddef.h>
34#include <linux/kmod.h>
Ahmed S. Darwishb322f402007-02-12 00:52:07 -080035#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/dma.h>
37#include <asm/io.h>
38#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/major.h>
41#include <linux/delay.h>
42#include <linux/proc_fs.h>
Arnd Bergmann645ef9e2010-09-14 21:53:41 +020043#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/module.h>
Al Virof23f6e02006-10-20 15:17:02 -040045#include <linux/mm.h>
Jean Delvare6473d162007-03-06 02:45:12 -080046#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * This ought to be moved into include/asm/dma.h
50 */
51#ifndef valid_dma
52#define valid_dma(n) ((n) >= 0 && (n) < MAX_DMA_CHANNELS && (n) != 4)
53#endif
54
55/*
56 * Table for permanently allocated memory (used when unloading the module)
57 */
Dan Carpenter444c1952010-01-03 12:39:27 +010058void * sound_mem_blocks[MAX_MEM_BLOCKS];
Arnd Bergmann645ef9e2010-09-14 21:53:41 +020059static DEFINE_MUTEX(soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060int sound_nblocks = 0;
61
62/* Persistent DMA buffers */
63#ifdef CONFIG_SOUND_DMAP
64int sound_dmap_flag = 1;
65#else
66int sound_dmap_flag = 0;
67#endif
68
69static char dma_alloc_map[MAX_DMA_CHANNELS];
70
71#define DMA_MAP_UNAVAIL 0
72#define DMA_MAP_FREE 1
73#define DMA_MAP_BUSY 2
74
75
76unsigned long seq_time = 0; /* Time for /dev/sequencer */
gregkh@suse.de619e6662005-03-23 09:51:41 -080077extern struct class *sound_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * Table for configurable mixer volume handling
81 */
82static mixer_vol_table mixer_vols[MAX_MIXER_DEV];
83static int num_mixer_volumes;
84
85int *load_mixer_volumes(char *name, int *levels, int present)
86{
87 int i, n;
88
89 for (i = 0; i < num_mixer_volumes; i++) {
90 if (strcmp(name, mixer_vols[i].name) == 0) {
91 if (present)
92 mixer_vols[i].num = i;
93 return mixer_vols[i].levels;
94 }
95 }
96 if (num_mixer_volumes >= MAX_MIXER_DEV) {
97 printk(KERN_ERR "Sound: Too many mixers (%s)\n", name);
98 return levels;
99 }
100 n = num_mixer_volumes++;
101
102 strcpy(mixer_vols[n].name, name);
103
104 if (present)
105 mixer_vols[n].num = n;
106 else
107 mixer_vols[n].num = -1;
108
109 for (i = 0; i < 32; i++)
110 mixer_vols[n].levels[i] = levels[i];
111 return mixer_vols[n].levels;
112}
Adrian Bunkece7f772006-10-04 02:17:31 -0700113EXPORT_SYMBOL(load_mixer_volumes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static int set_mixer_levels(void __user * arg)
116{
117 /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */
118 mixer_vol_table buf;
119
120 if (__copy_from_user(&buf, arg, sizeof(buf)))
121 return -EFAULT;
122 load_mixer_volumes(buf.name, buf.levels, 0);
123 if (__copy_to_user(arg, &buf, sizeof(buf)))
124 return -EFAULT;
125 return 0;
126}
127
128static int get_mixer_levels(void __user * arg)
129{
130 int n;
131
132 if (__get_user(n, (int __user *)(&(((mixer_vol_table __user *)arg)->num))))
133 return -EFAULT;
134 if (n < 0 || n >= num_mixer_volumes)
135 return -EINVAL;
136 if (__copy_to_user(arg, &mixer_vols[n], sizeof(mixer_vol_table)))
137 return -EFAULT;
138 return 0;
139}
140
141/* 4K page size but our output routines use some slack for overruns */
142#define PROC_BLOCK_SIZE (3*1024)
143
144static ssize_t sound_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
145{
Josef Sipek7bc56322006-12-08 02:37:40 -0800146 int dev = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int ret = -EINVAL;
148
149 /*
150 * The OSS drivers aren't remotely happy without this locking,
151 * and unless someone fixes them when they are about to bite the
152 * big one anyway, we might as well bandage here..
153 */
154
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200155 mutex_lock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 DEB(printk("sound_read(dev=%d, count=%d)\n", dev, count));
158 switch (dev & 0x0f) {
159 case SND_DEV_DSP:
160 case SND_DEV_DSP16:
161 case SND_DEV_AUDIO:
162 ret = audio_read(dev, file, buf, count);
163 break;
164
165 case SND_DEV_SEQ:
166 case SND_DEV_SEQ2:
167 ret = sequencer_read(dev, file, buf, count);
168 break;
169
170 case SND_DEV_MIDIN:
171 ret = MIDIbuf_read(dev, file, buf, count);
172 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200173 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return ret;
175}
176
177static ssize_t sound_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
178{
Josef Sipek7bc56322006-12-08 02:37:40 -0800179 int dev = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int ret = -EINVAL;
181
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200182 mutex_lock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 DEB(printk("sound_write(dev=%d, count=%d)\n", dev, count));
184 switch (dev & 0x0f) {
185 case SND_DEV_SEQ:
186 case SND_DEV_SEQ2:
187 ret = sequencer_write(dev, file, buf, count);
188 break;
189
190 case SND_DEV_DSP:
191 case SND_DEV_DSP16:
192 case SND_DEV_AUDIO:
193 ret = audio_write(dev, file, buf, count);
194 break;
195
196 case SND_DEV_MIDIN:
197 ret = MIDIbuf_write(dev, file, buf, count);
198 break;
199 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200200 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return ret;
202}
203
204static int sound_open(struct inode *inode, struct file *file)
205{
206 int dev = iminor(inode);
207 int retval;
208
209 DEB(printk("sound_open(dev=%d)\n", dev));
210 if ((dev >= SND_NDEVS) || (dev < 0)) {
211 printk(KERN_ERR "Invalid minor device %d\n", dev);
212 return -ENXIO;
213 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200214 mutex_lock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 switch (dev & 0x0f) {
216 case SND_DEV_CTL:
217 dev >>= 4;
218 if (dev >= 0 && dev < MAX_MIXER_DEV && mixer_devs[dev] == NULL) {
219 request_module("mixer%d", dev);
220 }
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200221 retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (dev && (dev >= num_mixers || mixer_devs[dev] == NULL))
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!try_module_get(mixer_devs[dev]->owner))
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200226 break;
227
228 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230
231 case SND_DEV_SEQ:
232 case SND_DEV_SEQ2:
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200233 retval = sequencer_open(dev, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235
236 case SND_DEV_MIDIN:
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200237 retval = MIDIbuf_open(dev, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
239
240 case SND_DEV_DSP:
241 case SND_DEV_DSP16:
242 case SND_DEV_AUDIO:
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200243 retval = audio_open(dev, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245
246 default:
247 printk(KERN_ERR "Invalid minor device %d\n", dev);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200248 retval = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200251 mutex_unlock(&soundcard_mutex);
Dan Carpenterb7d22cc2010-10-10 19:34:22 +0200252 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static int sound_release(struct inode *inode, struct file *file)
256{
257 int dev = iminor(inode);
258
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200259 mutex_lock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 DEB(printk("sound_release(dev=%d)\n", dev));
261 switch (dev & 0x0f) {
262 case SND_DEV_CTL:
263 module_put(mixer_devs[dev >> 4]->owner);
264 break;
265
266 case SND_DEV_SEQ:
267 case SND_DEV_SEQ2:
268 sequencer_release(dev, file);
269 break;
270
271 case SND_DEV_MIDIN:
272 MIDIbuf_release(dev, file);
273 break;
274
275 case SND_DEV_DSP:
276 case SND_DEV_DSP16:
277 case SND_DEV_AUDIO:
278 audio_release(dev, file);
279 break;
280
281 default:
282 printk(KERN_ERR "Sound error: Releasing unknown device 0x%02x\n", dev);
283 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200284 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return 0;
287}
288
289static int get_mixer_info(int dev, void __user *arg)
290{
291 mixer_info info;
292 memset(&info, 0, sizeof(info));
293 strlcpy(info.id, mixer_devs[dev]->id, sizeof(info.id));
294 strlcpy(info.name, mixer_devs[dev]->name, sizeof(info.name));
295 info.modify_counter = mixer_devs[dev]->modify_counter;
296 if (__copy_to_user(arg, &info, sizeof(info)))
297 return -EFAULT;
298 return 0;
299}
300
301static int get_old_mixer_info(int dev, void __user *arg)
302{
303 _old_mixer_info info;
304 memset(&info, 0, sizeof(info));
305 strlcpy(info.id, mixer_devs[dev]->id, sizeof(info.id));
306 strlcpy(info.name, mixer_devs[dev]->name, sizeof(info.name));
307 if (copy_to_user(arg, &info, sizeof(info)))
308 return -EFAULT;
309 return 0;
310}
311
312static int sound_mixer_ioctl(int mixdev, unsigned int cmd, void __user *arg)
313{
314 if (mixdev < 0 || mixdev >= MAX_MIXER_DEV)
315 return -ENXIO;
316 /* Try to load the mixer... */
317 if (mixer_devs[mixdev] == NULL) {
318 request_module("mixer%d", mixdev);
319 }
320 if (mixdev >= num_mixers || !mixer_devs[mixdev])
321 return -ENXIO;
322 if (cmd == SOUND_MIXER_INFO)
323 return get_mixer_info(mixdev, arg);
324 if (cmd == SOUND_OLD_MIXER_INFO)
325 return get_old_mixer_info(mixdev, arg);
326 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
327 mixer_devs[mixdev]->modify_counter++;
328 if (!mixer_devs[mixdev]->ioctl)
329 return -EINVAL;
330 return mixer_devs[mixdev]->ioctl(mixdev, cmd, arg);
331}
332
Alan Cox6b985152010-01-04 16:22:59 +0000333static long sound_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int len = 0, dtype;
Alan Cox6b985152010-01-04 16:22:59 +0000336 int dev = iminor(file->f_dentry->d_inode);
337 long ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 void __user *p = (void __user *)arg;
339
340 if (_SIOC_DIR(cmd) != _SIOC_NONE && _SIOC_DIR(cmd) != 0) {
341 /*
342 * Have to validate the address given by the process.
343 */
344 len = _SIOC_SIZE(cmd);
345 if (len < 1 || len > 65536 || !p)
346 return -EFAULT;
347 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
348 if (!access_ok(VERIFY_READ, p, len))
349 return -EFAULT;
350 if (_SIOC_DIR(cmd) & _SIOC_READ)
351 if (!access_ok(VERIFY_WRITE, p, len))
352 return -EFAULT;
353 }
354 DEB(printk("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)\n", dev, cmd, arg));
355 if (cmd == OSS_GETVERSION)
356 return __put_user(SOUND_VERSION, (int __user *)p);
357
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200358 mutex_lock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (_IOC_TYPE(cmd) == 'M' && num_mixers > 0 && /* Mixer ioctl */
360 (dev & 0x0f) != SND_DEV_CTL) {
361 dtype = dev & 0x0f;
362 switch (dtype) {
363 case SND_DEV_DSP:
364 case SND_DEV_DSP16:
365 case SND_DEV_AUDIO:
Alan Cox6b985152010-01-04 16:22:59 +0000366 ret = sound_mixer_ioctl(audio_devs[dev >> 4]->mixer_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 cmd, p);
Alan Cox6b985152010-01-04 16:22:59 +0000368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 default:
Alan Cox6b985152010-01-04 16:22:59 +0000370 ret = sound_mixer_ioctl(dev >> 4, cmd, p);
371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200373 mutex_unlock(&soundcard_mutex);
Alan Cox6b985152010-01-04 16:22:59 +0000374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Alan Cox6b985152010-01-04 16:22:59 +0000376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 switch (dev & 0x0f) {
378 case SND_DEV_CTL:
379 if (cmd == SOUND_MIXER_GETLEVELS)
Alan Cox6b985152010-01-04 16:22:59 +0000380 ret = get_mixer_levels(p);
381 else if (cmd == SOUND_MIXER_SETLEVELS)
382 ret = set_mixer_levels(p);
383 else
384 ret = sound_mixer_ioctl(dev >> 4, cmd, p);
385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 case SND_DEV_SEQ:
388 case SND_DEV_SEQ2:
Alan Cox6b985152010-01-04 16:22:59 +0000389 ret = sequencer_ioctl(dev, file, cmd, p);
390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 case SND_DEV_DSP:
393 case SND_DEV_DSP16:
394 case SND_DEV_AUDIO:
Dan Carpenterd4cfa4d2010-10-10 19:33:52 +0200395 ret = audio_ioctl(dev, file, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397
398 case SND_DEV_MIDIN:
Dan Carpenterd4cfa4d2010-10-10 19:33:52 +0200399 ret = MIDIbuf_ioctl(dev, file, cmd, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
401
402 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200403 mutex_unlock(&soundcard_mutex);
Alan Cox6b985152010-01-04 16:22:59 +0000404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407static unsigned int sound_poll(struct file *file, poll_table * wait)
408{
Josef Sipek7bc56322006-12-08 02:37:40 -0800409 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int dev = iminor(inode);
411
412 DEB(printk("sound_poll(dev=%d)\n", dev));
413 switch (dev & 0x0f) {
414 case SND_DEV_SEQ:
415 case SND_DEV_SEQ2:
416 return sequencer_poll(dev, file, wait);
417
418 case SND_DEV_MIDIN:
419 return MIDIbuf_poll(dev, file, wait);
420
421 case SND_DEV_DSP:
422 case SND_DEV_DSP16:
423 case SND_DEV_AUDIO:
424 return DMAbuf_poll(file, dev >> 4, wait);
425 }
426 return 0;
427}
428
429static int sound_mmap(struct file *file, struct vm_area_struct *vma)
430{
431 int dev_class;
432 unsigned long size;
433 struct dma_buffparms *dmap = NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -0800434 int dev = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 dev_class = dev & 0x0f;
437 dev >>= 4;
438
439 if (dev_class != SND_DEV_DSP && dev_class != SND_DEV_DSP16 && dev_class != SND_DEV_AUDIO) {
440 printk(KERN_ERR "Sound: mmap() not supported for other than audio devices\n");
441 return -EINVAL;
442 }
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200443 mutex_lock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (vma->vm_flags & VM_WRITE) /* Map write and read/write to the output buf */
445 dmap = audio_devs[dev]->dmap_out;
446 else if (vma->vm_flags & VM_READ)
447 dmap = audio_devs[dev]->dmap_in;
448 else {
449 printk(KERN_ERR "Sound: Undefined mmap() access\n");
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200450 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return -EINVAL;
452 }
453
454 if (dmap == NULL) {
455 printk(KERN_ERR "Sound: mmap() error. dmap == NULL\n");
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200456 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -EIO;
458 }
459 if (dmap->raw_buf == NULL) {
460 printk(KERN_ERR "Sound: mmap() called when raw_buf == NULL\n");
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200461 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -EIO;
463 }
464 if (dmap->mapping_flags) {
465 printk(KERN_ERR "Sound: mmap() called twice for the same DMA buffer\n");
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200466 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EIO;
468 }
469 if (vma->vm_pgoff != 0) {
470 printk(KERN_ERR "Sound: mmap() offset must be 0.\n");
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200471 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EINVAL;
473 }
474 size = vma->vm_end - vma->vm_start;
475
476 if (size != dmap->bytes_in_use) {
477 printk(KERN_WARNING "Sound: mmap() size = %ld. Should be %d\n", size, dmap->bytes_in_use);
478 }
479 if (remap_pfn_range(vma, vma->vm_start,
480 virt_to_phys(dmap->raw_buf) >> PAGE_SHIFT,
481 vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200482 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return -EAGAIN;
484 }
485
486 dmap->mapping_flags |= DMA_MAP_MAPPED;
487
488 if( audio_devs[dev]->d->mmap)
489 audio_devs[dev]->d->mmap(dev);
490
491 memset(dmap->raw_buf,
492 dmap->neutral_byte,
493 dmap->bytes_in_use);
Arnd Bergmann645ef9e2010-09-14 21:53:41 +0200494 mutex_unlock(&soundcard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
497
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -0800498const struct file_operations oss_sound_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .owner = THIS_MODULE,
500 .llseek = no_llseek,
501 .read = sound_read,
502 .write = sound_write,
503 .poll = sound_poll,
Alan Cox6b985152010-01-04 16:22:59 +0000504 .unlocked_ioctl = sound_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .mmap = sound_mmap,
506 .open = sound_open,
507 .release = sound_release,
508};
509
510/*
511 * Create the required special subdevices
512 */
513
514static int create_special_devices(void)
515{
516 int seq1,seq2;
517 seq1=register_sound_special(&oss_sound_fops, 1);
518 if(seq1==-1)
519 goto bad;
520 seq2=register_sound_special(&oss_sound_fops, 8);
521 if(seq2!=-1)
522 return 0;
523 unregister_sound_special(1);
524bad:
525 return -1;
526}
527
528
529/* These device names follow the official Linux device list,
530 * Documentation/devices.txt. Let us know if there are other
531 * common names we should support for compatibility.
532 * Only those devices not created by the generic code in sound_core.c are
533 * registered here.
534 */
535static const struct {
536 unsigned short minor;
537 char *name;
538 umode_t mode;
539 int *num;
540} dev_list[] = { /* list of minor devices */
541/* seems to be some confusion here -- this device is not in the device list */
542 {SND_DEV_DSP16, "dspW", S_IWUGO | S_IRUSR | S_IRGRP,
543 &num_audiodevs},
544 {SND_DEV_AUDIO, "audio", S_IWUGO | S_IRUSR | S_IRGRP,
545 &num_audiodevs},
546};
547
548static int dmabuf;
549static int dmabug;
550
551module_param(dmabuf, int, 0444);
552module_param(dmabug, int, 0444);
553
554static int __init oss_init(void)
555{
556 int err;
557 int i, j;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#ifdef CONFIG_PCI
560 if(dmabug)
561 isa_dma_bridge_buggy = dmabug;
562#endif
563
564 err = create_special_devices();
565 if (err) {
566 printk(KERN_ERR "sound: driver already loaded/included in kernel\n");
567 return err;
568 }
569
570 /* Protecting the innocent */
571 sound_dmap_flag = (dmabuf > 0 ? 1 : 0);
572
Ahmed S. Darwishb322f402007-02-12 00:52:07 -0800573 for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
Greg Kroah-Hartmanabe9ab82008-07-21 20:03:34 -0700574 device_create(sound_class, NULL,
575 MKDEV(SOUND_MAJOR, dev_list[i].minor), NULL,
576 "%s", dev_list[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (!dev_list[i].num)
579 continue;
580
Greg Kroah-Hartman0936f262005-06-20 21:15:16 -0700581 for (j = 1; j < *dev_list[i].num; j++)
Greg Kroah-Hartmanabe9ab82008-07-21 20:03:34 -0700582 device_create(sound_class, NULL,
583 MKDEV(SOUND_MAJOR,
584 dev_list[i].minor + (j*0x10)),
585 NULL, "%s%d", dev_list[i].name, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Dan Carpenter444c1952010-01-03 12:39:27 +0100588 if (sound_nblocks >= MAX_MEM_BLOCKS - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 printk(KERN_ERR "Sound warning: Deallocation table was too small.\n");
590
591 return 0;
592}
593
594static void __exit oss_cleanup(void)
595{
596 int i, j;
597
Ahmed S. Darwishb322f402007-02-12 00:52:07 -0800598 for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700599 device_destroy(sound_class, MKDEV(SOUND_MAJOR, dev_list[i].minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!dev_list[i].num)
601 continue;
Greg Kroah-Hartman0936f262005-06-20 21:15:16 -0700602 for (j = 1; j < *dev_list[i].num; j++)
Greg Kroah-Hartmand80f19f2006-08-07 22:19:37 -0700603 device_destroy(sound_class, MKDEV(SOUND_MAJOR, dev_list[i].minor + (j*0x10)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 unregister_sound_special(1);
607 unregister_sound_special(8);
608
609 sound_stop_timer();
610
611 sequencer_unload();
612
613 for (i = 0; i < MAX_DMA_CHANNELS; i++)
614 if (dma_alloc_map[i] != DMA_MAP_UNAVAIL) {
615 printk(KERN_ERR "Sound: Hmm, DMA%d was left allocated - fixed\n", i);
616 sound_free_dma(i);
617 }
618
619 for (i = 0; i < sound_nblocks; i++)
620 vfree(sound_mem_blocks[i]);
621
622}
623
624module_init(oss_init);
625module_exit(oss_cleanup);
626MODULE_LICENSE("GPL");
Adrian Bunkece7f772006-10-04 02:17:31 -0700627MODULE_DESCRIPTION("OSS Sound subsystem");
628MODULE_AUTHOR("Hannu Savolainen, et al.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630
631int sound_alloc_dma(int chn, char *deviceID)
632{
633 int err;
634
635 if ((err = request_dma(chn, deviceID)) != 0)
636 return err;
637
638 dma_alloc_map[chn] = DMA_MAP_FREE;
639
640 return 0;
641}
Adrian Bunkece7f772006-10-04 02:17:31 -0700642EXPORT_SYMBOL(sound_alloc_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644int sound_open_dma(int chn, char *deviceID)
645{
646 if (!valid_dma(chn)) {
647 printk(KERN_ERR "sound_open_dma: Invalid DMA channel %d\n", chn);
648 return 1;
649 }
650
651 if (dma_alloc_map[chn] != DMA_MAP_FREE) {
652 printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn, dma_alloc_map[chn]);
653 return 1;
654 }
655 dma_alloc_map[chn] = DMA_MAP_BUSY;
656 return 0;
657}
Adrian Bunkece7f772006-10-04 02:17:31 -0700658EXPORT_SYMBOL(sound_open_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660void sound_free_dma(int chn)
661{
662 if (dma_alloc_map[chn] == DMA_MAP_UNAVAIL) {
663 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
664 return;
665 }
666 free_dma(chn);
667 dma_alloc_map[chn] = DMA_MAP_UNAVAIL;
668}
Adrian Bunkece7f772006-10-04 02:17:31 -0700669EXPORT_SYMBOL(sound_free_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671void sound_close_dma(int chn)
672{
673 if (dma_alloc_map[chn] != DMA_MAP_BUSY) {
674 printk(KERN_ERR "sound_close_dma: Bad access to DMA channel %d\n", chn);
675 return;
676 }
677 dma_alloc_map[chn] = DMA_MAP_FREE;
678}
Adrian Bunkece7f772006-10-04 02:17:31 -0700679EXPORT_SYMBOL(sound_close_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681static void do_sequencer_timer(unsigned long dummy)
682{
683 sequencer_timer(0);
684}
685
686
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700687static DEFINE_TIMER(seq_timer, do_sequencer_timer, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689void request_sound_timer(int count)
690{
691 extern unsigned long seq_time;
692
693 if (count < 0) {
694 seq_timer.expires = (-count) + jiffies;
695 add_timer(&seq_timer);
696 return;
697 }
698 count += seq_time;
699
700 count -= jiffies;
701
702 if (count < 1)
703 count = 1;
704
705 seq_timer.expires = (count) + jiffies;
706 add_timer(&seq_timer);
707}
708
709void sound_stop_timer(void)
710{
711 del_timer(&seq_timer);
712}
713
714void conf_printf(char *name, struct address_info *hw_config)
715{
716#ifndef CONFIG_SOUND_TRACEINIT
717 return;
718#else
719 printk("<%s> at 0x%03x", name, hw_config->io_base);
720
721 if (hw_config->irq)
722 printk(" irq %d", (hw_config->irq > 0) ? hw_config->irq : -hw_config->irq);
723
724 if (hw_config->dma != -1 || hw_config->dma2 != -1)
725 {
726 printk(" dma %d", hw_config->dma);
727 if (hw_config->dma2 != -1)
728 printk(",%d", hw_config->dma2);
729 }
730 printk("\n");
731#endif
732}
Adrian Bunkece7f772006-10-04 02:17:31 -0700733EXPORT_SYMBOL(conf_printf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735void conf_printf2(char *name, int base, int irq, int dma, int dma2)
736{
737#ifndef CONFIG_SOUND_TRACEINIT
738 return;
739#else
740 printk("<%s> at 0x%03x", name, base);
741
742 if (irq)
743 printk(" irq %d", (irq > 0) ? irq : -irq);
744
745 if (dma != -1 || dma2 != -1)
746 {
747 printk(" dma %d", dma);
748 if (dma2 != -1)
749 printk(",%d", dma2);
750 }
751 printk("\n");
752#endif
753}
Adrian Bunkece7f772006-10-04 02:17:31 -0700754EXPORT_SYMBOL(conf_printf2);
755