2 **********************************************************************
3 * audio.c -- /dev/dsp interface for emu10k1 driver
4 * Copyright 1999, 2000 Creative Labs, Inc.
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * October 20, 1999 Bertrand Lee base code release
11 * November 2, 1999 Alan Cox cleaned up types/leaks
13 **********************************************************************
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public
26 * License along with this program; if not, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
30 **********************************************************************
33 #include <linux/module.h>
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/bitops.h>
38 #include <linux/sched.h>
40 #include <linux/smp_lock.h>
50 static void calculate_ofrag(struct woinst *);
51 static void calculate_ifrag(struct wiinst *);
53 static void emu10k1_waveout_bh(unsigned long refdata);
54 static void emu10k1_wavein_bh(unsigned long refdata);
56 /* Audio file operations */
57 static ssize_t emu10k1_audio_read(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
59 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
60 struct wiinst *wiinst = wave_dev->wiinst;
64 DPD(3, "emu10k1_audio_read(), buffer=%p, count=%d\n", buffer, (u32) count);
66 if (!access_ok(VERIFY_WRITE, buffer, count))
69 spin_lock_irqsave(&wiinst->lock, flags);
71 if (wiinst->mmapped) {
72 spin_unlock_irqrestore(&wiinst->lock, flags);
76 if (wiinst->state == WAVE_STATE_CLOSED) {
77 calculate_ifrag(wiinst);
79 while (emu10k1_wavein_open(wave_dev) < 0) {
80 spin_unlock_irqrestore(&wiinst->lock, flags);
82 if (file->f_flags & O_NONBLOCK)
85 interruptible_sleep_on(&wave_dev->card->open_wait);
87 if (signal_pending(current))
90 spin_lock_irqsave(&wiinst->lock, flags);
94 spin_unlock_irqrestore(&wiinst->lock, flags);
99 spin_lock_irqsave(&wiinst->lock, flags);
101 if (!(wiinst->state & WAVE_STATE_STARTED)
102 && (wave_dev->enablebits & PCM_ENABLE_INPUT))
103 emu10k1_wavein_start(wave_dev);
105 emu10k1_wavein_update(wave_dev->card, wiinst);
106 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
108 spin_unlock_irqrestore(&wiinst->lock, flags);
110 DPD(3, "bytestocopy --> %d\n", bytestocopy);
112 if ((bytestocopy >= wiinst->buffer.fragment_size)
113 || (bytestocopy >= count)) {
116 bytestocopy = min_t(u32, bytestocopy, count);
118 rc = emu10k1_wavein_xferdata(wiinst,
124 count -= bytestocopy;
125 buffer += bytestocopy;
130 if ((file->f_flags & O_NONBLOCK)
131 || (!(wave_dev->enablebits & PCM_ENABLE_INPUT)))
132 return (ret ? ret : -EAGAIN);
134 interruptible_sleep_on(&wiinst->wait_queue);
136 if (signal_pending(current))
137 return (ret ? ret : -ERESTARTSYS);
142 DPD(3, "bytes copied -> %d\n", (u32) ret);
147 static ssize_t emu10k1_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t * ppos)
149 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
150 struct woinst *woinst = wave_dev->woinst;
154 DPD(3, "emu10k1_audio_write(), buffer=%p, count=%d\n", buffer, (u32) count);
156 if (!access_ok(VERIFY_READ, buffer, count))
159 spin_lock_irqsave(&woinst->lock, flags);
161 if (woinst->mmapped) {
162 spin_unlock_irqrestore(&woinst->lock, flags);
165 // This is for emu10k1 revs less than 7, we need to go through tram
166 if (woinst->format.passthrough == 1) {
169 woinst->buffer.ossfragshift = PT_BLOCKSIZE_LOG2;
170 woinst->buffer.numfrags = PT_BLOCKCOUNT;
171 calculate_ofrag(woinst);
173 r = emu10k1_pt_write(file, buffer, count);
174 spin_unlock_irqrestore(&woinst->lock, flags);
178 if (woinst->state == WAVE_STATE_CLOSED) {
179 calculate_ofrag(woinst);
181 while (emu10k1_waveout_open(wave_dev) < 0) {
182 spin_unlock_irqrestore(&woinst->lock, flags);
184 if (file->f_flags & O_NONBLOCK)
187 interruptible_sleep_on(&wave_dev->card->open_wait);
189 if (signal_pending(current))
192 spin_lock_irqsave(&woinst->lock, flags);
196 spin_unlock_irqrestore(&woinst->lock, flags);
199 if (count % woinst->format.bytespersample)
202 count /= woinst->num_voices;
207 spin_lock_irqsave(&woinst->lock, flags);
208 emu10k1_waveout_update(woinst);
209 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
210 spin_unlock_irqrestore(&woinst->lock, flags);
212 DPD(3, "bytestocopy --> %d\n", bytestocopy);
214 if ((bytestocopy >= woinst->buffer.fragment_size)
215 || (bytestocopy >= count)) {
217 bytestocopy = min_t(u32, bytestocopy, count);
219 emu10k1_waveout_xferdata(woinst, (u8 __user *) buffer, &bytestocopy);
221 count -= bytestocopy;
222 buffer += bytestocopy * woinst->num_voices;
223 ret += bytestocopy * woinst->num_voices;
225 spin_lock_irqsave(&woinst->lock, flags);
226 woinst->total_copied += bytestocopy;
228 if (!(woinst->state & WAVE_STATE_STARTED)
229 && (wave_dev->enablebits & PCM_ENABLE_OUTPUT)
230 && (woinst->total_copied >= woinst->buffer.fragment_size))
231 emu10k1_waveout_start(wave_dev);
233 spin_unlock_irqrestore(&woinst->lock, flags);
237 if ((file->f_flags & O_NONBLOCK)
238 || (!(wave_dev->enablebits & PCM_ENABLE_OUTPUT)))
239 return (ret ? ret : -EAGAIN);
241 interruptible_sleep_on(&woinst->wait_queue);
243 if (signal_pending(current))
244 return (ret ? ret : -ERESTARTSYS);
248 DPD(3, "bytes copied -> %d\n", (u32) ret);
253 static int emu10k1_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
255 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
256 struct woinst *woinst = NULL;
257 struct wiinst *wiinst = NULL;
261 int __user *p = (int __user *)arg;
263 DPF(4, "emu10k1_audio_ioctl()\n");
265 if (file->f_mode & FMODE_WRITE)
266 woinst = wave_dev->woinst;
268 if (file->f_mode & FMODE_READ)
269 wiinst = wave_dev->wiinst;
273 DPF(2, "OSS_GETVERSION:\n");
274 return put_user(SOUND_VERSION, p);
276 case SNDCTL_DSP_RESET:
277 DPF(2, "SNDCTL_DSP_RESET:\n");
278 wave_dev->enablebits = PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT;
280 if (file->f_mode & FMODE_WRITE) {
281 spin_lock_irqsave(&woinst->lock, flags);
283 if (woinst->state & WAVE_STATE_OPEN) {
284 emu10k1_waveout_close(wave_dev);
288 woinst->total_copied = 0;
289 woinst->total_played = 0;
292 spin_unlock_irqrestore(&woinst->lock, flags);
295 if (file->f_mode & FMODE_READ) {
296 spin_lock_irqsave(&wiinst->lock, flags);
298 if (wiinst->state & WAVE_STATE_OPEN) {
299 emu10k1_wavein_close(wave_dev);
303 wiinst->total_recorded = 0;
305 spin_unlock_irqrestore(&wiinst->lock, flags);
310 case SNDCTL_DSP_SYNC:
311 DPF(2, "SNDCTL_DSP_SYNC:\n");
313 if (file->f_mode & FMODE_WRITE) {
315 spin_lock_irqsave(&woinst->lock, flags);
317 if (woinst->state & WAVE_STATE_OPEN) {
319 if (woinst->state & WAVE_STATE_STARTED)
320 while ((woinst->total_played < woinst->total_copied)
321 && !signal_pending(current)) {
322 spin_unlock_irqrestore(&woinst->lock, flags);
323 interruptible_sleep_on(&woinst->wait_queue);
324 spin_lock_irqsave(&woinst->lock, flags);
326 emu10k1_waveout_close(wave_dev);
330 woinst->total_copied = 0;
331 woinst->total_played = 0;
334 spin_unlock_irqrestore(&woinst->lock, flags);
337 if (file->f_mode & FMODE_READ) {
338 spin_lock_irqsave(&wiinst->lock, flags);
340 if (wiinst->state & WAVE_STATE_OPEN) {
341 emu10k1_wavein_close(wave_dev);
345 wiinst->total_recorded = 0;
347 spin_unlock_irqrestore(&wiinst->lock, flags);
352 case SNDCTL_DSP_SETDUPLEX:
353 DPF(2, "SNDCTL_DSP_SETDUPLEX:\n");
356 case SNDCTL_DSP_GETCAPS:
357 DPF(2, "SNDCTL_DSP_GETCAPS:\n");
358 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
359 DSP_CAP_TRIGGER | DSP_CAP_MMAP |
360 DSP_CAP_COPROC| DSP_CAP_MULTI, p);
361 case SNDCTL_DSP_SPEED:
362 DPF(2, "SNDCTL_DSP_SPEED:\n");
364 if (get_user(val, p))
367 DPD(2, "val is %d\n", val);
370 if (file->f_mode & FMODE_READ) {
371 struct wave_format format;
373 spin_lock_irqsave(&wiinst->lock, flags);
375 format = wiinst->format;
376 format.samplingrate = val;
378 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
379 spin_unlock_irqrestore(&wiinst->lock, flags);
383 val = wiinst->format.samplingrate;
385 spin_unlock_irqrestore(&wiinst->lock, flags);
387 DPD(2, "set recording sampling rate -> %d\n", val);
390 if (file->f_mode & FMODE_WRITE) {
391 struct wave_format format;
393 spin_lock_irqsave(&woinst->lock, flags);
395 format = woinst->format;
396 format.samplingrate = val;
398 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
399 spin_unlock_irqrestore(&woinst->lock, flags);
403 val = woinst->format.samplingrate;
405 spin_unlock_irqrestore(&woinst->lock, flags);
407 DPD(2, "set playback sampling rate -> %d\n", val);
410 return put_user(val, p);
412 if (file->f_mode & FMODE_READ)
413 val = wiinst->format.samplingrate;
414 else if (file->f_mode & FMODE_WRITE)
415 val = woinst->format.samplingrate;
417 return put_user(val, p);
421 case SNDCTL_DSP_STEREO:
422 DPF(2, "SNDCTL_DSP_STEREO:\n");
424 if (get_user(val, p))
427 DPD(2, " val is %d\n", val);
429 if (file->f_mode & FMODE_READ) {
430 struct wave_format format;
432 spin_lock_irqsave(&wiinst->lock, flags);
434 format = wiinst->format;
435 format.channels = val ? 2 : 1;
437 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
438 spin_unlock_irqrestore(&wiinst->lock, flags);
442 val = wiinst->format.channels - 1;
444 spin_unlock_irqrestore(&wiinst->lock, flags);
445 DPD(2, "set recording stereo -> %d\n", val);
448 if (file->f_mode & FMODE_WRITE) {
449 struct wave_format format;
451 spin_lock_irqsave(&woinst->lock, flags);
453 format = woinst->format;
454 format.channels = val ? 2 : 1;
456 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
457 spin_unlock_irqrestore(&woinst->lock, flags);
461 val = woinst->format.channels - 1;
463 spin_unlock_irqrestore(&woinst->lock, flags);
465 DPD(2, "set playback stereo -> %d\n", val);
468 return put_user(val, p);
472 case SNDCTL_DSP_CHANNELS:
473 DPF(2, "SNDCTL_DSP_CHANNELS:\n");
475 if (get_user(val, p))
478 DPD(2, " val is %d\n", val);
481 if (file->f_mode & FMODE_READ) {
482 struct wave_format format;
484 spin_lock_irqsave(&wiinst->lock, flags);
486 format = wiinst->format;
487 format.channels = val;
489 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
490 spin_unlock_irqrestore(&wiinst->lock, flags);
493 val = wiinst->format.channels;
495 spin_unlock_irqrestore(&wiinst->lock, flags);
496 DPD(2, "set recording number of channels -> %d\n", val);
499 if (file->f_mode & FMODE_WRITE) {
500 struct wave_format format;
502 spin_lock_irqsave(&woinst->lock, flags);
504 format = woinst->format;
505 format.channels = val;
507 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
508 spin_unlock_irqrestore(&woinst->lock, flags);
512 val = woinst->format.channels;
514 spin_unlock_irqrestore(&woinst->lock, flags);
515 DPD(2, "set playback number of channels -> %d\n", val);
518 return put_user(val, p);
520 if (file->f_mode & FMODE_READ)
521 val = wiinst->format.channels;
522 else if (file->f_mode & FMODE_WRITE)
523 val = woinst->format.channels;
525 return put_user(val, p);
529 case SNDCTL_DSP_GETFMTS:
530 DPF(2, "SNDCTL_DSP_GETFMTS:\n");
532 if (file->f_mode & FMODE_READ)
534 else if (file->f_mode & FMODE_WRITE) {
535 val = AFMT_S16_LE | AFMT_U8;
536 if (emu10k1_find_control_gpr(&wave_dev->card->mgr,
537 wave_dev->card->pt.patch_name,
538 wave_dev->card->pt.enable_gpr_name) >= 0)
541 return put_user(val, p);
543 case SNDCTL_DSP_SETFMT: /* Same as SNDCTL_DSP_SAMPLESIZE */
544 DPF(2, "SNDCTL_DSP_SETFMT:\n");
546 if (get_user(val, p))
549 DPD(2, " val is %d\n", val);
551 if (val != AFMT_QUERY) {
552 if (file->f_mode & FMODE_READ) {
553 struct wave_format format;
555 spin_lock_irqsave(&wiinst->lock, flags);
557 format = wiinst->format;
560 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
561 spin_unlock_irqrestore(&wiinst->lock, flags);
565 val = wiinst->format.id;
567 spin_unlock_irqrestore(&wiinst->lock, flags);
568 DPD(2, "set recording format -> %d\n", val);
571 if (file->f_mode & FMODE_WRITE) {
572 struct wave_format format;
574 spin_lock_irqsave(&woinst->lock, flags);
576 format = woinst->format;
579 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
580 spin_unlock_irqrestore(&woinst->lock, flags);
584 val = woinst->format.id;
586 spin_unlock_irqrestore(&woinst->lock, flags);
587 DPD(2, "set playback format -> %d\n", val);
590 return put_user(val, p);
592 if (file->f_mode & FMODE_READ)
593 val = wiinst->format.id;
594 else if (file->f_mode & FMODE_WRITE)
595 val = woinst->format.id;
597 return put_user(val, p);
601 case SOUND_PCM_READ_BITS:
603 if (file->f_mode & FMODE_READ)
604 val = wiinst->format.bitsperchannel;
605 else if (file->f_mode & FMODE_WRITE)
606 val = woinst->format.bitsperchannel;
608 return put_user(val, p);
610 case SOUND_PCM_READ_RATE:
612 if (file->f_mode & FMODE_READ)
613 val = wiinst->format.samplingrate;
614 else if (file->f_mode & FMODE_WRITE)
615 val = woinst->format.samplingrate;
617 return put_user(val, p);
619 case SOUND_PCM_READ_CHANNELS:
621 if (file->f_mode & FMODE_READ)
622 val = wiinst->format.channels;
623 else if (file->f_mode & FMODE_WRITE)
624 val = woinst->format.channels;
626 return put_user(val, p);
628 case SOUND_PCM_WRITE_FILTER:
629 DPF(2, "SOUND_PCM_WRITE_FILTER: not implemented\n");
632 case SOUND_PCM_READ_FILTER:
633 DPF(2, "SOUND_PCM_READ_FILTER: not implemented\n");
636 case SNDCTL_DSP_SETSYNCRO:
637 DPF(2, "SNDCTL_DSP_SETSYNCRO: not implemented\n");
640 case SNDCTL_DSP_GETTRIGGER:
641 DPF(2, "SNDCTL_DSP_GETTRIGGER:\n");
643 if (file->f_mode & FMODE_WRITE && (wave_dev->enablebits & PCM_ENABLE_OUTPUT))
644 val |= PCM_ENABLE_OUTPUT;
646 if (file->f_mode & FMODE_READ && (wave_dev->enablebits & PCM_ENABLE_INPUT))
647 val |= PCM_ENABLE_INPUT;
649 return put_user(val, p);
651 case SNDCTL_DSP_SETTRIGGER:
652 DPF(2, "SNDCTL_DSP_SETTRIGGER:\n");
654 if (get_user(val, p))
657 if (file->f_mode & FMODE_WRITE) {
658 spin_lock_irqsave(&woinst->lock, flags);
660 if (val & PCM_ENABLE_OUTPUT) {
661 wave_dev->enablebits |= PCM_ENABLE_OUTPUT;
662 if (woinst->state & WAVE_STATE_OPEN)
663 emu10k1_waveout_start(wave_dev);
665 wave_dev->enablebits &= ~PCM_ENABLE_OUTPUT;
666 if (woinst->state & WAVE_STATE_STARTED)
667 emu10k1_waveout_stop(wave_dev);
670 spin_unlock_irqrestore(&woinst->lock, flags);
673 if (file->f_mode & FMODE_READ) {
674 spin_lock_irqsave(&wiinst->lock, flags);
676 if (val & PCM_ENABLE_INPUT) {
677 wave_dev->enablebits |= PCM_ENABLE_INPUT;
678 if (wiinst->state & WAVE_STATE_OPEN)
679 emu10k1_wavein_start(wave_dev);
681 wave_dev->enablebits &= ~PCM_ENABLE_INPUT;
682 if (wiinst->state & WAVE_STATE_STARTED)
683 emu10k1_wavein_stop(wave_dev);
686 spin_unlock_irqrestore(&wiinst->lock, flags);
690 case SNDCTL_DSP_GETOSPACE:
694 DPF(4, "SNDCTL_DSP_GETOSPACE:\n");
696 if (!(file->f_mode & FMODE_WRITE))
699 spin_lock_irqsave(&woinst->lock, flags);
701 if (woinst->state & WAVE_STATE_OPEN) {
702 emu10k1_waveout_update(woinst);
703 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
704 info.bytes = bytestocopy;
706 calculate_ofrag(woinst);
707 info.bytes = woinst->buffer.size;
709 spin_unlock_irqrestore(&woinst->lock, flags);
711 info.bytes *= woinst->num_voices;
712 info.fragsize = woinst->buffer.fragment_size * woinst->num_voices;
713 info.fragstotal = woinst->buffer.numfrags * woinst->num_voices;
714 info.fragments = info.bytes / info.fragsize;
716 if (copy_to_user(p, &info, sizeof(info)))
721 case SNDCTL_DSP_GETISPACE:
725 DPF(4, "SNDCTL_DSP_GETISPACE:\n");
727 if (!(file->f_mode & FMODE_READ))
730 spin_lock_irqsave(&wiinst->lock, flags);
731 if (wiinst->state & WAVE_STATE_OPEN) {
732 emu10k1_wavein_update(wave_dev->card, wiinst);
733 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
734 info.bytes = bytestocopy;
736 calculate_ifrag(wiinst);
739 spin_unlock_irqrestore(&wiinst->lock, flags);
741 info.fragstotal = wiinst->buffer.numfrags;
742 info.fragments = info.bytes / wiinst->buffer.fragment_size;
743 info.fragsize = wiinst->buffer.fragment_size;
745 if (copy_to_user(p, &info, sizeof(info)))
750 case SNDCTL_DSP_NONBLOCK:
751 DPF(2, "SNDCTL_DSP_NONBLOCK:\n");
753 file->f_flags |= O_NONBLOCK;
756 case SNDCTL_DSP_GETODELAY:
757 DPF(4, "SNDCTL_DSP_GETODELAY:\n");
759 if (!(file->f_mode & FMODE_WRITE))
762 spin_lock_irqsave(&woinst->lock, flags);
763 if (woinst->state & WAVE_STATE_OPEN) {
764 emu10k1_waveout_update(woinst);
765 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
766 val = woinst->buffer.size - bytestocopy;
770 val *= woinst->num_voices;
771 spin_unlock_irqrestore(&woinst->lock, flags);
773 return put_user(val, p);
775 case SNDCTL_DSP_GETIPTR:
779 DPF(4, "SNDCTL_DSP_GETIPTR: \n");
781 if (!(file->f_mode & FMODE_READ))
784 spin_lock_irqsave(&wiinst->lock, flags);
786 if (wiinst->state & WAVE_STATE_OPEN) {
787 emu10k1_wavein_update(wave_dev->card, wiinst);
788 cinfo.ptr = wiinst->buffer.hw_pos;
789 cinfo.bytes = cinfo.ptr + wiinst->total_recorded - wiinst->total_recorded % wiinst->buffer.size;
790 cinfo.blocks = cinfo.bytes / wiinst->buffer.fragment_size - wiinst->blocks;
791 wiinst->blocks = cinfo.bytes / wiinst->buffer.fragment_size;
799 wiinst->buffer.bytestocopy %= wiinst->buffer.fragment_size;
801 spin_unlock_irqrestore(&wiinst->lock, flags);
803 if (copy_to_user(p, &cinfo, sizeof(cinfo)))
808 case SNDCTL_DSP_GETOPTR:
812 DPF(4, "SNDCTL_DSP_GETOPTR:\n");
814 if (!(file->f_mode & FMODE_WRITE))
817 spin_lock_irqsave(&woinst->lock, flags);
819 if (woinst->state & WAVE_STATE_OPEN ||
820 ((woinst->format.passthrough == 1) && wave_dev->card->pt.state)) {
823 if (woinst->format.passthrough == 1) {
824 emu10k1_pt_waveout_update(wave_dev);
825 cinfo.bytes = woinst->total_played;
827 emu10k1_waveout_update(woinst);
828 cinfo.bytes = woinst->total_played;
831 cinfo.ptr = woinst->buffer.hw_pos;
832 num_fragments = cinfo.bytes / woinst->buffer.fragment_size;
833 cinfo.blocks = num_fragments - woinst->blocks;
834 woinst->blocks = num_fragments;
836 cinfo.bytes *= woinst->num_voices;
837 cinfo.ptr *= woinst->num_voices;
845 woinst->buffer.free_bytes %= woinst->buffer.fragment_size;
847 spin_unlock_irqrestore(&woinst->lock, flags);
849 if (copy_to_user(p, &cinfo, sizeof(cinfo)))
854 case SNDCTL_DSP_GETBLKSIZE:
855 DPF(2, "SNDCTL_DSP_GETBLKSIZE:\n");
857 if (file->f_mode & FMODE_WRITE) {
858 spin_lock_irqsave(&woinst->lock, flags);
860 calculate_ofrag(woinst);
861 val = woinst->buffer.fragment_size * woinst->num_voices;
863 spin_unlock_irqrestore(&woinst->lock, flags);
866 if (file->f_mode & FMODE_READ) {
867 spin_lock_irqsave(&wiinst->lock, flags);
869 calculate_ifrag(wiinst);
870 val = wiinst->buffer.fragment_size;
872 spin_unlock_irqrestore(&wiinst->lock, flags);
875 return put_user(val, p);
879 case SNDCTL_DSP_POST:
880 if (file->f_mode & FMODE_WRITE) {
881 spin_lock_irqsave(&woinst->lock, flags);
883 if (!(woinst->state & WAVE_STATE_STARTED)
884 && (wave_dev->enablebits & PCM_ENABLE_OUTPUT)
885 && (woinst->total_copied > 0))
886 emu10k1_waveout_start(wave_dev);
888 spin_unlock_irqrestore(&woinst->lock, flags);
893 case SNDCTL_DSP_SUBDIVIDE:
894 DPF(2, "SNDCTL_DSP_SUBDIVIDE: not implemented\n");
897 case SNDCTL_DSP_SETFRAGMENT:
898 DPF(2, "SNDCTL_DSP_SETFRAGMENT:\n");
900 if (get_user(val, p))
903 DPD(2, "val is %#x\n", val);
908 if (file->f_mode & FMODE_WRITE) {
909 /* digital pass-through fragment count and size are fixed values */
910 if (woinst->state & WAVE_STATE_OPEN || (woinst->format.passthrough == 1))
911 return -EINVAL; /* too late to change */
913 woinst->buffer.ossfragshift = val & 0xffff;
914 woinst->buffer.numfrags = (val >> 16) & 0xffff;
917 if (file->f_mode & FMODE_READ) {
918 if (wiinst->state & WAVE_STATE_OPEN)
919 return -EINVAL; /* too late to change */
921 wiinst->buffer.ossfragshift = val & 0xffff;
922 wiinst->buffer.numfrags = (val >> 16) & 0xffff;
927 case SNDCTL_COPR_LOAD:
932 DPF(4, "SNDCTL_COPR_LOAD:\n");
934 buf = kmalloc(sizeof(copr_buffer), GFP_KERNEL);
938 if (copy_from_user(buf, p, sizeof(copr_buffer))) {
943 if ((buf->command != CMD_READ) && (buf->command != CMD_WRITE)) {
948 if (buf->command == CMD_WRITE) {
951 if ((buf->offs < 0) || (buf->offs + buf->len > 0xe00) || (buf->len > 1000)) {
953 if (((buf->offs < 0x100) || (buf->offs + buf->len > (wave_dev->card->is_audigy ? 0xe00 : 0x800)) || (buf->len > 1000)
955 //any register allowed raw access to users goes here:
958 && (buf->len == 1))) {
964 if ((buf->offs < 0) || (buf->offs + buf->len > 0xe00) || (buf->len > 1000)) {
970 if (((unsigned)buf->flags) > 0x3f)
973 if (buf->command == CMD_READ) {
974 for (i = 0; i < buf->len; i++)
975 ((u32 *) buf->data)[i] = sblive_readptr(wave_dev->card, buf->offs + i, buf->flags);
977 if (copy_to_user(p, buf, sizeof(copr_buffer))) {
982 for (i = 0; i < buf->len; i++)
983 sblive_writeptr(wave_dev->card, buf->offs + i, buf->flags, ((u32 *) buf->data)[i]);
990 default: /* Default is unrecognized command */
991 DPD(2, "default: %#x\n", cmd);
997 static struct page *emu10k1_mm_nopage (struct vm_area_struct * vma, unsigned long address, int *type)
999 struct emu10k1_wavedevice *wave_dev = vma->vm_private_data;
1000 struct woinst *woinst = wave_dev->woinst;
1001 struct wiinst *wiinst = wave_dev->wiinst;
1002 struct page *dmapage;
1003 unsigned long pgoff;
1006 DPF(3, "emu10k1_mm_nopage()\n");
1007 DPD(3, "addr: %#lx\n", address);
1009 if (address > vma->vm_end) {
1010 DPF(1, "EXIT, returning NOPAGE_SIGBUS\n");
1011 return NOPAGE_SIGBUS; /* Disallow mremap */
1014 pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
1016 wr = woinst->mmapped;
1021 rd = wiinst->mmapped;
1025 /* if full-duplex (read+write) and we have two sets of bufs,
1026 * then the playback buffers come first, sez soundcard.c */
1028 if (pgoff >= woinst->buffer.pages) {
1029 pgoff -= woinst->buffer.pages;
1030 dmapage = virt_to_page ((u8 *) wiinst->buffer.addr + pgoff * PAGE_SIZE);
1032 dmapage = virt_to_page (woinst->voice[0].mem.addr[pgoff]);
1034 dmapage = virt_to_page ((u8 *) wiinst->buffer.addr + pgoff * PAGE_SIZE);
1039 DPD(3, "page: %#lx\n", (unsigned long) dmapage);
1041 *type = VM_FAULT_MINOR;
1045 static struct vm_operations_struct emu10k1_mm_ops = {
1046 .nopage = emu10k1_mm_nopage,
1049 static int emu10k1_audio_mmap(struct file *file, struct vm_area_struct *vma)
1051 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1052 unsigned long max_pages, n_pages, pgoffset;
1053 struct woinst *woinst = NULL;
1054 struct wiinst *wiinst = NULL;
1055 unsigned long flags;
1057 DPF(2, "emu10k1_audio_mmap()\n");
1060 if (vma->vm_flags & VM_WRITE) {
1061 woinst = wave_dev->woinst;
1063 spin_lock_irqsave(&woinst->lock, flags);
1065 /* No m'mapping possible for multichannel */
1066 if (woinst->num_voices > 1) {
1067 spin_unlock_irqrestore(&woinst->lock, flags);
1071 if (woinst->state == WAVE_STATE_CLOSED) {
1072 calculate_ofrag(woinst);
1074 if (emu10k1_waveout_open(wave_dev) < 0) {
1075 spin_unlock_irqrestore(&woinst->lock, flags);
1081 woinst->mmapped = 1;
1082 max_pages += woinst->buffer.pages;
1083 spin_unlock_irqrestore(&woinst->lock, flags);
1086 if (vma->vm_flags & VM_READ) {
1087 wiinst = wave_dev->wiinst;
1089 spin_lock_irqsave(&wiinst->lock, flags);
1090 if (wiinst->state == WAVE_STATE_CLOSED) {
1091 calculate_ifrag(wiinst);
1093 if (emu10k1_wavein_open(wave_dev) < 0) {
1094 spin_unlock_irqrestore(&wiinst->lock, flags);
1100 wiinst->mmapped = 1;
1101 max_pages += wiinst->buffer.pages;
1102 spin_unlock_irqrestore(&wiinst->lock, flags);
1105 n_pages = ((vma->vm_end - vma->vm_start) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1106 pgoffset = vma->vm_pgoff;
1108 DPD(2, "vma_start: %#lx, vma_end: %#lx, vma_offset: %ld\n", vma->vm_start, vma->vm_end, pgoffset);
1109 DPD(2, "n_pages: %ld, max_pages: %ld\n", n_pages, max_pages);
1111 if (pgoffset + n_pages > max_pages)
1114 vma->vm_flags |= VM_RESERVED;
1115 vma->vm_ops = &emu10k1_mm_ops;
1116 vma->vm_private_data = wave_dev;
1120 static int emu10k1_audio_open(struct inode *inode, struct file *file)
1122 int minor = iminor(inode);
1123 struct emu10k1_card *card = NULL;
1124 struct list_head *entry;
1125 struct emu10k1_wavedevice *wave_dev;
1127 DPF(2, "emu10k1_audio_open()\n");
1129 /* Check for correct device to open */
1131 list_for_each(entry, &emu10k1_devs) {
1132 card = list_entry(entry, struct emu10k1_card, list);
1134 if (!((card->audio_dev ^ minor) & ~0xf) || !((card->audio_dev1 ^ minor) & ~0xf))
1142 wave_dev = kmalloc(sizeof(struct emu10k1_wavedevice), GFP_KERNEL);
1144 if (wave_dev == NULL) {
1149 wave_dev->card = card;
1150 wave_dev->wiinst = NULL;
1151 wave_dev->woinst = NULL;
1152 wave_dev->enablebits = PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT; /* Default */
1154 if (file->f_mode & FMODE_READ) {
1156 struct wiinst *wiinst;
1158 if ((wiinst = kmalloc(sizeof(struct wiinst), GFP_KERNEL)) == NULL) {
1164 wiinst->recsrc = card->wavein.recsrc;
1165 wiinst->fxwc = card->wavein.fxwc;
1167 switch (wiinst->recsrc) {
1168 case WAVERECORD_AC97:
1169 wiinst->format.id = AFMT_S16_LE;
1170 wiinst->format.samplingrate = 8000;
1171 wiinst->format.bitsperchannel = 16;
1172 wiinst->format.channels = 1;
1174 case WAVERECORD_MIC:
1175 wiinst->format.id = AFMT_S16_LE;
1176 wiinst->format.samplingrate = 8000;
1177 wiinst->format.bitsperchannel = 16;
1178 wiinst->format.channels = 1;
1181 wiinst->format.id = AFMT_S16_LE;
1182 wiinst->format.samplingrate = 48000;
1183 wiinst->format.bitsperchannel = 16;
1184 wiinst->format.channels = hweight32(wiinst->fxwc);
1193 wiinst->state = WAVE_STATE_CLOSED;
1195 wiinst->buffer.ossfragshift = 0;
1196 wiinst->buffer.fragment_size = 0;
1197 wiinst->buffer.numfrags = 0;
1199 init_waitqueue_head(&wiinst->wait_queue);
1201 wiinst->mmapped = 0;
1202 wiinst->total_recorded = 0;
1204 spin_lock_init(&wiinst->lock);
1205 tasklet_init(&wiinst->timer.tasklet, emu10k1_wavein_bh, (unsigned long) wave_dev);
1206 wave_dev->wiinst = wiinst;
1207 emu10k1_wavein_setformat(wave_dev, &wiinst->format);
1210 if (file->f_mode & FMODE_WRITE) {
1211 struct woinst *woinst;
1214 if ((woinst = kmalloc(sizeof(struct woinst), GFP_KERNEL)) == NULL) {
1220 if (wave_dev->wiinst != NULL) {
1221 woinst->format = wave_dev->wiinst->format;
1223 woinst->format.id = AFMT_U8;
1224 woinst->format.samplingrate = 8000;
1225 woinst->format.bitsperchannel = 8;
1226 woinst->format.channels = 1;
1229 woinst->state = WAVE_STATE_CLOSED;
1231 woinst->buffer.fragment_size = 0;
1232 woinst->buffer.ossfragshift = 0;
1233 woinst->buffer.numfrags = 0;
1234 woinst->device = (card->audio_dev1 == minor);
1235 woinst->timer.state = TIMER_STATE_UNINSTALLED;
1236 woinst->num_voices = 1;
1237 for (i = 0; i < WAVEOUT_MAXVOICES; i++) {
1238 woinst->voice[i].usage = VOICE_USAGE_FREE;
1239 woinst->voice[i].mem.emupageindex = -1;
1242 init_waitqueue_head(&woinst->wait_queue);
1244 woinst->mmapped = 0;
1245 woinst->total_copied = 0;
1246 woinst->total_played = 0;
1248 spin_lock_init(&woinst->lock);
1249 tasklet_init(&woinst->timer.tasklet, emu10k1_waveout_bh, (unsigned long) wave_dev);
1250 wave_dev->woinst = woinst;
1251 emu10k1_waveout_setformat(wave_dev, &woinst->format);
1254 file->private_data = (void *) wave_dev;
1256 return nonseekable_open(inode, file);
1259 static int emu10k1_audio_release(struct inode *inode, struct file *file)
1261 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1262 struct emu10k1_card *card;
1263 unsigned long flags;
1265 card = wave_dev->card;
1267 DPF(2, "emu10k1_audio_release()\n");
1269 if (file->f_mode & FMODE_WRITE) {
1270 struct woinst *woinst = wave_dev->woinst;
1272 spin_lock_irqsave(&woinst->lock, flags);
1273 if(woinst->format.passthrough==2)
1274 card->pt.state=PT_STATE_PLAYING;
1275 if (woinst->format.passthrough && card->pt.state != PT_STATE_INACTIVE){
1276 spin_lock(&card->pt.lock);
1277 emu10k1_pt_stop(card);
1278 spin_unlock(&card->pt.lock);
1280 if (woinst->state & WAVE_STATE_OPEN) {
1281 if (woinst->state & WAVE_STATE_STARTED) {
1282 if (!(file->f_flags & O_NONBLOCK)) {
1283 while (!signal_pending(current)
1284 && (woinst->total_played < woinst->total_copied)) {
1285 DPF(4, "Buffer hasn't been totally played, sleep....\n");
1286 spin_unlock_irqrestore(&woinst->lock, flags);
1287 interruptible_sleep_on(&woinst->wait_queue);
1288 spin_lock_irqsave(&woinst->lock, flags);
1292 emu10k1_waveout_close(wave_dev);
1295 spin_unlock_irqrestore(&woinst->lock, flags);
1296 /* remove the tasklet */
1297 tasklet_kill(&woinst->timer.tasklet);
1298 kfree(wave_dev->woinst);
1301 if (file->f_mode & FMODE_READ) {
1302 struct wiinst *wiinst = wave_dev->wiinst;
1304 spin_lock_irqsave(&wiinst->lock, flags);
1306 if (wiinst->state & WAVE_STATE_OPEN) {
1307 emu10k1_wavein_close(wave_dev);
1310 spin_unlock_irqrestore(&wiinst->lock, flags);
1311 tasklet_kill(&wiinst->timer.tasklet);
1312 kfree(wave_dev->wiinst);
1317 if (waitqueue_active(&card->open_wait))
1318 wake_up_interruptible(&card->open_wait);
1323 /* FIXME sort out poll() + mmap() */
1324 static unsigned int emu10k1_audio_poll(struct file *file, struct poll_table_struct *wait)
1326 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1327 struct woinst *woinst = wave_dev->woinst;
1328 struct wiinst *wiinst = wave_dev->wiinst;
1329 unsigned int mask = 0;
1331 unsigned long flags;
1333 DPF(4, "emu10k1_audio_poll()\n");
1335 if (file->f_mode & FMODE_WRITE)
1336 poll_wait(file, &woinst->wait_queue, wait);
1338 if (file->f_mode & FMODE_READ)
1339 poll_wait(file, &wiinst->wait_queue, wait);
1341 if (file->f_mode & FMODE_WRITE) {
1342 spin_lock_irqsave(&woinst->lock, flags);
1344 if (woinst->state & WAVE_STATE_OPEN) {
1345 emu10k1_waveout_update(woinst);
1346 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
1348 if (bytestocopy >= woinst->buffer.fragment_size)
1349 mask |= POLLOUT | POLLWRNORM;
1351 mask |= POLLOUT | POLLWRNORM;
1353 spin_unlock_irqrestore(&woinst->lock, flags);
1356 if (file->f_mode & FMODE_READ) {
1357 spin_lock_irqsave(&wiinst->lock, flags);
1359 if (wiinst->state & WAVE_STATE_OPEN) {
1360 emu10k1_wavein_update(wave_dev->card, wiinst);
1361 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
1363 if (bytestocopy >= wiinst->buffer.fragment_size)
1364 mask |= POLLIN | POLLRDNORM;
1367 spin_unlock_irqrestore(&wiinst->lock, flags);
1373 static void calculate_ofrag(struct woinst *woinst)
1375 struct waveout_buffer *buffer = &woinst->buffer;
1378 if (buffer->fragment_size)
1381 if (!buffer->ossfragshift) {
1382 fragsize = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTFRAGLEN) / 1000 - 1;
1386 buffer->ossfragshift++;
1390 if (buffer->ossfragshift < WAVEOUT_MINFRAGSHIFT)
1391 buffer->ossfragshift = WAVEOUT_MINFRAGSHIFT;
1393 buffer->fragment_size = 1 << buffer->ossfragshift;
1395 while (buffer->fragment_size * WAVEOUT_MINFRAGS > WAVEOUT_MAXBUFSIZE)
1396 buffer->fragment_size >>= 1;
1398 /* now we are sure that:
1399 (2^WAVEOUT_MINFRAGSHIFT) <= (fragment_size = 2^n) <= (WAVEOUT_MAXBUFSIZE / WAVEOUT_MINFRAGS)
1402 if (!buffer->numfrags) {
1405 numfrags = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTBUFLEN) /
1406 (buffer->fragment_size * 1000) - 1;
1408 buffer->numfrags = 1;
1412 buffer->numfrags <<= 1;
1416 if (buffer->numfrags < WAVEOUT_MINFRAGS)
1417 buffer->numfrags = WAVEOUT_MINFRAGS;
1419 if (buffer->numfrags * buffer->fragment_size > WAVEOUT_MAXBUFSIZE)
1420 buffer->numfrags = WAVEOUT_MAXBUFSIZE / buffer->fragment_size;
1422 if (buffer->numfrags < WAVEOUT_MINFRAGS)
1425 buffer->size = buffer->fragment_size * buffer->numfrags;
1426 buffer->pages = buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
1428 DPD(2, " calculated playback fragment_size -> %d\n", buffer->fragment_size);
1429 DPD(2, " calculated playback numfrags -> %d\n", buffer->numfrags);
1434 static void calculate_ifrag(struct wiinst *wiinst)
1436 struct wavein_buffer *buffer = &wiinst->buffer;
1437 u32 fragsize, bufsize, size[4];
1440 if (buffer->fragment_size)
1443 if (!buffer->ossfragshift) {
1444 fragsize = (wiinst->format.bytespersec * WAVEIN_DEFAULTFRAGLEN) / 1000 - 1;
1448 buffer->ossfragshift++;
1452 if (buffer->ossfragshift < WAVEIN_MINFRAGSHIFT)
1453 buffer->ossfragshift = WAVEIN_MINFRAGSHIFT;
1455 buffer->fragment_size = 1 << buffer->ossfragshift;
1457 while (buffer->fragment_size * WAVEIN_MINFRAGS > WAVEIN_MAXBUFSIZE)
1458 buffer->fragment_size >>= 1;
1460 /* now we are sure that:
1461 (2^WAVEIN_MINFRAGSHIFT) <= (fragment_size = 2^n) <= (WAVEIN_MAXBUFSIZE / WAVEIN_MINFRAGS)
1465 if (!buffer->numfrags)
1466 buffer->numfrags = (wiinst->format.bytespersec * WAVEIN_DEFAULTBUFLEN) / (buffer->fragment_size * 1000) - 1;
1468 if (buffer->numfrags < WAVEIN_MINFRAGS)
1469 buffer->numfrags = WAVEIN_MINFRAGS;
1471 if (buffer->numfrags * buffer->fragment_size > WAVEIN_MAXBUFSIZE)
1472 buffer->numfrags = WAVEIN_MAXBUFSIZE / buffer->fragment_size;
1474 if (buffer->numfrags < WAVEIN_MINFRAGS)
1477 bufsize = buffer->fragment_size * buffer->numfrags;
1479 /* the buffer size for recording is restricted to certain values, adjust it now */
1480 if (bufsize >= 0x10000) {
1481 buffer->size = 0x10000;
1482 buffer->sizeregval = 0x1f;
1490 for (i = 0; i < 8; i++)
1491 for (j = 0; j < 4; j++)
1492 if (bufsize >= size[j]) {
1493 buffer->size = size[j];
1495 buffer->sizeregval = i * 4 + j + 1;
1499 if (buffer->size == 0) {
1501 buffer->sizeregval = 0x01;
1505 /* adjust the fragment size so that buffer size is an integer multiple */
1506 while (buffer->size % buffer->fragment_size)
1507 buffer->fragment_size >>= 1;
1509 buffer->numfrags = buffer->size / buffer->fragment_size;
1510 buffer->pages = buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
1512 DPD(2, " calculated recording fragment_size -> %d\n", buffer->fragment_size);
1513 DPD(2, " calculated recording numfrags -> %d\n", buffer->numfrags);
1514 DPD(2, " buffer size register -> %#04x\n", buffer->sizeregval);
1519 static void emu10k1_wavein_bh(unsigned long refdata)
1521 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) refdata;
1522 struct wiinst *wiinst = wave_dev->wiinst;
1524 unsigned long flags;
1529 spin_lock_irqsave(&wiinst->lock, flags);
1531 if (!(wiinst->state & WAVE_STATE_STARTED)) {
1532 spin_unlock_irqrestore(&wiinst->lock, flags);
1536 emu10k1_wavein_update(wave_dev->card, wiinst);
1537 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
1539 spin_unlock_irqrestore(&wiinst->lock, flags);
1541 if (bytestocopy >= wiinst->buffer.fragment_size) {
1542 if (waitqueue_active(&wiinst->wait_queue))
1543 wake_up_interruptible(&wiinst->wait_queue);
1545 DPD(3, "Not enough transfer size, %d\n", bytestocopy);
1550 static void emu10k1_waveout_bh(unsigned long refdata)
1552 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) refdata;
1553 struct woinst *woinst = wave_dev->woinst;
1555 unsigned long flags;
1560 spin_lock_irqsave(&woinst->lock, flags);
1562 if (!(woinst->state & WAVE_STATE_STARTED)) {
1563 spin_unlock_irqrestore(&woinst->lock, flags);
1567 emu10k1_waveout_update(woinst);
1568 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
1570 if (woinst->buffer.fill_silence) {
1571 spin_unlock_irqrestore(&woinst->lock, flags);
1572 emu10k1_waveout_fillsilence(woinst);
1574 spin_unlock_irqrestore(&woinst->lock, flags);
1576 if (bytestocopy >= woinst->buffer.fragment_size) {
1577 if (waitqueue_active(&woinst->wait_queue))
1578 wake_up_interruptible(&woinst->wait_queue);
1580 DPD(3, "Not enough transfer size -> %d\n", bytestocopy);
1585 const struct file_operations emu10k1_audio_fops = {
1586 .owner = THIS_MODULE,
1587 .llseek = no_llseek,
1588 .read = emu10k1_audio_read,
1589 .write = emu10k1_audio_write,
1590 .poll = emu10k1_audio_poll,
1591 .ioctl = emu10k1_audio_ioctl,
1592 .mmap = emu10k1_audio_mmap,
1593 .open = emu10k1_audio_open,
1594 .release = emu10k1_audio_release,