riptide.c 64 KB

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