riptide.c 62 KB

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