xref: /linux/sound/oss/dmasound/dmasound_core.c (revision bc1d4e705f48f001f3a5480f04067c48bd00bcf0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/sound/oss/dmasound/dmasound_core.c
4  *
5  *
6  *  OSS/Free compatible Atari TT/Falcon and Amiga DMA sound driver for
7  *  Linux/m68k
8  *  Extended to support Power Macintosh for Linux/ppc by Paul Mackerras
9  *
10  *  (c) 1995 by Michael Schlueter & Michael Marte
11  *
12  *  Michael Schlueter (michael@duck.syd.de) did the basic structure of the VFS
13  *  interface and the u-law to signed byte conversion.
14  *
15  *  Michael Marte (marte@informatik.uni-muenchen.de) did the sound queue,
16  *  /dev/mixer, /dev/sndstat and complemented the VFS interface. He would like
17  *  to thank:
18  *    - Michael Schlueter for initial ideas and documentation on the MFP and
19  *	the DMA sound hardware.
20  *    - Therapy? for their CD 'Troublegum' which really made me rock.
21  *
22  *  /dev/sndstat is based on code by Hannu Savolainen, the author of the
23  *  VoxWare family of drivers.
24  *
25  *  History:
26  *
27  *	1995/8/25	First release
28  *
29  *	1995/9/02	Roman Hodek:
30  *			  - Fixed atari_stram_alloc() call, the timer
31  *			    programming and several race conditions
32  *	1995/9/14	Roman Hodek:
33  *			  - After some discussion with Michael Schlueter,
34  *			    revised the interrupt disabling
35  *			  - Slightly speeded up U8->S8 translation by using
36  *			    long operations where possible
37  *			  - Added 4:3 interpolation for /dev/audio
38  *
39  *	1995/9/20	Torsten Scherer:
40  *			  - Fixed a bug in sq_write and changed /dev/audio
41  *			    converting to play at 12517Hz instead of 6258Hz.
42  *
43  *	1995/9/23	Torsten Scherer:
44  *			  - Changed sq_interrupt() and sq_play() to pre-program
45  *			    the DMA for another frame while there's still one
46  *			    running. This allows the IRQ response to be
47  *			    arbitrarily delayed and playing will still continue.
48  *
49  *	1995/10/14	Guenther Kelleter, Torsten Scherer:
50  *			  - Better support for Falcon audio (the Falcon doesn't
51  *			    raise an IRQ at the end of a frame, but at the
52  *			    beginning instead!). uses 'if (codec_dma)' in lots
53  *			    of places to simply switch between Falcon and TT
54  *			    code.
55  *
56  *	1995/11/06	Torsten Scherer:
57  *			  - Started introducing a hardware abstraction scheme
58  *			    (may perhaps also serve for Amigas?)
59  *			  - Can now play samples at almost all frequencies by
60  *			    means of a more generalized expand routine
61  *			  - Takes a good deal of care to cut data only at
62  *			    sample sizes
63  *			  - Buffer size is now a kernel runtime option
64  *			  - Implemented fsync() & several minor improvements
65  *			Guenther Kelleter:
66  *			  - Useful hints and bug fixes
67  *			  - Cross-checked it for Falcons
68  *
69  *	1996/3/9	Geert Uytterhoeven:
70  *			  - Support added for Amiga, A-law, 16-bit little
71  *			    endian.
72  *			  - Unification to drivers/sound/dmasound.c.
73  *
74  *	1996/4/6	Martin Mitchell:
75  *			  - Updated to 1.3 kernel.
76  *
77  *	1996/6/13       Topi Kanerva:
78  *			  - Fixed things that were broken (mainly the amiga
79  *			    14-bit routines)
80  *			  - /dev/sndstat shows now the real hardware frequency
81  *			  - The lowpass filter is disabled by default now
82  *
83  *	1996/9/25	Geert Uytterhoeven:
84  *			  - Modularization
85  *
86  *	1998/6/10	Andreas Schwab:
87  *			  - Converted to use sound_core
88  *
89  *	1999/12/28	Richard Zidlicky:
90  *			  - Added support for Q40
91  *
92  *	2000/2/27	Geert Uytterhoeven:
93  *			  - Clean up and split the code into 4 parts:
94  *			      o dmasound_core: machine-independent code
95  *			      o dmasound_atari: Atari TT and Falcon support
96  *			      o dmasound_awacs: Apple PowerMac support
97  *			      o dmasound_paula: Amiga support
98  *
99  *	2000/3/25	Geert Uytterhoeven:
100  *			  - Integration of dmasound_q40
101  *			  - Small clean ups
102  *
103  *	2001/01/26 [1.0] Iain Sandoe
104  *			  - make /dev/sndstat show revision & edition info.
105  *			  - since dmasound.mach.sq_setup() can fail on pmac
106  *			    its type has been changed to int and the returns
107  *			    are checked.
108  *		   [1.1]  - stop missing translations from being called.
109  *	2001/02/08 [1.2]  - remove unused translation tables & move machine-
110  *			    specific tables to low-level.
111  *			  - return correct info. for SNDCTL_DSP_GETFMTS.
112  *		   [1.3]  - implement SNDCTL_DSP_GETCAPS fully.
113  *		   [1.4]  - make /dev/sndstat text length usage deterministic.
114  *			  - make /dev/sndstat call to low-level
115  *			    dmasound.mach.state_info() pass max space to ll driver.
116  *			  - tidy startup banners and output info.
117  *		   [1.5]  - tidy up a little (removed some unused #defines in
118  *			    dmasound.h)
119  *			  - fix up HAS_RECORD conditionalisation.
120  *			  - add record code in places it is missing...
121  *			  - change buf-sizes to bytes to allow < 1kb for pmac
122  *			    if user param entry is < 256 the value is taken to
123  *			    be in kb > 256 is taken to be in bytes.
124  *			  - make default buff/frag params conditional on
125  *			    machine to allow smaller values for pmac.
126  *			  - made the ioctls, read & write comply with the OSS
127  *			    rules on setting params.
128  *			  - added parsing of _setup() params for record.
129  *	2001/04/04 [1.6]  - fix bug where sample rates higher than maximum were
130  *			    being reported as OK.
131  *			  - fix open() to return -EBUSY as per OSS doc. when
132  *			    audio is in use - this is independent of O_NOBLOCK.
133  *			  - fix bug where SNDCTL_DSP_POST was blocking.
134  */
135 
136  /* Record capability notes 30/01/2001:
137   * At present these observations apply only to pmac LL driver (the only one
138   * that can do record, at present).  However, if other LL drivers for machines
139   * with record are added they may apply.
140   *
141   * The fragment parameters for the record and play channels are separate.
142   * However, if the driver is opened O_RDWR there is no way (in the current OSS
143   * API) to specify their values independently for the record and playback
144   * channels.  Since the only common factor between the input & output is the
145   * sample rate (on pmac) it should be possible to open /dev/dspX O_WRONLY and
146   * /dev/dspY O_RDONLY.  The input & output channels could then have different
147   * characteristics (other than the first that sets sample rate claiming the
148   * right to set it for ever).  As it stands, the format, channels, number of
149   * bits & sample rate are assumed to be common.  In the future perhaps these
150   * should be the responsibility of the LL driver - and then if a card really
151   * does not share items between record & playback they can be specified
152   * separately.
153 */
154 
155 /* Thread-safeness of shared_resources notes: 31/01/2001
156  * If the user opens O_RDWR and then splits record & play between two threads
157  * both of which inherit the fd - and then starts changing things from both
158  * - we will have difficulty telling.
159  *
160  * It's bad application coding - but ...
161  * TODO: think about how to sort this out... without bogging everything down in
162  * semaphores.
163  *
164  * Similarly, the OSS spec says "all changes to parameters must be between
165  * open() and the first read() or write(). - and a bit later on (by
166  * implication) "between SNDCTL_DSP_RESET and the first read() or write() after
167  * it".  If the app is multi-threaded and this rule is broken between threads
168  * we will have trouble spotting it - and the fault will be rather obscure :-(
169  *
170  * We will try and put out at least a kmsg if we see it happen... but I think
171  * it will be quite hard to trap it with an -EXXX return... because we can't
172  * see the fault until after the damage is done.
173 */
174 
175 #include <linux/module.h>
176 #include <linux/slab.h>
177 #include <linux/sound.h>
178 #include <linux/init.h>
179 #include <linux/soundcard.h>
180 #include <linux/poll.h>
181 #include <linux/mutex.h>
182 #include <linux/sched/signal.h>
183 
184 #include <linux/uaccess.h>
185 
186 #include "dmasound.h"
187 
188 #define DMASOUND_CORE_REVISION 1
189 #define DMASOUND_CORE_EDITION 6
190 
191     /*
192      *  Declarations
193      */
194 
195 static DEFINE_MUTEX(dmasound_core_mutex);
196 int dmasound_catchRadius = 0;
197 module_param(dmasound_catchRadius, int, 0);
198 
199 static unsigned int numWriteBufs = DEFAULT_N_BUFFERS;
200 module_param(numWriteBufs, int, 0);
201 static unsigned int writeBufSize = DEFAULT_BUFF_SIZE ;	/* in bytes */
202 module_param(writeBufSize, int, 0);
203 
204 MODULE_DESCRIPTION("Atari/Amiga/Q40 core DMA sound driver");
205 MODULE_LICENSE("GPL");
206 
207 static int sq_unit = -1;
208 static int mixer_unit = -1;
209 static int state_unit = -1;
210 static int irq_installed;
211 
212 /* control over who can modify resources shared between play/record */
213 static fmode_t shared_resource_owner;
214 static int shared_resources_initialised;
215 
216     /*
217      *  Mid level stuff
218      */
219 
220 struct sound_settings dmasound = {
221 	.lock = __SPIN_LOCK_UNLOCKED(dmasound.lock)
222 };
223 
sound_silence(void)224 static inline void sound_silence(void)
225 {
226 	dmasound.mach.silence(); /* _MUST_ stop DMA */
227 }
228 
sound_set_format(int format)229 static inline int sound_set_format(int format)
230 {
231 	return dmasound.mach.setFormat(format);
232 }
233 
234 
sound_set_speed(int speed)235 static int sound_set_speed(int speed)
236 {
237 	if (speed < 0)
238 		return dmasound.soft.speed;
239 
240 	/* trap out-of-range speed settings.
241 	   at present we allow (arbitrarily) low rates - using soft
242 	   up-conversion - but we can't allow > max because there is
243 	   no soft down-conversion.
244 	*/
245 	if (dmasound.mach.max_dsp_speed &&
246 	   (speed > dmasound.mach.max_dsp_speed))
247 		speed = dmasound.mach.max_dsp_speed ;
248 
249 	dmasound.soft.speed = speed;
250 
251 	if (dmasound.minDev == SND_DEV_DSP)
252 		dmasound.dsp.speed = dmasound.soft.speed;
253 
254 	return dmasound.soft.speed;
255 }
256 
sound_set_stereo(int stereo)257 static int sound_set_stereo(int stereo)
258 {
259 	if (stereo < 0)
260 		return dmasound.soft.stereo;
261 
262 	stereo = !!stereo;    /* should be 0 or 1 now */
263 
264 	dmasound.soft.stereo = stereo;
265 	if (dmasound.minDev == SND_DEV_DSP)
266 		dmasound.dsp.stereo = stereo;
267 
268 	return stereo;
269 }
270 
sound_copy_translate(TRANS * trans,const u_char __user * userPtr,size_t userCount,u_char frame[],ssize_t * frameUsed,ssize_t frameLeft)271 static ssize_t sound_copy_translate(TRANS *trans, const u_char __user *userPtr,
272 				    size_t userCount, u_char frame[],
273 				    ssize_t *frameUsed, ssize_t frameLeft)
274 {
275 	ssize_t (*ct_func)(const u_char __user *, size_t, u_char *, ssize_t *, ssize_t);
276 
277 	switch (dmasound.soft.format) {
278 	    case AFMT_MU_LAW:
279 		ct_func = trans->ct_ulaw;
280 		break;
281 	    case AFMT_A_LAW:
282 		ct_func = trans->ct_alaw;
283 		break;
284 	    case AFMT_S8:
285 		ct_func = trans->ct_s8;
286 		break;
287 	    case AFMT_U8:
288 		ct_func = trans->ct_u8;
289 		break;
290 	    case AFMT_S16_BE:
291 		ct_func = trans->ct_s16be;
292 		break;
293 	    case AFMT_U16_BE:
294 		ct_func = trans->ct_u16be;
295 		break;
296 	    case AFMT_S16_LE:
297 		ct_func = trans->ct_s16le;
298 		break;
299 	    case AFMT_U16_LE:
300 		ct_func = trans->ct_u16le;
301 		break;
302 	    default:
303 		return 0;
304 	}
305 	/* if the user has requested a non-existent translation don't try
306 	   to call it but just return 0 bytes moved
307 	*/
308 	if (ct_func)
309 		return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
310 	return 0;
311 }
312 
313     /*
314      *  /dev/mixer abstraction
315      */
316 
317 static struct {
318     int busy;
319     int modify_counter;
320 } mixer;
321 
mixer_open(struct inode * inode,struct file * file)322 static int mixer_open(struct inode *inode, struct file *file)
323 {
324 	mutex_lock(&dmasound_core_mutex);
325 	if (!try_module_get(dmasound.mach.owner)) {
326 		mutex_unlock(&dmasound_core_mutex);
327 		return -ENODEV;
328 	}
329 	mixer.busy = 1;
330 	mutex_unlock(&dmasound_core_mutex);
331 	return 0;
332 }
333 
mixer_release(struct inode * inode,struct file * file)334 static int mixer_release(struct inode *inode, struct file *file)
335 {
336 	mutex_lock(&dmasound_core_mutex);
337 	mixer.busy = 0;
338 	module_put(dmasound.mach.owner);
339 	mutex_unlock(&dmasound_core_mutex);
340 	return 0;
341 }
342 
mixer_ioctl(struct file * file,u_int cmd,u_long arg)343 static int mixer_ioctl(struct file *file, u_int cmd, u_long arg)
344 {
345 	if (_SIOC_DIR(cmd) & _SIOC_WRITE)
346 	    mixer.modify_counter++;
347 	switch (cmd) {
348 	    case OSS_GETVERSION:
349 		return IOCTL_OUT(arg, SOUND_VERSION);
350 	    case SOUND_MIXER_INFO:
351 		{
352 		    mixer_info info;
353 		    memset(&info, 0, sizeof(info));
354 		    strscpy(info.id, dmasound.mach.name2, sizeof(info.id));
355 		    strscpy(info.name, dmasound.mach.name2, sizeof(info.name));
356 		    info.modify_counter = mixer.modify_counter;
357 		    if (copy_to_user((void __user *)arg, &info, sizeof(info)))
358 			    return -EFAULT;
359 		    return 0;
360 		}
361 	}
362 	if (dmasound.mach.mixer_ioctl)
363 	    return dmasound.mach.mixer_ioctl(cmd, arg);
364 	return -EINVAL;
365 }
366 
mixer_unlocked_ioctl(struct file * file,u_int cmd,u_long arg)367 static long mixer_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
368 {
369 	int ret;
370 
371 	mutex_lock(&dmasound_core_mutex);
372 	ret = mixer_ioctl(file, cmd, arg);
373 	mutex_unlock(&dmasound_core_mutex);
374 
375 	return ret;
376 }
377 
378 static const struct file_operations mixer_fops =
379 {
380 	.owner		= THIS_MODULE,
381 	.unlocked_ioctl	= mixer_unlocked_ioctl,
382 	.compat_ioctl	= compat_ptr_ioctl,
383 	.open		= mixer_open,
384 	.release	= mixer_release,
385 };
386 
mixer_init(void)387 static void mixer_init(void)
388 {
389 	mixer_unit = register_sound_mixer(&mixer_fops, -1);
390 	if (mixer_unit < 0)
391 		return;
392 
393 	mixer.busy = 0;
394 	dmasound.treble = 0;
395 	dmasound.bass = 0;
396 	if (dmasound.mach.mixer_init)
397 	    dmasound.mach.mixer_init();
398 }
399 
400 
401     /*
402      *  Sound queue stuff, the heart of the driver
403      */
404 
405 struct sound_queue dmasound_write_sq;
406 static void sq_reset_output(void) ;
407 
sq_allocate_buffers(struct sound_queue * sq,int num,int size)408 static int sq_allocate_buffers(struct sound_queue *sq, int num, int size)
409 {
410 	int i;
411 
412 	if (sq->buffers)
413 		return 0;
414 	sq->numBufs = num;
415 	sq->bufSize = size;
416 	sq->buffers = kmalloc_array (num, sizeof(char *), GFP_KERNEL);
417 	if (!sq->buffers)
418 		return -ENOMEM;
419 	for (i = 0; i < num; i++) {
420 		sq->buffers[i] = dmasound.mach.dma_alloc(size, GFP_KERNEL);
421 		if (!sq->buffers[i]) {
422 			while (i--)
423 				dmasound.mach.dma_free(sq->buffers[i], size);
424 			kfree(sq->buffers);
425 			sq->buffers = NULL;
426 			return -ENOMEM;
427 		}
428 	}
429 	return 0;
430 }
431 
sq_release_buffers(struct sound_queue * sq)432 static void sq_release_buffers(struct sound_queue *sq)
433 {
434 	int i;
435 
436 	if (sq->buffers) {
437 		for (i = 0; i < sq->numBufs; i++)
438 			dmasound.mach.dma_free(sq->buffers[i], sq->bufSize);
439 		kfree(sq->buffers);
440 		sq->buffers = NULL;
441 	}
442 }
443 
444 
sq_setup(struct sound_queue * sq)445 static int sq_setup(struct sound_queue *sq)
446 {
447 	int (*setup_func)(void) = NULL;
448 	int hard_frame ;
449 
450 	if (sq->locked) { /* are we already set? - and not changeable */
451 #ifdef DEBUG_DMASOUND
452 printk("dmasound_core: tried to sq_setup a locked queue\n") ;
453 #endif
454 		return -EINVAL ;
455 	}
456 	sq->locked = 1 ; /* don't think we have a race prob. here _check_ */
457 
458 	/* make sure that the parameters are set up
459 	   This should have been done already...
460 	*/
461 
462 	dmasound.mach.init();
463 
464 	/* OK.  If the user has set fragment parameters explicitly, then we
465 	   should leave them alone... as long as they are valid.
466 	   Invalid user fragment params can occur if we allow the whole buffer
467 	   to be used when the user requests the fragments sizes (with no soft
468 	   x-lation) and then the user subsequently sets a soft x-lation that
469 	   requires increased internal buffering.
470 
471 	   Othwerwise (if the user did not set them) OSS says that we should
472 	   select frag params on the basis of 0.5 s output & 0.1 s input
473 	   latency. (TODO.  For now we will copy in the defaults.)
474 	*/
475 
476 	if (sq->user_frags <= 0) {
477 		sq->max_count = sq->numBufs ;
478 		sq->max_active = sq->numBufs ;
479 		sq->block_size = sq->bufSize;
480 		/* set up the user info */
481 		sq->user_frags = sq->numBufs ;
482 		sq->user_frag_size = sq->bufSize ;
483 		sq->user_frag_size *=
484 			(dmasound.soft.size * (dmasound.soft.stereo+1) ) ;
485 		sq->user_frag_size /=
486 			(dmasound.hard.size * (dmasound.hard.stereo+1) ) ;
487 	} else {
488 		/* work out requested block size */
489 		sq->block_size = sq->user_frag_size ;
490 		sq->block_size *=
491 			(dmasound.hard.size * (dmasound.hard.stereo+1) ) ;
492 		sq->block_size /=
493 			(dmasound.soft.size * (dmasound.soft.stereo+1) ) ;
494 		/* the user wants to write frag-size chunks */
495 		sq->block_size *= dmasound.hard.speed ;
496 		sq->block_size /= dmasound.soft.speed ;
497 		/* this only works for size values which are powers of 2 */
498 		hard_frame =
499 			(dmasound.hard.size * (dmasound.hard.stereo+1))/8 ;
500 		sq->block_size +=  (hard_frame - 1) ;
501 		sq->block_size &= ~(hard_frame - 1) ; /* make sure we are aligned */
502 		/* let's just check for obvious mistakes */
503 		if ( sq->block_size <= 0 || sq->block_size > sq->bufSize) {
504 #ifdef DEBUG_DMASOUND
505 printk("dmasound_core: invalid frag size (user set %d)\n", sq->user_frag_size) ;
506 #endif
507 			sq->block_size = sq->bufSize ;
508 		}
509 		if ( sq->user_frags <= sq->numBufs ) {
510 			sq->max_count = sq->user_frags ;
511 			/* if user has set max_active - then use it */
512 			sq->max_active = (sq->max_active <= sq->max_count) ?
513 				sq->max_active : sq->max_count ;
514 		} else {
515 #ifdef DEBUG_DMASOUND
516 printk("dmasound_core: invalid frag count (user set %d)\n", sq->user_frags) ;
517 #endif
518 			sq->max_count =
519 			sq->max_active = sq->numBufs ;
520 		}
521 	}
522 	sq->front = sq->count = sq->rear_size = 0;
523 	sq->syncing = 0;
524 	sq->active = 0;
525 
526 	if (sq == &write_sq) {
527 	    sq->rear = -1;
528 	    setup_func = dmasound.mach.write_sq_setup;
529 	}
530 	if (setup_func)
531 	    return setup_func();
532 	return 0 ;
533 }
534 
sq_play(void)535 static inline void sq_play(void)
536 {
537 	dmasound.mach.play();
538 }
539 
sq_write(struct file * file,const char __user * src,size_t uLeft,loff_t * ppos)540 static ssize_t sq_write(struct file *file, const char __user *src, size_t uLeft,
541 			loff_t *ppos)
542 {
543 	ssize_t uWritten = 0;
544 	u_char *dest;
545 	ssize_t uUsed = 0, bUsed, bLeft;
546 	unsigned long flags ;
547 
548 	/* ++TeSche: Is something like this necessary?
549 	 * Hey, that's an honest question! Or does any other part of the
550 	 * filesystem already checks this situation? I really don't know.
551 	 */
552 	if (uLeft == 0)
553 		return 0;
554 
555 	/* implement any changes we have made to the soft/hard params.
556 	   this is not satisfactory really, all we have done up to now is to
557 	   say what we would like - there hasn't been any real checking of capability
558 	*/
559 
560 	if (shared_resources_initialised == 0) {
561 		dmasound.mach.init() ;
562 		shared_resources_initialised = 1 ;
563 	}
564 
565 	/* set up the sq if it is not already done. This may seem a dumb place
566 	   to do it - but it is what OSS requires.  It means that write() can
567 	   return memory allocation errors.  To avoid this possibility use the
568 	   GETBLKSIZE or GETOSPACE ioctls (after you've fiddled with all the
569 	   params you want to change) - these ioctls also force the setup.
570 	*/
571 
572 	if (write_sq.locked == 0) {
573 		if ((uWritten = sq_setup(&write_sq)) < 0) return uWritten ;
574 		uWritten = 0 ;
575 	}
576 
577 /* FIXME: I think that this may be the wrong behaviour when we get strapped
578 	for time and the cpu is close to being (or actually) behind in sending data.
579 	- because we've lost the time that the N samples, already in the buffer,
580 	would have given us to get here with the next lot from the user.
581 */
582 	/* The interrupt doesn't start to play the last, incomplete frame.
583 	 * Thus we can append to it without disabling the interrupts! (Note
584 	 * also that write_sq.rear isn't affected by the interrupt.)
585 	 */
586 
587 	/* as of 1.6 this behaviour changes if SNDCTL_DSP_POST has been issued:
588 	   this will mimic the behaviour of syncing and allow the sq_play() to
589 	   queue a partial fragment.  Since sq_play() may/will be called from
590 	   the IRQ handler - at least on Pmac we have to deal with it.
591 	   The strategy - possibly not optimum - is to kill _POST status if we
592 	   get here.  This seems, at least, reasonable - in the sense that POST
593 	   is supposed to indicate that we might not write before the queue
594 	   is drained - and if we get here in time then it does not apply.
595 	*/
596 
597 	spin_lock_irqsave(&dmasound.lock, flags);
598 	write_sq.syncing &= ~2 ; /* take out POST status */
599 	spin_unlock_irqrestore(&dmasound.lock, flags);
600 
601 	if (write_sq.count > 0 &&
602 	    (bLeft = write_sq.block_size-write_sq.rear_size) > 0) {
603 		dest = write_sq.buffers[write_sq.rear];
604 		bUsed = write_sq.rear_size;
605 		uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft,
606 					     dest, &bUsed, bLeft);
607 		if (uUsed <= 0)
608 			return uUsed;
609 		src += uUsed;
610 		uWritten += uUsed;
611 		uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */
612 		write_sq.rear_size = bUsed;
613 	}
614 
615 	while (uLeft) {
616 		DEFINE_WAIT(wait);
617 
618 		while (write_sq.count >= write_sq.max_active) {
619 			prepare_to_wait(&write_sq.action_queue, &wait, TASK_INTERRUPTIBLE);
620 			sq_play();
621 			if (write_sq.non_blocking) {
622 				finish_wait(&write_sq.action_queue, &wait);
623 				return uWritten > 0 ? uWritten : -EAGAIN;
624 			}
625 			if (write_sq.count < write_sq.max_active)
626 				break;
627 
628 			schedule_timeout(HZ);
629 			if (signal_pending(current)) {
630 				finish_wait(&write_sq.action_queue, &wait);
631 				return uWritten > 0 ? uWritten : -EINTR;
632 			}
633 		}
634 
635 		finish_wait(&write_sq.action_queue, &wait);
636 
637 		/* Here, we can avoid disabling the interrupt by first
638 		 * copying and translating the data, and then updating
639 		 * the write_sq variables. Until this is done, the interrupt
640 		 * won't see the new frame and we can work on it
641 		 * undisturbed.
642 		 */
643 
644 		dest = write_sq.buffers[(write_sq.rear+1) % write_sq.max_count];
645 		bUsed = 0;
646 		bLeft = write_sq.block_size;
647 		uUsed = sound_copy_translate(dmasound.trans_write, src, uLeft,
648 					     dest, &bUsed, bLeft);
649 		if (uUsed <= 0)
650 			break;
651 		src += uUsed;
652 		uWritten += uUsed;
653 		uLeft = (uUsed <= uLeft) ? (uLeft - uUsed) : 0 ; /* paranoia */
654 		if (bUsed) {
655 			write_sq.rear = (write_sq.rear+1) % write_sq.max_count;
656 			write_sq.rear_size = bUsed;
657 			write_sq.count++;
658 		}
659 	} /* uUsed may have been 0 */
660 
661 	sq_play();
662 
663 	return uUsed < 0? uUsed: uWritten;
664 }
665 
sq_poll(struct file * file,struct poll_table_struct * wait)666 static __poll_t sq_poll(struct file *file, struct poll_table_struct *wait)
667 {
668 	__poll_t mask = 0;
669 	int retVal;
670 
671 	if (write_sq.locked == 0) {
672 		if ((retVal = sq_setup(&write_sq)) < 0)
673 			return retVal;
674 		return 0;
675 	}
676 	if (file->f_mode & FMODE_WRITE )
677 		poll_wait(file, &write_sq.action_queue, wait);
678 	if (file->f_mode & FMODE_WRITE)
679 		if (write_sq.count < write_sq.max_active || write_sq.block_size - write_sq.rear_size > 0)
680 			mask |= EPOLLOUT | EPOLLWRNORM;
681 	return mask;
682 
683 }
684 
sq_init_waitqueue(struct sound_queue * sq)685 static inline void sq_init_waitqueue(struct sound_queue *sq)
686 {
687 	init_waitqueue_head(&sq->action_queue);
688 	init_waitqueue_head(&sq->open_queue);
689 	init_waitqueue_head(&sq->sync_queue);
690 	sq->busy = 0;
691 }
692 
693 #if 0 /* blocking open() */
694 static inline void sq_wake_up(struct sound_queue *sq, struct file *file,
695 			      fmode_t mode)
696 {
697 	if (file->f_mode & mode) {
698 		sq->busy = 0; /* CHECK: IS THIS OK??? */
699 		WAKE_UP(sq->open_queue);
700 	}
701 }
702 #endif
703 
sq_open2(struct sound_queue * sq,struct file * file,fmode_t mode,int numbufs,int bufsize)704 static int sq_open2(struct sound_queue *sq, struct file *file, fmode_t mode,
705 		    int numbufs, int bufsize)
706 {
707 	int rc = 0;
708 
709 	if (file->f_mode & mode) {
710 		if (sq->busy) {
711 #if 0 /* blocking open() */
712 			rc = -EBUSY;
713 			if (file->f_flags & O_NONBLOCK)
714 				return rc;
715 			rc = -EINTR;
716 			if (wait_event_interruptible(sq->open_queue, !sq->busy))
717 				return rc;
718 			rc = 0;
719 #else
720 			/* OSS manual says we will return EBUSY regardless
721 			   of O_NOBLOCK.
722 			*/
723 			return -EBUSY ;
724 #endif
725 		}
726 		sq->busy = 1; /* Let's play spot-the-race-condition */
727 
728 		/* allocate the default number & size of buffers.
729 		   (i.e. specified in _setup() or as module params)
730 		   can't be changed at the moment - but _could_ be perhaps
731 		   in the setfragments ioctl.
732 		*/
733 		if (( rc = sq_allocate_buffers(sq, numbufs, bufsize))) {
734 #if 0 /* blocking open() */
735 			sq_wake_up(sq, file, mode);
736 #else
737 			sq->busy = 0 ;
738 #endif
739 			return rc;
740 		}
741 
742 		sq->non_blocking = file->f_flags & O_NONBLOCK;
743 	}
744 	return rc;
745 }
746 
747 #define write_sq_init_waitqueue()	sq_init_waitqueue(&write_sq)
748 #if 0 /* blocking open() */
749 #define write_sq_wake_up(file)		sq_wake_up(&write_sq, file, FMODE_WRITE)
750 #endif
751 #define write_sq_release_buffers()	sq_release_buffers(&write_sq)
752 #define write_sq_open(file)	\
753 	sq_open2(&write_sq, file, FMODE_WRITE, numWriteBufs, writeBufSize )
754 
sq_open(struct inode * inode,struct file * file)755 static int sq_open(struct inode *inode, struct file *file)
756 {
757 	int rc;
758 
759 	mutex_lock(&dmasound_core_mutex);
760 	if (!try_module_get(dmasound.mach.owner)) {
761 		mutex_unlock(&dmasound_core_mutex);
762 		return -ENODEV;
763 	}
764 
765 	rc = write_sq_open(file); /* checks the f_mode */
766 	if (rc)
767 		goto out;
768 	if (file->f_mode & FMODE_READ) {
769 		/* TODO: if O_RDWR, release any resources grabbed by write part */
770 		rc = -ENXIO ; /* I think this is what is required by open(2) */
771 		goto out;
772 	}
773 
774 	if (dmasound.mach.sq_open)
775 	    dmasound.mach.sq_open(file->f_mode);
776 
777 	/* CHECK whether this is sensible - in the case that dsp0 could be opened
778 	  O_RDONLY and dsp1 could be opened O_WRONLY
779 	*/
780 
781 	dmasound.minDev = iminor(inode) & 0x0f;
782 
783 	/* OK. - we should make some attempt at consistency. At least the H'ware
784 	   options should be set with a valid mode.  We will make it that the LL
785 	   driver must supply defaults for hard & soft params.
786 	*/
787 
788 	if (shared_resource_owner == 0) {
789 		/* you can make this AFMT_U8/mono/8K if you want to mimic old
790 		   OSS behaviour - while we still have soft translations ;-) */
791 		dmasound.soft = dmasound.mach.default_soft ;
792 		dmasound.dsp = dmasound.mach.default_soft ;
793 		dmasound.hard = dmasound.mach.default_hard ;
794 	}
795 
796 #ifndef DMASOUND_STRICT_OSS_COMPLIANCE
797 	/* none of the current LL drivers can actually do this "native" at the moment
798 	   OSS does not really require us to supply /dev/audio if we can't do it.
799 	*/
800 	if (dmasound.minDev == SND_DEV_AUDIO) {
801 		sound_set_speed(8000);
802 		sound_set_stereo(0);
803 		sound_set_format(AFMT_MU_LAW);
804 	}
805 #endif
806 	mutex_unlock(&dmasound_core_mutex);
807 	return 0;
808  out:
809 	module_put(dmasound.mach.owner);
810 	mutex_unlock(&dmasound_core_mutex);
811 	return rc;
812 }
813 
sq_reset_output(void)814 static void sq_reset_output(void)
815 {
816 	sound_silence(); /* this _must_ stop DMA, we might be about to lose the buffers */
817 	write_sq.active = 0;
818 	write_sq.count = 0;
819 	write_sq.rear_size = 0;
820 	/* write_sq.front = (write_sq.rear+1) % write_sq.max_count;*/
821 	write_sq.front = 0 ;
822 	write_sq.rear = -1 ; /* same as for set-up */
823 
824 	/* OK - we can unlock the parameters and fragment settings */
825 	write_sq.locked = 0 ;
826 	write_sq.user_frags = 0 ;
827 	write_sq.user_frag_size = 0 ;
828 }
829 
sq_reset(void)830 static void sq_reset(void)
831 {
832 	sq_reset_output() ;
833 	/* we could consider resetting the shared_resources_owner here... but I
834 	   think it is probably still rather non-obvious to application writer
835 	*/
836 
837 	/* we release everything else though */
838 	shared_resources_initialised = 0 ;
839 }
840 
sq_fsync(void)841 static int sq_fsync(void)
842 {
843 	int rc = 0;
844 	int timeout = 5;
845 
846 	write_sq.syncing |= 1;
847 	sq_play();	/* there may be an incomplete frame waiting */
848 
849 	while (write_sq.active) {
850 		wait_event_interruptible_timeout(write_sq.sync_queue,
851 						 !write_sq.active, HZ);
852 		if (signal_pending(current)) {
853 			/* While waiting for audio output to drain, an
854 			 * interrupt occurred.  Stop audio output immediately
855 			 * and clear the queue. */
856 			sq_reset_output();
857 			rc = -EINTR;
858 			break;
859 		}
860 		if (!--timeout) {
861 			printk(KERN_WARNING "dmasound: Timeout draining output\n");
862 			sq_reset_output();
863 			rc = -EIO;
864 			break;
865 		}
866 	}
867 
868 	/* flag no sync regardless of whether we had a DSP_POST or not */
869 	write_sq.syncing = 0 ;
870 	return rc;
871 }
872 
sq_release(struct inode * inode,struct file * file)873 static int sq_release(struct inode *inode, struct file *file)
874 {
875 	int rc = 0;
876 
877 	mutex_lock(&dmasound_core_mutex);
878 
879 	if (file->f_mode & FMODE_WRITE) {
880 		if (write_sq.busy)
881 			rc = sq_fsync();
882 
883 		sq_reset_output() ; /* make sure dma is stopped and all is quiet */
884 		write_sq_release_buffers();
885 		write_sq.busy = 0;
886 	}
887 
888 	if (file->f_mode & shared_resource_owner) { /* it's us that has them */
889 		shared_resource_owner = 0 ;
890 		shared_resources_initialised = 0 ;
891 		dmasound.hard = dmasound.mach.default_hard ;
892 	}
893 
894 	module_put(dmasound.mach.owner);
895 
896 #if 0 /* blocking open() */
897 	/* Wake up a process waiting for the queue being released.
898 	 * Note: There may be several processes waiting for a call
899 	 * to open() returning. */
900 
901 	/* Iain: hmm I don't understand this next comment ... */
902 	/* There is probably a DOS atack here. They change the mode flag. */
903 	/* XXX add check here,*/
904 	read_sq_wake_up(file); /* checks f_mode */
905 	write_sq_wake_up(file); /* checks f_mode */
906 #endif /* blocking open() */
907 
908 	mutex_unlock(&dmasound_core_mutex);
909 
910 	return rc;
911 }
912 
913 /* here we see if we have a right to modify format, channels, size and so on
914    if no-one else has claimed it already then we do...
915 
916    TODO: We might change this to mask O_RDWR such that only one or the other channel
917    is the owner - if we have problems.
918 */
919 
shared_resources_are_mine(fmode_t md)920 static int shared_resources_are_mine(fmode_t md)
921 {
922 	if (shared_resource_owner)
923 		return (shared_resource_owner & md) != 0;
924 	else {
925 		shared_resource_owner = md ;
926 		return 1 ;
927 	}
928 }
929 
930 /* if either queue is locked we must deny the right to change shared params
931 */
932 
queues_are_quiescent(void)933 static int queues_are_quiescent(void)
934 {
935 	if (write_sq.locked)
936 		return 0 ;
937 	return 1 ;
938 }
939 
940 /* check and set a queue's fragments per user's wishes...
941    we will check against the pre-defined literals and the actual sizes.
942    This is a bit fraught - because soft translations can mess with our
943    buffer requirements *after* this call - OSS says "call setfrags first"
944 */
945 
946 /* It is possible to replace all the -EINVAL returns with an override that
947    just puts the allowable value in.  This may be what many OSS apps require
948 */
949 
set_queue_frags(struct sound_queue * sq,int bufs,int size)950 static int set_queue_frags(struct sound_queue *sq, int bufs, int size)
951 {
952 	if (sq->locked) {
953 #ifdef DEBUG_DMASOUND
954 printk("dmasound_core: tried to set_queue_frags on a locked queue\n") ;
955 #endif
956 		return -EINVAL ;
957 	}
958 
959 	if ((size < MIN_FRAG_SIZE) || (size > MAX_FRAG_SIZE))
960 		return -EINVAL ;
961 	size = (1<<size) ; /* now in bytes */
962 	if (size > sq->bufSize)
963 		return -EINVAL ; /* this might still not work */
964 
965 	if (bufs <= 0)
966 		return -EINVAL ;
967 	if (bufs > sq->numBufs) /* the user is allowed say "don't care" with 0x7fff */
968 		bufs = sq->numBufs ;
969 
970 	/* there is, currently, no way to specify max_active separately
971 	   from max_count.  This could be a LL driver issue - I guess
972 	   if there is a requirement for these values to be different then
973 	  we will have to pass that info. up to this level.
974 	*/
975 	sq->user_frags =
976 	sq->max_active = bufs ;
977 	sq->user_frag_size = size ;
978 
979 	return 0 ;
980 }
981 
sq_ioctl(struct file * file,u_int cmd,u_long arg)982 static int sq_ioctl(struct file *file, u_int cmd, u_long arg)
983 {
984 	int val, result;
985 	u_long fmt;
986 	int data;
987 	int size, nbufs;
988 	audio_buf_info info;
989 
990 	switch (cmd) {
991 	case SNDCTL_DSP_RESET:
992 		sq_reset();
993 		return 0;
994 	case SNDCTL_DSP_GETFMTS:
995 		fmt = dmasound.mach.hardware_afmts ; /* this is what OSS says.. */
996 		return IOCTL_OUT(arg, fmt);
997 	case SNDCTL_DSP_GETBLKSIZE:
998 		/* this should tell the caller about bytes that the app can
999 		   read/write - the app doesn't care about our internal buffers.
1000 		   We force sq_setup() here as per OSS 1.1 (which should
1001 		   compute the values necessary).
1002 		   Since there is no mechanism to specify read/write separately, for
1003 		   fds opened O_RDWR, the write_sq values will, arbitrarily, overwrite
1004 		   the read_sq ones.
1005 		*/
1006 		size = 0 ;
1007 		if (file->f_mode & FMODE_WRITE) {
1008 			if ( !write_sq.locked )
1009 				sq_setup(&write_sq) ;
1010 			size = write_sq.user_frag_size ;
1011 		}
1012 		return IOCTL_OUT(arg, size);
1013 	case SNDCTL_DSP_POST:
1014 		/* all we are going to do is to tell the LL that any
1015 		   partial frags can be queued for output.
1016 		   The LL will have to clear this flag when last output
1017 		   is queued.
1018 		*/
1019 		write_sq.syncing |= 0x2 ;
1020 		sq_play() ;
1021 		return 0 ;
1022 	case SNDCTL_DSP_SYNC:
1023 		/* This call, effectively, has the same behaviour as SNDCTL_DSP_RESET
1024 		   except that it waits for output to finish before resetting
1025 		   everything - read, however, is killed immediately.
1026 		*/
1027 		result = 0 ;
1028 		if (file->f_mode & FMODE_WRITE) {
1029 			result = sq_fsync();
1030 			sq_reset_output() ;
1031 		}
1032 		/* if we are the shared resource owner then release them */
1033 		if (file->f_mode & shared_resource_owner)
1034 			shared_resources_initialised = 0 ;
1035 		return result ;
1036 	case SOUND_PCM_READ_RATE:
1037 		return IOCTL_OUT(arg, dmasound.soft.speed);
1038 	case SNDCTL_DSP_SPEED:
1039 		/* changing this on the fly will have weird effects on the sound.
1040 		   Where there are rate conversions implemented in soft form - it
1041 		   will cause the _ctx_xxx() functions to be substituted.
1042 		   However, there doesn't appear to be any reason to dis-allow it from
1043 		   a driver pov.
1044 		*/
1045 		if (shared_resources_are_mine(file->f_mode)) {
1046 			IOCTL_IN(arg, data);
1047 			data = sound_set_speed(data) ;
1048 			shared_resources_initialised = 0 ;
1049 			return IOCTL_OUT(arg, data);
1050 		} else
1051 			return -EINVAL ;
1052 		break ;
1053 	/* OSS says these next 4 actions are undefined when the device is
1054 	   busy/active - we will just return -EINVAL.
1055 	   To be allowed to change one - (a) you have to own the right
1056 	    (b) the queue(s) must be quiescent
1057 	*/
1058 	case SNDCTL_DSP_STEREO:
1059 		if (shared_resources_are_mine(file->f_mode) &&
1060 		    queues_are_quiescent()) {
1061 			IOCTL_IN(arg, data);
1062 			shared_resources_initialised = 0 ;
1063 			return IOCTL_OUT(arg, sound_set_stereo(data));
1064 		} else
1065 			return -EINVAL ;
1066 		break ;
1067 	case SOUND_PCM_WRITE_CHANNELS:
1068 		if (shared_resources_are_mine(file->f_mode) &&
1069 		    queues_are_quiescent()) {
1070 			IOCTL_IN(arg, data);
1071 			/* the user might ask for 20 channels, we will return 1 or 2 */
1072 			shared_resources_initialised = 0 ;
1073 			return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
1074 		} else
1075 			return -EINVAL ;
1076 		break ;
1077 	case SNDCTL_DSP_SETFMT:
1078 		if (shared_resources_are_mine(file->f_mode) &&
1079 		    queues_are_quiescent()) {
1080 		    	int format;
1081 			IOCTL_IN(arg, data);
1082 			shared_resources_initialised = 0 ;
1083 			format = sound_set_format(data);
1084 			result = IOCTL_OUT(arg, format);
1085 			if (result < 0)
1086 				return result;
1087 			if (format != data && data != AFMT_QUERY)
1088 				return -EINVAL;
1089 			return 0;
1090 		} else
1091 			return -EINVAL ;
1092 	case SNDCTL_DSP_SUBDIVIDE:
1093 		return -EINVAL ;
1094 	case SNDCTL_DSP_SETFRAGMENT:
1095 		/* we can do this independently for the two queues - with the
1096 		   proviso that for fds opened O_RDWR we cannot separate the
1097 		   actions and both queues will be set per the last call.
1098 		   NOTE: this does *NOT* actually set the queue up - merely
1099 		   registers our intentions.
1100 		*/
1101 		IOCTL_IN(arg, data);
1102 		result = 0 ;
1103 		nbufs = (data >> 16) & 0x7fff ; /* 0x7fff is 'use maximum' */
1104 		size = data & 0xffff;
1105 		if (file->f_mode & FMODE_WRITE) {
1106 			result = set_queue_frags(&write_sq, nbufs, size) ;
1107 			if (result)
1108 				return result ;
1109 		}
1110 		/* NOTE: this return value is irrelevant - OSS specifically says that
1111 		   the value is 'random' and that the user _must_ check the actual
1112 		   frags values using SNDCTL_DSP_GETBLKSIZE or similar */
1113 		return IOCTL_OUT(arg, data);
1114 	case SNDCTL_DSP_GETOSPACE:
1115 		/*
1116 		*/
1117 		if (file->f_mode & FMODE_WRITE) {
1118 			if ( !write_sq.locked )
1119 				sq_setup(&write_sq) ;
1120 			info.fragments = write_sq.max_active - write_sq.count;
1121 			info.fragstotal = write_sq.max_active;
1122 			info.fragsize = write_sq.user_frag_size;
1123 			info.bytes = info.fragments * info.fragsize;
1124 			if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1125 				return -EFAULT;
1126 			return 0;
1127 		} else
1128 			return -EINVAL ;
1129 		break ;
1130 	case SNDCTL_DSP_GETCAPS:
1131 		val = dmasound.mach.capabilities & 0xffffff00;
1132 		return IOCTL_OUT(arg,val);
1133 
1134 	default:
1135 		return mixer_ioctl(file, cmd, arg);
1136 	}
1137 	return -EINVAL;
1138 }
1139 
sq_unlocked_ioctl(struct file * file,u_int cmd,u_long arg)1140 static long sq_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
1141 {
1142 	int ret;
1143 
1144 	mutex_lock(&dmasound_core_mutex);
1145 	ret = sq_ioctl(file, cmd, arg);
1146 	mutex_unlock(&dmasound_core_mutex);
1147 
1148 	return ret;
1149 }
1150 
1151 static const struct file_operations sq_fops =
1152 {
1153 	.owner		= THIS_MODULE,
1154 	.write		= sq_write,
1155 	.poll		= sq_poll,
1156 	.unlocked_ioctl	= sq_unlocked_ioctl,
1157 	.compat_ioctl	= compat_ptr_ioctl,
1158 	.open		= sq_open,
1159 	.release	= sq_release,
1160 };
1161 
sq_init(void)1162 static int sq_init(void)
1163 {
1164 	const struct file_operations *fops = &sq_fops;
1165 
1166 	sq_unit = register_sound_dsp(fops, -1);
1167 	if (sq_unit < 0) {
1168 		printk(KERN_ERR "dmasound_core: couldn't register fops\n") ;
1169 		return sq_unit ;
1170 	}
1171 
1172 	write_sq_init_waitqueue();
1173 
1174 	/* These parameters will be restored for every clean open()
1175 	 * in the case of multiple open()s (e.g. dsp0 & dsp1) they
1176 	 * will be set so long as the shared resources have no owner.
1177 	 */
1178 
1179 	if (shared_resource_owner == 0) {
1180 		dmasound.soft = dmasound.mach.default_soft ;
1181 		dmasound.hard = dmasound.mach.default_hard ;
1182 		dmasound.dsp = dmasound.mach.default_soft ;
1183 		shared_resources_initialised = 0 ;
1184 	}
1185 	return 0 ;
1186 }
1187 
1188 
1189     /*
1190      *  /dev/sndstat
1191      */
1192 
1193 /* we allow more space for record-enabled because there are extra output lines.
1194    the number here must include the amount we are prepared to give to the low-level
1195    driver.
1196 */
1197 
1198 #define STAT_BUFF_LEN 768
1199 
1200 /* this is how much space we will allow the low-level driver to use
1201    in the stat buffer.  Currently, 2 * (80 character line + <NL>).
1202    We do not police this (it is up to the ll driver to be honest).
1203 */
1204 
1205 #define LOW_LEVEL_STAT_ALLOC 162
1206 
1207 static struct {
1208     int busy;
1209     char buf[STAT_BUFF_LEN];	/* state.buf should not overflow! */
1210     int len, ptr;
1211 } state;
1212 
1213 /* publish this function for use by low-level code, if required */
1214 
get_afmt_string(int afmt)1215 static char *get_afmt_string(int afmt)
1216 {
1217         switch(afmt) {
1218             case AFMT_MU_LAW:
1219                 return "mu-law";
1220             case AFMT_A_LAW:
1221                 return "A-law";
1222             case AFMT_U8:
1223                 return "unsigned 8 bit";
1224             case AFMT_S8:
1225                 return "signed 8 bit";
1226             case AFMT_S16_BE:
1227                 return "signed 16 bit BE";
1228             case AFMT_U16_BE:
1229                 return "unsigned 16 bit BE";
1230             case AFMT_S16_LE:
1231                 return "signed 16 bit LE";
1232             case AFMT_U16_LE:
1233                 return "unsigned 16 bit LE";
1234 	    case 0:
1235 		return "format not set" ;
1236             default:
1237                 break ;
1238         }
1239         return "ERROR: Unsupported AFMT_XXXX code" ;
1240 }
1241 
state_open(struct inode * inode,struct file * file)1242 static int state_open(struct inode *inode, struct file *file)
1243 {
1244 	char *buffer = state.buf;
1245 	int len = 0;
1246 	int ret;
1247 
1248 	mutex_lock(&dmasound_core_mutex);
1249 	ret = -EBUSY;
1250 	if (state.busy)
1251 		goto out;
1252 
1253 	ret = -ENODEV;
1254 	if (!try_module_get(dmasound.mach.owner))
1255 		goto out;
1256 
1257 	state.ptr = 0;
1258 	state.busy = 1;
1259 
1260 	len += sprintf(buffer+len, "%sDMA sound driver rev %03d :\n",
1261 		dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) +
1262 		((dmasound.mach.version>>8) & 0x0f));
1263 	len += sprintf(buffer+len,
1264 		"Core driver edition %02d.%02d : %s driver edition %02d.%02d\n",
1265 		DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2,
1266 		(dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ;
1267 
1268 	/* call the low-level module to fill in any stat info. that it has
1269 	   if present.  Maximum buffer usage is specified.
1270 	*/
1271 
1272 	if (dmasound.mach.state_info)
1273 		len += dmasound.mach.state_info(buffer+len,
1274 			(size_t) LOW_LEVEL_STAT_ALLOC) ;
1275 
1276 	/* make usage of the state buffer as deterministic as poss.
1277 	   exceptional conditions could cause overrun - and this is flagged as
1278 	   a kernel error.
1279 	*/
1280 
1281 	/* formats and settings */
1282 
1283 	len += sprintf(buffer+len,"\t\t === Formats & settings ===\n") ;
1284 	len += sprintf(buffer+len,"Parameter %20s%20s\n","soft","hard") ;
1285 	len += sprintf(buffer+len,"Format   :%20s%20s\n",
1286 		get_afmt_string(dmasound.soft.format),
1287 		get_afmt_string(dmasound.hard.format));
1288 
1289 	len += sprintf(buffer+len,"Samp Rate:%14d s/sec%14d s/sec\n",
1290 		       dmasound.soft.speed, dmasound.hard.speed);
1291 
1292 	len += sprintf(buffer+len,"Channels :%20s%20s\n",
1293 		       dmasound.soft.stereo ? "stereo" : "mono",
1294 		       dmasound.hard.stereo ? "stereo" : "mono" );
1295 
1296 	/* sound queue status */
1297 
1298 	len += sprintf(buffer+len,"\t\t === Sound Queue status ===\n");
1299 	len += sprintf(buffer+len,"Allocated:%8s%6s\n","Buffers","Size") ;
1300 	len += sprintf(buffer+len,"%9s:%8d%6d\n",
1301 		"write", write_sq.numBufs, write_sq.bufSize) ;
1302 	len += sprintf(buffer+len,
1303 		"Current  : MaxFrg FragSiz MaxAct Frnt Rear "
1304 		"Cnt RrSize A B S L  xruns\n") ;
1305 	len += sprintf(buffer+len,"%9s:%7d%8d%7d%5d%5d%4d%7d%2d%2d%2d%2d%7d\n",
1306 		"write", write_sq.max_count, write_sq.block_size,
1307 		write_sq.max_active, write_sq.front, write_sq.rear,
1308 		write_sq.count, write_sq.rear_size, write_sq.active,
1309 		write_sq.busy, write_sq.syncing, write_sq.locked, write_sq.xruns) ;
1310 #ifdef DEBUG_DMASOUND
1311 printk("dmasound: stat buffer used %d bytes\n", len) ;
1312 #endif
1313 
1314 	if (len >= STAT_BUFF_LEN)
1315 		printk(KERN_ERR "dmasound_core: stat buffer overflowed!\n");
1316 
1317 	state.len = len;
1318 	ret = 0;
1319 out:
1320 	mutex_unlock(&dmasound_core_mutex);
1321 	return ret;
1322 }
1323 
state_release(struct inode * inode,struct file * file)1324 static int state_release(struct inode *inode, struct file *file)
1325 {
1326 	mutex_lock(&dmasound_core_mutex);
1327 	state.busy = 0;
1328 	module_put(dmasound.mach.owner);
1329 	mutex_unlock(&dmasound_core_mutex);
1330 	return 0;
1331 }
1332 
state_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1333 static ssize_t state_read(struct file *file, char __user *buf, size_t count,
1334 			  loff_t *ppos)
1335 {
1336 	int n = state.len - state.ptr;
1337 	if (n > count)
1338 		n = count;
1339 	if (n <= 0)
1340 		return 0;
1341 	if (copy_to_user(buf, &state.buf[state.ptr], n))
1342 		return -EFAULT;
1343 	state.ptr += n;
1344 	return n;
1345 }
1346 
1347 static const struct file_operations state_fops = {
1348 	.owner		= THIS_MODULE,
1349 	.read		= state_read,
1350 	.open		= state_open,
1351 	.release	= state_release,
1352 };
1353 
state_init(void)1354 static int state_init(void)
1355 {
1356 	state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
1357 	if (state_unit < 0)
1358 		return state_unit ;
1359 	state.busy = 0;
1360 	return 0 ;
1361 }
1362 
1363 
1364     /*
1365      *  Config & Setup
1366      *
1367      *  This function is called by _one_ chipset-specific driver
1368      */
1369 
dmasound_init(void)1370 int dmasound_init(void)
1371 {
1372 	int res ;
1373 
1374 	if (irq_installed)
1375 		return -EBUSY;
1376 
1377 	/* Set up sound queue, /dev/audio and /dev/dsp. */
1378 
1379 	/* Set default settings. */
1380 	if ((res = sq_init()) < 0)
1381 		return res ;
1382 
1383 	/* Set up /dev/sndstat. */
1384 	if ((res = state_init()) < 0)
1385 		return res ;
1386 
1387 	/* Set up /dev/mixer. */
1388 	mixer_init();
1389 
1390 	if (!dmasound.mach.irqinit()) {
1391 		printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
1392 		return -ENODEV;
1393 	}
1394 	irq_installed = 1;
1395 
1396 	printk(KERN_INFO "%s DMA sound driver rev %03d installed\n",
1397 		dmasound.mach.name, (DMASOUND_CORE_REVISION<<4) +
1398 		((dmasound.mach.version>>8) & 0x0f));
1399 	printk(KERN_INFO
1400 		"Core driver edition %02d.%02d : %s driver edition %02d.%02d\n",
1401 		DMASOUND_CORE_REVISION, DMASOUND_CORE_EDITION, dmasound.mach.name2,
1402 		(dmasound.mach.version >> 8), (dmasound.mach.version & 0xff)) ;
1403 	printk(KERN_INFO "Write will use %4d fragments of %7d bytes as default\n",
1404 		numWriteBufs, writeBufSize) ;
1405 	return 0;
1406 }
1407 
dmasound_deinit(void)1408 void dmasound_deinit(void)
1409 {
1410 	if (irq_installed) {
1411 		sound_silence();
1412 		dmasound.mach.irqcleanup();
1413 		irq_installed = 0;
1414 	}
1415 
1416 	write_sq_release_buffers();
1417 
1418 	if (mixer_unit >= 0)
1419 		unregister_sound_mixer(mixer_unit);
1420 	if (state_unit >= 0)
1421 		unregister_sound_special(state_unit);
1422 	if (sq_unit >= 0)
1423 		unregister_sound_dsp(sq_unit);
1424 }
1425 
dmasound_setup(char * str)1426 static int __maybe_unused dmasound_setup(char *str)
1427 {
1428 	int ints[6], size;
1429 
1430 	str = get_options(str, ARRAY_SIZE(ints), ints);
1431 
1432 	/* check the bootstrap parameter for "dmasound=" */
1433 
1434 	/* FIXME: other than in the most naive of cases there is no sense in these
1435 	 *	  buffers being other than powers of two.  This is not checked yet.
1436 	 */
1437 
1438 	switch (ints[0]) {
1439 	case 3:
1440 		if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
1441 			printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius);
1442 		else
1443 			catchRadius = ints[3];
1444 		fallthrough;
1445 	case 2:
1446 		if (ints[1] < MIN_BUFFERS)
1447 			printk("dmasound_setup: invalid number of buffers, using default = %d\n", numWriteBufs);
1448 		else
1449 			numWriteBufs = ints[1];
1450 		fallthrough;
1451 	case 1:
1452 		if ((size = ints[2]) < 256) /* check for small buffer specs */
1453 			size <<= 10 ;
1454                 if (size < MIN_BUFSIZE || size > MAX_BUFSIZE)
1455                         printk("dmasound_setup: invalid write buffer size, using default = %d\n", writeBufSize);
1456                 else
1457                         writeBufSize = size;
1458 	case 0:
1459 		break;
1460 	default:
1461 		printk("dmasound_setup: invalid number of arguments\n");
1462 		return 0;
1463 	}
1464 	return 1;
1465 }
1466 
1467 __setup("dmasound=", dmasound_setup);
1468 
1469     /*
1470      *  Conversion tables
1471      */
1472 
1473 #ifdef HAS_8BIT_TABLES
1474 /* 8 bit mu-law */
1475 
1476 char dmasound_ulaw2dma8[] = {
1477 	-126,	-122,	-118,	-114,	-110,	-106,	-102,	-98,
1478 	-94,	-90,	-86,	-82,	-78,	-74,	-70,	-66,
1479 	-63,	-61,	-59,	-57,	-55,	-53,	-51,	-49,
1480 	-47,	-45,	-43,	-41,	-39,	-37,	-35,	-33,
1481 	-31,	-30,	-29,	-28,	-27,	-26,	-25,	-24,
1482 	-23,	-22,	-21,	-20,	-19,	-18,	-17,	-16,
1483 	-16,	-15,	-15,	-14,	-14,	-13,	-13,	-12,
1484 	-12,	-11,	-11,	-10,	-10,	-9,	-9,	-8,
1485 	-8,	-8,	-7,	-7,	-7,	-7,	-6,	-6,
1486 	-6,	-6,	-5,	-5,	-5,	-5,	-4,	-4,
1487 	-4,	-4,	-4,	-4,	-3,	-3,	-3,	-3,
1488 	-3,	-3,	-3,	-3,	-2,	-2,	-2,	-2,
1489 	-2,	-2,	-2,	-2,	-2,	-2,	-2,	-2,
1490 	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
1491 	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
1492 	-1,	-1,	-1,	-1,	-1,	-1,	-1,	0,
1493 	125,	121,	117,	113,	109,	105,	101,	97,
1494 	93,	89,	85,	81,	77,	73,	69,	65,
1495 	62,	60,	58,	56,	54,	52,	50,	48,
1496 	46,	44,	42,	40,	38,	36,	34,	32,
1497 	30,	29,	28,	27,	26,	25,	24,	23,
1498 	22,	21,	20,	19,	18,	17,	16,	15,
1499 	15,	14,	14,	13,	13,	12,	12,	11,
1500 	11,	10,	10,	9,	9,	8,	8,	7,
1501 	7,	7,	6,	6,	6,	6,	5,	5,
1502 	5,	5,	4,	4,	4,	4,	3,	3,
1503 	3,	3,	3,	3,	2,	2,	2,	2,
1504 	2,	2,	2,	2,	1,	1,	1,	1,
1505 	1,	1,	1,	1,	1,	1,	1,	1,
1506 	0,	0,	0,	0,	0,	0,	0,	0,
1507 	0,	0,	0,	0,	0,	0,	0,	0,
1508 	0,	0,	0,	0,	0,	0,	0,	0
1509 };
1510 
1511 /* 8 bit A-law */
1512 
1513 char dmasound_alaw2dma8[] = {
1514 	-22,	-21,	-24,	-23,	-18,	-17,	-20,	-19,
1515 	-30,	-29,	-32,	-31,	-26,	-25,	-28,	-27,
1516 	-11,	-11,	-12,	-12,	-9,	-9,	-10,	-10,
1517 	-15,	-15,	-16,	-16,	-13,	-13,	-14,	-14,
1518 	-86,	-82,	-94,	-90,	-70,	-66,	-78,	-74,
1519 	-118,	-114,	-126,	-122,	-102,	-98,	-110,	-106,
1520 	-43,	-41,	-47,	-45,	-35,	-33,	-39,	-37,
1521 	-59,	-57,	-63,	-61,	-51,	-49,	-55,	-53,
1522 	-2,	-2,	-2,	-2,	-2,	-2,	-2,	-2,
1523 	-2,	-2,	-2,	-2,	-2,	-2,	-2,	-2,
1524 	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
1525 	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,
1526 	-6,	-6,	-6,	-6,	-5,	-5,	-5,	-5,
1527 	-8,	-8,	-8,	-8,	-7,	-7,	-7,	-7,
1528 	-3,	-3,	-3,	-3,	-3,	-3,	-3,	-3,
1529 	-4,	-4,	-4,	-4,	-4,	-4,	-4,	-4,
1530 	21,	20,	23,	22,	17,	16,	19,	18,
1531 	29,	28,	31,	30,	25,	24,	27,	26,
1532 	10,	10,	11,	11,	8,	8,	9,	9,
1533 	14,	14,	15,	15,	12,	12,	13,	13,
1534 	86,	82,	94,	90,	70,	66,	78,	74,
1535 	118,	114,	126,	122,	102,	98,	110,	106,
1536 	43,	41,	47,	45,	35,	33,	39,	37,
1537 	59,	57,	63,	61,	51,	49,	55,	53,
1538 	1,	1,	1,	1,	1,	1,	1,	1,
1539 	1,	1,	1,	1,	1,	1,	1,	1,
1540 	0,	0,	0,	0,	0,	0,	0,	0,
1541 	0,	0,	0,	0,	0,	0,	0,	0,
1542 	5,	5,	5,	5,	4,	4,	4,	4,
1543 	7,	7,	7,	7,	6,	6,	6,	6,
1544 	2,	2,	2,	2,	2,	2,	2,	2,
1545 	3,	3,	3,	3,	3,	3,	3,	3
1546 };
1547 #endif /* HAS_8BIT_TABLES */
1548 
1549     /*
1550      *  Visible symbols for modules
1551      */
1552 
1553 EXPORT_SYMBOL(dmasound);
1554 EXPORT_SYMBOL(dmasound_init);
1555 EXPORT_SYMBOL(dmasound_deinit);
1556 EXPORT_SYMBOL(dmasound_write_sq);
1557 EXPORT_SYMBOL(dmasound_catchRadius);
1558 #ifdef HAS_8BIT_TABLES
1559 EXPORT_SYMBOL(dmasound_ulaw2dma8);
1560 EXPORT_SYMBOL(dmasound_alaw2dma8);
1561 #endif
1562