Blame view

sound/oss/soundcard.c 16.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
f30c22695   Uwe Zeisberger   fix file specific...
2
   * linux/sound/oss/soundcard.c
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   *
   * Sound card driver for Linux
   *
   *
   * Copyright (C) by Hannu Savolainen 1993-1997
   *
   * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
   * Version 2 (June 1991). See the "COPYING" file distributed with this software
   * for more info.
   *
   *
   * Thomas Sailer     : ioctl code reworked (vmalloc/vfree removed)
   *                   integrated sound_switch.c
   * Stefan Reinauer   : integrated /proc/sound (equals to /dev/sndstat,
   *                   which should disappear in the near future)
   * Eric Dumas	     : devfs support (22-Jan-98) <dumas@linux.eu.org> with
   *                   fixups by C. Scott Ananian <cananian@alumni.princeton.edu>
   * Richard Gooch     : moved common (non OSS-specific) devices to sound_core.c
   * Rob Riggs	     : Added persistent DMA buffers support (1998/10/17)
   * Christoph Hellwig : Some cleanup work (2000/03/01)
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
27
28
29
30
31
32
33
  
  #include "sound_config.h"
  #include <linux/init.h>
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/signal.h>
  #include <linux/fcntl.h>
  #include <linux/ctype.h>
  #include <linux/stddef.h>
  #include <linux/kmod.h>
b322f4095   Ahmed S. Darwish   [PATCH] OSS: Use ...
34
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
  #include <asm/dma.h>
  #include <asm/io.h>
  #include <linux/wait.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  #include <linux/ioport.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
  #include <linux/major.h>
  #include <linux/delay.h>
  #include <linux/proc_fs.h>
645ef9ef1   Arnd Bergmann   sound: autoconver...
42
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
  #include <linux/module.h>
f23f6e08c   Al Viro   [PATCH] severing ...
44
  #include <linux/mm.h>
6473d160b   Jean Delvare   PCI: Cleanup the ...
45
  #include <linux/device.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
49
50
51
52
53
54
55
56
  
  /*
   * This ought to be moved into include/asm/dma.h
   */
  #ifndef valid_dma
  #define valid_dma(n) ((n) >= 0 && (n) < MAX_DMA_CHANNELS && (n) != 4)
  #endif
  
  /*
   * Table for permanently allocated memory (used when unloading the module)
   */
444c1953d   Dan Carpenter   sound: oss: off b...
57
  void *          sound_mem_blocks[MAX_MEM_BLOCKS];
645ef9ef1   Arnd Bergmann   sound: autoconver...
58
  static DEFINE_MUTEX(soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  int             sound_nblocks = 0;
  
  /* Persistent DMA buffers */
  #ifdef CONFIG_SOUND_DMAP
  int             sound_dmap_flag = 1;
  #else
  int             sound_dmap_flag = 0;
  #endif
  
  static char     dma_alloc_map[MAX_DMA_CHANNELS];
  
  #define DMA_MAP_UNAVAIL		0
  #define DMA_MAP_FREE		1
  #define DMA_MAP_BUSY		2
  
  
  unsigned long seq_time = 0;	/* Time for /dev/sequencer */
619e666b7   Greg Kroah-Hartman   [PATCH] class: co...
76
  extern struct class *sound_class;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
83
84
85
86
87
88
  
  /*
   * Table for configurable mixer volume handling
   */
  static mixer_vol_table mixer_vols[MAX_MIXER_DEV];
  static int num_mixer_volumes;
  
  int *load_mixer_volumes(char *name, int *levels, int present)
  {
  	int             i, n;
  
  	for (i = 0; i < num_mixer_volumes; i++) {
d81a12bc2   Dan Rosenberg   sound: Prevent bu...
89
  		if (strncmp(name, mixer_vols[i].name, 32) == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
94
95
96
97
98
99
100
  			if (present)
  				mixer_vols[i].num = i;
  			return mixer_vols[i].levels;
  		}
  	}
  	if (num_mixer_volumes >= MAX_MIXER_DEV) {
  		printk(KERN_ERR "Sound: Too many mixers (%s)
  ", name);
  		return levels;
  	}
  	n = num_mixer_volumes++;
d81a12bc2   Dan Rosenberg   sound: Prevent bu...
101
  	strncpy(mixer_vols[n].name, name, 32);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
105
106
107
108
109
110
111
  
  	if (present)
  		mixer_vols[n].num = n;
  	else
  		mixer_vols[n].num = -1;
  
  	for (i = 0; i < 32; i++)
  		mixer_vols[n].levels[i] = levels[i];
  	return mixer_vols[n].levels;
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
112
  EXPORT_SYMBOL(load_mixer_volumes);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  
  static int set_mixer_levels(void __user * arg)
  {
          /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */
  	mixer_vol_table buf;   
  
  	if (__copy_from_user(&buf, arg, sizeof(buf)))
  		return -EFAULT;
  	load_mixer_volumes(buf.name, buf.levels, 0);
  	if (__copy_to_user(arg, &buf, sizeof(buf)))
  		return -EFAULT;
  	return 0;
  }
  
  static int get_mixer_levels(void __user * arg)
  {
  	int n;
  
  	if (__get_user(n, (int __user *)(&(((mixer_vol_table __user *)arg)->num))))
  		return -EFAULT;
  	if (n < 0 || n >= num_mixer_volumes)
  		return -EINVAL;
  	if (__copy_to_user(arg, &mixer_vols[n], sizeof(mixer_vol_table)))
  		return -EFAULT;
  	return 0;
  }
  
  /* 4K page size but our output routines use some slack for overruns */
  #define PROC_BLOCK_SIZE (3*1024)
  
  static ssize_t sound_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  {
7bc563239   Josef Sipek   [PATCH] struct pa...
145
  	int dev = iminor(file->f_path.dentry->d_inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
151
152
153
  	int ret = -EINVAL;
  
  	/*
  	 *	The OSS drivers aren't remotely happy without this locking,
  	 *	and unless someone fixes them when they are about to bite the
  	 *	big one anyway, we might as well bandage here..
  	 */
  	 
645ef9ef1   Arnd Bergmann   sound: autoconver...
154
  	mutex_lock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  	
  	DEB(printk("sound_read(dev=%d, count=%d)
  ", dev, count));
  	switch (dev & 0x0f) {
  	case SND_DEV_DSP:
  	case SND_DEV_DSP16:
  	case SND_DEV_AUDIO:
  		ret = audio_read(dev, file, buf, count);
  		break;
  
  	case SND_DEV_SEQ:
  	case SND_DEV_SEQ2:
  		ret = sequencer_read(dev, file, buf, count);
  		break;
  
  	case SND_DEV_MIDIN:
  		ret = MIDIbuf_read(dev, file, buf, count);
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
173
  	mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
178
  	return ret;
  }
  
  static ssize_t sound_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  {
7bc563239   Josef Sipek   [PATCH] struct pa...
179
  	int dev = iminor(file->f_path.dentry->d_inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
  	int ret = -EINVAL;
  	
645ef9ef1   Arnd Bergmann   sound: autoconver...
182
  	mutex_lock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  	DEB(printk("sound_write(dev=%d, count=%d)
  ", dev, count));
  	switch (dev & 0x0f) {
  	case SND_DEV_SEQ:
  	case SND_DEV_SEQ2:
  		ret =  sequencer_write(dev, file, buf, count);
  		break;
  
  	case SND_DEV_DSP:
  	case SND_DEV_DSP16:
  	case SND_DEV_AUDIO:
  		ret = audio_write(dev, file, buf, count);
  		break;
  
  	case SND_DEV_MIDIN:
  		ret =  MIDIbuf_write(dev, file, buf, count);
  		break;
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
201
  	mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  	return ret;
  }
  
  static int sound_open(struct inode *inode, struct file *file)
  {
  	int dev = iminor(inode);
  	int retval;
  
  	DEB(printk("sound_open(dev=%d)
  ", dev));
  	if ((dev >= SND_NDEVS) || (dev < 0)) {
  		printk(KERN_ERR "Invalid minor device %d
  ", dev);
  		return -ENXIO;
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
217
  	mutex_lock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
221
222
223
  	switch (dev & 0x0f) {
  	case SND_DEV_CTL:
  		dev >>= 4;
  		if (dev >= 0 && dev < MAX_MIXER_DEV && mixer_devs[dev] == NULL) {
  			request_module("mixer%d", dev);
  		}
90dc763fe   Arnd Bergmann   sound: push BKL i...
224
  		retval = -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
  		if (dev && (dev >= num_mixers || mixer_devs[dev] == NULL))
90dc763fe   Arnd Bergmann   sound: push BKL i...
226
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
  	
  		if (!try_module_get(mixer_devs[dev]->owner))
90dc763fe   Arnd Bergmann   sound: push BKL i...
229
230
231
  			break;
  
  		retval = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
235
  		break;
  
  	case SND_DEV_SEQ:
  	case SND_DEV_SEQ2:
90dc763fe   Arnd Bergmann   sound: push BKL i...
236
  		retval = sequencer_open(dev, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
  		break;
  
  	case SND_DEV_MIDIN:
90dc763fe   Arnd Bergmann   sound: push BKL i...
240
  		retval = MIDIbuf_open(dev, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
245
  		break;
  
  	case SND_DEV_DSP:
  	case SND_DEV_DSP16:
  	case SND_DEV_AUDIO:
90dc763fe   Arnd Bergmann   sound: push BKL i...
246
  		retval = audio_open(dev, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
249
250
251
  		break;
  
  	default:
  		printk(KERN_ERR "Invalid minor device %d
  ", dev);
90dc763fe   Arnd Bergmann   sound: push BKL i...
252
  		retval = -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
254
  	mutex_unlock(&soundcard_mutex);
b7d22ccf0   Dan Carpenter   OSS: soundcard: f...
255
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
258
259
260
  }
  
  static int sound_release(struct inode *inode, struct file *file)
  {
  	int dev = iminor(inode);
645ef9ef1   Arnd Bergmann   sound: autoconver...
261
  	mutex_lock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  	DEB(printk("sound_release(dev=%d)
  ", dev));
  	switch (dev & 0x0f) {
  	case SND_DEV_CTL:
  		module_put(mixer_devs[dev >> 4]->owner);
  		break;
  		
  	case SND_DEV_SEQ:
  	case SND_DEV_SEQ2:
  		sequencer_release(dev, file);
  		break;
  
  	case SND_DEV_MIDIN:
  		MIDIbuf_release(dev, file);
  		break;
  
  	case SND_DEV_DSP:
  	case SND_DEV_DSP16:
  	case SND_DEV_AUDIO:
  		audio_release(dev, file);
  		break;
  
  	default:
  		printk(KERN_ERR "Sound error: Releasing unknown device 0x%02x
  ", dev);
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
288
  	mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  
  	return 0;
  }
  
  static int get_mixer_info(int dev, void __user *arg)
  {
  	mixer_info info;
  	memset(&info, 0, sizeof(info));
  	strlcpy(info.id, mixer_devs[dev]->id, sizeof(info.id));
  	strlcpy(info.name, mixer_devs[dev]->name, sizeof(info.name));
  	info.modify_counter = mixer_devs[dev]->modify_counter;
  	if (__copy_to_user(arg, &info,  sizeof(info)))
  		return -EFAULT;
  	return 0;
  }
  
  static int get_old_mixer_info(int dev, void __user *arg)
  {
  	_old_mixer_info info;
  	memset(&info, 0, sizeof(info));
   	strlcpy(info.id, mixer_devs[dev]->id, sizeof(info.id));
   	strlcpy(info.name, mixer_devs[dev]->name, sizeof(info.name));
   	if (copy_to_user(arg, &info,  sizeof(info)))
  		return -EFAULT;
  	return 0;
  }
  
  static int sound_mixer_ioctl(int mixdev, unsigned int cmd, void __user *arg)
  {
   	if (mixdev < 0 || mixdev >= MAX_MIXER_DEV)
   		return -ENXIO;
   	/* Try to load the mixer... */
   	if (mixer_devs[mixdev] == NULL) {
   		request_module("mixer%d", mixdev);
   	}
   	if (mixdev >= num_mixers || !mixer_devs[mixdev])
   		return -ENXIO;
  	if (cmd == SOUND_MIXER_INFO)
  		return get_mixer_info(mixdev, arg);
  	if (cmd == SOUND_OLD_MIXER_INFO)
  		return get_old_mixer_info(mixdev, arg);
  	if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  		mixer_devs[mixdev]->modify_counter++;
  	if (!mixer_devs[mixdev]->ioctl)
  		return -EINVAL;
  	return mixer_devs[mixdev]->ioctl(mixdev, cmd, arg);
  }
6b98515a6   Alan Cox   sound_oss: remove...
336
  static long sound_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
  {
  	int len = 0, dtype;
6b98515a6   Alan Cox   sound_oss: remove...
339
340
  	int dev = iminor(file->f_dentry->d_inode);
  	long ret = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
  	void __user *p = (void __user *)arg;
  
  	if (_SIOC_DIR(cmd) != _SIOC_NONE && _SIOC_DIR(cmd) != 0) {
  		/*
  		 * Have to validate the address given by the process.
  		 */
  		len = _SIOC_SIZE(cmd);
  		if (len < 1 || len > 65536 || !p)
  			return -EFAULT;
  		if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  			if (!access_ok(VERIFY_READ, p, len))
  				return -EFAULT;
  		if (_SIOC_DIR(cmd) & _SIOC_READ)
  			if (!access_ok(VERIFY_WRITE, p, len))
  				return -EFAULT;
  	}
  	DEB(printk("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)
  ", dev, cmd, arg));
  	if (cmd == OSS_GETVERSION)
  		return __put_user(SOUND_VERSION, (int __user *)p);
  	
645ef9ef1   Arnd Bergmann   sound: autoconver...
362
  	mutex_lock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
364
365
366
367
368
369
  	if (_IOC_TYPE(cmd) == 'M' && num_mixers > 0 &&   /* Mixer ioctl */
  	    (dev & 0x0f) != SND_DEV_CTL) {              
  		dtype = dev & 0x0f;
  		switch (dtype) {
  		case SND_DEV_DSP:
  		case SND_DEV_DSP16:
  		case SND_DEV_AUDIO:
6b98515a6   Alan Cox   sound_oss: remove...
370
  			ret = sound_mixer_ioctl(audio_devs[dev >> 4]->mixer_dev,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
  						 cmd, p);
6b98515a6   Alan Cox   sound_oss: remove...
372
  			break;			
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
373
  		default:
6b98515a6   Alan Cox   sound_oss: remove...
374
375
  			ret = sound_mixer_ioctl(dev >> 4, cmd, p);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
  		}
645ef9ef1   Arnd Bergmann   sound: autoconver...
377
  		mutex_unlock(&soundcard_mutex);
6b98515a6   Alan Cox   sound_oss: remove...
378
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
  	}
6b98515a6   Alan Cox   sound_oss: remove...
380

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
  	switch (dev & 0x0f) {
  	case SND_DEV_CTL:
  		if (cmd == SOUND_MIXER_GETLEVELS)
6b98515a6   Alan Cox   sound_oss: remove...
384
385
386
387
388
389
  			ret = get_mixer_levels(p);
  		else if (cmd == SOUND_MIXER_SETLEVELS)
  			ret = set_mixer_levels(p);
  		else
  			ret = sound_mixer_ioctl(dev >> 4, cmd, p);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
392
  
  	case SND_DEV_SEQ:
  	case SND_DEV_SEQ2:
6b98515a6   Alan Cox   sound_oss: remove...
393
394
  		ret = sequencer_ioctl(dev, file, cmd, p);
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395
396
397
398
  
  	case SND_DEV_DSP:
  	case SND_DEV_DSP16:
  	case SND_DEV_AUDIO:
d4cfa4d12   Dan Carpenter   OSS: soundcard: l...
399
  		ret = audio_ioctl(dev, file, cmd, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
402
  		break;
  
  	case SND_DEV_MIDIN:
d4cfa4d12   Dan Carpenter   OSS: soundcard: l...
403
  		ret = MIDIbuf_ioctl(dev, file, cmd, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
404
405
406
  		break;
  
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
407
  	mutex_unlock(&soundcard_mutex);
6b98515a6   Alan Cox   sound_oss: remove...
408
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
410
411
412
  }
  
  static unsigned int sound_poll(struct file *file, poll_table * wait)
  {
7bc563239   Josef Sipek   [PATCH] struct pa...
413
  	struct inode *inode = file->f_path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  	int dev = iminor(inode);
  
  	DEB(printk("sound_poll(dev=%d)
  ", dev));
  	switch (dev & 0x0f) {
  	case SND_DEV_SEQ:
  	case SND_DEV_SEQ2:
  		return sequencer_poll(dev, file, wait);
  
  	case SND_DEV_MIDIN:
  		return MIDIbuf_poll(dev, file, wait);
  
  	case SND_DEV_DSP:
  	case SND_DEV_DSP16:
  	case SND_DEV_AUDIO:
  		return DMAbuf_poll(file, dev >> 4, wait);
  	}
  	return 0;
  }
  
  static int sound_mmap(struct file *file, struct vm_area_struct *vma)
  {
  	int dev_class;
  	unsigned long size;
  	struct dma_buffparms *dmap = NULL;
7bc563239   Josef Sipek   [PATCH] struct pa...
439
  	int dev = iminor(file->f_path.dentry->d_inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
441
442
443
444
445
446
447
448
  
  	dev_class = dev & 0x0f;
  	dev >>= 4;
  
  	if (dev_class != SND_DEV_DSP && dev_class != SND_DEV_DSP16 && dev_class != SND_DEV_AUDIO) {
  		printk(KERN_ERR "Sound: mmap() not supported for other than audio devices
  ");
  		return -EINVAL;
  	}
645ef9ef1   Arnd Bergmann   sound: autoconver...
449
  	mutex_lock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
454
455
456
  	if (vma->vm_flags & VM_WRITE)	/* Map write and read/write to the output buf */
  		dmap = audio_devs[dev]->dmap_out;
  	else if (vma->vm_flags & VM_READ)
  		dmap = audio_devs[dev]->dmap_in;
  	else {
  		printk(KERN_ERR "Sound: Undefined mmap() access
  ");
645ef9ef1   Arnd Bergmann   sound: autoconver...
457
  		mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
461
462
463
  		return -EINVAL;
  	}
  
  	if (dmap == NULL) {
  		printk(KERN_ERR "Sound: mmap() error. dmap == NULL
  ");
645ef9ef1   Arnd Bergmann   sound: autoconver...
464
  		mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
467
468
469
  		return -EIO;
  	}
  	if (dmap->raw_buf == NULL) {
  		printk(KERN_ERR "Sound: mmap() called when raw_buf == NULL
  ");
645ef9ef1   Arnd Bergmann   sound: autoconver...
470
  		mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
473
474
475
  		return -EIO;
  	}
  	if (dmap->mapping_flags) {
  		printk(KERN_ERR "Sound: mmap() called twice for the same DMA buffer
  ");
645ef9ef1   Arnd Bergmann   sound: autoconver...
476
  		mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
478
479
480
481
  		return -EIO;
  	}
  	if (vma->vm_pgoff != 0) {
  		printk(KERN_ERR "Sound: mmap() offset must be 0.
  ");
645ef9ef1   Arnd Bergmann   sound: autoconver...
482
  		mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
484
485
486
487
488
489
490
491
492
493
  		return -EINVAL;
  	}
  	size = vma->vm_end - vma->vm_start;
  
  	if (size != dmap->bytes_in_use) {
  		printk(KERN_WARNING "Sound: mmap() size = %ld. Should be %d
  ", size, dmap->bytes_in_use);
  	}
  	if (remap_pfn_range(vma, vma->vm_start,
  			virt_to_phys(dmap->raw_buf) >> PAGE_SHIFT,
  			vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
645ef9ef1   Arnd Bergmann   sound: autoconver...
494
  		mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
497
498
499
500
501
502
503
504
505
  		return -EAGAIN;
  	}
  
  	dmap->mapping_flags |= DMA_MAP_MAPPED;
  
  	if( audio_devs[dev]->d->mmap)
  		audio_devs[dev]->d->mmap(dev);
  
  	memset(dmap->raw_buf,
  	       dmap->neutral_byte,
  	       dmap->bytes_in_use);
645ef9ef1   Arnd Bergmann   sound: autoconver...
506
  	mutex_unlock(&soundcard_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
  	return 0;
  }
9c2e08c59   Arjan van de Ven   [PATCH] mark stru...
509
  const struct file_operations oss_sound_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
513
514
  	.owner		= THIS_MODULE,
  	.llseek		= no_llseek,
  	.read		= sound_read,
  	.write		= sound_write,
  	.poll		= sound_poll,
6b98515a6   Alan Cox   sound_oss: remove...
515
  	.unlocked_ioctl	= sound_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  	.mmap		= sound_mmap,
  	.open		= sound_open,
  	.release	= sound_release,
  };
  
  /*
   *	Create the required special subdevices
   */
   
  static int create_special_devices(void)
  {
  	int seq1,seq2;
  	seq1=register_sound_special(&oss_sound_fops, 1);
  	if(seq1==-1)
  		goto bad;
  	seq2=register_sound_special(&oss_sound_fops, 8);
  	if(seq2!=-1)
  		return 0;
  	unregister_sound_special(1);
  bad:
  	return -1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
  static int dmabuf;
  static int dmabug;
  
  module_param(dmabuf, int, 0444);
  module_param(dmabug, int, 0444);
848669da3   Takashi Iwai   sound: Use sound_...
543
544
545
546
547
548
549
550
  /* additional minors for compatibility */
  struct oss_minor_dev {
  	unsigned short minor;
  	unsigned int enabled;
  } dev_list[] = {
  	{ SND_DEV_DSP16 },
  	{ SND_DEV_AUDIO },
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
554
555
  static int __init oss_init(void)
  {
  	int             err;
  	int i, j;
  	
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  #ifdef CONFIG_PCI
  	if(dmabug)
  		isa_dma_bridge_buggy = dmabug;
  #endif
  
  	err = create_special_devices();
  	if (err) {
  		printk(KERN_ERR "sound: driver already loaded/included in kernel
  ");
  		return err;
  	}
  
  	/* Protecting the innocent */
  	sound_dmap_flag = (dmabuf > 0 ? 1 : 0);
b322f4095   Ahmed S. Darwish   [PATCH] OSS: Use ...
570
  	for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
848669da3   Takashi Iwai   sound: Use sound_...
571
572
573
574
575
576
  		j = 0;
  		do {
  			unsigned short minor = dev_list[i].minor + j * 0x10;
  			if (!register_sound_special(&oss_sound_fops, minor))
  				dev_list[i].enabled = (1 << j);
  		} while (++j < num_audiodevs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
  	}
444c1953d   Dan Carpenter   sound: oss: off b...
578
  	if (sound_nblocks >= MAX_MEM_BLOCKS - 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
579
580
581
582
583
584
585
586
587
  		printk(KERN_ERR "Sound warning: Deallocation table was too small.
  ");
  	
  	return 0;
  }
  
  static void __exit oss_cleanup(void)
  {
  	int i, j;
b322f4095   Ahmed S. Darwish   [PATCH] OSS: Use ...
588
  	for (i = 0; i < ARRAY_SIZE(dev_list); i++) {
848669da3   Takashi Iwai   sound: Use sound_...
589
590
591
592
593
  		j = 0;
  		do {
  			if (dev_list[i].enabled & (1 << j))
  				unregister_sound_special(dev_list[i].minor);
  		} while (++j < num_audiodevs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
  	}
  	
  	unregister_sound_special(1);
  	unregister_sound_special(8);
  
  	sound_stop_timer();
  
  	sequencer_unload();
  
  	for (i = 0; i < MAX_DMA_CHANNELS; i++)
  		if (dma_alloc_map[i] != DMA_MAP_UNAVAIL) {
  			printk(KERN_ERR "Sound: Hmm, DMA%d was left allocated - fixed
  ", i);
  			sound_free_dma(i);
  		}
  
  	for (i = 0; i < sound_nblocks; i++)
  		vfree(sound_mem_blocks[i]);
  
  }
  
  module_init(oss_init);
  module_exit(oss_cleanup);
  MODULE_LICENSE("GPL");
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
618
619
  MODULE_DESCRIPTION("OSS Sound subsystem");
  MODULE_AUTHOR("Hannu Savolainen, et al.");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
621
622
623
624
625
626
627
628
629
630
631
632
  
  
  int sound_alloc_dma(int chn, char *deviceID)
  {
  	int err;
  
  	if ((err = request_dma(chn, deviceID)) != 0)
  		return err;
  
  	dma_alloc_map[chn] = DMA_MAP_FREE;
  
  	return 0;
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
633
  EXPORT_SYMBOL(sound_alloc_dma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  
  int sound_open_dma(int chn, char *deviceID)
  {
  	if (!valid_dma(chn)) {
  		printk(KERN_ERR "sound_open_dma: Invalid DMA channel %d
  ", chn);
  		return 1;
  	}
  
  	if (dma_alloc_map[chn] != DMA_MAP_FREE) {
  		printk("sound_open_dma: DMA channel %d busy or not allocated (%d)
  ", chn, dma_alloc_map[chn]);
  		return 1;
  	}
  	dma_alloc_map[chn] = DMA_MAP_BUSY;
  	return 0;
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
651
  EXPORT_SYMBOL(sound_open_dma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
654
655
656
657
658
659
660
661
662
  
  void sound_free_dma(int chn)
  {
  	if (dma_alloc_map[chn] == DMA_MAP_UNAVAIL) {
  		/* printk( "sound_free_dma: Bad access to DMA channel %d
  ",  chn); */
  		return;
  	}
  	free_dma(chn);
  	dma_alloc_map[chn] = DMA_MAP_UNAVAIL;
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
663
  EXPORT_SYMBOL(sound_free_dma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
669
670
671
672
673
  
  void sound_close_dma(int chn)
  {
  	if (dma_alloc_map[chn] != DMA_MAP_BUSY) {
  		printk(KERN_ERR "sound_close_dma: Bad access to DMA channel %d
  ", chn);
  		return;
  	}
  	dma_alloc_map[chn] = DMA_MAP_FREE;
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
674
  EXPORT_SYMBOL(sound_close_dma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
676
677
678
679
  
  static void do_sequencer_timer(unsigned long dummy)
  {
  	sequencer_timer(0);
  }
8d06afab7   Ingo Molnar   [PATCH] timer ini...
680
  static DEFINE_TIMER(seq_timer, do_sequencer_timer, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  
  void request_sound_timer(int count)
  {
  	extern unsigned long seq_time;
  
  	if (count < 0) {
  		seq_timer.expires = (-count) + jiffies;
  		add_timer(&seq_timer);
  		return;
  	}
  	count += seq_time;
  
  	count -= jiffies;
  
  	if (count < 1)
  		count = 1;
  
  	seq_timer.expires = (count) + jiffies;
  	add_timer(&seq_timer);
  }
  
  void sound_stop_timer(void)
  {
  	del_timer(&seq_timer);
  }
  
  void conf_printf(char *name, struct address_info *hw_config)
  {
  #ifndef CONFIG_SOUND_TRACEINIT
  	return;
  #else
  	printk("<%s> at 0x%03x", name, hw_config->io_base);
  
  	if (hw_config->irq)
  		printk(" irq %d", (hw_config->irq > 0) ? hw_config->irq : -hw_config->irq);
  
  	if (hw_config->dma != -1 || hw_config->dma2 != -1)
  	{
  		printk(" dma %d", hw_config->dma);
  		if (hw_config->dma2 != -1)
  			printk(",%d", hw_config->dma2);
  	}
  	printk("
  ");
  #endif
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
727
  EXPORT_SYMBOL(conf_printf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
  
  void conf_printf2(char *name, int base, int irq, int dma, int dma2)
  {
  #ifndef CONFIG_SOUND_TRACEINIT
  	return;
  #else
  	printk("<%s> at 0x%03x", name, base);
  
  	if (irq)
  		printk(" irq %d", (irq > 0) ? irq : -irq);
  
  	if (dma != -1 || dma2 != -1)
  	{
  		  printk(" dma %d", dma);
  		  if (dma2 != -1)
  			  printk(",%d", dma2);
  	}
  	printk("
  ");
  #endif
  }
ece7f77b8   Adrian Bunk   [PATCH] kill soun...
749
  EXPORT_SYMBOL(conf_printf2);