Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / sound / oss / emu10k1 / audio.c
1 /*
2  **********************************************************************
3  *     audio.c -- /dev/dsp interface for emu10k1 driver
4  *     Copyright 1999, 2000 Creative Labs, Inc.
5  *
6  **********************************************************************
7  *
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
12  *
13  **********************************************************************
14  *
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.
19  *
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.
24  *
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.
29  *
30  **********************************************************************
31  */
32
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>
40
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"
48
49 static void calculate_ofrag(struct woinst *);
50 static void calculate_ifrag(struct wiinst *);
51
52 static void emu10k1_waveout_bh(unsigned long refdata);
53 static void emu10k1_wavein_bh(unsigned long refdata);
54
55 /* Audio file operations */
56 static ssize_t emu10k1_audio_read(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
57 {
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;
62
63         DPD(3, "emu10k1_audio_read(), buffer=%p, count=%d\n", buffer, (u32) count);
64
65         if (!access_ok(VERIFY_WRITE, buffer, count))
66                 return -EFAULT;
67
68         spin_lock_irqsave(&wiinst->lock, flags);
69
70         if (wiinst->mmapped) {
71                 spin_unlock_irqrestore(&wiinst->lock, flags);
72                 return -ENXIO;
73         }
74
75         if (wiinst->state == WAVE_STATE_CLOSED) {
76                 calculate_ifrag(wiinst);
77
78                 while (emu10k1_wavein_open(wave_dev) < 0) {
79                         spin_unlock_irqrestore(&wiinst->lock, flags);
80
81                         if (file->f_flags & O_NONBLOCK)
82                                 return -EAGAIN;
83
84                         interruptible_sleep_on(&wave_dev->card->open_wait);
85
86                         if (signal_pending(current))
87                                 return -ERESTARTSYS;
88
89                         spin_lock_irqsave(&wiinst->lock, flags);
90                 }
91         }
92
93         spin_unlock_irqrestore(&wiinst->lock, flags);
94
95         while (count > 0) {
96                 u32 bytestocopy;
97
98                 spin_lock_irqsave(&wiinst->lock, flags);
99
100                 if (!(wiinst->state & WAVE_STATE_STARTED)
101                     && (wave_dev->enablebits & PCM_ENABLE_INPUT))
102                         emu10k1_wavein_start(wave_dev);
103
104                 emu10k1_wavein_update(wave_dev->card, wiinst);
105                 emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
106
107                 spin_unlock_irqrestore(&wiinst->lock, flags);
108
109                 DPD(3, "bytestocopy --> %d\n", bytestocopy);
110
111                 if ((bytestocopy >= wiinst->buffer.fragment_size)
112                     || (bytestocopy >= count)) {
113                         bytestocopy = min_t(u32, bytestocopy, count);
114
115                         emu10k1_wavein_xferdata(wiinst, (u8 __user *)buffer, &bytestocopy);
116
117                         count -= bytestocopy;
118                         buffer += bytestocopy;
119                         ret += bytestocopy;
120                 }
121
122                 if (count > 0) {
123                         if ((file->f_flags & O_NONBLOCK)
124                             || (!(wave_dev->enablebits & PCM_ENABLE_INPUT)))
125                                 return (ret ? ret : -EAGAIN);
126
127                         interruptible_sleep_on(&wiinst->wait_queue);
128
129                         if (signal_pending(current))
130                                 return (ret ? ret : -ERESTARTSYS);
131
132                 }
133         }
134
135         DPD(3, "bytes copied -> %d\n", (u32) ret);
136
137         return ret;
138 }
139
140 static ssize_t emu10k1_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t * ppos)
141 {
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;
146
147         DPD(3, "emu10k1_audio_write(), buffer=%p, count=%d\n", buffer, (u32) count);
148
149         if (!access_ok(VERIFY_READ, buffer, count))
150                 return -EFAULT;
151
152         spin_lock_irqsave(&woinst->lock, flags);
153
154         if (woinst->mmapped) {
155                 spin_unlock_irqrestore(&woinst->lock, flags);
156                 return -ENXIO;
157         }
158         // This is for emu10k1 revs less than 7, we need to go through tram
159         if (woinst->format.passthrough == 1) {
160                 int r;
161                 
162                 woinst->buffer.ossfragshift = PT_BLOCKSIZE_LOG2;
163                 woinst->buffer.numfrags = PT_BLOCKCOUNT;
164                 calculate_ofrag(woinst);
165                 
166                 r = emu10k1_pt_write(file, buffer, count);
167                 spin_unlock_irqrestore(&woinst->lock, flags);
168                 return r;
169         }
170
171         if (woinst->state == WAVE_STATE_CLOSED) {
172                 calculate_ofrag(woinst);
173
174                 while (emu10k1_waveout_open(wave_dev) < 0) {
175                         spin_unlock_irqrestore(&woinst->lock, flags);
176
177                         if (file->f_flags & O_NONBLOCK)
178                                 return -EAGAIN;
179
180                         interruptible_sleep_on(&wave_dev->card->open_wait);
181
182                         if (signal_pending(current))
183                                 return -ERESTARTSYS;
184
185                         spin_lock_irqsave(&woinst->lock, flags);
186                 }
187         }
188
189         spin_unlock_irqrestore(&woinst->lock, flags);
190
191         ret = 0;
192         if (count % woinst->format.bytespersample)
193                 return -EINVAL;
194
195         count /= woinst->num_voices;
196
197         while (count > 0) {
198                 u32 bytestocopy;
199
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);
204
205                 DPD(3, "bytestocopy --> %d\n", bytestocopy);
206
207                 if ((bytestocopy >= woinst->buffer.fragment_size)
208                     || (bytestocopy >= count)) {
209
210                         bytestocopy = min_t(u32, bytestocopy, count);
211
212                         emu10k1_waveout_xferdata(woinst, (u8 __user *) buffer, &bytestocopy);
213
214                         count -= bytestocopy;
215                         buffer += bytestocopy * woinst->num_voices;
216                         ret += bytestocopy * woinst->num_voices;
217
218                         spin_lock_irqsave(&woinst->lock, flags);
219                         woinst->total_copied += bytestocopy;
220
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);
225
226                         spin_unlock_irqrestore(&woinst->lock, flags);
227                 }
228
229                 if (count > 0) {
230                         if ((file->f_flags & O_NONBLOCK)
231                             || (!(wave_dev->enablebits & PCM_ENABLE_OUTPUT)))
232                                 return (ret ? ret : -EAGAIN);
233
234                         interruptible_sleep_on(&woinst->wait_queue);
235
236                         if (signal_pending(current))
237                                 return (ret ? ret : -ERESTARTSYS);
238                 }
239         }
240
241         DPD(3, "bytes copied -> %d\n", (u32) ret);
242
243         return ret;
244 }
245
246 static int emu10k1_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
247 {
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;
255
256         DPF(4, "emu10k1_audio_ioctl()\n");
257
258         if (file->f_mode & FMODE_WRITE)
259                 woinst = wave_dev->woinst;
260
261         if (file->f_mode & FMODE_READ)
262                 wiinst = wave_dev->wiinst;
263
264         switch (cmd) {
265         case OSS_GETVERSION:
266                 DPF(2, "OSS_GETVERSION:\n");
267                 return put_user(SOUND_VERSION, p);
268
269         case SNDCTL_DSP_RESET:
270                 DPF(2, "SNDCTL_DSP_RESET:\n");
271                 wave_dev->enablebits = PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT;
272
273                 if (file->f_mode & FMODE_WRITE) {
274                         spin_lock_irqsave(&woinst->lock, flags);
275
276                         if (woinst->state & WAVE_STATE_OPEN) {
277                                 emu10k1_waveout_close(wave_dev);
278                         }
279
280                         woinst->mmapped = 0;
281                         woinst->total_copied = 0;
282                         woinst->total_played = 0;
283                         woinst->blocks = 0;
284
285                         spin_unlock_irqrestore(&woinst->lock, flags);
286                 }
287
288                 if (file->f_mode & FMODE_READ) {
289                         spin_lock_irqsave(&wiinst->lock, flags);
290
291                         if (wiinst->state & WAVE_STATE_OPEN) {
292                                 emu10k1_wavein_close(wave_dev);
293                         }
294
295                         wiinst->mmapped = 0;
296                         wiinst->total_recorded = 0;
297                         wiinst->blocks = 0;
298                         spin_unlock_irqrestore(&wiinst->lock, flags);
299                 }
300
301                 break;
302
303         case SNDCTL_DSP_SYNC:
304                 DPF(2, "SNDCTL_DSP_SYNC:\n");
305
306                 if (file->f_mode & FMODE_WRITE) {
307
308                         spin_lock_irqsave(&woinst->lock, flags);
309
310                         if (woinst->state & WAVE_STATE_OPEN) {
311
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);
318                                         }
319                                 emu10k1_waveout_close(wave_dev);
320                         }
321
322                         woinst->mmapped = 0;
323                         woinst->total_copied = 0;
324                         woinst->total_played = 0;
325                         woinst->blocks = 0;
326
327                         spin_unlock_irqrestore(&woinst->lock, flags);
328                 }
329
330                 if (file->f_mode & FMODE_READ) {
331                         spin_lock_irqsave(&wiinst->lock, flags);
332
333                         if (wiinst->state & WAVE_STATE_OPEN) {
334                                 emu10k1_wavein_close(wave_dev);
335                         }
336
337                         wiinst->mmapped = 0;
338                         wiinst->total_recorded = 0;
339                         wiinst->blocks = 0;
340                         spin_unlock_irqrestore(&wiinst->lock, flags);
341                 }
342
343                 break;
344
345         case SNDCTL_DSP_SETDUPLEX:
346                 DPF(2, "SNDCTL_DSP_SETDUPLEX:\n");
347                 break;
348
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");
356
357                 if (get_user(val, p))
358                         return -EFAULT;
359
360                 DPD(2, "val is %d\n", val);
361
362                 if (val > 0) {
363                         if (file->f_mode & FMODE_READ) {
364                                 struct wave_format format;
365
366                                 spin_lock_irqsave(&wiinst->lock, flags);
367
368                                 format = wiinst->format;
369                                 format.samplingrate = val;
370
371                                 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
372                                         spin_unlock_irqrestore(&wiinst->lock, flags);
373                                         return -EINVAL;
374                                 }
375
376                                 val = wiinst->format.samplingrate;
377
378                                 spin_unlock_irqrestore(&wiinst->lock, flags);
379
380                                 DPD(2, "set recording sampling rate -> %d\n", val);
381                         }
382
383                         if (file->f_mode & FMODE_WRITE) {
384                                 struct wave_format format;
385
386                                 spin_lock_irqsave(&woinst->lock, flags);
387
388                                 format = woinst->format;
389                                 format.samplingrate = val;
390
391                                 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
392                                         spin_unlock_irqrestore(&woinst->lock, flags);
393                                         return -EINVAL;
394                                 }
395
396                                 val = woinst->format.samplingrate;
397
398                                 spin_unlock_irqrestore(&woinst->lock, flags);
399
400                                 DPD(2, "set playback sampling rate -> %d\n", val);
401                         }
402
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;
409
410                         return put_user(val, p);
411                 }
412                 break;
413
414         case SNDCTL_DSP_STEREO:
415                 DPF(2, "SNDCTL_DSP_STEREO:\n");
416
417                 if (get_user(val, p))
418                         return -EFAULT;
419
420                 DPD(2, " val is %d\n", val);
421
422                 if (file->f_mode & FMODE_READ) {
423                         struct wave_format format;
424
425                         spin_lock_irqsave(&wiinst->lock, flags);
426
427                         format = wiinst->format;
428                         format.channels = val ? 2 : 1;
429
430                         if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
431                                 spin_unlock_irqrestore(&wiinst->lock, flags);
432                                 return -EINVAL;
433                         }
434
435                         val = wiinst->format.channels - 1;
436
437                         spin_unlock_irqrestore(&wiinst->lock, flags);
438                         DPD(2, "set recording stereo -> %d\n", val);
439                 }
440
441                 if (file->f_mode & FMODE_WRITE) {
442                         struct wave_format format;
443
444                         spin_lock_irqsave(&woinst->lock, flags);
445
446                         format = woinst->format;
447                         format.channels = val ? 2 : 1;
448
449                         if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
450                                 spin_unlock_irqrestore(&woinst->lock, flags);
451                                 return -EINVAL;
452                         }
453
454                         val = woinst->format.channels - 1;
455
456                         spin_unlock_irqrestore(&woinst->lock, flags);
457
458                         DPD(2, "set playback stereo -> %d\n", val);
459                 }
460
461                 return put_user(val, p);
462
463                 break;
464
465         case SNDCTL_DSP_CHANNELS:
466                 DPF(2, "SNDCTL_DSP_CHANNELS:\n");
467
468                 if (get_user(val, p))
469                         return -EFAULT;
470
471                 DPD(2, " val is %d\n", val);
472
473                 if (val > 0) {
474                         if (file->f_mode & FMODE_READ) {
475                                 struct wave_format format;
476
477                                 spin_lock_irqsave(&wiinst->lock, flags);
478
479                                 format = wiinst->format;
480                                 format.channels = val;
481
482                                 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
483                                         spin_unlock_irqrestore(&wiinst->lock, flags);
484                                         return -EINVAL;
485                                 }
486                                 val = wiinst->format.channels;
487
488                                 spin_unlock_irqrestore(&wiinst->lock, flags);
489                                 DPD(2, "set recording number of channels -> %d\n", val);
490                         }
491
492                         if (file->f_mode & FMODE_WRITE) {
493                                 struct wave_format format;
494
495                                 spin_lock_irqsave(&woinst->lock, flags);
496
497                                 format = woinst->format;
498                                 format.channels = val;
499
500                                 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
501                                         spin_unlock_irqrestore(&woinst->lock, flags);
502                                         return -EINVAL;
503                                 }
504
505                                 val = woinst->format.channels;
506
507                                 spin_unlock_irqrestore(&woinst->lock, flags);
508                                 DPD(2, "set playback number of channels -> %d\n", val);
509                         }
510
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;
517
518                         return put_user(val, p);
519                 }
520                 break;
521
522         case SNDCTL_DSP_GETFMTS:
523                 DPF(2, "SNDCTL_DSP_GETFMTS:\n");
524
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;
533                 }
534                 return put_user(val, p);
535
536         case SNDCTL_DSP_SETFMT: /* Same as SNDCTL_DSP_SAMPLESIZE */
537                 DPF(2, "SNDCTL_DSP_SETFMT:\n");
538
539                 if (get_user(val, p))
540                         return -EFAULT;
541
542                 DPD(2, " val is %d\n", val);
543
544                 if (val != AFMT_QUERY) {
545                         if (file->f_mode & FMODE_READ) {
546                                 struct wave_format format;
547
548                                 spin_lock_irqsave(&wiinst->lock, flags);
549
550                                 format = wiinst->format;
551                                 format.id = val;
552
553                                 if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
554                                         spin_unlock_irqrestore(&wiinst->lock, flags);
555                                         return -EINVAL;
556                                 }
557
558                                 val = wiinst->format.id;
559
560                                 spin_unlock_irqrestore(&wiinst->lock, flags);
561                                 DPD(2, "set recording format -> %d\n", val);
562                         }
563
564                         if (file->f_mode & FMODE_WRITE) {
565                                 struct wave_format format;
566
567                                 spin_lock_irqsave(&woinst->lock, flags);
568
569                                 format = woinst->format;
570                                 format.id = val;
571
572                                 if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
573                                         spin_unlock_irqrestore(&woinst->lock, flags);
574                                         return -EINVAL;
575                                 }
576
577                                 val = woinst->format.id;
578
579                                 spin_unlock_irqrestore(&woinst->lock, flags);
580                                 DPD(2, "set playback format -> %d\n", val);
581                         }
582
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;
589
590                         return put_user(val, p);
591                 }
592                 break;
593
594         case SOUND_PCM_READ_BITS:
595
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;
600
601                 return put_user(val, p);
602
603         case SOUND_PCM_READ_RATE:
604
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;
609
610                 return put_user(val, p);
611
612         case SOUND_PCM_READ_CHANNELS:
613
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;
618
619                 return put_user(val, p);
620
621         case SOUND_PCM_WRITE_FILTER:
622                 DPF(2, "SOUND_PCM_WRITE_FILTER: not implemented\n");
623                 break;
624
625         case SOUND_PCM_READ_FILTER:
626                 DPF(2, "SOUND_PCM_READ_FILTER: not implemented\n");
627                 break;
628
629         case SNDCTL_DSP_SETSYNCRO:
630                 DPF(2, "SNDCTL_DSP_SETSYNCRO: not implemented\n");
631                 break;
632
633         case SNDCTL_DSP_GETTRIGGER:
634                 DPF(2, "SNDCTL_DSP_GETTRIGGER:\n");
635
636                 if (file->f_mode & FMODE_WRITE && (wave_dev->enablebits & PCM_ENABLE_OUTPUT))
637                         val |= PCM_ENABLE_OUTPUT;
638
639                 if (file->f_mode & FMODE_READ && (wave_dev->enablebits & PCM_ENABLE_INPUT))
640                         val |= PCM_ENABLE_INPUT;
641
642                 return put_user(val, p);
643
644         case SNDCTL_DSP_SETTRIGGER:
645                 DPF(2, "SNDCTL_DSP_SETTRIGGER:\n");
646
647                 if (get_user(val, p))
648                         return -EFAULT;
649
650                 if (file->f_mode & FMODE_WRITE) {
651                         spin_lock_irqsave(&woinst->lock, flags);
652
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);
661                         }
662
663                         spin_unlock_irqrestore(&woinst->lock, flags);
664                 }
665
666                 if (file->f_mode & FMODE_READ) {
667                         spin_lock_irqsave(&wiinst->lock, flags);
668
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);
677                         }
678
679                         spin_unlock_irqrestore(&wiinst->lock, flags);
680                 }
681                 break;
682
683         case SNDCTL_DSP_GETOSPACE:
684                 {
685                         audio_buf_info info;
686
687                         DPF(4, "SNDCTL_DSP_GETOSPACE:\n");
688
689                         if (!(file->f_mode & FMODE_WRITE))
690                                 return -EINVAL;
691
692                         spin_lock_irqsave(&woinst->lock, flags);
693
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;
701                         }
702                         spin_unlock_irqrestore(&woinst->lock, flags);
703
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;
708
709                         if (copy_to_user(p, &info, sizeof(info)))
710                                 return -EFAULT;
711                 }
712                 break;
713
714         case SNDCTL_DSP_GETISPACE:
715                 {
716                         audio_buf_info info;
717
718                         DPF(4, "SNDCTL_DSP_GETISPACE:\n");
719
720                         if (!(file->f_mode & FMODE_READ))
721                                 return -EINVAL;
722
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;
731                         }
732                         spin_unlock_irqrestore(&wiinst->lock, flags);
733
734                         info.fragstotal = wiinst->buffer.numfrags;
735                         info.fragments = info.bytes / wiinst->buffer.fragment_size;
736                         info.fragsize = wiinst->buffer.fragment_size;
737
738                         if (copy_to_user(p, &info, sizeof(info)))
739                                 return -EFAULT;
740                 }
741                 break;
742
743         case SNDCTL_DSP_NONBLOCK:
744                 DPF(2, "SNDCTL_DSP_NONBLOCK:\n");
745
746                 file->f_flags |= O_NONBLOCK;
747                 break;
748
749         case SNDCTL_DSP_GETODELAY:
750                 DPF(4, "SNDCTL_DSP_GETODELAY:\n");
751
752                 if (!(file->f_mode & FMODE_WRITE))
753                         return -EINVAL;
754
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;
762
763                 val *= woinst->num_voices;
764                 spin_unlock_irqrestore(&woinst->lock, flags);
765
766                 return put_user(val, p);
767
768         case SNDCTL_DSP_GETIPTR:
769                 {
770                         count_info cinfo;
771
772                         DPF(4, "SNDCTL_DSP_GETIPTR: \n");
773
774                         if (!(file->f_mode & FMODE_READ))
775                                 return -EINVAL;
776
777                         spin_lock_irqsave(&wiinst->lock, flags);
778
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;
789                         }
790
791                         if (wiinst->mmapped)
792                                 wiinst->buffer.bytestocopy %= wiinst->buffer.fragment_size;
793
794                         spin_unlock_irqrestore(&wiinst->lock, flags);
795
796                         if (copy_to_user(p, &cinfo, sizeof(cinfo)))
797                                 return -EFAULT;
798                 }
799                 break;
800
801         case SNDCTL_DSP_GETOPTR:
802                 {
803                         count_info cinfo;
804
805                         DPF(4, "SNDCTL_DSP_GETOPTR:\n");
806
807                         if (!(file->f_mode & FMODE_WRITE))
808                                 return -EINVAL;
809
810                         spin_lock_irqsave(&woinst->lock, flags);
811
812                         if (woinst->state & WAVE_STATE_OPEN || 
813                             ((woinst->format.passthrough == 1) && wave_dev->card->pt.state)) {
814                                 int num_fragments;
815
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;
822                                 }
823
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;
828
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;
835                         }
836
837                         if (woinst->mmapped)
838                                 woinst->buffer.free_bytes %= woinst->buffer.fragment_size;
839
840                         spin_unlock_irqrestore(&woinst->lock, flags);
841
842                         if (copy_to_user(p, &cinfo, sizeof(cinfo)))
843                                 return -EFAULT;
844                 }
845                 break;
846
847         case SNDCTL_DSP_GETBLKSIZE:
848                 DPF(2, "SNDCTL_DSP_GETBLKSIZE:\n");
849
850                 if (file->f_mode & FMODE_WRITE) {
851                         spin_lock_irqsave(&woinst->lock, flags);
852
853                         calculate_ofrag(woinst);
854                         val = woinst->buffer.fragment_size * woinst->num_voices;
855
856                         spin_unlock_irqrestore(&woinst->lock, flags);
857                 }
858
859                 if (file->f_mode & FMODE_READ) {
860                         spin_lock_irqsave(&wiinst->lock, flags);
861
862                         calculate_ifrag(wiinst);
863                         val = wiinst->buffer.fragment_size;
864
865                         spin_unlock_irqrestore(&wiinst->lock, flags);
866                 }
867
868                 return put_user(val, p);
869
870                 break;
871
872         case SNDCTL_DSP_POST:
873                 if (file->f_mode & FMODE_WRITE) {
874                         spin_lock_irqsave(&woinst->lock, flags);
875
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);
880
881                         spin_unlock_irqrestore(&woinst->lock, flags);
882                 }
883
884                 break;
885
886         case SNDCTL_DSP_SUBDIVIDE:
887                 DPF(2, "SNDCTL_DSP_SUBDIVIDE: not implemented\n");
888                 break;
889
890         case SNDCTL_DSP_SETFRAGMENT:
891                 DPF(2, "SNDCTL_DSP_SETFRAGMENT:\n");
892
893                 if (get_user(val, p))
894                         return -EFAULT;
895
896                 DPD(2, "val is %#x\n", val);
897
898                 if (val == 0)
899                         return -EIO;
900
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 */
905
906                         woinst->buffer.ossfragshift = val & 0xffff;
907                         woinst->buffer.numfrags = (val >> 16) & 0xffff;
908                 }
909
910                 if (file->f_mode & FMODE_READ) {
911                         if (wiinst->state & WAVE_STATE_OPEN)
912                                 return -EINVAL; /* too late to change */
913
914                         wiinst->buffer.ossfragshift = val & 0xffff;
915                         wiinst->buffer.numfrags = (val >> 16) & 0xffff;
916                 }
917
918                 break;
919
920         case SNDCTL_COPR_LOAD:
921                 {
922                         copr_buffer *buf;
923                         u32 i;
924
925                         DPF(4, "SNDCTL_COPR_LOAD:\n");
926
927                         buf = kmalloc(sizeof(copr_buffer), GFP_KERNEL);
928                         if (!buf)
929                                 return -ENOMEM;
930
931                         if (copy_from_user(buf, p, sizeof(copr_buffer))) {
932                                 kfree (buf);
933                                 return -EFAULT;
934                         }
935
936                         if ((buf->command != CMD_READ) && (buf->command != CMD_WRITE)) {
937                                 kfree (buf);
938                                 return -EINVAL;
939                         }
940
941                         if (buf->command == CMD_WRITE) {
942                                 
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;
955                                 }
956                         } else {
957                                 if ((buf->offs < 0) || (buf->offs + buf->len > 0xe00) || (buf->len > 1000)) {
958                                         kfree(buf);
959                                         return -EINVAL;
960                                 }
961                         }                       
962                                 
963                         if (((unsigned)buf->flags) > 0x3f)
964                                 buf->flags = 0;
965
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);
969
970                                 if (copy_to_user(p, buf, sizeof(copr_buffer))) {
971                                         kfree(buf);
972                                         return -EFAULT;
973                                 }
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]);
977                         }
978
979                         kfree (buf);
980                         break;
981                 }
982
983         default:                /* Default is unrecognized command */
984                 DPD(2, "default: %#x\n", cmd);
985                 return -EINVAL;
986         }
987         return 0;
988 }
989
990 static struct page *emu10k1_mm_nopage (struct vm_area_struct * vma, unsigned long address, int *type)
991 {
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;
998
999         DPF(3, "emu10k1_mm_nopage()\n");
1000         DPD(3, "addr: %#lx\n", address);
1001
1002         if (address > vma->vm_end) {
1003                 DPF(1, "EXIT, returning NOPAGE_SIGBUS\n");
1004                 return NOPAGE_SIGBUS; /* Disallow mremap */
1005         }
1006
1007         pgoff = vma->vm_pgoff + ((address - vma->vm_start) >> PAGE_SHIFT);
1008         if (woinst != NULL)
1009                 wr = woinst->mmapped;
1010         else
1011                 wr = 0;
1012
1013         if (wiinst != NULL)
1014                 rd = wiinst->mmapped;
1015         else
1016                 rd = 0;
1017
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);
1028         }
1029
1030         get_page (dmapage);
1031
1032         DPD(3, "page: %#lx\n", (unsigned long) dmapage);
1033         if (type)
1034                 *type = VM_FAULT_MINOR;
1035         return dmapage;
1036 }
1037
1038 static struct vm_operations_struct emu10k1_mm_ops = {
1039         .nopage         = emu10k1_mm_nopage,
1040 };
1041
1042 static int emu10k1_audio_mmap(struct file *file, struct vm_area_struct *vma)
1043 {
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;
1049
1050         DPF(2, "emu10k1_audio_mmap()\n");
1051
1052         max_pages = 0;
1053         if (vma->vm_flags & VM_WRITE) {
1054                 woinst = wave_dev->woinst;
1055
1056                 spin_lock_irqsave(&woinst->lock, flags);
1057
1058                 /* No m'mapping possible for multichannel */
1059                 if (woinst->num_voices > 1) {
1060                         spin_unlock_irqrestore(&woinst->lock, flags);
1061                         return -EINVAL;
1062                 }
1063
1064                 if (woinst->state == WAVE_STATE_CLOSED) {
1065                         calculate_ofrag(woinst);
1066
1067                         if (emu10k1_waveout_open(wave_dev) < 0) {
1068                                 spin_unlock_irqrestore(&woinst->lock, flags);
1069                                 ERROR();
1070                                 return -EINVAL;
1071                         }
1072                 }
1073
1074                 woinst->mmapped = 1;
1075                 max_pages += woinst->buffer.pages;
1076                 spin_unlock_irqrestore(&woinst->lock, flags);
1077         }
1078
1079         if (vma->vm_flags & VM_READ) {
1080                 wiinst = wave_dev->wiinst;
1081
1082                 spin_lock_irqsave(&wiinst->lock, flags);
1083                 if (wiinst->state == WAVE_STATE_CLOSED) {
1084                         calculate_ifrag(wiinst);
1085
1086                         if (emu10k1_wavein_open(wave_dev) < 0) {
1087                                 spin_unlock_irqrestore(&wiinst->lock, flags);
1088                                 ERROR();
1089                                 return -EINVAL;
1090                         }
1091                 }
1092
1093                 wiinst->mmapped = 1;
1094                 max_pages += wiinst->buffer.pages;
1095                 spin_unlock_irqrestore(&wiinst->lock, flags);
1096         }
1097
1098         n_pages = ((vma->vm_end - vma->vm_start) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1099         pgoffset = vma->vm_pgoff;
1100
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);
1103
1104         if (pgoffset + n_pages > max_pages)
1105                 return -EINVAL;
1106
1107         vma->vm_flags |= VM_RESERVED;
1108         vma->vm_ops = &emu10k1_mm_ops;
1109         vma->vm_private_data = wave_dev;
1110         return 0;
1111 }
1112
1113 static int emu10k1_audio_open(struct inode *inode, struct file *file)
1114 {
1115         int minor = iminor(inode);
1116         struct emu10k1_card *card = NULL;
1117         struct list_head *entry;
1118         struct emu10k1_wavedevice *wave_dev;
1119
1120         DPF(2, "emu10k1_audio_open()\n");
1121
1122         /* Check for correct device to open */
1123
1124         list_for_each(entry, &emu10k1_devs) {
1125                 card = list_entry(entry, struct emu10k1_card, list);
1126
1127                 if (!((card->audio_dev ^ minor) & ~0xf) || !((card->audio_dev1 ^ minor) & ~0xf))
1128                         goto match;
1129         }
1130
1131         return -ENODEV;
1132
1133 match:
1134
1135         wave_dev = (struct emu10k1_wavedevice *) kmalloc(sizeof(struct emu10k1_wavedevice), GFP_KERNEL);
1136
1137         if (wave_dev == NULL) { 
1138                 ERROR();
1139                 return -ENOMEM;
1140         }
1141
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 */
1146
1147         if (file->f_mode & FMODE_READ) {
1148                 /* Recording */
1149                 struct wiinst *wiinst;
1150
1151                 if ((wiinst = (struct wiinst *) kmalloc(sizeof(struct wiinst), GFP_KERNEL)) == NULL) {
1152                         ERROR();
1153                         kfree(wave_dev);
1154                         return -ENOMEM;
1155                 }
1156
1157                 wiinst->recsrc = card->wavein.recsrc;
1158                 wiinst->fxwc = card->wavein.fxwc;
1159
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;
1184                 }
1185
1186                 wiinst->state = WAVE_STATE_CLOSED;
1187
1188                 wiinst->buffer.ossfragshift = 0;
1189                 wiinst->buffer.fragment_size = 0;
1190                 wiinst->buffer.numfrags = 0;
1191
1192                 init_waitqueue_head(&wiinst->wait_queue);
1193
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);
1201         }
1202
1203         if (file->f_mode & FMODE_WRITE) {
1204                 struct woinst *woinst;
1205                 int i;
1206
1207                 if ((woinst = (struct woinst *) kmalloc(sizeof(struct woinst), GFP_KERNEL)) == NULL) {
1208                         ERROR();
1209                         kfree(wave_dev);
1210                         return -ENOMEM;
1211                 }
1212
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;
1220                 }
1221
1222                 woinst->state = WAVE_STATE_CLOSED;
1223
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;
1233                 }
1234
1235                 init_waitqueue_head(&woinst->wait_queue);
1236
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);
1245         }
1246
1247         file->private_data = (void *) wave_dev;
1248
1249         return nonseekable_open(inode, file);
1250 }
1251
1252 static int emu10k1_audio_release(struct inode *inode, struct file *file)
1253 {
1254         struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
1255         struct emu10k1_card *card;
1256         unsigned long flags;
1257
1258         card = wave_dev->card;
1259
1260         DPF(2, "emu10k1_audio_release()\n");
1261
1262         if (file->f_mode & FMODE_WRITE) {
1263                 struct woinst *woinst = wave_dev->woinst;
1264
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);
1272                 }
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);
1282                                         }
1283                                 }
1284                         }
1285                         emu10k1_waveout_close(wave_dev);
1286                 }
1287
1288                 spin_unlock_irqrestore(&woinst->lock, flags);
1289                 /* remove the tasklet */
1290                 tasklet_kill(&woinst->timer.tasklet);
1291                 kfree(wave_dev->woinst);
1292         }
1293
1294         if (file->f_mode & FMODE_READ) {
1295                 struct wiinst *wiinst = wave_dev->wiinst;
1296
1297                 spin_lock_irqsave(&wiinst->lock, flags);
1298
1299                 if (wiinst->state & WAVE_STATE_OPEN) {
1300                         emu10k1_wavein_close(wave_dev);
1301                 }
1302
1303                 spin_unlock_irqrestore(&wiinst->lock, flags);
1304                 tasklet_kill(&wiinst->timer.tasklet);
1305                 kfree(wave_dev->wiinst);
1306         }
1307
1308         kfree(wave_dev);
1309
1310         if (waitqueue_active(&card->open_wait))
1311                 wake_up_interruptible(&card->open_wait);
1312
1313         return 0;
1314 }
1315
1316 /* FIXME sort out poll() + mmap() */
1317 static unsigned int emu10k1_audio_poll(struct file *file, struct poll_table_struct *wait)
1318 {
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;
1325
1326         DPF(4, "emu10k1_audio_poll()\n");
1327
1328         if (file->f_mode & FMODE_WRITE)
1329                 poll_wait(file, &woinst->wait_queue, wait);
1330
1331         if (file->f_mode & FMODE_READ)
1332                 poll_wait(file, &wiinst->wait_queue, wait);
1333
1334         if (file->f_mode & FMODE_WRITE) {
1335                 spin_lock_irqsave(&woinst->lock, flags);
1336
1337                 if (woinst->state & WAVE_STATE_OPEN) {
1338                         emu10k1_waveout_update(woinst);
1339                         emu10k1_waveout_getxfersize(woinst, &bytestocopy);
1340
1341                         if (bytestocopy >= woinst->buffer.fragment_size)
1342                                 mask |= POLLOUT | POLLWRNORM;
1343                 } else
1344                         mask |= POLLOUT | POLLWRNORM;
1345
1346                 spin_unlock_irqrestore(&woinst->lock, flags);
1347         }
1348
1349         if (file->f_mode & FMODE_READ) {
1350                 spin_lock_irqsave(&wiinst->lock, flags);
1351
1352                 if (wiinst->state & WAVE_STATE_OPEN) {
1353                         emu10k1_wavein_update(wave_dev->card, wiinst);
1354                         emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
1355
1356                         if (bytestocopy >= wiinst->buffer.fragment_size)
1357                                 mask |= POLLIN | POLLRDNORM;
1358                 }
1359
1360                 spin_unlock_irqrestore(&wiinst->lock, flags);
1361         }
1362
1363         return mask;
1364 }
1365
1366 static void calculate_ofrag(struct woinst *woinst)
1367 {
1368         struct waveout_buffer *buffer = &woinst->buffer;
1369         u32 fragsize;
1370
1371         if (buffer->fragment_size)
1372                 return;
1373
1374         if (!buffer->ossfragshift) {
1375                 fragsize = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTFRAGLEN) / 1000 - 1;
1376
1377                 while (fragsize) {
1378                         fragsize >>= 1;
1379                         buffer->ossfragshift++;
1380                 }
1381         }
1382
1383         if (buffer->ossfragshift < WAVEOUT_MINFRAGSHIFT)
1384                 buffer->ossfragshift = WAVEOUT_MINFRAGSHIFT;
1385
1386         buffer->fragment_size = 1 << buffer->ossfragshift;
1387
1388         while (buffer->fragment_size * WAVEOUT_MINFRAGS > WAVEOUT_MAXBUFSIZE)
1389                 buffer->fragment_size >>= 1;
1390
1391         /* now we are sure that:
1392          (2^WAVEOUT_MINFRAGSHIFT) <= (fragment_size = 2^n) <= (WAVEOUT_MAXBUFSIZE / WAVEOUT_MINFRAGS)
1393         */
1394
1395         if (!buffer->numfrags) {
1396                 u32 numfrags;
1397
1398                 numfrags = (woinst->format.bytespervoicesample * woinst->format.samplingrate * WAVEOUT_DEFAULTBUFLEN) /
1399                            (buffer->fragment_size * 1000) - 1;
1400
1401                 buffer->numfrags = 1;
1402
1403                 while (numfrags) {
1404                         numfrags >>= 1;
1405                         buffer->numfrags <<= 1;
1406                 }
1407         }
1408
1409         if (buffer->numfrags < WAVEOUT_MINFRAGS)
1410                 buffer->numfrags = WAVEOUT_MINFRAGS;
1411
1412         if (buffer->numfrags * buffer->fragment_size > WAVEOUT_MAXBUFSIZE)
1413                 buffer->numfrags = WAVEOUT_MAXBUFSIZE / buffer->fragment_size;
1414
1415         if (buffer->numfrags < WAVEOUT_MINFRAGS)
1416                 BUG();
1417
1418         buffer->size = buffer->fragment_size * buffer->numfrags;
1419         buffer->pages = buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
1420
1421         DPD(2, " calculated playback fragment_size -> %d\n", buffer->fragment_size);
1422         DPD(2, " calculated playback numfrags -> %d\n", buffer->numfrags);
1423
1424         return;
1425 }
1426
1427 static void calculate_ifrag(struct wiinst *wiinst)
1428 {
1429         struct wavein_buffer *buffer = &wiinst->buffer;
1430         u32 fragsize, bufsize, size[4];
1431         int i, j;
1432
1433         if (buffer->fragment_size)
1434                 return;
1435
1436         if (!buffer->ossfragshift) {
1437                 fragsize = (wiinst->format.bytespersec * WAVEIN_DEFAULTFRAGLEN) / 1000 - 1;
1438
1439                 while (fragsize) {
1440                         fragsize >>= 1;
1441                         buffer->ossfragshift++;
1442                 }
1443         }
1444
1445         if (buffer->ossfragshift < WAVEIN_MINFRAGSHIFT)
1446                 buffer->ossfragshift = WAVEIN_MINFRAGSHIFT;
1447
1448         buffer->fragment_size = 1 << buffer->ossfragshift;
1449
1450         while (buffer->fragment_size * WAVEIN_MINFRAGS > WAVEIN_MAXBUFSIZE)
1451                 buffer->fragment_size >>= 1;
1452
1453         /* now we are sure that:
1454            (2^WAVEIN_MINFRAGSHIFT) <= (fragment_size = 2^n) <= (WAVEIN_MAXBUFSIZE / WAVEIN_MINFRAGS)
1455         */
1456
1457
1458         if (!buffer->numfrags)
1459                 buffer->numfrags = (wiinst->format.bytespersec * WAVEIN_DEFAULTBUFLEN) / (buffer->fragment_size * 1000) - 1;
1460
1461         if (buffer->numfrags < WAVEIN_MINFRAGS)
1462                 buffer->numfrags = WAVEIN_MINFRAGS;
1463
1464         if (buffer->numfrags * buffer->fragment_size > WAVEIN_MAXBUFSIZE)
1465                 buffer->numfrags = WAVEIN_MAXBUFSIZE / buffer->fragment_size;
1466
1467         if (buffer->numfrags < WAVEIN_MINFRAGS)
1468                 BUG();
1469
1470         bufsize = buffer->fragment_size * buffer->numfrags;
1471
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;
1482
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;
1495                 }
1496         }
1497
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;
1501
1502         buffer->numfrags = buffer->size / buffer->fragment_size;
1503         buffer->pages =  buffer->size / PAGE_SIZE + ((buffer->size % PAGE_SIZE) ? 1 : 0);
1504
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);
1508
1509         return;
1510 }
1511
1512 static void emu10k1_wavein_bh(unsigned long refdata)
1513 {
1514         struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) refdata;
1515         struct wiinst *wiinst = wave_dev->wiinst;
1516         u32 bytestocopy;
1517         unsigned long flags;
1518
1519         if (!wiinst)
1520                 return;
1521
1522         spin_lock_irqsave(&wiinst->lock, flags);
1523
1524         if (!(wiinst->state & WAVE_STATE_STARTED)) {
1525                 spin_unlock_irqrestore(&wiinst->lock, flags);
1526                 return;
1527         }
1528
1529         emu10k1_wavein_update(wave_dev->card, wiinst);
1530         emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
1531
1532         spin_unlock_irqrestore(&wiinst->lock, flags);
1533
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);
1539
1540         return;
1541 }
1542
1543 static void emu10k1_waveout_bh(unsigned long refdata)
1544 {
1545         struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) refdata;
1546         struct woinst *woinst = wave_dev->woinst;
1547         u32 bytestocopy;
1548         unsigned long flags;
1549
1550         if (!woinst)
1551                 return;
1552
1553         spin_lock_irqsave(&woinst->lock, flags);
1554
1555         if (!(woinst->state & WAVE_STATE_STARTED)) {
1556                 spin_unlock_irqrestore(&woinst->lock, flags);
1557                 return;
1558         }
1559
1560         emu10k1_waveout_update(woinst);
1561         emu10k1_waveout_getxfersize(woinst, &bytestocopy);
1562
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);
1568
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);
1574
1575         return;
1576 }
1577
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,
1588 };