vserver 1.9.3
[linux-2.6.git] / sound / oss / forte.c
1 /*
2  * forte.c - ForteMedia FM801 OSS Driver
3  *
4  * Written by Martin K. Petersen <mkp@mkp.net>
5  * Copyright (C) 2002 Hewlett-Packard Company
6  * Portions Copyright (C) 2003 Martin K. Petersen
7  *
8  * Latest version: http://mkp.net/forte/
9  *
10  * Based upon the ALSA FM801 driver by Jaroslav Kysela and OSS drivers
11  * by Thomas Sailer, Alan Cox, Zach Brown, and Jeff Garzik.  Thanks
12  * guys!
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License version
16  * 2 as published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26  * USA
27  *
28  */
29  
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33
34 #include <linux/init.h>
35 #include <linux/spinlock.h>
36 #include <linux/pci.h>
37
38 #include <linux/delay.h>
39 #include <linux/poll.h>
40
41 #include <linux/sound.h>
42 #include <linux/ac97_codec.h>
43 #include <linux/interrupt.h>
44
45 #include <linux/proc_fs.h>
46
47 #include <asm/uaccess.h>
48 #include <asm/io.h>
49
50 #define DRIVER_NAME     "forte"
51 #define DRIVER_VERSION  "$Id: forte.c,v 1.63 2003/03/01 05:32:42 mkp Exp $"
52 #define PFX             DRIVER_NAME ": "
53
54 #undef M_DEBUG
55
56 #ifdef M_DEBUG
57 #define DPRINTK(args...) printk(KERN_WARNING args)
58 #else
59 #define DPRINTK(args...)
60 #endif
61
62 /* Card capabilities */
63 #define FORTE_CAPS              (DSP_CAP_MMAP | DSP_CAP_TRIGGER)
64
65 /* Supported audio formats */
66 #define FORTE_FMTS              (AFMT_U8 | AFMT_S16_LE)
67
68 /* Buffers */
69 #define FORTE_MIN_FRAG_SIZE     256
70 #define FORTE_MAX_FRAG_SIZE     PAGE_SIZE
71 #define FORTE_DEF_FRAG_SIZE     256
72 #define FORTE_MIN_FRAGMENTS     2
73 #define FORTE_MAX_FRAGMENTS     256
74 #define FORTE_DEF_FRAGMENTS     2
75 #define FORTE_MIN_BUF_MSECS     500
76 #define FORTE_MAX_BUF_MSECS     1000
77
78 /* PCI BARs */
79 #define FORTE_PCM_VOL           0x00    /* PCM Output Volume */
80 #define FORTE_FM_VOL            0x02    /* FM Output Volume */
81 #define FORTE_I2S_VOL           0x04    /* I2S Volume */
82 #define FORTE_REC_SRC           0x06    /* Record Source */
83 #define FORTE_PLY_CTRL          0x08    /* Playback Control */
84 #define FORTE_PLY_COUNT         0x0a    /* Playback Count */
85 #define FORTE_PLY_BUF1          0x0c    /* Playback Buffer I */
86 #define FORTE_PLY_BUF2          0x10    /* Playback Buffer II */
87 #define FORTE_CAP_CTRL          0x14    /* Capture Control */
88 #define FORTE_CAP_COUNT         0x16    /* Capture Count */
89 #define FORTE_CAP_BUF1          0x18    /* Capture Buffer I */
90 #define FORTE_CAP_BUF2          0x1c    /* Capture Buffer II */
91 #define FORTE_CODEC_CTRL        0x22    /* Codec Control */
92 #define FORTE_I2S_MODE          0x24    /* I2S Mode Control */
93 #define FORTE_VOLUME            0x26    /* Volume Up/Down/Mute Status */
94 #define FORTE_I2C_CTRL          0x29    /* I2C Control */
95 #define FORTE_AC97_CMD          0x2a    /* AC'97 Command */
96 #define FORTE_AC97_DATA         0x2c    /* AC'97 Data */
97 #define FORTE_MPU401_DATA       0x30    /* MPU401 Data */
98 #define FORTE_MPU401_CMD        0x31    /* MPU401 Command */
99 #define FORTE_GPIO_CTRL         0x52    /* General Purpose I/O Control */
100 #define FORTE_GEN_CTRL          0x54    /* General Control */
101 #define FORTE_IRQ_MASK          0x56    /* Interrupt Mask */
102 #define FORTE_IRQ_STATUS        0x5a    /* Interrupt Status */
103 #define FORTE_OPL3_BANK0        0x68    /* OPL3 Status Read / Bank 0 Write */
104 #define FORTE_OPL3_DATA0        0x69    /* OPL3 Data 0 Write */
105 #define FORTE_OPL3_BANK1        0x6a    /* OPL3 Bank 1 Write */
106 #define FORTE_OPL3_DATA1        0x6b    /* OPL3 Bank 1 Write */
107 #define FORTE_POWERDOWN         0x70    /* Blocks Power Down Control */
108
109 #define FORTE_CAP_OFFSET        FORTE_CAP_CTRL - FORTE_PLY_CTRL
110
111 #define FORTE_AC97_ADDR_SHIFT   10
112
113 /* Playback and record control register bits */
114 #define FORTE_BUF1_LAST         (1<<1)
115 #define FORTE_BUF2_LAST         (1<<2)
116 #define FORTE_START             (1<<5)
117 #define FORTE_PAUSE             (1<<6)
118 #define FORTE_IMMED_STOP        (1<<7)
119 #define FORTE_RATE_SHIFT        8
120 #define FORTE_RATE_MASK         (15 << FORTE_RATE_SHIFT)
121 #define FORTE_CHANNELS_4        (1<<12) /* Playback only */
122 #define FORTE_CHANNELS_6        (2<<12) /* Playback only */
123 #define FORTE_CHANNELS_6MS      (3<<12) /* Playback only */
124 #define FORTE_CHANNELS_MASK     (3<<12)
125 #define FORTE_16BIT             (1<<14)
126 #define FORTE_STEREO            (1<<15)
127
128 /* IRQ status bits */
129 #define FORTE_IRQ_PLAYBACK      (1<<8)
130 #define FORTE_IRQ_CAPTURE       (1<<9)
131 #define FORTE_IRQ_VOLUME        (1<<14)
132 #define FORTE_IRQ_MPU           (1<<15)
133
134 /* CODEC control */
135 #define FORTE_CC_CODEC_RESET    (1<<5)
136 #define FORTE_CC_AC97_RESET     (1<<6)
137
138 /* AC97 cmd */
139 #define FORTE_AC97_WRITE        (0<<7)
140 #define FORTE_AC97_READ         (1<<7)
141 #define FORTE_AC97_DP_INVALID   (0<<8)
142 #define FORTE_AC97_DP_VALID     (1<<8)
143 #define FORTE_AC97_PORT_RDY     (0<<9)
144 #define FORTE_AC97_PORT_BSY     (1<<9)
145
146
147 struct forte_channel {
148         const char              *name;
149
150         unsigned short          ctrl;           /* Ctrl BAR contents */
151         unsigned long           iobase;         /* Ctrl BAR address */
152
153         wait_queue_head_t       wait;
154
155         void                    *buf;           /* Buffer */
156         dma_addr_t              buf_handle;     /* Buffer handle */
157
158         unsigned int            record;
159         unsigned int            format;
160         unsigned int            rate;
161         unsigned int            stereo;
162
163         unsigned int            frag_sz;        /* Current fragment size */
164         unsigned int            frag_num;       /* Current # of fragments */
165         unsigned int            frag_msecs;     /* Milliseconds per frag */
166         unsigned int            buf_sz;         /* Current buffer size */
167
168         unsigned int            hwptr;          /* Tail */
169         unsigned int            swptr;          /* Head */
170         unsigned int            filled_frags;   /* Fragments currently full */
171         unsigned int            next_buf;       /* Index of next buffer */
172
173         unsigned int            active;         /* Channel currently in use */
174         unsigned int            mapped;         /* mmap */
175
176         unsigned int            buf_pages;      /* Real size of buffer */
177         unsigned int            nr_irqs;        /* Number of interrupts */
178         unsigned int            bytes;          /* Total bytes */
179         unsigned int            residue;        /* Partial fragment */
180 };
181
182
183 struct forte_chip {
184         struct pci_dev          *pci_dev;
185         unsigned long           iobase;
186         int                     irq;
187
188         struct semaphore        open_sem;       /* Device access */
189         spinlock_t              lock;           /* State */
190
191         spinlock_t              ac97_lock;
192         struct ac97_codec       *ac97;
193
194         int                     multichannel;
195         int                     dsp;            /* OSS handle */
196         int                     trigger;        /* mmap I/O trigger */
197
198         struct forte_channel    play;
199         struct forte_channel    rec;
200 };
201
202
203 static int channels[] = { 2, 4, 6, };
204 static int rates[]    = { 5500, 8000, 9600, 11025, 16000, 19200, 
205                           22050, 32000, 38400, 44100, 48000, };
206
207 static struct forte_chip *forte;
208 static int found;
209
210
211 /* AC97 Codec -------------------------------------------------------------- */
212
213
214 /** 
215  * forte_ac97_wait:
216  * @chip:       fm801 instance whose AC97 codec to wait on
217  *
218  * FIXME:
219  *              Stop busy-waiting
220  */
221
222 static inline int
223 forte_ac97_wait (struct forte_chip *chip)
224 {
225         int i = 10000;
226
227         while ( (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_PORT_BSY) 
228                 && i-- )
229                 cpu_relax();
230
231         return i == 0;
232 }
233
234
235 /**
236  * forte_ac97_read:
237  * @codec:      AC97 codec to read from
238  * @reg:        register to read
239  */
240
241 u16
242 forte_ac97_read (struct ac97_codec *codec, u8 reg)
243 {
244         u16 ret = 0;
245         struct forte_chip *chip = codec->private_data;
246
247         spin_lock (&chip->ac97_lock);
248
249         /* Knock, knock */
250         if (forte_ac97_wait (chip)) {
251                 printk (KERN_ERR PFX "ac97_read: Serial bus busy\n");
252                 goto out;
253         }
254
255         /* Send read command */
256         outw (reg | (1<<7), chip->iobase + FORTE_AC97_CMD);
257
258         if (forte_ac97_wait (chip)) {
259                 printk (KERN_ERR PFX "ac97_read: Bus busy reading reg 0x%x\n",
260                         reg);
261                 goto out;
262         }
263         
264         /* Sanity checking */
265         if (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_DP_INVALID) {
266                 printk (KERN_ERR PFX "ac97_read: Invalid data port");
267                 goto out;
268         }
269
270         /* Fetch result */
271         ret = inw (chip->iobase + FORTE_AC97_DATA);
272
273  out:
274         spin_unlock (&chip->ac97_lock);
275         return ret;
276 }
277
278
279 /**
280  * forte_ac97_write:
281  * @codec:      AC97 codec to send command to
282  * @reg:        register to write
283  * @val:        value to write
284  */
285
286 void
287 forte_ac97_write (struct ac97_codec *codec, u8 reg, u16 val)
288 {
289         struct forte_chip *chip = codec->private_data;
290
291         spin_lock (&chip->ac97_lock);
292
293         /* Knock, knock */
294         if (forte_ac97_wait (chip)) {
295                 printk (KERN_ERR PFX "ac97_write: Serial bus busy\n");
296                 goto out;
297         }
298
299         outw (val, chip->iobase + FORTE_AC97_DATA);
300         outb (reg | FORTE_AC97_WRITE, chip->iobase + FORTE_AC97_CMD);
301
302         /* Wait for completion */
303         if (forte_ac97_wait (chip)) {
304                 printk (KERN_ERR PFX "ac97_write: Bus busy after write\n");
305                 goto out;
306         }
307
308  out:
309         spin_unlock (&chip->ac97_lock);
310 }
311
312
313 /* Mixer ------------------------------------------------------------------- */
314
315
316 /**
317  * forte_mixer_open:
318  * @inode:              
319  * @file:               
320  */
321
322 static int
323 forte_mixer_open (struct inode *inode, struct file *file)
324 {
325         struct forte_chip *chip = forte;
326         file->private_data = chip->ac97;
327         return 0;
328 }
329
330
331 /**
332  * forte_mixer_release:
333  * @inode:              
334  * @file:               
335  */
336
337 static int
338 forte_mixer_release (struct inode *inode, struct file *file)
339 {
340         /* We will welease Wodewick */
341         return 0;
342 }
343
344
345 /**
346  * forte_mixer_ioctl:
347  * @inode:              
348  * @file:               
349  */
350
351 static int
352 forte_mixer_ioctl (struct inode *inode, struct file *file, 
353                    unsigned int cmd, unsigned long arg)
354 {
355         struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
356
357         return codec->mixer_ioctl (codec, cmd, arg);
358 }
359
360
361 static struct file_operations forte_mixer_fops = {
362         .owner                  = THIS_MODULE,
363         .llseek                 = no_llseek,
364         .ioctl                  = forte_mixer_ioctl,
365         .open                   = forte_mixer_open,
366         .release                = forte_mixer_release,
367 };
368
369
370 /* Channel ----------------------------------------------------------------- */
371
372 /** 
373  * forte_channel_reset:
374  * @channel:    Channel to reset
375  * 
376  * Locking:     Must be called with lock held.
377  */
378
379 static void
380 forte_channel_reset (struct forte_channel *channel)
381 {
382         if (!channel || !channel->iobase)
383                 return;
384
385         DPRINTK ("%s: channel = %s\n", __FUNCTION__, channel->name);
386
387         channel->ctrl &= ~FORTE_START;
388         outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
389         
390         /* We always play at least two fragments, hence these defaults */
391         channel->hwptr = channel->frag_sz;
392         channel->next_buf = 1;
393         channel->swptr = 0;
394         channel->filled_frags = 0;
395         channel->active = 0;
396         channel->bytes = 0;
397         channel->nr_irqs = 0;
398         channel->mapped = 0;
399         channel->residue = 0;
400 }
401
402
403 /** 
404  * forte_channel_start:
405  * @channel:    Channel to start (record/playback)
406  *
407  * Locking:     Must be called with lock held.
408  */
409
410 static void inline
411 forte_channel_start (struct forte_channel *channel)
412 {
413         if (!channel || !channel->iobase || channel->active) 
414                 return;
415
416         channel->ctrl &= ~(FORTE_PAUSE | FORTE_BUF1_LAST | FORTE_BUF2_LAST
417                            | FORTE_IMMED_STOP);
418         channel->ctrl |= FORTE_START;
419         channel->active = 1;
420         outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
421 }
422
423
424 /** 
425  * forte_channel_stop:
426  * @channel:    Channel to stop
427  *
428  * Locking:     Must be called with lock held.
429  */
430
431 static void inline
432 forte_channel_stop (struct forte_channel *channel)
433 {
434         if (!channel || !channel->iobase) 
435                 return;
436
437         channel->ctrl &= ~(FORTE_START | FORTE_PAUSE);  
438         channel->ctrl |= FORTE_IMMED_STOP;
439
440         channel->active = 0;
441         outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
442 }
443
444
445 /** 
446  * forte_channel_pause:
447  * @channel:    Channel to pause
448  *
449  * Locking:     Must be called with lock held.
450  */
451
452 static void inline
453 forte_channel_pause (struct forte_channel *channel)
454 {
455         if (!channel || !channel->iobase) 
456                 return;
457
458         channel->ctrl |= FORTE_PAUSE;
459
460         channel->active = 0;
461         outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
462 }
463
464
465 /** 
466  * forte_channel_rate:
467  * @channel:    Channel whose rate to set.  Playback and record are
468  *              independent.
469  * @rate:       Channel rate in Hz
470  *
471  * Locking:     Must be called with lock held.
472  */
473
474 static int
475 forte_channel_rate (struct forte_channel *channel, unsigned int rate)
476 {
477         int new_rate;
478
479         if (!channel || !channel->iobase) 
480                 return -EINVAL;
481
482         /* The FM801 only supports a handful of fixed frequencies.
483          * We find the value closest to what userland requested.
484          */
485         if      (rate <= 6250)  { rate = 5500;  new_rate =  0; }
486         else if (rate <= 8800)  { rate = 8000;  new_rate =  1; }
487         else if (rate <= 10312) { rate = 9600;  new_rate =  2; }
488         else if (rate <= 13512) { rate = 11025; new_rate =  3; }
489         else if (rate <= 17600) { rate = 16000; new_rate =  4; }
490         else if (rate <= 20625) { rate = 19200; new_rate =  5; }
491         else if (rate <= 27025) { rate = 22050; new_rate =  6; }
492         else if (rate <= 35200) { rate = 32000; new_rate =  7; }
493         else if (rate <= 41250) { rate = 38400; new_rate =  8; }
494         else if (rate <= 46050) { rate = 44100; new_rate =  9; }
495         else                    { rate = 48000; new_rate = 10; }
496
497         channel->ctrl &= ~FORTE_RATE_MASK;
498         channel->ctrl |= new_rate << FORTE_RATE_SHIFT;
499         channel->rate = rate;
500
501         DPRINTK ("%s: %s rate = %d\n", __FUNCTION__, channel->name, rate);
502
503         return rate;
504 }
505
506
507 /** 
508  * forte_channel_format:
509  * @channel:    Channel whose audio format to set
510  * @format:     OSS format ID
511  *
512  * Locking:     Must be called with lock held.
513  */
514
515 static int
516 forte_channel_format (struct forte_channel *channel, int format)
517 {
518         if (!channel || !channel->iobase) 
519                 return -EINVAL;
520
521         switch (format) {
522
523         case AFMT_QUERY:
524                 break;
525         
526         case AFMT_U8:
527                 channel->ctrl &= ~FORTE_16BIT;
528                 channel->format = AFMT_U8;
529                 break;
530
531         case AFMT_S16_LE:
532         default:
533                 channel->ctrl |= FORTE_16BIT;
534                 channel->format = AFMT_S16_LE;
535                 break;
536         }
537
538         DPRINTK ("%s: %s want %d format, got %d\n", __FUNCTION__, channel->name, 
539                  format, channel->format);
540
541         return channel->format;
542 }
543
544
545 /** 
546  * forte_channel_stereo:
547  * @channel:    Channel to toggle
548  * @stereo:     0 for Mono, 1 for Stereo
549  *
550  * Locking:     Must be called with lock held.
551  */
552
553 static int
554 forte_channel_stereo (struct forte_channel *channel, unsigned int stereo)
555 {
556         int ret;
557
558         if (!channel || !channel->iobase)
559                 return -EINVAL;
560
561         DPRINTK ("%s: %s stereo = %d\n", __FUNCTION__, channel->name, stereo);
562
563         switch (stereo) {
564
565         case 0:
566                 channel->ctrl &= ~(FORTE_STEREO | FORTE_CHANNELS_MASK);
567                 channel-> stereo = stereo;
568                 ret = stereo;
569                 break;
570
571         case 1:
572                 channel->ctrl &= ~FORTE_CHANNELS_MASK;
573                 channel->ctrl |= FORTE_STEREO;
574                 channel-> stereo = stereo;
575                 ret = stereo;
576                 break;
577
578         default:
579                 DPRINTK ("Unsupported channel format");
580                 ret = -EINVAL;
581                 break;
582         }
583
584         return ret;
585 }
586
587
588 /** 
589  * forte_channel_buffer:
590  * @channel:    Channel whose buffer to set up
591  *
592  * Locking:     Must be called with lock held.
593  */
594
595 static void
596 forte_channel_buffer (struct forte_channel *channel, int sz, int num)
597 {
598         unsigned int msecs, shift;
599
600         /* Go away, I'm busy */
601         if (channel->filled_frags || channel->bytes)
602                 return;
603
604         /* Fragment size must be a power of 2 */
605         shift = 0; sz++;
606         while (sz >>= 1)
607                 shift++;
608         channel->frag_sz = 1 << shift;
609
610         /* Round fragment size to something reasonable */
611         if (channel->frag_sz < FORTE_MIN_FRAG_SIZE)
612                 channel->frag_sz = FORTE_MIN_FRAG_SIZE;
613
614         if (channel->frag_sz > FORTE_MAX_FRAG_SIZE)
615                 channel->frag_sz = FORTE_MAX_FRAG_SIZE;
616
617         /* Find fragment length in milliseconds */
618         msecs = channel->frag_sz /
619                 (channel->format == AFMT_S16_LE ? 2 : 1) /
620                 (channel->stereo ? 2 : 1) /
621                 (channel->rate / 1000);
622
623         channel->frag_msecs = msecs;
624
625         /* Pick a suitable number of fragments */
626         if (msecs * num < FORTE_MIN_BUF_MSECS)
627              num = FORTE_MIN_BUF_MSECS / msecs;
628
629         if (msecs * num > FORTE_MAX_BUF_MSECS)
630              num = FORTE_MAX_BUF_MSECS / msecs;
631
632         /* Fragment number must be a power of 2 */
633         shift = 0;      
634         while (num >>= 1)
635                 shift++;
636         channel->frag_num = 1 << (shift + 1);
637
638         /* Round fragment number to something reasonable */
639         if (channel->frag_num < FORTE_MIN_FRAGMENTS)
640                 channel->frag_num = FORTE_MIN_FRAGMENTS;
641
642         if (channel->frag_num > FORTE_MAX_FRAGMENTS)
643                 channel->frag_num = FORTE_MAX_FRAGMENTS;
644
645         channel->buf_sz = channel->frag_sz * channel->frag_num;
646
647         DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d\n",
648                  __FUNCTION__, channel->name, channel->frag_sz, 
649                  channel->frag_num, channel->buf_sz);
650 }
651
652
653 /** 
654  * forte_channel_prep:
655  * @channel:    Channel whose buffer to prepare
656  *
657  * Locking:     Lock held.
658  */
659
660 static void
661 forte_channel_prep (struct forte_channel *channel)
662 {
663         struct page *page;
664         int i;
665         
666         if (channel->buf)
667                 return;
668
669         forte_channel_buffer (channel, channel->frag_sz, channel->frag_num);
670         channel->buf_pages = channel->buf_sz >> PAGE_SHIFT;
671
672         if (channel->buf_sz % PAGE_SIZE)
673                 channel->buf_pages++;
674
675         DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d, pg = %d\n", 
676                  __FUNCTION__, channel->name, channel->frag_sz, 
677                  channel->frag_num, channel->buf_sz, channel->buf_pages);
678
679         /* DMA buffer */
680         channel->buf = pci_alloc_consistent (forte->pci_dev, 
681                                              channel->buf_pages * PAGE_SIZE,
682                                              &channel->buf_handle);
683
684         if (!channel->buf || !channel->buf_handle)
685                 BUG();
686
687         page = virt_to_page (channel->buf);
688         
689         /* FIXME: can this go away ? */
690         for (i = 0 ; i < channel->buf_pages ; i++)
691                 SetPageReserved(page++);
692
693         /* Prep buffer registers */
694         outw (channel->frag_sz - 1, channel->iobase + FORTE_PLY_COUNT);
695         outl (channel->buf_handle, channel->iobase + FORTE_PLY_BUF1);
696         outl (channel->buf_handle + channel->frag_sz, 
697               channel->iobase + FORTE_PLY_BUF2);
698
699         /* Reset hwptr */
700         channel->hwptr = channel->frag_sz;
701         channel->next_buf = 1;
702
703         DPRINTK ("%s: %s buffer @ %p (%p)\n", __FUNCTION__, channel->name, 
704                  channel->buf, channel->buf_handle);
705 }
706
707
708 /** 
709  * forte_channel_drain:
710  * @chip:       
711  * @channel:    
712  *
713  * Locking:     Don't hold the lock.
714  */
715
716 static inline int
717 forte_channel_drain (struct forte_channel *channel)
718 {
719         DECLARE_WAITQUEUE (wait, current);
720         unsigned long flags;
721
722         DPRINTK ("%s\n", __FUNCTION__);
723
724         if (channel->mapped) {
725                 spin_lock_irqsave (&forte->lock, flags);
726                 forte_channel_stop (channel);
727                 spin_unlock_irqrestore (&forte->lock, flags);
728                 return 0;
729         }
730
731         spin_lock_irqsave (&forte->lock, flags);
732         add_wait_queue (&channel->wait, &wait);
733
734         for (;;) {
735                 if (channel->active == 0 || channel->filled_frags == 1)
736                         break;
737
738                 spin_unlock_irqrestore (&forte->lock, flags);
739
740                 __set_current_state (TASK_INTERRUPTIBLE);
741                 schedule();
742
743                 spin_lock_irqsave (&forte->lock, flags);
744         }
745
746         forte_channel_stop (channel);
747         forte_channel_reset (channel);
748         set_current_state (TASK_RUNNING);
749         remove_wait_queue (&channel->wait, &wait);
750         spin_unlock_irqrestore (&forte->lock, flags);
751
752         return 0;
753 }
754
755
756 /** 
757  * forte_channel_init:
758  * @chip:       Forte chip instance the channel hangs off
759  * @channel:    Channel to initialize
760  *
761  * Description:
762  *              Initializes a channel, sets defaults, and allocates
763  *              buffers.
764  *
765  * Locking:     No lock held.
766  */
767
768 static int
769 forte_channel_init (struct forte_chip *chip, struct forte_channel *channel)
770 {
771         DPRINTK ("%s: chip iobase @ %p\n", __FUNCTION__, (void *)chip->iobase);
772
773         spin_lock_irq (&chip->lock);
774         memset (channel, 0x0, sizeof (*channel));
775
776         if (channel == &chip->play) {
777                 channel->name = "PCM_OUT";
778                 channel->iobase = chip->iobase;
779                 DPRINTK ("%s: PCM-OUT iobase @ %p\n", __FUNCTION__,
780                          (void *) channel->iobase);
781         }
782         else if (channel == &chip->rec) {
783                 channel->name = "PCM_IN";
784                 channel->iobase = chip->iobase + FORTE_CAP_OFFSET;
785                 channel->record = 1;
786                 DPRINTK ("%s: PCM-IN iobase @ %p\n", __FUNCTION__, 
787                          (void *) channel->iobase);
788         }
789         else
790                 BUG();
791
792         init_waitqueue_head (&channel->wait);
793
794         /* Defaults: 48kHz, 16-bit, stereo */
795         channel->ctrl = inw (channel->iobase + FORTE_PLY_CTRL);
796         forte_channel_reset (channel);
797         forte_channel_stereo (channel, 1);
798         forte_channel_format (channel, AFMT_S16_LE);
799         forte_channel_rate (channel, 48000);
800         channel->frag_sz = FORTE_DEF_FRAG_SIZE;
801         channel->frag_num = FORTE_DEF_FRAGMENTS;
802
803         chip->trigger = 0;
804         spin_unlock_irq (&chip->lock);
805
806         return 0;
807 }
808
809
810 /** 
811  * forte_channel_free:
812  * @chip:       Chip this channel hangs off
813  * @channel:    Channel to nuke 
814  *
815  * Description:
816  *              Resets channel and frees buffers.
817  *
818  * Locking:     Hold your horses.
819  */
820
821 static void
822 forte_channel_free (struct forte_chip *chip, struct forte_channel *channel)
823 {
824         DPRINTK ("%s: %s\n", __FUNCTION__, channel->name);
825
826         if (!channel->buf_handle)
827                 return;
828
829         pci_free_consistent (chip->pci_dev, channel->buf_pages * PAGE_SIZE, 
830                              channel->buf, channel->buf_handle);
831         
832         memset (channel, 0x0, sizeof (*channel));
833 }
834
835
836 /* DSP --------------------------------------------------------------------- */
837
838
839 /**
840  * forte_dsp_ioctl:
841  */
842
843 static int
844 forte_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
845                  unsigned long arg)
846 {
847         int ival=0, ret, rval=0, rd, wr, count;
848         struct forte_chip *chip;
849         struct audio_buf_info abi;
850         struct count_info cinfo;
851         void __user *argp = (void __user *)arg;
852         int __user *p = argp;
853
854         chip = file->private_data;
855         
856         if (file->f_mode & FMODE_WRITE)
857                 wr = 1;
858         else 
859                 wr = 0;
860
861         if (file->f_mode & FMODE_READ)
862                 rd = 1;
863         else
864                 rd = 0;
865
866         switch (cmd) {
867
868         case OSS_GETVERSION:
869                 return put_user (SOUND_VERSION, p);
870
871         case SNDCTL_DSP_GETCAPS:
872                 DPRINTK ("%s: GETCAPS\n", __FUNCTION__);
873
874                 ival = FORTE_CAPS; /* DUPLEX */
875                 return put_user (ival, p);
876
877         case SNDCTL_DSP_GETFMTS:
878                 DPRINTK ("%s: GETFMTS\n", __FUNCTION__);
879
880                 ival = FORTE_FMTS; /* U8, 16LE */
881                 return put_user (ival, p);
882
883         case SNDCTL_DSP_SETFMT: /* U8, 16LE */
884                 DPRINTK ("%s: SETFMT\n", __FUNCTION__);
885
886                 if (get_user (ival, p))
887                         return -EFAULT;
888
889                 spin_lock_irq (&chip->lock);
890
891                 if (rd) {
892                         forte_channel_stop (&chip->rec);
893                         rval = forte_channel_format (&chip->rec, ival);
894                 }
895
896                 if (wr) {
897                         forte_channel_stop (&chip->rec);
898                         rval = forte_channel_format (&chip->play, ival);
899                 }
900
901                 spin_unlock_irq (&chip->lock);
902         
903                 return put_user (rval, p);
904
905         case SNDCTL_DSP_STEREO: /* 0 - mono, 1 - stereo */
906                 DPRINTK ("%s: STEREO\n", __FUNCTION__);
907
908                 if (get_user (ival, p))
909                         return -EFAULT;
910
911                 spin_lock_irq (&chip->lock);
912
913                 if (rd) {
914                         forte_channel_stop (&chip->rec);
915                         rval = forte_channel_stereo (&chip->rec, ival);
916                 }
917
918                 if (wr) {
919                         forte_channel_stop (&chip->rec);
920                         rval = forte_channel_stereo (&chip->play, ival);
921                 }
922
923                 spin_unlock_irq (&chip->lock);
924
925                 return put_user (rval, p);
926
927         case SNDCTL_DSP_CHANNELS: /* 1 - mono, 2 - stereo */
928                 DPRINTK ("%s: CHANNELS\n", __FUNCTION__);
929
930                 if (get_user (ival, p))
931                         return -EFAULT;
932
933                 spin_lock_irq (&chip->lock);
934
935                 if (rd) {
936                         forte_channel_stop (&chip->rec);
937                         rval = forte_channel_stereo (&chip->rec, ival-1) + 1;
938                 }
939
940                 if (wr) {
941                         forte_channel_stop (&chip->play);
942                         rval = forte_channel_stereo (&chip->play, ival-1) + 1;
943                 }
944
945                 spin_unlock_irq (&chip->lock);
946
947                 return put_user (rval, p);
948
949         case SNDCTL_DSP_SPEED:
950                 DPRINTK ("%s: SPEED\n", __FUNCTION__);
951
952                 if (get_user (ival, p))
953                         return -EFAULT;
954
955                 spin_lock_irq (&chip->lock);
956
957                 if (rd) {
958                         forte_channel_stop (&chip->rec);
959                         rval = forte_channel_rate (&chip->rec, ival);
960                 }
961
962                 if (wr) {
963                         forte_channel_stop (&chip->play);
964                         rval = forte_channel_rate (&chip->play, ival);
965                 }
966
967                 spin_unlock_irq (&chip->lock);
968
969                 return put_user(rval, p);
970
971         case SNDCTL_DSP_GETBLKSIZE:
972                 DPRINTK ("%s: GETBLKSIZE\n", __FUNCTION__);
973
974                 spin_lock_irq (&chip->lock);
975
976                 if (rd)
977                         ival = chip->rec.frag_sz;
978
979                 if (wr)
980                         ival = chip->play.frag_sz;
981
982                 spin_unlock_irq (&chip->lock);
983
984                 return put_user (ival, p);
985
986         case SNDCTL_DSP_RESET:
987                 DPRINTK ("%s: RESET\n", __FUNCTION__);
988
989                 spin_lock_irq (&chip->lock);
990
991                 if (rd)
992                         forte_channel_reset (&chip->rec);
993
994                 if (wr)
995                         forte_channel_reset (&chip->play);
996
997                 spin_unlock_irq (&chip->lock);
998
999                 return 0;
1000
1001         case SNDCTL_DSP_SYNC:
1002                 DPRINTK ("%s: SYNC\n", __FUNCTION__);
1003
1004                 if (wr)
1005                         ret = forte_channel_drain (&chip->play);
1006
1007                 return 0;
1008
1009         case SNDCTL_DSP_POST:
1010                 DPRINTK ("%s: POST\n", __FUNCTION__);
1011
1012                 if (wr) {
1013                         spin_lock_irq (&chip->lock);
1014
1015                         if (chip->play.filled_frags)
1016                                 forte_channel_start (&chip->play);
1017
1018                         spin_unlock_irq (&chip->lock);
1019                 }
1020
1021                 return 0;
1022
1023         case SNDCTL_DSP_SETFRAGMENT:
1024                 DPRINTK ("%s: SETFRAGMENT\n", __FUNCTION__);
1025
1026                 if (get_user (ival, p))
1027                         return -EFAULT;
1028
1029                 spin_lock_irq (&chip->lock);
1030
1031                 if (rd) {
1032                         forte_channel_buffer (&chip->rec, ival & 0xffff, 
1033                                               (ival >> 16) & 0xffff);
1034                         ival = (chip->rec.frag_num << 16) + chip->rec.frag_sz;
1035                 }
1036
1037                 if (wr) {
1038                         forte_channel_buffer (&chip->play, ival & 0xffff, 
1039                                               (ival >> 16) & 0xffff);
1040                         ival = (chip->play.frag_num << 16) +chip->play.frag_sz;
1041                 }
1042
1043                 spin_unlock_irq (&chip->lock);
1044
1045                 return put_user (ival, p);
1046                 
1047         case SNDCTL_DSP_GETISPACE:
1048                 DPRINTK ("%s: GETISPACE\n", __FUNCTION__);
1049
1050                 if (!rd)
1051                         return -EINVAL;
1052
1053                 spin_lock_irq (&chip->lock);
1054
1055                 abi.fragstotal = chip->rec.frag_num;
1056                 abi.fragsize = chip->rec.frag_sz;
1057                         
1058                 if (chip->rec.mapped) {
1059                         abi.fragments = chip->rec.frag_num - 2;
1060                         abi.bytes = abi.fragments * abi.fragsize;
1061                 }
1062                 else {
1063                         abi.fragments = chip->rec.filled_frags;
1064                         abi.bytes = abi.fragments * abi.fragsize;
1065                 }
1066
1067                 spin_unlock_irq (&chip->lock);
1068
1069                 return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
1070
1071         case SNDCTL_DSP_GETIPTR:
1072                 DPRINTK ("%s: GETIPTR\n", __FUNCTION__);
1073
1074                 if (!rd)
1075                         return -EINVAL;
1076
1077                 spin_lock_irq (&chip->lock);
1078
1079                 if (chip->rec.active) 
1080                         cinfo.ptr = chip->rec.hwptr;
1081                 else
1082                         cinfo.ptr = 0;
1083
1084                 cinfo.bytes = chip->rec.bytes;
1085                 cinfo.blocks = chip->rec.nr_irqs;
1086                 chip->rec.nr_irqs = 0;
1087
1088                 spin_unlock_irq (&chip->lock);
1089
1090                 return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
1091
1092         case SNDCTL_DSP_GETOSPACE:
1093                 if (!wr)
1094                         return -EINVAL;
1095                 
1096                 spin_lock_irq (&chip->lock);
1097
1098                 abi.fragstotal = chip->play.frag_num;
1099                 abi.fragsize = chip->play.frag_sz;
1100
1101                 if (chip->play.mapped) {
1102                         abi.fragments = chip->play.frag_num - 2;
1103                         abi.bytes = chip->play.buf_sz;
1104                 }
1105                 else {
1106                         abi.fragments = chip->play.frag_num - 
1107                                 chip->play.filled_frags;
1108
1109                         if (chip->play.residue)
1110                                 abi.fragments--;
1111
1112                         abi.bytes = abi.fragments * abi.fragsize +
1113                                 chip->play.residue;
1114                 }
1115
1116                 spin_unlock_irq (&chip->lock);
1117                 
1118                 return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
1119
1120         case SNDCTL_DSP_GETOPTR:
1121                 if (!wr)
1122                         return -EINVAL;
1123
1124                 spin_lock_irq (&chip->lock);
1125
1126                 if (chip->play.active) 
1127                         cinfo.ptr = chip->play.hwptr;
1128                 else
1129                         cinfo.ptr = 0;
1130
1131                 cinfo.bytes = chip->play.bytes;
1132                 cinfo.blocks = chip->play.nr_irqs;
1133                 chip->play.nr_irqs = 0;
1134
1135                 spin_unlock_irq (&chip->lock);
1136
1137                 return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
1138
1139         case SNDCTL_DSP_GETODELAY:
1140                 if (!wr)
1141                         return -EINVAL;
1142
1143                 spin_lock_irq (&chip->lock);
1144
1145                 if (!chip->play.active) {
1146                         ival = 0;
1147                 }
1148                 else if (chip->play.mapped) {
1149                         count = inw (chip->play.iobase + FORTE_PLY_COUNT) + 1;
1150                         ival = chip->play.frag_sz - count;
1151                 }
1152                 else {
1153                         ival = chip->play.filled_frags * chip->play.frag_sz;
1154
1155                         if (chip->play.residue)
1156                                 ival += chip->play.frag_sz - chip->play.residue;
1157                 }
1158
1159                 spin_unlock_irq (&chip->lock);
1160
1161                 return put_user (ival, p);
1162
1163         case SNDCTL_DSP_SETDUPLEX:
1164                 DPRINTK ("%s: SETDUPLEX\n", __FUNCTION__);
1165
1166                 return -EINVAL;
1167
1168         case SNDCTL_DSP_GETTRIGGER:
1169                 DPRINTK ("%s: GETTRIGGER\n", __FUNCTION__);
1170                 
1171                 return put_user (chip->trigger, p);
1172                 
1173         case SNDCTL_DSP_SETTRIGGER:
1174
1175                 if (get_user (ival, p))
1176                         return -EFAULT;
1177
1178                 DPRINTK ("%s: SETTRIGGER %d\n", __FUNCTION__, ival);
1179
1180                 if (wr) {
1181                         spin_lock_irq (&chip->lock);
1182
1183                         if (ival & PCM_ENABLE_OUTPUT)
1184                                 forte_channel_start (&chip->play);
1185                         else {          
1186                                 chip->trigger = 1;
1187                                 forte_channel_prep (&chip->play);
1188                                 forte_channel_stop (&chip->play);
1189                         }
1190
1191                         spin_unlock_irq (&chip->lock);
1192                 }
1193                 else if (rd) {
1194                         spin_lock_irq (&chip->lock);
1195
1196                         if (ival & PCM_ENABLE_INPUT)
1197                                 forte_channel_start (&chip->rec);
1198                         else {          
1199                                 chip->trigger = 1;
1200                                 forte_channel_prep (&chip->rec);
1201                                 forte_channel_stop (&chip->rec);
1202                         }
1203
1204                         spin_unlock_irq (&chip->lock);
1205                 }
1206
1207                 return 0;
1208                 
1209         case SOUND_PCM_READ_RATE:
1210                 DPRINTK ("%s: PCM_READ_RATE\n", __FUNCTION__);          
1211                 return put_user (chip->play.rate, p);
1212
1213         case SOUND_PCM_READ_CHANNELS:
1214                 DPRINTK ("%s: PCM_READ_CHANNELS\n", __FUNCTION__);
1215                 return put_user (chip->play.stereo, p);
1216
1217         case SOUND_PCM_READ_BITS:
1218                 DPRINTK ("%s: PCM_READ_BITS\n", __FUNCTION__);          
1219                 return put_user (chip->play.format, p);
1220
1221         case SNDCTL_DSP_NONBLOCK:
1222                 DPRINTK ("%s: DSP_NONBLOCK\n", __FUNCTION__);           
1223                 file->f_flags |= O_NONBLOCK;
1224                 return 0;
1225
1226         default:
1227                 DPRINTK ("Unsupported ioctl: %x (%p)\n", cmd, argp);
1228                 break;
1229         }
1230
1231         return -EINVAL;
1232 }
1233
1234
1235 /**
1236  * forte_dsp_open:
1237  */
1238
1239 static int 
1240 forte_dsp_open (struct inode *inode, struct file *file)
1241 {
1242         struct forte_chip *chip = forte; /* FIXME: HACK FROM HELL! */
1243
1244         if (file->f_flags & O_NONBLOCK) {
1245                 if (down_trylock (&chip->open_sem)) {
1246                         DPRINTK ("%s: returning -EAGAIN\n", __FUNCTION__);
1247                         return -EAGAIN;
1248                 }
1249         }
1250         else {
1251                 if (down_interruptible (&chip->open_sem)) {
1252                         DPRINTK ("%s: returning -ERESTARTSYS\n", __FUNCTION__);
1253                         return -ERESTARTSYS;
1254                 }
1255         }
1256
1257         file->private_data = forte;
1258
1259         DPRINTK ("%s: dsp opened by %d\n", __FUNCTION__, current->pid);
1260
1261         if (file->f_mode & FMODE_WRITE)
1262                 forte_channel_init (forte, &forte->play);
1263
1264         if (file->f_mode & FMODE_READ)
1265                 forte_channel_init (forte, &forte->rec);
1266
1267         return nonseekable_open(inode, file);
1268 }
1269
1270
1271 /**
1272  * forte_dsp_release:
1273  */
1274
1275 static int 
1276 forte_dsp_release (struct inode *inode, struct file *file)
1277 {
1278         struct forte_chip *chip = file->private_data;
1279         int ret = 0;
1280
1281         DPRINTK ("%s: chip @ %p\n", __FUNCTION__, chip);
1282
1283         if (file->f_mode & FMODE_WRITE) {
1284                 forte_channel_drain (&chip->play);
1285
1286                 spin_lock_irq (&chip->lock);
1287
1288                 forte_channel_free (chip, &chip->play);
1289
1290                 spin_unlock_irq (&chip->lock);
1291         }
1292
1293         if (file->f_mode & FMODE_READ) {
1294                 while (chip->rec.filled_frags > 0)
1295                         interruptible_sleep_on (&chip->rec.wait);
1296
1297                 spin_lock_irq (&chip->lock);
1298
1299                 forte_channel_stop (&chip->rec);
1300                 forte_channel_free (chip, &chip->rec);
1301
1302                 spin_unlock_irq (&chip->lock);
1303         }
1304
1305         up (&chip->open_sem);
1306
1307         return ret;
1308 }
1309
1310
1311 /**
1312  * forte_dsp_poll:
1313  *
1314  */
1315
1316 static unsigned int 
1317 forte_dsp_poll (struct file *file, struct poll_table_struct *wait)
1318 {
1319         struct forte_chip *chip;
1320         struct forte_channel *channel;
1321         unsigned int mask = 0;
1322
1323         chip = file->private_data;
1324
1325         if (file->f_mode & FMODE_WRITE) {
1326                 channel = &chip->play;
1327
1328                 if (channel->active)
1329                         poll_wait (file, &channel->wait, wait);
1330
1331                 spin_lock_irq (&chip->lock);
1332
1333                 if (channel->frag_num - channel->filled_frags > 0)
1334                         mask |= POLLOUT | POLLWRNORM;
1335
1336                 spin_unlock_irq (&chip->lock);
1337         }
1338
1339         if (file->f_mode & FMODE_READ) {
1340                 channel = &chip->rec;
1341
1342                 if (channel->active)
1343                         poll_wait (file, &channel->wait, wait);
1344
1345                 spin_lock_irq (&chip->lock);
1346
1347                 if (channel->filled_frags > 0)
1348                         mask |= POLLIN | POLLRDNORM;
1349
1350                 spin_unlock_irq (&chip->lock);
1351         }
1352
1353         return mask;
1354 }
1355
1356
1357 /**
1358  * forte_dsp_mmap:
1359  */
1360
1361 static int
1362 forte_dsp_mmap (struct file *file, struct vm_area_struct *vma)
1363 {
1364         struct forte_chip *chip;
1365         struct forte_channel *channel;
1366         unsigned long size;
1367         int ret;
1368
1369         chip = file->private_data;
1370
1371         DPRINTK ("%s: start %lXh, size %ld, pgoff %ld\n", __FUNCTION__,
1372                  vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_pgoff);
1373
1374         spin_lock_irq (&chip->lock);
1375
1376         if (vma->vm_flags & VM_WRITE && chip->play.active) {
1377                 ret = -EBUSY;
1378                 goto out;
1379         }
1380
1381         if (vma->vm_flags & VM_READ && chip->rec.active) {
1382                 ret = -EBUSY;
1383                 goto out;
1384         }
1385
1386         if (file->f_mode & FMODE_WRITE)
1387                 channel = &chip->play;
1388         else if (file->f_mode & FMODE_READ)
1389                 channel = &chip->rec;
1390         else {
1391                 ret = -EINVAL;
1392                 goto out;
1393         }
1394
1395         forte_channel_prep (channel);
1396         channel->mapped = 1;
1397
1398         if (vma->vm_pgoff != 0) {
1399                 ret = -EINVAL;
1400                 goto out;
1401         }
1402
1403         size = vma->vm_end - vma->vm_start;
1404
1405         if (size > channel->buf_pages * PAGE_SIZE) {
1406                 DPRINTK ("%s: size (%ld) > buf_sz (%d) \n", __FUNCTION__,
1407                          size, channel->buf_sz);
1408                 ret = -EINVAL;
1409                 goto out;
1410         }
1411
1412         if (remap_page_range (vma, vma->vm_start, virt_to_phys (channel->buf),
1413                               size, vma->vm_page_prot)) {
1414                 DPRINTK ("%s: remap el a no worko\n", __FUNCTION__);
1415                 ret = -EAGAIN;
1416                 goto out;
1417         }
1418
1419         ret = 0;
1420
1421  out:
1422         spin_unlock_irq (&chip->lock);
1423         return ret;
1424 }
1425
1426
1427 /**
1428  * forte_dsp_write:
1429  */
1430
1431 static ssize_t 
1432 forte_dsp_write (struct file *file, const char __user *buffer, size_t bytes, 
1433                  loff_t *ppos)
1434 {
1435         struct forte_chip *chip;
1436         struct forte_channel *channel;
1437         unsigned int i = bytes, sz = 0;
1438         unsigned long flags;
1439
1440         if (!access_ok (VERIFY_READ, buffer, bytes))
1441                 return -EFAULT;
1442
1443         chip = (struct forte_chip *) file->private_data;
1444
1445         if (!chip)
1446                 BUG();
1447
1448         channel = &chip->play;
1449
1450         if (!channel)
1451                 BUG();
1452
1453         spin_lock_irqsave (&chip->lock, flags);
1454
1455         /* Set up buffers with the right fragment size */
1456         forte_channel_prep (channel);
1457
1458         while (i) {
1459                 /* All fragment buffers in use -> wait */
1460                 if (channel->frag_num - channel->filled_frags == 0) {
1461                         DECLARE_WAITQUEUE (wait, current);
1462
1463                         /* For trigger or non-blocking operation, get out */
1464                         if (chip->trigger || file->f_flags & O_NONBLOCK) {
1465                                 spin_unlock_irqrestore (&chip->lock, flags);
1466                                 return -EAGAIN;
1467                         }
1468
1469                         /* Otherwise wait for buffers */
1470                         add_wait_queue (&channel->wait, &wait);
1471
1472                         for (;;) {
1473                                 spin_unlock_irqrestore (&chip->lock, flags);
1474
1475                                 set_current_state (TASK_INTERRUPTIBLE);
1476                                 schedule();
1477
1478                                 spin_lock_irqsave (&chip->lock, flags);
1479
1480                                 if (channel->frag_num - channel->filled_frags)
1481                                         break;
1482                         }
1483
1484                         remove_wait_queue (&channel->wait, &wait);
1485                         set_current_state (TASK_RUNNING);
1486
1487                         if (signal_pending (current)) {
1488                                 spin_unlock_irqrestore (&chip->lock, flags);
1489                                 return -ERESTARTSYS;
1490                         }
1491                 }
1492
1493                 if (channel->residue)
1494                         sz = channel->residue;
1495                 else if (i > channel->frag_sz)
1496                         sz = channel->frag_sz;
1497                 else
1498                         sz = i;
1499
1500                 spin_unlock_irqrestore (&chip->lock, flags);
1501
1502                 if (copy_from_user ((void *) channel->buf + channel->swptr, buffer, sz))
1503                         return -EFAULT;
1504
1505                 spin_lock_irqsave (&chip->lock, flags);
1506
1507                 /* Advance software pointer */
1508                 buffer += sz;
1509                 channel->swptr += sz;
1510                 channel->swptr %= channel->buf_sz;
1511                 i -= sz;
1512
1513                 /* Only bump filled_frags if a full fragment has been written */
1514                 if (channel->swptr % channel->frag_sz == 0) {
1515                         channel->filled_frags++;
1516                         channel->residue = 0;
1517                 }
1518                 else
1519                         channel->residue = channel->frag_sz - sz;
1520
1521                 /* If playback isn't active, start it */
1522                 if (channel->active == 0 && chip->trigger == 0)
1523                         forte_channel_start (channel);
1524         }
1525
1526         spin_unlock_irqrestore (&chip->lock, flags);
1527
1528         return bytes - i;
1529 }
1530
1531
1532 /**
1533  * forte_dsp_read:
1534  */
1535
1536 static ssize_t 
1537 forte_dsp_read (struct file *file, char __user *buffer, size_t bytes, 
1538                 loff_t *ppos)
1539 {
1540         struct forte_chip *chip;
1541         struct forte_channel *channel;
1542         unsigned int i = bytes, sz;
1543         unsigned long flags;
1544
1545         if (!access_ok (VERIFY_WRITE, buffer, bytes))
1546                 return -EFAULT;
1547
1548         chip = (struct forte_chip *) file->private_data;
1549
1550         if (!chip)
1551                 BUG();
1552
1553         channel = &chip->rec;
1554
1555         if (!channel)
1556                 BUG();
1557
1558         spin_lock_irqsave (&chip->lock, flags);
1559
1560         /* Set up buffers with the right fragment size */
1561         forte_channel_prep (channel);
1562
1563         /* Start recording */
1564         if (!chip->trigger)
1565                 forte_channel_start (channel);
1566
1567         while (i) {
1568                 /* No fragment buffers in use -> wait */
1569                 if (channel->filled_frags == 0) {
1570                         DECLARE_WAITQUEUE (wait, current);
1571
1572                         /* For trigger mode operation, get out */
1573                         if (chip->trigger) {
1574                                 spin_unlock_irqrestore (&chip->lock, flags);
1575                                 return -EAGAIN;
1576                         }
1577
1578                         add_wait_queue (&channel->wait, &wait);
1579
1580                         for (;;) {
1581                                 if (channel->active == 0)
1582                                         break;
1583
1584                                 if (channel->filled_frags)
1585                                         break;
1586                                                 
1587                                 spin_unlock_irqrestore (&chip->lock, flags);
1588
1589                                 set_current_state (TASK_INTERRUPTIBLE);
1590                                 schedule();
1591
1592                                 spin_lock_irqsave (&chip->lock, flags);
1593                         }
1594
1595                         set_current_state (TASK_RUNNING);
1596                         remove_wait_queue (&channel->wait, &wait);
1597                 }
1598
1599                 if (i > channel->frag_sz)
1600                         sz = channel->frag_sz;
1601                 else
1602                         sz = i;
1603
1604                 spin_unlock_irqrestore (&chip->lock, flags);
1605
1606                 if (copy_to_user (buffer, (void *)channel->buf+channel->swptr, sz)) {
1607                         DPRINTK ("%s: copy_to_user failed\n", __FUNCTION__);
1608                         return -EFAULT;
1609                 }
1610
1611                 spin_lock_irqsave (&chip->lock, flags);
1612
1613                 /* Advance software pointer */
1614                 buffer += sz;
1615                 if (channel->filled_frags > 0)
1616                         channel->filled_frags--;
1617                 channel->swptr += channel->frag_sz;
1618                 channel->swptr %= channel->buf_sz;
1619                 i -= sz;
1620         }
1621
1622         spin_unlock_irqrestore (&chip->lock, flags);
1623
1624         return bytes - i;
1625 }
1626
1627
1628 static struct file_operations forte_dsp_fops = {
1629         .owner                  = THIS_MODULE,
1630         .llseek                 = &no_llseek,
1631         .read                   = &forte_dsp_read,
1632         .write                  = &forte_dsp_write,
1633         .poll                   = &forte_dsp_poll,
1634         .ioctl                  = &forte_dsp_ioctl,
1635         .open                   = &forte_dsp_open,
1636         .release                = &forte_dsp_release,
1637         .mmap                   = &forte_dsp_mmap,
1638 };
1639
1640
1641 /* Common ------------------------------------------------------------------ */
1642
1643
1644 /**
1645  * forte_interrupt:
1646  */
1647
1648 static irqreturn_t
1649 forte_interrupt (int irq, void *dev_id, struct pt_regs *regs)
1650 {
1651         struct forte_chip *chip = dev_id;
1652         struct forte_channel *channel = NULL;
1653         u16 status, count; 
1654
1655         status = inw (chip->iobase + FORTE_IRQ_STATUS);
1656
1657         /* If this is not for us, get outta here ASAP */
1658         if ((status & (FORTE_IRQ_PLAYBACK | FORTE_IRQ_CAPTURE)) == 0)
1659                 return IRQ_NONE;
1660         
1661         if (status & FORTE_IRQ_PLAYBACK) {
1662                 channel = &chip->play;
1663
1664                 spin_lock (&chip->lock);
1665
1666                 if (channel->frag_sz == 0)
1667                         goto pack;
1668
1669                 /* Declare a fragment done */
1670                 if (channel->filled_frags > 0)
1671                         channel->filled_frags--;
1672                 channel->bytes += channel->frag_sz;
1673                 channel->nr_irqs++;
1674                 
1675                 /* Flip-flop between buffer I and II */
1676                 channel->next_buf ^= 1;
1677
1678                 /* Advance hardware pointer by fragment size and wrap around */
1679                 channel->hwptr += channel->frag_sz;
1680                 channel->hwptr %= channel->buf_sz;
1681
1682                 /* Buffer I or buffer II BAR */
1683                 outl (channel->buf_handle + channel->hwptr, 
1684                       channel->next_buf == 0 ?
1685                       channel->iobase + FORTE_PLY_BUF1 :
1686                       channel->iobase + FORTE_PLY_BUF2);
1687
1688                 /* If the currently playing fragment is last, schedule pause */
1689                 if (channel->filled_frags == 1) 
1690                         forte_channel_pause (channel);
1691
1692         pack:
1693                 /* Acknowledge interrupt */
1694                 outw (FORTE_IRQ_PLAYBACK, chip->iobase + FORTE_IRQ_STATUS);
1695
1696                 if (waitqueue_active (&channel->wait)) 
1697                         wake_up_all (&channel->wait);
1698
1699                 spin_unlock (&chip->lock);
1700         }
1701
1702         if (status & FORTE_IRQ_CAPTURE) {
1703                 channel = &chip->rec;
1704                 spin_lock (&chip->lock);
1705
1706                 /* One fragment filled */
1707                 channel->filled_frags++;
1708
1709                 /* Get # of completed bytes */
1710                 count = inw (channel->iobase + FORTE_PLY_COUNT) + 1;
1711
1712                 if (count == 0) {
1713                         DPRINTK ("%s: last, filled_frags = %d\n", __FUNCTION__,
1714                                  channel->filled_frags);
1715                         channel->filled_frags = 0;
1716                         goto rack;
1717                 }
1718
1719                 /* Buffer I or buffer II BAR */
1720                 outl (channel->buf_handle + channel->hwptr, 
1721                       channel->next_buf == 0 ?
1722                       channel->iobase + FORTE_PLY_BUF1 :
1723                       channel->iobase + FORTE_PLY_BUF2);
1724
1725                 /* Flip-flop between buffer I and II */
1726                 channel->next_buf ^= 1;
1727
1728                 /* Advance hardware pointer by fragment size and wrap around */
1729                 channel->hwptr += channel->frag_sz;
1730                 channel->hwptr %= channel->buf_sz;
1731
1732                 /* Out of buffers */
1733                 if (channel->filled_frags == channel->frag_num - 1)
1734                         forte_channel_stop (channel);
1735         rack:
1736                 /* Acknowledge interrupt */
1737                 outw (FORTE_IRQ_CAPTURE, chip->iobase + FORTE_IRQ_STATUS);
1738
1739                 spin_unlock (&chip->lock);
1740
1741                 if (waitqueue_active (&channel->wait))
1742                         wake_up_all (&channel->wait);           
1743         }
1744
1745         return IRQ_HANDLED;
1746 }
1747
1748
1749 /**
1750  * forte_proc_read:
1751  */
1752
1753 static int
1754 forte_proc_read (char *page, char **start, off_t off, int count, 
1755                  int *eof, void *data)
1756 {
1757         int i = 0, p_rate, p_chan, r_rate;
1758         unsigned short p_reg, r_reg;
1759
1760         i += sprintf (page, "ForteMedia FM801 OSS Lite driver\n%s\n \n", 
1761                       DRIVER_VERSION);
1762
1763         if (!forte->iobase)
1764                 return i;
1765
1766         p_rate = p_chan = -1;
1767         p_reg  = inw (forte->iobase + FORTE_PLY_CTRL);
1768         p_rate = (p_reg >> 8) & 15;
1769         p_chan = (p_reg >> 12) & 3;
1770
1771         if (p_rate >= 0 || p_rate <= 10)
1772                 p_rate = rates[p_rate];
1773
1774         if (p_chan >= 0 || p_chan <= 2)
1775                 p_chan = channels[p_chan];
1776
1777         r_rate = -1;
1778         r_reg  = inw (forte->iobase + FORTE_CAP_CTRL);
1779         r_rate = (r_reg >> 8) & 15;
1780
1781         if (r_rate >= 0 || r_rate <= 10)
1782                 r_rate = rates[r_rate]; 
1783
1784         i += sprintf (page + i,
1785                       "             Playback  Capture\n"
1786                       "FIFO empty : %-3s       %-3s\n"
1787                       "Buf1 Last  : %-3s       %-3s\n"
1788                       "Buf2 Last  : %-3s       %-3s\n"
1789                       "Started    : %-3s       %-3s\n"
1790                       "Paused     : %-3s       %-3s\n"
1791                       "Immed Stop : %-3s       %-3s\n"
1792                       "Rate       : %-5d     %-5d\n"
1793                       "Channels   : %-5d     -\n"
1794                       "16-bit     : %-3s       %-3s\n"
1795                       "Stereo     : %-3s       %-3s\n"
1796                       " \n"
1797                       "Buffer Sz  : %-6d    %-6d\n"
1798                       "Frag Sz    : %-6d    %-6d\n"
1799                       "Frag Num   : %-6d    %-6d\n"
1800                       "Frag msecs : %-6d    %-6d\n"
1801                       "Used Frags : %-6d    %-6d\n"
1802                       "Mapped     : %-3s       %-3s\n",
1803                       p_reg & 1<<0  ? "yes" : "no",
1804                       r_reg & 1<<0  ? "yes" : "no",
1805                       p_reg & 1<<1  ? "yes" : "no",
1806                       r_reg & 1<<1  ? "yes" : "no",
1807                       p_reg & 1<<2  ? "yes" : "no",
1808                       r_reg & 1<<2  ? "yes" : "no",
1809                       p_reg & 1<<5  ? "yes" : "no",
1810                       r_reg & 1<<5  ? "yes" : "no",
1811                       p_reg & 1<<6  ? "yes" : "no",
1812                       r_reg & 1<<6  ? "yes" : "no",
1813                       p_reg & 1<<7  ? "yes" : "no",
1814                       r_reg & 1<<7  ? "yes" : "no",
1815                       p_rate, r_rate,
1816                       p_chan,
1817                       p_reg & 1<<14 ? "yes" : "no",
1818                       r_reg & 1<<14 ? "yes" : "no",
1819                       p_reg & 1<<15 ? "yes" : "no",
1820                       r_reg & 1<<15 ? "yes" : "no",
1821                       forte->play.buf_sz,       forte->rec.buf_sz,
1822                       forte->play.frag_sz,      forte->rec.frag_sz,
1823                       forte->play.frag_num,     forte->rec.frag_num,
1824                       forte->play.frag_msecs,   forte->rec.frag_msecs,
1825                       forte->play.filled_frags, forte->rec.filled_frags,
1826                       forte->play.mapped ? "yes" : "no",
1827                       forte->rec.mapped ? "yes" : "no"
1828                 );
1829
1830         return i;
1831 }
1832
1833
1834 /**
1835  * forte_proc_init:
1836  *
1837  * Creates driver info entries in /proc
1838  */
1839
1840 static int __init 
1841 forte_proc_init (void)
1842 {
1843         if (!proc_mkdir ("driver/forte", NULL))
1844                 return -EIO;
1845
1846         if (!create_proc_read_entry ("driver/forte/chip", 0, NULL, forte_proc_read, forte)) {
1847                 remove_proc_entry ("driver/forte", NULL);
1848                 return -EIO;
1849         }
1850
1851         if (!create_proc_read_entry("driver/forte/ac97", 0, NULL, ac97_read_proc, forte->ac97)) {
1852                 remove_proc_entry ("driver/forte/chip", NULL);
1853                 remove_proc_entry ("driver/forte", NULL);
1854                 return -EIO;
1855         }
1856
1857         return 0;
1858 }
1859
1860
1861 /**
1862  * forte_proc_remove:
1863  *
1864  * Removes driver info entries in /proc
1865  */
1866
1867 static void
1868 forte_proc_remove (void)
1869 {
1870         remove_proc_entry ("driver/forte/ac97", NULL);
1871         remove_proc_entry ("driver/forte/chip", NULL);
1872         remove_proc_entry ("driver/forte", NULL);       
1873 }
1874
1875
1876 /**
1877  * forte_chip_init:
1878  * @chip:       Chip instance to initialize
1879  *
1880  * Description:
1881  *              Resets chip, configures codec and registers the driver with
1882  *              the sound subsystem.
1883  *
1884  *              Press and hold Start for 8 secs, then switch on Run
1885  *              and hold for 4 seconds.  Let go of Start.  Numbers
1886  *              assume a properly oiled TWG.
1887  */
1888
1889 static int __devinit
1890 forte_chip_init (struct forte_chip *chip)
1891 {
1892         u8 revision;
1893         u16 cmdw;
1894         struct ac97_codec *codec;
1895
1896         pci_read_config_byte (chip->pci_dev, PCI_REVISION_ID, &revision);
1897
1898         if (revision >= 0xB1) {
1899                 chip->multichannel = 1;
1900                 printk (KERN_INFO PFX "Multi-channel device detected.\n");
1901         }
1902
1903         /* Reset chip */
1904         outw (FORTE_CC_CODEC_RESET | FORTE_CC_AC97_RESET, 
1905               chip->iobase + FORTE_CODEC_CTRL);
1906         udelay(100);
1907         outw (0, chip->iobase + FORTE_CODEC_CTRL);
1908
1909         /* Request read from AC97 */
1910         outw (FORTE_AC97_READ | (0 << FORTE_AC97_ADDR_SHIFT), 
1911               chip->iobase + FORTE_AC97_CMD);
1912         mdelay(750);
1913
1914         if ((inw (chip->iobase + FORTE_AC97_CMD) & (3<<8)) != (1<<8)) {
1915                 printk (KERN_INFO PFX "AC97 codec not responding");
1916                 return -EIO;
1917         }
1918
1919         /* Init volume */
1920         outw (0x0808, chip->iobase + FORTE_PCM_VOL);
1921         outw (0x9f1f, chip->iobase + FORTE_FM_VOL);
1922         outw (0x8808, chip->iobase + FORTE_I2S_VOL);
1923
1924         /* I2S control - I2S mode */
1925         outw (0x0003, chip->iobase + FORTE_I2S_MODE);
1926
1927         /* Interrupt setup - unmask PLAYBACK & CAPTURE */
1928         cmdw = inw (chip->iobase + FORTE_IRQ_MASK);
1929         cmdw &= ~0x0003;
1930         outw (cmdw, chip->iobase + FORTE_IRQ_MASK);
1931
1932         /* Interrupt clear */
1933         outw (FORTE_IRQ_PLAYBACK|FORTE_IRQ_CAPTURE, 
1934               chip->iobase + FORTE_IRQ_STATUS);
1935
1936         /* Set up the AC97 codec */
1937         if ((codec = ac97_alloc_codec()) == NULL)
1938                 return -ENOMEM;
1939         codec->private_data = chip;
1940         codec->codec_read = forte_ac97_read;
1941         codec->codec_write = forte_ac97_write;
1942         codec->id = 0;
1943
1944         if (ac97_probe_codec (codec) == 0) {
1945                 printk (KERN_ERR PFX "codec probe failed\n");
1946                 ac97_release_codec(codec);
1947                 return -1;
1948         }
1949
1950         /* Register mixer */
1951         if ((codec->dev_mixer = 
1952              register_sound_mixer (&forte_mixer_fops, -1)) < 0) {
1953                 printk (KERN_ERR PFX "couldn't register mixer!\n");
1954                 ac97_release_codec(codec);
1955                 return -1;
1956         }
1957
1958         chip->ac97 = codec;
1959
1960         /* Register DSP */
1961         if ((chip->dsp = register_sound_dsp (&forte_dsp_fops, -1) ) < 0) {
1962                 printk (KERN_ERR PFX "couldn't register dsp!\n");
1963                 return -1;
1964         }
1965
1966         /* Register with /proc */
1967         if (forte_proc_init()) {
1968                 printk (KERN_ERR PFX "couldn't add entries to /proc!\n");
1969                 return -1;
1970         }
1971
1972         return 0;
1973 }
1974
1975
1976 /**
1977  * forte_probe:
1978  * @pci_dev:    PCI struct for probed device
1979  * @pci_id:     
1980  *
1981  * Description:
1982  *              Allocates chip instance, I/O region, and IRQ
1983  */
1984 static int __init 
1985 forte_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
1986 {
1987         struct forte_chip *chip;
1988         int ret = 0;
1989
1990         /* FIXME: Support more than one chip */
1991         if (found++)
1992                 return -EIO;
1993
1994         /* Ignition */
1995         if (pci_enable_device (pci_dev))
1996                 return -EIO;
1997
1998         pci_set_master (pci_dev);
1999
2000         /* Allocate chip instance and configure */
2001         forte = (struct forte_chip *) 
2002                 kmalloc (sizeof (struct forte_chip), GFP_KERNEL);
2003         chip = forte;
2004
2005         if (chip == NULL) {
2006                 printk (KERN_WARNING PFX "Out of memory");
2007                 return -ENOMEM;
2008         }
2009
2010         memset (chip, 0, sizeof (struct forte_chip));
2011         chip->pci_dev = pci_dev;
2012
2013         init_MUTEX(&chip->open_sem);
2014         spin_lock_init (&chip->lock);
2015         spin_lock_init (&chip->ac97_lock);
2016
2017         if (! request_region (pci_resource_start (pci_dev, 0),
2018                               pci_resource_len (pci_dev, 0), DRIVER_NAME)) {
2019                 printk (KERN_WARNING PFX "Unable to reserve I/O space");
2020                 ret = -ENOMEM;
2021                 goto error;
2022         }
2023
2024         chip->iobase = pci_resource_start (pci_dev, 0);
2025         chip->irq = pci_dev->irq;
2026
2027         if (request_irq (chip->irq, forte_interrupt, SA_SHIRQ, DRIVER_NAME,
2028                          chip)) {
2029                 printk (KERN_WARNING PFX "Unable to reserve IRQ");
2030                 ret = -EIO;
2031                 goto error;
2032         }               
2033         
2034         pci_set_drvdata (pci_dev, chip);
2035
2036         printk (KERN_INFO PFX "FM801 chip found at 0x%04lX-0x%04lX IRQ %u\n", 
2037                 chip->iobase, pci_resource_end (pci_dev, 0), chip->irq);
2038
2039         /* Power it up */
2040         if ((ret = forte_chip_init (chip)) == 0)
2041                 return 0;
2042
2043  error:
2044         if (chip->irq)
2045                 free_irq (chip->irq, chip);
2046
2047         if (chip->iobase) 
2048                 release_region (pci_resource_start (pci_dev, 0),
2049                                 pci_resource_len (pci_dev, 0));
2050                 
2051         kfree (chip);
2052
2053         return ret;
2054 }
2055
2056
2057 /**
2058  * forte_remove:
2059  * @pci_dev:    PCI device to unclaim
2060  *
2061  */
2062
2063 static void 
2064 forte_remove (struct pci_dev *pci_dev)
2065 {
2066         struct forte_chip *chip = pci_get_drvdata (pci_dev);
2067
2068         if (chip == NULL)
2069                 return;
2070
2071         /* Turn volume down to avoid popping */
2072         outw (0x1f1f, chip->iobase + FORTE_PCM_VOL);
2073         outw (0x1f1f, chip->iobase + FORTE_FM_VOL);
2074         outw (0x1f1f, chip->iobase + FORTE_I2S_VOL);
2075
2076         forte_proc_remove();
2077         free_irq (chip->irq, chip);
2078         release_region (chip->iobase, pci_resource_len (pci_dev, 0));
2079
2080         unregister_sound_dsp (chip->dsp);
2081         unregister_sound_mixer (chip->ac97->dev_mixer);
2082         ac97_release_codec(chip->ac97);
2083         kfree (chip);
2084
2085         printk (KERN_INFO PFX "driver released\n");
2086 }
2087
2088
2089 static struct pci_device_id forte_pci_ids[] = {
2090         { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2091         { 0, }
2092 };
2093
2094
2095 static struct pci_driver forte_pci_driver = {
2096         .name                   = DRIVER_NAME,
2097         .id_table               = forte_pci_ids,
2098         .probe                  = forte_probe,
2099         .remove                 = forte_remove,
2100
2101 };
2102
2103
2104 /**
2105  * forte_init_module:
2106  *
2107  */
2108
2109 static int __init
2110 forte_init_module (void)
2111 {
2112         printk (KERN_INFO PFX DRIVER_VERSION "\n");
2113
2114         if (!pci_register_driver (&forte_pci_driver)) {
2115                 pci_unregister_driver (&forte_pci_driver);
2116                 return -ENODEV;
2117         }
2118
2119         return 0;
2120 }
2121
2122
2123 /**
2124  * forte_cleanup_module:
2125  *
2126  */
2127
2128 static void __exit 
2129 forte_cleanup_module (void)
2130 {
2131         pci_unregister_driver (&forte_pci_driver);
2132 }
2133
2134
2135 module_init(forte_init_module);
2136 module_exit(forte_cleanup_module);
2137
2138 MODULE_AUTHOR("Martin K. Petersen <mkp@mkp.net>");
2139 MODULE_DESCRIPTION("ForteMedia FM801 OSS Driver");
2140 MODULE_LICENSE("GPL");
2141 MODULE_DEVICE_TABLE (pci, forte_pci_ids);