1/*
2 *  Driver for SiS7019 Audio Accelerator
3 *
4 *  Copyright (C) 2004-2007, David Dillow
5 *  Written by David Dillow <dave@thedillows.org>
6 *  Inspired by the Trident 4D-WaveDX/NX driver.
7 *
8 *  All rights reserved.
9 *
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.
13 *
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.
18 *
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
22 */
23
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"
35
36MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
37MODULE_DESCRIPTION("SiS7019");
38MODULE_LICENSE("GPL");
39MODULE_SUPPORTED_DEVICE("{{SiS,SiS7019 Audio Accelerator}}");
40
41static int index = SNDRV_DEFAULT_IDX1;	/* Index 0-MAX */
42static char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
43static int enable = 1;
44
45module_param(index, int, 0444);
46MODULE_PARM_DESC(index, "Index value for SiS7019 Audio Accelerator.");
47module_param(id, charp, 0444);
48MODULE_PARM_DESC(id, "ID string for SiS7019 Audio Accelerator.");
49module_param(enable, bool, 0444);
50MODULE_PARM_DESC(enable, "Enable SiS7019 Audio Accelerator.");
51
52static DEFINE_PCI_DEVICE_TABLE(snd_sis7019_ids) = {
53	{ PCI_DEVICE(PCI_VENDOR_ID_SI, 0x7019) },
54	{ 0, }
55};
56
57MODULE_DEVICE_TABLE(pci, snd_sis7019_ids);
58
59struct voice {
60	u16 flags;
61#define 	VOICE_IN_USE		1
62#define 	VOICE_CAPTURE		2
63#define 	VOICE_SSO_TIMING	4
64#define 	VOICE_SYNC_TIMING	8
65	u16 sync_cso;
66	u16 period_size;
67	u16 buffer_size;
68	u16 sync_period_size;
69	u16 sync_buffer_size;
70	u32 sso;
71	u32 vperiod;
72	struct snd_pcm_substream *substream;
73	struct voice *timing;
74	void __iomem *ctrl_base;
75	void __iomem *wave_base;
76	void __iomem *sync_base;
77	int num;
78};
79
80/* We need four pages to store our wave parameters during a suspend. If
81 * we're not doing power management, we still need to allocate a page
82 * for the silence buffer.
83 */
84#ifdef CONFIG_PM
85#define SIS_SUSPEND_PAGES	4
86#else
87#define SIS_SUSPEND_PAGES	1
88#endif
89
90struct sis7019 {
91	unsigned long ioport;
92	void __iomem *ioaddr;
93	int irq;
94	int codecs_present;
95
96	struct pci_dev *pci;
97	struct snd_pcm *pcm;
98	struct snd_card *card;
99	struct snd_ac97 *ac97[3];
100
101	/* Protect against more than one thread hitting the AC97
102	 * registers (in a more polite manner than pounding the hardware
103	 * semaphore)
104	 */
105	struct mutex ac97_mutex;
106
107	/* voice_lock protects allocation/freeing of the voice descriptions
108	 */
109	spinlock_t voice_lock;
110
111	struct voice voices[64];
112	struct voice capture_voice;
113
114	/* Allocate pages to store the internal wave state during
115	 * suspends. When we're operating, this can be used as a silence
116	 * buffer for a timing channel.
117	 */
118	void *suspend_state[SIS_SUSPEND_PAGES];
119
120	int silence_users;
121	dma_addr_t silence_dma_addr;
122};
123
124#define SIS_PRIMARY_CODEC_PRESENT	0x0001
125#define SIS_SECONDARY_CODEC_PRESENT	0x0002
126#define SIS_TERTIARY_CODEC_PRESENT	0x0004
127
128/* The HW offset parameters (Loop End, Stop Sample, End Sample) have a
129 * documented range of 8-0xfff8 samples. Given that they are 0-based,
130 * that places our period/buffer range at 9-0xfff9 samples. That makes the
131 * max buffer size 0xfff9 samples * 2 channels * 2 bytes per sample, and
132 * max samples / min samples gives us the max periods in a buffer.
133 *
134 * We'll add a constraint upon open that limits the period and buffer sample
135 * size to values that are legal for the hardware.
136 */
137static struct snd_pcm_hardware sis_playback_hw_info = {
138	.info = (SNDRV_PCM_INFO_MMAP |
139		 SNDRV_PCM_INFO_MMAP_VALID |
140		 SNDRV_PCM_INFO_INTERLEAVED |
141		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
142		 SNDRV_PCM_INFO_SYNC_START |
143		 SNDRV_PCM_INFO_RESUME),
144	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
145		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
146	.rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS,
147	.rate_min = 4000,
148	.rate_max = 48000,
149	.channels_min = 1,
150	.channels_max = 2,
151	.buffer_bytes_max = (0xfff9 * 4),
152	.period_bytes_min = 9,
153	.period_bytes_max = (0xfff9 * 4),
154	.periods_min = 1,
155	.periods_max = (0xfff9 / 9),
156};
157
158static struct snd_pcm_hardware sis_capture_hw_info = {
159	.info = (SNDRV_PCM_INFO_MMAP |
160		 SNDRV_PCM_INFO_MMAP_VALID |
161		 SNDRV_PCM_INFO_INTERLEAVED |
162		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
163		 SNDRV_PCM_INFO_SYNC_START |
164		 SNDRV_PCM_INFO_RESUME),
165	.formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
166		    SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
167	.rates = SNDRV_PCM_RATE_48000,
168	.rate_min = 4000,
169	.rate_max = 48000,
170	.channels_min = 1,
171	.channels_max = 2,
172	.buffer_bytes_max = (0xfff9 * 4),
173	.period_bytes_min = 9,
174	.period_bytes_max = (0xfff9 * 4),
175	.periods_min = 1,
176	.periods_max = (0xfff9 / 9),
177};
178
179static void sis_update_sso(struct voice *voice, u16 period)
180{
181	void __iomem *base = voice->ctrl_base;
182
183	voice->sso += period;
184	if (voice->sso >= voice->buffer_size)
185		voice->sso -= voice->buffer_size;
186
187	/* Enforce the documented hardware minimum offset */
188	if (voice->sso < 8)
189		voice->sso = 8;
190
191	/* The SSO is in the upper 16 bits of the register. */
192	writew(voice->sso & 0xffff, base + SIS_PLAY_DMA_SSO_ESO + 2);
193}
194
195static void sis_update_voice(struct voice *voice)
196{
197	if (voice->flags & VOICE_SSO_TIMING) {
198		sis_update_sso(voice, voice->period_size);
199	} else if (voice->flags & VOICE_SYNC_TIMING) {
200		int sync;
201
202		/* If we've not hit the end of the virtual period, update
203		 * our records and keep going.
204		 */
205		if (voice->vperiod > voice->period_size) {
206			voice->vperiod -= voice->period_size;
207			if (voice->vperiod < voice->period_size)
208				sis_update_sso(voice, voice->vperiod);
209			else
210				sis_update_sso(voice, voice->period_size);
211			return;
212		}
213
214		/* Calculate our relative offset between the target and
215		 * the actual CSO value. Since we're operating in a loop,
216		 * if the value is more than half way around, we can
217		 * consider ourselves wrapped.
218		 */
219		sync = voice->sync_cso;
220		sync -= readw(voice->sync_base + SIS_CAPTURE_DMA_FORMAT_CSO);
221		if (sync > (voice->sync_buffer_size / 2))
222			sync -= voice->sync_buffer_size;
223
224		/* If sync is positive, then we interrupted too early, and
225		 * we'll need to come back in a few samples and try again.
226		 * There's a minimum wait, as it takes some time for the DMA
227		 * engine to startup, etc...
228		 */
229		if (sync > 0) {
230			if (sync < 16)
231				sync = 16;
232			sis_update_sso(voice, sync);
233			return;
234		}
235
236		/* Ok, we interrupted right on time, or (hopefully) just
237		 * a bit late. We'll adjst our next waiting period based
238		 * on how close we got.
239		 *
240		 * We need to stay just behind the actual channel to ensure
241		 * it really is past a period when we get our interrupt --
242		 * otherwise we'll fall into the early code above and have
243		 * a minimum wait time, which makes us quite late here,
244		 * eating into the user's time to refresh the buffer, esp.
245		 * if using small periods.
246		 *
247		 * If we're less than 9 samples behind, we're on target.
248		 * Otherwise, shorten the next vperiod by the amount we've
249		 * been delayed.
250		 */
251		if (sync > -9)
252			voice->vperiod = voice->sync_period_size + 1;
253		else
254			voice->vperiod = voice->sync_period_size + sync + 10;
255
256		if (voice->vperiod < voice->buffer_size) {
257			sis_update_sso(voice, voice->vperiod);
258			voice->vperiod = 0;
259		} else
260			sis_update_sso(voice, voice->period_size);
261
262		sync = voice->sync_cso + voice->sync_period_size;
263		if (sync >= voice->sync_buffer_size)
264			sync -= voice->sync_buffer_size;
265		voice->sync_cso = sync;
266	}
267
268	snd_pcm_period_elapsed(voice->substream);
269}
270
271static void sis_voice_irq(u32 status, struct voice *voice)
272{
273	int bit;
274
275	while (status) {
276		bit = __ffs(status);
277		status >>= bit + 1;
278		voice += bit;
279		sis_update_voice(voice);
280		voice++;
281	}
282}
283
284static irqreturn_t sis_interrupt(int irq, void *dev)
285{
286	struct sis7019 *sis = dev;
287	unsigned long io = sis->ioport;
288	struct voice *voice;
289	u32 intr, status;
290
291	/* We only use the DMA interrupts, and we don't enable any other
292	 * source of interrupts. But, it is possible to see an interupt
293	 * status that didn't actually interrupt us, so eliminate anything
294	 * we're not expecting to avoid falsely claiming an IRQ, and an
295	 * ensuing endless loop.
296	 */
297	intr = inl(io + SIS_GISR);
298	intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
299		SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
300	if (!intr)
301		return IRQ_NONE;
302
303	do {
304		status = inl(io + SIS_PISR_A);
305		if (status) {
306			sis_voice_irq(status, sis->voices);
307			outl(status, io + SIS_PISR_A);
308		}
309
310		status = inl(io + SIS_PISR_B);
311		if (status) {
312			sis_voice_irq(status, &sis->voices[32]);
313			outl(status, io + SIS_PISR_B);
314		}
315
316		status = inl(io + SIS_RISR);
317		if (status) {
318			voice = &sis->capture_voice;
319			if (!voice->timing)
320				snd_pcm_period_elapsed(voice->substream);
321
322			outl(status, io + SIS_RISR);
323		}
324
325		outl(intr, io + SIS_GISR);
326		intr = inl(io + SIS_GISR);
327		intr &= SIS_GISR_AUDIO_PLAY_DMA_IRQ_STATUS |
328			SIS_GISR_AUDIO_RECORD_DMA_IRQ_STATUS;
329	} while (intr);
330
331	return IRQ_HANDLED;
332}
333
334static u32 sis_rate_to_delta(unsigned int rate)
335{
336	u32 delta;
337
338	/* This was copied from the trident driver, but it seems its gotten
339	 * around a bit... nevertheless, it works well.
340	 *
341	 * We special case 44100 and 8000 since rounding with the equation
342	 * does not give us an accurate enough value. For 11025 and 22050
343	 * the equation gives us the best answer. All other frequencies will
344	 * also use the equation. JDW
345	 */
346	if (rate == 44100)
347		delta = 0xeb3;
348	else if (rate == 8000)
349		delta = 0x2ab;
350	else if (rate == 48000)
351		delta = 0x1000;
352	else
353		delta = (((rate << 12) + 24000) / 48000) & 0x0000ffff;
354	return delta;
355}
356
357static void __sis_map_silence(struct sis7019 *sis)
358{
359	/* Helper function: must hold sis->voice_lock on entry */
360	if (!sis->silence_users)
361		sis->silence_dma_addr = pci_map_single(sis->pci,
362						sis->suspend_state[0],
363						4096, PCI_DMA_TODEVICE);
364	sis->silence_users++;
365}
366
367static void __sis_unmap_silence(struct sis7019 *sis)
368{
369	/* Helper function: must hold sis->voice_lock on entry */
370	sis->silence_users--;
371	if (!sis->silence_users)
372		pci_unmap_single(sis->pci, sis->silence_dma_addr, 4096,
373					PCI_DMA_TODEVICE);
374}
375
376static void sis_free_voice(struct sis7019 *sis, struct voice *voice)
377{
378	unsigned long flags;
379
380	spin_lock_irqsave(&sis->voice_lock, flags);
381	if (voice->timing) {
382		__sis_unmap_silence(sis);
383		voice->timing->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING |
384						VOICE_SYNC_TIMING);
385		voice->timing = NULL;
386	}
387	voice->flags &= ~(VOICE_IN_USE | VOICE_SSO_TIMING | VOICE_SYNC_TIMING);
388	spin_unlock_irqrestore(&sis->voice_lock, flags);
389}
390
391static struct voice *__sis_alloc_playback_voice(struct sis7019 *sis)
392{
393	/* Must hold the voice_lock on entry */
394	struct voice *voice;
395	int i;
396
397	for (i = 0; i < 64; i++) {
398		voice = &sis->voices[i];
399		if (voice->flags & VOICE_IN_USE)
400			continue;
401		voice->flags |= VOICE_IN_USE;
402		goto found_one;
403	}
404	voice = NULL;
405
406found_one:
407	return voice;
408}
409
410static struct voice *sis_alloc_playback_voice(struct sis7019 *sis)
411{
412	struct voice *voice;
413	unsigned long flags;
414
415	spin_lock_irqsave(&sis->voice_lock, flags);
416	voice = __sis_alloc_playback_voice(sis);
417	spin_unlock_irqrestore(&sis->voice_lock, flags);
418
419	return voice;
420}
421
422static int sis_alloc_timing_voice(struct snd_pcm_substream *substream,
423					struct snd_pcm_hw_params *hw_params)
424{
425	struct sis7019 *sis = snd_pcm_substream_chip(substream);
426	struct snd_pcm_runtime *runtime = substream->runtime;
427	struct voice *voice = runtime->private_data;
428	unsigned int period_size, buffer_size;
429	unsigned long flags;
430	int needed;
431
432	/* If there are one or two periods per buffer, we don't need a
433	 * timing voice, as we can use the capture channel's interrupts
434	 * to clock out the periods.
435	 */
436	period_size = params_period_size(hw_params);
437	buffer_size = params_buffer_size(hw_params);
438	needed = (period_size != buffer_size &&
439			period_size != (buffer_size / 2));
440
441	if (needed && !voice->timing) {
442		spin_lock_irqsave(&sis->voice_lock, flags);
443		voice->timing = __sis_alloc_playback_voice(sis);
444		if (voice->timing)
445			__sis_map_silence(sis);
446		spin_unlock_irqrestore(&sis->voice_lock, flags);
447		if (!voice->timing)
448			return -ENOMEM;
449		voice->timing->substream = substream;
450	} else if (!needed && voice->timing) {
451		sis_free_voice(sis, voice);
452		voice->timing = NULL;
453	}
454
455	return 0;
456}
457
458static int sis_playback_open(struct snd_pcm_substream *substream)
459{
460	struct sis7019 *sis = snd_pcm_substream_chip(substream);
461	struct snd_pcm_runtime *runtime = substream->runtime;
462	struct voice *voice;
463
464	voice = sis_alloc_playback_voice(sis);
465	if (!voice)
466		return -EAGAIN;
467
468	voice->substream = substream;
469	runtime->private_data = voice;
470	runtime->hw = sis_playback_hw_info;
471	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
472						9, 0xfff9);
473	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
474						9, 0xfff9);
475	snd_pcm_set_sync(substream);
476	return 0;
477}
478
479static int sis_substream_close(struct snd_pcm_substream *substream)
480{
481	struct sis7019 *sis = snd_pcm_substream_chip(substream);
482	struct snd_pcm_runtime *runtime = substream->runtime;
483	struct voice *voice = runtime->private_data;
484
485	sis_free_voice(sis, voice);
486	return 0;
487}
488
489static int sis_playback_hw_params(struct snd_pcm_substream *substream,
490					struct snd_pcm_hw_params *hw_params)
491{
492	return snd_pcm_lib_malloc_pages(substream,
493					params_buffer_bytes(hw_params));
494}
495
496static int sis_hw_free(struct snd_pcm_substream *substream)
497{
498	return snd_pcm_lib_free_pages(substream);
499}
500
501static int sis_pcm_playback_prepare(struct snd_pcm_substream *substream)
502{
503	struct snd_pcm_runtime *runtime = substream->runtime;
504	struct voice *voice = runtime->private_data;
505	void __iomem *ctrl_base = voice->ctrl_base;
506	void __iomem *wave_base = voice->wave_base;
507	u32 format, dma_addr, control, sso_eso, delta, reg;
508	u16 leo;
509
510	/* We rely on the PCM core to ensure that the parameters for this
511	 * substream do not change on us while we're programming the HW.
512	 */
513	format = 0;
514	if (snd_pcm_format_width(runtime->format) == 8)
515		format |= SIS_PLAY_DMA_FORMAT_8BIT;
516	if (!snd_pcm_format_signed(runtime->format))
517		format |= SIS_PLAY_DMA_FORMAT_UNSIGNED;
518	if (runtime->channels == 1)
519		format |= SIS_PLAY_DMA_FORMAT_MONO;
520
521	/* The baseline setup is for a single period per buffer, and
522	 * we add bells and whistles as needed from there.
523	 */
524	dma_addr = runtime->dma_addr;
525	leo = runtime->buffer_size - 1;
526	control = leo | SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_LEO;
527	sso_eso = leo;
528
529	if (runtime->period_size == (runtime->buffer_size / 2)) {
530		control |= SIS_PLAY_DMA_INTR_AT_MLP;
531	} else if (runtime->period_size != runtime->buffer_size) {
532		voice->flags |= VOICE_SSO_TIMING;
533		voice->sso = runtime->period_size - 1;
534		voice->period_size = runtime->period_size;
535		voice->buffer_size = runtime->buffer_size;
536
537		control &= ~SIS_PLAY_DMA_INTR_AT_LEO;
538		control |= SIS_PLAY_DMA_INTR_AT_SSO;
539		sso_eso |= (runtime->period_size - 1) << 16;
540	}
541
542	delta = sis_rate_to_delta(runtime->rate);
543
544	/* Ok, we're ready to go, set up the channel.
545	 */
546	writel(format, ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
547	writel(dma_addr, ctrl_base + SIS_PLAY_DMA_BASE);
548	writel(control, ctrl_base + SIS_PLAY_DMA_CONTROL);
549	writel(sso_eso, ctrl_base + SIS_PLAY_DMA_SSO_ESO);
550
551	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
552		writel(0, wave_base + reg);
553
554	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
555	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
556	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
557			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
558			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
559			wave_base + SIS_WAVE_CHANNEL_CONTROL);
560
561	/* Force PCI writes to post. */
562	readl(ctrl_base);
563
564	return 0;
565}
566
567static int sis_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
568{
569	struct sis7019 *sis = snd_pcm_substream_chip(substream);
570	unsigned long io = sis->ioport;
571	struct snd_pcm_substream *s;
572	struct voice *voice;
573	void *chip;
574	int starting;
575	u32 record = 0;
576	u32 play[2] = { 0, 0 };
577
578	/* No locks needed, as the PCM core will hold the locks on the
579	 * substreams, and the HW will only start/stop the indicated voices
580	 * without changing the state of the others.
581	 */
582	switch (cmd) {
583	case SNDRV_PCM_TRIGGER_START:
584	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
585	case SNDRV_PCM_TRIGGER_RESUME:
586		starting = 1;
587		break;
588	case SNDRV_PCM_TRIGGER_STOP:
589	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
590	case SNDRV_PCM_TRIGGER_SUSPEND:
591		starting = 0;
592		break;
593	default:
594		return -EINVAL;
595	}
596
597	snd_pcm_group_for_each_entry(s, substream) {
598		/* Make sure it is for us... */
599		chip = snd_pcm_substream_chip(s);
600		if (chip != sis)
601			continue;
602
603		voice = s->runtime->private_data;
604		if (voice->flags & VOICE_CAPTURE) {
605			record |= 1 << voice->num;
606			voice = voice->timing;
607		}
608
609		/* voice could be NULL if this a recording stream, and it
610		 * doesn't have an external timing channel.
611		 */
612		if (voice)
613			play[voice->num / 32] |= 1 << (voice->num & 0x1f);
614
615		snd_pcm_trigger_done(s, substream);
616	}
617
618	if (starting) {
619		if (record)
620			outl(record, io + SIS_RECORD_START_REG);
621		if (play[0])
622			outl(play[0], io + SIS_PLAY_START_A_REG);
623		if (play[1])
624			outl(play[1], io + SIS_PLAY_START_B_REG);
625	} else {
626		if (record)
627			outl(record, io + SIS_RECORD_STOP_REG);
628		if (play[0])
629			outl(play[0], io + SIS_PLAY_STOP_A_REG);
630		if (play[1])
631			outl(play[1], io + SIS_PLAY_STOP_B_REG);
632	}
633	return 0;
634}
635
636static snd_pcm_uframes_t sis_pcm_pointer(struct snd_pcm_substream *substream)
637{
638	struct snd_pcm_runtime *runtime = substream->runtime;
639	struct voice *voice = runtime->private_data;
640	u32 cso;
641
642	cso = readl(voice->ctrl_base + SIS_PLAY_DMA_FORMAT_CSO);
643	cso &= 0xffff;
644	return cso;
645}
646
647static int sis_capture_open(struct snd_pcm_substream *substream)
648{
649	struct sis7019 *sis = snd_pcm_substream_chip(substream);
650	struct snd_pcm_runtime *runtime = substream->runtime;
651	struct voice *voice = &sis->capture_voice;
652	unsigned long flags;
653
654	spin_lock_irqsave(&sis->voice_lock, flags);
655	if (voice->flags & VOICE_IN_USE)
656		voice = NULL;
657	else
658		voice->flags |= VOICE_IN_USE;
659	spin_unlock_irqrestore(&sis->voice_lock, flags);
660
661	if (!voice)
662		return -EAGAIN;
663
664	voice->substream = substream;
665	runtime->private_data = voice;
666	runtime->hw = sis_capture_hw_info;
667	runtime->hw.rates = sis->ac97[0]->rates[AC97_RATES_ADC];
668	snd_pcm_limit_hw_rates(runtime);
669	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
670						9, 0xfff9);
671	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
672						9, 0xfff9);
673	snd_pcm_set_sync(substream);
674	return 0;
675}
676
677static int sis_capture_hw_params(struct snd_pcm_substream *substream,
678					struct snd_pcm_hw_params *hw_params)
679{
680	struct sis7019 *sis = snd_pcm_substream_chip(substream);
681	int rc;
682
683	rc = snd_ac97_set_rate(sis->ac97[0], AC97_PCM_LR_ADC_RATE,
684						params_rate(hw_params));
685	if (rc)
686		goto out;
687
688	rc = snd_pcm_lib_malloc_pages(substream,
689					params_buffer_bytes(hw_params));
690	if (rc < 0)
691		goto out;
692
693	rc = sis_alloc_timing_voice(substream, hw_params);
694
695out:
696	return rc;
697}
698
699static void sis_prepare_timing_voice(struct voice *voice,
700					struct snd_pcm_substream *substream)
701{
702	struct sis7019 *sis = snd_pcm_substream_chip(substream);
703	struct snd_pcm_runtime *runtime = substream->runtime;
704	struct voice *timing = voice->timing;
705	void __iomem *play_base = timing->ctrl_base;
706	void __iomem *wave_base = timing->wave_base;
707	u16 buffer_size, period_size;
708	u32 format, control, sso_eso, delta;
709	u32 vperiod, sso, reg;
710
711	/* Set our initial buffer and period as large as we can given a
712	 * single page of silence.
713	 */
714	buffer_size = 4096 / runtime->channels;
715	buffer_size /= snd_pcm_format_size(runtime->format, 1);
716	period_size = buffer_size;
717
718	/* Initially, we want to interrupt just a bit behind the end of
719	 * the period we're clocking out. 12 samples seems to give a good
720	 * delay.
721	 *
722	 * We want to spread our interrupts throughout the virtual period,
723	 * so that we don't end up with two interrupts back to back at the
724	 * end -- this helps minimize the effects of any jitter. Adjust our
725	 * clocking period size so that the last period is at least a fourth
726	 * of a full period.
727	 *
728	 * This is all moot if we don't need to use virtual periods.
729	 */
730	vperiod = runtime->period_size + 12;
731	if (vperiod > period_size) {
732		u16 tail = vperiod % period_size;
733		u16 quarter_period = period_size / 4;
734
735		if (tail && tail < quarter_period) {
736			u16 loops = vperiod / period_size;
737
738			tail = quarter_period - tail;
739			tail += loops - 1;
740			tail /= loops;
741			period_size -= tail;
742		}
743
744		sso = period_size - 1;
745	} else {
746		/* The initial period will fit inside the buffer, so we
747		 * don't need to use virtual periods -- disable them.
748		 */
749		period_size = runtime->period_size;
750		sso = vperiod - 1;
751		vperiod = 0;
752	}
753
754	/* The interrupt handler implements the timing syncronization, so
755	 * setup its state.
756	 */
757	timing->flags |= VOICE_SYNC_TIMING;
758	timing->sync_base = voice->ctrl_base;
759	timing->sync_cso = runtime->period_size;
760	timing->sync_period_size = runtime->period_size;
761	timing->sync_buffer_size = runtime->buffer_size;
762	timing->period_size = period_size;
763	timing->buffer_size = buffer_size;
764	timing->sso = sso;
765	timing->vperiod = vperiod;
766
767	/* Using unsigned samples with the all-zero silence buffer
768	 * forces the output to the lower rail, killing playback.
769	 * So ignore unsigned vs signed -- it doesn't change the timing.
770	 */
771	format = 0;
772	if (snd_pcm_format_width(runtime->format) == 8)
773		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
774	if (runtime->channels == 1)
775		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
776
777	control = timing->buffer_size - 1;
778	control |= SIS_PLAY_DMA_LOOP | SIS_PLAY_DMA_INTR_AT_SSO;
779	sso_eso = timing->buffer_size - 1;
780	sso_eso |= timing->sso << 16;
781
782	delta = sis_rate_to_delta(runtime->rate);
783
784	/* We've done the math, now configure the channel.
785	 */
786	writel(format, play_base + SIS_PLAY_DMA_FORMAT_CSO);
787	writel(sis->silence_dma_addr, play_base + SIS_PLAY_DMA_BASE);
788	writel(control, play_base + SIS_PLAY_DMA_CONTROL);
789	writel(sso_eso, play_base + SIS_PLAY_DMA_SSO_ESO);
790
791	for (reg = 0; reg < SIS_WAVE_SIZE; reg += 4)
792		writel(0, wave_base + reg);
793
794	writel(SIS_WAVE_GENERAL_WAVE_VOLUME, wave_base + SIS_WAVE_GENERAL);
795	writel(delta << 16, wave_base + SIS_WAVE_GENERAL_ARTICULATION);
796	writel(SIS_WAVE_CHANNEL_CONTROL_FIRST_SAMPLE |
797			SIS_WAVE_CHANNEL_CONTROL_AMP_ENABLE |
798			SIS_WAVE_CHANNEL_CONTROL_INTERPOLATE_ENABLE,
799			wave_base + SIS_WAVE_CHANNEL_CONTROL);
800}
801
802static int sis_pcm_capture_prepare(struct snd_pcm_substream *substream)
803{
804	struct snd_pcm_runtime *runtime = substream->runtime;
805	struct voice *voice = runtime->private_data;
806	void __iomem *rec_base = voice->ctrl_base;
807	u32 format, dma_addr, control;
808	u16 leo;
809
810	/* We rely on the PCM core to ensure that the parameters for this
811	 * substream do not change on us while we're programming the HW.
812	 */
813	format = 0;
814	if (snd_pcm_format_width(runtime->format) == 8)
815		format = SIS_CAPTURE_DMA_FORMAT_8BIT;
816	if (!snd_pcm_format_signed(runtime->format))
817		format |= SIS_CAPTURE_DMA_FORMAT_UNSIGNED;
818	if (runtime->channels == 1)
819		format |= SIS_CAPTURE_DMA_FORMAT_MONO;
820
821	dma_addr = runtime->dma_addr;
822	leo = runtime->buffer_size - 1;
823	control = leo | SIS_CAPTURE_DMA_LOOP;
824
825	/* If we've got more than two periods per buffer, then we have
826	 * use a timing voice to clock out the periods. Otherwise, we can
827	 * use the capture channel's interrupts.
828	 */
829	if (voice->timing) {
830		sis_prepare_timing_voice(voice, substream);
831	} else {
832		control |= SIS_CAPTURE_DMA_INTR_AT_LEO;
833		if (runtime->period_size != runtime->buffer_size)
834			control |= SIS_CAPTURE_DMA_INTR_AT_MLP;
835	}
836
837	writel(format, rec_base + SIS_CAPTURE_DMA_FORMAT_CSO);
838	writel(dma_addr, rec_base + SIS_CAPTURE_DMA_BASE);
839	writel(control, rec_base + SIS_CAPTURE_DMA_CONTROL);
840
841	/* Force the writes to post. */
842	readl(rec_base);
843
844	return 0;
845}
846
847static struct snd_pcm_ops sis_playback_ops = {
848	.open = sis_playback_open,
849	.close = sis_substream_close,
850	.ioctl = snd_pcm_lib_ioctl,
851	.hw_params = sis_playback_hw_params,
852	.hw_free = sis_hw_free,
853	.prepare = sis_pcm_playback_prepare,
854	.trigger = sis_pcm_trigger,
855	.pointer = sis_pcm_pointer,
856};
857
858static struct snd_pcm_ops sis_capture_ops = {
859	.open = sis_capture_open,
860	.close = sis_substream_close,
861	.ioctl = snd_pcm_lib_ioctl,
862	.hw_params = sis_capture_hw_params,
863	.hw_free = sis_hw_free,
864	.prepare = sis_pcm_capture_prepare,
865	.trigger = sis_pcm_trigger,
866	.pointer = sis_pcm_pointer,
867};
868
869static int __devinit sis_pcm_create(struct sis7019 *sis)
870{
871	struct snd_pcm *pcm;
872	int rc;
873
874	/* We have 64 voices, and the driver currently records from
875	 * only one channel, though that could change in the future.
876	 */
877	rc = snd_pcm_new(sis->card, "SiS7019", 0, 64, 1, &pcm);
878	if (rc)
879		return rc;
880
881	pcm->private_data = sis;
882	strcpy(pcm->name, "SiS7019");
883	sis->pcm = pcm;
884
885	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &sis_playback_ops);
886	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &sis_capture_ops);
887
888	/* Try to preallocate some memory, but it's not the end of the
889	 * world if this fails.
890	 */
891	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
892				snd_dma_pci_data(sis->pci), 64*1024, 128*1024);
893
894	return 0;
895}
896
897static unsigned short sis_ac97_rw(struct sis7019 *sis, int codec, u32 cmd)
898{
899	unsigned long io = sis->ioport;
900	unsigned short val = 0xffff;
901	u16 status;
902	u16 rdy;
903	int count;
904	static const u16 codec_ready[3] = {
905		SIS_AC97_STATUS_CODEC_READY,
906		SIS_AC97_STATUS_CODEC2_READY,
907		SIS_AC97_STATUS_CODEC3_READY,
908	};
909
910	rdy = codec_ready[codec];
911
912
913	/* Get the AC97 semaphore -- software first, so we don't spin
914	 * pounding out IO reads on the hardware semaphore...
915	 */
916	mutex_lock(&sis->ac97_mutex);
917
918	count = 0xffff;
919	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
920		udelay(1);
921
922	if (!count)
923		goto timeout;
924
925	/* ... and wait for any outstanding commands to complete ...
926	 */
927	count = 0xffff;
928	do {
929		status = inw(io + SIS_AC97_STATUS);
930		if ((status & rdy) && !(status & SIS_AC97_STATUS_BUSY))
931			break;
932
933		udelay(1);
934	} while (--count);
935
936	if (!count)
937		goto timeout_sema;
938
939	/* ... before sending our command and waiting for it to finish ...
940	 */
941	outl(cmd, io + SIS_AC97_CMD);
942	udelay(10);
943
944	count = 0xffff;
945	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
946		udelay(1);
947
948	/* ... and reading the results (if any).
949	 */
950	val = inl(io + SIS_AC97_CMD) >> 16;
951
952timeout_sema:
953	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
954timeout:
955	mutex_unlock(&sis->ac97_mutex);
956
957	if (!count) {
958		printk(KERN_ERR "sis7019: ac97 codec %d timeout cmd 0x%08x\n",
959					codec, cmd);
960	}
961
962	return val;
963}
964
965static void sis_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
966				unsigned short val)
967{
968	static const u32 cmd[3] = {
969		SIS_AC97_CMD_CODEC_WRITE,
970		SIS_AC97_CMD_CODEC2_WRITE,
971		SIS_AC97_CMD_CODEC3_WRITE,
972	};
973	sis_ac97_rw(ac97->private_data, ac97->num,
974			(val << 16) | (reg << 8) | cmd[ac97->num]);
975}
976
977static unsigned short sis_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
978{
979	static const u32 cmd[3] = {
980		SIS_AC97_CMD_CODEC_READ,
981		SIS_AC97_CMD_CODEC2_READ,
982		SIS_AC97_CMD_CODEC3_READ,
983	};
984	return sis_ac97_rw(ac97->private_data, ac97->num,
985					(reg << 8) | cmd[ac97->num]);
986}
987
988static int __devinit sis_mixer_create(struct sis7019 *sis)
989{
990	struct snd_ac97_bus *bus;
991	struct snd_ac97_template ac97;
992	static struct snd_ac97_bus_ops ops = {
993		.write = sis_ac97_write,
994		.read = sis_ac97_read,
995	};
996	int rc;
997
998	memset(&ac97, 0, sizeof(ac97));
999	ac97.private_data = sis;
1000
1001	rc = snd_ac97_bus(sis->card, 0, &ops, NULL, &bus);
1002	if (!rc && sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1003		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[0]);
1004	ac97.num = 1;
1005	if (!rc && (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT))
1006		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[1]);
1007	ac97.num = 2;
1008	if (!rc && (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT))
1009		rc = snd_ac97_mixer(bus, &ac97, &sis->ac97[2]);
1010
1011	/* If we return an error here, then snd_card_free() should
1012	 * free up any ac97 codecs that got created, as well as the bus.
1013	 */
1014	return rc;
1015}
1016
1017static void sis_free_suspend(struct sis7019 *sis)
1018{
1019	int i;
1020
1021	for (i = 0; i < SIS_SUSPEND_PAGES; i++)
1022		kfree(sis->suspend_state[i]);
1023}
1024
1025static int sis_chip_free(struct sis7019 *sis)
1026{
1027	/* Reset the chip, and disable all interrputs.
1028	 */
1029	outl(SIS_GCR_SOFTWARE_RESET, sis->ioport + SIS_GCR);
1030	udelay(25);
1031	outl(0, sis->ioport + SIS_GCR);
1032	outl(0, sis->ioport + SIS_GIER);
1033
1034	/* Now, free everything we allocated.
1035	 */
1036	if (sis->irq >= 0)
1037		free_irq(sis->irq, sis);
1038
1039	if (sis->ioaddr)
1040		iounmap(sis->ioaddr);
1041
1042	pci_release_regions(sis->pci);
1043	pci_disable_device(sis->pci);
1044
1045	sis_free_suspend(sis);
1046	return 0;
1047}
1048
1049static int sis_dev_free(struct snd_device *dev)
1050{
1051	struct sis7019 *sis = dev->device_data;
1052	return sis_chip_free(sis);
1053}
1054
1055static int sis_chip_init(struct sis7019 *sis)
1056{
1057	unsigned long io = sis->ioport;
1058	void __iomem *ioaddr = sis->ioaddr;
1059	u16 status;
1060	int count;
1061	int i;
1062
1063	/* Reset the audio controller
1064	 */
1065	outl(SIS_GCR_SOFTWARE_RESET, io + SIS_GCR);
1066	udelay(25);
1067	outl(0, io + SIS_GCR);
1068
1069	/* Get the AC-link semaphore, and reset the codecs
1070	 */
1071	count = 0xffff;
1072	while ((inw(io + SIS_AC97_SEMA) & SIS_AC97_SEMA_BUSY) && --count)
1073		udelay(1);
1074
1075	if (!count)
1076		return -EIO;
1077
1078	outl(SIS_AC97_CMD_CODEC_COLD_RESET, io + SIS_AC97_CMD);
1079	udelay(250);
1080
1081	count = 0xffff;
1082	while ((inw(io + SIS_AC97_STATUS) & SIS_AC97_STATUS_BUSY) && --count)
1083		udelay(1);
1084
1085	/* Now that we've finished the reset, find out what's attached.
1086	 */
1087	status = inl(io + SIS_AC97_STATUS);
1088	if (status & SIS_AC97_STATUS_CODEC_READY)
1089		sis->codecs_present |= SIS_PRIMARY_CODEC_PRESENT;
1090	if (status & SIS_AC97_STATUS_CODEC2_READY)
1091		sis->codecs_present |= SIS_SECONDARY_CODEC_PRESENT;
1092	if (status & SIS_AC97_STATUS_CODEC3_READY)
1093		sis->codecs_present |= SIS_TERTIARY_CODEC_PRESENT;
1094
1095	/* All done, let go of the semaphore, and check for errors
1096	 */
1097	outl(SIS_AC97_SEMA_RELEASE, io + SIS_AC97_SEMA);
1098	if (!sis->codecs_present || !count)
1099		return -EIO;
1100
1101	/* Let the hardware know that the audio driver is alive,
1102	 * and enable PCM slots on the AC-link for L/R playback (3 & 4) and
1103	 * record channels. We're going to want to use Variable Rate Audio
1104	 * for recording, to avoid needlessly resampling from 48kHZ.
1105	 */
1106	outl(SIS_AC97_CONF_AUDIO_ALIVE, io + SIS_AC97_CONF);
1107	outl(SIS_AC97_CONF_AUDIO_ALIVE | SIS_AC97_CONF_PCM_LR_ENABLE |
1108		SIS_AC97_CONF_PCM_CAP_MIC_ENABLE |
1109		SIS_AC97_CONF_PCM_CAP_LR_ENABLE |
1110		SIS_AC97_CONF_CODEC_VRA_ENABLE, io + SIS_AC97_CONF);
1111
1112	/* All AC97 PCM slots should be sourced from sub-mixer 0.
1113	 */
1114	outl(0, io + SIS_AC97_PSR);
1115
1116	/* There is only one valid DMA setup for a PCI environment.
1117	 */
1118	outl(SIS_DMA_CSR_PCI_SETTINGS, io + SIS_DMA_CSR);
1119
1120	/* Reset the syncronization groups for all of the channels
1121	 * to be asyncronous. If we start doing SPDIF or 5.1 sound, etc.
1122	 * we'll need to change how we handle these. Until then, we just
1123	 * assign sub-mixer 0 to all playback channels, and avoid any
1124	 * attenuation on the audio.
1125	 */
1126	outl(0, io + SIS_PLAY_SYNC_GROUP_A);
1127	outl(0, io + SIS_PLAY_SYNC_GROUP_B);
1128	outl(0, io + SIS_PLAY_SYNC_GROUP_C);
1129	outl(0, io + SIS_PLAY_SYNC_GROUP_D);
1130	outl(0, io + SIS_MIXER_SYNC_GROUP);
1131
1132	for (i = 0; i < 64; i++) {
1133		writel(i, SIS_MIXER_START_ADDR(ioaddr, i));
1134		writel(SIS_MIXER_RIGHT_NO_ATTEN | SIS_MIXER_LEFT_NO_ATTEN |
1135				SIS_MIXER_DEST_0, SIS_MIXER_ADDR(ioaddr, i));
1136	}
1137
1138	outl(0xffff0000, io + SIS_WEVCR);
1139
1140	/* Ensure that the wave engine is in normal operating mode.
1141	 */
1142	outl(0, io + SIS_WECCR);
1143
1144	/* Go ahead and enable the DMA interrupts. They won't go live
1145	 * until we start a channel.
1146	 */
1147	outl(SIS_GIER_AUDIO_PLAY_DMA_IRQ_ENABLE |
1148		SIS_GIER_AUDIO_RECORD_DMA_IRQ_ENABLE, io + SIS_GIER);
1149
1150	return 0;
1151}
1152
1153#ifdef CONFIG_PM
1154static int sis_suspend(struct pci_dev *pci, pm_message_t state)
1155{
1156	struct snd_card *card = pci_get_drvdata(pci);
1157	struct sis7019 *sis = card->private_data;
1158	void __iomem *ioaddr = sis->ioaddr;
1159	int i;
1160
1161	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1162	snd_pcm_suspend_all(sis->pcm);
1163	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1164		snd_ac97_suspend(sis->ac97[0]);
1165	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1166		snd_ac97_suspend(sis->ac97[1]);
1167	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1168		snd_ac97_suspend(sis->ac97[2]);
1169
1170	/* snd_pcm_suspend_all() stopped all channels, so we're quiescent.
1171	 */
1172	if (sis->irq >= 0) {
1173		free_irq(sis->irq, sis);
1174		sis->irq = -1;
1175	}
1176
1177	/* Save the internal state away
1178	 */
1179	for (i = 0; i < 4; i++) {
1180		memcpy_fromio(sis->suspend_state[i], ioaddr, 4096);
1181		ioaddr += 4096;
1182	}
1183
1184	pci_disable_device(pci);
1185	pci_save_state(pci);
1186	pci_set_power_state(pci, pci_choose_state(pci, state));
1187	return 0;
1188}
1189
1190static int sis_resume(struct pci_dev *pci)
1191{
1192	struct snd_card *card = pci_get_drvdata(pci);
1193	struct sis7019 *sis = card->private_data;
1194	void __iomem *ioaddr = sis->ioaddr;
1195	int i;
1196
1197	pci_set_power_state(pci, PCI_D0);
1198	pci_restore_state(pci);
1199
1200	if (pci_enable_device(pci) < 0) {
1201		printk(KERN_ERR "sis7019: unable to re-enable device\n");
1202		goto error;
1203	}
1204
1205	if (sis_chip_init(sis)) {
1206		printk(KERN_ERR "sis7019: unable to re-init controller\n");
1207		goto error;
1208	}
1209
1210	if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
1211				card->shortname, sis)) {
1212		printk(KERN_ERR "sis7019: unable to regain IRQ %d\n", pci->irq);
1213		goto error;
1214	}
1215
1216	/* Restore saved state, then clear out the page we use for the
1217	 * silence buffer.
1218	 */
1219	for (i = 0; i < 4; i++) {
1220		memcpy_toio(ioaddr, sis->suspend_state[i], 4096);
1221		ioaddr += 4096;
1222	}
1223
1224	memset(sis->suspend_state[0], 0, 4096);
1225
1226	sis->irq = pci->irq;
1227	pci_set_master(pci);
1228
1229	if (sis->codecs_present & SIS_PRIMARY_CODEC_PRESENT)
1230		snd_ac97_resume(sis->ac97[0]);
1231	if (sis->codecs_present & SIS_SECONDARY_CODEC_PRESENT)
1232		snd_ac97_resume(sis->ac97[1]);
1233	if (sis->codecs_present & SIS_TERTIARY_CODEC_PRESENT)
1234		snd_ac97_resume(sis->ac97[2]);
1235
1236	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1237	return 0;
1238
1239error:
1240	snd_card_disconnect(card);
1241	return -EIO;
1242}
1243#endif /* CONFIG_PM */
1244
1245static int sis_alloc_suspend(struct sis7019 *sis)
1246{
1247	int i;
1248
1249	/* We need 16K to store the internal wave engine state during a
1250	 * suspend, but we don't need it to be contiguous, so play nice
1251	 * with the memory system. We'll also use this area for a silence
1252	 * buffer.
1253	 */
1254	for (i = 0; i < SIS_SUSPEND_PAGES; i++) {
1255		sis->suspend_state[i] = kmalloc(4096, GFP_KERNEL);
1256		if (!sis->suspend_state[i])
1257			return -ENOMEM;
1258	}
1259	memset(sis->suspend_state[0], 0, 4096);
1260
1261	return 0;
1262}
1263
1264static int __devinit sis_chip_create(struct snd_card *card,
1265					struct pci_dev *pci)
1266{
1267	struct sis7019 *sis = card->private_data;
1268	struct voice *voice;
1269	static struct snd_device_ops ops = {
1270		.dev_free = sis_dev_free,
1271	};
1272	int rc;
1273	int i;
1274
1275	rc = pci_enable_device(pci);
1276	if (rc)
1277		goto error_out;
1278
1279	if (pci_set_dma_mask(pci, DMA_BIT_MASK(30)) < 0) {
1280		printk(KERN_ERR "sis7019: architecture does not support "
1281					"30-bit PCI busmaster DMA");
1282		goto error_out_enabled;
1283	}
1284
1285	memset(sis, 0, sizeof(*sis));
1286	mutex_init(&sis->ac97_mutex);
1287	spin_lock_init(&sis->voice_lock);
1288	sis->card = card;
1289	sis->pci = pci;
1290	sis->irq = -1;
1291	sis->ioport = pci_resource_start(pci, 0);
1292
1293	rc = pci_request_regions(pci, "SiS7019");
1294	if (rc) {
1295		printk(KERN_ERR "sis7019: unable request regions\n");
1296		goto error_out_enabled;
1297	}
1298
1299	rc = -EIO;
1300	sis->ioaddr = ioremap_nocache(pci_resource_start(pci, 1), 0x4000);
1301	if (!sis->ioaddr) {
1302		printk(KERN_ERR "sis7019: unable to remap MMIO, aborting\n");
1303		goto error_out_cleanup;
1304	}
1305
1306	rc = sis_alloc_suspend(sis);
1307	if (rc < 0) {
1308		printk(KERN_ERR "sis7019: unable to allocate state storage\n");
1309		goto error_out_cleanup;
1310	}
1311
1312	rc = sis_chip_init(sis);
1313	if (rc)
1314		goto error_out_cleanup;
1315
1316	if (request_irq(pci->irq, sis_interrupt, IRQF_DISABLED|IRQF_SHARED,
1317				card->shortname, sis)) {
1318		printk(KERN_ERR "unable to allocate irq %d\n", sis->irq);
1319		goto error_out_cleanup;
1320	}
1321
1322	sis->irq = pci->irq;
1323	pci_set_master(pci);
1324
1325	for (i = 0; i < 64; i++) {
1326		voice = &sis->voices[i];
1327		voice->num = i;
1328		voice->ctrl_base = SIS_PLAY_DMA_ADDR(sis->ioaddr, i);
1329		voice->wave_base = SIS_WAVE_ADDR(sis->ioaddr, i);
1330	}
1331
1332	voice = &sis->capture_voice;
1333	voice->flags = VOICE_CAPTURE;
1334	voice->num = SIS_CAPTURE_CHAN_AC97_PCM_IN;
1335	voice->ctrl_base = SIS_CAPTURE_DMA_ADDR(sis->ioaddr, voice->num);
1336
1337	rc = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sis, &ops);
1338	if (rc)
1339		goto error_out_cleanup;
1340
1341	snd_card_set_dev(card, &pci->dev);
1342
1343	return 0;
1344
1345error_out_cleanup:
1346	sis_chip_free(sis);
1347
1348error_out_enabled:
1349	pci_disable_device(pci);
1350
1351error_out:
1352	return rc;
1353}
1354
1355static int __devinit snd_sis7019_probe(struct pci_dev *pci,
1356					const struct pci_device_id *pci_id)
1357{
1358	struct snd_card *card;
1359	struct sis7019 *sis;
1360	int rc;
1361
1362	rc = -ENOENT;
1363	if (!enable)
1364		goto error_out;
1365
1366	rc = snd_card_create(index, id, THIS_MODULE, sizeof(*sis), &card);
1367	if (rc < 0)
1368		goto error_out;
1369
1370	strcpy(card->driver, "SiS7019");
1371	strcpy(card->shortname, "SiS7019");
1372	rc = sis_chip_create(card, pci);
1373	if (rc)
1374		goto card_error_out;
1375
1376	sis = card->private_data;
1377
1378	rc = sis_mixer_create(sis);
1379	if (rc)
1380		goto card_error_out;
1381
1382	rc = sis_pcm_create(sis);
1383	if (rc)
1384		goto card_error_out;
1385
1386	snprintf(card->longname, sizeof(card->longname),
1387			"%s Audio Accelerator with %s at 0x%lx, irq %d",
1388			card->shortname, snd_ac97_get_short_name(sis->ac97[0]),
1389			sis->ioport, sis->irq);
1390
1391	rc = snd_card_register(card);
1392	if (rc)
1393		goto card_error_out;
1394
1395	pci_set_drvdata(pci, card);
1396	return 0;
1397
1398card_error_out:
1399	snd_card_free(card);
1400
1401error_out:
1402	return rc;
1403}
1404
1405static void __devexit snd_sis7019_remove(struct pci_dev *pci)
1406{
1407	snd_card_free(pci_get_drvdata(pci));
1408	pci_set_drvdata(pci, NULL);
1409}
1410
1411static struct pci_driver sis7019_driver = {
1412	.name = "SiS7019",
1413	.id_table = snd_sis7019_ids,
1414	.probe = snd_sis7019_probe,
1415	.remove = __devexit_p(snd_sis7019_remove),
1416
1417#ifdef CONFIG_PM
1418	.suspend = sis_suspend,
1419	.resume = sis_resume,
1420#endif
1421};
1422
1423static int __init sis7019_init(void)
1424{
1425	return pci_register_driver(&sis7019_driver);
1426}
1427
1428static void __exit sis7019_exit(void)
1429{
1430	pci_unregister_driver(&sis7019_driver);
1431}
1432
1433module_init(sis7019_init);
1434module_exit(sis7019_exit);
1435