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