blob: 0294eec8ad9035ee621f516df74d692bab95faff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ingo Molnar910f5d22006-03-23 03:00:39 -080046#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/uaccess.h>
49#include <asm/io.h>
50
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 ": "
54
55#undef M_DEBUG
56
57#ifdef M_DEBUG
58#define DPRINTK(args...) printk(KERN_WARNING args)
59#else
60#define DPRINTK(args...)
61#endif
62
63/* Card capabilities */
64#define FORTE_CAPS (DSP_CAP_MMAP | DSP_CAP_TRIGGER)
65
66/* Supported audio formats */
67#define FORTE_FMTS (AFMT_U8 | AFMT_S16_LE)
68
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
78
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 */
109
110#define FORTE_CAP_OFFSET FORTE_CAP_CTRL - FORTE_PLY_CTRL
111
112#define FORTE_AC97_ADDR_SHIFT 10
113
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)
128
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)
134
135/* CODEC control */
136#define FORTE_CC_CODEC_RESET (1<<5)
137#define FORTE_CC_AC97_RESET (1<<6)
138
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)
146
147
148struct forte_channel {
149 const char *name;
150
151 unsigned short ctrl; /* Ctrl BAR contents */
152 unsigned long iobase; /* Ctrl BAR address */
153
154 wait_queue_head_t wait;
155
156 void *buf; /* Buffer */
157 dma_addr_t buf_handle; /* Buffer handle */
158
159 unsigned int record;
160 unsigned int format;
161 unsigned int rate;
162 unsigned int stereo;
163
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 */
168
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 */
173
174 unsigned int active; /* Channel currently in use */
175 unsigned int mapped; /* mmap */
176
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 */
181};
182
183
184struct forte_chip {
185 struct pci_dev *pci_dev;
186 unsigned long iobase;
187 int irq;
188
Ingo Molnar910f5d22006-03-23 03:00:39 -0800189 struct mutex open_mutex; /* Device access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spinlock_t lock; /* State */
191
192 spinlock_t ac97_lock;
193 struct ac97_codec *ac97;
194
195 int multichannel;
196 int dsp; /* OSS handle */
197 int trigger; /* mmap I/O trigger */
198
199 struct forte_channel play;
200 struct forte_channel rec;
201};
202
203
204static int channels[] = { 2, 4, 6, };
205static int rates[] = { 5500, 8000, 9600, 11025, 16000, 19200,
206 22050, 32000, 38400, 44100, 48000, };
207
208static struct forte_chip *forte;
209static int found;
210
211
212/* AC97 Codec -------------------------------------------------------------- */
213
214
215/**
216 * forte_ac97_wait:
217 * @chip: fm801 instance whose AC97 codec to wait on
218 *
219 * FIXME:
220 * Stop busy-waiting
221 */
222
223static inline int
224forte_ac97_wait (struct forte_chip *chip)
225{
226 int i = 10000;
227
228 while ( (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_PORT_BSY)
229 && i-- )
230 cpu_relax();
231
232 return i == 0;
233}
234
235
236/**
237 * forte_ac97_read:
238 * @codec: AC97 codec to read from
239 * @reg: register to read
240 */
241
242static u16
243forte_ac97_read (struct ac97_codec *codec, u8 reg)
244{
245 u16 ret = 0;
246 struct forte_chip *chip = codec->private_data;
247
248 spin_lock (&chip->ac97_lock);
249
250 /* Knock, knock */
251 if (forte_ac97_wait (chip)) {
252 printk (KERN_ERR PFX "ac97_read: Serial bus busy\n");
253 goto out;
254 }
255
256 /* Send read command */
257 outw (reg | (1<<7), chip->iobase + FORTE_AC97_CMD);
258
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;
263 }
264
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;
269 }
270
271 /* Fetch result */
272 ret = inw (chip->iobase + FORTE_AC97_DATA);
273
274 out:
275 spin_unlock (&chip->ac97_lock);
276 return ret;
277}
278
279
280/**
281 * forte_ac97_write:
282 * @codec: AC97 codec to send command to
283 * @reg: register to write
284 * @val: value to write
285 */
286
287static void
288forte_ac97_write (struct ac97_codec *codec, u8 reg, u16 val)
289{
290 struct forte_chip *chip = codec->private_data;
291
292 spin_lock (&chip->ac97_lock);
293
294 /* Knock, knock */
295 if (forte_ac97_wait (chip)) {
296 printk (KERN_ERR PFX "ac97_write: Serial bus busy\n");
297 goto out;
298 }
299
300 outw (val, chip->iobase + FORTE_AC97_DATA);
301 outb (reg | FORTE_AC97_WRITE, chip->iobase + FORTE_AC97_CMD);
302
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;
307 }
308
309 out:
310 spin_unlock (&chip->ac97_lock);
311}
312
313
314/* Mixer ------------------------------------------------------------------- */
315
316
317/**
318 * forte_mixer_open:
319 * @inode:
320 * @file:
321 */
322
323static int
324forte_mixer_open (struct inode *inode, struct file *file)
325{
326 struct forte_chip *chip = forte;
327 file->private_data = chip->ac97;
328 return 0;
329}
330
331
332/**
333 * forte_mixer_release:
334 * @inode:
335 * @file:
336 */
337
338static int
339forte_mixer_release (struct inode *inode, struct file *file)
340{
341 /* We will welease Wodewick */
342 return 0;
343}
344
345
346/**
347 * forte_mixer_ioctl:
348 * @inode:
349 * @file:
350 */
351
352static int
353forte_mixer_ioctl (struct inode *inode, struct file *file,
354 unsigned int cmd, unsigned long arg)
355{
356 struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
357
358 return codec->mixer_ioctl (codec, cmd, arg);
359}
360
361
362static 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,
368};
369
370
371/* Channel ----------------------------------------------------------------- */
372
373/**
374 * forte_channel_reset:
375 * @channel: Channel to reset
376 *
377 * Locking: Must be called with lock held.
378 */
379
380static void
381forte_channel_reset (struct forte_channel *channel)
382{
383 if (!channel || !channel->iobase)
384 return;
385
386 DPRINTK ("%s: channel = %s\n", __FUNCTION__, channel->name);
387
388 channel->ctrl &= ~FORTE_START;
389 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
390
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;
401}
402
403
404/**
405 * forte_channel_start:
406 * @channel: Channel to start (record/playback)
407 *
408 * Locking: Must be called with lock held.
409 */
410
411static void inline
412forte_channel_start (struct forte_channel *channel)
413{
414 if (!channel || !channel->iobase || channel->active)
415 return;
416
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);
422}
423
424
425/**
426 * forte_channel_stop:
427 * @channel: Channel to stop
428 *
429 * Locking: Must be called with lock held.
430 */
431
432static void inline
433forte_channel_stop (struct forte_channel *channel)
434{
435 if (!channel || !channel->iobase)
436 return;
437
438 channel->ctrl &= ~(FORTE_START | FORTE_PAUSE);
439 channel->ctrl |= FORTE_IMMED_STOP;
440
441 channel->active = 0;
442 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
443}
444
445
446/**
447 * forte_channel_pause:
448 * @channel: Channel to pause
449 *
450 * Locking: Must be called with lock held.
451 */
452
453static void inline
454forte_channel_pause (struct forte_channel *channel)
455{
456 if (!channel || !channel->iobase)
457 return;
458
459 channel->ctrl |= FORTE_PAUSE;
460
461 channel->active = 0;
462 outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
463}
464
465
466/**
467 * forte_channel_rate:
468 * @channel: Channel whose rate to set. Playback and record are
469 * independent.
470 * @rate: Channel rate in Hz
471 *
472 * Locking: Must be called with lock held.
473 */
474
475static int
476forte_channel_rate (struct forte_channel *channel, unsigned int rate)
477{
478 int new_rate;
479
480 if (!channel || !channel->iobase)
481 return -EINVAL;
482
483 /* The FM801 only supports a handful of fixed frequencies.
484 * We find the value closest to what userland requested.
485 */
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; }
497
498 channel->ctrl &= ~FORTE_RATE_MASK;
499 channel->ctrl |= new_rate << FORTE_RATE_SHIFT;
500 channel->rate = rate;
501
502 DPRINTK ("%s: %s rate = %d\n", __FUNCTION__, channel->name, rate);
503
504 return rate;
505}
506
507
508/**
509 * forte_channel_format:
510 * @channel: Channel whose audio format to set
511 * @format: OSS format ID
512 *
513 * Locking: Must be called with lock held.
514 */
515
516static int
517forte_channel_format (struct forte_channel *channel, int format)
518{
519 if (!channel || !channel->iobase)
520 return -EINVAL;
521
522 switch (format) {
523
524 case AFMT_QUERY:
525 break;
526
527 case AFMT_U8:
528 channel->ctrl &= ~FORTE_16BIT;
529 channel->format = AFMT_U8;
530 break;
531
532 case AFMT_S16_LE:
533 default:
534 channel->ctrl |= FORTE_16BIT;
535 channel->format = AFMT_S16_LE;
536 break;
537 }
538
539 DPRINTK ("%s: %s want %d format, got %d\n", __FUNCTION__, channel->name,
540 format, channel->format);
541
542 return channel->format;
543}
544
545
546/**
547 * forte_channel_stereo:
548 * @channel: Channel to toggle
549 * @stereo: 0 for Mono, 1 for Stereo
550 *
551 * Locking: Must be called with lock held.
552 */
553
554static int
555forte_channel_stereo (struct forte_channel *channel, unsigned int stereo)
556{
557 int ret;
558
559 if (!channel || !channel->iobase)
560 return -EINVAL;
561
562 DPRINTK ("%s: %s stereo = %d\n", __FUNCTION__, channel->name, stereo);
563
564 switch (stereo) {
565
566 case 0:
567 channel->ctrl &= ~(FORTE_STEREO | FORTE_CHANNELS_MASK);
568 channel-> stereo = stereo;
569 ret = stereo;
570 break;
571
572 case 1:
573 channel->ctrl &= ~FORTE_CHANNELS_MASK;
574 channel->ctrl |= FORTE_STEREO;
575 channel-> stereo = stereo;
576 ret = stereo;
577 break;
578
579 default:
580 DPRINTK ("Unsupported channel format");
581 ret = -EINVAL;
582 break;
583 }
584
585 return ret;
586}
587
588
589/**
590 * forte_channel_buffer:
591 * @channel: Channel whose buffer to set up
592 *
593 * Locking: Must be called with lock held.
594 */
595
596static void
597forte_channel_buffer (struct forte_channel *channel, int sz, int num)
598{
599 unsigned int msecs, shift;
600
601 /* Go away, I'm busy */
602 if (channel->filled_frags || channel->bytes)
603 return;
604
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;
610
611 /* Round fragment size to something reasonable */
612 if (channel->frag_sz < FORTE_MIN_FRAG_SIZE)
613 channel->frag_sz = FORTE_MIN_FRAG_SIZE;
614
615 if (channel->frag_sz > FORTE_MAX_FRAG_SIZE)
616 channel->frag_sz = FORTE_MAX_FRAG_SIZE;
617
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);
623
624 channel->frag_msecs = msecs;
625
626 /* Pick a suitable number of fragments */
627 if (msecs * num < FORTE_MIN_BUF_MSECS)
628 num = FORTE_MIN_BUF_MSECS / msecs;
629
630 if (msecs * num > FORTE_MAX_BUF_MSECS)
631 num = FORTE_MAX_BUF_MSECS / msecs;
632
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);
638
639 /* Round fragment number to something reasonable */
640 if (channel->frag_num < FORTE_MIN_FRAGMENTS)
641 channel->frag_num = FORTE_MIN_FRAGMENTS;
642
643 if (channel->frag_num > FORTE_MAX_FRAGMENTS)
644 channel->frag_num = FORTE_MAX_FRAGMENTS;
645
646 channel->buf_sz = channel->frag_sz * channel->frag_num;
647
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);
651}
652
653
654/**
655 * forte_channel_prep:
656 * @channel: Channel whose buffer to prepare
657 *
658 * Locking: Lock held.
659 */
660
661static void
662forte_channel_prep (struct forte_channel *channel)
663{
664 struct page *page;
665 int i;
666
667 if (channel->buf)
668 return;
669
670 forte_channel_buffer (channel, channel->frag_sz, channel->frag_num);
671 channel->buf_pages = channel->buf_sz >> PAGE_SHIFT;
672
673 if (channel->buf_sz % PAGE_SIZE)
674 channel->buf_pages++;
675
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);
679
680 /* DMA buffer */
681 channel->buf = pci_alloc_consistent (forte->pci_dev,
682 channel->buf_pages * PAGE_SIZE,
683 &channel->buf_handle);
684
685 if (!channel->buf || !channel->buf_handle)
686 BUG();
687
688 page = virt_to_page (channel->buf);
689
690 /* FIXME: can this go away ? */
691 for (i = 0 ; i < channel->buf_pages ; i++)
692 SetPageReserved(page++);
693
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);
699
700 /* Reset hwptr */
701 channel->hwptr = channel->frag_sz;
702 channel->next_buf = 1;
703
704 DPRINTK ("%s: %s buffer @ %p (%p)\n", __FUNCTION__, channel->name,
705 channel->buf, channel->buf_handle);
706}
707
708
709/**
710 * forte_channel_drain:
711 * @chip:
712 * @channel:
713 *
714 * Locking: Don't hold the lock.
715 */
716
717static inline int
718forte_channel_drain (struct forte_channel *channel)
719{
720 DECLARE_WAITQUEUE (wait, current);
721 unsigned long flags;
722
723 DPRINTK ("%s\n", __FUNCTION__);
724
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;
730 }
731
732 spin_lock_irqsave (&forte->lock, flags);
733 add_wait_queue (&channel->wait, &wait);
734
735 for (;;) {
736 if (channel->active == 0 || channel->filled_frags == 1)
737 break;
738
739 spin_unlock_irqrestore (&forte->lock, flags);
740
741 __set_current_state (TASK_INTERRUPTIBLE);
742 schedule();
743
744 spin_lock_irqsave (&forte->lock, flags);
745 }
746
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);
752
753 return 0;
754}
755
756
757/**
758 * forte_channel_init:
759 * @chip: Forte chip instance the channel hangs off
760 * @channel: Channel to initialize
761 *
762 * Description:
763 * Initializes a channel, sets defaults, and allocates
764 * buffers.
765 *
766 * Locking: No lock held.
767 */
768
769static int
770forte_channel_init (struct forte_chip *chip, struct forte_channel *channel)
771{
772 DPRINTK ("%s: chip iobase @ %p\n", __FUNCTION__, (void *)chip->iobase);
773
774 spin_lock_irq (&chip->lock);
775 memset (channel, 0x0, sizeof (*channel));
776
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);
782 }
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);
789 }
790 else
791 BUG();
792
793 init_waitqueue_head (&channel->wait);
794
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;
803
804 chip->trigger = 0;
805 spin_unlock_irq (&chip->lock);
806
807 return 0;
808}
809
810
811/**
812 * forte_channel_free:
813 * @chip: Chip this channel hangs off
814 * @channel: Channel to nuke
815 *
816 * Description:
817 * Resets channel and frees buffers.
818 *
819 * Locking: Hold your horses.
820 */
821
822static void
823forte_channel_free (struct forte_chip *chip, struct forte_channel *channel)
824{
825 DPRINTK ("%s: %s\n", __FUNCTION__, channel->name);
826
827 if (!channel->buf_handle)
828 return;
829
830 pci_free_consistent (chip->pci_dev, channel->buf_pages * PAGE_SIZE,
831 channel->buf, channel->buf_handle);
832
833 memset (channel, 0x0, sizeof (*channel));
834}
835
836
837/* DSP --------------------------------------------------------------------- */
838
839
840/**
841 * forte_dsp_ioctl:
842 */
843
844static int
845forte_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
846 unsigned long arg)
847{
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;
854
855 chip = file->private_data;
856
857 if (file->f_mode & FMODE_WRITE)
858 wr = 1;
859 else
860 wr = 0;
861
862 if (file->f_mode & FMODE_READ)
863 rd = 1;
864 else
865 rd = 0;
866
867 switch (cmd) {
868
869 case OSS_GETVERSION:
870 return put_user (SOUND_VERSION, p);
871
872 case SNDCTL_DSP_GETCAPS:
873 DPRINTK ("%s: GETCAPS\n", __FUNCTION__);
874
875 ival = FORTE_CAPS; /* DUPLEX */
876 return put_user (ival, p);
877
878 case SNDCTL_DSP_GETFMTS:
879 DPRINTK ("%s: GETFMTS\n", __FUNCTION__);
880
881 ival = FORTE_FMTS; /* U8, 16LE */
882 return put_user (ival, p);
883
884 case SNDCTL_DSP_SETFMT: /* U8, 16LE */
885 DPRINTK ("%s: SETFMT\n", __FUNCTION__);
886
887 if (get_user (ival, p))
888 return -EFAULT;
889
890 spin_lock_irq (&chip->lock);
891
892 if (rd) {
893 forte_channel_stop (&chip->rec);
894 rval = forte_channel_format (&chip->rec, ival);
895 }
896
897 if (wr) {
898 forte_channel_stop (&chip->rec);
899 rval = forte_channel_format (&chip->play, ival);
900 }
901
902 spin_unlock_irq (&chip->lock);
903
904 return put_user (rval, p);
905
906 case SNDCTL_DSP_STEREO: /* 0 - mono, 1 - stereo */
907 DPRINTK ("%s: STEREO\n", __FUNCTION__);
908
909 if (get_user (ival, p))
910 return -EFAULT;
911
912 spin_lock_irq (&chip->lock);
913
914 if (rd) {
915 forte_channel_stop (&chip->rec);
916 rval = forte_channel_stereo (&chip->rec, ival);
917 }
918
919 if (wr) {
920 forte_channel_stop (&chip->rec);
921 rval = forte_channel_stereo (&chip->play, ival);
922 }
923
924 spin_unlock_irq (&chip->lock);
925
926 return put_user (rval, p);
927
928 case SNDCTL_DSP_CHANNELS: /* 1 - mono, 2 - stereo */
929 DPRINTK ("%s: CHANNELS\n", __FUNCTION__);
930
931 if (get_user (ival, p))
932 return -EFAULT;
933
934 spin_lock_irq (&chip->lock);
935
936 if (rd) {
937 forte_channel_stop (&chip->rec);
938 rval = forte_channel_stereo (&chip->rec, ival-1) + 1;
939 }
940
941 if (wr) {
942 forte_channel_stop (&chip->play);
943 rval = forte_channel_stereo (&chip->play, ival-1) + 1;
944 }
945
946 spin_unlock_irq (&chip->lock);
947
948 return put_user (rval, p);
949
950 case SNDCTL_DSP_SPEED:
951 DPRINTK ("%s: SPEED\n", __FUNCTION__);
952
953 if (get_user (ival, p))
954 return -EFAULT;
955
956 spin_lock_irq (&chip->lock);
957
958 if (rd) {
959 forte_channel_stop (&chip->rec);
960 rval = forte_channel_rate (&chip->rec, ival);
961 }
962
963 if (wr) {
964 forte_channel_stop (&chip->play);
965 rval = forte_channel_rate (&chip->play, ival);
966 }
967
968 spin_unlock_irq (&chip->lock);
969
970 return put_user(rval, p);
971
972 case SNDCTL_DSP_GETBLKSIZE:
973 DPRINTK ("%s: GETBLKSIZE\n", __FUNCTION__);
974
975 spin_lock_irq (&chip->lock);
976
977 if (rd)
978 ival = chip->rec.frag_sz;
979
980 if (wr)
981 ival = chip->play.frag_sz;
982
983 spin_unlock_irq (&chip->lock);
984
985 return put_user (ival, p);
986
987 case SNDCTL_DSP_RESET:
988 DPRINTK ("%s: RESET\n", __FUNCTION__);
989
990 spin_lock_irq (&chip->lock);
991
992 if (rd)
993 forte_channel_reset (&chip->rec);
994
995 if (wr)
996 forte_channel_reset (&chip->play);
997
998 spin_unlock_irq (&chip->lock);
999
1000 return 0;
1001
1002 case SNDCTL_DSP_SYNC:
1003 DPRINTK ("%s: SYNC\n", __FUNCTION__);
1004
1005 if (wr)
1006 ret = forte_channel_drain (&chip->play);
1007
1008 return 0;
1009
1010 case SNDCTL_DSP_POST:
1011 DPRINTK ("%s: POST\n", __FUNCTION__);
1012
1013 if (wr) {
1014 spin_lock_irq (&chip->lock);
1015
1016 if (chip->play.filled_frags)
1017 forte_channel_start (&chip->play);
1018
1019 spin_unlock_irq (&chip->lock);
1020 }
1021
1022 return 0;
1023
1024 case SNDCTL_DSP_SETFRAGMENT:
1025 DPRINTK ("%s: SETFRAGMENT\n", __FUNCTION__);
1026
1027 if (get_user (ival, p))
1028 return -EFAULT;
1029
1030 spin_lock_irq (&chip->lock);
1031
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;
1036 }
1037
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;
1042 }
1043
1044 spin_unlock_irq (&chip->lock);
1045
1046 return put_user (ival, p);
1047
1048 case SNDCTL_DSP_GETISPACE:
1049 DPRINTK ("%s: GETISPACE\n", __FUNCTION__);
1050
1051 if (!rd)
1052 return -EINVAL;
1053
1054 spin_lock_irq (&chip->lock);
1055
1056 abi.fragstotal = chip->rec.frag_num;
1057 abi.fragsize = chip->rec.frag_sz;
1058
1059 if (chip->rec.mapped) {
1060 abi.fragments = chip->rec.frag_num - 2;
1061 abi.bytes = abi.fragments * abi.fragsize;
1062 }
1063 else {
1064 abi.fragments = chip->rec.filled_frags;
1065 abi.bytes = abi.fragments * abi.fragsize;
1066 }
1067
1068 spin_unlock_irq (&chip->lock);
1069
1070 return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
1071
1072 case SNDCTL_DSP_GETIPTR:
1073 DPRINTK ("%s: GETIPTR\n", __FUNCTION__);
1074
1075 if (!rd)
1076 return -EINVAL;
1077
1078 spin_lock_irq (&chip->lock);
1079
1080 if (chip->rec.active)
1081 cinfo.ptr = chip->rec.hwptr;
1082 else
1083 cinfo.ptr = 0;
1084
1085 cinfo.bytes = chip->rec.bytes;
1086 cinfo.blocks = chip->rec.nr_irqs;
1087 chip->rec.nr_irqs = 0;
1088
1089 spin_unlock_irq (&chip->lock);
1090
1091 return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
1092
1093 case SNDCTL_DSP_GETOSPACE:
1094 if (!wr)
1095 return -EINVAL;
1096
1097 spin_lock_irq (&chip->lock);
1098
1099 abi.fragstotal = chip->play.frag_num;
1100 abi.fragsize = chip->play.frag_sz;
1101
1102 if (chip->play.mapped) {
1103 abi.fragments = chip->play.frag_num - 2;
1104 abi.bytes = chip->play.buf_sz;
1105 }
1106 else {
1107 abi.fragments = chip->play.frag_num -
1108 chip->play.filled_frags;
1109
1110 if (chip->play.residue)
1111 abi.fragments--;
1112
1113 abi.bytes = abi.fragments * abi.fragsize +
1114 chip->play.residue;
1115 }
1116
1117 spin_unlock_irq (&chip->lock);
1118
1119 return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
1120
1121 case SNDCTL_DSP_GETOPTR:
1122 if (!wr)
1123 return -EINVAL;
1124
1125 spin_lock_irq (&chip->lock);
1126
1127 if (chip->play.active)
1128 cinfo.ptr = chip->play.hwptr;
1129 else
1130 cinfo.ptr = 0;
1131
1132 cinfo.bytes = chip->play.bytes;
1133 cinfo.blocks = chip->play.nr_irqs;
1134 chip->play.nr_irqs = 0;
1135
1136 spin_unlock_irq (&chip->lock);
1137
1138 return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
1139
1140 case SNDCTL_DSP_GETODELAY:
1141 if (!wr)
1142 return -EINVAL;
1143
1144 spin_lock_irq (&chip->lock);
1145
1146 if (!chip->play.active) {
1147 ival = 0;
1148 }
1149 else if (chip->play.mapped) {
1150 count = inw (chip->play.iobase + FORTE_PLY_COUNT) + 1;
1151 ival = chip->play.frag_sz - count;
1152 }
1153 else {
1154 ival = chip->play.filled_frags * chip->play.frag_sz;
1155
1156 if (chip->play.residue)
1157 ival += chip->play.frag_sz - chip->play.residue;
1158 }
1159
1160 spin_unlock_irq (&chip->lock);
1161
1162 return put_user (ival, p);
1163
1164 case SNDCTL_DSP_SETDUPLEX:
1165 DPRINTK ("%s: SETDUPLEX\n", __FUNCTION__);
1166
1167 return -EINVAL;
1168
1169 case SNDCTL_DSP_GETTRIGGER:
1170 DPRINTK ("%s: GETTRIGGER\n", __FUNCTION__);
1171
1172 return put_user (chip->trigger, p);
1173
1174 case SNDCTL_DSP_SETTRIGGER:
1175
1176 if (get_user (ival, p))
1177 return -EFAULT;
1178
1179 DPRINTK ("%s: SETTRIGGER %d\n", __FUNCTION__, ival);
1180
1181 if (wr) {
1182 spin_lock_irq (&chip->lock);
1183
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);
1190 }
1191
1192 spin_unlock_irq (&chip->lock);
1193 }
1194 else if (rd) {
1195 spin_lock_irq (&chip->lock);
1196
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);
1203 }
1204
1205 spin_unlock_irq (&chip->lock);
1206 }
1207
1208 return 0;
1209
1210 case SOUND_PCM_READ_RATE:
1211 DPRINTK ("%s: PCM_READ_RATE\n", __FUNCTION__);
1212 return put_user (chip->play.rate, p);
1213
1214 case SOUND_PCM_READ_CHANNELS:
1215 DPRINTK ("%s: PCM_READ_CHANNELS\n", __FUNCTION__);
1216 return put_user (chip->play.stereo, p);
1217
1218 case SOUND_PCM_READ_BITS:
1219 DPRINTK ("%s: PCM_READ_BITS\n", __FUNCTION__);
1220 return put_user (chip->play.format, p);
1221
1222 case SNDCTL_DSP_NONBLOCK:
1223 DPRINTK ("%s: DSP_NONBLOCK\n", __FUNCTION__);
1224 file->f_flags |= O_NONBLOCK;
1225 return 0;
1226
1227 default:
1228 DPRINTK ("Unsupported ioctl: %x (%p)\n", cmd, argp);
1229 break;
1230 }
1231
1232 return -EINVAL;
1233}
1234
1235
1236/**
1237 * forte_dsp_open:
1238 */
1239
1240static int
1241forte_dsp_open (struct inode *inode, struct file *file)
1242{
1243 struct forte_chip *chip = forte; /* FIXME: HACK FROM HELL! */
1244
1245 if (file->f_flags & O_NONBLOCK) {
Ingo Molnar910f5d22006-03-23 03:00:39 -08001246 if (!mutex_trylock(&chip->open_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 DPRINTK ("%s: returning -EAGAIN\n", __FUNCTION__);
1248 return -EAGAIN;
1249 }
1250 }
1251 else {
Ingo Molnar910f5d22006-03-23 03:00:39 -08001252 if (mutex_lock_interruptible(&chip->open_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 DPRINTK ("%s: returning -ERESTARTSYS\n", __FUNCTION__);
1254 return -ERESTARTSYS;
1255 }
1256 }
1257
1258 file->private_data = forte;
1259
1260 DPRINTK ("%s: dsp opened by %d\n", __FUNCTION__, current->pid);
1261
1262 if (file->f_mode & FMODE_WRITE)
1263 forte_channel_init (forte, &forte->play);
1264
1265 if (file->f_mode & FMODE_READ)
1266 forte_channel_init (forte, &forte->rec);
1267
1268 return nonseekable_open(inode, file);
1269}
1270
1271
1272/**
1273 * forte_dsp_release:
1274 */
1275
1276static int
1277forte_dsp_release (struct inode *inode, struct file *file)
1278{
1279 struct forte_chip *chip = file->private_data;
1280 int ret = 0;
1281
1282 DPRINTK ("%s: chip @ %p\n", __FUNCTION__, chip);
1283
1284 if (file->f_mode & FMODE_WRITE) {
1285 forte_channel_drain (&chip->play);
1286
1287 spin_lock_irq (&chip->lock);
1288
1289 forte_channel_free (chip, &chip->play);
1290
1291 spin_unlock_irq (&chip->lock);
1292 }
1293
1294 if (file->f_mode & FMODE_READ) {
1295 while (chip->rec.filled_frags > 0)
1296 interruptible_sleep_on (&chip->rec.wait);
1297
1298 spin_lock_irq (&chip->lock);
1299
1300 forte_channel_stop (&chip->rec);
1301 forte_channel_free (chip, &chip->rec);
1302
1303 spin_unlock_irq (&chip->lock);
1304 }
1305
Ingo Molnar910f5d22006-03-23 03:00:39 -08001306 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 return ret;
1309}
1310
1311
1312/**
1313 * forte_dsp_poll:
1314 *
1315 */
1316
1317static unsigned int
1318forte_dsp_poll (struct file *file, struct poll_table_struct *wait)
1319{
1320 struct forte_chip *chip;
1321 struct forte_channel *channel;
1322 unsigned int mask = 0;
1323
1324 chip = file->private_data;
1325
1326 if (file->f_mode & FMODE_WRITE) {
1327 channel = &chip->play;
1328
1329 if (channel->active)
1330 poll_wait (file, &channel->wait, wait);
1331
1332 spin_lock_irq (&chip->lock);
1333
1334 if (channel->frag_num - channel->filled_frags > 0)
1335 mask |= POLLOUT | POLLWRNORM;
1336
1337 spin_unlock_irq (&chip->lock);
1338 }
1339
1340 if (file->f_mode & FMODE_READ) {
1341 channel = &chip->rec;
1342
1343 if (channel->active)
1344 poll_wait (file, &channel->wait, wait);
1345
1346 spin_lock_irq (&chip->lock);
1347
1348 if (channel->filled_frags > 0)
1349 mask |= POLLIN | POLLRDNORM;
1350
1351 spin_unlock_irq (&chip->lock);
1352 }
1353
1354 return mask;
1355}
1356
1357
1358/**
1359 * forte_dsp_mmap:
1360 */
1361
1362static int
1363forte_dsp_mmap (struct file *file, struct vm_area_struct *vma)
1364{
1365 struct forte_chip *chip;
1366 struct forte_channel *channel;
1367 unsigned long size;
1368 int ret;
1369
1370 chip = file->private_data;
1371
1372 DPRINTK ("%s: start %lXh, size %ld, pgoff %ld\n", __FUNCTION__,
1373 vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_pgoff);
1374
1375 spin_lock_irq (&chip->lock);
1376
1377 if (vma->vm_flags & VM_WRITE && chip->play.active) {
1378 ret = -EBUSY;
1379 goto out;
1380 }
1381
1382 if (vma->vm_flags & VM_READ && chip->rec.active) {
1383 ret = -EBUSY;
1384 goto out;
1385 }
1386
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;
1394 }
1395
1396 forte_channel_prep (channel);
1397 channel->mapped = 1;
1398
1399 if (vma->vm_pgoff != 0) {
1400 ret = -EINVAL;
1401 goto out;
1402 }
1403
1404 size = vma->vm_end - vma->vm_start;
1405
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;
1411 }
1412
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;
1419 }
1420
1421 ret = 0;
1422
1423 out:
1424 spin_unlock_irq (&chip->lock);
1425 return ret;
1426}
1427
1428
1429/**
1430 * forte_dsp_write:
1431 */
1432
1433static ssize_t
1434forte_dsp_write (struct file *file, const char __user *buffer, size_t bytes,
1435 loff_t *ppos)
1436{
1437 struct forte_chip *chip;
1438 struct forte_channel *channel;
1439 unsigned int i = bytes, sz = 0;
1440 unsigned long flags;
1441
1442 if (!access_ok (VERIFY_READ, buffer, bytes))
1443 return -EFAULT;
1444
1445 chip = (struct forte_chip *) file->private_data;
1446
1447 if (!chip)
1448 BUG();
1449
1450 channel = &chip->play;
1451
1452 if (!channel)
1453 BUG();
1454
1455 spin_lock_irqsave (&chip->lock, flags);
1456
1457 /* Set up buffers with the right fragment size */
1458 forte_channel_prep (channel);
1459
1460 while (i) {
1461 /* All fragment buffers in use -> wait */
1462 if (channel->frag_num - channel->filled_frags == 0) {
1463 DECLARE_WAITQUEUE (wait, current);
1464
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;
1469 }
1470
1471 /* Otherwise wait for buffers */
1472 add_wait_queue (&channel->wait, &wait);
1473
1474 for (;;) {
1475 spin_unlock_irqrestore (&chip->lock, flags);
1476
1477 set_current_state (TASK_INTERRUPTIBLE);
1478 schedule();
1479
1480 spin_lock_irqsave (&chip->lock, flags);
1481
1482 if (channel->frag_num - channel->filled_frags)
1483 break;
1484 }
1485
1486 remove_wait_queue (&channel->wait, &wait);
1487 set_current_state (TASK_RUNNING);
1488
1489 if (signal_pending (current)) {
1490 spin_unlock_irqrestore (&chip->lock, flags);
1491 return -ERESTARTSYS;
1492 }
1493 }
1494
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;
1501
1502 spin_unlock_irqrestore (&chip->lock, flags);
1503
1504 if (copy_from_user ((void *) channel->buf + channel->swptr, buffer, sz))
1505 return -EFAULT;
1506
1507 spin_lock_irqsave (&chip->lock, flags);
1508
1509 /* Advance software pointer */
1510 buffer += sz;
1511 channel->swptr += sz;
1512 channel->swptr %= channel->buf_sz;
1513 i -= sz;
1514
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;
1519 }
1520 else
1521 channel->residue = channel->frag_sz - sz;
1522
1523 /* If playback isn't active, start it */
1524 if (channel->active == 0 && chip->trigger == 0)
1525 forte_channel_start (channel);
1526 }
1527
1528 spin_unlock_irqrestore (&chip->lock, flags);
1529
1530 return bytes - i;
1531}
1532
1533
1534/**
1535 * forte_dsp_read:
1536 */
1537
1538static ssize_t
1539forte_dsp_read (struct file *file, char __user *buffer, size_t bytes,
1540 loff_t *ppos)
1541{
1542 struct forte_chip *chip;
1543 struct forte_channel *channel;
1544 unsigned int i = bytes, sz;
1545 unsigned long flags;
1546
1547 if (!access_ok (VERIFY_WRITE, buffer, bytes))
1548 return -EFAULT;
1549
1550 chip = (struct forte_chip *) file->private_data;
1551
1552 if (!chip)
1553 BUG();
1554
1555 channel = &chip->rec;
1556
1557 if (!channel)
1558 BUG();
1559
1560 spin_lock_irqsave (&chip->lock, flags);
1561
1562 /* Set up buffers with the right fragment size */
1563 forte_channel_prep (channel);
1564
1565 /* Start recording */
1566 if (!chip->trigger)
1567 forte_channel_start (channel);
1568
1569 while (i) {
1570 /* No fragment buffers in use -> wait */
1571 if (channel->filled_frags == 0) {
1572 DECLARE_WAITQUEUE (wait, current);
1573
1574 /* For trigger mode operation, get out */
1575 if (chip->trigger) {
1576 spin_unlock_irqrestore (&chip->lock, flags);
1577 return -EAGAIN;
1578 }
1579
1580 add_wait_queue (&channel->wait, &wait);
1581
1582 for (;;) {
1583 if (channel->active == 0)
1584 break;
1585
1586 if (channel->filled_frags)
1587 break;
1588
1589 spin_unlock_irqrestore (&chip->lock, flags);
1590
1591 set_current_state (TASK_INTERRUPTIBLE);
1592 schedule();
1593
1594 spin_lock_irqsave (&chip->lock, flags);
1595 }
1596
1597 set_current_state (TASK_RUNNING);
1598 remove_wait_queue (&channel->wait, &wait);
1599 }
1600
1601 if (i > channel->frag_sz)
1602 sz = channel->frag_sz;
1603 else
1604 sz = i;
1605
1606 spin_unlock_irqrestore (&chip->lock, flags);
1607
1608 if (copy_to_user (buffer, (void *)channel->buf+channel->swptr, sz)) {
1609 DPRINTK ("%s: copy_to_user failed\n", __FUNCTION__);
1610 return -EFAULT;
1611 }
1612
1613 spin_lock_irqsave (&chip->lock, flags);
1614
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;
1622 }
1623
1624 spin_unlock_irqrestore (&chip->lock, flags);
1625
1626 return bytes - i;
1627}
1628
1629
1630static 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,
1640};
1641
1642
1643/* Common ------------------------------------------------------------------ */
1644
1645
1646/**
1647 * forte_interrupt:
1648 */
1649
1650static irqreturn_t
1651forte_interrupt (int irq, void *dev_id, struct pt_regs *regs)
1652{
1653 struct forte_chip *chip = dev_id;
1654 struct forte_channel *channel = NULL;
1655 u16 status, count;
1656
1657 status = inw (chip->iobase + FORTE_IRQ_STATUS);
1658
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;
1662
1663 if (status & FORTE_IRQ_PLAYBACK) {
1664 channel = &chip->play;
1665
1666 spin_lock (&chip->lock);
1667
1668 if (channel->frag_sz == 0)
1669 goto pack;
1670
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++;
1676
1677 /* Flip-flop between buffer I and II */
1678 channel->next_buf ^= 1;
1679
1680 /* Advance hardware pointer by fragment size and wrap around */
1681 channel->hwptr += channel->frag_sz;
1682 channel->hwptr %= channel->buf_sz;
1683
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);
1689
1690 /* If the currently playing fragment is last, schedule pause */
1691 if (channel->filled_frags == 1)
1692 forte_channel_pause (channel);
1693
1694 pack:
1695 /* Acknowledge interrupt */
1696 outw (FORTE_IRQ_PLAYBACK, chip->iobase + FORTE_IRQ_STATUS);
1697
1698 if (waitqueue_active (&channel->wait))
1699 wake_up_all (&channel->wait);
1700
1701 spin_unlock (&chip->lock);
1702 }
1703
1704 if (status & FORTE_IRQ_CAPTURE) {
1705 channel = &chip->rec;
1706 spin_lock (&chip->lock);
1707
1708 /* One fragment filled */
1709 channel->filled_frags++;
1710
1711 /* Get # of completed bytes */
1712 count = inw (channel->iobase + FORTE_PLY_COUNT) + 1;
1713
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;
1719 }
1720
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);
1726
1727 /* Flip-flop between buffer I and II */
1728 channel->next_buf ^= 1;
1729
1730 /* Advance hardware pointer by fragment size and wrap around */
1731 channel->hwptr += channel->frag_sz;
1732 channel->hwptr %= channel->buf_sz;
1733
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);
1740
1741 spin_unlock (&chip->lock);
1742
1743 if (waitqueue_active (&channel->wait))
1744 wake_up_all (&channel->wait);
1745 }
1746
1747 return IRQ_HANDLED;
1748}
1749
1750
1751/**
1752 * forte_proc_read:
1753 */
1754
1755static int
1756forte_proc_read (char *page, char **start, off_t off, int count,
1757 int *eof, void *data)
1758{
1759 int i = 0, p_rate, p_chan, r_rate;
1760 unsigned short p_reg, r_reg;
1761
1762 i += sprintf (page, "ForteMedia FM801 OSS Lite driver\n%s\n \n",
1763 DRIVER_VERSION);
1764
1765 if (!forte->iobase)
1766 return i;
1767
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;
1772
1773 if (p_rate >= 0 || p_rate <= 10)
1774 p_rate = rates[p_rate];
1775
1776 if (p_chan >= 0 || p_chan <= 2)
1777 p_chan = channels[p_chan];
1778
1779 r_rate = -1;
1780 r_reg = inw (forte->iobase + FORTE_CAP_CTRL);
1781 r_rate = (r_reg >> 8) & 15;
1782
1783 if (r_rate >= 0 || r_rate <= 10)
1784 r_rate = rates[r_rate];
1785
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"
1830 );
1831
1832 return i;
1833}
1834
1835
1836/**
1837 * forte_proc_init:
1838 *
1839 * Creates driver info entries in /proc
1840 */
1841
1842static int __init
1843forte_proc_init (void)
1844{
1845 if (!proc_mkdir ("driver/forte", NULL))
1846 return -EIO;
1847
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;
1851 }
1852
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;
1857 }
1858
1859 return 0;
1860}
1861
1862
1863/**
1864 * forte_proc_remove:
1865 *
1866 * Removes driver info entries in /proc
1867 */
1868
1869static void
1870forte_proc_remove (void)
1871{
1872 remove_proc_entry ("driver/forte/ac97", NULL);
1873 remove_proc_entry ("driver/forte/chip", NULL);
1874 remove_proc_entry ("driver/forte", NULL);
1875}
1876
1877
1878/**
1879 * forte_chip_init:
1880 * @chip: Chip instance to initialize
1881 *
1882 * Description:
1883 * Resets chip, configures codec and registers the driver with
1884 * the sound subsystem.
1885 *
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.
1889 */
1890
1891static int __devinit
1892forte_chip_init (struct forte_chip *chip)
1893{
1894 u8 revision;
1895 u16 cmdw;
1896 struct ac97_codec *codec;
1897
1898 pci_read_config_byte (chip->pci_dev, PCI_REVISION_ID, &revision);
1899
1900 if (revision >= 0xB1) {
1901 chip->multichannel = 1;
1902 printk (KERN_INFO PFX "Multi-channel device detected.\n");
1903 }
1904
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);
1910
1911 /* Request read from AC97 */
1912 outw (FORTE_AC97_READ | (0 << FORTE_AC97_ADDR_SHIFT),
1913 chip->iobase + FORTE_AC97_CMD);
1914 mdelay(750);
1915
1916 if ((inw (chip->iobase + FORTE_AC97_CMD) & (3<<8)) != (1<<8)) {
1917 printk (KERN_INFO PFX "AC97 codec not responding");
1918 return -EIO;
1919 }
1920
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);
1925
1926 /* I2S control - I2S mode */
1927 outw (0x0003, chip->iobase + FORTE_I2S_MODE);
1928
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);
1933
1934 /* Interrupt clear */
1935 outw (FORTE_IRQ_PLAYBACK|FORTE_IRQ_CAPTURE,
1936 chip->iobase + FORTE_IRQ_STATUS);
1937
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;
1945
1946 if (ac97_probe_codec (codec) == 0) {
1947 printk (KERN_ERR PFX "codec probe failed\n");
1948 ac97_release_codec(codec);
1949 return -1;
1950 }
1951
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;
1958 }
1959
1960 chip->ac97 = codec;
1961
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;
1966 }
1967
1968 /* Register with /proc */
1969 if (forte_proc_init()) {
1970 printk (KERN_ERR PFX "couldn't add entries to /proc!\n");
1971 return -1;
1972 }
1973
1974 return 0;
1975}
1976
1977
1978/**
1979 * forte_probe:
1980 * @pci_dev: PCI struct for probed device
1981 * @pci_id:
1982 *
1983 * Description:
1984 * Allocates chip instance, I/O region, and IRQ
1985 */
1986static int __init
1987forte_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
1988{
1989 struct forte_chip *chip;
1990 int ret = 0;
1991
1992 /* FIXME: Support more than one chip */
1993 if (found++)
1994 return -EIO;
1995
1996 /* Ignition */
1997 if (pci_enable_device (pci_dev))
1998 return -EIO;
1999
2000 pci_set_master (pci_dev);
2001
2002 /* Allocate chip instance and configure */
2003 forte = (struct forte_chip *)
2004 kmalloc (sizeof (struct forte_chip), GFP_KERNEL);
2005 chip = forte;
2006
2007 if (chip == NULL) {
2008 printk (KERN_WARNING PFX "Out of memory");
2009 return -ENOMEM;
2010 }
2011
2012 memset (chip, 0, sizeof (struct forte_chip));
2013 chip->pci_dev = pci_dev;
2014
Ingo Molnar910f5d22006-03-23 03:00:39 -08002015 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 spin_lock_init (&chip->lock);
2017 spin_lock_init (&chip->ac97_lock);
2018
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;
2024 }
2025
2026 chip->iobase = pci_resource_start (pci_dev, 0);
2027 chip->irq = pci_dev->irq;
2028
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;
2034 }
2035
2036 pci_set_drvdata (pci_dev, chip);
2037
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);
2040
2041 /* Power it up */
2042 if ((ret = forte_chip_init (chip)) == 0)
2043 return 0;
2044
2045 error:
2046 if (chip->irq)
2047 free_irq (chip->irq, chip);
2048
2049 if (chip->iobase)
2050 release_region (pci_resource_start (pci_dev, 0),
2051 pci_resource_len (pci_dev, 0));
2052
2053 kfree (chip);
2054
2055 return ret;
2056}
2057
2058
2059/**
2060 * forte_remove:
2061 * @pci_dev: PCI device to unclaim
2062 *
2063 */
2064
2065static void
2066forte_remove (struct pci_dev *pci_dev)
2067{
2068 struct forte_chip *chip = pci_get_drvdata (pci_dev);
2069
2070 if (chip == NULL)
2071 return;
2072
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);
2077
2078 forte_proc_remove();
2079 free_irq (chip->irq, chip);
2080 release_region (chip->iobase, pci_resource_len (pci_dev, 0));
2081
2082 unregister_sound_dsp (chip->dsp);
2083 unregister_sound_mixer (chip->ac97->dev_mixer);
2084 ac97_release_codec(chip->ac97);
2085 kfree (chip);
2086
2087 printk (KERN_INFO PFX "driver released\n");
2088}
2089
2090
2091static struct pci_device_id forte_pci_ids[] = {
2092 { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
2093 { 0, }
2094};
2095
2096
2097static struct pci_driver forte_pci_driver = {
2098 .name = DRIVER_NAME,
2099 .id_table = forte_pci_ids,
2100 .probe = forte_probe,
2101 .remove = forte_remove,
2102
2103};
2104
2105
2106/**
2107 * forte_init_module:
2108 *
2109 */
2110
2111static int __init
2112forte_init_module (void)
2113{
2114 printk (KERN_INFO PFX DRIVER_VERSION "\n");
2115
2116 return pci_register_driver (&forte_pci_driver);
2117}
2118
2119
2120/**
2121 * forte_cleanup_module:
2122 *
2123 */
2124
2125static void __exit
2126forte_cleanup_module (void)
2127{
2128 pci_unregister_driver (&forte_pci_driver);
2129}
2130
2131
2132module_init(forte_init_module);
2133module_exit(forte_cleanup_module);
2134
2135MODULE_AUTHOR("Martin K. Petersen <mkp@mkp.net>");
2136MODULE_DESCRIPTION("ForteMedia FM801 OSS Driver");
2137MODULE_LICENSE("GPL");
2138MODULE_DEVICE_TABLE (pci, forte_pci_ids);