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