IB/cache: Use correct pointer to calculate size
[linux-2.6/kvm.git] / sound / oss / forte.c
blob0294eec8ad9035ee621f516df74d692bab95faff
1 /*
2 * forte.c - ForteMedia FM801 OSS Driver
4 * Written by Martin K. Petersen <mkp@mkp.net>
5 * Copyright (C) 2002 Hewlett-Packard Company
6 * Portions Copyright (C) 2003 Martin K. Petersen
8 * Latest version: http://mkp.net/forte/
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!
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.
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.
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
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/spinlock.h>
36 #include <linux/pci.h>
38 #include <linux/delay.h>
39 #include <linux/poll.h>
41 #include <linux/sound.h>
42 #include <linux/ac97_codec.h>
43 #include <linux/interrupt.h>
45 #include <linux/proc_fs.h>
46 #include <linux/mutex.h>
48 #include <asm/uaccess.h>
49 #include <asm/io.h>
51 #define DRIVER_NAME "forte"
52 #define DRIVER_VERSION "$Id: forte.c,v 1.63 2003/03/01 05:32:42 mkp Exp $"
53 #define PFX DRIVER_NAME ": "
55 #undef M_DEBUG
57 #ifdef M_DEBUG
58 #define DPRINTK(args...) printk(KERN_WARNING args)
59 #else
60 #define DPRINTK(args...)
61 #endif
63 /* Card capabilities */
64 #define FORTE_CAPS (DSP_CAP_MMAP | DSP_CAP_TRIGGER)
66 /* Supported audio formats */
67 #define FORTE_FMTS (AFMT_U8 | AFMT_S16_LE)
69 /* Buffers */
70 #define FORTE_MIN_FRAG_SIZE 256
71 #define FORTE_MAX_FRAG_SIZE PAGE_SIZE
72 #define FORTE_DEF_FRAG_SIZE 256
73 #define FORTE_MIN_FRAGMENTS 2
74 #define FORTE_MAX_FRAGMENTS 256
75 #define FORTE_DEF_FRAGMENTS 2
76 #define FORTE_MIN_BUF_MSECS 500
77 #define FORTE_MAX_BUF_MSECS 1000
79 /* PCI BARs */
80 #define FORTE_PCM_VOL 0x00 /* PCM Output Volume */
81 #define FORTE_FM_VOL 0x02 /* FM Output Volume */
82 #define FORTE_I2S_VOL 0x04 /* I2S Volume */
83 #define FORTE_REC_SRC 0x06 /* Record Source */
84 #define FORTE_PLY_CTRL 0x08 /* Playback Control */
85 #define FORTE_PLY_COUNT 0x0a /* Playback Count */
86 #define FORTE_PLY_BUF1 0x0c /* Playback Buffer I */
87 #define FORTE_PLY_BUF2 0x10 /* Playback Buffer II */
88 #define FORTE_CAP_CTRL 0x14 /* Capture Control */
89 #define FORTE_CAP_COUNT 0x16 /* Capture Count */
90 #define FORTE_CAP_BUF1 0x18 /* Capture Buffer I */
91 #define FORTE_CAP_BUF2 0x1c /* Capture Buffer II */
92 #define FORTE_CODEC_CTRL 0x22 /* Codec Control */
93 #define FORTE_I2S_MODE 0x24 /* I2S Mode Control */
94 #define FORTE_VOLUME 0x26 /* Volume Up/Down/Mute Status */
95 #define FORTE_I2C_CTRL 0x29 /* I2C Control */
96 #define FORTE_AC97_CMD 0x2a /* AC'97 Command */
97 #define FORTE_AC97_DATA 0x2c /* AC'97 Data */
98 #define FORTE_MPU401_DATA 0x30 /* MPU401 Data */
99 #define FORTE_MPU401_CMD 0x31 /* MPU401 Command */
100 #define FORTE_GPIO_CTRL 0x52 /* General Purpose I/O Control */
101 #define FORTE_GEN_CTRL 0x54 /* General Control */
102 #define FORTE_IRQ_MASK 0x56 /* Interrupt Mask */
103 #define FORTE_IRQ_STATUS 0x5a /* Interrupt Status */
104 #define FORTE_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
105 #define FORTE_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
106 #define FORTE_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
107 #define FORTE_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
108 #define FORTE_POWERDOWN 0x70 /* Blocks Power Down Control */
110 #define FORTE_CAP_OFFSET FORTE_CAP_CTRL - FORTE_PLY_CTRL
112 #define FORTE_AC97_ADDR_SHIFT 10
114 /* Playback and record control register bits */
115 #define FORTE_BUF1_LAST (1<<1)
116 #define FORTE_BUF2_LAST (1<<2)
117 #define FORTE_START (1<<5)
118 #define FORTE_PAUSE (1<<6)
119 #define FORTE_IMMED_STOP (1<<7)
120 #define FORTE_RATE_SHIFT 8
121 #define FORTE_RATE_MASK (15 << FORTE_RATE_SHIFT)
122 #define FORTE_CHANNELS_4 (1<<12) /* Playback only */
123 #define FORTE_CHANNELS_6 (2<<12) /* Playback only */
124 #define FORTE_CHANNELS_6MS (3<<12) /* Playback only */
125 #define FORTE_CHANNELS_MASK (3<<12)
126 #define FORTE_16BIT (1<<14)
127 #define FORTE_STEREO (1<<15)
129 /* IRQ status bits */
130 #define FORTE_IRQ_PLAYBACK (1<<8)
131 #define FORTE_IRQ_CAPTURE (1<<9)
132 #define FORTE_IRQ_VOLUME (1<<14)
133 #define FORTE_IRQ_MPU (1<<15)
135 /* CODEC control */
136 #define FORTE_CC_CODEC_RESET (1<<5)
137 #define FORTE_CC_AC97_RESET (1<<6)
139 /* AC97 cmd */
140 #define FORTE_AC97_WRITE (0<<7)
141 #define FORTE_AC97_READ (1<<7)
142 #define FORTE_AC97_DP_INVALID (0<<8)
143 #define FORTE_AC97_DP_VALID (1<<8)
144 #define FORTE_AC97_PORT_RDY (0<<9)
145 #define FORTE_AC97_PORT_BSY (1<<9)
148 struct forte_channel {
149 const char *name;
151 unsigned short ctrl; /* Ctrl BAR contents */
152 unsigned long iobase; /* Ctrl BAR address */
154 wait_queue_head_t wait;
156 void *buf; /* Buffer */
157 dma_addr_t buf_handle; /* Buffer handle */
159 unsigned int record;
160 unsigned int format;
161 unsigned int rate;
162 unsigned int stereo;
164 unsigned int frag_sz; /* Current fragment size */
165 unsigned int frag_num; /* Current # of fragments */
166 unsigned int frag_msecs; /* Milliseconds per frag */
167 unsigned int buf_sz; /* Current buffer size */
169 unsigned int hwptr; /* Tail */
170 unsigned int swptr; /* Head */
171 unsigned int filled_frags; /* Fragments currently full */
172 unsigned int next_buf; /* Index of next buffer */
174 unsigned int active; /* Channel currently in use */
175 unsigned int mapped; /* mmap */
177 unsigned int buf_pages; /* Real size of buffer */
178 unsigned int nr_irqs; /* Number of interrupts */
179 unsigned int bytes; /* Total bytes */
180 unsigned int residue; /* Partial fragment */
184 struct forte_chip {
185 struct pci_dev *pci_dev;
186 unsigned long iobase;
187 int irq;
189 struct mutex open_mutex; /* Device access */
190 spinlock_t lock; /* State */
192 spinlock_t ac97_lock;
193 struct ac97_codec *ac97;
195 int multichannel;
196 int dsp; /* OSS handle */
197 int trigger; /* mmap I/O trigger */
199 struct forte_channel play;
200 struct forte_channel rec;
204 static int channels[] = { 2, 4, 6, };
205 static int rates[] = { 5500, 8000, 9600, 11025, 16000, 19200,
206 22050, 32000, 38400, 44100, 48000, };
208 static struct forte_chip *forte;
209 static int found;
212 /* AC97 Codec -------------------------------------------------------------- */
215 /**
216 * forte_ac97_wait:
217 * @chip: fm801 instance whose AC97 codec to wait on
219 * FIXME:
220 * Stop busy-waiting
223 static inline int
224 forte_ac97_wait (struct forte_chip *chip)
226 int i = 10000;
228 while ( (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_PORT_BSY)
229 && i-- )
230 cpu_relax();
232 return i == 0;
237 * forte_ac97_read:
238 * @codec: AC97 codec to read from
239 * @reg: register to read
242 static u16
243 forte_ac97_read (struct ac97_codec *codec, u8 reg)
245 u16 ret = 0;
246 struct forte_chip *chip = codec->private_data;
248 spin_lock (&chip->ac97_lock);
250 /* Knock, knock */
251 if (forte_ac97_wait (chip)) {
252 printk (KERN_ERR PFX "ac97_read: Serial bus busy\n");
253 goto out;
256 /* Send read command */
257 outw (reg | (1<<7), chip->iobase + FORTE_AC97_CMD);
259 if (forte_ac97_wait (chip)) {
260 printk (KERN_ERR PFX "ac97_read: Bus busy reading reg 0x%x\n",
261 reg);
262 goto out;
265 /* Sanity checking */
266 if (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_DP_INVALID) {
267 printk (KERN_ERR PFX "ac97_read: Invalid data port");
268 goto out;
271 /* Fetch result */
272 ret = inw (chip->iobase + FORTE_AC97_DATA);
274 out:
275 spin_unlock (&chip->ac97_lock);
276 return ret;
281 * forte_ac97_write:
282 * @codec: AC97 codec to send command to
283 * @reg: register to write
284 * @val: value to write
287 static void
288 forte_ac97_write (struct ac97_codec *codec, u8 reg, u16 val)
290 struct forte_chip *chip = codec->private_data;
292 spin_lock (&chip->ac97_lock);
294 /* Knock, knock */
295 if (forte_ac97_wait (chip)) {
296 printk (KERN_ERR PFX "ac97_write: Serial bus busy\n");
297 goto out;
300 outw (val, chip->iobase + FORTE_AC97_DATA);
301 outb (reg | FORTE_AC97_WRITE, chip->iobase + FORTE_AC97_CMD);
303 /* Wait for completion */
304 if (forte_ac97_wait (chip)) {
305 printk (KERN_ERR PFX "ac97_write: Bus busy after write\n");
306 goto out;
309 out:
310 spin_unlock (&chip->ac97_lock);
314 /* Mixer ------------------------------------------------------------------- */
318 * forte_mixer_open:
319 * @inode:
320 * @file:
323 static int
324 forte_mixer_open (struct inode *inode, struct file *file)
326 struct forte_chip *chip = forte;
327 file->private_data = chip->ac97;
328 return 0;
333 * forte_mixer_release:
334 * @inode:
335 * @file:
338 static int
339 forte_mixer_release (struct inode *inode, struct file *file)
341 /* We will welease Wodewick */
342 return 0;
347 * forte_mixer_ioctl:
348 * @inode:
349 * @file:
352 static int
353 forte_mixer_ioctl (struct inode *inode, struct file *file,
354 unsigned int cmd, unsigned long arg)
356 struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
358 return codec->mixer_ioctl (codec, cmd, arg);
362 static struct file_operations forte_mixer_fops = {
363 .owner = THIS_MODULE,
364 .llseek = no_llseek,
365 .ioctl = forte_mixer_ioctl,
366 .open = forte_mixer_open,
367 .release = forte_mixer_release,
371 /* Channel ----------------------------------------------------------------- */
373 /**
374 * forte_channel_reset:
375 * @channel: Channel to reset
377 * Locking: Must be called with lock held.
380 static void
381 forte_channel_reset (struct forte_channel *channel)
383 if (!channel || !channel->iobase)
384 return;
386 DPRINTK ("%s: channel = %s\n", __FUNCTION__, channel->name);
388 channel->ctrl &= ~FORTE_START;
389 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
391 /* We always play at least two fragments, hence these defaults */
392 channel->hwptr = channel->frag_sz;
393 channel->next_buf = 1;
394 channel->swptr = 0;
395 channel->filled_frags = 0;
396 channel->active = 0;
397 channel->bytes = 0;
398 channel->nr_irqs = 0;
399 channel->mapped = 0;
400 channel->residue = 0;
404 /**
405 * forte_channel_start:
406 * @channel: Channel to start (record/playback)
408 * Locking: Must be called with lock held.
411 static void inline
412 forte_channel_start (struct forte_channel *channel)
414 if (!channel || !channel->iobase || channel->active)
415 return;
417 channel->ctrl &= ~(FORTE_PAUSE | FORTE_BUF1_LAST | FORTE_BUF2_LAST
418 | FORTE_IMMED_STOP);
419 channel->ctrl |= FORTE_START;
420 channel->active = 1;
421 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
425 /**
426 * forte_channel_stop:
427 * @channel: Channel to stop
429 * Locking: Must be called with lock held.
432 static void inline
433 forte_channel_stop (struct forte_channel *channel)
435 if (!channel || !channel->iobase)
436 return;
438 channel->ctrl &= ~(FORTE_START | FORTE_PAUSE);
439 channel->ctrl |= FORTE_IMMED_STOP;
441 channel->active = 0;
442 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
446 /**
447 * forte_channel_pause:
448 * @channel: Channel to pause
450 * Locking: Must be called with lock held.
453 static void inline
454 forte_channel_pause (struct forte_channel *channel)
456 if (!channel || !channel->iobase)
457 return;
459 channel->ctrl |= FORTE_PAUSE;
461 channel->active = 0;
462 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
466 /**
467 * forte_channel_rate:
468 * @channel: Channel whose rate to set. Playback and record are
469 * independent.
470 * @rate: Channel rate in Hz
472 * Locking: Must be called with lock held.
475 static int
476 forte_channel_rate (struct forte_channel *channel, unsigned int rate)
478 int new_rate;
480 if (!channel || !channel->iobase)
481 return -EINVAL;
483 /* The FM801 only supports a handful of fixed frequencies.
484 * We find the value closest to what userland requested.
486 if (rate <= 6250) { rate = 5500; new_rate = 0; }
487 else if (rate <= 8800) { rate = 8000; new_rate = 1; }
488 else if (rate <= 10312) { rate = 9600; new_rate = 2; }
489 else if (rate <= 13512) { rate = 11025; new_rate = 3; }
490 else if (rate <= 17600) { rate = 16000; new_rate = 4; }
491 else if (rate <= 20625) { rate = 19200; new_rate = 5; }
492 else if (rate <= 27025) { rate = 22050; new_rate = 6; }
493 else if (rate <= 35200) { rate = 32000; new_rate = 7; }
494 else if (rate <= 41250) { rate = 38400; new_rate = 8; }
495 else if (rate <= 46050) { rate = 44100; new_rate = 9; }
496 else { rate = 48000; new_rate = 10; }
498 channel->ctrl &= ~FORTE_RATE_MASK;
499 channel->ctrl |= new_rate << FORTE_RATE_SHIFT;
500 channel->rate = rate;
502 DPRINTK ("%s: %s rate = %d\n", __FUNCTION__, channel->name, rate);
504 return rate;
508 /**
509 * forte_channel_format:
510 * @channel: Channel whose audio format to set
511 * @format: OSS format ID
513 * Locking: Must be called with lock held.
516 static int
517 forte_channel_format (struct forte_channel *channel, int format)
519 if (!channel || !channel->iobase)
520 return -EINVAL;
522 switch (format) {
524 case AFMT_QUERY:
525 break;
527 case AFMT_U8:
528 channel->ctrl &= ~FORTE_16BIT;
529 channel->format = AFMT_U8;
530 break;
532 case AFMT_S16_LE:
533 default:
534 channel->ctrl |= FORTE_16BIT;
535 channel->format = AFMT_S16_LE;
536 break;
539 DPRINTK ("%s: %s want %d format, got %d\n", __FUNCTION__, channel->name,
540 format, channel->format);
542 return channel->format;
546 /**
547 * forte_channel_stereo:
548 * @channel: Channel to toggle
549 * @stereo: 0 for Mono, 1 for Stereo
551 * Locking: Must be called with lock held.
554 static int
555 forte_channel_stereo (struct forte_channel *channel, unsigned int stereo)
557 int ret;
559 if (!channel || !channel->iobase)
560 return -EINVAL;
562 DPRINTK ("%s: %s stereo = %d\n", __FUNCTION__, channel->name, stereo);
564 switch (stereo) {
566 case 0:
567 channel->ctrl &= ~(FORTE_STEREO | FORTE_CHANNELS_MASK);
568 channel-> stereo = stereo;
569 ret = stereo;
570 break;
572 case 1:
573 channel->ctrl &= ~FORTE_CHANNELS_MASK;
574 channel->ctrl |= FORTE_STEREO;
575 channel-> stereo = stereo;
576 ret = stereo;
577 break;
579 default:
580 DPRINTK ("Unsupported channel format");
581 ret = -EINVAL;
582 break;
585 return ret;
589 /**
590 * forte_channel_buffer:
591 * @channel: Channel whose buffer to set up
593 * Locking: Must be called with lock held.
596 static void
597 forte_channel_buffer (struct forte_channel *channel, int sz, int num)
599 unsigned int msecs, shift;
601 /* Go away, I'm busy */
602 if (channel->filled_frags || channel->bytes)
603 return;
605 /* Fragment size must be a power of 2 */
606 shift = 0; sz++;
607 while (sz >>= 1)
608 shift++;
609 channel->frag_sz = 1 << shift;
611 /* Round fragment size to something reasonable */
612 if (channel->frag_sz < FORTE_MIN_FRAG_SIZE)
613 channel->frag_sz = FORTE_MIN_FRAG_SIZE;
615 if (channel->frag_sz > FORTE_MAX_FRAG_SIZE)
616 channel->frag_sz = FORTE_MAX_FRAG_SIZE;
618 /* Find fragment length in milliseconds */
619 msecs = channel->frag_sz /
620 (channel->format == AFMT_S16_LE ? 2 : 1) /
621 (channel->stereo ? 2 : 1) /
622 (channel->rate / 1000);
624 channel->frag_msecs = msecs;
626 /* Pick a suitable number of fragments */
627 if (msecs * num < FORTE_MIN_BUF_MSECS)
628 num = FORTE_MIN_BUF_MSECS / msecs;
630 if (msecs * num > FORTE_MAX_BUF_MSECS)
631 num = FORTE_MAX_BUF_MSECS / msecs;
633 /* Fragment number must be a power of 2 */
634 shift = 0;
635 while (num >>= 1)
636 shift++;
637 channel->frag_num = 1 << (shift + 1);
639 /* Round fragment number to something reasonable */
640 if (channel->frag_num < FORTE_MIN_FRAGMENTS)
641 channel->frag_num = FORTE_MIN_FRAGMENTS;
643 if (channel->frag_num > FORTE_MAX_FRAGMENTS)
644 channel->frag_num = FORTE_MAX_FRAGMENTS;
646 channel->buf_sz = channel->frag_sz * channel->frag_num;
648 DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d\n",
649 __FUNCTION__, channel->name, channel->frag_sz,
650 channel->frag_num, channel->buf_sz);
654 /**
655 * forte_channel_prep:
656 * @channel: Channel whose buffer to prepare
658 * Locking: Lock held.
661 static void
662 forte_channel_prep (struct forte_channel *channel)
664 struct page *page;
665 int i;
667 if (channel->buf)
668 return;
670 forte_channel_buffer (channel, channel->frag_sz, channel->frag_num);
671 channel->buf_pages = channel->buf_sz >> PAGE_SHIFT;
673 if (channel->buf_sz % PAGE_SIZE)
674 channel->buf_pages++;
676 DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d, pg = %d\n",
677 __FUNCTION__, channel->name, channel->frag_sz,
678 channel->frag_num, channel->buf_sz, channel->buf_pages);
680 /* DMA buffer */
681 channel->buf = pci_alloc_consistent (forte->pci_dev,
682 channel->buf_pages * PAGE_SIZE,
683 &channel->buf_handle);
685 if (!channel->buf || !channel->buf_handle)
686 BUG();
688 page = virt_to_page (channel->buf);
690 /* FIXME: can this go away ? */
691 for (i = 0 ; i < channel->buf_pages ; i++)
692 SetPageReserved(page++);
694 /* Prep buffer registers */
695 outw (channel->frag_sz - 1, channel->iobase + FORTE_PLY_COUNT);
696 outl (channel->buf_handle, channel->iobase + FORTE_PLY_BUF1);
697 outl (channel->buf_handle + channel->frag_sz,
698 channel->iobase + FORTE_PLY_BUF2);
700 /* Reset hwptr */
701 channel->hwptr = channel->frag_sz;
702 channel->next_buf = 1;
704 DPRINTK ("%s: %s buffer @ %p (%p)\n", __FUNCTION__, channel->name,
705 channel->buf, channel->buf_handle);
709 /**
710 * forte_channel_drain:
711 * @chip:
712 * @channel:
714 * Locking: Don't hold the lock.
717 static inline int
718 forte_channel_drain (struct forte_channel *channel)
720 DECLARE_WAITQUEUE (wait, current);
721 unsigned long flags;
723 DPRINTK ("%s\n", __FUNCTION__);
725 if (channel->mapped) {
726 spin_lock_irqsave (&forte->lock, flags);
727 forte_channel_stop (channel);
728 spin_unlock_irqrestore (&forte->lock, flags);
729 return 0;
732 spin_lock_irqsave (&forte->lock, flags);
733 add_wait_queue (&channel->wait, &wait);
735 for (;;) {
736 if (channel->active == 0 || channel->filled_frags == 1)
737 break;
739 spin_unlock_irqrestore (&forte->lock, flags);
741 __set_current_state (TASK_INTERRUPTIBLE);
742 schedule();
744 spin_lock_irqsave (&forte->lock, flags);
747 forte_channel_stop (channel);
748 forte_channel_reset (channel);
749 set_current_state (TASK_RUNNING);
750 remove_wait_queue (&channel->wait, &wait);
751 spin_unlock_irqrestore (&forte->lock, flags);
753 return 0;
757 /**
758 * forte_channel_init:
759 * @chip: Forte chip instance the channel hangs off
760 * @channel: Channel to initialize
762 * Description:
763 * Initializes a channel, sets defaults, and allocates
764 * buffers.
766 * Locking: No lock held.
769 static int
770 forte_channel_init (struct forte_chip *chip, struct forte_channel *channel)
772 DPRINTK ("%s: chip iobase @ %p\n", __FUNCTION__, (void *)chip->iobase);
774 spin_lock_irq (&chip->lock);
775 memset (channel, 0x0, sizeof (*channel));
777 if (channel == &chip->play) {
778 channel->name = "PCM_OUT";
779 channel->iobase = chip->iobase;
780 DPRINTK ("%s: PCM-OUT iobase @ %p\n", __FUNCTION__,
781 (void *) channel->iobase);
783 else if (channel == &chip->rec) {
784 channel->name = "PCM_IN";
785 channel->iobase = chip->iobase + FORTE_CAP_OFFSET;
786 channel->record = 1;
787 DPRINTK ("%s: PCM-IN iobase @ %p\n", __FUNCTION__,
788 (void *) channel->iobase);
790 else
791 BUG();
793 init_waitqueue_head (&channel->wait);
795 /* Defaults: 48kHz, 16-bit, stereo */
796 channel->ctrl = inw (channel->iobase + FORTE_PLY_CTRL);
797 forte_channel_reset (channel);
798 forte_channel_stereo (channel, 1);
799 forte_channel_format (channel, AFMT_S16_LE);
800 forte_channel_rate (channel, 48000);
801 channel->frag_sz = FORTE_DEF_FRAG_SIZE;
802 channel->frag_num = FORTE_DEF_FRAGMENTS;
804 chip->trigger = 0;
805 spin_unlock_irq (&chip->lock);
807 return 0;
811 /**
812 * forte_channel_free:
813 * @chip: Chip this channel hangs off
814 * @channel: Channel to nuke
816 * Description:
817 * Resets channel and frees buffers.
819 * Locking: Hold your horses.
822 static void
823 forte_channel_free (struct forte_chip *chip, struct forte_channel *channel)
825 DPRINTK ("%s: %s\n", __FUNCTION__, channel->name);
827 if (!channel->buf_handle)
828 return;
830 pci_free_consistent (chip->pci_dev, channel->buf_pages * PAGE_SIZE,
831 channel->buf, channel->buf_handle);
833 memset (channel, 0x0, sizeof (*channel));
837 /* DSP --------------------------------------------------------------------- */
841 * forte_dsp_ioctl:
844 static int
845 forte_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
846 unsigned long arg)
848 int ival=0, ret, rval=0, rd, wr, count;
849 struct forte_chip *chip;
850 struct audio_buf_info abi;
851 struct count_info cinfo;
852 void __user *argp = (void __user *)arg;
853 int __user *p = argp;
855 chip = file->private_data;
857 if (file->f_mode & FMODE_WRITE)
858 wr = 1;
859 else
860 wr = 0;
862 if (file->f_mode & FMODE_READ)
863 rd = 1;
864 else
865 rd = 0;
867 switch (cmd) {
869 case OSS_GETVERSION:
870 return put_user (SOUND_VERSION, p);
872 case SNDCTL_DSP_GETCAPS:
873 DPRINTK ("%s: GETCAPS\n", __FUNCTION__);
875 ival = FORTE_CAPS; /* DUPLEX */
876 return put_user (ival, p);
878 case SNDCTL_DSP_GETFMTS:
879 DPRINTK ("%s: GETFMTS\n", __FUNCTION__);
881 ival = FORTE_FMTS; /* U8, 16LE */
882 return put_user (ival, p);
884 case SNDCTL_DSP_SETFMT: /* U8, 16LE */
885 DPRINTK ("%s: SETFMT\n", __FUNCTION__);
887 if (get_user (ival, p))
888 return -EFAULT;
890 spin_lock_irq (&chip->lock);
892 if (rd) {
893 forte_channel_stop (&chip->rec);
894 rval = forte_channel_format (&chip->rec, ival);
897 if (wr) {
898 forte_channel_stop (&chip->rec);
899 rval = forte_channel_format (&chip->play, ival);
902 spin_unlock_irq (&chip->lock);
904 return put_user (rval, p);
906 case SNDCTL_DSP_STEREO: /* 0 - mono, 1 - stereo */
907 DPRINTK ("%s: STEREO\n", __FUNCTION__);
909 if (get_user (ival, p))
910 return -EFAULT;
912 spin_lock_irq (&chip->lock);
914 if (rd) {
915 forte_channel_stop (&chip->rec);
916 rval = forte_channel_stereo (&chip->rec, ival);
919 if (wr) {
920 forte_channel_stop (&chip->rec);
921 rval = forte_channel_stereo (&chip->play, ival);
924 spin_unlock_irq (&chip->lock);
926 return put_user (rval, p);
928 case SNDCTL_DSP_CHANNELS: /* 1 - mono, 2 - stereo */
929 DPRINTK ("%s: CHANNELS\n", __FUNCTION__);
931 if (get_user (ival, p))
932 return -EFAULT;
934 spin_lock_irq (&chip->lock);
936 if (rd) {
937 forte_channel_stop (&chip->rec);
938 rval = forte_channel_stereo (&chip->rec, ival-1) + 1;
941 if (wr) {
942 forte_channel_stop (&chip->play);
943 rval = forte_channel_stereo (&chip->play, ival-1) + 1;
946 spin_unlock_irq (&chip->lock);
948 return put_user (rval, p);
950 case SNDCTL_DSP_SPEED:
951 DPRINTK ("%s: SPEED\n", __FUNCTION__);
953 if (get_user (ival, p))
954 return -EFAULT;
956 spin_lock_irq (&chip->lock);
958 if (rd) {
959 forte_channel_stop (&chip->rec);
960 rval = forte_channel_rate (&chip->rec, ival);
963 if (wr) {
964 forte_channel_stop (&chip->play);
965 rval = forte_channel_rate (&chip->play, ival);
968 spin_unlock_irq (&chip->lock);
970 return put_user(rval, p);
972 case SNDCTL_DSP_GETBLKSIZE:
973 DPRINTK ("%s: GETBLKSIZE\n", __FUNCTION__);
975 spin_lock_irq (&chip->lock);
977 if (rd)
978 ival = chip->rec.frag_sz;
980 if (wr)
981 ival = chip->play.frag_sz;
983 spin_unlock_irq (&chip->lock);
985 return put_user (ival, p);
987 case SNDCTL_DSP_RESET:
988 DPRINTK ("%s: RESET\n", __FUNCTION__);
990 spin_lock_irq (&chip->lock);
992 if (rd)
993 forte_channel_reset (&chip->rec);
995 if (wr)
996 forte_channel_reset (&chip->play);
998 spin_unlock_irq (&chip->lock);
1000 return 0;
1002 case SNDCTL_DSP_SYNC:
1003 DPRINTK ("%s: SYNC\n", __FUNCTION__);
1005 if (wr)
1006 ret = forte_channel_drain (&chip->play);
1008 return 0;
1010 case SNDCTL_DSP_POST:
1011 DPRINTK ("%s: POST\n", __FUNCTION__);
1013 if (wr) {
1014 spin_lock_irq (&chip->lock);
1016 if (chip->play.filled_frags)
1017 forte_channel_start (&chip->play);
1019 spin_unlock_irq (&chip->lock);
1022 return 0;
1024 case SNDCTL_DSP_SETFRAGMENT:
1025 DPRINTK ("%s: SETFRAGMENT\n", __FUNCTION__);
1027 if (get_user (ival, p))
1028 return -EFAULT;
1030 spin_lock_irq (&chip->lock);
1032 if (rd) {
1033 forte_channel_buffer (&chip->rec, ival & 0xffff,
1034 (ival >> 16) & 0xffff);
1035 ival = (chip->rec.frag_num << 16) + chip->rec.frag_sz;
1038 if (wr) {
1039 forte_channel_buffer (&chip->play, ival & 0xffff,
1040 (ival >> 16) & 0xffff);
1041 ival = (chip->play.frag_num << 16) +chip->play.frag_sz;
1044 spin_unlock_irq (&chip->lock);
1046 return put_user (ival, p);
1048 case SNDCTL_DSP_GETISPACE:
1049 DPRINTK ("%s: GETISPACE\n", __FUNCTION__);
1051 if (!rd)
1052 return -EINVAL;
1054 spin_lock_irq (&chip->lock);
1056 abi.fragstotal = chip->rec.frag_num;
1057 abi.fragsize = chip->rec.frag_sz;
1059 if (chip->rec.mapped) {
1060 abi.fragments = chip->rec.frag_num - 2;
1061 abi.bytes = abi.fragments * abi.fragsize;
1063 else {
1064 abi.fragments = chip->rec.filled_frags;
1065 abi.bytes = abi.fragments * abi.fragsize;
1068 spin_unlock_irq (&chip->lock);
1070 return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
1072 case SNDCTL_DSP_GETIPTR:
1073 DPRINTK ("%s: GETIPTR\n", __FUNCTION__);
1075 if (!rd)
1076 return -EINVAL;
1078 spin_lock_irq (&chip->lock);
1080 if (chip->rec.active)
1081 cinfo.ptr = chip->rec.hwptr;
1082 else
1083 cinfo.ptr = 0;
1085 cinfo.bytes = chip->rec.bytes;
1086 cinfo.blocks = chip->rec.nr_irqs;
1087 chip->rec.nr_irqs = 0;
1089 spin_unlock_irq (&chip->lock);
1091 return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
1093 case SNDCTL_DSP_GETOSPACE:
1094 if (!wr)
1095 return -EINVAL;
1097 spin_lock_irq (&chip->lock);
1099 abi.fragstotal = chip->play.frag_num;
1100 abi.fragsize = chip->play.frag_sz;
1102 if (chip->play.mapped) {
1103 abi.fragments = chip->play.frag_num - 2;
1104 abi.bytes = chip->play.buf_sz;
1106 else {
1107 abi.fragments = chip->play.frag_num -
1108 chip->play.filled_frags;
1110 if (chip->play.residue)
1111 abi.fragments--;
1113 abi.bytes = abi.fragments * abi.fragsize +
1114 chip->play.residue;
1117 spin_unlock_irq (&chip->lock);
1119 return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
1121 case SNDCTL_DSP_GETOPTR:
1122 if (!wr)
1123 return -EINVAL;
1125 spin_lock_irq (&chip->lock);
1127 if (chip->play.active)
1128 cinfo.ptr = chip->play.hwptr;
1129 else
1130 cinfo.ptr = 0;
1132 cinfo.bytes = chip->play.bytes;
1133 cinfo.blocks = chip->play.nr_irqs;
1134 chip->play.nr_irqs = 0;
1136 spin_unlock_irq (&chip->lock);
1138 return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
1140 case SNDCTL_DSP_GETODELAY:
1141 if (!wr)
1142 return -EINVAL;
1144 spin_lock_irq (&chip->lock);
1146 if (!chip->play.active) {
1147 ival = 0;
1149 else if (chip->play.mapped) {
1150 count = inw (chip->play.iobase + FORTE_PLY_COUNT) + 1;
1151 ival = chip->play.frag_sz - count;
1153 else {
1154 ival = chip->play.filled_frags * chip->play.frag_sz;
1156 if (chip->play.residue)
1157 ival += chip->play.frag_sz - chip->play.residue;
1160 spin_unlock_irq (&chip->lock);
1162 return put_user (ival, p);
1164 case SNDCTL_DSP_SETDUPLEX:
1165 DPRINTK ("%s: SETDUPLEX\n", __FUNCTION__);
1167 return -EINVAL;
1169 case SNDCTL_DSP_GETTRIGGER:
1170 DPRINTK ("%s: GETTRIGGER\n", __FUNCTION__);
1172 return put_user (chip->trigger, p);
1174 case SNDCTL_DSP_SETTRIGGER:
1176 if (get_user (ival, p))
1177 return -EFAULT;
1179 DPRINTK ("%s: SETTRIGGER %d\n", __FUNCTION__, ival);
1181 if (wr) {
1182 spin_lock_irq (&chip->lock);
1184 if (ival & PCM_ENABLE_OUTPUT)
1185 forte_channel_start (&chip->play);
1186 else {
1187 chip->trigger = 1;
1188 forte_channel_prep (&chip->play);
1189 forte_channel_stop (&chip->play);
1192 spin_unlock_irq (&chip->lock);
1194 else if (rd) {
1195 spin_lock_irq (&chip->lock);
1197 if (ival & PCM_ENABLE_INPUT)
1198 forte_channel_start (&chip->rec);
1199 else {
1200 chip->trigger = 1;
1201 forte_channel_prep (&chip->rec);
1202 forte_channel_stop (&chip->rec);
1205 spin_unlock_irq (&chip->lock);
1208 return 0;
1210 case SOUND_PCM_READ_RATE:
1211 DPRINTK ("%s: PCM_READ_RATE\n", __FUNCTION__);
1212 return put_user (chip->play.rate, p);
1214 case SOUND_PCM_READ_CHANNELS:
1215 DPRINTK ("%s: PCM_READ_CHANNELS\n", __FUNCTION__);
1216 return put_user (chip->play.stereo, p);
1218 case SOUND_PCM_READ_BITS:
1219 DPRINTK ("%s: PCM_READ_BITS\n", __FUNCTION__);
1220 return put_user (chip->play.format, p);
1222 case SNDCTL_DSP_NONBLOCK:
1223 DPRINTK ("%s: DSP_NONBLOCK\n", __FUNCTION__);
1224 file->f_flags |= O_NONBLOCK;
1225 return 0;
1227 default:
1228 DPRINTK ("Unsupported ioctl: %x (%p)\n", cmd, argp);
1229 break;
1232 return -EINVAL;
1237 * forte_dsp_open:
1240 static int
1241 forte_dsp_open (struct inode *inode, struct file *file)
1243 struct forte_chip *chip = forte; /* FIXME: HACK FROM HELL! */
1245 if (file->f_flags & O_NONBLOCK) {
1246 if (!mutex_trylock(&chip->open_mutex)) {
1247 DPRINTK ("%s: returning -EAGAIN\n", __FUNCTION__);
1248 return -EAGAIN;
1251 else {
1252 if (mutex_lock_interruptible(&chip->open_mutex)) {
1253 DPRINTK ("%s: returning -ERESTARTSYS\n", __FUNCTION__);
1254 return -ERESTARTSYS;
1258 file->private_data = forte;
1260 DPRINTK ("%s: dsp opened by %d\n", __FUNCTION__, current->pid);
1262 if (file->f_mode & FMODE_WRITE)
1263 forte_channel_init (forte, &forte->play);
1265 if (file->f_mode & FMODE_READ)
1266 forte_channel_init (forte, &forte->rec);
1268 return nonseekable_open(inode, file);
1273 * forte_dsp_release:
1276 static int
1277 forte_dsp_release (struct inode *inode, struct file *file)
1279 struct forte_chip *chip = file->private_data;
1280 int ret = 0;
1282 DPRINTK ("%s: chip @ %p\n", __FUNCTION__, chip);
1284 if (file->f_mode & FMODE_WRITE) {
1285 forte_channel_drain (&chip->play);
1287 spin_lock_irq (&chip->lock);
1289 forte_channel_free (chip, &chip->play);
1291 spin_unlock_irq (&chip->lock);
1294 if (file->f_mode & FMODE_READ) {
1295 while (chip->rec.filled_frags > 0)
1296 interruptible_sleep_on (&chip->rec.wait);
1298 spin_lock_irq (&chip->lock);
1300 forte_channel_stop (&chip->rec);
1301 forte_channel_free (chip, &chip->rec);
1303 spin_unlock_irq (&chip->lock);
1306 mutex_unlock(&chip->open_mutex);
1308 return ret;
1313 * forte_dsp_poll:
1317 static unsigned int
1318 forte_dsp_poll (struct file *file, struct poll_table_struct *wait)
1320 struct forte_chip *chip;
1321 struct forte_channel *channel;
1322 unsigned int mask = 0;
1324 chip = file->private_data;
1326 if (file->f_mode & FMODE_WRITE) {
1327 channel = &chip->play;
1329 if (channel->active)
1330 poll_wait (file, &channel->wait, wait);
1332 spin_lock_irq (&chip->lock);
1334 if (channel->frag_num - channel->filled_frags > 0)
1335 mask |= POLLOUT | POLLWRNORM;
1337 spin_unlock_irq (&chip->lock);
1340 if (file->f_mode & FMODE_READ) {
1341 channel = &chip->rec;
1343 if (channel->active)
1344 poll_wait (file, &channel->wait, wait);
1346 spin_lock_irq (&chip->lock);
1348 if (channel->filled_frags > 0)
1349 mask |= POLLIN | POLLRDNORM;
1351 spin_unlock_irq (&chip->lock);
1354 return mask;
1359 * forte_dsp_mmap:
1362 static int
1363 forte_dsp_mmap (struct file *file, struct vm_area_struct *vma)
1365 struct forte_chip *chip;
1366 struct forte_channel *channel;
1367 unsigned long size;
1368 int ret;
1370 chip = file->private_data;
1372 DPRINTK ("%s: start %lXh, size %ld, pgoff %ld\n", __FUNCTION__,
1373 vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_pgoff);
1375 spin_lock_irq (&chip->lock);
1377 if (vma->vm_flags & VM_WRITE && chip->play.active) {
1378 ret = -EBUSY;
1379 goto out;
1382 if (vma->vm_flags & VM_READ && chip->rec.active) {
1383 ret = -EBUSY;
1384 goto out;
1387 if (file->f_mode & FMODE_WRITE)
1388 channel = &chip->play;
1389 else if (file->f_mode & FMODE_READ)
1390 channel = &chip->rec;
1391 else {
1392 ret = -EINVAL;
1393 goto out;
1396 forte_channel_prep (channel);
1397 channel->mapped = 1;
1399 if (vma->vm_pgoff != 0) {
1400 ret = -EINVAL;
1401 goto out;
1404 size = vma->vm_end - vma->vm_start;
1406 if (size > channel->buf_pages * PAGE_SIZE) {
1407 DPRINTK ("%s: size (%ld) > buf_sz (%d) \n", __FUNCTION__,
1408 size, channel->buf_sz);
1409 ret = -EINVAL;
1410 goto out;
1413 if (remap_pfn_range(vma, vma->vm_start,
1414 virt_to_phys(channel->buf) >> PAGE_SHIFT,
1415 size, vma->vm_page_prot)) {
1416 DPRINTK ("%s: remap el a no worko\n", __FUNCTION__);
1417 ret = -EAGAIN;
1418 goto out;
1421 ret = 0;
1423 out:
1424 spin_unlock_irq (&chip->lock);
1425 return ret;
1430 * forte_dsp_write:
1433 static ssize_t
1434 forte_dsp_write (struct file *file, const char __user *buffer, size_t bytes,
1435 loff_t *ppos)
1437 struct forte_chip *chip;
1438 struct forte_channel *channel;
1439 unsigned int i = bytes, sz = 0;
1440 unsigned long flags;
1442 if (!access_ok (VERIFY_READ, buffer, bytes))
1443 return -EFAULT;
1445 chip = (struct forte_chip *) file->private_data;
1447 if (!chip)
1448 BUG();
1450 channel = &chip->play;
1452 if (!channel)
1453 BUG();
1455 spin_lock_irqsave (&chip->lock, flags);
1457 /* Set up buffers with the right fragment size */
1458 forte_channel_prep (channel);
1460 while (i) {
1461 /* All fragment buffers in use -> wait */
1462 if (channel->frag_num - channel->filled_frags == 0) {
1463 DECLARE_WAITQUEUE (wait, current);
1465 /* For trigger or non-blocking operation, get out */
1466 if (chip->trigger || file->f_flags & O_NONBLOCK) {
1467 spin_unlock_irqrestore (&chip->lock, flags);
1468 return -EAGAIN;
1471 /* Otherwise wait for buffers */
1472 add_wait_queue (&channel->wait, &wait);
1474 for (;;) {
1475 spin_unlock_irqrestore (&chip->lock, flags);
1477 set_current_state (TASK_INTERRUPTIBLE);
1478 schedule();
1480 spin_lock_irqsave (&chip->lock, flags);
1482 if (channel->frag_num - channel->filled_frags)
1483 break;
1486 remove_wait_queue (&channel->wait, &wait);
1487 set_current_state (TASK_RUNNING);
1489 if (signal_pending (current)) {
1490 spin_unlock_irqrestore (&chip->lock, flags);
1491 return -ERESTARTSYS;
1495 if (channel->residue)
1496 sz = channel->residue;
1497 else if (i > channel->frag_sz)
1498 sz = channel->frag_sz;
1499 else
1500 sz = i;
1502 spin_unlock_irqrestore (&chip->lock, flags);
1504 if (copy_from_user ((void *) channel->buf + channel->swptr, buffer, sz))
1505 return -EFAULT;
1507 spin_lock_irqsave (&chip->lock, flags);
1509 /* Advance software pointer */
1510 buffer += sz;
1511 channel->swptr += sz;
1512 channel->swptr %= channel->buf_sz;
1513 i -= sz;
1515 /* Only bump filled_frags if a full fragment has been written */
1516 if (channel->swptr % channel->frag_sz == 0) {
1517 channel->filled_frags++;
1518 channel->residue = 0;
1520 else
1521 channel->residue = channel->frag_sz - sz;
1523 /* If playback isn't active, start it */
1524 if (channel->active == 0 && chip->trigger == 0)
1525 forte_channel_start (channel);
1528 spin_unlock_irqrestore (&chip->lock, flags);
1530 return bytes - i;
1535 * forte_dsp_read:
1538 static ssize_t
1539 forte_dsp_read (struct file *file, char __user *buffer, size_t bytes,
1540 loff_t *ppos)
1542 struct forte_chip *chip;
1543 struct forte_channel *channel;
1544 unsigned int i = bytes, sz;
1545 unsigned long flags;
1547 if (!access_ok (VERIFY_WRITE, buffer, bytes))
1548 return -EFAULT;
1550 chip = (struct forte_chip *) file->private_data;
1552 if (!chip)
1553 BUG();
1555 channel = &chip->rec;
1557 if (!channel)
1558 BUG();
1560 spin_lock_irqsave (&chip->lock, flags);
1562 /* Set up buffers with the right fragment size */
1563 forte_channel_prep (channel);
1565 /* Start recording */
1566 if (!chip->trigger)
1567 forte_channel_start (channel);
1569 while (i) {
1570 /* No fragment buffers in use -> wait */
1571 if (channel->filled_frags == 0) {
1572 DECLARE_WAITQUEUE (wait, current);
1574 /* For trigger mode operation, get out */
1575 if (chip->trigger) {
1576 spin_unlock_irqrestore (&chip->lock, flags);
1577 return -EAGAIN;
1580 add_wait_queue (&channel->wait, &wait);
1582 for (;;) {
1583 if (channel->active == 0)
1584 break;
1586 if (channel->filled_frags)
1587 break;
1589 spin_unlock_irqrestore (&chip->lock, flags);
1591 set_current_state (TASK_INTERRUPTIBLE);
1592 schedule();
1594 spin_lock_irqsave (&chip->lock, flags);
1597 set_current_state (TASK_RUNNING);
1598 remove_wait_queue (&channel->wait, &wait);
1601 if (i > channel->frag_sz)
1602 sz = channel->frag_sz;
1603 else
1604 sz = i;
1606 spin_unlock_irqrestore (&chip->lock, flags);
1608 if (copy_to_user (buffer, (void *)channel->buf+channel->swptr, sz)) {
1609 DPRINTK ("%s: copy_to_user failed\n", __FUNCTION__);
1610 return -EFAULT;
1613 spin_lock_irqsave (&chip->lock, flags);
1615 /* Advance software pointer */
1616 buffer += sz;
1617 if (channel->filled_frags > 0)
1618 channel->filled_frags--;
1619 channel->swptr += channel->frag_sz;
1620 channel->swptr %= channel->buf_sz;
1621 i -= sz;
1624 spin_unlock_irqrestore (&chip->lock, flags);
1626 return bytes - i;
1630 static struct file_operations forte_dsp_fops = {
1631 .owner = THIS_MODULE,
1632 .llseek = &no_llseek,
1633 .read = &forte_dsp_read,
1634 .write = &forte_dsp_write,
1635 .poll = &forte_dsp_poll,
1636 .ioctl = &forte_dsp_ioctl,
1637 .open = &forte_dsp_open,
1638 .release = &forte_dsp_release,
1639 .mmap = &forte_dsp_mmap,
1643 /* Common ------------------------------------------------------------------ */
1647 * forte_interrupt:
1650 static irqreturn_t
1651 forte_interrupt (int irq, void *dev_id, struct pt_regs *regs)
1653 struct forte_chip *chip = dev_id;
1654 struct forte_channel *channel = NULL;
1655 u16 status, count;
1657 status = inw (chip->iobase + FORTE_IRQ_STATUS);
1659 /* If this is not for us, get outta here ASAP */
1660 if ((status & (FORTE_IRQ_PLAYBACK | FORTE_IRQ_CAPTURE)) == 0)
1661 return IRQ_NONE;
1663 if (status & FORTE_IRQ_PLAYBACK) {
1664 channel = &chip->play;
1666 spin_lock (&chip->lock);
1668 if (channel->frag_sz == 0)
1669 goto pack;
1671 /* Declare a fragment done */
1672 if (channel->filled_frags > 0)
1673 channel->filled_frags--;
1674 channel->bytes += channel->frag_sz;
1675 channel->nr_irqs++;
1677 /* Flip-flop between buffer I and II */
1678 channel->next_buf ^= 1;
1680 /* Advance hardware pointer by fragment size and wrap around */
1681 channel->hwptr += channel->frag_sz;
1682 channel->hwptr %= channel->buf_sz;
1684 /* Buffer I or buffer II BAR */
1685 outl (channel->buf_handle + channel->hwptr,
1686 channel->next_buf == 0 ?
1687 channel->iobase + FORTE_PLY_BUF1 :
1688 channel->iobase + FORTE_PLY_BUF2);
1690 /* If the currently playing fragment is last, schedule pause */
1691 if (channel->filled_frags == 1)
1692 forte_channel_pause (channel);
1694 pack:
1695 /* Acknowledge interrupt */
1696 outw (FORTE_IRQ_PLAYBACK, chip->iobase + FORTE_IRQ_STATUS);
1698 if (waitqueue_active (&channel->wait))
1699 wake_up_all (&channel->wait);
1701 spin_unlock (&chip->lock);
1704 if (status & FORTE_IRQ_CAPTURE) {
1705 channel = &chip->rec;
1706 spin_lock (&chip->lock);
1708 /* One fragment filled */
1709 channel->filled_frags++;
1711 /* Get # of completed bytes */
1712 count = inw (channel->iobase + FORTE_PLY_COUNT) + 1;
1714 if (count == 0) {
1715 DPRINTK ("%s: last, filled_frags = %d\n", __FUNCTION__,
1716 channel->filled_frags);
1717 channel->filled_frags = 0;
1718 goto rack;
1721 /* Buffer I or buffer II BAR */
1722 outl (channel->buf_handle + channel->hwptr,
1723 channel->next_buf == 0 ?
1724 channel->iobase + FORTE_PLY_BUF1 :
1725 channel->iobase + FORTE_PLY_BUF2);
1727 /* Flip-flop between buffer I and II */
1728 channel->next_buf ^= 1;
1730 /* Advance hardware pointer by fragment size and wrap around */
1731 channel->hwptr += channel->frag_sz;
1732 channel->hwptr %= channel->buf_sz;
1734 /* Out of buffers */
1735 if (channel->filled_frags == channel->frag_num - 1)
1736 forte_channel_stop (channel);
1737 rack:
1738 /* Acknowledge interrupt */
1739 outw (FORTE_IRQ_CAPTURE, chip->iobase + FORTE_IRQ_STATUS);
1741 spin_unlock (&chip->lock);
1743 if (waitqueue_active (&channel->wait))
1744 wake_up_all (&channel->wait);
1747 return IRQ_HANDLED;
1752 * forte_proc_read:
1755 static int
1756 forte_proc_read (char *page, char **start, off_t off, int count,
1757 int *eof, void *data)
1759 int i = 0, p_rate, p_chan, r_rate;
1760 unsigned short p_reg, r_reg;
1762 i += sprintf (page, "ForteMedia FM801 OSS Lite driver\n%s\n \n",
1763 DRIVER_VERSION);
1765 if (!forte->iobase)
1766 return i;
1768 p_rate = p_chan = -1;
1769 p_reg = inw (forte->iobase + FORTE_PLY_CTRL);
1770 p_rate = (p_reg >> 8) & 15;
1771 p_chan = (p_reg >> 12) & 3;
1773 if (p_rate >= 0 || p_rate <= 10)
1774 p_rate = rates[p_rate];
1776 if (p_chan >= 0 || p_chan <= 2)
1777 p_chan = channels[p_chan];
1779 r_rate = -1;
1780 r_reg = inw (forte->iobase + FORTE_CAP_CTRL);
1781 r_rate = (r_reg >> 8) & 15;
1783 if (r_rate >= 0 || r_rate <= 10)
1784 r_rate = rates[r_rate];
1786 i += sprintf (page + i,
1787 " Playback Capture\n"
1788 "FIFO empty : %-3s %-3s\n"
1789 "Buf1 Last : %-3s %-3s\n"
1790 "Buf2 Last : %-3s %-3s\n"
1791 "Started : %-3s %-3s\n"
1792 "Paused : %-3s %-3s\n"
1793 "Immed Stop : %-3s %-3s\n"
1794 "Rate : %-5d %-5d\n"
1795 "Channels : %-5d -\n"
1796 "16-bit : %-3s %-3s\n"
1797 "Stereo : %-3s %-3s\n"
1798 " \n"
1799 "Buffer Sz : %-6d %-6d\n"
1800 "Frag Sz : %-6d %-6d\n"
1801 "Frag Num : %-6d %-6d\n"
1802 "Frag msecs : %-6d %-6d\n"
1803 "Used Frags : %-6d %-6d\n"
1804 "Mapped : %-3s %-3s\n",
1805 p_reg & 1<<0 ? "yes" : "no",
1806 r_reg & 1<<0 ? "yes" : "no",
1807 p_reg & 1<<1 ? "yes" : "no",
1808 r_reg & 1<<1 ? "yes" : "no",
1809 p_reg & 1<<2 ? "yes" : "no",
1810 r_reg & 1<<2 ? "yes" : "no",
1811 p_reg & 1<<5 ? "yes" : "no",
1812 r_reg & 1<<5 ? "yes" : "no",
1813 p_reg & 1<<6 ? "yes" : "no",
1814 r_reg & 1<<6 ? "yes" : "no",
1815 p_reg & 1<<7 ? "yes" : "no",
1816 r_reg & 1<<7 ? "yes" : "no",
1817 p_rate, r_rate,
1818 p_chan,
1819 p_reg & 1<<14 ? "yes" : "no",
1820 r_reg & 1<<14 ? "yes" : "no",
1821 p_reg & 1<<15 ? "yes" : "no",
1822 r_reg & 1<<15 ? "yes" : "no",
1823 forte->play.buf_sz, forte->rec.buf_sz,
1824 forte->play.frag_sz, forte->rec.frag_sz,
1825 forte->play.frag_num, forte->rec.frag_num,
1826 forte->play.frag_msecs, forte->rec.frag_msecs,
1827 forte->play.filled_frags, forte->rec.filled_frags,
1828 forte->play.mapped ? "yes" : "no",
1829 forte->rec.mapped ? "yes" : "no"
1832 return i;
1837 * forte_proc_init:
1839 * Creates driver info entries in /proc
1842 static int __init
1843 forte_proc_init (void)
1845 if (!proc_mkdir ("driver/forte", NULL))
1846 return -EIO;
1848 if (!create_proc_read_entry ("driver/forte/chip", 0, NULL, forte_proc_read, forte)) {
1849 remove_proc_entry ("driver/forte", NULL);
1850 return -EIO;
1853 if (!create_proc_read_entry("driver/forte/ac97", 0, NULL, ac97_read_proc, forte->ac97)) {
1854 remove_proc_entry ("driver/forte/chip", NULL);
1855 remove_proc_entry ("driver/forte", NULL);
1856 return -EIO;
1859 return 0;
1864 * forte_proc_remove:
1866 * Removes driver info entries in /proc
1869 static void
1870 forte_proc_remove (void)
1872 remove_proc_entry ("driver/forte/ac97", NULL);
1873 remove_proc_entry ("driver/forte/chip", NULL);
1874 remove_proc_entry ("driver/forte", NULL);
1879 * forte_chip_init:
1880 * @chip: Chip instance to initialize
1882 * Description:
1883 * Resets chip, configures codec and registers the driver with
1884 * the sound subsystem.
1886 * Press and hold Start for 8 secs, then switch on Run
1887 * and hold for 4 seconds. Let go of Start. Numbers
1888 * assume a properly oiled TWG.
1891 static int __devinit
1892 forte_chip_init (struct forte_chip *chip)
1894 u8 revision;
1895 u16 cmdw;
1896 struct ac97_codec *codec;
1898 pci_read_config_byte (chip->pci_dev, PCI_REVISION_ID, &revision);
1900 if (revision >= 0xB1) {
1901 chip->multichannel = 1;
1902 printk (KERN_INFO PFX "Multi-channel device detected.\n");
1905 /* Reset chip */
1906 outw (FORTE_CC_CODEC_RESET | FORTE_CC_AC97_RESET,
1907 chip->iobase + FORTE_CODEC_CTRL);
1908 udelay(100);
1909 outw (0, chip->iobase + FORTE_CODEC_CTRL);
1911 /* Request read from AC97 */
1912 outw (FORTE_AC97_READ | (0 << FORTE_AC97_ADDR_SHIFT),
1913 chip->iobase + FORTE_AC97_CMD);
1914 mdelay(750);
1916 if ((inw (chip->iobase + FORTE_AC97_CMD) & (3<<8)) != (1<<8)) {
1917 printk (KERN_INFO PFX "AC97 codec not responding");
1918 return -EIO;
1921 /* Init volume */
1922 outw (0x0808, chip->iobase + FORTE_PCM_VOL);
1923 outw (0x9f1f, chip->iobase + FORTE_FM_VOL);
1924 outw (0x8808, chip->iobase + FORTE_I2S_VOL);
1926 /* I2S control - I2S mode */
1927 outw (0x0003, chip->iobase + FORTE_I2S_MODE);
1929 /* Interrupt setup - unmask PLAYBACK & CAPTURE */
1930 cmdw = inw (chip->iobase + FORTE_IRQ_MASK);
1931 cmdw &= ~0x0003;
1932 outw (cmdw, chip->iobase + FORTE_IRQ_MASK);
1934 /* Interrupt clear */
1935 outw (FORTE_IRQ_PLAYBACK|FORTE_IRQ_CAPTURE,
1936 chip->iobase + FORTE_IRQ_STATUS);
1938 /* Set up the AC97 codec */
1939 if ((codec = ac97_alloc_codec()) == NULL)
1940 return -ENOMEM;
1941 codec->private_data = chip;
1942 codec->codec_read = forte_ac97_read;
1943 codec->codec_write = forte_ac97_write;
1944 codec->id = 0;
1946 if (ac97_probe_codec (codec) == 0) {
1947 printk (KERN_ERR PFX "codec probe failed\n");
1948 ac97_release_codec(codec);
1949 return -1;
1952 /* Register mixer */
1953 if ((codec->dev_mixer =
1954 register_sound_mixer (&forte_mixer_fops, -1)) < 0) {
1955 printk (KERN_ERR PFX "couldn't register mixer!\n");
1956 ac97_release_codec(codec);
1957 return -1;
1960 chip->ac97 = codec;
1962 /* Register DSP */
1963 if ((chip->dsp = register_sound_dsp (&forte_dsp_fops, -1) ) < 0) {
1964 printk (KERN_ERR PFX "couldn't register dsp!\n");
1965 return -1;
1968 /* Register with /proc */
1969 if (forte_proc_init()) {
1970 printk (KERN_ERR PFX "couldn't add entries to /proc!\n");
1971 return -1;
1974 return 0;
1979 * forte_probe:
1980 * @pci_dev: PCI struct for probed device
1981 * @pci_id:
1983 * Description:
1984 * Allocates chip instance, I/O region, and IRQ
1986 static int __init
1987 forte_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
1989 struct forte_chip *chip;
1990 int ret = 0;
1992 /* FIXME: Support more than one chip */
1993 if (found++)
1994 return -EIO;
1996 /* Ignition */
1997 if (pci_enable_device (pci_dev))
1998 return -EIO;
2000 pci_set_master (pci_dev);
2002 /* Allocate chip instance and configure */
2003 forte = (struct forte_chip *)
2004 kmalloc (sizeof (struct forte_chip), GFP_KERNEL);
2005 chip = forte;
2007 if (chip == NULL) {
2008 printk (KERN_WARNING PFX "Out of memory");
2009 return -ENOMEM;
2012 memset (chip, 0, sizeof (struct forte_chip));
2013 chip->pci_dev = pci_dev;
2015 mutex_init(&chip->open_mutex);
2016 spin_lock_init (&chip->lock);
2017 spin_lock_init (&chip->ac97_lock);
2019 if (! request_region (pci_resource_start (pci_dev, 0),
2020 pci_resource_len (pci_dev, 0), DRIVER_NAME)) {
2021 printk (KERN_WARNING PFX "Unable to reserve I/O space");
2022 ret = -ENOMEM;
2023 goto error;
2026 chip->iobase = pci_resource_start (pci_dev, 0);
2027 chip->irq = pci_dev->irq;
2029 if (request_irq (chip->irq, forte_interrupt, SA_SHIRQ, DRIVER_NAME,
2030 chip)) {
2031 printk (KERN_WARNING PFX "Unable to reserve IRQ");
2032 ret = -EIO;
2033 goto error;
2036 pci_set_drvdata (pci_dev, chip);
2038 printk (KERN_INFO PFX "FM801 chip found at 0x%04lX-0x%04lX IRQ %u\n",
2039 chip->iobase, pci_resource_end (pci_dev, 0), chip->irq);
2041 /* Power it up */
2042 if ((ret = forte_chip_init (chip)) == 0)
2043 return 0;
2045 error:
2046 if (chip->irq)
2047 free_irq (chip->irq, chip);
2049 if (chip->iobase)
2050 release_region (pci_resource_start (pci_dev, 0),
2051 pci_resource_len (pci_dev, 0));
2053 kfree (chip);
2055 return ret;
2060 * forte_remove:
2061 * @pci_dev: PCI device to unclaim
2065 static void
2066 forte_remove (struct pci_dev *pci_dev)
2068 struct forte_chip *chip = pci_get_drvdata (pci_dev);
2070 if (chip == NULL)
2071 return;
2073 /* Turn volume down to avoid popping */
2074 outw (0x1f1f, chip->iobase + FORTE_PCM_VOL);
2075 outw (0x1f1f, chip->iobase + FORTE_FM_VOL);
2076 outw (0x1f1f, chip->iobase + FORTE_I2S_VOL);
2078 forte_proc_remove();
2079 free_irq (chip->irq, chip);
2080 release_region (chip->iobase, pci_resource_len (pci_dev, 0));
2082 unregister_sound_dsp (chip->dsp);
2083 unregister_sound_mixer (chip->ac97->dev_mixer);
2084 ac97_release_codec(chip->ac97);
2085 kfree (chip);
2087 printk (KERN_INFO PFX "driver released\n");
2091 static struct pci_device_id forte_pci_ids[] = {
2092 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2093 { 0, }
2097 static struct pci_driver forte_pci_driver = {
2098 .name = DRIVER_NAME,
2099 .id_table = forte_pci_ids,
2100 .probe = forte_probe,
2101 .remove = forte_remove,
2107 * forte_init_module:
2111 static int __init
2112 forte_init_module (void)
2114 printk (KERN_INFO PFX DRIVER_VERSION "\n");
2116 return pci_register_driver (&forte_pci_driver);
2121 * forte_cleanup_module:
2125 static void __exit
2126 forte_cleanup_module (void)
2128 pci_unregister_driver (&forte_pci_driver);
2132 module_init(forte_init_module);
2133 module_exit(forte_cleanup_module);
2135 MODULE_AUTHOR("Martin K. Petersen <mkp@mkp.net>");
2136 MODULE_DESCRIPTION("ForteMedia FM801 OSS Driver");
2137 MODULE_LICENSE("GPL");
2138 MODULE_DEVICE_TABLE (pci, forte_pci_ids);