xref: /OK3568_Linux_fs/kernel/sound/pci/als4000.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
4*4882a593Smuzhiyun  *  Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
5*4882a593Smuzhiyun  *			  Jaroslav Kysela <perex@perex.cz>
6*4882a593Smuzhiyun  *  Copyright (C) 2002, 2008 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  Framework borrowed from Massimo Piccioni's card-als100.c.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * NOTES
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *  Since Avance does not provide any meaningful documentation, and I
13*4882a593Smuzhiyun  *  bought an ALS4000 based soundcard, I was forced to base this driver
14*4882a593Smuzhiyun  *  on reverse engineering.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  Note: this is no longer true (thank you!):
17*4882a593Smuzhiyun  *  pretty verbose chip docu (ALS4000a.PDF) can be found on the ALSA web site.
18*4882a593Smuzhiyun  *  Page numbers stated anywhere below with the "SPECS_PAGE:" tag
19*4882a593Smuzhiyun  *  refer to: ALS4000a.PDF specs Ver 1.0, May 28th, 1998.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *  The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
22*4882a593Smuzhiyun  *  ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport
23*4882a593Smuzhiyun  *  interface. These subsystems can be mapped into ISA io-port space,
24*4882a593Smuzhiyun  *  using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ
25*4882a593Smuzhiyun  *  services to the subsystems.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * While ALS4000 is very similar to a SoundBlaster, the differences in
28*4882a593Smuzhiyun  * DMA and capturing require more changes to the SoundBlaster than
29*4882a593Smuzhiyun  * desirable, so I made this separate driver.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * The ALS4000 can do real full duplex playback/capture.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * FMDAC:
34*4882a593Smuzhiyun  * - 0x4f -> port 0x14
35*4882a593Smuzhiyun  * - port 0x15 |= 1
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * Enable/disable 3D sound:
38*4882a593Smuzhiyun  * - 0x50 -> port 0x14
39*4882a593Smuzhiyun  * - change bit 6 (0x40) of port 0x15
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * Set QSound:
42*4882a593Smuzhiyun  * - 0xdb -> port 0x14
43*4882a593Smuzhiyun  * - set port 0x15:
44*4882a593Smuzhiyun  *   0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Set KSound:
47*4882a593Smuzhiyun  * - value -> some port 0x0c0d
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * ToDo:
50*4882a593Smuzhiyun  * - by default, don't enable legacy game and use PCI game I/O
51*4882a593Smuzhiyun  * - power management? (card can do voice wakeup according to datasheet!!)
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #include <linux/io.h>
55*4882a593Smuzhiyun #include <linux/init.h>
56*4882a593Smuzhiyun #include <linux/pci.h>
57*4882a593Smuzhiyun #include <linux/gameport.h>
58*4882a593Smuzhiyun #include <linux/module.h>
59*4882a593Smuzhiyun #include <linux/dma-mapping.h>
60*4882a593Smuzhiyun #include <sound/core.h>
61*4882a593Smuzhiyun #include <sound/pcm.h>
62*4882a593Smuzhiyun #include <sound/rawmidi.h>
63*4882a593Smuzhiyun #include <sound/mpu401.h>
64*4882a593Smuzhiyun #include <sound/opl3.h>
65*4882a593Smuzhiyun #include <sound/sb.h>
66*4882a593Smuzhiyun #include <sound/initval.h>
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>, Andreas Mohr");
69*4882a593Smuzhiyun MODULE_DESCRIPTION("Avance Logic ALS4000");
70*4882a593Smuzhiyun MODULE_LICENSE("GPL");
71*4882a593Smuzhiyun MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #if IS_REACHABLE(CONFIG_GAMEPORT)
74*4882a593Smuzhiyun #define SUPPORT_JOYSTICK 1
75*4882a593Smuzhiyun #endif
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
78*4882a593Smuzhiyun static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
79*4882a593Smuzhiyun static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
80*4882a593Smuzhiyun #ifdef SUPPORT_JOYSTICK
81*4882a593Smuzhiyun static int joystick_port[SNDRV_CARDS];
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun module_param_array(index, int, NULL, 0444);
85*4882a593Smuzhiyun MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
86*4882a593Smuzhiyun module_param_array(id, charp, NULL, 0444);
87*4882a593Smuzhiyun MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
88*4882a593Smuzhiyun module_param_array(enable, bool, NULL, 0444);
89*4882a593Smuzhiyun MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
90*4882a593Smuzhiyun #ifdef SUPPORT_JOYSTICK
91*4882a593Smuzhiyun module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
92*4882a593Smuzhiyun MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
93*4882a593Smuzhiyun #endif
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun struct snd_card_als4000 {
96*4882a593Smuzhiyun 	/* most frequent access first */
97*4882a593Smuzhiyun 	unsigned long iobase;
98*4882a593Smuzhiyun 	struct pci_dev *pci;
99*4882a593Smuzhiyun 	struct snd_sb *chip;
100*4882a593Smuzhiyun #ifdef SUPPORT_JOYSTICK
101*4882a593Smuzhiyun 	struct gameport *gameport;
102*4882a593Smuzhiyun #endif
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun static const struct pci_device_id snd_als4000_ids[] = {
106*4882a593Smuzhiyun 	{ 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* ALS4000 */
107*4882a593Smuzhiyun 	{ 0, }
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun enum als4k_iobase_t {
113*4882a593Smuzhiyun 	/* IOx: B == Byte, W = Word, D = DWord; SPECS_PAGE: 37 */
114*4882a593Smuzhiyun 	ALS4K_IOD_00_AC97_ACCESS = 0x00,
115*4882a593Smuzhiyun 	ALS4K_IOW_04_AC97_READ = 0x04,
116*4882a593Smuzhiyun 	ALS4K_IOB_06_AC97_STATUS = 0x06,
117*4882a593Smuzhiyun 	ALS4K_IOB_07_IRQSTATUS = 0x07,
118*4882a593Smuzhiyun 	ALS4K_IOD_08_GCR_DATA = 0x08,
119*4882a593Smuzhiyun 	ALS4K_IOB_0C_GCR_INDEX = 0x0c,
120*4882a593Smuzhiyun 	ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU = 0x0e,
121*4882a593Smuzhiyun 	ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
122*4882a593Smuzhiyun 	ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
123*4882a593Smuzhiyun 	ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
124*4882a593Smuzhiyun 	ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
125*4882a593Smuzhiyun 	ALS4K_IOB_14_MIXER_INDEX = 0x14,
126*4882a593Smuzhiyun 	ALS4K_IOB_15_MIXER_DATA = 0x15,
127*4882a593Smuzhiyun 	ALS4K_IOB_16_ESP_RESET = 0x16,
128*4882a593Smuzhiyun 	ALS4K_IOB_16_ACK_FOR_CR1E = 0x16, /* 2nd function */
129*4882a593Smuzhiyun 	ALS4K_IOB_18_OPL_ADDR0 = 0x18,
130*4882a593Smuzhiyun 	ALS4K_IOB_19_OPL_ADDR1 = 0x19,
131*4882a593Smuzhiyun 	ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
132*4882a593Smuzhiyun 	ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
133*4882a593Smuzhiyun 	ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
134*4882a593Smuzhiyun 	ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
135*4882a593Smuzhiyun 	ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
136*4882a593Smuzhiyun 	ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
137*4882a593Smuzhiyun 	ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
138*4882a593Smuzhiyun 	ALS4K_IOB_30_MIDI_DATA = 0x30,
139*4882a593Smuzhiyun 	ALS4K_IOB_31_MIDI_STATUS = 0x31,
140*4882a593Smuzhiyun 	ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun enum als4k_iobase_0e_t {
144*4882a593Smuzhiyun 	ALS4K_IOB_0E_MPU_IRQ = 0x10,
145*4882a593Smuzhiyun 	ALS4K_IOB_0E_CR1E_IRQ = 0x40,
146*4882a593Smuzhiyun 	ALS4K_IOB_0E_SB_DMA_IRQ = 0x80,
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun enum als4k_gcr_t { /* all registers 32bit wide; SPECS_PAGE: 38 to 42 */
150*4882a593Smuzhiyun 	ALS4K_GCR8C_MISC_CTRL = 0x8c,
151*4882a593Smuzhiyun 	ALS4K_GCR90_TEST_MODE_REG = 0x90,
152*4882a593Smuzhiyun 	ALS4K_GCR91_DMA0_ADDR = 0x91,
153*4882a593Smuzhiyun 	ALS4K_GCR92_DMA0_MODE_COUNT = 0x92,
154*4882a593Smuzhiyun 	ALS4K_GCR93_DMA1_ADDR = 0x93,
155*4882a593Smuzhiyun 	ALS4K_GCR94_DMA1_MODE_COUNT = 0x94,
156*4882a593Smuzhiyun 	ALS4K_GCR95_DMA3_ADDR = 0x95,
157*4882a593Smuzhiyun 	ALS4K_GCR96_DMA3_MODE_COUNT = 0x96,
158*4882a593Smuzhiyun 	ALS4K_GCR99_DMA_EMULATION_CTRL = 0x99,
159*4882a593Smuzhiyun 	ALS4K_GCRA0_FIFO1_CURRENT_ADDR = 0xa0,
160*4882a593Smuzhiyun 	ALS4K_GCRA1_FIFO1_STATUS_BYTECOUNT = 0xa1,
161*4882a593Smuzhiyun 	ALS4K_GCRA2_FIFO2_PCIADDR = 0xa2,
162*4882a593Smuzhiyun 	ALS4K_GCRA3_FIFO2_COUNT = 0xa3,
163*4882a593Smuzhiyun 	ALS4K_GCRA4_FIFO2_CURRENT_ADDR = 0xa4,
164*4882a593Smuzhiyun 	ALS4K_GCRA5_FIFO1_STATUS_BYTECOUNT = 0xa5,
165*4882a593Smuzhiyun 	ALS4K_GCRA6_PM_CTRL = 0xa6,
166*4882a593Smuzhiyun 	ALS4K_GCRA7_PCI_ACCESS_STORAGE = 0xa7,
167*4882a593Smuzhiyun 	ALS4K_GCRA8_LEGACY_CFG1 = 0xa8,
168*4882a593Smuzhiyun 	ALS4K_GCRA9_LEGACY_CFG2 = 0xa9,
169*4882a593Smuzhiyun 	ALS4K_GCRFF_DUMMY_SCRATCH = 0xff,
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun enum als4k_gcr8c_t {
173*4882a593Smuzhiyun 	ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
174*4882a593Smuzhiyun 	ALS4K_GCR8C_CHIP_REV_MASK = 0xf0000
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun 
snd_als4k_iobase_writeb(unsigned long iobase,enum als4k_iobase_t reg,u8 val)177*4882a593Smuzhiyun static inline void snd_als4k_iobase_writeb(unsigned long iobase,
178*4882a593Smuzhiyun 						enum als4k_iobase_t reg,
179*4882a593Smuzhiyun 						u8 val)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	outb(val, iobase + reg);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
snd_als4k_iobase_writel(unsigned long iobase,enum als4k_iobase_t reg,u32 val)184*4882a593Smuzhiyun static inline void snd_als4k_iobase_writel(unsigned long iobase,
185*4882a593Smuzhiyun 						enum als4k_iobase_t reg,
186*4882a593Smuzhiyun 						u32 val)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	outl(val, iobase + reg);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
snd_als4k_iobase_readb(unsigned long iobase,enum als4k_iobase_t reg)191*4882a593Smuzhiyun static inline u8 snd_als4k_iobase_readb(unsigned long iobase,
192*4882a593Smuzhiyun 						enum als4k_iobase_t reg)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	return inb(iobase + reg);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
snd_als4k_iobase_readl(unsigned long iobase,enum als4k_iobase_t reg)197*4882a593Smuzhiyun static inline u32 snd_als4k_iobase_readl(unsigned long iobase,
198*4882a593Smuzhiyun 						enum als4k_iobase_t reg)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	return inl(iobase + reg);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
snd_als4k_gcr_write_addr(unsigned long iobase,enum als4k_gcr_t reg,u32 val)203*4882a593Smuzhiyun static inline void snd_als4k_gcr_write_addr(unsigned long iobase,
204*4882a593Smuzhiyun 						 enum als4k_gcr_t reg,
205*4882a593Smuzhiyun 						 u32 val)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
208*4882a593Smuzhiyun 	snd_als4k_iobase_writel(iobase, ALS4K_IOD_08_GCR_DATA, val);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
snd_als4k_gcr_write(struct snd_sb * sb,enum als4k_gcr_t reg,u32 val)211*4882a593Smuzhiyun static inline void snd_als4k_gcr_write(struct snd_sb *sb,
212*4882a593Smuzhiyun 					 enum als4k_gcr_t reg,
213*4882a593Smuzhiyun 					 u32 val)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	snd_als4k_gcr_write_addr(sb->alt_port, reg, val);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
snd_als4k_gcr_read_addr(unsigned long iobase,enum als4k_gcr_t reg)218*4882a593Smuzhiyun static inline u32 snd_als4k_gcr_read_addr(unsigned long iobase,
219*4882a593Smuzhiyun 						 enum als4k_gcr_t reg)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	/* SPECS_PAGE: 37/38 */
222*4882a593Smuzhiyun 	snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
223*4882a593Smuzhiyun 	return snd_als4k_iobase_readl(iobase, ALS4K_IOD_08_GCR_DATA);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
snd_als4k_gcr_read(struct snd_sb * sb,enum als4k_gcr_t reg)226*4882a593Smuzhiyun static inline u32 snd_als4k_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	return snd_als4k_gcr_read_addr(sb->alt_port, reg);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun enum als4k_cr_t { /* all registers 8bit wide; SPECS_PAGE: 20 to 23 */
232*4882a593Smuzhiyun 	ALS4K_CR0_SB_CONFIG = 0x00,
233*4882a593Smuzhiyun 	ALS4K_CR2_MISC_CONTROL = 0x02,
234*4882a593Smuzhiyun 	ALS4K_CR3_CONFIGURATION = 0x03,
235*4882a593Smuzhiyun 	ALS4K_CR17_FIFO_STATUS = 0x17,
236*4882a593Smuzhiyun 	ALS4K_CR18_ESP_MAJOR_VERSION = 0x18,
237*4882a593Smuzhiyun 	ALS4K_CR19_ESP_MINOR_VERSION = 0x19,
238*4882a593Smuzhiyun 	ALS4K_CR1A_MPU401_UART_MODE_CONTROL = 0x1a,
239*4882a593Smuzhiyun 	ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO = 0x1c,
240*4882a593Smuzhiyun 	ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI = 0x1d,
241*4882a593Smuzhiyun 	ALS4K_CR1E_FIFO2_CONTROL = 0x1e, /* secondary PCM FIFO (recording) */
242*4882a593Smuzhiyun 	ALS4K_CR3A_MISC_CONTROL = 0x3a,
243*4882a593Smuzhiyun 	ALS4K_CR3B_CRC32_BYTE0 = 0x3b, /* for testing, activate via CR3A */
244*4882a593Smuzhiyun 	ALS4K_CR3C_CRC32_BYTE1 = 0x3c,
245*4882a593Smuzhiyun 	ALS4K_CR3D_CRC32_BYTE2 = 0x3d,
246*4882a593Smuzhiyun 	ALS4K_CR3E_CRC32_BYTE3 = 0x3e,
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun enum als4k_cr0_t {
250*4882a593Smuzhiyun 	ALS4K_CR0_DMA_CONTIN_MODE_CTRL = 0x02, /* IRQ/FIFO controlled for 0/1 */
251*4882a593Smuzhiyun 	ALS4K_CR0_DMA_90H_MODE_CTRL = 0x04, /* IRQ/FIFO controlled for 0/1 */
252*4882a593Smuzhiyun 	ALS4K_CR0_MX80_81_REG_WRITE_ENABLE = 0x80,
253*4882a593Smuzhiyun };
254*4882a593Smuzhiyun 
snd_als4_cr_write(struct snd_sb * chip,enum als4k_cr_t reg,u8 data)255*4882a593Smuzhiyun static inline void snd_als4_cr_write(struct snd_sb *chip,
256*4882a593Smuzhiyun 					enum als4k_cr_t reg,
257*4882a593Smuzhiyun 					u8 data)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	/* Control Register is reg | 0xc0 (bit 7, 6 set) on sbmixer_index
260*4882a593Smuzhiyun 	 * NOTE: assumes chip->mixer_lock to be locked externally already!
261*4882a593Smuzhiyun 	 * SPECS_PAGE: 6 */
262*4882a593Smuzhiyun 	snd_sbmixer_write(chip, reg | 0xc0, data);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
snd_als4_cr_read(struct snd_sb * chip,enum als4k_cr_t reg)265*4882a593Smuzhiyun static inline u8 snd_als4_cr_read(struct snd_sb *chip,
266*4882a593Smuzhiyun 					enum als4k_cr_t reg)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	/* NOTE: assumes chip->mixer_lock to be locked externally already! */
269*4882a593Smuzhiyun 	return snd_sbmixer_read(chip, reg | 0xc0);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 
snd_als4000_set_rate(struct snd_sb * chip,unsigned int rate)274*4882a593Smuzhiyun static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	if (!(chip->mode & SB_RATE_LOCK)) {
277*4882a593Smuzhiyun 		snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
278*4882a593Smuzhiyun 		snd_sbdsp_command(chip, rate>>8);
279*4882a593Smuzhiyun 		snd_sbdsp_command(chip, rate);
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun 
snd_als4000_set_capture_dma(struct snd_sb * chip,dma_addr_t addr,unsigned size)283*4882a593Smuzhiyun static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
284*4882a593Smuzhiyun 					       dma_addr_t addr, unsigned size)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	/* SPECS_PAGE: 40 */
287*4882a593Smuzhiyun 	snd_als4k_gcr_write(chip, ALS4K_GCRA2_FIFO2_PCIADDR, addr);
288*4882a593Smuzhiyun 	snd_als4k_gcr_write(chip, ALS4K_GCRA3_FIFO2_COUNT, (size-1));
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
snd_als4000_set_playback_dma(struct snd_sb * chip,dma_addr_t addr,unsigned size)291*4882a593Smuzhiyun static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
292*4882a593Smuzhiyun 						dma_addr_t addr,
293*4882a593Smuzhiyun 						unsigned size)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	/* SPECS_PAGE: 38 */
296*4882a593Smuzhiyun 	snd_als4k_gcr_write(chip, ALS4K_GCR91_DMA0_ADDR, addr);
297*4882a593Smuzhiyun 	snd_als4k_gcr_write(chip, ALS4K_GCR92_DMA0_MODE_COUNT,
298*4882a593Smuzhiyun 							(size-1)|0x180000);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun #define ALS4000_FORMAT_SIGNED	(1<<0)
302*4882a593Smuzhiyun #define ALS4000_FORMAT_16BIT	(1<<1)
303*4882a593Smuzhiyun #define ALS4000_FORMAT_STEREO	(1<<2)
304*4882a593Smuzhiyun 
snd_als4000_get_format(struct snd_pcm_runtime * runtime)305*4882a593Smuzhiyun static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	int result;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	result = 0;
310*4882a593Smuzhiyun 	if (snd_pcm_format_signed(runtime->format))
311*4882a593Smuzhiyun 		result |= ALS4000_FORMAT_SIGNED;
312*4882a593Smuzhiyun 	if (snd_pcm_format_physical_width(runtime->format) == 16)
313*4882a593Smuzhiyun 		result |= ALS4000_FORMAT_16BIT;
314*4882a593Smuzhiyun 	if (runtime->channels > 1)
315*4882a593Smuzhiyun 		result |= ALS4000_FORMAT_STEREO;
316*4882a593Smuzhiyun 	return result;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun /* structure for setting up playback */
320*4882a593Smuzhiyun static const struct {
321*4882a593Smuzhiyun 	unsigned char dsp_cmd, dma_on, dma_off, format;
322*4882a593Smuzhiyun } playback_cmd_vals[]={
323*4882a593Smuzhiyun /* ALS4000_FORMAT_U8_MONO */
324*4882a593Smuzhiyun { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
325*4882a593Smuzhiyun /* ALS4000_FORMAT_S8_MONO */
326*4882a593Smuzhiyun { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
327*4882a593Smuzhiyun /* ALS4000_FORMAT_U16L_MONO */
328*4882a593Smuzhiyun { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
329*4882a593Smuzhiyun /* ALS4000_FORMAT_S16L_MONO */
330*4882a593Smuzhiyun { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
331*4882a593Smuzhiyun /* ALS4000_FORMAT_U8_STEREO */
332*4882a593Smuzhiyun { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
333*4882a593Smuzhiyun /* ALS4000_FORMAT_S8_STEREO */
334*4882a593Smuzhiyun { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
335*4882a593Smuzhiyun /* ALS4000_FORMAT_U16L_STEREO */
336*4882a593Smuzhiyun { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
337*4882a593Smuzhiyun /* ALS4000_FORMAT_S16L_STEREO */
338*4882a593Smuzhiyun { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
339*4882a593Smuzhiyun };
340*4882a593Smuzhiyun #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun /* structure for setting up capture */
343*4882a593Smuzhiyun enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
344*4882a593Smuzhiyun static const unsigned char capture_cmd_vals[]=
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun CMD_WIDTH8|CMD_MONO,			/* ALS4000_FORMAT_U8_MONO */
347*4882a593Smuzhiyun CMD_WIDTH8|CMD_SIGNED|CMD_MONO,		/* ALS4000_FORMAT_S8_MONO */
348*4882a593Smuzhiyun CMD_MONO,				/* ALS4000_FORMAT_U16L_MONO */
349*4882a593Smuzhiyun CMD_SIGNED|CMD_MONO,			/* ALS4000_FORMAT_S16L_MONO */
350*4882a593Smuzhiyun CMD_WIDTH8|CMD_STEREO,			/* ALS4000_FORMAT_U8_STEREO */
351*4882a593Smuzhiyun CMD_WIDTH8|CMD_SIGNED|CMD_STEREO,	/* ALS4000_FORMAT_S8_STEREO */
352*4882a593Smuzhiyun CMD_STEREO,				/* ALS4000_FORMAT_U16L_STEREO */
353*4882a593Smuzhiyun CMD_SIGNED|CMD_STEREO,			/* ALS4000_FORMAT_S16L_STEREO */
354*4882a593Smuzhiyun };
355*4882a593Smuzhiyun #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
356*4882a593Smuzhiyun 
snd_als4000_capture_prepare(struct snd_pcm_substream * substream)357*4882a593Smuzhiyun static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
360*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
361*4882a593Smuzhiyun 	unsigned long size;
362*4882a593Smuzhiyun 	unsigned count;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	chip->capture_format = snd_als4000_get_format(runtime);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	size = snd_pcm_lib_buffer_bytes(substream);
367*4882a593Smuzhiyun 	count = snd_pcm_lib_period_bytes(substream);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	if (chip->capture_format & ALS4000_FORMAT_16BIT)
370*4882a593Smuzhiyun 		count >>= 1;
371*4882a593Smuzhiyun 	count--;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	spin_lock_irq(&chip->reg_lock);
374*4882a593Smuzhiyun 	snd_als4000_set_rate(chip, runtime->rate);
375*4882a593Smuzhiyun 	snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
376*4882a593Smuzhiyun 	spin_unlock_irq(&chip->reg_lock);
377*4882a593Smuzhiyun 	spin_lock_irq(&chip->mixer_lock);
378*4882a593Smuzhiyun 	snd_als4_cr_write(chip, ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO, count & 0xff);
379*4882a593Smuzhiyun 	snd_als4_cr_write(chip, ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI, count >> 8);
380*4882a593Smuzhiyun 	spin_unlock_irq(&chip->mixer_lock);
381*4882a593Smuzhiyun 	return 0;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun 
snd_als4000_playback_prepare(struct snd_pcm_substream * substream)384*4882a593Smuzhiyun static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
387*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
388*4882a593Smuzhiyun 	unsigned long size;
389*4882a593Smuzhiyun 	unsigned count;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	chip->playback_format = snd_als4000_get_format(runtime);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	size = snd_pcm_lib_buffer_bytes(substream);
394*4882a593Smuzhiyun 	count = snd_pcm_lib_period_bytes(substream);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (chip->playback_format & ALS4000_FORMAT_16BIT)
397*4882a593Smuzhiyun 		count >>= 1;
398*4882a593Smuzhiyun 	count--;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* FIXME: from second playback on, there's a lot more clicks and pops
401*4882a593Smuzhiyun 	 * involved here than on first playback. Fiddling with
402*4882a593Smuzhiyun 	 * tons of different settings didn't help (DMA, speaker on/off,
403*4882a593Smuzhiyun 	 * reordering, ...). Something seems to get enabled on playback
404*4882a593Smuzhiyun 	 * that I haven't found out how to disable again, which then causes
405*4882a593Smuzhiyun 	 * the switching pops to reach the speakers the next time here. */
406*4882a593Smuzhiyun 	spin_lock_irq(&chip->reg_lock);
407*4882a593Smuzhiyun 	snd_als4000_set_rate(chip, runtime->rate);
408*4882a593Smuzhiyun 	snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	/* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
411*4882a593Smuzhiyun 	/* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
412*4882a593Smuzhiyun 	snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
413*4882a593Smuzhiyun 	snd_sbdsp_command(chip, playback_cmd(chip).format);
414*4882a593Smuzhiyun 	snd_sbdsp_command(chip, count & 0xff);
415*4882a593Smuzhiyun 	snd_sbdsp_command(chip, count >> 8);
416*4882a593Smuzhiyun 	snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
417*4882a593Smuzhiyun 	spin_unlock_irq(&chip->reg_lock);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	return 0;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
snd_als4000_capture_trigger(struct snd_pcm_substream * substream,int cmd)422*4882a593Smuzhiyun static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
425*4882a593Smuzhiyun 	int result = 0;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* FIXME race condition in here!!!
428*4882a593Smuzhiyun 	   chip->mode non-atomic update gets consistently protected
429*4882a593Smuzhiyun 	   by reg_lock always, _except_ for this place!!
430*4882a593Smuzhiyun 	   Probably need to take reg_lock as outer (or inner??) lock, too.
431*4882a593Smuzhiyun 	   (or serialize both lock operations? probably not, though... - racy?)
432*4882a593Smuzhiyun 	*/
433*4882a593Smuzhiyun 	spin_lock(&chip->mixer_lock);
434*4882a593Smuzhiyun 	switch (cmd) {
435*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_START:
436*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_RESUME:
437*4882a593Smuzhiyun 		chip->mode |= SB_RATE_LOCK_CAPTURE;
438*4882a593Smuzhiyun 		snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
439*4882a593Smuzhiyun 							 capture_cmd(chip));
440*4882a593Smuzhiyun 		break;
441*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_STOP:
442*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_SUSPEND:
443*4882a593Smuzhiyun 		chip->mode &= ~SB_RATE_LOCK_CAPTURE;
444*4882a593Smuzhiyun 		snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
445*4882a593Smuzhiyun 							 capture_cmd(chip));
446*4882a593Smuzhiyun 		break;
447*4882a593Smuzhiyun 	default:
448*4882a593Smuzhiyun 		result = -EINVAL;
449*4882a593Smuzhiyun 		break;
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 	spin_unlock(&chip->mixer_lock);
452*4882a593Smuzhiyun 	return result;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
snd_als4000_playback_trigger(struct snd_pcm_substream * substream,int cmd)455*4882a593Smuzhiyun static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
458*4882a593Smuzhiyun 	int result = 0;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	spin_lock(&chip->reg_lock);
461*4882a593Smuzhiyun 	switch (cmd) {
462*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_START:
463*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_RESUME:
464*4882a593Smuzhiyun 		chip->mode |= SB_RATE_LOCK_PLAYBACK;
465*4882a593Smuzhiyun 		snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
466*4882a593Smuzhiyun 		break;
467*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_STOP:
468*4882a593Smuzhiyun 	case SNDRV_PCM_TRIGGER_SUSPEND:
469*4882a593Smuzhiyun 		snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
470*4882a593Smuzhiyun 		chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
471*4882a593Smuzhiyun 		break;
472*4882a593Smuzhiyun 	default:
473*4882a593Smuzhiyun 		result = -EINVAL;
474*4882a593Smuzhiyun 		break;
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 	spin_unlock(&chip->reg_lock);
477*4882a593Smuzhiyun 	return result;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
snd_als4000_capture_pointer(struct snd_pcm_substream * substream)480*4882a593Smuzhiyun static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
483*4882a593Smuzhiyun 	unsigned int result;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	spin_lock(&chip->reg_lock);
486*4882a593Smuzhiyun 	result = snd_als4k_gcr_read(chip, ALS4K_GCRA4_FIFO2_CURRENT_ADDR);
487*4882a593Smuzhiyun 	spin_unlock(&chip->reg_lock);
488*4882a593Smuzhiyun 	result &= 0xffff;
489*4882a593Smuzhiyun 	return bytes_to_frames( substream->runtime, result );
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
snd_als4000_playback_pointer(struct snd_pcm_substream * substream)492*4882a593Smuzhiyun static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
495*4882a593Smuzhiyun 	unsigned result;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	spin_lock(&chip->reg_lock);
498*4882a593Smuzhiyun 	result = snd_als4k_gcr_read(chip, ALS4K_GCRA0_FIFO1_CURRENT_ADDR);
499*4882a593Smuzhiyun 	spin_unlock(&chip->reg_lock);
500*4882a593Smuzhiyun 	result &= 0xffff;
501*4882a593Smuzhiyun 	return bytes_to_frames( substream->runtime, result );
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun /* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
505*4882a593Smuzhiyun  * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
506*4882a593Smuzhiyun  * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
507*4882a593Smuzhiyun  * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
508*4882a593Smuzhiyun  * register (alt_port + ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU). Probably something
509*4882a593Smuzhiyun  * could be optimized here to query/write one register only...
510*4882a593Smuzhiyun  * And even if both registers need to be queried, then there's still the
511*4882a593Smuzhiyun  * question of whether it's actually correct to ACK PCI IRQ before reading
512*4882a593Smuzhiyun  * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
513*4882a593Smuzhiyun  * SB IRQ status.
514*4882a593Smuzhiyun  * (hmm, SPECS_PAGE: 38 mentions it the other way around!)
515*4882a593Smuzhiyun  * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
516*4882a593Smuzhiyun  * */
snd_als4000_interrupt(int irq,void * dev_id)517*4882a593Smuzhiyun static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	struct snd_sb *chip = dev_id;
520*4882a593Smuzhiyun 	unsigned pci_irqstatus;
521*4882a593Smuzhiyun 	unsigned sb_irqstatus;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	/* find out which bit of the ALS4000 PCI block produced the interrupt,
524*4882a593Smuzhiyun 	   SPECS_PAGE: 38, 5 */
525*4882a593Smuzhiyun 	pci_irqstatus = snd_als4k_iobase_readb(chip->alt_port,
526*4882a593Smuzhiyun 				 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU);
527*4882a593Smuzhiyun 	if ((pci_irqstatus & ALS4K_IOB_0E_SB_DMA_IRQ)
528*4882a593Smuzhiyun 	 && (chip->playback_substream)) /* playback */
529*4882a593Smuzhiyun 		snd_pcm_period_elapsed(chip->playback_substream);
530*4882a593Smuzhiyun 	if ((pci_irqstatus & ALS4K_IOB_0E_CR1E_IRQ)
531*4882a593Smuzhiyun 	 && (chip->capture_substream)) /* capturing */
532*4882a593Smuzhiyun 		snd_pcm_period_elapsed(chip->capture_substream);
533*4882a593Smuzhiyun 	if ((pci_irqstatus & ALS4K_IOB_0E_MPU_IRQ)
534*4882a593Smuzhiyun 	 && (chip->rmidi)) /* MPU401 interrupt */
535*4882a593Smuzhiyun 		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
536*4882a593Smuzhiyun 	/* ACK the PCI block IRQ */
537*4882a593Smuzhiyun 	snd_als4k_iobase_writeb(chip->alt_port,
538*4882a593Smuzhiyun 			 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU, pci_irqstatus);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	spin_lock(&chip->mixer_lock);
541*4882a593Smuzhiyun 	/* SPECS_PAGE: 20 */
542*4882a593Smuzhiyun 	sb_irqstatus = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
543*4882a593Smuzhiyun 	spin_unlock(&chip->mixer_lock);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (sb_irqstatus & SB_IRQTYPE_8BIT)
546*4882a593Smuzhiyun 		snd_sb_ack_8bit(chip);
547*4882a593Smuzhiyun 	if (sb_irqstatus & SB_IRQTYPE_16BIT)
548*4882a593Smuzhiyun 		snd_sb_ack_16bit(chip);
549*4882a593Smuzhiyun 	if (sb_irqstatus & SB_IRQTYPE_MPUIN)
550*4882a593Smuzhiyun 		inb(chip->mpu_port);
551*4882a593Smuzhiyun 	if (sb_irqstatus & ALS4K_IRQTYPE_CR1E_DMA)
552*4882a593Smuzhiyun 		snd_als4k_iobase_readb(chip->alt_port,
553*4882a593Smuzhiyun 					ALS4K_IOB_16_ACK_FOR_CR1E);
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	/* dev_dbg(chip->card->dev, "als4000: irq 0x%04x 0x%04x\n",
556*4882a593Smuzhiyun 					 pci_irqstatus, sb_irqstatus); */
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	/* only ack the things we actually handled above */
559*4882a593Smuzhiyun 	return IRQ_RETVAL(
560*4882a593Smuzhiyun 	     (pci_irqstatus & (ALS4K_IOB_0E_SB_DMA_IRQ|ALS4K_IOB_0E_CR1E_IRQ|
561*4882a593Smuzhiyun 				ALS4K_IOB_0E_MPU_IRQ))
562*4882a593Smuzhiyun 	  || (sb_irqstatus & (SB_IRQTYPE_8BIT|SB_IRQTYPE_16BIT|
563*4882a593Smuzhiyun 				SB_IRQTYPE_MPUIN|ALS4K_IRQTYPE_CR1E_DMA))
564*4882a593Smuzhiyun 	);
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /*****************************************************************/
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun static const struct snd_pcm_hardware snd_als4000_playback =
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
572*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_MMAP_VALID),
573*4882a593Smuzhiyun 	.formats =		SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
574*4882a593Smuzhiyun 				SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,	/* formats */
575*4882a593Smuzhiyun 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
576*4882a593Smuzhiyun 	.rate_min =		4000,
577*4882a593Smuzhiyun 	.rate_max =		48000,
578*4882a593Smuzhiyun 	.channels_min =		1,
579*4882a593Smuzhiyun 	.channels_max =		2,
580*4882a593Smuzhiyun 	.buffer_bytes_max =	65536,
581*4882a593Smuzhiyun 	.period_bytes_min =	64,
582*4882a593Smuzhiyun 	.period_bytes_max =	65536,
583*4882a593Smuzhiyun 	.periods_min =		1,
584*4882a593Smuzhiyun 	.periods_max =		1024,
585*4882a593Smuzhiyun 	.fifo_size =		0
586*4882a593Smuzhiyun };
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun static const struct snd_pcm_hardware snd_als4000_capture =
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
591*4882a593Smuzhiyun 				 SNDRV_PCM_INFO_MMAP_VALID),
592*4882a593Smuzhiyun 	.formats =		SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
593*4882a593Smuzhiyun 				SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,	/* formats */
594*4882a593Smuzhiyun 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
595*4882a593Smuzhiyun 	.rate_min =		4000,
596*4882a593Smuzhiyun 	.rate_max =		48000,
597*4882a593Smuzhiyun 	.channels_min =		1,
598*4882a593Smuzhiyun 	.channels_max =		2,
599*4882a593Smuzhiyun 	.buffer_bytes_max =	65536,
600*4882a593Smuzhiyun 	.period_bytes_min =	64,
601*4882a593Smuzhiyun 	.period_bytes_max =	65536,
602*4882a593Smuzhiyun 	.periods_min =		1,
603*4882a593Smuzhiyun 	.periods_max =		1024,
604*4882a593Smuzhiyun 	.fifo_size =		0
605*4882a593Smuzhiyun };
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun /*****************************************************************/
608*4882a593Smuzhiyun 
snd_als4000_playback_open(struct snd_pcm_substream * substream)609*4882a593Smuzhiyun static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
612*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	chip->playback_substream = substream;
615*4882a593Smuzhiyun 	runtime->hw = snd_als4000_playback;
616*4882a593Smuzhiyun 	return 0;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
snd_als4000_playback_close(struct snd_pcm_substream * substream)619*4882a593Smuzhiyun static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	chip->playback_substream = NULL;
624*4882a593Smuzhiyun 	return 0;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
snd_als4000_capture_open(struct snd_pcm_substream * substream)627*4882a593Smuzhiyun static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
630*4882a593Smuzhiyun 	struct snd_pcm_runtime *runtime = substream->runtime;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	chip->capture_substream = substream;
633*4882a593Smuzhiyun 	runtime->hw = snd_als4000_capture;
634*4882a593Smuzhiyun 	return 0;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
snd_als4000_capture_close(struct snd_pcm_substream * substream)637*4882a593Smuzhiyun static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	struct snd_sb *chip = snd_pcm_substream_chip(substream);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	chip->capture_substream = NULL;
642*4882a593Smuzhiyun 	return 0;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun /******************************************************************/
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun static const struct snd_pcm_ops snd_als4000_playback_ops = {
648*4882a593Smuzhiyun 	.open =		snd_als4000_playback_open,
649*4882a593Smuzhiyun 	.close =	snd_als4000_playback_close,
650*4882a593Smuzhiyun 	.prepare =	snd_als4000_playback_prepare,
651*4882a593Smuzhiyun 	.trigger =	snd_als4000_playback_trigger,
652*4882a593Smuzhiyun 	.pointer =	snd_als4000_playback_pointer
653*4882a593Smuzhiyun };
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun static const struct snd_pcm_ops snd_als4000_capture_ops = {
656*4882a593Smuzhiyun 	.open =		snd_als4000_capture_open,
657*4882a593Smuzhiyun 	.close =	snd_als4000_capture_close,
658*4882a593Smuzhiyun 	.prepare =	snd_als4000_capture_prepare,
659*4882a593Smuzhiyun 	.trigger =	snd_als4000_capture_trigger,
660*4882a593Smuzhiyun 	.pointer =	snd_als4000_capture_pointer
661*4882a593Smuzhiyun };
662*4882a593Smuzhiyun 
snd_als4000_pcm(struct snd_sb * chip,int device)663*4882a593Smuzhiyun static int snd_als4000_pcm(struct snd_sb *chip, int device)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	struct snd_pcm *pcm;
666*4882a593Smuzhiyun 	int err;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm);
669*4882a593Smuzhiyun 	if (err < 0)
670*4882a593Smuzhiyun 		return err;
671*4882a593Smuzhiyun 	pcm->private_data = chip;
672*4882a593Smuzhiyun 	pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
673*4882a593Smuzhiyun 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
674*4882a593Smuzhiyun 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
677*4882a593Smuzhiyun 				       &chip->pci->dev, 64*1024, 64*1024);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	chip->pcm = pcm;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	return 0;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun /******************************************************************/
685*4882a593Smuzhiyun 
snd_als4000_set_addr(unsigned long iobase,unsigned int sb_io,unsigned int mpu_io,unsigned int opl_io,unsigned int game_io)686*4882a593Smuzhiyun static void snd_als4000_set_addr(unsigned long iobase,
687*4882a593Smuzhiyun 					unsigned int sb_io,
688*4882a593Smuzhiyun 					unsigned int mpu_io,
689*4882a593Smuzhiyun 					unsigned int opl_io,
690*4882a593Smuzhiyun 					unsigned int game_io)
691*4882a593Smuzhiyun {
692*4882a593Smuzhiyun 	u32 cfg1 = 0;
693*4882a593Smuzhiyun 	u32 cfg2 = 0;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (mpu_io > 0)
696*4882a593Smuzhiyun 		cfg2 |= (mpu_io | 1) << 16;
697*4882a593Smuzhiyun 	if (sb_io > 0)
698*4882a593Smuzhiyun 		cfg2 |= (sb_io | 1);
699*4882a593Smuzhiyun 	if (game_io > 0)
700*4882a593Smuzhiyun 		cfg1 |= (game_io | 1) << 16;
701*4882a593Smuzhiyun 	if (opl_io > 0)
702*4882a593Smuzhiyun 		cfg1 |= (opl_io | 1);
703*4882a593Smuzhiyun 	snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA8_LEGACY_CFG1, cfg1);
704*4882a593Smuzhiyun 	snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA9_LEGACY_CFG2, cfg2);
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
snd_als4000_configure(struct snd_sb * chip)707*4882a593Smuzhiyun static void snd_als4000_configure(struct snd_sb *chip)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	u8 tmp;
710*4882a593Smuzhiyun 	int i;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	/* do some more configuration */
713*4882a593Smuzhiyun 	spin_lock_irq(&chip->mixer_lock);
714*4882a593Smuzhiyun 	tmp = snd_als4_cr_read(chip, ALS4K_CR0_SB_CONFIG);
715*4882a593Smuzhiyun 	snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
716*4882a593Smuzhiyun 				tmp|ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
717*4882a593Smuzhiyun 	/* always select DMA channel 0, since we do not actually use DMA
718*4882a593Smuzhiyun 	 * SPECS_PAGE: 19/20 */
719*4882a593Smuzhiyun 	snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
720*4882a593Smuzhiyun 	snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
721*4882a593Smuzhiyun 				 tmp & ~ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
722*4882a593Smuzhiyun 	spin_unlock_irq(&chip->mixer_lock);
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	spin_lock_irq(&chip->reg_lock);
725*4882a593Smuzhiyun 	/* enable interrupts */
726*4882a593Smuzhiyun 	snd_als4k_gcr_write(chip, ALS4K_GCR8C_MISC_CTRL,
727*4882a593Smuzhiyun 					ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE);
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	/* SPECS_PAGE: 39 */
730*4882a593Smuzhiyun 	for (i = ALS4K_GCR91_DMA0_ADDR; i <= ALS4K_GCR96_DMA3_MODE_COUNT; ++i)
731*4882a593Smuzhiyun 		snd_als4k_gcr_write(chip, i, 0);
732*4882a593Smuzhiyun 	/* enable burst mode to prevent dropouts during high PCI bus usage */
733*4882a593Smuzhiyun 	snd_als4k_gcr_write(chip, ALS4K_GCR99_DMA_EMULATION_CTRL,
734*4882a593Smuzhiyun 		(snd_als4k_gcr_read(chip, ALS4K_GCR99_DMA_EMULATION_CTRL) & ~0x07) | 0x04);
735*4882a593Smuzhiyun 	spin_unlock_irq(&chip->reg_lock);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun #ifdef SUPPORT_JOYSTICK
snd_als4000_create_gameport(struct snd_card_als4000 * acard,int dev)739*4882a593Smuzhiyun static int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun 	struct gameport *gp;
742*4882a593Smuzhiyun 	struct resource *r;
743*4882a593Smuzhiyun 	int io_port;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	if (joystick_port[dev] == 0)
746*4882a593Smuzhiyun 		return -ENODEV;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	if (joystick_port[dev] == 1) { /* auto-detect */
749*4882a593Smuzhiyun 		for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
750*4882a593Smuzhiyun 			r = request_region(io_port, 8, "ALS4000 gameport");
751*4882a593Smuzhiyun 			if (r)
752*4882a593Smuzhiyun 				break;
753*4882a593Smuzhiyun 		}
754*4882a593Smuzhiyun 	} else {
755*4882a593Smuzhiyun 		io_port = joystick_port[dev];
756*4882a593Smuzhiyun 		r = request_region(io_port, 8, "ALS4000 gameport");
757*4882a593Smuzhiyun 	}
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	if (!r) {
760*4882a593Smuzhiyun 		dev_warn(&acard->pci->dev, "cannot reserve joystick ports\n");
761*4882a593Smuzhiyun 		return -EBUSY;
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	acard->gameport = gp = gameport_allocate_port();
765*4882a593Smuzhiyun 	if (!gp) {
766*4882a593Smuzhiyun 		dev_err(&acard->pci->dev, "cannot allocate memory for gameport\n");
767*4882a593Smuzhiyun 		release_and_free_resource(r);
768*4882a593Smuzhiyun 		return -ENOMEM;
769*4882a593Smuzhiyun 	}
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	gameport_set_name(gp, "ALS4000 Gameport");
772*4882a593Smuzhiyun 	gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
773*4882a593Smuzhiyun 	gameport_set_dev_parent(gp, &acard->pci->dev);
774*4882a593Smuzhiyun 	gp->io = io_port;
775*4882a593Smuzhiyun 	gameport_set_port_data(gp, r);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* Enable legacy joystick port */
778*4882a593Smuzhiyun 	snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	gameport_register_port(acard->gameport);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	return 0;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
snd_als4000_free_gameport(struct snd_card_als4000 * acard)785*4882a593Smuzhiyun static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	if (acard->gameport) {
788*4882a593Smuzhiyun 		struct resource *r = gameport_get_port_data(acard->gameport);
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 		gameport_unregister_port(acard->gameport);
791*4882a593Smuzhiyun 		acard->gameport = NULL;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 		/* disable joystick */
794*4882a593Smuzhiyun 		snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 		release_and_free_resource(r);
797*4882a593Smuzhiyun 	}
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun #else
snd_als4000_create_gameport(struct snd_card_als4000 * acard,int dev)800*4882a593Smuzhiyun static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
snd_als4000_free_gameport(struct snd_card_als4000 * acard)801*4882a593Smuzhiyun static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
802*4882a593Smuzhiyun #endif
803*4882a593Smuzhiyun 
snd_card_als4000_free(struct snd_card * card)804*4882a593Smuzhiyun static void snd_card_als4000_free( struct snd_card *card )
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun 	struct snd_card_als4000 *acard = card->private_data;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	/* make sure that interrupts are disabled */
809*4882a593Smuzhiyun 	snd_als4k_gcr_write_addr(acard->iobase, ALS4K_GCR8C_MISC_CTRL, 0);
810*4882a593Smuzhiyun 	/* free resources */
811*4882a593Smuzhiyun 	snd_als4000_free_gameport(acard);
812*4882a593Smuzhiyun 	pci_release_regions(acard->pci);
813*4882a593Smuzhiyun 	pci_disable_device(acard->pci);
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun 
snd_card_als4000_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)816*4882a593Smuzhiyun static int snd_card_als4000_probe(struct pci_dev *pci,
817*4882a593Smuzhiyun 				  const struct pci_device_id *pci_id)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	static int dev;
820*4882a593Smuzhiyun 	struct snd_card *card;
821*4882a593Smuzhiyun 	struct snd_card_als4000 *acard;
822*4882a593Smuzhiyun 	unsigned long iobase;
823*4882a593Smuzhiyun 	struct snd_sb *chip;
824*4882a593Smuzhiyun 	struct snd_opl3 *opl3;
825*4882a593Smuzhiyun 	unsigned short word;
826*4882a593Smuzhiyun 	int err;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	if (dev >= SNDRV_CARDS)
829*4882a593Smuzhiyun 		return -ENODEV;
830*4882a593Smuzhiyun 	if (!enable[dev]) {
831*4882a593Smuzhiyun 		dev++;
832*4882a593Smuzhiyun 		return -ENOENT;
833*4882a593Smuzhiyun 	}
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* enable PCI device */
836*4882a593Smuzhiyun 	if ((err = pci_enable_device(pci)) < 0) {
837*4882a593Smuzhiyun 		return err;
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 	/* check, if we can restrict PCI DMA transfers to 24 bits */
840*4882a593Smuzhiyun 	if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
841*4882a593Smuzhiyun 	    dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
842*4882a593Smuzhiyun 		dev_err(&pci->dev, "architecture does not support 24bit PCI busmaster DMA\n");
843*4882a593Smuzhiyun 		pci_disable_device(pci);
844*4882a593Smuzhiyun 		return -ENXIO;
845*4882a593Smuzhiyun 	}
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
848*4882a593Smuzhiyun 		pci_disable_device(pci);
849*4882a593Smuzhiyun 		return err;
850*4882a593Smuzhiyun 	}
851*4882a593Smuzhiyun 	iobase = pci_resource_start(pci, 0);
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	pci_read_config_word(pci, PCI_COMMAND, &word);
854*4882a593Smuzhiyun 	pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
855*4882a593Smuzhiyun 	pci_set_master(pci);
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
858*4882a593Smuzhiyun 			   sizeof(*acard) /* private_data: acard */,
859*4882a593Smuzhiyun 			   &card);
860*4882a593Smuzhiyun 	if (err < 0) {
861*4882a593Smuzhiyun 		pci_release_regions(pci);
862*4882a593Smuzhiyun 		pci_disable_device(pci);
863*4882a593Smuzhiyun 		return err;
864*4882a593Smuzhiyun 	}
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	acard = card->private_data;
867*4882a593Smuzhiyun 	acard->pci = pci;
868*4882a593Smuzhiyun 	acard->iobase = iobase;
869*4882a593Smuzhiyun 	card->private_free = snd_card_als4000_free;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	/* disable all legacy ISA stuff */
872*4882a593Smuzhiyun 	snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	if ((err = snd_sbdsp_create(card,
875*4882a593Smuzhiyun 				    iobase + ALS4K_IOB_10_ADLIB_ADDR0,
876*4882a593Smuzhiyun 				    pci->irq,
877*4882a593Smuzhiyun 		/* internally registered as IRQF_SHARED in case of ALS4000 SB */
878*4882a593Smuzhiyun 				    snd_als4000_interrupt,
879*4882a593Smuzhiyun 				    -1,
880*4882a593Smuzhiyun 				    -1,
881*4882a593Smuzhiyun 				    SB_HW_ALS4000,
882*4882a593Smuzhiyun 				    &chip)) < 0) {
883*4882a593Smuzhiyun 		goto out_err;
884*4882a593Smuzhiyun 	}
885*4882a593Smuzhiyun 	acard->chip = chip;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	chip->pci = pci;
888*4882a593Smuzhiyun 	chip->alt_port = iobase;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	snd_als4000_configure(chip);
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	strcpy(card->driver, "ALS4000");
893*4882a593Smuzhiyun 	strcpy(card->shortname, "Avance Logic ALS4000");
894*4882a593Smuzhiyun 	sprintf(card->longname, "%s at 0x%lx, irq %i",
895*4882a593Smuzhiyun 		card->shortname, chip->alt_port, chip->irq);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
898*4882a593Smuzhiyun 					iobase + ALS4K_IOB_30_MIDI_DATA,
899*4882a593Smuzhiyun 					MPU401_INFO_INTEGRATED |
900*4882a593Smuzhiyun 					MPU401_INFO_IRQ_HOOK,
901*4882a593Smuzhiyun 					-1, &chip->rmidi)) < 0) {
902*4882a593Smuzhiyun 		dev_err(&pci->dev, "no MPU-401 device at 0x%lx?\n",
903*4882a593Smuzhiyun 				iobase + ALS4K_IOB_30_MIDI_DATA);
904*4882a593Smuzhiyun 		goto out_err;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 	/* FIXME: ALS4000 has interesting MPU401 configuration features
907*4882a593Smuzhiyun 	 * at ALS4K_CR1A_MPU401_UART_MODE_CONTROL
908*4882a593Smuzhiyun 	 * (pass-thru / UART switching, fast MIDI clock, etc.),
909*4882a593Smuzhiyun 	 * however there doesn't seem to be an ALSA API for this...
910*4882a593Smuzhiyun 	 * SPECS_PAGE: 21 */
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	if ((err = snd_als4000_pcm(chip, 0)) < 0) {
913*4882a593Smuzhiyun 		goto out_err;
914*4882a593Smuzhiyun 	}
915*4882a593Smuzhiyun 	if ((err = snd_sbmixer_new(chip)) < 0) {
916*4882a593Smuzhiyun 		goto out_err;
917*4882a593Smuzhiyun 	}
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	if (snd_opl3_create(card,
920*4882a593Smuzhiyun 				iobase + ALS4K_IOB_10_ADLIB_ADDR0,
921*4882a593Smuzhiyun 				iobase + ALS4K_IOB_12_ADLIB_ADDR2,
922*4882a593Smuzhiyun 			    OPL3_HW_AUTO, 1, &opl3) < 0) {
923*4882a593Smuzhiyun 		dev_err(&pci->dev, "no OPL device at 0x%lx-0x%lx?\n",
924*4882a593Smuzhiyun 			   iobase + ALS4K_IOB_10_ADLIB_ADDR0,
925*4882a593Smuzhiyun 			   iobase + ALS4K_IOB_12_ADLIB_ADDR2);
926*4882a593Smuzhiyun 	} else {
927*4882a593Smuzhiyun 		if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
928*4882a593Smuzhiyun 			goto out_err;
929*4882a593Smuzhiyun 		}
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	snd_als4000_create_gameport(acard, dev);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	if ((err = snd_card_register(card)) < 0) {
935*4882a593Smuzhiyun 		goto out_err;
936*4882a593Smuzhiyun 	}
937*4882a593Smuzhiyun 	pci_set_drvdata(pci, card);
938*4882a593Smuzhiyun 	dev++;
939*4882a593Smuzhiyun 	err = 0;
940*4882a593Smuzhiyun 	goto out;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun out_err:
943*4882a593Smuzhiyun 	snd_card_free(card);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun out:
946*4882a593Smuzhiyun 	return err;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
snd_card_als4000_remove(struct pci_dev * pci)949*4882a593Smuzhiyun static void snd_card_als4000_remove(struct pci_dev *pci)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun 	snd_card_free(pci_get_drvdata(pci));
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
snd_als4000_suspend(struct device * dev)955*4882a593Smuzhiyun static int snd_als4000_suspend(struct device *dev)
956*4882a593Smuzhiyun {
957*4882a593Smuzhiyun 	struct snd_card *card = dev_get_drvdata(dev);
958*4882a593Smuzhiyun 	struct snd_card_als4000 *acard = card->private_data;
959*4882a593Smuzhiyun 	struct snd_sb *chip = acard->chip;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	snd_sbmixer_suspend(chip);
964*4882a593Smuzhiyun 	return 0;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun 
snd_als4000_resume(struct device * dev)967*4882a593Smuzhiyun static int snd_als4000_resume(struct device *dev)
968*4882a593Smuzhiyun {
969*4882a593Smuzhiyun 	struct snd_card *card = dev_get_drvdata(dev);
970*4882a593Smuzhiyun 	struct snd_card_als4000 *acard = card->private_data;
971*4882a593Smuzhiyun 	struct snd_sb *chip = acard->chip;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	snd_als4000_configure(chip);
974*4882a593Smuzhiyun 	snd_sbdsp_reset(chip);
975*4882a593Smuzhiyun 	snd_sbmixer_resume(chip);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun #ifdef SUPPORT_JOYSTICK
978*4882a593Smuzhiyun 	if (acard->gameport)
979*4882a593Smuzhiyun 		snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
980*4882a593Smuzhiyun #endif
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
983*4882a593Smuzhiyun 	return 0;
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(snd_als4000_pm, snd_als4000_suspend, snd_als4000_resume);
987*4882a593Smuzhiyun #define SND_ALS4000_PM_OPS	&snd_als4000_pm
988*4882a593Smuzhiyun #else
989*4882a593Smuzhiyun #define SND_ALS4000_PM_OPS	NULL
990*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun static struct pci_driver als4000_driver = {
993*4882a593Smuzhiyun 	.name = KBUILD_MODNAME,
994*4882a593Smuzhiyun 	.id_table = snd_als4000_ids,
995*4882a593Smuzhiyun 	.probe = snd_card_als4000_probe,
996*4882a593Smuzhiyun 	.remove = snd_card_als4000_remove,
997*4882a593Smuzhiyun 	.driver = {
998*4882a593Smuzhiyun 		.pm = SND_ALS4000_PM_OPS,
999*4882a593Smuzhiyun 	},
1000*4882a593Smuzhiyun };
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun module_pci_driver(als4000_driver);
1003