2 * drivers/sbus/audio/audio.c
4 * Copyright (C) 1996,1997 Thomas K. Dyas (tdyas@eden.rutgers.edu)
5 * Copyright (C) 1997 Derrick J. Brashear (shadow@dementia.org)
6 * Copyright (C) 1997 Brent Baccala (baccala@freesoft.org)
8 * Mixer code adapted from code contributed by and
9 * Copyright (C) 1998 Michael Mraka (michael@fi.muni.cz)
12 * This is the audio midlayer that sits between the VFS character
13 * devices and the low-level audio hardware device drivers.
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
23 #include <linux/tqueue.h>
24 #include <linux/major.h>
25 #include <linux/malloc.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/soundcard.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
32 #include <asm/audioio.h>
36 * Low-level driver interface.
39 /* We only support one low-level audio driver currently. */
40 static struct sparcaudio_driver
*driver
= NULL
;
42 int register_sparcaudio_driver(struct sparcaudio_driver
*drv
)
46 /* If a driver is already present, don't allow the register. */
50 /* Ensure that the driver has a proper operations structure. */
51 if (!drv
->ops
|| !drv
->ops
->start_output
|| !drv
->ops
->stop_output
)
54 /* Setup the circular queues of output and input buffers
56 * Each buffer is a single page, but output buffers might
57 * be partially filled (by a write with count < 4096),
58 * so each output buffer also has a paired output size.
60 * Input buffers, on the other hand, always fill completely,
61 * so we don't need input counts - each contains 4096
62 * bytes of audio data.
64 * TODO: Make number of input/output buffers tunable parameters
67 drv
->num_output_buffers
= 32;
68 drv
->playing_count
= 0;
69 drv
->output_front
= 0;
71 drv
->output_count
= 0;
72 drv
->output_active
= 0;
73 drv
->output_buffers
= kmalloc(drv
->num_output_buffers
* sizeof(__u8
*), GFP_KERNEL
);
74 drv
->output_sizes
= kmalloc(drv
->num_output_buffers
* sizeof(size_t), GFP_KERNEL
);
75 if (!drv
->output_buffers
|| !drv
->output_sizes
) goto kmalloc_failed1
;
77 /* Allocate the pages for each output buffer. */
78 for (i
= 0; i
< drv
->num_output_buffers
; i
++) {
79 drv
->output_buffers
[i
] = (void *) __get_free_page(GFP_KERNEL
);
80 if (!drv
->output_buffers
[i
]) goto kmalloc_failed2
;
83 /* Setup the circular queue of input buffers. */
84 drv
->num_input_buffers
= 32;
85 drv
->recording_count
= 0;
89 drv
->input_active
= 0;
90 drv
->input_buffers
= kmalloc(drv
->num_input_buffers
* sizeof(__u8
*), GFP_KERNEL
);
91 if (!drv
->input_buffers
) goto kmalloc_failed3
;
93 /* Allocate the pages for each input buffer. */
94 for (i
= 0; i
< drv
->num_input_buffers
; i
++) {
95 drv
->input_buffers
[i
] = (void *) __get_free_page(GFP_KERNEL
);
96 if (!drv
->input_buffers
[i
]) goto kmalloc_failed4
;
99 /* Ensure that the driver is marked as not being open. */
109 for (i
--; i
>= 0; i
--)
110 free_page((unsigned long) drv
->input_buffers
[i
]);
113 if (drv
->input_buffers
)
114 kfree(drv
->input_buffers
);
115 i
= drv
->num_output_buffers
;
118 for (i
--; i
>= 0; i
--)
119 free_page((unsigned long) drv
->output_buffers
[i
]);
122 if (drv
->output_buffers
)
123 kfree(drv
->output_buffers
);
124 if (drv
->output_sizes
)
125 kfree(drv
->output_sizes
);
130 int unregister_sparcaudio_driver(struct sparcaudio_driver
*drv
)
134 /* Make sure that the current driver is unregistering. */
138 /* Deallocate the queue of output buffers. */
139 for (i
= 0; i
< driver
->num_output_buffers
; i
++)
140 free_page((unsigned long) driver
->output_buffers
[i
]);
141 kfree(driver
->output_buffers
);
142 kfree(driver
->output_sizes
);
144 /* Deallocate the queue of input buffers. */
145 for (i
= 0; i
< driver
->num_input_buffers
; i
++)
146 free_page((unsigned long) driver
->input_buffers
[i
]);
147 kfree(driver
->input_buffers
);
155 void sparcaudio_output_done(struct sparcaudio_driver
* drv
, int reclaim
)
157 /* Reclaim a buffer unless it's still in the DMA pipe */
159 if (drv
->output_count
> 0)
162 if (drv
->playing_count
> 0)
163 drv
->playing_count
--;
165 drv
->playing_count
++;
167 /* Point the queue after the "done" buffer. */
168 drv
->output_size
-= drv
->output_sizes
[drv
->output_front
];
169 drv
->output_front
= (drv
->output_front
+ 1) % drv
->num_output_buffers
;
171 /* If the output queue is empty, shutdown the driver. */
172 if (drv
->output_count
== 0) {
173 if (drv
->playing_count
== 0) {
174 /* Stop the lowlevel driver from outputing. */
175 drv
->ops
->stop_output(drv
);
176 drv
->output_active
= 0;
178 /* Wake up any waiting writers or syncers and return. */
179 wake_up_interruptible(&drv
->output_write_wait
);
180 wake_up_interruptible(&drv
->output_drain_wait
);
185 /* If we got back a buffer, see if anyone wants to write to it */
186 if (reclaim
|| ((drv
->output_count
+ drv
->playing_count
)
187 < drv
->num_output_buffers
))
188 wake_up_interruptible(&drv
->output_write_wait
);
190 drv
->ops
->start_output(drv
, drv
->output_buffers
[drv
->output_front
],
191 drv
->output_sizes
[drv
->output_front
]);
195 void sparcaudio_input_done(struct sparcaudio_driver
* drv
)
197 /* Point the queue after the "done" buffer. */
198 drv
->input_front
= (drv
->input_front
+ 1) % drv
->num_input_buffers
;
201 /* If the input queue is full, shutdown the driver. */
202 if (drv
->input_count
== drv
->num_input_buffers
) {
203 /* Stop the lowlevel driver from inputing. */
204 drv
->ops
->stop_input(drv
);
205 drv
->input_active
= 0;
207 /* Otherwise, give the driver the next buffer. */
208 drv
->ops
->start_input(drv
, drv
->input_buffers
[drv
->input_front
],
212 /* Wake up any tasks that are waiting. */
213 wake_up_interruptible(&drv
->input_read_wait
);
219 * VFS layer interface
222 static loff_t
sparcaudio_llseek(struct file
* file
, loff_t offset
, int origin
)
227 static ssize_t
sparcaudio_read(struct file
* file
,
228 char *buf
, size_t count
, loff_t
*ppos
)
232 if (! file
->f_mode
& FMODE_READ
)
235 if (driver
->input_count
== 0) {
236 interruptible_sleep_on(&driver
->input_read_wait
);
237 if (signal_pending(current
))
241 bytes_to_copy
= 4096 - driver
->input_offset
;
242 if (bytes_to_copy
> count
)
243 bytes_to_copy
= count
;
245 copy_to_user_ret(buf
, driver
->input_buffers
[driver
->input_rear
]+driver
->input_offset
,
246 bytes_to_copy
, -EFAULT
);
247 driver
->input_offset
+= bytes_to_copy
;
249 if (driver
->input_offset
>= 4096) {
250 driver
->input_rear
= (driver
->input_rear
+ 1) % driver
->num_input_buffers
;
251 driver
->input_count
--;
252 driver
->input_offset
= 0;
255 return bytes_to_copy
;
258 static void sparcaudio_reorganize_buffers(struct sparcaudio_driver
* driver
)
260 /* It may never matter but if it does this routine will pack */
261 /* buffers to free space for more data */
264 static void sparcaudio_sync_output(struct sparcaudio_driver
* driver
)
268 /* If the low-level driver is not active, activate it. */
270 if ((!driver
->output_active
) && (driver
->output_count
> 0)) {
271 driver
->ops
->start_output(driver
,
272 driver
->output_buffers
[driver
->output_front
],
273 driver
->output_sizes
[driver
->output_front
]);
274 driver
->output_active
= 1;
276 restore_flags(flags
);
279 static ssize_t
sparcaudio_write(struct file
* file
, const char *buf
,
280 size_t count
, loff_t
*ppos
)
282 int bytes_written
= 0, bytes_to_copy
;
284 if (! file
->f_mode
& FMODE_WRITE
)
287 /* Loop until all output is written to device. */
289 /* Check to make sure that an output buffer is available. */
290 /* If not, make valiant attempt */
291 if (driver
->num_output_buffers
==
292 (driver
->output_count
+ driver
->playing_count
))
293 sparcaudio_reorganize_buffers(driver
);
295 if (driver
->num_output_buffers
==
296 (driver
->output_count
+ driver
->playing_count
)) {
297 /* We need buffers, so... */
298 sparcaudio_sync_output(driver
);
299 interruptible_sleep_on(&driver
->output_write_wait
);
300 if (signal_pending(current
))
301 return bytes_written
> 0 ? bytes_written
: -EINTR
;
304 /* No buffers were freed. Go back to sleep */
305 if (driver
->num_output_buffers
==
306 (driver
->output_count
+ driver
->playing_count
))
309 /* Determine how much we can copy in this iteration. */
310 bytes_to_copy
= count
;
311 if (bytes_to_copy
> 4096)
312 bytes_to_copy
= 4096;
314 copy_from_user_ret(driver
->output_buffers
[driver
->output_rear
],
315 buf
, bytes_to_copy
, -EFAULT
);
317 /* Update the queue pointers. */
318 buf
+= bytes_to_copy
;
319 count
-= bytes_to_copy
;
320 bytes_written
+= bytes_to_copy
;
321 driver
->output_sizes
[driver
->output_rear
] = bytes_to_copy
;
322 driver
->output_rear
= (driver
->output_rear
+ 1) % driver
->num_output_buffers
;
323 driver
->output_count
++;
324 driver
->output_size
+= bytes_to_copy
;
326 sparcaudio_sync_output(driver
);
328 /* Return the number of bytes written to the caller. */
329 return bytes_written
;
332 #define COPY_IN(arg, get) get_user(get, (int *)arg)
333 #define COPY_OUT(arg, ret) put_user(ret, (int *)arg)
335 /* Add these in as new devices are supported. Belongs in audioio.h, actually */
336 #define SUPPORTED_MIXER_DEVICES (SOUND_MASK_VOLUME)
337 #define MONO_DEVICES (SOUND_MASK_BASS | SOUND_MASK_TREBLE | SOUND_MASK_SPEAKER | SOUND_MASK_MIC)
339 static inline int sparcaudio_mixer_ioctl(struct inode
* inode
, struct file
* file
,
340 unsigned int cmd
, unsigned long arg
)
343 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
344 /* For any missing routines, pretend we changed things anyhow for now */
345 switch (cmd
& 0xff) {
346 case SOUND_MIXER_VOLUME
:
347 if (driver
->ops
->get_output_channels
)
348 j
= driver
->ops
->get_output_channels(driver
);
352 if (driver
->ops
->set_output_volume
)
353 driver
->ops
->set_output_volume(driver
, i
* 255/100);
354 if (driver
->ops
->get_output_volume
)
355 i
= driver
->ops
->get_output_volume(driver
);
358 /* there should be stuff here which calculates balance and
359 volume on a stereo device. will do it eventually */
361 if (driver
->ops
->set_output_volume
)
362 driver
->ops
->set_output_volume(driver
, i
* 255/100);
363 if (driver
->ops
->get_output_volume
)
364 i
= driver
->ops
->get_output_volume(driver
);
366 if (driver
->ops
->set_output_balance
)
367 driver
->ops
->set_output_balance(driver
, j
);
368 if (driver
->ops
->get_output_balance
)
369 j
= driver
->ops
->get_output_balance(driver
);
372 return COPY_OUT(arg
, i
);
374 /* Play like we support other things */
375 return COPY_OUT(arg
, i
);
378 switch (cmd
& 0xff) {
379 case SOUND_MIXER_RECSRC
:
380 if (driver
->ops
->get_input_port
)
381 i
= driver
->ops
->get_input_port(driver
);
382 /* only one should ever be selected */
383 if (i
& AUDIO_ANALOG_LOOPBACK
) j
= SOUND_MASK_IMIX
; /* ? */
384 if (i
& AUDIO_CD
) j
= SOUND_MASK_CD
;
385 if (i
& AUDIO_LINE_IN
) j
= SOUND_MASK_LINE
;
386 if (i
& AUDIO_MICROPHONE
) j
= SOUND_MASK_MIC
;
388 return COPY_OUT(arg
, j
);
390 case SOUND_MIXER_RECMASK
:
391 if (driver
->ops
->get_input_ports
)
392 i
= driver
->ops
->get_input_ports(driver
);
393 /* what do we support? */
394 if (i
& AUDIO_MICROPHONE
) j
|= SOUND_MASK_MIC
;
395 if (i
& AUDIO_LINE_IN
) j
|= SOUND_MASK_LINE
;
396 if (i
& AUDIO_CD
) j
|= SOUND_MASK_CD
;
397 if (i
& AUDIO_ANALOG_LOOPBACK
) j
|= SOUND_MASK_IMIX
; /* ? */
399 return COPY_OUT(arg
, j
);
401 case SOUND_MIXER_CAPS
: /* mixer capabilities */
402 i
= SOUND_CAP_EXCL_INPUT
;
403 return COPY_OUT(arg
, i
);
405 case SOUND_MIXER_DEVMASK
: /* all supported devices */
406 case SOUND_MIXER_STEREODEVS
: /* what supports stereo */
407 if (driver
->ops
->get_input_ports
)
408 i
= driver
->ops
->get_input_ports(driver
);
409 /* what do we support? */
410 if (i
& AUDIO_MICROPHONE
) j
|= SOUND_MASK_MIC
;
411 if (i
& AUDIO_LINE_IN
) j
|= SOUND_MASK_LINE
;
412 if (i
& AUDIO_CD
) j
|= SOUND_MASK_CD
;
413 if (i
& AUDIO_ANALOG_LOOPBACK
) j
|= SOUND_MASK_IMIX
; /* ? */
415 if (driver
->ops
->get_output_ports
)
416 i
= driver
->ops
->get_output_ports(driver
);
417 if (i
& AUDIO_SPEAKER
) j
|= SOUND_MASK_SPEAKER
;
418 if (i
& AUDIO_HEADPHONE
) j
|= SOUND_MASK_LINE
; /* ? */
419 if (i
& AUDIO_LINE_OUT
) j
|= SOUND_MASK_LINE
;
421 j
|= SOUND_MASK_VOLUME
;
423 if ((cmd
& 0xff) == SOUND_MIXER_STEREODEVS
)
424 j
&= ~(MONO_DEVICES
);
425 return COPY_OUT(arg
, j
);
427 case SOUND_MIXER_VOLUME
:
428 if (driver
->ops
->get_output_channels
)
429 j
= driver
->ops
->get_output_channels(driver
);
431 if (driver
->ops
->get_output_volume
)
432 i
= driver
->ops
->get_output_volume(driver
);
435 /* there should be stuff here which calculates balance and
436 volume on a stereo device. will do it eventually */
437 if (driver
->ops
->get_output_volume
)
438 i
= driver
->ops
->get_output_volume(driver
);
439 if (driver
->ops
->get_output_balance
)
440 j
= driver
->ops
->get_output_balance(driver
);
443 return COPY_OUT(arg
, i
);
446 /* Play like we support other things */
447 return COPY_OUT(arg
, i
);
452 static int sparcaudio_ioctl(struct inode
* inode
, struct file
* file
,
453 unsigned int cmd
, unsigned long arg
)
456 struct audio_info ainfo
;
458 if (((cmd
>> 8) & 0xff) == 'M') {
459 return sparcaudio_mixer_ioctl(inode
, file
, cmd
, arg
);
463 case SNDCTL_DSP_SYNC
:
465 if (driver
->output_count
> 0) {
466 interruptible_sleep_on(&driver
->output_drain_wait
);
467 retval
= signal_pending(current
) ? -EINTR
: 0;
472 if (driver
->output_active
&& (file
->f_mode
& FMODE_WRITE
)) {
473 wake_up_interruptible(&driver
->output_write_wait
);
474 driver
->ops
->stop_output(driver
);
475 driver
->output_active
= 0;
476 driver
->output_front
= 0;
477 driver
->output_rear
= 0;
478 driver
->output_count
= 0;
479 driver
->output_size
= 0;
480 driver
->playing_count
= 0;
482 if (driver
->input_active
&& (file
->f_mode
& FMODE_READ
)) {
483 wake_up_interruptible(&driver
->input_read_wait
);
484 driver
->ops
->stop_input(driver
);
485 driver
->input_active
= 0;
486 driver
->input_front
= 0;
487 driver
->input_rear
= 0;
488 driver
->input_count
= 0;
489 driver
->recording_count
= 0;
491 if ((file
->f_mode
& FMODE_READ
) &&
492 !(driver
->flags
& SDF_OPEN_READ
)) {
493 driver
->ops
->start_input(driver
,
494 driver
->input_buffers
[driver
->input_front
],
496 driver
->input_active
= 1;
498 if ((file
->f_mode
& FMODE_WRITE
) &&
499 !(driver
->flags
& SDF_OPEN_WRITE
)) {
500 sparcaudio_sync_output(driver
);
506 if (driver
->ops
->sunaudio_getdev
) {
509 driver
->ops
->sunaudio_getdev(driver
, &tmp
);
511 copy_to_user_ret((audio_device_t
*)arg
, &tmp
, sizeof(tmp
), -EFAULT
);
517 case AUDIO_GETDEV_SUNOS
:
518 if (driver
->ops
->sunaudio_getdev_sunos
) {
519 int tmp
=driver
->ops
->sunaudio_getdev_sunos(driver
);
521 if (put_user(tmp
, (int *)arg
))
530 AUDIO_INITINFO(&ainfo
);
532 if (driver
->ops
->get_input_rate
)
533 ainfo
.record
.sample_rate
=
534 driver
->ops
->get_input_rate(driver
);
535 if (driver
->ops
->get_input_channels
)
536 ainfo
.record
.channels
=
537 driver
->ops
->get_input_channels(driver
);
538 if (driver
->ops
->get_input_precision
)
539 ainfo
.record
.precision
=
540 driver
->ops
->get_input_precision(driver
);
541 if (driver
->ops
->get_input_encoding
)
542 ainfo
.record
.encoding
=
543 driver
->ops
->get_input_encoding(driver
);
544 if (driver
->ops
->get_input_volume
)
546 driver
->ops
->get_input_volume(driver
);
547 if (driver
->ops
->get_input_port
)
549 driver
->ops
->get_input_port(driver
);
550 if (driver
->ops
->get_input_ports
)
551 ainfo
.record
.avail_ports
=
552 driver
->ops
->get_input_ports(driver
);
553 ainfo
.record
.buffer_size
= 4096;
554 ainfo
.record
.samples
= 0;
555 ainfo
.record
.eof
= 0;
556 ainfo
.record
.pause
= 0;
557 ainfo
.record
.error
= 0;
558 ainfo
.record
.waiting
= 0;
559 if (driver
->ops
->get_input_balance
)
560 ainfo
.record
.balance
=
561 driver
->ops
->get_input_balance(driver
);
562 ainfo
.record
.minordev
= 4;
563 ainfo
.record
.open
= 1;
564 ainfo
.record
.active
= 0;
566 if (driver
->ops
->get_output_rate
)
567 ainfo
.play
.sample_rate
=
568 driver
->ops
->get_output_rate(driver
);
569 if (driver
->ops
->get_output_channels
)
570 ainfo
.play
.channels
=
571 driver
->ops
->get_output_channels(driver
);
572 if (driver
->ops
->get_output_precision
)
573 ainfo
.play
.precision
=
574 driver
->ops
->get_output_precision(driver
);
575 if (driver
->ops
->get_output_encoding
)
576 ainfo
.play
.encoding
=
577 driver
->ops
->get_output_encoding(driver
);
578 if (driver
->ops
->get_output_volume
)
580 driver
->ops
->get_output_volume(driver
);
581 if (driver
->ops
->get_output_port
)
583 driver
->ops
->get_output_port(driver
);
584 if (driver
->ops
->get_output_ports
)
585 ainfo
.play
.avail_ports
=
586 driver
->ops
->get_output_ports(driver
);
587 /* This is not defined in the play context in Solaris */
588 ainfo
.play
.buffer_size
= 0;
589 ainfo
.play
.samples
= 0;
591 ainfo
.play
.pause
= 0;
592 ainfo
.play
.error
= 0;
593 ainfo
.play
.waiting
= waitqueue_active(&driver
->open_wait
);
594 if (driver
->ops
->get_output_balance
)
596 (unsigned char)driver
->ops
->get_output_balance(driver
);
597 ainfo
.play
.minordev
= 4;
599 ainfo
.play
.active
= driver
->output_active
;
601 if (driver
->ops
->get_monitor_volume
)
603 driver
->ops
->get_monitor_volume(driver
);
605 if (driver
->ops
->get_output_muted
)
607 (unsigned char)driver
->ops
->get_output_muted(driver
);
609 copy_to_user_ret((struct audio_info
*)arg
, &ainfo
,
610 sizeof(ainfo
), -EFAULT
);
616 audio_info_t curinfo
, newinfo
;
618 copy_from_user_ret(&ainfo
, (audio_info_t
*) arg
, sizeof(audio_info_t
), -EFAULT
);
620 /* Without these there's no point in trying */
621 if (!driver
->ops
->get_input_precision
||
622 !driver
->ops
->get_input_channels
||
623 !driver
->ops
->get_input_rate
||
624 !driver
->ops
->get_input_encoding
||
625 !driver
->ops
->get_output_precision
||
626 !driver
->ops
->get_output_channels
||
627 !driver
->ops
->get_output_rate
||
628 !driver
->ops
->get_output_encoding
)
634 /* Do bounds checking for things which always apply.
635 * Follow with enforcement of basic tenets of certain
636 * encodings. Everything over and above generic is
637 * enforced by the driver, which can assume that
638 * Martian cases are taken care of here. */
639 if (Modify(ainfo
.play
.gain
) &&
640 ((ainfo
.play
.gain
> AUDIO_MAX_GAIN
) ||
641 (ainfo
.play
.gain
< AUDIO_MIN_GAIN
))) {
642 /* Need to differentiate this from e.g. the above error */
646 if (Modify(ainfo
.record
.gain
) &&
647 ((ainfo
.record
.gain
> AUDIO_MAX_GAIN
) ||
648 (ainfo
.record
.gain
< AUDIO_MIN_GAIN
))) {
652 if (Modify(ainfo
.monitor_gain
) &&
653 ((ainfo
.monitor_gain
> AUDIO_MAX_GAIN
) ||
654 (ainfo
.monitor_gain
< AUDIO_MIN_GAIN
))) {
658 /* Don't need to check less than zero on these */
659 if (Modifyc(ainfo
.play
.balance
) &&
660 (ainfo
.play
.balance
> AUDIO_RIGHT_BALANCE
)) {
664 if (Modifyc(ainfo
.record
.balance
) &&
665 (ainfo
.record
.balance
> AUDIO_RIGHT_BALANCE
)) {
670 /* If any of these changed, record them all, then make
671 * changes atomically. If something fails, back it all out. */
672 if (Modify(ainfo
.record
.precision
) ||
673 Modify(ainfo
.record
.sample_rate
) ||
674 Modify(ainfo
.record
.channels
) ||
675 Modify(ainfo
.record
.encoding
) ||
676 Modify(ainfo
.play
.precision
) ||
677 Modify(ainfo
.play
.sample_rate
) ||
678 Modify(ainfo
.play
.channels
) ||
679 Modify(ainfo
.play
.encoding
))
681 /* If they're trying to change something we
682 * have no routine for, they lose */
683 if ((!driver
->ops
->set_input_encoding
&&
684 Modify(ainfo
.record
.encoding
)) ||
685 (!driver
->ops
->set_input_rate
&&
686 Modify(ainfo
.record
.sample_rate
)) ||
687 (!driver
->ops
->set_input_precision
&&
688 Modify(ainfo
.record
.precision
)) ||
689 (!driver
->ops
->set_input_channels
&&
690 Modify(ainfo
.record
.channels
))) {
695 curinfo
.record
.encoding
= driver
->ops
->get_input_encoding(driver
);
696 curinfo
.record
.sample_rate
= driver
->ops
->get_input_rate(driver
);
697 curinfo
.record
.precision
= driver
->ops
->get_input_precision(driver
);
698 curinfo
.record
.channels
= driver
->ops
->get_input_channels(driver
);
699 newinfo
.record
.encoding
= Modify(ainfo
.record
.encoding
) ?
700 ainfo
.record
.encoding
: curinfo
.record
.encoding
;
701 newinfo
.record
.sample_rate
= Modify(ainfo
.record
.sample_rate
)?
702 ainfo
.record
.sample_rate
: curinfo
.record
.sample_rate
;
703 newinfo
.record
.precision
= Modify(ainfo
.record
.precision
) ?
704 ainfo
.record
.precision
: curinfo
.record
.precision
;
705 newinfo
.record
.channels
= Modify(ainfo
.record
.channels
) ?
706 ainfo
.record
.channels
: curinfo
.record
.channels
;
708 switch (newinfo
.record
.encoding
) {
709 case AUDIO_ENCODING_ALAW
:
710 case AUDIO_ENCODING_ULAW
:
711 if (newinfo
.record
.precision
!= 8) {
715 if (newinfo
.record
.channels
!= 1) {
720 case AUDIO_ENCODING_LINEAR
:
721 case AUDIO_ENCODING_LINEARLE
:
722 if (newinfo
.record
.precision
!= 16) {
726 if (newinfo
.record
.channels
!= 1 &&
727 newinfo
.record
.channels
!= 2)
733 case AUDIO_ENCODING_LINEAR8
:
734 if (newinfo
.record
.precision
!= 8) {
738 if (newinfo
.record
.channels
!= 1 &&
739 newinfo
.record
.channels
!= 2)
749 /* If they're trying to change something we
750 * have no routine for, they lose */
751 if ((!driver
->ops
->set_output_encoding
&&
752 Modify(ainfo
.play
.encoding
)) ||
753 (!driver
->ops
->set_output_rate
&&
754 Modify(ainfo
.play
.sample_rate
)) ||
755 (!driver
->ops
->set_output_precision
&&
756 Modify(ainfo
.play
.precision
)) ||
757 (!driver
->ops
->set_output_channels
&&
758 Modify(ainfo
.play
.channels
))) {
763 curinfo
.play
.encoding
= driver
->ops
->get_output_encoding(driver
);
764 curinfo
.play
.sample_rate
= driver
->ops
->get_output_rate(driver
);
765 curinfo
.play
.precision
= driver
->ops
->get_output_precision(driver
);
766 curinfo
.play
.channels
= driver
->ops
->get_output_channels(driver
);
767 newinfo
.play
.encoding
= Modify(ainfo
.play
.encoding
) ?
768 ainfo
.play
.encoding
: curinfo
.play
.encoding
;
769 newinfo
.play
.sample_rate
= Modify(ainfo
.play
.sample_rate
) ?
770 ainfo
.play
.sample_rate
: curinfo
.play
.sample_rate
;
771 newinfo
.play
.precision
= Modify(ainfo
.play
.precision
) ?
772 ainfo
.play
.precision
: curinfo
.play
.precision
;
773 newinfo
.play
.channels
= Modify(ainfo
.play
.channels
) ?
774 ainfo
.play
.channels
: curinfo
.play
.channels
;
776 switch (newinfo
.play
.encoding
) {
777 case AUDIO_ENCODING_ALAW
:
778 case AUDIO_ENCODING_ULAW
:
779 if (newinfo
.play
.precision
!= 8) {
783 if (newinfo
.play
.channels
!= 1) {
788 case AUDIO_ENCODING_LINEAR
:
789 case AUDIO_ENCODING_LINEARLE
:
790 if (newinfo
.play
.precision
!= 16) {
794 if (newinfo
.play
.channels
!= 1 &&
795 newinfo
.play
.channels
!= 2)
801 case AUDIO_ENCODING_LINEAR8
:
802 if (newinfo
.play
.precision
!= 8) {
806 if (newinfo
.play
.channels
!= 1 &&
807 newinfo
.play
.channels
!= 2)
817 /* If we got this far, we're at least sane with
818 * respect to generics. Try the changes. */
819 if ((driver
->ops
->set_input_precision(driver
, ainfo
.record
.precision
) < 0) ||
820 (driver
->ops
->set_output_precision(driver
, ainfo
.play
.precision
) < 0) ||
821 (driver
->ops
->set_input_channels(driver
, ainfo
.record
.channels
) < 0) ||
822 (driver
->ops
->set_output_channels(driver
, ainfo
.play
.channels
) < 0) ||
823 (driver
->ops
->set_input_rate(driver
, ainfo
.record
.sample_rate
) < 0) ||
824 (driver
->ops
->set_output_rate(driver
, ainfo
.play
.sample_rate
) < 0) ||
825 (driver
->ops
->set_input_encoding(driver
, ainfo
.record
.encoding
) < 0) ||
826 (driver
->ops
->set_output_encoding(driver
, ainfo
.play
.encoding
) < 0))
828 /* Pray we can set it all back. If not, uh... */
829 driver
->ops
->set_input_precision(driver
, curinfo
.record
.precision
);
830 driver
->ops
->set_output_precision(driver
, curinfo
.play
.precision
);
831 driver
->ops
->set_input_channels(driver
, curinfo
.record
.channels
);
832 driver
->ops
->set_output_channels(driver
, curinfo
.play
.channels
);
833 driver
->ops
->set_input_rate(driver
, curinfo
.record
.sample_rate
);
834 driver
->ops
->set_output_rate(driver
, curinfo
.play
.sample_rate
);
835 driver
->ops
->set_input_encoding(driver
, curinfo
.record
.encoding
);
836 driver
->ops
->set_output_encoding(driver
, curinfo
.play
.encoding
);
840 /* Maybe this should be a routine instead of a macro */
841 #define IF_SET_DO(x,y) if ((x) && Modify(y)) x(driver, y)
842 #define IF_SETC_DO(x,y) if ((x) && Modifyc(y)) x(driver, y)
843 IF_SETC_DO(driver
->ops
->set_input_balance
, (int)ainfo
.record
.balance
);
844 IF_SETC_DO(driver
->ops
->set_output_balance
, (int)ainfo
.play
.balance
);
845 IF_SET_DO(driver
->ops
->set_input_volume
, ainfo
.record
.gain
);
846 IF_SET_DO(driver
->ops
->set_output_volume
, ainfo
.play
.gain
);
847 IF_SET_DO(driver
->ops
->set_input_port
, ainfo
.record
.port
);
848 IF_SET_DO(driver
->ops
->set_output_port
, ainfo
.play
.port
);
849 IF_SET_DO(driver
->ops
->set_monitor_volume
, ainfo
.monitor_gain
);
850 IF_SETC_DO(driver
->ops
->set_output_muted
, (int)ainfo
.output_muted
);
858 if (driver
->ops
->ioctl
)
859 retval
= driver
->ops
->ioctl(inode
,file
,cmd
,arg
,driver
);
868 static int sparcaudioctl_release(struct inode
* inode
, struct file
* file
)
875 static struct file_operations sparcaudioctl_fops
= {
879 NULL
, /* sparcaudio_readdir */
880 NULL
, /* sparcaudio_select */
882 NULL
, /* sparcaudio_mmap */
885 sparcaudioctl_release
888 static int sparcaudio_open(struct inode
* inode
, struct file
* file
)
890 int minor
= MINOR(inode
->i_rdev
);
893 /* A low-level audio driver must exist. */
898 case SPARCAUDIO_AUDIOCTL_MINOR
:
899 file
->f_op
= &sparcaudioctl_fops
;
902 case SPARCAUDIO_DSP16_MINOR
:
903 case SPARCAUDIO_DSP_MINOR
:
904 case SPARCAUDIO_AUDIO_MINOR
:
905 /* If the driver is busy, then wait to get through. */
907 if (file
->f_mode
& FMODE_READ
&& driver
->flags
& SDF_OPEN_READ
) {
908 if (file
->f_flags
& O_NONBLOCK
)
911 interruptible_sleep_on(&driver
->open_wait
);
912 if (signal_pending(current
))
916 if (file
->f_mode
& FMODE_WRITE
&& driver
->flags
& SDF_OPEN_WRITE
) {
917 if (file
->f_flags
& O_NONBLOCK
)
920 interruptible_sleep_on(&driver
->open_wait
);
921 if (signal_pending(current
))
926 /* Allow the low-level driver to initialize itself. */
927 if (driver
->ops
->open
) {
928 err
= driver
->ops
->open(inode
,file
,driver
);
933 /* Mark the driver as locked for read and/or write. */
934 if (file
->f_mode
& FMODE_READ
) {
935 driver
->input_offset
= 0;
936 driver
->input_front
= 0;
937 driver
->input_rear
= 0;
938 driver
->input_count
= 0;
939 driver
->recording_count
= 0;
940 driver
->ops
->start_input(driver
, driver
->input_buffers
[driver
->input_front
],
942 driver
->input_active
= 1;
943 driver
->flags
|= SDF_OPEN_READ
;
945 if (file
->f_mode
& FMODE_WRITE
) {
946 driver
->playing_count
= 0;
947 driver
->output_size
= 0;
948 driver
->output_front
= 0;
949 driver
->output_rear
= 0;
950 driver
->output_count
= 0;
951 driver
->output_active
= 0;
952 driver
->flags
|= SDF_OPEN_WRITE
;
955 case SPARCAUDIO_MIXER_MINOR
:
956 file
->f_op
= &sparcaudioctl_fops
;
969 static int sparcaudio_release(struct inode
* inode
, struct file
* file
)
971 /* Anything in the queue? */
972 sparcaudio_sync_output(driver
);
975 driver
->ops
->stop_input(driver
);
976 driver
->input_active
= 0;
978 /* Wait for any output still in the queue to be played. */
979 if (driver
->output_count
> 0)
980 interruptible_sleep_on(&driver
->output_drain_wait
);
982 /* Force any output to be stopped. */
983 driver
->ops
->stop_output(driver
);
984 driver
->output_active
= 0;
986 /* Let the low-level driver do any release processing. */
987 if (driver
->ops
->release
)
988 driver
->ops
->release(inode
,file
,driver
);
990 if (file
->f_mode
& FMODE_READ
)
991 driver
->flags
&= ~(SDF_OPEN_READ
);
993 if (file
->f_mode
& FMODE_WRITE
)
994 driver
->flags
&= ~(SDF_OPEN_WRITE
);
998 wake_up_interruptible(&driver
->open_wait
);
1003 static struct file_operations sparcaudio_fops
= {
1007 NULL
, /* sparcaudio_readdir */
1008 NULL
, /* sparcaudio_select */
1010 NULL
, /* sparcaudio_mmap */
1015 EXPORT_SYMBOL(register_sparcaudio_driver
);
1016 EXPORT_SYMBOL(unregister_sparcaudio_driver
);
1017 EXPORT_SYMBOL(sparcaudio_output_done
);
1018 EXPORT_SYMBOL(sparcaudio_input_done
);
1021 int init_module(void)
1023 __initfunc(int sparcaudio_init(void))
1026 /* Register our character device driver with the VFS. */
1027 if (register_chrdev(SOUND_MAJOR
, "sparcaudio", &sparcaudio_fops
))
1030 #ifdef CONFIG_SPARCAUDIO_AMD7930
1034 #ifdef CONFIG_SPARCAUDIO_CS4231
1042 void cleanup_module(void)
1044 unregister_chrdev(SOUND_MAJOR
, "sparcaudio");
1049 * Overrides for Emacs so that we follow Linus's tabbing style.
1050 * Emacs will notice this stuff at the end of the file and automatically
1051 * adjust the settings for this buffer only. This must remain at the end
1053 * ---------------------------------------------------------------------------
1056 * c-brace-imaginary-offset: 0
1057 * c-brace-offset: -4
1058 * c-argdecl-indent: 4
1059 * c-label-offset: -4
1060 * c-continued-statement-offset: 4
1061 * c-continued-brace-offset: 0
1062 * indent-tabs-mode: nil