riptide.c 64 KB

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