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