1/*
2 *  als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
3 *  Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
4 *
5 *  This program is free software; you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation; either version 2 of the License, or
8 *  (at your option) any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program; if not, write to the Free Software
17 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 *
19 *  TODO
20 *  4 channel playback for ALS300+
21 *  gameport
22 *  mpu401
23 *  opl3
24 *
25 *  NOTES
26 *  The BLOCK_COUNTER registers for the ALS300(+) return a figure related to
27 *  the position in the current period, NOT the whole buffer. It is important
28 *  to know which period we are in so we can calculate the correct pointer.
29 *  This is why we always use 2 periods. We can then use a flip-flop variable
30 *  to keep track of what period we are in.
31 */
32
33#include <linux/delay.h>
34#include <linux/init.h>
35#include <linux/moduleparam.h>
36#include <linux/pci.h>
37#include <linux/dma-mapping.h>
38#include <linux/interrupt.h>
39#include <linux/slab.h>
40
41#include <asm/io.h>
42
43#include <sound/core.h>
44#include <sound/control.h>
45#include <sound/initval.h>
46#include <sound/pcm.h>
47#include <sound/pcm_params.h>
48#include <sound/ac97_codec.h>
49#include <sound/opl3.h>
50
51/* snd_als300_set_irq_flag */
52#define IRQ_DISABLE		0
53#define IRQ_ENABLE		1
54
55/* I/O port layout */
56#define AC97_ACCESS		0x00
57#define AC97_READ		0x04
58#define AC97_STATUS		0x06
59#define   AC97_DATA_AVAIL		(1<<6)
60#define   AC97_BUSY			(1<<7)
61#define ALS300_IRQ_STATUS	0x07		/* ALS300 Only */
62#define   IRQ_PLAYBACK			(1<<3)
63#define   IRQ_CAPTURE			(1<<2)
64#define GCR_DATA		0x08
65#define GCR_INDEX		0x0C
66#define ALS300P_DRAM_IRQ_STATUS	0x0D		/* ALS300+ Only */
67#define MPU_IRQ_STATUS		0x0E		/* ALS300 Rev. E+, ALS300+ */
68#define ALS300P_IRQ_STATUS	0x0F		/* ALS300+ Only */
69
70/* General Control Registers */
71#define PLAYBACK_START		0x80
72#define PLAYBACK_END		0x81
73#define PLAYBACK_CONTROL	0x82
74#define   TRANSFER_START		(1<<16)
75#define   FIFO_PAUSE			(1<<17)
76#define RECORD_START		0x83
77#define RECORD_END		0x84
78#define RECORD_CONTROL		0x85
79#define DRAM_WRITE_CONTROL	0x8B
80#define   WRITE_TRANS_START		(1<<16)
81#define   DRAM_MODE_2			(1<<17)
82#define MISC_CONTROL		0x8C
83#define   IRQ_SET_BIT			(1<<15)
84#define   VMUTE_NORMAL			(1<<20)
85#define   MMUTE_NORMAL			(1<<21)
86#define MUS_VOC_VOL		0x8E
87#define PLAYBACK_BLOCK_COUNTER	0x9A
88#define RECORD_BLOCK_COUNTER	0x9B
89
90#define DEBUG_CALLS	0
91#define DEBUG_PLAY_REC	0
92
93#if DEBUG_CALLS
94#define snd_als300_dbgcalls(format, args...) printk(KERN_DEBUG format, ##args)
95#define snd_als300_dbgcallenter() printk(KERN_ERR "--> %s\n", __func__)
96#define snd_als300_dbgcallleave() printk(KERN_ERR "<-- %s\n", __func__)
97#else
98#define snd_als300_dbgcalls(format, args...)
99#define snd_als300_dbgcallenter()
100#define snd_als300_dbgcallleave()
101#endif
102
103#if DEBUG_PLAY_REC
104#define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
105#else
106#define snd_als300_dbgplay(format, args...)
107#endif
108
109enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
110
111MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
112MODULE_DESCRIPTION("Avance Logic ALS300");
113MODULE_LICENSE("GPL");
114MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}");
115
116static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
117static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
118static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
119
120struct snd_als300 {
121	unsigned long port;
122	spinlock_t reg_lock;
123	struct snd_card *card;
124	struct pci_dev *pci;
125
126	struct snd_pcm *pcm;
127	struct snd_pcm_substream *playback_substream;
128	struct snd_pcm_substream *capture_substream;
129
130	struct snd_ac97 *ac97;
131	struct snd_opl3 *opl3;
132
133	struct resource *res_port;
134
135	int irq;
136
137	int chip_type; /* ALS300 or ALS300+ */
138
139	char revision;
140};
141
142struct snd_als300_substream_data {
143	int period_flipflop;
144	int control_register;
145	int block_counter_register;
146};
147
148static DEFINE_PCI_DEVICE_TABLE(snd_als300_ids) = {
149	{ 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
150	{ 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
151	{ 0, }
152};
153
154MODULE_DEVICE_TABLE(pci, snd_als300_ids);
155
156static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
157{
158	outb(reg, port+GCR_INDEX);
159	return inl(port+GCR_DATA);
160}
161
162static inline void snd_als300_gcr_write(unsigned long port,
163						unsigned short reg, u32 val)
164{
165	outb(reg, port+GCR_INDEX);
166	outl(val, port+GCR_DATA);
167}
168
169/* Enable/Disable Interrupts */
170static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
171{
172	u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
173	snd_als300_dbgcallenter();
174
175	/* boolean XOR check, since old vs. new hardware have
176	   directly reversed bit setting for ENABLE and DISABLE.
177	   ALS300+ acts like newer versions of ALS300 */
178	if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
179						(cmd == IRQ_ENABLE)) == 0)
180		tmp |= IRQ_SET_BIT;
181	else
182		tmp &= ~IRQ_SET_BIT;
183	snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
184	snd_als300_dbgcallleave();
185}
186
187static int snd_als300_free(struct snd_als300 *chip)
188{
189	snd_als300_dbgcallenter();
190	snd_als300_set_irq_flag(chip, IRQ_DISABLE);
191	if (chip->irq >= 0)
192		free_irq(chip->irq, chip);
193	pci_release_regions(chip->pci);
194	pci_disable_device(chip->pci);
195	kfree(chip);
196	snd_als300_dbgcallleave();
197	return 0;
198}
199
200static int snd_als300_dev_free(struct snd_device *device)
201{
202	struct snd_als300 *chip = device->device_data;
203	return snd_als300_free(chip);
204}
205
206static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
207{
208	u8 status;
209	struct snd_als300 *chip = dev_id;
210	struct snd_als300_substream_data *data;
211
212	status = inb(chip->port+ALS300_IRQ_STATUS);
213	if (!status) /* shared IRQ, for different device?? Exit ASAP! */
214		return IRQ_NONE;
215
216	/* ACK everything ASAP */
217	outb(status, chip->port+ALS300_IRQ_STATUS);
218	if (status & IRQ_PLAYBACK) {
219		if (chip->pcm && chip->playback_substream) {
220			data = chip->playback_substream->runtime->private_data;
221			data->period_flipflop ^= 1;
222			snd_pcm_period_elapsed(chip->playback_substream);
223			snd_als300_dbgplay("IRQ_PLAYBACK\n");
224		}
225	}
226	if (status & IRQ_CAPTURE) {
227		if (chip->pcm && chip->capture_substream) {
228			data = chip->capture_substream->runtime->private_data;
229			data->period_flipflop ^= 1;
230			snd_pcm_period_elapsed(chip->capture_substream);
231			snd_als300_dbgplay("IRQ_CAPTURE\n");
232		}
233	}
234	return IRQ_HANDLED;
235}
236
237static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
238{
239	u8 general, mpu, dram;
240	struct snd_als300 *chip = dev_id;
241	struct snd_als300_substream_data *data;
242
243	general = inb(chip->port+ALS300P_IRQ_STATUS);
244	mpu = inb(chip->port+MPU_IRQ_STATUS);
245	dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
246
247	/* shared IRQ, for different device?? Exit ASAP! */
248	if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
249		return IRQ_NONE;
250
251	if (general & IRQ_PLAYBACK) {
252		if (chip->pcm && chip->playback_substream) {
253			outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
254			data = chip->playback_substream->runtime->private_data;
255			data->period_flipflop ^= 1;
256			snd_pcm_period_elapsed(chip->playback_substream);
257			snd_als300_dbgplay("IRQ_PLAYBACK\n");
258		}
259	}
260	if (general & IRQ_CAPTURE) {
261		if (chip->pcm && chip->capture_substream) {
262			outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
263			data = chip->capture_substream->runtime->private_data;
264			data->period_flipflop ^= 1;
265			snd_pcm_period_elapsed(chip->capture_substream);
266			snd_als300_dbgplay("IRQ_CAPTURE\n");
267		}
268	}
269	return IRQ_HANDLED;
270}
271
272static void __devexit snd_als300_remove(struct pci_dev *pci)
273{
274	snd_als300_dbgcallenter();
275	snd_card_free(pci_get_drvdata(pci));
276	pci_set_drvdata(pci, NULL);
277	snd_als300_dbgcallleave();
278}
279
280static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
281							unsigned short reg)
282{
283	int i;
284	struct snd_als300 *chip = ac97->private_data;
285
286	for (i = 0; i < 1000; i++) {
287		if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
288			break;
289		udelay(10);
290	}
291	outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
292
293	for (i = 0; i < 1000; i++) {
294		if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
295			break;
296		udelay(10);
297	}
298	return inw(chip->port+AC97_READ);
299}
300
301static void snd_als300_ac97_write(struct snd_ac97 *ac97,
302				unsigned short reg, unsigned short val)
303{
304	int i;
305	struct snd_als300 *chip = ac97->private_data;
306
307	for (i = 0; i < 1000; i++) {
308		if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
309			break;
310		udelay(10);
311	}
312	outl((reg << 24) | val, chip->port+AC97_ACCESS);
313}
314
315static int snd_als300_ac97(struct snd_als300 *chip)
316{
317	struct snd_ac97_bus *bus;
318	struct snd_ac97_template ac97;
319	int err;
320	static struct snd_ac97_bus_ops ops = {
321		.write = snd_als300_ac97_write,
322		.read = snd_als300_ac97_read,
323	};
324
325	snd_als300_dbgcallenter();
326	if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
327		return err;
328
329	memset(&ac97, 0, sizeof(ac97));
330	ac97.private_data = chip;
331
332	snd_als300_dbgcallleave();
333	return snd_ac97_mixer(bus, &ac97, &chip->ac97);
334}
335
336/* hardware definition
337 *
338 * In AC97 mode, we always use 48k/16bit/stereo.
339 * Any request to change data type is ignored by
340 * the card when it is running outside of legacy
341 * mode.
342 */
343static struct snd_pcm_hardware snd_als300_playback_hw =
344{
345	.info =			(SNDRV_PCM_INFO_MMAP |
346				SNDRV_PCM_INFO_INTERLEAVED |
347				SNDRV_PCM_INFO_PAUSE |
348				SNDRV_PCM_INFO_MMAP_VALID),
349	.formats =		SNDRV_PCM_FMTBIT_S16,
350	.rates =		SNDRV_PCM_RATE_48000,
351	.rate_min =		48000,
352	.rate_max =		48000,
353	.channels_min =		2,
354	.channels_max =		2,
355	.buffer_bytes_max =	64 * 1024,
356	.period_bytes_min =	64,
357	.period_bytes_max =	32 * 1024,
358	.periods_min =		2,
359	.periods_max =		2,
360};
361
362static struct snd_pcm_hardware snd_als300_capture_hw =
363{
364	.info =			(SNDRV_PCM_INFO_MMAP |
365				SNDRV_PCM_INFO_INTERLEAVED |
366				SNDRV_PCM_INFO_PAUSE |
367				SNDRV_PCM_INFO_MMAP_VALID),
368	.formats =		SNDRV_PCM_FMTBIT_S16,
369	.rates =		SNDRV_PCM_RATE_48000,
370	.rate_min =		48000,
371	.rate_max =		48000,
372	.channels_min =		2,
373	.channels_max =		2,
374	.buffer_bytes_max =	64 * 1024,
375	.period_bytes_min =	64,
376	.period_bytes_max =	32 * 1024,
377	.periods_min =		2,
378	.periods_max =		2,
379};
380
381static int snd_als300_playback_open(struct snd_pcm_substream *substream)
382{
383	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
384	struct snd_pcm_runtime *runtime = substream->runtime;
385	struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
386								GFP_KERNEL);
387
388	snd_als300_dbgcallenter();
389	chip->playback_substream = substream;
390	runtime->hw = snd_als300_playback_hw;
391	runtime->private_data = data;
392	data->control_register = PLAYBACK_CONTROL;
393	data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
394	snd_als300_dbgcallleave();
395	return 0;
396}
397
398static int snd_als300_playback_close(struct snd_pcm_substream *substream)
399{
400	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
401	struct snd_als300_substream_data *data;
402
403	data = substream->runtime->private_data;
404	snd_als300_dbgcallenter();
405	kfree(data);
406	chip->playback_substream = NULL;
407	snd_pcm_lib_free_pages(substream);
408	snd_als300_dbgcallleave();
409	return 0;
410}
411
412static int snd_als300_capture_open(struct snd_pcm_substream *substream)
413{
414	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
415	struct snd_pcm_runtime *runtime = substream->runtime;
416	struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
417								GFP_KERNEL);
418
419	snd_als300_dbgcallenter();
420	chip->capture_substream = substream;
421	runtime->hw = snd_als300_capture_hw;
422	runtime->private_data = data;
423	data->control_register = RECORD_CONTROL;
424	data->block_counter_register = RECORD_BLOCK_COUNTER;
425	snd_als300_dbgcallleave();
426	return 0;
427}
428
429static int snd_als300_capture_close(struct snd_pcm_substream *substream)
430{
431	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
432	struct snd_als300_substream_data *data;
433
434	data = substream->runtime->private_data;
435	snd_als300_dbgcallenter();
436	kfree(data);
437	chip->capture_substream = NULL;
438	snd_pcm_lib_free_pages(substream);
439	snd_als300_dbgcallleave();
440	return 0;
441}
442
443static int snd_als300_pcm_hw_params(struct snd_pcm_substream *substream,
444				    struct snd_pcm_hw_params *hw_params)
445{
446	return snd_pcm_lib_malloc_pages(substream,
447					params_buffer_bytes(hw_params));
448}
449
450static int snd_als300_pcm_hw_free(struct snd_pcm_substream *substream)
451{
452	return snd_pcm_lib_free_pages(substream);
453}
454
455static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
456{
457	u32 tmp;
458	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
459	struct snd_pcm_runtime *runtime = substream->runtime;
460	unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
461	unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
462
463	snd_als300_dbgcallenter();
464	spin_lock_irq(&chip->reg_lock);
465	tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
466	tmp &= ~TRANSFER_START;
467
468	snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
469						period_bytes, buffer_bytes);
470
471	/* set block size */
472	tmp &= 0xffff0000;
473	tmp |= period_bytes - 1;
474	snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
475
476	/* set dma area */
477	snd_als300_gcr_write(chip->port, PLAYBACK_START,
478					runtime->dma_addr);
479	snd_als300_gcr_write(chip->port, PLAYBACK_END,
480					runtime->dma_addr + buffer_bytes - 1);
481	spin_unlock_irq(&chip->reg_lock);
482	snd_als300_dbgcallleave();
483	return 0;
484}
485
486static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
487{
488	u32 tmp;
489	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
490	struct snd_pcm_runtime *runtime = substream->runtime;
491	unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
492	unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
493
494	snd_als300_dbgcallenter();
495	spin_lock_irq(&chip->reg_lock);
496	tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
497	tmp &= ~TRANSFER_START;
498
499	snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
500							buffer_bytes);
501
502	/* set block size */
503	tmp &= 0xffff0000;
504	tmp |= period_bytes - 1;
505
506	/* set dma area */
507	snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
508	snd_als300_gcr_write(chip->port, RECORD_START,
509					runtime->dma_addr);
510	snd_als300_gcr_write(chip->port, RECORD_END,
511					runtime->dma_addr + buffer_bytes - 1);
512	spin_unlock_irq(&chip->reg_lock);
513	snd_als300_dbgcallleave();
514	return 0;
515}
516
517static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
518{
519	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
520	u32 tmp;
521	struct snd_als300_substream_data *data;
522	unsigned short reg;
523	int ret = 0;
524
525	data = substream->runtime->private_data;
526	reg = data->control_register;
527
528	snd_als300_dbgcallenter();
529	spin_lock(&chip->reg_lock);
530	switch (cmd) {
531	case SNDRV_PCM_TRIGGER_START:
532	case SNDRV_PCM_TRIGGER_RESUME:
533		tmp = snd_als300_gcr_read(chip->port, reg);
534		data->period_flipflop = 1;
535		snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
536		snd_als300_dbgplay("TRIGGER START\n");
537		break;
538	case SNDRV_PCM_TRIGGER_STOP:
539	case SNDRV_PCM_TRIGGER_SUSPEND:
540		tmp = snd_als300_gcr_read(chip->port, reg);
541		snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
542		snd_als300_dbgplay("TRIGGER STOP\n");
543		break;
544	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
545		tmp = snd_als300_gcr_read(chip->port, reg);
546		snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
547		snd_als300_dbgplay("TRIGGER PAUSE\n");
548		break;
549	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
550		tmp = snd_als300_gcr_read(chip->port, reg);
551		snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
552		snd_als300_dbgplay("TRIGGER RELEASE\n");
553		break;
554	default:
555		snd_als300_dbgplay("TRIGGER INVALID\n");
556		ret = -EINVAL;
557	}
558	spin_unlock(&chip->reg_lock);
559	snd_als300_dbgcallleave();
560	return ret;
561}
562
563static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
564{
565	u16 current_ptr;
566	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
567	struct snd_als300_substream_data *data;
568	unsigned short period_bytes;
569
570	data = substream->runtime->private_data;
571	period_bytes = snd_pcm_lib_period_bytes(substream);
572
573	snd_als300_dbgcallenter();
574	spin_lock(&chip->reg_lock);
575	current_ptr = (u16) snd_als300_gcr_read(chip->port,
576					data->block_counter_register) + 4;
577	spin_unlock(&chip->reg_lock);
578	if (current_ptr > period_bytes)
579		current_ptr = 0;
580	else
581		current_ptr = period_bytes - current_ptr;
582
583	if (data->period_flipflop == 0)
584		current_ptr += period_bytes;
585	snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
586	snd_als300_dbgcallleave();
587	return bytes_to_frames(substream->runtime, current_ptr);
588}
589
590static struct snd_pcm_ops snd_als300_playback_ops = {
591	.open =		snd_als300_playback_open,
592	.close =	snd_als300_playback_close,
593	.ioctl =	snd_pcm_lib_ioctl,
594	.hw_params =	snd_als300_pcm_hw_params,
595	.hw_free =	snd_als300_pcm_hw_free,
596	.prepare =	snd_als300_playback_prepare,
597	.trigger =	snd_als300_trigger,
598	.pointer =	snd_als300_pointer,
599};
600
601static struct snd_pcm_ops snd_als300_capture_ops = {
602	.open =		snd_als300_capture_open,
603	.close =	snd_als300_capture_close,
604	.ioctl =	snd_pcm_lib_ioctl,
605	.hw_params =	snd_als300_pcm_hw_params,
606	.hw_free =	snd_als300_pcm_hw_free,
607	.prepare =	snd_als300_capture_prepare,
608	.trigger =	snd_als300_trigger,
609	.pointer =	snd_als300_pointer,
610};
611
612static int __devinit snd_als300_new_pcm(struct snd_als300 *chip)
613{
614	struct snd_pcm *pcm;
615	int err;
616
617	snd_als300_dbgcallenter();
618	err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
619	if (err < 0)
620		return err;
621	pcm->private_data = chip;
622	strcpy(pcm->name, "ALS300");
623	chip->pcm = pcm;
624
625	/* set operators */
626	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
627				&snd_als300_playback_ops);
628	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
629				&snd_als300_capture_ops);
630
631	/* pre-allocation of buffers */
632	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
633	snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
634	snd_als300_dbgcallleave();
635	return 0;
636}
637
638static void snd_als300_init(struct snd_als300 *chip)
639{
640	unsigned long flags;
641	u32 tmp;
642
643	snd_als300_dbgcallenter();
644	spin_lock_irqsave(&chip->reg_lock, flags);
645	chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
646								& 0x0000000F;
647	/* Setup DRAM */
648	tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
649	snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
650						(tmp | DRAM_MODE_2)
651						& ~WRITE_TRANS_START);
652
653	/* Enable IRQ output */
654	snd_als300_set_irq_flag(chip, IRQ_ENABLE);
655
656	/* Unmute hardware devices so their outputs get routed to
657	 * the onboard mixer */
658	tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
659	snd_als300_gcr_write(chip->port, MISC_CONTROL,
660			tmp | VMUTE_NORMAL | MMUTE_NORMAL);
661
662	/* Reset volumes */
663	snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
664
665	/* Make sure playback transfer is stopped */
666	tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
667	snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
668			tmp & ~TRANSFER_START);
669	spin_unlock_irqrestore(&chip->reg_lock, flags);
670	snd_als300_dbgcallleave();
671}
672
673static int __devinit snd_als300_create(struct snd_card *card,
674				       struct pci_dev *pci, int chip_type,
675				       struct snd_als300 **rchip)
676{
677	struct snd_als300 *chip;
678	void *irq_handler;
679	int err;
680
681	static struct snd_device_ops ops = {
682		.dev_free = snd_als300_dev_free,
683	};
684	*rchip = NULL;
685
686	snd_als300_dbgcallenter();
687	if ((err = pci_enable_device(pci)) < 0)
688		return err;
689
690	if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
691		pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
692		printk(KERN_ERR "error setting 28bit DMA mask\n");
693		pci_disable_device(pci);
694		return -ENXIO;
695	}
696	pci_set_master(pci);
697
698	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
699	if (chip == NULL) {
700		pci_disable_device(pci);
701		return -ENOMEM;
702	}
703
704	chip->card = card;
705	chip->pci = pci;
706	chip->irq = -1;
707	chip->chip_type = chip_type;
708	spin_lock_init(&chip->reg_lock);
709
710	if ((err = pci_request_regions(pci, "ALS300")) < 0) {
711		kfree(chip);
712		pci_disable_device(pci);
713		return err;
714	}
715	chip->port = pci_resource_start(pci, 0);
716
717	if (chip->chip_type == DEVICE_ALS300_PLUS)
718		irq_handler = snd_als300plus_interrupt;
719	else
720		irq_handler = snd_als300_interrupt;
721
722	if (request_irq(pci->irq, irq_handler, IRQF_SHARED,
723			card->shortname, chip)) {
724		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
725		snd_als300_free(chip);
726		return -EBUSY;
727	}
728	chip->irq = pci->irq;
729
730
731	snd_als300_init(chip);
732
733	err = snd_als300_ac97(chip);
734	if (err < 0) {
735		snd_printk(KERN_WARNING "Could not create ac97\n");
736		snd_als300_free(chip);
737		return err;
738	}
739
740	if ((err = snd_als300_new_pcm(chip)) < 0) {
741		snd_printk(KERN_WARNING "Could not create PCM\n");
742		snd_als300_free(chip);
743		return err;
744	}
745
746	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
747						chip, &ops)) < 0) {
748		snd_als300_free(chip);
749		return err;
750	}
751
752	snd_card_set_dev(card, &pci->dev);
753
754	*rchip = chip;
755	snd_als300_dbgcallleave();
756	return 0;
757}
758
759#ifdef CONFIG_PM
760static int snd_als300_suspend(struct pci_dev *pci, pm_message_t state)
761{
762	struct snd_card *card = pci_get_drvdata(pci);
763	struct snd_als300 *chip = card->private_data;
764
765	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
766	snd_pcm_suspend_all(chip->pcm);
767	snd_ac97_suspend(chip->ac97);
768
769	pci_disable_device(pci);
770	pci_save_state(pci);
771	pci_set_power_state(pci, pci_choose_state(pci, state));
772	return 0;
773}
774
775static int snd_als300_resume(struct pci_dev *pci)
776{
777	struct snd_card *card = pci_get_drvdata(pci);
778	struct snd_als300 *chip = card->private_data;
779
780	pci_set_power_state(pci, PCI_D0);
781	pci_restore_state(pci);
782	if (pci_enable_device(pci) < 0) {
783		printk(KERN_ERR "als300: pci_enable_device failed, "
784		       "disabling device\n");
785		snd_card_disconnect(card);
786		return -EIO;
787	}
788	pci_set_master(pci);
789
790	snd_als300_init(chip);
791	snd_ac97_resume(chip->ac97);
792
793	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
794	return 0;
795}
796#endif
797
798static int __devinit snd_als300_probe(struct pci_dev *pci,
799                             const struct pci_device_id *pci_id)
800{
801	static int dev;
802	struct snd_card *card;
803	struct snd_als300 *chip;
804	int err, chip_type;
805
806	if (dev >= SNDRV_CARDS)
807		return -ENODEV;
808	if (!enable[dev]) {
809		dev++;
810		return -ENOENT;
811	}
812
813	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
814
815	if (err < 0)
816		return err;
817
818	chip_type = pci_id->driver_data;
819
820	if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
821		snd_card_free(card);
822		return err;
823	}
824	card->private_data = chip;
825
826	strcpy(card->driver, "ALS300");
827	if (chip->chip_type == DEVICE_ALS300_PLUS)
828		/* don't know much about ALS300+ yet
829		 * print revision number for now */
830		sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
831	else
832		sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
833							chip->revision - 1);
834	sprintf(card->longname, "%s at 0x%lx irq %i",
835				card->shortname, chip->port, chip->irq);
836
837	if ((err = snd_card_register(card)) < 0) {
838		snd_card_free(card);
839		return err;
840	}
841	pci_set_drvdata(pci, card);
842	dev++;
843	return 0;
844}
845
846static struct pci_driver driver = {
847	.name = "ALS300",
848	.id_table = snd_als300_ids,
849	.probe = snd_als300_probe,
850	.remove = __devexit_p(snd_als300_remove),
851#ifdef CONFIG_PM
852	.suspend = snd_als300_suspend,
853	.resume = snd_als300_resume,
854#endif
855};
856
857static int __init alsa_card_als300_init(void)
858{
859	return pci_register_driver(&driver);
860}
861
862static void __exit alsa_card_als300_exit(void)
863{
864	pci_unregister_driver(&driver);
865}
866
867module_init(alsa_card_als300_init)
868module_exit(alsa_card_als300_exit)
869