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