riptide.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  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 device *dev)
  1042. {
  1043. struct pci_dev *pci = to_pci_dev(dev);
  1044. struct snd_card *card = dev_get_drvdata(dev);
  1045. struct snd_riptide *chip = card->private_data;
  1046. chip->in_suspend = 1;
  1047. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1048. snd_pcm_suspend_all(chip->pcm);
  1049. snd_ac97_suspend(chip->ac97);
  1050. pci_disable_device(pci);
  1051. pci_save_state(pci);
  1052. pci_set_power_state(pci, PCI_D3hot);
  1053. return 0;
  1054. }
  1055. static int riptide_resume(struct device *dev)
  1056. {
  1057. struct pci_dev *pci = to_pci_dev(dev);
  1058. struct snd_card *card = dev_get_drvdata(dev);
  1059. struct snd_riptide *chip = card->private_data;
  1060. pci_set_power_state(pci, PCI_D0);
  1061. pci_restore_state(pci);
  1062. if (pci_enable_device(pci) < 0) {
  1063. printk(KERN_ERR "riptide: pci_enable_device failed, "
  1064. "disabling device\n");
  1065. snd_card_disconnect(card);
  1066. return -EIO;
  1067. }
  1068. pci_set_master(pci);
  1069. snd_riptide_initialize(chip);
  1070. snd_ac97_resume(chip->ac97);
  1071. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1072. chip->in_suspend = 0;
  1073. return 0;
  1074. }
  1075. static SIMPLE_DEV_PM_OPS(riptide_pm, riptide_suspend, riptide_resume);
  1076. #define RIPTIDE_PM_OPS &riptide_pm
  1077. #else
  1078. #define RIPTIDE_PM_OPS NULL
  1079. #endif /* CONFIG_PM */
  1080. static int try_to_load_firmware(struct cmdif *cif, struct snd_riptide *chip)
  1081. {
  1082. union firmware_version firmware = { .ret = CMDRET_ZERO };
  1083. int i, timeout, err;
  1084. for (i = 0; i < 2; i++) {
  1085. WRITE_PORT_ULONG(cif->hwport->port[i].data1, 0);
  1086. WRITE_PORT_ULONG(cif->hwport->port[i].data2, 0);
  1087. }
  1088. SET_GRESET(cif->hwport);
  1089. udelay(100);
  1090. UNSET_GRESET(cif->hwport);
  1091. udelay(100);
  1092. for (timeout = 100000; --timeout; udelay(10)) {
  1093. if (IS_READY(cif->hwport) && !IS_GERR(cif->hwport))
  1094. break;
  1095. }
  1096. if (!timeout) {
  1097. snd_printk(KERN_ERR
  1098. "Riptide: device not ready, audio status: 0x%x "
  1099. "ready: %d gerr: %d\n",
  1100. READ_AUDIO_STATUS(cif->hwport),
  1101. IS_READY(cif->hwport), IS_GERR(cif->hwport));
  1102. return -EIO;
  1103. } else {
  1104. snd_printdd
  1105. ("Riptide: audio status: 0x%x ready: %d gerr: %d\n",
  1106. READ_AUDIO_STATUS(cif->hwport),
  1107. IS_READY(cif->hwport), IS_GERR(cif->hwport));
  1108. }
  1109. SEND_GETV(cif, &firmware.ret);
  1110. snd_printdd("Firmware version: ASIC: %d CODEC %d AUXDSP %d PROG %d\n",
  1111. firmware.firmware.ASIC, firmware.firmware.CODEC,
  1112. firmware.firmware.AUXDSP, firmware.firmware.PROG);
  1113. if (!chip)
  1114. return 1;
  1115. for (i = 0; i < FIRMWARE_VERSIONS; i++) {
  1116. if (!memcmp(&firmware_versions[i], &firmware, sizeof(firmware)))
  1117. return 1; /* OK */
  1118. }
  1119. snd_printdd("Writing Firmware\n");
  1120. if (!chip->fw_entry) {
  1121. err = request_firmware(&chip->fw_entry, "riptide.hex",
  1122. &chip->pci->dev);
  1123. if (err) {
  1124. snd_printk(KERN_ERR
  1125. "Riptide: Firmware not available %d\n", err);
  1126. return -EIO;
  1127. }
  1128. }
  1129. err = loadfirmware(cif, chip->fw_entry->data, chip->fw_entry->size);
  1130. if (err) {
  1131. snd_printk(KERN_ERR
  1132. "Riptide: Could not load firmware %d\n", err);
  1133. return err;
  1134. }
  1135. chip->firmware = firmware;
  1136. return 1; /* OK */
  1137. }
  1138. static int riptide_reset(struct cmdif *cif, struct snd_riptide *chip)
  1139. {
  1140. union cmdret rptr = CMDRET_ZERO;
  1141. int err, tries;
  1142. if (!cif)
  1143. return -EINVAL;
  1144. cif->cmdcnt = 0;
  1145. cif->cmdtime = 0;
  1146. cif->cmdtimemax = 0;
  1147. cif->cmdtimemin = 0xffffffff;
  1148. cif->errcnt = 0;
  1149. cif->is_reset = 0;
  1150. tries = RESET_TRIES;
  1151. do {
  1152. err = try_to_load_firmware(cif, chip);
  1153. if (err < 0)
  1154. return err;
  1155. } while (!err && --tries);
  1156. SEND_SACR(cif, 0, AC97_RESET);
  1157. SEND_RACR(cif, AC97_RESET, &rptr);
  1158. snd_printdd("AC97: 0x%x 0x%x\n", rptr.retlongs[0], rptr.retlongs[1]);
  1159. SEND_PLST(cif, 0);
  1160. SEND_SLST(cif, 0);
  1161. SEND_DLST(cif, 0);
  1162. SEND_ALST(cif, 0);
  1163. SEND_KDMA(cif);
  1164. writearm(cif, 0x301F8, 1, 1);
  1165. writearm(cif, 0x301F4, 1, 1);
  1166. SEND_LSEL(cif, MODEM_CMD, 0, 0, MODEM_INTDEC, MODEM_MERGER,
  1167. MODEM_SPLITTER, MODEM_MIXER);
  1168. setmixer(cif, MODEM_MIXER, 0x7fff, 0x7fff);
  1169. alloclbuspath(cif, ARM2LBUS_FIFO13, lbus_play_modem, NULL, NULL);
  1170. SEND_LSEL(cif, FM_CMD, 0, 0, FM_INTDEC, FM_MERGER, FM_SPLITTER,
  1171. FM_MIXER);
  1172. setmixer(cif, FM_MIXER, 0x7fff, 0x7fff);
  1173. writearm(cif, 0x30648 + FM_MIXER * 4, 0x01, 0x00000005);
  1174. writearm(cif, 0x301A8, 0x02, 0x00000002);
  1175. writearm(cif, 0x30264, 0x08, 0xffffffff);
  1176. alloclbuspath(cif, OPL3_SAMPLE, lbus_play_opl3, NULL, NULL);
  1177. SEND_SSRC(cif, I2S_INTDEC, 48000,
  1178. ((u32) I2S_RATE * 65536) / 48000,
  1179. ((u32) I2S_RATE * 65536) % 48000);
  1180. SEND_LSEL(cif, I2S_CMD0, 0, 0, I2S_INTDEC, I2S_MERGER, I2S_SPLITTER,
  1181. I2S_MIXER);
  1182. SEND_SI2S(cif, 1);
  1183. alloclbuspath(cif, ARM2LBUS_FIFO0, lbus_play_i2s, NULL, NULL);
  1184. alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_out, NULL, NULL);
  1185. alloclbuspath(cif, DIGITAL_MIXER_OUT0, lbus_play_outhp, NULL, NULL);
  1186. SET_AIACK(cif->hwport);
  1187. SET_AIE(cif->hwport);
  1188. SET_AIACK(cif->hwport);
  1189. cif->is_reset = 1;
  1190. return 0;
  1191. }
  1192. static struct snd_pcm_hardware snd_riptide_playback = {
  1193. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1194. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1195. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
  1196. .formats =
  1197. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
  1198. | SNDRV_PCM_FMTBIT_U16_LE,
  1199. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1200. .rate_min = 5500,
  1201. .rate_max = 48000,
  1202. .channels_min = 1,
  1203. .channels_max = 2,
  1204. .buffer_bytes_max = (64 * 1024),
  1205. .period_bytes_min = PAGE_SIZE >> 1,
  1206. .period_bytes_max = PAGE_SIZE << 8,
  1207. .periods_min = 2,
  1208. .periods_max = 64,
  1209. .fifo_size = 0,
  1210. };
  1211. static struct snd_pcm_hardware snd_riptide_capture = {
  1212. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1213. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1214. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
  1215. .formats =
  1216. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8
  1217. | SNDRV_PCM_FMTBIT_U16_LE,
  1218. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1219. .rate_min = 5500,
  1220. .rate_max = 48000,
  1221. .channels_min = 1,
  1222. .channels_max = 2,
  1223. .buffer_bytes_max = (64 * 1024),
  1224. .period_bytes_min = PAGE_SIZE >> 1,
  1225. .period_bytes_max = PAGE_SIZE << 3,
  1226. .periods_min = 2,
  1227. .periods_max = 64,
  1228. .fifo_size = 0,
  1229. };
  1230. static snd_pcm_uframes_t snd_riptide_pointer(struct snd_pcm_substream
  1231. *substream)
  1232. {
  1233. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1234. struct snd_pcm_runtime *runtime = substream->runtime;
  1235. struct pcmhw *data = get_pcmhwdev(substream);
  1236. struct cmdif *cif = chip->cif;
  1237. union cmdret rptr = CMDRET_ZERO;
  1238. snd_pcm_uframes_t ret;
  1239. SEND_GPOS(cif, 0, data->id, &rptr);
  1240. if (data->size && runtime->period_size) {
  1241. snd_printdd
  1242. ("pointer stream %d position 0x%x(0x%x in buffer) bytes 0x%lx(0x%lx in period) frames\n",
  1243. data->id, rptr.retlongs[1], rptr.retlongs[1] % data->size,
  1244. bytes_to_frames(runtime, rptr.retlongs[1]),
  1245. bytes_to_frames(runtime,
  1246. rptr.retlongs[1]) % runtime->period_size);
  1247. if (rptr.retlongs[1] > data->pointer)
  1248. ret =
  1249. bytes_to_frames(runtime,
  1250. rptr.retlongs[1] % data->size);
  1251. else
  1252. ret =
  1253. bytes_to_frames(runtime,
  1254. data->pointer % data->size);
  1255. } else {
  1256. snd_printdd("stream not started or strange parms (%d %ld)\n",
  1257. data->size, runtime->period_size);
  1258. ret = bytes_to_frames(runtime, 0);
  1259. }
  1260. return ret;
  1261. }
  1262. static int snd_riptide_trigger(struct snd_pcm_substream *substream, int cmd)
  1263. {
  1264. int i, j;
  1265. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1266. struct pcmhw *data = get_pcmhwdev(substream);
  1267. struct cmdif *cif = chip->cif;
  1268. union cmdret rptr = CMDRET_ZERO;
  1269. spin_lock(&chip->lock);
  1270. switch (cmd) {
  1271. case SNDRV_PCM_TRIGGER_START:
  1272. case SNDRV_PCM_TRIGGER_RESUME:
  1273. if (!(data->state & ST_PLAY)) {
  1274. SEND_SSTR(cif, data->id, data->sgdlist.addr);
  1275. SET_AIE(cif->hwport);
  1276. data->state = ST_PLAY;
  1277. if (data->mixer != 0xff)
  1278. setmixer(cif, data->mixer, 0x7fff, 0x7fff);
  1279. chip->openstreams++;
  1280. data->oldpos = 0;
  1281. data->pointer = 0;
  1282. }
  1283. break;
  1284. case SNDRV_PCM_TRIGGER_STOP:
  1285. case SNDRV_PCM_TRIGGER_SUSPEND:
  1286. if (data->mixer != 0xff)
  1287. setmixer(cif, data->mixer, 0, 0);
  1288. setmixer(cif, data->mixer, 0, 0);
  1289. SEND_KSTR(cif, data->id);
  1290. data->state = ST_STOP;
  1291. chip->openstreams--;
  1292. j = 0;
  1293. do {
  1294. i = rptr.retlongs[1];
  1295. SEND_GPOS(cif, 0, data->id, &rptr);
  1296. udelay(1);
  1297. } while (i != rptr.retlongs[1] && j++ < MAX_WRITE_RETRY);
  1298. if (j > MAX_WRITE_RETRY)
  1299. snd_printk(KERN_ERR "Riptide: Could not stop stream!");
  1300. break;
  1301. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1302. if (!(data->state & ST_PAUSE)) {
  1303. SEND_PSTR(cif, data->id);
  1304. data->state |= ST_PAUSE;
  1305. chip->openstreams--;
  1306. }
  1307. break;
  1308. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1309. if (data->state & ST_PAUSE) {
  1310. SEND_SSTR(cif, data->id, data->sgdlist.addr);
  1311. data->state &= ~ST_PAUSE;
  1312. chip->openstreams++;
  1313. }
  1314. break;
  1315. default:
  1316. spin_unlock(&chip->lock);
  1317. return -EINVAL;
  1318. }
  1319. spin_unlock(&chip->lock);
  1320. return 0;
  1321. }
  1322. static int snd_riptide_prepare(struct snd_pcm_substream *substream)
  1323. {
  1324. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1325. struct snd_pcm_runtime *runtime = substream->runtime;
  1326. struct pcmhw *data = get_pcmhwdev(substream);
  1327. struct cmdif *cif = chip->cif;
  1328. unsigned char *lbuspath = NULL;
  1329. unsigned int rate, channels;
  1330. int err = 0;
  1331. snd_pcm_format_t format;
  1332. if (snd_BUG_ON(!cif || !data))
  1333. return -EINVAL;
  1334. snd_printdd("prepare id %d ch: %d f:0x%x r:%d\n", data->id,
  1335. runtime->channels, runtime->format, runtime->rate);
  1336. spin_lock_irq(&chip->lock);
  1337. channels = runtime->channels;
  1338. format = runtime->format;
  1339. rate = runtime->rate;
  1340. switch (channels) {
  1341. case 1:
  1342. if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
  1343. lbuspath = data->paths.noconv;
  1344. else
  1345. lbuspath = data->paths.mono;
  1346. break;
  1347. case 2:
  1348. if (rate == 48000 && format == SNDRV_PCM_FORMAT_S16_LE)
  1349. lbuspath = data->paths.noconv;
  1350. else
  1351. lbuspath = data->paths.stereo;
  1352. break;
  1353. }
  1354. snd_printdd("use sgdlist at 0x%p\n",
  1355. data->sgdlist.area);
  1356. if (data->sgdlist.area) {
  1357. unsigned int i, j, size, pages, f, pt, period;
  1358. struct sgd *c, *p = NULL;
  1359. size = frames_to_bytes(runtime, runtime->buffer_size);
  1360. period = frames_to_bytes(runtime, runtime->period_size);
  1361. f = PAGE_SIZE;
  1362. while ((size + (f >> 1) - 1) <= (f << 7) && (f << 1) > period)
  1363. f = f >> 1;
  1364. pages = (size + f - 1) / f;
  1365. data->size = size;
  1366. data->pages = pages;
  1367. snd_printdd
  1368. ("create sgd size: 0x%x pages %d of size 0x%x for period 0x%x\n",
  1369. size, pages, f, period);
  1370. pt = 0;
  1371. j = 0;
  1372. for (i = 0; i < pages; i++) {
  1373. unsigned int ofs, addr;
  1374. c = &data->sgdbuf[i];
  1375. if (p)
  1376. p->dwNextLink = cpu_to_le32(data->sgdlist.addr +
  1377. (i *
  1378. sizeof(struct
  1379. sgd)));
  1380. c->dwNextLink = cpu_to_le32(data->sgdlist.addr);
  1381. ofs = j << PAGE_SHIFT;
  1382. addr = snd_pcm_sgbuf_get_addr(substream, ofs) + pt;
  1383. c->dwSegPtrPhys = cpu_to_le32(addr);
  1384. pt = (pt + f) % PAGE_SIZE;
  1385. if (pt == 0)
  1386. j++;
  1387. c->dwSegLen = cpu_to_le32(f);
  1388. c->dwStat_Ctl =
  1389. cpu_to_le32(IEOB_ENABLE | IEOS_ENABLE |
  1390. IEOC_ENABLE);
  1391. p = c;
  1392. size -= f;
  1393. }
  1394. data->sgdbuf[i].dwSegLen = cpu_to_le32(size);
  1395. }
  1396. if (lbuspath && lbuspath != data->lbuspath) {
  1397. if (data->lbuspath)
  1398. freelbuspath(cif, data->source, data->lbuspath);
  1399. alloclbuspath(cif, data->source, lbuspath,
  1400. &data->mixer, data->intdec);
  1401. data->lbuspath = lbuspath;
  1402. data->rate = 0;
  1403. }
  1404. if (data->rate != rate || data->format != format ||
  1405. data->channels != channels) {
  1406. data->rate = rate;
  1407. data->format = format;
  1408. data->channels = channels;
  1409. if (setsampleformat
  1410. (cif, data->mixer, data->id, channels, format)
  1411. || setsamplerate(cif, data->intdec, rate))
  1412. err = -EIO;
  1413. }
  1414. spin_unlock_irq(&chip->lock);
  1415. return err;
  1416. }
  1417. static int
  1418. snd_riptide_hw_params(struct snd_pcm_substream *substream,
  1419. struct snd_pcm_hw_params *hw_params)
  1420. {
  1421. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1422. struct pcmhw *data = get_pcmhwdev(substream);
  1423. struct snd_dma_buffer *sgdlist = &data->sgdlist;
  1424. int err;
  1425. snd_printdd("hw params id %d (sgdlist: 0x%p 0x%lx %d)\n", data->id,
  1426. sgdlist->area, (unsigned long)sgdlist->addr,
  1427. (int)sgdlist->bytes);
  1428. if (sgdlist->area)
  1429. snd_dma_free_pages(sgdlist);
  1430. if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  1431. snd_dma_pci_data(chip->pci),
  1432. sizeof(struct sgd) * (DESC_MAX_MASK + 1),
  1433. sgdlist)) < 0) {
  1434. snd_printk(KERN_ERR "Riptide: failed to alloc %d dma bytes\n",
  1435. (int)sizeof(struct sgd) * (DESC_MAX_MASK + 1));
  1436. return err;
  1437. }
  1438. data->sgdbuf = (struct sgd *)sgdlist->area;
  1439. return snd_pcm_lib_malloc_pages(substream,
  1440. params_buffer_bytes(hw_params));
  1441. }
  1442. static int snd_riptide_hw_free(struct snd_pcm_substream *substream)
  1443. {
  1444. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1445. struct pcmhw *data = get_pcmhwdev(substream);
  1446. struct cmdif *cif = chip->cif;
  1447. if (cif && data) {
  1448. if (data->lbuspath)
  1449. freelbuspath(cif, data->source, data->lbuspath);
  1450. data->lbuspath = NULL;
  1451. data->source = 0xff;
  1452. data->intdec[0] = 0xff;
  1453. data->intdec[1] = 0xff;
  1454. if (data->sgdlist.area) {
  1455. snd_dma_free_pages(&data->sgdlist);
  1456. data->sgdlist.area = NULL;
  1457. }
  1458. }
  1459. return snd_pcm_lib_free_pages(substream);
  1460. }
  1461. static int snd_riptide_playback_open(struct snd_pcm_substream *substream)
  1462. {
  1463. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1464. struct snd_pcm_runtime *runtime = substream->runtime;
  1465. struct pcmhw *data;
  1466. int sub_num = substream->number;
  1467. chip->playback_substream[sub_num] = substream;
  1468. runtime->hw = snd_riptide_playback;
  1469. data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
  1470. if (data == NULL)
  1471. return -ENOMEM;
  1472. data->paths = lbus_play_paths[sub_num];
  1473. data->id = play_ids[sub_num];
  1474. data->source = play_sources[sub_num];
  1475. data->intdec[0] = 0xff;
  1476. data->intdec[1] = 0xff;
  1477. data->state = ST_STOP;
  1478. runtime->private_data = data;
  1479. return snd_pcm_hw_constraint_integer(runtime,
  1480. SNDRV_PCM_HW_PARAM_PERIODS);
  1481. }
  1482. static int snd_riptide_capture_open(struct snd_pcm_substream *substream)
  1483. {
  1484. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1485. struct snd_pcm_runtime *runtime = substream->runtime;
  1486. struct pcmhw *data;
  1487. chip->capture_substream = substream;
  1488. runtime->hw = snd_riptide_capture;
  1489. data = kzalloc(sizeof(struct pcmhw), GFP_KERNEL);
  1490. if (data == NULL)
  1491. return -ENOMEM;
  1492. data->paths = lbus_rec_path;
  1493. data->id = PADC;
  1494. data->source = ACLNK2PADC;
  1495. data->intdec[0] = 0xff;
  1496. data->intdec[1] = 0xff;
  1497. data->state = ST_STOP;
  1498. runtime->private_data = data;
  1499. return snd_pcm_hw_constraint_integer(runtime,
  1500. SNDRV_PCM_HW_PARAM_PERIODS);
  1501. }
  1502. static int snd_riptide_playback_close(struct snd_pcm_substream *substream)
  1503. {
  1504. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1505. struct pcmhw *data = get_pcmhwdev(substream);
  1506. int sub_num = substream->number;
  1507. substream->runtime->private_data = NULL;
  1508. chip->playback_substream[sub_num] = NULL;
  1509. kfree(data);
  1510. return 0;
  1511. }
  1512. static int snd_riptide_capture_close(struct snd_pcm_substream *substream)
  1513. {
  1514. struct snd_riptide *chip = snd_pcm_substream_chip(substream);
  1515. struct pcmhw *data = get_pcmhwdev(substream);
  1516. substream->runtime->private_data = NULL;
  1517. chip->capture_substream = NULL;
  1518. kfree(data);
  1519. return 0;
  1520. }
  1521. static struct snd_pcm_ops snd_riptide_playback_ops = {
  1522. .open = snd_riptide_playback_open,
  1523. .close = snd_riptide_playback_close,
  1524. .ioctl = snd_pcm_lib_ioctl,
  1525. .hw_params = snd_riptide_hw_params,
  1526. .hw_free = snd_riptide_hw_free,
  1527. .prepare = snd_riptide_prepare,
  1528. .page = snd_pcm_sgbuf_ops_page,
  1529. .trigger = snd_riptide_trigger,
  1530. .pointer = snd_riptide_pointer,
  1531. };
  1532. static struct snd_pcm_ops snd_riptide_capture_ops = {
  1533. .open = snd_riptide_capture_open,
  1534. .close = snd_riptide_capture_close,
  1535. .ioctl = snd_pcm_lib_ioctl,
  1536. .hw_params = snd_riptide_hw_params,
  1537. .hw_free = snd_riptide_hw_free,
  1538. .prepare = snd_riptide_prepare,
  1539. .page = snd_pcm_sgbuf_ops_page,
  1540. .trigger = snd_riptide_trigger,
  1541. .pointer = snd_riptide_pointer,
  1542. };
  1543. static int __devinit
  1544. snd_riptide_pcm(struct snd_riptide *chip, int device, struct snd_pcm **rpcm)
  1545. {
  1546. struct snd_pcm *pcm;
  1547. int err;
  1548. if (rpcm)
  1549. *rpcm = NULL;
  1550. if ((err =
  1551. snd_pcm_new(chip->card, "RIPTIDE", device, PLAYBACK_SUBSTREAMS, 1,
  1552. &pcm)) < 0)
  1553. return err;
  1554. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1555. &snd_riptide_playback_ops);
  1556. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1557. &snd_riptide_capture_ops);
  1558. pcm->private_data = chip;
  1559. pcm->info_flags = 0;
  1560. strcpy(pcm->name, "RIPTIDE");
  1561. chip->pcm = pcm;
  1562. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  1563. snd_dma_pci_data(chip->pci),
  1564. 64 * 1024, 128 * 1024);
  1565. if (rpcm)
  1566. *rpcm = pcm;
  1567. return 0;
  1568. }
  1569. static irqreturn_t
  1570. snd_riptide_interrupt(int irq, void *dev_id)
  1571. {
  1572. struct snd_riptide *chip = dev_id;
  1573. struct cmdif *cif = chip->cif;
  1574. if (cif) {
  1575. chip->received_irqs++;
  1576. if (IS_EOBIRQ(cif->hwport) || IS_EOSIRQ(cif->hwport) ||
  1577. IS_EOCIRQ(cif->hwport)) {
  1578. chip->handled_irqs++;
  1579. tasklet_schedule(&chip->riptide_tq);
  1580. }
  1581. if (chip->rmidi && IS_MPUIRQ(cif->hwport)) {
  1582. chip->handled_irqs++;
  1583. snd_mpu401_uart_interrupt(irq,
  1584. chip->rmidi->private_data);
  1585. }
  1586. SET_AIACK(cif->hwport);
  1587. }
  1588. return IRQ_HANDLED;
  1589. }
  1590. static void
  1591. snd_riptide_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  1592. unsigned short val)
  1593. {
  1594. struct snd_riptide *chip = ac97->private_data;
  1595. struct cmdif *cif = chip->cif;
  1596. union cmdret rptr = CMDRET_ZERO;
  1597. int i = 0;
  1598. if (snd_BUG_ON(!cif))
  1599. return;
  1600. snd_printdd("Write AC97 reg 0x%x 0x%x\n", reg, val);
  1601. do {
  1602. SEND_SACR(cif, val, reg);
  1603. SEND_RACR(cif, reg, &rptr);
  1604. } while (rptr.retwords[1] != val && i++ < MAX_WRITE_RETRY);
  1605. if (i > MAX_WRITE_RETRY)
  1606. snd_printdd("Write AC97 reg failed\n");
  1607. }
  1608. static unsigned short snd_riptide_codec_read(struct snd_ac97 *ac97,
  1609. unsigned short reg)
  1610. {
  1611. struct snd_riptide *chip = ac97->private_data;
  1612. struct cmdif *cif = chip->cif;
  1613. union cmdret rptr = CMDRET_ZERO;
  1614. if (snd_BUG_ON(!cif))
  1615. return 0;
  1616. if (SEND_RACR(cif, reg, &rptr) != 0)
  1617. SEND_RACR(cif, reg, &rptr);
  1618. snd_printdd("Read AC97 reg 0x%x got 0x%x\n", reg, rptr.retwords[1]);
  1619. return rptr.retwords[1];
  1620. }
  1621. static int snd_riptide_initialize(struct snd_riptide *chip)
  1622. {
  1623. struct cmdif *cif;
  1624. unsigned int device_id;
  1625. int err;
  1626. if (snd_BUG_ON(!chip))
  1627. return -EINVAL;
  1628. cif = chip->cif;
  1629. if (!cif) {
  1630. if ((cif = kzalloc(sizeof(struct cmdif), GFP_KERNEL)) == NULL)
  1631. return -ENOMEM;
  1632. cif->hwport = (struct riptideport *)chip->port;
  1633. spin_lock_init(&cif->lock);
  1634. chip->cif = cif;
  1635. }
  1636. cif->is_reset = 0;
  1637. if ((err = riptide_reset(cif, chip)) != 0)
  1638. return err;
  1639. device_id = chip->device_id;
  1640. switch (device_id) {
  1641. case 0x4310:
  1642. case 0x4320:
  1643. case 0x4330:
  1644. snd_printdd("Modem enable?\n");
  1645. SEND_SETDPLL(cif);
  1646. break;
  1647. }
  1648. snd_printdd("Enabling MPU IRQs\n");
  1649. if (chip->rmidi)
  1650. SET_EMPUIRQ(cif->hwport);
  1651. return err;
  1652. }
  1653. static int snd_riptide_free(struct snd_riptide *chip)
  1654. {
  1655. struct cmdif *cif;
  1656. if (!chip)
  1657. return 0;
  1658. if ((cif = chip->cif)) {
  1659. SET_GRESET(cif->hwport);
  1660. udelay(100);
  1661. UNSET_GRESET(cif->hwport);
  1662. kfree(chip->cif);
  1663. }
  1664. if (chip->irq >= 0)
  1665. free_irq(chip->irq, chip);
  1666. release_firmware(chip->fw_entry);
  1667. release_and_free_resource(chip->res_port);
  1668. kfree(chip);
  1669. return 0;
  1670. }
  1671. static int snd_riptide_dev_free(struct snd_device *device)
  1672. {
  1673. struct snd_riptide *chip = device->device_data;
  1674. return snd_riptide_free(chip);
  1675. }
  1676. static int __devinit
  1677. snd_riptide_create(struct snd_card *card, struct pci_dev *pci,
  1678. struct snd_riptide **rchip)
  1679. {
  1680. struct snd_riptide *chip;
  1681. struct riptideport *hwport;
  1682. int err;
  1683. static struct snd_device_ops ops = {
  1684. .dev_free = snd_riptide_dev_free,
  1685. };
  1686. *rchip = NULL;
  1687. if ((err = pci_enable_device(pci)) < 0)
  1688. return err;
  1689. if (!(chip = kzalloc(sizeof(struct snd_riptide), GFP_KERNEL)))
  1690. return -ENOMEM;
  1691. spin_lock_init(&chip->lock);
  1692. chip->card = card;
  1693. chip->pci = pci;
  1694. chip->irq = -1;
  1695. chip->openstreams = 0;
  1696. chip->port = pci_resource_start(pci, 0);
  1697. chip->received_irqs = 0;
  1698. chip->handled_irqs = 0;
  1699. chip->cif = NULL;
  1700. tasklet_init(&chip->riptide_tq, riptide_handleirq, (unsigned long)chip);
  1701. if ((chip->res_port =
  1702. request_region(chip->port, 64, "RIPTIDE")) == NULL) {
  1703. snd_printk(KERN_ERR
  1704. "Riptide: unable to grab region 0x%lx-0x%lx\n",
  1705. chip->port, chip->port + 64 - 1);
  1706. snd_riptide_free(chip);
  1707. return -EBUSY;
  1708. }
  1709. hwport = (struct riptideport *)chip->port;
  1710. UNSET_AIE(hwport);
  1711. if (request_irq(pci->irq, snd_riptide_interrupt, IRQF_SHARED,
  1712. KBUILD_MODNAME, chip)) {
  1713. snd_printk(KERN_ERR "Riptide: unable to grab IRQ %d\n",
  1714. pci->irq);
  1715. snd_riptide_free(chip);
  1716. return -EBUSY;
  1717. }
  1718. chip->irq = pci->irq;
  1719. chip->device_id = pci->device;
  1720. pci_set_master(pci);
  1721. if ((err = snd_riptide_initialize(chip)) < 0) {
  1722. snd_riptide_free(chip);
  1723. return err;
  1724. }
  1725. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  1726. snd_riptide_free(chip);
  1727. return err;
  1728. }
  1729. snd_card_set_dev(card, &pci->dev);
  1730. *rchip = chip;
  1731. return 0;
  1732. }
  1733. static void
  1734. snd_riptide_proc_read(struct snd_info_entry *entry,
  1735. struct snd_info_buffer *buffer)
  1736. {
  1737. struct snd_riptide *chip = entry->private_data;
  1738. struct pcmhw *data;
  1739. int i;
  1740. struct cmdif *cif = NULL;
  1741. unsigned char p[256];
  1742. unsigned short rval = 0, lval = 0;
  1743. unsigned int rate;
  1744. if (!chip)
  1745. return;
  1746. snd_iprintf(buffer, "%s\n\n", chip->card->longname);
  1747. snd_iprintf(buffer, "Device ID: 0x%x\nReceived IRQs: (%ld)%ld\nPorts:",
  1748. chip->device_id, chip->handled_irqs, chip->received_irqs);
  1749. for (i = 0; i < 64; i += 4)
  1750. snd_iprintf(buffer, "%c%02x: %08x",
  1751. (i % 16) ? ' ' : '\n', i, inl(chip->port + i));
  1752. if ((cif = chip->cif)) {
  1753. snd_iprintf(buffer,
  1754. "\nVersion: ASIC: %d CODEC: %d AUXDSP: %d PROG: %d",
  1755. chip->firmware.firmware.ASIC,
  1756. chip->firmware.firmware.CODEC,
  1757. chip->firmware.firmware.AUXDSP,
  1758. chip->firmware.firmware.PROG);
  1759. snd_iprintf(buffer, "\nDigital mixer:");
  1760. for (i = 0; i < 12; i++) {
  1761. getmixer(cif, i, &rval, &lval);
  1762. snd_iprintf(buffer, "\n %d: %d %d", i, rval, lval);
  1763. }
  1764. snd_iprintf(buffer,
  1765. "\nARM Commands num: %d failed: %d time: %d max: %d min: %d",
  1766. cif->cmdcnt, cif->errcnt,
  1767. cif->cmdtime, cif->cmdtimemax, cif->cmdtimemin);
  1768. }
  1769. snd_iprintf(buffer, "\nOpen streams %d:\n", chip->openstreams);
  1770. for (i = 0; i < PLAYBACK_SUBSTREAMS; i++) {
  1771. if (chip->playback_substream[i]
  1772. && chip->playback_substream[i]->runtime
  1773. && (data =
  1774. chip->playback_substream[i]->runtime->private_data)) {
  1775. snd_iprintf(buffer,
  1776. "stream: %d mixer: %d source: %d (%d,%d)\n",
  1777. data->id, data->mixer, data->source,
  1778. data->intdec[0], data->intdec[1]);
  1779. if (!(getsamplerate(cif, data->intdec, &rate)))
  1780. snd_iprintf(buffer, "rate: %d\n", rate);
  1781. }
  1782. }
  1783. if (chip->capture_substream
  1784. && chip->capture_substream->runtime
  1785. && (data = chip->capture_substream->runtime->private_data)) {
  1786. snd_iprintf(buffer,
  1787. "stream: %d mixer: %d source: %d (%d,%d)\n",
  1788. data->id, data->mixer,
  1789. data->source, data->intdec[0], data->intdec[1]);
  1790. if (!(getsamplerate(cif, data->intdec, &rate)))
  1791. snd_iprintf(buffer, "rate: %d\n", rate);
  1792. }
  1793. snd_iprintf(buffer, "Paths:\n");
  1794. i = getpaths(cif, p);
  1795. while (i >= 2) {
  1796. i -= 2;
  1797. snd_iprintf(buffer, "%x->%x ", p[i], p[i + 1]);
  1798. }
  1799. snd_iprintf(buffer, "\n");
  1800. }
  1801. static void __devinit snd_riptide_proc_init(struct snd_riptide *chip)
  1802. {
  1803. struct snd_info_entry *entry;
  1804. if (!snd_card_proc_new(chip->card, "riptide", &entry))
  1805. snd_info_set_text_ops(entry, chip, snd_riptide_proc_read);
  1806. }
  1807. static int __devinit snd_riptide_mixer(struct snd_riptide *chip)
  1808. {
  1809. struct snd_ac97_bus *pbus;
  1810. struct snd_ac97_template ac97;
  1811. int err = 0;
  1812. static struct snd_ac97_bus_ops ops = {
  1813. .write = snd_riptide_codec_write,
  1814. .read = snd_riptide_codec_read,
  1815. };
  1816. memset(&ac97, 0, sizeof(ac97));
  1817. ac97.private_data = chip;
  1818. ac97.scaps = AC97_SCAP_SKIP_MODEM;
  1819. if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &pbus)) < 0)
  1820. return err;
  1821. chip->ac97_bus = pbus;
  1822. ac97.pci = chip->pci;
  1823. if ((err = snd_ac97_mixer(pbus, &ac97, &chip->ac97)) < 0)
  1824. return err;
  1825. return err;
  1826. }
  1827. #ifdef SUPPORT_JOYSTICK
  1828. static int __devinit
  1829. snd_riptide_joystick_probe(struct pci_dev *pci, const struct pci_device_id *id)
  1830. {
  1831. static int dev;
  1832. struct gameport *gameport;
  1833. if (dev >= SNDRV_CARDS)
  1834. return -ENODEV;
  1835. if (!enable[dev]) {
  1836. dev++;
  1837. return -ENOENT;
  1838. }
  1839. if (!joystick_port[dev++])
  1840. return 0;
  1841. gameport = gameport_allocate_port();
  1842. if (!gameport)
  1843. return -ENOMEM;
  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. return -EBUSY;
  1850. }
  1851. gameport->io = joystick_port[dev];
  1852. gameport_register_port(gameport);
  1853. pci_set_drvdata(pci, gameport);
  1854. return 0;
  1855. }
  1856. static void __devexit snd_riptide_joystick_remove(struct pci_dev *pci)
  1857. {
  1858. struct gameport *gameport = pci_get_drvdata(pci);
  1859. if (gameport) {
  1860. release_region(gameport->io, 8);
  1861. gameport_unregister_port(gameport);
  1862. pci_set_drvdata(pci, NULL);
  1863. }
  1864. }
  1865. #endif
  1866. static int __devinit
  1867. snd_card_riptide_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
  1868. {
  1869. static int dev;
  1870. struct snd_card *card;
  1871. struct snd_riptide *chip;
  1872. unsigned short val;
  1873. int err;
  1874. if (dev >= SNDRV_CARDS)
  1875. return -ENODEV;
  1876. if (!enable[dev]) {
  1877. dev++;
  1878. return -ENOENT;
  1879. }
  1880. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  1881. if (err < 0)
  1882. return err;
  1883. err = snd_riptide_create(card, pci, &chip);
  1884. if (err < 0)
  1885. goto error;
  1886. card->private_data = chip;
  1887. err = snd_riptide_pcm(chip, 0, NULL);
  1888. if (err < 0)
  1889. goto error;
  1890. err = snd_riptide_mixer(chip);
  1891. if (err < 0)
  1892. goto error;
  1893. val = LEGACY_ENABLE_ALL;
  1894. if (opl3_port[dev])
  1895. val |= LEGACY_ENABLE_FM;
  1896. #ifdef SUPPORT_JOYSTICK
  1897. if (joystick_port[dev])
  1898. val |= LEGACY_ENABLE_GAMEPORT;
  1899. #endif
  1900. if (mpu_port[dev])
  1901. val |= LEGACY_ENABLE_MPU_INT | LEGACY_ENABLE_MPU;
  1902. val |= (chip->irq << 4) & 0xf0;
  1903. pci_write_config_word(chip->pci, PCI_EXT_Legacy_Mask, val);
  1904. if (mpu_port[dev]) {
  1905. val = mpu_port[dev];
  1906. pci_write_config_word(chip->pci, PCI_EXT_MPU_Base, val);
  1907. err = snd_mpu401_uart_new(card, 0, MPU401_HW_RIPTIDE,
  1908. val, MPU401_INFO_IRQ_HOOK, -1,
  1909. &chip->rmidi);
  1910. if (err < 0)
  1911. snd_printk(KERN_WARNING
  1912. "Riptide: Can't Allocate MPU at 0x%x\n",
  1913. val);
  1914. else
  1915. chip->mpuaddr = val;
  1916. }
  1917. if (opl3_port[dev]) {
  1918. val = opl3_port[dev];
  1919. pci_write_config_word(chip->pci, PCI_EXT_FM_Base, val);
  1920. err = snd_opl3_create(card, val, val + 2,
  1921. OPL3_HW_RIPTIDE, 0, &chip->opl3);
  1922. if (err < 0)
  1923. snd_printk(KERN_WARNING
  1924. "Riptide: Can't Allocate OPL3 at 0x%x\n",
  1925. val);
  1926. else {
  1927. chip->opladdr = val;
  1928. err = snd_opl3_hwdep_new(chip->opl3, 0, 1, NULL);
  1929. if (err < 0)
  1930. snd_printk(KERN_WARNING
  1931. "Riptide: Can't Allocate OPL3-HWDEP\n");
  1932. }
  1933. }
  1934. #ifdef SUPPORT_JOYSTICK
  1935. if (joystick_port[dev]) {
  1936. val = joystick_port[dev];
  1937. pci_write_config_word(chip->pci, PCI_EXT_Game_Base, val);
  1938. chip->gameaddr = val;
  1939. }
  1940. #endif
  1941. strcpy(card->driver, "RIPTIDE");
  1942. strcpy(card->shortname, "Riptide");
  1943. #ifdef SUPPORT_JOYSTICK
  1944. snprintf(card->longname, sizeof(card->longname),
  1945. "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x gameport 0x%x",
  1946. card->shortname, chip->port, chip->irq, chip->mpuaddr,
  1947. chip->opladdr, chip->gameaddr);
  1948. #else
  1949. snprintf(card->longname, sizeof(card->longname),
  1950. "%s at 0x%lx, irq %i mpu 0x%x opl3 0x%x",
  1951. card->shortname, chip->port, chip->irq, chip->mpuaddr,
  1952. chip->opladdr);
  1953. #endif
  1954. snd_riptide_proc_init(chip);
  1955. err = snd_card_register(card);
  1956. if (err < 0)
  1957. goto error;
  1958. pci_set_drvdata(pci, card);
  1959. dev++;
  1960. return 0;
  1961. error:
  1962. snd_card_free(card);
  1963. return err;
  1964. }
  1965. static void __devexit snd_card_riptide_remove(struct pci_dev *pci)
  1966. {
  1967. snd_card_free(pci_get_drvdata(pci));
  1968. pci_set_drvdata(pci, NULL);
  1969. }
  1970. static struct pci_driver driver = {
  1971. .name = KBUILD_MODNAME,
  1972. .id_table = snd_riptide_ids,
  1973. .probe = snd_card_riptide_probe,
  1974. .remove = __devexit_p(snd_card_riptide_remove),
  1975. .driver = {
  1976. .pm = RIPTIDE_PM_OPS,
  1977. },
  1978. };
  1979. #ifdef SUPPORT_JOYSTICK
  1980. static struct pci_driver joystick_driver = {
  1981. .name = KBUILD_MODNAME "-joystick",
  1982. .id_table = snd_riptide_joystick_ids,
  1983. .probe = snd_riptide_joystick_probe,
  1984. .remove = __devexit_p(snd_riptide_joystick_remove),
  1985. };
  1986. #endif
  1987. static int __init alsa_card_riptide_init(void)
  1988. {
  1989. int err;
  1990. err = pci_register_driver(&driver);
  1991. if (err < 0)
  1992. return err;
  1993. #if defined(SUPPORT_JOYSTICK)
  1994. err = pci_register_driver(&joystick_driver);
  1995. /* On failure unregister formerly registered audio driver */
  1996. if (err < 0)
  1997. pci_unregister_driver(&driver);
  1998. #endif
  1999. return err;
  2000. }
  2001. static void __exit alsa_card_riptide_exit(void)
  2002. {
  2003. pci_unregister_driver(&driver);
  2004. #if defined(SUPPORT_JOYSTICK)
  2005. pci_unregister_driver(&joystick_driver);
  2006. #endif
  2007. }
  2008. module_init(alsa_card_riptide_init);
  2009. module_exit(alsa_card_riptide_exit);