riptide.c 63 KB

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