Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Driver for the Conexant Riptide Soundchip
0004  *
0005  *  Copyright (c) 2004 Peter Gruber <nokos@gmx.net>
0006  */
0007 /*
0008   History:
0009    - 02/15/2004 first release
0010    
0011   This Driver is based on the OSS Driver version from Linuxant (riptide-0.6lnxtbeta03111100)
0012   credits from the original files:
0013   
0014   MODULE NAME:        cnxt_rt.h                       
0015   AUTHOR:             K. Lazarev  (Transcribed by KNL)
0016   HISTORY:         Major Revision               Date        By
0017             -----------------------------     --------     -----
0018             Created                           02/1/2000     KNL
0019 
0020   MODULE NAME:     int_mdl.c                       
0021   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
0022   HISTORY:         Major Revision               Date        By
0023             -----------------------------     --------     -----
0024             Created                           10/01/99      KNL
0025         
0026   MODULE NAME:        riptide.h                       
0027   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
0028   HISTORY:         Major Revision               Date        By
0029             -----------------------------     --------     -----
0030             Created                           10/16/97      OLD
0031 
0032   MODULE NAME:        Rp_Cmdif.cpp                       
0033   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
0034                       K. Lazarev    (Transcribed by KNL)
0035   HISTORY:         Major Revision               Date        By
0036             -----------------------------     --------     -----
0037             Adopted from NT4 driver            6/22/99      OLD
0038             Ported to Linux                    9/01/99      KNL
0039 
0040   MODULE NAME:        rt_hw.c                       
0041   AUTHOR:             O. Druzhinin  (Transcribed by OLD)
0042                       C. Lazarev    (Transcribed by CNL)
0043   HISTORY:         Major Revision               Date        By
0044             -----------------------------     --------     -----
0045             Created                           11/18/97      OLD
0046             Hardware functions for RipTide    11/24/97      CNL
0047             (ES1) are coded
0048             Hardware functions for RipTide    12/24/97      CNL
0049             (A0) are coded
0050             Hardware functions for RipTide    03/20/98      CNL
0051             (A1) are coded
0052             Boot loader is included           05/07/98      CNL
0053             Redesigned for WDM                07/27/98      CNL
0054             Redesigned for Linux              09/01/99      CNL
0055 
0056   MODULE NAME:        rt_hw.h
0057   AUTHOR:             C. Lazarev    (Transcribed by CNL)
0058   HISTORY:         Major Revision               Date        By
0059             -----------------------------     --------     -----
0060             Created                           11/18/97      CNL
0061 
0062   MODULE NAME:     rt_mdl.c                       
0063   AUTHOR:          Konstantin Lazarev    (Transcribed by KNL)
0064   HISTORY:         Major Revision               Date        By
0065             -----------------------------     --------     -----
0066             Created                           10/01/99      KNL
0067 
0068   MODULE NAME:        mixer.h                        
0069   AUTHOR:             K. Kenney
0070   HISTORY:         Major Revision                   Date          By
0071             -----------------------------          --------     -----
0072             Created from MS W95 Sample             11/28/95      KRS
0073             RipTide                                10/15/97      KRS
0074             Adopted for Windows NT driver          01/20/98      CNL
0075 */
0076 
0077 #include <linux/delay.h>
0078 #include <linux/init.h>
0079 #include <linux/interrupt.h>
0080 #include <linux/pci.h>
0081 #include <linux/slab.h>
0082 #include <linux/wait.h>
0083 #include <linux/gameport.h>
0084 #include <linux/device.h>
0085 #include <linux/firmware.h>
0086 #include <linux/kernel.h>
0087 #include <linux/module.h>
0088 #include <linux/io.h>
0089 #include <sound/core.h>
0090 #include <sound/info.h>
0091 #include <sound/control.h>
0092 #include <sound/pcm.h>
0093 #include <sound/pcm_params.h>
0094 #include <sound/ac97_codec.h>
0095 #include <sound/mpu401.h>
0096 #include <sound/opl3.h>
0097 #include <sound/initval.h>
0098 
0099 #if IS_REACHABLE(CONFIG_GAMEPORT)
0100 #define SUPPORT_JOYSTICK 1
0101 #endif
0102 
0103 MODULE_AUTHOR("Peter Gruber <nokos@gmx.net>");
0104 MODULE_DESCRIPTION("riptide");
0105 MODULE_LICENSE("GPL");
0106 MODULE_FIRMWARE("riptide.hex");
0107 
0108 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0109 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0110 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
0111 
0112 #ifdef SUPPORT_JOYSTICK
0113 static int joystick_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x200 };
0114 #endif
0115 static int mpu_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x330 };
0116 static int opl3_port[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS - 1)] = 0x388 };
0117 
0118 module_param_array(index, int, NULL, 0444);
0119 MODULE_PARM_DESC(index, "Index value for Riptide soundcard.");
0120 module_param_array(id, charp, NULL, 0444);
0121 MODULE_PARM_DESC(id, "ID string for Riptide soundcard.");
0122 module_param_array(enable, bool, NULL, 0444);
0123 MODULE_PARM_DESC(enable, "Enable Riptide soundcard.");
0124 #ifdef SUPPORT_JOYSTICK
0125 module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
0126 MODULE_PARM_DESC(joystick_port, "Joystick port # for Riptide soundcard.");
0127 #endif
0128 module_param_hw_array(mpu_port, int, ioport, NULL, 0444);
0129 MODULE_PARM_DESC(mpu_port, "MPU401 port # for Riptide driver.");
0130 module_param_hw_array(opl3_port, int, ioport, NULL, 0444);
0131 MODULE_PARM_DESC(opl3_port, "OPL3 port # for Riptide driver.");
0132 
0133 /*
0134  */
0135 
0136 #define MPU401_HW_RIPTIDE MPU401_HW_MPU401
0137 #define OPL3_HW_RIPTIDE   OPL3_HW_OPL3
0138 
0139 #define PCI_EXT_CapId       0x40
0140 #define PCI_EXT_NextCapPrt  0x41
0141 #define PCI_EXT_PWMC        0x42
0142 #define PCI_EXT_PWSCR       0x44
0143 #define PCI_EXT_Data00      0x46
0144 #define PCI_EXT_PMSCR_BSE   0x47
0145 #define PCI_EXT_SB_Base     0x48
0146 #define PCI_EXT_FM_Base     0x4a
0147 #define PCI_EXT_MPU_Base    0x4C
0148 #define PCI_EXT_Game_Base   0x4E
0149 #define PCI_EXT_Legacy_Mask 0x50
0150 #define PCI_EXT_AsicRev     0x52
0151 #define PCI_EXT_Reserved3   0x53
0152 
0153 #define LEGACY_ENABLE_ALL      0x8000   /* legacy device options */
0154 #define LEGACY_ENABLE_SB       0x4000
0155 #define LEGACY_ENABLE_FM       0x2000
0156 #define LEGACY_ENABLE_MPU_INT  0x1000
0157 #define LEGACY_ENABLE_MPU      0x0800
0158 #define LEGACY_ENABLE_GAMEPORT 0x0400
0159 
0160 #define MAX_WRITE_RETRY  10 /* cmd interface limits */
0161 #define MAX_ERROR_COUNT  10
0162 #define CMDIF_TIMEOUT    50000
0163 #define RESET_TRIES      5
0164 
0165 #define READ_PORT_ULONG(p)     inl((unsigned long)&(p))
0166 #define WRITE_PORT_ULONG(p,x)  outl(x,(unsigned long)&(p))
0167 
0168 #define READ_AUDIO_CONTROL(p)     READ_PORT_ULONG(p->audio_control)
0169 #define WRITE_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,x)
0170 #define UMASK_AUDIO_CONTROL(p,x)  WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)|x)
0171 #define MASK_AUDIO_CONTROL(p,x)   WRITE_PORT_ULONG(p->audio_control,READ_PORT_ULONG(p->audio_control)&x)
0172 #define READ_AUDIO_STATUS(p)      READ_PORT_ULONG(p->audio_status)
0173 
0174 #define SET_GRESET(p)     UMASK_AUDIO_CONTROL(p,0x0001) /* global reset switch */
0175 #define UNSET_GRESET(p)   MASK_AUDIO_CONTROL(p,~0x0001)
0176 #define SET_AIE(p)        UMASK_AUDIO_CONTROL(p,0x0004) /* interrupt enable */
0177 #define UNSET_AIE(p)      MASK_AUDIO_CONTROL(p,~0x0004)
0178 #define SET_AIACK(p)      UMASK_AUDIO_CONTROL(p,0x0008) /* interrupt acknowledge */
0179 #define UNSET_AIACKT(p)   MASKAUDIO_CONTROL(p,~0x0008)
0180 #define SET_ECMDAE(p)     UMASK_AUDIO_CONTROL(p,0x0010)
0181 #define UNSET_ECMDAE(p)   MASK_AUDIO_CONTROL(p,~0x0010)
0182 #define SET_ECMDBE(p)     UMASK_AUDIO_CONTROL(p,0x0020)
0183 #define UNSET_ECMDBE(p)   MASK_AUDIO_CONTROL(p,~0x0020)
0184 #define SET_EDATAF(p)     UMASK_AUDIO_CONTROL(p,0x0040)
0185 #define UNSET_EDATAF(p)   MASK_AUDIO_CONTROL(p,~0x0040)
0186 #define SET_EDATBF(p)     UMASK_AUDIO_CONTROL(p,0x0080)
0187 #define UNSET_EDATBF(p)   MASK_AUDIO_CONTROL(p,~0x0080)
0188 #define SET_ESBIRQON(p)   UMASK_AUDIO_CONTROL(p,0x0100)
0189 #define UNSET_ESBIRQON(p) MASK_AUDIO_CONTROL(p,~0x0100)
0190 #define SET_EMPUIRQ(p)    UMASK_AUDIO_CONTROL(p,0x0200)
0191 #define UNSET_EMPUIRQ(p)  MASK_AUDIO_CONTROL(p,~0x0200)
0192 #define IS_CMDE(a)        (READ_PORT_ULONG(a->stat)&0x1)    /* cmd empty */
0193 #define IS_DATF(a)        (READ_PORT_ULONG(a->stat)&0x2)    /* data filled */
0194 #define IS_READY(p)       (READ_AUDIO_STATUS(p)&0x0001)
0195 #define IS_DLREADY(p)     (READ_AUDIO_STATUS(p)&0x0002)
0196 #define IS_DLERR(p)       (READ_AUDIO_STATUS(p)&0x0004)
0197 #define IS_GERR(p)        (READ_AUDIO_STATUS(p)&0x0008) /* error ! */
0198 #define IS_CMDAEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0010)
0199 #define IS_CMDBEIRQ(p)    (READ_AUDIO_STATUS(p)&0x0020)
0200 #define IS_DATAFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0040)
0201 #define IS_DATBFIRQ(p)    (READ_AUDIO_STATUS(p)&0x0080)
0202 #define IS_EOBIRQ(p)      (READ_AUDIO_STATUS(p)&0x0100) /* interrupt status */
0203 #define IS_EOSIRQ(p)      (READ_AUDIO_STATUS(p)&0x0200)
0204 #define IS_EOCIRQ(p)      (READ_AUDIO_STATUS(p)&0x0400)
0205 #define IS_UNSLIRQ(p)     (READ_AUDIO_STATUS(p)&0x0800)
0206 #define IS_SBIRQ(p)       (READ_AUDIO_STATUS(p)&0x1000)
0207 #define IS_MPUIRQ(p)      (READ_AUDIO_STATUS(p)&0x2000)
0208 
0209 #define RESP 0x00000001     /* command flags */
0210 #define PARM 0x00000002
0211 #define CMDA 0x00000004
0212 #define CMDB 0x00000008
0213 #define NILL 0x00000000
0214 
0215 #define LONG0(a)   ((u32)a) /* shifts and masks */
0216 #define BYTE0(a)   (LONG0(a)&0xff)
0217 #define BYTE1(a)   (BYTE0(a)<<8)
0218 #define BYTE2(a)   (BYTE0(a)<<16)
0219 #define BYTE3(a)   (BYTE0(a)<<24)
0220 #define WORD0(a)   (LONG0(a)&0xffff)
0221 #define WORD1(a)   (WORD0(a)<<8)
0222 #define WORD2(a)   (WORD0(a)<<16)
0223 #define TRINIB0(a) (LONG0(a)&0xffffff)
0224 #define TRINIB1(a) (TRINIB0(a)<<8)
0225 
0226 #define RET(a)     ((union cmdret *)(a))
0227 
0228 #define SEND_GETV(p,b)             sendcmd(p,RESP,GETV,0,RET(b))    /* get version */
0229 #define SEND_GETC(p,b,c)           sendcmd(p,PARM|RESP,GETC,c,RET(b))
0230 #define SEND_GUNS(p,b)             sendcmd(p,RESP,GUNS,0,RET(b))
0231 #define SEND_SCID(p,b)             sendcmd(p,RESP,SCID,0,RET(b))
0232 #define SEND_RMEM(p,b,c,d)         sendcmd(p,PARM|RESP,RMEM|BYTE1(b),LONG0(c),RET(d))   /* memory access for firmware write */
0233 #define SEND_SMEM(p,b,c)           sendcmd(p,PARM,SMEM|BYTE1(b),LONG0(c),RET(0))    /* memory access for firmware write */
0234 #define SEND_WMEM(p,b,c)           sendcmd(p,PARM,WMEM|BYTE1(b),LONG0(c),RET(0))    /* memory access for firmware write */
0235 #define SEND_SDTM(p,b,c)           sendcmd(p,PARM|RESP,SDTM|TRINIB1(b),0,RET(c))    /* memory access for firmware write */
0236 #define SEND_GOTO(p,b)             sendcmd(p,PARM,GOTO,LONG0(b),RET(0)) /* memory access for firmware write */
0237 #define SEND_SETDPLL(p)            sendcmd(p,0,ARM_SETDPLL,0,RET(0))
0238 #define SEND_SSTR(p,b,c)           sendcmd(p,PARM,SSTR|BYTE3(b),LONG0(c),RET(0))    /* start stream */
0239 #define SEND_PSTR(p,b)             sendcmd(p,PARM,PSTR,BYTE3(b),RET(0)) /* pause stream */
0240 #define SEND_KSTR(p,b)             sendcmd(p,PARM,KSTR,BYTE3(b),RET(0)) /* stop stream */
0241 #define SEND_KDMA(p)               sendcmd(p,0,KDMA,0,RET(0))   /* stop all dma */
0242 #define SEND_GPOS(p,b,c,d)         sendcmd(p,PARM|RESP,GPOS,BYTE3(c)|BYTE2(b),RET(d))   /* get position in dma */
0243 #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 */
0244 #define SEND_GSTS(p,b,c,d)         sendcmd(p,PARM|RESP,GSTS,BYTE3(c)|BYTE2(b),RET(d))
0245 #define SEND_NGPOS(p,b,c,d)        sendcmd(p,PARM|RESP,NGPOS,BYTE3(c)|BYTE2(b),RET(d))
0246 #define SEND_PSEL(p,b,c)           sendcmd(p,PARM,PSEL,BYTE2(b)|BYTE3(c),RET(0))    /* activate lbus path */
0247 #define SEND_PCLR(p,b,c)           sendcmd(p,PARM,PCLR,BYTE2(b)|BYTE3(c),RET(0))    /* deactivate lbus path */
0248 #define SEND_PLST(p,b)             sendcmd(p,PARM,PLST,BYTE3(b),RET(0))
0249 #define SEND_RSSV(p,b,c,d)         sendcmd(p,PARM|RESP,RSSV,BYTE2(b)|BYTE3(c),RET(d))
0250 #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 */
0251 #define SEND_SSRC(p,b,c,d,e)       sendcmd(p,PARM,SSRC|BYTE1(b)|WORD2(c),WORD0(d)|WORD2(e),RET(0))  /* configure source */
0252 #define SEND_SLST(p,b)             sendcmd(p,PARM,SLST,BYTE3(b),RET(0))
0253 #define SEND_RSRC(p,b,c)           sendcmd(p,RESP,RSRC|BYTE1(b),0,RET(c))   /* read source config */
0254 #define SEND_SSRB(p,b,c)           sendcmd(p,PARM,SSRB|BYTE1(b),WORD2(c),RET(0))
0255 #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 */
0256 #define SEND_RDGV(p,b,c,d)         sendcmd(p,PARM|RESP,RDGV|BYTE2(b)|BYTE3(c),0,RET(d)) /* read digital mixer */
0257 #define SEND_DLST(p,b)             sendcmd(p,PARM,DLST,BYTE3(b),RET(0))
0258 #define SEND_SACR(p,b,c)           sendcmd(p,PARM,SACR,WORD0(b)|WORD2(c),RET(0))    /* set AC97 register */
0259 #define SEND_RACR(p,b,c)           sendcmd(p,PARM|RESP,RACR,WORD2(b),RET(c))    /* get AC97 register */
0260 #define SEND_ALST(p,b)             sendcmd(p,PARM,ALST,BYTE3(b),RET(0))
0261 #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))
0262 #define SEND_RXAC(p,b,c,d)         sendcmd(p,PARM|RESP,RXAC,BYTE2(b)|BYTE3(c),RET(d))
0263 #define SEND_SI2S(p,b)             sendcmd(p,PARM,SI2S,WORD2(b),RET(0))
0264 
0265 #define EOB_STATUS         0x80000000   /* status flags : block boundary */
0266 #define EOS_STATUS         0x40000000   /*              : stoppped */
0267 #define EOC_STATUS         0x20000000   /*              : stream end */
0268 #define ERR_STATUS         0x10000000
0269 #define EMPTY_STATUS       0x08000000
0270 
0271 #define IEOB_ENABLE        0x1  /* enable interrupts for status notification above */
0272 #define IEOS_ENABLE        0x2
0273 #define IEOC_ENABLE        0x4
0274 #define RDONCE             0x8
0275 #define DESC_MAX_MASK      0xff
0276 
0277 #define ST_PLAY  0x1        /* stream states */
0278 #define ST_STOP  0x2
0279 #define ST_PAUSE 0x4
0280 
0281 #define I2S_INTDEC     3    /* config for I2S link */
0282 #define I2S_MERGER     0
0283 #define I2S_SPLITTER   0
0284 #define I2S_MIXER      7
0285 #define I2S_RATE       44100
0286 
0287 #define MODEM_INTDEC   4    /* config for modem link */
0288 #define MODEM_MERGER   3
0289 #define MODEM_SPLITTER 0
0290 #define MODEM_MIXER    11
0291 
0292 #define FM_INTDEC      3    /* config for FM/OPL3 link */
0293 #define FM_MERGER      0
0294 #define FM_SPLITTER    0
0295 #define FM_MIXER       9
0296 
0297 #define SPLIT_PATH  0x80    /* path splitting flag */
0298 
0299 enum FIRMWARE {
0300     DATA_REC = 0, EXT_END_OF_FILE, EXT_SEG_ADDR_REC, EXT_GOTO_CMD_REC,
0301     EXT_LIN_ADDR_REC,
0302 };
0303 
0304 enum CMDS {
0305     GETV = 0x00, GETC, GUNS, SCID, RMEM =
0306         0x10, SMEM, WMEM, SDTM, GOTO, SSTR =
0307         0x20, PSTR, KSTR, KDMA, GPOS, SETF, GSTS, NGPOS, PSEL =
0308         0x30, PCLR, PLST, RSSV, LSEL, SSRC = 0x40, SLST, RSRC, SSRB, SDGV =
0309         0x50, RDGV, DLST, SACR = 0x60, RACR, ALST, TXAC, RXAC, SI2S =
0310         0x70, ARM_SETDPLL = 0x72,
0311 };
0312 
0313 enum E1SOURCE {
0314     ARM2LBUS_FIFO0 = 0, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2, ARM2LBUS_FIFO3,
0315     ARM2LBUS_FIFO4, ARM2LBUS_FIFO5, ARM2LBUS_FIFO6, ARM2LBUS_FIFO7,
0316     ARM2LBUS_FIFO8, ARM2LBUS_FIFO9, ARM2LBUS_FIFO10, ARM2LBUS_FIFO11,
0317     ARM2LBUS_FIFO12, ARM2LBUS_FIFO13, ARM2LBUS_FIFO14, ARM2LBUS_FIFO15,
0318     INTER0_OUT, INTER1_OUT, INTER2_OUT, INTER3_OUT, INTER4_OUT,
0319     INTERM0_OUT, INTERM1_OUT, INTERM2_OUT, INTERM3_OUT, INTERM4_OUT,
0320     INTERM5_OUT, INTERM6_OUT, DECIMM0_OUT, DECIMM1_OUT, DECIMM2_OUT,
0321     DECIMM3_OUT, DECIM0_OUT, SR3_4_OUT, OPL3_SAMPLE, ASRC0, ASRC1,
0322     ACLNK2PADC, ACLNK2MODEM0RX, ACLNK2MIC, ACLNK2MODEM1RX, ACLNK2HNDMIC,
0323     DIGITAL_MIXER_OUT0, GAINFUNC0_OUT, GAINFUNC1_OUT, GAINFUNC2_OUT,
0324     GAINFUNC3_OUT, GAINFUNC4_OUT, SOFTMODEMTX, SPLITTER0_OUTL,
0325     SPLITTER0_OUTR, SPLITTER1_OUTL, SPLITTER1_OUTR, SPLITTER2_OUTL,
0326     SPLITTER2_OUTR, SPLITTER3_OUTL, SPLITTER3_OUTR, MERGER0_OUT,
0327     MERGER1_OUT, MERGER2_OUT, MERGER3_OUT, ARM2LBUS_FIFO_DIRECT, NO_OUT
0328 };
0329 
0330 enum E2SINK {
0331     LBUS2ARM_FIFO0 = 0, LBUS2ARM_FIFO1, LBUS2ARM_FIFO2, LBUS2ARM_FIFO3,
0332     LBUS2ARM_FIFO4, LBUS2ARM_FIFO5, LBUS2ARM_FIFO6, LBUS2ARM_FIFO7,
0333     INTER0_IN, INTER1_IN, INTER2_IN, INTER3_IN, INTER4_IN, INTERM0_IN,
0334     INTERM1_IN, INTERM2_IN, INTERM3_IN, INTERM4_IN, INTERM5_IN, INTERM6_IN,
0335     DECIMM0_IN, DECIMM1_IN, DECIMM2_IN, DECIMM3_IN, DECIM0_IN, SR3_4_IN,
0336     PDAC2ACLNK, MODEM0TX2ACLNK, MODEM1TX2ACLNK, HNDSPK2ACLNK,
0337     DIGITAL_MIXER_IN0, DIGITAL_MIXER_IN1, DIGITAL_MIXER_IN2,
0338     DIGITAL_MIXER_IN3, DIGITAL_MIXER_IN4, DIGITAL_MIXER_IN5,
0339     DIGITAL_MIXER_IN6, DIGITAL_MIXER_IN7, DIGITAL_MIXER_IN8,
0340     DIGITAL_MIXER_IN9, DIGITAL_MIXER_IN10, DIGITAL_MIXER_IN11,
0341     GAINFUNC0_IN, GAINFUNC1_IN, GAINFUNC2_IN, GAINFUNC3_IN, GAINFUNC4_IN,
0342     SOFTMODEMRX, SPLITTER0_IN, SPLITTER1_IN, SPLITTER2_IN, SPLITTER3_IN,
0343     MERGER0_INL, MERGER0_INR, MERGER1_INL, MERGER1_INR, MERGER2_INL,
0344     MERGER2_INR, MERGER3_INL, MERGER3_INR, E2SINK_MAX
0345 };
0346 
0347 enum LBUS_SINK {
0348     LS_SRC_INTERPOLATOR = 0, LS_SRC_INTERPOLATORM, LS_SRC_DECIMATOR,
0349     LS_SRC_DECIMATORM, LS_MIXER_IN, LS_MIXER_GAIN_FUNCTION,
0350     LS_SRC_SPLITTER, LS_SRC_MERGER, LS_NONE1, LS_NONE2,
0351 };
0352 
0353 enum RT_CHANNEL_IDS {
0354     M0TX = 0, M1TX, TAMTX, HSSPKR, PDAC, DSNDTX0, DSNDTX1, DSNDTX2,
0355     DSNDTX3, DSNDTX4, DSNDTX5, DSNDTX6, DSNDTX7, WVSTRTX, COP3DTX, SPARE,
0356     M0RX, HSMIC, M1RX, CLEANRX, MICADC, PADC, COPRX1, COPRX2,
0357     CHANNEL_ID_COUNTER
0358 };
0359 
0360 enum { SB_CMD = 0, MODEM_CMD, I2S_CMD0, I2S_CMD1, FM_CMD, MAX_CMD };
0361 
0362 struct lbuspath {
0363     const unsigned char *noconv;
0364     const unsigned char *stereo;
0365     const unsigned char *mono;
0366 };
0367 
0368 struct cmdport {
0369     u32 data1;      /* cmd,param */
0370     u32 data2;      /* param */
0371     u32 stat;       /* status */
0372     u32 pad[5];
0373 };
0374 
0375 struct riptideport {
0376     u32 audio_control;  /* status registers */
0377     u32 audio_status;
0378     u32 pad[2];
0379     struct cmdport port[2]; /* command ports */
0380 };
0381 
0382 struct cmdif {
0383     struct riptideport *hwport;
0384     spinlock_t lock;
0385     unsigned int cmdcnt;    /* cmd statistics */
0386     unsigned int cmdtime;
0387     unsigned int cmdtimemax;
0388     unsigned int cmdtimemin;
0389     unsigned int errcnt;
0390     int is_reset;
0391 };
0392 
0393 struct riptide_firmware {
0394     u16 ASIC;
0395     u16 CODEC;
0396     u16 AUXDSP;
0397     u16 PROG;
0398 };
0399 
0400 union cmdret {
0401     u8 retbytes[8];
0402     u16 retwords[4];
0403     u32 retlongs[2];
0404 };
0405 
0406 union firmware_version {
0407     union cmdret ret;
0408     struct riptide_firmware firmware;
0409 };
0410 
0411 #define get_pcmhwdev(substream) (struct pcmhw *)(substream->runtime->private_data)
0412 
0413 #define PLAYBACK_SUBSTREAMS 3
0414 struct snd_riptide {
0415     struct snd_card *card;
0416     struct pci_dev *pci;
0417     const struct firmware *fw_entry;
0418 
0419     struct cmdif *cif;
0420 
0421     struct snd_pcm *pcm;
0422     struct snd_pcm *pcm_i2s;
0423     struct snd_rawmidi *rmidi;
0424     struct snd_opl3 *opl3;
0425     struct snd_ac97 *ac97;
0426     struct snd_ac97_bus *ac97_bus;
0427 
0428     struct snd_pcm_substream *playback_substream[PLAYBACK_SUBSTREAMS];
0429     struct snd_pcm_substream *capture_substream;
0430 
0431     int openstreams;
0432 
0433     int irq;
0434     unsigned long port;
0435     unsigned short mpuaddr;
0436     unsigned short opladdr;
0437 #ifdef SUPPORT_JOYSTICK
0438     unsigned short gameaddr;
0439 #endif
0440     struct resource *res_port;
0441 
0442     unsigned short device_id;
0443 
0444     union firmware_version firmware;
0445 
0446     spinlock_t lock;
0447     struct snd_info_entry *proc_entry;
0448 
0449     unsigned long received_irqs;
0450     unsigned long handled_irqs;
0451 #ifdef CONFIG_PM_SLEEP
0452     int in_suspend;
0453 #endif
0454 };
0455 
0456 struct sgd {            /* scatter gather desriptor */
0457     __le32 dwNextLink;
0458     __le32 dwSegPtrPhys;
0459     __le32 dwSegLen;
0460     __le32 dwStat_Ctl;
0461 };
0462 
0463 struct pcmhw {          /* pcm descriptor */
0464     struct lbuspath paths;
0465     const unsigned char *lbuspath;
0466     unsigned char source;
0467     unsigned char intdec[2];
0468     unsigned char mixer;
0469     unsigned char id;
0470     unsigned char state;
0471     unsigned int rate;
0472     unsigned int channels;
0473     snd_pcm_format_t format;
0474     struct snd_dma_buffer sgdlist;
0475     struct sgd *sgdbuf;
0476     unsigned int size;
0477     unsigned int pages;
0478     unsigned int oldpos;
0479     unsigned int pointer;
0480 };
0481 
0482 #define CMDRET_ZERO (union cmdret){{(u32)0, (u32) 0}}
0483 
0484 static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
0485            union cmdret *ret);
0486 static int getsourcesink(struct cmdif *cif, unsigned char source,
0487              unsigned char sink, unsigned char *a,
0488              unsigned char *b);
0489 static int snd_riptide_initialize(struct snd_riptide *chip);
0490 static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip);
0491 
0492 /*
0493  */
0494 
0495 static const struct pci_device_id snd_riptide_ids[] = {
0496     { PCI_DEVICE(0x127a, 0x4310) },
0497     { PCI_DEVICE(0x127a, 0x4320) },
0498     { PCI_DEVICE(0x127a, 0x4330) },
0499     { PCI_DEVICE(0x127a, 0x4340) },
0500     {0,},
0501 };
0502 
0503 #ifdef SUPPORT_JOYSTICK
0504 static const struct pci_device_id snd_riptide_joystick_ids[] = {
0505     { PCI_DEVICE(0x127a, 0x4312) },
0506     { PCI_DEVICE(0x127a, 0x4322) },
0507     { PCI_DEVICE(0x127a, 0x4332) },
0508     { PCI_DEVICE(0x127a, 0x4342) },
0509     {0,},
0510 };
0511 #endif
0512 
0513 MODULE_DEVICE_TABLE(pci, snd_riptide_ids);
0514 
0515 /*
0516  */
0517 
0518 static const unsigned char lbusin2out[E2SINK_MAX + 1][2] = {
0519     {NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
0520                                      LS_NONE2},
0521     {NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1}, {NO_OUT,
0522                                      LS_NONE2},
0523     {INTER0_OUT, LS_SRC_INTERPOLATOR}, {INTER1_OUT, LS_SRC_INTERPOLATOR},
0524     {INTER2_OUT, LS_SRC_INTERPOLATOR}, {INTER3_OUT, LS_SRC_INTERPOLATOR},
0525     {INTER4_OUT, LS_SRC_INTERPOLATOR}, {INTERM0_OUT, LS_SRC_INTERPOLATORM},
0526     {INTERM1_OUT, LS_SRC_INTERPOLATORM}, {INTERM2_OUT,
0527                           LS_SRC_INTERPOLATORM},
0528     {INTERM3_OUT, LS_SRC_INTERPOLATORM}, {INTERM4_OUT,
0529                           LS_SRC_INTERPOLATORM},
0530     {INTERM5_OUT, LS_SRC_INTERPOLATORM}, {INTERM6_OUT,
0531                           LS_SRC_INTERPOLATORM},
0532     {DECIMM0_OUT, LS_SRC_DECIMATORM}, {DECIMM1_OUT, LS_SRC_DECIMATORM},
0533     {DECIMM2_OUT, LS_SRC_DECIMATORM}, {DECIMM3_OUT, LS_SRC_DECIMATORM},
0534     {DECIM0_OUT, LS_SRC_DECIMATOR}, {SR3_4_OUT, LS_NONE1}, {NO_OUT,
0535                                 LS_NONE2},
0536     {NO_OUT, LS_NONE1}, {NO_OUT, LS_NONE2}, {NO_OUT, LS_NONE1},
0537     {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
0538     {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
0539     {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
0540     {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
0541     {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
0542     {DIGITAL_MIXER_OUT0, LS_MIXER_IN}, {DIGITAL_MIXER_OUT0, LS_MIXER_IN},
0543     {GAINFUNC0_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC1_OUT,
0544                           LS_MIXER_GAIN_FUNCTION},
0545     {GAINFUNC2_OUT, LS_MIXER_GAIN_FUNCTION}, {GAINFUNC3_OUT,
0546                           LS_MIXER_GAIN_FUNCTION},
0547     {GAINFUNC4_OUT, LS_MIXER_GAIN_FUNCTION}, {SOFTMODEMTX, LS_NONE1},
0548     {SPLITTER0_OUTL, LS_SRC_SPLITTER}, {SPLITTER1_OUTL, LS_SRC_SPLITTER},
0549     {SPLITTER2_OUTL, LS_SRC_SPLITTER}, {SPLITTER3_OUTL, LS_SRC_SPLITTER},
0550     {MERGER0_OUT, LS_SRC_MERGER}, {MERGER0_OUT, LS_SRC_MERGER},
0551     {MERGER1_OUT, LS_SRC_MERGER},
0552     {MERGER1_OUT, LS_SRC_MERGER}, {MERGER2_OUT, LS_SRC_MERGER},
0553     {MERGER2_OUT, LS_SRC_MERGER},
0554     {MERGER3_OUT, LS_SRC_MERGER}, {MERGER3_OUT, LS_SRC_MERGER}, {NO_OUT,
0555                                      LS_NONE2},
0556 };
0557 
0558 static const unsigned char lbus_play_opl3[] = {
0559     DIGITAL_MIXER_IN0 + FM_MIXER, 0xff
0560 };
0561 static const unsigned char lbus_play_modem[] = {
0562     DIGITAL_MIXER_IN0 + MODEM_MIXER, 0xff
0563 };
0564 static const unsigned char lbus_play_i2s[] = {
0565     INTER0_IN + I2S_INTDEC, DIGITAL_MIXER_IN0 + I2S_MIXER, 0xff
0566 };
0567 static const unsigned char lbus_play_out[] = {
0568     PDAC2ACLNK, 0xff
0569 };
0570 static const unsigned char lbus_play_outhp[] = {
0571     HNDSPK2ACLNK, 0xff
0572 };
0573 static const unsigned char lbus_play_noconv1[] = {
0574     DIGITAL_MIXER_IN0, 0xff
0575 };
0576 static const unsigned char lbus_play_stereo1[] = {
0577     INTER0_IN, DIGITAL_MIXER_IN0, 0xff
0578 };
0579 static const unsigned char lbus_play_mono1[] = {
0580     INTERM0_IN, DIGITAL_MIXER_IN0, 0xff
0581 };
0582 static const unsigned char lbus_play_noconv2[] = {
0583     DIGITAL_MIXER_IN1, 0xff
0584 };
0585 static const unsigned char lbus_play_stereo2[] = {
0586     INTER1_IN, DIGITAL_MIXER_IN1, 0xff
0587 };
0588 static const unsigned char lbus_play_mono2[] = {
0589     INTERM1_IN, DIGITAL_MIXER_IN1, 0xff
0590 };
0591 static const unsigned char lbus_play_noconv3[] = {
0592     DIGITAL_MIXER_IN2, 0xff
0593 };
0594 static const unsigned char lbus_play_stereo3[] = {
0595     INTER2_IN, DIGITAL_MIXER_IN2, 0xff
0596 };
0597 static const unsigned char lbus_play_mono3[] = {
0598     INTERM2_IN, DIGITAL_MIXER_IN2, 0xff
0599 };
0600 static const unsigned char lbus_rec_noconv1[] = {
0601     LBUS2ARM_FIFO5, 0xff
0602 };
0603 static const unsigned char lbus_rec_stereo1[] = {
0604     DECIM0_IN, LBUS2ARM_FIFO5, 0xff
0605 };
0606 static const unsigned char lbus_rec_mono1[] = {
0607     DECIMM3_IN, LBUS2ARM_FIFO5, 0xff
0608 };
0609 
0610 static const unsigned char play_ids[] = { 4, 1, 2, };
0611 static const unsigned char play_sources[] = {
0612     ARM2LBUS_FIFO4, ARM2LBUS_FIFO1, ARM2LBUS_FIFO2,
0613 };
0614 static const struct lbuspath lbus_play_paths[] = {
0615     {
0616      .noconv = lbus_play_noconv1,
0617      .stereo = lbus_play_stereo1,
0618      .mono = lbus_play_mono1,
0619      },
0620     {
0621      .noconv = lbus_play_noconv2,
0622      .stereo = lbus_play_stereo2,
0623      .mono = lbus_play_mono2,
0624      },
0625     {
0626      .noconv = lbus_play_noconv3,
0627      .stereo = lbus_play_stereo3,
0628      .mono = lbus_play_mono3,
0629      },
0630 };
0631 static const struct lbuspath lbus_rec_path = {
0632     .noconv = lbus_rec_noconv1,
0633     .stereo = lbus_rec_stereo1,
0634     .mono = lbus_rec_mono1,
0635 };
0636 
0637 #define FIRMWARE_VERSIONS 1
0638 static union firmware_version firmware_versions[] = {
0639     {
0640         .firmware = {
0641             .ASIC = 3,
0642             .CODEC = 2,
0643             .AUXDSP = 3,
0644             .PROG = 773,
0645         },
0646     },
0647 };
0648 
0649 static u32 atoh(const unsigned char *in, unsigned int len)
0650 {
0651     u32 sum = 0;
0652     unsigned int mult = 1;
0653     unsigned char c;
0654 
0655     while (len) {
0656         int value;
0657 
0658         c = in[len - 1];
0659         value = hex_to_bin(c);
0660         if (value >= 0)
0661             sum += mult * value;
0662         mult *= 16;
0663         --len;
0664     }
0665     return sum;
0666 }
0667 
0668 static int senddata(struct cmdif *cif, const unsigned char *in, u32 offset)
0669 {
0670     u32 addr;
0671     u32 data;
0672     u32 i;
0673     const unsigned char *p;
0674 
0675     i = atoh(&in[1], 2);
0676     addr = offset + atoh(&in[3], 4);
0677     if (SEND_SMEM(cif, 0, addr) != 0)
0678         return -EACCES;
0679     p = in + 9;
0680     while (i) {
0681         data = atoh(p, 8);
0682         if (SEND_WMEM(cif, 2,
0683                   ((data & 0x0f0f0f0f) << 4) | ((data & 0xf0f0f0f0)
0684                                 >> 4)))
0685             return -EACCES;
0686         i -= 4;
0687         p += 8;
0688     }
0689     return 0;
0690 }
0691 
0692 static int loadfirmware(struct cmdif *cif, const unsigned char *img,
0693             unsigned int size)
0694 {
0695     const unsigned char *in;
0696     u32 laddr, saddr, t, val;
0697     int err = 0;
0698 
0699     laddr = saddr = 0;
0700     while (size > 0 && err == 0) {
0701         in = img;
0702         if (in[0] == ':') {
0703             t = atoh(&in[7], 2);
0704             switch (t) {
0705             case DATA_REC:
0706                 err = senddata(cif, in, laddr + saddr);
0707                 break;
0708             case EXT_SEG_ADDR_REC:
0709                 saddr = atoh(&in[9], 4) << 4;
0710                 break;
0711             case EXT_LIN_ADDR_REC:
0712                 laddr = atoh(&in[9], 4) << 16;
0713                 break;
0714             case EXT_GOTO_CMD_REC:
0715                 val = atoh(&in[9], 8);
0716                 if (SEND_GOTO(cif, val) != 0)
0717                     err = -EACCES;
0718                 break;
0719             case EXT_END_OF_FILE:
0720                 size = 0;
0721                 break;
0722             default:
0723                 break;
0724             }
0725             while (size > 0) {
0726                 size--;
0727                 if (*img++ == '\n')
0728                     break;
0729             }
0730         }
0731     }
0732     snd_printdd("load firmware return %d\n", err);
0733     return err;
0734 }
0735 
0736 static void
0737 alloclbuspath(struct cmdif *cif, unsigned char source,
0738           const unsigned char *path, unsigned char *mixer, unsigned char *s)
0739 {
0740     while (*path != 0xff) {
0741         unsigned char sink, type;
0742 
0743         sink = *path & (~SPLIT_PATH);
0744         if (sink != E2SINK_MAX) {
0745             snd_printdd("alloc path 0x%x->0x%x\n", source, sink);
0746             SEND_PSEL(cif, source, sink);
0747             source = lbusin2out[sink][0];
0748             type = lbusin2out[sink][1];
0749             if (type == LS_MIXER_IN) {
0750                 if (mixer)
0751                     *mixer = sink - DIGITAL_MIXER_IN0;
0752             }
0753             if (type == LS_SRC_DECIMATORM ||
0754                 type == LS_SRC_DECIMATOR ||
0755                 type == LS_SRC_INTERPOLATORM ||
0756                 type == LS_SRC_INTERPOLATOR) {
0757                 if (s) {
0758                     if (s[0] != 0xff)
0759                         s[1] = sink;
0760                     else
0761                         s[0] = sink;
0762                 }
0763             }
0764         }
0765         if (*path++ & SPLIT_PATH) {
0766             const unsigned char *npath = path;
0767 
0768             while (*npath != 0xff)
0769                 npath++;
0770             alloclbuspath(cif, source + 1, ++npath, mixer, s);
0771         }
0772     }
0773 }
0774 
0775 static void
0776 freelbuspath(struct cmdif *cif, unsigned char source, const unsigned char *path)
0777 {
0778     while (*path != 0xff) {
0779         unsigned char sink;
0780 
0781         sink = *path & (~SPLIT_PATH);
0782         if (sink != E2SINK_MAX) {
0783             snd_printdd("free path 0x%x->0x%x\n", source, sink);
0784             SEND_PCLR(cif, source, sink);
0785             source = lbusin2out[sink][0];
0786         }
0787         if (*path++ & SPLIT_PATH) {
0788             const unsigned char *npath = path;
0789 
0790             while (*npath != 0xff)
0791                 npath++;
0792             freelbuspath(cif, source + 1, ++npath);
0793         }
0794     }
0795 }
0796 
0797 static int writearm(struct cmdif *cif, u32 addr, u32 data, u32 mask)
0798 {
0799     union cmdret rptr = CMDRET_ZERO;
0800     unsigned int i = MAX_WRITE_RETRY;
0801     int flag = 1;
0802 
0803     SEND_RMEM(cif, 0x02, addr, &rptr);
0804     rptr.retlongs[0] &= (~mask);
0805 
0806     while (--i) {
0807         SEND_SMEM(cif, 0x01, addr);
0808         SEND_WMEM(cif, 0x02, (rptr.retlongs[0] | data));
0809         SEND_RMEM(cif, 0x02, addr, &rptr);
0810         if ((rptr.retlongs[0] & data) == data) {
0811             flag = 0;
0812             break;
0813         } else
0814             rptr.retlongs[0] &= ~mask;
0815     }
0816     snd_printdd("send arm 0x%x 0x%x 0x%x return %d\n", addr, data, mask,
0817             flag);
0818     return flag;
0819 }
0820 
0821 static int sendcmd(struct cmdif *cif, u32 flags, u32 cmd, u32 parm,
0822            union cmdret *ret)
0823 {
0824     int i, j;
0825     int err;
0826     unsigned int time = 0;
0827     unsigned long irqflags;
0828     struct riptideport *hwport;
0829     struct cmdport *cmdport = NULL;
0830 
0831     if (snd_BUG_ON(!cif))
0832         return -EINVAL;
0833 
0834     hwport = cif->hwport;
0835     if (cif->errcnt > MAX_ERROR_COUNT) {
0836         if (cif->is_reset) {
0837             snd_printk(KERN_ERR
0838                    "Riptide: Too many failed cmds, reinitializing\n");
0839             if (riptide_reset(cif, NULL) == 0) {
0840                 cif->errcnt = 0;
0841                 return -EIO;
0842             }
0843         }
0844         snd_printk(KERN_ERR "Riptide: Initialization failed.\n");
0845         return -EINVAL;
0846     }
0847     if (ret) {
0848         ret->retlongs[0] = 0;
0849         ret->retlongs[1] = 0;
0850     }
0851     i = 0;
0852     spin_lock_irqsave(&cif->lock, irqflags);
0853     while (i++ < CMDIF_TIMEOUT && !IS_READY(cif->hwport))
0854         udelay(10);
0855     if (i > CMDIF_TIMEOUT) {
0856         err = -EBUSY;
0857         goto errout;
0858     }
0859 
0860     err = 0;
0861     for (j = 0, time = 0; time < CMDIF_TIMEOUT; j++, time += 2) {
0862         cmdport = &(hwport->port[j % 2]);
0863         if (IS_DATF(cmdport)) { /* free pending data */
0864             READ_PORT_ULONG(cmdport->data1);
0865             READ_PORT_ULONG(cmdport->data2);
0866         }
0867         if (IS_CMDE(cmdport)) {
0868             if (flags & PARM)   /* put data */
0869                 WRITE_PORT_ULONG(cmdport->data2, parm);
0870             WRITE_PORT_ULONG(cmdport->data1, cmd);  /* write cmd */
0871             if ((flags & RESP) && ret) {
0872                 while (!IS_DATF(cmdport) &&
0873                        time < CMDIF_TIMEOUT) {
0874                     udelay(10);
0875                     time++;
0876                 }
0877                 if (time < CMDIF_TIMEOUT) { /* read response */
0878                     ret->retlongs[0] =
0879                         READ_PORT_ULONG(cmdport->data1);
0880                     ret->retlongs[1] =
0881                         READ_PORT_ULONG(cmdport->data2);
0882                 } else {
0883                     err = -ENOSYS;
0884                     goto errout;
0885                 }
0886             }
0887             break;
0888         }
0889         udelay(20);
0890     }
0891     if (time == CMDIF_TIMEOUT) {
0892         err = -ENODATA;
0893         goto errout;
0894     }
0895     spin_unlock_irqrestore(&cif->lock, irqflags);
0896 
0897     cif->cmdcnt++;      /* update command statistics */
0898     cif->cmdtime += time;
0899     if (time > cif->cmdtimemax)
0900         cif->cmdtimemax = time;
0901     if (time < cif->cmdtimemin)
0902         cif->cmdtimemin = time;
0903     if ((cif->cmdcnt) % 1000 == 0)
0904         snd_printdd
0905             ("send cmd %d time: %d mintime: %d maxtime %d err: %d\n",
0906              cif->cmdcnt, cif->cmdtime, cif->cmdtimemin,
0907              cif->cmdtimemax, cif->errcnt);
0908     return 0;
0909 
0910       errout:
0911     cif->errcnt++;
0912     spin_unlock_irqrestore(&cif->lock, irqflags);
0913     snd_printdd
0914         ("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",
0915          cif->cmdcnt, (int)((void *)&(cmdport->stat) - (void *)hwport),
0916          flags, cmd, parm, ret ? ret->retlongs[0] : 0,
0917          ret ? ret->retlongs[1] : 0, IS_CMDE(cmdport), IS_DATF(cmdport),
0918          err);
0919     return err;
0920 }
0921 
0922 static int
0923 setmixer(struct cmdif *cif, short num, unsigned short rval, unsigned short lval)
0924 {
0925     union cmdret rptr = CMDRET_ZERO;
0926     int i = 0;
0927 
0928     snd_printdd("sent mixer %d: 0x%x 0x%x\n", num, rval, lval);
0929     do {
0930         SEND_SDGV(cif, num, num, rval, lval);
0931         SEND_RDGV(cif, num, num, &rptr);
0932         if (rptr.retwords[0] == lval && rptr.retwords[1] == rval)
0933             return 0;
0934     } while (i++ < MAX_WRITE_RETRY);
0935     snd_printdd("sent mixer failed\n");
0936     return -EIO;
0937 }
0938 
0939 static int getpaths(struct cmdif *cif, unsigned char *o)
0940 {
0941     unsigned char src[E2SINK_MAX];
0942     unsigned char sink[E2SINK_MAX];
0943     int i, j = 0;
0944 
0945     for (i = 0; i < E2SINK_MAX; i++) {
0946         getsourcesink(cif, i, i, &src[i], &sink[i]);
0947         if (sink[i] < E2SINK_MAX) {
0948             o[j++] = sink[i];
0949             o[j++] = i;
0950         }
0951     }
0952     return j;
0953 }
0954 
0955 static int
0956 getsourcesink(struct cmdif *cif, unsigned char source, unsigned char sink,
0957           unsigned char *a, unsigned char *b)
0958 {
0959     union cmdret rptr = CMDRET_ZERO;
0960 
0961     if (SEND_RSSV(cif, source, sink, &rptr) &&
0962         SEND_RSSV(cif, source, sink, &rptr))
0963         return -EIO;
0964     *a = rptr.retbytes[0];
0965     *b = rptr.retbytes[1];
0966     snd_printdd("getsourcesink 0x%x 0x%x\n", *a, *b);
0967     return 0;
0968 }
0969 
0970 static int
0971 getsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int *rate)
0972 {
0973     unsigned char *s;
0974     unsigned int p[2] = { 0, 0 };
0975     int i;
0976     union cmdret rptr = CMDRET_ZERO;
0977 
0978     s = intdec;
0979     for (i = 0; i < 2; i++) {
0980         if (*s != 0xff) {
0981             if (SEND_RSRC(cif, *s, &rptr) &&
0982                 SEND_RSRC(cif, *s, &rptr))
0983                 return -EIO;
0984             p[i] += rptr.retwords[1];
0985             p[i] *= rptr.retwords[2];
0986             p[i] += rptr.retwords[3];
0987             p[i] /= 65536;
0988         }
0989         s++;
0990     }
0991     if (p[0]) {
0992         if (p[1] != p[0])
0993             snd_printdd("rates differ %d %d\n", p[0], p[1]);
0994         *rate = (unsigned int)p[0];
0995     } else
0996         *rate = (unsigned int)p[1];
0997     snd_printdd("getsampleformat %d %d %d\n", intdec[0], intdec[1], *rate);
0998     return 0;
0999 }
1000 
1001 static int
1002 setsampleformat(struct cmdif *cif,
1003         unsigned char mixer, unsigned char id,
1004         unsigned char channels, snd_pcm_format_t format)
1005 {
1006     unsigned char w, ch, sig, order;
1007 
1008     snd_printdd
1009         ("setsampleformat mixer: %d id: %d channels: %d format: %d\n",
1010          mixer, id, channels, format);
1011     ch = channels == 1;
1012     w = snd_pcm_format_width(format) == 8;
1013     sig = snd_pcm_format_unsigned(format) != 0;
1014     order = snd_pcm_format_big_endian(format) != 0;
1015 
1016     if (SEND_SETF(cif, mixer, w, ch, order, sig, id) &&
1017         SEND_SETF(cif, mixer, w, ch, order, sig, id)) {
1018         snd_printdd("setsampleformat failed\n");
1019         return -EIO;
1020     }
1021     return 0;
1022 }
1023 
1024 static int
1025 setsamplerate(struct cmdif *cif, unsigned char *intdec, unsigned int rate)
1026 {
1027     u32 D, M, N;
1028     union cmdret rptr = CMDRET_ZERO;
1029     int i;
1030 
1031     snd_printdd("setsamplerate intdec: %d,%d rate: %d\n", intdec[0],
1032             intdec[1], rate);
1033     D = 48000;
1034     M = ((rate == 48000) ? 47999 : rate) * 65536;
1035     N = M % D;
1036     M /= D;
1037     for (i = 0; i < 2; i++) {
1038         if (*intdec != 0xff) {
1039             do {
1040                 SEND_SSRC(cif, *intdec, D, M, N);
1041                 SEND_RSRC(cif, *intdec, &rptr);
1042             } while (rptr.retwords[1] != D &&
1043                  rptr.retwords[2] != M &&
1044                  rptr.retwords[3] != N &&
1045                  i++ < MAX_WRITE_RETRY);
1046             if (i > MAX_WRITE_RETRY) {
1047                 snd_printdd("sent samplerate %d: %d failed\n",
1048                         *intdec, rate);
1049                 return -EIO;
1050             }
1051         }
1052         intdec++;
1053     }
1054     return 0;
1055 }
1056 
1057 static int
1058 getmixer(struct cmdif *cif, short num, unsigned short *rval,
1059      unsigned short *lval)
1060 {
1061     union cmdret rptr = CMDRET_ZERO;
1062 
1063     if (SEND_RDGV(cif, num, num, &rptr) && SEND_RDGV(cif, num, num, &rptr))
1064         return -EIO;
1065     *rval = rptr.retwords[0];
1066     *lval = rptr.retwords[1];
1067     snd_printdd("got mixer %d: 0x%x 0x%x\n", num, *rval, *lval);
1068     return 0;
1069 }
1070 
1071 static irqreturn_t riptide_handleirq(int irq, void *dev_id)
1072 {
1073     struct snd_riptide *chip = dev_id;
1074     struct cmdif *cif = chip->cif;
1075     struct snd_pcm_substream *substream[PLAYBACK_SUBSTREAMS + 1];
1076     struct snd_pcm_runtime *runtime;
1077     struct pcmhw *data = NULL;
1078     unsigned int pos, period_bytes;
1079     struct sgd *c;
1080     int i, j;
1081     unsigned int flag;
1082 
1083     if (!cif)
1084         return IRQ_HANDLED;
1085 
1086     for (i = 0; i < PLAYBACK_SUBSTREAMS; i++)
1087         substream[i] = chip->playback_substream[i];
1088     substream[i] = chip->capture_substream;
1089     for (i = 0; i < PLAYBACK_SUBSTREAMS + 1; i++) {
1090         if (!substream[i])
1091             continue;
1092         runtime = substream[i]->runtime;
1093         if (!runtime)
1094             continue;
1095         data = runtime->private_data;
1096         if (!data)
1097             continue;
1098         if (data->state != ST_STOP) {
1099             pos = 0;
1100             for (j = 0; j < data->pages; j++) {
1101                 c = &data->sgdbuf[j];
1102                 flag = le32_to_cpu(c->dwStat_Ctl);
1103                 if (flag & EOB_STATUS)
1104                     pos += le32_to_cpu(c->dwSegLen);
1105                 if (flag & EOC_STATUS)
1106                     pos += le32_to_cpu(c->dwSegLen);
1107                 if ((flag & EOS_STATUS)
1108                     && (data->state == ST_PLAY)) {
1109                     data->state = ST_STOP;
1110                     snd_printk(KERN_ERR
1111                            "Riptide: DMA stopped unexpectedly\n");
1112                 }
1113                 c->dwStat_Ctl =
1114                     cpu_to_le32(flag &
1115                         ~(EOS_STATUS | EOB_STATUS |
1116                           EOC_STATUS));
1117             }
1118             data->pointer += pos;
1119             pos += data->oldpos;
1120             if (data->state != ST_STOP) {
1121                 period_bytes =
1122                     frames_to_bytes(runtime,
1123                             runtime->period_size);
1124                 snd_printdd
1125                     ("interrupt 0x%x after 0x%lx of 0x%lx frames in period\n",
1126                      READ_AUDIO_STATUS(cif->hwport),
1127                      bytes_to_frames(runtime, pos),
1128                      runtime->period_size);
1129                 j = 0;
1130                 if (pos >= period_bytes) {
1131                     j++;
1132                     while (pos >= period_bytes)
1133                         pos -= period_bytes;
1134                 }
1135                 data->oldpos = pos;
1136                 if (j > 0)
1137                     snd_pcm_period_elapsed(substream[i]);
1138             }
1139         }
1140     }
1141 
1142     return IRQ_HANDLED;
1143 }
1144 
1145 #ifdef CONFIG_PM_SLEEP
1146 static int riptide_suspend(struct device *dev)
1147 {
1148     struct snd_card *card = dev_get_drvdata(dev);
1149     struct snd_riptide *chip = card->private_data;
1150 
1151     chip->in_suspend = 1;
1152     snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1153     snd_ac97_suspend(chip->ac97);
1154     return 0;
1155 }
1156 
1157 static int riptide_resume(struct device *dev)
1158 {
1159     struct snd_card *card = dev_get_drvdata(dev);
1160     struct snd_riptide *chip = card->private_data;
1161 
1162     snd_riptide_initialize(chip);
1163     snd_ac97_resume(chip->ac97);
1164     snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1165     chip->in_suspend = 0;
1166     return 0;
1167 }
1168 
1169 static SIMPLE_DEV_PM_OPS(riptide_pm, riptide_suspend, riptide_resume);
1170 #define RIPTIDE_PM_OPS  &riptide_pm
1171 #else
1172 #define RIPTIDE_PM_OPS  NULL
1173 #endif /* CONFIG_PM_SLEEP */
1174 
1175 static int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
1176 {
1177     union firmware_version firmware = { .ret = CMDRET_ZERO };
1178     int i, timeout, err;
1179 
1180     for (i = 0; i < 2; i++) {
1181         WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
1182         WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
1183     }
1184     SET_GRESET(cif->hwport);
1185     udelay(100);
1186     UNSET_GRESET(cif->hwport);
1187     udelay(100);
1188 
1189     for (timeout = 100000; --timeout; udelay(10)) {
1190         if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
1191             break;
1192     }
1193     if (!timeout) {
1194         snd_printk(KERN_ERR
1195                "Riptide: device not ready, audio status: 0x%x "
1196                "ready: %d gerr: %d\n",
1197                READ_AUDIO_STATUS(cif->hwport),
1198                IS_READY(cif->hwport), IS_GERR(cif->hwport));
1199         return -EIO;
1200     } else {
1201         snd_printdd
1202             ("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
1203              READ_AUDIO_STATUS(cif->hwport),
1204              IS_READY(cif->hwport), IS_GERR(cif->hwport));
1205     }
1206 
1207     SEND_GETV(cif, &firmware.ret);
1208     snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
1209             firmware.firmware.ASIC, firmware.firmware.CODEC,
1210             firmware.firmware.AUXDSP, firmware.firmware.PROG);
1211 
1212     if (!chip)
1213         return 1;
1214 
1215     for (i = 0; i < FIRMWARE_VERSIONS; i++) {
1216         if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
1217             return 1; /* OK */
1218 
1219     }
1220 
1221     snd_printdd("Writing Firmware\n");
1222     if (!chip->fw_entry) {
1223         err = request_firmware(&chip->fw_entry, "riptide.hex",
1224                        &chip->pci->dev);
1225         if (err) {
1226             snd_printk(KERN_ERR
1227                    "Riptide: Firmware not available %d\n", err);
1228             return -EIO;
1229         }
1230     }
1231     err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
1232     if (err) {
1233         snd_printk(KERN_ERR
1234                "Riptide: Could not load firmware %d\n", err);
1235         return err;
1236     }
1237 
1238     chip->firmware = firmware;
1239 
1240     return 1; /* OK */
1241 }
1242 
1243 static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
1244 {
1245     union cmdret rptr = CMDRET_ZERO;
1246     int err, tries;
1247 
1248     if (!cif)
1249         return -EINVAL;
1250 
1251     cif->cmdcnt = 0;
1252     cif->cmdtime = 0;
1253     cif->cmdtimemax = 0;
1254     cif->cmdtimemin = 0xffffffff;
1255     cif->errcnt = 0;
1256     cif->is_reset = 0;
1257 
1258     tries = RESET_TRIES;
1259     do {
1260         err = try_to_load_firmware(cif, chip);
1261         if (err < 0)
1262             return err;
1263     } while (!err && --tries);
1264 
1265     SEND_SACR(cif, 0, AC97_RESET);
1266     SEND_RACR(cif, AC97_RESET, &rptr);
1267     snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
1268 
1269     SEND_PLST(cif, 0);
1270     SEND_SLST(cif, 0);
1271     SEND_DLST(cif, 0);
1272     SEND_ALST(cif, 0);
1273     SEND_KDMA(cif);
1274 
1275     writearm(cif, 0x301F8, 1, 1);
1276     writearm(cif, 0x301F4, 1, 1);
1277 
1278     SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
1279           MODEM_SPLITTER, MODEM_MIXER);
1280     setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
1281     alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
1282 
1283     SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
1284           FM_MIXER);
1285     setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
1286     writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
1287     writearm(cif, 0x301A8, 0x02, 0x00000002);
1288     writearm(cif, 0x30264, 0x08, 0xffffffff);
1289     alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
1290 
1291     SEND_SSRC(cif, I2S_INTDEC, 48000,
1292           ((u32) I2S_RATE * 65536) / 48000,
1293           ((u32) I2S_RATE * 65536) % 48000);
1294     SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
1295           I2S_MIXER);
1296     SEND_SI2S(cif, 1);
1297     alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
1298     alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
1299     alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
1300 
1301     SET_AIACK(cif->hwport);
1302     SET_AIE(cif->hwport);
1303     SET_AIACK(cif->hwport);
1304     cif->is_reset = 1;
1305 
1306     return 0;
1307 }
1308 
1309 static const struct snd_pcm_hardware snd_riptide_playback = {
1310     .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1311          SNDRV_PCM_INFO_BLOCK_TRANSFER |
1312          SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1313     .formats =
1314         SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1315         | SNDRV_PCM_FMTBIT_U16_LE,
1316     .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1317     .rate_min = 5500,
1318     .rate_max = 48000,
1319     .channels_min = 1,
1320     .channels_max = 2,
1321     .buffer_bytes_max = (64 * 1024),
1322     .period_bytes_min = PAGE_SIZE >> 1,
1323     .period_bytes_max = PAGE_SIZE << 8,
1324     .periods_min = 2,
1325     .periods_max = 64,
1326     .fifo_size = 0,
1327 };
1328 static const struct snd_pcm_hardware snd_riptide_capture = {
1329     .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1330          SNDRV_PCM_INFO_BLOCK_TRANSFER |
1331          SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
1332     .formats =
1333         SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
1334         | SNDRV_PCM_FMTBIT_U16_LE,
1335     .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1336     .rate_min = 5500,
1337     .rate_max = 48000,
1338     .channels_min = 1,
1339     .channels_max = 2,
1340     .buffer_bytes_max = (64 * 1024),
1341     .period_bytes_min = PAGE_SIZE >> 1,
1342     .period_bytes_max = PAGE_SIZE << 3,
1343     .periods_min = 2,
1344     .periods_max = 64,
1345     .fifo_size = 0,
1346 };
1347 
1348 static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
1349                          *substream)
1350 {
1351     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1352     struct snd_pcm_runtime *runtime = substream->runtime;
1353     struct pcmhw *data = get_pcmhwdev(substream);
1354     struct cmdif *cif = chip->cif;
1355     union cmdret rptr = CMDRET_ZERO;
1356     snd_pcm_uframes_t ret;
1357 
1358     SEND_GPOS(cif, 0, data->id, &rptr);
1359     if (data->size && runtime->period_size) {
1360         snd_printdd
1361             ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
1362              data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
1363              bytes_to_frames(runtime, rptr.retlongs[1]),
1364              bytes_to_frames(runtime,
1365                      rptr.retlongs[1]) % runtime->period_size);
1366         if (rptr.retlongs[1] > data->pointer)
1367             ret =
1368                 bytes_to_frames(runtime,
1369                         rptr.retlongs[1] % data->size);
1370         else
1371             ret =
1372                 bytes_to_frames(runtime,
1373                         data->pointer % data->size);
1374     } else {
1375         snd_printdd("stream not started or strange parms (%d %ld)\n",
1376                 data->size, runtime->period_size);
1377         ret = bytes_to_frames(runtime, 0);
1378     }
1379     return ret;
1380 }
1381 
1382 static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
1383 {
1384     int i, j;
1385     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1386     struct pcmhw *data = get_pcmhwdev(substream);
1387     struct cmdif *cif = chip->cif;
1388     union cmdret rptr = CMDRET_ZERO;
1389 
1390     spin_lock(&chip->lock);
1391     switch (cmd) {
1392     case SNDRV_PCM_TRIGGER_START:
1393     case SNDRV_PCM_TRIGGER_RESUME:
1394         if (!(data->state & ST_PLAY)) {
1395             SEND_SSTR(cif, data->id, data->sgdlist.addr);
1396             SET_AIE(cif->hwport);
1397             data->state = ST_PLAY;
1398             if (data->mixer != 0xff)
1399                 setmixer(cif, data->mixer, 0x7fff, 0x7fff);
1400             chip->openstreams++;
1401             data->oldpos = 0;
1402             data->pointer = 0;
1403         }
1404         break;
1405     case SNDRV_PCM_TRIGGER_STOP:
1406     case SNDRV_PCM_TRIGGER_SUSPEND:
1407         if (data->mixer != 0xff)
1408             setmixer(cif, data->mixer, 0, 0);
1409         setmixer(cif, data->mixer, 0, 0);
1410         SEND_KSTR(cif, data->id);
1411         data->state = ST_STOP;
1412         chip->openstreams--;
1413         j = 0;
1414         do {
1415             i = rptr.retlongs[1];
1416             SEND_GPOS(cif, 0, data->id, &rptr);
1417             udelay(1);
1418         } while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
1419         if (j > MAX_WRITE_RETRY)
1420             snd_printk(KERN_ERR "Riptide: Could not stop stream!");
1421         break;
1422     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1423         if (!(data->state & ST_PAUSE)) {
1424             SEND_PSTR(cif, data->id);
1425             data->state |= ST_PAUSE;
1426             chip->openstreams--;
1427         }
1428         break;
1429     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1430         if (data->state & ST_PAUSE) {
1431             SEND_SSTR(cif, data->id, data->sgdlist.addr);
1432             data->state &= ~ST_PAUSE;
1433             chip->openstreams++;
1434         }
1435         break;
1436     default:
1437         spin_unlock(&chip->lock);
1438         return -EINVAL;
1439     }
1440     spin_unlock(&chip->lock);
1441     return 0;
1442 }
1443 
1444 static int snd_riptide_prepare(struct snd_pcm_substream *substream)
1445 {
1446     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1447     struct snd_pcm_runtime *runtime = substream->runtime;
1448     struct pcmhw *data = get_pcmhwdev(substream);
1449     struct cmdif *cif = chip->cif;
1450     const unsigned char *lbuspath = NULL;
1451     unsigned int rate, channels;
1452     int err = 0;
1453     snd_pcm_format_t format;
1454 
1455     if (snd_BUG_ON(!cif || !data))
1456         return -EINVAL;
1457 
1458     snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
1459             runtime->channels, runtime->format, runtime->rate);
1460 
1461     spin_lock_irq(&chip->lock);
1462     channels = runtime->channels;
1463     format = runtime->format;
1464     rate = runtime->rate;
1465     switch (channels) {
1466     case 1:
1467         if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1468             lbuspath = data->paths.noconv;
1469         else
1470             lbuspath = data->paths.mono;
1471         break;
1472     case 2:
1473         if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
1474             lbuspath = data->paths.noconv;
1475         else
1476             lbuspath = data->paths.stereo;
1477         break;
1478     }
1479     snd_printdd("use sgdlist at 0x%p\n",
1480             data->sgdlist.area);
1481     if (data->sgdlist.area) {
1482         unsigned int i, j, size, pages, f, pt, period;
1483         struct sgd *c, *p = NULL;
1484 
1485         size = frames_to_bytes(runtime, runtime->buffer_size);
1486         period = frames_to_bytes(runtime, runtime->period_size);
1487         f = PAGE_SIZE;
1488         while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
1489             f = f >> 1;
1490         pages = DIV_ROUND_UP(size, f);
1491         data->size = size;
1492         data->pages = pages;
1493         snd_printdd
1494             ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
1495              size, pages, f, period);
1496         pt = 0;
1497         j = 0;
1498         for (i = 0; i < pages; i++) {
1499             unsigned int ofs, addr;
1500             c = &data->sgdbuf[i];
1501             if (p)
1502                 p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
1503                                 (i *
1504                                  sizeof(struct
1505                                     sgd)));
1506             c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
1507             ofs = j << PAGE_SHIFT;
1508             addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
1509             c->dwSegPtrPhys = cpu_to_le32(addr);
1510             pt = (pt + f) % PAGE_SIZE;
1511             if (pt == 0)
1512                 j++;
1513             c->dwSegLen = cpu_to_le32(f);
1514             c->dwStat_Ctl =
1515                 cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
1516                     IEOC_ENABLE);
1517             p = c;
1518             size -= f;
1519         }
1520         data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
1521     }
1522     if (lbuspath && lbuspath != data->lbuspath) {
1523         if (data->lbuspath)
1524             freelbuspath(cif, data->source, data->lbuspath);
1525         alloclbuspath(cif, data->source, lbuspath,
1526                   &data->mixer, data->intdec);
1527         data->lbuspath = lbuspath;
1528         data->rate = 0;
1529     }
1530     if (data->rate != rate || data->format != format ||
1531         data->channels != channels) {
1532         data->rate = rate;
1533         data->format = format;
1534         data->channels = channels;
1535         if (setsampleformat
1536             (cif, data->mixer, data->id, channels, format)
1537             || setsamplerate(cif, data->intdec, rate))
1538             err = -EIO;
1539     }
1540     spin_unlock_irq(&chip->lock);
1541     return err;
1542 }
1543 
1544 static int
1545 snd_riptide_hw_params(struct snd_pcm_substream *substream,
1546               struct snd_pcm_hw_params *hw_params)
1547 {
1548     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1549     struct pcmhw *data = get_pcmhwdev(substream);
1550     struct snd_dma_buffer *sgdlist = &data->sgdlist;
1551     int err;
1552 
1553     snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
1554             sgdlist->area, (unsigned long)sgdlist->addr,
1555             (int)sgdlist->bytes);
1556     if (sgdlist->area)
1557         snd_dma_free_pages(sgdlist);
1558     err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
1559                   sizeof(struct sgd) * (DESC_MAX_MASK + 1),
1560                   sgdlist);
1561     if (err < 0) {
1562         snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
1563                (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
1564         return err;
1565     }
1566     data->sgdbuf = (struct sgd *)sgdlist->area;
1567     return 0;
1568 }
1569 
1570 static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
1571 {
1572     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1573     struct pcmhw *data = get_pcmhwdev(substream);
1574     struct cmdif *cif = chip->cif;
1575 
1576     if (cif && data) {
1577         if (data->lbuspath)
1578             freelbuspath(cif, data->source, data->lbuspath);
1579         data->lbuspath = NULL;
1580         data->source = 0xff;
1581         data->intdec[0] = 0xff;
1582         data->intdec[1] = 0xff;
1583 
1584         if (data->sgdlist.area) {
1585             snd_dma_free_pages(&data->sgdlist);
1586             data->sgdlist.area = NULL;
1587         }
1588     }
1589     return 0;
1590 }
1591 
1592 static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
1593 {
1594     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1595     struct snd_pcm_runtime *runtime = substream->runtime;
1596     struct pcmhw *data;
1597     int sub_num = substream->number;
1598 
1599     chip->playback_substream[sub_num] = substream;
1600     runtime->hw = snd_riptide_playback;
1601 
1602     data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
1603     if (data == NULL)
1604         return -ENOMEM;
1605     data->paths = lbus_play_paths[sub_num];
1606     data->id = play_ids[sub_num];
1607     data->source = play_sources[sub_num];
1608     data->intdec[0] = 0xff;
1609     data->intdec[1] = 0xff;
1610     data->state = ST_STOP;
1611     runtime->private_data = data;
1612     return snd_pcm_hw_constraint_integer(runtime,
1613                          SNDRV_PCM_HW_PARAM_PERIODS);
1614 }
1615 
1616 static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
1617 {
1618     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1619     struct snd_pcm_runtime *runtime = substream->runtime;
1620     struct pcmhw *data;
1621 
1622     chip->capture_substream = substream;
1623     runtime->hw = snd_riptide_capture;
1624 
1625     data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
1626     if (data == NULL)
1627         return -ENOMEM;
1628     data->paths = lbus_rec_path;
1629     data->id = PADC;
1630     data->source = ACLNK2PADC;
1631     data->intdec[0] = 0xff;
1632     data->intdec[1] = 0xff;
1633     data->state = ST_STOP;
1634     runtime->private_data = data;
1635     return snd_pcm_hw_constraint_integer(runtime,
1636                          SNDRV_PCM_HW_PARAM_PERIODS);
1637 }
1638 
1639 static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
1640 {
1641     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1642     struct pcmhw *data = get_pcmhwdev(substream);
1643     int sub_num = substream->number;
1644 
1645     substream->runtime->private_data = NULL;
1646     chip->playback_substream[sub_num] = NULL;
1647     kfree(data);
1648     return 0;
1649 }
1650 
1651 static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
1652 {
1653     struct snd_riptide *chip = snd_pcm_substream_chip(substream);
1654     struct pcmhw *data = get_pcmhwdev(substream);
1655 
1656     substream->runtime->private_data = NULL;
1657     chip->capture_substream = NULL;
1658     kfree(data);
1659     return 0;
1660 }
1661 
1662 static const struct snd_pcm_ops snd_riptide_playback_ops = {
1663     .open = snd_riptide_playback_open,
1664     .close = snd_riptide_playback_close,
1665     .hw_params = snd_riptide_hw_params,
1666     .hw_free = snd_riptide_hw_free,
1667     .prepare = snd_riptide_prepare,
1668     .trigger = snd_riptide_trigger,
1669     .pointer = snd_riptide_pointer,
1670 };
1671 static const struct snd_pcm_ops snd_riptide_capture_ops = {
1672     .open = snd_riptide_capture_open,
1673     .close = snd_riptide_capture_close,
1674     .hw_params = snd_riptide_hw_params,
1675     .hw_free = snd_riptide_hw_free,
1676     .prepare = snd_riptide_prepare,
1677     .trigger = snd_riptide_trigger,
1678     .pointer = snd_riptide_pointer,
1679 };
1680 
1681 static int snd_riptide_pcm(struct snd_riptide *chip, int device)
1682 {
1683     struct snd_pcm *pcm;
1684     int err;
1685 
1686     err = snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
1687               &pcm);
1688     if (err < 0)
1689         return err;
1690     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1691             &snd_riptide_playback_ops);
1692     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1693             &snd_riptide_capture_ops);
1694     pcm->private_data = chip;
1695     pcm->info_flags = 0;
1696     strcpy(pcm->name, "RIPTIDE");
1697     chip->pcm = pcm;
1698     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
1699                        &chip->pci->dev, 64 * 1024, 128 * 1024);
1700     return 0;
1701 }
1702 
1703 static irqreturn_t
1704 snd_riptide_interrupt(int irq, void *dev_id)
1705 {
1706     struct snd_riptide *chip = dev_id;
1707     struct cmdif *cif = chip->cif;
1708     irqreturn_t ret = IRQ_HANDLED;
1709 
1710     if (cif) {
1711         chip->received_irqs++;
1712         if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
1713             IS_EOCIRQ(cif->hwport)) {
1714             chip->handled_irqs++;
1715             ret = IRQ_WAKE_THREAD;
1716         }
1717         if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
1718             chip->handled_irqs++;
1719             snd_mpu401_uart_interrupt(irq,
1720                           chip->rmidi->private_data);
1721         }
1722         SET_AIACK(cif->hwport);
1723     }
1724     return ret;
1725 }
1726 
1727 static void
1728 snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
1729             unsigned short val)
1730 {
1731     struct snd_riptide *chip = ac97->private_data;
1732     struct cmdif *cif = chip->cif;
1733     union cmdret rptr = CMDRET_ZERO;
1734     int i = 0;
1735 
1736     if (snd_BUG_ON(!cif))
1737         return;
1738 
1739     snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
1740     do {
1741         SEND_SACR(cif, val, reg);
1742         SEND_RACR(cif, reg, &rptr);
1743     } while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
1744     if (i > MAX_WRITE_RETRY)
1745         snd_printdd("Write AC97 reg failed\n");
1746 }
1747 
1748 static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
1749                          unsigned short reg)
1750 {
1751     struct snd_riptide *chip = ac97->private_data;
1752     struct cmdif *cif = chip->cif;
1753     union cmdret rptr = CMDRET_ZERO;
1754 
1755     if (snd_BUG_ON(!cif))
1756         return 0;
1757 
1758     if (SEND_RACR(cif, reg, &rptr) != 0)
1759         SEND_RACR(cif, reg, &rptr);
1760     snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
1761     return rptr.retwords[1];
1762 }
1763 
1764 static int snd_riptide_initialize(struct snd_riptide *chip)
1765 {
1766     struct cmdif *cif;
1767     unsigned int device_id;
1768     int err;
1769 
1770     if (snd_BUG_ON(!chip))
1771         return -EINVAL;
1772 
1773     cif = chip->cif;
1774     if (!cif) {
1775         cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL);
1776         if (!cif)
1777             return -ENOMEM;
1778         cif->hwport = (struct riptideport *)chip->port;
1779         spin_lock_init(&cif->lock);
1780         chip->cif = cif;
1781     }
1782     cif->is_reset = 0;
1783     err = riptide_reset(cif, chip);
1784     if (err)
1785         return err;
1786     device_id = chip->device_id;
1787     switch (device_id) {
1788     case 0x4310:
1789     case 0x4320:
1790     case 0x4330:
1791         snd_printdd("Modem enable?\n");
1792         SEND_SETDPLL(cif);
1793         break;
1794     }
1795     snd_printdd("Enabling MPU IRQs\n");
1796     if (chip->rmidi)
1797         SET_EMPUIRQ(cif->hwport);
1798     return err;
1799 }
1800 
1801 static void snd_riptide_free(struct snd_card *card)
1802 {
1803     struct snd_riptide *chip = card->private_data;
1804     struct cmdif *cif;
1805 
1806     cif = chip->cif;
1807     if (cif) {
1808         SET_GRESET(cif->hwport);
1809         udelay(100);
1810         UNSET_GRESET(cif->hwport);
1811         kfree(chip->cif);
1812     }
1813     release_firmware(chip->fw_entry);
1814 }
1815 
1816 static int
1817 snd_riptide_create(struct snd_card *card, struct pci_dev *pci)
1818 {
1819     struct snd_riptide *chip = card->private_data;
1820     struct riptideport *hwport;
1821     int err;
1822 
1823     err = pcim_enable_device(pci);
1824     if (err < 0)
1825         return err;
1826 
1827     spin_lock_init(&chip->lock);
1828     chip->card = card;
1829     chip->pci = pci;
1830     chip->irq = -1;
1831     chip->openstreams = 0;
1832     chip->port = pci_resource_start(pci, 0);
1833     chip->received_irqs = 0;
1834     chip->handled_irqs = 0;
1835     chip->cif = NULL;
1836     card->private_free = snd_riptide_free;
1837 
1838     err = pci_request_regions(pci, "RIPTIDE");
1839     if (err < 0)
1840         return err;
1841     hwport = (struct riptideport *)chip->port;
1842     UNSET_AIE(hwport);
1843 
1844     if (devm_request_threaded_irq(&pci->dev, pci->irq,
1845                       snd_riptide_interrupt,
1846                       riptide_handleirq, IRQF_SHARED,
1847                       KBUILD_MODNAME, chip)) {
1848         snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
1849                pci->irq);
1850         return -EBUSY;
1851     }
1852     chip->irq = pci->irq;
1853     card->sync_irq = chip->irq;
1854     chip->device_id = pci->device;
1855     pci_set_master(pci);
1856     err = snd_riptide_initialize(chip);
1857     if (err < 0)
1858         return err;
1859 
1860     return 0;
1861 }
1862 
1863 static void
1864 snd_riptide_proc_read(struct snd_info_entry *entry,
1865               struct snd_info_buffer *buffer)
1866 {
1867     struct snd_riptide *chip = entry->private_data;
1868     struct pcmhw *data;
1869     int i;
1870     struct cmdif *cif = NULL;
1871     unsigned char p[256];
1872     unsigned short rval = 0, lval = 0;
1873     unsigned int rate;
1874 
1875     if (!chip)
1876         return;
1877 
1878     snd_iprintf(buffer, "%s\n\n", chip->card->longname);
1879     snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
1880             chip->device_id, chip->handled_irqs, chip->received_irqs);
1881     for (i = 0; i < 64; i += 4)
1882         snd_iprintf(buffer, "%c%02x: %08x",
1883                 (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
1884     cif = chip->cif;
1885     if (cif) {
1886         snd_iprintf(buffer,
1887                 "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
1888                 chip->firmware.firmware.ASIC,
1889                 chip->firmware.firmware.CODEC,
1890                 chip->firmware.firmware.AUXDSP,
1891                 chip->firmware.firmware.PROG);
1892         snd_iprintf(buffer, "\nDigital mixer:");
1893         for (i = 0; i < 12; i++) {
1894             getmixer(cif, i, &rval, &lval);
1895             snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
1896         }
1897         snd_iprintf(buffer,
1898                 "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
1899                 cif->cmdcnt, cif->errcnt,
1900                 cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
1901     }
1902     snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
1903     for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
1904         if (!chip->playback_substream[i] ||
1905             !chip->playback_substream[i]->runtime)
1906             continue;
1907         data = chip->playback_substream[i]->runtime->private_data;
1908         if (data) {
1909             snd_iprintf(buffer,
1910                     "stream: %d mixer: %d source: %d (%d,%d)\n",
1911                     data->id, data->mixer, data->source,
1912                     data->intdec[0], data->intdec[1]);
1913             if (!(getsamplerate(cif, data->intdec, &rate)))
1914                 snd_iprintf(buffer, "rate: %d\n", rate);
1915         }
1916     }
1917     if (chip->capture_substream && chip->capture_substream->runtime) {
1918         data = chip->capture_substream->runtime->private_data;
1919         if (data) {
1920             snd_iprintf(buffer,
1921                     "stream: %d mixer: %d source: %d (%d,%d)\n",
1922                     data->id, data->mixer,
1923                     data->source, data->intdec[0], data->intdec[1]);
1924             if (!(getsamplerate(cif, data->intdec, &rate)))
1925                 snd_iprintf(buffer, "rate: %d\n", rate);
1926         }
1927     }
1928     snd_iprintf(buffer, "Paths:\n");
1929     i = getpaths(cif, p);
1930     while (i >= 2) {
1931         i -= 2;
1932         snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
1933     }
1934     snd_iprintf(buffer, "\n");
1935 }
1936 
1937 static void snd_riptide_proc_init(struct snd_riptide *chip)
1938 {
1939     snd_card_ro_proc_new(chip->card, "riptide", chip,
1940                  snd_riptide_proc_read);
1941 }
1942 
1943 static int snd_riptide_mixer(struct snd_riptide *chip)
1944 {
1945     struct snd_ac97_bus *pbus;
1946     struct snd_ac97_template ac97;
1947     int err = 0;
1948     static const struct snd_ac97_bus_ops ops = {
1949         .write = snd_riptide_codec_write,
1950         .read = snd_riptide_codec_read,
1951     };
1952 
1953     memset(&ac97, 0, sizeof(ac97));
1954     ac97.private_data = chip;
1955     ac97.scaps = AC97_SCAP_SKIP_MODEM;
1956 
1957     err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus);
1958     if (err < 0)
1959         return err;
1960 
1961     chip->ac97_bus = pbus;
1962     ac97.pci = chip->pci;
1963     err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1964     if (err < 0)
1965         return err;
1966     return err;
1967 }
1968 
1969 #ifdef SUPPORT_JOYSTICK
1970 
1971 static int
1972 snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
1973 {
1974     static int dev;
1975     struct gameport *gameport;
1976     int ret;
1977 
1978     if (dev >= SNDRV_CARDS)
1979         return -ENODEV;
1980 
1981     if (!enable[dev]) {
1982         ret = -ENOENT;
1983         goto inc_dev;
1984     }
1985 
1986     if (!joystick_port[dev]) {
1987         ret = 0;
1988         goto inc_dev;
1989     }
1990 
1991     gameport = gameport_allocate_port();
1992     if (!gameport) {
1993         ret = -ENOMEM;
1994         goto inc_dev;
1995     }
1996     if (!request_region(joystick_port[dev], 8, "Riptide gameport")) {
1997         snd_printk(KERN_WARNING
1998                "Riptide: cannot grab gameport 0x%x\n",
1999                joystick_port[dev]);
2000         gameport_free_port(gameport);
2001         ret = -EBUSY;
2002         goto inc_dev;
2003     }
2004 
2005     gameport->io = joystick_port[dev];
2006     gameport_register_port(gameport);
2007     pci_set_drvdata(pci, gameport);
2008 
2009     ret = 0;
2010 inc_dev:
2011     dev++;
2012     return ret;
2013 }
2014 
2015 static void snd_riptide_joystick_remove(struct pci_dev *pci)
2016 {
2017     struct gameport *gameport = pci_get_drvdata(pci);
2018     if (gameport) {
2019         release_region(gameport->io, 8);
2020         gameport_unregister_port(gameport);
2021     }
2022 }
2023 #endif
2024 
2025 static int
2026 __snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2027 {
2028     static int dev;
2029     struct snd_card *card;
2030     struct snd_riptide *chip;
2031     unsigned short val;
2032     int err;
2033 
2034     if (dev >= SNDRV_CARDS)
2035         return -ENODEV;
2036     if (!enable[dev]) {
2037         dev++;
2038         return -ENOENT;
2039     }
2040 
2041     err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2042                 sizeof(*chip), &card);
2043     if (err < 0)
2044         return err;
2045     chip = card->private_data;
2046     err = snd_riptide_create(card, pci);
2047     if (err < 0)
2048         return err;
2049     err = snd_riptide_pcm(chip, 0);
2050     if (err < 0)
2051         return err;
2052     err = snd_riptide_mixer(chip);
2053     if (err < 0)
2054         return err;
2055 
2056     val = LEGACY_ENABLE_ALL;
2057     if (opl3_port[dev])
2058         val |= LEGACY_ENABLE_FM;
2059 #ifdef SUPPORT_JOYSTICK
2060     if (joystick_port[dev])
2061         val |= LEGACY_ENABLE_GAMEPORT;
2062 #endif
2063     if (mpu_port[dev])
2064         val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
2065     val |= (chip->irq << 4) & 0xf0;
2066     pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
2067     if (mpu_port[dev]) {
2068         val = mpu_port[dev];
2069         pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
2070         err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
2071                       val, MPU401_INFO_IRQ_HOOK, -1,
2072                       &chip->rmidi);
2073         if (err < 0)
2074             snd_printk(KERN_WARNING
2075                    "Riptide: Can't Allocate MPU at 0x%x\n",
2076                    val);
2077         else
2078             chip->mpuaddr = val;
2079     }
2080     if (opl3_port[dev]) {
2081         val = opl3_port[dev];
2082         pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
2083         err = snd_opl3_create(card, val, val + 2,
2084                       OPL3_HW_RIPTIDE, 0, &chip->opl3);
2085         if (err < 0)
2086             snd_printk(KERN_WARNING
2087                    "Riptide: Can't Allocate OPL3 at 0x%x\n",
2088                    val);
2089         else {
2090             chip->opladdr = val;
2091             err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
2092             if (err < 0)
2093                 snd_printk(KERN_WARNING
2094                        "Riptide: Can't Allocate OPL3-HWDEP\n");
2095         }
2096     }
2097 #ifdef SUPPORT_JOYSTICK
2098     if (joystick_port[dev]) {
2099         val = joystick_port[dev];
2100         pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
2101         chip->gameaddr = val;
2102     }
2103 #endif
2104 
2105     strcpy(card->driver, "RIPTIDE");
2106     strcpy(card->shortname, "Riptide");
2107 #ifdef SUPPORT_JOYSTICK
2108     snprintf(card->longname, sizeof(card->longname),
2109          "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
2110          card->shortname, chip->port, chip->irq, chip->mpuaddr,
2111          chip->opladdr, chip->gameaddr);
2112 #else
2113     snprintf(card->longname, sizeof(card->longname),
2114          "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
2115          card->shortname, chip->port, chip->irq, chip->mpuaddr,
2116          chip->opladdr);
2117 #endif
2118     snd_riptide_proc_init(chip);
2119     err = snd_card_register(card);
2120     if (err < 0)
2121         return err;
2122     pci_set_drvdata(pci, card);
2123     dev++;
2124     return 0;
2125 }
2126 
2127 static int
2128 snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2129 {
2130     return snd_card_free_on_error(&pci->dev, __snd_card_riptide_probe(pci, pci_id));
2131 }
2132 
2133 static struct pci_driver driver = {
2134     .name = KBUILD_MODNAME,
2135     .id_table = snd_riptide_ids,
2136     .probe = snd_card_riptide_probe,
2137     .driver = {
2138         .pm = RIPTIDE_PM_OPS,
2139     },
2140 };
2141 
2142 #ifdef SUPPORT_JOYSTICK
2143 static struct pci_driver joystick_driver = {
2144     .name = KBUILD_MODNAME "-joystick",
2145     .id_table = snd_riptide_joystick_ids,
2146     .probe = snd_riptide_joystick_probe,
2147     .remove = snd_riptide_joystick_remove,
2148 };
2149 #endif
2150 
2151 static int __init alsa_card_riptide_init(void)
2152 {
2153     int err;
2154     err = pci_register_driver(&driver);
2155     if (err < 0)
2156         return err;
2157 #if defined(SUPPORT_JOYSTICK)
2158     err = pci_register_driver(&joystick_driver);
2159     /* On failure unregister formerly registered audio driver */
2160     if (err < 0)
2161         pci_unregister_driver(&driver);
2162 #endif
2163     return err;
2164 }
2165 
2166 static void __exit alsa_card_riptide_exit(void)
2167 {
2168     pci_unregister_driver(&driver);
2169 #if defined(SUPPORT_JOYSTICK)
2170     pci_unregister_driver(&joystick_driver);
2171 #endif
2172 }
2173 
2174 module_init(alsa_card_riptide_init);
2175 module_exit(alsa_card_riptide_exit);