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

source code of linux/sound/pci/riptide/riptide.c