Force this on for -next/mm testing purposes.
[linux-2.6/next.git] / sound / pci / sis7019.c
blobbcf61524a13f7ae92ee0abdc7a4cd0ac8294dfba
1 /*
2 * Driver for SiS7019 Audio Accelerator
4 * Copyright (C) 2004-2007, David Dillow
5 * Written by David Dillow <dave@thedillows.org>
6 * Inspired by the Trident 4D-WaveDX/NX driver.
8 * All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/init.h>
25 #include <linux/pci.h>
26 #include <linux/time.h>
27 #include <linux/slab.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <sound/core.h>
32 #include <sound/ac97_codec.h>
33 #include <sound/initval.h>
34 #include "sis7019.h"
36 MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
37 MODULE_DESCRIPTION("SiS7019");
38 MODULE_LICENSE("GPL");
39 MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
41 static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
42 static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
43 static int enable = 1;
45 module_param(index, int, 0444);
46 MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
47 module_param(id, charp, 0444);
48 MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
49 module_param(enable, bool, 0444);
50 MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
52 static DEFINE_PCI_DEVICE_TABLE(snd_sis7019_ids) = {
53 { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
54 { 0, }
57 MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
59 /* There are three timing modes for the voices.
61 * For both playback and capture, when the buffer is one or two periods long,
62 * we use the hardware's built-in Mid-Loop Interrupt and End-Loop Interrupt
63 * to let us know when the periods have ended.
65 * When performing playback with more than two periods per buffer, we set
66 * the "Stop Sample Offset" and tell the hardware to interrupt us when we
67 * reach it. We then update the offset and continue on until we are
68 * interrupted for the next period.
70 * Capture channels do not have a SSO, so we allocate a playback channel to
71 * use as a timer for the capture periods. We use the SSO on the playback
72 * channel to clock out virtual periods, and adjust the virtual period length
73 * to maintain synchronization. This algorithm came from the Trident driver.
75 * FIXME: It'd be nice to make use of some of the synth features in the
76 * hardware, but a woeful lack of documentation is a significant roadblock.
78 struct voice {
79 u16 flags;
80 #define VOICE_IN_USE 1
81 #define VOICE_CAPTURE 2
82 #define VOICE_SSO_TIMING 4
83 #define VOICE_SYNC_TIMING 8
84 u16 sync_cso;
85 u16 period_size;
86 u16 buffer_size;
87 u16 sync_period_size;
88 u16 sync_buffer_size;
89 u32 sso;
90 u32 vperiod;
91 struct snd_pcm_substream *substream;
92 struct voice *timing;
93 void __iomem *ctrl_base;
94 void __iomem *wave_base;
95 void __iomem *sync_base;
96 int num;
99 /* We need four pages to store our wave parameters during a suspend. If
100 * we're not doing power management, we still need to allocate a page
101 * for the silence buffer.
103 #ifdef CONFIG_PM
104 #define SIS_SUSPEND_PAGES 4
105 #else
106 #define SIS_SUSPEND_PAGES 1
107 #endif
109 struct sis7019 {
110 unsigned long ioport;
111 void __iomem *ioaddr;
112 int irq;
113 int codecs_present;
115 struct pci_dev *pci;
116 struct snd_pcm *pcm;
117 struct snd_card *card;
118 struct snd_ac97 *ac97[3];
120 /* Protect against more than one thread hitting the AC97
121 * registers (in a more polite manner than pounding the hardware
122 * semaphore)
124 struct mutex ac97_mutex;
126 /* voice_lock protects allocation/freeing of the voice descriptions
128 spinlock_t voice_lock;
130 struct voice voices[64];
131 struct voice capture_voice;
133 /* Allocate pages to store the internal wave state during
134 * suspends. When we're operating, this can be used as a silence
135 * buffer for a timing channel.
137 void *suspend_state[SIS_SUSPEND_PAGES];
139 int silence_users;
140 dma_addr_t silence_dma_addr;
143 #define SIS_PRIMARY_CODEC_PRESENT 0x0001
144 #define SIS_SECONDARY_CODEC_PRESENT 0x0002
145 #define SIS_TERTIARY_CODEC_PRESENT 0x0004
147 /* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
148 * documented range of 8-0xfff8 samples. Given that they are 0-based,
149 * that places our period/buffer range at 9-0xfff9 samples. That makes the
150 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
151 * max samples / min samples gives us the max periods in a buffer.
153 * We'll add a constraint upon open that limits the period and buffer sample
154 * size to values that are legal for the hardware.
156 static struct snd_pcm_hardware sis_playback_hw_info = {
157 .info = (SNDRV_PCM_INFO_MMAP |
158 SNDRV_PCM_INFO_MMAP_VALID |
159 SNDRV_PCM_INFO_INTERLEAVED |
160 SNDRV_PCM_INFO_BLOCK_TRANSFER |
161 SNDRV_PCM_INFO_SYNC_START |
162 SNDRV_PCM_INFO_RESUME),
163 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
164 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
165 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
166 .rate_min = 4000,
167 .rate_max = 48000,
168 .channels_min = 1,
169 .channels_max = 2,
170 .buffer_bytes_max = (0xfff9 * 4),
171 .period_bytes_min = 9,
172 .period_bytes_max = (0xfff9 * 4),
173 .periods_min = 1,
174 .periods_max = (0xfff9 / 9),
177 static struct snd_pcm_hardware sis_capture_hw_info = {
178 .info = (SNDRV_PCM_INFO_MMAP |
179 SNDRV_PCM_INFO_MMAP_VALID |
180 SNDRV_PCM_INFO_INTERLEAVED |
181 SNDRV_PCM_INFO_BLOCK_TRANSFER |
182 SNDRV_PCM_INFO_SYNC_START |
183 SNDRV_PCM_INFO_RESUME),
184 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
185 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
186 .rates = SNDRV_PCM_RATE_48000,
187 .rate_min = 4000,
188 .rate_max = 48000,
189 .channels_min = 1,
190 .channels_max = 2,
191 .buffer_bytes_max = (0xfff9 * 4),
192 .period_bytes_min = 9,
193 .period_bytes_max = (0xfff9 * 4),
194 .periods_min = 1,
195 .periods_max = (0xfff9 / 9),
198 static void sis_update_sso(struct voice *voice, u16 period)
200 void __iomem *base = voice->ctrl_base;
202 voice->sso += period;
203 if (voice->sso >= voice->buffer_size)
204 voice->sso -= voice->buffer_size;
206 /* Enforce the documented hardware minimum offset */
207 if (voice->sso < 8)
208 voice->sso = 8;
210 /* The SSO is in the upper 16 bits of the register. */
211 writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
214 static void sis_update_voice(struct voice *voice)
216 if (voice->flags & VOICE_SSO_TIMING) {
217 sis_update_sso(voice, voice->period_size);
218 } else if (voice->flags & VOICE_SYNC_TIMING) {
219 int sync;
221 /* If we've not hit the end of the virtual period, update
222 * our records and keep going.
224 if (voice->vperiod > voice->period_size) {
225 voice->vperiod -= voice->period_size;
226 if (voice->vperiod < voice->period_size)
227 sis_update_sso(voice, voice->vperiod);
228 else
229 sis_update_sso(voice, voice->period_size);
230 return;
233 /* Calculate our relative offset between the target and
234 * the actual CSO value. Since we're operating in a loop,
235 * if the value is more than half way around, we can
236 * consider ourselves wrapped.
238 sync = voice->sync_cso;
239 sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
240 if (sync > (voice->sync_buffer_size / 2))
241 sync -= voice->sync_buffer_size;
243 /* If sync is positive, then we interrupted too early, and
244 * we'll need to come back in a few samples and try again.
245 * There's a minimum wait, as it takes some time for the DMA
246 * engine to startup, etc...
248 if (sync > 0) {
249 if (sync < 16)
250 sync = 16;
251 sis_update_sso(voice, sync);
252 return;
255 /* Ok, we interrupted right on time, or (hopefully) just
256 * a bit late. We'll adjst our next waiting period based
257 * on how close we got.
259 * We need to stay just behind the actual channel to ensure
260 * it really is past a period when we get our interrupt --
261 * otherwise we'll fall into the early code above and have
262 * a minimum wait time, which makes us quite late here,
263 * eating into the user's time to refresh the buffer, esp.
264 * if using small periods.
266 * If we're less than 9 samples behind, we're on target.
267 * Otherwise, shorten the next vperiod by the amount we've
268 * been delayed.
270 if (sync > -9)
271 voice->vperiod = voice->sync_period_size + 1;
272 else
273 voice->vperiod = voice->sync_period_size + sync + 10;
275 if (voice->vperiod < voice->buffer_size) {
276 sis_update_sso(voice, voice->vperiod);
277 voice->vperiod = 0;
278 } else
279 sis_update_sso(voice, voice->period_size);
281 sync = voice->sync_cso + voice->sync_period_size;
282 if (sync >= voice->sync_buffer_size)
283 sync -= voice->sync_buffer_size;
284 voice->sync_cso = sync;
287 snd_pcm_period_elapsed(voice->substream);
290 static void sis_voice_irq(u32 status, struct voice *voice)
292 int bit;
294 while (status) {
295 bit = __ffs(status);
296 status >>= bit + 1;
297 voice += bit;
298 sis_update_voice(voice);
299 voice++;
303 static irqreturn_t sis_interrupt(int irq, void *dev)
305 struct sis7019 *sis = dev;
306 unsigned long io = sis->ioport;
307 struct voice *voice;
308 u32 intr, status;
310 /* We only use the DMA interrupts, and we don't enable any other
311 * source of interrupts. But, it is possible to see an interrupt
312 * status that didn't actually interrupt us, so eliminate anything
313 * we're not expecting to avoid falsely claiming an IRQ, and an
314 * ensuing endless loop.
316 intr = inl(io + SIS_GISR);
317 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
318 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
319 if (!intr)
320 return IRQ_NONE;
322 do {
323 status = inl(io + SIS_PISR_A);
324 if (status) {
325 sis_voice_irq(status, sis->voices);
326 outl(status, io + SIS_PISR_A);
329 status = inl(io + SIS_PISR_B);
330 if (status) {
331 sis_voice_irq(status, &sis->voices[32]);
332 outl(status, io + SIS_PISR_B);
335 status = inl(io + SIS_RISR);
336 if (status) {
337 voice = &sis->capture_voice;
338 if (!voice->timing)
339 snd_pcm_period_elapsed(voice->substream);
341 outl(status, io + SIS_RISR);
344 outl(intr, io + SIS_GISR);
345 intr = inl(io + SIS_GISR);
346 intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
347 SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
348 } while (intr);
350 return IRQ_HANDLED;
353 static u32 sis_rate_to_delta(unsigned int rate)
355 u32 delta;
357 /* This was copied from the trident driver, but it seems its gotten
358 * around a bit... nevertheless, it works well.
360 * We special case 44100 and 8000 since rounding with the equation
361 * does not give us an accurate enough value. For 11025 and 22050
362 * the equation gives us the best answer. All other frequencies will
363 * also use the equation. JDW
365 if (rate == 44100)
366 delta = 0xeb3;
367 else if (rate == 8000)
368 delta = 0x2ab;
369 else if (rate == 48000)
370 delta = 0x1000;
371 else
372 delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
373 return delta;
376 static void __sis_map_silence(struct sis7019 *sis)
378 /* Helper function: must hold sis->voice_lock on entry */
379 if (!sis->silence_users)
380 sis->silence_dma_addr = pci_map_single(sis->pci,
381 sis->suspend_state[0],
382 4096, PCI_DMA_TODEVICE);
383 sis->silence_users++;
386 static void __sis_unmap_silence(struct sis7019 *sis)
388 /* Helper function: must hold sis->voice_lock on entry */
389 sis->silence_users--;
390 if (!sis->silence_users)
391 pci_unmap_single(sis->pci, sis->silence_dma_addr, 4096,
392 PCI_DMA_TODEVICE);
395 static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
397 unsigned long flags;
399 spin_lock_irqsave(&sis->voice_lock, flags);
400 if (voice->timing) {
401 __sis_unmap_silence(sis);
402 voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
403 VOICE_SYNC_TIMING);
404 voice->timing = NULL;
406 voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
407 spin_unlock_irqrestore(&sis->voice_lock, flags);
410 static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
412 /* Must hold the voice_lock on entry */
413 struct voice *voice;
414 int i;
416 for (i = 0; i < 64; i++) {
417 voice = &sis->voices[i];
418 if (voice->flags & VOICE_IN_USE)
419 continue;
420 voice->flags |= VOICE_IN_USE;
421 goto found_one;
423 voice = NULL;
425 found_one:
426 return voice;
429 static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
431 struct voice *voice;
432 unsigned long flags;
434 spin_lock_irqsave(&sis->voice_lock, flags);
435 voice = __sis_alloc_playback_voice(sis);
436 spin_unlock_irqrestore(&sis->voice_lock, flags);
438 return voice;
441 static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
442 struct snd_pcm_hw_params *hw_params)
444 struct sis7019 *sis = snd_pcm_substream_chip(substream);
445 struct snd_pcm_runtime *runtime = substream->runtime;
446 struct voice *voice = runtime->private_data;
447 unsigned int period_size, buffer_size;
448 unsigned long flags;
449 int needed;
451 /* If there are one or two periods per buffer, we don't need a
452 * timing voice, as we can use the capture channel's interrupts
453 * to clock out the periods.
455 period_size = params_period_size(hw_params);
456 buffer_size = params_buffer_size(hw_params);
457 needed = (period_size != buffer_size &&
458 period_size != (buffer_size / 2));
460 if (needed && !voice->timing) {
461 spin_lock_irqsave(&sis->voice_lock, flags);
462 voice->timing = __sis_alloc_playback_voice(sis);
463 if (voice->timing)
464 __sis_map_silence(sis);
465 spin_unlock_irqrestore(&sis->voice_lock, flags);
466 if (!voice->timing)
467 return -ENOMEM;
468 voice->timing->substream = substream;
469 } else if (!needed && voice->timing) {
470 sis_free_voice(sis, voice);
471 voice->timing = NULL;
474 return 0;
477 static int sis_playback_open(struct snd_pcm_substream *substream)
479 struct sis7019 *sis = snd_pcm_substream_chip(substream);
480 struct snd_pcm_runtime *runtime = substream->runtime;
481 struct voice *voice;
483 voice = sis_alloc_playback_voice(sis);
484 if (!voice)
485 return -EAGAIN;
487 voice->substream = substream;
488 runtime->private_data = voice;
489 runtime->hw = sis_playback_hw_info;
490 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
491 9, 0xfff9);
492 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
493 9, 0xfff9);
494 snd_pcm_set_sync(substream);
495 return 0;
498 static int sis_substream_close(struct snd_pcm_substream *substream)
500 struct sis7019 *sis = snd_pcm_substream_chip(substream);
501 struct snd_pcm_runtime *runtime = substream->runtime;
502 struct voice *voice = runtime->private_data;
504 sis_free_voice(sis, voice);
505 return 0;
508 static int sis_playback_hw_params(struct snd_pcm_substream *substream,
509 struct snd_pcm_hw_params *hw_params)
511 return snd_pcm_lib_malloc_pages(substream,
512 params_buffer_bytes(hw_params));
515 static int sis_hw_free(struct snd_pcm_substream *substream)
517 return snd_pcm_lib_free_pages(substream);
520 static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
522 struct snd_pcm_runtime *runtime = substream->runtime;
523 struct voice *voice = runtime->private_data;
524 void __iomem *ctrl_base = voice->ctrl_base;
525 void __iomem *wave_base = voice->wave_base;
526 u32 format, dma_addr, control, sso_eso, delta, reg;
527 u16 leo;
529 /* We rely on the PCM core to ensure that the parameters for this
530 * substream do not change on us while we're programming the HW.
532 format = 0;
533 if (snd_pcm_format_width(runtime->format) == 8)
534 format |= SIS_PLAY_DMA_FORMAT_8BIT;
535 if (!snd_pcm_format_signed(runtime->format))
536 format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
537 if (runtime->channels == 1)
538 format |= SIS_PLAY_DMA_FORMAT_MONO;
540 /* The baseline setup is for a single period per buffer, and
541 * we add bells and whistles as needed from there.
543 dma_addr = runtime->dma_addr;
544 leo = runtime->buffer_size - 1;
545 control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
546 sso_eso = leo;
548 if (runtime->period_size == (runtime->buffer_size / 2)) {
549 control |= SIS_PLAY_DMA_INTR_AT_MLP;
550 } else if (runtime->period_size != runtime->buffer_size) {
551 voice->flags |= VOICE_SSO_TIMING;
552 voice->sso = runtime->period_size - 1;
553 voice->period_size = runtime->period_size;
554 voice->buffer_size = runtime->buffer_size;
556 control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
557 control |= SIS_PLAY_DMA_INTR_AT_SSO;
558 sso_eso |= (runtime->period_size - 1) << 16;
561 delta = sis_rate_to_delta(runtime->rate);
563 /* Ok, we're ready to go, set up the channel.
565 writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
566 writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
567 writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
568 writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
570 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
571 writel(0, wave_base + reg);
573 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
574 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
575 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
576 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
577 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
578 wave_base + SIS_WAVE_CHANNEL_CONTROL);
580 /* Force PCI writes to post. */
581 readl(ctrl_base);
583 return 0;
586 static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
588 struct sis7019 *sis = snd_pcm_substream_chip(substream);
589 unsigned long io = sis->ioport;
590 struct snd_pcm_substream *s;
591 struct voice *voice;
592 void *chip;
593 int starting;
594 u32 record = 0;
595 u32 play[2] = { 0, 0 };
597 /* No locks needed, as the PCM core will hold the locks on the
598 * substreams, and the HW will only start/stop the indicated voices
599 * without changing the state of the others.
601 switch (cmd) {
602 case SNDRV_PCM_TRIGGER_START:
603 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
604 case SNDRV_PCM_TRIGGER_RESUME:
605 starting = 1;
606 break;
607 case SNDRV_PCM_TRIGGER_STOP:
608 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
609 case SNDRV_PCM_TRIGGER_SUSPEND:
610 starting = 0;
611 break;
612 default:
613 return -EINVAL;
616 snd_pcm_group_for_each_entry(s, substream) {
617 /* Make sure it is for us... */
618 chip = snd_pcm_substream_chip(s);
619 if (chip != sis)
620 continue;
622 voice = s->runtime->private_data;
623 if (voice->flags & VOICE_CAPTURE) {
624 record |= 1 << voice->num;
625 voice = voice->timing;
628 /* voice could be NULL if this a recording stream, and it
629 * doesn't have an external timing channel.
631 if (voice)
632 play[voice->num / 32] |= 1 << (voice->num & 0x1f);
634 snd_pcm_trigger_done(s, substream);
637 if (starting) {
638 if (record)
639 outl(record, io + SIS_RECORD_START_REG);
640 if (play[0])
641 outl(play[0], io + SIS_PLAY_START_A_REG);
642 if (play[1])
643 outl(play[1], io + SIS_PLAY_START_B_REG);
644 } else {
645 if (record)
646 outl(record, io + SIS_RECORD_STOP_REG);
647 if (play[0])
648 outl(play[0], io + SIS_PLAY_STOP_A_REG);
649 if (play[1])
650 outl(play[1], io + SIS_PLAY_STOP_B_REG);
652 return 0;
655 static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
657 struct snd_pcm_runtime *runtime = substream->runtime;
658 struct voice *voice = runtime->private_data;
659 u32 cso;
661 cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
662 cso &= 0xffff;
663 return cso;
666 static int sis_capture_open(struct snd_pcm_substream *substream)
668 struct sis7019 *sis = snd_pcm_substream_chip(substream);
669 struct snd_pcm_runtime *runtime = substream->runtime;
670 struct voice *voice = &sis->capture_voice;
671 unsigned long flags;
673 /* FIXME: The driver only supports recording from one channel
674 * at the moment, but it could support more.
676 spin_lock_irqsave(&sis->voice_lock, flags);
677 if (voice->flags & VOICE_IN_USE)
678 voice = NULL;
679 else
680 voice->flags |= VOICE_IN_USE;
681 spin_unlock_irqrestore(&sis->voice_lock, flags);
683 if (!voice)
684 return -EAGAIN;
686 voice->substream = substream;
687 runtime->private_data = voice;
688 runtime->hw = sis_capture_hw_info;
689 runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
690 snd_pcm_limit_hw_rates(runtime);
691 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
692 9, 0xfff9);
693 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
694 9, 0xfff9);
695 snd_pcm_set_sync(substream);
696 return 0;
699 static int sis_capture_hw_params(struct snd_pcm_substream *substream,
700 struct snd_pcm_hw_params *hw_params)
702 struct sis7019 *sis = snd_pcm_substream_chip(substream);
703 int rc;
705 rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
706 params_rate(hw_params));
707 if (rc)
708 goto out;
710 rc = snd_pcm_lib_malloc_pages(substream,
711 params_buffer_bytes(hw_params));
712 if (rc < 0)
713 goto out;
715 rc = sis_alloc_timing_voice(substream, hw_params);
717 out:
718 return rc;
721 static void sis_prepare_timing_voice(struct voice *voice,
722 struct snd_pcm_substream *substream)
724 struct sis7019 *sis = snd_pcm_substream_chip(substream);
725 struct snd_pcm_runtime *runtime = substream->runtime;
726 struct voice *timing = voice->timing;
727 void __iomem *play_base = timing->ctrl_base;
728 void __iomem *wave_base = timing->wave_base;
729 u16 buffer_size, period_size;
730 u32 format, control, sso_eso, delta;
731 u32 vperiod, sso, reg;
733 /* Set our initial buffer and period as large as we can given a
734 * single page of silence.
736 buffer_size = 4096 / runtime->channels;
737 buffer_size /= snd_pcm_format_size(runtime->format, 1);
738 period_size = buffer_size;
740 /* Initially, we want to interrupt just a bit behind the end of
741 * the period we're clocking out. 12 samples seems to give a good
742 * delay.
744 * We want to spread our interrupts throughout the virtual period,
745 * so that we don't end up with two interrupts back to back at the
746 * end -- this helps minimize the effects of any jitter. Adjust our
747 * clocking period size so that the last period is at least a fourth
748 * of a full period.
750 * This is all moot if we don't need to use virtual periods.
752 vperiod = runtime->period_size + 12;
753 if (vperiod > period_size) {
754 u16 tail = vperiod % period_size;
755 u16 quarter_period = period_size / 4;
757 if (tail && tail < quarter_period) {
758 u16 loops = vperiod / period_size;
760 tail = quarter_period - tail;
761 tail += loops - 1;
762 tail /= loops;
763 period_size -= tail;
766 sso = period_size - 1;
767 } else {
768 /* The initial period will fit inside the buffer, so we
769 * don't need to use virtual periods -- disable them.
771 period_size = runtime->period_size;
772 sso = vperiod - 1;
773 vperiod = 0;
776 /* The interrupt handler implements the timing synchronization, so
777 * setup its state.
779 timing->flags |= VOICE_SYNC_TIMING;
780 timing->sync_base = voice->ctrl_base;
781 timing->sync_cso = runtime->period_size;
782 timing->sync_period_size = runtime->period_size;
783 timing->sync_buffer_size = runtime->buffer_size;
784 timing->period_size = period_size;
785 timing->buffer_size = buffer_size;
786 timing->sso = sso;
787 timing->vperiod = vperiod;
789 /* Using unsigned samples with the all-zero silence buffer
790 * forces the output to the lower rail, killing playback.
791 * So ignore unsigned vs signed -- it doesn't change the timing.
793 format = 0;
794 if (snd_pcm_format_width(runtime->format) == 8)
795 format = SIS_CAPTURE_DMA_FORMAT_8BIT;
796 if (runtime->channels == 1)
797 format |= SIS_CAPTURE_DMA_FORMAT_MONO;
799 control = timing->buffer_size - 1;
800 control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
801 sso_eso = timing->buffer_size - 1;
802 sso_eso |= timing->sso << 16;
804 delta = sis_rate_to_delta(runtime->rate);
806 /* We've done the math, now configure the channel.
808 writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
809 writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
810 writel(control, play_base + SIS_PLAY_DMA_CONTROL);
811 writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
813 for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
814 writel(0, wave_base + reg);
816 writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
817 writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
818 writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
819 SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
820 SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
821 wave_base + SIS_WAVE_CHANNEL_CONTROL);
824 static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
826 struct snd_pcm_runtime *runtime = substream->runtime;
827 struct voice *voice = runtime->private_data;
828 void __iomem *rec_base = voice->ctrl_base;
829 u32 format, dma_addr, control;
830 u16 leo;
832 /* We rely on the PCM core to ensure that the parameters for this
833 * substream do not change on us while we're programming the HW.
835 format = 0;
836 if (snd_pcm_format_width(runtime->format) == 8)
837 format = SIS_CAPTURE_DMA_FORMAT_8BIT;
838 if (!snd_pcm_format_signed(runtime->format))
839 format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
840 if (runtime->channels == 1)
841 format |= SIS_CAPTURE_DMA_FORMAT_MONO;
843 dma_addr = runtime->dma_addr;
844 leo = runtime->buffer_size - 1;
845 control = leo | SIS_CAPTURE_DMA_LOOP;
847 /* If we've got more than two periods per buffer, then we have
848 * use a timing voice to clock out the periods. Otherwise, we can
849 * use the capture channel's interrupts.
851 if (voice->timing) {
852 sis_prepare_timing_voice(voice, substream);
853 } else {
854 control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
855 if (runtime->period_size != runtime->buffer_size)
856 control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
859 writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
860 writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
861 writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
863 /* Force the writes to post. */
864 readl(rec_base);
866 return 0;
869 static struct snd_pcm_ops sis_playback_ops = {
870 .open = sis_playback_open,
871 .close = sis_substream_close,
872 .ioctl = snd_pcm_lib_ioctl,
873 .hw_params = sis_playback_hw_params,
874 .hw_free = sis_hw_free,
875 .prepare = sis_pcm_playback_prepare,
876 .trigger = sis_pcm_trigger,
877 .pointer = sis_pcm_pointer,
880 static struct snd_pcm_ops sis_capture_ops = {
881 .open = sis_capture_open,
882 .close = sis_substream_close,
883 .ioctl = snd_pcm_lib_ioctl,
884 .hw_params = sis_capture_hw_params,
885 .hw_free = sis_hw_free,
886 .prepare = sis_pcm_capture_prepare,
887 .trigger = sis_pcm_trigger,
888 .pointer = sis_pcm_pointer,
891 static int __devinit sis_pcm_create(struct sis7019 *sis)
893 struct snd_pcm *pcm;
894 int rc;
896 /* We have 64 voices, and the driver currently records from
897 * only one channel, though that could change in the future.
899 rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
900 if (rc)
901 return rc;
903 pcm->private_data = sis;
904 strcpy(pcm->name, "SiS7019");
905 sis->pcm = pcm;
907 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
908 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
910 /* Try to preallocate some memory, but it's not the end of the
911 * world if this fails.
913 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
914 snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
916 return 0;
919 static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
921 unsigned long io = sis->ioport;
922 unsigned short val = 0xffff;
923 u16 status;
924 u16 rdy;
925 int count;
926 static const u16 codec_ready[3] = {
927 SIS_AC97_STATUS_CODEC_READY,
928 SIS_AC97_STATUS_CODEC2_READY,
929 SIS_AC97_STATUS_CODEC3_READY,
932 rdy = codec_ready[codec];
935 /* Get the AC97 semaphore -- software first, so we don't spin
936 * pounding out IO reads on the hardware semaphore...
938 mutex_lock(&sis->ac97_mutex);
940 count = 0xffff;
941 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
942 udelay(1);
944 if (!count)
945 goto timeout;
947 /* ... and wait for any outstanding commands to complete ...
949 count = 0xffff;
950 do {
951 status = inw(io + SIS_AC97_STATUS);
952 if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
953 break;
955 udelay(1);
956 } while (--count);
958 if (!count)
959 goto timeout_sema;
961 /* ... before sending our command and waiting for it to finish ...
963 outl(cmd, io + SIS_AC97_CMD);
964 udelay(10);
966 count = 0xffff;
967 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
968 udelay(1);
970 /* ... and reading the results (if any).
972 val = inl(io + SIS_AC97_CMD) >> 16;
974 timeout_sema:
975 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
976 timeout:
977 mutex_unlock(&sis->ac97_mutex);
979 if (!count) {
980 printk(KERN_ERR "sis7019: ac97 codec %d timeout cmd 0x%08x\n",
981 codec, cmd);
984 return val;
987 static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
988 unsigned short val)
990 static const u32 cmd[3] = {
991 SIS_AC97_CMD_CODEC_WRITE,
992 SIS_AC97_CMD_CODEC2_WRITE,
993 SIS_AC97_CMD_CODEC3_WRITE,
995 sis_ac97_rw(ac97->private_data, ac97->num,
996 (val << 16) | (reg << 8) | cmd[ac97->num]);
999 static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
1001 static const u32 cmd[3] = {
1002 SIS_AC97_CMD_CODEC_READ,
1003 SIS_AC97_CMD_CODEC2_READ,
1004 SIS_AC97_CMD_CODEC3_READ,
1006 return sis_ac97_rw(ac97->private_data, ac97->num,
1007 (reg << 8) | cmd[ac97->num]);
1010 static int __devinit sis_mixer_create(struct sis7019 *sis)
1012 struct snd_ac97_bus *bus;
1013 struct snd_ac97_template ac97;
1014 static struct snd_ac97_bus_ops ops = {
1015 .write = sis_ac97_write,
1016 .read = sis_ac97_read,
1018 int rc;
1020 memset(&ac97, 0, sizeof(ac97));
1021 ac97.private_data = sis;
1023 rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
1024 if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1025 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
1026 ac97.num = 1;
1027 if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1028 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1029 ac97.num = 2;
1030 if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1031 rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1033 /* If we return an error here, then snd_card_free() should
1034 * free up any ac97 codecs that got created, as well as the bus.
1036 return rc;
1039 static void sis_free_suspend(struct sis7019 *sis)
1041 int i;
1043 for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1044 kfree(sis->suspend_state[i]);
1047 static int sis_chip_free(struct sis7019 *sis)
1049 /* Reset the chip, and disable all interrputs.
1051 outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1052 udelay(25);
1053 outl(0, sis->ioport + SIS_GCR);
1054 outl(0, sis->ioport + SIS_GIER);
1056 /* Now, free everything we allocated.
1058 if (sis->irq >= 0)
1059 free_irq(sis->irq, sis);
1061 if (sis->ioaddr)
1062 iounmap(sis->ioaddr);
1064 pci_release_regions(sis->pci);
1065 pci_disable_device(sis->pci);
1067 sis_free_suspend(sis);
1068 return 0;
1071 static int sis_dev_free(struct snd_device *dev)
1073 struct sis7019 *sis = dev->device_data;
1074 return sis_chip_free(sis);
1077 static int sis_chip_init(struct sis7019 *sis)
1079 unsigned long io = sis->ioport;
1080 void __iomem *ioaddr = sis->ioaddr;
1081 u16 status;
1082 int count;
1083 int i;
1085 /* Reset the audio controller
1087 outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1088 udelay(25);
1089 outl(0, io + SIS_GCR);
1091 /* Get the AC-link semaphore, and reset the codecs
1093 count = 0xffff;
1094 while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1095 udelay(1);
1097 if (!count)
1098 return -EIO;
1100 outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1101 udelay(250);
1103 count = 0xffff;
1104 while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1105 udelay(1);
1107 /* Now that we've finished the reset, find out what's attached.
1109 status = inl(io + SIS_AC97_STATUS);
1110 if (status & SIS_AC97_STATUS_CODEC_READY)
1111 sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1112 if (status & SIS_AC97_STATUS_CODEC2_READY)
1113 sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1114 if (status & SIS_AC97_STATUS_CODEC3_READY)
1115 sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1117 /* All done, let go of the semaphore, and check for errors
1119 outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1120 if (!sis->codecs_present || !count)
1121 return -EIO;
1123 /* Let the hardware know that the audio driver is alive,
1124 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1125 * record channels. We're going to want to use Variable Rate Audio
1126 * for recording, to avoid needlessly resampling from 48kHZ.
1128 outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1129 outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1130 SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1131 SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1132 SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1134 /* All AC97 PCM slots should be sourced from sub-mixer 0.
1136 outl(0, io + SIS_AC97_PSR);
1138 /* There is only one valid DMA setup for a PCI environment.
1140 outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1142 /* Reset the synchronization groups for all of the channels
1143 * to be asyncronous. If we start doing SPDIF or 5.1 sound, etc.
1144 * we'll need to change how we handle these. Until then, we just
1145 * assign sub-mixer 0 to all playback channels, and avoid any
1146 * attenuation on the audio.
1148 outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1149 outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1150 outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1151 outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1152 outl(0, io + SIS_MIXER_SYNC_GROUP);
1154 for (i = 0; i < 64; i++) {
1155 writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1156 writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1157 SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1160 /* Don't attenuate any audio set for the wave amplifier.
1162 * FIXME: Maximum attenuation is set for the music amp, which will
1163 * need to change if we start using the synth engine.
1165 outl(0xffff0000, io + SIS_WEVCR);
1167 /* Ensure that the wave engine is in normal operating mode.
1169 outl(0, io + SIS_WECCR);
1171 /* Go ahead and enable the DMA interrupts. They won't go live
1172 * until we start a channel.
1174 outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1175 SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1177 return 0;
1180 #ifdef CONFIG_PM
1181 static int sis_suspend(struct pci_dev *pci, pm_message_t state)
1183 struct snd_card *card = pci_get_drvdata(pci);
1184 struct sis7019 *sis = card->private_data;
1185 void __iomem *ioaddr = sis->ioaddr;
1186 int i;
1188 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1189 snd_pcm_suspend_all(sis->pcm);
1190 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1191 snd_ac97_suspend(sis->ac97[0]);
1192 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1193 snd_ac97_suspend(sis->ac97[1]);
1194 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1195 snd_ac97_suspend(sis->ac97[2]);
1197 /* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1199 if (sis->irq >= 0) {
1200 free_irq(sis->irq, sis);
1201 sis->irq = -1;
1204 /* Save the internal state away
1206 for (i = 0; i < 4; i++) {
1207 memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1208 ioaddr += 4096;
1211 pci_disable_device(pci);
1212 pci_save_state(pci);
1213 pci_set_power_state(pci, pci_choose_state(pci, state));
1214 return 0;
1217 static int sis_resume(struct pci_dev *pci)
1219 struct snd_card *card = pci_get_drvdata(pci);
1220 struct sis7019 *sis = card->private_data;
1221 void __iomem *ioaddr = sis->ioaddr;
1222 int i;
1224 pci_set_power_state(pci, PCI_D0);
1225 pci_restore_state(pci);
1227 if (pci_enable_device(pci) < 0) {
1228 printk(KERN_ERR "sis7019: unable to re-enable device\n");
1229 goto error;
1232 if (sis_chip_init(sis)) {
1233 printk(KERN_ERR "sis7019: unable to re-init controller\n");
1234 goto error;
1237 if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
1238 KBUILD_MODNAME, sis)) {
1239 printk(KERN_ERR "sis7019: unable to regain IRQ %d\n", pci->irq);
1240 goto error;
1243 /* Restore saved state, then clear out the page we use for the
1244 * silence buffer.
1246 for (i = 0; i < 4; i++) {
1247 memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1248 ioaddr += 4096;
1251 memset(sis->suspend_state[0], 0, 4096);
1253 sis->irq = pci->irq;
1254 pci_set_master(pci);
1256 if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1257 snd_ac97_resume(sis->ac97[0]);
1258 if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1259 snd_ac97_resume(sis->ac97[1]);
1260 if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1261 snd_ac97_resume(sis->ac97[2]);
1263 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1264 return 0;
1266 error:
1267 snd_card_disconnect(card);
1268 return -EIO;
1270 #endif /* CONFIG_PM */
1272 static int sis_alloc_suspend(struct sis7019 *sis)
1274 int i;
1276 /* We need 16K to store the internal wave engine state during a
1277 * suspend, but we don't need it to be contiguous, so play nice
1278 * with the memory system. We'll also use this area for a silence
1279 * buffer.
1281 for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1282 sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1283 if (!sis->suspend_state[i])
1284 return -ENOMEM;
1286 memset(sis->suspend_state[0], 0, 4096);
1288 return 0;
1291 static int __devinit sis_chip_create(struct snd_card *card,
1292 struct pci_dev *pci)
1294 struct sis7019 *sis = card->private_data;
1295 struct voice *voice;
1296 static struct snd_device_ops ops = {
1297 .dev_free = sis_dev_free,
1299 int rc;
1300 int i;
1302 rc = pci_enable_device(pci);
1303 if (rc)
1304 goto error_out;
1306 if (pci_set_dma_mask(pci, DMA_BIT_MASK(30)) < 0) {
1307 printk(KERN_ERR "sis7019: architecture does not support "
1308 "30-bit PCI busmaster DMA");
1309 goto error_out_enabled;
1312 memset(sis, 0, sizeof(*sis));
1313 mutex_init(&sis->ac97_mutex);
1314 spin_lock_init(&sis->voice_lock);
1315 sis->card = card;
1316 sis->pci = pci;
1317 sis->irq = -1;
1318 sis->ioport = pci_resource_start(pci, 0);
1320 rc = pci_request_regions(pci, "SiS7019");
1321 if (rc) {
1322 printk(KERN_ERR "sis7019: unable request regions\n");
1323 goto error_out_enabled;
1326 rc = -EIO;
1327 sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1328 if (!sis->ioaddr) {
1329 printk(KERN_ERR "sis7019: unable to remap MMIO, aborting\n");
1330 goto error_out_cleanup;
1333 rc = sis_alloc_suspend(sis);
1334 if (rc < 0) {
1335 printk(KERN_ERR "sis7019: unable to allocate state storage\n");
1336 goto error_out_cleanup;
1339 rc = sis_chip_init(sis);
1340 if (rc)
1341 goto error_out_cleanup;
1343 if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
1344 KBUILD_MODNAME, sis)) {
1345 printk(KERN_ERR "unable to allocate irq %d\n", sis->irq);
1346 goto error_out_cleanup;
1349 sis->irq = pci->irq;
1350 pci_set_master(pci);
1352 for (i = 0; i < 64; i++) {
1353 voice = &sis->voices[i];
1354 voice->num = i;
1355 voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1356 voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1359 voice = &sis->capture_voice;
1360 voice->flags = VOICE_CAPTURE;
1361 voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1362 voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1364 rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1365 if (rc)
1366 goto error_out_cleanup;
1368 snd_card_set_dev(card, &pci->dev);
1370 return 0;
1372 error_out_cleanup:
1373 sis_chip_free(sis);
1375 error_out_enabled:
1376 pci_disable_device(pci);
1378 error_out:
1379 return rc;
1382 static int __devinit snd_sis7019_probe(struct pci_dev *pci,
1383 const struct pci_device_id *pci_id)
1385 struct snd_card *card;
1386 struct sis7019 *sis;
1387 int rc;
1389 rc = -ENOENT;
1390 if (!enable)
1391 goto error_out;
1393 rc = snd_card_create(index, id, THIS_MODULE, sizeof(*sis), &card);
1394 if (rc < 0)
1395 goto error_out;
1397 strcpy(card->driver, "SiS7019");
1398 strcpy(card->shortname, "SiS7019");
1399 rc = sis_chip_create(card, pci);
1400 if (rc)
1401 goto card_error_out;
1403 sis = card->private_data;
1405 rc = sis_mixer_create(sis);
1406 if (rc)
1407 goto card_error_out;
1409 rc = sis_pcm_create(sis);
1410 if (rc)
1411 goto card_error_out;
1413 snprintf(card->longname, sizeof(card->longname),
1414 "%s Audio Accelerator with %s at 0x%lx, irq %d",
1415 card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1416 sis->ioport, sis->irq);
1418 rc = snd_card_register(card);
1419 if (rc)
1420 goto card_error_out;
1422 pci_set_drvdata(pci, card);
1423 return 0;
1425 card_error_out:
1426 snd_card_free(card);
1428 error_out:
1429 return rc;
1432 static void __devexit snd_sis7019_remove(struct pci_dev *pci)
1434 snd_card_free(pci_get_drvdata(pci));
1435 pci_set_drvdata(pci, NULL);
1438 static struct pci_driver sis7019_driver = {
1439 .name = KBUILD_MODNAME,
1440 .id_table = snd_sis7019_ids,
1441 .probe = snd_sis7019_probe,
1442 .remove = __devexit_p(snd_sis7019_remove),
1444 #ifdef CONFIG_PM
1445 .suspend = sis_suspend,
1446 .resume = sis_resume,
1447 #endif
1450 static int __init sis7019_init(void)
1452 return pci_register_driver(&sis7019_driver);
1455 static void __exit sis7019_exit(void)
1457 pci_unregister_driver(&sis7019_driver);
1460 module_init(sis7019_init);
1461 module_exit(sis7019_exit);