2 * ALI ali5455 and friends ICH driver for Linux
3 * LEI HU <Lei_Hu@ali.com.tw>
6 * drivers/sound/i810_audio
8 * The ALi 5455 is similar but not quite identical to the Intel ICH
9 * series of controllers. Its easier to keep the driver separated from
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.
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.
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.
27 * ALi 5455 theory of operation
29 * The chipset provides three DMA channels that talk to an AC97
30 * CODEC (AC97 is a digital/analog mixer standard). At its simplest
31 * you get 48Khz audio with basic volume and mixer controls. At the
32 * best you get rate adaption in the codec. We set the card up so
33 * that we never take completion interrupts but instead keep the card
34 * chasing its tail around a ring buffer. This is needed for mmap
35 * mode audio and happens to work rather well for non-mmap modes too.
37 * The board has one output channel for PCM audio (supported) and
38 * a stereo line in and mono microphone input. Again these are normally
39 * locked to 48Khz only. Right now recording is not finished.
41 * There is no midi support, no synth support. Use timidity. To get
42 * esd working you need to use esd -r 48000 as it won't probe 48KHz
43 * by default. mpg123 can't handle 48Khz only audio so use xmms.
45 * If you need to force a specific rate set the clocking= option
49 #include <linux/module.h>
50 #include <linux/string.h>
51 #include <linux/ctype.h>
52 #include <linux/ioport.h>
53 #include <linux/sched.h>
54 #include <linux/delay.h>
55 #include <linux/sound.h>
56 #include <linux/slab.h>
57 #include <linux/soundcard.h>
58 #include <linux/pci.h>
61 #include <linux/init.h>
62 #include <linux/poll.h>
63 #include <linux/spinlock.h>
64 #include <linux/smp_lock.h>
65 #include <linux/ac97_codec.h>
66 #include <linux/interrupt.h>
67 #include <asm/uaccess.h>
68 #include <asm/hardirq.h>
70 #ifndef PCI_DEVICE_ID_ALI_5455
71 #define PCI_DEVICE_ID_ALI_5455 0x5455
74 #ifndef PCI_VENDOR_ID_ALI
75 #define PCI_VENDOR_ID_ALI 0x10b9
78 static int strict_clocking = 0;
79 static unsigned int clocking = 0;
80 static unsigned int codec_pcmout_share_spdif_locked = 0;
81 static unsigned int codec_independent_spdif_locked = 0;
82 static unsigned int controller_pcmout_share_spdif_locked = 0;
83 static unsigned int controller_independent_spdif_locked = 0;
84 static unsigned int globel = 0;
88 #define CODEC_SPDIFOUT_RUNNING 8
89 #define CONTROLLER_SPDIFOUT_RUNNING 4
91 #define SPDIF_ENABLE_OUTPUT 4 /* bits 0,1 are PCM */
93 #define ALI5455_FMT_16BIT 1
94 #define ALI5455_FMT_STEREO 2
95 #define ALI5455_FMT_MASK 3
97 #define SPDIF_ON 0x0004
98 #define SURR_ON 0x0010
99 #define CENTER_LFE_ON 0x0020
100 #define VOL_MUTED 0x8000
103 #define ALI_SPDIF_OUT_CH_STATUS 0xbf
104 /* the 810's array of pointers to data buffers */
107 #define BUSADDR_MASK 0xFFFFFFFE
109 #define CON_IOC 0x80000000 /* interrupt on completion */
110 #define CON_BUFPAD 0x40000000 /* pad underrun with last sample, else 0 */
111 #define CON_BUFLEN_MASK 0x0000ffff /* buffer length in samples */
115 /* an instance of the ali channel */
118 /* these sg guys should probably be allocated
119 separately as nocache. Must be 8 byte aligned */
120 struct sg_item sg[SG_LEN]; /* 32*8 */
128 * we have 3 separate dma engines. pcm in, pcm out, and mic.
129 * each dma engine has controlling registers. These goofy
130 * names are from the datasheet, but make it easy to write
131 * code while leafing through it.
134 #define ENUM_ENGINE(PRE,DIG) \
136 PRE##_BDBAR = 0x##DIG##0, /* Buffer Descriptor list Base Address */ \
137 PRE##_CIV = 0x##DIG##4, /* Current Index Value */ \
138 PRE##_LVI = 0x##DIG##5, /* Last Valid Index */ \
139 PRE##_SR = 0x##DIG##6, /* Status Register */ \
140 PRE##_PICB = 0x##DIG##8, /* Position In Current Buffer */ \
141 PRE##_CR = 0x##DIG##b /* Control Register */ \
144 ENUM_ENGINE(OFF, 0); /* Offsets */
145 ENUM_ENGINE(PI, 4); /* PCM In */
146 ENUM_ENGINE(PO, 5); /* PCM Out */
147 ENUM_ENGINE(MC, 6); /* Mic In */
148 ENUM_ENGINE(CODECSPDIFOUT, 7); /* CODEC SPDIF OUT */
149 ENUM_ENGINE(CONTROLLERSPDIFIN, A); /* CONTROLLER SPDIF In */
150 ENUM_ENGINE(CONTROLLERSPDIFOUT, B); /* CONTROLLER SPDIF OUT */
154 ALI_SCR = 0x00, /* System Control Register */
155 ALI_SSR = 0x04, /* System Status Register */
156 ALI_DMACR = 0x08, /* DMA Control Register */
157 ALI_FIFOCR1 = 0x0c, /* FIFO Control Register 1 */
158 ALI_INTERFACECR = 0x10, /* Interface Control Register */
159 ALI_INTERRUPTCR = 0x14, /* Interrupt control Register */
160 ALI_INTERRUPTSR = 0x18, /* Interrupt Status Register */
161 ALI_FIFOCR2 = 0x1c, /* FIFO Control Register 2 */
162 ALI_CPR = 0x20, /* Command Port Register */
163 ALI_SPR = 0x24, /* Status Port Register */
164 ALI_FIFOCR3 = 0x2c, /* FIFO Control Register 3 */
165 ALI_TTSR = 0x30, /* Transmit Tag Slot Register */
166 ALI_RTSR = 0x34, /* Receive Tag Slot Register */
167 ALI_CSPSR = 0x38, /* Command/Status Port Status Register */
168 ALI_CAS = 0x3c, /* Codec Write Semaphore Register */
169 ALI_SPDIFCSR = 0xf8, /* spdif channel status register */
170 ALI_SPDIFICS = 0xfc /* spdif interface control/status */
173 // x-status register(x:pcm in ,pcm out, mic in,)
174 /* interrupts for a dma engine */
175 #define DMA_INT_FIFO (1<<4) /* fifo under/over flow */
176 #define DMA_INT_COMPLETE (1<<3) /* buffer read/write complete and ioc set */
177 #define DMA_INT_LVI (1<<2) /* last valid done */
178 #define DMA_INT_CELV (1<<1) /* last valid is current */
179 #define DMA_INT_DCH (1) /* DMA Controller Halted (happens on LVI interrupts) */ //not eqult intel
180 #define DMA_INT_MASK (DMA_INT_FIFO|DMA_INT_COMPLETE|DMA_INT_LVI)
182 /* interrupts for the whole chip */// by interrupt status register finish
184 #define INT_SPDIFOUT (1<<23) /* controller spdif out INTERRUPT */
185 #define INT_SPDIFIN (1<<22)
186 #define INT_CODECSPDIFOUT (1<<19)
187 #define INT_MICIN (1<<18)
188 #define INT_PCMOUT (1<<17)
189 #define INT_PCMIN (1<<16)
190 #define INT_CPRAIS (1<<7)
191 #define INT_SPRAIS (1<<5)
192 #define INT_GPIO (1<<1)
193 #define INT_MASK (INT_SPDIFOUT|INT_CODECSPDIFOUT|INT_MICIN|INT_PCMOUT|INT_PCMIN)
195 #define DRIVER_VERSION "0.02ac"
197 /* magic numbers to protect our data structures */
198 #define ALI5455_CARD_MAGIC 0x5072696E /* "Prin" */
199 #define ALI5455_STATE_MAGIC 0x63657373 /* "cess" */
200 #define ALI5455_DMA_MASK 0xffffffff /* DMA buffer mask for pci_alloc_consist */
201 #define NR_HW_CH 5 //I think 5 channel
203 /* maxinum number of AC97 codecs connected, AC97 2.0 defined 4 */
206 /* Please note that an 8bit mono stream is not valid on this card, you must have a 16bit */
207 /* stream at a minimum for this card to be happy */
208 static const unsigned sample_size[] = { 1, 2, 2, 4 };
209 /* Samples are 16bit values, so we are shifting to a word, not to a byte, hence shift */
210 /* values are one less than might be expected */
211 static const unsigned sample_shift[] = { -1, 0, 0, 1 };
214 static char *card_names[] = {
218 static struct pci_device_id ali_pci_tbl[] = {
219 {PCI_VENDOR_ID_ALI, PCI_DEVICE_ID_ALI_5455,
220 PCI_ANY_ID, PCI_ANY_ID, 0, 0, ALI5455},
224 MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
227 #define PM_SUSPENDED(card) (card->pm_suspended)
229 #define PM_SUSPENDED(card) (0)
232 /* "software" or virtual channel, an instance of opened /dev/dsp */
235 struct ali_card *card; /* Card info */
237 /* single open lock mechanism, only used for recording */
238 struct semaphore open_sem;
239 wait_queue_head_t open_wait;
244 /* virtual channel number */
248 unsigned int pm_saved_dac_rate, pm_saved_adc_rate;
251 /* wave sample stuff */
253 unsigned char fmt, enable, trigger;
255 /* hardware channel */
256 struct ali_channel *read_channel;
257 struct ali_channel *write_channel;
258 struct ali_channel *codec_spdifout_channel;
259 struct ali_channel *controller_spdifout_channel;
261 /* OSS buffer management stuff */
263 dma_addr_t dma_handle;
268 /* our buffer acts like a circular ring */
269 unsigned hwptr; /* where dma last started, updated by update_ptr */
270 unsigned swptr; /* where driver last clear/filled, updated by read/write */
271 int count; /* bytes to be consumed or been generated by dma machine */
272 unsigned total_bytes; /* total bytes dmaed by hardware */
274 unsigned error; /* number of over/underruns */
275 wait_queue_head_t wait; /* put process on wait queue when no more space in buffer */
277 /* redundant, but makes calculations easier */
278 /* what the hardware uses */
281 unsigned fragsamples;
283 /* what we tell the user to expect */
285 unsigned userfragsize;
290 unsigned update_flag;
291 unsigned ossfragsize;
292 unsigned ossmaxfrags;
293 unsigned subdivision;
299 struct ali_channel channel[5];
302 /* We keep ali5455 cards in a linked list */
303 struct ali_card *next;
305 /* The ali has a certain amount of cross channel interaction
306 so we use a single per card lock */
308 spinlock_t ac97_lock;
310 /* PCI device stuff */
311 struct pci_dev *pci_dev;
315 u32 pm_save_state[64 / sizeof(u32)];
316 int pm_saved_mixer_settings[SOUND_MIXER_NRDEVICES][NR_AC97];
318 /* soundcore stuff */
321 /* structures for abstraction of hardware facilities, codecs, banks and channels */
322 struct ac97_codec *ac97_codec[NR_AC97];
323 struct ali_state *states[NR_HW_CH];
329 /* hardware resources */
330 unsigned long iobase;
334 /* Function support */
335 struct ali_channel *(*alloc_pcm_channel) (struct ali_card *);
336 struct ali_channel *(*alloc_rec_pcm_channel) (struct ali_card *);
337 struct ali_channel *(*alloc_rec_mic_channel) (struct ali_card *);
338 struct ali_channel *(*alloc_codec_spdifout_channel) (struct ali_card *);
339 struct ali_channel *(*alloc_controller_spdifout_channel) (struct ali_card *);
340 void (*free_pcm_channel) (struct ali_card *, int chan);
342 /* We have a *very* long init time possibly, so use this to block */
343 /* attempts to open our devices before we are ready (stops oops'es) */
348 static struct ali_card *devs = NULL;
350 static int ali_open_mixdev(struct inode *inode, struct file *file);
351 static int ali_ioctl_mixdev(struct inode *inode, struct file *file,
352 unsigned int cmd, unsigned long arg);
353 static u16 ali_ac97_get(struct ac97_codec *dev, u8 reg);
354 static void ali_ac97_set(struct ac97_codec *dev, u8 reg, u16 data);
356 static struct ali_channel *ali_alloc_pcm_channel(struct ali_card *card)
358 if (card->channel[1].used == 1)
360 card->channel[1].used = 1;
361 return &card->channel[1];
364 static struct ali_channel *ali_alloc_rec_pcm_channel(struct ali_card *card)
366 if (card->channel[0].used == 1)
368 card->channel[0].used = 1;
369 return &card->channel[0];
372 static struct ali_channel *ali_alloc_rec_mic_channel(struct ali_card *card)
374 if (card->channel[2].used == 1)
376 card->channel[2].used = 1;
377 return &card->channel[2];
380 static struct ali_channel *ali_alloc_codec_spdifout_channel(struct ali_card *card)
382 if (card->channel[3].used == 1)
384 card->channel[3].used = 1;
385 return &card->channel[3];
388 static struct ali_channel *ali_alloc_controller_spdifout_channel(struct ali_card *card)
390 if (card->channel[4].used == 1)
392 card->channel[4].used = 1;
393 return &card->channel[4];
395 static void ali_free_pcm_channel(struct ali_card *card, int channel)
397 card->channel[channel].used = 0;
401 //add support codec spdif out
402 static int ali_valid_spdif_rate(struct ac97_codec *codec, int rate)
404 unsigned long id = 0L;
406 id = (ali_ac97_get(codec, AC97_VENDOR_ID1) << 16);
407 id |= ali_ac97_get(codec, AC97_VENDOR_ID2) & 0xffff;
409 case 0x41445361: /* AD1886 */
414 case 0x414c4720: /* ALC650 */
419 default: /* all other codecs, until we know otherwiae */
420 if (rate == 48000 || rate == 44100 || rate == 32000) {
428 /* ali_set_spdif_output
430 * Configure the S/PDIF output transmitter. When we turn on
431 * S/PDIF, we turn off the analog output. This may not be
432 * the right thing to do.
435 * The DSP sample rate must already be set to a supported
436 * S/PDIF rate (32kHz, 44.1kHz, or 48kHz) or we abort.
438 static void ali_set_spdif_output(struct ali_state *state, int slots,
443 struct ac97_codec *codec = state->card->ac97_codec[0];
445 if (!(state->card->ac97_features & 4)) {
446 state->card->ac97_status &= ~SPDIF_ON;
448 if (slots == -1) { /* Turn off S/PDIF */
449 aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
450 ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
452 /* If the volume wasn't muted before we turned on S/PDIF, unmute it */
453 if (!(state->card->ac97_status & VOL_MUTED)) {
454 aud_reg = ali_ac97_get(codec, AC97_MASTER_VOL_STEREO);
455 ali_ac97_set(codec, AC97_MASTER_VOL_STEREO,
456 (aud_reg & ~VOL_MUTED));
458 state->card->ac97_status &= ~(VOL_MUTED | SPDIF_ON);
462 vol = ali_ac97_get(codec, AC97_MASTER_VOL_STEREO);
463 state->card->ac97_status = vol & VOL_MUTED;
465 /* Set S/PDIF transmitter sample rate */
466 aud_reg = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
469 aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_32K;
472 aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_44K;
475 aud_reg = (aud_reg & AC97_SC_SPSR_MASK) | AC97_SC_SPSR_48K;
478 /* turn off S/PDIF */
479 aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
480 ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
481 state->card->ac97_status &= ~SPDIF_ON;
485 ali_ac97_set(codec, AC97_SPDIF_CONTROL, aud_reg);
487 aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
488 aud_reg = (aud_reg & AC97_EA_SLOT_MASK) | slots | AC97_EA_SPDIF;
489 ali_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
491 aud_reg = ali_ac97_get(codec, AC97_POWER_CONTROL);
493 ali_ac97_set(codec, AC97_POWER_CONTROL, aud_reg);
496 state->card->ac97_status |= SPDIF_ON;
498 /* Check to make sure the configuration is valid */
499 aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
500 if (!(aud_reg & 0x0400)) {
501 /* turn off S/PDIF */
502 ali_ac97_set(codec, AC97_EXTENDED_STATUS, (aud_reg & ~AC97_EA_SPDIF));
503 state->card->ac97_status &= ~SPDIF_ON;
506 if (codec_independent_spdif_locked > 0) {
507 aud_reg = ali_ac97_get(codec, 0x6a);
508 ali_ac97_set(codec, 0x6a, (aud_reg & 0xefff));
510 /* Mute the analog output */
511 /* Should this only mute the PCM volume??? */
515 /* ali_set_dac_channels
517 * Configure the codec's multi-channel DACs
519 * The logic is backwards. Setting the bit to 1 turns off the DAC.
521 * What about the ICH? We currently configure it using the
522 * SNDCTL_DSP_CHANNELS ioctl. If we're turnning on the DAC,
523 * does that imply that we want the ICH set to support
527 * vailidate that the codec really supports these DACs
528 * before turning them on.
530 static void ali_set_dac_channels(struct ali_state *state, int channel)
533 struct ac97_codec *codec = state->card->ac97_codec[0];
535 aud_reg = ali_ac97_get(codec, AC97_EXTENDED_STATUS);
536 aud_reg |= AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK;
537 state->card->ac97_status &= ~(SURR_ON | CENTER_LFE_ON);
540 case 2: /* always enabled */
543 aud_reg &= ~AC97_EA_PRJ;
544 state->card->ac97_status |= SURR_ON;
547 aud_reg &= ~(AC97_EA_PRJ | AC97_EA_PRI | AC97_EA_PRK);
548 state->card->ac97_status |= SURR_ON | CENTER_LFE_ON;
553 ali_ac97_set(codec, AC97_EXTENDED_STATUS, aud_reg);
557 /* set playback sample rate */
558 static unsigned int ali_set_dac_rate(struct ali_state *state,
561 struct dmabuf *dmabuf = &state->dmabuf;
563 struct ac97_codec *codec = state->card->ac97_codec[0];
565 if (!(state->card->ac97_features & 0x0001)) {
566 dmabuf->rate = clocking;
577 * Adjust for misclocked crap
580 rate = (rate * clocking) / 48000;
582 if (strict_clocking && rate < 8000) {
584 dmabuf->rate = (rate * 48000) / clocking;
587 new_rate = ac97_set_dac_rate(codec, rate);
588 if (new_rate != rate) {
589 dmabuf->rate = (new_rate * 48000) / clocking;
595 /* set recording sample rate */
596 static unsigned int ali_set_adc_rate(struct ali_state *state,
599 struct dmabuf *dmabuf = &state->dmabuf;
601 struct ac97_codec *codec = state->card->ac97_codec[0];
603 if (!(state->card->ac97_features & 0x0001)) {
604 dmabuf->rate = clocking;
615 * Adjust for misclocked crap
618 rate = (rate * clocking) / 48000;
619 if (strict_clocking && rate < 8000) {
621 dmabuf->rate = (rate * 48000) / clocking;
624 new_rate = ac97_set_adc_rate(codec, rate);
626 if (new_rate != rate) {
627 dmabuf->rate = (new_rate * 48000) / clocking;
633 /* set codec independent spdifout sample rate */
634 static unsigned int ali_set_codecspdifout_rate(struct ali_state *state,
637 struct dmabuf *dmabuf = &state->dmabuf;
639 if (!(state->card->ac97_features & 0x0001)) {
640 dmabuf->rate = clocking;
653 /* set controller independent spdif out function sample rate */
654 static void ali_set_spdifout_rate(struct ali_state *state,
657 unsigned char ch_st_sel;
658 unsigned short status_rate;
673 ch_st_sel = inb(state->card->iobase + ALI_SPDIFICS) & ALI_SPDIF_OUT_CH_STATUS; //select spdif_out
675 ch_st_sel |= 0x80; //select right
676 outb(ch_st_sel, (state->card->iobase + ALI_SPDIFICS));
677 outb(status_rate | 0x20, (state->card->iobase + ALI_SPDIFCSR + 2));
679 ch_st_sel &= (~0x80); //select left
680 outb(ch_st_sel, (state->card->iobase + ALI_SPDIFICS));
681 outw(status_rate | 0x10, (state->card->iobase + ALI_SPDIFCSR + 2));
684 /* get current playback/recording dma buffer pointer (byte offset from LBA),
685 called with spinlock held! */
687 static inline unsigned ali_get_dma_addr(struct ali_state *state, int rec)
689 struct dmabuf *dmabuf = &state->dmabuf;
690 unsigned int civ, offset, port, port_picb;
697 port = state->card->iobase + dmabuf->read_channel->port;
699 port = state->card->iobase + dmabuf->codec_spdifout_channel->port;
701 port = state->card->iobase + dmabuf->controller_spdifout_channel->port;
703 port = state->card->iobase + dmabuf->write_channel->port;
705 port_picb = port + OFF_PICB;
708 civ = inb(port + OFF_CIV) & 31;
709 offset = inw(port_picb);
710 /* Must have a delay here! */
714 /* Reread both registers and make sure that that total
715 * offset from the first reading to the second is 0.
716 * There is an issue with SiS hardware where it will count
717 * picb down to 0, then update civ to the next value,
718 * then set the new picb to fragsize bytes. We can catch
719 * it between the civ update and the picb update, making
720 * it look as though we are 1 fragsize ahead of where we
721 * are. The next to we get the address though, it will
722 * be back in thdelay is more than long enough
723 * that we won't have to worry about the chip still being
724 * out of sync with reality ;-)
726 } while (civ != (inb(port + OFF_CIV) & 31) || offset != inw(port_picb));
728 data = ((civ + 1) * dmabuf->fragsize - (2 * offset)) % dmabuf->dmasize;
729 if (inw(port_picb) == 0)
735 /* Stop recording (lock held) */
736 static inline void __stop_adc(struct ali_state *state)
738 struct dmabuf *dmabuf = &state->dmabuf;
739 struct ali_card *card = state->card;
741 dmabuf->enable &= ~ADC_RUNNING;
743 outl((1 << 18) | (1 << 16), card->iobase + ALI_DMACR);
746 outb(0, card->iobase + PI_CR);
747 while (inb(card->iobase + PI_CR) != 0);
749 // now clear any latent interrupt bits (like the halt bit)
750 outb(inb(card->iobase + PI_SR) | 0x001e, card->iobase + PI_SR);
751 outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_PCMIN, card->iobase + ALI_INTERRUPTSR);
754 static void stop_adc(struct ali_state *state)
756 struct ali_card *card = state->card;
758 spin_lock_irqsave(&card->lock, flags);
760 spin_unlock_irqrestore(&card->lock, flags);
763 static inline void __start_adc(struct ali_state *state)
765 struct dmabuf *dmabuf = &state->dmabuf;
767 if (dmabuf->count < dmabuf->dmasize && dmabuf->ready
768 && !dmabuf->enable && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
769 dmabuf->enable |= ADC_RUNNING;
770 outb((1 << 4) | (1 << 2), state->card->iobase + PI_CR);
771 if (state->card->channel[0].used == 1)
772 outl(1, state->card->iobase + ALI_DMACR); // DMA CONTROL REGISTRER
774 if (state->card->channel[2].used == 1)
775 outl((1 << 2), state->card->iobase + ALI_DMACR); //DMA CONTROL REGISTER
780 static void start_adc(struct ali_state *state)
782 struct ali_card *card = state->card;
785 spin_lock_irqsave(&card->lock, flags);
787 spin_unlock_irqrestore(&card->lock, flags);
790 /* stop playback (lock held) */
791 static inline void __stop_dac(struct ali_state *state)
793 struct dmabuf *dmabuf = &state->dmabuf;
794 struct ali_card *card = state->card;
796 dmabuf->enable &= ~DAC_RUNNING;
797 outl(0x00020000, card->iobase + 0x08);
798 outb(0, card->iobase + PO_CR);
799 while (inb(card->iobase + PO_CR) != 0)
802 outb(inb(card->iobase + PO_SR) | 0x001e, card->iobase + PO_SR);
804 outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_PCMOUT, card->iobase + ALI_INTERRUPTSR);
807 static void stop_dac(struct ali_state *state)
809 struct ali_card *card = state->card;
811 spin_lock_irqsave(&card->lock, flags);
813 spin_unlock_irqrestore(&card->lock, flags);
816 static inline void __start_dac(struct ali_state *state)
818 struct dmabuf *dmabuf = &state->dmabuf;
819 if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
820 (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
821 dmabuf->enable |= DAC_RUNNING;
822 outb((1 << 4) | (1 << 2), state->card->iobase + PO_CR);
823 outl((1 << 1), state->card->iobase + 0x08); //dma control register
827 static void start_dac(struct ali_state *state)
829 struct ali_card *card = state->card;
831 spin_lock_irqsave(&card->lock, flags);
833 spin_unlock_irqrestore(&card->lock, flags);
836 /* stop codec and controller spdif out (lock held) */
837 static inline void __stop_spdifout(struct ali_state *state)
839 struct dmabuf *dmabuf = &state->dmabuf;
840 struct ali_card *card = state->card;
842 if (codec_independent_spdif_locked > 0) {
843 dmabuf->enable &= ~CODEC_SPDIFOUT_RUNNING;
844 outl((1 << 19), card->iobase + 0x08);
845 outb(0, card->iobase + CODECSPDIFOUT_CR);
847 while (inb(card->iobase + CODECSPDIFOUT_CR) != 0)
850 outb(inb(card->iobase + CODECSPDIFOUT_SR) | 0x001e, card->iobase + CODECSPDIFOUT_SR);
851 outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_CODECSPDIFOUT, card->iobase + ALI_INTERRUPTSR);
853 if (controller_independent_spdif_locked > 0) {
854 dmabuf->enable &= ~CONTROLLER_SPDIFOUT_RUNNING;
855 outl((1 << 23), card->iobase + 0x08);
856 outb(0, card->iobase + CONTROLLERSPDIFOUT_CR);
857 while (inb(card->iobase + CONTROLLERSPDIFOUT_CR) != 0)
859 outb(inb(card->iobase + CONTROLLERSPDIFOUT_SR) | 0x001e, card->iobase + CONTROLLERSPDIFOUT_SR);
860 outl(inl(card->iobase + ALI_INTERRUPTSR) & INT_SPDIFOUT, card->iobase + ALI_INTERRUPTSR);
865 static void stop_spdifout(struct ali_state *state)
867 struct ali_card *card = state->card;
869 spin_lock_irqsave(&card->lock, flags);
870 __stop_spdifout(state);
871 spin_unlock_irqrestore(&card->lock, flags);
874 static inline void __start_spdifout(struct ali_state *state)
876 struct dmabuf *dmabuf = &state->dmabuf;
877 if (dmabuf->count > 0 && dmabuf->ready && !dmabuf->enable &&
878 (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
879 if (codec_independent_spdif_locked > 0) {
880 dmabuf->enable |= CODEC_SPDIFOUT_RUNNING;
881 outb((1 << 4) | (1 << 2), state->card->iobase + CODECSPDIFOUT_CR);
882 outl((1 << 3), state->card->iobase + 0x08); //dma control register
884 if (controller_independent_spdif_locked > 0) {
885 dmabuf->enable |= CONTROLLER_SPDIFOUT_RUNNING;
886 outb((1 << 4) | (1 << 2), state->card->iobase + CONTROLLERSPDIFOUT_CR);
887 outl((1 << 7), state->card->iobase + 0x08); //dma control register
893 static void start_spdifout(struct ali_state *state)
895 struct ali_card *card = state->card;
897 spin_lock_irqsave(&card->lock, flags);
898 __start_spdifout(state);
899 spin_unlock_irqrestore(&card->lock, flags);
902 #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
903 #define DMABUF_MINORDER 1
905 /* allocate DMA buffer, playback , recording,spdif out buffer should be allocated separately */
906 static int alloc_dmabuf(struct ali_state *state)
908 struct dmabuf *dmabuf = &state->dmabuf;
911 struct page *page, *pend;
913 /* If we don't have any oss frag params, then use our default ones */
914 if (dmabuf->ossmaxfrags == 0)
915 dmabuf->ossmaxfrags = 4;
916 if (dmabuf->ossfragsize == 0)
917 dmabuf->ossfragsize = (PAGE_SIZE << DMABUF_DEFAULTORDER) / dmabuf->ossmaxfrags;
918 size = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
920 if (dmabuf->rawbuf && (PAGE_SIZE << dmabuf->buforder) == size)
922 /* alloc enough to satisfy the oss params */
923 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
924 if ((PAGE_SIZE << order) > size)
926 if ((rawbuf = pci_alloc_consistent(state->card->pci_dev,
928 &dmabuf->dma_handle)))
934 dmabuf->ready = dmabuf->mapped = 0;
935 dmabuf->rawbuf = rawbuf;
936 dmabuf->buforder = order;
938 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
939 pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
940 for (page = virt_to_page(rawbuf); page <= pend; page++)
941 SetPageReserved(page);
945 /* free DMA buffer */
946 static void dealloc_dmabuf(struct ali_state *state)
948 struct dmabuf *dmabuf = &state->dmabuf;
949 struct page *page, *pend;
951 if (dmabuf->rawbuf) {
952 /* undo marking the pages as reserved */
953 pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
954 for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
955 ClearPageReserved(page);
956 pci_free_consistent(state->card->pci_dev,
957 PAGE_SIZE << dmabuf->buforder,
958 dmabuf->rawbuf, dmabuf->dma_handle);
960 dmabuf->rawbuf = NULL;
961 dmabuf->mapped = dmabuf->ready = 0;
964 static int prog_dmabuf(struct ali_state *state, unsigned rec)
966 struct dmabuf *dmabuf = &state->dmabuf;
967 struct ali_channel *c = NULL;
974 spin_lock_irqsave(&state->card->lock, flags);
975 if (dmabuf->enable & DAC_RUNNING)
977 if (dmabuf->enable & ADC_RUNNING)
979 if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
980 __stop_spdifout(state);
981 if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
982 __stop_spdifout(state);
984 dmabuf->total_bytes = 0;
985 dmabuf->count = dmabuf->error = 0;
986 dmabuf->swptr = dmabuf->hwptr = 0;
987 spin_unlock_irqrestore(&state->card->lock, flags);
989 /* allocate DMA buffer, let alloc_dmabuf determine if we are already
990 * allocated well enough or if we should replace the current buffer
991 * (assuming one is already allocated, if it isn't, then allocate it).
993 if ((ret = alloc_dmabuf(state)))
996 /* FIXME: figure out all this OSS fragment stuff */
997 /* I did, it now does what it should according to the OSS API. DL */
998 /* We may not have realloced our dmabuf, but the fragment size to
999 * fragment number ratio may have changed, so go ahead and reprogram
1003 dmabuf->dmasize = PAGE_SIZE << dmabuf->buforder;
1004 dmabuf->numfrag = SG_LEN;
1005 dmabuf->fragsize = dmabuf->dmasize / dmabuf->numfrag;
1006 dmabuf->fragsamples = dmabuf->fragsize >> 1;
1007 dmabuf->userfragsize = dmabuf->ossfragsize;
1008 dmabuf->userfrags = dmabuf->dmasize / dmabuf->ossfragsize;
1010 memset(dmabuf->rawbuf, 0, dmabuf->dmasize);
1012 if (dmabuf->ossmaxfrags == 4) {
1014 dmabuf->fragshift = 2;
1015 } else if (dmabuf->ossmaxfrags == 8) {
1017 dmabuf->fragshift = 3;
1018 } else if (dmabuf->ossmaxfrags == 16) {
1020 dmabuf->fragshift = 4;
1023 dmabuf->fragshift = 5;
1026 * Now set up the ring
1030 c = dmabuf->read_channel;
1032 c = dmabuf->codec_spdifout_channel;
1034 c = dmabuf->controller_spdifout_channel;
1036 c = dmabuf->write_channel;
1040 * Load up 32 sg entries and take an interrupt at half
1041 * way (we might want more interrupts later..)
1043 for (i = 0; i < dmabuf->numfrag; i++) {
1045 virt_to_bus(dmabuf->rawbuf +
1046 dmabuf->fragsize * i);
1047 // the card will always be doing 16bit stereo
1048 sg->control = dmabuf->fragsamples;
1049 sg->control |= CON_BUFPAD; //I modify
1050 // set us up to get IOC interrupts as often as needed to
1051 // satisfy numfrag requirements, no more
1052 if (((i + 1) % fragint) == 0) {
1053 sg->control |= CON_IOC;
1057 spin_lock_irqsave(&state->card->lock, flags);
1058 outb(2, state->card->iobase + c->port + OFF_CR); /* reset DMA machine */
1059 outl(virt_to_bus(&c->sg[0]), state->card->iobase + c->port + OFF_BDBAR);
1060 outb(0, state->card->iobase + c->port + OFF_CIV);
1061 outb(0, state->card->iobase + c->port + OFF_LVI);
1062 spin_unlock_irqrestore(&state->card->lock, flags);
1064 /* set the ready flag for the dma buffer */
1069 static void __ali_update_lvi(struct ali_state *state, int rec)
1071 struct dmabuf *dmabuf = &state->dmabuf;
1073 port = state->card->iobase;
1075 port += dmabuf->read_channel->port;
1077 port += dmabuf->codec_spdifout_channel->port;
1079 port += dmabuf->controller_spdifout_channel->port;
1081 port += dmabuf->write_channel->port;
1082 /* if we are currently stopped, then our CIV is actually set to our
1083 * *last* sg segment and we are ready to wrap to the next. However,
1084 * if we set our LVI to the last sg segment, then it won't wrap to
1085 * the next sg segment, it won't even get a start. So, instead, when
1086 * we are stopped, we set both the LVI value and also we increment
1087 * the CIV value to the next sg segment to be played so that when
1088 * we call start_{dac,adc}, things will operate properly
1090 if (!dmabuf->enable && dmabuf->ready) {
1091 if (rec && dmabuf->count < dmabuf->dmasize && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
1092 outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1094 while (! (inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1096 } else if (!rec && dmabuf->count && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
1097 outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1099 while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1101 } else if (rec && dmabuf->count && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
1102 if (codec_independent_spdif_locked > 0) {
1103 // outb((inb(port+OFF_CIV))&31, port+OFF_LVI);
1104 outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1105 __start_spdifout(state);
1106 while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1109 if (controller_independent_spdif_locked > 0) {
1110 outb((inb(port + OFF_CIV) + 1) & 31, port + OFF_LVI);
1111 __start_spdifout(state);
1112 while (!(inb(port + OFF_CR) & ((1 << 4) | (1 << 2))))
1119 /* swptr - 1 is the tail of our transfer */
1120 x = (dmabuf->dmasize + dmabuf->swptr - 1) % dmabuf->dmasize;
1121 x /= dmabuf->fragsize;
1122 outb(x, port + OFF_LVI);
1125 static void ali_update_lvi(struct ali_state *state, int rec)
1127 struct dmabuf *dmabuf = &state->dmabuf;
1128 unsigned long flags;
1131 spin_lock_irqsave(&state->card->lock, flags);
1132 __ali_update_lvi(state, rec);
1133 spin_unlock_irqrestore(&state->card->lock, flags);
1136 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1137 static void ali_update_ptr(struct ali_state *state)
1139 struct dmabuf *dmabuf = &state->dmabuf;
1143 /* error handling and process wake up for DAC */
1144 if (dmabuf->enable == ADC_RUNNING) {
1145 /* update hardware pointer */
1146 hwptr = ali_get_dma_addr(state, 1);
1147 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1148 dmabuf->hwptr = hwptr;
1149 dmabuf->total_bytes += diff;
1150 dmabuf->count += diff;
1151 if (dmabuf->count > dmabuf->dmasize) {
1152 /* buffer underrun or buffer overrun */
1153 /* this is normal for the end of a read */
1154 /* only give an error if we went past the */
1155 /* last valid sg entry */
1156 if ((inb(state->card->iobase + PI_CIV) & 31) != (inb(state->card->iobase + PI_LVI) & 31)) {
1157 printk(KERN_WARNING "ali_audio: DMA overrun on read\n");
1161 if (dmabuf->count > dmabuf->userfragsize)
1162 wake_up(&dmabuf->wait);
1164 /* error handling and process wake up for DAC */
1165 if (dmabuf->enable == DAC_RUNNING) {
1166 /* update hardware pointer */
1167 hwptr = ali_get_dma_addr(state, 0);
1169 (dmabuf->dmasize + hwptr -
1170 dmabuf->hwptr) % dmabuf->dmasize;
1171 #if defined(DEBUG_INTERRUPTS) || defined(DEBUG_MMAP)
1172 printk("DAC HWP %d,%d,%d\n", hwptr, dmabuf->hwptr, diff);
1174 dmabuf->hwptr = hwptr;
1175 dmabuf->total_bytes += diff;
1176 dmabuf->count -= diff;
1177 if (dmabuf->count < 0) {
1178 /* buffer underrun or buffer overrun */
1179 /* this is normal for the end of a write */
1180 /* only give an error if we went past the */
1181 /* last valid sg entry */
1182 if ((inb(state->card->iobase + PO_CIV) & 31) != (inb(state->card->iobase + PO_LVI) & 31)) {
1183 printk(KERN_WARNING "ali_audio: DMA overrun on write\n");
1184 printk(KERN_DEBUG "ali_audio: CIV %d, LVI %d, hwptr %x, count %d\n",
1185 inb(state->card->iobase + PO_CIV) & 31,
1186 inb(state->card->iobase + PO_LVI) & 31,
1192 if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
1193 wake_up(&dmabuf->wait);
1196 /* error handling and process wake up for CODEC SPDIF OUT */
1197 if (dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
1198 /* update hardware pointer */
1199 hwptr = ali_get_dma_addr(state, 2);
1200 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1201 dmabuf->hwptr = hwptr;
1202 dmabuf->total_bytes += diff;
1203 dmabuf->count -= diff;
1204 if (dmabuf->count < 0) {
1205 /* buffer underrun or buffer overrun */
1206 /* this is normal for the end of a write */
1207 /* only give an error if we went past the */
1208 /* last valid sg entry */
1209 if ((inb(state->card->iobase + CODECSPDIFOUT_CIV) & 31) != (inb(state->card->iobase + CODECSPDIFOUT_LVI) & 31)) {
1210 printk(KERN_WARNING "ali_audio: DMA overrun on write\n");
1211 printk(KERN_DEBUG "ali_audio: CIV %d, LVI %d, hwptr %x, count %d\n",
1212 inb(state->card->iobase + CODECSPDIFOUT_CIV) & 31,
1213 inb(state->card->iobase + CODECSPDIFOUT_LVI) & 31,
1214 dmabuf->hwptr, dmabuf->count);
1218 if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
1219 wake_up(&dmabuf->wait);
1221 /* error handling and process wake up for CONTROLLER SPDIF OUT */
1222 if (dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
1223 /* update hardware pointer */
1224 hwptr = ali_get_dma_addr(state, 3);
1225 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1226 dmabuf->hwptr = hwptr;
1227 dmabuf->total_bytes += diff;
1228 dmabuf->count -= diff;
1229 if (dmabuf->count < 0) {
1230 /* buffer underrun or buffer overrun */
1231 /* this is normal for the end of a write */
1232 /* only give an error if we went past the */
1233 /* last valid sg entry */
1234 if ((inb(state->card->iobase + CONTROLLERSPDIFOUT_CIV) & 31) != (inb(state->card->iobase + CONTROLLERSPDIFOUT_LVI) & 31)) {
1236 "ali_audio: DMA overrun on write\n");
1237 printk("ali_audio: CIV %d, LVI %d, hwptr %x, "
1239 inb(state->card->iobase + CONTROLLERSPDIFOUT_CIV) & 31,
1240 inb(state->card->iobase + CONTROLLERSPDIFOUT_LVI) & 31,
1241 dmabuf->hwptr, dmabuf->count);
1245 if (dmabuf->count < (dmabuf->dmasize - dmabuf->userfragsize))
1246 wake_up(&dmabuf->wait);
1250 static inline int ali_get_free_write_space(struct
1254 struct dmabuf *dmabuf = &state->dmabuf;
1257 if (dmabuf->count < 0) {
1259 dmabuf->swptr = dmabuf->hwptr;
1261 free = dmabuf->dmasize - dmabuf->swptr;
1262 if ((dmabuf->count + free) > dmabuf->dmasize){
1263 free = dmabuf->dmasize - dmabuf->count;
1268 static inline int ali_get_available_read_data(struct
1272 struct dmabuf *dmabuf = &state->dmabuf;
1274 ali_update_ptr(state);
1275 // catch overruns during record
1276 if (dmabuf->count > dmabuf->dmasize) {
1277 dmabuf->count = dmabuf->dmasize;
1278 dmabuf->swptr = dmabuf->hwptr;
1280 avail = dmabuf->count;
1281 avail -= (dmabuf->hwptr % dmabuf->fragsize);
1287 static int drain_dac(struct ali_state *state, int signals_allowed)
1290 DECLARE_WAITQUEUE(wait, current);
1291 struct dmabuf *dmabuf = &state->dmabuf;
1292 unsigned long flags;
1297 if (dmabuf->mapped) {
1301 add_wait_queue(&dmabuf->wait, &wait);
1304 spin_lock_irqsave(&state->card->lock, flags);
1305 ali_update_ptr(state);
1306 count = dmabuf->count;
1307 spin_unlock_irqrestore(&state->card->lock, flags);
1311 * This will make sure that our LVI is correct, that our
1312 * pointer is updated, and that the DAC is running. We
1313 * have to force the setting of dmabuf->trigger to avoid
1314 * any possible deadlocks.
1316 if (!dmabuf->enable) {
1317 dmabuf->trigger = PCM_ENABLE_OUTPUT;
1318 ali_update_lvi(state, 0);
1320 if (signal_pending(current) && signals_allowed) {
1324 /* It seems that we have to set the current state to
1325 * TASK_INTERRUPTIBLE every time to make the process
1326 * really go to sleep. This also has to be *after* the
1327 * update_ptr() call because update_ptr is likely to
1328 * do a wake_up() which will unset this before we ever
1329 * try to sleep, resuling in a tight loop in this code
1330 * instead of actually sleeping and waiting for an
1331 * interrupt to wake us up!
1333 set_current_state(TASK_INTERRUPTIBLE);
1335 * set the timeout to significantly longer than it *should*
1336 * take for the DAC to drain the DMA buffer
1338 tmo = (count * HZ) / (dmabuf->rate);
1339 if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1340 printk(KERN_ERR "ali_audio: drain_dac, dma timeout?\n");
1345 set_current_state(TASK_RUNNING);
1346 remove_wait_queue(&dmabuf->wait, &wait);
1347 if (count > 0 && signal_pending(current) && signals_allowed)
1348 return -ERESTARTSYS;
1354 static int drain_spdifout(struct ali_state *state, int signals_allowed)
1357 DECLARE_WAITQUEUE(wait, current);
1358 struct dmabuf *dmabuf = &state->dmabuf;
1359 unsigned long flags;
1364 if (dmabuf->mapped) {
1365 stop_spdifout(state);
1368 add_wait_queue(&dmabuf->wait, &wait);
1371 spin_lock_irqsave(&state->card->lock, flags);
1372 ali_update_ptr(state);
1373 count = dmabuf->count;
1374 spin_unlock_irqrestore(&state->card->lock, flags);
1378 * This will make sure that our LVI is correct, that our
1379 * pointer is updated, and that the DAC is running. We
1380 * have to force the setting of dmabuf->trigger to avoid
1381 * any possible deadlocks.
1383 if (!dmabuf->enable) {
1384 if (codec_independent_spdif_locked > 0) {
1385 dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1386 ali_update_lvi(state, 2);
1388 if (controller_independent_spdif_locked > 0) {
1389 dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1390 ali_update_lvi(state, 3);
1394 if (signal_pending(current) && signals_allowed) {
1398 /* It seems that we have to set the current state to
1399 * TASK_INTERRUPTIBLE every time to make the process
1400 * really go to sleep. This also has to be *after* the
1401 * update_ptr() call because update_ptr is likely to
1402 * do a wake_up() which will unset this before we ever
1403 * try to sleep, resuling in a tight loop in this code
1404 * instead of actually sleeping and waiting for an
1405 * interrupt to wake us up!
1407 set_current_state(TASK_INTERRUPTIBLE);
1409 * set the timeout to significantly longer than it *should*
1410 * take for the DAC to drain the DMA buffer
1412 tmo = (count * HZ) / (dmabuf->rate);
1413 if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1414 printk(KERN_ERR "ali_audio: drain_spdifout, dma timeout?\n");
1419 set_current_state(TASK_RUNNING);
1420 remove_wait_queue(&dmabuf->wait, &wait);
1421 if (count > 0 && signal_pending(current) && signals_allowed)
1422 return -ERESTARTSYS;
1423 stop_spdifout(state);
1427 static void ali_channel_interrupt(struct ali_card *card)
1431 for (i = 0; i < NR_HW_CH; i++) {
1432 struct ali_state *state = card->states[i];
1433 struct ali_channel *c = NULL;
1434 struct dmabuf *dmabuf;
1435 unsigned long port = card->iobase;
1439 if (!state->dmabuf.ready)
1441 dmabuf = &state->dmabuf;
1442 if (codec_independent_spdif_locked > 0) {
1443 if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
1444 c = dmabuf->codec_spdifout_channel;
1447 if (controller_independent_spdif_locked > 0) {
1448 if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1449 c = dmabuf->controller_spdifout_channel;
1451 if (dmabuf->enable & DAC_RUNNING) {
1452 c = dmabuf->write_channel;
1453 } else if (dmabuf->enable & ADC_RUNNING) {
1454 c = dmabuf->read_channel;
1461 status = inw(port + OFF_SR);
1463 if (status & DMA_INT_COMPLETE) {
1464 /* only wake_up() waiters if this interrupt signals
1465 * us being beyond a userfragsize of data open or
1466 * available, and ali_update_ptr() does that for
1469 ali_update_ptr(state);
1472 if (status & DMA_INT_LVI) {
1473 ali_update_ptr(state);
1474 wake_up(&dmabuf->wait);
1476 if (dmabuf->enable & DAC_RUNNING)
1477 count = dmabuf->count;
1478 else if (dmabuf->enable & ADC_RUNNING)
1479 count = dmabuf->dmasize - dmabuf->count;
1480 else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1481 count = dmabuf->count;
1482 else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1483 count = dmabuf->count;
1487 if (dmabuf->enable & DAC_RUNNING)
1488 outl((1 << 1), state->card->iobase + ALI_DMACR);
1489 else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1490 outl((1 << 3), state->card->iobase + ALI_DMACR);
1491 else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1492 outl((1 << 7), state->card->iobase + ALI_DMACR);
1494 if (dmabuf->enable & DAC_RUNNING)
1496 if (dmabuf->enable & ADC_RUNNING)
1498 if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1499 __stop_spdifout(state);
1500 if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1501 __stop_spdifout(state);
1503 wake_up(&dmabuf->wait);
1507 if (!(status & DMA_INT_DCH)) {
1508 ali_update_ptr(state);
1509 wake_up(&dmabuf->wait);
1510 if (dmabuf->enable & DAC_RUNNING)
1511 count = dmabuf->count;
1512 else if (dmabuf->enable & ADC_RUNNING)
1513 count = dmabuf->dmasize - dmabuf->count;
1514 else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1515 count = dmabuf->count;
1516 else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1517 count = dmabuf->count;
1522 if (dmabuf->enable & DAC_RUNNING)
1523 outl((1 << 1), state->card->iobase + ALI_DMACR);
1524 else if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1525 outl((1 << 3), state->card->iobase + ALI_DMACR);
1526 else if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1527 outl((1 << 7), state->card->iobase + ALI_DMACR);
1529 if (dmabuf->enable & DAC_RUNNING)
1531 if (dmabuf->enable & ADC_RUNNING)
1533 if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING)
1534 __stop_spdifout(state);
1535 if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)
1536 __stop_spdifout(state);
1538 wake_up(&dmabuf->wait);
1541 outw(status & DMA_INT_MASK, port + OFF_SR);
1545 static irqreturn_t ali_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1547 struct ali_card *card = (struct ali_card *) dev_id;
1551 spin_lock(&card->lock);
1552 status = inl(card->iobase + ALI_INTERRUPTSR);
1553 if (!(status & INT_MASK)) {
1554 spin_unlock(&card->lock);
1555 return IRQ_NONE; /* not for us */
1558 if (codec_independent_spdif_locked > 0) {
1561 status2 = inw(card->iobase + 0x76);
1562 outw(status2 | 0x000c, card->iobase + 0x76);
1564 if (status & (INT_PCMOUT | INT_PCMIN | INT_MICIN | INT_SPDIFOUT | INT_CODECSPDIFOUT))
1565 ali_channel_interrupt(card);
1568 if (status & (INT_PCMOUT | INT_PCMIN | INT_MICIN | INT_SPDIFOUT | INT_CODECSPDIFOUT))
1569 ali_channel_interrupt(card);
1573 outl(status & INT_MASK, card->iobase + ALI_INTERRUPTSR);
1574 spin_unlock(&card->lock);
1578 /* in this loop, dmabuf.count signifies the amount of data that is
1579 waiting to be copied to the user's buffer. It is filled by the dma
1580 machine and drained by this loop. */
1582 static ssize_t ali_read(struct file *file, char __user *buffer,
1583 size_t count, loff_t * ppos)
1585 struct ali_state *state = (struct ali_state *) file->private_data;
1586 struct ali_card *card = state ? state->card : 0;
1587 struct dmabuf *dmabuf = &state->dmabuf;
1589 unsigned long flags;
1592 DECLARE_WAITQUEUE(waita, current);
1594 printk("ali_audio: ali_read called, count = %d\n", count);
1596 if (ppos != &file->f_pos)
1600 if (dmabuf->enable & DAC_RUNNING)
1602 if (!dmabuf->read_channel) {
1604 dmabuf->read_channel = card->alloc_rec_pcm_channel(card);
1605 if (!dmabuf->read_channel) {
1609 if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
1611 if (!access_ok(VERIFY_WRITE, buffer, count))
1614 add_wait_queue(&dmabuf->wait, &waita);
1616 set_current_state(TASK_INTERRUPTIBLE);
1617 spin_lock_irqsave(&card->lock, flags);
1618 if (PM_SUSPENDED(card)) {
1619 spin_unlock_irqrestore(&card->lock, flags);
1621 if (signal_pending(current)) {
1628 swptr = dmabuf->swptr;
1629 cnt = ali_get_available_read_data(state);
1630 // this is to make the copy_to_user simpler below
1631 if (cnt > (dmabuf->dmasize - swptr))
1632 cnt = dmabuf->dmasize - swptr;
1633 spin_unlock_irqrestore(&card->lock, flags);
1636 /* Lop off the last two bits to force the code to always
1637 * write in full samples. This keeps software that sets
1638 * O_NONBLOCK but doesn't check the return value of the
1639 * write call from getting things out of state where they
1640 * think a full 4 byte sample was written when really only
1641 * a portion was, resulting in odd sound and stereo
1648 * Don't let us deadlock. The ADC won't start if
1649 * dmabuf->trigger isn't set. A call to SETTRIGGER
1650 * could have turned it off after we set it to on
1653 dmabuf->trigger = PCM_ENABLE_INPUT;
1655 * This does three things. Updates LVI to be correct,
1656 * makes sure the ADC is running, and updates the
1659 ali_update_lvi(state, 1);
1660 if (file->f_flags & O_NONBLOCK) {
1665 /* Set the timeout to how long it would take to fill
1666 * two of our buffers. If we haven't been woke up
1667 * by then, then we know something is wrong.
1669 tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
1671 /* There are two situations when sleep_on_timeout returns, one is when
1672 the interrupt is serviced correctly and the process is waked up by
1673 ISR ON TIME. Another is when timeout is expired, which means that
1674 either interrupt is NOT serviced correctly (pending interrupt) or it
1675 is TOO LATE for the process to be scheduled to run (scheduler latency)
1676 which results in a (potential) buffer overrun. And worse, there is
1677 NOTHING we can do to prevent it. */
1678 if (!schedule_timeout(tmo >= 2 ? tmo : 2)) {
1680 "ali_audio: recording schedule timeout, "
1681 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1682 dmabuf->dmasize, dmabuf->fragsize,
1683 dmabuf->count, dmabuf->hwptr,
1685 /* a buffer overrun, we delay the recovery until next time the
1686 while loop begin and we REALLY have space to record */
1688 if (signal_pending(current)) {
1689 ret = ret ? ret : -ERESTARTSYS;
1695 if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1701 swptr = (swptr + cnt) % dmabuf->dmasize;
1702 spin_lock_irqsave(&card->lock, flags);
1703 if (PM_SUSPENDED(card)) {
1704 spin_unlock_irqrestore(&card->lock, flags);
1707 dmabuf->swptr = swptr;
1708 dmabuf->count -= cnt;
1709 spin_unlock_irqrestore(&card->lock, flags);
1715 ali_update_lvi(state, 1);
1716 set_current_state(TASK_RUNNING);
1717 remove_wait_queue(&dmabuf->wait, &waita);
1721 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
1722 the soundcard. it is drained by the dma machine and filled by this loop. */
1723 static ssize_t ali_write(struct file *file,
1724 const char __user *buffer, size_t count, loff_t * ppos)
1726 struct ali_state *state = (struct ali_state *) file->private_data;
1727 struct ali_card *card = state ? state->card : 0;
1728 struct dmabuf *dmabuf = &state->dmabuf;
1730 unsigned long flags;
1731 unsigned int swptr = 0;
1733 DECLARE_WAITQUEUE(waita, current);
1735 printk("ali_audio: ali_write called, count = %d\n", count);
1737 if (ppos != &file->f_pos)
1741 if (dmabuf->enable & ADC_RUNNING)
1743 if (codec_independent_spdif_locked > 0) {
1744 if (!dmabuf->codec_spdifout_channel) {
1746 dmabuf->codec_spdifout_channel = card->alloc_codec_spdifout_channel(card);
1747 if (!dmabuf->codec_spdifout_channel)
1751 if (controller_independent_spdif_locked > 0) {
1752 if (!dmabuf->controller_spdifout_channel) {
1754 dmabuf->controller_spdifout_channel = card->alloc_controller_spdifout_channel(card);
1755 if (!dmabuf->controller_spdifout_channel)
1759 if (!dmabuf->write_channel) {
1761 dmabuf->write_channel =
1762 card->alloc_pcm_channel(card);
1763 if (!dmabuf->write_channel)
1769 if (codec_independent_spdif_locked > 0) {
1770 if (!dmabuf->ready && (ret = prog_dmabuf(state, 2)))
1773 if (controller_independent_spdif_locked > 0) {
1774 if (!dmabuf->ready && (ret = prog_dmabuf(state, 3)))
1778 if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
1782 if (!access_ok(VERIFY_READ, buffer, count))
1785 add_wait_queue(&dmabuf->wait, &waita);
1787 set_current_state(TASK_INTERRUPTIBLE);
1788 spin_lock_irqsave(&state->card->lock, flags);
1789 if (PM_SUSPENDED(card)) {
1790 spin_unlock_irqrestore(&card->lock, flags);
1792 if (signal_pending(current)) {
1800 swptr = dmabuf->swptr;
1801 cnt = ali_get_free_write_space(state);
1802 /* Bound the maximum size to how much we can copy to the
1803 * dma buffer before we hit the end. If we have more to
1804 * copy then it will get done in a second pass of this
1805 * loop starting from the beginning of the buffer.
1807 if (cnt > (dmabuf->dmasize - swptr))
1808 cnt = dmabuf->dmasize - swptr;
1809 spin_unlock_irqrestore(&state->card->lock, flags);
1812 "ali_audio: ali_write: %d bytes available space\n",
1817 /* Lop off the last two bits to force the code to always
1818 * write in full samples. This keeps software that sets
1819 * O_NONBLOCK but doesn't check the return value of the
1820 * write call from getting things out of state where they
1821 * think a full 4 byte sample was written when really only
1822 * a portion was, resulting in odd sound and stereo
1828 // There is data waiting to be played
1830 * Force the trigger setting since we would
1831 * deadlock with it set any other way
1833 if (codec_independent_spdif_locked > 0) {
1834 dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1835 ali_update_lvi(state, 2);
1837 if (controller_independent_spdif_locked > 0) {
1838 dmabuf->trigger = SPDIF_ENABLE_OUTPUT;
1839 ali_update_lvi(state, 3);
1842 dmabuf->trigger = PCM_ENABLE_OUTPUT;
1843 ali_update_lvi(state, 0);
1846 if (file->f_flags & O_NONBLOCK) {
1851 /* Not strictly correct but works */
1852 tmo = (dmabuf->dmasize * HZ * 2) / (dmabuf->rate * 4);
1853 /* There are two situations when sleep_on_timeout returns, one is when
1854 the interrupt is serviced correctly and the process is waked up by
1855 ISR ON TIME. Another is when timeout is expired, which means that
1856 either interrupt is NOT serviced correctly (pending interrupt) or it
1857 is TOO LATE for the process to be scheduled to run (scheduler latency)
1858 which results in a (potential) buffer underrun. And worse, there is
1859 NOTHING we can do to prevent it. */
1861 /* FIXME - do timeout handling here !! */
1862 schedule_timeout(tmo >= 2 ? tmo : 2);
1864 if (signal_pending(current)) {
1871 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, cnt)) {
1877 swptr = (swptr + cnt) % dmabuf->dmasize;
1878 spin_lock_irqsave(&state->card->lock, flags);
1879 if (PM_SUSPENDED(card)) {
1880 spin_unlock_irqrestore(&card->lock, flags);
1884 dmabuf->swptr = swptr;
1885 dmabuf->count += cnt;
1889 spin_unlock_irqrestore(&state->card->lock, flags);
1891 if (swptr % dmabuf->fragsize) {
1892 x = dmabuf->fragsize - (swptr % dmabuf->fragsize);
1893 memset(dmabuf->rawbuf + swptr, '\0', x);
1896 if (codec_independent_spdif_locked > 0) {
1897 ali_update_lvi(state, 2);
1899 if (controller_independent_spdif_locked > 0) {
1900 ali_update_lvi(state, 3);
1902 ali_update_lvi(state, 0);
1905 set_current_state(TASK_RUNNING);
1906 remove_wait_queue(&dmabuf->wait, &waita);
1910 /* No kernel lock - we have our own spinlock */
1911 static unsigned int ali_poll(struct file *file, struct poll_table_struct
1914 struct ali_state *state = (struct ali_state *) file->private_data;
1915 struct dmabuf *dmabuf = &state->dmabuf;
1916 unsigned long flags;
1917 unsigned int mask = 0;
1920 poll_wait(file, &dmabuf->wait, wait);
1921 spin_lock_irqsave(&state->card->lock, flags);
1922 ali_update_ptr(state);
1923 if (file->f_mode & FMODE_READ && dmabuf->enable & ADC_RUNNING) {
1924 if (dmabuf->count >= (signed) dmabuf->fragsize)
1925 mask |= POLLIN | POLLRDNORM;
1927 if (file->f_mode & FMODE_WRITE && (dmabuf->enable & (DAC_RUNNING|CODEC_SPDIFOUT_RUNNING|CONTROLLER_SPDIFOUT_RUNNING))) {
1928 if ((signed) dmabuf->dmasize >= dmabuf->count + (signed) dmabuf->fragsize)
1929 mask |= POLLOUT | POLLWRNORM;
1931 spin_unlock_irqrestore(&state->card->lock, flags);
1935 static int ali_mmap(struct file *file, struct vm_area_struct *vma)
1937 struct ali_state *state = (struct ali_state *) file->private_data;
1938 struct dmabuf *dmabuf = &state->dmabuf;
1942 if (vma->vm_flags & VM_WRITE) {
1943 if (!dmabuf->write_channel && (dmabuf->write_channel = state->card->alloc_pcm_channel(state->card)) == NULL) {
1948 if (vma->vm_flags & VM_READ) {
1949 if (!dmabuf->read_channel && (dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card)) == NULL) {
1954 if ((ret = prog_dmabuf(state, 0)) != 0)
1957 if (vma->vm_pgoff != 0)
1959 size = vma->vm_end - vma->vm_start;
1960 if (size > (PAGE_SIZE << dmabuf->buforder))
1963 if (remap_page_range(vma, vma->vm_start, virt_to_phys(dmabuf->rawbuf), size, vma->vm_page_prot))
1966 dmabuf->trigger = 0;
1973 static int ali_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1975 struct ali_state *state = (struct ali_state *) file->private_data;
1976 struct ali_channel *c = NULL;
1977 struct dmabuf *dmabuf = &state->dmabuf;
1978 unsigned long flags;
1979 audio_buf_info abinfo;
1983 struct ac97_codec *codec = state->card->ac97_codec[0];
1984 void __user *argp = (void __user *)arg;
1985 int __user *p = argp;
1988 printk("ali_audio: ali_ioctl, arg=0x%x, cmd=",
1992 case OSS_GETVERSION:
1994 printk("OSS_GETVERSION\n");
1996 return put_user(SOUND_VERSION, p);
1997 case SNDCTL_DSP_RESET:
1999 printk("SNDCTL_DSP_RESET\n");
2001 spin_lock_irqsave(&state->card->lock, flags);
2002 if (dmabuf->enable == DAC_RUNNING) {
2003 c = dmabuf->write_channel;
2006 if (dmabuf->enable == ADC_RUNNING) {
2007 c = dmabuf->read_channel;
2010 if (dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
2011 c = dmabuf->codec_spdifout_channel;
2012 __stop_spdifout(state);
2014 if (dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
2015 c = dmabuf->controller_spdifout_channel;
2016 __stop_spdifout(state);
2019 outb(2, state->card->iobase + c->port + OFF_CR); /* reset DMA machine */
2020 outl(virt_to_bus(&c->sg[0]),
2021 state->card->iobase + c->port + OFF_BDBAR);
2022 outb(0, state->card->iobase + c->port + OFF_CIV);
2023 outb(0, state->card->iobase + c->port + OFF_LVI);
2026 spin_unlock_irqrestore(&state->card->lock, flags);
2027 synchronize_irq(state->card->pci_dev->irq);
2029 dmabuf->swptr = dmabuf->hwptr = 0;
2030 dmabuf->count = dmabuf->total_bytes = 0;
2032 case SNDCTL_DSP_SYNC:
2034 printk("SNDCTL_DSP_SYNC\n");
2036 if (codec_independent_spdif_locked > 0) {
2037 if (dmabuf->enable != CODEC_SPDIFOUT_RUNNING
2038 || file->f_flags & O_NONBLOCK)
2040 if ((val = drain_spdifout(state, 1)))
2043 if (controller_independent_spdif_locked > 0) {
2044 if (dmabuf->enable !=
2045 CONTROLLER_SPDIFOUT_RUNNING
2046 || file->f_flags & O_NONBLOCK)
2048 if ((val = drain_spdifout(state, 1)))
2051 if (dmabuf->enable != DAC_RUNNING
2052 || file->f_flags & O_NONBLOCK)
2054 if ((val = drain_dac(state, 1)))
2058 dmabuf->total_bytes = 0;
2060 case SNDCTL_DSP_SPEED: /* set smaple rate */
2062 printk("SNDCTL_DSP_SPEED\n");
2064 if (get_user(val, p))
2067 if (file->f_mode & FMODE_WRITE) {
2068 if ((state->card->ac97_status & SPDIF_ON)) { /* S/PDIF Enabled */
2069 /* RELTEK ALC650 only support 48000, need to check that */
2070 if (ali_valid_spdif_rate(codec, val)) {
2071 if (codec_independent_spdif_locked > 0) {
2072 ali_set_spdif_output(state, -1, 0);
2073 stop_spdifout(state);
2075 /* I add test codec independent spdif out */
2076 spin_lock_irqsave(&state->card->lock, flags);
2077 ali_set_codecspdifout_rate(state, val); // I modified
2078 spin_unlock_irqrestore(&state->card->lock, flags);
2079 /* Set S/PDIF transmitter rate. */
2080 i_scr = inl(state->card->iobase + ALI_SCR);
2081 if ((i_scr & 0x00300000) == 0x00100000) {
2082 ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2084 if ((i_scr&0x00300000) == 0x00200000)
2086 ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
2088 if ((i_scr & 0x00300000) == 0x00300000) {
2089 ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
2091 ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2096 if (!(state->card->ac97_status & SPDIF_ON)) {
2100 if (controller_independent_spdif_locked > 0)
2102 stop_spdifout(state);
2104 spin_lock_irqsave(&state->card->lock, flags);
2105 ali_set_spdifout_rate(state, controller_independent_spdif_locked);
2106 spin_unlock_irqrestore(&state->card->lock, flags);
2109 ali_set_spdif_output(state, -1, 0);
2112 spin_lock_irqsave(&state->card->lock, flags);
2113 ali_set_dac_rate(state, val);
2114 spin_unlock_irqrestore(&state->card->lock, flags);
2115 /* Set S/PDIF transmitter rate. */
2116 ali_set_spdif_output(state, AC97_EA_SPSA_3_4, val);
2117 if (!(state->card->ac97_status & SPDIF_ON))
2123 } else { /* Not a valid rate for S/PDIF, ignore it */
2129 spin_lock_irqsave(&state->card->lock, flags);
2130 ali_set_dac_rate(state, val);
2131 spin_unlock_irqrestore(&state->card->lock, flags);
2134 if (file->f_mode & FMODE_READ) {
2137 spin_lock_irqsave(&state->card->lock, flags);
2138 ali_set_adc_rate(state, val);
2139 spin_unlock_irqrestore(&state->card->lock, flags);
2142 return put_user(dmabuf->rate, p);
2143 case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2145 printk("SNDCTL_DSP_STEREO\n");
2147 if (dmabuf->enable & DAC_RUNNING) {
2150 if (dmabuf->enable & ADC_RUNNING) {
2153 if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
2154 stop_spdifout(state);
2156 if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING) {
2157 stop_spdifout(state);
2159 return put_user(1, p);
2160 case SNDCTL_DSP_GETBLKSIZE:
2161 if (file->f_mode & FMODE_WRITE) {
2162 if (codec_independent_spdif_locked > 0) {
2163 if (!dmabuf->ready && (val = prog_dmabuf(state, 2)))
2166 if (controller_independent_spdif_locked > 0) {
2167 if (!dmabuf->ready && (val = prog_dmabuf(state, 3)))
2170 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)))
2176 if (file->f_mode & FMODE_READ) {
2177 if (!dmabuf->ready && (val = prog_dmabuf(state, 1)))
2181 printk("SNDCTL_DSP_GETBLKSIZE %d\n", dmabuf->userfragsize);
2183 return put_user(dmabuf->userfragsize, p);
2184 case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format */
2186 printk("SNDCTL_DSP_GETFMTS\n");
2188 return put_user(AFMT_S16_LE, p);
2189 case SNDCTL_DSP_SETFMT: /* Select sample format */
2191 printk("SNDCTL_DSP_SETFMT\n");
2193 return put_user(AFMT_S16_LE, p);
2194 case SNDCTL_DSP_CHANNELS: // add support 4,6 channel
2196 printk("SNDCTL_DSP_CHANNELS\n");
2198 if (get_user(val, p))
2201 if (dmabuf->enable & DAC_RUNNING) {
2204 if (dmabuf->enable & CODEC_SPDIFOUT_RUNNING) {
2205 stop_spdifout(state);
2207 if (dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING) {
2208 stop_spdifout(state);
2210 if (dmabuf->enable & ADC_RUNNING) {
2214 return put_user(state->card->channels, p);
2217 i_scr = inl(state->card->iobase + ALI_SCR);
2218 /* Current # of channels enabled */
2219 if (i_scr & 0x00000100)
2221 else if (i_scr & 0x00000200)
2226 case 2: /* 2 channels is always supported */
2227 if (codec_independent_spdif_locked > 0) {
2228 outl(((i_scr & 0xfffffcff) | 0x00100000), (state->card->iobase + ALI_SCR));
2230 outl((i_scr & 0xfffffcff), (state->card->iobase + ALI_SCR));
2231 /* Do we need to change mixer settings???? */
2233 case 4: /* Supported on some chipsets, better check first */
2234 if (codec_independent_spdif_locked > 0) {
2235 outl(((i_scr & 0xfffffcff) | 0x00000100 | 0x00200000), (state->card->iobase + ALI_SCR));
2237 outl(((i_scr & 0xfffffcff) | 0x00000100), (state->card->iobase + ALI_SCR));
2239 case 6: /* Supported on some chipsets, better check first */
2240 if (codec_independent_spdif_locked > 0) {
2241 outl(((i_scr & 0xfffffcff) | 0x00000200 | 0x00008000 | 0x00300000), (state->card->iobase + ALI_SCR));
2243 outl(((i_scr & 0xfffffcff) | 0x00000200 | 0x00008000), (state->card->iobase + ALI_SCR));
2245 default: /* nothing else is ever supported by the chipset */
2249 return put_user(val, p);
2250 case SNDCTL_DSP_POST: /* the user has sent all data and is notifying us */
2251 /* we update the swptr to the end of the last sg segment then return */
2253 printk("SNDCTL_DSP_POST\n");
2255 if (codec_independent_spdif_locked > 0) {
2256 if (!dmabuf->ready || (dmabuf->enable != CODEC_SPDIFOUT_RUNNING))
2259 if (controller_independent_spdif_locked > 0) {
2260 if (!dmabuf->ready || (dmabuf->enable != CONTROLLER_SPDIFOUT_RUNNING))
2263 if (!dmabuf->ready || (dmabuf->enable != DAC_RUNNING))
2267 if ((dmabuf->swptr % dmabuf->fragsize) != 0) {
2268 val = dmabuf->fragsize - (dmabuf->swptr % dmabuf->fragsize);
2269 dmabuf->swptr += val;
2270 dmabuf->count += val;
2273 case SNDCTL_DSP_SUBDIVIDE:
2274 if (dmabuf->subdivision)
2276 if (get_user(val, p))
2278 if (val != 1 && val != 2 && val != 4)
2281 printk("SNDCTL_DSP_SUBDIVIDE %d\n", val);
2283 dmabuf->subdivision = val;
2286 case SNDCTL_DSP_SETFRAGMENT:
2287 if (get_user(val, p))
2289 dmabuf->ossfragsize = 1 << (val & 0xffff);
2290 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2291 if (!dmabuf->ossfragsize || !dmabuf->ossmaxfrags)
2294 * Bound the frag size into our allowed range of 256 - 4096
2296 if (dmabuf->ossfragsize < 256)
2297 dmabuf->ossfragsize = 256;
2298 else if (dmabuf->ossfragsize > 4096)
2299 dmabuf->ossfragsize = 4096;
2301 * The numfrags could be something reasonable, or it could
2302 * be 0xffff meaning "Give me as much as possible". So,
2303 * we check the numfrags * fragsize doesn't exceed our
2304 * 64k buffer limit, nor is it less than our 8k minimum.
2305 * If it fails either one of these checks, then adjust the
2306 * number of fragments, not the size of them. It's OK if
2307 * our number of fragments doesn't equal 32 or anything
2308 * like our hardware based number now since we are using
2309 * a different frag count for the hardware. Before we get
2310 * into this though, bound the maxfrags to avoid overflow
2311 * issues. A reasonable bound would be 64k / 256 since our
2312 * maximum buffer size is 64k and our minimum frag size is
2313 * 256. On the other end, our minimum buffer size is 8k and
2314 * our maximum frag size is 4k, so the lower bound should
2317 if (dmabuf->ossmaxfrags > 256)
2318 dmabuf->ossmaxfrags = 256;
2319 else if (dmabuf->ossmaxfrags < 2)
2320 dmabuf->ossmaxfrags = 2;
2321 val = dmabuf->ossfragsize * dmabuf->ossmaxfrags;
2322 while (val < 8192) {
2324 dmabuf->ossmaxfrags <<= 1;
2326 while (val > 65536) {
2328 dmabuf->ossmaxfrags >>= 1;
2332 printk("SNDCTL_DSP_SETFRAGMENT 0x%x, %d, %d\n", val,
2333 dmabuf->ossfragsize, dmabuf->ossmaxfrags);
2336 case SNDCTL_DSP_GETOSPACE:
2337 if (!(file->f_mode & FMODE_WRITE))
2339 if (codec_independent_spdif_locked > 0) {
2340 if (!dmabuf->ready && (val = prog_dmabuf(state, 2)) != 0)
2343 if (controller_independent_spdif_locked > 0) {
2344 if (!dmabuf->ready && (val = prog_dmabuf(state, 3)) != 0)
2347 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2351 spin_lock_irqsave(&state->card->lock, flags);
2352 ali_update_ptr(state);
2353 abinfo.fragsize = dmabuf->userfragsize;
2354 abinfo.fragstotal = dmabuf->userfrags;
2356 abinfo.bytes = dmabuf->dmasize;
2358 abinfo.bytes = ali_get_free_write_space(state);
2359 abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
2360 spin_unlock_irqrestore(&state->card->lock, flags);
2361 #if defined(DEBUG) || defined(DEBUG_MMAP)
2362 printk("SNDCTL_DSP_GETOSPACE %d, %d, %d, %d\n",
2363 abinfo.bytes, abinfo.fragsize, abinfo.fragments,
2366 return copy_to_user(argp, &abinfo,
2367 sizeof(abinfo)) ? -EFAULT : 0;
2368 case SNDCTL_DSP_GETOPTR:
2369 if (!(file->f_mode & FMODE_WRITE))
2371 if (codec_independent_spdif_locked > 0) {
2372 if (!dmabuf->ready && (val = prog_dmabuf(state, 2)) != 0)
2375 if (controller_independent_spdif_locked > 0) {
2376 if (!dmabuf->ready && (val = prog_dmabuf(state, 3)) != 0)
2379 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2383 spin_lock_irqsave(&state->card->lock, flags);
2384 val = ali_get_free_write_space(state);
2385 cinfo.bytes = dmabuf->total_bytes;
2386 cinfo.ptr = dmabuf->hwptr;
2387 cinfo.blocks = val / dmabuf->userfragsize;
2388 if (codec_independent_spdif_locked > 0) {
2389 if (dmabuf->mapped && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
2390 dmabuf->count += val;
2391 dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2392 __ali_update_lvi(state, 2);
2395 if (controller_independent_spdif_locked > 0) {
2396 if (dmabuf->mapped && (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)) {
2397 dmabuf->count += val;
2398 dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2399 __ali_update_lvi(state, 3);
2402 if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_OUTPUT)) {
2403 dmabuf->count += val;
2404 dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2405 __ali_update_lvi(state, 0);
2409 spin_unlock_irqrestore(&state->card->lock, flags);
2410 #if defined(DEBUG) || defined(DEBUG_MMAP)
2411 printk("SNDCTL_DSP_GETOPTR %d, %d, %d, %d\n", cinfo.bytes,
2412 cinfo.blocks, cinfo.ptr, dmabuf->count);
2414 return copy_to_user(argp, &cinfo, sizeof(cinfo))? -EFAULT : 0;
2415 case SNDCTL_DSP_GETISPACE:
2416 if (!(file->f_mode & FMODE_READ))
2418 if (!dmabuf->ready && (val = prog_dmabuf(state, 1)) != 0)
2420 spin_lock_irqsave(&state->card->lock, flags);
2421 abinfo.bytes = ali_get_available_read_data(state);
2422 abinfo.fragsize = dmabuf->userfragsize;
2423 abinfo.fragstotal = dmabuf->userfrags;
2424 abinfo.fragments = abinfo.bytes / dmabuf->userfragsize;
2425 spin_unlock_irqrestore(&state->card->lock, flags);
2426 #if defined(DEBUG) || defined(DEBUG_MMAP)
2427 printk("SNDCTL_DSP_GETISPACE %d, %d, %d, %d\n",
2428 abinfo.bytes, abinfo.fragsize, abinfo.fragments,
2431 return copy_to_user(argp, &abinfo,
2432 sizeof(abinfo)) ? -EFAULT : 0;
2433 case SNDCTL_DSP_GETIPTR:
2434 if (!(file->f_mode & FMODE_READ))
2436 if (!dmabuf->ready && (val = prog_dmabuf(state, 0)) != 0)
2438 spin_lock_irqsave(&state->card->lock, flags);
2439 val = ali_get_available_read_data(state);
2440 cinfo.bytes = dmabuf->total_bytes;
2441 cinfo.blocks = val / dmabuf->userfragsize;
2442 cinfo.ptr = dmabuf->hwptr;
2443 if (dmabuf->mapped && (dmabuf->trigger & PCM_ENABLE_INPUT)) {
2444 dmabuf->count -= val;
2445 dmabuf->swptr = (dmabuf->swptr + val) % dmabuf->dmasize;
2446 __ali_update_lvi(state, 1);
2448 spin_unlock_irqrestore(&state->card->lock, flags);
2449 #if defined(DEBUG) || defined(DEBUG_MMAP)
2450 printk("SNDCTL_DSP_GETIPTR %d, %d, %d, %d\n", cinfo.bytes,
2451 cinfo.blocks, cinfo.ptr, dmabuf->count);
2453 return copy_to_user(argp, &cinfo, sizeof(cinfo))? -EFAULT: 0;
2454 case SNDCTL_DSP_NONBLOCK:
2456 printk("SNDCTL_DSP_NONBLOCK\n");
2458 file->f_flags |= O_NONBLOCK;
2460 case SNDCTL_DSP_GETCAPS:
2462 printk("SNDCTL_DSP_GETCAPS\n");
2464 return put_user(DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2465 DSP_CAP_MMAP | DSP_CAP_BIND, p);
2466 case SNDCTL_DSP_GETTRIGGER:
2469 printk("SNDCTL_DSP_GETTRIGGER 0x%x\n", dmabuf->trigger);
2471 return put_user(dmabuf->trigger, p);
2472 case SNDCTL_DSP_SETTRIGGER:
2473 if (get_user(val, p))
2475 #if defined(DEBUG) || defined(DEBUG_MMAP)
2476 printk("SNDCTL_DSP_SETTRIGGER 0x%x\n", val);
2478 if (!(val & PCM_ENABLE_INPUT) && dmabuf->enable == ADC_RUNNING) {
2481 if (!(val & PCM_ENABLE_OUTPUT) && dmabuf->enable == DAC_RUNNING) {
2484 if (!(val & SPDIF_ENABLE_OUTPUT) && dmabuf->enable == CODEC_SPDIFOUT_RUNNING) {
2485 stop_spdifout(state);
2487 if (!(val & SPDIF_ENABLE_OUTPUT) && dmabuf->enable == CONTROLLER_SPDIFOUT_RUNNING) {
2488 stop_spdifout(state);
2490 dmabuf->trigger = val;
2491 if (val & PCM_ENABLE_OUTPUT && !(dmabuf->enable & DAC_RUNNING)) {
2492 if (!dmabuf->write_channel) {
2494 dmabuf->write_channel = state->card->alloc_pcm_channel(state->card);
2495 if (!dmabuf->write_channel)
2498 if (!dmabuf->ready && (ret = prog_dmabuf(state, 0)))
2500 if (dmabuf->mapped) {
2501 spin_lock_irqsave(&state->card->lock, flags);
2502 ali_update_ptr(state);
2504 dmabuf->swptr = dmabuf->hwptr;
2505 dmabuf->count = ali_get_free_write_space(state);
2506 dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2507 __ali_update_lvi(state, 0);
2508 spin_unlock_irqrestore(&state->card->lock,
2513 if (val & SPDIF_ENABLE_OUTPUT && !(dmabuf->enable & CODEC_SPDIFOUT_RUNNING)) {
2514 if (!dmabuf->codec_spdifout_channel) {
2516 dmabuf->codec_spdifout_channel = state->card->alloc_codec_spdifout_channel(state->card);
2517 if (!dmabuf->codec_spdifout_channel)
2520 if (!dmabuf->ready && (ret = prog_dmabuf(state, 2)))
2522 if (dmabuf->mapped) {
2523 spin_lock_irqsave(&state->card->lock, flags);
2524 ali_update_ptr(state);
2526 dmabuf->swptr = dmabuf->hwptr;
2527 dmabuf->count = ali_get_free_write_space(state);
2528 dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2529 __ali_update_lvi(state, 2);
2530 spin_unlock_irqrestore(&state->card->lock,
2533 start_spdifout(state);
2535 if (val & SPDIF_ENABLE_OUTPUT && !(dmabuf->enable & CONTROLLER_SPDIFOUT_RUNNING)) {
2536 if (!dmabuf->controller_spdifout_channel) {
2538 dmabuf->controller_spdifout_channel = state->card->alloc_controller_spdifout_channel(state->card);
2539 if (!dmabuf->controller_spdifout_channel)
2542 if (!dmabuf->ready && (ret = prog_dmabuf(state, 3)))
2544 if (dmabuf->mapped) {
2545 spin_lock_irqsave(&state->card->lock, flags);
2546 ali_update_ptr(state);
2548 dmabuf->swptr = dmabuf->hwptr;
2549 dmabuf->count = ali_get_free_write_space(state);
2550 dmabuf->swptr = (dmabuf->swptr + dmabuf->count) % dmabuf->dmasize;
2551 __ali_update_lvi(state, 3);
2552 spin_unlock_irqrestore(&state->card->lock, flags);
2554 start_spdifout(state);
2556 if (val & PCM_ENABLE_INPUT && !(dmabuf->enable & ADC_RUNNING)) {
2557 if (!dmabuf->read_channel) {
2559 dmabuf->read_channel = state->card->alloc_rec_pcm_channel(state->card);
2560 if (!dmabuf->read_channel)
2563 if (!dmabuf->ready && (ret = prog_dmabuf(state, 1)))
2565 if (dmabuf->mapped) {
2566 spin_lock_irqsave(&state->card->lock,
2568 ali_update_ptr(state);
2569 dmabuf->swptr = dmabuf->hwptr;
2571 spin_unlock_irqrestore(&state->card->lock, flags);
2573 ali_update_lvi(state, 1);
2577 case SNDCTL_DSP_SETDUPLEX:
2579 printk("SNDCTL_DSP_SETDUPLEX\n");
2582 case SNDCTL_DSP_GETODELAY:
2583 if (!(file->f_mode & FMODE_WRITE))
2585 spin_lock_irqsave(&state->card->lock, flags);
2586 ali_update_ptr(state);
2587 val = dmabuf->count;
2588 spin_unlock_irqrestore(&state->card->lock, flags);
2590 printk("SNDCTL_DSP_GETODELAY %d\n", dmabuf->count);
2592 return put_user(val, p);
2593 case SOUND_PCM_READ_RATE:
2595 printk("SOUND_PCM_READ_RATE %d\n", dmabuf->rate);
2597 return put_user(dmabuf->rate, p);
2598 case SOUND_PCM_READ_CHANNELS:
2600 printk("SOUND_PCM_READ_CHANNELS\n");
2602 return put_user(2, p);
2603 case SOUND_PCM_READ_BITS:
2605 printk("SOUND_PCM_READ_BITS\n");
2607 return put_user(AFMT_S16_LE, p);
2608 case SNDCTL_DSP_SETSPDIF: /* Set S/PDIF Control register */
2610 printk("SNDCTL_DSP_SETSPDIF\n");
2612 if (get_user(val, p))
2614 /* Check to make sure the codec supports S/PDIF transmitter */
2615 if ((state->card->ac97_features & 4)) {
2616 /* mask out the transmitter speed bits so the user can't set them */
2618 /* Add the current transmitter speed bits to the passed value */
2619 ret = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
2620 val |= (ret & 0x3000);
2621 ali_ac97_set(codec, AC97_SPDIF_CONTROL, val);
2622 if (ali_ac97_get(codec, AC97_SPDIF_CONTROL) != val) {
2623 printk(KERN_ERR "ali_audio: Unable to set S/PDIF configuration to 0x%04x.\n", val);
2629 printk(KERN_WARNING "ali_audio: S/PDIF transmitter not avalible.\n");
2631 return put_user(val, p);
2632 case SNDCTL_DSP_GETSPDIF: /* Get S/PDIF Control register */
2634 printk("SNDCTL_DSP_GETSPDIF\n");
2636 if (get_user(val, p))
2638 /* Check to make sure the codec supports S/PDIF transmitter */
2639 if (!(state->card->ac97_features & 4)) {
2641 printk(KERN_WARNING "ali_audio: S/PDIF transmitter not avalible.\n");
2645 val = ali_ac97_get(codec, AC97_SPDIF_CONTROL);
2648 return put_user(val, p);
2649 //end add support spdif out
2650 //add support 4,6 channel
2651 case SNDCTL_DSP_GETCHANNELMASK:
2653 printk("SNDCTL_DSP_GETCHANNELMASK\n");
2655 if (get_user(val, p))
2657 /* Based on AC'97 DAC support, not ICH hardware */
2658 val = DSP_BIND_FRONT;
2659 if (state->card->ac97_features & 0x0004)
2660 val |= DSP_BIND_SPDIF;
2661 if (state->card->ac97_features & 0x0080)
2662 val |= DSP_BIND_SURR;
2663 if (state->card->ac97_features & 0x0140)
2664 val |= DSP_BIND_CENTER_LFE;
2665 return put_user(val, p);
2666 case SNDCTL_DSP_BIND_CHANNEL:
2668 printk("SNDCTL_DSP_BIND_CHANNEL\n");
2670 if (get_user(val, p))
2672 if (val == DSP_BIND_QUERY) {
2673 val = DSP_BIND_FRONT; /* Always report this as being enabled */
2674 if (state->card->ac97_status & SPDIF_ON)
2675 val |= DSP_BIND_SPDIF;
2677 if (state->card->ac97_status & SURR_ON)
2678 val |= DSP_BIND_SURR;
2680 ac97_status & CENTER_LFE_ON)
2681 val |= DSP_BIND_CENTER_LFE;
2683 } else { /* Not a query, set it */
2684 if (!(file->f_mode & FMODE_WRITE))
2686 if (dmabuf->enable == DAC_RUNNING) {
2689 if (val & DSP_BIND_SPDIF) { /* Turn on SPDIF */
2690 /* Ok, this should probably define what slots
2691 * to use. For now, we'll only set it to the
2694 * non multichannel codec maps to slots 3&4
2695 * 2 channel codec maps to slots 7&8
2696 * 4 channel codec maps to slots 6&9
2697 * 6 channel codec maps to slots 10&11
2699 * there should be some way for the app to
2700 * select the slot assignment.
2702 i_scr = inl(state->card->iobase + ALI_SCR);
2703 if (codec_independent_spdif_locked > 0) {
2705 if ((i_scr & 0x00300000) == 0x00100000) {
2706 ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2708 if ((i_scr & 0x00300000) == 0x00200000) {
2709 ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
2711 if ((i_scr & 0x00300000) == 0x00300000) {
2712 ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
2716 } else { /* codec spdif out (pcm out share ) */
2717 ali_set_spdif_output(state, AC97_EA_SPSA_3_4, dmabuf->rate); //I do not modify
2720 if (!(state->card->ac97_status & SPDIF_ON))
2721 val &= ~DSP_BIND_SPDIF;
2725 /* Turn off S/PDIF if it was on */
2726 if (state->card->ac97_status & SPDIF_ON)
2727 ali_set_spdif_output(state, -1, 0);
2729 val & (DSP_BIND_FRONT | DSP_BIND_SURR |
2730 DSP_BIND_CENTER_LFE);
2732 case DSP_BIND_FRONT:
2735 case DSP_BIND_FRONT | DSP_BIND_SURR:
2738 case DSP_BIND_FRONT | DSP_BIND_SURR | DSP_BIND_CENTER_LFE:
2742 val = DSP_BIND_FRONT;
2746 ali_set_dac_channels(state, channels);
2747 /* check that they really got turned on */
2748 if (!state->card->ac97_status & SURR_ON)
2749 val &= ~DSP_BIND_SURR;
2751 ac97_status & CENTER_LFE_ON)
2752 val &= ~DSP_BIND_CENTER_LFE;
2755 return put_user(val, p);
2756 case SNDCTL_DSP_MAPINBUF:
2757 case SNDCTL_DSP_MAPOUTBUF:
2758 case SNDCTL_DSP_SETSYNCRO:
2759 case SOUND_PCM_WRITE_FILTER:
2760 case SOUND_PCM_READ_FILTER:
2766 static int ali_open(struct inode *inode, struct file *file)
2769 struct ali_card *card = devs;
2770 struct ali_state *state = NULL;
2771 struct dmabuf *dmabuf = NULL;
2774 /* find an available virtual channel (instance of /dev/dsp) */
2776 while (card != NULL) {
2779 * If we are initializing and then fail, card could go
2780 * away unuexpectedly while we are in the for() loop.
2781 * So, check for card on each iteration before we check
2782 * for card->initializing to avoid a possible oops.
2783 * This usually only matters for times when the driver is
2784 * autoloaded by kmod.
2786 for (i = 0; i < 50 && card && card->initializing; i++) {
2787 set_current_state(TASK_UNINTERRUPTIBLE);
2788 schedule_timeout(HZ / 20);
2791 for (i = 0; i < NR_HW_CH && card && !card->initializing; i++) {
2792 if (card->states[i] == NULL) {
2793 state = card->states[i] = (struct ali_state *) kmalloc(sizeof(struct ali_state), GFP_KERNEL);
2796 memset(state, 0, sizeof(struct ali_state));
2797 dmabuf = &state->dmabuf;
2804 /* no more virtual channel avaiable */
2808 /* initialize the virtual channel */
2812 state->magic = ALI5455_STATE_MAGIC;
2813 init_waitqueue_head(&dmabuf->wait);
2814 init_MUTEX(&state->open_sem);
2815 file->private_data = state;
2816 dmabuf->trigger = 0;
2817 /* allocate hardware channels */
2818 if (file->f_mode & FMODE_READ) {
2819 if ((dmabuf->read_channel =
2820 card->alloc_rec_pcm_channel(card)) == NULL) {
2821 kfree(card->states[i]);
2822 card->states[i] = NULL;
2825 dmabuf->trigger |= PCM_ENABLE_INPUT;
2826 ali_set_adc_rate(state, 8000);
2828 if (file->f_mode & FMODE_WRITE) {
2829 if (codec_independent_spdif_locked > 0) {
2830 if ((dmabuf->codec_spdifout_channel = card->alloc_codec_spdifout_channel(card)) == NULL) {
2831 kfree(card->states[i]);
2832 card->states[i] = NULL;
2835 dmabuf->trigger |= SPDIF_ENABLE_OUTPUT;
2836 ali_set_codecspdifout_rate(state, codec_independent_spdif_locked); //It must add
2837 i_scr = inl(state->card->iobase + ALI_SCR);
2838 if ((i_scr & 0x00300000) == 0x00100000) {
2839 ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2841 if ((i_scr & 0x00300000) == 0x00200000) {
2842 ali_set_spdif_output(state, AC97_EA_SPSA_6_9, codec_independent_spdif_locked);
2844 if ((i_scr & 0x00300000) == 0x00300000) {
2845 ali_set_spdif_output(state, AC97_EA_SPSA_10_11, codec_independent_spdif_locked);
2847 ali_set_spdif_output(state, AC97_EA_SPSA_7_8, codec_independent_spdif_locked);
2853 if (controller_independent_spdif_locked > 0) {
2854 if ((dmabuf->controller_spdifout_channel = card->alloc_controller_spdifout_channel(card)) == NULL) {
2855 kfree(card->states[i]);
2856 card->states[i] = NULL;
2859 dmabuf->trigger |= SPDIF_ENABLE_OUTPUT;
2860 ali_set_spdifout_rate(state, controller_independent_spdif_locked);
2862 if ((dmabuf->write_channel = card->alloc_pcm_channel(card)) == NULL) {
2863 kfree(card->states[i]);
2864 card->states[i] = NULL;
2867 /* Initialize to 8kHz? What if we don't support 8kHz? */
2868 /* Let's change this to check for S/PDIF stuff */
2870 dmabuf->trigger |= PCM_ENABLE_OUTPUT;
2871 if (codec_pcmout_share_spdif_locked) {
2872 ali_set_dac_rate(state, codec_pcmout_share_spdif_locked);
2873 ali_set_spdif_output(state, AC97_EA_SPSA_3_4, codec_pcmout_share_spdif_locked);
2875 ali_set_dac_rate(state, 8000);
2882 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
2883 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
2884 /dev/dspW will accept 16-bits sample, but we don't support those so we
2885 set it immediately to stereo and 16bit, which is all we do support */
2886 dmabuf->fmt |= ALI5455_FMT_16BIT | ALI5455_FMT_STEREO;
2887 dmabuf->ossfragsize = 0;
2888 dmabuf->ossmaxfrags = 0;
2889 dmabuf->subdivision = 0;
2890 state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2891 outl(0x00000000, card->iobase + ALI_INTERRUPTCR);
2892 outl(0x00000000, card->iobase + ALI_INTERRUPTSR);
2896 static int ali_release(struct inode *inode, struct file *file)
2898 struct ali_state *state = (struct ali_state *) file->private_data;
2899 struct ali_card *card = state->card;
2900 struct dmabuf *dmabuf = &state->dmabuf;
2901 unsigned long flags;
2904 /* stop DMA state machine and free DMA buffers/channels */
2905 if (dmabuf->trigger & PCM_ENABLE_OUTPUT)
2906 drain_dac(state, 0);
2908 if (dmabuf->trigger & SPDIF_ENABLE_OUTPUT)
2909 drain_spdifout(state, 0);
2911 if (dmabuf->trigger & PCM_ENABLE_INPUT)
2914 spin_lock_irqsave(&card->lock, flags);
2915 dealloc_dmabuf(state);
2916 if (file->f_mode & FMODE_WRITE) {
2917 if (codec_independent_spdif_locked > 0) {
2918 state->card->free_pcm_channel(state->card, dmabuf->codec_spdifout_channel->num);
2920 if (controller_independent_spdif_locked > 0)
2921 state->card->free_pcm_channel(state->card,
2922 dmabuf->controller_spdifout_channel->num);
2923 else state->card->free_pcm_channel(state->card,
2924 dmabuf->write_channel->num);
2927 if (file->f_mode & FMODE_READ)
2928 state->card->free_pcm_channel(state->card, dmabuf->read_channel->num);
2930 state->card->states[state->virt] = NULL;
2932 spin_unlock_irqrestore(&card->lock, flags);
2937 static /*const */ struct file_operations ali_audio_fops = {
2938 .owner = THIS_MODULE,
2939 .llseek = no_llseek,
2946 .release = ali_release,
2949 /* Read AC97 codec registers */
2950 static u16 ali_ac97_get(struct ac97_codec *dev, u8 reg)
2952 struct ali_card *card = dev->private_data;
2955 unsigned short int data = 0, count, addr1, addr2 = 0;
2957 spin_lock(&card->ac97_lock);
2958 while (count1-- && (inl(card->iobase + ALI_CAS) & 0x80000000))
2963 for (count = 0; count < 0x7f; count++) {
2964 val = inb(card->iobase + ALI_CSPSR);
2970 spin_unlock(&card->ac97_lock);
2973 outw(reg, (card->iobase + ALI_CPR) + 2);
2974 for (count = 0; count < 0x7f; count++) {
2975 val = inb(card->iobase + ALI_CSPSR);
2977 data = inw(card->iobase + ALI_SPR);
2978 addr2 = inw((card->iobase + ALI_SPR) + 2);
2982 spin_unlock(&card->ac97_lock);
2987 return ((u16) data);
2990 /* write ac97 codec register */
2992 static void ali_ac97_set(struct ac97_codec *dev, u8 reg, u16 data)
2994 struct ali_card *card = dev->private_data;
2997 unsigned short int count;
2999 spin_lock(&card->ac97_lock);
3000 while (count1-- && (inl(card->iobase + ALI_CAS) & 0x80000000))
3003 for (count = 0; count < 0x7f; count++) {
3004 val = inb(card->iobase + ALI_CSPSR);
3008 if (count == 0x7f) {
3009 printk(KERN_WARNING "ali_ac97_set: AC97 codec register access timed out. \n");
3010 spin_unlock(&card->ac97_lock);
3013 outw(data, (card->iobase + ALI_CPR));
3014 outb(reg, (card->iobase + ALI_CPR) + 2);
3015 for (count = 0; count < 0x7f; count++) {
3016 val = inb(card->iobase + ALI_CSPSR);
3020 spin_unlock(&card->ac97_lock);
3022 printk(KERN_WARNING "ali_ac97_set: AC97 codec register access timed out. \n");
3026 /* OSS /dev/mixer file operation methods */
3028 static int ali_open_mixdev(struct inode *inode, struct file *file)
3031 int minor = iminor(inode);
3032 struct ali_card *card = devs;
3033 for (card = devs; card != NULL; card = card->next) {
3035 * If we are initializing and then fail, card could go
3036 * away unuexpectedly while we are in the for() loop.
3037 * So, check for card on each iteration before we check
3038 * for card->initializing to avoid a possible oops.
3039 * This usually only matters for times when the driver is
3040 * autoloaded by kmod.
3042 for (i = 0; i < 50 && card && card->initializing; i++) {
3043 set_current_state(TASK_UNINTERRUPTIBLE);
3044 schedule_timeout(HZ / 20);
3046 for (i = 0; i < NR_AC97 && card && !card->initializing; i++)
3047 if (card->ac97_codec[i] != NULL
3048 && card->ac97_codec[i]->dev_mixer == minor) {
3049 file->private_data = card->ac97_codec[i];
3056 static int ali_ioctl_mixdev(struct inode *inode,
3058 unsigned int cmd, unsigned long arg)
3060 struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
3061 return codec->mixer_ioctl(codec, cmd, arg);
3064 static /*const */ struct file_operations ali_mixer_fops = {
3065 .owner = THIS_MODULE,
3066 .llseek = no_llseek,
3067 .ioctl = ali_ioctl_mixdev,
3068 .open = ali_open_mixdev,
3071 /* AC97 codec initialisation. These small functions exist so we don't
3072 duplicate code between module init and apm resume */
3074 static inline int ali_ac97_exists(struct ali_card *card, int ac97_number)
3077 u32 reg = inl(card->iobase + ALI_RTSR);
3081 reg = inl(card->iobase + ALI_RTSR);
3085 outl(reg | 0x00000040,
3086 card->iobase + 0x34);
3094 reg = inl(card->iobase + ALI_RTSR);
3098 outl(reg | 0x00000080,
3099 card->iobase + 0x34);
3112 static inline int ali_ac97_enable_variable_rate(struct ac97_codec *codec)
3114 ali_ac97_set(codec, AC97_EXTENDED_STATUS, 9);
3115 ali_ac97_set(codec, AC97_EXTENDED_STATUS, ali_ac97_get(codec, AC97_EXTENDED_STATUS) | 0xE800);
3116 return (ali_ac97_get(codec, AC97_EXTENDED_STATUS) & 1);
3120 static int ali_ac97_probe_and_powerup(struct ali_card *card, struct ac97_codec *codec)
3122 /* Returns 0 on failure */
3125 if (ac97_probe_codec(codec) == 0)
3127 /* ac97_probe_codec is success ,then begin to init codec */
3128 ali_ac97_set(codec, AC97_RESET, 0xffff);
3129 if (card->channel[0].used == 1) {
3130 ali_ac97_set(codec, AC97_RECORD_SELECT, 0x0000);
3131 ali_ac97_set(codec, AC97_LINEIN_VOL, 0x0808);
3132 ali_ac97_set(codec, AC97_RECORD_GAIN, 0x0F0F);
3135 if (card->channel[2].used == 1) //if MICin then init codec
3137 ali_ac97_set(codec, AC97_RECORD_SELECT, 0x0000);
3138 ali_ac97_set(codec, AC97_MIC_VOL, 0x8808);
3139 ali_ac97_set(codec, AC97_RECORD_GAIN, 0x0F0F);
3140 ali_ac97_set(codec, AC97_RECORD_GAIN_MIC, 0x0000);
3143 ali_ac97_set(codec, AC97_MASTER_VOL_STEREO, 0x0000);
3144 ali_ac97_set(codec, AC97_HEADPHONE_VOL, 0x0000);
3145 ali_ac97_set(codec, AC97_PCMOUT_VOL, 0x0000);
3146 ali_ac97_set(codec, AC97_CD_VOL, 0x0808);
3147 ali_ac97_set(codec, AC97_VIDEO_VOL, 0x0808);
3148 ali_ac97_set(codec, AC97_AUX_VOL, 0x0808);
3149 ali_ac97_set(codec, AC97_PHONE_VOL, 0x8048);
3150 ali_ac97_set(codec, AC97_PCBEEP_VOL, 0x0000);
3151 ali_ac97_set(codec, AC97_GENERAL_PURPOSE, AC97_GP_MIX);
3152 ali_ac97_set(codec, AC97_MASTER_VOL_MONO, 0x0000);
3153 ali_ac97_set(codec, 0x38, 0x0000);
3154 addr = ali_ac97_get(codec, 0x2a);
3155 ali_ac97_set(codec, 0x2a, addr | 0x0001);
3156 addr = ali_ac97_get(codec, 0x2a);
3157 addr = ali_ac97_get(codec, 0x28);
3158 ali_ac97_set(codec, 0x2c, 0xbb80);
3159 addr = ali_ac97_get(codec, 0x2c);
3160 /* power it all up */
3161 ali_ac97_set(codec, AC97_POWER_CONTROL,
3162 ali_ac97_get(codec, AC97_POWER_CONTROL) & ~0x7f00);
3163 /* wait for analog ready */
3164 for (i = 10; i && ((ali_ac97_get(codec, AC97_POWER_CONTROL) & 0xf) != 0xf); i--) {
3165 set_current_state(TASK_UNINTERRUPTIBLE);
3166 schedule_timeout(HZ / 20);
3174 /* I clone ali5455(2.4.7 ) not clone i810_audio(2.4.18) */
3176 static int ali_reset_5455(struct ali_card *card)
3178 outl(0x80000003, card->iobase + ALI_SCR);
3179 outl(0x83838383, card->iobase + ALI_FIFOCR1);
3180 outl(0x83838383, card->iobase + ALI_FIFOCR2);
3181 if (controller_pcmout_share_spdif_locked > 0) {
3182 outl((inl(card->iobase + ALI_SPDIFICS) | 0x00000001),
3183 card->iobase + ALI_SPDIFICS);
3184 outl(0x0408000a, card->iobase + ALI_INTERFACECR);
3186 if (codec_independent_spdif_locked > 0) {
3187 outl((inl(card->iobase + ALI_SCR) | 0x00100000), card->iobase + ALI_SCR); // now I select slot 7 & 8
3188 outl(0x00200000, card->iobase + ALI_INTERFACECR); //enable codec independent spdifout
3190 outl(0x04080002, card->iobase + ALI_INTERFACECR);
3193 outl(0x00000000, card->iobase + ALI_INTERRUPTCR);
3194 outl(0x00000000, card->iobase + ALI_INTERRUPTSR);
3195 if (controller_independent_spdif_locked > 0)
3196 outl((inl(card->iobase + ALI_SPDIFICS) | 0x00000001),
3197 card->iobase + ALI_SPDIFICS);
3202 static int ali_ac97_random_init_stuff(struct ali_card
3205 u32 reg = inl(card->iobase + ALI_SCR);
3207 reg = inl(card->iobase + ALI_SCR);
3208 if ((reg & 2) == 0) /* Cold required */
3211 reg |= 1; /* Warm */
3212 reg &= ~0x80000000; /* ACLink on */
3213 outl(reg, card->iobase + ALI_SCR);
3216 if ((inl(card->iobase + 0x18) & (1 << 1)) == 0)
3218 current->state = TASK_UNINTERRUPTIBLE;
3219 schedule_timeout(HZ / 20);
3223 printk(KERN_ERR "ali_audio: AC'97 reset failed.\n");
3227 set_current_state(TASK_UNINTERRUPTIBLE);
3228 schedule_timeout(HZ / 2);
3232 /* AC97 codec initialisation. */
3234 static int __devinit ali_ac97_init(struct ali_card *card)
3237 int total_channels = 0;
3238 struct ac97_codec *codec;
3241 if (!ali_ac97_random_init_stuff(card))
3244 /* Number of channels supported */
3245 /* What about the codec? Just because the ICH supports */
3246 /* multiple channels doesn't mean the codec does. */
3247 /* we'll have to modify this in the codec section below */
3248 /* to reflect what the codec has. */
3249 /* ICH and ICH0 only support 2 channels so don't bother */
3251 inl(card->iobase + ALI_CPR);
3254 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3256 /* Assume codec isn't available until we go through the
3258 card->ac97_codec[num_ac97] = NULL;
3259 /* The ICH programmer's reference says you should */
3260 /* check the ready status before probing. So we chk */
3261 /* What do we do if it's not ready? Wait and try */
3262 /* again, or abort? */
3263 if (!ali_ac97_exists(card, num_ac97)) {
3265 printk(KERN_ERR "ali_audio: Primary codec not ready.\n");
3269 if ((codec = ac97_alloc_codec()) == NULL)
3271 /* initialize some basic codec information, other fields will be filled
3272 in ac97_probe_codec */
3273 codec->private_data = card;
3274 codec->id = num_ac97;
3275 codec->codec_read = ali_ac97_get;
3276 codec->codec_write = ali_ac97_set;
3277 if (!ali_ac97_probe_and_powerup(card, codec)) {
3278 printk(KERN_ERR "ali_audio: timed out waiting for codec %d analog ready",
3281 break; /* it didn't work */
3284 /* Store state information about S/PDIF transmitter */
3285 card->ac97_status = 0;
3286 /* Don't attempt to get eid until powerup is complete */
3287 eid = ali_ac97_get(codec, AC97_EXTENDED_ID);
3288 if (eid == 0xFFFF) {
3289 printk(KERN_ERR "ali_audio: no codec attached ?\n");
3294 card->ac97_features = eid;
3295 /* Now check the codec for useful features to make up for
3296 the dumbness of the ali5455 hardware engine */
3297 if (!(eid & 0x0001))
3299 "ali_audio: only 48Khz playback available.\n");
3301 if (!ali_ac97_enable_variable_rate(codec)) {
3303 "ali_audio: Codec refused to allow VRA, using 48Khz only.\n");
3304 card->ac97_features &= ~1;
3308 /* Determine how many channels the codec(s) support */
3309 /* - The primary codec always supports 2 */
3310 /* - If the codec supports AMAP, surround DACs will */
3311 /* automaticlly get assigned to slots. */
3312 /* * Check for surround DACs and increment if */
3314 /* - Else check if the codec is revision 2.2 */
3315 /* * If surround DACs exist, assign them to slots */
3316 /* and increment channel count. */
3318 /* All of this only applies to ICH2 and above. ICH */
3319 /* and ICH0 only support 2 channels. ICH2 will only */
3320 /* support multiple codecs in a "split audio" config. */
3321 /* as described above. */
3323 /* TODO: Remove all the debugging messages! */
3325 if ((eid & 0xc000) == 0) /* primary codec */
3326 total_channels += 2;
3327 if ((codec->dev_mixer = register_sound_mixer(&ali_mixer_fops, -1)) < 0) {
3328 printk(KERN_ERR "ali_audio: couldn't register mixer!\n");
3332 card->ac97_codec[num_ac97] = codec;
3334 /* pick the minimum of channels supported by ICHx or codec(s) */
3335 card->channels = (card->channels > total_channels) ? total_channels : card->channels;
3339 static void __devinit ali_configure_clocking(void)
3341 struct ali_card *card;
3342 struct ali_state *state;
3343 struct dmabuf *dmabuf;
3344 unsigned int i, offset, new_offset;
3345 unsigned long flags;
3348 /* We could try to set the clocking for multiple cards, but can you even have
3349 * more than one ali in a machine? Besides, clocking is global, so unless
3350 * someone actually thinks more than one ali in a machine is possible and
3351 * decides to rewrite that little bit, setting the rate for more than one card
3352 * is a waste of time.
3355 state = card->states[0] = (struct ali_state *)
3356 kmalloc(sizeof(struct ali_state), GFP_KERNEL);
3359 memset(state, 0, sizeof(struct ali_state));
3360 dmabuf = &state->dmabuf;
3361 dmabuf->write_channel = card->alloc_pcm_channel(card);
3364 state->magic = ALI5455_STATE_MAGIC;
3365 init_waitqueue_head(&dmabuf->wait);
3366 init_MUTEX(&state->open_sem);
3367 dmabuf->fmt = ALI5455_FMT_STEREO | ALI5455_FMT_16BIT;
3368 dmabuf->trigger = PCM_ENABLE_OUTPUT;
3369 ali_set_dac_rate(state, 48000);
3370 if (prog_dmabuf(state, 0) != 0)
3371 goto config_out_nodmabuf;
3373 if (dmabuf->dmasize < 16384)
3376 dmabuf->count = dmabuf->dmasize;
3377 outb(31, card->iobase + dmabuf->write_channel->port + OFF_LVI);
3379 local_irq_save(flags);
3381 offset = ali_get_dma_addr(state, 0);
3383 new_offset = ali_get_dma_addr(state, 0);
3386 outb(2, card->iobase + dmabuf->write_channel->port + OFF_CR);
3387 local_irq_restore(flags);
3389 i = new_offset - offset;
3394 if (i > 48500 || i < 47500) {
3395 clocking = clocking * clocking / i;
3398 dealloc_dmabuf(state);
3399 config_out_nodmabuf:
3400 state->card->free_pcm_channel(state->card, state->dmabuf. write_channel->num);
3402 card->states[0] = NULL;
3406 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
3407 until "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
3409 static int __devinit ali_probe(struct pci_dev *pci_dev,
3410 const struct pci_device_id *pci_id)
3412 struct ali_card *card;
3413 if (pci_enable_device(pci_dev))
3415 if (pci_set_dma_mask(pci_dev, ALI5455_DMA_MASK)) {
3416 printk(KERN_ERR "ali5455: architecture does not support"
3417 " 32bit PCI busmaster DMA\n");
3421 if ((card = kmalloc(sizeof(struct ali_card), GFP_KERNEL)) == NULL) {
3422 printk(KERN_ERR "ali_audio: out of memory\n");
3425 memset(card, 0, sizeof(*card));
3426 card->initializing = 1;
3427 card->iobase = pci_resource_start(pci_dev, 0);
3428 card->pci_dev = pci_dev;
3429 card->pci_id = pci_id->device;
3430 card->irq = pci_dev->irq;
3432 card->magic = ALI5455_CARD_MAGIC;
3434 card->pm_suspended = 0;
3436 spin_lock_init(&card->lock);
3437 spin_lock_init(&card->ac97_lock);
3439 pci_set_master(pci_dev);
3440 printk(KERN_INFO "ali: %s found at IO 0x%04lx, IRQ %d\n",
3441 card_names[pci_id->driver_data], card->iobase, card->irq);
3442 card->alloc_pcm_channel = ali_alloc_pcm_channel;
3443 card->alloc_rec_pcm_channel = ali_alloc_rec_pcm_channel;
3444 card->alloc_rec_mic_channel = ali_alloc_rec_mic_channel;
3445 card->alloc_codec_spdifout_channel = ali_alloc_codec_spdifout_channel;
3446 card->alloc_controller_spdifout_channel = ali_alloc_controller_spdifout_channel;
3447 card->free_pcm_channel = ali_free_pcm_channel;
3448 card->channel[0].offset = 0;
3449 card->channel[0].port = 0x40;
3450 card->channel[0].num = 0;
3451 card->channel[1].offset = 0;
3452 card->channel[1].port = 0x50;
3453 card->channel[1].num = 1;
3454 card->channel[2].offset = 0;
3455 card->channel[2].port = 0x60;
3456 card->channel[2].num = 2;
3457 card->channel[3].offset = 0;
3458 card->channel[3].port = 0x70;
3459 card->channel[3].num = 3;
3460 card->channel[4].offset = 0;
3461 card->channel[4].port = 0xb0;
3462 card->channel[4].num = 4;
3463 /* claim our iospace and irq */
3464 request_region(card->iobase, 256, card_names[pci_id->driver_data]);
3465 if (request_irq(card->irq, &ali_interrupt, SA_SHIRQ,
3466 card_names[pci_id->driver_data], card)) {
3467 printk(KERN_ERR "ali_audio: unable to allocate irq %d\n",
3469 release_region(card->iobase, 256);
3474 if (ali_reset_5455(card) <= 0) {
3475 unregister_sound_dsp(card->dev_audio);
3476 release_region(card->iobase, 256);
3477 free_irq(card->irq, card);
3482 /* initialize AC97 codec and register /dev/mixer */
3483 if (ali_ac97_init(card) < 0) {
3484 release_region(card->iobase, 256);
3485 free_irq(card->irq, card);
3490 pci_set_drvdata(pci_dev, card);
3492 if (clocking == 0) {
3494 ali_configure_clocking();
3497 /* register /dev/dsp */
3498 if ((card->dev_audio = register_sound_dsp(&ali_audio_fops, -1)) < 0) {
3500 printk(KERN_ERR"ali_audio: couldn't register DSP device!\n");
3501 release_region(card->iobase, 256);
3502 free_irq(card->irq, card);
3503 for (i = 0; i < NR_AC97; i++)
3504 if (card->ac97_codec[i] != NULL) {
3505 unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
3506 kfree(card->ac97_codec[i]);
3511 card->initializing = 0;
3515 static void __devexit ali_remove(struct pci_dev *pci_dev)
3518 struct ali_card *card = pci_get_drvdata(pci_dev);
3519 /* free hardware resources */
3520 free_irq(card->irq, devs);
3521 release_region(card->iobase, 256);
3522 /* unregister audio devices */
3523 for (i = 0; i < NR_AC97; i++)
3524 if (card->ac97_codec[i] != NULL) {
3525 unregister_sound_mixer(card->ac97_codec[i]->
3527 ac97_release_codec(card->ac97_codec[i]);
3528 card->ac97_codec[i] = NULL;
3530 unregister_sound_dsp(card->dev_audio);
3535 static int ali_pm_suspend(struct pci_dev *dev, u32 pm_state)
3537 struct ali_card *card = pci_get_drvdata(dev);
3538 struct ali_state *state;
3539 unsigned long flags;
3540 struct dmabuf *dmabuf;
3545 spin_lock_irqsave(&card->lock, flags);
3546 card->pm_suspended = 1;
3547 for (i = 0; i < NR_HW_CH; i++) {
3548 state = card->states[i];
3551 /* this happens only if there are open files */
3552 dmabuf = &state->dmabuf;
3553 if (dmabuf->enable & DAC_RUNNING ||
3555 && (dmabuf->trigger & PCM_ENABLE_OUTPUT))) {
3556 state->pm_saved_dac_rate = dmabuf->rate;
3559 state->pm_saved_dac_rate = 0;
3561 if (dmabuf->enable & ADC_RUNNING) {
3562 state->pm_saved_adc_rate = dmabuf->rate;
3565 state->pm_saved_adc_rate = 0;
3568 dmabuf->swptr = dmabuf->hwptr = 0;
3569 dmabuf->count = dmabuf->total_bytes = 0;
3572 spin_unlock_irqrestore(&card->lock, flags);
3573 /* save mixer settings */
3574 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3575 struct ac97_codec *codec = card->ac97_codec[num_ac97];
3578 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3579 if ((supported_mixer(codec, i)) && (codec->read_mixer)) {
3580 card->pm_saved_mixer_settings[i][num_ac97] = codec->read_mixer(codec, i);
3584 pci_save_state(dev, card->pm_save_state); /* XXX do we need this? */
3585 pci_disable_device(dev); /* disable busmastering */
3586 pci_set_power_state(dev, 3); /* Zzz. */
3591 static int ali_pm_resume(struct pci_dev *dev)
3593 int num_ac97, i = 0;
3594 struct ali_card *card = pci_get_drvdata(dev);
3595 pci_enable_device(dev);
3596 pci_restore_state(dev, card->pm_save_state);
3597 /* observation of a toshiba portege 3440ct suggests that the
3598 hardware has to be more or less completely reinitialized from
3599 scratch after an apm suspend. Works For Me. -dan */
3600 ali_ac97_random_init_stuff(card);
3601 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
3602 struct ac97_codec *codec = card->ac97_codec[num_ac97];
3603 /* check they haven't stolen the hardware while we were
3605 if (!codec || !ali_ac97_exists(card, num_ac97)) {
3611 if (!ali_ac97_probe_and_powerup(card, codec))
3613 if ((card->ac97_features & 0x0001)) {
3614 /* at probe time we found we could do variable
3615 rates, but APM suspend has made it forget
3616 its magical powers */
3617 if (!ali_ac97_enable_variable_rate(codec))
3620 /* we lost our mixer settings, so restore them */
3621 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
3622 if (supported_mixer(codec, i)) {
3623 int val = card->pm_saved_mixer_settings[i][num_ac97];
3624 codec->mixer_state[i] = val;
3625 codec->write_mixer(codec, i,
3627 ((val >> 8) & 0xff));
3632 /* we need to restore the sample rate from whatever it was */
3633 for (i = 0; i < NR_HW_CH; i++) {
3634 struct ali_state *state = card->states[i];
3636 if (state->pm_saved_adc_rate)
3637 ali_set_adc_rate(state, state->pm_saved_adc_rate);
3638 if (state->pm_saved_dac_rate)
3639 ali_set_dac_rate(state, state->pm_saved_dac_rate);
3643 card->pm_suspended = 0;
3644 /* any processes that were reading/writing during the suspend
3645 probably ended up here */
3646 for (i = 0; i < NR_HW_CH; i++) {
3647 struct ali_state *state = card->states[i];
3649 wake_up(&state->dmabuf.wait);
3653 #endif /* CONFIG_PM */
3656 MODULE_DESCRIPTION("ALI 5455 audio support");
3657 MODULE_LICENSE("GPL");
3658 MODULE_PARM(clocking, "i");
3659 MODULE_PARM(strict_clocking, "i");
3660 MODULE_PARM(codec_pcmout_share_spdif_locked, "i");
3661 MODULE_PARM(codec_independent_spdif_locked, "i");
3662 MODULE_PARM(controller_pcmout_share_spdif_locked, "i");
3663 MODULE_PARM(controller_independent_spdif_locked, "i");
3664 #define ALI5455_MODULE_NAME "ali5455"
3665 static struct pci_driver ali_pci_driver = {
3666 .name = ALI5455_MODULE_NAME,
3667 .id_table = ali_pci_tbl,
3669 .remove = __devexit_p(ali_remove),
3671 .suspend = ali_pm_suspend,
3672 .resume = ali_pm_resume,
3673 #endif /* CONFIG_PM */
3676 static int __init ali_init_module(void)
3678 printk(KERN_INFO "ALI 5455 + AC97 Audio, version "
3679 DRIVER_VERSION ", " __TIME__ " " __DATE__ "\n");
3681 if (codec_independent_spdif_locked > 0) {
3682 if (codec_independent_spdif_locked == 32000
3683 || codec_independent_spdif_locked == 44100
3684 || codec_independent_spdif_locked == 48000) {
3685 printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", codec_independent_spdif_locked);
3687 printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3688 codec_independent_spdif_locked = 0;
3691 if (controller_independent_spdif_locked > 0) {
3692 if (controller_independent_spdif_locked == 32000
3693 || controller_independent_spdif_locked == 44100
3694 || controller_independent_spdif_locked == 48000) {
3695 printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", controller_independent_spdif_locked);
3697 printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3698 controller_independent_spdif_locked = 0;
3702 if (codec_pcmout_share_spdif_locked > 0) {
3703 if (codec_pcmout_share_spdif_locked == 32000
3704 || codec_pcmout_share_spdif_locked == 44100
3705 || codec_pcmout_share_spdif_locked == 48000) {
3706 printk(KERN_INFO "ali_audio: Enabling S/PDIF at sample rate %dHz.\n", codec_pcmout_share_spdif_locked);
3708 printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3709 codec_pcmout_share_spdif_locked = 0;
3712 if (controller_pcmout_share_spdif_locked > 0) {
3713 if (controller_pcmout_share_spdif_locked == 32000
3714 || controller_pcmout_share_spdif_locked == 44100
3715 || controller_pcmout_share_spdif_locked == 48000) {
3716 printk(KERN_INFO "ali_audio: Enabling controller S/PDIF at sample rate %dHz.\n", controller_pcmout_share_spdif_locked);
3718 printk(KERN_INFO "ali_audio: S/PDIF can only be locked to 32000, 44100, or 48000Hz.\n");
3719 controller_pcmout_share_spdif_locked = 0;
3722 if (!pci_register_driver(&ali_pci_driver)) {
3723 pci_unregister_driver(&ali_pci_driver);
3729 static void __exit ali_cleanup_module(void)
3731 pci_unregister_driver(&ali_pci_driver);
3734 module_init(ali_init_module);
3735 module_exit(ali_cleanup_module);