xref: /openbmc/linux/sound/pci/riptide/riptide.c (revision a67ff6a54095e27093ea501fb143fefe51a536c2)
1109a9638SPeter Gruber /*
2109a9638SPeter Gruber  *   Driver for the Conexant Riptide Soundchip
3109a9638SPeter Gruber  *
4109a9638SPeter Gruber  *	Copyright (c) 2004 Peter Gruber <nokos@gmx.net>
5109a9638SPeter Gruber  *
6109a9638SPeter Gruber  *   This program is free software; you can redistribute it and/or modify
7109a9638SPeter Gruber  *   it under the terms of the GNU General Public License as published by
8109a9638SPeter Gruber  *   the Free Software Foundation; either version 2 of the License, or
9109a9638SPeter Gruber  *   (at your option) any later version.
10109a9638SPeter Gruber  *
11109a9638SPeter Gruber  *   This program is distributed in the hope that it will be useful,
12109a9638SPeter Gruber  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13109a9638SPeter Gruber  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14109a9638SPeter Gruber  *   GNU General Public License for more details.
15109a9638SPeter Gruber  *
16109a9638SPeter Gruber  *   You should have received a copy of the GNU General Public License
17109a9638SPeter Gruber  *   along with this program; if not, write to the Free Software
18109a9638SPeter Gruber  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19109a9638SPeter Gruber  *
20109a9638SPeter Gruber  */
21109a9638SPeter Gruber /*
22109a9638SPeter Gruber   History:
23109a9638SPeter Gruber    - 02/15/2004 first release
24109a9638SPeter Gruber 
25109a9638SPeter Gruber   This Driver is based on the OSS Driver version from Linuxant (riptide-0.6lnxtbeta03111100)
26109a9638SPeter Gruber   credits from the original files:
27109a9638SPeter Gruber 
28109a9638SPeter Gruber   MODULE NAME:        cnxt_rt.h
29109a9638SPeter Gruber   AUTHOR:             K. Lazarev  (Transcribed by KNL)
30109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
31109a9638SPeter Gruber             -----------------------------     --------     -----
32109a9638SPeter Gruber             Created                           02/1/2000     KNL
33109a9638SPeter Gruber 
34109a9638SPeter Gruber   MODULE NAME:     int_mdl.c
35109a9638SPeter Gruber   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
36109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
37109a9638SPeter Gruber             -----------------------------     --------     -----
38109a9638SPeter Gruber             Created                           10/01/99      KNL
39109a9638SPeter Gruber 
40109a9638SPeter Gruber   MODULE NAME:        riptide.h
41109a9638SPeter Gruber   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
42109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
43109a9638SPeter Gruber             -----------------------------     --------     -----
44109a9638SPeter Gruber             Created                           10/16/97      OLD
45109a9638SPeter Gruber 
46109a9638SPeter Gruber   MODULE NAME:        Rp_Cmdif.cpp
47109a9638SPeter Gruber   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
48109a9638SPeter Gruber                       K. Lazarev    (Transcribed by KNL)
49109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
50109a9638SPeter Gruber             -----------------------------     --------     -----
51109a9638SPeter Gruber             Adopted from NT4 driver            6/22/99      OLD
52109a9638SPeter Gruber             Ported to Linux                    9/01/99      KNL
53109a9638SPeter Gruber 
54109a9638SPeter Gruber   MODULE NAME:        rt_hw.c
55109a9638SPeter Gruber   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
56109a9638SPeter Gruber                       C. Lazarev    (Transcribed by CNL)
57109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
58109a9638SPeter Gruber             -----------------------------     --------     -----
59109a9638SPeter Gruber             Created                           11/18/97      OLD
60109a9638SPeter Gruber             Hardware functions for RipTide    11/24/97      CNL
61109a9638SPeter Gruber             (ES1) are coded
62109a9638SPeter Gruber             Hardware functions for RipTide    12/24/97      CNL
63109a9638SPeter Gruber             (A0) are coded
64109a9638SPeter Gruber             Hardware functions for RipTide    03/20/98      CNL
65109a9638SPeter Gruber             (A1) are coded
66109a9638SPeter Gruber             Boot loader is included           05/07/98      CNL
67109a9638SPeter Gruber             Redesigned for WDM                07/27/98      CNL
68109a9638SPeter Gruber             Redesigned for Linux              09/01/99      CNL
69109a9638SPeter Gruber 
70109a9638SPeter Gruber   MODULE NAME:        rt_hw.h
71109a9638SPeter Gruber   AUTHOR:             C. Lazarev    (Transcribed by CNL)
72109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
73109a9638SPeter Gruber             -----------------------------     --------     -----
74109a9638SPeter Gruber             Created                           11/18/97      CNL
75109a9638SPeter Gruber 
76109a9638SPeter Gruber   MODULE NAME:     rt_mdl.c
77109a9638SPeter Gruber   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
78109a9638SPeter Gruber   HISTORY:         Major Revision               Date        By
79109a9638SPeter Gruber             -----------------------------     --------     -----
80109a9638SPeter Gruber             Created                           10/01/99      KNL
81109a9638SPeter Gruber 
82109a9638SPeter Gruber   MODULE NAME:        mixer.h
83109a9638SPeter Gruber   AUTHOR:             K. Kenney
84109a9638SPeter Gruber   HISTORY:         Major Revision                   Date          By
85109a9638SPeter Gruber             -----------------------------          --------     -----
86109a9638SPeter Gruber             Created from MS W95 Sample             11/28/95      KRS
87109a9638SPeter Gruber             RipTide                                10/15/97      KRS
88109a9638SPeter Gruber             Adopted for Windows NT driver          01/20/98      CNL
89109a9638SPeter Gruber */
90109a9638SPeter Gruber 
91109a9638SPeter Gruber #include <linux/delay.h>
92109a9638SPeter Gruber #include <linux/init.h>
93109a9638SPeter Gruber #include <linux/interrupt.h>
94109a9638SPeter Gruber #include <linux/pci.h>
95109a9638SPeter Gruber #include <linux/slab.h>
96109a9638SPeter Gruber #include <linux/wait.h>
97109a9638SPeter Gruber #include <linux/gameport.h>
98109a9638SPeter Gruber #include <linux/device.h>
99109a9638SPeter Gruber #include <linux/firmware.h>
100c9ff921aSAndy Shevchenko #include <linux/kernel.h>
101da155d5bSPaul Gortmaker #include <linux/module.h>
102109a9638SPeter Gruber #include <asm/io.h>
103109a9638SPeter Gruber #include <sound/core.h>
104109a9638SPeter Gruber #include <sound/info.h>
105109a9638SPeter Gruber #include <sound/control.h>
106109a9638SPeter Gruber #include <sound/pcm.h>
107109a9638SPeter Gruber #include <sound/pcm_params.h>
108109a9638SPeter Gruber #include <sound/ac97_codec.h>
109109a9638SPeter Gruber #include <sound/mpu401.h>
110109a9638SPeter Gruber #include <sound/opl3.h>
111109a9638SPeter Gruber #include <sound/initval.h>
112109a9638SPeter Gruber 
113109a9638SPeter Gruber #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
114109a9638SPeter Gruber #define SUPPORT_JOYSTICK 1
115109a9638SPeter Gruber #endif
116109a9638SPeter Gruber 
117109a9638SPeter Gruber MODULE_AUTHOR("Peter Gruber <nokos@gmx.net>");
118109a9638SPeter Gruber MODULE_DESCRIPTION("riptide");
119109a9638SPeter Gruber MODULE_LICENSE("GPL");
120109a9638SPeter Gruber MODULE_SUPPORTED_DEVICE("{{Conexant,Riptide}}");
1217e0af29dSClemens Ladisch MODULE_FIRMWARE("riptide.hex");
122109a9638SPeter Gruber 
123109a9638SPeter Gruber static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
124109a9638SPeter Gruber static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
125*a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
126109a9638SPeter Gruber 
127109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
128109a9638SPeter Gruber static int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
129109a9638SPeter Gruber #endif
130109a9638SPeter Gruber static int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
131109a9638SPeter Gruber static int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
132109a9638SPeter Gruber 
133109a9638SPeter Gruber module_param_array(index, int, NULL, 0444);
134109a9638SPeter Gruber MODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
135109a9638SPeter Gruber module_param_array(id, charp, NULL, 0444);
136109a9638SPeter Gruber MODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
137109a9638SPeter Gruber module_param_array(enable, bool, NULL, 0444);
138109a9638SPeter Gruber MODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
139109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
140109a9638SPeter Gruber module_param_array(joystick_port, int, NULL, 0444);
141109a9638SPeter Gruber MODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
142109a9638SPeter Gruber #endif
143109a9638SPeter Gruber module_param_array(mpu_port, int, NULL, 0444);
144109a9638SPeter Gruber MODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
145109a9638SPeter Gruber module_param_array(opl3_port, int, NULL, 0444);
146109a9638SPeter Gruber MODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
147109a9638SPeter Gruber 
148109a9638SPeter Gruber /*
149109a9638SPeter Gruber  */
150109a9638SPeter Gruber 
151109a9638SPeter Gruber #define MPU401_HW_RIPTIDE MPU401_HW_MPU401
152109a9638SPeter Gruber #define OPL3_HW_RIPTIDE   OPL3_HW_OPL3
153109a9638SPeter Gruber 
154109a9638SPeter Gruber #define PCI_EXT_CapId       0x40
155109a9638SPeter Gruber #define PCI_EXT_NextCapPrt  0x41
156109a9638SPeter Gruber #define PCI_EXT_PWMC        0x42
157109a9638SPeter Gruber #define PCI_EXT_PWSCR       0x44
158109a9638SPeter Gruber #define PCI_EXT_Data00      0x46
159109a9638SPeter Gruber #define PCI_EXT_PMSCR_BSE   0x47
160109a9638SPeter Gruber #define PCI_EXT_SB_Base     0x48
161109a9638SPeter Gruber #define PCI_EXT_FM_Base     0x4a
162109a9638SPeter Gruber #define PCI_EXT_MPU_Base    0x4C
163109a9638SPeter Gruber #define PCI_EXT_Game_Base   0x4E
164109a9638SPeter Gruber #define PCI_EXT_Legacy_Mask 0x50
165109a9638SPeter Gruber #define PCI_EXT_AsicRev     0x52
166109a9638SPeter Gruber #define PCI_EXT_Reserved3   0x53
167109a9638SPeter Gruber 
168109a9638SPeter Gruber #define LEGACY_ENABLE_ALL      0x8000	/* legacy device options */
169109a9638SPeter Gruber #define LEGACY_ENABLE_SB       0x4000
170109a9638SPeter Gruber #define LEGACY_ENABLE_FM       0x2000
171109a9638SPeter Gruber #define LEGACY_ENABLE_MPU_INT  0x1000
172109a9638SPeter Gruber #define LEGACY_ENABLE_MPU      0x0800
173109a9638SPeter Gruber #define LEGACY_ENABLE_GAMEPORT 0x0400
174109a9638SPeter Gruber 
175109a9638SPeter Gruber #define MAX_WRITE_RETRY  10	/* cmd interface limits */
176109a9638SPeter Gruber #define MAX_ERROR_COUNT  10
1770f620830SPeter Gruber #define CMDIF_TIMEOUT    50000
178109a9638SPeter Gruber #define RESET_TRIES      5
179109a9638SPeter Gruber 
180109a9638SPeter Gruber #define READ_PORT_ULONG(p)     inl((unsigned long)&(p))
181109a9638SPeter Gruber #define WRITE_PORT_ULONG(p,x)  outl(x,(unsigned long)&(p))
182109a9638SPeter Gruber 
183109a9638SPeter Gruber #define READ_AUDIO_CONTROL(p)     READ_PORT_ULONG(p->audio_control)
184109a9638SPeter Gruber #define WRITE_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,x)
185109a9638SPeter Gruber #define UMASK_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
186109a9638SPeter Gruber #define MASK_AUDIO_CONTROL(p,x)   WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
187109a9638SPeter Gruber #define READ_AUDIO_STATUS(p)      READ_PORT_ULONG(p->audio_status)
188109a9638SPeter Gruber 
189109a9638SPeter Gruber #define SET_GRESET(p)     UMASK_AUDIO_CONTROL(p,0x0001)	/* global reset switch */
190109a9638SPeter Gruber #define UNSET_GRESET(p)   MASK_AUDIO_CONTROL(p,~0x0001)
191109a9638SPeter Gruber #define SET_AIE(p)        UMASK_AUDIO_CONTROL(p,0x0004)	/* interrupt enable */
192109a9638SPeter Gruber #define UNSET_AIE(p)      MASK_AUDIO_CONTROL(p,~0x0004)
193109a9638SPeter Gruber #define SET_AIACK(p)      UMASK_AUDIO_CONTROL(p,0x0008)	/* interrupt acknowledge */
194109a9638SPeter Gruber #define UNSET_AIACKT(p)   MASKAUDIO_CONTROL(p,~0x0008)
195109a9638SPeter Gruber #define SET_ECMDAE(p)     UMASK_AUDIO_CONTROL(p,0x0010)
196109a9638SPeter Gruber #define UNSET_ECMDAE(p)   MASK_AUDIO_CONTROL(p,~0x0010)
197109a9638SPeter Gruber #define SET_ECMDBE(p)     UMASK_AUDIO_CONTROL(p,0x0020)
198109a9638SPeter Gruber #define UNSET_ECMDBE(p)   MASK_AUDIO_CONTROL(p,~0x0020)
199109a9638SPeter Gruber #define SET_EDATAF(p)     UMASK_AUDIO_CONTROL(p,0x0040)
200109a9638SPeter Gruber #define UNSET_EDATAF(p)   MASK_AUDIO_CONTROL(p,~0x0040)
201109a9638SPeter Gruber #define SET_EDATBF(p)     UMASK_AUDIO_CONTROL(p,0x0080)
202109a9638SPeter Gruber #define UNSET_EDATBF(p)   MASK_AUDIO_CONTROL(p,~0x0080)
203109a9638SPeter Gruber #define SET_ESBIRQON(p)   UMASK_AUDIO_CONTROL(p,0x0100)
204109a9638SPeter Gruber #define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
205109a9638SPeter Gruber #define SET_EMPUIRQ(p)    UMASK_AUDIO_CONTROL(p,0x0200)
206109a9638SPeter Gruber #define UNSET_EMPUIRQ(p)  MASK_AUDIO_CONTROL(p,~0x0200)
207109a9638SPeter Gruber #define IS_CMDE(a)        (READ_PORT_ULONG(a->stat)&0x1)	/* cmd empty */
208109a9638SPeter Gruber #define IS_DATF(a)        (READ_PORT_ULONG(a->stat)&0x2)	/* data filled */
209109a9638SPeter Gruber #define IS_READY(p)       (READ_AUDIO_STATUS(p)&0x0001)
210109a9638SPeter Gruber #define IS_DLREADY(p)     (READ_AUDIO_STATUS(p)&0x0002)
211109a9638SPeter Gruber #define IS_DLERR(p)       (READ_AUDIO_STATUS(p)&0x0004)
212109a9638SPeter Gruber #define IS_GERR(p)        (READ_AUDIO_STATUS(p)&0x0008)	/* error ! */
213109a9638SPeter Gruber #define IS_CMDAEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0010)
214109a9638SPeter Gruber #define IS_CMDBEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0020)
215109a9638SPeter Gruber #define IS_DATAFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0040)
216109a9638SPeter Gruber #define IS_DATBFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0080)
217109a9638SPeter Gruber #define IS_EOBIRQ(p)      (READ_AUDIO_STATUS(p)&0x0100)	/* interrupt status */
218109a9638SPeter Gruber #define IS_EOSIRQ(p)      (READ_AUDIO_STATUS(p)&0x0200)
219109a9638SPeter Gruber #define IS_EOCIRQ(p)      (READ_AUDIO_STATUS(p)&0x0400)
220109a9638SPeter Gruber #define IS_UNSLIRQ(p)     (READ_AUDIO_STATUS(p)&0x0800)
221109a9638SPeter Gruber #define IS_SBIRQ(p)       (READ_AUDIO_STATUS(p)&0x1000)
222109a9638SPeter Gruber #define IS_MPUIRQ(p)      (READ_AUDIO_STATUS(p)&0x2000)
223109a9638SPeter Gruber 
224109a9638SPeter Gruber #define RESP 0x00000001		/* command flags */
225109a9638SPeter Gruber #define PARM 0x00000002
226109a9638SPeter Gruber #define CMDA 0x00000004
227109a9638SPeter Gruber #define CMDB 0x00000008
228109a9638SPeter Gruber #define NILL 0x00000000
229109a9638SPeter Gruber 
230109a9638SPeter Gruber #define LONG0(a)   ((u32)a)	/* shifts and masks */
231109a9638SPeter Gruber #define BYTE0(a)   (LONG0(a)&0xff)
232109a9638SPeter Gruber #define BYTE1(a)   (BYTE0(a)<<8)
233109a9638SPeter Gruber #define BYTE2(a)   (BYTE0(a)<<16)
234109a9638SPeter Gruber #define BYTE3(a)   (BYTE0(a)<<24)
235109a9638SPeter Gruber #define WORD0(a)   (LONG0(a)&0xffff)
236109a9638SPeter Gruber #define WORD1(a)   (WORD0(a)<<8)
237109a9638SPeter Gruber #define WORD2(a)   (WORD0(a)<<16)
238109a9638SPeter Gruber #define TRINIB0(a) (LONG0(a)&0xffffff)
239109a9638SPeter Gruber #define TRINIB1(a) (TRINIB0(a)<<8)
240109a9638SPeter Gruber 
241109a9638SPeter Gruber #define RET(a)     ((union cmdret *)(a))
242109a9638SPeter Gruber 
243109a9638SPeter Gruber #define SEND_GETV(p,b)             sendcmd(p,RESP,GETV,0,RET(b))	/* get version */
244109a9638SPeter Gruber #define SEND_GETC(p,b,c)           sendcmd(p,PARM|RESP,GETC,c,RET(b))
245109a9638SPeter Gruber #define SEND_GUNS(p,b)             sendcmd(p,RESP,GUNS,0,RET(b))
246109a9638SPeter Gruber #define SEND_SCID(p,b)             sendcmd(p,RESP,SCID,0,RET(b))
247109a9638SPeter Gruber #define SEND_RMEM(p,b,c,d)         sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))	/* memory access for firmware write */
248109a9638SPeter Gruber #define SEND_SMEM(p,b,c)           sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
249109a9638SPeter Gruber #define SEND_WMEM(p,b,c)           sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))	/* memory access for firmware write */
250109a9638SPeter Gruber #define SEND_SDTM(p,b,c)           sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))	/* memory access for firmware write */
251109a9638SPeter Gruber #define SEND_GOTO(p,b)             sendcmd(p,PARM,GOTO,LONG0(b),RET(0))	/* memory access for firmware write */
252109a9638SPeter Gruber #define SEND_SETDPLL(p)	           sendcmd(p,0,ARM_SETDPLL,0,RET(0))
253109a9638SPeter Gruber #define SEND_SSTR(p,b,c)           sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))	/* start stream */
254109a9638SPeter Gruber #define SEND_PSTR(p,b)             sendcmd(p,PARM,PSTR,BYTE3(b),RET(0))	/* pause stream */
255109a9638SPeter Gruber #define SEND_KSTR(p,b)             sendcmd(p,PARM,KSTR,BYTE3(b),RET(0))	/* stop stream */
256109a9638SPeter Gruber #define SEND_KDMA(p)               sendcmd(p,0,KDMA,0,RET(0))	/* stop all dma */
257109a9638SPeter Gruber #define SEND_GPOS(p,b,c,d)         sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))	/* get position in dma */
258109a9638SPeter Gruber #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 */
259109a9638SPeter Gruber #define SEND_GSTS(p,b,c,d)         sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
260109a9638SPeter Gruber #define SEND_NGPOS(p,b,c,d)        sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
261109a9638SPeter Gruber #define SEND_PSEL(p,b,c)           sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))	/* activate lbus path */
262109a9638SPeter Gruber #define SEND_PCLR(p,b,c)           sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))	/* deactivate lbus path */
263109a9638SPeter Gruber #define SEND_PLST(p,b)             sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
264109a9638SPeter Gruber #define SEND_RSSV(p,b,c,d)         sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
265109a9638SPeter Gruber #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 */
266109a9638SPeter Gruber #define SEND_SSRC(p,b,c,d,e)       sendcmd(p,PARM,SSRC|BYTE1(b)|WORD2(c),WORD0(d)|WORD2(e),RET(0))	/* configure source */
267109a9638SPeter Gruber #define SEND_SLST(p,b)             sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
268109a9638SPeter Gruber #define SEND_RSRC(p,b,c)           sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))	/* read source config */
269109a9638SPeter Gruber #define SEND_SSRB(p,b,c)           sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
270109a9638SPeter Gruber #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 */
271109a9638SPeter Gruber #define SEND_RDGV(p,b,c,d)         sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d))	/* read digital mixer */
272109a9638SPeter Gruber #define SEND_DLST(p,b)             sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
273109a9638SPeter Gruber #define SEND_SACR(p,b,c)           sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))	/* set AC97 register */
274109a9638SPeter Gruber #define SEND_RACR(p,b,c)           sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))	/* get AC97 register */
275109a9638SPeter Gruber #define SEND_ALST(p,b)             sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
276109a9638SPeter Gruber #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))
277109a9638SPeter Gruber #define SEND_RXAC(p,b,c,d)         sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
278109a9638SPeter Gruber #define SEND_SI2S(p,b)             sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
279109a9638SPeter Gruber 
280109a9638SPeter Gruber #define EOB_STATUS         0x80000000	/* status flags : block boundary */
281109a9638SPeter Gruber #define EOS_STATUS         0x40000000	/*              : stoppped */
282109a9638SPeter Gruber #define EOC_STATUS         0x20000000	/*              : stream end */
283109a9638SPeter Gruber #define ERR_STATUS         0x10000000
284109a9638SPeter Gruber #define EMPTY_STATUS       0x08000000
285109a9638SPeter Gruber 
286109a9638SPeter Gruber #define IEOB_ENABLE        0x1	/* enable interrupts for status notification above */
287109a9638SPeter Gruber #define IEOS_ENABLE        0x2
288109a9638SPeter Gruber #define IEOC_ENABLE        0x4
289109a9638SPeter Gruber #define RDONCE             0x8
290109a9638SPeter Gruber #define DESC_MAX_MASK      0xff
291109a9638SPeter Gruber 
292109a9638SPeter Gruber #define ST_PLAY  0x1		/* stream states */
293109a9638SPeter Gruber #define ST_STOP  0x2
294109a9638SPeter Gruber #define ST_PAUSE 0x4
295109a9638SPeter Gruber 
296109a9638SPeter Gruber #define I2S_INTDEC     3	/* config for I2S link */
297109a9638SPeter Gruber #define I2S_MERGER     0
298109a9638SPeter Gruber #define I2S_SPLITTER   0
299109a9638SPeter Gruber #define I2S_MIXER      7
300109a9638SPeter Gruber #define I2S_RATE       44100
301109a9638SPeter Gruber 
302109a9638SPeter Gruber #define MODEM_INTDEC   4	/* config for modem link */
303109a9638SPeter Gruber #define MODEM_MERGER   3
304109a9638SPeter Gruber #define MODEM_SPLITTER 0
305109a9638SPeter Gruber #define MODEM_MIXER    11
306109a9638SPeter Gruber 
307109a9638SPeter Gruber #define FM_INTDEC      3	/* config for FM/OPL3 link */
308109a9638SPeter Gruber #define FM_MERGER      0
309109a9638SPeter Gruber #define FM_SPLITTER    0
310109a9638SPeter Gruber #define FM_MIXER       9
311109a9638SPeter Gruber 
312109a9638SPeter Gruber #define SPLIT_PATH  0x80	/* path splitting flag */
313109a9638SPeter Gruber 
314109a9638SPeter Gruber enum FIRMWARE {
315109a9638SPeter Gruber 	DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
316109a9638SPeter Gruber 	EXT_LIN_ADDR_REC,
317109a9638SPeter Gruber };
318109a9638SPeter Gruber 
319109a9638SPeter Gruber enum CMDS {
320109a9638SPeter Gruber 	GETV = 0x00, GETC, GUNS, SCID, RMEM =
321109a9638SPeter Gruber 	    0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
322109a9638SPeter Gruber 	    0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
323109a9638SPeter Gruber 	    0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
324109a9638SPeter Gruber 	    0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
325109a9638SPeter Gruber 	    0x70, ARM_SETDPLL = 0x72,
326109a9638SPeter Gruber };
327109a9638SPeter Gruber 
328109a9638SPeter Gruber enum E1SOURCE {
329109a9638SPeter Gruber 	ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
330109a9638SPeter Gruber 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
331109a9638SPeter Gruber 	ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
332109a9638SPeter Gruber 	ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
333109a9638SPeter Gruber 	INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
334109a9638SPeter Gruber 	INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
335109a9638SPeter Gruber 	INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
336109a9638SPeter Gruber 	DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
337109a9638SPeter Gruber 	ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
338109a9638SPeter Gruber 	DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
339109a9638SPeter Gruber 	GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
340109a9638SPeter Gruber 	SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
341109a9638SPeter Gruber 	SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
342109a9638SPeter Gruber 	MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
343109a9638SPeter Gruber };
344109a9638SPeter Gruber 
345109a9638SPeter Gruber enum E2SINK {
346109a9638SPeter Gruber 	LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
347109a9638SPeter Gruber 	LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
348109a9638SPeter Gruber 	INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
349109a9638SPeter Gruber 	INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
350109a9638SPeter Gruber 	DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
351109a9638SPeter Gruber 	PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
352109a9638SPeter Gruber 	DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
353109a9638SPeter Gruber 	DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
354109a9638SPeter Gruber 	DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
355109a9638SPeter Gruber 	DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
356109a9638SPeter Gruber 	GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
357109a9638SPeter Gruber 	SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
358109a9638SPeter Gruber 	MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
359109a9638SPeter Gruber 	MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
360109a9638SPeter Gruber };
361109a9638SPeter Gruber 
362109a9638SPeter Gruber enum LBUS_SINK {
363109a9638SPeter Gruber 	LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
364109a9638SPeter Gruber 	LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
365109a9638SPeter Gruber 	LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
366109a9638SPeter Gruber };
367109a9638SPeter Gruber 
368109a9638SPeter Gruber enum RT_CHANNEL_IDS {
369109a9638SPeter Gruber 	M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
370109a9638SPeter Gruber 	DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
371109a9638SPeter Gruber 	M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
372109a9638SPeter Gruber 	CHANNEL_ID_COUNTER
373109a9638SPeter Gruber };
374109a9638SPeter Gruber 
375109a9638SPeter Gruber enum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
376109a9638SPeter Gruber 
377109a9638SPeter Gruber struct lbuspath {
378109a9638SPeter Gruber 	unsigned char *noconv;
379109a9638SPeter Gruber 	unsigned char *stereo;
380109a9638SPeter Gruber 	unsigned char *mono;
381109a9638SPeter Gruber };
382109a9638SPeter Gruber 
383109a9638SPeter Gruber struct cmdport {
384109a9638SPeter Gruber 	u32 data1;		/* cmd,param */
385109a9638SPeter Gruber 	u32 data2;		/* param */
386109a9638SPeter Gruber 	u32 stat;		/* status */
387109a9638SPeter Gruber 	u32 pad[5];
388109a9638SPeter Gruber };
389109a9638SPeter Gruber 
390109a9638SPeter Gruber struct riptideport {
391109a9638SPeter Gruber 	u32 audio_control;	/* status registers */
392109a9638SPeter Gruber 	u32 audio_status;
393109a9638SPeter Gruber 	u32 pad[2];
394109a9638SPeter Gruber 	struct cmdport port[2];	/* command ports */
395109a9638SPeter Gruber };
396109a9638SPeter Gruber 
397109a9638SPeter Gruber struct cmdif {
398109a9638SPeter Gruber 	struct riptideport *hwport;
399109a9638SPeter Gruber 	spinlock_t lock;
400109a9638SPeter Gruber 	unsigned int cmdcnt;	/* cmd statistics */
401109a9638SPeter Gruber 	unsigned int cmdtime;
402109a9638SPeter Gruber 	unsigned int cmdtimemax;
403109a9638SPeter Gruber 	unsigned int cmdtimemin;
404109a9638SPeter Gruber 	unsigned int errcnt;
405109a9638SPeter Gruber 	int is_reset;
406109a9638SPeter Gruber };
407109a9638SPeter Gruber 
408109a9638SPeter Gruber struct riptide_firmware {
409109a9638SPeter Gruber 	u16 ASIC;
410109a9638SPeter Gruber 	u16 CODEC;
411109a9638SPeter Gruber 	u16 AUXDSP;
412109a9638SPeter Gruber 	u16 PROG;
413109a9638SPeter Gruber };
414109a9638SPeter Gruber 
415109a9638SPeter Gruber union cmdret {
416109a9638SPeter Gruber 	u8 retbytes[8];
417109a9638SPeter Gruber 	u16 retwords[4];
418109a9638SPeter Gruber 	u32 retlongs[2];
419109a9638SPeter Gruber };
420109a9638SPeter Gruber 
421109a9638SPeter Gruber union firmware_version {
422109a9638SPeter Gruber 	union cmdret ret;
423109a9638SPeter Gruber 	struct riptide_firmware firmware;
424109a9638SPeter Gruber };
425109a9638SPeter Gruber 
426109a9638SPeter Gruber #define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
427109a9638SPeter Gruber 
428109a9638SPeter Gruber #define PLAYBACK_SUBSTREAMS 3
429109a9638SPeter Gruber struct snd_riptide {
430109a9638SPeter Gruber 	struct snd_card *card;
431109a9638SPeter Gruber 	struct pci_dev *pci;
432109a9638SPeter Gruber 	const struct firmware *fw_entry;
433109a9638SPeter Gruber 
434109a9638SPeter Gruber 	struct cmdif *cif;
435109a9638SPeter Gruber 
436109a9638SPeter Gruber 	struct snd_pcm *pcm;
437109a9638SPeter Gruber 	struct snd_pcm *pcm_i2s;
438109a9638SPeter Gruber 	struct snd_rawmidi *rmidi;
439109a9638SPeter Gruber 	struct snd_opl3 *opl3;
440109a9638SPeter Gruber 	struct snd_ac97 *ac97;
441109a9638SPeter Gruber 	struct snd_ac97_bus *ac97_bus;
442109a9638SPeter Gruber 
443109a9638SPeter Gruber 	struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
444109a9638SPeter Gruber 	struct snd_pcm_substream *capture_substream;
445109a9638SPeter Gruber 
446109a9638SPeter Gruber 	int openstreams;
447109a9638SPeter Gruber 
448109a9638SPeter Gruber 	int irq;
449109a9638SPeter Gruber 	unsigned long port;
450109a9638SPeter Gruber 	unsigned short mpuaddr;
451109a9638SPeter Gruber 	unsigned short opladdr;
452109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
453109a9638SPeter Gruber 	unsigned short gameaddr;
454109a9638SPeter Gruber #endif
455109a9638SPeter Gruber 	struct resource *res_port;
456109a9638SPeter Gruber 
457109a9638SPeter Gruber 	unsigned short device_id;
458109a9638SPeter Gruber 
459109a9638SPeter Gruber 	union firmware_version firmware;
460109a9638SPeter Gruber 
461109a9638SPeter Gruber 	spinlock_t lock;
462109a9638SPeter Gruber 	struct tasklet_struct riptide_tq;
463109a9638SPeter Gruber 	struct snd_info_entry *proc_entry;
464109a9638SPeter Gruber 
465109a9638SPeter Gruber 	unsigned long received_irqs;
466109a9638SPeter Gruber 	unsigned long handled_irqs;
467109a9638SPeter Gruber #ifdef CONFIG_PM
468109a9638SPeter Gruber 	int in_suspend;
469109a9638SPeter Gruber #endif
470109a9638SPeter Gruber };
471109a9638SPeter Gruber 
472109a9638SPeter Gruber struct sgd {			/* scatter gather desriptor */
473109a9638SPeter Gruber 	u32 dwNextLink;
474109a9638SPeter Gruber 	u32 dwSegPtrPhys;
475109a9638SPeter Gruber 	u32 dwSegLen;
476109a9638SPeter Gruber 	u32 dwStat_Ctl;
477109a9638SPeter Gruber };
478109a9638SPeter Gruber 
479109a9638SPeter Gruber struct pcmhw {			/* pcm descriptor */
480109a9638SPeter Gruber 	struct lbuspath paths;
481109a9638SPeter Gruber 	unsigned char *lbuspath;
482109a9638SPeter Gruber 	unsigned char source;
483109a9638SPeter Gruber 	unsigned char intdec[2];
484109a9638SPeter Gruber 	unsigned char mixer;
485109a9638SPeter Gruber 	unsigned char id;
486109a9638SPeter Gruber 	unsigned char state;
487109a9638SPeter Gruber 	unsigned int rate;
488109a9638SPeter Gruber 	unsigned int channels;
489109a9638SPeter Gruber 	snd_pcm_format_t format;
490109a9638SPeter Gruber 	struct snd_dma_buffer sgdlist;
491109a9638SPeter Gruber 	struct sgd *sgdbuf;
492109a9638SPeter Gruber 	unsigned int size;
493109a9638SPeter Gruber 	unsigned int pages;
494109a9638SPeter Gruber 	unsigned int oldpos;
495109a9638SPeter Gruber 	unsigned int pointer;
496109a9638SPeter Gruber };
497109a9638SPeter Gruber 
498109a9638SPeter Gruber #define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
499109a9638SPeter Gruber 
500109a9638SPeter Gruber static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
501109a9638SPeter Gruber 		   union cmdret *ret);
502109a9638SPeter Gruber static int getsourcesink(struct cmdif *cif, unsigned char source,
503109a9638SPeter Gruber 			 unsigned char sink, unsigned char *a,
504109a9638SPeter Gruber 			 unsigned char *b);
505109a9638SPeter Gruber static int snd_riptide_initialize(struct snd_riptide *chip);
506109a9638SPeter Gruber static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
507109a9638SPeter Gruber 
508109a9638SPeter Gruber /*
509109a9638SPeter Gruber  */
510109a9638SPeter Gruber 
511cebe41d4SAlexey Dobriyan static DEFINE_PCI_DEVICE_TABLE(snd_riptide_ids) = {
512a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4310) },
513a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4320) },
514a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4330) },
515a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4340) },
516109a9638SPeter Gruber 	{0,},
517109a9638SPeter Gruber };
518109a9638SPeter Gruber 
519109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
520cebe41d4SAlexey Dobriyan static DEFINE_PCI_DEVICE_TABLE(snd_riptide_joystick_ids) = {
521a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4312) },
522a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4322) },
523a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4332) },
524a693a26fSTakashi Iwai 	{ PCI_DEVICE(0x127a, 0x4342) },
525109a9638SPeter Gruber 	{0,},
526109a9638SPeter Gruber };
527109a9638SPeter Gruber #endif
528109a9638SPeter Gruber 
529109a9638SPeter Gruber MODULE_DEVICE_TABLE(pci, snd_riptide_ids);
530109a9638SPeter Gruber 
531109a9638SPeter Gruber /*
532109a9638SPeter Gruber  */
533109a9638SPeter Gruber 
534109a9638SPeter Gruber static unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
535109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
536109a9638SPeter Gruber 								     LS_NONE2},
537109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
538109a9638SPeter Gruber 								     LS_NONE2},
539109a9638SPeter Gruber 	{INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
540109a9638SPeter Gruber 	{INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
541109a9638SPeter Gruber 	{INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
542109a9638SPeter Gruber 	{INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
543109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
544109a9638SPeter Gruber 	{INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
545109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
546109a9638SPeter Gruber 	{INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
547109a9638SPeter Gruber 					      LS_SRC_INTERPOLATORM},
548109a9638SPeter Gruber 	{DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
549109a9638SPeter Gruber 	{DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
550109a9638SPeter Gruber 	{DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
551109a9638SPeter Gruber 								LS_NONE2},
552109a9638SPeter Gruber 	{NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
553109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
554109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
555109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
556109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
557109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
558109a9638SPeter Gruber 	{DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
559109a9638SPeter Gruber 	{GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
560109a9638SPeter Gruber 						  LS_MIXER_GAIN_FUNCTION},
561109a9638SPeter Gruber 	{GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
562109a9638SPeter Gruber 						  LS_MIXER_GAIN_FUNCTION},
563109a9638SPeter Gruber 	{GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
564109a9638SPeter Gruber 	{SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
565109a9638SPeter Gruber 	{SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
566109a9638SPeter Gruber 	{MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
567109a9638SPeter Gruber 	{MERGER1_OUT, LS_SRC_MERGER},
568109a9638SPeter Gruber 	{MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
569109a9638SPeter Gruber 	{MERGER2_OUT, LS_SRC_MERGER},
570109a9638SPeter Gruber 	{MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
571109a9638SPeter Gruber 								     LS_NONE2},
572109a9638SPeter Gruber };
573109a9638SPeter Gruber 
574109a9638SPeter Gruber static unsigned char lbus_play_opl3[] = {
575109a9638SPeter Gruber 	DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
576109a9638SPeter Gruber };
577109a9638SPeter Gruber static unsigned char lbus_play_modem[] = {
578109a9638SPeter Gruber 	DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
579109a9638SPeter Gruber };
580109a9638SPeter Gruber static unsigned char lbus_play_i2s[] = {
581109a9638SPeter Gruber 	INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
582109a9638SPeter Gruber };
583109a9638SPeter Gruber static unsigned char lbus_play_out[] = {
584109a9638SPeter Gruber 	PDAC2ACLNK, 0xff
585109a9638SPeter Gruber };
586109a9638SPeter Gruber static unsigned char lbus_play_outhp[] = {
587109a9638SPeter Gruber 	HNDSPK2ACLNK, 0xff
588109a9638SPeter Gruber };
589109a9638SPeter Gruber static unsigned char lbus_play_noconv1[] = {
590109a9638SPeter Gruber 	DIGITAL_MIXER_IN0, 0xff
591109a9638SPeter Gruber };
592109a9638SPeter Gruber static unsigned char lbus_play_stereo1[] = {
593109a9638SPeter Gruber 	INTER0_IN, DIGITAL_MIXER_IN0, 0xff
594109a9638SPeter Gruber };
595109a9638SPeter Gruber static unsigned char lbus_play_mono1[] = {
596109a9638SPeter Gruber 	INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
597109a9638SPeter Gruber };
598109a9638SPeter Gruber static unsigned char lbus_play_noconv2[] = {
599109a9638SPeter Gruber 	DIGITAL_MIXER_IN1, 0xff
600109a9638SPeter Gruber };
601109a9638SPeter Gruber static unsigned char lbus_play_stereo2[] = {
602109a9638SPeter Gruber 	INTER1_IN, DIGITAL_MIXER_IN1, 0xff
603109a9638SPeter Gruber };
604109a9638SPeter Gruber static unsigned char lbus_play_mono2[] = {
605109a9638SPeter Gruber 	INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
606109a9638SPeter Gruber };
607109a9638SPeter Gruber static unsigned char lbus_play_noconv3[] = {
608109a9638SPeter Gruber 	DIGITAL_MIXER_IN2, 0xff
609109a9638SPeter Gruber };
610109a9638SPeter Gruber static unsigned char lbus_play_stereo3[] = {
611109a9638SPeter Gruber 	INTER2_IN, DIGITAL_MIXER_IN2, 0xff
612109a9638SPeter Gruber };
613109a9638SPeter Gruber static unsigned char lbus_play_mono3[] = {
614109a9638SPeter Gruber 	INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
615109a9638SPeter Gruber };
616109a9638SPeter Gruber static unsigned char lbus_rec_noconv1[] = {
617109a9638SPeter Gruber 	LBUS2ARM_FIFO5, 0xff
618109a9638SPeter Gruber };
619109a9638SPeter Gruber static unsigned char lbus_rec_stereo1[] = {
620109a9638SPeter Gruber 	DECIM0_IN, LBUS2ARM_FIFO5, 0xff
621109a9638SPeter Gruber };
622109a9638SPeter Gruber static unsigned char lbus_rec_mono1[] = {
623109a9638SPeter Gruber 	DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
624109a9638SPeter Gruber };
625109a9638SPeter Gruber 
626109a9638SPeter Gruber static unsigned char play_ids[] = { 4, 1, 2, };
627109a9638SPeter Gruber static unsigned char play_sources[] = {
628109a9638SPeter Gruber 	ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
629109a9638SPeter Gruber };
630109a9638SPeter Gruber static struct lbuspath lbus_play_paths[] = {
631109a9638SPeter Gruber 	{
632109a9638SPeter Gruber 	 .noconv = lbus_play_noconv1,
633109a9638SPeter Gruber 	 .stereo = lbus_play_stereo1,
634109a9638SPeter Gruber 	 .mono = lbus_play_mono1,
635109a9638SPeter Gruber 	 },
636109a9638SPeter Gruber 	{
637109a9638SPeter Gruber 	 .noconv = lbus_play_noconv2,
638109a9638SPeter Gruber 	 .stereo = lbus_play_stereo2,
639109a9638SPeter Gruber 	 .mono = lbus_play_mono2,
640109a9638SPeter Gruber 	 },
641109a9638SPeter Gruber 	{
642109a9638SPeter Gruber 	 .noconv = lbus_play_noconv3,
643109a9638SPeter Gruber 	 .stereo = lbus_play_stereo3,
644109a9638SPeter Gruber 	 .mono = lbus_play_mono3,
645109a9638SPeter Gruber 	 },
646109a9638SPeter Gruber };
647109a9638SPeter Gruber static struct lbuspath lbus_rec_path = {
648109a9638SPeter Gruber 	.noconv = lbus_rec_noconv1,
649109a9638SPeter Gruber 	.stereo = lbus_rec_stereo1,
650109a9638SPeter Gruber 	.mono = lbus_rec_mono1,
651109a9638SPeter Gruber };
652109a9638SPeter Gruber 
653109a9638SPeter Gruber #define FIRMWARE_VERSIONS 1
654109a9638SPeter Gruber static union firmware_version firmware_versions[] = {
655109a9638SPeter Gruber 	{
656adf75dcaSClemens Ladisch 		.firmware = {
657adf75dcaSClemens Ladisch 			.ASIC = 3,
658adf75dcaSClemens Ladisch 			.CODEC = 2,
659adf75dcaSClemens Ladisch 			.AUXDSP = 3,
660adf75dcaSClemens Ladisch 			.PROG = 773,
661adf75dcaSClemens Ladisch 		},
662109a9638SPeter Gruber 	},
663109a9638SPeter Gruber };
664109a9638SPeter Gruber 
66593a9c901SDavid Woodhouse static u32 atoh(const unsigned char *in, unsigned int len)
666109a9638SPeter Gruber {
667109a9638SPeter Gruber 	u32 sum = 0;
668109a9638SPeter Gruber 	unsigned int mult = 1;
669109a9638SPeter Gruber 	unsigned char c;
670109a9638SPeter Gruber 
671109a9638SPeter Gruber 	while (len) {
672c9ff921aSAndy Shevchenko 		int value;
673c9ff921aSAndy Shevchenko 
674109a9638SPeter Gruber 		c = in[len - 1];
675c9ff921aSAndy Shevchenko 		value = hex_to_bin(c);
676c9ff921aSAndy Shevchenko 		if (value >= 0)
677c9ff921aSAndy Shevchenko 			sum += mult * value;
678109a9638SPeter Gruber 		mult *= 16;
679109a9638SPeter Gruber 		--len;
680109a9638SPeter Gruber 	}
681109a9638SPeter Gruber 	return sum;
682109a9638SPeter Gruber }
683109a9638SPeter Gruber 
68493a9c901SDavid Woodhouse static int senddata(struct cmdif *cif, const unsigned char *in, u32 offset)
685109a9638SPeter Gruber {
686109a9638SPeter Gruber 	u32 addr;
687109a9638SPeter Gruber 	u32 data;
688109a9638SPeter Gruber 	u32 i;
68993a9c901SDavid Woodhouse 	const unsigned char *p;
690109a9638SPeter Gruber 
691109a9638SPeter Gruber 	i = atoh(&in[1], 2);
692109a9638SPeter Gruber 	addr = offset + atoh(&in[3], 4);
693109a9638SPeter Gruber 	if (SEND_SMEM(cif, 0, addr) != 0)
694109a9638SPeter Gruber 		return -EACCES;
695109a9638SPeter Gruber 	p = in + 9;
696109a9638SPeter Gruber 	while (i) {
697109a9638SPeter Gruber 		data = atoh(p, 8);
698109a9638SPeter Gruber 		if (SEND_WMEM(cif, 2,
699109a9638SPeter Gruber 			      ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
700109a9638SPeter Gruber 							    >> 4)))
701109a9638SPeter Gruber 			return -EACCES;
702109a9638SPeter Gruber 		i -= 4;
703109a9638SPeter Gruber 		p += 8;
704109a9638SPeter Gruber 	}
705109a9638SPeter Gruber 	return 0;
706109a9638SPeter Gruber }
707109a9638SPeter Gruber 
70893a9c901SDavid Woodhouse static int loadfirmware(struct cmdif *cif, const unsigned char *img,
709109a9638SPeter Gruber 			unsigned int size)
710109a9638SPeter Gruber {
71193a9c901SDavid Woodhouse 	const unsigned char *in;
712109a9638SPeter Gruber 	u32 laddr, saddr, t, val;
713109a9638SPeter Gruber 	int err = 0;
714109a9638SPeter Gruber 
715109a9638SPeter Gruber 	laddr = saddr = 0;
716109a9638SPeter Gruber 	while (size > 0 && err == 0) {
717109a9638SPeter Gruber 		in = img;
718109a9638SPeter Gruber 		if (in[0] == ':') {
719109a9638SPeter Gruber 			t = atoh(&in[7], 2);
720109a9638SPeter Gruber 			switch (t) {
721109a9638SPeter Gruber 			case DATA_REC:
722109a9638SPeter Gruber 				err = senddata(cif, in, laddr + saddr);
723109a9638SPeter Gruber 				break;
724109a9638SPeter Gruber 			case EXT_SEG_ADDR_REC:
725109a9638SPeter Gruber 				saddr = atoh(&in[9], 4) << 4;
726109a9638SPeter Gruber 				break;
727109a9638SPeter Gruber 			case EXT_LIN_ADDR_REC:
728109a9638SPeter Gruber 				laddr = atoh(&in[9], 4) << 16;
729109a9638SPeter Gruber 				break;
730109a9638SPeter Gruber 			case EXT_GOTO_CMD_REC:
731109a9638SPeter Gruber 				val = atoh(&in[9], 8);
732109a9638SPeter Gruber 				if (SEND_GOTO(cif, val) != 0)
733109a9638SPeter Gruber 					err = -EACCES;
734109a9638SPeter Gruber 				break;
735109a9638SPeter Gruber 			case EXT_END_OF_FILE:
736109a9638SPeter Gruber 				size = 0;
737109a9638SPeter Gruber 				break;
738109a9638SPeter Gruber 			default:
739109a9638SPeter Gruber 				break;
740109a9638SPeter Gruber 			}
741109a9638SPeter Gruber 			while (size > 0) {
742109a9638SPeter Gruber 				size--;
743109a9638SPeter Gruber 				if (*img++ == '\n')
744109a9638SPeter Gruber 					break;
745109a9638SPeter Gruber 			}
746109a9638SPeter Gruber 		}
747109a9638SPeter Gruber 	}
748109a9638SPeter Gruber 	snd_printdd("load firmware return %d\n", err);
749109a9638SPeter Gruber 	return err;
750109a9638SPeter Gruber }
751109a9638SPeter Gruber 
752109a9638SPeter Gruber static void
753109a9638SPeter Gruber alloclbuspath(struct cmdif *cif, unsigned char source,
754109a9638SPeter Gruber 	      unsigned char *path, unsigned char *mixer, unsigned char *s)
755109a9638SPeter Gruber {
756109a9638SPeter Gruber 	while (*path != 0xff) {
757109a9638SPeter Gruber 		unsigned char sink, type;
758109a9638SPeter Gruber 
759109a9638SPeter Gruber 		sink = *path & (~SPLIT_PATH);
760109a9638SPeter Gruber 		if (sink != E2SINK_MAX) {
761109a9638SPeter Gruber 			snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
762109a9638SPeter Gruber 			SEND_PSEL(cif, source, sink);
763109a9638SPeter Gruber 			source = lbusin2out[sink][0];
764109a9638SPeter Gruber 			type = lbusin2out[sink][1];
765109a9638SPeter Gruber 			if (type == LS_MIXER_IN) {
766109a9638SPeter Gruber 				if (mixer)
767109a9638SPeter Gruber 					*mixer = sink - DIGITAL_MIXER_IN0;
768109a9638SPeter Gruber 			}
769109a9638SPeter Gruber 			if (type == LS_SRC_DECIMATORM ||
770109a9638SPeter Gruber 			    type == LS_SRC_DECIMATOR ||
771109a9638SPeter Gruber 			    type == LS_SRC_INTERPOLATORM ||
772109a9638SPeter Gruber 			    type == LS_SRC_INTERPOLATOR) {
773109a9638SPeter Gruber 				if (s) {
774109a9638SPeter Gruber 					if (s[0] != 0xff)
775109a9638SPeter Gruber 						s[1] = sink;
776109a9638SPeter Gruber 					else
777109a9638SPeter Gruber 						s[0] = sink;
778109a9638SPeter Gruber 				}
779109a9638SPeter Gruber 			}
780109a9638SPeter Gruber 		}
781109a9638SPeter Gruber 		if (*path++ & SPLIT_PATH) {
782109a9638SPeter Gruber 			unsigned char *npath = path;
783109a9638SPeter Gruber 
784109a9638SPeter Gruber 			while (*npath != 0xff)
785109a9638SPeter Gruber 				npath++;
786109a9638SPeter Gruber 			alloclbuspath(cif, source + 1, ++npath, mixer, s);
787109a9638SPeter Gruber 		}
788109a9638SPeter Gruber 	}
789109a9638SPeter Gruber }
790109a9638SPeter Gruber 
791109a9638SPeter Gruber static void
792109a9638SPeter Gruber freelbuspath(struct cmdif *cif, unsigned char source, unsigned char *path)
793109a9638SPeter Gruber {
794109a9638SPeter Gruber 	while (*path != 0xff) {
795109a9638SPeter Gruber 		unsigned char sink;
796109a9638SPeter Gruber 
797109a9638SPeter Gruber 		sink = *path & (~SPLIT_PATH);
798109a9638SPeter Gruber 		if (sink != E2SINK_MAX) {
799109a9638SPeter Gruber 			snd_printdd("free path 0x%x->0x%x\n", source, sink);
800109a9638SPeter Gruber 			SEND_PCLR(cif, source, sink);
801109a9638SPeter Gruber 			source = lbusin2out[sink][0];
802109a9638SPeter Gruber 		}
803109a9638SPeter Gruber 		if (*path++ & SPLIT_PATH) {
804109a9638SPeter Gruber 			unsigned char *npath = path;
805109a9638SPeter Gruber 
806109a9638SPeter Gruber 			while (*npath != 0xff)
807109a9638SPeter Gruber 				npath++;
808109a9638SPeter Gruber 			freelbuspath(cif, source + 1, ++npath);
809109a9638SPeter Gruber 		}
810109a9638SPeter Gruber 	}
811109a9638SPeter Gruber }
812109a9638SPeter Gruber 
813109a9638SPeter Gruber static int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
814109a9638SPeter Gruber {
815109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
816109a9638SPeter Gruber 	unsigned int i = MAX_WRITE_RETRY;
817109a9638SPeter Gruber 	int flag = 1;
818109a9638SPeter Gruber 
819109a9638SPeter Gruber 	SEND_RMEM(cif, 0x02, addr, &rptr);
820109a9638SPeter Gruber 	rptr.retlongs[0] &= (~mask);
821109a9638SPeter Gruber 
822109a9638SPeter Gruber 	while (--i) {
823109a9638SPeter Gruber 		SEND_SMEM(cif, 0x01, addr);
824109a9638SPeter Gruber 		SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
825109a9638SPeter Gruber 		SEND_RMEM(cif, 0x02, addr, &rptr);
826109a9638SPeter Gruber 		if ((rptr.retlongs[0] & data) == data) {
827109a9638SPeter Gruber 			flag = 0;
828109a9638SPeter Gruber 			break;
829109a9638SPeter Gruber 		} else
830109a9638SPeter Gruber 			rptr.retlongs[0] &= ~mask;
831109a9638SPeter Gruber 	}
832109a9638SPeter Gruber 	snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
833109a9638SPeter Gruber 		    flag);
834109a9638SPeter Gruber 	return flag;
835109a9638SPeter Gruber }
836109a9638SPeter Gruber 
837109a9638SPeter Gruber static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
838109a9638SPeter Gruber 		   union cmdret *ret)
839109a9638SPeter Gruber {
840109a9638SPeter Gruber 	int i, j;
841109a9638SPeter Gruber 	int err;
842109a9638SPeter Gruber 	unsigned int time = 0;
843109a9638SPeter Gruber 	unsigned long irqflags;
844109a9638SPeter Gruber 	struct riptideport *hwport;
845109a9638SPeter Gruber 	struct cmdport *cmdport = NULL;
846109a9638SPeter Gruber 
847da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
848da3cec35STakashi Iwai 		return -EINVAL;
849109a9638SPeter Gruber 
850109a9638SPeter Gruber 	hwport = cif->hwport;
851109a9638SPeter Gruber 	if (cif->errcnt > MAX_ERROR_COUNT) {
852109a9638SPeter Gruber 		if (cif->is_reset) {
853109a9638SPeter Gruber 			snd_printk(KERN_ERR
854109a9638SPeter Gruber 				   "Riptide: Too many failed cmds, reinitializing\n");
855109a9638SPeter Gruber 			if (riptide_reset(cif, NULL) == 0) {
856109a9638SPeter Gruber 				cif->errcnt = 0;
857109a9638SPeter Gruber 				return -EIO;
858109a9638SPeter Gruber 			}
859109a9638SPeter Gruber 		}
860109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
861109a9638SPeter Gruber 		return -EINVAL;
862109a9638SPeter Gruber 	}
863109a9638SPeter Gruber 	if (ret) {
864109a9638SPeter Gruber 		ret->retlongs[0] = 0;
865109a9638SPeter Gruber 		ret->retlongs[1] = 0;
866109a9638SPeter Gruber 	}
867109a9638SPeter Gruber 	i = 0;
868109a9638SPeter Gruber 	spin_lock_irqsave(&cif->lock, irqflags);
869109a9638SPeter Gruber 	while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
870109a9638SPeter Gruber 		udelay(10);
8711b1cc7f2SRoel Kluin 	if (i > CMDIF_TIMEOUT) {
872109a9638SPeter Gruber 		err = -EBUSY;
873109a9638SPeter Gruber 		goto errout;
874109a9638SPeter Gruber 	}
875109a9638SPeter Gruber 
876109a9638SPeter Gruber 	err = 0;
877109a9638SPeter Gruber 	for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
878109a9638SPeter Gruber 		cmdport = &(hwport->port[j % 2]);
879109a9638SPeter Gruber 		if (IS_DATF(cmdport)) {	/* free pending data */
880109a9638SPeter Gruber 			READ_PORT_ULONG(cmdport->data1);
881109a9638SPeter Gruber 			READ_PORT_ULONG(cmdport->data2);
882109a9638SPeter Gruber 		}
883109a9638SPeter Gruber 		if (IS_CMDE(cmdport)) {
884109a9638SPeter Gruber 			if (flags & PARM)	/* put data */
885109a9638SPeter Gruber 				WRITE_PORT_ULONG(cmdport->data2, parm);
886109a9638SPeter Gruber 			WRITE_PORT_ULONG(cmdport->data1, cmd);	/* write cmd */
887109a9638SPeter Gruber 			if ((flags & RESP) && ret) {
888109a9638SPeter Gruber 				while (!IS_DATF(cmdport) &&
8891b1cc7f2SRoel Kluin 				       time < CMDIF_TIMEOUT) {
890109a9638SPeter Gruber 					udelay(10);
8911b1cc7f2SRoel Kluin 					time++;
8921b1cc7f2SRoel Kluin 				}
893109a9638SPeter Gruber 				if (time < CMDIF_TIMEOUT) {	/* read response */
894109a9638SPeter Gruber 					ret->retlongs[0] =
895109a9638SPeter Gruber 					    READ_PORT_ULONG(cmdport->data1);
896109a9638SPeter Gruber 					ret->retlongs[1] =
897109a9638SPeter Gruber 					    READ_PORT_ULONG(cmdport->data2);
898109a9638SPeter Gruber 				} else {
899109a9638SPeter Gruber 					err = -ENOSYS;
900109a9638SPeter Gruber 					goto errout;
901109a9638SPeter Gruber 				}
902109a9638SPeter Gruber 			}
903109a9638SPeter Gruber 			break;
904109a9638SPeter Gruber 		}
905109a9638SPeter Gruber 		udelay(20);
906109a9638SPeter Gruber 	}
907109a9638SPeter Gruber 	if (time == CMDIF_TIMEOUT) {
908109a9638SPeter Gruber 		err = -ENODATA;
909109a9638SPeter Gruber 		goto errout;
910109a9638SPeter Gruber 	}
911109a9638SPeter Gruber 	spin_unlock_irqrestore(&cif->lock, irqflags);
912109a9638SPeter Gruber 
913109a9638SPeter Gruber 	cif->cmdcnt++;		/* update command statistics */
914109a9638SPeter Gruber 	cif->cmdtime += time;
915109a9638SPeter Gruber 	if (time > cif->cmdtimemax)
916109a9638SPeter Gruber 		cif->cmdtimemax = time;
917109a9638SPeter Gruber 	if (time < cif->cmdtimemin)
918109a9638SPeter Gruber 		cif->cmdtimemin = time;
919109a9638SPeter Gruber 	if ((cif->cmdcnt) % 1000 == 0)
920109a9638SPeter Gruber 		snd_printdd
921109a9638SPeter Gruber 		    ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
922109a9638SPeter Gruber 		     cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
923109a9638SPeter Gruber 		     cif->cmdtimemax, cif->errcnt);
924109a9638SPeter Gruber 	return 0;
925109a9638SPeter Gruber 
926109a9638SPeter Gruber       errout:
927109a9638SPeter Gruber 	cif->errcnt++;
928109a9638SPeter Gruber 	spin_unlock_irqrestore(&cif->lock, irqflags);
929109a9638SPeter Gruber 	snd_printdd
930109a9638SPeter Gruber 	    ("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",
931109a9638SPeter Gruber 	     cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
932109a9638SPeter Gruber 	     flags, cmd, parm, ret ? ret->retlongs[0] : 0,
933109a9638SPeter Gruber 	     ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
934109a9638SPeter Gruber 	     err);
935109a9638SPeter Gruber 	return err;
936109a9638SPeter Gruber }
937109a9638SPeter Gruber 
938109a9638SPeter Gruber static int
939109a9638SPeter Gruber setmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
940109a9638SPeter Gruber {
941109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
942109a9638SPeter Gruber 	int i = 0;
943109a9638SPeter Gruber 
944109a9638SPeter Gruber 	snd_printdd("sent mixer %d: 0x%d 0x%d\n", num, rval, lval);
945109a9638SPeter Gruber 	do {
946109a9638SPeter Gruber 		SEND_SDGV(cif, num, num, rval, lval);
947109a9638SPeter Gruber 		SEND_RDGV(cif, num, num, &rptr);
948109a9638SPeter Gruber 		if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
949109a9638SPeter Gruber 			return 0;
950109a9638SPeter Gruber 	} while (i++ < MAX_WRITE_RETRY);
951109a9638SPeter Gruber 	snd_printdd("sent mixer failed\n");
952109a9638SPeter Gruber 	return -EIO;
953109a9638SPeter Gruber }
954109a9638SPeter Gruber 
955109a9638SPeter Gruber static int getpaths(struct cmdif *cif, unsigned char *o)
956109a9638SPeter Gruber {
957109a9638SPeter Gruber 	unsigned char src[E2SINK_MAX];
958109a9638SPeter Gruber 	unsigned char sink[E2SINK_MAX];
959109a9638SPeter Gruber 	int i, j = 0;
960109a9638SPeter Gruber 
961109a9638SPeter Gruber 	for (i = 0; i < E2SINK_MAX; i++) {
962109a9638SPeter Gruber 		getsourcesink(cif, i, i, &src[i], &sink[i]);
963109a9638SPeter Gruber 		if (sink[i] < E2SINK_MAX) {
964109a9638SPeter Gruber 			o[j++] = sink[i];
965109a9638SPeter Gruber 			o[j++] = i;
966109a9638SPeter Gruber 		}
967109a9638SPeter Gruber 	}
968109a9638SPeter Gruber 	return j;
969109a9638SPeter Gruber }
970109a9638SPeter Gruber 
971109a9638SPeter Gruber static int
972109a9638SPeter Gruber getsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
973109a9638SPeter Gruber 	      unsigned char *a, unsigned char *b)
974109a9638SPeter Gruber {
975109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
976109a9638SPeter Gruber 
977109a9638SPeter Gruber 	if (SEND_RSSV(cif, source, sink, &rptr) &&
978109a9638SPeter Gruber 	    SEND_RSSV(cif, source, sink, &rptr))
979109a9638SPeter Gruber 		return -EIO;
980109a9638SPeter Gruber 	*a = rptr.retbytes[0];
981109a9638SPeter Gruber 	*b = rptr.retbytes[1];
982109a9638SPeter Gruber 	snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
983109a9638SPeter Gruber 	return 0;
984109a9638SPeter Gruber }
985109a9638SPeter Gruber 
986109a9638SPeter Gruber static int
987109a9638SPeter Gruber getsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
988109a9638SPeter Gruber {
989109a9638SPeter Gruber 	unsigned char *s;
990109a9638SPeter Gruber 	unsigned int p[2] = { 0, 0 };
991109a9638SPeter Gruber 	int i;
992109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
993109a9638SPeter Gruber 
994109a9638SPeter Gruber 	s = intdec;
995109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
996109a9638SPeter Gruber 		if (*s != 0xff) {
997109a9638SPeter Gruber 			if (SEND_RSRC(cif, *s, &rptr) &&
998109a9638SPeter Gruber 			    SEND_RSRC(cif, *s, &rptr))
999109a9638SPeter Gruber 				return -EIO;
1000109a9638SPeter Gruber 			p[i] += rptr.retwords[1];
1001109a9638SPeter Gruber 			p[i] *= rptr.retwords[2];
1002109a9638SPeter Gruber 			p[i] += rptr.retwords[3];
1003109a9638SPeter Gruber 			p[i] /= 65536;
1004109a9638SPeter Gruber 		}
1005109a9638SPeter Gruber 		s++;
1006109a9638SPeter Gruber 	}
1007109a9638SPeter Gruber 	if (p[0]) {
1008109a9638SPeter Gruber 		if (p[1] != p[0])
1009109a9638SPeter Gruber 			snd_printdd("rates differ %d %d\n", p[0], p[1]);
1010109a9638SPeter Gruber 		*rate = (unsigned int)p[0];
1011109a9638SPeter Gruber 	} else
1012109a9638SPeter Gruber 		*rate = (unsigned int)p[1];
1013109a9638SPeter Gruber 	snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
1014109a9638SPeter Gruber 	return 0;
1015109a9638SPeter Gruber }
1016109a9638SPeter Gruber 
1017109a9638SPeter Gruber static int
1018109a9638SPeter Gruber setsampleformat(struct cmdif *cif,
1019109a9638SPeter Gruber 		unsigned char mixer, unsigned char id,
1020109a9638SPeter Gruber 		unsigned char channels, unsigned char format)
1021109a9638SPeter Gruber {
1022109a9638SPeter Gruber 	unsigned char w, ch, sig, order;
1023109a9638SPeter Gruber 
1024109a9638SPeter Gruber 	snd_printdd
1025109a9638SPeter Gruber 	    ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
1026109a9638SPeter Gruber 	     mixer, id, channels, format);
1027109a9638SPeter Gruber 	ch = channels == 1;
1028109a9638SPeter Gruber 	w = snd_pcm_format_width(format) == 8;
1029109a9638SPeter Gruber 	sig = snd_pcm_format_unsigned(format) != 0;
1030109a9638SPeter Gruber 	order = snd_pcm_format_big_endian(format) != 0;
1031109a9638SPeter Gruber 
1032109a9638SPeter Gruber 	if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
1033109a9638SPeter Gruber 	    SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
1034109a9638SPeter Gruber 		snd_printdd("setsampleformat failed\n");
1035109a9638SPeter Gruber 		return -EIO;
1036109a9638SPeter Gruber 	}
1037109a9638SPeter Gruber 	return 0;
1038109a9638SPeter Gruber }
1039109a9638SPeter Gruber 
1040109a9638SPeter Gruber static int
1041109a9638SPeter Gruber setsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
1042109a9638SPeter Gruber {
1043109a9638SPeter Gruber 	u32 D, M, N;
1044109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1045109a9638SPeter Gruber 	int i;
1046109a9638SPeter Gruber 
1047109a9638SPeter Gruber 	snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
1048109a9638SPeter Gruber 		    intdec[1], rate);
1049109a9638SPeter Gruber 	D = 48000;
1050109a9638SPeter Gruber 	M = ((rate == 48000) ? 47999 : rate) * 65536;
1051109a9638SPeter Gruber 	N = M % D;
1052109a9638SPeter Gruber 	M /= D;
1053109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
1054109a9638SPeter Gruber 		if (*intdec != 0xff) {
1055109a9638SPeter Gruber 			do {
1056109a9638SPeter Gruber 				SEND_SSRC(cif, *intdec, D, M, N);
1057109a9638SPeter Gruber 				SEND_RSRC(cif, *intdec, &rptr);
1058109a9638SPeter Gruber 			} while (rptr.retwords[1] != D &&
1059109a9638SPeter Gruber 				 rptr.retwords[2] != M &&
1060109a9638SPeter Gruber 				 rptr.retwords[3] != N &&
1061109a9638SPeter Gruber 				 i++ < MAX_WRITE_RETRY);
10629980c620SRoel Kluin 			if (i > MAX_WRITE_RETRY) {
1063109a9638SPeter Gruber 				snd_printdd("sent samplerate %d: %d failed\n",
1064109a9638SPeter Gruber 					    *intdec, rate);
1065109a9638SPeter Gruber 				return -EIO;
1066109a9638SPeter Gruber 			}
1067109a9638SPeter Gruber 		}
1068109a9638SPeter Gruber 		intdec++;
1069109a9638SPeter Gruber 	}
1070109a9638SPeter Gruber 	return 0;
1071109a9638SPeter Gruber }
1072109a9638SPeter Gruber 
1073109a9638SPeter Gruber static int
1074109a9638SPeter Gruber getmixer(struct cmdif *cif, short num, unsigned short *rval,
1075109a9638SPeter Gruber 	 unsigned short *lval)
1076109a9638SPeter Gruber {
1077109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1078109a9638SPeter Gruber 
1079109a9638SPeter Gruber 	if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
1080109a9638SPeter Gruber 		return -EIO;
1081109a9638SPeter Gruber 	*rval = rptr.retwords[0];
1082109a9638SPeter Gruber 	*lval = rptr.retwords[1];
1083109a9638SPeter Gruber 	snd_printdd("got mixer %d: 0x%d 0x%d\n", num, *rval, *lval);
1084109a9638SPeter Gruber 	return 0;
1085109a9638SPeter Gruber }
1086109a9638SPeter Gruber 
1087109a9638SPeter Gruber static void riptide_handleirq(unsigned long dev_id)
1088109a9638SPeter Gruber {
1089109a9638SPeter Gruber 	struct snd_riptide *chip = (void *)dev_id;
1090109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1091109a9638SPeter Gruber 	struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
1092109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime;
1093109a9638SPeter Gruber 	struct pcmhw *data = NULL;
1094109a9638SPeter Gruber 	unsigned int pos, period_bytes;
1095109a9638SPeter Gruber 	struct sgd *c;
1096109a9638SPeter Gruber 	int i, j;
1097109a9638SPeter Gruber 	unsigned int flag;
1098109a9638SPeter Gruber 
1099109a9638SPeter Gruber 	if (!cif)
1100109a9638SPeter Gruber 		return;
1101109a9638SPeter Gruber 
1102109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
1103109a9638SPeter Gruber 		substream[i] = chip->playback_substream[i];
1104109a9638SPeter Gruber 	substream[i] = chip->capture_substream;
1105109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
1106109a9638SPeter Gruber 		if (substream[i] &&
1107109a9638SPeter Gruber 		    (runtime = substream[i]->runtime) &&
1108109a9638SPeter Gruber 		    (data = runtime->private_data) && data->state != ST_STOP) {
1109109a9638SPeter Gruber 			pos = 0;
1110109a9638SPeter Gruber 			for (j = 0; j < data->pages; j++) {
1111109a9638SPeter Gruber 				c = &data->sgdbuf[j];
1112109a9638SPeter Gruber 				flag = le32_to_cpu(c->dwStat_Ctl);
1113109a9638SPeter Gruber 				if (flag & EOB_STATUS)
1114109a9638SPeter Gruber 					pos += le32_to_cpu(c->dwSegLen);
1115109a9638SPeter Gruber 				if (flag & EOC_STATUS)
1116109a9638SPeter Gruber 					pos += le32_to_cpu(c->dwSegLen);
1117109a9638SPeter Gruber 				if ((flag & EOS_STATUS)
1118109a9638SPeter Gruber 				    && (data->state == ST_PLAY)) {
1119109a9638SPeter Gruber 					data->state = ST_STOP;
1120109a9638SPeter Gruber 					snd_printk(KERN_ERR
1121109a9638SPeter Gruber 						   "Riptide: DMA stopped unexpectedly\n");
1122109a9638SPeter Gruber 				}
1123109a9638SPeter Gruber 				c->dwStat_Ctl =
1124109a9638SPeter Gruber 				    cpu_to_le32(flag &
1125109a9638SPeter Gruber 						~(EOS_STATUS | EOB_STATUS |
1126109a9638SPeter Gruber 						  EOC_STATUS));
1127109a9638SPeter Gruber 			}
1128109a9638SPeter Gruber 			data->pointer += pos;
1129109a9638SPeter Gruber 			pos += data->oldpos;
1130109a9638SPeter Gruber 			if (data->state != ST_STOP) {
1131109a9638SPeter Gruber 				period_bytes =
1132109a9638SPeter Gruber 				    frames_to_bytes(runtime,
1133109a9638SPeter Gruber 						    runtime->period_size);
1134109a9638SPeter Gruber 				snd_printdd
1135109a9638SPeter Gruber 				    ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
1136109a9638SPeter Gruber 				     READ_AUDIO_STATUS(cif->hwport),
1137109a9638SPeter Gruber 				     bytes_to_frames(runtime, pos),
1138109a9638SPeter Gruber 				     runtime->period_size);
1139109a9638SPeter Gruber 				j = 0;
1140109a9638SPeter Gruber 				if (pos >= period_bytes) {
1141109a9638SPeter Gruber 					j++;
1142109a9638SPeter Gruber 					while (pos >= period_bytes)
1143109a9638SPeter Gruber 						pos -= period_bytes;
1144109a9638SPeter Gruber 				}
1145109a9638SPeter Gruber 				data->oldpos = pos;
1146109a9638SPeter Gruber 				if (j > 0)
1147109a9638SPeter Gruber 					snd_pcm_period_elapsed(substream[i]);
1148109a9638SPeter Gruber 			}
1149109a9638SPeter Gruber 		}
1150109a9638SPeter Gruber 	}
1151109a9638SPeter Gruber }
1152109a9638SPeter Gruber 
1153109a9638SPeter Gruber #ifdef CONFIG_PM
1154109a9638SPeter Gruber static int riptide_suspend(struct pci_dev *pci, pm_message_t state)
1155109a9638SPeter Gruber {
1156109a9638SPeter Gruber 	struct snd_card *card = pci_get_drvdata(pci);
1157109a9638SPeter Gruber 	struct snd_riptide *chip = card->private_data;
1158109a9638SPeter Gruber 
1159109a9638SPeter Gruber 	chip->in_suspend = 1;
1160109a9638SPeter Gruber 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1161109a9638SPeter Gruber 	snd_pcm_suspend_all(chip->pcm);
1162109a9638SPeter Gruber 	snd_ac97_suspend(chip->ac97);
1163109a9638SPeter Gruber 	pci_disable_device(pci);
1164109a9638SPeter Gruber 	pci_save_state(pci);
116530b35399STakashi Iwai 	pci_set_power_state(pci, pci_choose_state(pci, state));
1166109a9638SPeter Gruber 	return 0;
1167109a9638SPeter Gruber }
1168109a9638SPeter Gruber 
1169109a9638SPeter Gruber static int riptide_resume(struct pci_dev *pci)
1170109a9638SPeter Gruber {
1171109a9638SPeter Gruber 	struct snd_card *card = pci_get_drvdata(pci);
1172109a9638SPeter Gruber 	struct snd_riptide *chip = card->private_data;
1173109a9638SPeter Gruber 
1174109a9638SPeter Gruber 	pci_set_power_state(pci, PCI_D0);
117530b35399STakashi Iwai 	pci_restore_state(pci);
117630b35399STakashi Iwai 	if (pci_enable_device(pci) < 0) {
117730b35399STakashi Iwai 		printk(KERN_ERR "riptide: pci_enable_device failed, "
117830b35399STakashi Iwai 		       "disabling device\n");
117930b35399STakashi Iwai 		snd_card_disconnect(card);
118030b35399STakashi Iwai 		return -EIO;
118130b35399STakashi Iwai 	}
1182109a9638SPeter Gruber 	pci_set_master(pci);
1183109a9638SPeter Gruber 	snd_riptide_initialize(chip);
1184109a9638SPeter Gruber 	snd_ac97_resume(chip->ac97);
1185109a9638SPeter Gruber 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1186109a9638SPeter Gruber 	chip->in_suspend = 0;
1187109a9638SPeter Gruber 	return 0;
1188109a9638SPeter Gruber }
1189109a9638SPeter Gruber #endif
1190109a9638SPeter Gruber 
1191a693a26fSTakashi Iwai static int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
1192109a9638SPeter Gruber {
1193a693a26fSTakashi Iwai 	union firmware_version firmware = { .ret = CMDRET_ZERO };
1194a693a26fSTakashi Iwai 	int i, timeout, err;
1195109a9638SPeter Gruber 
1196109a9638SPeter Gruber 	for (i = 0; i < 2; i++) {
1197109a9638SPeter Gruber 		WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
1198109a9638SPeter Gruber 		WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
1199109a9638SPeter Gruber 	}
1200109a9638SPeter Gruber 	SET_GRESET(cif->hwport);
1201109a9638SPeter Gruber 	udelay(100);
1202109a9638SPeter Gruber 	UNSET_GRESET(cif->hwport);
1203109a9638SPeter Gruber 	udelay(100);
1204109a9638SPeter Gruber 
1205109a9638SPeter Gruber 	for (timeout = 100000; --timeout; udelay(10)) {
1206109a9638SPeter Gruber 		if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
1207109a9638SPeter Gruber 			break;
1208109a9638SPeter Gruber 	}
1209a693a26fSTakashi Iwai 	if (!timeout) {
1210109a9638SPeter Gruber 		snd_printk(KERN_ERR
1211a693a26fSTakashi Iwai 			   "Riptide: device not ready, audio status: 0x%x "
1212a693a26fSTakashi Iwai 			   "ready: %d gerr: %d\n",
1213109a9638SPeter Gruber 			   READ_AUDIO_STATUS(cif->hwport),
1214109a9638SPeter Gruber 			   IS_READY(cif->hwport), IS_GERR(cif->hwport));
1215109a9638SPeter Gruber 		return -EIO;
1216109a9638SPeter Gruber 	} else {
1217109a9638SPeter Gruber 		snd_printdd
1218109a9638SPeter Gruber 			("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
1219109a9638SPeter Gruber 			 READ_AUDIO_STATUS(cif->hwport),
1220109a9638SPeter Gruber 			 IS_READY(cif->hwport), IS_GERR(cif->hwport));
1221109a9638SPeter Gruber 	}
1222109a9638SPeter Gruber 
1223a693a26fSTakashi Iwai 	SEND_GETV(cif, &firmware.ret);
1224a693a26fSTakashi Iwai 	snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
1225109a9638SPeter Gruber 		    firmware.firmware.ASIC, firmware.firmware.CODEC,
1226109a9638SPeter Gruber 		    firmware.firmware.AUXDSP, firmware.firmware.PROG);
1227109a9638SPeter Gruber 
1228a5ba6bebSTakashi Iwai 	if (!chip)
1229a5ba6bebSTakashi Iwai 		return 1;
1230a5ba6bebSTakashi Iwai 
1231a693a26fSTakashi Iwai 	for (i = 0; i < FIRMWARE_VERSIONS; i++) {
1232a693a26fSTakashi Iwai 		if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
1233a693a26fSTakashi Iwai 			return 1; /* OK */
1234a693a26fSTakashi Iwai 
1235a5ba6bebSTakashi Iwai 	}
1236a5ba6bebSTakashi Iwai 
1237109a9638SPeter Gruber 	snd_printdd("Writing Firmware\n");
1238109a9638SPeter Gruber 	if (!chip->fw_entry) {
1239a693a26fSTakashi Iwai 		err = request_firmware(&chip->fw_entry, "riptide.hex",
1240a693a26fSTakashi Iwai 				       &chip->pci->dev);
1241a693a26fSTakashi Iwai 		if (err) {
1242109a9638SPeter Gruber 			snd_printk(KERN_ERR
1243a693a26fSTakashi Iwai 				   "Riptide: Firmware not available %d\n", err);
1244109a9638SPeter Gruber 			return -EIO;
1245109a9638SPeter Gruber 		}
1246109a9638SPeter Gruber 	}
1247a693a26fSTakashi Iwai 	err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
1248a693a26fSTakashi Iwai 	if (err) {
1249109a9638SPeter Gruber 		snd_printk(KERN_ERR
1250a693a26fSTakashi Iwai 			   "Riptide: Could not load firmware %d\n", err);
1251a693a26fSTakashi Iwai 		return err;
1252109a9638SPeter Gruber 	}
1253a693a26fSTakashi Iwai 
1254a693a26fSTakashi Iwai 	chip->firmware = firmware;
1255a693a26fSTakashi Iwai 
1256a693a26fSTakashi Iwai 	return 1; /* OK */
1257109a9638SPeter Gruber }
1258109a9638SPeter Gruber 
1259a693a26fSTakashi Iwai static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
1260a693a26fSTakashi Iwai {
1261a693a26fSTakashi Iwai 	union cmdret rptr = CMDRET_ZERO;
1262a693a26fSTakashi Iwai 	int err, tries;
1263a693a26fSTakashi Iwai 
1264a693a26fSTakashi Iwai 	if (!cif)
1265a693a26fSTakashi Iwai 		return -EINVAL;
1266a693a26fSTakashi Iwai 
1267a693a26fSTakashi Iwai 	cif->cmdcnt = 0;
1268a693a26fSTakashi Iwai 	cif->cmdtime = 0;
1269a693a26fSTakashi Iwai 	cif->cmdtimemax = 0;
1270a693a26fSTakashi Iwai 	cif->cmdtimemin = 0xffffffff;
1271a693a26fSTakashi Iwai 	cif->errcnt = 0;
1272a693a26fSTakashi Iwai 	cif->is_reset = 0;
1273a693a26fSTakashi Iwai 
1274a693a26fSTakashi Iwai 	tries = RESET_TRIES;
1275a693a26fSTakashi Iwai 	do {
1276a693a26fSTakashi Iwai 		err = try_to_load_firmware(cif, chip);
1277a693a26fSTakashi Iwai 		if (err < 0)
1278a693a26fSTakashi Iwai 			return err;
1279a693a26fSTakashi Iwai 	} while (!err && --tries);
1280a693a26fSTakashi Iwai 
1281109a9638SPeter Gruber 	SEND_SACR(cif, 0, AC97_RESET);
1282109a9638SPeter Gruber 	SEND_RACR(cif, AC97_RESET, &rptr);
1283109a9638SPeter Gruber 	snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
1284109a9638SPeter Gruber 
1285109a9638SPeter Gruber 	SEND_PLST(cif, 0);
1286109a9638SPeter Gruber 	SEND_SLST(cif, 0);
1287109a9638SPeter Gruber 	SEND_DLST(cif, 0);
1288109a9638SPeter Gruber 	SEND_ALST(cif, 0);
1289109a9638SPeter Gruber 	SEND_KDMA(cif);
1290109a9638SPeter Gruber 
1291109a9638SPeter Gruber 	writearm(cif, 0x301F8, 1, 1);
1292109a9638SPeter Gruber 	writearm(cif, 0x301F4, 1, 1);
1293109a9638SPeter Gruber 
1294109a9638SPeter Gruber 	SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
1295109a9638SPeter Gruber 		  MODEM_SPLITTER, MODEM_MIXER);
1296109a9638SPeter Gruber 	setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
1297109a9638SPeter Gruber 	alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
1298109a9638SPeter Gruber 
1299109a9638SPeter Gruber 	SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
1300109a9638SPeter Gruber 		  FM_MIXER);
1301109a9638SPeter Gruber 	setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
1302109a9638SPeter Gruber 	writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
1303109a9638SPeter Gruber 	writearm(cif, 0x301A8, 0x02, 0x00000002);
1304109a9638SPeter Gruber 	writearm(cif, 0x30264, 0x08, 0xffffffff);
1305109a9638SPeter Gruber 	alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
1306109a9638SPeter Gruber 
1307109a9638SPeter Gruber 	SEND_SSRC(cif, I2S_INTDEC, 48000,
1308109a9638SPeter Gruber 		  ((u32) I2S_RATE * 65536) / 48000,
1309109a9638SPeter Gruber 		  ((u32) I2S_RATE * 65536) % 48000);
1310109a9638SPeter Gruber 	SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
1311109a9638SPeter Gruber 		  I2S_MIXER);
1312109a9638SPeter Gruber 	SEND_SI2S(cif, 1);
1313109a9638SPeter Gruber 	alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
1314109a9638SPeter Gruber 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
1315109a9638SPeter Gruber 	alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
1316109a9638SPeter Gruber 
1317109a9638SPeter Gruber 	SET_AIACK(cif->hwport);
1318109a9638SPeter Gruber 	SET_AIE(cif->hwport);
1319109a9638SPeter Gruber 	SET_AIACK(cif->hwport);
1320109a9638SPeter Gruber 	cif->is_reset = 1;
1321109a9638SPeter Gruber 
1322109a9638SPeter Gruber 	return 0;
1323109a9638SPeter Gruber }
1324109a9638SPeter Gruber 
1325109a9638SPeter Gruber static struct snd_pcm_hardware snd_riptide_playback = {
1326109a9638SPeter Gruber 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1327109a9638SPeter Gruber 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1328109a9638SPeter Gruber 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1329109a9638SPeter Gruber 	.formats =
1330109a9638SPeter Gruber 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1331109a9638SPeter Gruber 	    | SNDRV_PCM_FMTBIT_U16_LE,
1332109a9638SPeter Gruber 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1333109a9638SPeter Gruber 	.rate_min = 5500,
1334109a9638SPeter Gruber 	.rate_max = 48000,
1335109a9638SPeter Gruber 	.channels_min = 1,
1336109a9638SPeter Gruber 	.channels_max = 2,
1337109a9638SPeter Gruber 	.buffer_bytes_max = (64 * 1024),
1338109a9638SPeter Gruber 	.period_bytes_min = PAGE_SIZE >> 1,
1339109a9638SPeter Gruber 	.period_bytes_max = PAGE_SIZE << 8,
1340109a9638SPeter Gruber 	.periods_min = 2,
1341109a9638SPeter Gruber 	.periods_max = 64,
1342109a9638SPeter Gruber 	.fifo_size = 0,
1343109a9638SPeter Gruber };
1344109a9638SPeter Gruber static struct snd_pcm_hardware snd_riptide_capture = {
1345109a9638SPeter Gruber 	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1346109a9638SPeter Gruber 		 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1347109a9638SPeter Gruber 		 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1348109a9638SPeter Gruber 	.formats =
1349109a9638SPeter Gruber 	    SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1350109a9638SPeter Gruber 	    | SNDRV_PCM_FMTBIT_U16_LE,
1351109a9638SPeter Gruber 	.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1352109a9638SPeter Gruber 	.rate_min = 5500,
1353109a9638SPeter Gruber 	.rate_max = 48000,
1354109a9638SPeter Gruber 	.channels_min = 1,
1355109a9638SPeter Gruber 	.channels_max = 2,
1356109a9638SPeter Gruber 	.buffer_bytes_max = (64 * 1024),
1357109a9638SPeter Gruber 	.period_bytes_min = PAGE_SIZE >> 1,
1358109a9638SPeter Gruber 	.period_bytes_max = PAGE_SIZE << 3,
1359109a9638SPeter Gruber 	.periods_min = 2,
1360109a9638SPeter Gruber 	.periods_max = 64,
1361109a9638SPeter Gruber 	.fifo_size = 0,
1362109a9638SPeter Gruber };
1363109a9638SPeter Gruber 
1364109a9638SPeter Gruber static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
1365109a9638SPeter Gruber 					     *substream)
1366109a9638SPeter Gruber {
1367109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1368109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1369109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1370109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1371109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1372109a9638SPeter Gruber 	snd_pcm_uframes_t ret;
1373109a9638SPeter Gruber 
1374109a9638SPeter Gruber 	SEND_GPOS(cif, 0, data->id, &rptr);
1375109a9638SPeter Gruber 	if (data->size && runtime->period_size) {
1376109a9638SPeter Gruber 		snd_printdd
1377109a9638SPeter Gruber 		    ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
1378109a9638SPeter Gruber 		     data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
1379109a9638SPeter Gruber 		     bytes_to_frames(runtime, rptr.retlongs[1]),
1380109a9638SPeter Gruber 		     bytes_to_frames(runtime,
1381109a9638SPeter Gruber 				     rptr.retlongs[1]) % runtime->period_size);
1382109a9638SPeter Gruber 		if (rptr.retlongs[1] > data->pointer)
1383109a9638SPeter Gruber 			ret =
1384109a9638SPeter Gruber 			    bytes_to_frames(runtime,
1385109a9638SPeter Gruber 					    rptr.retlongs[1] % data->size);
1386109a9638SPeter Gruber 		else
1387109a9638SPeter Gruber 			ret =
1388109a9638SPeter Gruber 			    bytes_to_frames(runtime,
1389109a9638SPeter Gruber 					    data->pointer % data->size);
1390109a9638SPeter Gruber 	} else {
1391109a9638SPeter Gruber 		snd_printdd("stream not started or strange parms (%d %ld)\n",
1392109a9638SPeter Gruber 			    data->size, runtime->period_size);
1393109a9638SPeter Gruber 		ret = bytes_to_frames(runtime, 0);
1394109a9638SPeter Gruber 	}
1395109a9638SPeter Gruber 	return ret;
1396109a9638SPeter Gruber }
1397109a9638SPeter Gruber 
1398109a9638SPeter Gruber static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
1399109a9638SPeter Gruber {
1400109a9638SPeter Gruber 	int i, j;
1401109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1402109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1403109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1404109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1405109a9638SPeter Gruber 
1406109a9638SPeter Gruber 	spin_lock(&chip->lock);
1407109a9638SPeter Gruber 	switch (cmd) {
1408109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_START:
1409109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_RESUME:
1410109a9638SPeter Gruber 		if (!(data->state & ST_PLAY)) {
1411109a9638SPeter Gruber 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1412109a9638SPeter Gruber 			SET_AIE(cif->hwport);
1413109a9638SPeter Gruber 			data->state = ST_PLAY;
1414109a9638SPeter Gruber 			if (data->mixer != 0xff)
1415109a9638SPeter Gruber 				setmixer(cif, data->mixer, 0x7fff, 0x7fff);
1416109a9638SPeter Gruber 			chip->openstreams++;
1417109a9638SPeter Gruber 			data->oldpos = 0;
1418109a9638SPeter Gruber 			data->pointer = 0;
1419109a9638SPeter Gruber 		}
1420109a9638SPeter Gruber 		break;
1421109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_STOP:
1422109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_SUSPEND:
1423109a9638SPeter Gruber 		if (data->mixer != 0xff)
1424109a9638SPeter Gruber 			setmixer(cif, data->mixer, 0, 0);
1425109a9638SPeter Gruber 		setmixer(cif, data->mixer, 0, 0);
1426109a9638SPeter Gruber 		SEND_KSTR(cif, data->id);
1427109a9638SPeter Gruber 		data->state = ST_STOP;
1428109a9638SPeter Gruber 		chip->openstreams--;
1429109a9638SPeter Gruber 		j = 0;
1430109a9638SPeter Gruber 		do {
1431109a9638SPeter Gruber 			i = rptr.retlongs[1];
1432109a9638SPeter Gruber 			SEND_GPOS(cif, 0, data->id, &rptr);
1433109a9638SPeter Gruber 			udelay(1);
1434109a9638SPeter Gruber 		} while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
14351b1cc7f2SRoel Kluin 		if (j > MAX_WRITE_RETRY)
1436109a9638SPeter Gruber 			snd_printk(KERN_ERR "Riptide: Could not stop stream!");
1437109a9638SPeter Gruber 		break;
1438109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1439109a9638SPeter Gruber 		if (!(data->state & ST_PAUSE)) {
1440109a9638SPeter Gruber 			SEND_PSTR(cif, data->id);
1441109a9638SPeter Gruber 			data->state |= ST_PAUSE;
1442109a9638SPeter Gruber 			chip->openstreams--;
1443109a9638SPeter Gruber 		}
1444109a9638SPeter Gruber 		break;
1445109a9638SPeter Gruber 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1446109a9638SPeter Gruber 		if (data->state & ST_PAUSE) {
1447109a9638SPeter Gruber 			SEND_SSTR(cif, data->id, data->sgdlist.addr);
1448109a9638SPeter Gruber 			data->state &= ~ST_PAUSE;
1449109a9638SPeter Gruber 			chip->openstreams++;
1450109a9638SPeter Gruber 		}
1451109a9638SPeter Gruber 		break;
1452109a9638SPeter Gruber 	default:
1453109a9638SPeter Gruber 		spin_unlock(&chip->lock);
1454109a9638SPeter Gruber 		return -EINVAL;
1455109a9638SPeter Gruber 	}
1456109a9638SPeter Gruber 	spin_unlock(&chip->lock);
1457109a9638SPeter Gruber 	return 0;
1458109a9638SPeter Gruber }
1459109a9638SPeter Gruber 
1460109a9638SPeter Gruber static int snd_riptide_prepare(struct snd_pcm_substream *substream)
1461109a9638SPeter Gruber {
1462109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1463109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1464109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1465109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1466109a9638SPeter Gruber 	unsigned char *lbuspath = NULL;
1467109a9638SPeter Gruber 	unsigned int rate, channels;
1468109a9638SPeter Gruber 	int err = 0;
1469109a9638SPeter Gruber 	snd_pcm_format_t format;
1470109a9638SPeter Gruber 
1471da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif || !data))
1472da3cec35STakashi Iwai 		return -EINVAL;
1473109a9638SPeter Gruber 
1474109a9638SPeter Gruber 	snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
1475109a9638SPeter Gruber 		    runtime->channels, runtime->format, runtime->rate);
1476109a9638SPeter Gruber 
1477109a9638SPeter Gruber 	spin_lock_irq(&chip->lock);
1478109a9638SPeter Gruber 	channels = runtime->channels;
1479109a9638SPeter Gruber 	format = runtime->format;
1480109a9638SPeter Gruber 	rate = runtime->rate;
1481109a9638SPeter Gruber 	switch (channels) {
1482109a9638SPeter Gruber 	case 1:
1483109a9638SPeter Gruber 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1484109a9638SPeter Gruber 			lbuspath = data->paths.noconv;
1485109a9638SPeter Gruber 		else
1486109a9638SPeter Gruber 			lbuspath = data->paths.mono;
1487109a9638SPeter Gruber 		break;
1488109a9638SPeter Gruber 	case 2:
1489109a9638SPeter Gruber 		if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1490109a9638SPeter Gruber 			lbuspath = data->paths.noconv;
1491109a9638SPeter Gruber 		else
1492109a9638SPeter Gruber 			lbuspath = data->paths.stereo;
1493109a9638SPeter Gruber 		break;
1494109a9638SPeter Gruber 	}
149577a23f26STakashi Iwai 	snd_printdd("use sgdlist at 0x%p\n",
149677a23f26STakashi Iwai 		    data->sgdlist.area);
149777a23f26STakashi Iwai 	if (data->sgdlist.area) {
1498109a9638SPeter Gruber 		unsigned int i, j, size, pages, f, pt, period;
1499109a9638SPeter Gruber 		struct sgd *c, *p = NULL;
1500109a9638SPeter Gruber 
1501109a9638SPeter Gruber 		size = frames_to_bytes(runtime, runtime->buffer_size);
1502109a9638SPeter Gruber 		period = frames_to_bytes(runtime, runtime->period_size);
1503109a9638SPeter Gruber 		f = PAGE_SIZE;
1504109a9638SPeter Gruber 		while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
1505109a9638SPeter Gruber 			f = f >> 1;
1506109a9638SPeter Gruber 		pages = (size + f - 1) / f;
1507109a9638SPeter Gruber 		data->size = size;
1508109a9638SPeter Gruber 		data->pages = pages;
1509109a9638SPeter Gruber 		snd_printdd
1510109a9638SPeter Gruber 		    ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
1511109a9638SPeter Gruber 		     size, pages, f, period);
1512109a9638SPeter Gruber 		pt = 0;
1513109a9638SPeter Gruber 		j = 0;
1514109a9638SPeter Gruber 		for (i = 0; i < pages; i++) {
151577a23f26STakashi Iwai 			unsigned int ofs, addr;
1516109a9638SPeter Gruber 			c = &data->sgdbuf[i];
1517109a9638SPeter Gruber 			if (p)
1518109a9638SPeter Gruber 				p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
1519109a9638SPeter Gruber 							    (i *
1520109a9638SPeter Gruber 							     sizeof(struct
1521109a9638SPeter Gruber 								    sgd)));
1522109a9638SPeter Gruber 			c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
152377a23f26STakashi Iwai 			ofs = j << PAGE_SHIFT;
152477a23f26STakashi Iwai 			addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
152577a23f26STakashi Iwai 			c->dwSegPtrPhys = cpu_to_le32(addr);
1526109a9638SPeter Gruber 			pt = (pt + f) % PAGE_SIZE;
1527109a9638SPeter Gruber 			if (pt == 0)
1528109a9638SPeter Gruber 				j++;
1529109a9638SPeter Gruber 			c->dwSegLen = cpu_to_le32(f);
1530109a9638SPeter Gruber 			c->dwStat_Ctl =
1531109a9638SPeter Gruber 			    cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
1532109a9638SPeter Gruber 					IEOC_ENABLE);
1533109a9638SPeter Gruber 			p = c;
1534109a9638SPeter Gruber 			size -= f;
1535109a9638SPeter Gruber 		}
1536109a9638SPeter Gruber 		data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
1537109a9638SPeter Gruber 	}
1538109a9638SPeter Gruber 	if (lbuspath && lbuspath != data->lbuspath) {
1539109a9638SPeter Gruber 		if (data->lbuspath)
1540109a9638SPeter Gruber 			freelbuspath(cif, data->source, data->lbuspath);
1541109a9638SPeter Gruber 		alloclbuspath(cif, data->source, lbuspath,
1542109a9638SPeter Gruber 			      &data->mixer, data->intdec);
1543109a9638SPeter Gruber 		data->lbuspath = lbuspath;
1544109a9638SPeter Gruber 		data->rate = 0;
1545109a9638SPeter Gruber 	}
1546109a9638SPeter Gruber 	if (data->rate != rate || data->format != format ||
1547109a9638SPeter Gruber 	    data->channels != channels) {
1548109a9638SPeter Gruber 		data->rate = rate;
1549109a9638SPeter Gruber 		data->format = format;
1550109a9638SPeter Gruber 		data->channels = channels;
1551109a9638SPeter Gruber 		if (setsampleformat
1552109a9638SPeter Gruber 		    (cif, data->mixer, data->id, channels, format)
1553109a9638SPeter Gruber 		    || setsamplerate(cif, data->intdec, rate))
1554109a9638SPeter Gruber 			err = -EIO;
1555109a9638SPeter Gruber 	}
1556109a9638SPeter Gruber 	spin_unlock_irq(&chip->lock);
1557109a9638SPeter Gruber 	return err;
1558109a9638SPeter Gruber }
1559109a9638SPeter Gruber 
1560109a9638SPeter Gruber static int
1561109a9638SPeter Gruber snd_riptide_hw_params(struct snd_pcm_substream *substream,
1562109a9638SPeter Gruber 		      struct snd_pcm_hw_params *hw_params)
1563109a9638SPeter Gruber {
1564109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1565109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1566109a9638SPeter Gruber 	struct snd_dma_buffer *sgdlist = &data->sgdlist;
1567109a9638SPeter Gruber 	int err;
1568109a9638SPeter Gruber 
1569109a9638SPeter Gruber 	snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
1570109a9638SPeter Gruber 		    sgdlist->area, (unsigned long)sgdlist->addr,
1571109a9638SPeter Gruber 		    (int)sgdlist->bytes);
1572109a9638SPeter Gruber 	if (sgdlist->area)
1573109a9638SPeter Gruber 		snd_dma_free_pages(sgdlist);
1574109a9638SPeter Gruber 	if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
1575109a9638SPeter Gruber 				       snd_dma_pci_data(chip->pci),
1576109a9638SPeter Gruber 				       sizeof(struct sgd) * (DESC_MAX_MASK + 1),
1577109a9638SPeter Gruber 				       sgdlist)) < 0) {
1578109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
1579109a9638SPeter Gruber 			   (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
1580109a9638SPeter Gruber 		return err;
1581109a9638SPeter Gruber 	}
1582109a9638SPeter Gruber 	data->sgdbuf = (struct sgd *)sgdlist->area;
1583109a9638SPeter Gruber 	return snd_pcm_lib_malloc_pages(substream,
1584109a9638SPeter Gruber 					params_buffer_bytes(hw_params));
1585109a9638SPeter Gruber }
1586109a9638SPeter Gruber 
1587109a9638SPeter Gruber static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
1588109a9638SPeter Gruber {
1589109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1590109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1591109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1592109a9638SPeter Gruber 
1593109a9638SPeter Gruber 	if (cif && data) {
1594109a9638SPeter Gruber 		if (data->lbuspath)
1595109a9638SPeter Gruber 			freelbuspath(cif, data->source, data->lbuspath);
1596109a9638SPeter Gruber 		data->lbuspath = NULL;
1597109a9638SPeter Gruber 		data->source = 0xff;
1598109a9638SPeter Gruber 		data->intdec[0] = 0xff;
1599109a9638SPeter Gruber 		data->intdec[1] = 0xff;
1600109a9638SPeter Gruber 
1601109a9638SPeter Gruber 		if (data->sgdlist.area) {
1602109a9638SPeter Gruber 			snd_dma_free_pages(&data->sgdlist);
1603109a9638SPeter Gruber 			data->sgdlist.area = NULL;
1604109a9638SPeter Gruber 		}
1605109a9638SPeter Gruber 	}
1606109a9638SPeter Gruber 	return snd_pcm_lib_free_pages(substream);
1607109a9638SPeter Gruber }
1608109a9638SPeter Gruber 
1609109a9638SPeter Gruber static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
1610109a9638SPeter Gruber {
1611109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1612109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1613109a9638SPeter Gruber 	struct pcmhw *data;
16140cd87b10SHarvey Harrison 	int sub_num = substream->number;
1615109a9638SPeter Gruber 
16160cd87b10SHarvey Harrison 	chip->playback_substream[sub_num] = substream;
1617109a9638SPeter Gruber 	runtime->hw = snd_riptide_playback;
161868bf5700SKulikov Vasiliy 
1619109a9638SPeter Gruber 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
162068bf5700SKulikov Vasiliy 	if (data == NULL)
162168bf5700SKulikov Vasiliy 		return -ENOMEM;
16220cd87b10SHarvey Harrison 	data->paths = lbus_play_paths[sub_num];
16230cd87b10SHarvey Harrison 	data->id = play_ids[sub_num];
16240cd87b10SHarvey Harrison 	data->source = play_sources[sub_num];
1625109a9638SPeter Gruber 	data->intdec[0] = 0xff;
1626109a9638SPeter Gruber 	data->intdec[1] = 0xff;
1627109a9638SPeter Gruber 	data->state = ST_STOP;
1628109a9638SPeter Gruber 	runtime->private_data = data;
1629109a9638SPeter Gruber 	return snd_pcm_hw_constraint_integer(runtime,
1630109a9638SPeter Gruber 					     SNDRV_PCM_HW_PARAM_PERIODS);
1631109a9638SPeter Gruber }
1632109a9638SPeter Gruber 
1633109a9638SPeter Gruber static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
1634109a9638SPeter Gruber {
1635109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1636109a9638SPeter Gruber 	struct snd_pcm_runtime *runtime = substream->runtime;
1637109a9638SPeter Gruber 	struct pcmhw *data;
1638109a9638SPeter Gruber 
1639109a9638SPeter Gruber 	chip->capture_substream = substream;
1640109a9638SPeter Gruber 	runtime->hw = snd_riptide_capture;
164168bf5700SKulikov Vasiliy 
1642109a9638SPeter Gruber 	data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
164368bf5700SKulikov Vasiliy 	if (data == NULL)
164468bf5700SKulikov Vasiliy 		return -ENOMEM;
1645109a9638SPeter Gruber 	data->paths = lbus_rec_path;
1646109a9638SPeter Gruber 	data->id = PADC;
1647109a9638SPeter Gruber 	data->source = ACLNK2PADC;
1648109a9638SPeter Gruber 	data->intdec[0] = 0xff;
1649109a9638SPeter Gruber 	data->intdec[1] = 0xff;
1650109a9638SPeter Gruber 	data->state = ST_STOP;
1651109a9638SPeter Gruber 	runtime->private_data = data;
1652109a9638SPeter Gruber 	return snd_pcm_hw_constraint_integer(runtime,
1653109a9638SPeter Gruber 					     SNDRV_PCM_HW_PARAM_PERIODS);
1654109a9638SPeter Gruber }
1655109a9638SPeter Gruber 
1656109a9638SPeter Gruber static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
1657109a9638SPeter Gruber {
1658109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1659109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
16600cd87b10SHarvey Harrison 	int sub_num = substream->number;
1661109a9638SPeter Gruber 
1662109a9638SPeter Gruber 	substream->runtime->private_data = NULL;
16630cd87b10SHarvey Harrison 	chip->playback_substream[sub_num] = NULL;
1664109a9638SPeter Gruber 	kfree(data);
1665109a9638SPeter Gruber 	return 0;
1666109a9638SPeter Gruber }
1667109a9638SPeter Gruber 
1668109a9638SPeter Gruber static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
1669109a9638SPeter Gruber {
1670109a9638SPeter Gruber 	struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1671109a9638SPeter Gruber 	struct pcmhw *data = get_pcmhwdev(substream);
1672109a9638SPeter Gruber 
1673109a9638SPeter Gruber 	substream->runtime->private_data = NULL;
1674109a9638SPeter Gruber 	chip->capture_substream = NULL;
1675109a9638SPeter Gruber 	kfree(data);
1676109a9638SPeter Gruber 	return 0;
1677109a9638SPeter Gruber }
1678109a9638SPeter Gruber 
1679109a9638SPeter Gruber static struct snd_pcm_ops snd_riptide_playback_ops = {
1680109a9638SPeter Gruber 	.open = snd_riptide_playback_open,
1681109a9638SPeter Gruber 	.close = snd_riptide_playback_close,
1682109a9638SPeter Gruber 	.ioctl = snd_pcm_lib_ioctl,
1683109a9638SPeter Gruber 	.hw_params = snd_riptide_hw_params,
1684109a9638SPeter Gruber 	.hw_free = snd_riptide_hw_free,
1685109a9638SPeter Gruber 	.prepare = snd_riptide_prepare,
1686109a9638SPeter Gruber 	.page = snd_pcm_sgbuf_ops_page,
1687109a9638SPeter Gruber 	.trigger = snd_riptide_trigger,
1688109a9638SPeter Gruber 	.pointer = snd_riptide_pointer,
1689109a9638SPeter Gruber };
1690109a9638SPeter Gruber static struct snd_pcm_ops snd_riptide_capture_ops = {
1691109a9638SPeter Gruber 	.open = snd_riptide_capture_open,
1692109a9638SPeter Gruber 	.close = snd_riptide_capture_close,
1693109a9638SPeter Gruber 	.ioctl = snd_pcm_lib_ioctl,
1694109a9638SPeter Gruber 	.hw_params = snd_riptide_hw_params,
1695109a9638SPeter Gruber 	.hw_free = snd_riptide_hw_free,
1696109a9638SPeter Gruber 	.prepare = snd_riptide_prepare,
1697109a9638SPeter Gruber 	.page = snd_pcm_sgbuf_ops_page,
1698109a9638SPeter Gruber 	.trigger = snd_riptide_trigger,
1699109a9638SPeter Gruber 	.pointer = snd_riptide_pointer,
1700109a9638SPeter Gruber };
1701109a9638SPeter Gruber 
1702109a9638SPeter Gruber static int __devinit
1703109a9638SPeter Gruber snd_riptide_pcm(struct snd_riptide *chip, int device, struct snd_pcm **rpcm)
1704109a9638SPeter Gruber {
1705109a9638SPeter Gruber 	struct snd_pcm *pcm;
1706109a9638SPeter Gruber 	int err;
1707109a9638SPeter Gruber 
1708109a9638SPeter Gruber 	if (rpcm)
1709109a9638SPeter Gruber 		*rpcm = NULL;
1710109a9638SPeter Gruber 	if ((err =
1711109a9638SPeter Gruber 	     snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
1712109a9638SPeter Gruber 			 &pcm)) < 0)
1713109a9638SPeter Gruber 		return err;
1714109a9638SPeter Gruber 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1715109a9638SPeter Gruber 			&snd_riptide_playback_ops);
1716109a9638SPeter Gruber 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1717109a9638SPeter Gruber 			&snd_riptide_capture_ops);
1718109a9638SPeter Gruber 	pcm->private_data = chip;
1719109a9638SPeter Gruber 	pcm->info_flags = 0;
1720109a9638SPeter Gruber 	strcpy(pcm->name, "RIPTIDE");
1721109a9638SPeter Gruber 	chip->pcm = pcm;
1722109a9638SPeter Gruber 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1723109a9638SPeter Gruber 					      snd_dma_pci_data(chip->pci),
1724109a9638SPeter Gruber 					      64 * 1024, 128 * 1024);
1725109a9638SPeter Gruber 	if (rpcm)
1726109a9638SPeter Gruber 		*rpcm = pcm;
1727109a9638SPeter Gruber 	return 0;
1728109a9638SPeter Gruber }
1729109a9638SPeter Gruber 
1730109a9638SPeter Gruber static irqreturn_t
17317d12e780SDavid Howells snd_riptide_interrupt(int irq, void *dev_id)
1732109a9638SPeter Gruber {
1733109a9638SPeter Gruber 	struct snd_riptide *chip = dev_id;
1734109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1735109a9638SPeter Gruber 
1736109a9638SPeter Gruber 	if (cif) {
1737109a9638SPeter Gruber 		chip->received_irqs++;
1738109a9638SPeter Gruber 		if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
1739109a9638SPeter Gruber 		    IS_EOCIRQ(cif->hwport)) {
1740109a9638SPeter Gruber 			chip->handled_irqs++;
17411f04128aSTakashi Iwai 			tasklet_schedule(&chip->riptide_tq);
1742109a9638SPeter Gruber 		}
1743109a9638SPeter Gruber 		if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
1744109a9638SPeter Gruber 			chip->handled_irqs++;
1745109a9638SPeter Gruber 			snd_mpu401_uart_interrupt(irq,
17467d12e780SDavid Howells 						  chip->rmidi->private_data);
1747109a9638SPeter Gruber 		}
1748109a9638SPeter Gruber 		SET_AIACK(cif->hwport);
1749109a9638SPeter Gruber 	}
1750109a9638SPeter Gruber 	return IRQ_HANDLED;
1751109a9638SPeter Gruber }
1752109a9638SPeter Gruber 
1753109a9638SPeter Gruber static void
1754109a9638SPeter Gruber snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
1755109a9638SPeter Gruber 			unsigned short val)
1756109a9638SPeter Gruber {
1757109a9638SPeter Gruber 	struct snd_riptide *chip = ac97->private_data;
1758109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1759109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1760109a9638SPeter Gruber 	int i = 0;
1761109a9638SPeter Gruber 
1762da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
1763da3cec35STakashi Iwai 		return;
1764109a9638SPeter Gruber 
1765109a9638SPeter Gruber 	snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
1766109a9638SPeter Gruber 	do {
1767109a9638SPeter Gruber 		SEND_SACR(cif, val, reg);
1768109a9638SPeter Gruber 		SEND_RACR(cif, reg, &rptr);
1769109a9638SPeter Gruber 	} while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
17701b1cc7f2SRoel Kluin 	if (i > MAX_WRITE_RETRY)
1771109a9638SPeter Gruber 		snd_printdd("Write AC97 reg failed\n");
1772109a9638SPeter Gruber }
1773109a9638SPeter Gruber 
1774109a9638SPeter Gruber static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
1775109a9638SPeter Gruber 					     unsigned short reg)
1776109a9638SPeter Gruber {
1777109a9638SPeter Gruber 	struct snd_riptide *chip = ac97->private_data;
1778109a9638SPeter Gruber 	struct cmdif *cif = chip->cif;
1779109a9638SPeter Gruber 	union cmdret rptr = CMDRET_ZERO;
1780109a9638SPeter Gruber 
1781da3cec35STakashi Iwai 	if (snd_BUG_ON(!cif))
1782da3cec35STakashi Iwai 		return 0;
1783109a9638SPeter Gruber 
1784109a9638SPeter Gruber 	if (SEND_RACR(cif, reg, &rptr) != 0)
1785109a9638SPeter Gruber 		SEND_RACR(cif, reg, &rptr);
1786109a9638SPeter Gruber 	snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
1787109a9638SPeter Gruber 	return rptr.retwords[1];
1788109a9638SPeter Gruber }
1789109a9638SPeter Gruber 
1790109a9638SPeter Gruber static int snd_riptide_initialize(struct snd_riptide *chip)
1791109a9638SPeter Gruber {
1792109a9638SPeter Gruber 	struct cmdif *cif;
1793109a9638SPeter Gruber 	unsigned int device_id;
1794109a9638SPeter Gruber 	int err;
1795109a9638SPeter Gruber 
1796da3cec35STakashi Iwai 	if (snd_BUG_ON(!chip))
1797da3cec35STakashi Iwai 		return -EINVAL;
1798109a9638SPeter Gruber 
1799109a9638SPeter Gruber 	cif = chip->cif;
1800109a9638SPeter Gruber 	if (!cif) {
1801109a9638SPeter Gruber 		if ((cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL)) == NULL)
1802109a9638SPeter Gruber 			return -ENOMEM;
1803109a9638SPeter Gruber 		cif->hwport = (struct riptideport *)chip->port;
1804109a9638SPeter Gruber 		spin_lock_init(&cif->lock);
1805109a9638SPeter Gruber 		chip->cif = cif;
1806109a9638SPeter Gruber 	}
1807109a9638SPeter Gruber 	cif->is_reset = 0;
1808109a9638SPeter Gruber 	if ((err = riptide_reset(cif, chip)) != 0)
1809109a9638SPeter Gruber 		return err;
1810109a9638SPeter Gruber 	device_id = chip->device_id;
1811109a9638SPeter Gruber 	switch (device_id) {
1812109a9638SPeter Gruber 	case 0x4310:
1813109a9638SPeter Gruber 	case 0x4320:
1814109a9638SPeter Gruber 	case 0x4330:
1815109a9638SPeter Gruber 		snd_printdd("Modem enable?\n");
1816109a9638SPeter Gruber 		SEND_SETDPLL(cif);
1817109a9638SPeter Gruber 		break;
1818109a9638SPeter Gruber 	}
1819109a9638SPeter Gruber 	snd_printdd("Enabling MPU IRQs\n");
1820109a9638SPeter Gruber 	if (chip->rmidi)
1821109a9638SPeter Gruber 		SET_EMPUIRQ(cif->hwport);
1822109a9638SPeter Gruber 	return err;
1823109a9638SPeter Gruber }
1824109a9638SPeter Gruber 
1825109a9638SPeter Gruber static int snd_riptide_free(struct snd_riptide *chip)
1826109a9638SPeter Gruber {
1827109a9638SPeter Gruber 	struct cmdif *cif;
1828109a9638SPeter Gruber 
1829da3cec35STakashi Iwai 	if (!chip)
1830da3cec35STakashi Iwai 		return 0;
1831109a9638SPeter Gruber 
1832109a9638SPeter Gruber 	if ((cif = chip->cif)) {
1833109a9638SPeter Gruber 		SET_GRESET(cif->hwport);
1834109a9638SPeter Gruber 		udelay(100);
1835109a9638SPeter Gruber 		UNSET_GRESET(cif->hwport);
1836109a9638SPeter Gruber 		kfree(chip->cif);
1837109a9638SPeter Gruber 	}
18380a50d2b2STakashi Iwai 	if (chip->irq >= 0)
18390a50d2b2STakashi Iwai 		free_irq(chip->irq, chip);
1840109a9638SPeter Gruber 	if (chip->fw_entry)
1841109a9638SPeter Gruber 		release_firmware(chip->fw_entry);
184243560116STakashi Iwai 	release_and_free_resource(chip->res_port);
1843109a9638SPeter Gruber 	kfree(chip);
1844109a9638SPeter Gruber 	return 0;
1845109a9638SPeter Gruber }
1846109a9638SPeter Gruber 
1847109a9638SPeter Gruber static int snd_riptide_dev_free(struct snd_device *device)
1848109a9638SPeter Gruber {
1849109a9638SPeter Gruber 	struct snd_riptide *chip = device->device_data;
1850109a9638SPeter Gruber 
1851109a9638SPeter Gruber 	return snd_riptide_free(chip);
1852109a9638SPeter Gruber }
1853109a9638SPeter Gruber 
1854109a9638SPeter Gruber static int __devinit
1855109a9638SPeter Gruber snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
1856109a9638SPeter Gruber 		   struct snd_riptide **rchip)
1857109a9638SPeter Gruber {
1858109a9638SPeter Gruber 	struct snd_riptide *chip;
1859109a9638SPeter Gruber 	struct riptideport *hwport;
1860109a9638SPeter Gruber 	int err;
1861109a9638SPeter Gruber 	static struct snd_device_ops ops = {
1862109a9638SPeter Gruber 		.dev_free = snd_riptide_dev_free,
1863109a9638SPeter Gruber 	};
1864109a9638SPeter Gruber 
1865109a9638SPeter Gruber 	*rchip = NULL;
1866109a9638SPeter Gruber 	if ((err = pci_enable_device(pci)) < 0)
1867109a9638SPeter Gruber 		return err;
1868109a9638SPeter Gruber 	if (!(chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL)))
1869109a9638SPeter Gruber 		return -ENOMEM;
1870109a9638SPeter Gruber 
1871109a9638SPeter Gruber 	spin_lock_init(&chip->lock);
1872109a9638SPeter Gruber 	chip->card = card;
1873109a9638SPeter Gruber 	chip->pci = pci;
1874109a9638SPeter Gruber 	chip->irq = -1;
1875109a9638SPeter Gruber 	chip->openstreams = 0;
1876109a9638SPeter Gruber 	chip->port = pci_resource_start(pci, 0);
1877109a9638SPeter Gruber 	chip->received_irqs = 0;
1878109a9638SPeter Gruber 	chip->handled_irqs = 0;
1879109a9638SPeter Gruber 	chip->cif = NULL;
1880109a9638SPeter Gruber 	tasklet_init(&chip->riptide_tq, riptide_handleirq, (unsigned long)chip);
1881109a9638SPeter Gruber 
1882109a9638SPeter Gruber 	if ((chip->res_port =
1883109a9638SPeter Gruber 	     request_region(chip->port, 64, "RIPTIDE")) == NULL) {
1884109a9638SPeter Gruber 		snd_printk(KERN_ERR
1885109a9638SPeter Gruber 			   "Riptide: unable to grab region 0x%lx-0x%lx\n",
1886109a9638SPeter Gruber 			   chip->port, chip->port + 64 - 1);
1887109a9638SPeter Gruber 		snd_riptide_free(chip);
1888109a9638SPeter Gruber 		return -EBUSY;
1889109a9638SPeter Gruber 	}
1890109a9638SPeter Gruber 	hwport = (struct riptideport *)chip->port;
1891109a9638SPeter Gruber 	UNSET_AIE(hwport);
1892109a9638SPeter Gruber 
1893437a5a46STakashi Iwai 	if (request_irq(pci->irq, snd_riptide_interrupt, IRQF_SHARED,
1894934c2b6dSTakashi Iwai 			KBUILD_MODNAME, chip)) {
1895109a9638SPeter Gruber 		snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
1896109a9638SPeter Gruber 			   pci->irq);
1897109a9638SPeter Gruber 		snd_riptide_free(chip);
1898109a9638SPeter Gruber 		return -EBUSY;
1899109a9638SPeter Gruber 	}
1900109a9638SPeter Gruber 	chip->irq = pci->irq;
1901109a9638SPeter Gruber 	chip->device_id = pci->device;
1902109a9638SPeter Gruber 	pci_set_master(pci);
1903109a9638SPeter Gruber 	if ((err = snd_riptide_initialize(chip)) < 0) {
1904109a9638SPeter Gruber 		snd_riptide_free(chip);
1905109a9638SPeter Gruber 		return err;
1906109a9638SPeter Gruber 	}
1907109a9638SPeter Gruber 
1908109a9638SPeter Gruber 	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1909109a9638SPeter Gruber 		snd_riptide_free(chip);
1910109a9638SPeter Gruber 		return err;
1911109a9638SPeter Gruber 	}
1912109a9638SPeter Gruber 
1913c187c041STakashi Iwai 	snd_card_set_dev(card, &pci->dev);
1914c187c041STakashi Iwai 
1915109a9638SPeter Gruber 	*rchip = chip;
1916109a9638SPeter Gruber 	return 0;
1917109a9638SPeter Gruber }
1918109a9638SPeter Gruber 
1919109a9638SPeter Gruber static void
1920109a9638SPeter Gruber snd_riptide_proc_read(struct snd_info_entry *entry,
1921109a9638SPeter Gruber 		      struct snd_info_buffer *buffer)
1922109a9638SPeter Gruber {
1923109a9638SPeter Gruber 	struct snd_riptide *chip = entry->private_data;
1924109a9638SPeter Gruber 	struct pcmhw *data;
1925109a9638SPeter Gruber 	int i;
1926109a9638SPeter Gruber 	struct cmdif *cif = NULL;
1927109a9638SPeter Gruber 	unsigned char p[256];
1928109a9638SPeter Gruber 	unsigned short rval = 0, lval = 0;
1929109a9638SPeter Gruber 	unsigned int rate;
1930109a9638SPeter Gruber 
1931109a9638SPeter Gruber 	if (!chip)
1932109a9638SPeter Gruber 		return;
1933109a9638SPeter Gruber 
1934109a9638SPeter Gruber 	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1935109a9638SPeter Gruber 	snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
1936109a9638SPeter Gruber 		    chip->device_id, chip->handled_irqs, chip->received_irqs);
1937109a9638SPeter Gruber 	for (i = 0; i < 64; i += 4)
1938109a9638SPeter Gruber 		snd_iprintf(buffer, "%c%02x: %08x",
1939109a9638SPeter Gruber 			    (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
1940109a9638SPeter Gruber 	if ((cif = chip->cif)) {
1941109a9638SPeter Gruber 		snd_iprintf(buffer,
1942109a9638SPeter Gruber 			    "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
1943109a9638SPeter Gruber 			    chip->firmware.firmware.ASIC,
1944109a9638SPeter Gruber 			    chip->firmware.firmware.CODEC,
1945109a9638SPeter Gruber 			    chip->firmware.firmware.AUXDSP,
1946109a9638SPeter Gruber 			    chip->firmware.firmware.PROG);
1947109a9638SPeter Gruber 		snd_iprintf(buffer, "\nDigital mixer:");
1948109a9638SPeter Gruber 		for (i = 0; i < 12; i++) {
1949109a9638SPeter Gruber 			getmixer(cif, i, &rval, &lval);
1950109a9638SPeter Gruber 			snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
1951109a9638SPeter Gruber 		}
1952109a9638SPeter Gruber 		snd_iprintf(buffer,
1953109a9638SPeter Gruber 			    "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
1954109a9638SPeter Gruber 			    cif->cmdcnt, cif->errcnt,
1955109a9638SPeter Gruber 			    cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
1956109a9638SPeter Gruber 	}
1957109a9638SPeter Gruber 	snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
1958109a9638SPeter Gruber 	for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
1959109a9638SPeter Gruber 		if (chip->playback_substream[i]
1960109a9638SPeter Gruber 		    && chip->playback_substream[i]->runtime
1961109a9638SPeter Gruber 		    && (data =
1962109a9638SPeter Gruber 			chip->playback_substream[i]->runtime->private_data)) {
1963109a9638SPeter Gruber 			snd_iprintf(buffer,
1964109a9638SPeter Gruber 				    "stream: %d mixer: %d source: %d (%d,%d)\n",
1965109a9638SPeter Gruber 				    data->id, data->mixer, data->source,
1966109a9638SPeter Gruber 				    data->intdec[0], data->intdec[1]);
1967109a9638SPeter Gruber 			if (!(getsamplerate(cif, data->intdec, &rate)))
1968109a9638SPeter Gruber 				snd_iprintf(buffer, "rate: %d\n", rate);
1969109a9638SPeter Gruber 		}
1970109a9638SPeter Gruber 	}
1971109a9638SPeter Gruber 	if (chip->capture_substream
1972109a9638SPeter Gruber 	    && chip->capture_substream->runtime
1973109a9638SPeter Gruber 	    && (data = chip->capture_substream->runtime->private_data)) {
1974109a9638SPeter Gruber 		snd_iprintf(buffer,
1975109a9638SPeter Gruber 			    "stream: %d mixer: %d source: %d (%d,%d)\n",
1976109a9638SPeter Gruber 			    data->id, data->mixer,
1977109a9638SPeter Gruber 			    data->source, data->intdec[0], data->intdec[1]);
1978109a9638SPeter Gruber 		if (!(getsamplerate(cif, data->intdec, &rate)))
1979109a9638SPeter Gruber 			snd_iprintf(buffer, "rate: %d\n", rate);
1980109a9638SPeter Gruber 	}
1981109a9638SPeter Gruber 	snd_iprintf(buffer, "Paths:\n");
1982109a9638SPeter Gruber 	i = getpaths(cif, p);
1983282572b5SDan Carpenter 	while (i >= 2) {
1984282572b5SDan Carpenter 		i -= 2;
1985282572b5SDan Carpenter 		snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
1986109a9638SPeter Gruber 	}
1987109a9638SPeter Gruber 	snd_iprintf(buffer, "\n");
1988109a9638SPeter Gruber }
1989109a9638SPeter Gruber 
1990109a9638SPeter Gruber static void __devinit snd_riptide_proc_init(struct snd_riptide *chip)
1991109a9638SPeter Gruber {
1992109a9638SPeter Gruber 	struct snd_info_entry *entry;
1993109a9638SPeter Gruber 
1994109a9638SPeter Gruber 	if (!snd_card_proc_new(chip->card, "riptide", &entry))
1995bf850204STakashi Iwai 		snd_info_set_text_ops(entry, chip, snd_riptide_proc_read);
1996109a9638SPeter Gruber }
1997109a9638SPeter Gruber 
1998109a9638SPeter Gruber static int __devinit snd_riptide_mixer(struct snd_riptide *chip)
1999109a9638SPeter Gruber {
2000109a9638SPeter Gruber 	struct snd_ac97_bus *pbus;
2001109a9638SPeter Gruber 	struct snd_ac97_template ac97;
2002109a9638SPeter Gruber 	int err = 0;
2003109a9638SPeter Gruber 	static struct snd_ac97_bus_ops ops = {
2004109a9638SPeter Gruber 		.write = snd_riptide_codec_write,
2005109a9638SPeter Gruber 		.read = snd_riptide_codec_read,
2006109a9638SPeter Gruber 	};
2007109a9638SPeter Gruber 
2008109a9638SPeter Gruber 	memset(&ac97, 0, sizeof(ac97));
2009109a9638SPeter Gruber 	ac97.private_data = chip;
2010109a9638SPeter Gruber 	ac97.scaps = AC97_SCAP_SKIP_MODEM;
2011109a9638SPeter Gruber 
2012109a9638SPeter Gruber 	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
2013109a9638SPeter Gruber 		return err;
2014109a9638SPeter Gruber 
2015109a9638SPeter Gruber 	chip->ac97_bus = pbus;
2016109a9638SPeter Gruber 	ac97.pci = chip->pci;
2017109a9638SPeter Gruber 	if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
2018109a9638SPeter Gruber 		return err;
2019109a9638SPeter Gruber 	return err;
2020109a9638SPeter Gruber }
2021109a9638SPeter Gruber 
2022109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2023109a9638SPeter Gruber 
2024109a9638SPeter Gruber static int __devinit
2025109a9638SPeter Gruber snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
2026109a9638SPeter Gruber {
2027109a9638SPeter Gruber 	static int dev;
2028db1005ecSTakashi Iwai 	struct gameport *gameport;
2029109a9638SPeter Gruber 
2030109a9638SPeter Gruber 	if (dev >= SNDRV_CARDS)
2031109a9638SPeter Gruber 		return -ENODEV;
2032109a9638SPeter Gruber 	if (!enable[dev]) {
2033109a9638SPeter Gruber 		dev++;
2034109a9638SPeter Gruber 		return -ENOENT;
2035109a9638SPeter Gruber 	}
2036109a9638SPeter Gruber 
2037db1005ecSTakashi Iwai 	if (!joystick_port[dev++])
2038db1005ecSTakashi Iwai 		return 0;
2039db1005ecSTakashi Iwai 
2040db1005ecSTakashi Iwai 	gameport = gameport_allocate_port();
2041db1005ecSTakashi Iwai 	if (!gameport)
2042db1005ecSTakashi Iwai 		return -ENOMEM;
2043db1005ecSTakashi Iwai 	if (!request_region(joystick_port[dev], 8, "Riptide gameport")) {
2044109a9638SPeter Gruber 		snd_printk(KERN_WARNING
2045109a9638SPeter Gruber 			   "Riptide: cannot grab gameport 0x%x\n",
2046109a9638SPeter Gruber 			   joystick_port[dev]);
2047db1005ecSTakashi Iwai 		gameport_free_port(gameport);
2048db1005ecSTakashi Iwai 		return -EBUSY;
2049109a9638SPeter Gruber 	}
2050db1005ecSTakashi Iwai 
2051db1005ecSTakashi Iwai 	gameport->io = joystick_port[dev];
2052db1005ecSTakashi Iwai 	gameport_register_port(gameport);
2053db1005ecSTakashi Iwai 	pci_set_drvdata(pci, gameport);
2054109a9638SPeter Gruber 	return 0;
2055109a9638SPeter Gruber }
2056109a9638SPeter Gruber 
2057109a9638SPeter Gruber static void __devexit snd_riptide_joystick_remove(struct pci_dev *pci)
2058109a9638SPeter Gruber {
2059db1005ecSTakashi Iwai 	struct gameport *gameport = pci_get_drvdata(pci);
2060db1005ecSTakashi Iwai 	if (gameport) {
2061db1005ecSTakashi Iwai 		release_region(gameport->io, 8);
2062db1005ecSTakashi Iwai 		gameport_unregister_port(gameport);
2063db1005ecSTakashi Iwai 		pci_set_drvdata(pci, NULL);
2064109a9638SPeter Gruber 	}
2065109a9638SPeter Gruber }
2066109a9638SPeter Gruber #endif
2067109a9638SPeter Gruber 
2068109a9638SPeter Gruber static int __devinit
2069109a9638SPeter Gruber snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2070109a9638SPeter Gruber {
2071109a9638SPeter Gruber 	static int dev;
2072109a9638SPeter Gruber 	struct snd_card *card;
2073109a9638SPeter Gruber 	struct snd_riptide *chip;
2074a693a26fSTakashi Iwai 	unsigned short val;
2075a693a26fSTakashi Iwai 	int err;
2076109a9638SPeter Gruber 
2077109a9638SPeter Gruber 	if (dev >= SNDRV_CARDS)
2078109a9638SPeter Gruber 		return -ENODEV;
2079109a9638SPeter Gruber 	if (!enable[dev]) {
2080109a9638SPeter Gruber 		dev++;
2081109a9638SPeter Gruber 		return -ENOENT;
2082109a9638SPeter Gruber 	}
2083109a9638SPeter Gruber 
2084e58de7baSTakashi Iwai 	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2085e58de7baSTakashi Iwai 	if (err < 0)
2086e58de7baSTakashi Iwai 		return err;
2087a693a26fSTakashi Iwai 	err = snd_riptide_create(card, pci, &chip);
2088a693a26fSTakashi Iwai 	if (err < 0)
2089a693a26fSTakashi Iwai 		goto error;
2090109a9638SPeter Gruber 	card->private_data = chip;
2091a693a26fSTakashi Iwai 	err = snd_riptide_pcm(chip, 0, NULL);
2092a693a26fSTakashi Iwai 	if (err < 0)
2093a693a26fSTakashi Iwai 		goto error;
2094a693a26fSTakashi Iwai 	err = snd_riptide_mixer(chip);
2095a693a26fSTakashi Iwai 	if (err < 0)
2096a693a26fSTakashi Iwai 		goto error;
2097a693a26fSTakashi Iwai 
2098a693a26fSTakashi Iwai 	val = LEGACY_ENABLE_ALL;
2099a693a26fSTakashi Iwai 	if (opl3_port[dev])
2100a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_FM;
2101109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2102a693a26fSTakashi Iwai 	if (joystick_port[dev])
2103a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_GAMEPORT;
2104109a9638SPeter Gruber #endif
2105a693a26fSTakashi Iwai 	if (mpu_port[dev])
2106a693a26fSTakashi Iwai 		val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
2107a693a26fSTakashi Iwai 	val |= (chip->irq << 4) & 0xf0;
2108a693a26fSTakashi Iwai 	pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
2109a693a26fSTakashi Iwai 	if (mpu_port[dev]) {
2110a693a26fSTakashi Iwai 		val = mpu_port[dev];
2111a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
2112a693a26fSTakashi Iwai 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
2113dba8b469SClemens Ladisch 					  val, MPU401_INFO_IRQ_HOOK, -1,
2114a693a26fSTakashi Iwai 					  &chip->rmidi);
2115a693a26fSTakashi Iwai 		if (err < 0)
2116109a9638SPeter Gruber 			snd_printk(KERN_WARNING
2117109a9638SPeter Gruber 				   "Riptide: Can't Allocate MPU at 0x%x\n",
2118a693a26fSTakashi Iwai 				   val);
2119109a9638SPeter Gruber 		else
2120a693a26fSTakashi Iwai 			chip->mpuaddr = val;
2121109a9638SPeter Gruber 	}
2122a693a26fSTakashi Iwai 	if (opl3_port[dev]) {
2123a693a26fSTakashi Iwai 		val = opl3_port[dev];
2124a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
2125a693a26fSTakashi Iwai 		err = snd_opl3_create(card, val, val + 2,
2126a693a26fSTakashi Iwai 				      OPL3_HW_RIPTIDE, 0, &chip->opl3);
2127a693a26fSTakashi Iwai 		if (err < 0)
2128109a9638SPeter Gruber 			snd_printk(KERN_WARNING
2129109a9638SPeter Gruber 				   "Riptide: Can't Allocate OPL3 at 0x%x\n",
2130a693a26fSTakashi Iwai 				   val);
2131109a9638SPeter Gruber 		else {
2132a693a26fSTakashi Iwai 			chip->opladdr = val;
2133a693a26fSTakashi Iwai 			err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
2134a693a26fSTakashi Iwai 			if (err < 0)
2135109a9638SPeter Gruber 				snd_printk(KERN_WARNING
2136109a9638SPeter Gruber 					   "Riptide: Can't Allocate OPL3-HWDEP\n");
2137109a9638SPeter Gruber 		}
2138109a9638SPeter Gruber 	}
2139109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2140a693a26fSTakashi Iwai 	if (joystick_port[dev]) {
2141a693a26fSTakashi Iwai 		val = joystick_port[dev];
2142a693a26fSTakashi Iwai 		pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
2143a693a26fSTakashi Iwai 		chip->gameaddr = val;
2144109a9638SPeter Gruber 	}
2145109a9638SPeter Gruber #endif
2146109a9638SPeter Gruber 
2147109a9638SPeter Gruber 	strcpy(card->driver, "RIPTIDE");
2148109a9638SPeter Gruber 	strcpy(card->shortname, "Riptide");
2149109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2150109a9638SPeter Gruber 	snprintf(card->longname, sizeof(card->longname),
2151109a9638SPeter Gruber 		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
2152109a9638SPeter Gruber 		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2153109a9638SPeter Gruber 		 chip->opladdr, chip->gameaddr);
2154109a9638SPeter Gruber #else
2155109a9638SPeter Gruber 	snprintf(card->longname, sizeof(card->longname),
2156109a9638SPeter Gruber 		 "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
2157109a9638SPeter Gruber 		 card->shortname, chip->port, chip->irq, chip->mpuaddr,
2158109a9638SPeter Gruber 		 chip->opladdr);
2159109a9638SPeter Gruber #endif
2160109a9638SPeter Gruber 	snd_riptide_proc_init(chip);
2161a693a26fSTakashi Iwai 	err = snd_card_register(card);
2162a693a26fSTakashi Iwai 	if (err < 0)
2163a693a26fSTakashi Iwai 		goto error;
2164109a9638SPeter Gruber 	pci_set_drvdata(pci, card);
2165109a9638SPeter Gruber 	dev++;
2166109a9638SPeter Gruber 	return 0;
2167a693a26fSTakashi Iwai 
2168a693a26fSTakashi Iwai  error:
2169a693a26fSTakashi Iwai 	snd_card_free(card);
2170a693a26fSTakashi Iwai 	return err;
2171109a9638SPeter Gruber }
2172109a9638SPeter Gruber 
2173109a9638SPeter Gruber static void __devexit snd_card_riptide_remove(struct pci_dev *pci)
2174109a9638SPeter Gruber {
2175109a9638SPeter Gruber 	snd_card_free(pci_get_drvdata(pci));
2176109a9638SPeter Gruber 	pci_set_drvdata(pci, NULL);
2177109a9638SPeter Gruber }
2178109a9638SPeter Gruber 
2179109a9638SPeter Gruber static struct pci_driver driver = {
21803733e424STakashi Iwai 	.name = KBUILD_MODNAME,
2181109a9638SPeter Gruber 	.id_table = snd_riptide_ids,
2182109a9638SPeter Gruber 	.probe = snd_card_riptide_probe,
2183109a9638SPeter Gruber 	.remove = __devexit_p(snd_card_riptide_remove),
2184109a9638SPeter Gruber #ifdef CONFIG_PM
2185109a9638SPeter Gruber 	.suspend = riptide_suspend,
2186109a9638SPeter Gruber 	.resume = riptide_resume,
2187109a9638SPeter Gruber #endif
2188109a9638SPeter Gruber };
2189109a9638SPeter Gruber 
2190109a9638SPeter Gruber #ifdef SUPPORT_JOYSTICK
2191109a9638SPeter Gruber static struct pci_driver joystick_driver = {
21923733e424STakashi Iwai 	.name = KBUILD_MODNAME "-joystick",
2193109a9638SPeter Gruber 	.id_table = snd_riptide_joystick_ids,
2194109a9638SPeter Gruber 	.probe = snd_riptide_joystick_probe,
2195109a9638SPeter Gruber 	.remove = __devexit_p(snd_riptide_joystick_remove),
2196109a9638SPeter Gruber };
2197109a9638SPeter Gruber #endif
2198109a9638SPeter Gruber 
2199109a9638SPeter Gruber static int __init alsa_card_riptide_init(void)
2200109a9638SPeter Gruber {
2201109a9638SPeter Gruber 	int err;
2202db1005ecSTakashi Iwai 	err = pci_register_driver(&driver);
2203db1005ecSTakashi Iwai 	if (err < 0)
2204109a9638SPeter Gruber 		return err;
2205109a9638SPeter Gruber #if defined(SUPPORT_JOYSTICK)
2206cb65c873SJaswinder Singh Rajput 	err = pci_register_driver(&joystick_driver);
2207cb65c873SJaswinder Singh Rajput 	/* On failure unregister formerly registered audio driver */
2208cb65c873SJaswinder Singh Rajput 	if (err < 0)
2209cb65c873SJaswinder Singh Rajput 		pci_unregister_driver(&driver);
2210109a9638SPeter Gruber #endif
2211cb65c873SJaswinder Singh Rajput 	return err;
2212109a9638SPeter Gruber }
2213109a9638SPeter Gruber 
2214109a9638SPeter Gruber static void __exit alsa_card_riptide_exit(void)
2215109a9638SPeter Gruber {
2216109a9638SPeter Gruber 	pci_unregister_driver(&driver);
2217109a9638SPeter Gruber #if defined(SUPPORT_JOYSTICK)
2218109a9638SPeter Gruber 	pci_unregister_driver(&joystick_driver);
2219109a9638SPeter Gruber #endif
2220109a9638SPeter Gruber }
2221109a9638SPeter Gruber 
2222109a9638SPeter Gruber module_init(alsa_card_riptide_init);
2223109a9638SPeter Gruber module_exit(alsa_card_riptide_exit);
2224