riptide.c 64 KB

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