xref: /linux/sound/pci/riptide/riptide.c (revision bc95f3669f5e6f63cf0b84fe4922c3c6dd4aa775)
1 /*
2  *   Driver for the Conexant Riptide Soundchip
3  *
4  *	Copyright (c) 2004 Peter Gruber <nokos@gmx.net>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21 /*
22   History:
23    - 02/15/2004 first release
24 
25   This Driver is based on the OSS Driver version from Linuxant (riptide-0.6lnxtbeta03111100)
26   credits from the original files:
27 
28   MODULE NAME:        cnxt_rt.h
29   AUTHOR:             K. Lazarev  (Transcribed by KNL)
30   HISTORY:         Major Revision               Date        By
31             -----------------------------     --------     -----
32             Created                           02/1/2000     KNL
33 
34   MODULE NAME:     int_mdl.c
35   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
36   HISTORY:         Major Revision               Date        By
37             -----------------------------     --------     -----
38             Created                           10/01/99      KNL
39 
40   MODULE NAME:        riptide.h
41   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
42   HISTORY:         Major Revision               Date        By
43             -----------------------------     --------     -----
44             Created                           10/16/97      OLD
45 
46   MODULE NAME:        Rp_Cmdif.cpp
47   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
48                       K. Lazarev    (Transcribed by KNL)
49   HISTORY:         Major Revision               Date        By
50             -----------------------------     --------     -----
51             Adopted from NT4 driver            6/22/99      OLD
52             Ported to Linux                    9/01/99      KNL
53 
54   MODULE NAME:        rt_hw.c
55   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
56                       C. Lazarev    (Transcribed by CNL)
57   HISTORY:         Major Revision               Date        By
58             -----------------------------     --------     -----
59             Created                           11/18/97      OLD
60             Hardware functions for RipTide    11/24/97      CNL
61             (ES1) are coded
62             Hardware functions for RipTide    12/24/97      CNL
63             (A0) are coded
64             Hardware functions for RipTide    03/20/98      CNL
65             (A1) are coded
66             Boot loader is included           05/07/98      CNL
67             Redesigned for WDM                07/27/98      CNL
68             Redesigned for Linux              09/01/99      CNL
69 
70   MODULE NAME:        rt_hw.h
71   AUTHOR:             C. Lazarev    (Transcribed by CNL)
72   HISTORY:         Major Revision               Date        By
73             -----------------------------     --------     -----
74             Created                           11/18/97      CNL
75 
76   MODULE NAME:     rt_mdl.c
77   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
78   HISTORY:         Major Revision               Date        By
79             -----------------------------     --------     -----
80             Created                           10/01/99      KNL
81 
82   MODULE NAME:        mixer.h
83   AUTHOR:             K. Kenney
84   HISTORY:         Major Revision                   Date          By
85             -----------------------------          --------     -----
86             Created from MS W95 Sample             11/28/95      KRS
87             RipTide                                10/15/97      KRS
88             Adopted for Windows NT driver          01/20/98      CNL
89 */
90 
91 #include <sound/driver.h>
92 #include <linux/delay.h>
93 #include <linux/init.h>
94 #include <linux/interrupt.h>
95 #include <linux/pci.h>
96 #include <linux/slab.h>
97 #include <linux/wait.h>
98 #include <linux/gameport.h>
99 #include <linux/device.h>
100 #include <linux/firmware.h>
101 #include <asm/io.h>
102 #include <sound/core.h>
103 #include <sound/info.h>
104 #include <sound/control.h>
105 #include <sound/pcm.h>
106 #include <sound/pcm_params.h>
107 #include <sound/ac97_codec.h>
108 #include <sound/mpu401.h>
109 #include <sound/opl3.h>
110 #include <sound/initval.h>
111 
112 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
113 #define SUPPORT_JOYSTICK 1
114 #endif
115 
116 MODULE_AUTHOR("Peter Gruber <nokos@gmx.net>");
117 MODULE_DESCRIPTION("riptide");
118 MODULE_LICENSE("GPL");
119 MODULE_SUPPORTED_DEVICE("{{Conexant,Riptide}}");
120 
121 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
122 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
123 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
124 
125 #ifdef SUPPORT_JOYSTICK
126 static int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
127 #endif
128 static int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
129 static int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
130 
131 module_param_array(index, int, NULL, 0444);
132 MODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
133 module_param_array(id, charp, NULL, 0444);
134 MODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
135 module_param_array(enable, bool, NULL, 0444);
136 MODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
137 #ifdef SUPPORT_JOYSTICK
138 module_param_array(joystick_port, int, NULL, 0444);
139 MODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
140 #endif
141 module_param_array(mpu_port, int, NULL, 0444);
142 MODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
143 module_param_array(opl3_port, int, NULL, 0444);
144 MODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
145 
146 /*
147  */
148 
149 #define MPU401_HW_RIPTIDE MPU401_HW_MPU401
150 #define OPL3_HW_RIPTIDE   OPL3_HW_OPL3
151 
152 #define PCI_EXT_CapId       0x40
153 #define PCI_EXT_NextCapPrt  0x41
154 #define PCI_EXT_PWMC        0x42
155 #define PCI_EXT_PWSCR       0x44
156 #define PCI_EXT_Data00      0x46
157 #define PCI_EXT_PMSCR_BSE   0x47
158 #define PCI_EXT_SB_Base     0x48
159 #define PCI_EXT_FM_Base     0x4a
160 #define PCI_EXT_MPU_Base    0x4C
161 #define PCI_EXT_Game_Base   0x4E
162 #define PCI_EXT_Legacy_Mask 0x50
163 #define PCI_EXT_AsicRev     0x52
164 #define PCI_EXT_Reserved3   0x53
165 
166 #define LEGACY_ENABLE_ALL      0x8000	/* legacy device options */
167 #define LEGACY_ENABLE_SB       0x4000
168 #define LEGACY_ENABLE_FM       0x2000
169 #define LEGACY_ENABLE_MPU_INT  0x1000
170 #define LEGACY_ENABLE_MPU      0x0800
171 #define LEGACY_ENABLE_GAMEPORT 0x0400
172 
173 #define MAX_WRITE_RETRY  10	/* cmd interface limits */
174 #define MAX_ERROR_COUNT  10
175 #define CMDIF_TIMEOUT    500000
176 #define RESET_TRIES      5
177 
178 #define READ_PORT_ULONG(p)     inl((unsigned long)&(p))
179 #define WRITE_PORT_ULONG(p,x)  outl(x,(unsigned long)&(p))
180 
181 #define READ_AUDIO_CONTROL(p)     READ_PORT_ULONG(p->audio_control)
182 #define WRITE_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,x)
183 #define UMASK_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
184 #define MASK_AUDIO_CONTROL(p,x)   WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
185 #define READ_AUDIO_STATUS(p)      READ_PORT_ULONG(p->audio_status)
186 
187 #define SET_GRESET(p)     UMASK_AUDIO_CONTROL(p,0x0001)	/* global reset switch */
188 #define UNSET_GRESET(p)   MASK_AUDIO_CONTROL(p,~0x0001)
189 #define SET_AIE(p)        UMASK_AUDIO_CONTROL(p,0x0004)	/* interrupt enable */
190 #define UNSET_AIE(p)      MASK_AUDIO_CONTROL(p,~0x0004)
191 #define SET_AIACK(p)      UMASK_AUDIO_CONTROL(p,0x0008)	/* interrupt acknowledge */
192 #define UNSET_AIACKT(p)   MASKAUDIO_CONTROL(p,~0x0008)
193 #define SET_ECMDAE(p)     UMASK_AUDIO_CONTROL(p,0x0010)
194 #define UNSET_ECMDAE(p)   MASK_AUDIO_CONTROL(p,~0x0010)
195 #define SET_ECMDBE(p)     UMASK_AUDIO_CONTROL(p,0x0020)
196 #define UNSET_ECMDBE(p)   MASK_AUDIO_CONTROL(p,~0x0020)
197 #define SET_EDATAF(p)     UMASK_AUDIO_CONTROL(p,0x0040)
198 #define UNSET_EDATAF(p)   MASK_AUDIO_CONTROL(p,~0x0040)
199 #define SET_EDATBF(p)     UMASK_AUDIO_CONTROL(p,0x0080)
200 #define UNSET_EDATBF(p)   MASK_AUDIO_CONTROL(p,~0x0080)
201 #define SET_ESBIRQON(p)   UMASK_AUDIO_CONTROL(p,0x0100)
202 #define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
203 #define SET_EMPUIRQ(p)    UMASK_AUDIO_CONTROL(p,0x0200)
204 #define UNSET_EMPUIRQ(p)  MASK_AUDIO_CONTROL(p,~0x0200)
205 #define IS_CMDE(a)        (READ_PORT_ULONG(a->stat)&0x1)	/* cmd empty */
206 #define IS_DATF(a)        (READ_PORT_ULONG(a->stat)&0x2)	/* data filled */
207 #define IS_READY(p)       (READ_AUDIO_STATUS(p)&0x0001)
208 #define IS_DLREADY(p)     (READ_AUDIO_STATUS(p)&0x0002)
209 #define IS_DLERR(p)       (READ_AUDIO_STATUS(p)&0x0004)
210 #define IS_GERR(p)        (READ_AUDIO_STATUS(p)&0x0008)	/* error ! */
211 #define IS_CMDAEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0010)
212 #define IS_CMDBEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0020)
213 #define IS_DATAFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0040)
214 #define IS_DATBFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0080)
215 #define IS_EOBIRQ(p)      (READ_AUDIO_STATUS(p)&0x0100)	/* interrupt status */
216 #define IS_EOSIRQ(p)      (READ_AUDIO_STATUS(p)&0x0200)
217 #define IS_EOCIRQ(p)      (READ_AUDIO_STATUS(p)&0x0400)
218 #define IS_UNSLIRQ(p)     (READ_AUDIO_STATUS(p)&0x0800)
219 #define IS_SBIRQ(p)       (READ_AUDIO_STATUS(p)&0x1000)
220 #define IS_MPUIRQ(p)      (READ_AUDIO_STATUS(p)&0x2000)
221 
222 #define RESP 0x00000001		/* command flags */
223 #define PARM 0x00000002
224 #define CMDA 0x00000004
225 #define CMDB 0x00000008
226 #define NILL 0x00000000
227 
228 #define LONG0(a)   ((u32)a)	/* shifts and masks */
229 #define BYTE0(a)   (LONG0(a)&0xff)
230 #define BYTE1(a)   (BYTE0(a)<<8)
231 #define BYTE2(a)   (BYTE0(a)<<16)
232 #define BYTE3(a)   (BYTE0(a)<<24)
233 #define WORD0(a)   (LONG0(a)&0xffff)
234 #define WORD1(a)   (WORD0(a)<<8)
235 #define WORD2(a)   (WORD0(a)<<16)
236 #define TRINIB0(a) (LONG0(a)&0xffffff)
237 #define TRINIB1(a) (TRINIB0(a)<<8)
238 
239 #define RET(a)     ((union cmdret *)(a))
240 
241 #define SEND_GETV(p,b)             sendcmd(p,RESP,GETV,0,RET(b))	/* get version */
242 #define SEND_GETC(p,b,c)           sendcmd(p,PARM|RESP,GETC,c,RET(b))
243 #define SEND_GUNS(p,b)             sendcmd(p,RESP,GUNS,0,RET(b))
244 #define SEND_SCID(p,b)             sendcmd(p,RESP,SCID,0,RET(b))
245 #define SEND_RMEM(p,b,c,d)         sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))	/* memory access for firmware write */
246 #define SEND_SMEM(p,b,c)           sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
247 #define SEND_WMEM(p,b,c)           sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
248 #define SEND_SDTM(p,b,c)           sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))	/* memory access for firmware write */
249 #define SEND_GOTO(p,b)             sendcmd(p,PARM,GOTO,LONG0(b),RET(0))	/* memory access for firmware write */
250 #define SEND_SETDPLL(p)	           sendcmd(p,0,ARM_SETDPLL,0,RET(0))
251 #define SEND_SSTR(p,b,c)           sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))	/* start stream */
252 #define SEND_PSTR(p,b)             sendcmd(p,PARM,PSTR,BYTE3(b),RET(0))	/* pause stream */
253 #define SEND_KSTR(p,b)             sendcmd(p,PARM,KSTR,BYTE3(b),RET(0))	/* stop stream */
254 #define SEND_KDMA(p)               sendcmd(p,0,KDMA,0,RET(0))	/* stop all dma */
255 #define SEND_GPOS(p,b,c,d)         sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))	/* get position in dma */
256 #define SEND_SETF(p,b,c,d,e,f,g)   sendcmd(p,PARM,SETF|WORD1(b)|BYTE3(c),d|BYTE1(e)|BYTE2(f)|BYTE3(g),RET(0))	/* set sample format at mixer */
257 #define SEND_GSTS(p,b,c,d)         sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
258 #define SEND_NGPOS(p,b,c,d)        sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
259 #define SEND_PSEL(p,b,c)           sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))	/* activate lbus path */
260 #define SEND_PCLR(p,b,c)           sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))	/* deactivate lbus path */
261 #define SEND_PLST(p,b)             sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
262 #define SEND_RSSV(p,b,c,d)         sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
263 #define SEND_LSEL(p,b,c,d,e,f,g,h) sendcmd(p,PARM,LSEL|BYTE1(b)|BYTE2(c)|BYTE3(d),BYTE0(e)|BYTE1(f)|BYTE2(g)|BYTE3(h),RET(0))	/* select paths for internal connections */
264 #define SEND_SSRC(p,b,c,d,e)       sendcmd(p,PARM,SSRC|BYTE1(b)|WORD2(c),WORD0(d)|WORD2(e),RET(0))	/* configure source */
265 #define SEND_SLST(p,b)             sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
266 #define SEND_RSRC(p,b,c)           sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))	/* read source config */
267 #define SEND_SSRB(p,b,c)           sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
268 #define SEND_SDGV(p,b,c,d,e)       sendcmd(p,PARM,SDGV|BYTE2(b)|BYTE3(c),WORD0(d)|WORD2(e),RET(0))	/* set digital mixer */
269 #define SEND_RDGV(p,b,c,d)         sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d))	/* read digital mixer */
270 #define SEND_DLST(p,b)             sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
271 #define SEND_SACR(p,b,c)           sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))	/* set AC97 register */
272 #define SEND_RACR(p,b,c)           sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))	/* get AC97 register */
273 #define SEND_ALST(p,b)             sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
274 #define SEND_TXAC(p,b,c,d,e,f)     sendcmd(p,PARM,TXAC|BYTE1(b)|WORD2(c),WORD0(d)|BYTE2(e)|BYTE3(f),RET(0))
275 #define SEND_RXAC(p,b,c,d)         sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
276 #define SEND_SI2S(p,b)             sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
277 
278 #define EOB_STATUS         0x80000000	/* status flags : block boundary */
279 #define EOS_STATUS         0x40000000	/*              : stoppped */
280 #define EOC_STATUS         0x20000000	/*              : stream end */
281 #define ERR_STATUS         0x10000000
282 #define EMPTY_STATUS       0x08000000
283 
284 #define IEOB_ENABLE        0x1	/* enable interrupts for status notification above */
285 #define IEOS_ENABLE        0x2
286 #define IEOC_ENABLE        0x4
287 #define RDONCE             0x8
288 #define DESC_MAX_MASK      0xff
289 
290 #define ST_PLAY  0x1		/* stream states */
291 #define ST_STOP  0x2
292 #define ST_PAUSE 0x4
293 
294 #define I2S_INTDEC     3	/* config for I2S link */
295 #define I2S_MERGER     0
296 #define I2S_SPLITTER   0
297 #define I2S_MIXER      7
298 #define I2S_RATE       44100
299 
300 #define MODEM_INTDEC   4	/* config for modem link */
301 #define MODEM_MERGER   3
302 #define MODEM_SPLITTER 0
303 #define MODEM_MIXER    11
304 
305 #define FM_INTDEC      3	/* config for FM/OPL3 link */
306 #define FM_MERGER      0
307 #define FM_SPLITTER    0
308 #define FM_MIXER       9
309 
310 #define SPLIT_PATH  0x80	/* path splitting flag */
311 
312 enum FIRMWARE {
313 	DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
314 	EXT_LIN_ADDR_REC,
315 };
316 
317 enum CMDS {
318 	GETV = 0x00, GETC, GUNS, SCID, RMEM =
319 	    0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
320 	    0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
321 	    0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
322 	    0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
323 	    0x70, ARM_SETDPLL = 0x72,
324 };
325 
326 enum E1SOURCE {
327 	ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
328 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
329 	ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
330 	ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
331 	INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
332 	INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
333 	INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
334 	DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
335 	ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
336 	DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
337 	GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
338 	SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
339 	SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
340 	MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
341 };
342 
343 enum E2SINK {
344 	LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
345 	LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
346 	INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
347 	INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
348 	DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
349 	PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
350 	DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
351 	DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
352 	DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
353 	DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
354 	GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
355 	SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
356 	MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
357 	MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
358 };
359 
360 enum LBUS_SINK {
361 	LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
362 	LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
363 	LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
364 };
365 
366 enum RT_CHANNEL_IDS {
367 	M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
368 	DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
369 	M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
370 	CHANNEL_ID_COUNTER
371 };
372 
373 enum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
374 
375 struct lbuspath {
376 	unsigned char *noconv;
377 	unsigned char *stereo;
378 	unsigned char *mono;
379 };
380 
381 struct cmdport {
382 	u32 data1;		/* cmd,param */
383 	u32 data2;		/* param */
384 	u32 stat;		/* status */
385 	u32 pad[5];
386 };
387 
388 struct riptideport {
389 	u32 audio_control;	/* status registers */
390 	u32 audio_status;
391 	u32 pad[2];
392 	struct cmdport port[2];	/* command ports */
393 };
394 
395 struct cmdif {
396 	struct riptideport *hwport;
397 	spinlock_t lock;
398 	unsigned int cmdcnt;	/* cmd statistics */
399 	unsigned int cmdtime;
400 	unsigned int cmdtimemax;
401 	unsigned int cmdtimemin;
402 	unsigned int errcnt;
403 	int is_reset;
404 };
405 
406 struct riptide_firmware {
407 	u16 ASIC;
408 	u16 CODEC;
409 	u16 AUXDSP;
410 	u16 PROG;
411 };
412 
413 union cmdret {
414 	u8 retbytes[8];
415 	u16 retwords[4];
416 	u32 retlongs[2];
417 };
418 
419 union firmware_version {
420 	union cmdret ret;
421 	struct riptide_firmware firmware;
422 };
423 
424 #define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
425 
426 #define PLAYBACK_SUBSTREAMS 3
427 struct snd_riptide {
428 	struct snd_card *card;
429 	struct pci_dev *pci;
430 	const struct firmware *fw_entry;
431 
432 	struct cmdif *cif;
433 
434 	struct snd_pcm *pcm;
435 	struct snd_pcm *pcm_i2s;
436 	struct snd_rawmidi *rmidi;
437 	struct snd_opl3 *opl3;
438 	struct snd_ac97 *ac97;
439 	struct snd_ac97_bus *ac97_bus;
440 
441 	struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
442 	struct snd_pcm_substream *capture_substream;
443 
444 	int openstreams;
445 
446 	int irq;
447 	unsigned long port;
448 	unsigned short mpuaddr;
449 	unsigned short opladdr;
450 #ifdef SUPPORT_JOYSTICK
451 	unsigned short gameaddr;
452 #endif
453 	struct resource *res_port;
454 
455 	unsigned short device_id;
456 
457 	union firmware_version firmware;
458 
459 	spinlock_t lock;
460 	struct tasklet_struct riptide_tq;
461 	struct snd_info_entry *proc_entry;
462 
463 	unsigned long received_irqs;
464 	unsigned long handled_irqs;
465 #ifdef CONFIG_PM
466 	int in_suspend;
467 #endif
468 };
469 
470 struct sgd {			/* scatter gather desriptor */
471 	u32 dwNextLink;
472 	u32 dwSegPtrPhys;
473 	u32 dwSegLen;
474 	u32 dwStat_Ctl;
475 };
476 
477 struct pcmhw {			/* pcm descriptor */
478 	struct lbuspath paths;
479 	unsigned char *lbuspath;
480 	unsigned char source;
481 	unsigned char intdec[2];
482 	unsigned char mixer;
483 	unsigned char id;
484 	unsigned char state;
485 	unsigned int rate;
486 	unsigned int channels;
487 	snd_pcm_format_t format;
488 	struct snd_dma_buffer sgdlist;
489 	struct sgd *sgdbuf;
490 	unsigned int size;
491 	unsigned int pages;
492 	unsigned int oldpos;
493 	unsigned int pointer;
494 };
495 
496 #define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
497 
498 static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
499 		   union cmdret *ret);
500 static int getsourcesink(struct cmdif *cif, unsigned char source,
501 			 unsigned char sink, unsigned char *a,
502 			 unsigned char *b);
503 static int snd_riptide_initialize(struct snd_riptide *chip);
504 static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
505 
506 /*
507  */
508 
509 static struct pci_device_id snd_riptide_ids[] = {
510 	{
511 	 .vendor = 0x127a,.device = 0x4310,
512 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
513 	 },
514 	{
515 	 .vendor = 0x127a,.device = 0x4320,
516 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
517 	 },
518 	{
519 	 .vendor = 0x127a,.device = 0x4330,
520 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
521 	 },
522 	{
523 	 .vendor = 0x127a,.device = 0x4340,
524 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
525 	 },
526 	{0,},
527 };
528 
529 #ifdef SUPPORT_JOYSTICK
530 static struct pci_device_id snd_riptide_joystick_ids[] __devinitdata = {
531 	{
532 	 .vendor = 0x127a,.device = 0x4312,
533 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
534 	 },
535 	{
536 	 .vendor = 0x127a,.device = 0x4322,
537 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
538 	 },
539 	{.vendor = 0x127a,.device = 0x4332,
540 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
541 	 },
542 	{.vendor = 0x127a,.device = 0x4342,
543 	 .subvendor = PCI_ANY_ID,.subdevice = PCI_ANY_ID,
544 	 },
545 	{0,},
546 };
547 #endif
548 
549 MODULE_DEVICE_TABLE(pci, snd_riptide_ids);
550 
551 /*
552  */
553 
554 static unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
555 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
556 								     LS_NONE2},
557 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
558 								     LS_NONE2},
559 	{INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
560 	{INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
561 	{INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
562 	{INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
563 					      LS_SRC_INTERPOLATORM},
564 	{INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
565 					      LS_SRC_INTERPOLATORM},
566 	{INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
567 					      LS_SRC_INTERPOLATORM},
568 	{DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
569 	{DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
570 	{DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
571 								LS_NONE2},
572 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
573 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
574 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
575 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
576 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
577 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
578 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
579 	{GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
580 						  LS_MIXER_GAIN_FUNCTION},
581 	{GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
582 						  LS_MIXER_GAIN_FUNCTION},
583 	{GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
584 	{SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
585 	{SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
586 	{MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
587 	{MERGER1_OUT, LS_SRC_MERGER},
588 	{MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
589 	{MERGER2_OUT, LS_SRC_MERGER},
590 	{MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
591 								     LS_NONE2},
592 };
593 
594 static unsigned char lbus_play_opl3[] = {
595 	DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
596 };
597 static unsigned char lbus_play_modem[] = {
598 	DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
599 };
600 static unsigned char lbus_play_i2s[] = {
601 	INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
602 };
603 static unsigned char lbus_play_out[] = {
604 	PDAC2ACLNK, 0xff
605 };
606 static unsigned char lbus_play_outhp[] = {
607 	HNDSPK2ACLNK, 0xff
608 };
609 static unsigned char lbus_play_noconv1[] = {
610 	DIGITAL_MIXER_IN0, 0xff
611 };
612 static unsigned char lbus_play_stereo1[] = {
613 	INTER0_IN, DIGITAL_MIXER_IN0, 0xff
614 };
615 static unsigned char lbus_play_mono1[] = {
616 	INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
617 };
618 static unsigned char lbus_play_noconv2[] = {
619 	DIGITAL_MIXER_IN1, 0xff
620 };
621 static unsigned char lbus_play_stereo2[] = {
622 	INTER1_IN, DIGITAL_MIXER_IN1, 0xff
623 };
624 static unsigned char lbus_play_mono2[] = {
625 	INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
626 };
627 static unsigned char lbus_play_noconv3[] = {
628 	DIGITAL_MIXER_IN2, 0xff
629 };
630 static unsigned char lbus_play_stereo3[] = {
631 	INTER2_IN, DIGITAL_MIXER_IN2, 0xff
632 };
633 static unsigned char lbus_play_mono3[] = {
634 	INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
635 };
636 static unsigned char lbus_rec_noconv1[] = {
637 	LBUS2ARM_FIFO5, 0xff
638 };
639 static unsigned char lbus_rec_stereo1[] = {
640 	DECIM0_IN, LBUS2ARM_FIFO5, 0xff
641 };
642 static unsigned char lbus_rec_mono1[] = {
643 	DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
644 };
645 
646 static unsigned char play_ids[] = { 4, 1, 2, };
647 static unsigned char play_sources[] = {
648 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
649 };
650 static struct lbuspath lbus_play_paths[] = {
651 	{
652 	 .noconv = lbus_play_noconv1,
653 	 .stereo = lbus_play_stereo1,
654 	 .mono = lbus_play_mono1,
655 	 },
656 	{
657 	 .noconv = lbus_play_noconv2,
658 	 .stereo = lbus_play_stereo2,
659 	 .mono = lbus_play_mono2,
660 	 },
661 	{
662 	 .noconv = lbus_play_noconv3,
663 	 .stereo = lbus_play_stereo3,
664 	 .mono = lbus_play_mono3,
665 	 },
666 };
667 static struct lbuspath lbus_rec_path = {
668 	.noconv = lbus_rec_noconv1,
669 	.stereo = lbus_rec_stereo1,
670 	.mono = lbus_rec_mono1,
671 };
672 
673 #define FIRMWARE_VERSIONS 1
674 static union firmware_version firmware_versions[] = {
675 	{
676 		.firmware = {
677 			.ASIC = 3,
678 			.CODEC = 2,
679 			.AUXDSP = 3,
680 			.PROG = 773,
681 		},
682 	},
683 };
684 
685 static u32 atoh(unsigned char *in, unsigned int len)
686 {
687 	u32 sum = 0;
688 	unsigned int mult = 1;
689 	unsigned char c;
690 
691 	while (len) {
692 		c = in[len - 1];
693 		if ((c >= '0') && (c <= '9'))
694 			sum += mult * (c - '0');
695 		else if ((c >= 'A') && (c <= 'F'))
696 			sum += mult * (c - ('A' - 10));
697 		else if ((c >= 'a') && (c <= 'f'))
698 			sum += mult * (c - ('a' - 10));
699 		mult *= 16;
700 		--len;
701 	}
702 	return sum;
703 }
704 
705 static int senddata(struct cmdif *cif, unsigned char *in, u32 offset)
706 {
707 	u32 addr;
708 	u32 data;
709 	u32 i;
710 	unsigned char *p;
711 
712 	i = atoh(&in[1], 2);
713 	addr = offset + atoh(&in[3], 4);
714 	if (SEND_SMEM(cif, 0, addr) != 0)
715 		return -EACCES;
716 	p = in + 9;
717 	while (i) {
718 		data = atoh(p, 8);
719 		if (SEND_WMEM(cif, 2,
720 			      ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
721 							    >> 4)))
722 			return -EACCES;
723 		i -= 4;
724 		p += 8;
725 	}
726 	return 0;
727 }
728 
729 static int loadfirmware(struct cmdif *cif, unsigned char *img,
730 			unsigned int size)
731 {
732 	unsigned char *in;
733 	u32 laddr, saddr, t, val;
734 	int err = 0;
735 
736 	laddr = saddr = 0;
737 	while (size > 0 && err == 0) {
738 		in = img;
739 		if (in[0] == ':') {
740 			t = atoh(&in[7], 2);
741 			switch (t) {
742 			case DATA_REC:
743 				err = senddata(cif, in, laddr + saddr);
744 				break;
745 			case EXT_SEG_ADDR_REC:
746 				saddr = atoh(&in[9], 4) << 4;
747 				break;
748 			case EXT_LIN_ADDR_REC:
749 				laddr = atoh(&in[9], 4) << 16;
750 				break;
751 			case EXT_GOTO_CMD_REC:
752 				val = atoh(&in[9], 8);
753 				if (SEND_GOTO(cif, val) != 0)
754 					err = -EACCES;
755 				break;
756 			case EXT_END_OF_FILE:
757 				size = 0;
758 				break;
759 			default:
760 				break;
761 			}
762 			while (size > 0) {
763 				size--;
764 				if (*img++ == '\n')
765 					break;
766 			}
767 		}
768 	}
769 	snd_printdd("load firmware return %d\n", err);
770 	return err;
771 }
772 
773 static void
774 alloclbuspath(struct cmdif *cif, unsigned char source,
775 	      unsigned char *path, unsigned char *mixer, unsigned char *s)
776 {
777 	while (*path != 0xff) {
778 		unsigned char sink, type;
779 
780 		sink = *path & (~SPLIT_PATH);
781 		if (sink != E2SINK_MAX) {
782 			snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
783 			SEND_PSEL(cif, source, sink);
784 			source = lbusin2out[sink][0];
785 			type = lbusin2out[sink][1];
786 			if (type == LS_MIXER_IN) {
787 				if (mixer)
788 					*mixer = sink - DIGITAL_MIXER_IN0;
789 			}
790 			if (type == LS_SRC_DECIMATORM ||
791 			    type == LS_SRC_DECIMATOR ||
792 			    type == LS_SRC_INTERPOLATORM ||
793 			    type == LS_SRC_INTERPOLATOR) {
794 				if (s) {
795 					if (s[0] != 0xff)
796 						s[1] = sink;
797 					else
798 						s[0] = sink;
799 				}
800 			}
801 		}
802 		if (*path++ & SPLIT_PATH) {
803 			unsigned char *npath = path;
804 
805 			while (*npath != 0xff)
806 				npath++;
807 			alloclbuspath(cif, source + 1, ++npath, mixer, s);
808 		}
809 	}
810 }
811 
812 static void
813 freelbuspath(struct cmdif *cif, unsigned char source, unsigned char *path)
814 {
815 	while (*path != 0xff) {
816 		unsigned char sink;
817 
818 		sink = *path & (~SPLIT_PATH);
819 		if (sink != E2SINK_MAX) {
820 			snd_printdd("free path 0x%x->0x%x\n", source, sink);
821 			SEND_PCLR(cif, source, sink);
822 			source = lbusin2out[sink][0];
823 		}
824 		if (*path++ & SPLIT_PATH) {
825 			unsigned char *npath = path;
826 
827 			while (*npath != 0xff)
828 				npath++;
829 			freelbuspath(cif, source + 1, ++npath);
830 		}
831 	}
832 }
833 
834 static int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
835 {
836 	union cmdret rptr = CMDRET_ZERO;
837 	unsigned int i = MAX_WRITE_RETRY;
838 	int flag = 1;
839 
840 	SEND_RMEM(cif, 0x02, addr, &rptr);
841 	rptr.retlongs[0] &= (~mask);
842 
843 	while (--i) {
844 		SEND_SMEM(cif, 0x01, addr);
845 		SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
846 		SEND_RMEM(cif, 0x02, addr, &rptr);
847 		if ((rptr.retlongs[0] & data) == data) {
848 			flag = 0;
849 			break;
850 		} else
851 			rptr.retlongs[0] &= ~mask;
852 	}
853 	snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
854 		    flag);
855 	return flag;
856 }
857 
858 static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
859 		   union cmdret *ret)
860 {
861 	int i, j;
862 	int err;
863 	unsigned int time = 0;
864 	unsigned long irqflags;
865 	struct riptideport *hwport;
866 	struct cmdport *cmdport = NULL;
867 
868 	snd_assert(cif, return -EINVAL);
869 
870 	hwport = cif->hwport;
871 	if (cif->errcnt > MAX_ERROR_COUNT) {
872 		if (cif->is_reset) {
873 			snd_printk(KERN_ERR
874 				   "Riptide: Too many failed cmds, reinitializing\n");
875 			if (riptide_reset(cif, NULL) == 0) {
876 				cif->errcnt = 0;
877 				return -EIO;
878 			}
879 		}
880 		snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
881 		return -EINVAL;
882 	}
883 	if (ret) {
884 		ret->retlongs[0] = 0;
885 		ret->retlongs[1] = 0;
886 	}
887 	i = 0;
888 	spin_lock_irqsave(&cif->lock, irqflags);
889 	while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
890 		udelay(10);
891 	if (i >= CMDIF_TIMEOUT) {
892 		err = -EBUSY;
893 		goto errout;
894 	}
895 
896 	err = 0;
897 	for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
898 		cmdport = &(hwport->port[j % 2]);
899 		if (IS_DATF(cmdport)) {	/* free pending data */
900 			READ_PORT_ULONG(cmdport->data1);
901 			READ_PORT_ULONG(cmdport->data2);
902 		}
903 		if (IS_CMDE(cmdport)) {
904 			if (flags & PARM)	/* put data */
905 				WRITE_PORT_ULONG(cmdport->data2, parm);
906 			WRITE_PORT_ULONG(cmdport->data1, cmd);	/* write cmd */
907 			if ((flags & RESP) && ret) {
908 				while (!IS_DATF(cmdport) &&
909 				       time++ < CMDIF_TIMEOUT)
910 					udelay(10);
911 				if (time < CMDIF_TIMEOUT) {	/* read response */
912 					ret->retlongs[0] =
913 					    READ_PORT_ULONG(cmdport->data1);
914 					ret->retlongs[1] =
915 					    READ_PORT_ULONG(cmdport->data2);
916 				} else {
917 					err = -ENOSYS;
918 					goto errout;
919 				}
920 			}
921 			break;
922 		}
923 		udelay(20);
924 	}
925 	if (time == CMDIF_TIMEOUT) {
926 		err = -ENODATA;
927 		goto errout;
928 	}
929 	spin_unlock_irqrestore(&cif->lock, irqflags);
930 
931 	cif->cmdcnt++;		/* update command statistics */
932 	cif->cmdtime += time;
933 	if (time > cif->cmdtimemax)
934 		cif->cmdtimemax = time;
935 	if (time < cif->cmdtimemin)
936 		cif->cmdtimemin = time;
937 	if ((cif->cmdcnt) % 1000 == 0)
938 		snd_printdd
939 		    ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
940 		     cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
941 		     cif->cmdtimemax, cif->errcnt);
942 	return 0;
943 
944       errout:
945 	cif->errcnt++;
946 	spin_unlock_irqrestore(&cif->lock, irqflags);
947 	snd_printdd
948 	    ("send cmd %d hw: 0x%x flag: 0x%x cmd: 0x%x parm: 0x%x ret: 0x%x 0x%x CMDE: %d DATF: %d failed %d\n",
949 	     cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
950 	     flags, cmd, parm, ret ? ret->retlongs[0] : 0,
951 	     ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
952 	     err);
953 	return err;
954 }
955 
956 static int
957 setmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
958 {
959 	union cmdret rptr = CMDRET_ZERO;
960 	int i = 0;
961 
962 	snd_printdd("sent mixer %d: 0x%d 0x%d\n", num, rval, lval);
963 	do {
964 		SEND_SDGV(cif, num, num, rval, lval);
965 		SEND_RDGV(cif, num, num, &rptr);
966 		if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
967 			return 0;
968 	} while (i++ < MAX_WRITE_RETRY);
969 	snd_printdd("sent mixer failed\n");
970 	return -EIO;
971 }
972 
973 static int getpaths(struct cmdif *cif, unsigned char *o)
974 {
975 	unsigned char src[E2SINK_MAX];
976 	unsigned char sink[E2SINK_MAX];
977 	int i, j = 0;
978 
979 	for (i = 0; i < E2SINK_MAX; i++) {
980 		getsourcesink(cif, i, i, &src[i], &sink[i]);
981 		if (sink[i] < E2SINK_MAX) {
982 			o[j++] = sink[i];
983 			o[j++] = i;
984 		}
985 	}
986 	return j;
987 }
988 
989 static int
990 getsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
991 	      unsigned char *a, unsigned char *b)
992 {
993 	union cmdret rptr = CMDRET_ZERO;
994 
995 	if (SEND_RSSV(cif, source, sink, &rptr) &&
996 	    SEND_RSSV(cif, source, sink, &rptr))
997 		return -EIO;
998 	*a = rptr.retbytes[0];
999 	*b = rptr.retbytes[1];
1000 	snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
1001 	return 0;
1002 }
1003 
1004 static int
1005 getsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
1006 {
1007 	unsigned char *s;
1008 	unsigned int p[2] = { 0, 0 };
1009 	int i;
1010 	union cmdret rptr = CMDRET_ZERO;
1011 
1012 	s = intdec;
1013 	for (i = 0; i < 2; i++) {
1014 		if (*s != 0xff) {
1015 			if (SEND_RSRC(cif, *s, &rptr) &&
1016 			    SEND_RSRC(cif, *s, &rptr))
1017 				return -EIO;
1018 			p[i] += rptr.retwords[1];
1019 			p[i] *= rptr.retwords[2];
1020 			p[i] += rptr.retwords[3];
1021 			p[i] /= 65536;
1022 		}
1023 		s++;
1024 	}
1025 	if (p[0]) {
1026 		if (p[1] != p[0])
1027 			snd_printdd("rates differ %d %d\n", p[0], p[1]);
1028 		*rate = (unsigned int)p[0];
1029 	} else
1030 		*rate = (unsigned int)p[1];
1031 	snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
1032 	return 0;
1033 }
1034 
1035 static int
1036 setsampleformat(struct cmdif *cif,
1037 		unsigned char mixer, unsigned char id,
1038 		unsigned char channels, unsigned char format)
1039 {
1040 	unsigned char w, ch, sig, order;
1041 
1042 	snd_printdd
1043 	    ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
1044 	     mixer, id, channels, format);
1045 	ch = channels == 1;
1046 	w = snd_pcm_format_width(format) == 8;
1047 	sig = snd_pcm_format_unsigned(format) != 0;
1048 	order = snd_pcm_format_big_endian(format) != 0;
1049 
1050 	if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
1051 	    SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
1052 		snd_printdd("setsampleformat failed\n");
1053 		return -EIO;
1054 	}
1055 	return 0;
1056 }
1057 
1058 static int
1059 setsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
1060 {
1061 	u32 D, M, N;
1062 	union cmdret rptr = CMDRET_ZERO;
1063 	int i;
1064 
1065 	snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
1066 		    intdec[1], rate);
1067 	D = 48000;
1068 	M = ((rate == 48000) ? 47999 : rate) * 65536;
1069 	N = M % D;
1070 	M /= D;
1071 	for (i = 0; i < 2; i++) {
1072 		if (*intdec != 0xff) {
1073 			do {
1074 				SEND_SSRC(cif, *intdec, D, M, N);
1075 				SEND_RSRC(cif, *intdec, &rptr);
1076 			} while (rptr.retwords[1] != D &&
1077 				 rptr.retwords[2] != M &&
1078 				 rptr.retwords[3] != N &&
1079 				 i++ < MAX_WRITE_RETRY);
1080 			if (i == MAX_WRITE_RETRY) {
1081 				snd_printdd("sent samplerate %d: %d failed\n",
1082 					    *intdec, rate);
1083 				return -EIO;
1084 			}
1085 		}
1086 		intdec++;
1087 	}
1088 	return 0;
1089 }
1090 
1091 static int
1092 getmixer(struct cmdif *cif, short num, unsigned short *rval,
1093 	 unsigned short *lval)
1094 {
1095 	union cmdret rptr = CMDRET_ZERO;
1096 
1097 	if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
1098 		return -EIO;
1099 	*rval = rptr.retwords[0];
1100 	*lval = rptr.retwords[1];
1101 	snd_printdd("got mixer %d: 0x%d 0x%d\n", num, *rval, *lval);
1102 	return 0;
1103 }
1104 
1105 static void riptide_handleirq(unsigned long dev_id)
1106 {
1107 	struct snd_riptide *chip = (void *)dev_id;
1108 	struct cmdif *cif = chip->cif;
1109 	struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
1110 	struct snd_pcm_runtime *runtime;
1111 	struct pcmhw *data = NULL;
1112 	unsigned int pos, period_bytes;
1113 	struct sgd *c;
1114 	int i, j;
1115 	unsigned int flag;
1116 
1117 	if (!cif)
1118 		return;
1119 
1120 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
1121 		substream[i] = chip->playback_substream[i];
1122 	substream[i] = chip->capture_substream;
1123 	for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
1124 		if (substream[i] &&
1125 		    (runtime = substream[i]->runtime) &&
1126 		    (data = runtime->private_data) && data->state != ST_STOP) {
1127 			pos = 0;
1128 			for (j = 0; j < data->pages; j++) {
1129 				c = &data->sgdbuf[j];
1130 				flag = le32_to_cpu(c->dwStat_Ctl);
1131 				if (flag & EOB_STATUS)
1132 					pos += le32_to_cpu(c->dwSegLen);
1133 				if (flag & EOC_STATUS)
1134 					pos += le32_to_cpu(c->dwSegLen);
1135 				if ((flag & EOS_STATUS)
1136 				    && (data->state == ST_PLAY)) {
1137 					data->state = ST_STOP;
1138 					snd_printk(KERN_ERR
1139 						   "Riptide: DMA stopped unexpectedly\n");
1140 				}
1141 				c->dwStat_Ctl =
1142 				    cpu_to_le32(flag &
1143 						~(EOS_STATUS | EOB_STATUS |
1144 						  EOC_STATUS));
1145 			}
1146 			data->pointer += pos;
1147 			pos += data->oldpos;
1148 			if (data->state != ST_STOP) {
1149 				period_bytes =
1150 				    frames_to_bytes(runtime,
1151 						    runtime->period_size);
1152 				snd_printdd
1153 				    ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
1154 				     READ_AUDIO_STATUS(cif->hwport),
1155 				     bytes_to_frames(runtime, pos),
1156 				     runtime->period_size);
1157 				j = 0;
1158 				if (pos >= period_bytes) {
1159 					j++;
1160 					while (pos >= period_bytes)
1161 						pos -= period_bytes;
1162 				}
1163 				data->oldpos = pos;
1164 				if (j > 0)
1165 					snd_pcm_period_elapsed(substream[i]);
1166 			}
1167 		}
1168 	}
1169 }
1170 
1171 #ifdef CONFIG_PM
1172 static int riptide_suspend(struct pci_dev *pci, pm_message_t state)
1173 {
1174 	struct snd_card *card = pci_get_drvdata(pci);
1175 	struct snd_riptide *chip = card->private_data;
1176 
1177 	chip->in_suspend = 1;
1178 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1179 	snd_pcm_suspend_all(chip->pcm);
1180 	snd_ac97_suspend(chip->ac97);
1181 	pci_disable_device(pci);
1182 	pci_save_state(pci);
1183 	pci_set_power_state(pci, pci_choose_state(pci, state));
1184 	return 0;
1185 }
1186 
1187 static int riptide_resume(struct pci_dev *pci)
1188 {
1189 	struct snd_card *card = pci_get_drvdata(pci);
1190 	struct snd_riptide *chip = card->private_data;
1191 
1192 	pci_set_power_state(pci, PCI_D0);
1193 	pci_restore_state(pci);
1194 	if (pci_enable_device(pci) < 0) {
1195 		printk(KERN_ERR "riptide: pci_enable_device failed, "
1196 		       "disabling device\n");
1197 		snd_card_disconnect(card);
1198 		return -EIO;
1199 	}
1200 	pci_set_master(pci);
1201 	snd_riptide_initialize(chip);
1202 	snd_ac97_resume(chip->ac97);
1203 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1204 	chip->in_suspend = 0;
1205 	return 0;
1206 }
1207 #endif
1208 
1209 static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
1210 {
1211 	int timeout, tries;
1212 	union cmdret rptr = CMDRET_ZERO;
1213 	union firmware_version firmware;
1214 	int i, j, err, has_firmware;
1215 
1216 	if (!cif)
1217 		return -EINVAL;
1218 
1219 	cif->cmdcnt = 0;
1220 	cif->cmdtime = 0;
1221 	cif->cmdtimemax = 0;
1222 	cif->cmdtimemin = 0xffffffff;
1223 	cif->errcnt = 0;
1224 	cif->is_reset = 0;
1225 
1226 	tries = RESET_TRIES;
1227 	has_firmware = 0;
1228 	while (has_firmware == 0 && tries-- > 0) {
1229 		for (i = 0; i < 2; i++) {
1230 			WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
1231 			WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
1232 		}
1233 		SET_GRESET(cif->hwport);
1234 		udelay(100);
1235 		UNSET_GRESET(cif->hwport);
1236 		udelay(100);
1237 
1238 		for (timeout = 100000; --timeout; udelay(10)) {
1239 			if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
1240 				break;
1241 		}
1242 		if (timeout == 0) {
1243 			snd_printk(KERN_ERR
1244 				   "Riptide: device not ready, audio status: 0x%x ready: %d gerr: %d\n",
1245 				   READ_AUDIO_STATUS(cif->hwport),
1246 				   IS_READY(cif->hwport), IS_GERR(cif->hwport));
1247 			return -EIO;
1248 		} else {
1249 			snd_printdd
1250 			    ("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
1251 			     READ_AUDIO_STATUS(cif->hwport),
1252 			     IS_READY(cif->hwport), IS_GERR(cif->hwport));
1253 		}
1254 
1255 		SEND_GETV(cif, &rptr);
1256 		for (i = 0; i < 4; i++)
1257 			firmware.ret.retwords[i] = rptr.retwords[i];
1258 
1259 		snd_printdd
1260 		    ("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
1261 		     firmware.firmware.ASIC, firmware.firmware.CODEC,
1262 		     firmware.firmware.AUXDSP, firmware.firmware.PROG);
1263 
1264 		for (j = 0; j < FIRMWARE_VERSIONS; j++) {
1265 			has_firmware = 1;
1266 			for (i = 0; i < 4; i++) {
1267 				if (firmware_versions[j].ret.retwords[i] !=
1268 				    firmware.ret.retwords[i])
1269 					has_firmware = 0;
1270 			}
1271 			if (has_firmware)
1272 				break;
1273 		}
1274 
1275 		if (chip != NULL && has_firmware == 0) {
1276 			snd_printdd("Writing Firmware\n");
1277 			if (!chip->fw_entry) {
1278 				if ((err =
1279 				     request_firmware(&chip->fw_entry,
1280 						      "riptide.hex",
1281 						      &chip->pci->dev)) != 0) {
1282 					snd_printk(KERN_ERR
1283 						   "Riptide: Firmware not available %d\n",
1284 						   err);
1285 					return -EIO;
1286 				}
1287 			}
1288 			err = loadfirmware(cif, chip->fw_entry->data,
1289 					   chip->fw_entry->size);
1290 			if (err)
1291 				snd_printk(KERN_ERR
1292 					   "Riptide: Could not load firmware %d\n",
1293 					   err);
1294 		}
1295 	}
1296 
1297 	SEND_SACR(cif, 0, AC97_RESET);
1298 	SEND_RACR(cif, AC97_RESET, &rptr);
1299 	snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
1300 
1301 	SEND_PLST(cif, 0);
1302 	SEND_SLST(cif, 0);
1303 	SEND_DLST(cif, 0);
1304 	SEND_ALST(cif, 0);
1305 	SEND_KDMA(cif);
1306 
1307 	writearm(cif, 0x301F8, 1, 1);
1308 	writearm(cif, 0x301F4, 1, 1);
1309 
1310 	SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
1311 		  MODEM_SPLITTER, MODEM_MIXER);
1312 	setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
1313 	alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
1314 
1315 	SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
1316 		  FM_MIXER);
1317 	setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
1318 	writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
1319 	writearm(cif, 0x301A8, 0x02, 0x00000002);
1320 	writearm(cif, 0x30264, 0x08, 0xffffffff);
1321 	alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
1322 
1323 	SEND_SSRC(cif, I2S_INTDEC, 48000,
1324 		  ((u32) I2S_RATE * 65536) / 48000,
1325 		  ((u32) I2S_RATE * 65536) % 48000);
1326 	SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
1327 		  I2S_MIXER);
1328 	SEND_SI2S(cif, 1);
1329 	alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
1330 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
1331 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
1332 
1333 	SET_AIACK(cif->hwport);
1334 	SET_AIE(cif->hwport);
1335 	SET_AIACK(cif->hwport);
1336 	cif->is_reset = 1;
1337 	if (chip) {
1338 		for (i = 0; i < 4; i++)
1339 			chip->firmware.ret.retwords[i] =
1340 			    firmware.ret.retwords[i];
1341 	}
1342 
1343 	return 0;
1344 }
1345 
1346 static struct snd_pcm_hardware snd_riptide_playback = {
1347 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1348 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1349 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1350 	.formats =
1351 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1352 	    | SNDRV_PCM_FMTBIT_U16_LE,
1353 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1354 	.rate_min = 5500,
1355 	.rate_max = 48000,
1356 	.channels_min = 1,
1357 	.channels_max = 2,
1358 	.buffer_bytes_max = (64 * 1024),
1359 	.period_bytes_min = PAGE_SIZE >> 1,
1360 	.period_bytes_max = PAGE_SIZE << 8,
1361 	.periods_min = 2,
1362 	.periods_max = 64,
1363 	.fifo_size = 0,
1364 };
1365 static struct snd_pcm_hardware snd_riptide_capture = {
1366 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1367 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1368 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1369 	.formats =
1370 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1371 	    | SNDRV_PCM_FMTBIT_U16_LE,
1372 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1373 	.rate_min = 5500,
1374 	.rate_max = 48000,
1375 	.channels_min = 1,
1376 	.channels_max = 2,
1377 	.buffer_bytes_max = (64 * 1024),
1378 	.period_bytes_min = PAGE_SIZE >> 1,
1379 	.period_bytes_max = PAGE_SIZE << 3,
1380 	.periods_min = 2,
1381 	.periods_max = 64,
1382 	.fifo_size = 0,
1383 };
1384 
1385 static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
1386 					     *substream)
1387 {
1388 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1389 	struct snd_pcm_runtime *runtime = substream->runtime;
1390 	struct pcmhw *data = get_pcmhwdev(substream);
1391 	struct cmdif *cif = chip->cif;
1392 	union cmdret rptr = CMDRET_ZERO;
1393 	snd_pcm_uframes_t ret;
1394 
1395 	SEND_GPOS(cif, 0, data->id, &rptr);
1396 	if (data->size && runtime->period_size) {
1397 		snd_printdd
1398 		    ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
1399 		     data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
1400 		     bytes_to_frames(runtime, rptr.retlongs[1]),
1401 		     bytes_to_frames(runtime,
1402 				     rptr.retlongs[1]) % runtime->period_size);
1403 		if (rptr.retlongs[1] > data->pointer)
1404 			ret =
1405 			    bytes_to_frames(runtime,
1406 					    rptr.retlongs[1] % data->size);
1407 		else
1408 			ret =
1409 			    bytes_to_frames(runtime,
1410 					    data->pointer % data->size);
1411 	} else {
1412 		snd_printdd("stream not started or strange parms (%d %ld)\n",
1413 			    data->size, runtime->period_size);
1414 		ret = bytes_to_frames(runtime, 0);
1415 	}
1416 	return ret;
1417 }
1418 
1419 static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
1420 {
1421 	int i, j;
1422 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1423 	struct pcmhw *data = get_pcmhwdev(substream);
1424 	struct cmdif *cif = chip->cif;
1425 	union cmdret rptr = CMDRET_ZERO;
1426 
1427 	spin_lock(&chip->lock);
1428 	switch (cmd) {
1429 	case SNDRV_PCM_TRIGGER_START:
1430 	case SNDRV_PCM_TRIGGER_RESUME:
1431 		if (!(data->state & ST_PLAY)) {
1432 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1433 			SET_AIE(cif->hwport);
1434 			data->state = ST_PLAY;
1435 			if (data->mixer != 0xff)
1436 				setmixer(cif, data->mixer, 0x7fff, 0x7fff);
1437 			chip->openstreams++;
1438 			data->oldpos = 0;
1439 			data->pointer = 0;
1440 		}
1441 		break;
1442 	case SNDRV_PCM_TRIGGER_STOP:
1443 	case SNDRV_PCM_TRIGGER_SUSPEND:
1444 		if (data->mixer != 0xff)
1445 			setmixer(cif, data->mixer, 0, 0);
1446 		setmixer(cif, data->mixer, 0, 0);
1447 		SEND_KSTR(cif, data->id);
1448 		data->state = ST_STOP;
1449 		chip->openstreams--;
1450 		j = 0;
1451 		do {
1452 			i = rptr.retlongs[1];
1453 			SEND_GPOS(cif, 0, data->id, &rptr);
1454 			udelay(1);
1455 		} while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
1456 		if (j >= MAX_WRITE_RETRY)
1457 			snd_printk(KERN_ERR "Riptide: Could not stop stream!");
1458 		break;
1459 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1460 		if (!(data->state & ST_PAUSE)) {
1461 			SEND_PSTR(cif, data->id);
1462 			data->state |= ST_PAUSE;
1463 			chip->openstreams--;
1464 		}
1465 		break;
1466 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1467 		if (data->state & ST_PAUSE) {
1468 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1469 			data->state &= ~ST_PAUSE;
1470 			chip->openstreams++;
1471 		}
1472 		break;
1473 	default:
1474 		spin_unlock(&chip->lock);
1475 		return -EINVAL;
1476 	}
1477 	spin_unlock(&chip->lock);
1478 	return 0;
1479 }
1480 
1481 static int snd_riptide_prepare(struct snd_pcm_substream *substream)
1482 {
1483 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1484 	struct snd_pcm_runtime *runtime = substream->runtime;
1485 	struct snd_sg_buf *sgbuf = snd_pcm_substream_sgbuf(substream);
1486 	struct pcmhw *data = get_pcmhwdev(substream);
1487 	struct cmdif *cif = chip->cif;
1488 	unsigned char *lbuspath = NULL;
1489 	unsigned int rate, channels;
1490 	int err = 0;
1491 	snd_pcm_format_t format;
1492 
1493 	snd_assert(cif && data, return -EINVAL);
1494 
1495 	snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
1496 		    runtime->channels, runtime->format, runtime->rate);
1497 
1498 	spin_lock_irq(&chip->lock);
1499 	channels = runtime->channels;
1500 	format = runtime->format;
1501 	rate = runtime->rate;
1502 	switch (channels) {
1503 	case 1:
1504 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1505 			lbuspath = data->paths.noconv;
1506 		else
1507 			lbuspath = data->paths.mono;
1508 		break;
1509 	case 2:
1510 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1511 			lbuspath = data->paths.noconv;
1512 		else
1513 			lbuspath = data->paths.stereo;
1514 		break;
1515 	}
1516 	snd_printdd("use sgdlist at 0x%p and buffer at 0x%p\n",
1517 		    data->sgdlist.area, sgbuf);
1518 	if (data->sgdlist.area && sgbuf) {
1519 		unsigned int i, j, size, pages, f, pt, period;
1520 		struct sgd *c, *p = NULL;
1521 
1522 		size = frames_to_bytes(runtime, runtime->buffer_size);
1523 		period = frames_to_bytes(runtime, runtime->period_size);
1524 		f = PAGE_SIZE;
1525 		while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
1526 			f = f >> 1;
1527 		pages = (size + f - 1) / f;
1528 		data->size = size;
1529 		data->pages = pages;
1530 		snd_printdd
1531 		    ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
1532 		     size, pages, f, period);
1533 		pt = 0;
1534 		j = 0;
1535 		for (i = 0; i < pages; i++) {
1536 			c = &data->sgdbuf[i];
1537 			if (p)
1538 				p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
1539 							    (i *
1540 							     sizeof(struct
1541 								    sgd)));
1542 			c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
1543 			c->dwSegPtrPhys =
1544 			    cpu_to_le32(sgbuf->table[j].addr + pt);
1545 			pt = (pt + f) % PAGE_SIZE;
1546 			if (pt == 0)
1547 				j++;
1548 			c->dwSegLen = cpu_to_le32(f);
1549 			c->dwStat_Ctl =
1550 			    cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
1551 					IEOC_ENABLE);
1552 			p = c;
1553 			size -= f;
1554 		}
1555 		data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
1556 	}
1557 	if (lbuspath && lbuspath != data->lbuspath) {
1558 		if (data->lbuspath)
1559 			freelbuspath(cif, data->source, data->lbuspath);
1560 		alloclbuspath(cif, data->source, lbuspath,
1561 			      &data->mixer, data->intdec);
1562 		data->lbuspath = lbuspath;
1563 		data->rate = 0;
1564 	}
1565 	if (data->rate != rate || data->format != format ||
1566 	    data->channels != channels) {
1567 		data->rate = rate;
1568 		data->format = format;
1569 		data->channels = channels;
1570 		if (setsampleformat
1571 		    (cif, data->mixer, data->id, channels, format)
1572 		    || setsamplerate(cif, data->intdec, rate))
1573 			err = -EIO;
1574 	}
1575 	spin_unlock_irq(&chip->lock);
1576 	return err;
1577 }
1578 
1579 static int
1580 snd_riptide_hw_params(struct snd_pcm_substream *substream,
1581 		      struct snd_pcm_hw_params *hw_params)
1582 {
1583 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1584 	struct pcmhw *data = get_pcmhwdev(substream);
1585 	struct snd_dma_buffer *sgdlist = &data->sgdlist;
1586 	int err;
1587 
1588 	snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
1589 		    sgdlist->area, (unsigned long)sgdlist->addr,
1590 		    (int)sgdlist->bytes);
1591 	if (sgdlist->area)
1592 		snd_dma_free_pages(sgdlist);
1593 	if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
1594 				       snd_dma_pci_data(chip->pci),
1595 				       sizeof(struct sgd) * (DESC_MAX_MASK + 1),
1596 				       sgdlist)) < 0) {
1597 		snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
1598 			   (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
1599 		return err;
1600 	}
1601 	data->sgdbuf = (struct sgd *)sgdlist->area;
1602 	return snd_pcm_lib_malloc_pages(substream,
1603 					params_buffer_bytes(hw_params));
1604 }
1605 
1606 static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
1607 {
1608 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1609 	struct pcmhw *data = get_pcmhwdev(substream);
1610 	struct cmdif *cif = chip->cif;
1611 
1612 	if (cif && data) {
1613 		if (data->lbuspath)
1614 			freelbuspath(cif, data->source, data->lbuspath);
1615 		data->lbuspath = NULL;
1616 		data->source = 0xff;
1617 		data->intdec[0] = 0xff;
1618 		data->intdec[1] = 0xff;
1619 
1620 		if (data->sgdlist.area) {
1621 			snd_dma_free_pages(&data->sgdlist);
1622 			data->sgdlist.area = NULL;
1623 		}
1624 	}
1625 	return snd_pcm_lib_free_pages(substream);
1626 }
1627 
1628 static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
1629 {
1630 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1631 	struct snd_pcm_runtime *runtime = substream->runtime;
1632 	struct pcmhw *data;
1633 	int index = substream->number;
1634 
1635 	chip->playback_substream[index] = substream;
1636 	runtime->hw = snd_riptide_playback;
1637 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
1638 	data->paths = lbus_play_paths[index];
1639 	data->id = play_ids[index];
1640 	data->source = play_sources[index];
1641 	data->intdec[0] = 0xff;
1642 	data->intdec[1] = 0xff;
1643 	data->state = ST_STOP;
1644 	runtime->private_data = data;
1645 	return snd_pcm_hw_constraint_integer(runtime,
1646 					     SNDRV_PCM_HW_PARAM_PERIODS);
1647 }
1648 
1649 static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
1650 {
1651 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1652 	struct snd_pcm_runtime *runtime = substream->runtime;
1653 	struct pcmhw *data;
1654 
1655 	chip->capture_substream = substream;
1656 	runtime->hw = snd_riptide_capture;
1657 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
1658 	data->paths = lbus_rec_path;
1659 	data->id = PADC;
1660 	data->source = ACLNK2PADC;
1661 	data->intdec[0] = 0xff;
1662 	data->intdec[1] = 0xff;
1663 	data->state = ST_STOP;
1664 	runtime->private_data = data;
1665 	return snd_pcm_hw_constraint_integer(runtime,
1666 					     SNDRV_PCM_HW_PARAM_PERIODS);
1667 }
1668 
1669 static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
1670 {
1671 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1672 	struct pcmhw *data = get_pcmhwdev(substream);
1673 	int index = substream->number;
1674 
1675 	substream->runtime->private_data = NULL;
1676 	chip->playback_substream[index] = NULL;
1677 	kfree(data);
1678 	return 0;
1679 }
1680 
1681 static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
1682 {
1683 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1684 	struct pcmhw *data = get_pcmhwdev(substream);
1685 
1686 	substream->runtime->private_data = NULL;
1687 	chip->capture_substream = NULL;
1688 	kfree(data);
1689 	return 0;
1690 }
1691 
1692 static struct snd_pcm_ops snd_riptide_playback_ops = {
1693 	.open = snd_riptide_playback_open,
1694 	.close = snd_riptide_playback_close,
1695 	.ioctl = snd_pcm_lib_ioctl,
1696 	.hw_params = snd_riptide_hw_params,
1697 	.hw_free = snd_riptide_hw_free,
1698 	.prepare = snd_riptide_prepare,
1699 	.page = snd_pcm_sgbuf_ops_page,
1700 	.trigger = snd_riptide_trigger,
1701 	.pointer = snd_riptide_pointer,
1702 };
1703 static struct snd_pcm_ops snd_riptide_capture_ops = {
1704 	.open = snd_riptide_capture_open,
1705 	.close = snd_riptide_capture_close,
1706 	.ioctl = snd_pcm_lib_ioctl,
1707 	.hw_params = snd_riptide_hw_params,
1708 	.hw_free = snd_riptide_hw_free,
1709 	.prepare = snd_riptide_prepare,
1710 	.page = snd_pcm_sgbuf_ops_page,
1711 	.trigger = snd_riptide_trigger,
1712 	.pointer = snd_riptide_pointer,
1713 };
1714 
1715 static int __devinit
1716 snd_riptide_pcm(struct snd_riptide *chip, int device, struct snd_pcm **rpcm)
1717 {
1718 	struct snd_pcm *pcm;
1719 	int err;
1720 
1721 	if (rpcm)
1722 		*rpcm = NULL;
1723 	if ((err =
1724 	     snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
1725 			 &pcm)) < 0)
1726 		return err;
1727 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1728 			&snd_riptide_playback_ops);
1729 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1730 			&snd_riptide_capture_ops);
1731 	pcm->private_data = chip;
1732 	pcm->info_flags = 0;
1733 	strcpy(pcm->name, "RIPTIDE");
1734 	chip->pcm = pcm;
1735 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1736 					      snd_dma_pci_data(chip->pci),
1737 					      64 * 1024, 128 * 1024);
1738 	if (rpcm)
1739 		*rpcm = pcm;
1740 	return 0;
1741 }
1742 
1743 static irqreturn_t
1744 snd_riptide_interrupt(int irq, void *dev_id)
1745 {
1746 	struct snd_riptide *chip = dev_id;
1747 	struct cmdif *cif = chip->cif;
1748 
1749 	if (cif) {
1750 		chip->received_irqs++;
1751 		if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
1752 		    IS_EOCIRQ(cif->hwport)) {
1753 			chip->handled_irqs++;
1754 			tasklet_hi_schedule(&chip->riptide_tq);
1755 		}
1756 		if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
1757 			chip->handled_irqs++;
1758 			snd_mpu401_uart_interrupt(irq,
1759 						  chip->rmidi->private_data);
1760 		}
1761 		SET_AIACK(cif->hwport);
1762 	}
1763 	return IRQ_HANDLED;
1764 }
1765 
1766 static void
1767 snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
1768 			unsigned short val)
1769 {
1770 	struct snd_riptide *chip = ac97->private_data;
1771 	struct cmdif *cif = chip->cif;
1772 	union cmdret rptr = CMDRET_ZERO;
1773 	int i = 0;
1774 
1775 	snd_assert(cif, return);
1776 
1777 	snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
1778 	do {
1779 		SEND_SACR(cif, val, reg);
1780 		SEND_RACR(cif, reg, &rptr);
1781 	} while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
1782 	if (i == MAX_WRITE_RETRY)
1783 		snd_printdd("Write AC97 reg failed\n");
1784 }
1785 
1786 static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
1787 					     unsigned short reg)
1788 {
1789 	struct snd_riptide *chip = ac97->private_data;
1790 	struct cmdif *cif = chip->cif;
1791 	union cmdret rptr = CMDRET_ZERO;
1792 
1793 	snd_assert(cif, return 0);
1794 
1795 	if (SEND_RACR(cif, reg, &rptr) != 0)
1796 		SEND_RACR(cif, reg, &rptr);
1797 	snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
1798 	return rptr.retwords[1];
1799 }
1800 
1801 static int snd_riptide_initialize(struct snd_riptide *chip)
1802 {
1803 	struct cmdif *cif;
1804 	unsigned int device_id;
1805 	int err;
1806 
1807 	snd_assert(chip, return -EINVAL);
1808 
1809 	cif = chip->cif;
1810 	if (!cif) {
1811 		if ((cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL)) == NULL)
1812 			return -ENOMEM;
1813 		cif->hwport = (struct riptideport *)chip->port;
1814 		spin_lock_init(&cif->lock);
1815 		chip->cif = cif;
1816 	}
1817 	cif->is_reset = 0;
1818 	if ((err = riptide_reset(cif, chip)) != 0)
1819 		return err;
1820 	device_id = chip->device_id;
1821 	switch (device_id) {
1822 	case 0x4310:
1823 	case 0x4320:
1824 	case 0x4330:
1825 		snd_printdd("Modem enable?\n");
1826 		SEND_SETDPLL(cif);
1827 		break;
1828 	}
1829 	snd_printdd("Enabling MPU IRQs\n");
1830 	if (chip->rmidi)
1831 		SET_EMPUIRQ(cif->hwport);
1832 	return err;
1833 }
1834 
1835 static int snd_riptide_free(struct snd_riptide *chip)
1836 {
1837 	struct cmdif *cif;
1838 
1839 	snd_assert(chip, return 0);
1840 
1841 	if ((cif = chip->cif)) {
1842 		SET_GRESET(cif->hwport);
1843 		udelay(100);
1844 		UNSET_GRESET(cif->hwport);
1845 		kfree(chip->cif);
1846 	}
1847 	if (chip->irq >= 0)
1848 		free_irq(chip->irq, chip);
1849 	if (chip->fw_entry)
1850 		release_firmware(chip->fw_entry);
1851 	release_and_free_resource(chip->res_port);
1852 	kfree(chip);
1853 	return 0;
1854 }
1855 
1856 static int snd_riptide_dev_free(struct snd_device *device)
1857 {
1858 	struct snd_riptide *chip = device->device_data;
1859 
1860 	return snd_riptide_free(chip);
1861 }
1862 
1863 static int __devinit
1864 snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
1865 		   struct snd_riptide **rchip)
1866 {
1867 	struct snd_riptide *chip;
1868 	struct riptideport *hwport;
1869 	int err;
1870 	static struct snd_device_ops ops = {
1871 		.dev_free = snd_riptide_dev_free,
1872 	};
1873 
1874 	*rchip = NULL;
1875 	if ((err = pci_enable_device(pci)) < 0)
1876 		return err;
1877 	if (!(chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL)))
1878 		return -ENOMEM;
1879 
1880 	spin_lock_init(&chip->lock);
1881 	chip->card = card;
1882 	chip->pci = pci;
1883 	chip->irq = -1;
1884 	chip->openstreams = 0;
1885 	chip->port = pci_resource_start(pci, 0);
1886 	chip->received_irqs = 0;
1887 	chip->handled_irqs = 0;
1888 	chip->cif = NULL;
1889 	tasklet_init(&chip->riptide_tq, riptide_handleirq, (unsigned long)chip);
1890 
1891 	if ((chip->res_port =
1892 	     request_region(chip->port, 64, "RIPTIDE")) == NULL) {
1893 		snd_printk(KERN_ERR
1894 			   "Riptide: unable to grab region 0x%lx-0x%lx\n",
1895 			   chip->port, chip->port + 64 - 1);
1896 		snd_riptide_free(chip);
1897 		return -EBUSY;
1898 	}
1899 	hwport = (struct riptideport *)chip->port;
1900 	UNSET_AIE(hwport);
1901 
1902 	if (request_irq(pci->irq, snd_riptide_interrupt, IRQF_SHARED,
1903 			"RIPTIDE", chip)) {
1904 		snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
1905 			   pci->irq);
1906 		snd_riptide_free(chip);
1907 		return -EBUSY;
1908 	}
1909 	chip->irq = pci->irq;
1910 	chip->device_id = pci->device;
1911 	pci_set_master(pci);
1912 	if ((err = snd_riptide_initialize(chip)) < 0) {
1913 		snd_riptide_free(chip);
1914 		return err;
1915 	}
1916 
1917 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1918 		snd_riptide_free(chip);
1919 		return err;
1920 	}
1921 
1922 	snd_card_set_dev(card, &pci->dev);
1923 
1924 	*rchip = chip;
1925 	return 0;
1926 }
1927 
1928 static void
1929 snd_riptide_proc_read(struct snd_info_entry *entry,
1930 		      struct snd_info_buffer *buffer)
1931 {
1932 	struct snd_riptide *chip = entry->private_data;
1933 	struct pcmhw *data;
1934 	int i;
1935 	struct cmdif *cif = NULL;
1936 	unsigned char p[256];
1937 	unsigned short rval = 0, lval = 0;
1938 	unsigned int rate;
1939 
1940 	if (!chip)
1941 		return;
1942 
1943 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1944 	snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
1945 		    chip->device_id, chip->handled_irqs, chip->received_irqs);
1946 	for (i = 0; i < 64; i += 4)
1947 		snd_iprintf(buffer, "%c%02x: %08x",
1948 			    (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
1949 	if ((cif = chip->cif)) {
1950 		snd_iprintf(buffer,
1951 			    "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
1952 			    chip->firmware.firmware.ASIC,
1953 			    chip->firmware.firmware.CODEC,
1954 			    chip->firmware.firmware.AUXDSP,
1955 			    chip->firmware.firmware.PROG);
1956 		snd_iprintf(buffer, "\nDigital mixer:");
1957 		for (i = 0; i < 12; i++) {
1958 			getmixer(cif, i, &rval, &lval);
1959 			snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
1960 		}
1961 		snd_iprintf(buffer,
1962 			    "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
1963 			    cif->cmdcnt, cif->errcnt,
1964 			    cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
1965 	}
1966 	snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
1967 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
1968 		if (chip->playback_substream[i]
1969 		    && chip->playback_substream[i]->runtime
1970 		    && (data =
1971 			chip->playback_substream[i]->runtime->private_data)) {
1972 			snd_iprintf(buffer,
1973 				    "stream: %d mixer: %d source: %d (%d,%d)\n",
1974 				    data->id, data->mixer, data->source,
1975 				    data->intdec[0], data->intdec[1]);
1976 			if (!(getsamplerate(cif, data->intdec, &rate)))
1977 				snd_iprintf(buffer, "rate: %d\n", rate);
1978 		}
1979 	}
1980 	if (chip->capture_substream
1981 	    && chip->capture_substream->runtime
1982 	    && (data = chip->capture_substream->runtime->private_data)) {
1983 		snd_iprintf(buffer,
1984 			    "stream: %d mixer: %d source: %d (%d,%d)\n",
1985 			    data->id, data->mixer,
1986 			    data->source, data->intdec[0], data->intdec[1]);
1987 		if (!(getsamplerate(cif, data->intdec, &rate)))
1988 			snd_iprintf(buffer, "rate: %d\n", rate);
1989 	}
1990 	snd_iprintf(buffer, "Paths:\n");
1991 	i = getpaths(cif, p);
1992 	while (i--) {
1993 		snd_iprintf(buffer, "%x->%x ", p[i - 1], p[i]);
1994 		i--;
1995 	}
1996 	snd_iprintf(buffer, "\n");
1997 }
1998 
1999 static void __devinit snd_riptide_proc_init(struct snd_riptide *chip)
2000 {
2001 	struct snd_info_entry *entry;
2002 
2003 	if (!snd_card_proc_new(chip->card, "riptide", &entry))
2004 		snd_info_set_text_ops(entry, chip, snd_riptide_proc_read);
2005 }
2006 
2007 static int __devinit snd_riptide_mixer(struct snd_riptide *chip)
2008 {
2009 	struct snd_ac97_bus *pbus;
2010 	struct snd_ac97_template ac97;
2011 	int err = 0;
2012 	static struct snd_ac97_bus_ops ops = {
2013 		.write = snd_riptide_codec_write,
2014 		.read = snd_riptide_codec_read,
2015 	};
2016 
2017 	memset(&ac97, 0, sizeof(ac97));
2018 	ac97.private_data = chip;
2019 	ac97.scaps = AC97_SCAP_SKIP_MODEM;
2020 
2021 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
2022 		return err;
2023 
2024 	chip->ac97_bus = pbus;
2025 	ac97.pci = chip->pci;
2026 	if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2027 		return err;
2028 	return err;
2029 }
2030 
2031 #ifdef SUPPORT_JOYSTICK
2032 static int have_joystick;
2033 static struct pci_dev *riptide_gameport_pci;
2034 static struct gameport *riptide_gameport;
2035 
2036 static int __devinit
2037 snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
2038 {
2039 	static int dev;
2040 
2041 	if (dev >= SNDRV_CARDS)
2042 		return -ENODEV;
2043 	if (!enable[dev]) {
2044 		dev++;
2045 		return -ENOENT;
2046 	}
2047 
2048 	if (joystick_port[dev]) {
2049 		riptide_gameport = gameport_allocate_port();
2050 		if (riptide_gameport) {
2051 			if (!request_region
2052 			    (joystick_port[dev], 8, "Riptide gameport")) {
2053 				snd_printk(KERN_WARNING
2054 					   "Riptide: cannot grab gameport 0x%x\n",
2055 					   joystick_port[dev]);
2056 				gameport_free_port(riptide_gameport);
2057 				riptide_gameport = NULL;
2058 			} else {
2059 				riptide_gameport_pci = pci;
2060 				riptide_gameport->io = joystick_port[dev];
2061 				gameport_register_port(riptide_gameport);
2062 			}
2063 		}
2064 	}
2065 	dev++;
2066 	return 0;
2067 }
2068 
2069 static void __devexit snd_riptide_joystick_remove(struct pci_dev *pci)
2070 {
2071 	if (riptide_gameport) {
2072 		if (riptide_gameport_pci == pci) {
2073 			release_region(riptide_gameport->io, 8);
2074 			riptide_gameport_pci = NULL;
2075 			gameport_unregister_port(riptide_gameport);
2076 			riptide_gameport = NULL;
2077 		}
2078 	}
2079 }
2080 #endif
2081 
2082 static int __devinit
2083 snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2084 {
2085 	static int dev;
2086 	struct snd_card *card;
2087 	struct snd_riptide *chip;
2088 	unsigned short addr;
2089 	int err = 0;
2090 
2091 	if (dev >= SNDRV_CARDS)
2092 		return -ENODEV;
2093 	if (!enable[dev]) {
2094 		dev++;
2095 		return -ENOENT;
2096 	}
2097 
2098 	card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2099 	if (card == NULL)
2100 		return -ENOMEM;
2101 	if ((err = snd_riptide_create(card, pci, &chip)) < 0) {
2102 		snd_card_free(card);
2103 		return err;
2104 	}
2105 	card->private_data = chip;
2106 	if ((err = snd_riptide_pcm(chip, 0, NULL)) < 0) {
2107 		snd_card_free(card);
2108 		return err;
2109 	}
2110 	if ((err = snd_riptide_mixer(chip)) < 0) {
2111 		snd_card_free(card);
2112 		return err;
2113 	}
2114 	pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, LEGACY_ENABLE_ALL
2115 			      | (opl3_port[dev] ? LEGACY_ENABLE_FM : 0)
2116 #ifdef SUPPORT_JOYSTICK
2117 			      | (joystick_port[dev] ? LEGACY_ENABLE_GAMEPORT :
2118 				 0)
2119 #endif
2120 			      | (mpu_port[dev]
2121 				 ? (LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU) :
2122 				 0)
2123 			      | ((chip->irq << 4) & 0xF0));
2124 	if ((addr = mpu_port[dev]) != 0) {
2125 		pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, addr);
2126 		if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
2127 					       addr, 0, chip->irq, 0,
2128 					       &chip->rmidi)) < 0)
2129 			snd_printk(KERN_WARNING
2130 				   "Riptide: Can't Allocate MPU at 0x%x\n",
2131 				   addr);
2132 		else
2133 			chip->mpuaddr = addr;
2134 	}
2135 	if ((addr = opl3_port[dev]) != 0) {
2136 		pci_write_config_word(chip->pci, PCI_EXT_FM_Base, addr);
2137 		if ((err = snd_opl3_create(card, addr, addr + 2,
2138 					   OPL3_HW_RIPTIDE, 0,
2139 					   &chip->opl3)) < 0)
2140 			snd_printk(KERN_WARNING
2141 				   "Riptide: Can't Allocate OPL3 at 0x%x\n",
2142 				   addr);
2143 		else {
2144 			chip->opladdr = addr;
2145 			if ((err =
2146 			     snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL)) < 0)
2147 				snd_printk(KERN_WARNING
2148 					   "Riptide: Can't Allocate OPL3-HWDEP\n");
2149 		}
2150 	}
2151 #ifdef SUPPORT_JOYSTICK
2152 	if ((addr = joystick_port[dev]) != 0) {
2153 		pci_write_config_word(chip->pci, PCI_EXT_Game_Base, addr);
2154 		chip->gameaddr = addr;
2155 	}
2156 #endif
2157 
2158 	strcpy(card->driver, "RIPTIDE");
2159 	strcpy(card->shortname, "Riptide");
2160 #ifdef SUPPORT_JOYSTICK
2161 	snprintf(card->longname, sizeof(card->longname),
2162 		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
2163 		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2164 		 chip->opladdr, chip->gameaddr);
2165 #else
2166 	snprintf(card->longname, sizeof(card->longname),
2167 		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
2168 		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2169 		 chip->opladdr);
2170 #endif
2171 	snd_riptide_proc_init(chip);
2172 	if ((err = snd_card_register(card)) < 0) {
2173 		snd_card_free(card);
2174 		return err;
2175 	}
2176 	pci_set_drvdata(pci, card);
2177 	dev++;
2178 	return 0;
2179 }
2180 
2181 static void __devexit snd_card_riptide_remove(struct pci_dev *pci)
2182 {
2183 	snd_card_free(pci_get_drvdata(pci));
2184 	pci_set_drvdata(pci, NULL);
2185 }
2186 
2187 static struct pci_driver driver = {
2188 	.name = "RIPTIDE",
2189 	.id_table = snd_riptide_ids,
2190 	.probe = snd_card_riptide_probe,
2191 	.remove = __devexit_p(snd_card_riptide_remove),
2192 #ifdef CONFIG_PM
2193 	.suspend = riptide_suspend,
2194 	.resume = riptide_resume,
2195 #endif
2196 };
2197 
2198 #ifdef SUPPORT_JOYSTICK
2199 static struct pci_driver joystick_driver = {
2200 	.name = "Riptide Joystick",
2201 	.id_table = snd_riptide_joystick_ids,
2202 	.probe = snd_riptide_joystick_probe,
2203 	.remove = __devexit_p(snd_riptide_joystick_remove),
2204 };
2205 #endif
2206 
2207 static int __init alsa_card_riptide_init(void)
2208 {
2209 	int err;
2210 	if ((err = pci_register_driver(&driver)) < 0)
2211 		return err;
2212 #if defined(SUPPORT_JOYSTICK)
2213 	if (pci_register_driver(&joystick_driver) < 0) {
2214 		have_joystick = 0;
2215 		snd_printk(KERN_INFO "no joystick found\n");
2216 	} else
2217 		have_joystick = 1;
2218 #endif
2219 	return 0;
2220 }
2221 
2222 static void __exit alsa_card_riptide_exit(void)
2223 {
2224 	pci_unregister_driver(&driver);
2225 #if defined(SUPPORT_JOYSTICK)
2226 	if (have_joystick)
2227 		pci_unregister_driver(&joystick_driver);
2228 #endif
2229 }
2230 
2231 module_init(alsa_card_riptide_init);
2232 module_exit(alsa_card_riptide_exit);
2233