OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [sound/] [forte.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.