Import 2.1.118
[davej-history.git] / drivers / sbus / audio / audio.c
blobc98923da439ef96a884f0ad7e741d451e8e632b2
1 /*
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)
7 *
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>
19 #include <linux/fs.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.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)
44 int i;
46 /* If a driver is already present, don't allow the register. */
47 if (driver)
48 return -EIO;
50 /* Ensure that the driver has a proper operations structure. */
51 if (!drv->ops || !drv->ops->start_output || !drv->ops->stop_output)
52 return -EINVAL;
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;
70 drv->output_rear = 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;
86 drv->input_front = 0;
87 drv->input_rear = 0;
88 drv->input_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. */
100 drv->flags = 0;
102 MOD_INC_USE_COUNT;
104 driver = drv;
105 return 0;
108 kmalloc_failed4:
109 for (i--; i >= 0; i--)
110 free_page((unsigned long) drv->input_buffers[i]);
112 kmalloc_failed3:
113 if (drv->input_buffers)
114 kfree(drv->input_buffers);
115 i = drv->num_output_buffers;
117 kmalloc_failed2:
118 for (i--; i >= 0; i--)
119 free_page((unsigned long) drv->output_buffers[i]);
121 kmalloc_failed1:
122 if (drv->output_buffers)
123 kfree(drv->output_buffers);
124 if (drv->output_sizes)
125 kfree(drv->output_sizes);
127 return -ENOMEM;
130 int unregister_sparcaudio_driver(struct sparcaudio_driver *drv)
132 int i;
134 /* Make sure that the current driver is unregistering. */
135 if (driver != drv)
136 return -EIO;
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);
149 MOD_DEC_USE_COUNT;
151 driver = NULL;
152 return 0;
155 void sparcaudio_output_done(struct sparcaudio_driver * drv, int reclaim)
157 /* Reclaim a buffer unless it's still in the DMA pipe */
158 if (reclaim) {
159 if (drv->output_count > 0)
160 drv->output_count--;
161 else
162 if (drv->playing_count > 0)
163 drv->playing_count--;
164 } else
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);
181 return;
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;
199 drv->input_count++;
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;
206 } else {
207 /* Otherwise, give the driver the next buffer. */
208 drv->ops->start_input(drv, drv->input_buffers[drv->input_front],
209 4096);
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)
224 return -ESPIPE;
227 static ssize_t sparcaudio_read(struct file * file,
228 char *buf, size_t count, loff_t *ppos)
230 int bytes_to_copy;
232 if (! file->f_mode & FMODE_READ)
233 return -EINVAL;
235 if (driver->input_count == 0) {
236 interruptible_sleep_on(&driver->input_read_wait);
237 if (signal_pending(current))
238 return -EINTR;
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)
266 unsigned long flags;
268 /* If the low-level driver is not active, activate it. */
269 save_and_cli(flags);
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)
285 return -EINVAL;
287 /* Loop until all output is written to device. */
288 while (count > 0) {
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))
307 continue;
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)
342 int i = 0, j = 0;
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);
349 COPY_IN(arg, i);
350 if (j == 1) {
351 i = s_to_m(i);
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);
356 i = m_to_s(i);
357 } else {
358 /* there should be stuff here which calculates balance and
359 volume on a stereo device. will do it eventually */
360 i = s_to_g(i);
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);
365 j = s_to_b(i);
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);
370 i = b_to_s(i,j);
372 return COPY_OUT(arg, i);
373 default:
374 /* Play like we support other things */
375 return COPY_OUT(arg, i);
377 } else {
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);
430 if (j == 1) {
431 if (driver->ops->get_output_volume)
432 i = driver->ops->get_output_volume(driver);
433 i = m_to_s(i);
434 } else {
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);
441 i = b_to_s(i,j);
443 return COPY_OUT(arg, i);
445 default:
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)
455 int retval = 0;
456 struct audio_info ainfo;
458 if (((cmd >> 8) & 0xff) == 'M') {
459 return sparcaudio_mixer_ioctl(inode, file, cmd, arg);
462 switch (cmd) {
463 case SNDCTL_DSP_SYNC:
464 case AUDIO_DRAIN:
465 if (driver->output_count > 0) {
466 interruptible_sleep_on(&driver->output_drain_wait);
467 retval = signal_pending(current) ? -EINTR : 0;
469 break;
471 case AUDIO_FLUSH:
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],
495 4096);
496 driver->input_active = 1;
498 if ((file->f_mode & FMODE_WRITE) &&
499 !(driver->flags & SDF_OPEN_WRITE)) {
500 sparcaudio_sync_output(driver);
502 break;
505 case AUDIO_GETDEV:
506 if (driver->ops->sunaudio_getdev) {
507 audio_device_t tmp;
509 driver->ops->sunaudio_getdev(driver, &tmp);
511 copy_to_user_ret((audio_device_t *)arg, &tmp, sizeof(tmp), -EFAULT);
512 } else
513 retval = -EINVAL;
515 break;
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))
522 retval = -EFAULT;
523 } else
524 retval = -EINVAL;
526 break;
528 case AUDIO_GETINFO:
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)
545 ainfo.record.gain =
546 driver->ops->get_input_volume(driver);
547 if (driver->ops->get_input_port)
548 ainfo.record.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)
579 ainfo.play.gain =
580 driver->ops->get_output_volume(driver);
581 if (driver->ops->get_output_port)
582 ainfo.play.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;
590 ainfo.play.eof = 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)
595 ainfo.play.balance =
596 (unsigned char)driver->ops->get_output_balance(driver);
597 ainfo.play.minordev = 4;
598 ainfo.play.open = 1;
599 ainfo.play.active = driver->output_active;
601 if (driver->ops->get_monitor_volume)
602 ainfo.monitor_gain =
603 driver->ops->get_monitor_volume(driver);
605 if (driver->ops->get_output_muted)
606 ainfo.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);
612 break;
614 case AUDIO_SETINFO:
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)
630 retval = -EINVAL;
631 break;
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 */
643 retval = -EINVAL;
644 break;
646 if (Modify(ainfo.record.gain) &&
647 ((ainfo.record.gain > AUDIO_MAX_GAIN) ||
648 (ainfo.record.gain < AUDIO_MIN_GAIN))) {
649 retval = -EINVAL;
650 break;
652 if (Modify(ainfo.monitor_gain) &&
653 ((ainfo.monitor_gain > AUDIO_MAX_GAIN) ||
654 (ainfo.monitor_gain < AUDIO_MIN_GAIN))) {
655 retval = -EINVAL;
656 break;
658 /* Don't need to check less than zero on these */
659 if (Modifyc(ainfo.play.balance) &&
660 (ainfo.play.balance > AUDIO_RIGHT_BALANCE)) {
661 retval = -EINVAL;
662 break;
664 if (Modifyc(ainfo.record.balance) &&
665 (ainfo.record.balance > AUDIO_RIGHT_BALANCE)) {
666 retval = -EINVAL;
667 break;
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))) {
691 retval = -EINVAL;
692 break;
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) {
712 retval = -EINVAL;
713 break;
715 if (newinfo.record.channels != 1) {
716 retval = -EINVAL;
717 break;
719 break;
720 case AUDIO_ENCODING_LINEAR:
721 case AUDIO_ENCODING_LINEARLE:
722 if (newinfo.record.precision != 16) {
723 retval = -EINVAL;
724 break;
726 if (newinfo.record.channels != 1 &&
727 newinfo.record.channels != 2)
729 retval = -EINVAL;
730 break;
732 break;
733 case AUDIO_ENCODING_LINEAR8:
734 if (newinfo.record.precision != 8) {
735 retval = -EINVAL;
736 break;
738 if (newinfo.record.channels != 1 &&
739 newinfo.record.channels != 2)
741 retval = -EINVAL;
742 break;
746 if (retval < 0)
747 break;
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))) {
759 retval = -EINVAL;
760 break;
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) {
780 retval = -EINVAL;
781 break;
783 if (newinfo.play.channels != 1) {
784 retval = -EINVAL;
785 break;
787 break;
788 case AUDIO_ENCODING_LINEAR:
789 case AUDIO_ENCODING_LINEARLE:
790 if (newinfo.play.precision != 16) {
791 retval = -EINVAL;
792 break;
794 if (newinfo.play.channels != 1 &&
795 newinfo.play.channels != 2)
797 retval = -EINVAL;
798 break;
800 break;
801 case AUDIO_ENCODING_LINEAR8:
802 if (newinfo.play.precision != 8) {
803 retval = -EINVAL;
804 break;
806 if (newinfo.play.channels != 1 &&
807 newinfo.play.channels != 2)
809 retval = -EINVAL;
810 break;
814 if (retval < 0)
815 break;
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);
851 #undef IF_SET_DO
852 #undef IF_SETC_DO
854 break;
857 default:
858 if (driver->ops->ioctl)
859 retval = driver->ops->ioctl(inode,file,cmd,arg,driver);
860 else {
861 retval = -EINVAL;
865 return retval;
868 static int sparcaudioctl_release(struct inode * inode, struct file * file)
870 MOD_DEC_USE_COUNT;
872 return 0;
875 static struct file_operations sparcaudioctl_fops = {
876 NULL,
877 NULL,
878 NULL,
879 NULL, /* sparcaudio_readdir */
880 NULL, /* sparcaudio_select */
881 sparcaudio_ioctl,
882 NULL, /* sparcaudio_mmap */
883 NULL,
884 NULL, /* flush */
885 sparcaudioctl_release
888 static int sparcaudio_open(struct inode * inode, struct file * file)
890 int minor = MINOR(inode->i_rdev);
891 int err;
893 /* A low-level audio driver must exist. */
894 if (!driver)
895 return -ENODEV;
897 switch (minor) {
898 case SPARCAUDIO_AUDIOCTL_MINOR:
899 file->f_op = &sparcaudioctl_fops;
900 break;
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. */
906 retry_open:
907 if (file->f_mode & FMODE_READ && driver->flags & SDF_OPEN_READ) {
908 if (file->f_flags & O_NONBLOCK)
909 return -EBUSY;
911 interruptible_sleep_on(&driver->open_wait);
912 if (signal_pending(current))
913 return -EINTR;
914 goto retry_open;
916 if (file->f_mode & FMODE_WRITE && driver->flags & SDF_OPEN_WRITE) {
917 if (file->f_flags & O_NONBLOCK)
918 return -EBUSY;
920 interruptible_sleep_on(&driver->open_wait);
921 if (signal_pending(current))
922 return -EINTR;
923 goto retry_open;
926 /* Allow the low-level driver to initialize itself. */
927 if (driver->ops->open) {
928 err = driver->ops->open(inode,file,driver);
929 if (err < 0)
930 return err;
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],
941 4096);
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;
954 break;
955 case SPARCAUDIO_MIXER_MINOR:
956 file->f_op = &sparcaudioctl_fops;
957 break;
959 default:
960 return -ENXIO;
963 MOD_INC_USE_COUNT;
965 /* Success! */
966 return 0;
969 static int sparcaudio_release(struct inode * inode, struct file * file)
971 /* Anything in the queue? */
972 sparcaudio_sync_output(driver);
974 /* Stop input */
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);
996 MOD_DEC_USE_COUNT;
998 wake_up_interruptible(&driver->open_wait);
1000 return 0;
1003 static struct file_operations sparcaudio_fops = {
1004 sparcaudio_llseek,
1005 sparcaudio_read,
1006 sparcaudio_write,
1007 NULL, /* sparcaudio_readdir */
1008 NULL, /* sparcaudio_select */
1009 sparcaudio_ioctl,
1010 NULL, /* sparcaudio_mmap */
1011 sparcaudio_open,
1012 sparcaudio_release
1015 EXPORT_SYMBOL(register_sparcaudio_driver);
1016 EXPORT_SYMBOL(unregister_sparcaudio_driver);
1017 EXPORT_SYMBOL(sparcaudio_output_done);
1018 EXPORT_SYMBOL(sparcaudio_input_done);
1020 #ifdef MODULE
1021 int init_module(void)
1022 #else
1023 __initfunc(int sparcaudio_init(void))
1024 #endif
1026 /* Register our character device driver with the VFS. */
1027 if (register_chrdev(SOUND_MAJOR, "sparcaudio", &sparcaudio_fops))
1028 return -EIO;
1030 #ifdef CONFIG_SPARCAUDIO_AMD7930
1031 amd7930_init();
1032 #endif
1034 #ifdef CONFIG_SPARCAUDIO_CS4231
1035 cs4231_init();
1036 #endif
1038 return 0;
1041 #ifdef MODULE
1042 void cleanup_module(void)
1044 unregister_chrdev(SOUND_MAJOR, "sparcaudio");
1046 #endif
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
1052 * of the file.
1053 * ---------------------------------------------------------------------------
1054 * Local variables:
1055 * c-indent-level: 4
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
1063 * tab-width: 8
1064 * End: