riptide.c 63 KB

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