[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / sound / oss / emu10k1 / audio.c
blobcde4d59d5430664e9d48a85ffa9961f744a9c6e8
1 /*
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,
28 * USA.
30 **********************************************************************
33 #include <linux/module.h>
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/bitops.h>
37 #include <asm/io.h>
38 #include <linux/sched.h>
39 #include <linux/smp_lock.h>
41 #include "hwaccess.h"
42 #include "cardwo.h"
43 #include "cardwi.h"
44 #include "recmgr.h"
45 #include "irqmgr.h"
46 #include "audio.h"
47 #include "8010.h"
49 static void calculate_ofrag(struct woinst *);
50 static void calculate_ifrag(struct wiinst *);
52 static void emu10k1_waveout_bh(unsigned long refdata);
53 static void emu10k1_wavein_bh(unsigned long refdata);
55 /* Audio file operations */
56 static ssize_t emu10k1_audio_read(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
58 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
59 struct wiinst *wiinst = wave_dev->wiinst;
60 ssize_t ret = 0;
61 unsigned long flags;
63 DPD(3, "emu10k1_audio_read(), buffer=%p, count=%d\n", buffer, (u32) count);
65 if (!access_ok(VERIFY_WRITE, buffer, count))
66 return -EFAULT;
68 spin_lock_irqsave(&wiinst->lock, flags);
70 if (wiinst->mmapped) {
71 spin_unlock_irqrestore(&wiinst->lock, flags);
72 return -ENXIO;
75 if (wiinst->state == WAVE_STATE_CLOSED) {
76 calculate_ifrag(wiinst);
78 while (emu10k1_wavein_open(wave_dev) < 0) {
79 spin_unlock_irqrestore(&wiinst->lock, flags);
81 if (file->f_flags & O_NONBLOCK)
82 return -EAGAIN;
84 interruptible_sleep_on(&wave_dev->card->open_wait);
86 if (signal_pending(current))
87 return -ERESTARTSYS;
89 spin_lock_irqsave(&wiinst->lock, flags);
93 spin_unlock_irqrestore(&wiinst->lock, flags);
95 while (count > 0) {
96 u32 bytestocopy;
98 spin_lock_irqsave(&wiinst->lock, flags);
100 if (!(wiinst->state & WAVE_STATE_STARTED)
101 && (wave_dev->enablebits & PCM_ENABLE_INPUT))
102 emu10k1_wavein_start(wave_dev);
104 emu10k1_wavein_update(wave_dev->card, wiinst);
105 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
107 spin_unlock_irqrestore(&wiinst->lock, flags);
109 DPD(3, "bytestocopy --> %d\n", bytestocopy);
111 if ((bytestocopy >= wiinst->buffer.fragment_size)
112 || (bytestocopy >= count)) {
113 bytestocopy = min_t(u32, bytestocopy, count);
115 emu10k1_wavein_xferdata(wiinst, (u8 __user *)buffer, &bytestocopy);
117 count -= bytestocopy;
118 buffer += bytestocopy;
119 ret += bytestocopy;
122 if (count > 0) {
123 if ((file->f_flags & O_NONBLOCK)
124 || (!(wave_dev->enablebits & PCM_ENABLE_INPUT)))
125 return (ret ? ret : -EAGAIN);
127 interruptible_sleep_on(&wiinst->wait_queue);
129 if (signal_pending(current))
130 return (ret ? ret : -ERESTARTSYS);
135 DPD(3, "bytes copied -> %d\n", (u32) ret);
137 return ret;
140 static ssize_t emu10k1_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t * ppos)
142 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
143 struct woinst *woinst = wave_dev->woinst;
144 ssize_t ret;
145 unsigned long flags;
147 DPD(3, "emu10k1_audio_write(), buffer=%p, count=%d\n", buffer, (u32) count);
149 if (!access_ok(VERIFY_READ, buffer, count))
150 return -EFAULT;
152 spin_lock_irqsave(&woinst->lock, flags);
154 if (woinst->mmapped) {
155 spin_unlock_irqrestore(&woinst->lock, flags);
156 return -ENXIO;
158 // This is for emu10k1 revs less than 7, we need to go through tram
159 if (woinst->format.passthrough == 1) {
160 int r;
162 woinst->buffer.ossfragshift = PT_BLOCKSIZE_LOG2;
163 woinst->buffer.numfrags = PT_BLOCKCOUNT;
164 calculate_ofrag(woinst);
166 r = emu10k1_pt_write(file, buffer, count);
167 spin_unlock_irqrestore(&woinst->lock, flags);
168 return r;
171 if (woinst->state == WAVE_STATE_CLOSED) {
172 calculate_ofrag(woinst);
174 while (emu10k1_waveout_open(wave_dev) < 0) {
175 spin_unlock_irqrestore(&woinst->lock, flags);
177 if (file->f_flags & O_NONBLOCK)
178 return -EAGAIN;
180 interruptible_sleep_on(&wave_dev->card->open_wait);
182 if (signal_pending(current))
183 return -ERESTARTSYS;
185 spin_lock_irqsave(&woinst->lock, flags);
189 spin_unlock_irqrestore(&woinst->lock, flags);
191 ret = 0;
192 if (count % woinst->format.bytespersample)
193 return -EINVAL;
195 count /= woinst->num_voices;
197 while (count > 0) {
198 u32 bytestocopy;
200 spin_lock_irqsave(&woinst->lock, flags);
201 emu10k1_waveout_update(woinst);
202 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
203 spin_unlock_irqrestore(&woinst->lock, flags);
205 DPD(3, "bytestocopy --> %d\n", bytestocopy);
207 if ((bytestocopy >= woinst->buffer.fragment_size)
208 || (bytestocopy >= count)) {
210 bytestocopy = min_t(u32, bytestocopy, count);
212 emu10k1_waveout_xferdata(woinst, (u8 __user *) buffer, &bytestocopy);
214 count -= bytestocopy;
215 buffer += bytestocopy * woinst->num_voices;
216 ret += bytestocopy * woinst->num_voices;
218 spin_lock_irqsave(&woinst->lock, flags);
219 woinst->total_copied += bytestocopy;
221 if (!(woinst->state & WAVE_STATE_STARTED)
222 && (wave_dev->enablebits & PCM_ENABLE_OUTPUT)
223 && (woinst->total_copied >= woinst->buffer.fragment_size))
224 emu10k1_waveout_start(wave_dev);
226 spin_unlock_irqrestore(&woinst->lock, flags);
229 if (count > 0) {
230 if ((file->f_flags & O_NONBLOCK)
231 || (!(wave_dev->enablebits & PCM_ENABLE_OUTPUT)))
232 return (ret ? ret : -EAGAIN);
234 interruptible_sleep_on(&woinst->wait_queue);
236 if (signal_pending(current))
237 return (ret ? ret : -ERESTARTSYS);
241 DPD(3, "bytes copied -> %d\n", (u32) ret);
243 return ret;
246 static int emu10k1_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
248 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
249 struct woinst *woinst = NULL;
250 struct wiinst *wiinst = NULL;
251 int val = 0;
252 u32 bytestocopy;
253 unsigned long flags;
254 int __user *p = (int __user *)arg;
256 DPF(4, "emu10k1_audio_ioctl()\n");
258 if (file->f_mode & FMODE_WRITE)
259 woinst = wave_dev->woinst;
261 if (file->f_mode & FMODE_READ)
262 wiinst = wave_dev->wiinst;
264 switch (cmd) {
265 case OSS_GETVERSION:
266 DPF(2, "OSS_GETVERSION:\n");
267 return put_user(SOUND_VERSION, p);
269 case SNDCTL_DSP_RESET:
270 DPF(2, "SNDCTL_DSP_RESET:\n");
271 wave_dev->enablebits = PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT;
273 if (file->f_mode & FMODE_WRITE) {
274 spin_lock_irqsave(&woinst->lock, flags);
276 if (woinst->state & WAVE_STATE_OPEN) {
277 emu10k1_waveout_close(wave_dev);
280 woinst->mmapped = 0;
281 woinst->total_copied = 0;
282 woinst->total_played = 0;
283 woinst->blocks = 0;
285 spin_unlock_irqrestore(&woinst->lock, flags);
288 if (file->f_mode & FMODE_READ) {
289 spin_lock_irqsave(&wiinst->lock, flags);
291 if (wiinst->state & WAVE_STATE_OPEN) {
292 emu10k1_wavein_close(wave_dev);
295 wiinst->mmapped = 0;
296 wiinst->total_recorded = 0;
297 wiinst->blocks = 0;
298 spin_unlock_irqrestore(&wiinst->lock, flags);
301 break;
303 case SNDCTL_DSP_SYNC:
304 DPF(2, "SNDCTL_DSP_SYNC:\n");
306 if (file->f_mode & FMODE_WRITE) {
308 spin_lock_irqsave(&woinst->lock, flags);
310 if (woinst->state & WAVE_STATE_OPEN) {
312 if (woinst->state & WAVE_STATE_STARTED)
313 while ((woinst->total_played < woinst->total_copied)
314 && !signal_pending(current)) {
315 spin_unlock_irqrestore(&woinst->lock, flags);
316 interruptible_sleep_on(&woinst->wait_queue);
317 spin_lock_irqsave(&woinst->lock, flags);
319 emu10k1_waveout_close(wave_dev);
322 woinst->mmapped = 0;
323 woinst->total_copied = 0;
324 woinst->total_played = 0;
325 woinst->blocks = 0;
327 spin_unlock_irqrestore(&woinst->lock, flags);
330 if (file->f_mode & FMODE_READ) {
331 spin_lock_irqsave(&wiinst->lock, flags);
333 if (wiinst->state & WAVE_STATE_OPEN) {
334 emu10k1_wavein_close(wave_dev);
337 wiinst->mmapped = 0;
338 wiinst->total_recorded = 0;
339 wiinst->blocks = 0;
340 spin_unlock_irqrestore(&wiinst->lock, flags);
343 break;
345 case SNDCTL_DSP_SETDUPLEX:
346 DPF(2, "SNDCTL_DSP_SETDUPLEX:\n");
347 break;
349 case SNDCTL_DSP_GETCAPS:
350 DPF(2, "SNDCTL_DSP_GETCAPS:\n");
351 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
352 DSP_CAP_TRIGGER | DSP_CAP_MMAP |
353 DSP_CAP_COPROC| DSP_CAP_MULTI, p);
354 case SNDCTL_DSP_SPEED:
355 DPF(2, "SNDCTL_DSP_SPEED:\n");
357 if (get_user(val, p))
358 return -EFAULT;
360 DPD(2, "val is %d\n", val);
362 if (val > 0) {
363 if (file->f_mode & FMODE_READ) {
364 struct wave_format format;
366 spin_lock_irqsave(&wiinst->lock, flags);
368 format = wiinst->format;
369 format.samplingrate = val;
371 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
372 spin_unlock_irqrestore(&wiinst->lock, flags);
373 return -EINVAL;
376 val = wiinst->format.samplingrate;
378 spin_unlock_irqrestore(&wiinst->lock, flags);
380 DPD(2, "set recording sampling rate -> %d\n", val);
383 if (file->f_mode & FMODE_WRITE) {
384 struct wave_format format;
386 spin_lock_irqsave(&woinst->lock, flags);
388 format = woinst->format;
389 format.samplingrate = val;
391 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
392 spin_unlock_irqrestore(&woinst->lock, flags);
393 return -EINVAL;
396 val = woinst->format.samplingrate;
398 spin_unlock_irqrestore(&woinst->lock, flags);
400 DPD(2, "set playback sampling rate -> %d\n", val);
403 return put_user(val, p);
404 } else {
405 if (file->f_mode & FMODE_READ)
406 val = wiinst->format.samplingrate;
407 else if (file->f_mode & FMODE_WRITE)
408 val = woinst->format.samplingrate;
410 return put_user(val, p);
412 break;
414 case SNDCTL_DSP_STEREO:
415 DPF(2, "SNDCTL_DSP_STEREO:\n");
417 if (get_user(val, p))
418 return -EFAULT;
420 DPD(2, " val is %d\n", val);
422 if (file->f_mode & FMODE_READ) {
423 struct wave_format format;
425 spin_lock_irqsave(&wiinst->lock, flags);
427 format = wiinst->format;
428 format.channels = val ? 2 : 1;
430 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
431 spin_unlock_irqrestore(&wiinst->lock, flags);
432 return -EINVAL;
435 val = wiinst->format.channels - 1;
437 spin_unlock_irqrestore(&wiinst->lock, flags);
438 DPD(2, "set recording stereo -> %d\n", val);
441 if (file->f_mode & FMODE_WRITE) {
442 struct wave_format format;
444 spin_lock_irqsave(&woinst->lock, flags);
446 format = woinst->format;
447 format.channels = val ? 2 : 1;
449 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
450 spin_unlock_irqrestore(&woinst->lock, flags);
451 return -EINVAL;
454 val = woinst->format.channels - 1;
456 spin_unlock_irqrestore(&woinst->lock, flags);
458 DPD(2, "set playback stereo -> %d\n", val);
461 return put_user(val, p);
463 break;
465 case SNDCTL_DSP_CHANNELS:
466 DPF(2, "SNDCTL_DSP_CHANNELS:\n");
468 if (get_user(val, p))
469 return -EFAULT;
471 DPD(2, " val is %d\n", val);
473 if (val > 0) {
474 if (file->f_mode & FMODE_READ) {
475 struct wave_format format;
477 spin_lock_irqsave(&wiinst->lock, flags);
479 format = wiinst->format;
480 format.channels = val;
482 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
483 spin_unlock_irqrestore(&wiinst->lock, flags);
484 return -EINVAL;
486 val = wiinst->format.channels;
488 spin_unlock_irqrestore(&wiinst->lock, flags);
489 DPD(2, "set recording number of channels -> %d\n", val);
492 if (file->f_mode & FMODE_WRITE) {
493 struct wave_format format;
495 spin_lock_irqsave(&woinst->lock, flags);
497 format = woinst->format;
498 format.channels = val;
500 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
501 spin_unlock_irqrestore(&woinst->lock, flags);
502 return -EINVAL;
505 val = woinst->format.channels;
507 spin_unlock_irqrestore(&woinst->lock, flags);
508 DPD(2, "set playback number of channels -> %d\n", val);
511 return put_user(val, p);
512 } else {
513 if (file->f_mode & FMODE_READ)
514 val = wiinst->format.channels;
515 else if (file->f_mode & FMODE_WRITE)
516 val = woinst->format.channels;
518 return put_user(val, p);
520 break;
522 case SNDCTL_DSP_GETFMTS:
523 DPF(2, "SNDCTL_DSP_GETFMTS:\n");
525 if (file->f_mode & FMODE_READ)
526 val = AFMT_S16_LE;
527 else if (file->f_mode & FMODE_WRITE) {
528 val = AFMT_S16_LE | AFMT_U8;
529 if (emu10k1_find_control_gpr(&wave_dev->card->mgr,
530 wave_dev->card->pt.patch_name,
531 wave_dev->card->pt.enable_gpr_name) >= 0)
532 val |= AFMT_AC3;
534 return put_user(val, p);
536 case SNDCTL_DSP_SETFMT: /* Same as SNDCTL_DSP_SAMPLESIZE */
537 DPF(2, "SNDCTL_DSP_SETFMT:\n");
539 if (get_user(val, p))
540 return -EFAULT;
542 DPD(2, " val is %d\n", val);
544 if (val != AFMT_QUERY) {
545 if (file->f_mode & FMODE_READ) {
546 struct wave_format format;
548 spin_lock_irqsave(&wiinst->lock, flags);
550 format = wiinst->format;
551 format.id = val;
553 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
554 spin_unlock_irqrestore(&wiinst->lock, flags);
555 return -EINVAL;
558 val = wiinst->format.id;
560 spin_unlock_irqrestore(&wiinst->lock, flags);
561 DPD(2, "set recording format -> %d\n", val);
564 if (file->f_mode & FMODE_WRITE) {
565 struct wave_format format;
567 spin_lock_irqsave(&woinst->lock, flags);
569 format = woinst->format;
570 format.id = val;
572 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
573 spin_unlock_irqrestore(&woinst->lock, flags);
574 return -EINVAL;
577 val = woinst->format.id;
579 spin_unlock_irqrestore(&woinst->lock, flags);
580 DPD(2, "set playback format -> %d\n", val);
583 return put_user(val, p);
584 } else {
585 if (file->f_mode & FMODE_READ)
586 val = wiinst->format.id;
587 else if (file->f_mode & FMODE_WRITE)
588 val = woinst->format.id;
590 return put_user(val, p);
592 break;
594 case SOUND_PCM_READ_BITS:
596 if (file->f_mode & FMODE_READ)
597 val = wiinst->format.bitsperchannel;
598 else if (file->f_mode & FMODE_WRITE)
599 val = woinst->format.bitsperchannel;
601 return put_user(val, p);
603 case SOUND_PCM_READ_RATE:
605 if (file->f_mode & FMODE_READ)
606 val = wiinst->format.samplingrate;
607 else if (file->f_mode & FMODE_WRITE)
608 val = woinst->format.samplingrate;
610 return put_user(val, p);
612 case SOUND_PCM_READ_CHANNELS:
614 if (file->f_mode & FMODE_READ)
615 val = wiinst->format.channels;
616 else if (file->f_mode & FMODE_WRITE)
617 val = woinst->format.channels;
619 return put_user(val, p);
621 case SOUND_PCM_WRITE_FILTER:
622 DPF(2, "SOUND_PCM_WRITE_FILTER: not implemented\n");
623 break;
625 case SOUND_PCM_READ_FILTER:
626 DPF(2, "SOUND_PCM_READ_FILTER: not implemented\n");
627 break;
629 case SNDCTL_DSP_SETSYNCRO:
630 DPF(2, "SNDCTL_DSP_SETSYNCRO: not implemented\n");
631 break;
633 case SNDCTL_DSP_GETTRIGGER:
634 DPF(2, "SNDCTL_DSP_GETTRIGGER:\n");
636 if (file->f_mode & FMODE_WRITE && (wave_dev->enablebits & PCM_ENABLE_OUTPUT))
637 val |= PCM_ENABLE_OUTPUT;
639 if (file->f_mode & FMODE_READ && (wave_dev->enablebits & PCM_ENABLE_INPUT))
640 val |= PCM_ENABLE_INPUT;
642 return put_user(val, p);
644 case SNDCTL_DSP_SETTRIGGER:
645 DPF(2, "SNDCTL_DSP_SETTRIGGER:\n");
647 if (get_user(val, p))
648 return -EFAULT;
650 if (file->f_mode & FMODE_WRITE) {
651 spin_lock_irqsave(&woinst->lock, flags);
653 if (val & PCM_ENABLE_OUTPUT) {
654 wave_dev->enablebits |= PCM_ENABLE_OUTPUT;
655 if (woinst->state & WAVE_STATE_OPEN)
656 emu10k1_waveout_start(wave_dev);
657 } else {
658 wave_dev->enablebits &= ~PCM_ENABLE_OUTPUT;
659 if (woinst->state & WAVE_STATE_STARTED)
660 emu10k1_waveout_stop(wave_dev);
663 spin_unlock_irqrestore(&woinst->lock, flags);
666 if (file->f_mode & FMODE_READ) {
667 spin_lock_irqsave(&wiinst->lock, flags);
669 if (val & PCM_ENABLE_INPUT) {
670 wave_dev->enablebits |= PCM_ENABLE_INPUT;
671 if (wiinst->state & WAVE_STATE_OPEN)
672 emu10k1_wavein_start(wave_dev);
673 } else {
674 wave_dev->enablebits &= ~PCM_ENABLE_INPUT;
675 if (wiinst->state & WAVE_STATE_STARTED)
676 emu10k1_wavein_stop(wave_dev);
679 spin_unlock_irqrestore(&wiinst->lock, flags);
681 break;
683 case SNDCTL_DSP_GETOSPACE:
685 audio_buf_info info;
687 DPF(4, "SNDCTL_DSP_GETOSPACE:\n");
689 if (!(file->f_mode & FMODE_WRITE))
690 return -EINVAL;
692 spin_lock_irqsave(&woinst->lock, flags);
694 if (woinst->state & WAVE_STATE_OPEN) {
695 emu10k1_waveout_update(woinst);
696 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
697 info.bytes = bytestocopy;
698 } else {
699 calculate_ofrag(woinst);
700 info.bytes = woinst->buffer.size;
702 spin_unlock_irqrestore(&woinst->lock, flags);
704 info.bytes *= woinst->num_voices;
705 info.fragsize = woinst->buffer.fragment_size * woinst->num_voices;
706 info.fragstotal = woinst->buffer.numfrags * woinst->num_voices;
707 info.fragments = info.bytes / info.fragsize;
709 if (copy_to_user(p, &info, sizeof(info)))
710 return -EFAULT;
712 break;
714 case SNDCTL_DSP_GETISPACE:
716 audio_buf_info info;
718 DPF(4, "SNDCTL_DSP_GETISPACE:\n");
720 if (!(file->f_mode & FMODE_READ))
721 return -EINVAL;
723 spin_lock_irqsave(&wiinst->lock, flags);
724 if (wiinst->state & WAVE_STATE_OPEN) {
725 emu10k1_wavein_update(wave_dev->card, wiinst);
726 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
727 info.bytes = bytestocopy;
728 } else {
729 calculate_ifrag(wiinst);
730 info.bytes = 0;
732 spin_unlock_irqrestore(&wiinst->lock, flags);
734 info.fragstotal = wiinst->buffer.numfrags;
735 info.fragments = info.bytes / wiinst->buffer.fragment_size;
736 info.fragsize = wiinst->buffer.fragment_size;
738 if (copy_to_user(p, &info, sizeof(info)))
739 return -EFAULT;
741 break;
743 case SNDCTL_DSP_NONBLOCK:
744 DPF(2, "SNDCTL_DSP_NONBLOCK:\n");
746 file->f_flags |= O_NONBLOCK;
747 break;
749 case SNDCTL_DSP_GETODELAY:
750 DPF(4, "SNDCTL_DSP_GETODELAY:\n");
752 if (!(file->f_mode & FMODE_WRITE))
753 return -EINVAL;
755 spin_lock_irqsave(&woinst->lock, flags);
756 if (woinst->state & WAVE_STATE_OPEN) {
757 emu10k1_waveout_update(woinst);
758 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
759 val = woinst->buffer.size - bytestocopy;
760 } else
761 val = 0;
763 val *= woinst->num_voices;
764 spin_unlock_irqrestore(&woinst->lock, flags);
766 return put_user(val, p);
768 case SNDCTL_DSP_GETIPTR:
770 count_info cinfo;
772 DPF(4, "SNDCTL_DSP_GETIPTR: \n");
774 if (!(file->f_mode & FMODE_READ))
775 return -EINVAL;
777 spin_lock_irqsave(&wiinst->lock, flags);
779 if (wiinst->state & WAVE_STATE_OPEN) {
780 emu10k1_wavein_update(wave_dev->card, wiinst);
781 cinfo.ptr = wiinst->buffer.hw_pos;
782 cinfo.bytes = cinfo.ptr + wiinst->total_recorded - wiinst->total_recorded % wiinst->buffer.size;
783 cinfo.blocks = cinfo.bytes / wiinst->buffer.fragment_size - wiinst->blocks;
784 wiinst->blocks = cinfo.bytes / wiinst->buffer.fragment_size;
785 } else {
786 cinfo.ptr = 0;
787 cinfo.bytes = 0;
788 cinfo.blocks = 0;
791 if (wiinst->mmapped)
792 wiinst->buffer.bytestocopy %= wiinst->buffer.fragment_size;
794 spin_unlock_irqrestore(&wiinst->lock, flags);
796 if (copy_to_user(p, &cinfo, sizeof(cinfo)))
797 return -EFAULT;
799 break;
801 case SNDCTL_DSP_GETOPTR:
803 count_info cinfo;
805 DPF(4, "SNDCTL_DSP_GETOPTR:\n");
807 if (!(file->f_mode & FMODE_WRITE))
808 return -EINVAL;
810 spin_lock_irqsave(&woinst->lock, flags);
812 if (woinst->state & WAVE_STATE_OPEN ||
813 ((woinst->format.passthrough == 1) && wave_dev->card->pt.state)) {
814 int num_fragments;
816 if (woinst->format.passthrough == 1) {
817 emu10k1_pt_waveout_update(wave_dev);
818 cinfo.bytes = woinst->total_played;
819 } else {
820 emu10k1_waveout_update(woinst);
821 cinfo.bytes = woinst->total_played;
824 cinfo.ptr = woinst->buffer.hw_pos;
825 num_fragments = cinfo.bytes / woinst->buffer.fragment_size;
826 cinfo.blocks = num_fragments - woinst->blocks;
827 woinst->blocks = num_fragments;
829 cinfo.bytes *= woinst->num_voices;
830 cinfo.ptr *= woinst->num_voices;
831 } else {
832 cinfo.ptr = 0;
833 cinfo.bytes = 0;
834 cinfo.blocks = 0;
837 if (woinst->mmapped)
838 woinst->buffer.free_bytes %= woinst->buffer.fragment_size;
840 spin_unlock_irqrestore(&woinst->lock, flags);
842 if (copy_to_user(p, &cinfo, sizeof(cinfo)))
843 return -EFAULT;
845 break;
847 case SNDCTL_DSP_GETBLKSIZE:
848 DPF(2, "SNDCTL_DSP_GETBLKSIZE:\n");
850 if (file->f_mode & FMODE_WRITE) {
851 spin_lock_irqsave(&woinst->lock, flags);
853 calculate_ofrag(woinst);
854 val = woinst->buffer.fragment_size * woinst->num_voices;
856 spin_unlock_irqrestore(&woinst->lock, flags);
859 if (file->f_mode & FMODE_READ) {
860 spin_lock_irqsave(&wiinst->lock, flags);
862 calculate_ifrag(wiinst);
863 val = wiinst->buffer.fragment_size;
865 spin_unlock_irqrestore(&wiinst->lock, flags);
868 return put_user(val, p);
870 break;
872 case SNDCTL_DSP_POST:
873 if (file->f_mode & FMODE_WRITE) {
874 spin_lock_irqsave(&woinst->lock, flags);
876 if (!(woinst->state & WAVE_STATE_STARTED)
877 && (wave_dev->enablebits & PCM_ENABLE_OUTPUT)
878 && (woinst->total_copied > 0))
879 emu10k1_waveout_start(wave_dev);
881 spin_unlock_irqrestore(&woinst->lock, flags);
884 break;
886 case SNDCTL_DSP_SUBDIVIDE:
887 DPF(2, "SNDCTL_DSP_SUBDIVIDE: not implemented\n");
888 break;
890 case SNDCTL_DSP_SETFRAGMENT:
891 DPF(2, "SNDCTL_DSP_SETFRAGMENT:\n");
893 if (get_user(val, p))
894 return -EFAULT;
896 DPD(2, "val is %#x\n", val);
898 if (val == 0)
899 return -EIO;
901 if (file->f_mode & FMODE_WRITE) {
902 /* digital pass-through fragment count and size are fixed values */
903 if (woinst->state & WAVE_STATE_OPEN || (woinst->format.passthrough == 1))
904 return -EINVAL; /* too late to change */
906 woinst->buffer.ossfragshift = val & 0xffff;
907 woinst->buffer.numfrags = (val >> 16) & 0xffff;
910 if (file->f_mode & FMODE_READ) {
911 if (wiinst->state & WAVE_STATE_OPEN)
912 return -EINVAL; /* too late to change */
914 wiinst->buffer.ossfragshift = val & 0xffff;
915 wiinst->buffer.numfrags = (val >> 16) & 0xffff;
918 break;
920 case SNDCTL_COPR_LOAD:
922 copr_buffer *buf;
923 u32 i;
925 DPF(4, "SNDCTL_COPR_LOAD:\n");
927 buf = kmalloc(sizeof(copr_buffer), GFP_KERNEL);
928 if (!buf)
929 return -ENOMEM;
931 if (copy_from_user(buf, p, sizeof(copr_buffer))) {
932 kfree (buf);
933 return -EFAULT;
936 if ((buf->command != CMD_READ) && (buf->command != CMD_WRITE)) {
937 kfree (buf);
938 return -EINVAL;
941 if (buf->command == CMD_WRITE) {
943 #ifdef DBGEMU
944 if ((buf->offs < 0) || (buf->offs + buf->len > 0xe00) || (buf->len > 1000)) {
945 #else
946 if (((buf->offs < 0x100) || (buf->offs + buf->len > (wave_dev->card->is_audigy ? 0xe00 : 0x800)) || (buf->len > 1000)
947 ) && !(
948 //any register allowed raw access to users goes here:
949 (buf->offs == DBG ||
950 buf->offs == A_DBG)
951 && (buf->len == 1))) {
952 #endif
953 kfree(buf);
954 return -EINVAL;
956 } else {
957 if ((buf->offs < 0) || (buf->offs + buf->len > 0xe00) || (buf->len > 1000)) {
958 kfree(buf);
959 return -EINVAL;
963 if (((unsigned)buf->flags) > 0x3f)
964 buf->flags = 0;
966 if (buf->command == CMD_READ) {
967 for (i = 0; i < buf->len; i++)
968 ((u32 *) buf->data)[i] = sblive_readptr(wave_dev->card, buf->offs + i, buf->flags);
970 if (copy_to_user(p, buf, sizeof(copr_buffer))) {
971 kfree(buf);
972 return -EFAULT;
974 } else {
975 for (i = 0; i < buf->len; i++)
976 sblive_writeptr(wave_dev->card, buf->offs + i, buf->flags, ((u32 *) buf->data)[i]);
979 kfree (buf);
980 break;
983 default: /* Default is unrecognized command */
984 DPD(2, "default: %#x\n", cmd);
985 return -EINVAL;
987 return 0;
990 static struct page *emu10k1_mm_nopage (struct vm_area_struct * vma, unsigned long address, int *type)
992 struct emu10k1_wavedevice *wave_dev = vma->vm_private_data;
993 struct woinst *woinst = wave_dev->woinst;
994 struct wiinst *wiinst = wave_dev->wiinst;
995 struct page *dmapage;
996 unsigned long pgoff;
997 int rd, wr;
999 DPF(3, "emu10k1_mm_nopage()\n");
1000 DPD(3, "addr: %#lx\n", address);
1002 if (address > vma->vm_end) {
1003 DPF(1, "EXIT, returning NOPAGE_SIGBUS\n");
1004 return NOPAGE_SIGBUS; /* Disallow mremap */
1007 pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
1008 if (woinst != NULL)
1009 wr = woinst->mmapped;
1010 else
1011 wr = 0;
1013 if (wiinst != NULL)
1014 rd = wiinst->mmapped;
1015 else
1016 rd = 0;
1018 /* if full-duplex (read+write) and we have two sets of bufs,
1019 * then the playback buffers come first, sez soundcard.c */
1020 if (wr) {
1021 if (pgoff >= woinst->buffer.pages) {
1022 pgoff -= woinst->buffer.pages;
1023 dmapage = virt_to_page ((u8 *) wiinst->buffer.addr + pgoff * PAGE_SIZE);
1024 } else
1025 dmapage = virt_to_page (woinst->voice[0].mem.addr[pgoff]);
1026 } else {
1027 dmapage = virt_to_page ((u8 *) wiinst->buffer.addr + pgoff * PAGE_SIZE);
1030 get_page (dmapage);
1032 DPD(3, "page: %#lx\n", (unsigned long) dmapage);
1033 if (type)
1034 *type = VM_FAULT_MINOR;
1035 return dmapage;
1038 static struct vm_operations_struct emu10k1_mm_ops = {
1039 .nopage = emu10k1_mm_nopage,
1042 static int emu10k1_audio_mmap(struct file *file, struct vm_area_struct *vma)
1044 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1045 unsigned long max_pages, n_pages, pgoffset;
1046 struct woinst *woinst = NULL;
1047 struct wiinst *wiinst = NULL;
1048 unsigned long flags;
1050 DPF(2, "emu10k1_audio_mmap()\n");
1052 max_pages = 0;
1053 if (vma->vm_flags & VM_WRITE) {
1054 woinst = wave_dev->woinst;
1056 spin_lock_irqsave(&woinst->lock, flags);
1058 /* No m'mapping possible for multichannel */
1059 if (woinst->num_voices > 1) {
1060 spin_unlock_irqrestore(&woinst->lock, flags);
1061 return -EINVAL;
1064 if (woinst->state == WAVE_STATE_CLOSED) {
1065 calculate_ofrag(woinst);
1067 if (emu10k1_waveout_open(wave_dev) < 0) {
1068 spin_unlock_irqrestore(&woinst->lock, flags);
1069 ERROR();
1070 return -EINVAL;
1074 woinst->mmapped = 1;
1075 max_pages += woinst->buffer.pages;
1076 spin_unlock_irqrestore(&woinst->lock, flags);
1079 if (vma->vm_flags & VM_READ) {
1080 wiinst = wave_dev->wiinst;
1082 spin_lock_irqsave(&wiinst->lock, flags);
1083 if (wiinst->state == WAVE_STATE_CLOSED) {
1084 calculate_ifrag(wiinst);
1086 if (emu10k1_wavein_open(wave_dev) < 0) {
1087 spin_unlock_irqrestore(&wiinst->lock, flags);
1088 ERROR();
1089 return -EINVAL;
1093 wiinst->mmapped = 1;
1094 max_pages += wiinst->buffer.pages;
1095 spin_unlock_irqrestore(&wiinst->lock, flags);
1098 n_pages = ((vma->vm_end - vma->vm_start) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1099 pgoffset = vma->vm_pgoff;
1101 DPD(2, "vma_start: %#lx, vma_end: %#lx, vma_offset: %ld\n", vma->vm_start, vma->vm_end, pgoffset);
1102 DPD(2, "n_pages: %ld, max_pages: %ld\n", n_pages, max_pages);
1104 if (pgoffset + n_pages > max_pages)
1105 return -EINVAL;
1107 vma->vm_flags |= VM_RESERVED;
1108 vma->vm_ops = &emu10k1_mm_ops;
1109 vma->vm_private_data = wave_dev;
1110 return 0;
1113 static int emu10k1_audio_open(struct inode *inode, struct file *file)
1115 int minor = iminor(inode);
1116 struct emu10k1_card *card = NULL;
1117 struct list_head *entry;
1118 struct emu10k1_wavedevice *wave_dev;
1120 DPF(2, "emu10k1_audio_open()\n");
1122 /* Check for correct device to open */
1124 list_for_each(entry, &emu10k1_devs) {
1125 card = list_entry(entry, struct emu10k1_card, list);
1127 if (!((card->audio_dev ^ minor) & ~0xf) || !((card->audio_dev1 ^ minor) & ~0xf))
1128 goto match;
1131 return -ENODEV;
1133 match:
1135 wave_dev = (struct emu10k1_wavedevice *) kmalloc(sizeof(struct emu10k1_wavedevice), GFP_KERNEL);
1137 if (wave_dev == NULL) {
1138 ERROR();
1139 return -ENOMEM;
1142 wave_dev->card = card;
1143 wave_dev->wiinst = NULL;
1144 wave_dev->woinst = NULL;
1145 wave_dev->enablebits = PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT; /* Default */
1147 if (file->f_mode & FMODE_READ) {
1148 /* Recording */
1149 struct wiinst *wiinst;
1151 if ((wiinst = (struct wiinst *) kmalloc(sizeof(struct wiinst), GFP_KERNEL)) == NULL) {
1152 ERROR();
1153 kfree(wave_dev);
1154 return -ENOMEM;
1157 wiinst->recsrc = card->wavein.recsrc;
1158 wiinst->fxwc = card->wavein.fxwc;
1160 switch (wiinst->recsrc) {
1161 case WAVERECORD_AC97:
1162 wiinst->format.id = AFMT_S16_LE;
1163 wiinst->format.samplingrate = 8000;
1164 wiinst->format.bitsperchannel = 16;
1165 wiinst->format.channels = 1;
1166 break;
1167 case WAVERECORD_MIC:
1168 wiinst->format.id = AFMT_S16_LE;
1169 wiinst->format.samplingrate = 8000;
1170 wiinst->format.bitsperchannel = 16;
1171 wiinst->format.channels = 1;
1172 break;
1173 case WAVERECORD_FX:
1174 wiinst->format.id = AFMT_S16_LE;
1175 wiinst->format.samplingrate = 48000;
1176 wiinst->format.bitsperchannel = 16;
1177 wiinst->format.channels = hweight32(wiinst->fxwc);
1178 break;
1179 default:
1180 kfree(wave_dev);
1181 kfree(wiinst);
1182 BUG();
1183 break;
1186 wiinst->state = WAVE_STATE_CLOSED;
1188 wiinst->buffer.ossfragshift = 0;
1189 wiinst->buffer.fragment_size = 0;
1190 wiinst->buffer.numfrags = 0;
1192 init_waitqueue_head(&wiinst->wait_queue);
1194 wiinst->mmapped = 0;
1195 wiinst->total_recorded = 0;
1196 wiinst->blocks = 0;
1197 spin_lock_init(&wiinst->lock);
1198 tasklet_init(&wiinst->timer.tasklet, emu10k1_wavein_bh, (unsigned long) wave_dev);
1199 wave_dev->wiinst = wiinst;
1200 emu10k1_wavein_setformat(wave_dev, &wiinst->format);
1203 if (file->f_mode & FMODE_WRITE) {
1204 struct woinst *woinst;
1205 int i;
1207 if ((woinst = (struct woinst *) kmalloc(sizeof(struct woinst), GFP_KERNEL)) == NULL) {
1208 ERROR();
1209 kfree(wave_dev);
1210 return -ENOMEM;
1213 if (wave_dev->wiinst != NULL) {
1214 woinst->format = wave_dev->wiinst->format;
1215 } else {
1216 woinst->format.id = AFMT_U8;
1217 woinst->format.samplingrate = 8000;
1218 woinst->format.bitsperchannel = 8;
1219 woinst->format.channels = 1;
1222 woinst->state = WAVE_STATE_CLOSED;
1224 woinst->buffer.fragment_size = 0;
1225 woinst->buffer.ossfragshift = 0;
1226 woinst->buffer.numfrags = 0;
1227 woinst->device = (card->audio_dev1 == minor);
1228 woinst->timer.state = TIMER_STATE_UNINSTALLED;
1229 woinst->num_voices = 1;
1230 for (i = 0; i < WAVEOUT_MAXVOICES; i++) {
1231 woinst->voice[i].usage = VOICE_USAGE_FREE;
1232 woinst->voice[i].mem.emupageindex = -1;
1235 init_waitqueue_head(&woinst->wait_queue);
1237 woinst->mmapped = 0;
1238 woinst->total_copied = 0;
1239 woinst->total_played = 0;
1240 woinst->blocks = 0;
1241 spin_lock_init(&woinst->lock);
1242 tasklet_init(&woinst->timer.tasklet, emu10k1_waveout_bh, (unsigned long) wave_dev);
1243 wave_dev->woinst = woinst;
1244 emu10k1_waveout_setformat(wave_dev, &woinst->format);
1247 file->private_data = (void *) wave_dev;
1249 return nonseekable_open(inode, file);
1252 static int emu10k1_audio_release(struct inode *inode, struct file *file)
1254 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1255 struct emu10k1_card *card;
1256 unsigned long flags;
1258 card = wave_dev->card;
1260 DPF(2, "emu10k1_audio_release()\n");
1262 if (file->f_mode & FMODE_WRITE) {
1263 struct woinst *woinst = wave_dev->woinst;
1265 spin_lock_irqsave(&woinst->lock, flags);
1266 if(woinst->format.passthrough==2)
1267 card->pt.state=PT_STATE_PLAYING;
1268 if (woinst->format.passthrough && card->pt.state != PT_STATE_INACTIVE){
1269 spin_lock(&card->pt.lock);
1270 emu10k1_pt_stop(card);
1271 spin_unlock(&card->pt.lock);
1273 if (woinst->state & WAVE_STATE_OPEN) {
1274 if (woinst->state & WAVE_STATE_STARTED) {
1275 if (!(file->f_flags & O_NONBLOCK)) {
1276 while (!signal_pending(current)
1277 && (woinst->total_played < woinst->total_copied)) {
1278 DPF(4, "Buffer hasn't been totally played, sleep....\n");
1279 spin_unlock_irqrestore(&woinst->lock, flags);
1280 interruptible_sleep_on(&woinst->wait_queue);
1281 spin_lock_irqsave(&woinst->lock, flags);
1285 emu10k1_waveout_close(wave_dev);
1288 spin_unlock_irqrestore(&woinst->lock, flags);
1289 /* remove the tasklet */
1290 tasklet_kill(&woinst->timer.tasklet);
1291 kfree(wave_dev->woinst);
1294 if (file->f_mode & FMODE_READ) {
1295 struct wiinst *wiinst = wave_dev->wiinst;
1297 spin_lock_irqsave(&wiinst->lock, flags);
1299 if (wiinst->state & WAVE_STATE_OPEN) {
1300 emu10k1_wavein_close(wave_dev);
1303 spin_unlock_irqrestore(&wiinst->lock, flags);
1304 tasklet_kill(&wiinst->timer.tasklet);
1305 kfree(wave_dev->wiinst);
1308 kfree(wave_dev);
1310 if (waitqueue_active(&card->open_wait))
1311 wake_up_interruptible(&card->open_wait);
1313 return 0;
1316 /* FIXME sort out poll() + mmap() */
1317 static unsigned int emu10k1_audio_poll(struct file *file, struct poll_table_struct *wait)
1319 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1320 struct woinst *woinst = wave_dev->woinst;
1321 struct wiinst *wiinst = wave_dev->wiinst;
1322 unsigned int mask = 0;
1323 u32 bytestocopy;
1324 unsigned long flags;
1326 DPF(4, "emu10k1_audio_poll()\n");
1328 if (file->f_mode & FMODE_WRITE)
1329 poll_wait(file, &woinst->wait_queue, wait);
1331 if (file->f_mode & FMODE_READ)
1332 poll_wait(file, &wiinst->wait_queue, wait);
1334 if (file->f_mode & FMODE_WRITE) {
1335 spin_lock_irqsave(&woinst->lock, flags);
1337 if (woinst->state & WAVE_STATE_OPEN) {
1338 emu10k1_waveout_update(woinst);
1339 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
1341 if (bytestocopy >= woinst->buffer.fragment_size)
1342 mask |= POLLOUT | POLLWRNORM;
1343 } else
1344 mask |= POLLOUT | POLLWRNORM;
1346 spin_unlock_irqrestore(&woinst->lock, flags);
1349 if (file->f_mode & FMODE_READ) {
1350 spin_lock_irqsave(&wiinst->lock, flags);
1352 if (wiinst->state & WAVE_STATE_OPEN) {
1353 emu10k1_wavein_update(wave_dev->card, wiinst);
1354 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
1356 if (bytestocopy >= wiinst->buffer.fragment_size)
1357 mask |= POLLIN | POLLRDNORM;
1360 spin_unlock_irqrestore(&wiinst->lock, flags);
1363 return mask;
1366 static void calculate_ofrag(struct woinst *woinst)
1368 struct waveout_buffer *buffer = &woinst->buffer;
1369 u32 fragsize;
1371 if (buffer->fragment_size)
1372 return;
1374 if (!buffer->ossfragshift) {
1375 fragsize = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTFRAGLEN) / 1000 - 1;
1377 while (fragsize) {
1378 fragsize >>= 1;
1379 buffer->ossfragshift++;
1383 if (buffer->ossfragshift < WAVEOUT_MINFRAGSHIFT)
1384 buffer->ossfragshift = WAVEOUT_MINFRAGSHIFT;
1386 buffer->fragment_size = 1 << buffer->ossfragshift;
1388 while (buffer->fragment_size * WAVEOUT_MINFRAGS > WAVEOUT_MAXBUFSIZE)
1389 buffer->fragment_size >>= 1;
1391 /* now we are sure that:
1392 (2^WAVEOUT_MINFRAGSHIFT) <= (fragment_size = 2^n) <= (WAVEOUT_MAXBUFSIZE / WAVEOUT_MINFRAGS)
1395 if (!buffer->numfrags) {
1396 u32 numfrags;
1398 numfrags = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTBUFLEN) /
1399 (buffer->fragment_size * 1000) - 1;
1401 buffer->numfrags = 1;
1403 while (numfrags) {
1404 numfrags >>= 1;
1405 buffer->numfrags <<= 1;
1409 if (buffer->numfrags < WAVEOUT_MINFRAGS)
1410 buffer->numfrags = WAVEOUT_MINFRAGS;
1412 if (buffer->numfrags * buffer->fragment_size > WAVEOUT_MAXBUFSIZE)
1413 buffer->numfrags = WAVEOUT_MAXBUFSIZE / buffer->fragment_size;
1415 if (buffer->numfrags < WAVEOUT_MINFRAGS)
1416 BUG();
1418 buffer->size = buffer->fragment_size * buffer->numfrags;
1419 buffer->pages = buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
1421 DPD(2, " calculated playback fragment_size -> %d\n", buffer->fragment_size);
1422 DPD(2, " calculated playback numfrags -> %d\n", buffer->numfrags);
1424 return;
1427 static void calculate_ifrag(struct wiinst *wiinst)
1429 struct wavein_buffer *buffer = &wiinst->buffer;
1430 u32 fragsize, bufsize, size[4];
1431 int i, j;
1433 if (buffer->fragment_size)
1434 return;
1436 if (!buffer->ossfragshift) {
1437 fragsize = (wiinst->format.bytespersec * WAVEIN_DEFAULTFRAGLEN) / 1000 - 1;
1439 while (fragsize) {
1440 fragsize >>= 1;
1441 buffer->ossfragshift++;
1445 if (buffer->ossfragshift < WAVEIN_MINFRAGSHIFT)
1446 buffer->ossfragshift = WAVEIN_MINFRAGSHIFT;
1448 buffer->fragment_size = 1 << buffer->ossfragshift;
1450 while (buffer->fragment_size * WAVEIN_MINFRAGS > WAVEIN_MAXBUFSIZE)
1451 buffer->fragment_size >>= 1;
1453 /* now we are sure that:
1454 (2^WAVEIN_MINFRAGSHIFT) <= (fragment_size = 2^n) <= (WAVEIN_MAXBUFSIZE / WAVEIN_MINFRAGS)
1458 if (!buffer->numfrags)
1459 buffer->numfrags = (wiinst->format.bytespersec * WAVEIN_DEFAULTBUFLEN) / (buffer->fragment_size * 1000) - 1;
1461 if (buffer->numfrags < WAVEIN_MINFRAGS)
1462 buffer->numfrags = WAVEIN_MINFRAGS;
1464 if (buffer->numfrags * buffer->fragment_size > WAVEIN_MAXBUFSIZE)
1465 buffer->numfrags = WAVEIN_MAXBUFSIZE / buffer->fragment_size;
1467 if (buffer->numfrags < WAVEIN_MINFRAGS)
1468 BUG();
1470 bufsize = buffer->fragment_size * buffer->numfrags;
1472 /* the buffer size for recording is restricted to certain values, adjust it now */
1473 if (bufsize >= 0x10000) {
1474 buffer->size = 0x10000;
1475 buffer->sizeregval = 0x1f;
1476 } else {
1477 buffer->size = 0;
1478 size[0] = 384;
1479 size[1] = 448;
1480 size[2] = 512;
1481 size[3] = 640;
1483 for (i = 0; i < 8; i++)
1484 for (j = 0; j < 4; j++)
1485 if (bufsize >= size[j]) {
1486 buffer->size = size[j];
1487 size[j] *= 2;
1488 buffer->sizeregval = i * 4 + j + 1;
1489 } else
1490 goto exitloop;
1491 exitloop:
1492 if (buffer->size == 0) {
1493 buffer->size = 384;
1494 buffer->sizeregval = 0x01;
1498 /* adjust the fragment size so that buffer size is an integer multiple */
1499 while (buffer->size % buffer->fragment_size)
1500 buffer->fragment_size >>= 1;
1502 buffer->numfrags = buffer->size / buffer->fragment_size;
1503 buffer->pages = buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
1505 DPD(2, " calculated recording fragment_size -> %d\n", buffer->fragment_size);
1506 DPD(2, " calculated recording numfrags -> %d\n", buffer->numfrags);
1507 DPD(2, " buffer size register -> %#04x\n", buffer->sizeregval);
1509 return;
1512 static void emu10k1_wavein_bh(unsigned long refdata)
1514 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) refdata;
1515 struct wiinst *wiinst = wave_dev->wiinst;
1516 u32 bytestocopy;
1517 unsigned long flags;
1519 if (!wiinst)
1520 return;
1522 spin_lock_irqsave(&wiinst->lock, flags);
1524 if (!(wiinst->state & WAVE_STATE_STARTED)) {
1525 spin_unlock_irqrestore(&wiinst->lock, flags);
1526 return;
1529 emu10k1_wavein_update(wave_dev->card, wiinst);
1530 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
1532 spin_unlock_irqrestore(&wiinst->lock, flags);
1534 if (bytestocopy >= wiinst->buffer.fragment_size) {
1535 if (waitqueue_active(&wiinst->wait_queue))
1536 wake_up_interruptible(&wiinst->wait_queue);
1537 } else
1538 DPD(3, "Not enough transfer size, %d\n", bytestocopy);
1540 return;
1543 static void emu10k1_waveout_bh(unsigned long refdata)
1545 struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) refdata;
1546 struct woinst *woinst = wave_dev->woinst;
1547 u32 bytestocopy;
1548 unsigned long flags;
1550 if (!woinst)
1551 return;
1553 spin_lock_irqsave(&woinst->lock, flags);
1555 if (!(woinst->state & WAVE_STATE_STARTED)) {
1556 spin_unlock_irqrestore(&woinst->lock, flags);
1557 return;
1560 emu10k1_waveout_update(woinst);
1561 emu10k1_waveout_getxfersize(woinst, &bytestocopy);
1563 if (woinst->buffer.fill_silence) {
1564 spin_unlock_irqrestore(&woinst->lock, flags);
1565 emu10k1_waveout_fillsilence(woinst);
1566 } else
1567 spin_unlock_irqrestore(&woinst->lock, flags);
1569 if (bytestocopy >= woinst->buffer.fragment_size) {
1570 if (waitqueue_active(&woinst->wait_queue))
1571 wake_up_interruptible(&woinst->wait_queue);
1572 } else
1573 DPD(3, "Not enough transfer size -> %d\n", bytestocopy);
1575 return;
1578 struct file_operations emu10k1_audio_fops = {
1579 .owner = THIS_MODULE,
1580 .llseek = no_llseek,
1581 .read = emu10k1_audio_read,
1582 .write = emu10k1_audio_write,
1583 .poll = emu10k1_audio_poll,
1584 .ioctl = emu10k1_audio_ioctl,
1585 .mmap = emu10k1_audio_mmap,
1586 .open = emu10k1_audio_open,
1587 .release = emu10k1_audio_release,