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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *      Driver for ALI 5455  Audio PCI soundcard
3
 *      LEI HU <Lei_Hu@ali.com.tw>
4
 *
5
 *  Built from:
6
 *      drivers/sound/i810_audio
7
 *
8
 *      The ALi 5455 is similar but not quite identical to the Intel ICH
9
 *      series of controllers. Its easier to keep the driver seperated from
10
 *      the i810 driver.
11
 *
12
 *      This program is free software; you can redistribute it and/or modify
13
 *      it under the terms of the GNU General Public License as published by
14
 *      the Free Software Foundation; either version 2 of the License, or
15
 *      (at your option) any later version.
16
 *
17
 *      This program is distributed in the hope that it will be useful,
18
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *      GNU General Public License for more details.
21
 *
22
 *      You should have received a copy of the GNU General Public License
23
 *      along with this program; if not, write to the Free Software
24
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
 *
26
 *      The chipset provides five DMA channels that talk to an AC97
27
 *      CODEC (AC97 is a digital/analog mixer standard). At its simplest
28
 *      you get 48Khz audio with basic volume and mixer controls. At the
29
 *      best you get rate adaption in the codec. We set the card up so
30
 *      that we never take completion interrupts but instead keep the card
31
 *      chasing its tail around a ring buffer. This is needed for mmap
32
 *      mode audio and happens to work rather well for non-mmap modes too.
33
 *
34
 *      The board has one output channel for PCM audio (supported) and
35
 *      a stereo line in and mono microphone input. Again these are normally
36
 *      locked to 48Khz only. Right now recording is not finished.
37
 *
38
 *      There is no midi support, no synth support. Use timidity. To get
39
 *      esd working you need to use esd -r 48000 as it won't probe 48KHz
40
 *      by default. mpg123 can't handle 48Khz only audio so use xmms.
41
 *
42
 *
43
 *      Not everyone uses 48KHz. We know of no way to detect this reliably
44
 *      and certainly not to get the right data. If your ali audio sounds
45
 *      stupid you may need to investigate other speeds. According to Analog
46
 *      they tend to use a 14.318MHz clock which gives you a base rate of
47
 *      41194Hz.
48
 *
49
 *      If you need to force a specific rate set the clocking= option
50
 *
51
 */
52
 
53
#include <linux/module.h>
54
#include <linux/version.h>
55
#include <linux/string.h>
56
#include <linux/ctype.h>
57
#include <linux/ioport.h>
58
#include <linux/sched.h>
59
#include <linux/delay.h>
60
#include <linux/sound.h>
61
#include <linux/slab.h>
62
#include <linux/soundcard.h>
63
#include <linux/pci.h>
64
#include <asm/io.h>
65
#include <asm/dma.h>
66
#include <linux/init.h>
67
#include <linux/poll.h>
68
#include <linux/spinlock.h>
69
#include <linux/smp_lock.h>
70
#include <linux/ac97_codec.h>
71
#include <linux/wrapper.h>
72
#include <asm/uaccess.h>
73
#include <asm/hardirq.h>
74
 
75
#ifndef PCI_DEVICE_ID_ALI_5455
76
#define PCI_DEVICE_ID_ALI_5455  0x5455
77
#endif
78
 
79
#ifndef PCI_VENDOR_ID_ALI
80
#define PCI_VENDOR_ID_ALI       0x10b9
81
#endif
82
 
83
static int strict_clocking = 0;
84
static unsigned int clocking = 0;
85
#ifdef CONFIG_SOUND_ALI5455_CODECSPDIFOUT_PCMOUTSHARE
86
static unsigned int codec_pcmout_share_spdif_locked = 48000;
87
#else
88
 static unsigned int codec_pcmout_share_spdif_locked = 0;
89
#endif
90
#ifdef CONFIG_SOUND_ALI5455_CODECSPDIFOUT_CODECINDEPENDENTDMA
91
static unsigned int codec_independent_spdif_locked = 48000;
92
#else
93
static unsigned int codec_independent_spdif_locked = 0;
94
#endif
95
#ifdef CONFIG_SOUND_ALI5455_CONTROLLERSPDIFOUT_PCMOUTSHARE
96
static unsigned int controller_pcmout_share_spdif_locked = 48000;
97
#else
98
static unsigned int controller_pcmout_share_spdif_locked = 0;
99
#endif
100
#ifdef CONFIG_SOUND_ALI5455_CONTROLLERSPDIFOUT_CONTROLLERINDEPENDENTDMA
101
static unsigned int controller_independent_spdif_locked = 48000;
102
#else
103
static unsigned int controller_independent_spdif_locked = 0;
104
#endif
105
 
106
#define ADC_RUNNING                     1
107
#define DAC_RUNNING                     2
108
#define CONTROLLER_SPDIFOUT_RUNNING     4
109
#define CODEC_SPDIFOUT_RUNNING          8
110
 
111
#define ALI5455_FMT_16BIT               1
112
#define ALI5455_FMT_STEREO              2
113
#define ALI5455_FMT_MASK                3
114
 
115
#define SPDIF_ON                        0x0004
116
#define SURR_ON                         0x0010
117
#define CENTER_LFE_ON                   0x0020
118
#define VOL_MUTED                       0x8000
119
 
120
#define SPDIF_ENABLE_OUTPUT             0x00000004
121
 
122
#define ALI_SPDIF_OUT_CH_STATUS         0xbf
123
/* the ali5455 's array of pointers to data buffers */
124
 
125
struct sg_item {
126
#define BUSADDR_MASK    0xFFFFFFFE
127
        u32 busaddr;
128
#define CON_IOC         0x80000000      /* interrupt on completion */
129
#define CON_BUFPAD      0x40000000      /* pad underrun with last sample, else 0 */
130
#define CON_BUFLEN_MASK 0x0000ffff      /* buffer length in samples */
131
        u32 control;
132
};
133
 
134
/* An instance of the ali 5455 channel */
135
#define SG_LEN 32
136
struct ali_channel {
137
        /* these sg guys should probably be allocated
138
           seperately as nocache. Must be 8 byte aligned */
139
        struct sg_item sg[SG_LEN];      /* 32*8 */
140
        u32 offset;                     /* 4 */
141
        u32 port;                       /* 4 */
142
        u32 used;
143
        u32 num;
144
};
145
 
146
/*
147
 * we have 5 seperate dma engines.  pcm in, pcm out, mc in,
148
 * codec independant DMA, and controller independant DMA
149
 * each dma engine has controlling registers.  These goofy
150
 * names are from the datasheet, but make it easy to write
151
 * code while leafing through it.
152
 */
153
 
154
#define ENUM_ENGINE(PRE,DIG)                                                                    \
155
enum {                                                                                          \
156
        PRE##_BDBAR =   0x##DIG##0,             /* Buffer Descriptor list Base Address */       \
157
        PRE##_CIV =     0x##DIG##4,             /* Current Index Value */                       \
158
        PRE##_LVI =     0x##DIG##5,             /* Last Valid Index */                          \
159
        PRE##_SR =      0x##DIG##6,             /* Status Register */                           \
160
        PRE##_PICB =    0x##DIG##8,             /* Position In Current Buffer */                \
161
        PRE##_CR =      0x##DIG##b              /* Control Register */                          \
162
}
163
 
164
ENUM_ENGINE(OFF, 0);                     /* Offsets */
165
ENUM_ENGINE(PI, 4);                     /* PCM In */
166
ENUM_ENGINE(PO, 5);                     /* PCM Out */
167
ENUM_ENGINE(MC, 6);                     /* Mic In */
168
ENUM_ENGINE(CODECSPDIFOUT, 7);          /* CODEC SPDIF OUT  */
169
ENUM_ENGINE(CONTROLLERSPDIFIN, A);      /* CONTROLLER SPDIF In */
170
ENUM_ENGINE(CONTROLLERSPDIFOUT, B);     /* CONTROLLER SPDIF OUT */
171
 
172
 
173
enum {
174
        ALI_SCR = 0x00,         /* System Control Register */
175
        ALI_SSR = 0x04,         /* System Status Register  */
176
        ALI_DMACR = 0x08,       /* DMA Control Register    */
177
        ALI_FIFOCR1 = 0x0c,     /* FIFO Control Register 1  */
178
        ALI_INTERFACECR = 0x10, /* Interface Control Register */
179
        ALI_INTERRUPTCR = 0x14, /* Interrupt control Register */
180
        ALI_INTERRUPTSR = 0x18, /* Interrupt  Status Register */
181
        ALI_FIFOCR2 = 0x1c,     /* FIFO Control Register 2   */
182
        ALI_CPR = 0x20,         /* Command Port Register     */
183
        ALI_SPR = 0x24,         /* Status Port Register      */
184
        ALI_FIFOCR3 = 0x2c,     /* FIFO Control Register 3  */
185
        ALI_TTSR = 0x30,        /* Transmit Tag Slot Register */
186
        ALI_RTSR = 0x34,        /* Receive Tag Slot  Register */
187
        ALI_CSPSR = 0x38,       /* Command/Status Port Status Register */
188
        ALI_CAS = 0x3c,         /* Codec Write Semaphore Register */
189
        ALI_SPDIFCSR = 0xf8,    /* spdif channel status register  */
190
        ALI_SPDIFICS = 0xfc     /* spdif interface control/status  */
191
};
192
 
193
/* x-status register(x:pcm in ,pcm out, mic in,codec independent DMA.controller independent DMA) */
194
/* interrupts for a dma engine */
195
#define DMA_INT_FIFO            (1<<4)  /* fifo under/over flow */
196
#define DMA_INT_COMPLETE        (1<<3)  /* buffer read/write complete and ioc set */
197
#define DMA_INT_LVI             (1<<2)  /* last valid done */
198
#define DMA_INT_CELV            (1<<1)  /* last valid is current */
199
#define DMA_INT_DCH             (1)     /* DMA Controller Halted (happens on LVI interrupts) */ //not eqult intel
200
#define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
201
 
202
/* interrupts for the whole chip */
203
#define INT_SPDIFOUT   (1<<23)  /* controller spdif out INTERRUPT */
204
#define INT_SPDIFIN   (1<<22)
205
#define INT_CODECSPDIFOUT   (1<<19)
206
#define INT_MICIN   (1<<18)
207
#define INT_PCMOUT   (1<<17)
208
#define INT_PCMIN   (1<<16)
209
#define INT_CPRAIS   (1<<7)
210
#define INT_SPRAIS   (1<<5)
211
#define INT_GPIO    (1<<1)
212
#define INT_MASK   (INT_SPDIFOUT|INT_CODECSPDIFOUT|INT_MICIN|INT_PCMOUT|INT_PCMIN)
213
 
214
#define DRIVER_VERSION "0.03-ac"
215
 
216
/* magic numbers to protect our data structures */
217
#define ALI5455_CARD_MAGIC              0x5072696E      /* "Prin" */
218
#define ALI5455_STATE_MAGIC             0x63657373      /* "cess" */
219
#define ALI5455_DMA_MASK                0xffffffff      /* DMA buffer mask for pci_alloc_consist */
220
 
221
#define NR_HW_CH                        5               /* I think 5 channel */
222
 
223
/* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
224
#define NR_AC97         2
225
 
226
/* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
227
/* stream at a minimum for this card to be happy */
228
static const unsigned sample_size[] = { 1, 2, 2, 4 };
229
/* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
230
/* values are one less than might be expected */
231
static const unsigned sample_shift[] = { -1, 0, 0, 1 };
232
 
233
#define ALI5455
234
static char *card_names[] = {
235
        "ALI 5455"
236
};
237
 
238
static struct pci_device_id ali_pci_tbl[] __devinitdata = {
239
        {PCI_VENDOR_ID_ALI, PCI_DEVICE_ID_ALI_5455,
240
         PCI_ANY_ID, PCI_ANY_ID, 0, 0, ALI5455},
241
        {0,}
242
};
243
 
244
MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
245
 
246
#ifdef CONFIG_PM
247
#define PM_SUSPENDED(card) (card->pm_suspended)
248
#else
249
#define PM_SUSPENDED(card) (0)
250
#endif
251
 
252
/* "software" or virtual channel, an instance of opened /dev/dsp */
253
struct ali_state {
254
        unsigned int magic;
255
        struct ali_card *card;  /* Card info */
256
 
257
        /* single open lock mechanism, only used for recording */
258
        struct semaphore open_sem;
259
        wait_queue_head_t open_wait;
260
 
261
        /* file mode */
262
        mode_t open_mode;
263
 
264
        /* virtual channel number */
265
        int virt;
266
 
267
#ifdef CONFIG_PM
268
        unsigned int pm_saved_dac_rate, pm_saved_adc_rate;
269
#endif
270
        struct dmabuf {
271
                /* wave sample stuff */
272
                unsigned int rate;
273
                unsigned char fmt, enable, trigger;
274
 
275
                /* hardware channel */
276
                struct ali_channel *read_channel;
277
                struct ali_channel *write_channel;
278
                struct ali_channel *codec_spdifout_channel;
279
                struct ali_channel *controller_spdifout_channel;
280
 
281
                /* OSS buffer management stuff */
282
                void *rawbuf;
283
                dma_addr_t dma_handle;
284
                unsigned buforder;
285
                unsigned numfrag;
286
                unsigned fragshift;
287
 
288
                /* our buffer acts like a circular ring */
289
                unsigned hwptr;         /* where dma last started, updated by update_ptr */
290
                unsigned swptr;         /* where driver last clear/filled, updated by read/write */
291
                int count;              /* bytes to be consumed or been generated by dma machine */
292
                unsigned total_bytes;   /* total bytes dmaed by hardware */
293
 
294
                unsigned error;         /* number of over/underruns */
295
                wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
296
 
297
                /* redundant, but makes calculations easier */
298
                /* what the hardware uses */
299
                unsigned dmasize;
300
                unsigned fragsize;
301
                unsigned fragsamples;
302
 
303
                /* what we tell the user to expect */
304
                unsigned userfrags;
305
                unsigned userfragsize;
306
 
307
                /* OSS stuff */
308
                unsigned mapped:1;
309
                unsigned ready:1;
310
                unsigned update_flag;
311
                unsigned ossfragsize;
312
                unsigned ossmaxfrags;
313
                unsigned subdivision;
314
        } dmabuf;
315
};
316
 
317
 
318
struct ali_card {
319
        struct ali_channel channel[5];
320
        unsigned int magic;
321
 
322
        /* We keep ali5455 cards in a linked list */
323
        struct ali_card *next;
324
 
325
        /* The ali has a certain amount of cross channel interaction
326
           so we use a single per card lock */
327
        spinlock_t lock;
328
        spinlock_t ac97_lock;
329
 
330
        /* PCI device stuff */
331
        struct pci_dev *pci_dev;
332
        u16 pci_id;
333
#ifdef CONFIG_PM
334
        u16 pm_suspended;
335
        u32 pm_save_state[64 / sizeof(u32)];
336
        int pm_saved_mixer_settings[SOUND_MIXER_NRDEVICES][NR_AC97];
337
#endif
338
        /* soundcore stuff */
339
        int dev_audio;
340
 
341
        /* structures for abstraction of hardware facilities, codecs, banks and channels */
342
        struct ac97_codec *ac97_codec[NR_AC97];
343
        struct ali_state *states[NR_HW_CH];
344
 
345
        u16 ac97_features;
346
        u16 ac97_status;
347
        u16 channels;
348
 
349
        /* hardware resources */
350
        unsigned long iobase;
351
 
352
        u32 irq;
353
 
354
        /* Function support */
355
        struct ali_channel *(*alloc_pcm_channel) (struct ali_card *);
356
        struct ali_channel *(*alloc_rec_pcm_channel) (struct ali_card *);
357
        struct ali_channel *(*alloc_rec_mic_channel) (struct ali_card *);
358
        struct ali_channel *(*alloc_codec_spdifout_channel) (struct ali_card *);
359
        struct ali_channel *(*alloc_controller_spdifout_channel) (struct  ali_card *);
360
        void (*free_pcm_channel) (struct ali_card *, int chan);
361
 
362
        /* We have a *very* long init time possibly, so use this to block */
363
        /* attempts to open our devices before we are ready (stops oops'es) */
364
        int initializing;
365
};
366
 
367
 
368
static struct ali_card *devs = NULL;
369
 
370
static int ali_open_mixdev(struct inode *inode, struct file *file);
371
static int ali_ioctl_mixdev(struct inode *inode, struct file *file,
372
                            unsigned int cmd, unsigned long arg);
373
static u16 ali_ac97_get(struct ac97_codec *dev, u8 reg);
374
static void ali_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
375
 
376
static struct ali_channel *ali_alloc_pcm_channel(struct ali_card *card)
377
{
378
        if (card->channel[1].used == 1)
379
                return NULL;
380
        card->channel[1].used = 1;
381
        return &card->channel[1];
382
}
383
 
384
static struct ali_channel *ali_alloc_rec_pcm_channel(struct ali_card *card)
385
{
386
        if (card->channel[0].used == 1)
387
                return NULL;
388
        card->channel[0].used = 1;
389
        return &card->channel[0];
390
}
391
 
392
static struct ali_channel *ali_alloc_rec_mic_channel(struct ali_card *card)
393
{
394
        if (card->channel[2].used == 1)
395
                return NULL;
396
        card->channel[2].used = 1;
397
        return &card->channel[2];
398
}
399
 
400
static struct ali_channel *ali_alloc_codec_spdifout_channel(struct ali_card *card)
401
{
402
        if (card->channel[3].used == 1)
403
                return NULL;
404
        card->channel[3].used = 1;
405
        return &card->channel[3];
406
}
407
 
408
static struct ali_channel *ali_alloc_controller_spdifout_channel(struct ali_card *card)
409
{
410
        if (card->channel[4].used == 1)
411
                return NULL;
412
        card->channel[4].used = 1;
413
        return &card->channel[4];
414
}
415
static void ali_free_pcm_channel(struct ali_card *card, int channel)
416
{
417
        card->channel[channel].used = 0;
418
}
419
 
420
 
421
/*
422
 *      we use ALC650 which only support  48k sample rate, so we test firstly
423
 *      spdifout 's sample rate validity
424
 */
425
 
426
static int ali_valid_spdif_rate(struct ac97_codec *codec, int rate)
427
{
428
        unsigned long id = 0L;
429
 
430
        id = (ali_ac97_get(codec, AC97_VENDOR_ID1) << 16);
431
        id |= ali_ac97_get(codec, AC97_VENDOR_ID2) & 0xffff;
432
        switch (id) {
433
        case 0x41445361:        /* AD1886 */
434
                if (rate == 48000) {
435
                        return 1;
436
                }
437
                break;
438
        case 0x414c4720:        /* ALC650 */
439
                if (rate == 48000) {
440
                        return 1;
441
                }
442
                break;
443
        default:                /* all other codecs, until we know otherwiae */
444
                if (rate == 48000 || rate == 44100 || rate == 32000) {
445
                        return 1;
446
                }
447
                break;
448
        }
449
        return (0);
450
}
451
 
452
/* ali_set_spdif_output
453
 *
454
 *  Configure the S/PDIF output transmitter.
455
 *
456
 *  Assumptions:
457
 *     The DSP sample rate must already be set to a supported
458
 *     S/PDIF rate (32kHz, 44.1kHz, or 48kHz) or we abort.
459
 */
460
static void ali_set_spdif_output(struct ali_state *state, int slots, int rate)
461
{
462
        int vol;
463
        int aud_reg;
464
        struct ac97_codec *codec = state->card->ac97_codec[0];
465
 
466
        if (!(state->card->ac97_features & 4)) {
467
                state->card->ac97_status &= ~SPDIF_ON;
468
        } else {
469
                if (slots == -1) {      /* Turn off S/PDIF */
470
                        aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
471
                        ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
472
 
473
                        /* If the volume wasn't muted before we turned on S/PDIF, unmute it */
474
                        if (!(state->card->ac97_status & VOL_MUTED)) {
475
                                aud_reg = ali_ac97_get(codec, AC97_MASTER_VOL_STEREO);
476
                                ali_ac97_set(codec, AC97_MASTER_VOL_STEREO, (aud_reg & ~VOL_MUTED));
477
                        }
478
                        state->card->ac97_status &= ~(VOL_MUTED | SPDIF_ON);
479
                        return;
480
                }
481
 
482
                vol = ali_ac97_get(codec, AC97_MASTER_VOL_STEREO);
483
                state->card->ac97_status = vol & VOL_MUTED;
484
 
485
                /* Set S/PDIF transmitter sample rate */
486
                aud_reg = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
487
                switch (rate) {
488
                case 32000:
489
                        aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
490
                        break;
491
                case 44100:
492
                        aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
493
                        break;
494
                case 48000:
495
                        aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
496
                        break;
497
                default:
498
                        /* turn off S/PDIF */
499
                        aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
500
                        ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
501
                        state->card->ac97_status &= ~SPDIF_ON;
502
                        return;
503
                }
504
 
505
                ali_ac97_set(codec, AC97_SPDIF_CONTROL, aud_reg);
506
 
507
                aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
508
                aud_reg = (aud_reg & AC97_EA_SLOT_MASK) | slots | AC97_EA_SPDIF; /* ALC650 don't support VRA */
509
                ali_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
510
 
511
                aud_reg = ali_ac97_get(codec, AC97_POWER_CONTROL);
512
                aud_reg |= 0x0002;
513
                ali_ac97_set(codec, AC97_POWER_CONTROL, aud_reg);
514
                udelay(1);
515
 
516
                state->card->ac97_status |= SPDIF_ON;
517
 
518
                /* Check to make sure the configuration is valid */
519
                aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
520
                if (!(aud_reg & 0x0400)) {
521
                        /* turn off S/PDIF */
522
                        ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
523
                        state->card->ac97_status &= ~SPDIF_ON;
524
                        return;
525
                }
526
                if (codec_independent_spdif_locked > 0) {
527
                        aud_reg = ali_ac97_get(codec, 0x6a);
528
                        ali_ac97_set(codec, 0x6a, (aud_reg & 0xefff));
529
                }
530
        }
531
}
532
 
533
/* ali_set_dac_channels
534
 *
535
 *  Configure the codec's multi-channel DACs
536
 *
537
 *  TODO:
538
 *    vailidate that the codec really supports these DACs
539
 *    before turning them on.
540
 */
541
static void ali_set_dac_channels(struct ali_state *state, int channel)
542
{
543
        int aud_reg;
544
        struct ac97_codec *codec = state->card->ac97_codec[0];
545
 
546
        aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
547
        aud_reg |= AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK;
548
        state->card->ac97_status &= ~(SURR_ON | CENTER_LFE_ON);
549
 
550
        switch (channel) {
551
        case 2:         /* always enabled */
552
                break;
553
        case 4:
554
                aud_reg &= ~AC97_EA_PRJ;
555
                state->card->ac97_status |= SURR_ON;
556
                break;
557
        case 6:
558
                aud_reg &= ~(AC97_EA_PRJ | AC97_EA_PRI | AC97_EA_PRK);
559
                state->card->ac97_status |= SURR_ON | CENTER_LFE_ON;
560
                break;
561
        default:
562
                break;
563
        }
564
        ali_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
565
 
566
}
567
 
568
/* set playback sample rate */
569
static unsigned int ali_set_dac_rate(struct ali_state *state, unsigned int rate)
570
{
571
        struct dmabuf *dmabuf = &state->dmabuf;
572
        u32 new_rate;
573
        struct ac97_codec *codec = state->card->ac97_codec[0];
574
 
575
        if (!(state->card->ac97_features & 0x0001)) {
576
                dmabuf->rate = clocking;
577
                return clocking;
578
        }
579
 
580
        if (rate > 48000)
581
                rate = 48000;
582
        if (rate < 8000)
583
                rate = 8000;
584
        dmabuf->rate = rate;
585
 
586
        /*
587
         *      Adjust for misclocked crap
588
         */
589
 
590
        rate = (rate * clocking) / 48000;
591
 
592
        if (strict_clocking && rate < 8000) {
593
                rate = 8000;
594
                dmabuf->rate = (rate * 48000) / clocking;
595
        }
596
 
597
        new_rate = ac97_set_dac_rate(codec, rate);
598
        if (new_rate != rate) {
599
                dmabuf->rate = (new_rate * 48000) / clocking;
600
        }
601
        rate = new_rate;
602
        return dmabuf->rate;
603
}
604
 
605
/* set recording sample rate */
606
static unsigned int ali_set_adc_rate(struct ali_state *state, unsigned int rate)
607
{
608
        struct dmabuf *dmabuf = &state->dmabuf;
609
        u32 new_rate;
610
        struct ac97_codec *codec = state->card->ac97_codec[0];
611
 
612
        if (!(state->card->ac97_features & 0x0001)) {
613
                dmabuf->rate = clocking;
614
                return clocking;
615
        }
616
 
617
        if (rate > 48000)
618
                rate = 48000;
619
        if (rate < 8000)
620
                rate = 8000;
621
        dmabuf->rate = rate;
622
 
623
        /*
624
         *      Adjust for misclocked crap
625
         */
626
 
627
        rate = (rate * clocking) / 48000;
628
        if (strict_clocking && rate < 8000) {
629
                rate = 8000;
630
                dmabuf->rate = (rate * 48000) / clocking;
631
        }
632
 
633
        new_rate = ac97_set_adc_rate(codec, rate);
634
 
635
        if (new_rate != rate) {
636
                dmabuf->rate = (new_rate * 48000) / clocking;
637
                rate = new_rate;
638
        }
639
        return dmabuf->rate;
640
}
641
 
642
/* set codec independent spdifout sample rate */
643
static unsigned int ali_set_codecspdifout_rate(struct ali_state *state, unsigned int rate)
644
{
645
        struct dmabuf *dmabuf = &state->dmabuf;
646
 
647
        if (!(state->card->ac97_features & 0x0001)) {
648
                dmabuf->rate = clocking;
649
                return clocking;
650
        }
651
 
652
        if (rate > 48000)
653
                rate = 48000;
654
        if (rate < 8000)
655
                rate = 8000;
656
        dmabuf->rate = rate;
657
 
658
        return dmabuf->rate;
659
}
660
 
661
/* set  controller independent spdif out function sample rate */
662
static void ali_set_spdifout_rate(struct ali_state *state, unsigned int rate)
663
{
664
        unsigned char ch_st_sel;
665
        unsigned short status_rate;
666
 
667
        switch (rate) {
668
        case 44100:
669
                status_rate = 0;
670
                break;
671
        case 32000:
672
                status_rate = 0x300;
673
                break;
674
        case 48000:
675
        default:
676
                status_rate = 0x200;
677
                break;
678
        }
679
 
680
        ch_st_sel = inb(state->card->iobase + ALI_SPDIFICS) & ALI_SPDIF_OUT_CH_STATUS;  //select spdif_out
681
 
682
        ch_st_sel |= 0x80;      //select right
683
        outb(ch_st_sel, (state->card->iobase + ALI_SPDIFICS));
684
        outb(status_rate | 0x20, (state->card->iobase + ALI_SPDIFCSR + 2));
685
 
686
        ch_st_sel &= (~0x80);   //select left
687
        outb(ch_st_sel, (state->card->iobase + ALI_SPDIFICS));
688
        outw(status_rate | 0x10, (state->card->iobase + ALI_SPDIFCSR + 2));
689
}
690
 
691
/* get current playback/recording dma buffer pointer (byte offset from LBA),
692
   called with spinlock held! */
693
 
694
static inline unsigned ali_get_dma_addr(struct ali_state *state, int rec)
695
{
696
        struct dmabuf *dmabuf = &state->dmabuf;
697
        unsigned int civ, offset, port, port_picb;
698
        unsigned int data;
699
 
700
        if (!dmabuf->enable)
701
                return 0;
702
 
703
        if (rec == 1)
704
                port = state->card->iobase + dmabuf->read_channel->port;
705
        else if (rec == 2)
706
                port = state->card->iobase + dmabuf->codec_spdifout_channel->port;
707
        else if (rec == 3)
708
                port = state->card->iobase + dmabuf->controller_spdifout_channel->port;
709
        else
710
                port = state->card->iobase + dmabuf->write_channel->port;
711
 
712
        port_picb = port + OFF_PICB;
713
 
714
        do {
715
                civ = inb(port + OFF_CIV) & 31;
716
                offset = inw(port_picb);
717
                /* Must have a delay here! */
718
                if (offset == 0)
719
                        udelay(1);
720
 
721
                /* Reread both registers and make sure that that total
722
                 * offset from the first reading to the second is 0.
723
                 * There is an issue with SiS hardware where it will count
724
                 * picb down to 0, then update civ to the next value,
725
                 * then set the new picb to fragsize bytes.  We can catch
726
                 * it between the civ update and the picb update, making
727
                 * it look as though we are 1 fragsize ahead of where we
728
                 * are.  The next to we get the address though, it will
729
                 * be back in thdelay is more than long enough
730
                 * that we won't have to worry about the chip still being
731
                 * out of sync with reality ;-)
732
                 */
733
        } while (civ != (inb(port + OFF_CIV) & 31) || offset != inw(port_picb));
734
 
735
        data = ((civ + 1) * dmabuf->fragsize - (2 * offset)) % dmabuf->dmasize;
736
        if (inw(port_picb) == 0)
737
                data -= 2048;
738
        /* It is  hardware  bug  when read port 's PICB ==0 */
739
        if ( inw(port_picb) == 0 )
740
                data -= 2048;
741
        return data;
742
}
743
 
744
/* Stop recording (lock held) */
745
static inline void __stop_adc(struct ali_state *state)
746
{
747
        struct dmabuf *dmabuf = &state->dmabuf;
748
        struct ali_card *card = state->card;
749
 
750
        dmabuf->enable &= ~ADC_RUNNING;
751
 
752
        outl((1 << 18) | (1 << 16), card->iobase + ALI_DMACR);
753
        udelay(1);
754
 
755
        outb(0, card->iobase + PI_CR);
756
 
757
        // wait for the card to acknowledge shutdown
758
        while (inb(card->iobase + PI_CR) != 0);
759
 
760
        // now clear any latent interrupt bits (like the halt bit)
761
        outb(inb(card->iobase + PI_SR) | 0x001e, card->iobase + PI_SR);
762
        outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_PCMIN, card->iobase + ALI_INTERRUPTSR);
763
}
764
 
765
static void stop_adc(struct ali_state *state)
766
{
767
        struct ali_card *card = state->card;
768
        unsigned long flags;
769
        spin_lock_irqsave(&card->lock, flags);
770
        __stop_adc(state);
771
        spin_unlock_irqrestore(&card->lock, flags);
772
}
773
 
774
static inline void __start_adc(struct ali_state *state)
775
{
776
        struct dmabuf *dmabuf = &state->dmabuf;
777
 
778
        if (dmabuf->count < dmabuf->dmasize && dmabuf->ready
779
            && !dmabuf->enable && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
780
                dmabuf->enable |= ADC_RUNNING;
781
                outb((1 << 4) | (1 << 2), state->card->iobase + PI_CR);
782
                if (state->card->channel[0].used == 1)
783
                        outl(1, state->card->iobase + ALI_DMACR);       // DMA CONTROL REGISTRER
784
                if (state->card->channel[2].used == 1)
785
                        outl((1 << 2), state->card->iobase + ALI_DMACR);        //DMA CONTROL REGISTER
786
        }
787
}
788
 
789
static void start_adc(struct ali_state *state)
790
{
791
        struct ali_card *card = state->card;
792
        unsigned long flags;
793
 
794
        spin_lock_irqsave(&card->lock, flags);
795
        __start_adc(state);
796
        spin_unlock_irqrestore(&card->lock, flags);
797
}
798
 
799
/* stop playback (lock held) */
800
static inline void __stop_dac(struct ali_state *state)
801
{
802
        struct dmabuf *dmabuf = &state->dmabuf;
803
        struct ali_card *card = state->card;
804
 
805
        dmabuf->enable &= ~DAC_RUNNING;
806
        outl(0x00020000, card->iobase + 0x08);
807
        outb(0, card->iobase + PO_CR);
808
        while (inb(card->iobase + PO_CR) != 0)
809
                cpu_relax();
810
 
811
        outb(inb(card->iobase + PO_SR) | 0x001e, card->iobase + PO_SR);
812
 
813
        outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_PCMOUT, card->iobase + ALI_INTERRUPTSR);
814
}
815
 
816
static void stop_dac(struct ali_state *state)
817
{
818
        struct ali_card *card = state->card;
819
        unsigned long flags;
820
        spin_lock_irqsave(&card->lock, flags);
821
        __stop_dac(state);
822
        spin_unlock_irqrestore(&card->lock, flags);
823
}
824
 
825
static inline void __start_dac(struct ali_state *state)
826
{
827
        struct dmabuf *dmabuf = &state->dmabuf;
828
        if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
829
            (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
830
                dmabuf->enable |= DAC_RUNNING;
831
                outb((1 << 4) | (1 << 2), state->card->iobase + PO_CR);
832
                outl((1 << 1), state->card->iobase + 0x08);     //dma control register
833
        }
834
}
835
 
836
static void start_dac(struct ali_state *state)
837
{
838
        struct ali_card *card = state->card;
839
        unsigned long flags;
840
        spin_lock_irqsave(&card->lock, flags);
841
        __start_dac(state);
842
        spin_unlock_irqrestore(&card->lock, flags);
843
}
844
 
845
/* stop codec and controller spdif out  (lock held) */
846
static inline void __stop_spdifout(struct ali_state *state)
847
{
848
        struct dmabuf *dmabuf = &state->dmabuf;
849
        struct ali_card *card = state->card;
850
 
851
        if (codec_independent_spdif_locked > 0) {
852
                dmabuf->enable &= ~CODEC_SPDIFOUT_RUNNING;
853
                outl((1 << 19), card->iobase + 0x08);
854
                outb(0, card->iobase + CODECSPDIFOUT_CR);
855
 
856
                while (inb(card->iobase + CODECSPDIFOUT_CR) != 0)
857
                        cpu_relax();
858
 
859
                outb(inb(card->iobase + CODECSPDIFOUT_SR) | 0x001e, card->iobase + CODECSPDIFOUT_SR);
860
                outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_CODECSPDIFOUT, card->iobase + ALI_INTERRUPTSR);
861
        } else {
862
                if (controller_independent_spdif_locked > 0) {
863
                        dmabuf->enable &= ~CONTROLLER_SPDIFOUT_RUNNING;
864
                        outl((1 << 23), card->iobase + 0x08);
865
                        outb(0, card->iobase + CONTROLLERSPDIFOUT_CR);
866
                        while (inb(card->iobase + CONTROLLERSPDIFOUT_CR) != 0)
867
                                cpu_relax();
868
                        outb(inb(card->iobase + CONTROLLERSPDIFOUT_SR) | 0x001e, card->iobase + CONTROLLERSPDIFOUT_SR);
869
                        outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_SPDIFOUT, card->iobase + ALI_INTERRUPTSR);
870
                }
871
        }
872
}
873
 
874
static void stop_spdifout(struct ali_state *state)
875
{
876
        struct ali_card *card = state->card;
877
        unsigned long flags;
878
        spin_lock_irqsave(&card->lock, flags);
879
        __stop_spdifout(state);
880
        spin_unlock_irqrestore(&card->lock, flags);
881
}
882
 
883
static inline void __start_spdifout(struct ali_state *state)
884
{
885
        struct dmabuf *dmabuf = &state->dmabuf;
886
        if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
887
            (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
888
                if (codec_independent_spdif_locked > 0) {
889
                        dmabuf->enable |= CODEC_SPDIFOUT_RUNNING;
890
                        outb((1 << 4) | (1 << 2), state->card->iobase + CODECSPDIFOUT_CR);
891
                        outl((1 << 3), state->card->iobase + 0x08);     //dma control register
892
                } else {
893
                        if (controller_independent_spdif_locked > 0) {
894
                                dmabuf->enable |= CONTROLLER_SPDIFOUT_RUNNING;
895
                                outb((1 << 4) | (1 << 2), state->card->iobase + CONTROLLERSPDIFOUT_CR);
896
                                outl((1 << 7), state->card->iobase + 0x08);     //dma control register
897
                        }
898
                }
899
        }
900
}
901
 
902
static void start_spdifout(struct ali_state *state)
903
{
904
        struct ali_card *card = state->card;
905
        unsigned long flags;
906
        spin_lock_irqsave(&card->lock, flags);
907
        __start_spdifout(state);
908
        spin_unlock_irqrestore(&card->lock, flags);
909
}
910
 
911
#define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
912
#define DMABUF_MINORDER 1
913
 
914
/* allocate DMA buffer, playback , recording,spdif out  buffer should be allocated seperately */
915
static int alloc_dmabuf(struct ali_state *state)
916
{
917
        struct dmabuf *dmabuf = &state->dmabuf;
918
        void *rawbuf = NULL;
919
        int order, size;
920
        struct page *page, *pend;
921
 
922
        /* If we don't have any oss frag params, then use our default ones */
923
        if (dmabuf->ossmaxfrags == 0)
924
                dmabuf->ossmaxfrags = 4;
925
        if (dmabuf->ossfragsize == 0)
926
                dmabuf->ossfragsize = (PAGE_SIZE << DMABUF_DEFAULTORDER) / dmabuf->ossmaxfrags;
927
        size = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
928
 
929
        if (dmabuf->rawbuf && (PAGE_SIZE << dmabuf->buforder) == size)
930
                return 0;
931
        /* alloc enough to satisfy the oss params */
932
        for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
933
                if ((PAGE_SIZE << order) > size)
934
                        continue;
935
                if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
936
                                                   PAGE_SIZE << order,
937
                                                   &dmabuf->dma_handle)))
938
                        break;
939
        }
940
        if (!rawbuf)
941
                return -ENOMEM;
942
 
943
        dmabuf->ready = dmabuf->mapped = 0;
944
        dmabuf->rawbuf = rawbuf;
945
        dmabuf->buforder = order;
946
 
947
        /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
948
        pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
949
        for (page = virt_to_page(rawbuf); page <= pend; page++)
950
                mem_map_reserve(page);
951
        return 0;
952
}
953
 
954
/* free DMA buffer */
955
static void dealloc_dmabuf(struct ali_state *state)
956
{
957
        struct dmabuf *dmabuf = &state->dmabuf;
958
        struct page *page, *pend;
959
 
960
        if (dmabuf->rawbuf) {
961
                /* undo marking the pages as reserved */
962
                pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
963
                for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
964
                        mem_map_unreserve(page);
965
                pci_free_consistent(state->card->pci_dev,
966
                                    PAGE_SIZE << dmabuf->buforder,
967
                                    dmabuf->rawbuf, dmabuf->dma_handle);
968
        }
969
        dmabuf->rawbuf = NULL;
970
        dmabuf->mapped = dmabuf->ready = 0;
971
}
972
 
973
static int prog_dmabuf(struct ali_state *state, unsigned rec)
974
{
975
        struct dmabuf *dmabuf = &state->dmabuf;
976
        struct ali_channel *c = NULL ;
977
        struct sg_item *sg;
978
        unsigned long flags;
979
        int ret;
980
        unsigned fragint;
981
        int i;
982
 
983
        spin_lock_irqsave(&state->card->lock, flags);
984
        if (dmabuf->enable & DAC_RUNNING)
985
                __stop_dac(state);
986
        if (dmabuf->enable & ADC_RUNNING)
987
                __stop_adc(state);
988
        if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
989
                __stop_spdifout(state);
990
        if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
991
                __stop_spdifout(state);
992
 
993
        dmabuf->total_bytes = 0;
994
        dmabuf->count = dmabuf->error = 0;
995
        dmabuf->swptr = dmabuf->hwptr = 0;
996
        spin_unlock_irqrestore(&state->card->lock, flags);
997
 
998
        /* allocate DMA buffer, let alloc_dmabuf determine if we are already
999
         * allocated well enough or if we should replace the current buffer
1000
         * (assuming one is already allocated, if it isn't, then allocate it).
1001
         */
1002
        if ((ret = alloc_dmabuf(state)))
1003
                return ret;
1004
 
1005
        /* FIXME: figure out all this OSS fragment stuff */
1006
        /* I did, it now does what it should according to the OSS API.  DL */
1007
        /* We may not have realloced our dmabuf, but the fragment size to
1008
         * fragment number ratio may have changed, so go ahead and reprogram
1009
         * things
1010
         */
1011
 
1012
        dmabuf->dmasize = PAGE_SIZE << dmabuf->buforder;
1013
        dmabuf->numfrag = SG_LEN;
1014
        dmabuf->fragsize = dmabuf->dmasize / dmabuf->numfrag;
1015
        dmabuf->fragsamples = dmabuf->fragsize >> 1;
1016
        dmabuf->userfragsize = dmabuf->ossfragsize;
1017
        dmabuf->userfrags = dmabuf->dmasize / dmabuf->ossfragsize;
1018
 
1019
        memset(dmabuf->rawbuf, 0, dmabuf->dmasize);
1020
 
1021
        if (dmabuf->ossmaxfrags == 4) {
1022
                fragint = 8;
1023
                dmabuf->fragshift = 2;
1024
        } else if (dmabuf->ossmaxfrags == 8) {
1025
                fragint = 4;
1026
                dmabuf->fragshift = 3;
1027
        } else if (dmabuf->ossmaxfrags == 16) {
1028
                fragint = 2;
1029
                dmabuf->fragshift = 4;
1030
        } else {
1031
                fragint = 1;
1032
                dmabuf->fragshift = 5;
1033
        }
1034
        /*
1035
         *      Now set up the ring
1036
         */
1037
 
1038
        if (rec == 1)
1039
                c = dmabuf->read_channel;
1040
        else if (rec == 2)
1041
                c = dmabuf->codec_spdifout_channel;
1042
        else if (rec == 3)
1043
                c = dmabuf->controller_spdifout_channel;
1044
        else if (rec == 0)
1045
                c = dmabuf->write_channel;
1046
        if (c != NULL) {
1047
                sg = &c->sg[0];
1048
                /*
1049
                 *      Load up 32 sg entries and take an interrupt at half
1050
                 *      way (we might want more interrupts later..)
1051
                 */
1052
                for (i = 0; i < dmabuf->numfrag; i++) {
1053
                        sg->busaddr =
1054
                            virt_to_bus(dmabuf->rawbuf +
1055
                                        dmabuf->fragsize * i);
1056
                        // the card will always be doing 16bit stereo
1057
                        sg->control = dmabuf->fragsamples;
1058
                        sg->control |= CON_BUFPAD;      //I modify
1059
                        // set us up to get IOC interrupts as often as needed to
1060
                        // satisfy numfrag requirements, no more
1061
                        if (((i + 1) % fragint) == 0) {
1062
                                sg->control |= CON_IOC;
1063
                        }
1064
                        sg++;
1065
                }
1066
                spin_lock_irqsave(&state->card->lock, flags);
1067
                outb(2, state->card->iobase + c->port + OFF_CR);        /* reset DMA machine */
1068
                outl(virt_to_bus(&c->sg[0]), state->card->iobase + c->port + OFF_BDBAR);
1069
                outb(0, state->card->iobase + c->port + OFF_CIV);
1070
                outb(0, state->card->iobase + c->port + OFF_LVI);
1071
                spin_unlock_irqrestore(&state->card->lock, flags);
1072
        }
1073
        /* set the ready flag for the dma buffer */
1074
        dmabuf->ready = 1;
1075
        return 0;
1076
}
1077
 
1078
static void __ali_update_lvi(struct ali_state *state, int rec)
1079
{
1080
        struct dmabuf *dmabuf = &state->dmabuf;
1081
        int x, port;
1082
        port = state->card->iobase;
1083
        if (rec == 1)
1084
                port += dmabuf->read_channel->port;
1085
        else if (rec == 2)
1086
                port += dmabuf->codec_spdifout_channel->port;
1087
        else if (rec == 3)
1088
                port += dmabuf->controller_spdifout_channel->port;
1089
        else if (rec == 0)
1090
                port += dmabuf->write_channel->port;
1091
        /* if we are currently stopped, then our CIV is actually set to our
1092
         * *last* sg segment and we are ready to wrap to the next.  However,
1093
         * if we set our LVI to the last sg segment, then it won't wrap to
1094
         * the next sg segment, it won't even get a start.  So, instead, when
1095
         * we are stopped, we set both the LVI value and also we increment
1096
         * the CIV value to the next sg segment to be played so that when
1097
         * we call start_{dac,adc}, things will operate properly
1098
         */
1099
        if (!dmabuf->enable && dmabuf->ready) {
1100
                if (rec && dmabuf->count < dmabuf->dmasize && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
1101
                        outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1102
                        __start_adc(state);
1103
                        while (! (inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1104
                                cpu_relax();
1105
                } else if (!rec && dmabuf->count && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
1106
                        outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1107
                        __start_dac(state);
1108
                        while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1109
                                cpu_relax();
1110
                } else if (rec && dmabuf->count && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
1111
                        if (codec_independent_spdif_locked > 0) {
1112
                                // outb((inb(port+OFF_CIV))&31, port+OFF_LVI);
1113
                                outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1114
                                __start_spdifout(state);
1115
                                while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1116
                                        cpu_relax();
1117
                        } else {
1118
                                if (controller_independent_spdif_locked > 0) {
1119
                                        outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1120
                                        __start_spdifout(state);
1121
                                        while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1122
                                                cpu_relax();
1123
                                }
1124
                        }
1125
                }
1126
        }
1127
 
1128
        /* swptr - 1 is the tail of our transfer */
1129
        x = (dmabuf->dmasize + dmabuf->swptr - 1) % dmabuf->dmasize;
1130
        x /= dmabuf->fragsize;
1131
        outb(x, port + OFF_LVI);
1132
}
1133
 
1134
static void ali_update_lvi(struct ali_state *state, int rec)
1135
{
1136
        struct dmabuf *dmabuf = &state->dmabuf;
1137
        unsigned long flags;
1138
        if (!dmabuf->ready)
1139
                return;
1140
        spin_lock_irqsave(&state->card->lock, flags);
1141
        __ali_update_lvi(state, rec);
1142
        spin_unlock_irqrestore(&state->card->lock, flags);
1143
}
1144
 
1145
/* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1146
static void ali_update_ptr(struct ali_state *state)
1147
{
1148
        struct dmabuf *dmabuf = &state->dmabuf;
1149
        unsigned hwptr;
1150
        int diff;
1151
 
1152
        /* error handling and process wake up for DAC */
1153
        if (dmabuf->enable == ADC_RUNNING) {
1154
                /* update hardware pointer */
1155
                hwptr = ali_get_dma_addr(state, 1);
1156
                diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1157
                dmabuf->hwptr = hwptr;
1158
                dmabuf->total_bytes += diff;
1159
                dmabuf->count += diff;
1160
                if (dmabuf->count > dmabuf->dmasize) {
1161
                        /* buffer underrun or buffer overrun */
1162
                        /* this is normal for the end of a read */
1163
                        /* only give an error if we went past the */
1164
                        /* last valid sg entry */
1165
                        if ((inb(state->card->iobase + PI_CIV) & 31) != (inb(state->card->iobase + PI_LVI) & 31)) {
1166
                                printk(KERN_WARNING "ali_audio: DMA overrun on read\n");
1167
                                dmabuf->error++;
1168
                        }
1169
                }
1170
                if (dmabuf->count > dmabuf->userfragsize)
1171
                        wake_up(&dmabuf->wait);
1172
        }
1173
        /* error handling and process wake up for DAC */
1174
        if (dmabuf->enable == DAC_RUNNING) {
1175
                /* update hardware pointer */
1176
                hwptr = ali_get_dma_addr(state, 0);
1177
                diff =
1178
                    (dmabuf->dmasize + hwptr -
1179
                     dmabuf->hwptr) % dmabuf->dmasize;
1180
#if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
1181
                printk("DAC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
1182
#endif
1183
                dmabuf->hwptr = hwptr;
1184
                dmabuf->total_bytes += diff;
1185
                dmabuf->count -= diff;
1186
                if (dmabuf->count < 0) {
1187
                        /* buffer underrun or buffer overrun */
1188
                        /* this is normal for the end of a write */
1189
                        /* only give an error if we went past the */
1190
                        /* last valid sg entry */
1191
                        if ((inb(state->card->iobase + PO_CIV) & 31) != (inb(state->card->iobase + PO_LVI) & 31)) {
1192
                                printk(KERN_WARNING "ali_audio: DMA overrun on write\n");
1193
                                printk(KERN_DEBUG "ali_audio: CIV %d, LVI %d, hwptr %x, count %d\n",
1194
                                                        inb(state->card->iobase + PO_CIV) & 31,
1195
                                                        inb(state->card->iobase + PO_LVI) & 31,
1196
                                                        dmabuf->hwptr,
1197
                                                        dmabuf->count);
1198
                                dmabuf->error++;
1199
                        }
1200
                }
1201
                if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
1202
                        wake_up(&dmabuf->wait);
1203
        }
1204
 
1205
        /* error handling and process wake up for CODEC SPDIF OUT */
1206
        if (dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
1207
                /* update hardware pointer */
1208
                hwptr = ali_get_dma_addr(state, 2);
1209
                diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1210
                dmabuf->hwptr = hwptr;
1211
                dmabuf->total_bytes += diff;
1212
                dmabuf->count -= diff;
1213
                if (dmabuf->count < 0) {
1214
                        /* buffer underrun or buffer overrun */
1215
                        /* this is normal for the end of a write */
1216
                        /* only give an error if we went past the */
1217
                        /* last valid sg entry */
1218
                        if ((inb(state->card->iobase + CODECSPDIFOUT_CIV) & 31) != (inb(state->card->iobase + CODECSPDIFOUT_LVI) & 31)) {
1219
                                printk(KERN_WARNING "ali_audio: DMA overrun on write\n");
1220
                                printk(KERN_DEBUG "ali_audio: CIV %d, LVI %d, hwptr %x, count %d\n",
1221
                                        inb(state->card->iobase + CODECSPDIFOUT_CIV) & 31,
1222
                                        inb(state->card->iobase + CODECSPDIFOUT_LVI) & 31,
1223
                                        dmabuf->hwptr, dmabuf->count);
1224
                                dmabuf->error++;
1225
                        }
1226
                }
1227
                if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
1228
                        wake_up(&dmabuf->wait);
1229
        }
1230
        /* error handling and process wake up for CONTROLLER SPDIF OUT */
1231
        if (dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
1232
                /* update hardware pointer */
1233
                hwptr = ali_get_dma_addr(state, 3);
1234
                diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1235
                dmabuf->hwptr = hwptr;
1236
                dmabuf->total_bytes += diff;
1237
                dmabuf->count -= diff;
1238
                if (dmabuf->count < 0) {
1239
                        /* buffer underrun or buffer overrun */
1240
                        /* this is normal for the end of a write */
1241
                        /* only give an error if we went past the */
1242
                        /* last valid sg entry */
1243
                        if ((inb(state->card->iobase + CONTROLLERSPDIFOUT_CIV) & 31) != (inb(state->card->iobase + CONTROLLERSPDIFOUT_LVI) & 31)) {
1244
                                printk(KERN_WARNING
1245
                                       "ali_audio: DMA overrun on write\n");
1246
                                printk("ali_audio: CIV %d, LVI %d, hwptr %x, "
1247
                                        "count %d\n",
1248
                                                inb(state->card->iobase + CONTROLLERSPDIFOUT_CIV) & 31,
1249
                                                inb(state->card->iobase + CONTROLLERSPDIFOUT_LVI) & 31,
1250
                                                dmabuf->hwptr, dmabuf->count);
1251
                                dmabuf->error++;
1252
                        }
1253
                }
1254
                if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
1255
                        wake_up(&dmabuf->wait);
1256
        }
1257
}
1258
 
1259
static inline int ali_get_free_write_space(struct ali_state *state)
1260
{
1261
        struct dmabuf *dmabuf = &state->dmabuf;
1262
        int free;
1263
        ali_update_ptr(state);
1264
        // catch underruns during playback
1265
        if (dmabuf->count < 0) {
1266
                dmabuf->count = 0;
1267
                dmabuf->swptr = dmabuf->hwptr;
1268
        }
1269
        free = dmabuf->dmasize - dmabuf->count;
1270
        free -= (dmabuf->hwptr % dmabuf->fragsize);
1271
        if (free < 0)
1272
                return (0);
1273
        return (free);
1274
}
1275
 
1276
static inline int ali_get_available_read_data(struct ali_state *state)
1277
{
1278
        struct dmabuf *dmabuf = &state->dmabuf;
1279
        int avail;
1280
        ali_update_ptr(state);
1281
        // catch overruns during record
1282
        if (dmabuf->count > dmabuf->dmasize) {
1283
                dmabuf->count = dmabuf->dmasize;
1284
                dmabuf->swptr = dmabuf->hwptr;
1285
        }
1286
        avail = dmabuf->count;
1287
        avail -= (dmabuf->hwptr % dmabuf->fragsize);
1288
        if (avail < 0)
1289
                return (0);
1290
        return (avail);
1291
}
1292
 
1293
static int drain_dac(struct ali_state *state, int signals_allowed)
1294
{
1295
        DECLARE_WAITQUEUE(wait, current);
1296
        struct dmabuf *dmabuf = &state->dmabuf;
1297
        unsigned long flags;
1298
        unsigned long tmo;
1299
        int count;
1300
 
1301
        if (!dmabuf->ready)
1302
                return 0;
1303
        if (dmabuf->mapped) {
1304
                stop_dac(state);
1305
                return 0;
1306
        }
1307
        add_wait_queue(&dmabuf->wait, &wait);
1308
        for (;;) {
1309
                spin_lock_irqsave(&state->card->lock, flags);
1310
                ali_update_ptr(state);
1311
                count = dmabuf->count;
1312
                spin_unlock_irqrestore(&state->card->lock, flags);
1313
 
1314
                if (count <= 0)
1315
                        break;
1316
                /*
1317
                 * This will make sure that our LVI is correct, that our
1318
                 * pointer is updated, and that the DAC is running.  We
1319
                 * have to force the setting of dmabuf->trigger to avoid
1320
                 * any possible deadlocks.
1321
                 */
1322
                if (!dmabuf->enable) {
1323
                        dmabuf->trigger = PCM_ENABLE_OUTPUT;
1324
                        ali_update_lvi(state, 0);
1325
                }
1326
                if (signal_pending(current) && signals_allowed) {
1327
                        break;
1328
                }
1329
 
1330
                /* It seems that we have to set the current state to
1331
                 * TASK_INTERRUPTIBLE every time to make the process
1332
                 * really go to sleep.  This also has to be *after* the
1333
                 * update_ptr() call because update_ptr is likely to
1334
                 * do a wake_up() which will unset this before we ever
1335
                 * try to sleep, resuling in a tight loop in this code
1336
                 * instead of actually sleeping and waiting for an
1337
                 * interrupt to wake us up!
1338
                 */
1339
                set_current_state(TASK_INTERRUPTIBLE);
1340
                /*
1341
                 * set the timeout to significantly longer than it *should*
1342
                 * take for the DAC to drain the DMA buffer
1343
                 */
1344
                tmo = (count * HZ) / (dmabuf->rate);
1345
                if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1346
                        printk(KERN_ERR "ali_audio: drain_dac, dma timeout?\n");
1347
                        count = 0;
1348
                        break;
1349
                }
1350
        }
1351
        set_current_state(TASK_RUNNING);
1352
        remove_wait_queue(&dmabuf->wait, &wait);
1353
        if (count > 0 && signal_pending(current) && signals_allowed)
1354
                return -ERESTARTSYS;
1355
        stop_dac(state);
1356
        return 0;
1357
}
1358
 
1359
 
1360
static int drain_spdifout(struct ali_state *state, int signals_allowed)
1361
{
1362
        DECLARE_WAITQUEUE(wait, current);
1363
        struct dmabuf *dmabuf = &state->dmabuf;
1364
        unsigned long flags;
1365
        unsigned long tmo;
1366
        int count;
1367
        if (!dmabuf->ready)
1368
                return 0;
1369
        if (dmabuf->mapped) {
1370
                stop_spdifout(state);
1371
                return 0;
1372
        }
1373
        add_wait_queue(&dmabuf->wait, &wait);
1374
        for (;;) {
1375
 
1376
                spin_lock_irqsave(&state->card->lock, flags);
1377
                ali_update_ptr(state);
1378
                count = dmabuf->count;
1379
                spin_unlock_irqrestore(&state->card->lock, flags);
1380
                if (count <= 0)
1381
                        break;
1382
                /*
1383
                 * This will make sure that our LVI is correct, that our
1384
                 * pointer is updated, and that the DAC is running.  We
1385
                 * have to force the setting of dmabuf->trigger to avoid
1386
                 * any possible deadlocks.
1387
                 */
1388
                if (!dmabuf->enable) {
1389
                        if (codec_independent_spdif_locked > 0) {
1390
                                dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1391
                                ali_update_lvi(state, 2);
1392
                        } else {
1393
                                if (controller_independent_spdif_locked > 0) {
1394
                                        dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1395
                                        ali_update_lvi(state, 3);
1396
                                }
1397
                        }
1398
                }
1399
                if (signal_pending(current) && signals_allowed) {
1400
                        break;
1401
                }
1402
 
1403
                /* It seems that we have to set the current state to
1404
                 * TASK_INTERRUPTIBLE every time to make the process
1405
                 * really go to sleep.  This also has to be *after* the
1406
                 * update_ptr() call because update_ptr is likely to
1407
                 * do a wake_up() which will unset this before we ever
1408
                 * try to sleep, resuling in a tight loop in this code
1409
                 * instead of actually sleeping and waiting for an
1410
                 * interrupt to wake us up!
1411
                 */
1412
                set_current_state(TASK_INTERRUPTIBLE);
1413
                /*
1414
                 * set the timeout to significantly longer than it *should*
1415
                 * take for the DAC to drain the DMA buffer
1416
                 */
1417
                tmo = (count * HZ) / (dmabuf->rate);
1418
                if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1419
                        printk(KERN_ERR "ali_audio: drain_spdifout, dma timeout?\n");
1420
                        count = 0;
1421
                        break;
1422
                }
1423
        }
1424
        set_current_state(TASK_RUNNING);
1425
        remove_wait_queue(&dmabuf->wait, &wait);
1426
        if (count > 0 && signal_pending(current) && signals_allowed)
1427
                return -ERESTARTSYS;
1428
        stop_spdifout(state);
1429
        return 0;
1430
}
1431
 
1432
static void ali_channel_interrupt(struct ali_card *card)
1433
{
1434
        int i, count;
1435
 
1436
        for (i = 0; i < NR_HW_CH; i++) {
1437
                struct ali_state *state = card->states[i];
1438
                struct ali_channel *c = NULL;
1439
                struct dmabuf *dmabuf;
1440
                unsigned long port = card->iobase;
1441
                u16 status;
1442
                if (!state)
1443
                        continue;
1444
                if (!state->dmabuf.ready)
1445
                        continue;
1446
                dmabuf = &state->dmabuf;
1447
                if (codec_independent_spdif_locked > 0) {
1448
                        if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
1449
                                c = dmabuf->codec_spdifout_channel;
1450
                        }
1451
                } else {
1452
                        if (controller_independent_spdif_locked > 0) {
1453
                                if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1454
                                        c = dmabuf->controller_spdifout_channel;
1455
                        } else {
1456
                                if (dmabuf->enable & DAC_RUNNING) {
1457
                                        c = dmabuf->write_channel;
1458
                                } else if (dmabuf->enable & ADC_RUNNING) {
1459
                                        c = dmabuf->read_channel;
1460
                                } else
1461
                                        continue;
1462
                        }
1463
                }
1464
                port += c->port;
1465
 
1466
                status = inw(port + OFF_SR);
1467
 
1468
                if (status & DMA_INT_COMPLETE) {
1469
                        /* only wake_up() waiters if this interrupt signals
1470
                         * us being beyond a userfragsize of data open or
1471
                         * available, and ali_update_ptr() does that for
1472
                         * us
1473
                         */
1474
                        ali_update_ptr(state);
1475
                }
1476
 
1477
                if (status & DMA_INT_LVI) {
1478
                        ali_update_ptr(state);
1479
                        wake_up(&dmabuf->wait);
1480
 
1481
                        if (dmabuf->enable & DAC_RUNNING)
1482
                                count = dmabuf->count;
1483
                        else if (dmabuf->enable & ADC_RUNNING)
1484
                                count = dmabuf->dmasize - dmabuf->count;
1485
                        else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1486
                                count = dmabuf->count;
1487
                        else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1488
                                count = dmabuf->count;
1489
                        else count = 0;
1490
 
1491
                        if (count > 0) {
1492
                                if (dmabuf->enable & DAC_RUNNING)
1493
                                        outl((1 << 1), state->card->iobase + ALI_DMACR);
1494
                                else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1495
                                                outl((1 << 3), state->card->iobase + ALI_DMACR);
1496
                                else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1497
                                        outl((1 << 7), state->card->iobase + ALI_DMACR);
1498
                        } else {
1499
                                if (dmabuf->enable & DAC_RUNNING)
1500
                                        __stop_dac(state);
1501
                                if (dmabuf->enable & ADC_RUNNING)
1502
                                        __stop_adc(state);
1503
                                if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1504
                                        __stop_spdifout(state);
1505
                                if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1506
                                        __stop_spdifout(state);
1507
                                dmabuf->enable = 0;
1508
                                wake_up(&dmabuf->wait);
1509
                        }
1510
 
1511
                }
1512
                if (!(status & DMA_INT_DCH)) {
1513
                        ali_update_ptr(state);
1514
                        wake_up(&dmabuf->wait);
1515
                        if (dmabuf->enable & DAC_RUNNING)
1516
                                count = dmabuf->count;
1517
                        else if (dmabuf->enable & ADC_RUNNING)
1518
                                count = dmabuf->dmasize - dmabuf->count;
1519
                        else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1520
                                count = dmabuf->count;
1521
                        else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1522
                                count = dmabuf->count;
1523
                        else
1524
                                count = 0;
1525
 
1526
                        if (count > 0) {
1527
                                if (dmabuf->enable & DAC_RUNNING)
1528
                                        outl((1 << 1), state->card->iobase + ALI_DMACR);
1529
                                else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1530
                                        outl((1 << 3), state->card->iobase + ALI_DMACR);
1531
                                else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1532
                                        outl((1 << 7), state->card->iobase + ALI_DMACR);
1533
                        } else {
1534
                                if (dmabuf->enable & DAC_RUNNING)
1535
                                        __stop_dac(state);
1536
                                if (dmabuf->enable & ADC_RUNNING)
1537
                                        __stop_adc(state);
1538
                                if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1539
                                        __stop_spdifout(state);
1540
                                if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1541
                                        __stop_spdifout(state);
1542
                                dmabuf->enable = 0;
1543
                                wake_up(&dmabuf->wait);
1544
                        }
1545
                }
1546
                outw(status & DMA_INT_MASK, port + OFF_SR);
1547
        }
1548
}
1549
 
1550
static void ali_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1551
{
1552
        struct ali_card *card = (struct ali_card *) dev_id;
1553
        u32 status;
1554
 
1555
        spin_lock(&card->lock);
1556
        status = inl(card->iobase + ALI_INTERRUPTSR);
1557
        if (!(status & INT_MASK)) {
1558
                spin_unlock(&card->lock);
1559
                return;         /* not for us */
1560
        }
1561
 
1562
        if (status & (INT_PCMOUT | INT_PCMIN | INT_MICIN | INT_SPDIFOUT | INT_CODECSPDIFOUT))
1563
                ali_channel_interrupt(card);
1564
 
1565
        /* clear 'em */
1566
        outl(status & INT_MASK, card->iobase + ALI_INTERRUPTSR);
1567
        spin_unlock(&card->lock);
1568
}
1569
 
1570
/* in this loop, dmabuf.count signifies the amount of data that is
1571
   waiting to be copied to the user's buffer.  It is filled by the dma
1572
   machine and drained by this loop. */
1573
 
1574
static ssize_t ali_read(struct file *file, char *buffer, size_t count, loff_t * ppos)
1575
{
1576
        struct ali_state *state = (struct ali_state *) file->private_data;
1577
        struct ali_card *card = state ? state->card : 0;
1578
        struct dmabuf *dmabuf = &state->dmabuf;
1579
        ssize_t ret;
1580
        unsigned long flags;
1581
        unsigned int swptr;
1582
        int cnt;
1583
        DECLARE_WAITQUEUE(waita, current);
1584
#ifdef DEBUG2
1585
        printk("ali_audio: ali_read called, count = %d\n", count);
1586
#endif
1587
        if (ppos != &file->f_pos)
1588
                return -ESPIPE;
1589
        if (dmabuf->mapped)
1590
                return -ENXIO;
1591
        if (dmabuf->enable & DAC_RUNNING)
1592
                return -ENODEV;
1593
        if (!dmabuf->read_channel) {
1594
                dmabuf->ready = 0;
1595
                dmabuf->read_channel = card->alloc_rec_pcm_channel(card);
1596
                if (!dmabuf->read_channel) {
1597
                        return -EBUSY;
1598
                }
1599
        }
1600
        if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
1601
                return ret;
1602
        if (!access_ok(VERIFY_WRITE, buffer, count))
1603
                return -EFAULT;
1604
        ret = 0;
1605
        add_wait_queue(&dmabuf->wait, &waita);
1606
        while (count > 0) {
1607
                set_current_state(TASK_INTERRUPTIBLE);
1608
                spin_lock_irqsave(&card->lock, flags);
1609
                if (PM_SUSPENDED(card)) {
1610
                        spin_unlock_irqrestore(&card->lock, flags);
1611
                        schedule();
1612
                        if (signal_pending(current)) {
1613
                                if (!ret)
1614
                                        ret = -EAGAIN;
1615
                                break;
1616
                        }
1617
                        continue;
1618
                }
1619
                swptr = dmabuf->swptr;
1620
                cnt = ali_get_available_read_data(state);
1621
                // this is to make the copy_to_user simpler below
1622
                if (cnt > (dmabuf->dmasize - swptr))
1623
                        cnt = dmabuf->dmasize - swptr;
1624
                spin_unlock_irqrestore(&card->lock, flags);
1625
                if (cnt > count)
1626
                        cnt = count;
1627
                /* Lop off the last two bits to force the code to always
1628
                 * write in full samples.  This keeps software that sets
1629
                 * O_NONBLOCK but doesn't check the return value of the
1630
                 * write call from getting things out of state where they
1631
                 * think a full 4 byte sample was written when really only
1632
                 * a portion was, resulting in odd sound and stereo
1633
                 * hysteresis.
1634
                 */
1635
                cnt &= ~0x3;
1636
                if (cnt <= 0) {
1637
                        unsigned long tmo;
1638
                        /*
1639
                         * Don't let us deadlock.  The ADC won't start if
1640
                         * dmabuf->trigger isn't set.  A call to SETTRIGGER
1641
                         * could have turned it off after we set it to on
1642
                         * previously.
1643
                         */
1644
                        dmabuf->trigger = PCM_ENABLE_INPUT;
1645
                        /*
1646
                         * This does three things.  Updates LVI to be correct,
1647
                         * makes sure the ADC is running, and updates the
1648
                         * hwptr.
1649
                         */
1650
                        ali_update_lvi(state, 1);
1651
                        if (file->f_flags & O_NONBLOCK) {
1652
                                if (!ret)
1653
                                        ret = -EAGAIN;
1654
                                goto done;
1655
                        }
1656
                        /* Set the timeout to how long it would take to fill
1657
                         * two of our buffers.  If we haven't been woke up
1658
                         * by then, then we know something is wrong.
1659
                         */
1660
                        tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
1661
 
1662
                        /* There are two situations when sleep_on_timeout returns, one is when
1663
                           the interrupt is serviced correctly and the process is waked up by
1664
                           ISR ON TIME. Another is when timeout is expired, which means that
1665
                           either interrupt is NOT serviced correctly (pending interrupt) or it
1666
                           is TOO LATE for the process to be scheduled to run (scheduler latency)
1667
                           which results in a (potential) buffer overrun. And worse, there is
1668
                           NOTHING we can do to prevent it. */
1669
                        if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1670
                                printk(KERN_ERR
1671
                                       "ali_audio: recording schedule timeout, "
1672
                                       "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1673
                                       dmabuf->dmasize, dmabuf->fragsize,
1674
                                       dmabuf->count, dmabuf->hwptr,
1675
                                       dmabuf->swptr);
1676
                                /* a buffer overrun, we delay the recovery until next time the
1677
                                   while loop begin and we REALLY have space to record */
1678
                        }
1679
                        if (signal_pending(current)) {
1680
                                ret = ret ? ret : -ERESTARTSYS;
1681
                                goto done;
1682
                        }
1683
                        continue;
1684
                }
1685
 
1686
                if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1687
                        if (!ret)
1688
                                ret = -EFAULT;
1689
                        goto done;
1690
                }
1691
 
1692
                swptr = (swptr + cnt) % dmabuf->dmasize;
1693
                spin_lock_irqsave(&card->lock, flags);
1694
                if (PM_SUSPENDED(card)) {
1695
                        spin_unlock_irqrestore(&card->lock, flags);
1696
                        continue;
1697
                }
1698
                dmabuf->swptr = swptr;
1699
                dmabuf->count -= cnt;
1700
                spin_unlock_irqrestore(&card->lock, flags);
1701
                count -= cnt;
1702
                buffer += cnt;
1703
                ret += cnt;
1704
        }
1705
done:
1706
        ali_update_lvi(state, 1);
1707
        set_current_state(TASK_RUNNING);
1708
        remove_wait_queue(&dmabuf->wait, &waita);
1709
        return ret;
1710
}
1711
 
1712
/* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
1713
   the soundcard.  it is drained by the dma machine and filled by this loop. */
1714
static ssize_t ali_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
1715
{
1716
        struct ali_state *state = (struct ali_state *) file->private_data;
1717
        struct ali_card *card = state ? state->card : 0;
1718
        struct dmabuf *dmabuf = &state->dmabuf;
1719
        ssize_t ret;
1720
        unsigned long flags;
1721
        unsigned int swptr = 0;
1722
        int cnt, x;
1723
        DECLARE_WAITQUEUE(waita, current);
1724
#ifdef DEBUG2
1725
        printk("ali_audio: ali_write called, count = %d\n", count);
1726
#endif
1727
        if (ppos != &file->f_pos)
1728
                return -ESPIPE;
1729
        if (dmabuf->mapped)
1730
                return -ENXIO;
1731
        if (dmabuf->enable & ADC_RUNNING)
1732
                return -ENODEV;
1733
        if (codec_independent_spdif_locked > 0) {
1734
                if (!dmabuf->codec_spdifout_channel) {
1735
                        dmabuf->ready = 0;
1736
                        dmabuf->codec_spdifout_channel = card->alloc_codec_spdifout_channel(card);
1737
                        if (!dmabuf->codec_spdifout_channel)
1738
                                return -EBUSY;
1739
                }
1740
        } else {
1741
                if (controller_independent_spdif_locked > 0) {
1742
                        if (!dmabuf->controller_spdifout_channel) {
1743
                                dmabuf->ready = 0;
1744
                                dmabuf->controller_spdifout_channel = card->alloc_controller_spdifout_channel(card);
1745
                                if (!dmabuf->controller_spdifout_channel)
1746
                                        return -EBUSY;
1747
                        }
1748
                } else {
1749
                        if (!dmabuf->write_channel) {
1750
                                dmabuf->ready = 0;
1751
                                dmabuf->write_channel =
1752
                                    card->alloc_pcm_channel(card);
1753
                                if (!dmabuf->write_channel)
1754
                                        return -EBUSY;
1755
                        }
1756
                }
1757
        }
1758
 
1759
        if (codec_independent_spdif_locked > 0) {
1760
                if (!dmabuf->ready && (ret = prog_dmabuf(state, 2)))
1761
                        return ret;
1762
        } else {
1763
                if (controller_independent_spdif_locked > 0) {
1764
                        if (!dmabuf->ready && (ret = prog_dmabuf(state, 3)))
1765
                                return ret;
1766
                } else {
1767
 
1768
                        if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
1769
                                return ret;
1770
                }
1771
        }
1772
        if (!access_ok(VERIFY_READ, buffer, count))
1773
                return -EFAULT;
1774
        ret = 0;
1775
        add_wait_queue(&dmabuf->wait, &waita);
1776
        while (count > 0) {
1777
                set_current_state(TASK_INTERRUPTIBLE);
1778
                spin_lock_irqsave(&state->card->lock, flags);
1779
                if (PM_SUSPENDED(card)) {
1780
                        spin_unlock_irqrestore(&card->lock, flags);
1781
                        schedule();
1782
                        if (signal_pending(current)) {
1783
                                if (!ret)
1784
                                        ret = -EAGAIN;
1785
                                break;
1786
                        }
1787
                        continue;
1788
                }
1789
 
1790
                swptr = dmabuf->swptr;
1791
                cnt = ali_get_free_write_space(state);
1792
                /* Bound the maximum size to how much we can copy to the
1793
                 * dma buffer before we hit the end.  If we have more to
1794
                 * copy then it will get done in a second pass of this
1795
                 * loop starting from the beginning of the buffer.
1796
                 */
1797
                if (cnt > (dmabuf->dmasize - swptr))
1798
                        cnt = dmabuf->dmasize - swptr;
1799
                spin_unlock_irqrestore(&state->card->lock, flags);
1800
#ifdef DEBUG2
1801
                printk(KERN_INFO
1802
                       "ali_audio: ali_write: %d bytes available space\n",
1803
                       cnt);
1804
#endif
1805
                if (cnt > count)
1806
                        cnt = count;
1807
                /* Lop off the last two bits to force the code to always
1808
                 * write in full samples.  This keeps software that sets
1809
                 * O_NONBLOCK but doesn't check the return value of the
1810
                 * write call from getting things out of state where they
1811
                 * think a full 4 byte sample was written when really only
1812
                 * a portion was, resulting in odd sound and stereo
1813
                 * hysteresis.
1814
                 */
1815
                cnt &= ~0x3;
1816
                if (cnt <= 0) {
1817
                        unsigned long tmo;
1818
                        // There is data waiting to be played
1819
                        /*
1820
                         * Force the trigger setting since we would
1821
                         * deadlock with it set any other way
1822
                         */
1823
                        if (codec_independent_spdif_locked > 0) {
1824
                                dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1825
                                ali_update_lvi(state, 2);
1826
                        } else {
1827
                                if (controller_independent_spdif_locked > 0) {
1828
                                        dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1829
                                        ali_update_lvi(state, 3);
1830
                                } else {
1831
 
1832
                                        dmabuf->trigger = PCM_ENABLE_OUTPUT;
1833
                                        ali_update_lvi(state, 0);
1834
                                }
1835
                        }
1836
                        if (file->f_flags & O_NONBLOCK) {
1837
                                if (!ret)
1838
                                        ret = -EAGAIN;
1839
                                goto ret;
1840
                        }
1841
                        /* Not strictly correct but works */
1842
                        tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
1843
                        /* There are two situations when sleep_on_timeout returns, one is when
1844
                           the interrupt is serviced correctly and the process is waked up by
1845
                           ISR ON TIME. Another is when timeout is expired, which means that
1846
                           either interrupt is NOT serviced correctly (pending interrupt) or it
1847
                           is TOO LATE for the process to be scheduled to run (scheduler latency)
1848
                           which results in a (potential) buffer underrun. And worse, there is
1849
                           NOTHING we can do to prevent it. */
1850
 
1851
                        /* FIXME - do timeout handling here !! */
1852
 
1853
                        if (signal_pending(current)) {
1854
                                if (!ret)
1855
                                        ret = -ERESTARTSYS;
1856
                                goto ret;
1857
                        }
1858
                        continue;
1859
                }
1860
                if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
1861
                        if (!ret)
1862
                                ret = -EFAULT;
1863
                        goto ret;
1864
                }
1865
 
1866
                swptr = (swptr + cnt) % dmabuf->dmasize;
1867
                spin_lock_irqsave(&state->card->lock, flags);
1868
                if (PM_SUSPENDED(card)) {
1869
                        spin_unlock_irqrestore(&card->lock, flags);
1870
                        continue;
1871
                }
1872
 
1873
                dmabuf->swptr = swptr;
1874
                dmabuf->count += cnt;
1875
                count -= cnt;
1876
                buffer += cnt;
1877
                ret += cnt;
1878
                spin_unlock_irqrestore(&state->card->lock, flags);
1879
        }
1880
        if (swptr % dmabuf->fragsize) {
1881
                x = dmabuf->fragsize - (swptr % dmabuf->fragsize);
1882
                memset(dmabuf->rawbuf + swptr, '\0', x);
1883
        }
1884
ret:
1885
        if (codec_independent_spdif_locked > 0) {
1886
                ali_update_lvi(state, 2);
1887
        } else {
1888
                if (controller_independent_spdif_locked > 0) {
1889
                        ali_update_lvi(state, 3);
1890
                } else {
1891
                        ali_update_lvi(state, 0);
1892
                }
1893
        }
1894
        set_current_state(TASK_RUNNING);
1895
        remove_wait_queue(&dmabuf->wait, &waita);
1896
        return ret;
1897
}
1898
 
1899
/* No kernel lock - we have our own spinlock */
1900
static unsigned int ali_poll(struct file *file, struct poll_table_struct *wait)
1901
{
1902
        struct ali_state *state = (struct ali_state *) file->private_data;
1903
        struct dmabuf *dmabuf = &state->dmabuf;
1904
        unsigned long flags;
1905
        unsigned int mask = 0;
1906
        if (!dmabuf->ready)
1907
                return 0;
1908
        poll_wait(file, &dmabuf->wait, wait);
1909
        spin_lock_irqsave(&state->card->lock, flags);
1910
        ali_update_ptr(state);
1911
        if (file->f_mode & FMODE_READ && dmabuf->enable & ADC_RUNNING) {
1912
                if (dmabuf->count >= (signed) dmabuf->fragsize)
1913
                        mask |= POLLIN | POLLRDNORM;
1914
        }
1915
        if (file->f_mode & FMODE_WRITE  && (dmabuf->enable & (DAC_RUNNING|CODEC_SPDIFOUT_RUNNING|CONTROLLER_SPDIFOUT_RUNNING))) {
1916
                if ((signed) dmabuf->dmasize >= dmabuf->count + (signed) dmabuf->fragsize)
1917
                        mask |= POLLOUT | POLLWRNORM;
1918
        }
1919
        spin_unlock_irqrestore(&state->card->lock, flags);
1920
        return mask;
1921
}
1922
 
1923
static int ali_mmap(struct file *file, struct vm_area_struct *vma)
1924
{
1925
        struct ali_state *state = (struct ali_state *) file->private_data;
1926
        struct dmabuf *dmabuf = &state->dmabuf;
1927
        int ret = -EINVAL;
1928
        unsigned long size;
1929
        lock_kernel();
1930
        if (vma->vm_flags & VM_WRITE) {
1931
                if (!dmabuf->write_channel && (dmabuf->write_channel = state->card->alloc_pcm_channel(state->card)) == NULL) {
1932
                        ret = -EBUSY;
1933
                        goto out;
1934
                }
1935
        }
1936
        if (vma->vm_flags & VM_READ) {
1937
                if (!dmabuf->read_channel && (dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card)) == NULL) {
1938
                        ret = -EBUSY;
1939
                        goto out;
1940
                }
1941
        }
1942
        if ((ret = prog_dmabuf(state, 0)) != 0)
1943
                goto out;
1944
        ret = -EINVAL;
1945
        if (vma->vm_pgoff != 0)
1946
                goto out;
1947
        size = vma->vm_end - vma->vm_start;
1948
        if (size > (PAGE_SIZE << dmabuf->buforder))
1949
                goto out;
1950
        ret = -EAGAIN;
1951
        if (remap_page_range(vma->vm_start, virt_to_phys(dmabuf->rawbuf), size, vma->vm_page_prot))
1952
                goto out;
1953
        dmabuf->mapped = 1;
1954
        dmabuf->trigger = 0;
1955
        ret = 0;
1956
out:
1957
        unlock_kernel();
1958
        return ret;
1959
}
1960
 
1961
static int ali_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1962
{
1963
        struct ali_state *state = (struct ali_state *) file->private_data;
1964
        struct ali_channel *c = NULL;
1965
        struct dmabuf *dmabuf = &state->dmabuf;
1966
        unsigned long flags;
1967
        audio_buf_info abinfo;
1968
        count_info cinfo;
1969
        unsigned int i_scr;
1970
        int val = 0, ret;
1971
        struct ac97_codec *codec = state->card->ac97_codec[0];
1972
#ifdef DEBUG
1973
        printk("ali_audio: ali_ioctl, arg=0x%x, cmd=",
1974
               arg ? *(int *) arg : 0);
1975
#endif
1976
        switch (cmd) {
1977
        case OSS_GETVERSION:
1978
#ifdef DEBUG
1979
                printk("OSS_GETVERSION\n");
1980
#endif
1981
                return put_user(SOUND_VERSION, (int *) arg);
1982
        case SNDCTL_DSP_RESET:
1983
#ifdef DEBUG
1984
                printk("SNDCTL_DSP_RESET\n");
1985
#endif
1986
                spin_lock_irqsave(&state->card->lock, flags);
1987
                if (dmabuf->enable == DAC_RUNNING) {
1988
                        c = dmabuf->write_channel;
1989
                        __stop_dac(state);
1990
                }
1991
                if (dmabuf->enable == ADC_RUNNING) {
1992
                        c = dmabuf->read_channel;
1993
                        __stop_adc(state);
1994
                }
1995
                if (dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
1996
                        c = dmabuf->codec_spdifout_channel;
1997
                        __stop_spdifout(state);
1998
                }
1999
                if (dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
2000
                        c = dmabuf->controller_spdifout_channel;
2001
                        __stop_spdifout(state);
2002
                }
2003
                if (c != NULL) {
2004
                        outb(2, state->card->iobase + c->port + OFF_CR);        /* reset DMA machine */
2005
                        outl(virt_to_bus(&c->sg[0]), state->card->iobase + c->port + OFF_BDBAR);
2006
                        outb(0, state->card->iobase + c->port + OFF_CIV);
2007
                        outb(0, state->card->iobase + c->port + OFF_LVI);
2008
                }
2009
 
2010
                spin_unlock_irqrestore(&state->card->lock, flags);
2011
                synchronize_irq();
2012
                dmabuf->ready = 0;
2013
                dmabuf->swptr = dmabuf->hwptr = 0;
2014
                dmabuf->count = dmabuf->total_bytes = 0;
2015
                return 0;
2016
        case SNDCTL_DSP_SYNC:
2017
#ifdef DEBUG
2018
                printk("SNDCTL_DSP_SYNC\n");
2019
#endif
2020
                if (codec_independent_spdif_locked > 0) {
2021
                        if (dmabuf->enable != CODEC_SPDIFOUT_RUNNING
2022
                            || file->f_flags & O_NONBLOCK)
2023
                                return 0;
2024
                        if ((val = drain_spdifout(state, 1)))
2025
                                return val;
2026
                } else {
2027
                        if (controller_independent_spdif_locked > 0) {
2028
                                if (dmabuf->enable !=
2029
                                    CONTROLLER_SPDIFOUT_RUNNING
2030
                                    || file->f_flags & O_NONBLOCK)
2031
                                        return 0;
2032
                                if ((val = drain_spdifout(state, 1)))
2033
                                        return val;
2034
                        } else {
2035
                                if (dmabuf->enable != DAC_RUNNING
2036
                                    || file->f_flags & O_NONBLOCK)
2037
                                        return 0;
2038
                                if ((val = drain_dac(state, 1)))
2039
                                        return val;
2040
                        }
2041
                }
2042
                dmabuf->total_bytes = 0;
2043
                return 0;
2044
        case SNDCTL_DSP_SPEED:  /* set smaple rate */
2045
#ifdef DEBUG
2046
                printk("SNDCTL_DSP_SPEED\n");
2047
#endif
2048
                if (get_user(val, (int *) arg))
2049
                        return -EFAULT;
2050
                if (val >= 0) {
2051
                        if (file->f_mode & FMODE_WRITE) {
2052
                                if ((state->card->ac97_status & SPDIF_ON)) {    /* S/PDIF Enabled */
2053
                                        /* RELTEK ALC650 only support 48000, need to check that */
2054
                                        if (ali_valid_spdif_rate(codec, val)) {
2055
                                                if (codec_independent_spdif_locked > 0) {
2056
                                                        ali_set_spdif_output(state, -1, 0);
2057
                                                        stop_spdifout(state);
2058
                                                        dmabuf->ready = 0;
2059
                                                        /* I add test codec independent spdif out */
2060
                                                        spin_lock_irqsave(&state->card->lock, flags);
2061
                                                        ali_set_codecspdifout_rate(state, val); // I modified
2062
                                                        spin_unlock_irqrestore(&state->card->lock, flags);
2063
                                                        /* Set S/PDIF transmitter rate. */
2064
                                                        i_scr = inl(state->card->iobase + ALI_SCR);
2065
                                                        if ((i_scr & 0x00300000) == 0x00100000) {
2066
                                                                ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2067
                                                        } else {
2068
                                                                if ((i_scr&0x00300000)  == 0x00200000)
2069
                                                                {
2070
                                                                        ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
2071
                                                                } else {
2072
                                                                        if ((i_scr & 0x00300000) == 0x00300000) {
2073
                                                                                ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
2074
                                                                        } else {
2075
                                                                                ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2076
                                                                        }
2077
                                                                }
2078
                                                        }
2079
 
2080
                                                        if (!(state->card->ac97_status & SPDIF_ON)) {
2081
                                                                val = dmabuf->rate;
2082
                                                        }
2083
                                                } else {
2084
                                                        if (controller_independent_spdif_locked > 0)
2085
                                                        {
2086
                                                                stop_spdifout(state);
2087
                                                                dmabuf->ready = 0;
2088
                                                                spin_lock_irqsave(&state->card->lock, flags);
2089
                                                                ali_set_spdifout_rate(state, controller_independent_spdif_locked);
2090
                                                                spin_unlock_irqrestore(&state->card->lock, flags);
2091
                                                        } else {
2092
                                                                /* Set DAC rate */
2093
                                                                ali_set_spdif_output(state, -1, 0);
2094
                                                                stop_dac(state);
2095
                                                                dmabuf->ready = 0;
2096
                                                                spin_lock_irqsave(&state->card->lock, flags);
2097
                                                                ali_set_dac_rate(state, val);
2098
                                                                spin_unlock_irqrestore(&state->card->lock, flags);
2099
                                                                /* Set S/PDIF transmitter rate. */
2100
                                                                ali_set_spdif_output(state, AC97_EA_SPSA_3_4, val);
2101
                                                                if (!(state->card->ac97_status & SPDIF_ON))
2102
                                                                {
2103
                                                                        val = dmabuf->rate;
2104
                                                                }
2105
                                                        }
2106
                                                }
2107
                                        } else {        /* Not a valid rate for S/PDIF, ignore it */
2108
                                                val = dmabuf->rate;
2109
                                        }
2110
                                } else {
2111
                                        stop_dac(state);
2112
                                        dmabuf->ready = 0;
2113
                                        spin_lock_irqsave(&state->card->lock, flags);
2114
                                        ali_set_dac_rate(state, val);
2115
                                        spin_unlock_irqrestore(&state->card->lock, flags);
2116
                                }
2117
                        }
2118
                        if (file->f_mode & FMODE_READ) {
2119
                                stop_adc(state);
2120
                                dmabuf->ready = 0;
2121
                                spin_lock_irqsave(&state->card->lock, flags);
2122
                                ali_set_adc_rate(state, val);
2123
                                spin_unlock_irqrestore(&state->card->lock, flags);
2124
                        }
2125
                }
2126
                return put_user(dmabuf->rate, (int *) arg);
2127
        case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2128
#ifdef DEBUG
2129
                printk("SNDCTL_DSP_STEREO\n");
2130
#endif
2131
                if (dmabuf->enable & DAC_RUNNING) {
2132
                        stop_dac(state);
2133
                }
2134
                if (dmabuf->enable & ADC_RUNNING) {
2135
                        stop_adc(state);
2136
                }
2137
                if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
2138
                        stop_spdifout(state);
2139
                }
2140
                if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING) {
2141
                        stop_spdifout(state);
2142
                }
2143
                return put_user(1, (int *) arg);
2144
        case SNDCTL_DSP_GETBLKSIZE:
2145
                if (file->f_mode & FMODE_WRITE) {
2146
                        if (codec_independent_spdif_locked > 0) {
2147
                                if (!dmabuf->ready && (val = prog_dmabuf(state, 2)))
2148
                                        return val;
2149
                        } else {
2150
                                if (controller_independent_spdif_locked > 0) {
2151
                                        if (!dmabuf->ready && (val = prog_dmabuf(state, 3)))
2152
                                                return val;
2153
                                } else {
2154
                                        if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
2155
                                                return val;
2156
                                }
2157
                        }
2158
                }
2159
 
2160
                if (file->f_mode & FMODE_READ) {
2161
                        if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
2162
                                return val;
2163
                }
2164
#ifdef DEBUG
2165
                printk("SNDCTL_DSP_GETBLKSIZE %d\n", dmabuf->userfragsize);
2166
#endif
2167
                return put_user(dmabuf->userfragsize, (int *) arg);
2168
        case SNDCTL_DSP_GETFMTS:        /* Returns a mask of supported sample format */
2169
#ifdef DEBUG
2170
                printk("SNDCTL_DSP_GETFMTS\n");
2171
#endif
2172
                return put_user(AFMT_S16_LE, (int *) arg);
2173
        case SNDCTL_DSP_SETFMT: /* Select sample format */
2174
#ifdef DEBUG
2175
                printk("SNDCTL_DSP_SETFMT\n");
2176
#endif
2177
                return put_user(AFMT_S16_LE, (int *) arg);
2178
        case SNDCTL_DSP_CHANNELS:       // add support 4,6 channel 
2179
#ifdef DEBUG
2180
                printk("SNDCTL_DSP_CHANNELS\n");
2181
#endif
2182
                if (get_user(val, (int *) arg))
2183
                        return -EFAULT;
2184
                if (val > 0) {
2185
                        if (dmabuf->enable & DAC_RUNNING) {
2186
                                stop_dac(state);
2187
                        }
2188
                        if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
2189
                                stop_spdifout(state);
2190
                        }
2191
                        if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING) {
2192
                                stop_spdifout(state);
2193
                        }
2194
                        if (dmabuf->enable & ADC_RUNNING) {
2195
                                stop_adc(state);
2196
                        }
2197
                } else {
2198
                        return put_user(state->card->channels, (int *) arg);
2199
                }
2200
 
2201
                i_scr = inl(state->card->iobase + ALI_SCR);
2202
                /* Current # of channels enabled */
2203
                if (i_scr & 0x00000100)
2204
                        ret = 4;
2205
                else if (i_scr & 0x00000200)
2206
                        ret = 6;
2207
                else
2208
                        ret = 2;
2209
                switch (val) {
2210
                case 2: /* 2 channels is always supported */
2211
                        if (codec_independent_spdif_locked > 0) {
2212
                                outl(((i_scr & 0xfffffcff) | 0x00100000), (state->card->iobase + ALI_SCR));
2213
                        } else
2214
                                outl((i_scr & 0xfffffcff), (state->card->iobase + ALI_SCR));
2215
                        /* Do we need to change mixer settings????  */
2216
                        break;
2217
                case 4: /* Supported on some chipsets, better check first */
2218
                        if (codec_independent_spdif_locked > 0) {
2219
                                outl(((i_scr & 0xfffffcff) | 0x00000100 | 0x00200000), (state->card->iobase + ALI_SCR));
2220
                        } else
2221
                                outl(((i_scr & 0xfffffcff) | 0x00000100), (state->card->iobase + ALI_SCR));
2222
                        break;
2223
                case 6: /* Supported on some chipsets, better check first */
2224
                        if (codec_independent_spdif_locked > 0) {
2225
                                outl(((i_scr & 0xfffffcff) | 0x00000200 | 0x00008000 | 0x00300000), (state->card->iobase + ALI_SCR));
2226
                        } else
2227
                                outl(((i_scr & 0xfffffcff) | 0x00000200 | 0x00008000), (state->card->iobase + ALI_SCR));
2228
                        break;
2229
                default:        /* nothing else is ever supported by the chipset */
2230
                        val = ret;
2231
                        break;
2232
                }
2233
                return put_user(val, (int *) arg);
2234
        case SNDCTL_DSP_POST:   /* the user has sent all data and is notifying us */
2235
                /* we update the swptr to the end of the last sg segment then return */
2236
#ifdef DEBUG
2237
                printk("SNDCTL_DSP_POST\n");
2238
#endif
2239
                if (codec_independent_spdif_locked > 0) {
2240
                        if (!dmabuf->ready || (dmabuf->enable != CODEC_SPDIFOUT_RUNNING))
2241
                                return 0;
2242
                } else {
2243
                        if (controller_independent_spdif_locked > 0) {
2244
                                if (!dmabuf->ready || (dmabuf->enable != CONTROLLER_SPDIFOUT_RUNNING))
2245
                                        return 0;
2246
                        } else {
2247
                                if (!dmabuf->ready || (dmabuf->enable != DAC_RUNNING))
2248
                                        return 0;
2249
                        }
2250
                }
2251
                if ((dmabuf->swptr % dmabuf->fragsize) != 0) {
2252
                        val = dmabuf->fragsize - (dmabuf->swptr % dmabuf->fragsize);
2253
                        dmabuf->swptr += val;
2254
                        dmabuf->count += val;
2255
                }
2256
                return 0;
2257
        case SNDCTL_DSP_SUBDIVIDE:
2258
                if (dmabuf->subdivision)
2259
                        return -EINVAL;
2260
                if (get_user(val, (int *) arg))
2261
                        return -EFAULT;
2262
                if (val != 1 && val != 2 && val != 4)
2263
                        return -EINVAL;
2264
#ifdef DEBUG
2265
                printk("SNDCTL_DSP_SUBDIVIDE %d\n", val);
2266
#endif
2267
                dmabuf->subdivision = val;
2268
                dmabuf->ready = 0;
2269
                return 0;
2270
        case SNDCTL_DSP_SETFRAGMENT:
2271
                if (get_user(val, (int *) arg))
2272
                        return -EFAULT;
2273
                dmabuf->ossfragsize = 1 << (val & 0xffff);
2274
                dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2275
                if (!dmabuf->ossfragsize || !dmabuf->ossmaxfrags)
2276
                        return -EINVAL;
2277
                /*
2278
                 * Bound the frag size into our allowed range of 256 - 4096
2279
                 */
2280
                if (dmabuf->ossfragsize < 256)
2281
                        dmabuf->ossfragsize = 256;
2282
                else if (dmabuf->ossfragsize > 4096)
2283
                        dmabuf->ossfragsize = 4096;
2284
                /*
2285
                 * The numfrags could be something reasonable, or it could
2286
                 * be 0xffff meaning "Give me as much as possible".  So,
2287
                 * we check the numfrags * fragsize doesn't exceed our
2288
                 * 64k buffer limit, nor is it less than our 8k minimum.
2289
                 * If it fails either one of these checks, then adjust the
2290
                 * number of fragments, not the size of them.  It's OK if
2291
                 * our number of fragments doesn't equal 32 or anything
2292
                 * like our hardware based number now since we are using
2293
                 * a different frag count for the hardware.  Before we get
2294
                 * into this though, bound the maxfrags to avoid overflow
2295
                 * issues.  A reasonable bound would be 64k / 256 since our
2296
                 * maximum buffer size is 64k and our minimum frag size is
2297
                 * 256.  On the other end, our minimum buffer size is 8k and
2298
                 * our maximum frag size is 4k, so the lower bound should
2299
                 * be 2.
2300
                 */
2301
                if (dmabuf->ossmaxfrags > 256)
2302
                        dmabuf->ossmaxfrags = 256;
2303
                else if (dmabuf->ossmaxfrags < 2)
2304
                        dmabuf->ossmaxfrags = 2;
2305
                val = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
2306
                while (val < 8192) {
2307
                        val <<= 1;
2308
                        dmabuf->ossmaxfrags <<= 1;
2309
                }
2310
                while (val > 65536) {
2311
                        val >>= 1;
2312
                        dmabuf->ossmaxfrags >>= 1;
2313
                }
2314
                dmabuf->ready = 0;
2315
#ifdef DEBUG
2316
                printk("SNDCTL_DSP_SETFRAGMENT 0x%x, %d, %d\n", val,
2317
                       dmabuf->ossfragsize, dmabuf->ossmaxfrags);
2318
#endif
2319
                return 0;
2320
        case SNDCTL_DSP_GETOSPACE:
2321
                if (!(file->f_mode & FMODE_WRITE))
2322
                        return -EINVAL;
2323
                if (codec_independent_spdif_locked > 0) {
2324
                        if (!dmabuf->ready && (val = prog_dmabuf(state, 2)) != 0)
2325
                                return val;
2326
                } else {
2327
                        if (controller_independent_spdif_locked > 0) {
2328
                                if (!dmabuf->ready && (val = prog_dmabuf(state, 3)) != 0)
2329
                                        return val;
2330
                        } else {
2331
                                if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2332
                                        return val;
2333
                        }
2334
                }
2335
                spin_lock_irqsave(&state->card->lock, flags);
2336
                ali_update_ptr(state);
2337
                abinfo.fragsize = dmabuf->userfragsize;
2338
                abinfo.fragstotal = dmabuf->userfrags;
2339
                if (dmabuf->mapped)
2340
                        abinfo.bytes = dmabuf->dmasize;
2341
                else
2342
                        abinfo.bytes = ali_get_free_write_space(state);
2343
                abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
2344
                spin_unlock_irqrestore(&state->card->lock, flags);
2345
#if defined(DEBUG) || defined(DEBUG_MMAP)
2346
                printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %d\n",
2347
                       abinfo.bytes, abinfo.fragsize, abinfo.fragments,
2348
                       abinfo.fragstotal);
2349
#endif
2350
                return copy_to_user((void *) arg, &abinfo,
2351
                                    sizeof(abinfo)) ? -EFAULT : 0;
2352
        case SNDCTL_DSP_GETOPTR:
2353
                if (!(file->f_mode & FMODE_WRITE))
2354
                        return -EINVAL;
2355
                if (codec_independent_spdif_locked > 0) {
2356
                        if (!dmabuf->ready && (val = prog_dmabuf(state, 2)) != 0)
2357
                                return val;
2358
                } else {
2359
                        if (controller_independent_spdif_locked > 0) {
2360
                                if (!dmabuf->ready && (val = prog_dmabuf(state, 3)) != 0)
2361
                                        return val;
2362
                        } else {
2363
                                if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2364
                                        return val;
2365
                        }
2366
                }
2367
                spin_lock_irqsave(&state->card->lock, flags);
2368
                val = ali_get_free_write_space(state);
2369
                cinfo.bytes = dmabuf->total_bytes;
2370
                cinfo.ptr = dmabuf->hwptr;
2371
                cinfo.blocks = val / dmabuf->userfragsize;
2372
                if (codec_independent_spdif_locked > 0) {
2373
                        if (dmabuf->mapped && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
2374
                                dmabuf->count += val;
2375
                                dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2376
                                __ali_update_lvi(state, 2);
2377
                        }
2378
                } else {
2379
                        if (controller_independent_spdif_locked > 0) {
2380
                                if (dmabuf->mapped && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
2381
                                        dmabuf->count += val;
2382
                                        dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2383
                                        __ali_update_lvi(state, 3);
2384
                                }
2385
                        } else {
2386
                                if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
2387
                                        dmabuf->count += val;
2388
                                        dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2389
                                        __ali_update_lvi(state, 0);
2390
                                }
2391
                        }
2392
                }
2393
                spin_unlock_irqrestore(&state->card->lock, flags);
2394
#if defined(DEBUG) || defined(DEBUG_MMAP)
2395
                printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %d\n", cinfo.bytes,
2396
                       cinfo.blocks, cinfo.ptr, dmabuf->count);
2397
#endif
2398
                return copy_to_user((void *) arg, &cinfo, sizeof(cinfo));
2399
        case SNDCTL_DSP_GETISPACE:
2400
                if (!(file->f_mode & FMODE_READ))
2401
                        return -EINVAL;
2402
                if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
2403
                        return val;
2404
                spin_lock_irqsave(&state->card->lock, flags);
2405
                abinfo.bytes = ali_get_available_read_data(state);
2406
                abinfo.fragsize = dmabuf->userfragsize;
2407
                abinfo.fragstotal = dmabuf->userfrags;
2408
                abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
2409
                spin_unlock_irqrestore(&state->card->lock, flags);
2410
#if defined(DEBUG) || defined(DEBUG_MMAP)
2411
                printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %d\n",
2412
                       abinfo.bytes, abinfo.fragsize, abinfo.fragments,
2413
                       abinfo.fragstotal);
2414
#endif
2415
                return copy_to_user((void *) arg, &abinfo,
2416
                                    sizeof(abinfo)) ? -EFAULT : 0;
2417
        case SNDCTL_DSP_GETIPTR:
2418
                if (!(file->f_mode & FMODE_READ))
2419
                        return -EINVAL;
2420
                if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2421
                        return val;
2422
                spin_lock_irqsave(&state->card->lock, flags);
2423
                val = ali_get_available_read_data(state);
2424
                cinfo.bytes = dmabuf->total_bytes;
2425
                cinfo.blocks = val / dmabuf->userfragsize;
2426
                cinfo.ptr = dmabuf->hwptr;
2427
                if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
2428
                        dmabuf->count -= val;
2429
                        dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2430
                        __ali_update_lvi(state, 1);
2431
                }
2432
                spin_unlock_irqrestore(&state->card->lock, flags);
2433
#if defined(DEBUG) || defined(DEBUG_MMAP)
2434
                printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %d\n", cinfo.bytes,
2435
                       cinfo.blocks, cinfo.ptr, dmabuf->count);
2436
#endif
2437
                return copy_to_user((void *) arg, &cinfo, sizeof(cinfo));
2438
        case SNDCTL_DSP_NONBLOCK:
2439
#ifdef DEBUG
2440
                printk("SNDCTL_DSP_NONBLOCK\n");
2441
#endif
2442
                file->f_flags |= O_NONBLOCK;
2443
                return 0;
2444
        case SNDCTL_DSP_GETCAPS:
2445
#ifdef DEBUG
2446
                printk("SNDCTL_DSP_GETCAPS\n");
2447
#endif
2448
                return put_user(DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2449
                                DSP_CAP_MMAP | DSP_CAP_BIND, (int *) arg);
2450
        case SNDCTL_DSP_GETTRIGGER:
2451
                val = 0;
2452
#ifdef DEBUG
2453
                printk("SNDCTL_DSP_GETTRIGGER 0x%x\n", dmabuf->trigger);
2454
#endif
2455
                return put_user(dmabuf->trigger, (int *) arg);
2456
        case SNDCTL_DSP_SETTRIGGER:
2457
                if (get_user(val, (int *) arg))
2458
                        return -EFAULT;
2459
#if defined(DEBUG) || defined(DEBUG_MMAP)
2460
                printk("SNDCTL_DSP_SETTRIGGER 0x%x\n", val);
2461
#endif
2462
                if (!(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
2463
                        stop_adc(state);
2464
                }
2465
                if (!(val & PCM_ENABLE_OUTPUT) && dmabuf->enable == DAC_RUNNING) {
2466
                        stop_dac(state);
2467
                }
2468
                if (!(val & SPDIF_ENABLE_OUTPUT) && dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
2469
                        stop_spdifout(state);
2470
                }
2471
                if (!(val & SPDIF_ENABLE_OUTPUT) && dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
2472
                        stop_spdifout(state);
2473
                }
2474
                dmabuf->trigger = val;
2475
                if (val & PCM_ENABLE_OUTPUT && !(dmabuf->enable & DAC_RUNNING)) {
2476
                        if (!dmabuf->write_channel) {
2477
                                dmabuf->ready = 0;
2478
                                dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
2479
                                if (!dmabuf->write_channel)
2480
                                        return -EBUSY;
2481
                        }
2482
                        if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
2483
                                return ret;
2484
                        if (dmabuf->mapped) {
2485
                                spin_lock_irqsave(&state->card->lock, flags);
2486
                                ali_update_ptr(state);
2487
                                dmabuf->count = 0;
2488
                                dmabuf->swptr = dmabuf->hwptr;
2489
                                dmabuf->count = ali_get_free_write_space(state);
2490
                                dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2491
                                __ali_update_lvi(state, 0);
2492
                                spin_unlock_irqrestore(&state->card->lock,
2493
                                                       flags);
2494
                        } else
2495
                                start_dac(state);
2496
                }
2497
                if (val & SPDIF_ENABLE_OUTPUT && !(dmabuf->enable & CODEC_SPDIFOUT_RUNNING)) {
2498
                        if (!dmabuf->codec_spdifout_channel) {
2499
                                dmabuf->ready = 0;
2500
                                dmabuf->codec_spdifout_channel = state->card->alloc_codec_spdifout_channel(state->card);
2501
                                if (!dmabuf->codec_spdifout_channel)
2502
                                        return -EBUSY;
2503
                        }
2504
                        if (!dmabuf->ready && (ret = prog_dmabuf(state, 2)))
2505
                                return ret;
2506
                        if (dmabuf->mapped) {
2507
                                spin_lock_irqsave(&state->card->lock, flags);
2508
                                ali_update_ptr(state);
2509
                                dmabuf->count = 0;
2510
                                dmabuf->swptr = dmabuf->hwptr;
2511
                                dmabuf->count = ali_get_free_write_space(state);
2512
                                dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2513
                                __ali_update_lvi(state, 2);
2514
                                spin_unlock_irqrestore(&state->card->lock,
2515
                                                       flags);
2516
                        } else
2517
                                start_spdifout(state);
2518
                }
2519
                if (val & SPDIF_ENABLE_OUTPUT && !(dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)) {
2520
                        if (!dmabuf->controller_spdifout_channel) {
2521
                                dmabuf->ready = 0;
2522
                                dmabuf->controller_spdifout_channel = state->card->alloc_controller_spdifout_channel(state->card);
2523
                                if (!dmabuf->controller_spdifout_channel)
2524
                                        return -EBUSY;
2525
                        }
2526
                        if (!dmabuf->ready && (ret = prog_dmabuf(state, 3)))
2527
                                return ret;
2528
                        if (dmabuf->mapped) {
2529
                                spin_lock_irqsave(&state->card->lock, flags);
2530
                                ali_update_ptr(state);
2531
                                dmabuf->count = 0;
2532
                                dmabuf->swptr = dmabuf->hwptr;
2533
                                dmabuf->count = ali_get_free_write_space(state);
2534
                                dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2535
                                __ali_update_lvi(state, 3);
2536
                                spin_unlock_irqrestore(&state->card->lock, flags);
2537
                        } else
2538
                                start_spdifout(state);
2539
                }
2540
                if (val & PCM_ENABLE_INPUT && !(dmabuf->enable & ADC_RUNNING)) {
2541
                        if (!dmabuf->read_channel) {
2542
                                dmabuf->ready = 0;
2543
                                dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
2544
                                if (!dmabuf->read_channel)
2545
                                        return -EBUSY;
2546
                        }
2547
                        if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
2548
                                return ret;
2549
                        if (dmabuf->mapped) {
2550
                                spin_lock_irqsave(&state->card->lock,
2551
                                                  flags);
2552
                                ali_update_ptr(state);
2553
                                dmabuf->swptr = dmabuf->hwptr;
2554
                                dmabuf->count = 0;
2555
                                spin_unlock_irqrestore(&state->card->lock, flags);
2556
                        }
2557
                        ali_update_lvi(state, 1);
2558
                        start_adc(state);
2559
                }
2560
                return 0;
2561
        case SNDCTL_DSP_SETDUPLEX:
2562
#ifdef DEBUG
2563
                printk("SNDCTL_DSP_SETDUPLEX\n");
2564
#endif
2565
                return -EINVAL;
2566
        case SNDCTL_DSP_GETODELAY:
2567
                if (!(file->f_mode & FMODE_WRITE))
2568
                        return -EINVAL;
2569
                spin_lock_irqsave(&state->card->lock, flags);
2570
                ali_update_ptr(state);
2571
                val = dmabuf->count;
2572
                spin_unlock_irqrestore(&state->card->lock, flags);
2573
#ifdef DEBUG
2574
                printk("SNDCTL_DSP_GETODELAY %d\n", dmabuf->count);
2575
#endif
2576
                return put_user(val, (int *) arg);
2577
        case SOUND_PCM_READ_RATE:
2578
#ifdef DEBUG
2579
                printk("SOUND_PCM_READ_RATE %d\n", dmabuf->rate);
2580
#endif
2581
                return put_user(dmabuf->rate, (int *) arg);
2582
        case SOUND_PCM_READ_CHANNELS:
2583
#ifdef DEBUG
2584
                printk("SOUND_PCM_READ_CHANNELS\n");
2585
#endif
2586
                return put_user(2, (int *) arg);
2587
        case SOUND_PCM_READ_BITS:
2588
#ifdef DEBUG
2589
                printk("SOUND_PCM_READ_BITS\n");
2590
#endif
2591
                return put_user(AFMT_S16_LE, (int *) arg);
2592
        case SNDCTL_DSP_SETSPDIF:       /* Set S/PDIF Control register */
2593
#ifdef DEBUG
2594
                printk("SNDCTL_DSP_SETSPDIF\n");
2595
#endif
2596
                if (get_user(val, (int *) arg))
2597
                        return -EFAULT;
2598
                /* Check to make sure the codec supports S/PDIF transmitter */
2599
                if ((state->card->ac97_features & 4)) {
2600
                        /* mask out the transmitter speed bits so the user can't set them */
2601
                        val &= ~0x3000;
2602
                        /* Add the current transmitter speed bits to the passed value */
2603
                        ret = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
2604
                        val |= (ret & 0x3000);
2605
                        ali_ac97_set(codec, AC97_SPDIF_CONTROL, val);
2606
                        if (ali_ac97_get(codec, AC97_SPDIF_CONTROL) != val) {
2607
                                printk(KERN_ERR "ali_audio: Unable to set S/PDIF configuration to 0x%04x.\n", val);
2608
                                return -EFAULT;
2609
                        }
2610
                }
2611
#ifdef DEBUG
2612
                else
2613
                        printk(KERN_WARNING "ali_audio: S/PDIF transmitter not avalible.\n");
2614
#endif
2615
                return put_user(val, (int *) arg);
2616
        case SNDCTL_DSP_GETSPDIF:       /* Get S/PDIF Control register */
2617
#ifdef DEBUG
2618
                printk("SNDCTL_DSP_GETSPDIF\n");
2619
#endif
2620
                if (get_user(val, (int *) arg))
2621
                        return -EFAULT;
2622
                /* Check to make sure the codec supports S/PDIF transmitter */
2623
                if (!(state->card->ac97_features & 4)) {
2624
#ifdef DEBUG
2625
                        printk(KERN_WARNING "ali_audio: S/PDIF transmitter not avalible.\n");
2626
#endif
2627
                        val = 0;
2628
                } else {
2629
                        val = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
2630
                }
2631
 
2632
                return put_user(val, (int *) arg);
2633
//end add support spdif out
2634
//add support 4,6 channel
2635
        case SNDCTL_DSP_GETCHANNELMASK:
2636
#ifdef DEBUG
2637
                printk("SNDCTL_DSP_GETCHANNELMASK\n");
2638
#endif
2639
                if (get_user(val, (int *) arg))
2640
                        return -EFAULT;
2641
                /* Based on AC'97 DAC support, not ICH hardware */
2642
                val = DSP_BIND_FRONT;
2643
                if (state->card->ac97_features & 0x0004)
2644
                        val |= DSP_BIND_SPDIF;
2645
                if (state->card->ac97_features & 0x0080)
2646
                        val |= DSP_BIND_SURR;
2647
                if (state->card->ac97_features & 0x0140)
2648
                        val |= DSP_BIND_CENTER_LFE;
2649
                return put_user(val, (int *) arg);
2650
        case SNDCTL_DSP_BIND_CHANNEL:
2651
#ifdef DEBUG
2652
                printk("SNDCTL_DSP_BIND_CHANNEL\n");
2653
#endif
2654
                if (get_user(val, (int *) arg))
2655
                        return -EFAULT;
2656
                if (val == DSP_BIND_QUERY) {
2657
                        val = DSP_BIND_FRONT;   /* Always report this as being enabled */
2658
                        if (state->card->ac97_status & SPDIF_ON)
2659
                                val |= DSP_BIND_SPDIF;
2660
                        else {
2661
                                if (state->card->ac97_status & SURR_ON)
2662
                                        val |= DSP_BIND_SURR;
2663
                                if (state->card->
2664
                                    ac97_status & CENTER_LFE_ON)
2665
                                        val |= DSP_BIND_CENTER_LFE;
2666
                        }
2667
                } else {        /* Not a query, set it */
2668
                        if (!(file->f_mode & FMODE_WRITE))
2669
                                return -EINVAL;
2670
                        if (dmabuf->enable == DAC_RUNNING) {
2671
                                stop_dac(state);
2672
                        }
2673
                        if (val & DSP_BIND_SPDIF) {     /* Turn on SPDIF */
2674
                                /*  Ok, this should probably define what slots
2675
                                 *  to use. For now, we'll only set it to the
2676
                                 *  defaults:
2677
                                 *
2678
                                 *   non multichannel codec maps to slots 3&4
2679
                                 *   2 channel codec maps to slots 7&8
2680
                                 *   4 channel codec maps to slots 6&9
2681
                                 *   6 channel codec maps to slots 10&11
2682
                                 *
2683
                                 *  there should be some way for the app to
2684
                                 *  select the slot assignment.
2685
                                 */
2686
                                i_scr = inl(state->card->iobase + ALI_SCR);
2687
                                if (codec_independent_spdif_locked > 0) {
2688
 
2689
                                        if ((i_scr & 0x00300000) == 0x00100000) {
2690
                                                ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2691
                                        } else {
2692
                                                if ((i_scr & 0x00300000) == 0x00200000) {
2693
                                                        ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
2694
                                                } else {
2695
                                                        if ((i_scr & 0x00300000) == 0x00300000) {
2696
                                                                ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
2697
                                                        }
2698
                                                }
2699
                                        }
2700
                                } else {        /* codec spdif out (pcm out share ) */
2701
                                        ali_set_spdif_output(state, AC97_EA_SPSA_3_4, dmabuf->rate);    //I do not modify
2702
                                }
2703
 
2704
                                if (!(state->card->ac97_status & SPDIF_ON))
2705
                                        val &= ~DSP_BIND_SPDIF;
2706
                        } else {
2707
                                int mask;
2708
                                int channels;
2709
                                /* Turn off S/PDIF if it was on */
2710
                                if (state->card->ac97_status & SPDIF_ON)
2711
                                        ali_set_spdif_output(state, -1, 0);
2712
                                mask =
2713
                                    val & (DSP_BIND_FRONT | DSP_BIND_SURR |
2714
                                           DSP_BIND_CENTER_LFE);
2715
                                switch (mask) {
2716
                                case DSP_BIND_FRONT:
2717
                                        channels = 2;
2718
                                        break;
2719
                                case DSP_BIND_FRONT | DSP_BIND_SURR:
2720
                                        channels = 4;
2721
                                        break;
2722
                                case DSP_BIND_FRONT | DSP_BIND_SURR | DSP_BIND_CENTER_LFE:
2723
                                        channels = 6;
2724
                                        break;
2725
                                default:
2726
                                        val = DSP_BIND_FRONT;
2727
                                        channels = 2;
2728
                                        break;
2729
                                }
2730
                                ali_set_dac_channels(state, channels);
2731
                                /* check that they really got turned on */
2732
                                if (!state->card->ac97_status & SURR_ON)
2733
                                        val &= ~DSP_BIND_SURR;
2734
                                if (!state->card->
2735
                                    ac97_status & CENTER_LFE_ON)
2736
                                        val &= ~DSP_BIND_CENTER_LFE;
2737
                        }
2738
                }
2739
                return put_user(val, (int *) arg);
2740
        case SNDCTL_DSP_MAPINBUF:
2741
        case SNDCTL_DSP_MAPOUTBUF:
2742
        case SNDCTL_DSP_SETSYNCRO:
2743
        case SOUND_PCM_WRITE_FILTER:
2744
        case SOUND_PCM_READ_FILTER:
2745
                return -EINVAL;
2746
        }
2747
        return -EINVAL;
2748
}
2749
 
2750
static int ali_open(struct inode *inode, struct file *file)
2751
{
2752
        int i = 0;
2753
        struct ali_card *card = devs;
2754
        struct ali_state *state = NULL;
2755
        struct dmabuf *dmabuf = NULL;
2756
        unsigned int i_scr;
2757
 
2758
        /* find an available virtual channel (instance of /dev/dsp) */
2759
 
2760
        while (card != NULL) {
2761
 
2762
                /*
2763
                 * If we are initializing and then fail, card could go
2764
                 * away unuexpectedly while we are in the for() loop.
2765
                 * So, check for card on each iteration before we check
2766
                 * for card->initializing to avoid a possible oops.
2767
                 * This usually only matters for times when the driver is
2768
                 * autoloaded by kmod.
2769
                 */
2770
                for (i = 0; i < 50 && card && card->initializing; i++) {
2771
                        set_current_state(TASK_UNINTERRUPTIBLE);
2772
                        schedule_timeout(HZ / 20);
2773
                }
2774
 
2775
                for (i = 0; i < NR_HW_CH && card && !card->initializing; i++) {
2776
                        if (card->states[i] == NULL) {
2777
                                state = card->states[i] = (struct ali_state *) kmalloc(sizeof(struct ali_state), GFP_KERNEL);
2778
                                if (state == NULL)
2779
                                        return -ENOMEM;
2780
                                memset(state, 0, sizeof(struct ali_state));
2781
                                dmabuf = &state->dmabuf;
2782
                                goto found_virt;
2783
                        }
2784
                }
2785
                card = card->next;
2786
        }
2787
 
2788
        /* no more virtual channel avaiable */
2789
        if (!state)
2790
                return -ENODEV;
2791
found_virt:
2792
        /* initialize the virtual channel */
2793
 
2794
        state->virt = i;
2795
        state->card = card;
2796
        state->magic = ALI5455_STATE_MAGIC;
2797
        init_waitqueue_head(&dmabuf->wait);
2798
        init_MUTEX(&state->open_sem);
2799
        file->private_data = state;
2800
        dmabuf->trigger = 0;
2801
        /* allocate hardware channels */
2802
        if (file->f_mode & FMODE_READ) {
2803
                if ((dmabuf->read_channel =
2804
                     card->alloc_rec_pcm_channel(card)) == NULL) {
2805
                        kfree(card->states[i]);
2806
                        card->states[i] = NULL;
2807
                        return -EBUSY;
2808
                }
2809
                dmabuf->trigger |= PCM_ENABLE_INPUT;
2810
                ali_set_adc_rate(state, 8000);
2811
        }
2812
        if (file->f_mode & FMODE_WRITE) {
2813
                if (codec_independent_spdif_locked > 0) {
2814
                        if ((dmabuf->codec_spdifout_channel = card->alloc_codec_spdifout_channel(card)) == NULL) {
2815
                                kfree(card->states[i]);
2816
                                card->states[i] = NULL;
2817
                                return -EBUSY;
2818
                        }
2819
                        dmabuf->trigger |= SPDIF_ENABLE_OUTPUT;
2820
                        ali_set_codecspdifout_rate(state, codec_independent_spdif_locked);      //It must add
2821
                        i_scr = inl(state->card->iobase + ALI_SCR);
2822
                        if ((i_scr & 0x00300000) == 0x00100000) {
2823
                                ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2824
                        } else {
2825
                                if ((i_scr & 0x00300000) == 0x00200000) {
2826
                                        ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
2827
                                } else {
2828
                                        if ((i_scr & 0x00300000) == 0x00300000) {
2829
                                                ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
2830
                                        } else {
2831
                                                ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2832
                                        }
2833
                                }
2834
 
2835
                        }
2836
                } else {
2837
                        if (controller_independent_spdif_locked > 0) {
2838
                                if ((dmabuf->controller_spdifout_channel = card->alloc_controller_spdifout_channel(card)) == NULL) {
2839
                                        kfree(card->states[i]);
2840
                                        card->states[i] = NULL;
2841
                                        return -EBUSY;
2842
                                }
2843
                                dmabuf->trigger |= SPDIF_ENABLE_OUTPUT;
2844
                                ali_set_spdifout_rate(state, controller_independent_spdif_locked);
2845
                        } else {
2846
                                if ((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
2847
                                        kfree(card->states[i]);
2848
                                        card->states[i] = NULL;
2849
                                        return -EBUSY;
2850
                                }
2851
                                /* Initialize to 8kHz?  What if we don't support 8kHz? */
2852
                                /*  Let's change this to check for S/PDIF stuff */
2853
 
2854
                                dmabuf->trigger |= PCM_ENABLE_OUTPUT;
2855
                                if (codec_pcmout_share_spdif_locked) {
2856
                                        ali_set_dac_rate(state, codec_pcmout_share_spdif_locked);
2857
                                        ali_set_spdif_output(state, AC97_EA_SPSA_3_4, codec_pcmout_share_spdif_locked);
2858
                                } else {
2859
                                        ali_set_dac_rate(state, 8000);
2860
                                }
2861
                        }
2862
 
2863
                }
2864
        }
2865
 
2866
        /* set default sample format. According to OSS Programmer's Guide  /dev/dsp
2867
           should be default to unsigned 8-bits, mono, with sample rate 8kHz and
2868
           /dev/dspW will accept 16-bits sample, but we don't support those so we
2869
           set it immediately to stereo and 16bit, which is all we do support */
2870
        dmabuf->fmt |= ALI5455_FMT_16BIT | ALI5455_FMT_STEREO;
2871
        dmabuf->ossfragsize = 0;
2872
        dmabuf->ossmaxfrags = 0;
2873
        dmabuf->subdivision = 0;
2874
        state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2875
        outl(0x00000000, card->iobase + ALI_INTERRUPTCR);
2876
        outl(0x00000000, card->iobase + ALI_INTERRUPTSR);
2877
        return 0;
2878
}
2879
 
2880
static int ali_release(struct inode *inode, struct file *file)
2881
{
2882
        struct ali_state *state = (struct ali_state *) file->private_data;
2883
        struct ali_card *card = state->card;
2884
        struct dmabuf *dmabuf = &state->dmabuf;
2885
        unsigned long flags;
2886
        lock_kernel();
2887
 
2888
        /* stop DMA state machine and free DMA buffers/channels */
2889
        if (dmabuf->trigger & PCM_ENABLE_OUTPUT)
2890
                drain_dac(state, 0);
2891
 
2892
        if (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)
2893
                drain_spdifout(state, 0);
2894
 
2895
        if (dmabuf->trigger & PCM_ENABLE_INPUT)
2896
                stop_adc(state);
2897
 
2898
        spin_lock_irqsave(&card->lock, flags);
2899
        dealloc_dmabuf(state);
2900
        if (file->f_mode & FMODE_WRITE) {
2901
                if (codec_independent_spdif_locked > 0) {
2902
                        state->card->free_pcm_channel(state->card, dmabuf->codec_spdifout_channel->num);
2903
                } else {
2904
                        if (controller_independent_spdif_locked > 0)
2905
                                state->card->free_pcm_channel(state->card,
2906
                                                              dmabuf->controller_spdifout_channel->num);
2907
                        else state->card->free_pcm_channel(state->card,
2908
                                                              dmabuf->write_channel->num);
2909
                }
2910
        }
2911
        if (file->f_mode & FMODE_READ)
2912
                state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
2913
 
2914
        state->card->states[state->virt] = NULL;
2915
        kfree(state);
2916
        spin_unlock_irqrestore(&card->lock, flags);
2917
        unlock_kernel();
2918
        return 0;
2919
}
2920
 
2921
static /*const */ struct file_operations ali_audio_fops = {
2922
        owner:THIS_MODULE,
2923
        llseek:no_llseek,
2924
        read:ali_read,
2925
        write:ali_write,
2926
        poll:ali_poll,
2927
        ioctl:ali_ioctl,
2928
        mmap:ali_mmap,
2929
        open:ali_open,
2930
        release:ali_release,
2931
};
2932
 
2933
/* Read AC97 codec registers */
2934
static u16 ali_ac97_get(struct ac97_codec *dev, u8 reg)
2935
{
2936
        struct ali_card *card = dev->private_data;
2937
        int count1 = 100;
2938
        char val;
2939
        unsigned short int data, count, addr1, addr2;
2940
 
2941
        spin_lock(&card->ac97_lock);
2942
        while (count1-- && (inl(card->iobase + ALI_CAS) & 0x80000000))
2943
                udelay(1);
2944
 
2945
        addr1 = reg;
2946
        reg |= 0x0080;
2947
        for (count = 0; count < 0x7f; count++) {
2948
                val = inb(card->iobase + ALI_CSPSR);
2949
                if (val & 0x08)
2950
                        break;
2951
        }
2952
        if (count == 0x7f)
2953
        {
2954
                spin_unlock(&card->ac97_lock);
2955
                return -1;
2956
        }
2957
        outw(reg, (card->iobase + ALI_CPR) + 2);
2958
        for (count = 0; count < 0x7f; count++) {
2959
                val = inb(card->iobase + ALI_CSPSR);
2960
                if (val & 0x02) {
2961
                        data = inw(card->iobase + ALI_SPR);
2962
                        addr2 = inw((card->iobase + ALI_SPR) + 2);
2963
                        break;
2964
                }
2965
        }
2966
        spin_unlock(&card->ac97_lock);
2967
        if (count == 0x7f)
2968
                return -1;
2969
        if (addr2 != addr1)
2970
                return -1;
2971
        return ((u16) data);
2972
}
2973
 
2974
/* write ac97 codec register   */
2975
 
2976
static void ali_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
2977
{
2978
        struct ali_card *card = dev->private_data;
2979
        int count1 = 100;
2980
        char val;
2981
        unsigned short int count;
2982
 
2983
        spin_lock(&card->ac97_lock);
2984
        while (count1-- && (inl(card->iobase + ALI_CAS) & 0x80000000))
2985
                udelay(1);
2986
 
2987
        for (count = 0; count < 0x7f; count++) {
2988
                val = inb(card->iobase + ALI_CSPSR);
2989
                if (val & 0x08)
2990
                        break;
2991
        }
2992
        if (count == 0x7f) {
2993
                printk(KERN_WARNING "ali_ac97_set: AC97 codec register access timed out. \n");
2994
                spin_unlock(&card->ac97_lock);
2995
                return;
2996
        }
2997
        outw(data, (card->iobase + ALI_CPR));
2998
        outb(reg, (card->iobase + ALI_CPR) + 2);
2999
        for (count = 0; count < 0x7f; count++) {
3000
                val = inb(card->iobase + ALI_CSPSR);
3001
                if (val & 0x01)
3002
                        break;
3003
        }
3004
        spin_unlock(&card->ac97_lock);
3005
        if (count == 0x7f)
3006
                printk(KERN_WARNING "ali_ac97_set: AC97 codec register access timed out. \n");
3007
        return;
3008
}
3009
 
3010
/* OSS /dev/mixer file operation methods */
3011
 
3012
static int ali_open_mixdev(struct inode *inode, struct file *file)
3013
{
3014
        int i;
3015
        int minor = MINOR(inode->i_rdev);
3016
        struct ali_card *card = devs;
3017
        for (card = devs; card != NULL; card = card->next) {
3018
                /*
3019
                 * If we are initializing and then fail, card could go
3020
                 * away unuexpectedly while we are in the for() loop.
3021
                 * So, check for card on each iteration before we check
3022
                 * for card->initializing to avoid a possible oops.
3023
                 * This usually only matters for times when the driver is
3024
                 * autoloaded by kmod.
3025
                 */
3026
                for (i = 0; i < 50 && card && card->initializing; i++) {
3027
                        set_current_state(TASK_UNINTERRUPTIBLE);
3028
                        schedule_timeout(HZ / 20);
3029
                }
3030
                for (i = 0; i < NR_AC97 && card && !card->initializing; i++)
3031
                        if (card->ac97_codec[i] != NULL
3032
                            && card->ac97_codec[i]->dev_mixer == minor) {
3033
                                file->private_data = card->ac97_codec[i];
3034
                                return 0;
3035
                        }
3036
        }
3037
        return -ENODEV;
3038
}
3039
 
3040
static int ali_ioctl_mixdev(struct inode *inode,
3041
                            struct file *file,
3042
                            unsigned int cmd, unsigned long arg)
3043
{
3044
        struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
3045
        return codec->mixer_ioctl(codec, cmd, arg);
3046
}
3047
 
3048
static /*const */ struct file_operations ali_mixer_fops = {
3049
        owner:THIS_MODULE,
3050
        llseek:no_llseek,
3051
        ioctl:ali_ioctl_mixdev,
3052
        open:ali_open_mixdev,
3053
};
3054
 
3055
/* AC97 codec initialisation.  These small functions exist so we don't
3056
   duplicate code between module init and apm resume */
3057
 
3058
static inline int ali_ac97_exists(struct ali_card *card, int ac97_number)
3059
{
3060
        unsigned int i = 1;
3061
        u32 reg = inl(card->iobase + ALI_RTSR);
3062
        if (ac97_number) {
3063
                while (i < 100) {
3064
 
3065
                        reg = inl(card->iobase + ALI_RTSR);
3066
                        if (reg & 0x40) {
3067
                                break;
3068
                        } else {
3069
                                outl(reg | 0x00000040,
3070
                                     card->iobase + 0x34);
3071
                                udelay(1);
3072
                        }
3073
                        i++;
3074
                }
3075
 
3076
        } else {
3077
                while (i < 100) {
3078
                        reg = inl(card->iobase + ALI_RTSR);
3079
                        if (reg & 0x80) {
3080
                                break;
3081
                        } else {
3082
                                outl(reg | 0x00000080,
3083
                                     card->iobase + 0x34);
3084
                                udelay(1);
3085
                        }
3086
                        i++;
3087
                }
3088
        }
3089
 
3090
        if (ac97_number)
3091
                return reg & 0x40;
3092
        else
3093
                return reg & 0x80;
3094
}
3095
 
3096
static inline int ali_ac97_enable_variable_rate(struct ac97_codec *codec)
3097
{
3098
        ali_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
3099
        ali_ac97_set(codec, AC97_EXTENDED_STATUS, ali_ac97_get(codec, AC97_EXTENDED_STATUS) | 0xE800);
3100
        return (ali_ac97_get(codec, AC97_EXTENDED_STATUS) & 1);
3101
}
3102
 
3103
 
3104
static int ali_ac97_probe_and_powerup(struct ali_card *card, struct ac97_codec *codec)
3105
{
3106
        /* Returns 0 on failure */
3107
        int i;
3108
        u16 addr;
3109
        if (ac97_probe_codec(codec) == 0)
3110
                return 0;
3111
        /* ac97_probe_codec is success ,then begin to init codec */
3112
        ali_ac97_set(codec, AC97_RESET, 0xffff);
3113
        if (card->channel[0].used == 1) {
3114
                ali_ac97_set(codec, AC97_RECORD_SELECT, 0x0000);
3115
                ali_ac97_set(codec, AC97_LINEIN_VOL, 0x0808);
3116
                ali_ac97_set(codec, AC97_RECORD_GAIN, 0x0F0F);
3117
        }
3118
 
3119
        if (card->channel[2].used == 1) //if MICin then init codec
3120
        {
3121
                ali_ac97_set(codec, AC97_RECORD_SELECT, 0x0000);
3122
                ali_ac97_set(codec, AC97_MIC_VOL, 0x8808);
3123
                ali_ac97_set(codec, AC97_RECORD_GAIN, 0x0F0F);
3124
                ali_ac97_set(codec, AC97_RECORD_GAIN_MIC, 0x0000);
3125
        }
3126
 
3127
        ali_ac97_set(codec, AC97_MASTER_VOL_STEREO, 0x0000);
3128
        ali_ac97_set(codec, AC97_HEADPHONE_VOL, 0x0000);
3129
        ali_ac97_set(codec, AC97_PCMOUT_VOL, 0x0000);
3130
        ali_ac97_set(codec, AC97_CD_VOL, 0x0808);
3131
        ali_ac97_set(codec, AC97_VIDEO_VOL, 0x0808);
3132
        ali_ac97_set(codec, AC97_AUX_VOL, 0x0808);
3133
        ali_ac97_set(codec, AC97_PHONE_VOL, 0x8048);
3134
        ali_ac97_set(codec, AC97_PCBEEP_VOL, 0x0000);
3135
        ali_ac97_set(codec, AC97_GENERAL_PURPOSE, AC97_GP_MIX);
3136
        ali_ac97_set(codec, AC97_MASTER_VOL_MONO, 0x0000);
3137
        ali_ac97_set(codec, 0x38, 0x0000);
3138
        addr = ali_ac97_get(codec, 0x2a);
3139
        ali_ac97_set(codec, 0x2a, addr | 0x0001);
3140
        addr = ali_ac97_get(codec, 0x2a);
3141
        addr = ali_ac97_get(codec, 0x28);
3142
        ali_ac97_set(codec, 0x2c, 0xbb80);
3143
        addr = ali_ac97_get(codec, 0x2c);
3144
        /* power it all up */
3145
        ali_ac97_set(codec, AC97_POWER_CONTROL,
3146
                     ali_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
3147
        /* wait for analog ready */
3148
        for (i = 10; i && ((ali_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf); i--) {
3149
                set_current_state(TASK_UNINTERRUPTIBLE);
3150
                schedule_timeout(HZ / 20);
3151
        }
3152
        /* FIXME !! */
3153
        i++;
3154
        return i;
3155
}
3156
 
3157
 
3158
/* I clone ali5455(2.4.7 )  not clone i810_audio(2.4.18)  */
3159
 
3160
static int ali_reset_5455(struct ali_card *card)
3161
{
3162
        outl(0x80000003, card->iobase + ALI_SCR);
3163
        outl(0x83838383, card->iobase + ALI_FIFOCR1);
3164
        outl(0x83838383, card->iobase + ALI_FIFOCR2);
3165
        if (controller_pcmout_share_spdif_locked > 0) {
3166
                outl((inl(card->iobase + ALI_SPDIFICS) | 0x00000001),
3167
                     card->iobase + ALI_SPDIFICS);
3168
                outl(0x0408000a, card->iobase + ALI_INTERFACECR);
3169
        } else {
3170
                if (codec_independent_spdif_locked > 0) {
3171
                        outl((inl(card->iobase + ALI_SCR) | 0x00100000), card->iobase + ALI_SCR);       // now I select slot 7 & 8
3172
                        outl(0x00200000, card->iobase + ALI_INTERFACECR);       //enable codec independent spdifout 
3173
                } else
3174
                        outl(0x04080002, card->iobase + ALI_INTERFACECR);
3175
        }
3176
 
3177
        outl(0x00000000, card->iobase + ALI_INTERRUPTCR);
3178
        outl(0x00000000, card->iobase + ALI_INTERRUPTSR);
3179
        if (controller_independent_spdif_locked > 0)
3180
                outl((inl(card->iobase + ALI_SPDIFICS) | 0x00000001),
3181
                     card->iobase + ALI_SPDIFICS);
3182
        return 1;
3183
}
3184
 
3185
 
3186
static int ali_ac97_random_init_stuff(struct ali_card
3187
                                      *card)
3188
{
3189
        u32 reg = inl(card->iobase + ALI_SCR);
3190
        int i = 0;
3191
        reg = inl(card->iobase + ALI_SCR);
3192
        if ((reg & 2) == 0)      /* Cold required */
3193
                reg |= 2;
3194
        else
3195
                reg |= 1;       /* Warm */
3196
        reg &= ~0x80000000;     /* ACLink on */
3197
        outl(reg, card->iobase + ALI_SCR);
3198
 
3199
        while (i < 10) {
3200
                if ((inl(card->iobase + 0x18) & (1 << 1)) == 0)
3201
                        break;
3202
                current->state = TASK_UNINTERRUPTIBLE;
3203
                schedule_timeout(HZ / 20);
3204
                i++;
3205
        }
3206
        if (i == 10) {
3207
                printk(KERN_ERR "ali_audio: AC'97 reset failed.\n");
3208
                return 0;
3209
        }
3210
 
3211
        set_current_state(TASK_UNINTERRUPTIBLE);
3212
        schedule_timeout(HZ / 2);
3213
        return 1;
3214
}
3215
 
3216
/* AC97 codec initialisation. */
3217
 
3218
static int __init ali_ac97_init(struct ali_card *card)
3219
{
3220
        int num_ac97 = 0;
3221
        int total_channels = 0;
3222
        struct ac97_codec *codec;
3223
        u16 eid;
3224
 
3225
        if (!ali_ac97_random_init_stuff(card))
3226
                return 0;
3227
 
3228
        /* Number of channels supported */
3229
        /* What about the codec?  Just because the ICH supports */
3230
        /* multiple channels doesn't mean the codec does.       */
3231
        /* we'll have to modify this in the codec section below */
3232
        /* to reflect what the codec has.                       */
3233
        /* ICH and ICH0 only support 2 channels so don't bother */
3234
        /* to check....                                         */
3235
        inl(card->iobase + ALI_CPR);
3236
        card->channels = 2;
3237
 
3238
        for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3239
 
3240
                /* Assume codec isn't available until we go through the
3241
                 * gauntlet below */
3242
                card->ac97_codec[num_ac97] = NULL;
3243
                /* The ICH programmer's reference says you should   */
3244
                /* check the ready status before probing. So we chk */
3245
                /*   What do we do if it's not ready?  Wait and try */
3246
                /*   again, or abort?                               */
3247
                if (!ali_ac97_exists(card, num_ac97)) {
3248
                        if (num_ac97 == 0)
3249
                                printk(KERN_ERR "ali_audio: Primary codec not ready.\n");
3250
                        break;
3251
                }
3252
 
3253
                if ((codec = ac97_alloc_codec()) == NULL)
3254
                        return -ENOMEM;
3255
                /* initialize some basic codec information, other fields will be filled
3256
                   in ac97_probe_codec */
3257
                codec->private_data = card;
3258
                codec->id = num_ac97;
3259
                codec->codec_read = ali_ac97_get;
3260
                codec->codec_write = ali_ac97_set;
3261
                if (!ali_ac97_probe_and_powerup(card, codec)) {
3262
                        printk(KERN_ERR "ali_audio: timed out waiting for codec %d analog ready",
3263
                             num_ac97);
3264
                        kfree(codec);
3265
                        break;  /* it didn't work */
3266
                }
3267
 
3268
                /* Store state information about S/PDIF transmitter */
3269
                card->ac97_status = 0;
3270
                /* Don't attempt to get eid until powerup is complete */
3271
                eid = ali_ac97_get(codec, AC97_EXTENDED_ID);
3272
                if (eid == 0xFFFF) {
3273
                        printk(KERN_ERR "ali_audio: no codec attached ?\n");
3274
                        kfree(codec);
3275
                        break;
3276
                }
3277
 
3278
                card->ac97_features = eid;
3279
                /* Now check the codec for useful features to make up for
3280
                   the dumbness of the ali5455 hardware engine */
3281
                if (!(eid & 0x0001))
3282
                        printk(KERN_WARNING
3283
                               "ali_audio: only 48Khz playback available.\n");
3284
                else {
3285
                        if (!ali_ac97_enable_variable_rate(codec)) {
3286
                                printk(KERN_WARNING
3287
                                       "ali_audio: Codec refused to allow VRA, using 48Khz only.\n");
3288
                                card->ac97_features &= ~1;
3289
                        }
3290
                }
3291
 
3292
                /* Determine how many channels the codec(s) support   */
3293
                /*   - The primary codec always supports 2            */
3294
                /*   - If the codec supports AMAP, surround DACs will */
3295
                /*     automaticlly get assigned to slots.            */
3296
                /*     * Check for surround DACs and increment if     */
3297
                /*       found.                                       */
3298
                /*   - Else check if the codec is revision 2.2        */
3299
                /*     * If surround DACs exist, assign them to slots */
3300
                /*       and increment channel count.                 */
3301
 
3302
                /* All of this only applies to ICH2 and above. ICH    */
3303
                /* and ICH0 only support 2 channels.  ICH2 will only  */
3304
                /* support multiple codecs in a "split audio" config. */
3305
                /* as described above.                                */
3306
 
3307
                /* TODO: Remove all the debugging messages!           */
3308
 
3309
                if ((eid & 0xc000) == 0) /* primary codec */
3310
                        total_channels += 2;
3311
                if ((codec->dev_mixer = register_sound_mixer(&ali_mixer_fops, -1)) < 0) {
3312
                        printk(KERN_ERR "ali_audio: couldn't register mixer!\n");
3313
                        kfree(codec);
3314
                        break;
3315
                }
3316
                card->ac97_codec[num_ac97] = codec;
3317
        }
3318
        /* pick the minimum of channels supported by ICHx or codec(s) */
3319
        card->channels = (card->channels > total_channels) ? total_channels : card->channels;
3320
        return num_ac97;
3321
}
3322
 
3323
static void __init ali_configure_clocking(void)
3324
{
3325
        struct ali_card *card;
3326
        struct ali_state *state;
3327
        struct dmabuf *dmabuf;
3328
        unsigned int i, offset, new_offset;
3329
        unsigned long flags;
3330
        card = devs;
3331
 
3332
        /* We could try to set the clocking for multiple cards, but can you even have
3333
         * more than one ali in a machine?  Besides, clocking is global, so unless
3334
         * someone actually thinks more than one ali in a machine is possible and
3335
         * decides to rewrite that little bit, setting the rate for more than one card
3336
         * is a waste of time.
3337
         */
3338
        if (card != NULL) {
3339
                state = card->states[0] = (struct ali_state *)
3340
                    kmalloc(sizeof(struct ali_state), GFP_KERNEL);
3341
                if (state == NULL)
3342
                        return;
3343
                memset(state, 0, sizeof(struct ali_state));
3344
                dmabuf = &state->dmabuf;
3345
                dmabuf->write_channel = card->alloc_pcm_channel(card);
3346
                state->virt = 0;
3347
                state->card = card;
3348
                state->magic = ALI5455_STATE_MAGIC;
3349
                init_waitqueue_head(&dmabuf->wait);
3350
                init_MUTEX(&state->open_sem);
3351
                dmabuf->fmt = ALI5455_FMT_STEREO | ALI5455_FMT_16BIT;
3352
                dmabuf->trigger = PCM_ENABLE_OUTPUT;
3353
                ali_set_dac_rate(state, 48000);
3354
                if (prog_dmabuf(state, 0) != 0)
3355
                        goto config_out_nodmabuf;
3356
 
3357
                if (dmabuf->dmasize < 16384)
3358
                        goto config_out;
3359
 
3360
                dmabuf->count = dmabuf->dmasize;
3361
                outb(31, card->iobase + dmabuf->write_channel->port + OFF_LVI);
3362
 
3363
                save_flags(flags);
3364
                cli();
3365
                start_dac(state);
3366
                offset = ali_get_dma_addr(state, 0);
3367
                mdelay(50);
3368
                new_offset = ali_get_dma_addr(state, 0);
3369
                stop_dac(state);
3370
 
3371
                outb(2, card->iobase + dmabuf->write_channel->port + OFF_CR);
3372
                restore_flags(flags);
3373
 
3374
                i = new_offset - offset;
3375
 
3376
                if (i == 0)
3377
                        goto config_out;
3378
                i = i / 4 * 20;
3379
                if (i > 48500 || i < 47500) {
3380
                        clocking = clocking * clocking / i;
3381
                }
3382
config_out:
3383
                dealloc_dmabuf(state);
3384
config_out_nodmabuf:
3385
                state->card->free_pcm_channel(state->card, state->dmabuf. write_channel->num);
3386
                kfree(state);
3387
                card->states[0] = NULL;
3388
        }
3389
}
3390
 
3391
/* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
3392
   until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
3393
 
3394
static int __init ali_probe(struct pci_dev *pci_dev, const struct pci_device_id
3395
                            *pci_id)
3396
{
3397
        struct ali_card *card;
3398
        if (pci_enable_device(pci_dev))
3399
                return -EIO;
3400
        if (pci_set_dma_mask(pci_dev, ALI5455_DMA_MASK)) {
3401
                printk(KERN_ERR "ali5455: architecture does not support"
3402
                       " 32bit PCI busmaster DMA\n");
3403
                return -ENODEV;
3404
        }
3405
 
3406
        if ((card = kmalloc(sizeof(struct ali_card), GFP_KERNEL)) == NULL) {
3407
                printk(KERN_ERR "ali_audio: out of memory\n");
3408
                return -ENOMEM;
3409
        }
3410
        memset(card, 0, sizeof(*card));
3411
        card->initializing = 1;
3412
        card->iobase = pci_resource_start(pci_dev, 0);
3413
        card->pci_dev = pci_dev;
3414
        card->pci_id = pci_id->device;
3415
        card->irq = pci_dev->irq;
3416
        card->next = devs;
3417
        card->magic = ALI5455_CARD_MAGIC;
3418
#ifdef CONFIG_PM
3419
        card->pm_suspended = 0;
3420
#endif
3421
        spin_lock_init(&card->lock);
3422
        spin_lock_init(&card->ac97_lock);
3423
        devs = card;
3424
        pci_set_master(pci_dev);
3425
        printk(KERN_INFO "ali: %s found at IO 0x%04lx, IRQ %d\n",
3426
               card_names[pci_id->driver_data], card->iobase, card->irq);
3427
        card->alloc_pcm_channel = ali_alloc_pcm_channel;
3428
        card->alloc_rec_pcm_channel = ali_alloc_rec_pcm_channel;
3429
        card->alloc_rec_mic_channel = ali_alloc_rec_mic_channel;
3430
        card->alloc_codec_spdifout_channel = ali_alloc_codec_spdifout_channel;
3431
        card->alloc_controller_spdifout_channel = ali_alloc_controller_spdifout_channel;
3432
        card->free_pcm_channel = ali_free_pcm_channel;
3433
        card->channel[0].offset = 0;
3434
        card->channel[0].port = 0x40;
3435
        card->channel[0].num = 0;
3436
        card->channel[1].offset = 0;
3437
        card->channel[1].port = 0x50;
3438
        card->channel[1].num = 1;
3439
        card->channel[2].offset = 0;
3440
        card->channel[2].port = 0x60;
3441
        card->channel[2].num = 2;
3442
        card->channel[3].offset = 0;
3443
        card->channel[3].port = 0x70;
3444
        card->channel[3].num = 3;
3445
        card->channel[4].offset = 0;
3446
        card->channel[4].port = 0xb0;
3447
        card->channel[4].num = 4;
3448
        /* claim our iospace and irq */
3449
        request_region(card->iobase, 256, card_names[pci_id->driver_data]);
3450
        if (request_irq(card->irq, &ali_interrupt, SA_SHIRQ,
3451
                        card_names[pci_id->driver_data], card)) {
3452
                printk(KERN_ERR "ali_audio: unable to allocate irq %d\n",
3453
                       card->irq);
3454
                release_region(card->iobase, 256);
3455
                kfree(card);
3456
                return -ENODEV;
3457
        }
3458
 
3459
        if (ali_reset_5455(card) <= 0) {
3460
                unregister_sound_dsp(card->dev_audio);
3461
                release_region(card->iobase, 256);
3462
                free_irq(card->irq, card);
3463
                kfree(card);
3464
                return -ENODEV;
3465
        }
3466
 
3467
        /* initialize AC97 codec and register /dev/mixer */
3468
        if (ali_ac97_init(card) < 0) {
3469
                release_region(card->iobase, 256);
3470
                free_irq(card->irq, card);
3471
                kfree(card);
3472
                return -ENODEV;
3473
        }
3474
 
3475
        pci_set_drvdata(pci_dev, card);
3476
 
3477
        if (clocking == 0) {
3478
                clocking = 48000;
3479
                ali_configure_clocking();
3480
        }
3481
 
3482
        /* register /dev/dsp */
3483
        if ((card->dev_audio = register_sound_dsp(&ali_audio_fops, -1)) < 0) {
3484
                int i;
3485
                printk(KERN_ERR"ali_audio: couldn't register DSP device!\n");
3486
                release_region(card->iobase, 256);
3487
                free_irq(card->irq, card);
3488
                for (i = 0; i < NR_AC97; i++)
3489
                        if (card->ac97_codec[i] != NULL) {
3490
                                unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
3491
                                kfree(card->ac97_codec[i]);
3492
                        }
3493
                kfree(card);
3494
                return -ENODEV;
3495
        }
3496
        card->initializing = 0;
3497
        return 0;
3498
}
3499
 
3500
static void __devexit ali_remove(struct pci_dev *pci_dev)
3501
{
3502
        int i;
3503
        struct ali_card *card = pci_get_drvdata(pci_dev);
3504
        /* free hardware resources */
3505
        free_irq(card->irq, devs);
3506
        release_region(card->iobase, 256);
3507
        /* unregister audio devices */
3508
        for (i = 0; i < NR_AC97; i++)
3509
                if (card->ac97_codec[i] != NULL) {
3510
                        unregister_sound_mixer(card->ac97_codec[i]->
3511
                                               dev_mixer);
3512
                        ac97_release_codec(card->ac97_codec[i]);
3513
                        card->ac97_codec[i] = NULL;
3514
                }
3515
        unregister_sound_dsp(card->dev_audio);
3516
        kfree(card);
3517
}
3518
 
3519
#ifdef CONFIG_PM
3520
static int ali_pm_suspend(struct pci_dev *dev, u32 pm_state)
3521
{
3522
        struct ali_card *card = pci_get_drvdata(dev);
3523
        struct ali_state *state;
3524
        unsigned long flags;
3525
        struct dmabuf *dmabuf;
3526
        int i, num_ac97;
3527
 
3528
        if (!card)
3529
                return 0;
3530
        spin_lock_irqsave(&card->lock, flags);
3531
        card->pm_suspended = 1;
3532
        for (i = 0; i < NR_HW_CH; i++) {
3533
                state = card->states[i];
3534
                if (!state)
3535
                        continue;
3536
                /* this happens only if there are open files */
3537
                dmabuf = &state->dmabuf;
3538
                if (dmabuf->enable & DAC_RUNNING ||
3539
                    (dmabuf->count
3540
                     && (dmabuf->trigger & PCM_ENABLE_OUTPUT))) {
3541
                        state->pm_saved_dac_rate = dmabuf->rate;
3542
                        stop_dac(state);
3543
                } else {
3544
                        state->pm_saved_dac_rate = 0;
3545
                }
3546
                if (dmabuf->enable & ADC_RUNNING) {
3547
                        state->pm_saved_adc_rate = dmabuf->rate;
3548
                        stop_adc(state);
3549
                } else {
3550
                        state->pm_saved_adc_rate = 0;
3551
                }
3552
                dmabuf->ready = 0;
3553
                dmabuf->swptr = dmabuf->hwptr = 0;
3554
                dmabuf->count = dmabuf->total_bytes = 0;
3555
        }
3556
 
3557
        spin_unlock_irqrestore(&card->lock, flags);
3558
        /* save mixer settings */
3559
        for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3560
                struct ac97_codec *codec = card->ac97_codec[num_ac97];
3561
                if (!codec)
3562
                        continue;
3563
                for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3564
                        if ((supported_mixer(codec, i)) && (codec->read_mixer)) {
3565
                                card->pm_saved_mixer_settings[i][num_ac97] = codec->read_mixer(codec, i);
3566
                        }
3567
                }
3568
        }
3569
        pci_save_state(dev, card->pm_save_state);       /* XXX do we need this? */
3570
        pci_disable_device(dev);        /* disable busmastering */
3571
        pci_set_power_state(dev, 3);    /* Zzz. */
3572
        return 0;
3573
}
3574
 
3575
 
3576
static int ali_pm_resume(struct pci_dev *dev)
3577
{
3578
        int num_ac97, i = 0;
3579
        struct ali_card *card = pci_get_drvdata(dev);
3580
        pci_enable_device(dev);
3581
        pci_restore_state(dev, card->pm_save_state);
3582
        /* observation of a toshiba portege 3440ct suggests that the
3583
           hardware has to be more or less completely reinitialized from
3584
           scratch after an apm suspend.  Works For Me.   -dan */
3585
        ali_ac97_random_init_stuff(card);
3586
        for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3587
                struct ac97_codec *codec = card->ac97_codec[num_ac97];
3588
                /* check they haven't stolen the hardware while we were
3589
                   away */
3590
                if (!codec || !ali_ac97_exists(card, num_ac97)) {
3591
                        if (num_ac97)
3592
                                continue;
3593
                        else
3594
                                BUG();
3595
                }
3596
                if (!ali_ac97_probe_and_powerup(card, codec))
3597
                        BUG();
3598
                if ((card->ac97_features & 0x0001)) {
3599
                        /* at probe time we found we could do variable
3600
                           rates, but APM suspend has made it forget
3601
                           its magical powers */
3602
                        if (!ali_ac97_enable_variable_rate(codec))
3603
                                BUG();
3604
                }
3605
                /* we lost our mixer settings, so restore them */
3606
                for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3607
                        if (supported_mixer(codec, i)) {
3608
                                int val = card->pm_saved_mixer_settings[i][num_ac97];
3609
                                codec->mixer_state[i] = val;
3610
                                codec->write_mixer(codec, i,
3611
                                                   (val & 0xff),
3612
                                                   ((val >> 8) & 0xff));
3613
                        }
3614
                }
3615
        }
3616
 
3617
        /* we need to restore the sample rate from whatever it was */
3618
        for (i = 0; i < NR_HW_CH; i++) {
3619
                struct ali_state *state = card->states[i];
3620
                if (state) {
3621
                        if (state->pm_saved_adc_rate)
3622
                                ali_set_adc_rate(state, state->pm_saved_adc_rate);
3623
                        if (state->pm_saved_dac_rate)
3624
                                ali_set_dac_rate(state, state->pm_saved_dac_rate);
3625
                }
3626
        }
3627
 
3628
        card->pm_suspended = 0;
3629
        /* any processes that were reading/writing during the suspend
3630
           probably ended up here */
3631
        for (i = 0; i < NR_HW_CH; i++) {
3632
                struct ali_state *state = card->states[i];
3633
                if (state)
3634
                        wake_up(&state->dmabuf.wait);
3635
        }
3636
        return 0;
3637
}
3638
#endif                          /* CONFIG_PM */
3639
 
3640
MODULE_AUTHOR("");
3641
MODULE_DESCRIPTION("ALI 5455 audio support");
3642
MODULE_LICENSE("GPL");
3643
MODULE_PARM(clocking, "i");
3644
MODULE_PARM(strict_clocking, "i");
3645
MODULE_PARM(codec_pcmout_share_spdif_locked, "i");
3646
MODULE_PARM(codec_independent_spdif_locked, "i");
3647
MODULE_PARM(controller_pcmout_share_spdif_locked, "i");
3648
MODULE_PARM(controller_independent_spdif_locked, "i");
3649
 
3650
#define ALI5455_MODULE_NAME "ali5455"
3651
 
3652
static struct pci_driver ali_pci_driver = {
3653
        name:                   ALI5455_MODULE_NAME,
3654
        id_table:               ali_pci_tbl,
3655
        probe:                  ali_probe,
3656
        remove:                 __devexit_p(ali_remove),
3657
#ifdef CONFIG_PM
3658
        suspend:                ali_pm_suspend,
3659
        resume:                 ali_pm_resume,
3660
#endif                          /* CONFIG_PM */
3661
};
3662
 
3663
static int __init ali_init_module(void)
3664
{
3665
        if (!pci_present())     /* No PCI bus in this machine! */
3666
                return -ENODEV;
3667
        printk(KERN_INFO "ALI 5455 + AC97 Audio, version "
3668
               DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
3669
 
3670
        if (codec_independent_spdif_locked > 0) {
3671
                if (codec_independent_spdif_locked == 32000
3672
                    || codec_independent_spdif_locked == 44100
3673
                    || codec_independent_spdif_locked == 48000) {
3674
                        printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", codec_independent_spdif_locked);
3675
                } else {
3676
                        printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3677
                        codec_independent_spdif_locked = 0;
3678
                }
3679
        }
3680
        if (controller_independent_spdif_locked > 0) {
3681
                if (controller_independent_spdif_locked == 32000
3682
                    || controller_independent_spdif_locked == 44100
3683
                    || controller_independent_spdif_locked == 48000) {
3684
                        printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", controller_independent_spdif_locked);
3685
                } else {
3686
                        printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3687
                        controller_independent_spdif_locked = 0;
3688
                }
3689
        }
3690
 
3691
        if (codec_pcmout_share_spdif_locked > 0) {
3692
                if (codec_pcmout_share_spdif_locked == 32000
3693
                    || codec_pcmout_share_spdif_locked == 44100
3694
                    || codec_pcmout_share_spdif_locked == 48000) {
3695
                        printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", codec_pcmout_share_spdif_locked);
3696
                } else {
3697
                        printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3698
                        codec_pcmout_share_spdif_locked = 0;
3699
                }
3700
        }
3701
        if (controller_pcmout_share_spdif_locked > 0) {
3702
                if (controller_pcmout_share_spdif_locked == 32000
3703
                    || controller_pcmout_share_spdif_locked == 44100
3704
                    || controller_pcmout_share_spdif_locked == 48000) {
3705
                        printk(KERN_INFO "ali_audio: Enabling controller S/PDIF at sample rate %dHz.\n", controller_pcmout_share_spdif_locked);
3706
                } else {
3707
                        printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3708
                        controller_pcmout_share_spdif_locked = 0;
3709
                }
3710
        }
3711
        if (!pci_register_driver(&ali_pci_driver)) {
3712
                pci_unregister_driver(&ali_pci_driver);
3713
                return -ENODEV;
3714
        }
3715
        return 0;
3716
}
3717
 
3718
static void __exit ali_cleanup_module(void)
3719
{
3720
        pci_unregister_driver(&ali_pci_driver);
3721
}
3722
 
3723
module_init(ali_init_module);
3724
module_exit(ali_cleanup_module);
3725
/*
3726
Local Variables:
3727
c-basic-offset: 8
3728
End:
3729
*/

powered by: WebSVN 2.1.0

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