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