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